2 * Copyright 2008 Ben Skeggs
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"
28 #include "nv50_context.h"
31 nv50_push_elements_u08(struct nv50_context
*, uint8_t *, unsigned);
34 nv50_push_elements_u16(struct nv50_context
*, uint16_t *, unsigned);
37 nv50_push_elements_u32(struct nv50_context
*, uint32_t *, unsigned);
40 nv50_push_arrays(struct nv50_context
*, unsigned, unsigned);
42 #define NV50_USING_LOATHED_EDGEFLAG(ctx) ((ctx)->vertprog->cfg.edgeflag_in < 16)
44 static INLINE
unsigned
45 nv50_prim(unsigned mode
)
48 case PIPE_PRIM_POINTS
: return NV50TCL_VERTEX_BEGIN_POINTS
;
49 case PIPE_PRIM_LINES
: return NV50TCL_VERTEX_BEGIN_LINES
;
50 case PIPE_PRIM_LINE_LOOP
: return NV50TCL_VERTEX_BEGIN_LINE_LOOP
;
51 case PIPE_PRIM_LINE_STRIP
: return NV50TCL_VERTEX_BEGIN_LINE_STRIP
;
52 case PIPE_PRIM_TRIANGLES
: return NV50TCL_VERTEX_BEGIN_TRIANGLES
;
53 case PIPE_PRIM_TRIANGLE_STRIP
:
54 return NV50TCL_VERTEX_BEGIN_TRIANGLE_STRIP
;
55 case PIPE_PRIM_TRIANGLE_FAN
: return NV50TCL_VERTEX_BEGIN_TRIANGLE_FAN
;
56 case PIPE_PRIM_QUADS
: return NV50TCL_VERTEX_BEGIN_QUADS
;
57 case PIPE_PRIM_QUAD_STRIP
: return NV50TCL_VERTEX_BEGIN_QUAD_STRIP
;
58 case PIPE_PRIM_POLYGON
: return NV50TCL_VERTEX_BEGIN_POLYGON
;
59 case PIPE_PRIM_LINES_ADJACENCY
:
60 return NV50TCL_VERTEX_BEGIN_LINES_ADJACENCY
;
61 case PIPE_PRIM_LINE_STRIP_ADJACENCY
:
62 return NV50TCL_VERTEX_BEGIN_LINE_STRIP_ADJACENCY
;
63 case PIPE_PRIM_TRIANGLES_ADJACENCY
:
64 return NV50TCL_VERTEX_BEGIN_TRIANGLES_ADJACENCY
;
65 case PIPE_PRIM_TRIANGLE_STRIP_ADJACENCY
:
66 return NV50TCL_VERTEX_BEGIN_TRIANGLE_STRIP_ADJACENCY
;
71 NOUVEAU_ERR("invalid primitive type %d\n", mode
);
72 return NV50TCL_VERTEX_BEGIN_POINTS
;
75 static INLINE
uint32_t
76 nv50_vbo_type_to_hw(enum pipe_format format
)
78 const struct util_format_description
*desc
;
80 desc
= util_format_description(format
);
83 switch (desc
->channel
[0].type
) {
84 case UTIL_FORMAT_TYPE_FLOAT
:
85 return NV50TCL_VERTEX_ARRAY_ATTRIB_TYPE_FLOAT
;
86 case UTIL_FORMAT_TYPE_UNSIGNED
:
87 if (desc
->channel
[0].normalized
) {
88 return NV50TCL_VERTEX_ARRAY_ATTRIB_TYPE_UNORM
;
90 return NV50TCL_VERTEX_ARRAY_ATTRIB_TYPE_USCALED
;
91 case UTIL_FORMAT_TYPE_SIGNED
:
92 if (desc
->channel
[0].normalized
) {
93 return NV50TCL_VERTEX_ARRAY_ATTRIB_TYPE_SNORM
;
95 return NV50TCL_VERTEX_ARRAY_ATTRIB_TYPE_SSCALED
;
97 case PIPE_FORMAT_TYPE_UINT:
98 return NV50TCL_VERTEX_ARRAY_ATTRIB_TYPE_UINT;
99 case PIPE_FORMAT_TYPE_SINT:
100 return NV50TCL_VERTEX_ARRAY_ATTRIB_TYPE_SINT; */
106 static INLINE
uint32_t
107 nv50_vbo_size_to_hw(unsigned size
, unsigned nr_c
)
109 static const uint32_t hw_values
[] = {
111 NV50TCL_VERTEX_ARRAY_ATTRIB_FORMAT_8
,
112 NV50TCL_VERTEX_ARRAY_ATTRIB_FORMAT_8_8
,
113 NV50TCL_VERTEX_ARRAY_ATTRIB_FORMAT_8_8_8
,
114 NV50TCL_VERTEX_ARRAY_ATTRIB_FORMAT_8_8_8_8
,
115 NV50TCL_VERTEX_ARRAY_ATTRIB_FORMAT_16
,
116 NV50TCL_VERTEX_ARRAY_ATTRIB_FORMAT_16_16
,
117 NV50TCL_VERTEX_ARRAY_ATTRIB_FORMAT_16_16_16
,
118 NV50TCL_VERTEX_ARRAY_ATTRIB_FORMAT_16_16_16_16
,
120 NV50TCL_VERTEX_ARRAY_ATTRIB_FORMAT_32
,
121 NV50TCL_VERTEX_ARRAY_ATTRIB_FORMAT_32_32
,
122 NV50TCL_VERTEX_ARRAY_ATTRIB_FORMAT_32_32_32
,
123 NV50TCL_VERTEX_ARRAY_ATTRIB_FORMAT_32_32_32_32
};
125 /* we'd also have R11G11B10 and R10G10B10A2 */
127 assert(nr_c
> 0 && nr_c
<= 4);
133 return hw_values
[size
+ (nr_c
- 1)];
136 static INLINE
uint32_t
137 nv50_vbo_vtxelt_to_hw(struct pipe_vertex_element
*ve
)
139 uint32_t hw_type
, hw_size
;
140 enum pipe_format pf
= ve
->src_format
;
141 const struct util_format_description
*desc
;
142 unsigned size
, nr_components
;
144 desc
= util_format_description(pf
);
147 size
= util_format_get_component_bits(pf
, UTIL_FORMAT_COLORSPACE_RGB
, 0);
148 nr_components
= util_format_get_nr_components(pf
);
150 hw_type
= nv50_vbo_type_to_hw(pf
);
151 hw_size
= nv50_vbo_size_to_hw(size
, nr_components
);
153 if (!hw_type
|| !hw_size
) {
154 NOUVEAU_ERR("unsupported vbo format: %s\n", util_format_name(pf
));
159 if (desc
->swizzle
[0] == UTIL_FORMAT_SWIZZLE_Z
) /* BGRA */
160 hw_size
|= (1 << 31); /* no real swizzle bits :-( */
162 return (hw_type
| hw_size
);
165 /* For instanced drawing from user buffers, hitting the FIFO repeatedly
166 * with the same vertex data is probably worse than uploading all data.
169 nv50_upload_vtxbuf(struct nv50_context
*nv50
, unsigned i
)
171 struct nv50_screen
*nscreen
= nv50
->screen
;
172 struct pipe_screen
*pscreen
= &nscreen
->base
.base
;
173 struct pipe_buffer
*buf
= nscreen
->strm_vbuf
[i
];
174 struct pipe_vertex_buffer
*vb
= &nv50
->vtxbuf
[i
];
176 unsigned size
= align(vb
->buffer
->size
, 4096);
178 if (buf
&& buf
->size
< size
)
179 pipe_buffer_reference(&nscreen
->strm_vbuf
[i
], NULL
);
181 if (!nscreen
->strm_vbuf
[i
]) {
182 nscreen
->strm_vbuf
[i
] = pipe_buffer_create(
183 pscreen
, 0, PIPE_BUFFER_USAGE_VERTEX
, size
);
184 buf
= nscreen
->strm_vbuf
[i
];
187 src
= pipe_buffer_map(pscreen
, vb
->buffer
, PIPE_BUFFER_USAGE_CPU_READ
);
190 src
+= vb
->buffer_offset
;
192 size
= (vb
->max_index
+ 1) * vb
->stride
+ 16; /* + 16 is for stride 0 */
193 if (vb
->buffer_offset
+ size
> vb
->buffer
->size
)
194 size
= vb
->buffer
->size
- vb
->buffer_offset
;
196 pipe_buffer_write(pscreen
, buf
, vb
->buffer_offset
, size
, src
);
197 pipe_buffer_unmap(pscreen
, vb
->buffer
);
199 vb
->buffer
= buf
; /* don't pipe_reference, this is a private copy */
204 nv50_upload_user_vbufs(struct nv50_context
*nv50
)
209 nv50
->dirty
|= NV50_NEW_ARRAYS
;
210 if (!(nv50
->dirty
& NV50_NEW_ARRAYS
))
213 for (i
= 0; i
< nv50
->vtxbuf_nr
; ++i
) {
214 if (nv50
->vtxbuf
[i
].buffer
->usage
& PIPE_BUFFER_USAGE_VERTEX
)
216 nv50_upload_vtxbuf(nv50
, i
);
221 nv50_set_static_vtxattr(struct nv50_context
*nv50
, unsigned i
, void *data
)
223 struct nouveau_grobj
*tesla
= nv50
->screen
->tesla
;
224 struct nouveau_channel
*chan
= tesla
->channel
;
226 enum pipe_format pf
= nv50
->vtxelt
->pipe
[i
].src_format
;
227 unsigned nr_components
= util_format_get_nr_components(pf
);
229 util_format_read_4f(pf
, v
, 0, data
, 0, 0, 0, 1, 1);
231 switch (nr_components
) {
233 BEGIN_RING(chan
, tesla
, NV50TCL_VTX_ATTR_4F_X(i
), 4);
234 OUT_RINGf (chan
, v
[0]);
235 OUT_RINGf (chan
, v
[1]);
236 OUT_RINGf (chan
, v
[2]);
237 OUT_RINGf (chan
, v
[3]);
240 BEGIN_RING(chan
, tesla
, NV50TCL_VTX_ATTR_3F_X(i
), 3);
241 OUT_RINGf (chan
, v
[0]);
242 OUT_RINGf (chan
, v
[1]);
243 OUT_RINGf (chan
, v
[2]);
246 BEGIN_RING(chan
, tesla
, NV50TCL_VTX_ATTR_2F_X(i
), 2);
247 OUT_RINGf (chan
, v
[0]);
248 OUT_RINGf (chan
, v
[1]);
251 BEGIN_RING(chan
, tesla
, NV50TCL_VTX_ATTR_1F(i
), 1);
252 OUT_RINGf (chan
, v
[0]);
261 init_per_instance_arrays_immd(struct nv50_context
*nv50
,
262 unsigned startInstance
,
263 unsigned pos
[16], unsigned step
[16])
265 struct nouveau_bo
*bo
;
266 unsigned i
, b
, count
= 0;
268 for (i
= 0; i
< nv50
->vtxelt
->num_elements
; ++i
) {
269 if (!nv50
->vtxelt
->pipe
[i
].instance_divisor
)
272 b
= nv50
->vtxelt
->pipe
[i
].vertex_buffer_index
;
274 pos
[i
] = nv50
->vtxelt
->pipe
[i
].src_offset
+
275 nv50
->vtxbuf
[b
].buffer_offset
+
276 startInstance
* nv50
->vtxbuf
[b
].stride
;
277 step
[i
] = startInstance
%
278 nv50
->vtxelt
->pipe
[i
].instance_divisor
;
280 bo
= nouveau_bo(nv50
->vtxbuf
[b
].buffer
);
282 nouveau_bo_map(bo
, NOUVEAU_BO_RD
);
284 nv50_set_static_vtxattr(nv50
, i
, (uint8_t *)bo
->map
+ pos
[i
]);
291 init_per_instance_arrays(struct nv50_context
*nv50
,
292 unsigned startInstance
,
293 unsigned pos
[16], unsigned step
[16])
295 struct nouveau_grobj
*tesla
= nv50
->screen
->tesla
;
296 struct nouveau_channel
*chan
= tesla
->channel
;
297 struct nouveau_bo
*bo
;
298 struct nouveau_stateobj
*so
;
299 unsigned i
, b
, count
= 0, num_elements
= nv50
->vtxelt
->num_elements
;
300 const uint32_t rl
= NOUVEAU_BO_VRAM
| NOUVEAU_BO_GART
| NOUVEAU_BO_RD
;
303 return init_per_instance_arrays_immd(nv50
, startInstance
,
306 so
= so_new(num_elements
, num_elements
* 2, num_elements
* 2);
308 for (i
= 0; i
< nv50
->vtxelt
->num_elements
; ++i
) {
309 if (!nv50
->vtxelt
->pipe
[i
].instance_divisor
)
312 b
= nv50
->vtxelt
->pipe
[i
].vertex_buffer_index
;
314 pos
[i
] = nv50
->vtxelt
->pipe
[i
].src_offset
+
315 nv50
->vtxbuf
[b
].buffer_offset
+
316 startInstance
* nv50
->vtxbuf
[b
].stride
;
318 if (!startInstance
) {
322 step
[i
] = startInstance
%
323 nv50
->vtxelt
->pipe
[i
].instance_divisor
;
325 bo
= nouveau_bo(nv50
->vtxbuf
[b
].buffer
);
327 so_method(so
, tesla
, NV50TCL_VERTEX_ARRAY_START_HIGH(i
), 2);
328 so_reloc (so
, bo
, pos
[i
], rl
| NOUVEAU_BO_HIGH
, 0, 0);
329 so_reloc (so
, bo
, pos
[i
], rl
| NOUVEAU_BO_LOW
, 0, 0);
332 if (count
&& startInstance
) {
333 so_ref (so
, &nv50
->state
.instbuf
); /* for flush notify */
334 so_emit(chan
, nv50
->state
.instbuf
);
342 step_per_instance_arrays_immd(struct nv50_context
*nv50
,
343 unsigned pos
[16], unsigned step
[16])
345 struct nouveau_bo
*bo
;
348 for (i
= 0; i
< nv50
->vtxelt
->num_elements
; ++i
) {
349 if (!nv50
->vtxelt
->pipe
[i
].instance_divisor
)
351 if (++step
[i
] != nv50
->vtxelt
->pipe
[i
].instance_divisor
)
353 b
= nv50
->vtxelt
->pipe
[i
].vertex_buffer_index
;
354 bo
= nouveau_bo(nv50
->vtxbuf
[b
].buffer
);
357 pos
[i
] += nv50
->vtxbuf
[b
].stride
;
359 nv50_set_static_vtxattr(nv50
, i
, (uint8_t *)bo
->map
+ pos
[i
]);
364 step_per_instance_arrays(struct nv50_context
*nv50
,
365 unsigned pos
[16], unsigned step
[16])
367 struct nouveau_grobj
*tesla
= nv50
->screen
->tesla
;
368 struct nouveau_channel
*chan
= tesla
->channel
;
369 struct nouveau_bo
*bo
;
370 struct nouveau_stateobj
*so
;
371 unsigned i
, b
, num_elements
= nv50
->vtxelt
->num_elements
;
372 const uint32_t rl
= NOUVEAU_BO_VRAM
| NOUVEAU_BO_GART
| NOUVEAU_BO_RD
;
374 if (nv50
->vbo_fifo
) {
375 step_per_instance_arrays_immd(nv50
, pos
, step
);
379 so
= so_new(num_elements
, num_elements
* 2, num_elements
* 2);
381 for (i
= 0; i
< nv50
->vtxelt
->num_elements
; ++i
) {
382 if (!nv50
->vtxelt
->pipe
[i
].instance_divisor
)
384 b
= nv50
->vtxelt
->pipe
[i
].vertex_buffer_index
;
386 if (++step
[i
] == nv50
->vtxelt
->pipe
[i
].instance_divisor
) {
388 pos
[i
] += nv50
->vtxbuf
[b
].stride
;
391 bo
= nouveau_bo(nv50
->vtxbuf
[b
].buffer
);
393 so_method(so
, tesla
, NV50TCL_VERTEX_ARRAY_START_HIGH(i
), 2);
394 so_reloc (so
, bo
, pos
[i
], rl
| NOUVEAU_BO_HIGH
, 0, 0);
395 so_reloc (so
, bo
, pos
[i
], rl
| NOUVEAU_BO_LOW
, 0, 0);
398 so_ref (so
, &nv50
->state
.instbuf
); /* for flush notify */
401 so_emit(chan
, nv50
->state
.instbuf
);
405 nv50_unmap_vbufs(struct nv50_context
*nv50
)
409 for (i
= 0; i
< nv50
->vtxbuf_nr
; ++i
)
410 if (nouveau_bo(nv50
->vtxbuf
[i
].buffer
)->map
)
411 nouveau_bo_unmap(nouveau_bo(nv50
->vtxbuf
[i
].buffer
));
415 nv50_draw_arrays_instanced(struct pipe_context
*pipe
,
416 unsigned mode
, unsigned start
, unsigned count
,
417 unsigned startInstance
, unsigned instanceCount
)
419 struct nv50_context
*nv50
= nv50_context(pipe
);
420 struct nouveau_channel
*chan
= nv50
->screen
->tesla
->channel
;
421 struct nouveau_grobj
*tesla
= nv50
->screen
->tesla
;
422 unsigned i
, nz_divisors
;
423 unsigned step
[16], pos
[16];
425 if (!NV50_USING_LOATHED_EDGEFLAG(nv50
))
426 nv50_upload_user_vbufs(nv50
);
428 if (!nv50_state_validate(nv50
, 0))
430 chan
->flush_notify
= nv50_state_flush_notify
;
432 nz_divisors
= init_per_instance_arrays(nv50
, startInstance
, pos
, step
);
434 BEGIN_RING(chan
, tesla
, NV50TCL_CB_ADDR
, 2);
435 OUT_RING (chan
, NV50_CB_AUX
| (24 << 8));
436 OUT_RING (chan
, startInstance
);
438 BEGIN_RING(chan
, tesla
, NV50TCL_VERTEX_BEGIN
, 1);
439 OUT_RING (chan
, nv50_prim(mode
));
442 nv50_push_arrays(nv50
, start
, count
);
444 BEGIN_RING(chan
, tesla
, NV50TCL_VERTEX_BUFFER_FIRST
, 2);
445 OUT_RING (chan
, start
);
446 OUT_RING (chan
, count
);
448 BEGIN_RING(chan
, tesla
, NV50TCL_VERTEX_END
, 1);
451 for (i
= 1; i
< instanceCount
; i
++) {
452 if (nz_divisors
) /* any non-zero array divisors ? */
453 step_per_instance_arrays(nv50
, pos
, step
);
455 BEGIN_RING(chan
, tesla
, NV50TCL_VERTEX_BEGIN
, 1);
456 OUT_RING (chan
, nv50_prim(mode
) | (1 << 28));
459 nv50_push_arrays(nv50
, start
, count
);
461 BEGIN_RING(chan
, tesla
, NV50TCL_VERTEX_BUFFER_FIRST
, 2);
462 OUT_RING (chan
, start
);
463 OUT_RING (chan
, count
);
465 BEGIN_RING(chan
, tesla
, NV50TCL_VERTEX_END
, 1);
469 chan
->flush_notify
= NULL
;
470 nv50_unmap_vbufs(nv50
);
472 so_ref(NULL
, &nv50
->state
.instbuf
);
476 nv50_draw_arrays(struct pipe_context
*pipe
, unsigned mode
, unsigned start
,
479 struct nv50_context
*nv50
= nv50_context(pipe
);
480 struct nouveau_channel
*chan
= nv50
->screen
->tesla
->channel
;
481 struct nouveau_grobj
*tesla
= nv50
->screen
->tesla
;
484 if (!nv50_state_validate(nv50
, 11))
486 chan
->flush_notify
= nv50_state_flush_notify
;
488 BEGIN_RING(chan
, tesla
, 0x142c, 1);
490 BEGIN_RING(chan
, tesla
, 0x142c, 1);
493 BEGIN_RING(chan
, tesla
, NV50TCL_VERTEX_BEGIN
, 1);
494 OUT_RING (chan
, nv50_prim(mode
));
497 ret
= nv50_push_arrays(nv50
, start
, count
);
499 BEGIN_RING(chan
, tesla
, NV50TCL_VERTEX_BUFFER_FIRST
, 2);
500 OUT_RING (chan
, start
);
501 OUT_RING (chan
, count
);
504 BEGIN_RING(chan
, tesla
, NV50TCL_VERTEX_END
, 1);
507 chan
->flush_notify
= NULL
;
508 nv50_unmap_vbufs(nv50
);
510 /* XXX: not sure what to do if ret != TRUE: flush and retry?
515 static INLINE boolean
516 nv50_draw_elements_inline_u08(struct nv50_context
*nv50
, uint8_t *map
,
517 unsigned start
, unsigned count
)
519 struct nouveau_channel
*chan
= nv50
->screen
->tesla
->channel
;
520 struct nouveau_grobj
*tesla
= nv50
->screen
->tesla
;
525 return nv50_push_elements_u08(nv50
, map
, count
);
528 BEGIN_RING(chan
, tesla
, NV50TCL_VB_ELEMENT_U32
, 1);
529 OUT_RING (chan
, map
[0]);
535 unsigned nr
= count
> 2046 ? 2046 : count
;
538 BEGIN_RING_NI(chan
, tesla
, NV50TCL_VB_ELEMENT_U16
, nr
>> 1);
539 for (i
= 0; i
< nr
; i
+= 2)
540 OUT_RING (chan
, (map
[i
+ 1] << 16) | map
[i
]);
548 static INLINE boolean
549 nv50_draw_elements_inline_u16(struct nv50_context
*nv50
, uint16_t *map
,
550 unsigned start
, unsigned count
)
552 struct nouveau_channel
*chan
= nv50
->screen
->tesla
->channel
;
553 struct nouveau_grobj
*tesla
= nv50
->screen
->tesla
;
558 return nv50_push_elements_u16(nv50
, map
, count
);
561 BEGIN_RING(chan
, tesla
, NV50TCL_VB_ELEMENT_U32
, 1);
562 OUT_RING (chan
, map
[0]);
568 unsigned nr
= count
> 2046 ? 2046 : count
;
571 BEGIN_RING_NI(chan
, tesla
, NV50TCL_VB_ELEMENT_U16
, nr
>> 1);
572 for (i
= 0; i
< nr
; i
+= 2)
573 OUT_RING (chan
, (map
[i
+ 1] << 16) | map
[i
]);
581 static INLINE boolean
582 nv50_draw_elements_inline_u32(struct nv50_context
*nv50
, uint32_t *map
,
583 unsigned start
, unsigned count
)
585 struct nouveau_channel
*chan
= nv50
->screen
->tesla
->channel
;
586 struct nouveau_grobj
*tesla
= nv50
->screen
->tesla
;
591 return nv50_push_elements_u32(nv50
, map
, count
);
594 unsigned nr
= count
> 2047 ? 2047 : count
;
596 BEGIN_RING_NI(chan
, tesla
, NV50TCL_VB_ELEMENT_U32
, nr
);
597 OUT_RINGp (chan
, map
, nr
);
606 nv50_draw_elements_inline(struct nv50_context
*nv50
,
607 void *map
, unsigned indexSize
,
608 unsigned start
, unsigned count
)
612 nv50_draw_elements_inline_u08(nv50
, map
, start
, count
);
615 nv50_draw_elements_inline_u16(nv50
, map
, start
, count
);
618 nv50_draw_elements_inline_u32(nv50
, map
, start
, count
);
624 nv50_draw_elements_instanced(struct pipe_context
*pipe
,
625 struct pipe_buffer
*indexBuffer
,
627 unsigned mode
, unsigned start
, unsigned count
,
628 unsigned startInstance
, unsigned instanceCount
)
630 struct nv50_context
*nv50
= nv50_context(pipe
);
631 struct nouveau_grobj
*tesla
= nv50
->screen
->tesla
;
632 struct nouveau_channel
*chan
= tesla
->channel
;
633 struct pipe_screen
*pscreen
= pipe
->screen
;
635 unsigned i
, nz_divisors
;
636 unsigned step
[16], pos
[16];
638 map
= pipe_buffer_map(pscreen
, indexBuffer
, PIPE_BUFFER_USAGE_CPU_READ
);
640 if (!NV50_USING_LOATHED_EDGEFLAG(nv50
))
641 nv50_upload_user_vbufs(nv50
);
643 if (!nv50_state_validate(nv50
, 0))
645 chan
->flush_notify
= nv50_state_flush_notify
;
647 nz_divisors
= init_per_instance_arrays(nv50
, startInstance
, pos
, step
);
649 BEGIN_RING(chan
, tesla
, NV50TCL_CB_ADDR
, 2);
650 OUT_RING (chan
, NV50_CB_AUX
| (24 << 8));
651 OUT_RING (chan
, startInstance
);
653 BEGIN_RING(chan
, tesla
, NV50TCL_VERTEX_BEGIN
, 1);
654 OUT_RING (chan
, nv50_prim(mode
));
656 nv50_draw_elements_inline(nv50
, map
, indexSize
, start
, count
);
658 BEGIN_RING(chan
, tesla
, NV50TCL_VERTEX_END
, 1);
661 for (i
= 1; i
< instanceCount
; ++i
) {
662 if (nz_divisors
) /* any non-zero array divisors ? */
663 step_per_instance_arrays(nv50
, pos
, step
);
665 BEGIN_RING(chan
, tesla
, NV50TCL_VERTEX_BEGIN
, 1);
666 OUT_RING (chan
, nv50_prim(mode
) | (1 << 28));
668 nv50_draw_elements_inline(nv50
, map
, indexSize
, start
, count
);
670 BEGIN_RING(chan
, tesla
, NV50TCL_VERTEX_END
, 1);
674 chan
->flush_notify
= NULL
;
675 nv50_unmap_vbufs(nv50
);
677 so_ref(NULL
, &nv50
->state
.instbuf
);
681 nv50_draw_elements(struct pipe_context
*pipe
,
682 struct pipe_buffer
*indexBuffer
, unsigned indexSize
,
683 unsigned mode
, unsigned start
, unsigned count
)
685 struct nv50_context
*nv50
= nv50_context(pipe
);
686 struct nouveau_channel
*chan
= nv50
->screen
->tesla
->channel
;
687 struct nouveau_grobj
*tesla
= nv50
->screen
->tesla
;
688 struct pipe_screen
*pscreen
= pipe
->screen
;
691 if (!nv50_state_validate(nv50
, 14))
693 chan
->flush_notify
= nv50_state_flush_notify
;
695 BEGIN_RING(chan
, tesla
, 0x142c, 1);
697 BEGIN_RING(chan
, tesla
, 0x142c, 1);
700 BEGIN_RING(chan
, tesla
, NV50TCL_VERTEX_BEGIN
, 1);
701 OUT_RING (chan
, nv50_prim(mode
));
703 if (!nv50
->vbo_fifo
&& indexSize
== 4) {
704 BEGIN_RING(chan
, tesla
, NV50TCL_VB_ELEMENT_U32
| 0x30000, 0);
705 OUT_RING (chan
, count
);
706 nouveau_pushbuf_submit(chan
, nouveau_bo(indexBuffer
),
707 start
<< 2, count
<< 2);
709 if (!nv50
->vbo_fifo
&& indexSize
== 2) {
710 unsigned vb_start
= (start
& ~1);
711 unsigned vb_end
= (start
+ count
+ 1) & ~1;
712 unsigned dwords
= (vb_end
- vb_start
) >> 1;
714 BEGIN_RING(chan
, tesla
, NV50TCL_VB_ELEMENT_U16_SETUP
, 1);
715 OUT_RING (chan
, ((start
& 1) << 31) | count
);
716 BEGIN_RING(chan
, tesla
, NV50TCL_VB_ELEMENT_U16
| 0x30000, 0);
717 OUT_RING (chan
, dwords
);
718 nouveau_pushbuf_submit(chan
, nouveau_bo(indexBuffer
),
719 vb_start
<< 1, dwords
<< 2);
720 BEGIN_RING(chan
, tesla
, NV50TCL_VB_ELEMENT_U16_SETUP
, 1);
723 map
= pipe_buffer_map(pscreen
, indexBuffer
,
724 PIPE_BUFFER_USAGE_CPU_READ
);
725 nv50_draw_elements_inline(nv50
, map
, indexSize
, start
, count
);
726 nv50_unmap_vbufs(nv50
);
727 pipe_buffer_unmap(pscreen
, indexBuffer
);
730 BEGIN_RING(chan
, tesla
, NV50TCL_VERTEX_END
, 1);
732 chan
->flush_notify
= NULL
;
735 static INLINE boolean
736 nv50_vbo_static_attrib(struct nv50_context
*nv50
, unsigned attrib
,
737 struct nouveau_stateobj
**pso
,
738 struct pipe_vertex_element
*ve
,
739 struct pipe_vertex_buffer
*vb
)
742 struct nouveau_stateobj
*so
;
743 struct nouveau_grobj
*tesla
= nv50
->screen
->tesla
;
744 struct nouveau_bo
*bo
= nouveau_bo(vb
->buffer
);
747 unsigned nr_components
= util_format_get_nr_components(ve
->src_format
);
749 ret
= nouveau_bo_map(bo
, NOUVEAU_BO_RD
);
753 util_format_read_4f(ve
->src_format
, v
, 0, (uint8_t *)bo
->map
+
754 (vb
->buffer_offset
+ ve
->src_offset
), 0,
758 *pso
= so
= so_new(nv50
->vtxelt
->num_elements
,
759 nv50
->vtxelt
->num_elements
* 4, 0);
761 switch (nr_components
) {
763 so_method(so
, tesla
, NV50TCL_VTX_ATTR_4F_X(attrib
), 4);
764 so_data (so
, fui(v
[0]));
765 so_data (so
, fui(v
[1]));
766 so_data (so
, fui(v
[2]));
767 so_data (so
, fui(v
[3]));
770 so_method(so
, tesla
, NV50TCL_VTX_ATTR_3F_X(attrib
), 3);
771 so_data (so
, fui(v
[0]));
772 so_data (so
, fui(v
[1]));
773 so_data (so
, fui(v
[2]));
776 so_method(so
, tesla
, NV50TCL_VTX_ATTR_2F_X(attrib
), 2);
777 so_data (so
, fui(v
[0]));
778 so_data (so
, fui(v
[1]));
781 if (attrib
== nv50
->vertprog
->cfg
.edgeflag_in
) {
782 so_method(so
, tesla
, NV50TCL_EDGEFLAG_ENABLE
, 1);
783 so_data (so
, v
[0] ? 1 : 0);
785 so_method(so
, tesla
, NV50TCL_VTX_ATTR_1F(attrib
), 1);
786 so_data (so
, fui(v
[0]));
789 nouveau_bo_unmap(bo
);
793 nouveau_bo_unmap(bo
);
798 nv50_vtxelt_construct(struct nv50_vtxelt_stateobj
*cso
)
802 for (i
= 0; i
< cso
->num_elements
; ++i
) {
803 struct pipe_vertex_element
*ve
= &cso
->pipe
[i
];
805 cso
->hw
[i
] = nv50_vbo_vtxelt_to_hw(ve
);
809 struct nouveau_stateobj
*
810 nv50_vbo_validate(struct nv50_context
*nv50
)
812 struct nouveau_grobj
*tesla
= nv50
->screen
->tesla
;
813 struct nouveau_stateobj
*vtxbuf
, *vtxfmt
, *vtxattr
;
816 /* don't validate if Gallium took away our buffers */
817 if (nv50
->vtxbuf_nr
== 0)
821 for (i
= 0; i
< nv50
->vtxbuf_nr
; ++i
)
822 if (nv50
->vtxbuf
[i
].stride
&&
823 !(nv50
->vtxbuf
[i
].buffer
->usage
& PIPE_BUFFER_USAGE_VERTEX
))
824 nv50
->vbo_fifo
= 0xffff;
826 if (NV50_USING_LOATHED_EDGEFLAG(nv50
))
827 nv50
->vbo_fifo
= 0xffff; /* vertprog can't set edgeflag */
829 n_ve
= MAX2(nv50
->vtxelt
->num_elements
, nv50
->state
.vtxelt_nr
);
832 vtxbuf
= so_new(n_ve
* 2, n_ve
* 5, nv50
->vtxelt
->num_elements
* 4);
833 vtxfmt
= so_new(1, n_ve
, 0);
834 so_method(vtxfmt
, tesla
, NV50TCL_VERTEX_ARRAY_ATTRIB(0), n_ve
);
836 for (i
= 0; i
< nv50
->vtxelt
->num_elements
; i
++) {
837 struct pipe_vertex_element
*ve
= &nv50
->vtxelt
->pipe
[i
];
838 struct pipe_vertex_buffer
*vb
=
839 &nv50
->vtxbuf
[ve
->vertex_buffer_index
];
840 struct nouveau_bo
*bo
= nouveau_bo(vb
->buffer
);
841 uint32_t hw
= nv50
->vtxelt
->hw
[i
];
844 nv50_vbo_static_attrib(nv50
, i
, &vtxattr
, ve
, vb
)) {
845 so_data(vtxfmt
, hw
| (1 << 4));
847 so_method(vtxbuf
, tesla
,
848 NV50TCL_VERTEX_ARRAY_FORMAT(i
), 1);
851 nv50
->vbo_fifo
&= ~(1 << i
);
855 if (nv50
->vbo_fifo
) {
856 so_data (vtxfmt
, hw
|
857 (ve
->instance_divisor
? (1 << 4) : i
));
858 so_method(vtxbuf
, tesla
,
859 NV50TCL_VERTEX_ARRAY_FORMAT(i
), 1);
863 so_data(vtxfmt
, hw
| i
);
865 so_method(vtxbuf
, tesla
, NV50TCL_VERTEX_ARRAY_FORMAT(i
), 3);
866 so_data (vtxbuf
, 0x20000000 |
867 (ve
->instance_divisor
? 0 : vb
->stride
));
868 so_reloc (vtxbuf
, bo
, vb
->buffer_offset
+
869 ve
->src_offset
, NOUVEAU_BO_VRAM
| NOUVEAU_BO_GART
|
870 NOUVEAU_BO_RD
| NOUVEAU_BO_HIGH
, 0, 0);
871 so_reloc (vtxbuf
, bo
, vb
->buffer_offset
+
872 ve
->src_offset
, NOUVEAU_BO_VRAM
| NOUVEAU_BO_GART
|
873 NOUVEAU_BO_RD
| NOUVEAU_BO_LOW
, 0, 0);
875 /* vertex array limits */
876 so_method(vtxbuf
, tesla
, NV50TCL_VERTEX_ARRAY_LIMIT_HIGH(i
), 2);
877 so_reloc (vtxbuf
, bo
, vb
->buffer
->size
- 1,
878 NOUVEAU_BO_VRAM
| NOUVEAU_BO_GART
| NOUVEAU_BO_RD
|
879 NOUVEAU_BO_HIGH
, 0, 0);
880 so_reloc (vtxbuf
, bo
, vb
->buffer
->size
- 1,
881 NOUVEAU_BO_VRAM
| NOUVEAU_BO_GART
| NOUVEAU_BO_RD
|
882 NOUVEAU_BO_LOW
, 0, 0);
884 for (; i
< n_ve
; ++i
) {
885 so_data (vtxfmt
, 0x7e080010);
887 so_method(vtxbuf
, tesla
, NV50TCL_VERTEX_ARRAY_FORMAT(i
), 1);
890 nv50
->state
.vtxelt_nr
= nv50
->vtxelt
->num_elements
;
892 so_ref (vtxbuf
, &nv50
->state
.vtxbuf
);
893 so_ref (vtxattr
, &nv50
->state
.vtxattr
);
894 so_ref (NULL
, &vtxbuf
);
895 so_ref (NULL
, &vtxattr
);
899 typedef void (*pfn_push
)(struct nouveau_channel
*, void *);
901 struct nv50_vbo_emitctx
911 unsigned ve_edgeflag
;
915 emit_vtx_next(struct nouveau_channel
*chan
, struct nv50_vbo_emitctx
*emit
)
919 for (i
= 0; i
< emit
->nr_ve
; ++i
) {
920 emit
->push
[i
](chan
, emit
->map
[i
]);
921 emit
->map
[i
] += emit
->stride
[i
];
926 emit_vtx(struct nouveau_channel
*chan
, struct nv50_vbo_emitctx
*emit
,
931 for (i
= 0; i
< emit
->nr_ve
; ++i
)
932 emit
->push
[i
](chan
, emit
->map
[i
] + emit
->stride
[i
] * vi
);
935 static INLINE boolean
936 nv50_map_vbufs(struct nv50_context
*nv50
)
940 for (i
= 0; i
< nv50
->vtxbuf_nr
; ++i
) {
941 struct pipe_vertex_buffer
*vb
= &nv50
->vtxbuf
[i
];
942 unsigned size
= vb
->stride
* (vb
->max_index
+ 1) + 16;
944 if (nouveau_bo(vb
->buffer
)->map
)
947 size
= vb
->stride
* (vb
->max_index
+ 1) + 16;
948 size
= MIN2(size
, vb
->buffer
->size
);
950 size
= vb
->buffer
->size
;
952 if (nouveau_bo_map_range(nouveau_bo(vb
->buffer
),
953 0, size
, NOUVEAU_BO_RD
))
957 if (i
== nv50
->vtxbuf_nr
)
960 nouveau_bo_unmap(nouveau_bo(nv50
->vtxbuf
[i
].buffer
));
965 emit_b32_1(struct nouveau_channel
*chan
, void *data
)
969 OUT_RING(chan
, v
[0]);
973 emit_b32_2(struct nouveau_channel
*chan
, void *data
)
977 OUT_RING(chan
, v
[0]);
978 OUT_RING(chan
, v
[1]);
982 emit_b32_3(struct nouveau_channel
*chan
, void *data
)
986 OUT_RING(chan
, v
[0]);
987 OUT_RING(chan
, v
[1]);
988 OUT_RING(chan
, v
[2]);
992 emit_b32_4(struct nouveau_channel
*chan
, void *data
)
996 OUT_RING(chan
, v
[0]);
997 OUT_RING(chan
, v
[1]);
998 OUT_RING(chan
, v
[2]);
999 OUT_RING(chan
, v
[3]);
1003 emit_b16_1(struct nouveau_channel
*chan
, void *data
)
1007 OUT_RING(chan
, v
[0]);
1011 emit_b16_3(struct nouveau_channel
*chan
, void *data
)
1015 OUT_RING(chan
, (v
[1] << 16) | v
[0]);
1016 OUT_RING(chan
, v
[2]);
1020 emit_b08_1(struct nouveau_channel
*chan
, void *data
)
1024 OUT_RING(chan
, v
[0]);
1028 emit_b08_3(struct nouveau_channel
*chan
, void *data
)
1032 OUT_RING(chan
, (v
[2] << 16) | (v
[1] << 8) | v
[0]);
1036 emit_prepare(struct nv50_context
*nv50
, struct nv50_vbo_emitctx
*emit
,
1041 if (nv50_map_vbufs(nv50
) == FALSE
)
1044 emit
->ve_edgeflag
= nv50
->vertprog
->cfg
.edgeflag_in
;
1046 emit
->edgeflag
= 0.5f
;
1048 emit
->vtx_dwords
= 0;
1050 for (i
= 0; i
< nv50
->vtxelt
->num_elements
; ++i
) {
1051 struct pipe_vertex_element
*ve
;
1052 struct pipe_vertex_buffer
*vb
;
1053 unsigned n
, size
, nr_components
;
1054 const struct util_format_description
*desc
;
1056 ve
= &nv50
->vtxelt
->pipe
[i
];
1057 vb
= &nv50
->vtxbuf
[ve
->vertex_buffer_index
];
1058 if (!(nv50
->vbo_fifo
& (1 << i
)) || ve
->instance_divisor
)
1062 emit
->stride
[n
] = vb
->stride
;
1063 emit
->map
[n
] = (uint8_t *)nouveau_bo(vb
->buffer
)->map
+
1065 (start
* vb
->stride
+ ve
->src_offset
);
1067 desc
= util_format_description(ve
->src_format
);
1070 size
= util_format_get_component_bits(
1071 ve
->src_format
, UTIL_FORMAT_COLORSPACE_RGB
, 0);
1072 nr_components
= util_format_get_nr_components(ve
->src_format
);
1074 assert(nr_components
> 0 && nr_components
<= 4);
1076 /* It shouldn't be necessary to push the implicit 1s
1077 * for case 3 and size 8 cases 1, 2, 3.
1081 NOUVEAU_ERR("unsupported vtxelt size: %u\n", size
);
1084 switch (nr_components
) {
1085 case 1: emit
->push
[n
] = emit_b32_1
; break;
1086 case 2: emit
->push
[n
] = emit_b32_2
; break;
1087 case 3: emit
->push
[n
] = emit_b32_3
; break;
1088 case 4: emit
->push
[n
] = emit_b32_4
; break;
1090 emit
->vtx_dwords
+= nr_components
;
1093 switch (nr_components
) {
1094 case 1: emit
->push
[n
] = emit_b16_1
; break;
1095 case 2: emit
->push
[n
] = emit_b32_1
; break;
1096 case 3: emit
->push
[n
] = emit_b16_3
; break;
1097 case 4: emit
->push
[n
] = emit_b32_2
; break;
1099 emit
->vtx_dwords
+= (nr_components
+ 1) >> 1;
1102 switch (nr_components
) {
1103 case 1: emit
->push
[n
] = emit_b08_1
; break;
1104 case 2: emit
->push
[n
] = emit_b16_1
; break;
1105 case 3: emit
->push
[n
] = emit_b08_3
; break;
1106 case 4: emit
->push
[n
] = emit_b32_1
; break;
1108 emit
->vtx_dwords
+= 1;
1113 emit
->vtx_max
= 512 / emit
->vtx_dwords
;
1114 if (emit
->ve_edgeflag
< 16)
1121 set_edgeflag(struct nouveau_channel
*chan
,
1122 struct nouveau_grobj
*tesla
,
1123 struct nv50_vbo_emitctx
*emit
, uint32_t index
)
1125 unsigned i
= emit
->ve_edgeflag
;
1128 float f
= *((float *)(emit
->map
[i
] + index
* emit
->stride
[i
]));
1130 if (emit
->edgeflag
!= f
) {
1133 BEGIN_RING(chan
, tesla
, 0x15e4, 1);
1134 OUT_RING (chan
, f
? 1 : 0);
1140 nv50_push_arrays(struct nv50_context
*nv50
, unsigned start
, unsigned count
)
1142 struct nouveau_channel
*chan
= nv50
->screen
->base
.channel
;
1143 struct nouveau_grobj
*tesla
= nv50
->screen
->tesla
;
1144 struct nv50_vbo_emitctx emit
;
1146 if (emit_prepare(nv50
, &emit
, start
) == FALSE
)
1150 unsigned i
, dw
, nr
= MIN2(count
, emit
.vtx_max
);
1151 dw
= nr
* emit
.vtx_dwords
;
1153 set_edgeflag(chan
, tesla
, &emit
, 0); /* nr will be 1 */
1155 BEGIN_RING_NI(chan
, tesla
, NV50TCL_VERTEX_DATA
, dw
);
1156 for (i
= 0; i
< nr
; ++i
)
1157 emit_vtx_next(chan
, &emit
);
1166 nv50_push_elements_u32(struct nv50_context
*nv50
, uint32_t *map
, unsigned count
)
1168 struct nouveau_channel
*chan
= nv50
->screen
->base
.channel
;
1169 struct nouveau_grobj
*tesla
= nv50
->screen
->tesla
;
1170 struct nv50_vbo_emitctx emit
;
1172 if (emit_prepare(nv50
, &emit
, 0) == FALSE
)
1176 unsigned i
, dw
, nr
= MIN2(count
, emit
.vtx_max
);
1177 dw
= nr
* emit
.vtx_dwords
;
1179 set_edgeflag(chan
, tesla
, &emit
, *map
);
1181 BEGIN_RING_NI(chan
, tesla
, NV50TCL_VERTEX_DATA
, dw
);
1182 for (i
= 0; i
< nr
; ++i
)
1183 emit_vtx(chan
, &emit
, *map
++);
1192 nv50_push_elements_u16(struct nv50_context
*nv50
, uint16_t *map
, unsigned count
)
1194 struct nouveau_channel
*chan
= nv50
->screen
->base
.channel
;
1195 struct nouveau_grobj
*tesla
= nv50
->screen
->tesla
;
1196 struct nv50_vbo_emitctx emit
;
1198 if (emit_prepare(nv50
, &emit
, 0) == FALSE
)
1202 unsigned i
, dw
, nr
= MIN2(count
, emit
.vtx_max
);
1203 dw
= nr
* emit
.vtx_dwords
;
1205 set_edgeflag(chan
, tesla
, &emit
, *map
);
1207 BEGIN_RING_NI(chan
, tesla
, NV50TCL_VERTEX_DATA
, dw
);
1208 for (i
= 0; i
< nr
; ++i
)
1209 emit_vtx(chan
, &emit
, *map
++);
1218 nv50_push_elements_u08(struct nv50_context
*nv50
, uint8_t *map
, unsigned count
)
1220 struct nouveau_channel
*chan
= nv50
->screen
->base
.channel
;
1221 struct nouveau_grobj
*tesla
= nv50
->screen
->tesla
;
1222 struct nv50_vbo_emitctx emit
;
1224 if (emit_prepare(nv50
, &emit
, 0) == FALSE
)
1228 unsigned i
, dw
, nr
= MIN2(count
, emit
.vtx_max
);
1229 dw
= nr
* emit
.vtx_dwords
;
1231 set_edgeflag(chan
, tesla
, &emit
, *map
);
1233 BEGIN_RING_NI(chan
, tesla
, NV50TCL_VERTEX_DATA
, dw
);
1234 for (i
= 0; i
< nr
; ++i
)
1235 emit_vtx(chan
, &emit
, *map
++);