1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
|
#include "bufferobj.h"
#include "enums.h"
#include "nouveau_bufferobj.h"
#include "nouveau_buffers.h"
#include "nouveau_context.h"
#include "nouveau_drm.h"
#include "nouveau_object.h"
#include "nouveau_msg.h"
#define NOUVEAU_MEM_FREE(mem) do { \
nouveau_mem_free(ctx, (mem)); \
(mem) = NULL; \
} while(0)
#define DEBUG(fmt,args...) do { \
if (NOUVEAU_DEBUG & DEBUG_BUFFEROBJ) { \
fprintf(stderr, "%s: "fmt, __func__, ##args); \
} \
} while(0)
static GLboolean
nouveau_bo_download_from_screen(GLcontext *ctx, GLuint offset, GLuint size,
struct gl_buffer_object *bo)
{
nouveauContextPtr nmesa = NOUVEAU_CONTEXT(ctx);
nouveau_buffer_object *nbo = (nouveau_buffer_object *)bo;
nouveau_mem *in_mem;
DEBUG("bo=%p, offset=%d, size=%d\n", bo, offset, size);
/* If there's a permanent backing store, blit directly into it */
if (nbo->cpu_mem) {
if (nbo->cpu_mem != nbo->gpu_mem) {
DEBUG("..cpu_mem\n");
nouveau_memformat_flat_emit(ctx, nbo->cpu_mem,
nbo->gpu_mem,
offset, offset, size);
}
} else {
DEBUG("..sys_mem\n");
in_mem = nouveau_mem_alloc(ctx, NOUVEAU_MEM_AGP, size, 0);
if (in_mem) {
DEBUG("....via AGP\n");
/* otherwise, try blitting to faster memory and
* copying from there
*/
nouveau_memformat_flat_emit(ctx, in_mem, nbo->gpu_mem,
0, offset, size);
nouveau_notifier_wait_nop(ctx, nmesa->syncNotifier,
NvSubMemFormat);
_mesa_memcpy(nbo->cpu_mem_sys + offset,
in_mem->map, size);
NOUVEAU_MEM_FREE(in_mem);
} else {
DEBUG("....direct VRAM copy\n");
/* worst case, copy directly from vram */
_mesa_memcpy(nbo->cpu_mem_sys + offset,
nbo->gpu_mem + offset,
size);
}
}
return GL_TRUE;
}
static GLboolean
nouveau_bo_upload_to_screen(GLcontext *ctx, GLuint offset, GLuint size,
struct gl_buffer_object *bo)
{
nouveauContextPtr nmesa = NOUVEAU_CONTEXT(ctx);
nouveau_buffer_object *nbo = (nouveau_buffer_object *)bo;
nouveau_mem *out_mem;
DEBUG("bo=%p, offset=%d, size=%d\n", bo, offset, size);
if (nbo->cpu_mem) {
if (nbo->cpu_mem != nbo->gpu_mem) {
DEBUG("..cpu_mem\n");
nouveau_memformat_flat_emit(ctx, nbo->gpu_mem,
nbo->cpu_mem,
offset, offset, size);
}
} else {
out_mem = nouveau_mem_alloc(ctx, NOUVEAU_MEM_AGP |
NOUVEAU_MEM_MAPPED,
size, 0);
if (out_mem) {
DEBUG("....via AGP\n");
_mesa_memcpy(out_mem->map,
nbo->cpu_mem_sys + offset, size);
nouveau_memformat_flat_emit(ctx, nbo->gpu_mem, out_mem,
offset, 0, size);
nouveau_notifier_wait_nop(ctx, nmesa->syncNotifier,
NvSubMemFormat);
NOUVEAU_MEM_FREE(out_mem);
} else {
DEBUG("....direct VRAM copy\n");
_mesa_memcpy(nbo->gpu_mem->map + offset,
nbo->cpu_mem_sys + offset,
size);
}
}
return GL_TRUE;
}
GLboolean
nouveau_bo_move_in(GLcontext *ctx, struct gl_buffer_object *bo)
{
nouveau_buffer_object *nbo = (nouveau_buffer_object *)bo;
DEBUG("bo=%p\n", bo);
if (bo->OnCard)
return GL_TRUE;
assert(nbo->gpu_mem_flags);
nbo->gpu_mem = nouveau_mem_alloc(ctx, nbo->gpu_mem_flags |
NOUVEAU_MEM_MAPPED,
bo->Size, 0);
assert(nbo->gpu_mem);
if (nbo->cpu_mem_flags) {
if ((nbo->cpu_mem_flags|NOUVEAU_MEM_MAPPED) != nbo->gpu_mem->type) {
DEBUG("..need cpu_mem buffer\n");
nbo->cpu_mem = nouveau_mem_alloc(ctx,
nbo->cpu_mem_flags |
NOUVEAU_MEM_MAPPED,
bo->Size, 0);
if (nbo->cpu_mem) {
DEBUG("....alloc ok, kill sys_mem buffer\n");
_mesa_memcpy(nbo->cpu_mem->map,
nbo->cpu_mem_sys, bo->Size);
FREE(nbo->cpu_mem_sys);
}
} else {
DEBUG("..cpu direct access to GPU buffer\n");
nbo->cpu_mem = nbo->gpu_mem;
}
}
nouveau_bo_upload_to_screen(ctx, 0, bo->Size, bo);
bo->OnCard = GL_TRUE;
return GL_TRUE;
}
GLboolean
nouveau_bo_move_out(GLcontext *ctx, struct gl_buffer_object *bo)
{
nouveauContextPtr nmesa = NOUVEAU_CONTEXT(ctx);
nouveau_buffer_object *nbo = (nouveau_buffer_object *)bo;
GLuint nr_dirty;
DEBUG("bo=%p\n", bo);
if (!bo->OnCard)
return GL_TRUE;
nr_dirty = nouveau_bo_download_dirty(ctx, bo);
if (nbo->cpu_mem) {
if (nr_dirty && nbo->cpu_mem != nbo->gpu_mem)
nouveau_notifier_wait_nop(ctx, nmesa->syncNotifier,
NvSubMemFormat);
DEBUG("..destroy cpu_mem buffer\n");
nbo->cpu_mem_sys = malloc(bo->Size);
assert(nbo->cpu_mem_sys);
_mesa_memcpy(nbo->cpu_mem_sys, nbo->cpu_mem->map, bo->Size);
if (nbo->cpu_mem == nbo->gpu_mem)
nbo->cpu_mem = NULL;
else
NOUVEAU_MEM_FREE(nbo->cpu_mem);
}
NOUVEAU_MEM_FREE(nbo->gpu_mem);
bo->OnCard = GL_FALSE;
return GL_TRUE;
}
static void
nouveau_bo_choose_storage_method(GLcontext *ctx, GLenum usage,
struct gl_buffer_object *bo)
{
nouveau_buffer_object *nbo = (nouveau_buffer_object *)bo;
GLuint gpu_type = 0;
GLuint cpu_type = 0;
switch (usage) {
/* Client source, changes often, used by GL many times */
case GL_DYNAMIC_DRAW_ARB:
gpu_type = NOUVEAU_MEM_AGP | NOUVEAU_MEM_FB_ACCEPTABLE;
cpu_type = NOUVEAU_MEM_AGP;
break;
/* GL source, changes often, client reads many times */
case GL_DYNAMIC_READ_ARB:
/* Client source, specified once, used by GL many times */
case GL_STATIC_DRAW_ARB:
/* GL source, specified once, client reads many times */
case GL_STATIC_READ_ARB:
/* Client source, specified once, used by GL a few times */
case GL_STREAM_DRAW_ARB:
/* GL source, specified once, client reads a few times */
case GL_STREAM_READ_ARB:
/* GL source, changes often, used by GL many times*/
case GL_DYNAMIC_COPY_ARB:
/* GL source, specified once, used by GL many times */
case GL_STATIC_COPY_ARB:
/* GL source, specified once, used by GL a few times */
case GL_STREAM_COPY_ARB:
gpu_type = NOUVEAU_MEM_FB;
break;
default:
assert(0);
}
nbo->gpu_mem_flags = gpu_type;
nbo->cpu_mem_flags = cpu_type;
nbo->usage = usage;
}
void
nouveau_bo_init_storage(GLcontext *ctx, GLuint valid_gpu_access,
GLsizeiptrARB size,
const GLvoid *data,
GLenum usage,
struct gl_buffer_object *bo)
{
nouveau_buffer_object *nbo = (nouveau_buffer_object *)bo;
DEBUG("bo=%p\n", bo);
/* Free up previous buffers if we can't reuse them */
if (nbo->usage != usage ||
(nbo->gpu_mem && (nbo->gpu_mem->size != size))) {
if (nbo->cpu_mem_sys)
FREE(nbo->cpu_mem_sys);
if (nbo->cpu_mem) {
if (nbo->cpu_mem != nbo->gpu_mem)
NOUVEAU_MEM_FREE(nbo->cpu_mem);
else
nbo->cpu_mem = NULL;
}
if (nbo->gpu_mem)
NOUVEAU_MEM_FREE(nbo->gpu_mem);
bo->OnCard = GL_FALSE;
nbo->cpu_mem_sys = calloc(1, size);
}
nouveau_bo_choose_storage_method(ctx, usage, bo);
/* Force off flags that may not be ok for a given buffer */
nbo->gpu_mem_flags &= valid_gpu_access;
bo->Usage = usage;
bo->Size = size;
if (data) {
GLvoid *map = nouveau_bo_map(ctx, GL_WRITE_ONLY_ARB, bo);
_mesa_memcpy(map, data, size);
nouveau_bo_dirty_all(ctx, GL_FALSE, bo);
nouveau_bo_unmap(ctx, bo);
}
}
void *
nouveau_bo_map(GLcontext *ctx, GLenum access, struct gl_buffer_object *bo)
{
nouveauContextPtr nmesa = NOUVEAU_CONTEXT(ctx);
nouveau_buffer_object *nbo = (nouveau_buffer_object *)bo;
DEBUG("bo=%p, access=%s\n", bo, _mesa_lookup_enum_by_nr(access));
if (bo->OnCard &&
(access == GL_READ_ONLY_ARB || access == GL_READ_WRITE_ARB)) {
GLuint nr_dirty;
DEBUG("..on card\n");
nr_dirty = nouveau_bo_download_dirty(ctx, bo);
/* nouveau_bo_download_dirty won't wait unless it needs to
* free a temp buffer, which isn't the case if cpu_mem is
* present.
*/
if (nr_dirty && nbo->cpu_mem && nbo->cpu_mem != nbo->gpu_mem)
nouveau_notifier_wait_nop(ctx, nmesa->syncNotifier,
NvSubMemFormat);
}
if (nbo->cpu_mem) {
DEBUG("..access via cpu_mem\n");
return nbo->cpu_mem->map;
} else {
DEBUG("..access via cpu_mem_sys\n");
return nbo->cpu_mem_sys;
}
}
void
nouveau_bo_unmap(GLcontext *ctx, struct gl_buffer_object *bo)
{
DEBUG("unmap bo=%p\n", bo);
}
uint32_t
nouveau_bo_gpu_ref(GLcontext *ctx, struct gl_buffer_object *bo)
{
nouveau_buffer_object *nbo = (nouveau_buffer_object *)bo;
assert(nbo->mapped == GL_FALSE);
DEBUG("gpu_ref\n");
if (!bo->OnCard) {
nouveau_bo_move_in(ctx, bo);
bo->OnCard = GL_TRUE;
}
nouveau_bo_upload_dirty(ctx, bo);
return nouveau_mem_gpu_offset_get(ctx, nbo->gpu_mem);
}
void
nouveau_bo_dirty_linear(GLcontext *ctx, GLboolean on_card,
uint32_t offset, uint32_t size,
struct gl_buffer_object *bo)
{
nouveau_buffer_object *nbo = (nouveau_buffer_object *)bo;
nouveau_bufferobj_dirty *dirty;
uint32_t start = offset;
uint32_t end = offset + size;
int i;
if (nbo->cpu_mem == nbo->gpu_mem)
return;
dirty = on_card ? &nbo->gpu_dirty : &nbo->cpu_dirty;
DEBUG("on_card=%d, offset=%d, size=%d, bo=%p\n",
on_card, offset, size, bo);
for (i=0; i<dirty->nr_dirty; i++) {
nouveau_bufferobj_region *r = &dirty->dirty[i];
/* already dirty */
if (start >= r->start && end <= r->end) {
DEBUG("..already dirty\n");
return;
}
/* add to the end of a region */
if (start >= r->start && start <= r->end) {
if (end > r->end) {
DEBUG("..extend end of region\n");
r->end = end;
return;
}
}
/* add to the start of a region */
if (start < r->start && end >= r->end) {
DEBUG("..extend start of region\n");
r->start = start;
/* .. and to the end */
if (end > r->end) {
DEBUG("....and end\n");
r->end = end;
}
return;
}
}
/* new region */
DEBUG("..new dirty\n");
dirty->nr_dirty++;
dirty->dirty = realloc(dirty->dirty,
sizeof(nouveau_bufferobj_region) *
dirty->nr_dirty);
dirty->dirty[dirty->nr_dirty - 1].start = start;
dirty->dirty[dirty->nr_dirty - 1].end = end;
}
void
nouveau_bo_dirty_all(GLcontext *ctx, GLboolean on_card,
struct gl_buffer_object *bo)
{
nouveau_buffer_object *nbo = (nouveau_buffer_object *)bo;
nouveau_bufferobj_dirty *dirty;
dirty = on_card ? &nbo->gpu_dirty : &nbo->cpu_dirty;
DEBUG("dirty all\n");
if (dirty->nr_dirty) {
FREE(dirty->dirty);
dirty->dirty = NULL;
dirty->nr_dirty = 0;
}
nouveau_bo_dirty_linear(ctx, on_card, 0, bo->Size, bo);
}
GLuint
nouveau_bo_upload_dirty(GLcontext *ctx, struct gl_buffer_object *bo)
{
nouveau_buffer_object *nbo = (nouveau_buffer_object *)bo;
nouveau_bufferobj_dirty *dirty = &nbo->cpu_dirty;
GLuint nr_dirty;
int i;
nr_dirty = dirty->nr_dirty;
if (!nr_dirty) {
DEBUG("clean\n");
return nr_dirty;
}
for (i=0; i<nr_dirty; i++) {
nouveau_bufferobj_region *r = &dirty->dirty[i];
DEBUG("dirty %d: o=0x%08x, s=0x%08x\n",
i, r->start, r->end - r->start);
nouveau_bo_upload_to_screen(ctx,
r->start, r->end - r->start, bo);
}
FREE(dirty->dirty);
dirty->dirty = NULL;
dirty->nr_dirty = 0;
return nr_dirty;
}
GLuint
nouveau_bo_download_dirty(GLcontext *ctx, struct gl_buffer_object *bo)
{
nouveau_buffer_object *nbo = (nouveau_buffer_object *)bo;
nouveau_bufferobj_dirty *dirty = &nbo->gpu_dirty;
GLuint nr_dirty;
int i;
nr_dirty = dirty->nr_dirty;
if (nr_dirty) {
DEBUG("clean\n");
return nr_dirty;
}
for (i=0; i<nr_dirty; i++) {
nouveau_bufferobj_region *r = &dirty->dirty[i];
DEBUG("dirty %d: o=0x%08x, s=0x%08x\n",
i, r->start, r->end - r->start);
nouveau_bo_download_from_screen(ctx,
r->start,
r->end - r->start, bo);
}
FREE(dirty->dirty);
dirty->dirty = NULL;
dirty->nr_dirty = 0;
return nr_dirty;
}
static void
nouveauBindBuffer(GLcontext *ctx, GLenum target, struct gl_buffer_object *obj)
{
}
static struct gl_buffer_object *
nouveauNewBufferObject(GLcontext *ctx, GLuint buffer, GLenum target)
{
nouveau_buffer_object *nbo;
nbo = CALLOC_STRUCT(nouveau_buffer_object_t);
if (nbo)
_mesa_initialize_buffer_object(&nbo->mesa, buffer, target);
DEBUG("bo=%p\n", nbo);
return nbo ? &nbo->mesa : NULL;
}
static void
nouveauDeleteBuffer(GLcontext *ctx, struct gl_buffer_object *obj)
{
nouveau_buffer_object *nbo = (nouveau_buffer_object *)obj;
if (nbo->gpu_dirty.nr_dirty)
FREE(nbo->gpu_dirty.dirty);
if (nbo->cpu_dirty.nr_dirty)
FREE(nbo->cpu_dirty.dirty);
if (nbo->cpu_mem) nouveau_mem_free(ctx, nbo->cpu_mem);
if (nbo->gpu_mem) nouveau_mem_free(ctx, nbo->gpu_mem);
_mesa_delete_buffer_object(ctx, obj);
}
static void
nouveauBufferData(GLcontext *ctx, GLenum target, GLsizeiptrARB size,
const GLvoid *data, GLenum usage,
struct gl_buffer_object *obj)
{
GLuint gpu_flags;
DEBUG("target=%s, size=%d, data=%p, usage=%s, obj=%p\n",
_mesa_lookup_enum_by_nr(target),
(GLuint)size, data,
_mesa_lookup_enum_by_nr(usage),
obj);
switch (target) {
case GL_ELEMENT_ARRAY_BUFFER_ARB:
gpu_flags = 0;
break;
default:
gpu_flags = NOUVEAU_BO_VRAM_OK | NOUVEAU_BO_AGP_OK;
break;
}
nouveau_bo_init_storage(ctx, gpu_flags, size, data, usage, obj);
}
static void
nouveauBufferSubData(GLcontext *ctx, GLenum target, GLintptrARB offset,
GLsizeiptrARB size, const GLvoid *data,
struct gl_buffer_object *obj)
{
GLvoid *out;
DEBUG("target=%s, offset=0x%x, size=%d, data=%p, obj=%p\n",
_mesa_lookup_enum_by_nr(target),
(GLuint)offset, (GLuint)size, data, obj);
out = nouveau_bo_map(ctx, GL_WRITE_ONLY_ARB, obj);
_mesa_memcpy(out + offset, data, size);
nouveau_bo_dirty_linear(ctx, GL_FALSE, offset, size, obj);
nouveau_bo_unmap(ctx, obj);
}
static void
nouveauGetBufferSubData(GLcontext *ctx, GLenum target, GLintptrARB offset,
GLsizeiptrARB size, GLvoid *data,
struct gl_buffer_object *obj)
{
const GLvoid *in;
DEBUG("target=%s, offset=0x%x, size=%d, data=%p, obj=%p\n",
_mesa_lookup_enum_by_nr(target),
(GLuint)offset, (GLuint)size, data, obj);
in = nouveau_bo_map(ctx, GL_READ_ONLY_ARB, obj);
_mesa_memcpy(data, in + offset, size);
nouveau_bo_unmap(ctx, obj);
}
static void *
nouveauMapBuffer(GLcontext *ctx, GLenum target, GLenum access,
struct gl_buffer_object *obj)
{
DEBUG("target=%s, access=%s, obj=%p\n",
_mesa_lookup_enum_by_nr(target),
_mesa_lookup_enum_by_nr(access),
obj
);
/* Already mapped.. */
if (obj->Pointer)
return NULL;
/* Have to pass READ_WRITE here, nouveau_bo_map will only ensure that
* the cpu_mem buffer is up-to-date if we ask for read access.
*
* However, even if the client only asks for write access, we're still
* forced to reupload the entire buffer. So, we need the cpu_mem buffer
* to have the correct data all the time.
*/
obj->Pointer = nouveau_bo_map(ctx, GL_READ_WRITE_ARB, obj);
/* The GL spec says that a client attempting to write to a bufferobj
* mapped READ_ONLY object may have unpredictable results, possibly
* even program termination.
*
* We're going to use this, and only mark the buffer as dirtied if
* the client asks for write access.
*/
if (target != GL_READ_ONLY_ARB) {
/* We have no way of knowing what was modified by the client,
* so the entire buffer gets dirtied. */
nouveau_bo_dirty_all(ctx, GL_FALSE, obj);
}
return obj->Pointer;
}
static GLboolean
nouveauUnmapBuffer(GLcontext *ctx, GLenum target, struct gl_buffer_object *obj)
{
DEBUG("target=%s, obj=%p\n", _mesa_lookup_enum_by_nr(target), obj);
assert(obj->Pointer);
nouveau_bo_unmap(ctx, obj);
obj->Pointer = NULL;
return GL_TRUE;
}
void
nouveauInitBufferObjects(GLcontext *ctx)
{
ctx->Driver.BindBuffer = nouveauBindBuffer;
ctx->Driver.NewBufferObject = nouveauNewBufferObject;
ctx->Driver.DeleteBuffer = nouveauDeleteBuffer;
ctx->Driver.BufferData = nouveauBufferData;
ctx->Driver.BufferSubData = nouveauBufferSubData;
ctx->Driver.GetBufferSubData = nouveauGetBufferSubData;
ctx->Driver.MapBuffer = nouveauMapBuffer;
ctx->Driver.UnmapBuffer = nouveauUnmapBuffer;
}
|