• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**************************************************************************
2  *
3  * Copyright 2011 VMware, Inc.
4  * All Rights Reserved.
5  *
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:
13  *
14  * The above copyright notice and this permission notice (including the
15  * next paragraph) shall be included in all copies or substantial portions
16  * of the Software.
17  *
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.
25  *
26  **************************************************************************/
27 
28 
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"
34 
35 
36 /**
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.
41  *
42  * Note that if zero is returned it means that one or more buffers is
43  * too small to contain any valid vertex data.
44  */
45 unsigned
util_draw_max_index(const struct pipe_vertex_buffer * vertex_buffers,const struct pipe_vertex_element * vertex_elements,unsigned nr_vertex_elements,const struct pipe_draw_info * info)46 util_draw_max_index(
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)
51 {
52    unsigned max_index;
53    unsigned i;
54 
55    max_index = ~0U - 1;
56    for (i = 0; i < nr_vertex_elements; i++) {
57       const struct pipe_vertex_element *element =
58          &vertex_elements[i];
59       const struct pipe_vertex_buffer *buffer =
60          &vertex_buffers[element->vertex_buffer_index];
61       unsigned buffer_size;
62       const struct util_format_description *format_desc;
63       unsigned format_size;
64 
65       if (buffer->is_user_buffer || !buffer->buffer.resource) {
66          continue;
67       }
68 
69       assert(buffer->buffer.resource->height0 == 1);
70       assert(buffer->buffer.resource->depth0 == 1);
71       buffer_size = buffer->buffer.resource->width0;
72 
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;
78 
79       if (buffer->buffer_offset >= buffer_size) {
80          /* buffer is too small */
81          return 0;
82       }
83 
84       buffer_size -= buffer->buffer_offset;
85 
86       if (element->src_offset >= buffer_size) {
87          /* buffer is too small */
88          return 0;
89       }
90 
91       buffer_size -= element->src_offset;
92 
93       if (format_size > buffer_size) {
94          /* buffer is too small */
95          return 0;
96       }
97 
98       buffer_size -= format_size;
99 
100       if (buffer->stride != 0) {
101          unsigned buffer_max_index;
102 
103          buffer_max_index = buffer_size / buffer->stride;
104 
105          if (element->instance_divisor == 0) {
106             /* Per-vertex data */
107             max_index = MIN2(max_index, buffer_max_index);
108          }
109          else {
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
116                 * size. */
117                debug_printf("%s: too many instances for vertex buffer\n",
118                             __FUNCTION__);
119                return 0;
120             }
121          }
122       }
123    }
124 
125    return max_index + 1;
126 }
127 
128 struct u_indirect_params *
util_draw_indirect_read(struct pipe_context * pipe,const struct pipe_draw_info * info_in,const struct pipe_draw_indirect_info * indirect,unsigned * num_draws)129 util_draw_indirect_read(struct pipe_context *pipe,
130                         const struct pipe_draw_info *info_in,
131                         const struct pipe_draw_indirect_info *indirect,
132                         unsigned *num_draws)
133 {
134    struct pipe_transfer *transfer;
135    uint32_t *params;
136    struct u_indirect_params *draws;
137    unsigned num_params = info_in->index_size ? 5 : 4;
138 
139    assert(indirect);
140    assert(!indirect->count_from_stream_output);
141 
142    uint32_t draw_count = indirect->draw_count;
143    if (indirect->indirect_draw_count) {
144       struct pipe_transfer *dc_transfer;
145       uint32_t *dc_param = pipe_buffer_map_range(pipe,
146                                                  indirect->indirect_draw_count,
147                                                  indirect->indirect_draw_count_offset,
148                                                  4, PIPE_MAP_READ, &dc_transfer);
149       if (!dc_transfer) {
150          debug_printf("%s: failed to map indirect draw count buffer\n", __FUNCTION__);
151          return NULL;
152       }
153       draw_count = dc_param[0];
154       pipe_buffer_unmap(pipe, dc_transfer);
155    }
156    if (!draw_count) {
157       *num_draws = draw_count;
158       return NULL;
159    }
160    draws = malloc(sizeof(struct u_indirect_params) * draw_count);
161    if (!draws)
162       return NULL;
163 
164    unsigned map_size = (draw_count - 1) * indirect->stride + (num_params * sizeof(uint32_t));
165    params = pipe_buffer_map_range(pipe,
166                                   indirect->buffer,
167                                   indirect->offset,
168                                   map_size,
169                                   PIPE_MAP_READ,
170                                   &transfer);
171    if (!transfer) {
172       debug_printf("%s: failed to map indirect buffer\n", __FUNCTION__);
173       free(draws);
174       return NULL;
175    }
176 
177    for (unsigned i = 0; i < draw_count; i++) {
178       memcpy(&draws[i].info, info_in, sizeof(struct pipe_draw_info));
179       draws[i].draw.count = params[0];
180       draws[i].info.instance_count = params[1];
181       draws[i].draw.start = params[2];
182       draws[i].draw.index_bias = info_in->index_size ? params[3] : 0;
183       draws[i].info.start_instance = info_in->index_size ? params[4] : params[3];
184       params += indirect->stride / 4;
185    }
186    pipe_buffer_unmap(pipe, transfer);
187    *num_draws = draw_count;
188    return draws;
189 }
190 
191 /* This extracts the draw arguments from the indirect resource,
192  * puts them into a new instance of pipe_draw_info, and calls draw_vbo on it.
193  */
194 void
util_draw_indirect(struct pipe_context * pipe,const struct pipe_draw_info * info_in,const struct pipe_draw_indirect_info * indirect)195 util_draw_indirect(struct pipe_context *pipe,
196                    const struct pipe_draw_info *info_in,
197                    const struct pipe_draw_indirect_info *indirect)
198 {
199    struct pipe_draw_info info;
200    struct pipe_transfer *transfer;
201    uint32_t *params;
202    unsigned num_params = info_in->index_size ? 5 : 4;
203 
204    assert(indirect);
205    assert(!indirect->count_from_stream_output);
206 
207    memcpy(&info, info_in, sizeof(info));
208 
209    uint32_t draw_count = indirect->draw_count;
210 
211    if (indirect->indirect_draw_count) {
212       struct pipe_transfer *dc_transfer;
213       uint32_t *dc_param = pipe_buffer_map_range(pipe,
214                                                  indirect->indirect_draw_count,
215                                                  indirect->indirect_draw_count_offset,
216                                                  4, PIPE_MAP_READ, &dc_transfer);
217       if (!dc_transfer) {
218          debug_printf("%s: failed to map indirect draw count buffer\n", __FUNCTION__);
219          return;
220       }
221       if (dc_param[0] < draw_count)
222          draw_count = dc_param[0];
223       pipe_buffer_unmap(pipe, dc_transfer);
224    }
225 
226    if (indirect->stride)
227       num_params = MIN2(indirect->stride / 4, num_params);
228    params = (uint32_t *)
229       pipe_buffer_map_range(pipe,
230                             indirect->buffer,
231                             indirect->offset,
232                             (num_params * indirect->draw_count) * sizeof(uint32_t),
233                             PIPE_MAP_READ,
234                             &transfer);
235    if (!transfer) {
236       debug_printf("%s: failed to map indirect buffer\n", __FUNCTION__);
237       return;
238    }
239 
240    for (unsigned i = 0; i < draw_count; i++) {
241       struct pipe_draw_start_count_bias draw;
242 
243       draw.count = params[0];
244       info.instance_count = params[1];
245       draw.start = params[2];
246       draw.index_bias = info_in->index_size ? params[3] : 0;
247       info.start_instance = info_in->index_size ? params[4] : params[3];
248 
249       pipe->draw_vbo(pipe, &info, i, NULL, &draw, 1);
250 
251       params += indirect->stride / 4;
252    }
253    pipe_buffer_unmap(pipe, transfer);
254 }
255 
256 void
util_draw_multi(struct pipe_context * pctx,const struct pipe_draw_info * info,unsigned drawid_offset,const struct pipe_draw_indirect_info * indirect,const struct pipe_draw_start_count_bias * draws,unsigned num_draws)257 util_draw_multi(struct pipe_context *pctx, const struct pipe_draw_info *info,
258                 unsigned drawid_offset,
259                 const struct pipe_draw_indirect_info *indirect,
260                 const struct pipe_draw_start_count_bias *draws,
261                 unsigned num_draws)
262 {
263    struct pipe_draw_info tmp_info = *info;
264    unsigned drawid = drawid_offset;
265 
266    /* If you call this with num_draws==1, that is probably going to be
267     * an infinite loop
268     */
269    assert(num_draws > 1);
270 
271    for (unsigned i = 0; i < num_draws; i++) {
272       if (indirect || (draws[i].count && info->instance_count))
273          pctx->draw_vbo(pctx, &tmp_info, drawid, indirect, &draws[i], 1);
274       if (tmp_info.increment_draw_id)
275          drawid++;
276    }
277 }
278