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 "pipe/p_inlines.h"
27 #include "util/u_format.h"
29 #include "nv50_context.h"
32 nv50_push_elements_u08(struct nv50_context
*, uint8_t *, unsigned);
35 nv50_push_elements_u16(struct nv50_context
*, uint16_t *, unsigned);
38 nv50_push_elements_u32(struct nv50_context
*, uint32_t *, unsigned);
41 nv50_push_arrays(struct nv50_context
*, unsigned, unsigned);
43 static INLINE
unsigned
44 nv50_prim(unsigned mode
)
47 case PIPE_PRIM_POINTS
: return NV50TCL_VERTEX_BEGIN_POINTS
;
48 case PIPE_PRIM_LINES
: return NV50TCL_VERTEX_BEGIN_LINES
;
49 case PIPE_PRIM_LINE_LOOP
: return NV50TCL_VERTEX_BEGIN_LINE_LOOP
;
50 case PIPE_PRIM_LINE_STRIP
: return NV50TCL_VERTEX_BEGIN_LINE_STRIP
;
51 case PIPE_PRIM_TRIANGLES
: return NV50TCL_VERTEX_BEGIN_TRIANGLES
;
52 case PIPE_PRIM_TRIANGLE_STRIP
:
53 return NV50TCL_VERTEX_BEGIN_TRIANGLE_STRIP
;
54 case PIPE_PRIM_TRIANGLE_FAN
: return NV50TCL_VERTEX_BEGIN_TRIANGLE_FAN
;
55 case PIPE_PRIM_QUADS
: return NV50TCL_VERTEX_BEGIN_QUADS
;
56 case PIPE_PRIM_QUAD_STRIP
: return NV50TCL_VERTEX_BEGIN_QUAD_STRIP
;
57 case PIPE_PRIM_POLYGON
: return NV50TCL_VERTEX_BEGIN_POLYGON
;
58 case PIPE_PRIM_LINES_ADJACENCY
:
59 return NV50TCL_VERTEX_BEGIN_LINES_ADJACENCY
;
60 case PIPE_PRIM_LINE_STRIP_ADJACENCY
:
61 return NV50TCL_VERTEX_BEGIN_LINE_STRIP_ADJACENCY
;
62 case PIPE_PRIM_TRIANGLES_ADJACENCY
:
63 return NV50TCL_VERTEX_BEGIN_TRIANGLES_ADJACENCY
;
64 case PIPE_PRIM_TRIANGLE_STRIP_ADJACENCY
:
65 return NV50TCL_VERTEX_BEGIN_TRIANGLE_STRIP_ADJACENCY
;
70 NOUVEAU_ERR("invalid primitive type %d\n", mode
);
71 return NV50TCL_VERTEX_BEGIN_POINTS
;
74 static INLINE
uint32_t
75 nv50_vbo_type_to_hw(enum pipe_format format
)
77 const struct util_format_description
*desc
;
79 desc
= util_format_description(format
);
82 switch (desc
->channel
[0].type
) {
83 case UTIL_FORMAT_TYPE_FLOAT
:
84 return NV50TCL_VERTEX_ARRAY_ATTRIB_TYPE_FLOAT
;
85 case UTIL_FORMAT_TYPE_UNSIGNED
:
86 if (desc
->channel
[0].normalized
) {
87 return NV50TCL_VERTEX_ARRAY_ATTRIB_TYPE_UNORM
;
89 return NV50TCL_VERTEX_ARRAY_ATTRIB_TYPE_USCALED
;
90 case UTIL_FORMAT_TYPE_SIGNED
:
91 if (desc
->channel
[0].normalized
) {
92 return NV50TCL_VERTEX_ARRAY_ATTRIB_TYPE_SNORM
;
94 return NV50TCL_VERTEX_ARRAY_ATTRIB_TYPE_SSCALED
;
96 case PIPE_FORMAT_TYPE_UINT:
97 return NV50TCL_VERTEX_ARRAY_ATTRIB_TYPE_UINT;
98 case PIPE_FORMAT_TYPE_SINT:
99 return NV50TCL_VERTEX_ARRAY_ATTRIB_TYPE_SINT; */
105 static INLINE
uint32_t
106 nv50_vbo_size_to_hw(unsigned size
, unsigned nr_c
)
108 static const uint32_t hw_values
[] = {
110 NV50TCL_VERTEX_ARRAY_ATTRIB_FORMAT_8
,
111 NV50TCL_VERTEX_ARRAY_ATTRIB_FORMAT_8_8
,
112 NV50TCL_VERTEX_ARRAY_ATTRIB_FORMAT_8_8_8
,
113 NV50TCL_VERTEX_ARRAY_ATTRIB_FORMAT_8_8_8_8
,
114 NV50TCL_VERTEX_ARRAY_ATTRIB_FORMAT_16
,
115 NV50TCL_VERTEX_ARRAY_ATTRIB_FORMAT_16_16
,
116 NV50TCL_VERTEX_ARRAY_ATTRIB_FORMAT_16_16_16
,
117 NV50TCL_VERTEX_ARRAY_ATTRIB_FORMAT_16_16_16_16
,
119 NV50TCL_VERTEX_ARRAY_ATTRIB_FORMAT_32
,
120 NV50TCL_VERTEX_ARRAY_ATTRIB_FORMAT_32_32
,
121 NV50TCL_VERTEX_ARRAY_ATTRIB_FORMAT_32_32_32
,
122 NV50TCL_VERTEX_ARRAY_ATTRIB_FORMAT_32_32_32_32
};
124 /* we'd also have R11G11B10 and R10G10B10A2 */
126 assert(nr_c
> 0 && nr_c
<= 4);
132 return hw_values
[size
+ (nr_c
- 1)];
135 static INLINE
uint32_t
136 nv50_vbo_vtxelt_to_hw(struct pipe_vertex_element
*ve
)
138 uint32_t hw_type
, hw_size
;
139 enum pipe_format pf
= ve
->src_format
;
140 const struct util_format_description
*desc
;
143 desc
= util_format_description(pf
);
146 size
= util_format_get_component_bits(pf
, UTIL_FORMAT_COLORSPACE_RGB
, 0);
148 hw_type
= nv50_vbo_type_to_hw(pf
);
149 hw_size
= nv50_vbo_size_to_hw(size
, ve
->nr_components
);
151 if (!hw_type
|| !hw_size
) {
152 NOUVEAU_ERR("unsupported vbo format: %s\n", pf_name(pf
));
157 if (desc
->swizzle
[0] == UTIL_FORMAT_SWIZZLE_Z
) /* BGRA */
158 hw_size
|= (1 << 31); /* no real swizzle bits :-( */
160 return (hw_type
| hw_size
);
163 /* For instanced drawing from user buffers, hitting the FIFO repeatedly
164 * with the same vertex data is probably worse than uploading all data.
167 nv50_upload_vtxbuf(struct nv50_context
*nv50
, unsigned i
)
169 struct nv50_screen
*nscreen
= nv50
->screen
;
170 struct pipe_screen
*pscreen
= &nscreen
->base
.base
;
171 struct pipe_buffer
*buf
= nscreen
->strm_vbuf
[i
];
172 struct pipe_vertex_buffer
*vb
= &nv50
->vtxbuf
[i
];
174 unsigned size
= MAX2(vb
->buffer
->size
, 4096);
176 if (buf
&& buf
->size
< size
)
177 pipe_buffer_reference(&nscreen
->strm_vbuf
[i
], NULL
);
179 if (!nscreen
->strm_vbuf
[i
]) {
180 nscreen
->strm_vbuf
[i
] = pipe_buffer_create(
181 pscreen
, 0, PIPE_BUFFER_USAGE_VERTEX
, size
);
182 buf
= nscreen
->strm_vbuf
[i
];
185 src
= pipe_buffer_map(pscreen
, vb
->buffer
, PIPE_BUFFER_USAGE_CPU_READ
);
188 src
+= vb
->buffer_offset
;
190 size
= (vb
->max_index
+ 1) * vb
->stride
+ 16; /* + 16 is for stride 0 */
191 if (vb
->buffer_offset
+ size
> vb
->buffer
->size
)
192 size
= vb
->buffer
->size
- vb
->buffer_offset
;
194 pipe_buffer_write(pscreen
, buf
, vb
->buffer_offset
, size
, src
);
195 pipe_buffer_unmap(pscreen
, vb
->buffer
);
197 vb
->buffer
= buf
; /* don't pipe_reference, this is a private copy */
202 nv50_upload_user_vbufs(struct nv50_context
*nv50
)
207 nv50
->dirty
|= NV50_NEW_ARRAYS
;
208 if (!(nv50
->dirty
& NV50_NEW_ARRAYS
))
211 for (i
= 0; i
< nv50
->vtxbuf_nr
; ++i
) {
212 if (nv50
->vtxbuf
[i
].buffer
->usage
& PIPE_BUFFER_USAGE_VERTEX
)
214 nv50_upload_vtxbuf(nv50
, i
);
219 init_per_instance_arrays(struct nv50_context
*nv50
,
220 unsigned startInstance
,
221 unsigned pos
[16], unsigned step
[16])
223 struct nouveau_grobj
*tesla
= nv50
->screen
->tesla
;
224 struct nouveau_channel
*chan
= tesla
->channel
;
225 struct nouveau_bo
*bo
;
226 struct nouveau_stateobj
*so
;
227 unsigned i
, b
, count
= 0;
228 const uint32_t rl
= NOUVEAU_BO_VRAM
| NOUVEAU_BO_GART
| NOUVEAU_BO_RD
;
230 so
= so_new(nv50
->vtxelt_nr
, nv50
->vtxelt_nr
* 2, nv50
->vtxelt_nr
* 2);
232 for (i
= 0; i
< nv50
->vtxelt_nr
; ++i
) {
233 if (!nv50
->vtxelt
[i
].instance_divisor
)
236 b
= nv50
->vtxelt
[i
].vertex_buffer_index
;
238 pos
[i
] = nv50
->vtxelt
[i
].src_offset
+
239 nv50
->vtxbuf
[b
].buffer_offset
+
240 startInstance
* nv50
->vtxbuf
[b
].stride
;
242 if (!startInstance
) {
246 step
[i
] = startInstance
% nv50
->vtxelt
[i
].instance_divisor
;
248 bo
= nouveau_bo(nv50
->vtxbuf
[b
].buffer
);
250 so_method(so
, tesla
, NV50TCL_VERTEX_ARRAY_START_HIGH(i
), 2);
251 so_reloc (so
, bo
, pos
[i
], rl
| NOUVEAU_BO_LOW
, 0, 0);
252 so_reloc (so
, bo
, pos
[i
], rl
| NOUVEAU_BO_HIGH
, 0, 0);
256 so_ref (so
, &nv50
->state
.instbuf
); /* for flush notify */
257 so_emit(chan
, nv50
->state
.instbuf
);
265 step_per_instance_arrays(struct nv50_context
*nv50
,
266 unsigned pos
[16], unsigned step
[16])
268 struct nouveau_grobj
*tesla
= nv50
->screen
->tesla
;
269 struct nouveau_channel
*chan
= tesla
->channel
;
270 struct nouveau_bo
*bo
;
271 struct nouveau_stateobj
*so
;
273 const uint32_t rl
= NOUVEAU_BO_VRAM
| NOUVEAU_BO_GART
| NOUVEAU_BO_RD
;
275 so
= so_new(nv50
->vtxelt_nr
, nv50
->vtxelt_nr
* 2, nv50
->vtxelt_nr
* 2);
277 for (i
= 0; i
< nv50
->vtxelt_nr
; ++i
) {
278 if (!nv50
->vtxelt
[i
].instance_divisor
)
280 b
= nv50
->vtxelt
[i
].vertex_buffer_index
;
282 if (++step
[i
] == nv50
->vtxelt
[i
].instance_divisor
) {
284 pos
[i
] += nv50
->vtxbuf
[b
].stride
;
287 bo
= nouveau_bo(nv50
->vtxbuf
[b
].buffer
);
289 so_method(so
, tesla
, NV50TCL_VERTEX_ARRAY_START_HIGH(i
), 2);
290 so_reloc (so
, bo
, pos
[i
], rl
| NOUVEAU_BO_LOW
, 0, 0);
291 so_reloc (so
, bo
, pos
[i
], rl
| NOUVEAU_BO_HIGH
, 0, 0);
294 so_ref (so
, &nv50
->state
.instbuf
); /* for flush notify */
297 so_emit(chan
, nv50
->state
.instbuf
);
301 nv50_draw_arrays_instanced(struct pipe_context
*pipe
,
302 unsigned mode
, unsigned start
, unsigned count
,
303 unsigned startInstance
, unsigned instanceCount
)
305 struct nv50_context
*nv50
= nv50_context(pipe
);
306 struct nouveau_channel
*chan
= nv50
->screen
->tesla
->channel
;
307 struct nouveau_grobj
*tesla
= nv50
->screen
->tesla
;
308 unsigned i
, nz_divisors
;
309 unsigned step
[16], pos
[16];
311 nv50_upload_user_vbufs(nv50
);
313 nv50_state_validate(nv50
);
315 nz_divisors
= init_per_instance_arrays(nv50
, startInstance
, pos
, step
);
317 BEGIN_RING(chan
, tesla
, NV50TCL_CB_ADDR
, 2);
318 OUT_RING (chan
, NV50_CB_AUX
| (24 << 8));
319 OUT_RING (chan
, startInstance
);
321 BEGIN_RING(chan
, tesla
, NV50TCL_VERTEX_BEGIN
, 1);
322 OUT_RING (chan
, nv50_prim(mode
));
323 BEGIN_RING(chan
, tesla
, NV50TCL_VERTEX_BUFFER_FIRST
, 2);
324 OUT_RING (chan
, start
);
325 OUT_RING (chan
, count
);
326 BEGIN_RING(chan
, tesla
, NV50TCL_VERTEX_END
, 1);
329 for (i
= 1; i
< instanceCount
; i
++) {
330 if (nz_divisors
) /* any non-zero array divisors ? */
331 step_per_instance_arrays(nv50
, pos
, step
);
333 BEGIN_RING(chan
, tesla
, NV50TCL_VERTEX_BEGIN
, 1);
334 OUT_RING (chan
, nv50_prim(mode
) | (1 << 28));
335 BEGIN_RING(chan
, tesla
, NV50TCL_VERTEX_BUFFER_FIRST
, 2);
336 OUT_RING (chan
, start
);
337 OUT_RING (chan
, count
);
338 BEGIN_RING(chan
, tesla
, NV50TCL_VERTEX_END
, 1);
342 so_ref(NULL
, &nv50
->state
.instbuf
);
346 nv50_draw_arrays(struct pipe_context
*pipe
, unsigned mode
, unsigned start
,
349 struct nv50_context
*nv50
= nv50_context(pipe
);
350 struct nouveau_channel
*chan
= nv50
->screen
->tesla
->channel
;
351 struct nouveau_grobj
*tesla
= nv50
->screen
->tesla
;
354 nv50_state_validate(nv50
);
356 BEGIN_RING(chan
, tesla
, 0x142c, 1);
358 BEGIN_RING(chan
, tesla
, 0x142c, 1);
361 BEGIN_RING(chan
, tesla
, NV50TCL_VERTEX_BEGIN
, 1);
362 OUT_RING (chan
, nv50_prim(mode
));
365 ret
= nv50_push_arrays(nv50
, start
, count
);
367 BEGIN_RING(chan
, tesla
, NV50TCL_VERTEX_BUFFER_FIRST
, 2);
368 OUT_RING (chan
, start
);
369 OUT_RING (chan
, count
);
372 BEGIN_RING(chan
, tesla
, NV50TCL_VERTEX_END
, 1);
375 /* XXX: not sure what to do if ret != TRUE: flush and retry?
380 static INLINE boolean
381 nv50_draw_elements_inline_u08(struct nv50_context
*nv50
, uint8_t *map
,
382 unsigned start
, unsigned count
)
384 struct nouveau_channel
*chan
= nv50
->screen
->tesla
->channel
;
385 struct nouveau_grobj
*tesla
= nv50
->screen
->tesla
;
390 return nv50_push_elements_u08(nv50
, map
, count
);
393 BEGIN_RING(chan
, tesla
, NV50TCL_VB_ELEMENT_U32
, 1);
394 OUT_RING (chan
, map
[0]);
400 unsigned nr
= count
> 2046 ? 2046 : count
;
403 BEGIN_RING(chan
, tesla
, NV50TCL_VB_ELEMENT_U16
| 0x40000000, nr
>> 1);
404 for (i
= 0; i
< nr
; i
+= 2)
405 OUT_RING (chan
, (map
[i
+ 1] << 16) | map
[i
]);
413 static INLINE boolean
414 nv50_draw_elements_inline_u16(struct nv50_context
*nv50
, uint16_t *map
,
415 unsigned start
, unsigned count
)
417 struct nouveau_channel
*chan
= nv50
->screen
->tesla
->channel
;
418 struct nouveau_grobj
*tesla
= nv50
->screen
->tesla
;
423 return nv50_push_elements_u16(nv50
, map
, count
);
426 BEGIN_RING(chan
, tesla
, NV50TCL_VB_ELEMENT_U32
, 1);
427 OUT_RING (chan
, map
[0]);
433 unsigned nr
= count
> 2046 ? 2046 : count
;
436 BEGIN_RING(chan
, tesla
, NV50TCL_VB_ELEMENT_U16
| 0x40000000, nr
>> 1);
437 for (i
= 0; i
< nr
; i
+= 2)
438 OUT_RING (chan
, (map
[i
+ 1] << 16) | map
[i
]);
446 static INLINE boolean
447 nv50_draw_elements_inline_u32(struct nv50_context
*nv50
, uint32_t *map
,
448 unsigned start
, unsigned count
)
450 struct nouveau_channel
*chan
= nv50
->screen
->tesla
->channel
;
451 struct nouveau_grobj
*tesla
= nv50
->screen
->tesla
;
456 return nv50_push_elements_u32(nv50
, map
, count
);
459 unsigned nr
= count
> 2047 ? 2047 : count
;
461 BEGIN_RING(chan
, tesla
, NV50TCL_VB_ELEMENT_U32
| 0x40000000, nr
);
462 OUT_RINGp (chan
, map
, nr
);
471 nv50_draw_elements_inline(struct nv50_context
*nv50
,
472 void *map
, unsigned indexSize
,
473 unsigned start
, unsigned count
)
477 nv50_draw_elements_inline_u08(nv50
, map
, start
, count
);
480 nv50_draw_elements_inline_u16(nv50
, map
, start
, count
);
483 nv50_draw_elements_inline_u32(nv50
, map
, start
, count
);
489 nv50_draw_elements_instanced(struct pipe_context
*pipe
,
490 struct pipe_buffer
*indexBuffer
,
492 unsigned mode
, unsigned start
, unsigned count
,
493 unsigned startInstance
, unsigned instanceCount
)
495 struct nv50_context
*nv50
= nv50_context(pipe
);
496 struct nouveau_grobj
*tesla
= nv50
->screen
->tesla
;
497 struct nouveau_channel
*chan
= tesla
->channel
;
498 struct pipe_screen
*pscreen
= pipe
->screen
;
500 unsigned i
, nz_divisors
;
501 unsigned step
[16], pos
[16];
503 map
= pipe_buffer_map(pscreen
, indexBuffer
, PIPE_BUFFER_USAGE_CPU_READ
);
505 nv50_upload_user_vbufs(nv50
);
507 nv50_state_validate(nv50
);
509 nz_divisors
= init_per_instance_arrays(nv50
, startInstance
, pos
, step
);
511 BEGIN_RING(chan
, tesla
, NV50TCL_CB_ADDR
, 2);
512 OUT_RING (chan
, NV50_CB_AUX
| (24 << 8));
513 OUT_RING (chan
, startInstance
);
515 BEGIN_RING(chan
, tesla
, NV50TCL_VERTEX_BEGIN
, 1);
516 OUT_RING (chan
, nv50_prim(mode
) | (1 << 28));
518 nv50_draw_elements_inline(nv50
, map
, indexSize
, start
, count
);
520 BEGIN_RING(chan
, tesla
, NV50TCL_VERTEX_END
, 1);
523 for (i
= 1; i
< instanceCount
; ++i
) {
524 if (nz_divisors
) /* any non-zero array divisors ? */
525 step_per_instance_arrays(nv50
, pos
, step
);
527 BEGIN_RING(chan
, tesla
, NV50TCL_VERTEX_BEGIN
, 1);
528 OUT_RING (chan
, nv50_prim(mode
) | (1 << 28));
530 nv50_draw_elements_inline(nv50
, map
, indexSize
, start
, count
);
532 BEGIN_RING(chan
, tesla
, NV50TCL_VERTEX_END
, 1);
536 so_ref(NULL
, &nv50
->state
.instbuf
);
540 nv50_draw_elements(struct pipe_context
*pipe
,
541 struct pipe_buffer
*indexBuffer
, unsigned indexSize
,
542 unsigned mode
, unsigned start
, unsigned count
)
544 struct nv50_context
*nv50
= nv50_context(pipe
);
545 struct nouveau_channel
*chan
= nv50
->screen
->tesla
->channel
;
546 struct nouveau_grobj
*tesla
= nv50
->screen
->tesla
;
547 struct pipe_screen
*pscreen
= pipe
->screen
;
550 map
= pipe_buffer_map(pscreen
, indexBuffer
, PIPE_BUFFER_USAGE_CPU_READ
);
552 nv50_state_validate(nv50
);
554 BEGIN_RING(chan
, tesla
, 0x142c, 1);
556 BEGIN_RING(chan
, tesla
, 0x142c, 1);
559 BEGIN_RING(chan
, tesla
, NV50TCL_VERTEX_BEGIN
, 1);
560 OUT_RING (chan
, nv50_prim(mode
));
562 nv50_draw_elements_inline(nv50
, map
, indexSize
, start
, count
);
564 BEGIN_RING(chan
, tesla
, NV50TCL_VERTEX_END
, 1);
567 pipe_buffer_unmap(pscreen
, indexBuffer
);
570 static INLINE boolean
571 nv50_vbo_static_attrib(struct nv50_context
*nv50
, unsigned attrib
,
572 struct nouveau_stateobj
**pso
,
573 struct pipe_vertex_element
*ve
,
574 struct pipe_vertex_buffer
*vb
)
577 struct nouveau_stateobj
*so
;
578 struct nouveau_grobj
*tesla
= nv50
->screen
->tesla
;
579 struct nouveau_bo
*bo
= nouveau_bo(vb
->buffer
);
583 ret
= nouveau_bo_map(bo
, NOUVEAU_BO_RD
);
587 util_format_read_4f(ve
->src_format
, v
, 0, (uint8_t *)bo
->map
+
588 (vb
->buffer_offset
+ ve
->src_offset
), 0,
592 *pso
= so
= so_new(nv50
->vtxelt_nr
, nv50
->vtxelt_nr
* 4, 0);
594 switch (ve
->nr_components
) {
596 so_method(so
, tesla
, NV50TCL_VTX_ATTR_4F_X(attrib
), 4);
597 so_data (so
, fui(v
[0]));
598 so_data (so
, fui(v
[1]));
599 so_data (so
, fui(v
[2]));
600 so_data (so
, fui(v
[3]));
603 so_method(so
, tesla
, NV50TCL_VTX_ATTR_3F_X(attrib
), 3);
604 so_data (so
, fui(v
[0]));
605 so_data (so
, fui(v
[1]));
606 so_data (so
, fui(v
[2]));
609 so_method(so
, tesla
, NV50TCL_VTX_ATTR_2F_X(attrib
), 2);
610 so_data (so
, fui(v
[0]));
611 so_data (so
, fui(v
[1]));
614 if (attrib
== nv50
->vertprog
->cfg
.edgeflag_in
) {
615 so_method(so
, tesla
, NV50TCL_EDGEFLAG_ENABLE
, 1);
616 so_data (so
, v
[0] ? 1 : 0);
618 so_method(so
, tesla
, NV50TCL_VTX_ATTR_1F(attrib
), 1);
619 so_data (so
, fui(v
[0]));
622 nouveau_bo_unmap(bo
);
626 nouveau_bo_unmap(bo
);
631 nv50_vbo_validate(struct nv50_context
*nv50
)
633 struct nouveau_grobj
*tesla
= nv50
->screen
->tesla
;
634 struct nouveau_stateobj
*vtxbuf
, *vtxfmt
, *vtxattr
;
637 /* don't validate if Gallium took away our buffers */
638 if (nv50
->vtxbuf_nr
== 0)
642 for (i
= 0; i
< nv50
->vtxbuf_nr
; ++i
)
643 if (nv50
->vtxbuf
[i
].stride
&&
644 !(nv50
->vtxbuf
[i
].buffer
->usage
& PIPE_BUFFER_USAGE_VERTEX
))
645 nv50
->vbo_fifo
= 0xffff;
647 if (nv50
->vertprog
->cfg
.edgeflag_in
< 16)
648 nv50
->vbo_fifo
= 0xffff; /* vertprog can't set edgeflag */
650 n_ve
= MAX2(nv50
->vtxelt_nr
, nv50
->state
.vtxelt_nr
);
653 vtxbuf
= so_new(n_ve
* 2, n_ve
* 5, nv50
->vtxelt_nr
* 4);
654 vtxfmt
= so_new(1, n_ve
, 0);
655 so_method(vtxfmt
, tesla
, NV50TCL_VERTEX_ARRAY_ATTRIB(0), n_ve
);
657 for (i
= 0; i
< nv50
->vtxelt_nr
; i
++) {
658 struct pipe_vertex_element
*ve
= &nv50
->vtxelt
[i
];
659 struct pipe_vertex_buffer
*vb
=
660 &nv50
->vtxbuf
[ve
->vertex_buffer_index
];
661 struct nouveau_bo
*bo
= nouveau_bo(vb
->buffer
);
662 uint32_t hw
= nv50_vbo_vtxelt_to_hw(ve
);
665 nv50_vbo_static_attrib(nv50
, i
, &vtxattr
, ve
, vb
)) {
666 so_data(vtxfmt
, hw
| (1 << 4));
668 so_method(vtxbuf
, tesla
,
669 NV50TCL_VERTEX_ARRAY_FORMAT(i
), 1);
672 nv50
->vbo_fifo
&= ~(1 << i
);
675 so_data(vtxfmt
, hw
| i
);
677 if (nv50
->vbo_fifo
) {
678 so_method(vtxbuf
, tesla
,
679 NV50TCL_VERTEX_ARRAY_FORMAT(i
), 1);
684 so_method(vtxbuf
, tesla
, NV50TCL_VERTEX_ARRAY_FORMAT(i
), 3);
685 so_data (vtxbuf
, 0x20000000 |
686 (ve
->instance_divisor
? 0 : vb
->stride
));
687 so_reloc (vtxbuf
, bo
, vb
->buffer_offset
+
688 ve
->src_offset
, NOUVEAU_BO_VRAM
| NOUVEAU_BO_GART
|
689 NOUVEAU_BO_RD
| NOUVEAU_BO_HIGH
, 0, 0);
690 so_reloc (vtxbuf
, bo
, vb
->buffer_offset
+
691 ve
->src_offset
, NOUVEAU_BO_VRAM
| NOUVEAU_BO_GART
|
692 NOUVEAU_BO_RD
| NOUVEAU_BO_LOW
, 0, 0);
694 /* vertex array limits */
695 so_method(vtxbuf
, tesla
, NV50TCL_VERTEX_ARRAY_LIMIT_HIGH(i
), 2);
696 so_reloc (vtxbuf
, bo
, vb
->buffer
->size
- 1,
697 NOUVEAU_BO_VRAM
| NOUVEAU_BO_GART
| NOUVEAU_BO_RD
|
698 NOUVEAU_BO_HIGH
, 0, 0);
699 so_reloc (vtxbuf
, bo
, vb
->buffer
->size
- 1,
700 NOUVEAU_BO_VRAM
| NOUVEAU_BO_GART
| NOUVEAU_BO_RD
|
701 NOUVEAU_BO_LOW
, 0, 0);
703 for (; i
< n_ve
; ++i
) {
704 so_data (vtxfmt
, 0x7e080010);
706 so_method(vtxbuf
, tesla
, NV50TCL_VERTEX_ARRAY_FORMAT(i
), 1);
709 nv50
->state
.vtxelt_nr
= nv50
->vtxelt_nr
;
711 so_ref (vtxfmt
, &nv50
->state
.vtxfmt
);
712 so_ref (vtxbuf
, &nv50
->state
.vtxbuf
);
713 so_ref (vtxattr
, &nv50
->state
.vtxattr
);
714 so_ref (NULL
, &vtxbuf
);
715 so_ref (NULL
, &vtxfmt
);
716 so_ref (NULL
, &vtxattr
);
719 typedef void (*pfn_push
)(struct nouveau_channel
*, void *);
721 struct nv50_vbo_emitctx
731 unsigned ve_edgeflag
;
735 emit_vtx_next(struct nouveau_channel
*chan
, struct nv50_vbo_emitctx
*emit
)
739 for (i
= 0; i
< emit
->nr_ve
; ++i
) {
740 emit
->push
[i
](chan
, emit
->map
[i
]);
741 emit
->map
[i
] += emit
->stride
[i
];
746 emit_vtx(struct nouveau_channel
*chan
, struct nv50_vbo_emitctx
*emit
,
751 for (i
= 0; i
< emit
->nr_ve
; ++i
)
752 emit
->push
[i
](chan
, emit
->map
[i
] + emit
->stride
[i
] * vi
);
755 static INLINE boolean
756 nv50_map_vbufs(struct nv50_context
*nv50
)
760 for (i
= 0; i
< nv50
->vtxbuf_nr
; ++i
) {
761 struct pipe_vertex_buffer
*vb
= &nv50
->vtxbuf
[i
];
762 unsigned size
, delta
;
764 if (nouveau_bo(vb
->buffer
)->map
)
767 size
= vb
->stride
* (vb
->max_index
+ 1);
768 delta
= vb
->buffer_offset
;
771 size
= vb
->buffer
->size
- vb
->buffer_offset
;
773 if (nouveau_bo_map_range(nouveau_bo(vb
->buffer
),
774 delta
, size
, NOUVEAU_BO_RD
))
778 if (i
== nv50
->vtxbuf_nr
)
781 nouveau_bo_unmap(nouveau_bo(nv50
->vtxbuf
[i
].buffer
));
786 nv50_unmap_vbufs(struct nv50_context
*nv50
)
790 for (i
= 0; i
< nv50
->vtxbuf_nr
; ++i
)
791 if (nouveau_bo(nv50
->vtxbuf
[i
].buffer
)->map
)
792 nouveau_bo_unmap(nouveau_bo(nv50
->vtxbuf
[i
].buffer
));
796 emit_b32_1(struct nouveau_channel
*chan
, void *data
)
800 OUT_RING(chan
, v
[0]);
804 emit_b32_2(struct nouveau_channel
*chan
, void *data
)
808 OUT_RING(chan
, v
[0]);
809 OUT_RING(chan
, v
[1]);
813 emit_b32_3(struct nouveau_channel
*chan
, void *data
)
817 OUT_RING(chan
, v
[0]);
818 OUT_RING(chan
, v
[1]);
819 OUT_RING(chan
, v
[2]);
823 emit_b32_4(struct nouveau_channel
*chan
, void *data
)
827 OUT_RING(chan
, v
[0]);
828 OUT_RING(chan
, v
[1]);
829 OUT_RING(chan
, v
[2]);
830 OUT_RING(chan
, v
[3]);
834 emit_b16_1(struct nouveau_channel
*chan
, void *data
)
838 OUT_RING(chan
, v
[0]);
842 emit_b16_3(struct nouveau_channel
*chan
, void *data
)
846 OUT_RING(chan
, (v
[1] << 16) | v
[0]);
847 OUT_RING(chan
, v
[2]);
851 emit_b08_1(struct nouveau_channel
*chan
, void *data
)
855 OUT_RING(chan
, v
[0]);
859 emit_b08_3(struct nouveau_channel
*chan
, void *data
)
863 OUT_RING(chan
, (v
[2] << 16) | (v
[1] << 8) | v
[0]);
867 emit_prepare(struct nv50_context
*nv50
, struct nv50_vbo_emitctx
*emit
,
872 if (nv50_map_vbufs(nv50
) == FALSE
)
875 emit
->ve_edgeflag
= nv50
->vertprog
->cfg
.edgeflag_in
;
877 emit
->edgeflag
= 0.5f
;
879 emit
->vtx_dwords
= 0;
881 for (i
= 0; i
< nv50
->vtxelt_nr
; ++i
) {
882 struct pipe_vertex_element
*ve
;
883 struct pipe_vertex_buffer
*vb
;
885 const struct util_format_description
*desc
;
887 ve
= &nv50
->vtxelt
[i
];
888 vb
= &nv50
->vtxbuf
[ve
->vertex_buffer_index
];
889 if (!(nv50
->vbo_fifo
& (1 << i
)))
893 emit
->stride
[n
] = vb
->stride
;
894 emit
->map
[n
] = nouveau_bo(vb
->buffer
)->map
+
895 (start
* vb
->stride
+ ve
->src_offset
);
897 desc
= util_format_description(ve
->src_format
);
900 size
= util_format_get_component_bits(
901 ve
->src_format
, UTIL_FORMAT_COLORSPACE_RGB
, 0);
903 assert(ve
->nr_components
> 0 && ve
->nr_components
<= 4);
905 /* It shouldn't be necessary to push the implicit 1s
906 * for case 3 and size 8 cases 1, 2, 3.
910 NOUVEAU_ERR("unsupported vtxelt size: %u\n", size
);
913 switch (ve
->nr_components
) {
914 case 1: emit
->push
[n
] = emit_b32_1
; break;
915 case 2: emit
->push
[n
] = emit_b32_2
; break;
916 case 3: emit
->push
[n
] = emit_b32_3
; break;
917 case 4: emit
->push
[n
] = emit_b32_4
; break;
919 emit
->vtx_dwords
+= ve
->nr_components
;
922 switch (ve
->nr_components
) {
923 case 1: emit
->push
[n
] = emit_b16_1
; break;
924 case 2: emit
->push
[n
] = emit_b32_1
; break;
925 case 3: emit
->push
[n
] = emit_b16_3
; break;
926 case 4: emit
->push
[n
] = emit_b32_2
; break;
928 emit
->vtx_dwords
+= (ve
->nr_components
+ 1) >> 1;
931 switch (ve
->nr_components
) {
932 case 1: emit
->push
[n
] = emit_b08_1
; break;
933 case 2: emit
->push
[n
] = emit_b16_1
; break;
934 case 3: emit
->push
[n
] = emit_b08_3
; break;
935 case 4: emit
->push
[n
] = emit_b32_1
; break;
937 emit
->vtx_dwords
+= 1;
942 emit
->vtx_max
= 512 / emit
->vtx_dwords
;
943 if (emit
->ve_edgeflag
< 16)
950 set_edgeflag(struct nouveau_channel
*chan
,
951 struct nouveau_grobj
*tesla
,
952 struct nv50_vbo_emitctx
*emit
, uint32_t index
)
954 unsigned i
= emit
->ve_edgeflag
;
957 float f
= *((float *)(emit
->map
[i
] + index
* emit
->stride
[i
]));
959 if (emit
->edgeflag
!= f
) {
962 BEGIN_RING(chan
, tesla
, 0x15e4, 1);
963 OUT_RING (chan
, f
? 1 : 0);
969 nv50_push_arrays(struct nv50_context
*nv50
, unsigned start
, unsigned count
)
971 struct nouveau_channel
*chan
= nv50
->screen
->base
.channel
;
972 struct nouveau_grobj
*tesla
= nv50
->screen
->tesla
;
973 struct nv50_vbo_emitctx emit
;
975 if (emit_prepare(nv50
, &emit
, start
) == FALSE
)
979 unsigned i
, dw
, nr
= MIN2(count
, emit
.vtx_max
);
980 dw
= nr
* emit
.vtx_dwords
;
982 set_edgeflag(chan
, tesla
, &emit
, 0); /* nr will be 1 */
984 BEGIN_RING(chan
, tesla
, NV50TCL_VERTEX_DATA
| 0x40000000, dw
);
985 for (i
= 0; i
< nr
; ++i
)
986 emit_vtx_next(chan
, &emit
);
990 nv50_unmap_vbufs(nv50
);
996 nv50_push_elements_u32(struct nv50_context
*nv50
, uint32_t *map
, unsigned count
)
998 struct nouveau_channel
*chan
= nv50
->screen
->base
.channel
;
999 struct nouveau_grobj
*tesla
= nv50
->screen
->tesla
;
1000 struct nv50_vbo_emitctx emit
;
1002 if (emit_prepare(nv50
, &emit
, 0) == FALSE
)
1006 unsigned i
, dw
, nr
= MIN2(count
, emit
.vtx_max
);
1007 dw
= nr
* emit
.vtx_dwords
;
1009 set_edgeflag(chan
, tesla
, &emit
, *map
);
1011 BEGIN_RING(chan
, tesla
, NV50TCL_VERTEX_DATA
| 0x40000000, dw
);
1012 for (i
= 0; i
< nr
; ++i
)
1013 emit_vtx(chan
, &emit
, *map
++);
1017 nv50_unmap_vbufs(nv50
);
1023 nv50_push_elements_u16(struct nv50_context
*nv50
, uint16_t *map
, unsigned count
)
1025 struct nouveau_channel
*chan
= nv50
->screen
->base
.channel
;
1026 struct nouveau_grobj
*tesla
= nv50
->screen
->tesla
;
1027 struct nv50_vbo_emitctx emit
;
1029 if (emit_prepare(nv50
, &emit
, 0) == FALSE
)
1033 unsigned i
, dw
, nr
= MIN2(count
, emit
.vtx_max
);
1034 dw
= nr
* emit
.vtx_dwords
;
1036 set_edgeflag(chan
, tesla
, &emit
, *map
);
1038 BEGIN_RING(chan
, tesla
, NV50TCL_VERTEX_DATA
| 0x40000000, dw
);
1039 for (i
= 0; i
< nr
; ++i
)
1040 emit_vtx(chan
, &emit
, *map
++);
1044 nv50_unmap_vbufs(nv50
);
1050 nv50_push_elements_u08(struct nv50_context
*nv50
, uint8_t *map
, unsigned count
)
1052 struct nouveau_channel
*chan
= nv50
->screen
->base
.channel
;
1053 struct nouveau_grobj
*tesla
= nv50
->screen
->tesla
;
1054 struct nv50_vbo_emitctx emit
;
1056 if (emit_prepare(nv50
, &emit
, 0) == FALSE
)
1060 unsigned i
, dw
, nr
= MIN2(count
, emit
.vtx_max
);
1061 dw
= nr
* emit
.vtx_dwords
;
1063 set_edgeflag(chan
, tesla
, &emit
, *map
);
1065 BEGIN_RING(chan
, tesla
, NV50TCL_VERTEX_DATA
| 0x40000000, dw
);
1066 for (i
= 0; i
< nr
; ++i
)
1067 emit_vtx(chan
, &emit
, *map
++);
1071 nv50_unmap_vbufs(nv50
);