1 /**************************************************************************
3 * Copyright 2011 VMware, Inc.
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 VMWARE 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 **************************************************************************/
29 #include "util/u_debug.h"
30 #include "util/u_inlines.h"
31 #include "util/u_math.h"
32 #include "util/format/u_format.h"
33 #include "util/u_draw.h"
37 * Returns the largest legal index value plus one for the current set
38 * of bound vertex buffers. Regardless of any other consideration,
39 * all vertex lookups need to be clamped to 0..max_index-1 to prevent
40 * an out-of-bound access.
42 * Note that if zero is returned it means that one or more buffers is
43 * too small to contain any valid vertex data.
47 const struct pipe_vertex_buffer
*vertex_buffers
,
48 const struct pipe_vertex_element
*vertex_elements
,
49 unsigned nr_vertex_elements
,
50 const struct pipe_draw_info
*info
)
56 for (i
= 0; i
< nr_vertex_elements
; i
++) {
57 const struct pipe_vertex_element
*element
=
59 const struct pipe_vertex_buffer
*buffer
=
60 &vertex_buffers
[element
->vertex_buffer_index
];
62 const struct util_format_description
*format_desc
;
65 if (buffer
->is_user_buffer
|| !buffer
->buffer
.resource
) {
69 assert(buffer
->buffer
.resource
->height0
== 1);
70 assert(buffer
->buffer
.resource
->depth0
== 1);
71 buffer_size
= buffer
->buffer
.resource
->width0
;
73 format_desc
= util_format_description(element
->src_format
);
74 assert(format_desc
->block
.width
== 1);
75 assert(format_desc
->block
.height
== 1);
76 assert(format_desc
->block
.bits
% 8 == 0);
77 format_size
= format_desc
->block
.bits
/8;
79 if (buffer
->buffer_offset
>= buffer_size
) {
80 /* buffer is too small */
84 buffer_size
-= buffer
->buffer_offset
;
86 if (element
->src_offset
>= buffer_size
) {
87 /* buffer is too small */
91 buffer_size
-= element
->src_offset
;
93 if (format_size
> buffer_size
) {
94 /* buffer is too small */
98 buffer_size
-= format_size
;
100 if (buffer
->stride
!= 0) {
101 unsigned buffer_max_index
;
103 buffer_max_index
= buffer_size
/ buffer
->stride
;
105 if (element
->instance_divisor
== 0) {
106 /* Per-vertex data */
107 max_index
= MIN2(max_index
, buffer_max_index
);
110 /* Per-instance data. Simply make sure gallium frontends didn't
111 * request more instances than those that fit in the buffer */
112 if ((info
->start_instance
+ info
->instance_count
)/element
->instance_divisor
113 > (buffer_max_index
+ 1)) {
114 /* FIXME: We really should stop thinking in terms of maximum
115 * indices/instances and simply start clamping against buffer
117 debug_printf("%s: too many instances for vertex buffer\n",
125 return max_index
+ 1;
129 /* This extracts the draw arguments from the info_in->indirect resource,
130 * puts them into a new instance of pipe_draw_info, and calls draw_vbo on it.
133 util_draw_indirect(struct pipe_context
*pipe
,
134 const struct pipe_draw_info
*info_in
)
136 struct pipe_draw_info info
;
137 struct pipe_transfer
*transfer
;
139 unsigned num_params
= info_in
->index_size
? 5 : 4;
141 assert(info_in
->indirect
);
142 assert(!info_in
->count_from_stream_output
);
144 memcpy(&info
, info_in
, sizeof(info
));
146 uint32_t draw_count
= info_in
->indirect
->draw_count
;
148 if (info_in
->indirect
->indirect_draw_count
) {
149 struct pipe_transfer
*dc_transfer
;
150 uint32_t *dc_param
= pipe_buffer_map_range(pipe
,
151 info_in
->indirect
->indirect_draw_count
,
152 info_in
->indirect
->indirect_draw_count_offset
,
153 4, PIPE_TRANSFER_READ
, &dc_transfer
);
155 debug_printf("%s: failed to map indirect draw count buffer\n", __FUNCTION__
);
158 if (dc_param
[0] < draw_count
)
159 draw_count
= dc_param
[0];
160 pipe_buffer_unmap(pipe
, dc_transfer
);
163 if (info_in
->indirect
->stride
)
164 num_params
= MIN2(info_in
->indirect
->stride
/ 4, num_params
);
165 params
= (uint32_t *)
166 pipe_buffer_map_range(pipe
,
167 info_in
->indirect
->buffer
,
168 info_in
->indirect
->offset
,
169 (num_params
* info_in
->indirect
->draw_count
) * sizeof(uint32_t),
173 debug_printf("%s: failed to map indirect buffer\n", __FUNCTION__
);
177 for (unsigned i
= 0; i
< draw_count
; i
++) {
178 info
.count
= params
[0];
179 info
.instance_count
= params
[1];
180 info
.start
= params
[2];
181 info
.index_bias
= info_in
->index_size
? params
[3] : 0;
182 info
.start_instance
= info_in
->index_size
? params
[4] : params
[3];
184 info
.indirect
= NULL
;
186 pipe
->draw_vbo(pipe
, &info
);
188 params
+= info_in
->indirect
->stride
/ 4;
190 pipe_buffer_unmap(pipe
, transfer
);