1 #include "pipe/p_context.h"
2 #include "pipe/p_state.h"
3 #include "util/u_inlines.h"
4 #include "util/u_format.h"
6 #include "nouveau/nouveau_util.h"
7 #include "nv50_context.h"
8 #include "nv50_resource.h"
11 struct nv50_context
*nv50
;
27 void (*push
)(struct nouveau_channel
*, void *);
33 emit_b32_1(struct nouveau_channel
*chan
, void *data
)
41 emit_b32_2(struct nouveau_channel
*chan
, void *data
)
50 emit_b32_3(struct nouveau_channel
*chan
, void *data
)
60 emit_b32_4(struct nouveau_channel
*chan
, void *data
)
71 emit_b16_1(struct nouveau_channel
*chan
, void *data
)
79 emit_b16_3(struct nouveau_channel
*chan
, void *data
)
83 OUT_RING(chan
, (v
[1] << 16) | v
[0]);
88 emit_b08_1(struct nouveau_channel
*chan
, void *data
)
96 emit_b08_3(struct nouveau_channel
*chan
, void *data
)
100 OUT_RING(chan
, (v
[2] << 16) | (v
[1] << 8) | v
[0]);
104 emit_vertex(struct push_context
*ctx
, unsigned n
)
106 struct nouveau_grobj
*tesla
= ctx
->nv50
->screen
->tesla
;
107 struct nouveau_channel
*chan
= tesla
->channel
;
110 if (ctx
->edgeflag_attr
< 16) {
111 float *edgeflag
= (uint8_t *)ctx
->attr
[ctx
->edgeflag_attr
].map
+
112 ctx
->attr
[ctx
->edgeflag_attr
].stride
* n
;
114 if (*edgeflag
!= ctx
->edgeflag
) {
115 BEGIN_RING(chan
, tesla
, NV50TCL_EDGEFLAG_ENABLE
, 1);
116 OUT_RING (chan
, *edgeflag
? 1 : 0);
117 ctx
->edgeflag
= *edgeflag
;
121 BEGIN_RING_NI(chan
, tesla
, NV50TCL_VERTEX_DATA
, ctx
->vtx_size
);
122 for (i
= 0; i
< ctx
->attr_nr
; i
++)
123 ctx
->attr
[i
].push(chan
,
124 (uint8_t *)ctx
->attr
[i
].map
+ ctx
->attr
[i
].stride
* n
);
128 emit_edgeflag(void *priv
, boolean enabled
)
130 struct push_context
*ctx
= priv
;
131 struct nouveau_grobj
*tesla
= ctx
->nv50
->screen
->tesla
;
132 struct nouveau_channel
*chan
= tesla
->channel
;
134 BEGIN_RING(chan
, tesla
, NV50TCL_EDGEFLAG_ENABLE
, 1);
135 OUT_RING (chan
, enabled
? 1 : 0);
139 emit_elt08(void *priv
, unsigned start
, unsigned count
)
141 struct push_context
*ctx
= priv
;
142 uint8_t *idxbuf
= ctx
->idxbuf
;
145 emit_vertex(ctx
, idxbuf
[start
++]);
149 emit_elt08_biased(void *priv
, unsigned start
, unsigned count
)
151 struct push_context
*ctx
= priv
;
152 uint8_t *idxbuf
= ctx
->idxbuf
;
155 emit_vertex(ctx
, idxbuf
[start
++] + ctx
->idxbias
);
159 emit_elt16(void *priv
, unsigned start
, unsigned count
)
161 struct push_context
*ctx
= priv
;
162 uint16_t *idxbuf
= ctx
->idxbuf
;
165 emit_vertex(ctx
, idxbuf
[start
++]);
169 emit_elt16_biased(void *priv
, unsigned start
, unsigned count
)
171 struct push_context
*ctx
= priv
;
172 uint16_t *idxbuf
= ctx
->idxbuf
;
175 emit_vertex(ctx
, idxbuf
[start
++] + ctx
->idxbias
);
179 emit_elt32(void *priv
, unsigned start
, unsigned count
)
181 struct push_context
*ctx
= priv
;
182 uint32_t *idxbuf
= ctx
->idxbuf
;
185 emit_vertex(ctx
, idxbuf
[start
++]);
189 emit_elt32_biased(void *priv
, unsigned start
, unsigned count
)
191 struct push_context
*ctx
= priv
;
192 uint32_t *idxbuf
= ctx
->idxbuf
;
195 emit_vertex(ctx
, idxbuf
[start
++] + ctx
->idxbias
);
199 emit_verts(void *priv
, unsigned start
, unsigned count
)
202 emit_vertex(priv
, start
++);
206 nv50_push_elements_instanced(struct pipe_context
*pipe
,
207 struct pipe_resource
*idxbuf
,
208 unsigned idxsize
, int idxbias
,
209 unsigned mode
, unsigned start
, unsigned count
,
210 unsigned i_start
, unsigned i_count
)
212 struct nv50_context
*nv50
= nv50_context(pipe
);
213 struct nouveau_grobj
*tesla
= nv50
->screen
->tesla
;
214 struct nouveau_channel
*chan
= tesla
->channel
;
215 struct push_context ctx
;
216 const unsigned p_overhead
= 4 + /* begin/end */
217 4; /* potential edgeflag enable/disable */
218 const unsigned v_overhead
= 1 + /* VERTEX_DATA packet header */
219 2; /* potential edgeflag modification */
220 struct u_split_prim s
;
230 ctx
.edgeflag_attr
= nv50
->vertprog
->cfg
.edgeflag_in
;
232 /* map vertex buffers, determine vertex size */
233 for (i
= 0; i
< nv50
->vtxelt
->num_elements
; i
++) {
234 struct pipe_vertex_element
*ve
= &nv50
->vtxelt
->pipe
[i
];
235 struct pipe_vertex_buffer
*vb
= &nv50
->vtxbuf
[ve
->vertex_buffer_index
];
236 struct nouveau_bo
*bo
= nv50_resource(vb
->buffer
)->bo
;
237 unsigned size
, nr_components
, n
;
239 if (!(nv50
->vbo_fifo
& (1 << i
)))
243 if (nouveau_bo_map(bo
, NOUVEAU_BO_RD
)) {
247 ctx
.attr
[n
].map
= (uint8_t *)bo
->map
+ vb
->buffer_offset
+ ve
->src_offset
;
248 nouveau_bo_unmap(bo
);
250 ctx
.attr
[n
].stride
= vb
->stride
;
251 ctx
.attr
[n
].divisor
= ve
->instance_divisor
;
252 if (ctx
.attr
[n
].divisor
) {
253 ctx
.attr
[n
].step
= i_start
% ve
->instance_divisor
;
254 ctx
.attr
[n
].map
= (uint8_t *)ctx
.attr
[n
].map
+ i_start
* vb
->stride
;
257 size
= util_format_get_component_bits(ve
->src_format
,
258 UTIL_FORMAT_COLORSPACE_RGB
, 0);
259 nr_components
= util_format_get_nr_components(ve
->src_format
);
262 switch (nr_components
) {
263 case 1: ctx
.attr
[n
].push
= emit_b08_1
; break;
264 case 2: ctx
.attr
[n
].push
= emit_b16_1
; break;
265 case 3: ctx
.attr
[n
].push
= emit_b08_3
; break;
266 case 4: ctx
.attr
[n
].push
= emit_b32_1
; break;
271 switch (nr_components
) {
272 case 1: ctx
.attr
[n
].push
= emit_b16_1
; break;
273 case 2: ctx
.attr
[n
].push
= emit_b32_1
; break;
274 case 3: ctx
.attr
[n
].push
= emit_b16_3
; break;
275 case 4: ctx
.attr
[n
].push
= emit_b32_2
; break;
277 ctx
.vtx_size
+= (nr_components
+ 1) >> 1;
280 switch (nr_components
) {
281 case 1: ctx
.attr
[n
].push
= emit_b32_1
; break;
282 case 2: ctx
.attr
[n
].push
= emit_b32_2
; break;
283 case 3: ctx
.attr
[n
].push
= emit_b32_3
; break;
284 case 4: ctx
.attr
[n
].push
= emit_b32_4
; break;
286 ctx
.vtx_size
+= nr_components
;
293 vtx_size
= ctx
.vtx_size
+ v_overhead
;
295 /* map index buffer, if present */
297 struct nouveau_bo
*bo
= nv50_resource(idxbuf
)->bo
;
299 if (nouveau_bo_map(bo
, NOUVEAU_BO_RD
)) {
303 ctx
.idxbuf
= bo
->map
;
304 ctx
.idxbias
= idxbias
;
305 ctx
.idxsize
= idxsize
;
306 nouveau_bo_unmap(bo
);
310 s
.edge
= emit_edgeflag
;
313 s
.emit
= idxbias
? emit_elt08_biased
: emit_elt08
;
316 s
.emit
= idxbias
? emit_elt16_biased
: emit_elt16
;
318 s
.emit
= idxbias
? emit_elt32_biased
: emit_elt32
;
322 /* per-instance loop */
323 BEGIN_RING(chan
, tesla
, NV50TCL_CB_ADDR
, 2);
324 OUT_RING (chan
, NV50_CB_AUX
| (24 << 8));
325 OUT_RING (chan
, i_start
);
330 for (i
= 0; i
< ctx
.attr_nr
; i
++) {
331 if (!ctx
.attr
[i
].divisor
||
332 ctx
.attr
[i
].divisor
!= ++ctx
.attr
[i
].step
)
334 ctx
.attr
[i
].step
= 0;
335 ctx
.attr
[i
].map
= (uint8_t *)ctx
.attr
[i
].map
+ ctx
.attr
[i
].stride
;
338 u_split_prim_init(&s
, mode
, start
, count
);
340 if (AVAIL_RING(chan
) < p_overhead
+ (6 * vtx_size
)) {
342 if (!nv50_state_validate(nv50
, p_overhead
+ (6 * vtx_size
))) {
348 max_verts
= AVAIL_RING(chan
);
349 max_verts
-= p_overhead
;
350 max_verts
/= vtx_size
;
352 BEGIN_RING(chan
, tesla
, NV50TCL_VERTEX_BEGIN
, 1);
353 OUT_RING (chan
, nv50_prim(s
.mode
) | (nzi
? (1 << 28) : 0));
354 done
= u_split_prim_next(&s
, max_verts
);
355 BEGIN_RING(chan
, tesla
, NV50TCL_VERTEX_END
, 1);