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_context.h"
9 #include "nvc0_resource.h"
11 #include "nvc0_3d.xml.h"
14 struct nouveau_channel
*chan
;
18 uint32_t vertex_words
;
19 uint32_t packet_vertex_limit
;
21 struct translate
*translate
;
23 boolean primitive_restart
;
25 uint32_t restart_index
;
38 init_push_context(struct nvc0_context
*nvc0
, struct push_context
*ctx
)
40 struct pipe_vertex_element
*ve
;
42 ctx
->chan
= nvc0
->screen
->base
.channel
;
43 ctx
->translate
= nvc0
->vertex
->translate
;
45 ctx
->edgeflag
.value
= 0.5f
;
47 if (NVC0_USING_EDGEFLAG(nvc0
)) {
48 ve
= &nvc0
->vertex
->element
[nvc0
->vertprog
->vp
.edgeflag
].pipe
;
50 ctx
->edgeflag
.buffer
= ve
->vertex_buffer_index
;
51 ctx
->edgeflag
.offset
= ve
->src_offset
;
53 ctx
->packet_vertex_limit
= 1;
55 ctx
->edgeflag
.buffer
= -1;
56 ctx
->edgeflag
.offset
= 0;
57 ctx
->edgeflag
.stride
= 0;
58 ctx
->edgeflag
.data
= NULL
;
60 ctx
->packet_vertex_limit
= nvc0
->vertex
->vtx_per_packet_max
;
63 ctx
->vertex_words
= nvc0
->vertex
->vtx_size
;
67 set_edgeflag(struct push_context
*ctx
, unsigned vtx_id
)
69 float f
= *(float *)(ctx
->edgeflag
.data
+ vtx_id
* ctx
->edgeflag
.stride
);
71 if (ctx
->edgeflag
.value
!= f
) {
72 ctx
->edgeflag
.value
= f
;
73 IMMED_RING(ctx
->chan
, RING_3D(EDGEFLAG_ENABLE
), f
? 1 : 0);
77 static INLINE
unsigned
78 prim_restart_search_i08(uint8_t *elts
, unsigned push
, uint8_t index
)
81 for (i
= 0; i
< push
; ++i
)
87 static INLINE
unsigned
88 prim_restart_search_i16(uint16_t *elts
, unsigned push
, uint16_t index
)
91 for (i
= 0; i
< push
; ++i
)
97 static INLINE
unsigned
98 prim_restart_search_i32(uint32_t *elts
, unsigned push
, uint32_t index
)
101 for (i
= 0; i
< push
; ++i
)
102 if (elts
[i
] == index
)
108 emit_vertices_i08(struct push_context
*ctx
, unsigned start
, unsigned count
)
110 uint8_t *restrict elts
= (uint8_t *)ctx
->idxbuf
+ start
;
113 unsigned push
= MIN2(count
, ctx
->packet_vertex_limit
);
117 if (ctx
->primitive_restart
)
118 nr
= prim_restart_search_i08(elts
, push
, ctx
->restart_index
);
120 if (unlikely(ctx
->edgeflag
.buffer
>= 0) && nr
)
121 set_edgeflag(ctx
, elts
[0]);
123 size
= ctx
->vertex_words
* nr
;
125 BEGIN_RING_NI(ctx
->chan
, RING_3D(VERTEX_DATA
), size
);
127 ctx
->translate
->run_elts8(ctx
->translate
, elts
, nr
, ctx
->instance_id
,
130 ctx
->chan
->cur
+= size
;
137 BEGIN_RING(ctx
->chan
, RING_3D(VERTEX_END_GL
), 2);
138 OUT_RING (ctx
->chan
, 0);
139 OUT_RING (ctx
->chan
, NVC0_3D_VERTEX_BEGIN_GL_INSTANCE_CONT
|
140 (ctx
->prim
& ~NVC0_3D_VERTEX_BEGIN_GL_INSTANCE_NEXT
));
146 emit_vertices_i16(struct push_context
*ctx
, unsigned start
, unsigned count
)
148 uint16_t *restrict elts
= (uint16_t *)ctx
->idxbuf
+ start
;
151 unsigned push
= MIN2(count
, ctx
->packet_vertex_limit
);
155 if (ctx
->primitive_restart
)
156 nr
= prim_restart_search_i16(elts
, push
, ctx
->restart_index
);
158 if (unlikely(ctx
->edgeflag
.buffer
>= 0) && nr
)
159 set_edgeflag(ctx
, elts
[0]);
161 size
= ctx
->vertex_words
* nr
;
163 BEGIN_RING_NI(ctx
->chan
, RING_3D(VERTEX_DATA
), size
);
165 ctx
->translate
->run_elts16(ctx
->translate
, elts
, nr
, ctx
->instance_id
,
168 ctx
->chan
->cur
+= size
;
175 BEGIN_RING(ctx
->chan
, RING_3D(VERTEX_END_GL
), 2);
176 OUT_RING (ctx
->chan
, 0);
177 OUT_RING (ctx
->chan
, NVC0_3D_VERTEX_BEGIN_GL_INSTANCE_CONT
|
178 (ctx
->prim
& ~NVC0_3D_VERTEX_BEGIN_GL_INSTANCE_NEXT
));
184 emit_vertices_i32(struct push_context
*ctx
, unsigned start
, unsigned count
)
186 uint32_t *restrict elts
= (uint32_t *)ctx
->idxbuf
+ start
;
189 unsigned push
= MIN2(count
, ctx
->packet_vertex_limit
);
193 if (ctx
->primitive_restart
)
194 nr
= prim_restart_search_i32(elts
, push
, ctx
->restart_index
);
196 if (unlikely(ctx
->edgeflag
.buffer
>= 0) && nr
)
197 set_edgeflag(ctx
, elts
[0]);
199 size
= ctx
->vertex_words
* nr
;
201 BEGIN_RING_NI(ctx
->chan
, RING_3D(VERTEX_DATA
), size
);
203 ctx
->translate
->run_elts(ctx
->translate
, elts
, nr
, ctx
->instance_id
,
206 ctx
->chan
->cur
+= size
;
213 BEGIN_RING(ctx
->chan
, RING_3D(VERTEX_END_GL
), 2);
214 OUT_RING (ctx
->chan
, 0);
215 OUT_RING (ctx
->chan
, NVC0_3D_VERTEX_BEGIN_GL_INSTANCE_CONT
|
216 (ctx
->prim
& ~NVC0_3D_VERTEX_BEGIN_GL_INSTANCE_NEXT
));
222 emit_vertices_seq(struct push_context
*ctx
, unsigned start
, unsigned count
)
225 unsigned push
= MIN2(count
, ctx
->packet_vertex_limit
);
226 unsigned size
= ctx
->vertex_words
* push
;
228 if (unlikely(ctx
->edgeflag
.buffer
>= 0))
229 set_edgeflag(ctx
, start
);
231 BEGIN_RING_NI(ctx
->chan
, RING_3D(VERTEX_DATA
), size
);
233 ctx
->translate
->run(ctx
->translate
, start
, push
, ctx
->instance_id
,
235 ctx
->chan
->cur
+= size
;
242 #define NVC0_PRIM_GL_CASE(n) \
243 case PIPE_PRIM_##n: return NVC0_3D_VERTEX_BEGIN_GL_PRIMITIVE_##n
245 static INLINE
unsigned
246 nvc0_prim_gl(unsigned prim
)
249 NVC0_PRIM_GL_CASE(POINTS
);
250 NVC0_PRIM_GL_CASE(LINES
);
251 NVC0_PRIM_GL_CASE(LINE_LOOP
);
252 NVC0_PRIM_GL_CASE(LINE_STRIP
);
253 NVC0_PRIM_GL_CASE(TRIANGLES
);
254 NVC0_PRIM_GL_CASE(TRIANGLE_STRIP
);
255 NVC0_PRIM_GL_CASE(TRIANGLE_FAN
);
256 NVC0_PRIM_GL_CASE(QUADS
);
257 NVC0_PRIM_GL_CASE(QUAD_STRIP
);
258 NVC0_PRIM_GL_CASE(POLYGON
);
259 NVC0_PRIM_GL_CASE(LINES_ADJACENCY
);
260 NVC0_PRIM_GL_CASE(LINE_STRIP_ADJACENCY
);
261 NVC0_PRIM_GL_CASE(TRIANGLES_ADJACENCY
);
262 NVC0_PRIM_GL_CASE(TRIANGLE_STRIP_ADJACENCY
);
264 NVC0_PRIM_GL_CASE(PATCHES); */
266 return NVC0_3D_VERTEX_BEGIN_GL_PRIMITIVE_POINTS
;
272 nvc0_push_vbo(struct nvc0_context
*nvc0
, const struct pipe_draw_info
*info
)
274 struct push_context ctx
;
275 unsigned i
, index_size
;
276 unsigned inst
= info
->instance_count
;
277 boolean apply_bias
= info
->indexed
&& info
->index_bias
;
279 init_push_context(nvc0
, &ctx
);
281 for (i
= 0; i
< nvc0
->num_vtxbufs
; ++i
) {
283 struct pipe_vertex_buffer
*vb
= &nvc0
->vtxbuf
[i
];
284 struct nv04_resource
*res
= nv04_resource(vb
->buffer
);
286 data
= nouveau_resource_map_offset(&nvc0
->base
, res
,
287 vb
->buffer_offset
, NOUVEAU_BO_RD
);
289 if (apply_bias
&& likely(!(nvc0
->vertex
->instance_bufs
& (1 << i
))))
290 data
+= info
->index_bias
* vb
->stride
;
292 ctx
.translate
->set_buffer(ctx
.translate
, i
, data
, vb
->stride
, ~0);
294 if (unlikely(i
== ctx
.edgeflag
.buffer
)) {
295 ctx
.edgeflag
.data
= data
+ ctx
.edgeflag
.offset
;
296 ctx
.edgeflag
.stride
= vb
->stride
;
302 nouveau_resource_map_offset(&nvc0
->base
,
303 nv04_resource(nvc0
->idxbuf
.buffer
),
304 nvc0
->idxbuf
.offset
, NOUVEAU_BO_RD
);
307 index_size
= nvc0
->idxbuf
.index_size
;
308 ctx
.primitive_restart
= info
->primitive_restart
;
309 ctx
.restart_index
= info
->restart_index
;
313 ctx
.primitive_restart
= FALSE
;
314 ctx
.restart_index
= 0;
317 ctx
.instance_id
= info
->start_instance
;
318 ctx
.prim
= nvc0_prim_gl(info
->mode
);
321 BEGIN_RING(ctx
.chan
, RING_3D(VERTEX_BEGIN_GL
), 1);
322 OUT_RING (ctx
.chan
, ctx
.prim
);
323 switch (index_size
) {
325 emit_vertices_seq(&ctx
, info
->start
, info
->count
);
328 emit_vertices_i08(&ctx
, info
->start
, info
->count
);
331 emit_vertices_i16(&ctx
, info
->start
, info
->count
);
334 emit_vertices_i32(&ctx
, info
->start
, info
->count
);
340 IMMED_RING(ctx
.chan
, RING_3D(VERTEX_END_GL
), 0);
343 ctx
.prim
|= NVC0_3D_VERTEX_BEGIN_GL_INSTANCE_NEXT
;
346 if (unlikely(ctx
.edgeflag
.value
== 0.0f
))
347 IMMED_RING(ctx
.chan
, RING_3D(EDGEFLAG_ENABLE
), 1);
350 nouveau_resource_unmap(nv04_resource(nvc0
->idxbuf
.buffer
));
352 for (i
= 0; i
< nvc0
->num_vtxbufs
; ++i
)
353 nouveau_resource_unmap(nv04_resource(nvc0
->vtxbuf
[i
].buffer
));