2 #include "pipe/p_context.h"
3 #include "pipe/p_state.h"
4 #include "util/u_inlines.h"
5 #include "util/u_format.h"
6 #include "translate/translate.h"
8 #include "nvc0/nvc0_context.h"
9 #include "nvc0/nvc0_resource.h"
11 #include "nvc0/nvc0_3d.xml.h"
14 struct nouveau_pushbuf
*push
;
16 struct translate
*translate
;
21 uint32_t restart_index
;
35 static void nvc0_push_upload_vertex_ids(struct push_context
*,
36 struct nvc0_context
*,
37 const struct pipe_draw_info
*);
40 nvc0_push_context_init(struct nvc0_context
*nvc0
, struct push_context
*ctx
)
42 ctx
->push
= nvc0
->base
.pushbuf
;
44 ctx
->translate
= nvc0
->vertex
->translate
;
45 ctx
->vertex_size
= nvc0
->vertex
->size
;
48 nvc0
->vertprog
->vp
.need_vertex_id
&& (nvc0
->vertex
->num_elements
< 32);
50 ctx
->edgeflag
.value
= true;
51 ctx
->edgeflag
.enabled
= nvc0
->vertprog
->vp
.edgeflag
< PIPE_MAX_ATTRIBS
;
53 /* silence warnings */
54 ctx
->edgeflag
.data
= NULL
;
55 ctx
->edgeflag
.stride
= 0;
59 nvc0_vertex_configure_translate(struct nvc0_context
*nvc0
, int32_t index_bias
)
61 struct translate
*translate
= nvc0
->vertex
->translate
;
64 for (i
= 0; i
< nvc0
->num_vtxbufs
; ++i
) {
66 const struct pipe_vertex_buffer
*vb
= &nvc0
->vtxbuf
[i
];
68 if (likely(!vb
->buffer
))
69 map
= (const uint8_t *)vb
->user_buffer
;
71 map
= nouveau_resource_map_offset(&nvc0
->base
,
72 nv04_resource(vb
->buffer
), vb
->buffer_offset
, NOUVEAU_BO_RD
);
74 if (index_bias
&& !unlikely(nvc0
->vertex
->instance_bufs
& (1 << i
)))
75 map
+= (intptr_t)index_bias
* vb
->stride
;
77 translate
->set_buffer(translate
, i
, map
, vb
->stride
, ~0);
82 nvc0_push_map_idxbuf(struct push_context
*ctx
, struct nvc0_context
*nvc0
)
84 if (nvc0
->idxbuf
.buffer
) {
85 struct nv04_resource
*buf
= nv04_resource(nvc0
->idxbuf
.buffer
);
86 ctx
->idxbuf
= nouveau_resource_map_offset(&nvc0
->base
,
87 buf
, nvc0
->idxbuf
.offset
, NOUVEAU_BO_RD
);
89 ctx
->idxbuf
= nvc0
->idxbuf
.user_buffer
;
94 nvc0_push_map_edgeflag(struct push_context
*ctx
, struct nvc0_context
*nvc0
,
97 unsigned attr
= nvc0
->vertprog
->vp
.edgeflag
;
98 struct pipe_vertex_element
*ve
= &nvc0
->vertex
->element
[attr
].pipe
;
99 struct pipe_vertex_buffer
*vb
= &nvc0
->vtxbuf
[ve
->vertex_buffer_index
];
100 struct nv04_resource
*buf
= nv04_resource(vb
->buffer
);
102 ctx
->edgeflag
.stride
= vb
->stride
;
104 unsigned offset
= vb
->buffer_offset
+ ve
->src_offset
;
105 ctx
->edgeflag
.data
= nouveau_resource_map_offset(&nvc0
->base
,
106 buf
, offset
, NOUVEAU_BO_RD
);
108 ctx
->edgeflag
.data
= (const uint8_t *)vb
->user_buffer
+ ve
->src_offset
;
112 ctx
->edgeflag
.data
+= (intptr_t)index_bias
* vb
->stride
;
115 static INLINE
unsigned
116 prim_restart_search_i08(const uint8_t *elts
, unsigned push
, uint8_t index
)
119 for (i
= 0; i
< push
&& elts
[i
] != index
; ++i
);
123 static INLINE
unsigned
124 prim_restart_search_i16(const uint16_t *elts
, unsigned push
, uint16_t index
)
127 for (i
= 0; i
< push
&& elts
[i
] != index
; ++i
);
131 static INLINE
unsigned
132 prim_restart_search_i32(const uint32_t *elts
, unsigned push
, uint32_t index
)
135 for (i
= 0; i
< push
&& elts
[i
] != index
; ++i
);
140 ef_value(const struct push_context
*ctx
, uint32_t index
)
142 float *pf
= (float *)&ctx
->edgeflag
.data
[index
* ctx
->edgeflag
.stride
];
143 return *pf
? true : false;
147 ef_toggle(struct push_context
*ctx
)
149 ctx
->edgeflag
.value
= !ctx
->edgeflag
.value
;
150 return ctx
->edgeflag
.value
;
153 static INLINE
unsigned
154 ef_toggle_search_i08(struct push_context
*ctx
, const uint8_t *elts
, unsigned n
)
157 for (i
= 0; i
< n
&& ef_value(ctx
, elts
[i
]) == ctx
->edgeflag
.value
; ++i
);
161 static INLINE
unsigned
162 ef_toggle_search_i16(struct push_context
*ctx
, const uint16_t *elts
, unsigned n
)
165 for (i
= 0; i
< n
&& ef_value(ctx
, elts
[i
]) == ctx
->edgeflag
.value
; ++i
);
169 static INLINE
unsigned
170 ef_toggle_search_i32(struct push_context
*ctx
, const uint32_t *elts
, unsigned n
)
173 for (i
= 0; i
< n
&& ef_value(ctx
, elts
[i
]) == ctx
->edgeflag
.value
; ++i
);
177 static INLINE
unsigned
178 ef_toggle_search_seq(struct push_context
*ctx
, unsigned start
, unsigned n
)
181 for (i
= 0; i
< n
&& ef_value(ctx
, start
++) == ctx
->edgeflag
.value
; ++i
);
186 nvc0_push_setup_vertex_array(struct nvc0_context
*nvc0
, const unsigned count
)
188 struct nouveau_pushbuf
*push
= nvc0
->base
.pushbuf
;
189 struct nouveau_bo
*bo
;
191 const unsigned size
= count
* nvc0
->vertex
->size
;
193 void *const dest
= nouveau_scratch_get(&nvc0
->base
, size
, &va
, &bo
);
195 BEGIN_NVC0(push
, NVC0_3D(VERTEX_ARRAY_START_HIGH(0)), 2);
196 PUSH_DATAh(push
, va
);
197 PUSH_DATA (push
, va
);
198 BEGIN_NVC0(push
, NVC0_3D(VERTEX_ARRAY_LIMIT_HIGH(0)), 2);
199 PUSH_DATAh(push
, va
+ size
- 1);
200 PUSH_DATA (push
, va
+ size
- 1);
202 BCTX_REFN_bo(nvc0
->bufctx_3d
, VTX_TMP
, NOUVEAU_BO_GART
| NOUVEAU_BO_RD
,
204 nouveau_pushbuf_validate(push
);
210 disp_vertices_i08(struct push_context
*ctx
, unsigned start
, unsigned count
)
212 struct nouveau_pushbuf
*push
= ctx
->push
;
213 struct translate
*translate
= ctx
->translate
;
214 const uint8_t *restrict elts
= (uint8_t *)ctx
->idxbuf
+ start
;
220 if (unlikely(ctx
->prim_restart
))
221 nR
= prim_restart_search_i08(elts
, nR
, ctx
->restart_index
);
223 translate
->run_elts8(translate
, elts
, nR
, 0, ctx
->instance_id
, ctx
->dest
);
225 ctx
->dest
+= nR
* ctx
->vertex_size
;
230 if (unlikely(ctx
->edgeflag
.enabled
))
231 nE
= ef_toggle_search_i08(ctx
, elts
, nR
);
234 if (likely(nE
>= 2)) {
235 BEGIN_NVC0(push
, NVC0_3D(VERTEX_BUFFER_FIRST
), 2);
236 PUSH_DATA (push
, pos
);
237 PUSH_DATA (push
, nE
);
241 IMMED_NVC0(push
, NVC0_3D(VB_ELEMENT_U32
), pos
);
243 BEGIN_NVC0(push
, NVC0_3D(VB_ELEMENT_U32
), 1);
244 PUSH_DATA (push
, pos
);
247 if (unlikely(nE
!= nR
))
248 IMMED_NVC0(push
, NVC0_3D(EDGEFLAG
), ef_toggle(ctx
));
255 BEGIN_NVC0(push
, NVC0_3D(VB_ELEMENT_U32
), 1);
256 PUSH_DATA (push
, 0xffffffff);
258 ctx
->dest
+= ctx
->vertex_size
;
266 disp_vertices_i16(struct push_context
*ctx
, unsigned start
, unsigned count
)
268 struct nouveau_pushbuf
*push
= ctx
->push
;
269 struct translate
*translate
= ctx
->translate
;
270 const uint16_t *restrict elts
= (uint16_t *)ctx
->idxbuf
+ start
;
276 if (unlikely(ctx
->prim_restart
))
277 nR
= prim_restart_search_i16(elts
, nR
, ctx
->restart_index
);
279 translate
->run_elts16(translate
, elts
, nR
, 0, ctx
->instance_id
, ctx
->dest
);
281 ctx
->dest
+= nR
* ctx
->vertex_size
;
286 if (unlikely(ctx
->edgeflag
.enabled
))
287 nE
= ef_toggle_search_i16(ctx
, elts
, nR
);
290 if (likely(nE
>= 2)) {
291 BEGIN_NVC0(push
, NVC0_3D(VERTEX_BUFFER_FIRST
), 2);
292 PUSH_DATA (push
, pos
);
293 PUSH_DATA (push
, nE
);
297 IMMED_NVC0(push
, NVC0_3D(VB_ELEMENT_U32
), pos
);
299 BEGIN_NVC0(push
, NVC0_3D(VB_ELEMENT_U32
), 1);
300 PUSH_DATA (push
, pos
);
303 if (unlikely(nE
!= nR
))
304 IMMED_NVC0(push
, NVC0_3D(EDGEFLAG
), ef_toggle(ctx
));
311 BEGIN_NVC0(push
, NVC0_3D(VB_ELEMENT_U32
), 1);
312 PUSH_DATA (push
, 0xffffffff);
314 ctx
->dest
+= ctx
->vertex_size
;
322 disp_vertices_i32(struct push_context
*ctx
, unsigned start
, unsigned count
)
324 struct nouveau_pushbuf
*push
= ctx
->push
;
325 struct translate
*translate
= ctx
->translate
;
326 const uint32_t *restrict elts
= (uint32_t *)ctx
->idxbuf
+ start
;
332 if (unlikely(ctx
->prim_restart
))
333 nR
= prim_restart_search_i32(elts
, nR
, ctx
->restart_index
);
335 translate
->run_elts(translate
, elts
, nR
, 0, ctx
->instance_id
, ctx
->dest
);
337 ctx
->dest
+= nR
* ctx
->vertex_size
;
342 if (unlikely(ctx
->edgeflag
.enabled
))
343 nE
= ef_toggle_search_i32(ctx
, elts
, nR
);
346 if (likely(nE
>= 2)) {
347 BEGIN_NVC0(push
, NVC0_3D(VERTEX_BUFFER_FIRST
), 2);
348 PUSH_DATA (push
, pos
);
349 PUSH_DATA (push
, nE
);
353 IMMED_NVC0(push
, NVC0_3D(VB_ELEMENT_U32
), pos
);
355 BEGIN_NVC0(push
, NVC0_3D(VB_ELEMENT_U32
), 1);
356 PUSH_DATA (push
, pos
);
359 if (unlikely(nE
!= nR
))
360 IMMED_NVC0(push
, NVC0_3D(EDGEFLAG
), ef_toggle(ctx
));
367 BEGIN_NVC0(push
, NVC0_3D(VB_ELEMENT_U32
), 1);
368 PUSH_DATA (push
, 0xffffffff);
370 ctx
->dest
+= ctx
->vertex_size
;
378 disp_vertices_seq(struct push_context
*ctx
, unsigned start
, unsigned count
)
380 struct nouveau_pushbuf
*push
= ctx
->push
;
381 struct translate
*translate
= ctx
->translate
;
384 /* XXX: This will read the data corresponding to the primitive restart index,
385 * maybe we should avoid that ?
387 translate
->run(translate
, start
, count
, 0, ctx
->instance_id
, ctx
->dest
);
391 if (unlikely(ctx
->edgeflag
.enabled
))
392 nr
= ef_toggle_search_seq(ctx
, start
+ pos
, nr
);
396 BEGIN_NVC0(push
, NVC0_3D(VERTEX_BUFFER_FIRST
), 2);
397 PUSH_DATA (push
, pos
);
398 PUSH_DATA (push
, nr
);
400 if (unlikely(nr
!= count
))
401 IMMED_NVC0(push
, NVC0_3D(EDGEFLAG
), ef_toggle(ctx
));
409 #define NVC0_PRIM_GL_CASE(n) \
410 case PIPE_PRIM_##n: return NVC0_3D_VERTEX_BEGIN_GL_PRIMITIVE_##n
412 static INLINE
unsigned
413 nvc0_prim_gl(unsigned prim
)
416 NVC0_PRIM_GL_CASE(POINTS
);
417 NVC0_PRIM_GL_CASE(LINES
);
418 NVC0_PRIM_GL_CASE(LINE_LOOP
);
419 NVC0_PRIM_GL_CASE(LINE_STRIP
);
420 NVC0_PRIM_GL_CASE(TRIANGLES
);
421 NVC0_PRIM_GL_CASE(TRIANGLE_STRIP
);
422 NVC0_PRIM_GL_CASE(TRIANGLE_FAN
);
423 NVC0_PRIM_GL_CASE(QUADS
);
424 NVC0_PRIM_GL_CASE(QUAD_STRIP
);
425 NVC0_PRIM_GL_CASE(POLYGON
);
426 NVC0_PRIM_GL_CASE(LINES_ADJACENCY
);
427 NVC0_PRIM_GL_CASE(LINE_STRIP_ADJACENCY
);
428 NVC0_PRIM_GL_CASE(TRIANGLES_ADJACENCY
);
429 NVC0_PRIM_GL_CASE(TRIANGLE_STRIP_ADJACENCY
);
431 NVC0_PRIM_GL_CASE(PATCHES); */
433 return NVC0_3D_VERTEX_BEGIN_GL_PRIMITIVE_POINTS
;
438 nvc0_push_vbo(struct nvc0_context
*nvc0
, const struct pipe_draw_info
*info
)
440 struct push_context ctx
;
441 unsigned i
, index_size
;
442 unsigned inst_count
= info
->instance_count
;
443 unsigned vert_count
= info
->count
;
446 nvc0_push_context_init(nvc0
, &ctx
);
448 nvc0_vertex_configure_translate(nvc0
, info
->index_bias
);
450 if (nvc0
->state
.index_bias
) {
451 /* this is already taken care of by translate */
452 IMMED_NVC0(ctx
.push
, NVC0_3D(VB_ELEMENT_BASE
), 0);
453 nvc0
->state
.index_bias
= 0;
456 if (unlikely(ctx
.edgeflag
.enabled
))
457 nvc0_push_map_edgeflag(&ctx
, nvc0
, info
->index_bias
);
459 ctx
.prim_restart
= info
->primitive_restart
;
460 ctx
.restart_index
= info
->restart_index
;
462 if (info
->primitive_restart
) {
463 /* NOTE: I hope we won't ever need that last index (~0).
464 * If we do, we have to disable primitive restart here always and
465 * use END,BEGIN to restart. (XXX: would that affect PrimitiveID ?)
466 * We could also deactive PRIM_RESTART_WITH_DRAW_ARRAYS temporarily,
467 * and add manual restart to disp_vertices_seq.
469 BEGIN_NVC0(ctx
.push
, NVC0_3D(PRIM_RESTART_ENABLE
), 2);
470 PUSH_DATA (ctx
.push
, 1);
471 PUSH_DATA (ctx
.push
, info
->indexed
? 0xffffffff : info
->restart_index
);
473 if (nvc0
->state
.prim_restart
) {
474 IMMED_NVC0(ctx
.push
, NVC0_3D(PRIM_RESTART_ENABLE
), 0);
476 nvc0
->state
.prim_restart
= info
->primitive_restart
;
479 nvc0_push_map_idxbuf(&ctx
, nvc0
);
480 index_size
= nvc0
->idxbuf
.index_size
;
482 if (unlikely(info
->count_from_stream_output
)) {
483 struct pipe_context
*pipe
= &nvc0
->base
.pipe
;
484 struct nvc0_so_target
*targ
;
485 targ
= nvc0_so_target(info
->count_from_stream_output
);
486 pipe
->get_query_result(pipe
, targ
->pq
, true, (void *)&vert_count
);
487 vert_count
/= targ
->stride
;
489 ctx
.idxbuf
= NULL
; /* shut up warnings */
493 ctx
.instance_id
= info
->start_instance
;
495 prim
= nvc0_prim_gl(info
->mode
);
497 PUSH_SPACE(ctx
.push
, 9);
499 ctx
.dest
= nvc0_push_setup_vertex_array(nvc0
, vert_count
);
500 if (unlikely(!ctx
.dest
))
503 if (unlikely(ctx
.need_vertex_id
))
504 nvc0_push_upload_vertex_ids(&ctx
, nvc0
, info
);
506 if (nvc0
->screen
->eng3d
->oclass
< GM107_3D_CLASS
)
507 IMMED_NVC0(ctx
.push
, NVC0_3D(VERTEX_ARRAY_FLUSH
), 0);
508 BEGIN_NVC0(ctx
.push
, NVC0_3D(VERTEX_BEGIN_GL
), 1);
509 PUSH_DATA (ctx
.push
, prim
);
510 switch (index_size
) {
512 disp_vertices_i08(&ctx
, info
->start
, vert_count
);
515 disp_vertices_i16(&ctx
, info
->start
, vert_count
);
518 disp_vertices_i32(&ctx
, info
->start
, vert_count
);
521 assert(index_size
== 0);
522 disp_vertices_seq(&ctx
, info
->start
, vert_count
);
525 PUSH_SPACE(ctx
.push
, 1);
526 IMMED_NVC0(ctx
.push
, NVC0_3D(VERTEX_END_GL
), 0);
529 prim
|= NVC0_3D_VERTEX_BEGIN_GL_INSTANCE_NEXT
;
532 nouveau_bufctx_reset(nvc0
->bufctx_3d
, NVC0_BIND_VTX_TMP
);
533 nouveau_scratch_done(&nvc0
->base
);
534 } while (inst_count
);
537 /* reset state and unmap buffers (no-op) */
539 if (unlikely(!ctx
.edgeflag
.value
)) {
540 PUSH_SPACE(ctx
.push
, 1);
541 IMMED_NVC0(ctx
.push
, NVC0_3D(EDGEFLAG
), 1);
544 if (unlikely(ctx
.need_vertex_id
)) {
545 PUSH_SPACE(ctx
.push
, 4);
546 IMMED_NVC0(ctx
.push
, NVC0_3D(VERTEX_ID_REPLACE
), 0);
547 BEGIN_NVC0(ctx
.push
, NVC0_3D(VERTEX_ATTRIB_FORMAT(1)), 1);
549 NVC0_3D_VERTEX_ATTRIB_FORMAT_CONST
|
550 NVC0_3D_VERTEX_ATTRIB_FORMAT_TYPE_FLOAT
|
551 NVC0_3D_VERTEX_ATTRIB_FORMAT_SIZE_32
);
552 IMMED_NVC0(ctx
.push
, NVC0_3D(VERTEX_ARRAY_FETCH(1)), 0);
556 nouveau_resource_unmap(nv04_resource(nvc0
->idxbuf
.buffer
));
557 for (i
= 0; i
< nvc0
->num_vtxbufs
; ++i
)
558 nouveau_resource_unmap(nv04_resource(nvc0
->vtxbuf
[i
].buffer
));
560 NOUVEAU_DRV_STAT(&nvc0
->screen
->base
, draw_calls_fallback_count
, 1);
564 copy_indices_u8(uint32_t *dst
, const uint8_t *elts
, uint32_t bias
, unsigned n
)
567 for (i
= 0; i
< n
; ++i
)
568 dst
[i
] = elts
[i
] + bias
;
572 copy_indices_u16(uint32_t *dst
, const uint16_t *elts
, uint32_t bias
, unsigned n
)
575 for (i
= 0; i
< n
; ++i
)
576 dst
[i
] = elts
[i
] + bias
;
580 copy_indices_u32(uint32_t *dst
, const uint32_t *elts
, uint32_t bias
, unsigned n
)
583 for (i
= 0; i
< n
; ++i
)
584 dst
[i
] = elts
[i
] + bias
;
588 nvc0_push_upload_vertex_ids(struct push_context
*ctx
,
589 struct nvc0_context
*nvc0
,
590 const struct pipe_draw_info
*info
)
593 struct nouveau_pushbuf
*push
= ctx
->push
;
594 struct nouveau_bo
*bo
;
598 unsigned index_size
= nvc0
->idxbuf
.index_size
;
600 unsigned a
= nvc0
->vertex
->num_elements
;
602 if (!index_size
|| info
->index_bias
)
604 data
= (uint32_t *)nouveau_scratch_get(&nvc0
->base
,
605 info
->count
* index_size
, &va
, &bo
);
607 BCTX_REFN_bo(nvc0
->bufctx_3d
, VTX_TMP
, NOUVEAU_BO_GART
| NOUVEAU_BO_RD
,
609 nouveau_pushbuf_validate(push
);
612 if (!info
->index_bias
) {
613 memcpy(data
, ctx
->idxbuf
, info
->count
* index_size
);
615 switch (nvc0
->idxbuf
.index_size
) {
617 copy_indices_u8(data
, ctx
->idxbuf
, info
->index_bias
, info
->count
);
620 copy_indices_u16(data
, ctx
->idxbuf
, info
->index_bias
, info
->count
);
623 copy_indices_u32(data
, ctx
->idxbuf
, info
->index_bias
, info
->count
);
628 for (i
= 0; i
< info
->count
; ++i
)
629 data
[i
] = i
+ (info
->start
+ info
->index_bias
);
632 format
= (1 << NVC0_3D_VERTEX_ATTRIB_FORMAT_BUFFER__SHIFT
) |
633 NVC0_3D_VERTEX_ATTRIB_FORMAT_TYPE_UINT
;
635 switch (index_size
) {
637 format
|= NVC0_3D_VERTEX_ATTRIB_FORMAT_SIZE_8
;
640 format
|= NVC0_3D_VERTEX_ATTRIB_FORMAT_SIZE_16
;
643 format
|= NVC0_3D_VERTEX_ATTRIB_FORMAT_SIZE_32
;
647 PUSH_SPACE(push
, 12);
649 if (unlikely(nvc0
->state
.instance_elts
& 2)) {
650 nvc0
->state
.instance_elts
&= ~2;
651 IMMED_NVC0(push
, NVC0_3D(VERTEX_ARRAY_PER_INSTANCE(1)), 0);
654 BEGIN_NVC0(push
, NVC0_3D(VERTEX_ATTRIB_FORMAT(a
)), 1);
655 PUSH_DATA (push
, format
);
657 BEGIN_NVC0(push
, NVC0_3D(VERTEX_ARRAY_FETCH(1)), 3);
658 PUSH_DATA (push
, NVC0_3D_VERTEX_ARRAY_FETCH_ENABLE
| index_size
);
659 PUSH_DATAh(push
, va
);
660 PUSH_DATA (push
, va
);
661 BEGIN_NVC0(push
, NVC0_3D(VERTEX_ARRAY_LIMIT_HIGH(1)), 2);
662 PUSH_DATAh(push
, va
+ info
->count
* index_size
- 1);
663 PUSH_DATA (push
, va
+ info
->count
* index_size
- 1);
665 #define NVC0_3D_VERTEX_ID_REPLACE_SOURCE_ATTR_X(a) \
666 (((0x80 + (a) * 0x10) / 4) << NVC0_3D_VERTEX_ID_REPLACE_SOURCE__SHIFT)
668 BEGIN_NVC0(push
, NVC0_3D(VERTEX_ID_REPLACE
), 1);
669 PUSH_DATA (push
, NVC0_3D_VERTEX_ID_REPLACE_SOURCE_ATTR_X(a
) | 1);