1 /**************************************************************************
3 * Copyright 2011 Marek Olšák <maraeo@gmail.com>
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21 * IN NO EVENT SHALL AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR
22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26 **************************************************************************/
28 #include "util/u_vbuf.h"
30 #include "util/u_dump.h"
31 #include "util/u_format.h"
32 #include "util/u_inlines.h"
33 #include "util/u_memory.h"
34 #include "util/u_upload_mgr.h"
35 #include "translate/translate.h"
36 #include "translate/translate_cache.h"
37 #include "cso_cache/cso_cache.h"
38 #include "cso_cache/cso_hash.h"
40 struct u_vbuf_elements
{
42 struct pipe_vertex_element ve
[PIPE_MAX_ATTRIBS
];
44 unsigned src_format_size
[PIPE_MAX_ATTRIBS
];
46 /* If (velem[i].src_format != native_format[i]), the vertex buffer
47 * referenced by the vertex element cannot be used for rendering and
48 * its vertex data must be translated to native_format[i]. */
49 enum pipe_format native_format
[PIPE_MAX_ATTRIBS
];
50 unsigned native_format_size
[PIPE_MAX_ATTRIBS
];
52 /* This might mean two things:
53 * - src_format != native_format, as discussed above.
54 * - src_offset % 4 != 0 (if the caps don't allow such an offset). */
55 boolean incompatible_layout
;
56 /* Per-element flags. */
57 boolean incompatible_layout_elem
[PIPE_MAX_ATTRIBS
];
71 struct pipe_context
*pipe
;
72 struct translate_cache
*translate_cache
;
73 struct cso_cache
*cso_cache
;
75 /* Vertex buffers for the driver.
76 * There are no user buffers. */
77 struct pipe_vertex_buffer real_vertex_buffer
[PIPE_MAX_ATTRIBS
];
78 int nr_real_vertex_buffers
;
79 boolean vertex_buffers_dirty
;
81 /* The index buffer. */
82 struct pipe_index_buffer index_buffer
;
84 /* and its associated helper structure for this module. */
85 struct u_vbuf_elements
*ve
;
87 /* Vertex elements used for the translate fallback. */
88 struct pipe_vertex_element fallback_velems
[PIPE_MAX_ATTRIBS
];
89 /* If non-NULL, this is a vertex element state used for the translate
90 * fallback and therefore used for rendering too. */
92 /* The vertex buffer slot index where translated vertices have been
94 unsigned fallback_vbs
[VB_NUM
];
96 /* Whether there is any user buffer. */
98 /* Whether there is a buffer with a non-native layout. */
99 boolean incompatible_vb_layout
;
100 /* Per-buffer flags. */
101 boolean incompatible_vb
[PIPE_MAX_ATTRIBS
];
103 void (*driver_set_index_buffer
)(struct pipe_context
*pipe
,
104 const struct pipe_index_buffer
*);
105 void (*driver_set_vertex_buffers
)(struct pipe_context
*,
106 unsigned num_buffers
,
107 const struct pipe_vertex_buffer
*);
108 void *(*driver_create_vertex_elements_state
)(struct pipe_context
*,
109 unsigned num_elements
,
110 const struct pipe_vertex_element
*);
111 void (*driver_bind_vertex_elements_state
)(struct pipe_context
*, void *);
112 void (*driver_delete_vertex_elements_state
)(struct pipe_context
*, void *);
113 void (*driver_draw_vbo
)(struct pipe_context
*pipe
,
114 const struct pipe_draw_info
*info
);
117 static void u_vbuf_init_format_caps(struct u_vbuf_priv
*mgr
)
119 struct pipe_screen
*screen
= mgr
->pipe
->screen
;
121 mgr
->b
.caps
.format_fixed32
=
122 screen
->is_format_supported(screen
, PIPE_FORMAT_R32_FIXED
, PIPE_BUFFER
,
123 0, PIPE_BIND_VERTEX_BUFFER
);
125 mgr
->b
.caps
.format_float16
=
126 screen
->is_format_supported(screen
, PIPE_FORMAT_R16_FLOAT
, PIPE_BUFFER
,
127 0, PIPE_BIND_VERTEX_BUFFER
);
129 mgr
->b
.caps
.format_float64
=
130 screen
->is_format_supported(screen
, PIPE_FORMAT_R64_FLOAT
, PIPE_BUFFER
,
131 0, PIPE_BIND_VERTEX_BUFFER
);
133 mgr
->b
.caps
.format_norm32
=
134 screen
->is_format_supported(screen
, PIPE_FORMAT_R32_UNORM
, PIPE_BUFFER
,
135 0, PIPE_BIND_VERTEX_BUFFER
) &&
136 screen
->is_format_supported(screen
, PIPE_FORMAT_R32_SNORM
, PIPE_BUFFER
,
137 0, PIPE_BIND_VERTEX_BUFFER
);
139 mgr
->b
.caps
.format_scaled32
=
140 screen
->is_format_supported(screen
, PIPE_FORMAT_R32_USCALED
, PIPE_BUFFER
,
141 0, PIPE_BIND_VERTEX_BUFFER
) &&
142 screen
->is_format_supported(screen
, PIPE_FORMAT_R32_SSCALED
, PIPE_BUFFER
,
143 0, PIPE_BIND_VERTEX_BUFFER
);
146 static void u_vbuf_install(struct u_vbuf_priv
*mgr
);
149 u_vbuf_create(struct pipe_context
*pipe
,
150 unsigned upload_buffer_size
,
151 unsigned upload_buffer_alignment
,
152 unsigned upload_buffer_bind
,
153 enum u_fetch_alignment fetch_alignment
)
155 struct u_vbuf_priv
*mgr
= CALLOC_STRUCT(u_vbuf_priv
);
158 mgr
->cso_cache
= cso_cache_create();
159 mgr
->translate_cache
= translate_cache_create();
160 memset(mgr
->fallback_vbs
, ~0, sizeof(mgr
->fallback_vbs
));
162 mgr
->b
.uploader
= u_upload_create(pipe
, upload_buffer_size
,
163 upload_buffer_alignment
,
166 mgr
->b
.caps
.fetch_dword_unaligned
=
167 fetch_alignment
== U_VERTEX_FETCH_BYTE_ALIGNED
;
169 u_vbuf_init_format_caps(mgr
);
174 /* XXX I had to fork this off of cso_context. */
176 u_vbuf_cache_set_vertex_elements(struct u_vbuf_priv
*mgr
,
178 const struct pipe_vertex_element
*states
)
180 unsigned key_size
, hash_key
;
181 struct cso_hash_iter iter
;
183 struct cso_velems_state velems_state
;
185 /* need to include the count into the stored state data too. */
186 key_size
= sizeof(struct pipe_vertex_element
) * count
+ sizeof(unsigned);
187 velems_state
.count
= count
;
188 memcpy(velems_state
.velems
, states
,
189 sizeof(struct pipe_vertex_element
) * count
);
190 hash_key
= cso_construct_key((void*)&velems_state
, key_size
);
191 iter
= cso_find_state_template(mgr
->cso_cache
, hash_key
, CSO_VELEMENTS
,
192 (void*)&velems_state
, key_size
);
194 if (cso_hash_iter_is_null(iter
)) {
195 struct cso_velements
*cso
= MALLOC_STRUCT(cso_velements
);
196 memcpy(&cso
->state
, &velems_state
, key_size
);
198 mgr
->driver_create_vertex_elements_state(mgr
->pipe
, count
,
199 &cso
->state
.velems
[0]);
201 (cso_state_callback
)mgr
->driver_delete_vertex_elements_state
;
202 cso
->context
= mgr
->pipe
;
204 iter
= cso_insert_state(mgr
->cso_cache
, hash_key
, CSO_VELEMENTS
, cso
);
207 handle
= ((struct cso_velements
*)cso_hash_iter_data(iter
))->data
;
210 mgr
->driver_bind_vertex_elements_state(mgr
->pipe
, handle
);
214 void u_vbuf_destroy(struct u_vbuf
*mgrb
)
216 struct u_vbuf_priv
*mgr
= (struct u_vbuf_priv
*)mgrb
;
219 assert(mgr
->pipe
->draw
);
220 mgr
->pipe
->draw
= NULL
;
222 for (i
= 0; i
< mgr
->b
.nr_vertex_buffers
; i
++) {
223 pipe_resource_reference(&mgr
->b
.vertex_buffer
[i
].buffer
, NULL
);
225 for (i
= 0; i
< mgr
->nr_real_vertex_buffers
; i
++) {
226 pipe_resource_reference(&mgr
->real_vertex_buffer
[i
].buffer
, NULL
);
229 translate_cache_destroy(mgr
->translate_cache
);
230 u_upload_destroy(mgr
->b
.uploader
);
231 cso_cache_delete(mgr
->cso_cache
);
236 u_vbuf_translate_buffers(struct u_vbuf_priv
*mgr
, struct translate_key
*key
,
237 unsigned vb_mask
, unsigned out_vb
,
238 int start_vertex
, unsigned num_vertices
,
239 int start_index
, unsigned num_indices
, int min_index
,
242 struct translate
*tr
;
243 struct pipe_transfer
*vb_transfer
[PIPE_MAX_ATTRIBS
] = {0};
244 struct pipe_resource
*out_buffer
= NULL
;
246 unsigned i
, out_offset
;
248 /* Get a translate object. */
249 tr
= translate_cache_find(mgr
->translate_cache
, key
);
251 /* Map buffers we want to translate. */
252 for (i
= 0; i
< mgr
->b
.nr_vertex_buffers
; i
++) {
253 if (vb_mask
& (1 << i
)) {
254 struct pipe_vertex_buffer
*vb
= &mgr
->b
.vertex_buffer
[i
];
255 unsigned offset
= vb
->buffer_offset
+ vb
->stride
* start_vertex
;
258 if (vb
->buffer
->user_ptr
) {
259 map
= vb
->buffer
->user_ptr
+ offset
;
261 unsigned size
= vb
->stride
? num_vertices
* vb
->stride
264 if (offset
+size
> vb
->buffer
->width0
) {
265 size
= vb
->buffer
->width0
- offset
;
268 map
= pipe_buffer_map_range(mgr
->pipe
, vb
->buffer
, offset
, size
,
269 PIPE_TRANSFER_READ
, &vb_transfer
[i
]);
272 /* Subtract min_index so that indexing with the index buffer works. */
273 if (unroll_indices
) {
274 map
-= vb
->stride
* min_index
;
277 tr
->set_buffer(tr
, i
, map
, vb
->stride
, ~0);
282 if (unroll_indices
) {
283 struct pipe_index_buffer
*ib
= &mgr
->index_buffer
;
284 struct pipe_transfer
*transfer
= NULL
;
285 unsigned offset
= ib
->offset
+ start_index
* ib
->index_size
;
288 assert(ib
->buffer
&& ib
->index_size
);
290 if (ib
->buffer
->user_ptr
) {
291 map
= ib
->buffer
->user_ptr
+ offset
;
293 map
= pipe_buffer_map_range(mgr
->pipe
, ib
->buffer
, offset
,
294 num_indices
* ib
->index_size
,
295 PIPE_TRANSFER_READ
, &transfer
);
298 /* Create and map the output buffer. */
299 u_upload_alloc(mgr
->b
.uploader
, 0,
300 key
->output_stride
* num_indices
,
301 &out_offset
, &out_buffer
,
304 switch (ib
->index_size
) {
306 tr
->run_elts(tr
, (unsigned*)map
, num_indices
, 0, out_map
);
309 tr
->run_elts16(tr
, (uint16_t*)map
, num_indices
, 0, out_map
);
312 tr
->run_elts8(tr
, map
, num_indices
, 0, out_map
);
317 pipe_buffer_unmap(mgr
->pipe
, transfer
);
320 /* Create and map the output buffer. */
321 u_upload_alloc(mgr
->b
.uploader
,
322 key
->output_stride
* start_vertex
,
323 key
->output_stride
* num_vertices
,
324 &out_offset
, &out_buffer
,
327 out_offset
-= key
->output_stride
* start_vertex
;
329 tr
->run(tr
, 0, num_vertices
, 0, out_map
);
332 /* Unmap all buffers. */
333 for (i
= 0; i
< mgr
->b
.nr_vertex_buffers
; i
++) {
334 if (vb_transfer
[i
]) {
335 pipe_buffer_unmap(mgr
->pipe
, vb_transfer
[i
]);
339 /* Setup the new vertex buffer. */
340 mgr
->real_vertex_buffer
[out_vb
].buffer_offset
= out_offset
;
341 mgr
->real_vertex_buffer
[out_vb
].stride
= key
->output_stride
;
343 /* Move the buffer reference. */
344 pipe_resource_reference(
345 &mgr
->real_vertex_buffer
[out_vb
].buffer
, NULL
);
346 mgr
->real_vertex_buffer
[out_vb
].buffer
= out_buffer
;
350 u_vbuf_translate_find_free_vb_slots(struct u_vbuf_priv
*mgr
,
351 unsigned mask
[VB_NUM
])
354 unsigned nr
= mgr
->ve
->count
;
355 boolean used_vb
[PIPE_MAX_ATTRIBS
] = {0};
356 unsigned fallback_vbs
[VB_NUM
];
358 memset(fallback_vbs
, ~0, sizeof(fallback_vbs
));
360 /* Mark used vertex buffers as... used. */
361 for (i
= 0; i
< nr
; i
++) {
362 if (!mgr
->ve
->incompatible_layout_elem
[i
]) {
363 unsigned index
= mgr
->ve
->ve
[i
].vertex_buffer_index
;
365 if (!mgr
->incompatible_vb
[index
]) {
366 used_vb
[index
] = TRUE
;
371 /* Find free slots for each type if needed. */
373 for (type
= 0; type
< VB_NUM
; type
++) {
375 for (; i
< PIPE_MAX_ATTRIBS
; i
++) {
377 /*printf("found slot=%i for type=%i\n", i, type);*/
378 fallback_vbs
[type
] = i
;
380 if (i
> mgr
->nr_real_vertex_buffers
) {
381 mgr
->nr_real_vertex_buffers
= i
;
386 if (i
== PIPE_MAX_ATTRIBS
) {
387 /* fail, reset the number to its original value */
388 mgr
->nr_real_vertex_buffers
= mgr
->b
.nr_vertex_buffers
;
394 memcpy(mgr
->fallback_vbs
, fallback_vbs
, sizeof(fallback_vbs
));
399 u_vbuf_translate_begin(struct u_vbuf_priv
*mgr
,
400 int start_vertex
, unsigned num_vertices
,
401 int start_instance
, unsigned num_instances
,
402 int start_index
, unsigned num_indices
, int min_index
,
405 unsigned mask
[VB_NUM
] = {0};
406 struct translate_key key
[VB_NUM
];
407 unsigned elem_index
[VB_NUM
][PIPE_MAX_ATTRIBS
]; /* ... into key.elements */
410 int start
[VB_NUM
] = {
411 start_vertex
, /* VERTEX */
412 start_instance
, /* INSTANCE */
416 unsigned num
[VB_NUM
] = {
417 num_vertices
, /* VERTEX */
418 num_instances
, /* INSTANCE */
422 memset(key
, 0, sizeof(key
));
423 memset(elem_index
, ~0, sizeof(elem_index
));
425 /* See if there are vertex attribs of each type to translate and
427 for (i
= 0; i
< mgr
->ve
->count
; i
++) {
428 unsigned vb_index
= mgr
->ve
->ve
[i
].vertex_buffer_index
;
430 if (!mgr
->b
.vertex_buffer
[vb_index
].stride
) {
431 if (!mgr
->ve
->incompatible_layout_elem
[i
] &&
432 !mgr
->incompatible_vb
[vb_index
]) {
435 mask
[VB_CONST
] |= 1 << vb_index
;
436 } else if (mgr
->ve
->ve
[i
].instance_divisor
) {
437 if (!mgr
->ve
->incompatible_layout_elem
[i
] &&
438 !mgr
->incompatible_vb
[vb_index
]) {
441 mask
[VB_INSTANCE
] |= 1 << vb_index
;
443 if (!unroll_indices
&&
444 !mgr
->ve
->incompatible_layout_elem
[i
] &&
445 !mgr
->incompatible_vb
[vb_index
]) {
448 mask
[VB_VERTEX
] |= 1 << vb_index
;
452 assert(mask
[VB_VERTEX
] || mask
[VB_INSTANCE
] || mask
[VB_CONST
]);
454 /* Find free vertex buffer slots. */
455 if (!u_vbuf_translate_find_free_vb_slots(mgr
, mask
)) {
459 /* Initialize the translate keys. */
460 for (i
= 0; i
< mgr
->ve
->count
; i
++) {
461 struct translate_key
*k
;
462 struct translate_element
*te
;
463 unsigned bit
, vb_index
= mgr
->ve
->ve
[i
].vertex_buffer_index
;
466 if (!mgr
->ve
->incompatible_layout_elem
[i
] &&
467 !mgr
->incompatible_vb
[vb_index
] &&
468 (!unroll_indices
|| !(mask
[VB_VERTEX
] & bit
))) {
472 /* Set type to what we will translate.
473 * Whether vertex, instance, or constant attribs. */
474 for (type
= 0; type
< VB_NUM
; type
++) {
475 if (mask
[type
] & bit
) {
479 assert(type
< VB_NUM
);
480 assert(translate_is_output_format_supported(mgr
->ve
->native_format
[i
]));
481 /*printf("velem=%i type=%i\n", i, type);*/
483 /* Add the vertex element. */
485 elem_index
[type
][i
] = k
->nr_elements
;
487 te
= &k
->element
[k
->nr_elements
];
488 te
->type
= TRANSLATE_ELEMENT_NORMAL
;
489 te
->instance_divisor
= 0;
490 te
->input_buffer
= vb_index
;
491 te
->input_format
= mgr
->ve
->ve
[i
].src_format
;
492 te
->input_offset
= mgr
->ve
->ve
[i
].src_offset
;
493 te
->output_format
= mgr
->ve
->native_format
[i
];
494 te
->output_offset
= k
->output_stride
;
496 k
->output_stride
+= mgr
->ve
->native_format_size
[i
];
500 /* Translate buffers. */
501 for (type
= 0; type
< VB_NUM
; type
++) {
502 if (key
[type
].nr_elements
) {
503 u_vbuf_translate_buffers(mgr
, &key
[type
], mask
[type
],
504 mgr
->fallback_vbs
[type
],
505 start
[type
], num
[type
],
506 start_index
, num_indices
, min_index
,
507 unroll_indices
&& type
== VB_VERTEX
);
509 /* Fixup the stride for constant attribs. */
510 if (type
== VB_CONST
) {
511 mgr
->real_vertex_buffer
[mgr
->fallback_vbs
[VB_CONST
]].stride
= 0;
516 /* Setup new vertex elements. */
517 for (i
= 0; i
< mgr
->ve
->count
; i
++) {
518 for (type
= 0; type
< VB_NUM
; type
++) {
519 if (elem_index
[type
][i
] < key
[type
].nr_elements
) {
520 struct translate_element
*te
= &key
[type
].element
[elem_index
[type
][i
]];
521 mgr
->fallback_velems
[i
].instance_divisor
= mgr
->ve
->ve
[i
].instance_divisor
;
522 mgr
->fallback_velems
[i
].src_format
= te
->output_format
;
523 mgr
->fallback_velems
[i
].src_offset
= te
->output_offset
;
524 mgr
->fallback_velems
[i
].vertex_buffer_index
= mgr
->fallback_vbs
[type
];
526 /* elem_index[type][i] can only be set for one type. */
527 assert(type
> VB_INSTANCE
|| elem_index
[type
+1][i
] == ~0);
528 assert(type
> VB_VERTEX
|| elem_index
[type
+2][i
] == ~0);
532 /* No translating, just copy the original vertex element over. */
533 if (type
== VB_NUM
) {
534 memcpy(&mgr
->fallback_velems
[i
], &mgr
->ve
->ve
[i
],
535 sizeof(struct pipe_vertex_element
));
539 mgr
->fallback_ve
= u_vbuf_cache_set_vertex_elements(mgr
, mgr
->ve
->count
,
540 mgr
->fallback_velems
);
544 static void u_vbuf_translate_end(struct u_vbuf_priv
*mgr
)
548 /* Restore vertex elements. */
549 mgr
->driver_bind_vertex_elements_state(mgr
->pipe
, mgr
->ve
->driver_cso
);
550 mgr
->fallback_ve
= NULL
;
552 /* Unreference the now-unused VBOs. */
553 for (i
= 0; i
< VB_NUM
; i
++) {
554 unsigned vb
= mgr
->fallback_vbs
[i
];
556 pipe_resource_reference(&mgr
->real_vertex_buffer
[vb
].buffer
, NULL
);
557 mgr
->fallback_vbs
[i
] = ~0;
560 mgr
->nr_real_vertex_buffers
= mgr
->b
.nr_vertex_buffers
;
563 #define FORMAT_REPLACE(what, withwhat) \
564 case PIPE_FORMAT_##what: format = PIPE_FORMAT_##withwhat; break
567 u_vbuf_create_vertex_elements(struct pipe_context
*pipe
,
569 const struct pipe_vertex_element
*attribs
)
571 struct u_vbuf_priv
*mgr
= (struct u_vbuf_priv
*)pipe
->draw
;
573 struct pipe_vertex_element native_attribs
[PIPE_MAX_ATTRIBS
];
574 struct u_vbuf_elements
*ve
= CALLOC_STRUCT(u_vbuf_elements
);
578 memcpy(ve
->ve
, attribs
, sizeof(struct pipe_vertex_element
) * count
);
579 memcpy(native_attribs
, attribs
, sizeof(struct pipe_vertex_element
) * count
);
581 /* Set the best native format in case the original format is not
583 for (i
= 0; i
< count
; i
++) {
584 enum pipe_format format
= ve
->ve
[i
].src_format
;
586 ve
->src_format_size
[i
] = util_format_get_blocksize(format
);
588 /* Choose a native format.
589 * For now we don't care about the alignment, that's going to
590 * be sorted out later. */
591 if (!mgr
->b
.caps
.format_fixed32
) {
593 FORMAT_REPLACE(R32_FIXED
, R32_FLOAT
);
594 FORMAT_REPLACE(R32G32_FIXED
, R32G32_FLOAT
);
595 FORMAT_REPLACE(R32G32B32_FIXED
, R32G32B32_FLOAT
);
596 FORMAT_REPLACE(R32G32B32A32_FIXED
, R32G32B32A32_FLOAT
);
600 if (!mgr
->b
.caps
.format_float16
) {
602 FORMAT_REPLACE(R16_FLOAT
, R32_FLOAT
);
603 FORMAT_REPLACE(R16G16_FLOAT
, R32G32_FLOAT
);
604 FORMAT_REPLACE(R16G16B16_FLOAT
, R32G32B32_FLOAT
);
605 FORMAT_REPLACE(R16G16B16A16_FLOAT
, R32G32B32A32_FLOAT
);
609 if (!mgr
->b
.caps
.format_float64
) {
611 FORMAT_REPLACE(R64_FLOAT
, R32_FLOAT
);
612 FORMAT_REPLACE(R64G64_FLOAT
, R32G32_FLOAT
);
613 FORMAT_REPLACE(R64G64B64_FLOAT
, R32G32B32_FLOAT
);
614 FORMAT_REPLACE(R64G64B64A64_FLOAT
, R32G32B32A32_FLOAT
);
618 if (!mgr
->b
.caps
.format_norm32
) {
620 FORMAT_REPLACE(R32_UNORM
, R32_FLOAT
);
621 FORMAT_REPLACE(R32G32_UNORM
, R32G32_FLOAT
);
622 FORMAT_REPLACE(R32G32B32_UNORM
, R32G32B32_FLOAT
);
623 FORMAT_REPLACE(R32G32B32A32_UNORM
, R32G32B32A32_FLOAT
);
624 FORMAT_REPLACE(R32_SNORM
, R32_FLOAT
);
625 FORMAT_REPLACE(R32G32_SNORM
, R32G32_FLOAT
);
626 FORMAT_REPLACE(R32G32B32_SNORM
, R32G32B32_FLOAT
);
627 FORMAT_REPLACE(R32G32B32A32_SNORM
, R32G32B32A32_FLOAT
);
631 if (!mgr
->b
.caps
.format_scaled32
) {
633 FORMAT_REPLACE(R32_USCALED
, R32_FLOAT
);
634 FORMAT_REPLACE(R32G32_USCALED
, R32G32_FLOAT
);
635 FORMAT_REPLACE(R32G32B32_USCALED
, R32G32B32_FLOAT
);
636 FORMAT_REPLACE(R32G32B32A32_USCALED
,R32G32B32A32_FLOAT
);
637 FORMAT_REPLACE(R32_SSCALED
, R32_FLOAT
);
638 FORMAT_REPLACE(R32G32_SSCALED
, R32G32_FLOAT
);
639 FORMAT_REPLACE(R32G32B32_SSCALED
, R32G32B32_FLOAT
);
640 FORMAT_REPLACE(R32G32B32A32_SSCALED
,R32G32B32A32_FLOAT
);
645 native_attribs
[i
].src_format
= format
;
646 ve
->native_format
[i
] = format
;
647 ve
->native_format_size
[i
] =
648 util_format_get_blocksize(ve
->native_format
[i
]);
650 ve
->incompatible_layout_elem
[i
] =
651 ve
->ve
[i
].src_format
!= ve
->native_format
[i
] ||
652 (!mgr
->b
.caps
.fetch_dword_unaligned
&& ve
->ve
[i
].src_offset
% 4 != 0);
653 ve
->incompatible_layout
=
654 ve
->incompatible_layout
||
655 ve
->incompatible_layout_elem
[i
];
658 /* Align the formats to the size of DWORD if needed. */
659 if (!mgr
->b
.caps
.fetch_dword_unaligned
) {
660 for (i
= 0; i
< count
; i
++) {
661 ve
->native_format_size
[i
] = align(ve
->native_format_size
[i
], 4);
666 mgr
->driver_create_vertex_elements_state(pipe
, count
, native_attribs
);
670 static void u_vbuf_bind_vertex_elements(struct pipe_context
*pipe
,
673 struct u_vbuf_priv
*mgr
= (struct u_vbuf_priv
*)pipe
->draw
;
674 struct u_vbuf_elements
*ve
= cso
;
677 mgr
->b
.vertex_elements
= ve
;
678 mgr
->driver_bind_vertex_elements_state(pipe
, ve
? ve
->driver_cso
: NULL
);
681 static void u_vbuf_delete_vertex_elements(struct pipe_context
*pipe
,
684 struct u_vbuf_priv
*mgr
= (struct u_vbuf_priv
*)pipe
->draw
;
685 struct u_vbuf_elements
*ve
= cso
;
687 mgr
->driver_delete_vertex_elements_state(pipe
, ve
->driver_cso
);
691 static void u_vbuf_set_vertex_buffers(struct pipe_context
*pipe
,
693 const struct pipe_vertex_buffer
*bufs
)
695 struct u_vbuf_priv
*mgr
= (struct u_vbuf_priv
*)pipe
->draw
;
698 mgr
->any_user_vbs
= FALSE
;
699 mgr
->incompatible_vb_layout
= FALSE
;
700 memset(mgr
->incompatible_vb
, 0, sizeof(mgr
->incompatible_vb
));
702 if (!mgr
->b
.caps
.fetch_dword_unaligned
) {
703 /* Check if the strides and offsets are aligned to the size of DWORD. */
704 for (i
= 0; i
< count
; i
++) {
705 if (bufs
[i
].buffer
) {
706 if (bufs
[i
].stride
% 4 != 0 ||
707 bufs
[i
].buffer_offset
% 4 != 0) {
708 mgr
->incompatible_vb_layout
= TRUE
;
709 mgr
->incompatible_vb
[i
] = TRUE
;
715 for (i
= 0; i
< count
; i
++) {
716 const struct pipe_vertex_buffer
*vb
= &bufs
[i
];
718 pipe_resource_reference(&mgr
->b
.vertex_buffer
[i
].buffer
, vb
->buffer
);
720 mgr
->real_vertex_buffer
[i
].buffer_offset
=
721 mgr
->b
.vertex_buffer
[i
].buffer_offset
= vb
->buffer_offset
;
723 mgr
->real_vertex_buffer
[i
].stride
=
724 mgr
->b
.vertex_buffer
[i
].stride
= vb
->stride
;
727 mgr
->incompatible_vb
[i
]) {
728 pipe_resource_reference(&mgr
->real_vertex_buffer
[i
].buffer
, NULL
);
732 if (vb
->buffer
->user_ptr
) {
733 pipe_resource_reference(&mgr
->real_vertex_buffer
[i
].buffer
, NULL
);
734 mgr
->any_user_vbs
= TRUE
;
738 pipe_resource_reference(&mgr
->real_vertex_buffer
[i
].buffer
, vb
->buffer
);
741 for (i
= count
; i
< mgr
->b
.nr_vertex_buffers
; i
++) {
742 pipe_resource_reference(&mgr
->b
.vertex_buffer
[i
].buffer
, NULL
);
744 for (i
= count
; i
< mgr
->nr_real_vertex_buffers
; i
++) {
745 pipe_resource_reference(&mgr
->real_vertex_buffer
[i
].buffer
, NULL
);
748 mgr
->b
.nr_vertex_buffers
= count
;
749 mgr
->nr_real_vertex_buffers
= count
;
750 mgr
->vertex_buffers_dirty
= TRUE
;
753 static void u_vbuf_set_index_buffer(struct pipe_context
*pipe
,
754 const struct pipe_index_buffer
*ib
)
756 struct u_vbuf_priv
*mgr
= pipe
->draw
;
758 if (ib
&& ib
->buffer
) {
759 assert(ib
->offset
% ib
->index_size
== 0);
760 pipe_resource_reference(&mgr
->index_buffer
.buffer
, ib
->buffer
);
761 mgr
->index_buffer
.offset
= ib
->offset
;
762 mgr
->index_buffer
.index_size
= ib
->index_size
;
764 pipe_resource_reference(&mgr
->index_buffer
.buffer
, NULL
);
767 mgr
->driver_set_index_buffer(pipe
, ib
);
771 u_vbuf_upload_buffers(struct u_vbuf_priv
*mgr
,
772 int start_vertex
, unsigned num_vertices
,
773 int start_instance
, unsigned num_instances
)
776 unsigned nr_velems
= mgr
->ve
->count
;
777 unsigned nr_vbufs
= mgr
->b
.nr_vertex_buffers
;
778 struct pipe_vertex_element
*velems
=
779 mgr
->fallback_ve
? mgr
->fallback_velems
: mgr
->ve
->ve
;
780 unsigned start_offset
[PIPE_MAX_ATTRIBS
];
781 unsigned end_offset
[PIPE_MAX_ATTRIBS
] = {0};
783 /* Determine how much data needs to be uploaded. */
784 for (i
= 0; i
< nr_velems
; i
++) {
785 struct pipe_vertex_element
*velem
= &velems
[i
];
786 unsigned index
= velem
->vertex_buffer_index
;
787 struct pipe_vertex_buffer
*vb
= &mgr
->b
.vertex_buffer
[index
];
788 unsigned instance_div
, first
, size
;
790 /* Skip the buffers generated by translate. */
791 if (index
== mgr
->fallback_vbs
[VB_VERTEX
] ||
792 index
== mgr
->fallback_vbs
[VB_INSTANCE
] ||
793 index
== mgr
->fallback_vbs
[VB_CONST
]) {
799 if (!vb
->buffer
->user_ptr
) {
803 instance_div
= velem
->instance_divisor
;
804 first
= vb
->buffer_offset
+ velem
->src_offset
;
807 /* Constant attrib. */
808 size
= mgr
->ve
->src_format_size
[i
];
809 } else if (instance_div
) {
810 /* Per-instance attrib. */
811 unsigned count
= (num_instances
+ instance_div
- 1) / instance_div
;
812 first
+= vb
->stride
* start_instance
;
813 size
= vb
->stride
* (count
- 1) + mgr
->ve
->src_format_size
[i
];
815 /* Per-vertex attrib. */
816 first
+= vb
->stride
* start_vertex
;
817 size
= vb
->stride
* (num_vertices
- 1) + mgr
->ve
->src_format_size
[i
];
820 /* Update offsets. */
821 if (!end_offset
[index
]) {
822 start_offset
[index
] = first
;
823 end_offset
[index
] = first
+ size
;
825 if (first
< start_offset
[index
])
826 start_offset
[index
] = first
;
827 if (first
+ size
> end_offset
[index
])
828 end_offset
[index
] = first
+ size
;
832 /* Upload buffers. */
833 for (i
= 0; i
< nr_vbufs
; i
++) {
834 unsigned start
, end
= end_offset
[i
];
835 struct pipe_vertex_buffer
*real_vb
;
842 start
= start_offset
[i
];
845 real_vb
= &mgr
->real_vertex_buffer
[i
];
846 ptr
= mgr
->b
.vertex_buffer
[i
].buffer
->user_ptr
;
848 u_upload_data(mgr
->b
.uploader
, start
, end
- start
, ptr
+ start
,
849 &real_vb
->buffer_offset
, &real_vb
->buffer
);
851 real_vb
->buffer_offset
-= start
;
855 unsigned u_vbuf_draw_max_vertex_count(struct u_vbuf
*mgrb
)
857 struct u_vbuf_priv
*mgr
= (struct u_vbuf_priv
*)mgrb
;
858 unsigned i
, nr
= mgr
->ve
->count
;
859 struct pipe_vertex_element
*velems
=
860 mgr
->fallback_ve
? mgr
->fallback_velems
: mgr
->ve
->ve
;
861 unsigned result
= ~0;
863 for (i
= 0; i
< nr
; i
++) {
864 struct pipe_vertex_buffer
*vb
=
865 &mgr
->real_vertex_buffer
[velems
[i
].vertex_buffer_index
];
866 unsigned size
, max_count
, value
;
868 /* We're not interested in constant and per-instance attribs. */
871 velems
[i
].instance_divisor
) {
875 size
= vb
->buffer
->width0
;
877 /* Subtract buffer_offset. */
878 value
= vb
->buffer_offset
;
884 /* Subtract src_offset. */
885 value
= velems
[i
].src_offset
;
891 /* Subtract format_size. */
892 value
= mgr
->ve
->native_format_size
[i
];
898 /* Compute the max count. */
899 max_count
= 1 + size
/ vb
->stride
;
900 result
= MIN2(result
, max_count
);
905 static boolean
u_vbuf_need_minmax_index(struct u_vbuf_priv
*mgr
)
907 unsigned i
, nr
= mgr
->ve
->count
;
909 for (i
= 0; i
< nr
; i
++) {
910 struct pipe_vertex_buffer
*vb
;
913 /* Per-instance attribs don't need min/max_index. */
914 if (mgr
->ve
->ve
[i
].instance_divisor
) {
918 index
= mgr
->ve
->ve
[i
].vertex_buffer_index
;
919 vb
= &mgr
->b
.vertex_buffer
[index
];
921 /* Constant attribs don't need min/max_index. */
926 /* Per-vertex attribs need min/max_index. */
927 if (vb
->buffer
->user_ptr
||
928 mgr
->ve
->incompatible_layout_elem
[i
] ||
929 mgr
->incompatible_vb
[index
]) {
937 static boolean
u_vbuf_mapping_vertex_buffer_blocks(struct u_vbuf_priv
*mgr
)
939 unsigned i
, nr
= mgr
->ve
->count
;
941 for (i
= 0; i
< nr
; i
++) {
942 struct pipe_vertex_buffer
*vb
;
945 /* Per-instance attribs are not per-vertex data. */
946 if (mgr
->ve
->ve
[i
].instance_divisor
) {
950 index
= mgr
->ve
->ve
[i
].vertex_buffer_index
;
951 vb
= &mgr
->b
.vertex_buffer
[index
];
953 /* Constant attribs are not per-vertex data. */
958 /* Return true for the hw buffers which don't need to be translated. */
959 /* XXX we could use some kind of a is-busy query. */
960 if (!vb
->buffer
->user_ptr
&&
961 !mgr
->ve
->incompatible_layout_elem
[i
] &&
962 !mgr
->incompatible_vb
[index
]) {
970 static void u_vbuf_get_minmax_index(struct pipe_context
*pipe
,
971 struct pipe_index_buffer
*ib
,
972 const struct pipe_draw_info
*info
,
976 struct pipe_transfer
*transfer
= NULL
;
979 unsigned restart_index
= info
->restart_index
;
981 if (ib
->buffer
->user_ptr
) {
982 indices
= ib
->buffer
->user_ptr
+
983 ib
->offset
+ info
->start
* ib
->index_size
;
985 indices
= pipe_buffer_map_range(pipe
, ib
->buffer
,
986 ib
->offset
+ info
->start
* ib
->index_size
,
987 info
->count
* ib
->index_size
,
988 PIPE_TRANSFER_READ
, &transfer
);
991 switch (ib
->index_size
) {
993 const unsigned *ui_indices
= (const unsigned*)indices
;
995 unsigned min_ui
= ~0U;
996 if (info
->primitive_restart
) {
997 for (i
= 0; i
< info
->count
; i
++) {
998 if (ui_indices
[i
] != restart_index
) {
999 if (ui_indices
[i
] > max_ui
) max_ui
= ui_indices
[i
];
1000 if (ui_indices
[i
] < min_ui
) min_ui
= ui_indices
[i
];
1005 for (i
= 0; i
< info
->count
; i
++) {
1006 if (ui_indices
[i
] > max_ui
) max_ui
= ui_indices
[i
];
1007 if (ui_indices
[i
] < min_ui
) min_ui
= ui_indices
[i
];
1010 *out_min_index
= min_ui
;
1011 *out_max_index
= max_ui
;
1015 const unsigned short *us_indices
= (const unsigned short*)indices
;
1016 unsigned max_us
= 0;
1017 unsigned min_us
= ~0U;
1018 if (info
->primitive_restart
) {
1019 for (i
= 0; i
< info
->count
; i
++) {
1020 if (us_indices
[i
] != restart_index
) {
1021 if (us_indices
[i
] > max_us
) max_us
= us_indices
[i
];
1022 if (us_indices
[i
] < min_us
) min_us
= us_indices
[i
];
1027 for (i
= 0; i
< info
->count
; i
++) {
1028 if (us_indices
[i
] > max_us
) max_us
= us_indices
[i
];
1029 if (us_indices
[i
] < min_us
) min_us
= us_indices
[i
];
1032 *out_min_index
= min_us
;
1033 *out_max_index
= max_us
;
1037 const unsigned char *ub_indices
= (const unsigned char*)indices
;
1038 unsigned max_ub
= 0;
1039 unsigned min_ub
= ~0U;
1040 if (info
->primitive_restart
) {
1041 for (i
= 0; i
< info
->count
; i
++) {
1042 if (ub_indices
[i
] != restart_index
) {
1043 if (ub_indices
[i
] > max_ub
) max_ub
= ub_indices
[i
];
1044 if (ub_indices
[i
] < min_ub
) min_ub
= ub_indices
[i
];
1049 for (i
= 0; i
< info
->count
; i
++) {
1050 if (ub_indices
[i
] > max_ub
) max_ub
= ub_indices
[i
];
1051 if (ub_indices
[i
] < min_ub
) min_ub
= ub_indices
[i
];
1054 *out_min_index
= min_ub
;
1055 *out_max_index
= max_ub
;
1065 pipe_buffer_unmap(pipe
, transfer
);
1069 static void u_vbuf_draw_vbo(struct pipe_context
*pipe
,
1070 const struct pipe_draw_info
*info
)
1072 struct u_vbuf_priv
*mgr
= (struct u_vbuf_priv
*)pipe
->draw
;
1073 int start_vertex
, min_index
;
1074 unsigned num_vertices
;
1075 bool unroll_indices
= false;
1077 /* Normal draw. No fallback and no user buffers. */
1078 if (!mgr
->incompatible_vb_layout
&&
1079 !mgr
->ve
->incompatible_layout
&&
1080 !mgr
->any_user_vbs
) {
1081 /* Set vertex buffers if needed. */
1082 if (mgr
->vertex_buffers_dirty
) {
1083 mgr
->driver_set_vertex_buffers(pipe
, mgr
->nr_real_vertex_buffers
,
1084 mgr
->real_vertex_buffer
);
1085 mgr
->vertex_buffers_dirty
= FALSE
;
1088 mgr
->driver_draw_vbo(pipe
, info
);
1092 if (info
->indexed
) {
1094 bool index_bounds_valid
= false;
1096 if (info
->max_index
!= ~0) {
1097 min_index
= info
->min_index
;
1098 max_index
= info
->max_index
;
1099 index_bounds_valid
= true;
1100 } else if (u_vbuf_need_minmax_index(mgr
)) {
1101 u_vbuf_get_minmax_index(mgr
->pipe
, &mgr
->index_buffer
, info
,
1102 &min_index
, &max_index
);
1103 index_bounds_valid
= true;
1106 /* If the index bounds are valid, it means some upload or translation
1107 * of per-vertex attribs will be performed. */
1108 if (index_bounds_valid
) {
1109 assert(min_index
<= max_index
);
1111 start_vertex
= min_index
+ info
->index_bias
;
1112 num_vertices
= max_index
+ 1 - min_index
;
1114 /* Primitive restart doesn't work when unrolling indices.
1115 * We would have to break this drawing operation into several ones. */
1116 /* Use some heuristic to see if unrolling indices improves
1118 if (!info
->primitive_restart
&&
1119 num_vertices
> info
->count
*2 &&
1120 num_vertices
-info
->count
> 32 &&
1121 !u_vbuf_mapping_vertex_buffer_blocks(mgr
)) {
1122 /*printf("num_vertices=%i count=%i\n", num_vertices, info->count);*/
1123 unroll_indices
= true;
1126 /* Nothing to do for per-vertex attribs. */
1132 start_vertex
= info
->start
;
1133 num_vertices
= info
->count
;
1137 /* Translate vertices with non-native layouts or formats. */
1138 if (unroll_indices
||
1139 mgr
->incompatible_vb_layout
||
1140 mgr
->ve
->incompatible_layout
) {
1141 /* XXX check the return value */
1142 u_vbuf_translate_begin(mgr
, start_vertex
, num_vertices
,
1143 info
->start_instance
, info
->instance_count
,
1144 info
->start
, info
->count
, min_index
,
1148 /* Upload user buffers. */
1149 if (mgr
->any_user_vbs
) {
1150 u_vbuf_upload_buffers(mgr
, start_vertex
, num_vertices
,
1151 info
->start_instance
, info
->instance_count
);
1155 if (unroll_indices) {
1156 printf("unrolling indices: start_vertex = %i, num_vertices = %i\n",
1157 start_vertex, num_vertices);
1158 util_dump_draw_info(stdout, info);
1163 for (i = 0; i < mgr->b.nr_vertex_buffers; i++) {
1164 printf("input %i: ", i);
1165 util_dump_vertex_buffer(stdout, mgr->b.vertex_buffer+i);
1168 for (i = 0; i < mgr->nr_real_vertex_buffers; i++) {
1169 printf("real %i: ", i);
1170 util_dump_vertex_buffer(stdout, mgr->real_vertex_buffer+i);
1175 mgr
->driver_set_vertex_buffers(mgr
->pipe
, mgr
->nr_real_vertex_buffers
,
1176 mgr
->real_vertex_buffer
);
1178 if (unlikely(unroll_indices
)) {
1179 struct pipe_draw_info new_info
= *info
;
1180 new_info
.indexed
= FALSE
;
1181 new_info
.index_bias
= 0;
1182 new_info
.min_index
= 0;
1183 new_info
.max_index
= info
->count
- 1;
1186 mgr
->driver_draw_vbo(pipe
, &new_info
);
1188 mgr
->driver_draw_vbo(pipe
, info
);
1191 if (mgr
->fallback_ve
) {
1192 u_vbuf_translate_end(mgr
);
1194 mgr
->vertex_buffers_dirty
= TRUE
;
1197 static void u_vbuf_install(struct u_vbuf_priv
*mgr
)
1199 struct pipe_context
*pipe
= mgr
->pipe
;
1200 assert(!pipe
->draw
);
1203 mgr
->driver_set_index_buffer
= pipe
->set_index_buffer
;
1204 mgr
->driver_set_vertex_buffers
= pipe
->set_vertex_buffers
;
1205 mgr
->driver_create_vertex_elements_state
=
1206 pipe
->create_vertex_elements_state
;
1207 mgr
->driver_bind_vertex_elements_state
= pipe
->bind_vertex_elements_state
;
1208 mgr
->driver_delete_vertex_elements_state
=
1209 pipe
->delete_vertex_elements_state
;
1210 mgr
->driver_draw_vbo
= pipe
->draw_vbo
;
1212 pipe
->set_index_buffer
= u_vbuf_set_index_buffer
;
1213 pipe
->set_vertex_buffers
= u_vbuf_set_vertex_buffers
;
1214 pipe
->create_vertex_elements_state
= u_vbuf_create_vertex_elements
;
1215 pipe
->bind_vertex_elements_state
= u_vbuf_bind_vertex_elements
;
1216 pipe
->delete_vertex_elements_state
= u_vbuf_delete_vertex_elements
;
1217 pipe
->draw_vbo
= u_vbuf_draw_vbo
;