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 */
45unsigned
46util_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
128struct u_indirect_params *
129util_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      if (dc_param[0] < draw_count)
154         draw_count = dc_param[0];
155      pipe_buffer_unmap(pipe, dc_transfer);
156   }
157   draws = malloc(sizeof(struct u_indirect_params) * draw_count);
158   if (!draws)
159      return NULL;
160
161   if (indirect->stride)
162      num_params = MIN2(indirect->stride / 4, num_params);
163   params = pipe_buffer_map_range(pipe,
164                                  indirect->buffer,
165                                  indirect->offset,
166                                  (num_params * indirect->draw_count) * sizeof(uint32_t),
167                                  PIPE_MAP_READ,
168                                  &transfer);
169   if (!transfer) {
170      debug_printf("%s: failed to map indirect buffer\n", __FUNCTION__);
171      free(draws);
172      return NULL;
173   }
174
175   for (unsigned i = 0; i < draw_count; i++) {
176      memcpy(&draws[i].info, info_in, sizeof(struct pipe_draw_info));
177      draws[i].draw.count = params[0];
178      draws[i].info.instance_count = params[1];
179      draws[i].draw.start = params[2];
180      draws[i].draw.index_bias = info_in->index_size ? params[3] : 0;
181      draws[i].info.start_instance = info_in->index_size ? params[4] : params[3];
182      params += indirect->stride / 4;
183   }
184   pipe_buffer_unmap(pipe, transfer);
185   *num_draws = draw_count;
186   return draws;
187}
188
189/* This extracts the draw arguments from the indirect resource,
190 * puts them into a new instance of pipe_draw_info, and calls draw_vbo on it.
191 */
192void
193util_draw_indirect(struct pipe_context *pipe,
194                   const struct pipe_draw_info *info_in,
195                   const struct pipe_draw_indirect_info *indirect)
196{
197   struct pipe_draw_info info;
198   struct pipe_transfer *transfer;
199   uint32_t *params;
200   unsigned num_params = info_in->index_size ? 5 : 4;
201
202   assert(indirect);
203   assert(!indirect->count_from_stream_output);
204
205   memcpy(&info, info_in, sizeof(info));
206
207   uint32_t draw_count = indirect->draw_count;
208
209   if (indirect->indirect_draw_count) {
210      struct pipe_transfer *dc_transfer;
211      uint32_t *dc_param = pipe_buffer_map_range(pipe,
212                                                 indirect->indirect_draw_count,
213                                                 indirect->indirect_draw_count_offset,
214                                                 4, PIPE_MAP_READ, &dc_transfer);
215      if (!dc_transfer) {
216         debug_printf("%s: failed to map indirect draw count buffer\n", __FUNCTION__);
217         return;
218      }
219      if (dc_param[0] < draw_count)
220         draw_count = dc_param[0];
221      pipe_buffer_unmap(pipe, dc_transfer);
222   }
223
224   if (indirect->stride)
225      num_params = MIN2(indirect->stride / 4, num_params);
226   params = (uint32_t *)
227      pipe_buffer_map_range(pipe,
228                            indirect->buffer,
229                            indirect->offset,
230                            (num_params * indirect->draw_count) * sizeof(uint32_t),
231                            PIPE_MAP_READ,
232                            &transfer);
233   if (!transfer) {
234      debug_printf("%s: failed to map indirect buffer\n", __FUNCTION__);
235      return;
236   }
237
238   for (unsigned i = 0; i < draw_count; i++) {
239      struct pipe_draw_start_count_bias draw;
240
241      draw.count = params[0];
242      info.instance_count = params[1];
243      draw.start = params[2];
244      draw.index_bias = info_in->index_size ? params[3] : 0;
245      info.start_instance = info_in->index_size ? params[4] : params[3];
246
247      pipe->draw_vbo(pipe, &info, i, NULL, &draw, 1);
248
249      params += indirect->stride / 4;
250   }
251   pipe_buffer_unmap(pipe, transfer);
252}
253
254void
255util_draw_multi(struct pipe_context *pctx, const struct pipe_draw_info *info,
256                unsigned drawid_offset,
257                const struct pipe_draw_indirect_info *indirect,
258                const struct pipe_draw_start_count_bias *draws,
259                unsigned num_draws)
260{
261   struct pipe_draw_info tmp_info = *info;
262   unsigned drawid = drawid_offset;
263
264   /* If you call this with num_draws==1, that is probably going to be
265    * an infinite loop
266    */
267   assert(num_draws > 1);
268
269   for (unsigned i = 0; i < num_draws; i++) {
270      if (indirect || (draws[i].count && info->instance_count))
271         pctx->draw_vbo(pctx, &tmp_info, drawid, indirect, &draws[i], 1);
272      if (tmp_info.increment_draw_id)
273         drawid++;
274   }
275}
276