2 * Copyright 2010 Christoph Bumiller
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17 * THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
18 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF
19 * OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23 #include "pipe/p_context.h"
24 #include "pipe/p_state.h"
25 #include "util/u_inlines.h"
26 #include "util/u_format.h"
27 #include "translate/translate.h"
29 #include "nv50_context.h"
30 #include "nv50_resource.h"
32 #include "nv50_3d.xml.h"
35 nv50_vertex_state_delete(struct pipe_context
*pipe
,
38 struct nv50_vertex_stateobj
*so
= hwcso
;
41 so
->translate
->release(so
->translate
);
46 nv50_vertex_state_create(struct pipe_context
*pipe
,
47 unsigned num_elements
,
48 const struct pipe_vertex_element
*elements
)
50 struct nv50_vertex_stateobj
*so
;
51 struct translate_key transkey
;
54 so
= MALLOC(sizeof(*so
) +
55 num_elements
* sizeof(struct nv50_vertex_element
));
58 so
->num_elements
= num_elements
;
59 so
->instance_elts
= 0;
60 so
->instance_bufs
= 0;
61 so
->need_conversion
= FALSE
;
63 memset(so
->vb_access_size
, 0, sizeof(so
->vb_access_size
));
65 for (i
= 0; i
< PIPE_MAX_ATTRIBS
; ++i
)
66 so
->min_instance_div
[i
] = 0xffffffff;
68 transkey
.nr_elements
= 0;
69 transkey
.output_stride
= 0;
71 for (i
= 0; i
< num_elements
; ++i
) {
72 const struct pipe_vertex_element
*ve
= &elements
[i
];
73 const unsigned vbi
= ve
->vertex_buffer_index
;
75 enum pipe_format fmt
= ve
->src_format
;
77 so
->element
[i
].pipe
= elements
[i
];
78 so
->element
[i
].state
= nv50_format_table
[fmt
].vtx
;
80 if (!so
->element
[i
].state
) {
81 switch (util_format_get_nr_components(fmt
)) {
82 case 1: fmt
= PIPE_FORMAT_R32_FLOAT
; break;
83 case 2: fmt
= PIPE_FORMAT_R32G32_FLOAT
; break;
84 case 3: fmt
= PIPE_FORMAT_R32G32B32_FLOAT
; break;
85 case 4: fmt
= PIPE_FORMAT_R32G32B32A32_FLOAT
; break;
91 so
->element
[i
].state
= nv50_format_table
[fmt
].vtx
;
92 so
->need_conversion
= TRUE
;
94 so
->element
[i
].state
|= i
;
96 size
= util_format_get_blocksize(fmt
);
97 if (so
->vb_access_size
[vbi
] < (ve
->src_offset
+ size
))
98 so
->vb_access_size
[vbi
] = ve
->src_offset
+ size
;
101 unsigned j
= transkey
.nr_elements
++;
103 transkey
.element
[j
].type
= TRANSLATE_ELEMENT_NORMAL
;
104 transkey
.element
[j
].input_format
= ve
->src_format
;
105 transkey
.element
[j
].input_buffer
= vbi
;
106 transkey
.element
[j
].input_offset
= ve
->src_offset
;
107 transkey
.element
[j
].instance_divisor
= ve
->instance_divisor
;
109 transkey
.element
[j
].output_format
= fmt
;
110 transkey
.element
[j
].output_offset
= transkey
.output_stride
;
111 transkey
.output_stride
+= (util_format_get_stride(fmt
, 1) + 3) & ~3;
113 if (unlikely(ve
->instance_divisor
)) {
114 so
->instance_elts
|= 1 << i
;
115 so
->instance_bufs
|= 1 << vbi
;
116 if (ve
->instance_divisor
< so
->min_instance_div
[vbi
])
117 so
->min_instance_div
[vbi
] = ve
->instance_divisor
;
122 so
->translate
= translate_create(&transkey
);
123 so
->vertex_size
= transkey
.output_stride
/ 4;
124 so
->packet_vertex_limit
= NV04_PFIFO_MAX_PACKET_LEN
/
125 MAX2(so
->vertex_size
, 1);
130 #define NV50_3D_VERTEX_ATTRIB_INACTIVE \
131 NV50_3D_VERTEX_ARRAY_ATTRIB_TYPE_FLOAT | \
132 NV50_3D_VERTEX_ARRAY_ATTRIB_FORMAT_32_32_32_32 | \
133 NV50_3D_VERTEX_ARRAY_ATTRIB_CONST
136 nv50_emit_vtxattr(struct nv50_context
*nv50
, struct pipe_vertex_buffer
*vb
,
137 struct pipe_vertex_element
*ve
, unsigned attr
)
139 struct nouveau_pushbuf
*push
= nv50
->base
.pushbuf
;
140 const void *data
= (const uint8_t *)vb
->user_buffer
+ ve
->src_offset
;
142 const unsigned nc
= util_format_get_nr_components(ve
->src_format
);
144 assert(vb
->user_buffer
);
146 util_format_read_4f(ve
->src_format
, v
, 0, data
, 0, 0, 0, 1, 1);
150 BEGIN_NV04(push
, NV50_3D(VTX_ATTR_4F_X(attr
)), 4);
151 PUSH_DATAf(push
, v
[0]);
152 PUSH_DATAf(push
, v
[1]);
153 PUSH_DATAf(push
, v
[2]);
154 PUSH_DATAf(push
, v
[3]);
157 BEGIN_NV04(push
, NV50_3D(VTX_ATTR_3F_X(attr
)), 3);
158 PUSH_DATAf(push
, v
[0]);
159 PUSH_DATAf(push
, v
[1]);
160 PUSH_DATAf(push
, v
[2]);
163 BEGIN_NV04(push
, NV50_3D(VTX_ATTR_2F_X(attr
)), 2);
164 PUSH_DATAf(push
, v
[0]);
165 PUSH_DATAf(push
, v
[1]);
168 if (attr
== nv50
->vertprog
->vp
.edgeflag
) {
169 BEGIN_NV04(push
, NV50_3D(EDGEFLAG
), 1);
170 PUSH_DATA (push
, v
[0] ? 1 : 0);
172 BEGIN_NV04(push
, NV50_3D(VTX_ATTR_1F(attr
)), 1);
173 PUSH_DATAf(push
, v
[0]);
182 nv50_user_vbuf_range(struct nv50_context
*nv50
, int vbi
,
183 uint32_t *base
, uint32_t *size
)
185 if (unlikely(nv50
->vertex
->instance_bufs
& (1 << vbi
))) {
186 /* TODO: use min and max instance divisor to get a proper range */
188 *size
= nv50
->vtxbuf
[vbi
].buffer
->width0
;
190 /* NOTE: if there are user buffers, we *must* have index bounds */
191 assert(nv50
->vb_elt_limit
!= ~0);
192 *base
= nv50
->vb_elt_first
* nv50
->vtxbuf
[vbi
].stride
;
193 *size
= nv50
->vb_elt_limit
* nv50
->vtxbuf
[vbi
].stride
+
194 nv50
->vertex
->vb_access_size
[vbi
];
199 nv50_upload_user_buffers(struct nv50_context
*nv50
,
200 uint64_t addrs
[], uint32_t limits
[])
204 for (b
= 0; b
< nv50
->num_vtxbufs
; ++b
) {
205 struct nouveau_bo
*bo
;
206 const struct pipe_vertex_buffer
*vb
= &nv50
->vtxbuf
[b
];
209 if (!(nv50
->vbo_user
& (1 << b
)) || !vb
->stride
)
211 nv50_user_vbuf_range(nv50
, b
, &base
, &size
);
213 limits
[b
] = base
+ size
- 1;
214 addrs
[b
] = nouveau_scratch_data(&nv50
->base
, vb
->user_buffer
, base
, size
,
217 BCTX_REFN_bo(nv50
->bufctx_3d
, VERTEX_TMP
, NOUVEAU_BO_GART
|
220 nv50
->base
.vbo_dirty
= TRUE
;
224 nv50_update_user_vbufs(struct nv50_context
*nv50
)
226 uint64_t address
[PIPE_MAX_ATTRIBS
];
227 struct nouveau_pushbuf
*push
= nv50
->base
.pushbuf
;
229 uint32_t written
= 0;
231 for (i
= 0; i
< nv50
->vertex
->num_elements
; ++i
) {
232 struct pipe_vertex_element
*ve
= &nv50
->vertex
->element
[i
].pipe
;
233 const unsigned b
= ve
->vertex_buffer_index
;
234 struct pipe_vertex_buffer
*vb
= &nv50
->vtxbuf
[b
];
237 if (!(nv50
->vbo_user
& (1 << b
)))
241 nv50_emit_vtxattr(nv50
, vb
, ve
, i
);
244 nv50_user_vbuf_range(nv50
, b
, &base
, &size
);
246 if (!(written
& (1 << b
))) {
247 struct nouveau_bo
*bo
;
248 const uint32_t bo_flags
= NOUVEAU_BO_GART
| NOUVEAU_BO_RD
;
250 address
[b
] = nouveau_scratch_data(&nv50
->base
, vb
->user_buffer
,
253 BCTX_REFN_bo(nv50
->bufctx_3d
, VERTEX_TMP
, bo_flags
, bo
);
256 BEGIN_NV04(push
, NV50_3D(VERTEX_ARRAY_LIMIT_HIGH(i
)), 2);
257 PUSH_DATAh(push
, address
[b
] + base
+ size
- 1);
258 PUSH_DATA (push
, address
[b
] + base
+ size
- 1);
259 BEGIN_NV04(push
, NV50_3D(VERTEX_ARRAY_START_HIGH(i
)), 2);
260 PUSH_DATAh(push
, address
[b
] + ve
->src_offset
);
261 PUSH_DATA (push
, address
[b
] + ve
->src_offset
);
263 nv50
->base
.vbo_dirty
= TRUE
;
267 nv50_release_user_vbufs(struct nv50_context
*nv50
)
269 if (nv50
->vbo_user
) {
270 nouveau_bufctx_reset(nv50
->bufctx_3d
, NV50_BIND_VERTEX_TMP
);
271 nouveau_scratch_done(&nv50
->base
);
276 nv50_vertex_arrays_validate(struct nv50_context
*nv50
)
278 uint64_t addrs
[PIPE_MAX_ATTRIBS
];
279 uint32_t limits
[PIPE_MAX_ATTRIBS
];
280 struct nouveau_pushbuf
*push
= nv50
->base
.pushbuf
;
281 struct nv50_vertex_stateobj
*vertex
= nv50
->vertex
;
282 struct pipe_vertex_buffer
*vb
;
283 struct nv50_vertex_element
*ve
;
287 const unsigned n
= MAX2(vertex
->num_elements
, nv50
->state
.num_vtxelts
);
289 if (unlikely(vertex
->need_conversion
))
292 if (nv50
->vbo_user
& ~nv50
->vbo_constant
)
293 nv50
->vbo_fifo
= nv50
->vbo_push_hint
? ~0 : 0;
297 if (!nv50
->vbo_fifo
) {
298 /* if vertex buffer was written by GPU - flush VBO cache */
299 for (i
= 0; i
< nv50
->num_vtxbufs
; ++i
) {
300 struct nv04_resource
*buf
= nv04_resource(nv50
->vtxbuf
[i
].buffer
);
301 if (buf
&& buf
->status
& NOUVEAU_BUFFER_STATUS_GPU_WRITING
) {
302 buf
->status
&= ~NOUVEAU_BUFFER_STATUS_GPU_WRITING
;
303 nv50
->base
.vbo_dirty
= TRUE
;
309 /* update vertex format state */
310 BEGIN_NV04(push
, NV50_3D(VERTEX_ARRAY_ATTRIB(0)), n
);
311 if (nv50
->vbo_fifo
) {
312 nv50
->state
.num_vtxelts
= vertex
->num_elements
;
313 for (i
= 0; i
< vertex
->num_elements
; ++i
)
314 PUSH_DATA (push
, vertex
->element
[i
].state
);
316 PUSH_DATA (push
, NV50_3D_VERTEX_ATTRIB_INACTIVE
);
317 for (i
= 0; i
< n
; ++i
) {
318 BEGIN_NV04(push
, NV50_3D(VERTEX_ARRAY_FETCH(i
)), 1);
323 for (i
= 0; i
< vertex
->num_elements
; ++i
) {
324 const unsigned b
= vertex
->element
[i
].pipe
.vertex_buffer_index
;
325 ve
= &vertex
->element
[i
];
326 vb
= &nv50
->vtxbuf
[b
];
328 if (likely(vb
->stride
) || !(nv50
->vbo_user
& (1 << b
)))
329 PUSH_DATA(push
, ve
->state
);
331 PUSH_DATA(push
, ve
->state
| NV50_3D_VERTEX_ARRAY_ATTRIB_CONST
);
334 PUSH_DATA(push
, NV50_3D_VERTEX_ATTRIB_INACTIVE
);
336 /* update per-instance enables */
337 mask
= vertex
->instance_elts
^ nv50
->state
.instance_elts
;
339 const int i
= ffs(mask
) - 1;
341 BEGIN_NV04(push
, NV50_3D(VERTEX_ARRAY_PER_INSTANCE(i
)), 1);
342 PUSH_DATA (push
, (vertex
->instance_elts
>> i
) & 1);
344 nv50
->state
.instance_elts
= vertex
->instance_elts
;
346 if (nv50
->vbo_user
& ~nv50
->vbo_constant
)
347 nv50_upload_user_buffers(nv50
, addrs
, limits
);
349 /* update buffers and set constant attributes */
350 for (i
= 0; i
< vertex
->num_elements
; ++i
) {
351 uint64_t address
, limit
;
352 const unsigned b
= vertex
->element
[i
].pipe
.vertex_buffer_index
;
353 ve
= &vertex
->element
[i
];
354 vb
= &nv50
->vtxbuf
[b
];
356 if (unlikely(nv50
->vbo_constant
& (1 << b
))) {
357 BEGIN_NV04(push
, NV50_3D(VERTEX_ARRAY_FETCH(i
)), 1);
359 nv50_emit_vtxattr(nv50
, vb
, &ve
->pipe
, i
);
362 if (nv50
->vbo_user
& (1 << b
)) {
363 address
= addrs
[b
] + ve
->pipe
.src_offset
;
364 limit
= addrs
[b
] + limits
[b
];
366 struct nv04_resource
*buf
= nv04_resource(vb
->buffer
);
367 if (!(refd
& (1 << b
))) {
369 BCTX_REFN(nv50
->bufctx_3d
, VERTEX
, buf
, RD
);
371 address
= buf
->address
+ vb
->buffer_offset
+ ve
->pipe
.src_offset
;
372 limit
= buf
->address
+ buf
->base
.width0
- 1;
375 if (unlikely(ve
->pipe
.instance_divisor
)) {
376 BEGIN_NV04(push
, NV50_3D(VERTEX_ARRAY_FETCH(i
)), 4);
377 PUSH_DATA (push
, NV50_3D_VERTEX_ARRAY_FETCH_ENABLE
| vb
->stride
);
378 PUSH_DATAh(push
, address
);
379 PUSH_DATA (push
, address
);
380 PUSH_DATA (push
, ve
->pipe
.instance_divisor
);
382 BEGIN_NV04(push
, NV50_3D(VERTEX_ARRAY_FETCH(i
)), 3);
383 PUSH_DATA (push
, NV50_3D_VERTEX_ARRAY_FETCH_ENABLE
| vb
->stride
);
384 PUSH_DATAh(push
, address
);
385 PUSH_DATA (push
, address
);
387 BEGIN_NV04(push
, NV50_3D(VERTEX_ARRAY_LIMIT_HIGH(i
)), 2);
388 PUSH_DATAh(push
, limit
);
389 PUSH_DATA (push
, limit
);
391 for (; i
< nv50
->state
.num_vtxelts
; ++i
) {
392 BEGIN_NV04(push
, NV50_3D(VERTEX_ARRAY_FETCH(i
)), 1);
395 nv50
->state
.num_vtxelts
= vertex
->num_elements
;
398 #define NV50_PRIM_GL_CASE(n) \
399 case PIPE_PRIM_##n: return NV50_3D_VERTEX_BEGIN_GL_PRIMITIVE_##n
401 static INLINE
unsigned
402 nv50_prim_gl(unsigned prim
)
405 NV50_PRIM_GL_CASE(POINTS
);
406 NV50_PRIM_GL_CASE(LINES
);
407 NV50_PRIM_GL_CASE(LINE_LOOP
);
408 NV50_PRIM_GL_CASE(LINE_STRIP
);
409 NV50_PRIM_GL_CASE(TRIANGLES
);
410 NV50_PRIM_GL_CASE(TRIANGLE_STRIP
);
411 NV50_PRIM_GL_CASE(TRIANGLE_FAN
);
412 NV50_PRIM_GL_CASE(QUADS
);
413 NV50_PRIM_GL_CASE(QUAD_STRIP
);
414 NV50_PRIM_GL_CASE(POLYGON
);
415 NV50_PRIM_GL_CASE(LINES_ADJACENCY
);
416 NV50_PRIM_GL_CASE(LINE_STRIP_ADJACENCY
);
417 NV50_PRIM_GL_CASE(TRIANGLES_ADJACENCY
);
418 NV50_PRIM_GL_CASE(TRIANGLE_STRIP_ADJACENCY
);
420 return NV50_3D_VERTEX_BEGIN_GL_PRIMITIVE_POINTS
;
425 /* For pre-nva0 transform feedback. */
426 static const uint8_t nv50_pipe_prim_to_prim_size
[PIPE_PRIM_MAX
+ 1] =
428 [PIPE_PRIM_POINTS
] = 1,
429 [PIPE_PRIM_LINES
] = 2,
430 [PIPE_PRIM_LINE_LOOP
] = 2,
431 [PIPE_PRIM_LINE_STRIP
] = 2,
432 [PIPE_PRIM_TRIANGLES
] = 3,
433 [PIPE_PRIM_TRIANGLE_STRIP
] = 3,
434 [PIPE_PRIM_TRIANGLE_FAN
] = 3,
435 [PIPE_PRIM_QUADS
] = 3,
436 [PIPE_PRIM_QUAD_STRIP
] = 3,
437 [PIPE_PRIM_POLYGON
] = 3,
438 [PIPE_PRIM_LINES_ADJACENCY
] = 2,
439 [PIPE_PRIM_LINE_STRIP_ADJACENCY
] = 2,
440 [PIPE_PRIM_TRIANGLES_ADJACENCY
] = 3,
441 [PIPE_PRIM_TRIANGLE_STRIP_ADJACENCY
] = 3
445 nv50_draw_arrays(struct nv50_context
*nv50
,
446 unsigned mode
, unsigned start
, unsigned count
,
447 unsigned instance_count
)
449 struct nouveau_pushbuf
*push
= nv50
->base
.pushbuf
;
452 if (nv50
->state
.index_bias
) {
453 BEGIN_NV04(push
, NV50_3D(VB_ELEMENT_BASE
), 1);
455 nv50
->state
.index_bias
= 0;
458 prim
= nv50_prim_gl(mode
);
460 while (instance_count
--) {
461 BEGIN_NV04(push
, NV50_3D(VERTEX_BEGIN_GL
), 1);
462 PUSH_DATA (push
, prim
);
463 BEGIN_NV04(push
, NV50_3D(VERTEX_BUFFER_FIRST
), 2);
464 PUSH_DATA (push
, start
);
465 PUSH_DATA (push
, count
);
466 BEGIN_NV04(push
, NV50_3D(VERTEX_END_GL
), 1);
469 prim
|= NV50_3D_VERTEX_BEGIN_GL_INSTANCE_NEXT
;
474 nv50_draw_elements_inline_u08(struct nouveau_pushbuf
*push
, const uint8_t *map
,
475 unsigned start
, unsigned count
)
481 BEGIN_NI04(push
, NV50_3D(VB_ELEMENT_U32
), count
& 3);
482 for (i
= 0; i
< (count
& 3); ++i
)
483 PUSH_DATA(push
, *map
++);
487 unsigned i
, nr
= MIN2(count
, NV04_PFIFO_MAX_PACKET_LEN
* 4) / 4;
489 BEGIN_NI04(push
, NV50_3D(VB_ELEMENT_U8
), nr
);
490 for (i
= 0; i
< nr
; ++i
) {
492 (map
[3] << 24) | (map
[2] << 16) | (map
[1] << 8) | map
[0]);
500 nv50_draw_elements_inline_u16(struct nouveau_pushbuf
*push
, const uint16_t *map
,
501 unsigned start
, unsigned count
)
507 BEGIN_NV04(push
, NV50_3D(VB_ELEMENT_U32
), 1);
508 PUSH_DATA (push
, *map
++);
511 unsigned i
, nr
= MIN2(count
, NV04_PFIFO_MAX_PACKET_LEN
* 2) / 2;
513 BEGIN_NI04(push
, NV50_3D(VB_ELEMENT_U16
), nr
);
514 for (i
= 0; i
< nr
; ++i
) {
515 PUSH_DATA(push
, (map
[1] << 16) | map
[0]);
523 nv50_draw_elements_inline_u32(struct nouveau_pushbuf
*push
, const uint32_t *map
,
524 unsigned start
, unsigned count
)
529 const unsigned nr
= MIN2(count
, NV04_PFIFO_MAX_PACKET_LEN
);
531 BEGIN_NI04(push
, NV50_3D(VB_ELEMENT_U32
), nr
);
532 PUSH_DATAp(push
, map
, nr
);
540 nv50_draw_elements_inline_u32_short(struct nouveau_pushbuf
*push
,
542 unsigned start
, unsigned count
)
548 BEGIN_NV04(push
, NV50_3D(VB_ELEMENT_U32
), 1);
549 PUSH_DATA (push
, *map
++);
552 unsigned i
, nr
= MIN2(count
, NV04_PFIFO_MAX_PACKET_LEN
* 2) / 2;
554 BEGIN_NI04(push
, NV50_3D(VB_ELEMENT_U16
), nr
);
555 for (i
= 0; i
< nr
; ++i
) {
556 PUSH_DATA(push
, (map
[1] << 16) | map
[0]);
564 nv50_draw_elements(struct nv50_context
*nv50
, boolean shorten
,
565 unsigned mode
, unsigned start
, unsigned count
,
566 unsigned instance_count
, int32_t index_bias
)
568 struct nouveau_pushbuf
*push
= nv50
->base
.pushbuf
;
570 const unsigned index_size
= nv50
->idxbuf
.index_size
;
572 prim
= nv50_prim_gl(mode
);
574 if (index_bias
!= nv50
->state
.index_bias
) {
575 BEGIN_NV04(push
, NV50_3D(VB_ELEMENT_BASE
), 1);
576 PUSH_DATA (push
, index_bias
);
577 nv50
->state
.index_bias
= index_bias
;
580 if (nv50
->idxbuf
.buffer
) {
581 struct nv04_resource
*buf
= nv04_resource(nv50
->idxbuf
.buffer
);
584 const unsigned base
= (buf
->offset
+ nv50
->idxbuf
.offset
) & ~3;
586 start
+= ((buf
->offset
+ nv50
->idxbuf
.offset
) & 3) >> (index_size
>> 1);
588 assert(nouveau_resource_mapped_by_gpu(nv50
->idxbuf
.buffer
));
590 while (instance_count
--) {
591 BEGIN_NV04(push
, NV50_3D(VERTEX_BEGIN_GL
), 1);
592 PUSH_DATA (push
, prim
);
594 nouveau_pushbuf_space(push
, 8, 0, 1);
596 switch (index_size
) {
598 BEGIN_NL50(push
, NV50_3D(VB_ELEMENT_U32
), count
);
599 nouveau_pushbuf_data(push
, buf
->bo
, base
+ start
* 4, count
* 4);
602 pb_start
= (start
& ~1) * 2;
603 pb_bytes
= ((start
+ count
+ 1) & ~1) * 2 - pb_start
;
605 BEGIN_NV04(push
, NV50_3D(VB_ELEMENT_U16_SETUP
), 1);
606 PUSH_DATA (push
, (start
<< 31) | count
);
607 BEGIN_NL50(push
, NV50_3D(VB_ELEMENT_U16
), pb_bytes
/ 4);
608 nouveau_pushbuf_data(push
, buf
->bo
, base
+ pb_start
, pb_bytes
);
609 BEGIN_NV04(push
, NV50_3D(VB_ELEMENT_U16_SETUP
), 1);
613 assert(index_size
== 1);
614 pb_start
= start
& ~3;
615 pb_bytes
= ((start
+ count
+ 3) & ~3) - pb_start
;
617 BEGIN_NV04(push
, NV50_3D(VB_ELEMENT_U8_SETUP
), 1);
618 PUSH_DATA (push
, (start
<< 30) | count
);
619 BEGIN_NL50(push
, NV50_3D(VB_ELEMENT_U8
), pb_bytes
/ 4);
620 nouveau_pushbuf_data(push
, buf
->bo
, base
+ pb_start
, pb_bytes
);
621 BEGIN_NV04(push
, NV50_3D(VB_ELEMENT_U8_SETUP
), 1);
625 BEGIN_NV04(push
, NV50_3D(VERTEX_END_GL
), 1);
628 prim
|= NV50_3D_VERTEX_BEGIN_GL_INSTANCE_NEXT
;
631 const void *data
= nv50
->idxbuf
.user_buffer
;
633 while (instance_count
--) {
634 BEGIN_NV04(push
, NV50_3D(VERTEX_BEGIN_GL
), 1);
635 PUSH_DATA (push
, prim
);
636 switch (index_size
) {
638 nv50_draw_elements_inline_u08(push
, data
, start
, count
);
641 nv50_draw_elements_inline_u16(push
, data
, start
, count
);
645 nv50_draw_elements_inline_u32_short(push
, data
, start
, count
);
647 nv50_draw_elements_inline_u32(push
, data
, start
, count
);
653 BEGIN_NV04(push
, NV50_3D(VERTEX_END_GL
), 1);
656 prim
|= NV50_3D_VERTEX_BEGIN_GL_INSTANCE_NEXT
;
662 nva0_draw_stream_output(struct nv50_context
*nv50
,
663 const struct pipe_draw_info
*info
)
665 struct nouveau_pushbuf
*push
= nv50
->base
.pushbuf
;
666 struct nv50_so_target
*so
= nv50_so_target(info
->count_from_stream_output
);
667 struct nv04_resource
*res
= nv04_resource(so
->pipe
.buffer
);
668 unsigned num_instances
= info
->instance_count
;
669 unsigned mode
= nv50_prim_gl(info
->mode
);
671 if (unlikely(nv50
->screen
->base
.class_3d
< NVA0_3D_CLASS
)) {
672 /* A proper implementation without waiting doesn't seem possible,
675 NOUVEAU_ERR("draw_stream_output not supported on pre-NVA0 cards\n");
679 if (res
->status
& NOUVEAU_BUFFER_STATUS_GPU_WRITING
) {
680 res
->status
&= ~NOUVEAU_BUFFER_STATUS_GPU_WRITING
;
682 BEGIN_NV04(push
, SUBC_3D(NV50_GRAPH_SERIALIZE
), 1);
684 BEGIN_NV04(push
, NV50_3D(VERTEX_ARRAY_FLUSH
), 1);
688 assert(num_instances
);
691 BEGIN_NV04(push
, NV50_3D(VERTEX_BEGIN_GL
), 1);
692 PUSH_DATA (push
, mode
);
693 BEGIN_NV04(push
, NVA0_3D(DRAW_TFB_BASE
), 1);
695 BEGIN_NV04(push
, NVA0_3D(DRAW_TFB_STRIDE
), 1);
697 BEGIN_NV04(push
, NVA0_3D(DRAW_TFB_BYTES
), 1);
698 nv50_query_pushbuf_submit(push
, so
->pq
, 0x4);
699 BEGIN_NV04(push
, NV50_3D(VERTEX_END_GL
), 1);
702 mode
|= NV50_3D_VERTEX_BEGIN_GL_INSTANCE_NEXT
;
703 } while (--num_instances
);
707 nv50_draw_vbo_kick_notify(struct nouveau_pushbuf
*chan
)
709 struct nv50_screen
*screen
= chan
->user_priv
;
711 nouveau_fence_update(&screen
->base
, TRUE
);
713 nv50_bufctx_fence(screen
->cur_ctx
->bufctx_3d
, TRUE
);
717 nv50_draw_vbo(struct pipe_context
*pipe
, const struct pipe_draw_info
*info
)
719 struct nv50_context
*nv50
= nv50_context(pipe
);
720 struct nouveau_pushbuf
*push
= nv50
->base
.pushbuf
;
722 /* NOTE: caller must ensure that (min_index + index_bias) is >= 0 */
723 nv50
->vb_elt_first
= info
->min_index
+ info
->index_bias
;
724 nv50
->vb_elt_limit
= info
->max_index
- info
->min_index
;
725 nv50
->instance_off
= info
->start_instance
;
726 nv50
->instance_max
= info
->instance_count
- 1;
728 /* For picking only a few vertices from a large user buffer, push is better,
729 * if index count is larger and we expect repeated vertices, suggest upload.
731 nv50
->vbo_push_hint
= /* the 64 is heuristic */
732 !(info
->indexed
&& ((nv50
->vb_elt_limit
+ 64) < info
->count
));
734 if (nv50
->vbo_user
&& !(nv50
->dirty
& (NV50_NEW_ARRAYS
| NV50_NEW_VERTEX
))) {
735 if (!!nv50
->vbo_fifo
!= nv50
->vbo_push_hint
)
736 nv50
->dirty
|= NV50_NEW_ARRAYS
;
739 nv50_update_user_vbufs(nv50
);
742 if (unlikely(nv50
->num_so_targets
&& !nv50
->gmtyprog
))
743 nv50
->state
.prim_size
= nv50_pipe_prim_to_prim_size
[info
->mode
];
745 nv50_state_validate(nv50
, ~0, 8); /* 8 as minimum, we use flush_notify */
747 push
->kick_notify
= nv50_draw_vbo_kick_notify
;
749 if (nv50
->vbo_fifo
) {
750 nv50_push_vbo(nv50
, info
);
751 push
->kick_notify
= nv50_default_kick_notify
;
752 nouveau_pushbuf_bufctx(push
, NULL
);
756 if (nv50
->state
.instance_base
!= info
->start_instance
) {
757 nv50
->state
.instance_base
= info
->start_instance
;
758 /* NOTE: this does not affect the shader input, should it ? */
759 BEGIN_NV04(push
, NV50_3D(VB_INSTANCE_BASE
), 1);
760 PUSH_DATA (push
, info
->start_instance
);
763 if (nv50
->base
.vbo_dirty
) {
764 BEGIN_NV04(push
, NV50_3D(VERTEX_ARRAY_FLUSH
), 1);
766 nv50
->base
.vbo_dirty
= FALSE
;
770 boolean shorten
= info
->max_index
<= 65535;
772 if (info
->primitive_restart
!= nv50
->state
.prim_restart
) {
773 if (info
->primitive_restart
) {
774 BEGIN_NV04(push
, NV50_3D(PRIM_RESTART_ENABLE
), 2);
776 PUSH_DATA (push
, info
->restart_index
);
778 if (info
->restart_index
> 65535)
781 BEGIN_NV04(push
, NV50_3D(PRIM_RESTART_ENABLE
), 1);
784 nv50
->state
.prim_restart
= info
->primitive_restart
;
786 if (info
->primitive_restart
) {
787 BEGIN_NV04(push
, NV50_3D(PRIM_RESTART_INDEX
), 1);
788 PUSH_DATA (push
, info
->restart_index
);
790 if (info
->restart_index
> 65535)
794 nv50_draw_elements(nv50
, shorten
,
795 info
->mode
, info
->start
, info
->count
,
796 info
->instance_count
, info
->index_bias
);
798 if (unlikely(info
->count_from_stream_output
)) {
799 nva0_draw_stream_output(nv50
, info
);
801 nv50_draw_arrays(nv50
,
802 info
->mode
, info
->start
, info
->count
,
803 info
->instance_count
);
805 push
->kick_notify
= nv50_default_kick_notify
;
807 nv50_release_user_vbufs(nv50
);
809 nouveau_pushbuf_bufctx(push
, NULL
);