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 OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
18 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20 * OTHER DEALINGS IN THE SOFTWARE.
23 #define NVC0_PUSH_EXPLICIT_SPACE_CHECKING
25 #include "pipe/p_context.h"
26 #include "pipe/p_state.h"
27 #include "util/u_inlines.h"
28 #include "util/u_format.h"
29 #include "translate/translate.h"
31 #include "nvc0/nvc0_context.h"
32 #include "nvc0/nvc0_resource.h"
34 #include "nvc0/nvc0_3d.xml.h"
37 nvc0_vertex_state_delete(struct pipe_context
*pipe
,
40 struct nvc0_vertex_stateobj
*so
= hwcso
;
43 so
->translate
->release(so
->translate
);
48 nvc0_vertex_state_create(struct pipe_context
*pipe
,
49 unsigned num_elements
,
50 const struct pipe_vertex_element
*elements
)
52 struct nvc0_vertex_stateobj
*so
;
53 struct translate_key transkey
;
55 unsigned src_offset_max
= 0;
57 so
= MALLOC(sizeof(*so
) +
58 num_elements
* sizeof(struct nvc0_vertex_element
));
61 so
->num_elements
= num_elements
;
62 so
->instance_elts
= 0;
63 so
->instance_bufs
= 0;
64 so
->shared_slots
= FALSE
;
65 so
->need_conversion
= FALSE
;
67 memset(so
->vb_access_size
, 0, sizeof(so
->vb_access_size
));
69 for (i
= 0; i
< PIPE_MAX_ATTRIBS
; ++i
)
70 so
->min_instance_div
[i
] = 0xffffffff;
72 transkey
.nr_elements
= 0;
73 transkey
.output_stride
= 0;
75 for (i
= 0; i
< num_elements
; ++i
) {
76 const struct pipe_vertex_element
*ve
= &elements
[i
];
77 const unsigned vbi
= ve
->vertex_buffer_index
;
79 enum pipe_format fmt
= ve
->src_format
;
81 so
->element
[i
].pipe
= elements
[i
];
82 so
->element
[i
].state
= nvc0_format_table
[fmt
].vtx
;
84 if (!so
->element
[i
].state
) {
85 switch (util_format_get_nr_components(fmt
)) {
86 case 1: fmt
= PIPE_FORMAT_R32_FLOAT
; break;
87 case 2: fmt
= PIPE_FORMAT_R32G32_FLOAT
; break;
88 case 3: fmt
= PIPE_FORMAT_R32G32B32_FLOAT
; break;
89 case 4: fmt
= PIPE_FORMAT_R32G32B32A32_FLOAT
; break;
95 so
->element
[i
].state
= nvc0_format_table
[fmt
].vtx
;
96 so
->need_conversion
= TRUE
;
98 size
= util_format_get_blocksize(fmt
);
100 src_offset_max
= MAX2(src_offset_max
, ve
->src_offset
);
102 if (so
->vb_access_size
[vbi
] < (ve
->src_offset
+ size
))
103 so
->vb_access_size
[vbi
] = ve
->src_offset
+ size
;
105 if (unlikely(ve
->instance_divisor
)) {
106 so
->instance_elts
|= 1 << i
;
107 so
->instance_bufs
|= 1 << vbi
;
108 if (ve
->instance_divisor
< so
->min_instance_div
[vbi
])
109 so
->min_instance_div
[vbi
] = ve
->instance_divisor
;
114 unsigned j
= transkey
.nr_elements
++;
116 ca
= util_format_description(fmt
)->channel
[0].size
/ 8;
117 if (ca
!= 1 && ca
!= 2)
120 transkey
.element
[j
].type
= TRANSLATE_ELEMENT_NORMAL
;
121 transkey
.element
[j
].input_format
= ve
->src_format
;
122 transkey
.element
[j
].input_buffer
= vbi
;
123 transkey
.element
[j
].input_offset
= ve
->src_offset
;
124 transkey
.element
[j
].instance_divisor
= ve
->instance_divisor
;
126 transkey
.output_stride
= align(transkey
.output_stride
, ca
);
127 transkey
.element
[j
].output_format
= fmt
;
128 transkey
.element
[j
].output_offset
= transkey
.output_stride
;
129 transkey
.output_stride
+= size
;
131 so
->element
[i
].state_alt
= so
->element
[i
].state
;
132 so
->element
[i
].state_alt
|= transkey
.element
[j
].output_offset
<< 7;
135 so
->element
[i
].state
|= i
<< NVC0_3D_VERTEX_ATTRIB_FORMAT_BUFFER__SHIFT
;
137 transkey
.output_stride
= align(transkey
.output_stride
, 4);
139 so
->size
= transkey
.output_stride
;
140 so
->translate
= translate_create(&transkey
);
142 if (so
->instance_elts
|| src_offset_max
>= (1 << 14))
144 so
->shared_slots
= TRUE
;
146 for (i
= 0; i
< num_elements
; ++i
) {
147 const unsigned b
= elements
[i
].vertex_buffer_index
;
148 const unsigned s
= elements
[i
].src_offset
;
149 so
->element
[i
].state
&= ~NVC0_3D_VERTEX_ATTRIB_FORMAT_BUFFER__MASK
;
150 so
->element
[i
].state
|= b
<< NVC0_3D_VERTEX_ATTRIB_FORMAT_BUFFER__SHIFT
;
151 so
->element
[i
].state
|= s
<< NVC0_3D_VERTEX_ATTRIB_FORMAT_OFFSET__SHIFT
;
156 #define NVC0_3D_VERTEX_ATTRIB_INACTIVE \
157 NVC0_3D_VERTEX_ATTRIB_FORMAT_TYPE_FLOAT | \
158 NVC0_3D_VERTEX_ATTRIB_FORMAT_SIZE_32 | NVC0_3D_VERTEX_ATTRIB_FORMAT_CONST
160 #define VTX_ATTR(a, c, t, s) \
161 ((NVC0_3D_VTX_ATTR_DEFINE_TYPE_##t) | \
162 (NVC0_3D_VTX_ATTR_DEFINE_SIZE_##s) | \
163 ((a) << NVC0_3D_VTX_ATTR_DEFINE_ATTR__SHIFT) | \
164 ((c) << NVC0_3D_VTX_ATTR_DEFINE_COMP__SHIFT))
167 nvc0_set_constant_vertex_attrib(struct nvc0_context
*nvc0
, const unsigned a
)
169 struct nouveau_pushbuf
*push
= nvc0
->base
.pushbuf
;
170 struct pipe_vertex_element
*ve
= &nvc0
->vertex
->element
[a
].pipe
;
171 struct pipe_vertex_buffer
*vb
= &nvc0
->vtxbuf
[ve
->vertex_buffer_index
];
173 const struct util_format_description
*desc
;
175 const void *src
= (const uint8_t *)vb
->user_buffer
+ ve
->src_offset
;
178 desc
= util_format_description(ve
->src_format
);
181 BEGIN_NVC0(push
, NVC0_3D(VTX_ATTR_DEFINE
), 5);
183 if (desc
->channel
[0].pure_integer
) {
184 if (desc
->channel
[0].type
== UTIL_FORMAT_TYPE_SIGNED
) {
185 mode
= VTX_ATTR(a
, 4, SINT
, 32);
186 desc
->unpack_rgba_sint(dst
, 0, src
, 0, 1, 1);
188 mode
= VTX_ATTR(a
, 4, UINT
, 32);
189 desc
->unpack_rgba_uint(dst
, 0, src
, 0, 1, 1);
192 mode
= VTX_ATTR(a
, 4, FLOAT
, 32);
193 desc
->unpack_rgba_float(dst
, 0, src
, 0, 1, 1);
200 nvc0_user_vbuf_range(struct nvc0_context
*nvc0
, int vbi
,
201 uint32_t *base
, uint32_t *size
)
203 if (unlikely(nvc0
->vertex
->instance_bufs
& (1 << vbi
))) {
204 const uint32_t div
= nvc0
->vertex
->min_instance_div
[vbi
];
205 *base
= nvc0
->instance_off
* nvc0
->vtxbuf
[vbi
].stride
;
206 *size
= (nvc0
->instance_max
/ div
) * nvc0
->vtxbuf
[vbi
].stride
+
207 nvc0
->vertex
->vb_access_size
[vbi
];
209 /* NOTE: if there are user buffers, we *must* have index bounds */
210 assert(nvc0
->vb_elt_limit
!= ~0);
211 *base
= nvc0
->vb_elt_first
* nvc0
->vtxbuf
[vbi
].stride
;
212 *size
= nvc0
->vb_elt_limit
* nvc0
->vtxbuf
[vbi
].stride
+
213 nvc0
->vertex
->vb_access_size
[vbi
];
218 nvc0_release_user_vbufs(struct nvc0_context
*nvc0
)
220 if (nvc0
->vbo_user
) {
221 nouveau_bufctx_reset(nvc0
->bufctx_3d
, NVC0_BIND_VTX_TMP
);
222 nouveau_scratch_done(&nvc0
->base
);
227 nvc0_update_user_vbufs(struct nvc0_context
*nvc0
)
229 uint64_t address
[PIPE_MAX_ATTRIBS
];
230 struct nouveau_pushbuf
*push
= nvc0
->base
.pushbuf
;
232 uint32_t written
= 0;
234 PUSH_SPACE(push
, nvc0
->vertex
->num_elements
* 8);
235 for (i
= 0; i
< nvc0
->vertex
->num_elements
; ++i
) {
236 struct pipe_vertex_element
*ve
= &nvc0
->vertex
->element
[i
].pipe
;
237 const unsigned b
= ve
->vertex_buffer_index
;
238 struct pipe_vertex_buffer
*vb
= &nvc0
->vtxbuf
[b
];
241 if (!(nvc0
->vbo_user
& (1 << b
)))
243 if (nvc0
->constant_vbos
& (1 << b
)) {
244 nvc0_set_constant_vertex_attrib(nvc0
, i
);
247 nvc0_user_vbuf_range(nvc0
, b
, &base
, &size
);
249 if (!(written
& (1 << b
))) {
250 struct nouveau_bo
*bo
;
251 const uint32_t bo_flags
= NOUVEAU_BO_RD
| NOUVEAU_BO_GART
;
253 address
[b
] = nouveau_scratch_data(&nvc0
->base
, vb
->user_buffer
,
256 BCTX_REFN_bo(nvc0
->bufctx_3d
, VTX_TMP
, bo_flags
, bo
);
258 NOUVEAU_DRV_STAT(&nvc0
->screen
->base
, user_buffer_upload_bytes
, size
);
261 BEGIN_1IC0(push
, NVC0_3D(MACRO_VERTEX_ARRAY_SELECT
), 5);
263 PUSH_DATAh(push
, address
[b
] + base
+ size
- 1);
264 PUSH_DATA (push
, address
[b
] + base
+ size
- 1);
265 PUSH_DATAh(push
, address
[b
] + ve
->src_offset
);
266 PUSH_DATA (push
, address
[b
] + ve
->src_offset
);
268 nvc0
->base
.vbo_dirty
= TRUE
;
272 nvc0_update_user_vbufs_shared(struct nvc0_context
*nvc0
)
274 struct nouveau_pushbuf
*push
= nvc0
->base
.pushbuf
;
275 uint32_t mask
= nvc0
->vbo_user
& ~nvc0
->constant_vbos
;
277 PUSH_SPACE(push
, nvc0
->num_vtxbufs
* 8);
279 struct nouveau_bo
*bo
;
280 const uint32_t bo_flags
= NOUVEAU_BO_RD
| NOUVEAU_BO_GART
;
283 const int b
= ffs(mask
) - 1;
286 nvc0_user_vbuf_range(nvc0
, b
, &base
, &size
);
288 address
= nouveau_scratch_data(&nvc0
->base
, nvc0
->vtxbuf
[b
].user_buffer
,
291 BCTX_REFN_bo(nvc0
->bufctx_3d
, VTX_TMP
, bo_flags
, bo
);
293 BEGIN_1IC0(push
, NVC0_3D(MACRO_VERTEX_ARRAY_SELECT
), 5);
295 PUSH_DATAh(push
, address
+ base
+ size
- 1);
296 PUSH_DATA (push
, address
+ base
+ size
- 1);
297 PUSH_DATAh(push
, address
);
298 PUSH_DATA (push
, address
);
300 NOUVEAU_DRV_STAT(&nvc0
->screen
->base
, user_buffer_upload_bytes
, size
);
303 mask
= nvc0
->state
.constant_elts
;
305 int i
= ffs(mask
) - 1;
307 nvc0_set_constant_vertex_attrib(nvc0
, i
);
312 nvc0_validate_vertex_buffers(struct nvc0_context
*nvc0
)
314 struct nouveau_pushbuf
*push
= nvc0
->base
.pushbuf
;
315 const struct nvc0_vertex_stateobj
*vertex
= nvc0
->vertex
;
319 PUSH_SPACE(push
, vertex
->num_elements
* 8);
320 for (i
= 0; i
< vertex
->num_elements
; ++i
) {
321 const struct nvc0_vertex_element
*ve
;
322 const struct pipe_vertex_buffer
*vb
;
323 struct nv04_resource
*res
;
325 unsigned limit
, offset
;
327 if (nvc0
->state
.constant_elts
& (1 << i
))
329 ve
= &vertex
->element
[i
];
330 b
= ve
->pipe
.vertex_buffer_index
;
331 vb
= &nvc0
->vtxbuf
[b
];
334 if (!(nvc0
->constant_vbos
& (1 << b
))) {
335 if (ve
->pipe
.instance_divisor
) {
336 BEGIN_NVC0(push
, NVC0_3D(VERTEX_ARRAY_DIVISOR(i
)), 1);
337 PUSH_DATA (push
, ve
->pipe
.instance_divisor
);
339 BEGIN_NVC0(push
, NVC0_3D(VERTEX_ARRAY_FETCH(i
)), 1);
340 PUSH_DATA (push
, (1 << 12) | vb
->stride
);
342 /* address/value set in nvc0_update_user_vbufs */
345 res
= nv04_resource(vb
->buffer
);
346 offset
= ve
->pipe
.src_offset
+ vb
->buffer_offset
;
347 limit
= vb
->buffer
->width0
- 1;
349 if (unlikely(ve
->pipe
.instance_divisor
)) {
350 BEGIN_NVC0(push
, NVC0_3D(VERTEX_ARRAY_FETCH(i
)), 4);
351 PUSH_DATA (push
, (1 << 12) | vb
->stride
);
352 PUSH_DATAh(push
, res
->address
+ offset
);
353 PUSH_DATA (push
, res
->address
+ offset
);
354 PUSH_DATA (push
, ve
->pipe
.instance_divisor
);
356 BEGIN_NVC0(push
, NVC0_3D(VERTEX_ARRAY_FETCH(i
)), 3);
357 PUSH_DATA (push
, (1 << 12) | vb
->stride
);
358 PUSH_DATAh(push
, res
->address
+ offset
);
359 PUSH_DATA (push
, res
->address
+ offset
);
361 BEGIN_NVC0(push
, NVC0_3D(VERTEX_ARRAY_LIMIT_HIGH(i
)), 2);
362 PUSH_DATAh(push
, res
->address
+ limit
);
363 PUSH_DATA (push
, res
->address
+ limit
);
365 if (!(refd
& (1 << b
))) {
367 BCTX_REFN(nvc0
->bufctx_3d
, VTX
, res
, RD
);
371 nvc0_update_user_vbufs(nvc0
);
375 nvc0_validate_vertex_buffers_shared(struct nvc0_context
*nvc0
)
377 struct nouveau_pushbuf
*push
= nvc0
->base
.pushbuf
;
379 const uint32_t mask
= nvc0
->vbo_user
;
381 PUSH_SPACE(push
, nvc0
->num_vtxbufs
* 8);
382 for (b
= 0; b
< nvc0
->num_vtxbufs
; ++b
) {
383 struct pipe_vertex_buffer
*vb
= &nvc0
->vtxbuf
[b
];
384 struct nv04_resource
*buf
;
385 uint32_t offset
, limit
;
387 if (mask
& (1 << b
)) {
388 if (!(nvc0
->constant_vbos
& (1 << b
))) {
389 BEGIN_NVC0(push
, NVC0_3D(VERTEX_ARRAY_FETCH(b
)), 1);
390 PUSH_DATA (push
, NVC0_3D_VERTEX_ARRAY_FETCH_ENABLE
| vb
->stride
);
392 /* address/value set in nvc0_update_user_vbufs_shared */
395 buf
= nv04_resource(vb
->buffer
);
396 offset
= vb
->buffer_offset
;
397 limit
= buf
->base
.width0
- 1;
399 BEGIN_NVC0(push
, NVC0_3D(VERTEX_ARRAY_FETCH(b
)), 3);
400 PUSH_DATA (push
, NVC0_3D_VERTEX_ARRAY_FETCH_ENABLE
| vb
->stride
);
401 PUSH_DATAh(push
, buf
->address
+ offset
);
402 PUSH_DATA (push
, buf
->address
+ offset
);
403 BEGIN_NVC0(push
, NVC0_3D(VERTEX_ARRAY_LIMIT_HIGH(b
)), 2);
404 PUSH_DATAh(push
, buf
->address
+ limit
);
405 PUSH_DATA (push
, buf
->address
+ limit
);
407 BCTX_REFN(nvc0
->bufctx_3d
, VTX
, buf
, RD
);
410 nvc0_update_user_vbufs_shared(nvc0
);
414 nvc0_vertex_arrays_validate(struct nvc0_context
*nvc0
)
416 struct nouveau_pushbuf
*push
= nvc0
->base
.pushbuf
;
417 struct nvc0_vertex_stateobj
*vertex
= nvc0
->vertex
;
418 struct nvc0_vertex_element
*ve
;
422 boolean update_vertex
;
424 nouveau_bufctx_reset(nvc0
->bufctx_3d
, NVC0_BIND_VTX
);
427 if (unlikely(vertex
->need_conversion
) ||
428 unlikely(nvc0
->vertprog
->vp
.edgeflag
< PIPE_MAX_ATTRIBS
)) {
431 vbo_mode
= (nvc0
->vbo_user
&& nvc0
->vbo_push_hint
) ? 1 : 0;
433 const_vbos
= vbo_mode
? 0 : nvc0
->constant_vbos
;
435 update_vertex
= (nvc0
->dirty
& NVC0_NEW_VERTEX
) ||
436 (const_vbos
!= nvc0
->state
.constant_vbos
) ||
437 (vbo_mode
!= nvc0
->state
.vbo_mode
);
440 const unsigned n
= MAX2(vertex
->num_elements
, nvc0
->state
.num_vtxelts
);
442 nvc0
->state
.constant_vbos
= const_vbos
;
443 nvc0
->state
.constant_elts
= 0;
444 nvc0
->state
.num_vtxelts
= vertex
->num_elements
;
445 nvc0
->state
.vbo_mode
= vbo_mode
;
447 if (unlikely(vbo_mode
)) {
448 if (unlikely(nvc0
->state
.instance_elts
& 3)) {
449 /* translate mode uses only 2 vertex buffers */
450 nvc0
->state
.instance_elts
&= ~3;
452 BEGIN_NVC0(push
, NVC0_3D(VERTEX_ARRAY_PER_INSTANCE(0)), 2);
457 PUSH_SPACE(push
, n
* 2 + 4);
459 BEGIN_NVC0(push
, NVC0_3D(VERTEX_ATTRIB_FORMAT(0)), n
);
460 for (i
= 0; i
< vertex
->num_elements
; ++i
)
461 PUSH_DATA(push
, vertex
->element
[i
].state_alt
);
463 PUSH_DATA(push
, NVC0_3D_VERTEX_ATTRIB_INACTIVE
);
465 BEGIN_NVC0(push
, NVC0_3D(VERTEX_ARRAY_FETCH(0)), 1);
466 PUSH_DATA (push
, (1 << 12) | vertex
->size
);
467 for (i
= 1; i
< n
; ++i
)
468 IMMED_NVC0(push
, NVC0_3D(VERTEX_ARRAY_FETCH(i
)), 0);
470 uint32_t *restrict data
;
472 if (unlikely(vertex
->instance_elts
!= nvc0
->state
.instance_elts
)) {
473 nvc0
->state
.instance_elts
= vertex
->instance_elts
;
474 assert(n
); /* if (n == 0), both masks should be 0 */
476 BEGIN_NVC0(push
, NVC0_3D(MACRO_VERTEX_ARRAY_PER_INSTANCE
), 2);
478 PUSH_DATA (push
, vertex
->instance_elts
);
481 PUSH_SPACE(push
, n
* 2 + 1);
482 BEGIN_NVC0(push
, NVC0_3D(VERTEX_ATTRIB_FORMAT(0)), n
);
485 for (i
= 0; i
< vertex
->num_elements
; ++i
) {
486 ve
= &vertex
->element
[i
];
488 if (unlikely(const_vbos
& (1 << ve
->pipe
.vertex_buffer_index
))) {
489 nvc0
->state
.constant_elts
|= 1 << i
;
490 data
[i
] |= NVC0_3D_VERTEX_ATTRIB_FORMAT_CONST
;
491 IMMED_NVC0(push
, NVC0_3D(VERTEX_ARRAY_FETCH(i
)), 0);
495 data
[i
] = NVC0_3D_VERTEX_ATTRIB_INACTIVE
;
496 IMMED_NVC0(push
, NVC0_3D(VERTEX_ARRAY_FETCH(i
)), 0);
500 if (nvc0
->state
.vbo_mode
) /* using translate, don't set up arrays here */
503 if (vertex
->shared_slots
)
504 nvc0_validate_vertex_buffers_shared(nvc0
);
506 nvc0_validate_vertex_buffers(nvc0
);
510 nvc0_idxbuf_validate(struct nvc0_context
*nvc0
)
512 struct nouveau_pushbuf
*push
= nvc0
->base
.pushbuf
;
513 struct nv04_resource
*buf
= nv04_resource(nvc0
->idxbuf
.buffer
);
516 assert(nouveau_resource_mapped_by_gpu(&buf
->base
));
519 BEGIN_NVC0(push
, NVC0_3D(INDEX_ARRAY_START_HIGH
), 5);
520 PUSH_DATAh(push
, buf
->address
+ nvc0
->idxbuf
.offset
);
521 PUSH_DATA (push
, buf
->address
+ nvc0
->idxbuf
.offset
);
522 PUSH_DATAh(push
, buf
->address
+ buf
->base
.width0
- 1);
523 PUSH_DATA (push
, buf
->address
+ buf
->base
.width0
- 1);
524 PUSH_DATA (push
, nvc0
->idxbuf
.index_size
>> 1);
526 BCTX_REFN(nvc0
->bufctx_3d
, IDX
, buf
, RD
);
529 #define NVC0_PRIM_GL_CASE(n) \
530 case PIPE_PRIM_##n: return NVC0_3D_VERTEX_BEGIN_GL_PRIMITIVE_##n
532 static INLINE
unsigned
533 nvc0_prim_gl(unsigned prim
)
536 NVC0_PRIM_GL_CASE(POINTS
);
537 NVC0_PRIM_GL_CASE(LINES
);
538 NVC0_PRIM_GL_CASE(LINE_LOOP
);
539 NVC0_PRIM_GL_CASE(LINE_STRIP
);
540 NVC0_PRIM_GL_CASE(TRIANGLES
);
541 NVC0_PRIM_GL_CASE(TRIANGLE_STRIP
);
542 NVC0_PRIM_GL_CASE(TRIANGLE_FAN
);
543 NVC0_PRIM_GL_CASE(QUADS
);
544 NVC0_PRIM_GL_CASE(QUAD_STRIP
);
545 NVC0_PRIM_GL_CASE(POLYGON
);
546 NVC0_PRIM_GL_CASE(LINES_ADJACENCY
);
547 NVC0_PRIM_GL_CASE(LINE_STRIP_ADJACENCY
);
548 NVC0_PRIM_GL_CASE(TRIANGLES_ADJACENCY
);
549 NVC0_PRIM_GL_CASE(TRIANGLE_STRIP_ADJACENCY
);
551 NVC0_PRIM_GL_CASE(PATCHES); */
553 return NVC0_3D_VERTEX_BEGIN_GL_PRIMITIVE_POINTS
;
558 nvc0_draw_vbo_kick_notify(struct nouveau_pushbuf
*push
)
560 struct nvc0_screen
*screen
= push
->user_priv
;
562 nouveau_fence_update(&screen
->base
, TRUE
);
564 NOUVEAU_DRV_STAT(&screen
->base
, pushbuf_count
, 1);
568 nvc0_draw_arrays(struct nvc0_context
*nvc0
,
569 unsigned mode
, unsigned start
, unsigned count
,
570 unsigned instance_count
)
572 struct nouveau_pushbuf
*push
= nvc0
->base
.pushbuf
;
575 if (nvc0
->state
.index_bias
) {
577 IMMED_NVC0(push
, NVC0_3D(VB_ELEMENT_BASE
), 0);
578 nvc0
->state
.index_bias
= 0;
581 prim
= nvc0_prim_gl(mode
);
583 while (instance_count
--) {
585 BEGIN_NVC0(push
, NVC0_3D(VERTEX_BEGIN_GL
), 1);
586 PUSH_DATA (push
, prim
);
587 BEGIN_NVC0(push
, NVC0_3D(VERTEX_BUFFER_FIRST
), 2);
588 PUSH_DATA (push
, start
);
589 PUSH_DATA (push
, count
);
590 IMMED_NVC0(push
, NVC0_3D(VERTEX_END_GL
), 0);
592 prim
|= NVC0_3D_VERTEX_BEGIN_GL_INSTANCE_NEXT
;
594 NOUVEAU_DRV_STAT(&nvc0
->screen
->base
, draw_calls_array
, 1);
598 nvc0_draw_elements_inline_u08(struct nouveau_pushbuf
*push
, const uint8_t *map
,
599 unsigned start
, unsigned count
)
606 BEGIN_NIC0(push
, NVC0_3D(VB_ELEMENT_U32
), count
& 3);
607 for (i
= 0; i
< (count
& 3); ++i
)
608 PUSH_DATA(push
, *map
++);
612 unsigned i
, nr
= MIN2(count
, NV04_PFIFO_MAX_PACKET_LEN
* 4) / 4;
614 PUSH_SPACE(push
, nr
+ 1);
615 BEGIN_NIC0(push
, NVC0_3D(VB_ELEMENT_U8
), nr
);
616 for (i
= 0; i
< nr
; ++i
) {
618 (map
[3] << 24) | (map
[2] << 16) | (map
[1] << 8) | map
[0]);
626 nvc0_draw_elements_inline_u16(struct nouveau_pushbuf
*push
, const uint16_t *map
,
627 unsigned start
, unsigned count
)
634 BEGIN_NVC0(push
, NVC0_3D(VB_ELEMENT_U32
), 1);
635 PUSH_DATA (push
, *map
++);
638 unsigned i
, nr
= MIN2(count
, NV04_PFIFO_MAX_PACKET_LEN
* 2) / 2;
640 PUSH_SPACE(push
, nr
+ 1);
641 BEGIN_NIC0(push
, NVC0_3D(VB_ELEMENT_U16
), nr
);
642 for (i
= 0; i
< nr
; ++i
) {
643 PUSH_DATA(push
, (map
[1] << 16) | map
[0]);
651 nvc0_draw_elements_inline_u32(struct nouveau_pushbuf
*push
, const uint32_t *map
,
652 unsigned start
, unsigned count
)
657 const unsigned nr
= MIN2(count
, NV04_PFIFO_MAX_PACKET_LEN
);
659 PUSH_SPACE(push
, nr
+ 1);
660 BEGIN_NIC0(push
, NVC0_3D(VB_ELEMENT_U32
), nr
);
661 PUSH_DATAp(push
, map
, nr
);
669 nvc0_draw_elements_inline_u32_short(struct nouveau_pushbuf
*push
,
671 unsigned start
, unsigned count
)
678 BEGIN_NVC0(push
, NVC0_3D(VB_ELEMENT_U32
), 1);
679 PUSH_DATA (push
, *map
++);
682 unsigned i
, nr
= MIN2(count
, NV04_PFIFO_MAX_PACKET_LEN
* 2) / 2;
684 PUSH_SPACE(push
, nr
+ 1);
685 BEGIN_NIC0(push
, NVC0_3D(VB_ELEMENT_U16
), nr
);
686 for (i
= 0; i
< nr
; ++i
) {
687 PUSH_DATA(push
, (map
[1] << 16) | map
[0]);
695 nvc0_draw_elements(struct nvc0_context
*nvc0
, boolean shorten
,
696 unsigned mode
, unsigned start
, unsigned count
,
697 unsigned instance_count
, int32_t index_bias
)
699 struct nouveau_pushbuf
*push
= nvc0
->base
.pushbuf
;
701 const unsigned index_size
= nvc0
->idxbuf
.index_size
;
703 prim
= nvc0_prim_gl(mode
);
705 if (index_bias
!= nvc0
->state
.index_bias
) {
707 BEGIN_NVC0(push
, NVC0_3D(VB_ELEMENT_BASE
), 1);
708 PUSH_DATA (push
, index_bias
);
709 nvc0
->state
.index_bias
= index_bias
;
712 if (nvc0
->idxbuf
.buffer
) {
714 IMMED_NVC0(push
, NVC0_3D(VERTEX_BEGIN_GL
), prim
);
717 BEGIN_NVC0(push
, NVC0_3D(INDEX_BATCH_FIRST
), 2);
718 PUSH_DATA (push
, start
);
719 PUSH_DATA (push
, count
);
720 if (--instance_count
) {
721 BEGIN_NVC0(push
, NVC0_3D(VERTEX_END_GL
), 2);
723 PUSH_DATA (push
, prim
| NVC0_3D_VERTEX_BEGIN_GL_INSTANCE_NEXT
);
725 } while (instance_count
);
726 IMMED_NVC0(push
, NVC0_3D(VERTEX_END_GL
), 0);
728 const void *data
= nvc0
->idxbuf
.user_buffer
;
730 while (instance_count
--) {
732 BEGIN_NVC0(push
, NVC0_3D(VERTEX_BEGIN_GL
), 1);
733 PUSH_DATA (push
, prim
);
734 switch (index_size
) {
736 nvc0_draw_elements_inline_u08(push
, data
, start
, count
);
739 nvc0_draw_elements_inline_u16(push
, data
, start
, count
);
743 nvc0_draw_elements_inline_u32_short(push
, data
, start
, count
);
745 nvc0_draw_elements_inline_u32(push
, data
, start
, count
);
752 IMMED_NVC0(push
, NVC0_3D(VERTEX_END_GL
), 0);
754 prim
|= NVC0_3D_VERTEX_BEGIN_GL_INSTANCE_NEXT
;
757 NOUVEAU_DRV_STAT(&nvc0
->screen
->base
, draw_calls_indexed
, 1);
761 nvc0_draw_stream_output(struct nvc0_context
*nvc0
,
762 const struct pipe_draw_info
*info
)
764 struct nouveau_pushbuf
*push
= nvc0
->base
.pushbuf
;
765 struct nvc0_so_target
*so
= nvc0_so_target(info
->count_from_stream_output
);
766 struct nv04_resource
*res
= nv04_resource(so
->pipe
.buffer
);
767 unsigned mode
= nvc0_prim_gl(info
->mode
);
768 unsigned num_instances
= info
->instance_count
;
770 if (res
->status
& NOUVEAU_BUFFER_STATUS_GPU_WRITING
) {
771 res
->status
&= ~NOUVEAU_BUFFER_STATUS_GPU_WRITING
;
773 IMMED_NVC0(push
, NVC0_3D(SERIALIZE
), 0);
774 nvc0_query_fifo_wait(push
, so
->pq
);
775 if (nvc0
->screen
->eng3d
->oclass
< GM107_3D_CLASS
)
776 IMMED_NVC0(push
, NVC0_3D(VERTEX_ARRAY_FLUSH
), 0);
778 NOUVEAU_DRV_STAT(&nvc0
->screen
->base
, gpu_serialize_count
, 1);
781 while (num_instances
--) {
783 BEGIN_NVC0(push
, NVC0_3D(VERTEX_BEGIN_GL
), 1);
784 PUSH_DATA (push
, mode
);
785 BEGIN_NVC0(push
, NVC0_3D(DRAW_TFB_BASE
), 1);
787 BEGIN_NVC0(push
, NVC0_3D(DRAW_TFB_STRIDE
), 1);
788 PUSH_DATA (push
, so
->stride
);
789 BEGIN_NVC0(push
, NVC0_3D(DRAW_TFB_BYTES
), 1);
790 nvc0_query_pushbuf_submit(push
, so
->pq
, 0x4);
791 IMMED_NVC0(push
, NVC0_3D(VERTEX_END_GL
), 0);
793 mode
|= NVC0_3D_VERTEX_BEGIN_GL_INSTANCE_NEXT
;
798 nvc0_draw_vbo(struct pipe_context
*pipe
, const struct pipe_draw_info
*info
)
800 struct nvc0_context
*nvc0
= nvc0_context(pipe
);
801 struct nouveau_pushbuf
*push
= nvc0
->base
.pushbuf
;
804 /* NOTE: caller must ensure that (min_index + index_bias) is >= 0 */
805 nvc0
->vb_elt_first
= info
->min_index
+ info
->index_bias
;
806 nvc0
->vb_elt_limit
= info
->max_index
- info
->min_index
;
807 nvc0
->instance_off
= info
->start_instance
;
808 nvc0
->instance_max
= info
->instance_count
- 1;
810 /* For picking only a few vertices from a large user buffer, push is better,
811 * if index count is larger and we expect repeated vertices, suggest upload.
813 nvc0
->vbo_push_hint
=
814 info
->indexed
&& (nvc0
->vb_elt_limit
>= (info
->count
* 2));
816 /* Check whether we want to switch vertex-submission mode. */
817 if (nvc0
->vbo_user
&& !(nvc0
->dirty
& (NVC0_NEW_ARRAYS
| NVC0_NEW_VERTEX
))) {
818 if (nvc0
->vbo_push_hint
!= !!nvc0
->state
.vbo_mode
)
819 if (nvc0
->state
.vbo_mode
!= 3)
820 nvc0
->dirty
|= NVC0_NEW_ARRAYS
;
822 if (!(nvc0
->dirty
& NVC0_NEW_ARRAYS
) && nvc0
->state
.vbo_mode
== 0) {
823 if (nvc0
->vertex
->shared_slots
)
824 nvc0_update_user_vbufs_shared(nvc0
);
826 nvc0_update_user_vbufs(nvc0
);
830 /* 8 as minimum to avoid immediate double validation of new buffers */
831 nvc0_state_validate(nvc0
, ~0, 8);
833 push
->kick_notify
= nvc0_draw_vbo_kick_notify
;
835 if (nvc0
->state
.vbo_mode
) {
836 nvc0_push_vbo(nvc0
, info
);
837 push
->kick_notify
= nvc0_default_kick_notify
;
838 nouveau_pushbuf_bufctx(push
, NULL
);
842 /* space for base instance, flush, and prim restart */
845 if (nvc0
->state
.instance_base
!= info
->start_instance
) {
846 nvc0
->state
.instance_base
= info
->start_instance
;
847 /* NOTE: this does not affect the shader input, should it ? */
848 BEGIN_NVC0(push
, NVC0_3D(VB_INSTANCE_BASE
), 1);
849 PUSH_DATA (push
, info
->start_instance
);
852 for (i
= 0; i
< nvc0
->num_vtxbufs
&& !nvc0
->base
.vbo_dirty
; ++i
) {
853 if (!nvc0
->vtxbuf
[i
].buffer
)
855 if (nvc0
->vtxbuf
[i
].buffer
->flags
& PIPE_RESOURCE_FLAG_MAP_COHERENT
)
856 nvc0
->base
.vbo_dirty
= TRUE
;
859 if (!nvc0
->base
.vbo_dirty
&& nvc0
->idxbuf
.buffer
&&
860 nvc0
->idxbuf
.buffer
->flags
& PIPE_RESOURCE_FLAG_MAP_COHERENT
)
861 nvc0
->base
.vbo_dirty
= TRUE
;
863 if (nvc0
->base
.vbo_dirty
) {
864 if (nvc0
->screen
->eng3d
->oclass
< GM107_3D_CLASS
)
865 IMMED_NVC0(push
, NVC0_3D(VERTEX_ARRAY_FLUSH
), 0);
866 nvc0
->base
.vbo_dirty
= FALSE
;
870 boolean shorten
= info
->max_index
<= 65535;
872 if (info
->primitive_restart
!= nvc0
->state
.prim_restart
) {
873 if (info
->primitive_restart
) {
874 BEGIN_NVC0(push
, NVC0_3D(PRIM_RESTART_ENABLE
), 2);
876 PUSH_DATA (push
, info
->restart_index
);
878 if (info
->restart_index
> 65535)
881 IMMED_NVC0(push
, NVC0_3D(PRIM_RESTART_ENABLE
), 0);
883 nvc0
->state
.prim_restart
= info
->primitive_restart
;
885 if (info
->primitive_restart
) {
886 BEGIN_NVC0(push
, NVC0_3D(PRIM_RESTART_INDEX
), 1);
887 PUSH_DATA (push
, info
->restart_index
);
889 if (info
->restart_index
> 65535)
893 nvc0_draw_elements(nvc0
, shorten
,
894 info
->mode
, info
->start
, info
->count
,
895 info
->instance_count
, info
->index_bias
);
897 if (unlikely(info
->count_from_stream_output
)) {
898 nvc0_draw_stream_output(nvc0
, info
);
900 nvc0_draw_arrays(nvc0
,
901 info
->mode
, info
->start
, info
->count
,
902 info
->instance_count
);
904 push
->kick_notify
= nvc0_default_kick_notify
;
906 nvc0_release_user_vbufs(nvc0
);
908 nouveau_pushbuf_bufctx(push
, NULL
);