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/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 the state tracker 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
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.
131 */
132 void
util_draw_indirect(struct pipe_context * pipe,const struct pipe_draw_info * info_in)133 util_draw_indirect(struct pipe_context *pipe,
134 const struct pipe_draw_info *info_in)
135 {
136 struct pipe_draw_info info;
137 struct pipe_transfer *transfer;
138 uint32_t *params;
139 const unsigned num_params = info_in->index_size ? 5 : 4;
140
141 assert(info_in->indirect);
142 assert(!info_in->count_from_stream_output);
143
144 memcpy(&info, info_in, sizeof(info));
145
146 params = (uint32_t *)
147 pipe_buffer_map_range(pipe,
148 info_in->indirect->buffer,
149 info_in->indirect->offset,
150 num_params * sizeof(uint32_t),
151 PIPE_TRANSFER_READ,
152 &transfer);
153 if (!transfer) {
154 debug_printf("%s: failed to map indirect buffer\n", __FUNCTION__);
155 return;
156 }
157
158 info.count = params[0];
159 info.instance_count = params[1];
160 info.start = params[2];
161 info.index_bias = info_in->index_size ? params[3] : 0;
162 info.start_instance = info_in->index_size ? params[4] : params[3];
163 info.indirect = NULL;
164
165 pipe_buffer_unmap(pipe, transfer);
166
167 pipe->draw_vbo(pipe, &info);
168 }
169