8075ecbd9a39991d97d4e8bed7bd8a13921b92fa
[mesa.git] / src / mesa / state_tracker / st_atom_array.c
1
2 /**************************************************************************
3 *
4 * Copyright 2007 VMware, Inc.
5 * Copyright 2012 Marek Olšák <maraeo@gmail.com>
6 * All Rights Reserved.
7 *
8 * Permission is hereby granted, free of charge, to any person obtaining a
9 * copy of this software and associated documentation files (the
10 * "Software"), to deal in the Software without restriction, including
11 * without limitation the rights to use, copy, modify, merge, publish,
12 * distribute, sub license, and/or sell copies of the Software, and to
13 * permit persons to whom the Software is furnished to do so, subject to
14 * the following conditions:
15 *
16 * The above copyright notice and this permission notice (including the
17 * next paragraph) shall be included in all copies or substantial portions
18 * of the Software.
19 *
20 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
21 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
23 * IN NO EVENT SHALL AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR
24 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
25 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
26 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27 *
28 **************************************************************************/
29
30 /*
31 * This converts the VBO's vertex attribute/array information into
32 * Gallium vertex state and binds it.
33 *
34 * Authors:
35 * Keith Whitwell <keithw@vmware.com>
36 * Marek Olšák <maraeo@gmail.com>
37 */
38
39 #include "st_context.h"
40 #include "st_atom.h"
41 #include "st_cb_bufferobjects.h"
42 #include "st_draw.h"
43 #include "st_program.h"
44
45 #include "cso_cache/cso_context.h"
46 #include "util/u_math.h"
47 #include "util/u_upload_mgr.h"
48 #include "main/bufferobj.h"
49 #include "main/glformats.h"
50 #include "main/varray.h"
51 #include "main/arrayobj.h"
52
53 static void set_velement(struct pipe_vertex_element *velement,
54 int src_offset, int format,
55 int instance_divisor, int vbo_index)
56 {
57 velement->src_offset = src_offset;
58 velement->src_format = format;
59 velement->instance_divisor = instance_divisor;
60 velement->vertex_buffer_index = vbo_index;
61 assert(velement->src_format);
62 }
63
64 static void init_velement_64bit(const struct st_vertex_program *vp,
65 struct pipe_vertex_element *velements,
66 const struct gl_vertex_format *vformat,
67 int src_offset, int instance_divisor,
68 int vbo_index, int idx)
69 {
70 const GLubyte nr_components = vformat->Size;
71 int lower_format;
72
73 if (nr_components < 2)
74 lower_format = PIPE_FORMAT_R32G32_UINT;
75 else
76 lower_format = PIPE_FORMAT_R32G32B32A32_UINT;
77
78 set_velement(&velements[idx], src_offset,
79 lower_format, instance_divisor, vbo_index);
80 idx++;
81
82 if (idx < vp->num_inputs &&
83 vp->index_to_input[idx] == ST_DOUBLE_ATTRIB_PLACEHOLDER) {
84 if (nr_components >= 3) {
85 if (nr_components == 3)
86 lower_format = PIPE_FORMAT_R32G32_UINT;
87 else
88 lower_format = PIPE_FORMAT_R32G32B32A32_UINT;
89
90 set_velement(&velements[idx], src_offset + 4 * sizeof(float),
91 lower_format, instance_divisor, vbo_index);
92 } else {
93 /* The values here are undefined. Fill in some conservative
94 * dummy values.
95 */
96 set_velement(&velements[idx], src_offset, PIPE_FORMAT_R32G32_UINT,
97 instance_divisor, vbo_index);
98 }
99 }
100 }
101
102 /* Always inline the non-64bit element code, so that the compiler can see
103 * that velements is on the stack.
104 */
105 static void ALWAYS_INLINE
106 init_velement(const struct st_vertex_program *vp,
107 struct pipe_vertex_element *velements,
108 const struct gl_vertex_format *vformat,
109 int src_offset, int instance_divisor,
110 int vbo_index, int idx)
111 {
112 if (!vformat->Doubles) {
113 velements[idx].src_offset = src_offset;
114 velements[idx].src_format = vformat->_PipeFormat;
115 velements[idx].instance_divisor = instance_divisor;
116 velements[idx].vertex_buffer_index = vbo_index;
117 assert(velements[idx].src_format);
118 return;
119 }
120
121 init_velement_64bit(vp, velements, vformat, src_offset, instance_divisor,
122 vbo_index, idx);
123 }
124
125 /* ALWAYS_INLINE helps the compiler realize that most of the parameters are
126 * on the stack.
127 */
128 void ALWAYS_INLINE
129 st_setup_arrays(struct st_context *st,
130 const struct st_vertex_program *vp,
131 const struct st_common_variant *vp_variant,
132 struct pipe_vertex_element *velements,
133 struct pipe_vertex_buffer *vbuffer, unsigned *num_vbuffers,
134 bool *has_user_vertex_buffers)
135 {
136 struct gl_context *ctx = st->ctx;
137 const struct gl_vertex_array_object *vao = ctx->Array._DrawVAO;
138 const GLbitfield inputs_read = vp_variant->vert_attrib_mask;
139 const ubyte *input_to_index = vp->input_to_index;
140
141 /* Process attribute array data. */
142 GLbitfield mask = inputs_read & _mesa_draw_array_bits(ctx);
143 GLbitfield userbuf_attribs = inputs_read & _mesa_draw_user_array_bits(ctx);
144
145 *has_user_vertex_buffers = userbuf_attribs != 0;
146 st->draw_needs_minmax_index =
147 (userbuf_attribs & ~_mesa_draw_nonzero_divisor_bits(ctx)) != 0;
148
149 while (mask) {
150 /* The attribute index to start pulling a binding */
151 const gl_vert_attrib i = ffs(mask) - 1;
152 const struct gl_vertex_buffer_binding *const binding
153 = _mesa_draw_buffer_binding(vao, i);
154 const unsigned bufidx = (*num_vbuffers)++;
155
156 if (_mesa_is_bufferobj(binding->BufferObj)) {
157 /* Set the binding */
158 struct st_buffer_object *stobj = st_buffer_object(binding->BufferObj);
159
160 vbuffer[bufidx].buffer.resource = stobj ? stobj->buffer : NULL;
161 vbuffer[bufidx].is_user_buffer = false;
162 vbuffer[bufidx].buffer_offset = _mesa_draw_binding_offset(binding);
163 } else {
164 /* Set the binding */
165 const void *ptr = (const void *)_mesa_draw_binding_offset(binding);
166 vbuffer[bufidx].buffer.user = ptr;
167 vbuffer[bufidx].is_user_buffer = true;
168 vbuffer[bufidx].buffer_offset = 0;
169 }
170 vbuffer[bufidx].stride = binding->Stride; /* in bytes */
171
172 const GLbitfield boundmask = _mesa_draw_bound_attrib_bits(binding);
173 GLbitfield attrmask = mask & boundmask;
174 /* Mark the those attributes as processed */
175 mask &= ~boundmask;
176 /* We can assume that we have array for the binding */
177 assert(attrmask);
178 /* Walk attributes belonging to the binding */
179 do {
180 const gl_vert_attrib attr = u_bit_scan(&attrmask);
181 const struct gl_array_attributes *const attrib
182 = _mesa_draw_array_attrib(vao, attr);
183 const GLuint off = _mesa_draw_attributes_relative_offset(attrib);
184 init_velement(vp, velements, &attrib->Format, off,
185 binding->InstanceDivisor, bufidx,
186 input_to_index[attr]);
187 } while (attrmask);
188 }
189 }
190
191 /* ALWAYS_INLINE helps the compiler realize that most of the parameters are
192 * on the stack.
193 */
194 void ALWAYS_INLINE
195 st_setup_current(struct st_context *st,
196 const struct st_vertex_program *vp,
197 const struct st_common_variant *vp_variant,
198 struct pipe_vertex_element *velements,
199 struct pipe_vertex_buffer *vbuffer, unsigned *num_vbuffers)
200 {
201 struct gl_context *ctx = st->ctx;
202 const GLbitfield inputs_read = vp_variant->vert_attrib_mask;
203
204 /* Process values that should have better been uniforms in the application */
205 GLbitfield curmask = inputs_read & _mesa_draw_current_bits(ctx);
206 if (curmask) {
207 const ubyte *input_to_index = vp->input_to_index;
208 /* For each attribute, upload the maximum possible size. */
209 GLubyte data[VERT_ATTRIB_MAX * sizeof(GLdouble) * 4];
210 GLubyte *cursor = data;
211 const unsigned bufidx = (*num_vbuffers)++;
212 unsigned max_alignment = 1;
213
214 do {
215 const gl_vert_attrib attr = u_bit_scan(&curmask);
216 const struct gl_array_attributes *const attrib
217 = _mesa_draw_current_attrib(ctx, attr);
218 const unsigned size = attrib->Format._ElementSize;
219 const unsigned alignment = util_next_power_of_two(size);
220 max_alignment = MAX2(max_alignment, alignment);
221 memcpy(cursor, attrib->Ptr, size);
222 if (alignment != size)
223 memset(cursor + size, 0, alignment - size);
224
225 init_velement(vp, velements, &attrib->Format, cursor - data, 0,
226 bufidx, input_to_index[attr]);
227
228 cursor += alignment;
229 } while (curmask);
230
231 vbuffer[bufidx].is_user_buffer = false;
232 vbuffer[bufidx].buffer.resource = NULL;
233 /* vbuffer[bufidx].buffer_offset is set below */
234 vbuffer[bufidx].stride = 0;
235
236 /* Use const_uploader for zero-stride vertex attributes, because
237 * it may use a better memory placement than stream_uploader.
238 * The reason is that zero-stride attributes can be fetched many
239 * times (thousands of times), so a better placement is going to
240 * perform better.
241 */
242 struct u_upload_mgr *uploader = st->can_bind_const_buffer_as_vertex ?
243 st->pipe->const_uploader :
244 st->pipe->stream_uploader;
245 u_upload_data(uploader,
246 0, cursor - data, max_alignment, data,
247 &vbuffer[bufidx].buffer_offset,
248 &vbuffer[bufidx].buffer.resource);
249 /* Always unmap. The uploader might use explicit flushes. */
250 u_upload_unmap(uploader);
251 }
252 }
253
254 void
255 st_setup_current_user(struct st_context *st,
256 const struct st_vertex_program *vp,
257 const struct st_common_variant *vp_variant,
258 struct pipe_vertex_element *velements,
259 struct pipe_vertex_buffer *vbuffer, unsigned *num_vbuffers)
260 {
261 struct gl_context *ctx = st->ctx;
262 const GLbitfield inputs_read = vp_variant->vert_attrib_mask;
263 const ubyte *input_to_index = vp->input_to_index;
264
265 /* Process values that should have better been uniforms in the application */
266 GLbitfield curmask = inputs_read & _mesa_draw_current_bits(ctx);
267 /* For each attribute, make an own user buffer binding. */
268 while (curmask) {
269 const gl_vert_attrib attr = u_bit_scan(&curmask);
270 const struct gl_array_attributes *const attrib
271 = _mesa_draw_current_attrib(ctx, attr);
272 const unsigned bufidx = (*num_vbuffers)++;
273
274 init_velement(vp, velements, &attrib->Format, 0, 0,
275 bufidx, input_to_index[attr]);
276
277 vbuffer[bufidx].is_user_buffer = true;
278 vbuffer[bufidx].buffer.user = attrib->Ptr;
279 vbuffer[bufidx].buffer_offset = 0;
280 vbuffer[bufidx].stride = 0;
281 }
282 }
283
284 void
285 st_update_array(struct st_context *st)
286 {
287 /* vertex program validation must be done before this */
288 /* _NEW_PROGRAM, ST_NEW_VS_STATE */
289 const struct st_vertex_program *vp = (struct st_vertex_program *)st->vp;
290 const struct st_common_variant *vp_variant = st->vp_variant;
291
292 struct pipe_vertex_buffer vbuffer[PIPE_MAX_ATTRIBS];
293 unsigned num_vbuffers = 0, first_upload_vbuffer;
294 struct pipe_vertex_element velements[PIPE_MAX_ATTRIBS];
295 unsigned num_velements;
296 bool uses_user_vertex_buffers;
297
298 /* ST_NEW_VERTEX_ARRAYS alias ctx->DriverFlags.NewArray */
299 /* Setup arrays */
300 st_setup_arrays(st, vp, vp_variant, velements, vbuffer, &num_vbuffers,
301 &uses_user_vertex_buffers);
302
303 /* _NEW_CURRENT_ATTRIB */
304 /* Setup current uploads */
305 first_upload_vbuffer = num_vbuffers;
306 st_setup_current(st, vp, vp_variant, velements, vbuffer, &num_vbuffers);
307
308 /* Set the array into cso */
309 num_velements = vp->num_inputs + vp_variant->key.passthrough_edgeflags;
310
311 /* Set vertex buffers and elements. */
312 struct cso_context *cso = st->cso_context;
313 unsigned unbind_trailing_vbuffers =
314 st->last_num_vbuffers > num_vbuffers ?
315 st->last_num_vbuffers - num_vbuffers : 0;
316 cso_set_vertex_buffers_and_elements(cso, num_velements, velements,
317 num_vbuffers,
318 unbind_trailing_vbuffers,
319 vbuffer, uses_user_vertex_buffers);
320 st->last_num_vbuffers = num_vbuffers;
321
322 /* Unreference uploaded buffer resources. */
323 for (unsigned i = first_upload_vbuffer; i < num_vbuffers; ++i) {
324 pipe_resource_reference(&vbuffer[i].buffer.resource, NULL);
325 }
326 }