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
|
/*
* Copyright 2012 Red Hat Inc.
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
* OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*
* Authors: Ben Skeggs
*
*/
#include "draw/draw_context.h"
#include "draw/draw_vertex.h"
#include "draw/draw_pipe.h"
#include "draw/draw_vbuf.h"
#include "draw/draw_private.h"
#include "nouveau/nv_object.xml.h"
#include "nv30-40_3d.xml.h"
#include "nv30_context.h"
#include "nv30_format.h"
struct nv30_render {
struct vbuf_render base;
struct nv30_context *nv30;
struct pipe_transfer *transfer;
struct pipe_resource *buffer;
unsigned offset;
unsigned length;
struct vertex_info vertex_info;
struct nouveau_heap *vertprog;
uint32_t vtxprog[16][4];
uint32_t vtxfmt[16];
uint32_t vtxptr[16];
uint32_t prim;
};
static INLINE struct nv30_render *
nv30_render(struct vbuf_render *render)
{
return (struct nv30_render *)render;
}
static const struct vertex_info *
nv30_render_get_vertex_info(struct vbuf_render *render)
{
return &nv30_render(render)->vertex_info;
}
static boolean
nv30_render_allocate_vertices(struct vbuf_render *render,
ushort vertex_size, ushort nr_vertices)
{
struct nv30_render *r = nv30_render(render);
struct nv30_context *nv30 = r->nv30;
r->length = vertex_size * nr_vertices;
if (r->offset + r->length >= render->max_vertex_buffer_bytes) {
pipe_resource_reference(&r->buffer, NULL);
r->buffer = pipe_buffer_create(&nv30->screen->base.base,
PIPE_BIND_VERTEX_BUFFER, 0,
render->max_vertex_buffer_bytes);
if (!r->buffer)
return FALSE;
r->offset = 0;
}
return TRUE;
}
static void *
nv30_render_map_vertices(struct vbuf_render *render)
{
struct nv30_render *r = nv30_render(render);
char *map = pipe_buffer_map(&r->nv30->base.pipe, r->buffer,
PIPE_TRANSFER_WRITE |
PIPE_TRANSFER_UNSYNCHRONIZED, &r->transfer);
return map + r->offset;
}
static void
nv30_render_unmap_vertices(struct vbuf_render *render,
ushort min_index, ushort max_index)
{
struct nv30_render *r = nv30_render(render);
pipe_buffer_unmap(&r->nv30->base.pipe, r->transfer);
}
static void
nv30_render_set_primitive(struct vbuf_render *render, unsigned prim)
{
struct nv30_render *r = nv30_render(render);
r->prim = nv30_prim_gl(prim);
}
static void
nv30_render_draw_elements(struct vbuf_render *render,
const ushort *indices, uint count)
{
struct nv30_render *r = nv30_render(render);
struct nv30_context *nv30 = r->nv30;
struct nouveau_pushbuf *push = nv30->screen->base.pushbuf;
unsigned i;
BEGIN_NV04(push, NV30_3D(VTXBUF(0)), r->vertex_info.num_attribs);
for (i = 0; i < r->vertex_info.num_attribs; i++) {
PUSH_RESRC(push, NV30_3D(VTXBUF(i)), BUFCTX_VTXTMP,
nv04_resource(r->buffer), r->offset + r->vtxptr[i],
NOUVEAU_BO_LOW | NOUVEAU_BO_RD, 0, 0);
}
if (!nv30_state_validate(nv30, FALSE))
return;
BEGIN_NV04(push, NV30_3D(VERTEX_BEGIN_END), 1);
PUSH_DATA (push, r->prim);
if (count & 1) {
BEGIN_NV04(push, NV30_3D(VB_ELEMENT_U32), 1);
PUSH_DATA (push, *indices++);
}
count >>= 1;
while (count) {
unsigned npush = MIN2(count, NV04_PFIFO_MAX_PACKET_LEN);
count -= npush;
BEGIN_NI04(push, NV30_3D(VB_ELEMENT_U16), npush);
while (npush--) {
PUSH_DATA(push, (indices[1] << 16) | indices[0]);
indices += 2;
}
}
BEGIN_NV04(push, NV30_3D(VERTEX_BEGIN_END), 1);
PUSH_DATA (push, NV30_3D_VERTEX_BEGIN_END_STOP);
PUSH_RESET(push, BUFCTX_VTXTMP);
}
static void
nv30_render_draw_arrays(struct vbuf_render *render, unsigned start, uint nr)
{
struct nv30_render *r = nv30_render(render);
struct nv30_context *nv30 = r->nv30;
struct nouveau_pushbuf *push = nv30->base.pushbuf;
unsigned fn = nr >> 8, pn = nr & 0xff;
unsigned ps = fn + (pn ? 1 : 0);
unsigned i;
BEGIN_NV04(push, NV30_3D(VTXBUF(0)), r->vertex_info.num_attribs);
for (i = 0; i < r->vertex_info.num_attribs; i++) {
PUSH_RESRC(push, NV30_3D(VTXBUF(i)), BUFCTX_VTXTMP,
nv04_resource(r->buffer), r->offset + r->vtxptr[i],
NOUVEAU_BO_LOW | NOUVEAU_BO_RD, 0, 0);
}
if (!nv30_state_validate(nv30, FALSE))
return;
BEGIN_NV04(push, NV30_3D(VERTEX_BEGIN_END), 1);
PUSH_DATA (push, r->prim);
BEGIN_NI04(push, NV30_3D(VB_VERTEX_BATCH), ps);
while (fn--) {
PUSH_DATA (push, 0xff000000 | start);
start += 256;
}
if (pn)
PUSH_DATA (push, ((pn - 1) << 24) | start);
BEGIN_NV04(push, NV30_3D(VERTEX_BEGIN_END), 1);
PUSH_DATA (push, NV30_3D_VERTEX_BEGIN_END_STOP);
PUSH_RESET(push, BUFCTX_VTXTMP);
}
static void
nv30_render_release_vertices(struct vbuf_render *render)
{
struct nv30_render *r = nv30_render(render);
r->offset += r->length;
}
static const struct {
unsigned emit;
unsigned interp;
unsigned vp30;
unsigned vp40;
unsigned ow40;
} vroute [] = {
[TGSI_SEMANTIC_POSITION] = { EMIT_4F, INTERP_PERSPECTIVE, 0, 0, 0x00000000 },
[TGSI_SEMANTIC_COLOR ] = { EMIT_4F, INTERP_LINEAR , 3, 1, 0x00000001 },
[TGSI_SEMANTIC_BCOLOR ] = { EMIT_4F, INTERP_LINEAR , 1, 3, 0x00000004 },
[TGSI_SEMANTIC_FOG ] = { EMIT_4F, INTERP_PERSPECTIVE, 5, 5, 0x00000010 },
[TGSI_SEMANTIC_PSIZE ] = { EMIT_1F_PSIZE, INTERP_POS , 6, 6, 0x00000020 },
[TGSI_SEMANTIC_GENERIC ] = { EMIT_4F, INTERP_PERSPECTIVE, 8, 7, 0x00004000 }
};
static boolean
vroute_add(struct nv30_render *r, uint attrib, uint sem, uint *idx)
{
struct pipe_screen *pscreen = &r->nv30->screen->base.base;
struct nv30_fragprog *fp = r->nv30->fragprog.program;
struct vertex_info *vinfo = &r->vertex_info;
enum pipe_format format;
uint emit = EMIT_OMIT;
uint result = *idx;
if (sem == TGSI_SEMANTIC_GENERIC && result >= 8) {
for (result = 0; result < 8; result++) {
if (fp->texcoord[result] == *idx) {
emit = vroute[sem].emit;
break;
}
}
} else {
emit = vroute[sem].emit;
}
if (emit == EMIT_OMIT)
return FALSE;
draw_emit_vertex_attr(vinfo, emit, vroute[sem].interp, attrib);
format = draw_translate_vinfo_format(emit);
r->vtxfmt[attrib] = nv30_vtxfmt(pscreen, format)->hw;
r->vtxptr[attrib] = vinfo->size | NV30_3D_VTXBUF_DMA1;
vinfo->size += draw_translate_vinfo_size(emit);
if (nv30_screen(pscreen)->eng3d->oclass < NV40_3D_CLASS) {
r->vtxprog[attrib][0] = 0x001f38d8;
r->vtxprog[attrib][1] = 0x0080001b | (attrib << 9);
r->vtxprog[attrib][2] = 0x0836106c;
r->vtxprog[attrib][3] = 0x2000f800 | (result + vroute[sem].vp30) << 2;
} else {
r->vtxprog[attrib][0] = 0x401f9c6c;
r->vtxprog[attrib][1] = 0x0040000d | (attrib << 8);
r->vtxprog[attrib][2] = 0x8106c083;
r->vtxprog[attrib][3] = 0x6041ff80 | (result + vroute[sem].vp40) << 2;
}
*idx = vroute[sem].ow40 << result;
return TRUE;
}
static boolean
nv30_render_validate(struct nv30_context *nv30)
{
struct nv30_render *r = nv30_render(nv30->draw->render);
struct nv30_rasterizer_stateobj *rast = nv30->rast;
struct pipe_screen *pscreen = &nv30->screen->base.base;
struct nouveau_pushbuf *push = nv30->screen->base.pushbuf;
struct nouveau_object *eng3d = nv30->screen->eng3d;
struct nv30_vertprog *vp = nv30->vertprog.program;
struct vertex_info *vinfo = &r->vertex_info;
unsigned vp_attribs = 0;
unsigned vp_results = 0;
unsigned attrib = 0;
unsigned pntc;
int i;
if (!r->vertprog) {
struct nouveau_heap *heap = nv30_screen(pscreen)->vp_exec_heap;
if (nouveau_heap_alloc(heap, 16, &r->vertprog, &r->vertprog)) {
while (heap->next && heap->size < 16) {
struct nouveau_heap **evict = heap->next->priv;
nouveau_heap_free(evict);
}
if (nouveau_heap_alloc(heap, 16, &r->vertprog, &r->vertprog))
return FALSE;
}
}
vinfo->num_attribs = 0;
vinfo->size = 0;
/* setup routing for all necessary vp outputs */
for (i = 0; i < vp->info.num_outputs && attrib < 16; i++) {
uint semantic = vp->info.output_semantic_name[i];
uint index = vp->info.output_semantic_index[i];
if (vroute_add(r, attrib, semantic, &index)) {
vp_attribs |= (1 << attrib++);
vp_results |= index;
}
}
/* setup routing for replaced point coords not written by vp */
if (rast && rast->pipe.point_quad_rasterization)
pntc = rast->pipe.sprite_coord_enable & 0x000002ff;
else
pntc = 0;
while (pntc && attrib < 16) {
uint index = ffs(pntc) - 1; pntc &= ~(1 << index);
if (vroute_add(r, attrib, TGSI_SEMANTIC_GENERIC, &index)) {
vp_attribs |= (1 << attrib++);
vp_results |= index;
}
}
/* modify vertex format for correct stride, and stub out unused ones */
BEGIN_NV04(push, NV30_3D(VP_UPLOAD_FROM_ID), 1);
PUSH_DATA (push, r->vertprog->start);
r->vtxprog[attrib - 1][3] |= 1;
for (i = 0; i < attrib; i++) {
BEGIN_NV04(push, NV30_3D(VP_UPLOAD_INST(0)), 4);
PUSH_DATAp(push, r->vtxprog[i], 4);
r->vtxfmt[i] |= vinfo->size << 8;
}
for (; i < 16; i++)
r->vtxfmt[i] = NV30_3D_VTXFMT_TYPE_V32_FLOAT;
BEGIN_NV04(push, NV30_3D(VIEWPORT_TRANSLATE_X), 8);
PUSH_DATAf(push, 0.0);
PUSH_DATAf(push, 0.0);
PUSH_DATAf(push, 0.0);
PUSH_DATAf(push, 0.0);
PUSH_DATAf(push, 1.0);
PUSH_DATAf(push, 1.0);
PUSH_DATAf(push, 1.0);
PUSH_DATAf(push, 1.0);
BEGIN_NV04(push, NV30_3D(DEPTH_RANGE_NEAR), 2);
PUSH_DATAf(push, 0.0);
PUSH_DATAf(push, 1.0);
BEGIN_NV04(push, NV30_3D(VTXFMT(0)), 16);
PUSH_DATAp(push, r->vtxfmt, 16);
BEGIN_NV04(push, NV30_3D(VP_START_FROM_ID), 1);
PUSH_DATA (push, r->vertprog->start);
BEGIN_NV04(push, NV30_3D(ENGINE), 1);
PUSH_DATA (push, 0x00000103);
if (eng3d->oclass >= NV40_3D_CLASS) {
BEGIN_NV04(push, NV40_3D(VP_ATTRIB_EN), 2);
PUSH_DATA (push, vp_attribs);
PUSH_DATA (push, vp_results);
}
vinfo->size /= 4;
return TRUE;
}
void
nv30_render_vbo(struct pipe_context *pipe, const struct pipe_draw_info *info)
{
struct nv30_context *nv30 = nv30_context(pipe);
struct draw_context *draw = nv30->draw;
struct pipe_transfer *transfer[PIPE_MAX_ATTRIBS] = {NULL};
struct pipe_transfer *transferi = NULL;
int i;
nv30_render_validate(nv30);
if (nv30->draw_dirty & NV30_NEW_VIEWPORT)
draw_set_viewport_state(draw, &nv30->viewport);
if (nv30->draw_dirty & NV30_NEW_RASTERIZER)
draw_set_rasterizer_state(draw, &nv30->rast->pipe, NULL);
if (nv30->draw_dirty & NV30_NEW_CLIP)
draw_set_clip_state(draw, &nv30->clip);
if (nv30->draw_dirty & NV30_NEW_ARRAYS) {
draw_set_vertex_buffers(draw, 0, nv30->num_vtxbufs, nv30->vtxbuf);
draw_set_vertex_elements(draw, nv30->vertex->num_elements, nv30->vertex->pipe);
}
if (nv30->draw_dirty & NV30_NEW_FRAGPROG) {
struct nv30_fragprog *fp = nv30->fragprog.program;
if (!fp->draw)
fp->draw = draw_create_fragment_shader(draw, &fp->pipe);
draw_bind_fragment_shader(draw, fp->draw);
}
if (nv30->draw_dirty & NV30_NEW_VERTPROG) {
struct nv30_vertprog *vp = nv30->vertprog.program;
if (!vp->draw)
vp->draw = draw_create_vertex_shader(draw, &vp->pipe);
draw_bind_vertex_shader(draw, vp->draw);
}
if (nv30->draw_dirty & NV30_NEW_VERTCONST) {
if (nv30->vertprog.constbuf) {
void *map = nv04_resource(nv30->vertprog.constbuf)->data;
draw_set_mapped_constant_buffer(draw, PIPE_SHADER_VERTEX, 0,
map, nv30->vertprog.constbuf_nr);
}
}
for (i = 0; i < nv30->num_vtxbufs; i++) {
const void *map = nv30->vtxbuf[i].user_buffer;
if (!map) {
if (!nv30->vtxbuf[i].buffer) {
continue;
}
map = pipe_buffer_map(pipe, nv30->vtxbuf[i].buffer,
PIPE_TRANSFER_UNSYNCHRONIZED |
PIPE_TRANSFER_READ, &transfer[i]);
}
draw_set_mapped_vertex_buffer(draw, i, map, ~0);
}
if (info->indexed) {
const void *map = nv30->idxbuf.user_buffer;
if (!map)
pipe_buffer_map(pipe, nv30->idxbuf.buffer,
PIPE_TRANSFER_UNSYNCHRONIZED |
PIPE_TRANSFER_READ, &transferi);
draw_set_indexes(draw,
(ubyte *) map + nv30->idxbuf.offset,
nv30->idxbuf.index_size);
} else {
draw_set_indexes(draw, NULL, 0);
}
draw_vbo(draw, info);
draw_flush(draw);
if (info->indexed && transferi)
pipe_buffer_unmap(pipe, transferi);
for (i = 0; i < nv30->num_vtxbufs; i++)
if (transfer[i])
pipe_buffer_unmap(pipe, transfer[i]);
nv30->draw_dirty = 0;
nv30_state_release(nv30);
}
static void
nv30_render_destroy(struct vbuf_render *render)
{
FREE(render);
}
static struct vbuf_render *
nv30_render_create(struct nv30_context *nv30)
{
struct nv30_render *r = CALLOC_STRUCT(nv30_render);
if (!r)
return NULL;
r->nv30 = nv30;
r->offset = 1 * 1024 * 1024;
r->base.max_indices = 16 * 1024;
r->base.max_vertex_buffer_bytes = r->offset;
r->base.get_vertex_info = nv30_render_get_vertex_info;
r->base.allocate_vertices = nv30_render_allocate_vertices;
r->base.map_vertices = nv30_render_map_vertices;
r->base.unmap_vertices = nv30_render_unmap_vertices;
r->base.set_primitive = nv30_render_set_primitive;
r->base.draw_elements = nv30_render_draw_elements;
r->base.draw_arrays = nv30_render_draw_arrays;
r->base.release_vertices = nv30_render_release_vertices;
r->base.destroy = nv30_render_destroy;
return &r->base;
}
void
nv30_draw_init(struct pipe_context *pipe)
{
struct nv30_context *nv30 = nv30_context(pipe);
struct vbuf_render *render;
struct draw_context *draw;
struct draw_stage *stage;
draw = draw_create(pipe);
if (!draw)
return;
render = nv30_render_create(nv30);
if (!render) {
draw_destroy(draw);
return;
}
stage = draw_vbuf_stage(draw, render);
if (!stage) {
render->destroy(render);
draw_destroy(draw);
return;
}
draw_set_render(draw, render);
draw_set_rasterize_stage(draw, stage);
draw_wide_line_threshold(draw, 10000000.f);
draw_wide_point_threshold(draw, 10000000.f);
draw_wide_point_sprites(draw, TRUE);
nv30->draw = draw;
}
|