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
);
430 NVC0_PRIM_GL_CASE(PATCHES
);
432 return NVC0_3D_VERTEX_BEGIN_GL_PRIMITIVE_POINTS
;
437 nvc0_push_vbo(struct nvc0_context
*nvc0
, const struct pipe_draw_info
*info
)
439 struct push_context ctx
;
440 unsigned i
, index_size
;
441 unsigned inst_count
= info
->instance_count
;
442 unsigned vert_count
= info
->count
;
445 nvc0_push_context_init(nvc0
, &ctx
);
447 nvc0_vertex_configure_translate(nvc0
, info
->index_bias
);
449 if (nvc0
->state
.index_bias
) {
450 /* this is already taken care of by translate */
451 IMMED_NVC0(ctx
.push
, NVC0_3D(VB_ELEMENT_BASE
), 0);
452 nvc0
->state
.index_bias
= 0;
455 if (unlikely(ctx
.edgeflag
.enabled
))
456 nvc0_push_map_edgeflag(&ctx
, nvc0
, info
->index_bias
);
458 ctx
.prim_restart
= info
->primitive_restart
;
459 ctx
.restart_index
= info
->restart_index
;
461 if (info
->primitive_restart
) {
462 /* NOTE: I hope we won't ever need that last index (~0).
463 * If we do, we have to disable primitive restart here always and
464 * use END,BEGIN to restart. (XXX: would that affect PrimitiveID ?)
465 * We could also deactive PRIM_RESTART_WITH_DRAW_ARRAYS temporarily,
466 * and add manual restart to disp_vertices_seq.
468 BEGIN_NVC0(ctx
.push
, NVC0_3D(PRIM_RESTART_ENABLE
), 2);
469 PUSH_DATA (ctx
.push
, 1);
470 PUSH_DATA (ctx
.push
, info
->indexed
? 0xffffffff : info
->restart_index
);
472 if (nvc0
->state
.prim_restart
) {
473 IMMED_NVC0(ctx
.push
, NVC0_3D(PRIM_RESTART_ENABLE
), 0);
475 nvc0
->state
.prim_restart
= info
->primitive_restart
;
478 nvc0_push_map_idxbuf(&ctx
, nvc0
);
479 index_size
= nvc0
->idxbuf
.index_size
;
481 if (unlikely(info
->count_from_stream_output
)) {
482 struct pipe_context
*pipe
= &nvc0
->base
.pipe
;
483 struct nvc0_so_target
*targ
;
484 targ
= nvc0_so_target(info
->count_from_stream_output
);
485 pipe
->get_query_result(pipe
, targ
->pq
, true, (void *)&vert_count
);
486 vert_count
/= targ
->stride
;
488 ctx
.idxbuf
= NULL
; /* shut up warnings */
492 ctx
.instance_id
= info
->start_instance
;
494 prim
= nvc0_prim_gl(info
->mode
);
496 PUSH_SPACE(ctx
.push
, 9);
498 ctx
.dest
= nvc0_push_setup_vertex_array(nvc0
, vert_count
);
499 if (unlikely(!ctx
.dest
))
502 if (unlikely(ctx
.need_vertex_id
))
503 nvc0_push_upload_vertex_ids(&ctx
, nvc0
, info
);
505 if (nvc0
->screen
->eng3d
->oclass
< GM107_3D_CLASS
)
506 IMMED_NVC0(ctx
.push
, NVC0_3D(VERTEX_ARRAY_FLUSH
), 0);
507 BEGIN_NVC0(ctx
.push
, NVC0_3D(VERTEX_BEGIN_GL
), 1);
508 PUSH_DATA (ctx
.push
, prim
);
509 switch (index_size
) {
511 disp_vertices_i08(&ctx
, info
->start
, vert_count
);
514 disp_vertices_i16(&ctx
, info
->start
, vert_count
);
517 disp_vertices_i32(&ctx
, info
->start
, vert_count
);
520 assert(index_size
== 0);
521 disp_vertices_seq(&ctx
, info
->start
, vert_count
);
524 PUSH_SPACE(ctx
.push
, 1);
525 IMMED_NVC0(ctx
.push
, NVC0_3D(VERTEX_END_GL
), 0);
528 prim
|= NVC0_3D_VERTEX_BEGIN_GL_INSTANCE_NEXT
;
531 nouveau_bufctx_reset(nvc0
->bufctx_3d
, NVC0_BIND_VTX_TMP
);
532 nouveau_scratch_done(&nvc0
->base
);
533 } while (inst_count
);
536 /* reset state and unmap buffers (no-op) */
538 if (unlikely(!ctx
.edgeflag
.value
)) {
539 PUSH_SPACE(ctx
.push
, 1);
540 IMMED_NVC0(ctx
.push
, NVC0_3D(EDGEFLAG
), 1);
543 if (unlikely(ctx
.need_vertex_id
)) {
544 PUSH_SPACE(ctx
.push
, 4);
545 IMMED_NVC0(ctx
.push
, NVC0_3D(VERTEX_ID_REPLACE
), 0);
546 BEGIN_NVC0(ctx
.push
, NVC0_3D(VERTEX_ATTRIB_FORMAT(1)), 1);
548 NVC0_3D_VERTEX_ATTRIB_FORMAT_CONST
|
549 NVC0_3D_VERTEX_ATTRIB_FORMAT_TYPE_FLOAT
|
550 NVC0_3D_VERTEX_ATTRIB_FORMAT_SIZE_32
);
551 IMMED_NVC0(ctx
.push
, NVC0_3D(VERTEX_ARRAY_FETCH(1)), 0);
555 nouveau_resource_unmap(nv04_resource(nvc0
->idxbuf
.buffer
));
556 for (i
= 0; i
< nvc0
->num_vtxbufs
; ++i
)
557 nouveau_resource_unmap(nv04_resource(nvc0
->vtxbuf
[i
].buffer
));
559 NOUVEAU_DRV_STAT(&nvc0
->screen
->base
, draw_calls_fallback_count
, 1);
563 copy_indices_u8(uint32_t *dst
, const uint8_t *elts
, uint32_t bias
, unsigned n
)
566 for (i
= 0; i
< n
; ++i
)
567 dst
[i
] = elts
[i
] + bias
;
571 copy_indices_u16(uint32_t *dst
, const uint16_t *elts
, uint32_t bias
, unsigned n
)
574 for (i
= 0; i
< n
; ++i
)
575 dst
[i
] = elts
[i
] + bias
;
579 copy_indices_u32(uint32_t *dst
, const uint32_t *elts
, uint32_t bias
, unsigned n
)
582 for (i
= 0; i
< n
; ++i
)
583 dst
[i
] = elts
[i
] + bias
;
587 nvc0_push_upload_vertex_ids(struct push_context
*ctx
,
588 struct nvc0_context
*nvc0
,
589 const struct pipe_draw_info
*info
)
592 struct nouveau_pushbuf
*push
= ctx
->push
;
593 struct nouveau_bo
*bo
;
597 unsigned index_size
= nvc0
->idxbuf
.index_size
;
599 unsigned a
= nvc0
->vertex
->num_elements
;
601 if (!index_size
|| info
->index_bias
)
603 data
= (uint32_t *)nouveau_scratch_get(&nvc0
->base
,
604 info
->count
* index_size
, &va
, &bo
);
606 BCTX_REFN_bo(nvc0
->bufctx_3d
, VTX_TMP
, NOUVEAU_BO_GART
| NOUVEAU_BO_RD
,
608 nouveau_pushbuf_validate(push
);
611 if (!info
->index_bias
) {
612 memcpy(data
, ctx
->idxbuf
, info
->count
* index_size
);
614 switch (nvc0
->idxbuf
.index_size
) {
616 copy_indices_u8(data
, ctx
->idxbuf
, info
->index_bias
, info
->count
);
619 copy_indices_u16(data
, ctx
->idxbuf
, info
->index_bias
, info
->count
);
622 copy_indices_u32(data
, ctx
->idxbuf
, info
->index_bias
, info
->count
);
627 for (i
= 0; i
< info
->count
; ++i
)
628 data
[i
] = i
+ (info
->start
+ info
->index_bias
);
631 format
= (1 << NVC0_3D_VERTEX_ATTRIB_FORMAT_BUFFER__SHIFT
) |
632 NVC0_3D_VERTEX_ATTRIB_FORMAT_TYPE_UINT
;
634 switch (index_size
) {
636 format
|= NVC0_3D_VERTEX_ATTRIB_FORMAT_SIZE_8
;
639 format
|= NVC0_3D_VERTEX_ATTRIB_FORMAT_SIZE_16
;
642 format
|= NVC0_3D_VERTEX_ATTRIB_FORMAT_SIZE_32
;
646 PUSH_SPACE(push
, 12);
648 if (unlikely(nvc0
->state
.instance_elts
& 2)) {
649 nvc0
->state
.instance_elts
&= ~2;
650 IMMED_NVC0(push
, NVC0_3D(VERTEX_ARRAY_PER_INSTANCE(1)), 0);
653 BEGIN_NVC0(push
, NVC0_3D(VERTEX_ATTRIB_FORMAT(a
)), 1);
654 PUSH_DATA (push
, format
);
656 BEGIN_NVC0(push
, NVC0_3D(VERTEX_ARRAY_FETCH(1)), 3);
657 PUSH_DATA (push
, NVC0_3D_VERTEX_ARRAY_FETCH_ENABLE
| index_size
);
658 PUSH_DATAh(push
, va
);
659 PUSH_DATA (push
, va
);
660 BEGIN_NVC0(push
, NVC0_3D(VERTEX_ARRAY_LIMIT_HIGH(1)), 2);
661 PUSH_DATAh(push
, va
+ info
->count
* index_size
- 1);
662 PUSH_DATA (push
, va
+ info
->count
* index_size
- 1);
664 #define NVC0_3D_VERTEX_ID_REPLACE_SOURCE_ATTR_X(a) \
665 (((0x80 + (a) * 0x10) / 4) << NVC0_3D_VERTEX_ID_REPLACE_SOURCE__SHIFT)
667 BEGIN_NVC0(push
, NVC0_3D(VERTEX_ID_REPLACE
), 1);
668 PUSH_DATA (push
, NVC0_3D_VERTEX_ID_REPLACE_SOURCE_ATTR_X(a
) | 1);