intel: emit is_indexed_draw in the same VE than gl_DrawID
[mesa.git] / src / mesa / drivers / dri / i965 / brw_draw_upload.c
1 /*
2 * Copyright 2003 VMware, Inc.
3 * All Rights Reserved.
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the
7 * "Software"), to deal in the Software without restriction, including
8 * without limitation the rights to use, copy, modify, merge, publish,
9 * distribute, sublicense, and/or sell copies of the Software, and to
10 * permit persons to whom the Software is furnished to do so, subject to
11 * the following conditions:
12 *
13 * The above copyright notice and this permission notice (including the
14 * next paragraph) shall be included in all copies or substantial portions
15 * of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
20 * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR
21 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
22 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
23 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 */
25
26 #include "main/bufferobj.h"
27 #include "main/context.h"
28 #include "main/enums.h"
29 #include "main/macros.h"
30 #include "main/glformats.h"
31
32 #include "brw_draw.h"
33 #include "brw_defines.h"
34 #include "brw_context.h"
35 #include "brw_state.h"
36
37 #include "intel_batchbuffer.h"
38 #include "intel_buffer_objects.h"
39
40 static const GLuint double_types_float[5] = {
41 0,
42 ISL_FORMAT_R64_FLOAT,
43 ISL_FORMAT_R64G64_FLOAT,
44 ISL_FORMAT_R64G64B64_FLOAT,
45 ISL_FORMAT_R64G64B64A64_FLOAT
46 };
47
48 static const GLuint double_types_passthru[5] = {
49 0,
50 ISL_FORMAT_R64_PASSTHRU,
51 ISL_FORMAT_R64G64_PASSTHRU,
52 ISL_FORMAT_R64G64B64_PASSTHRU,
53 ISL_FORMAT_R64G64B64A64_PASSTHRU
54 };
55
56 static const GLuint float_types[5] = {
57 0,
58 ISL_FORMAT_R32_FLOAT,
59 ISL_FORMAT_R32G32_FLOAT,
60 ISL_FORMAT_R32G32B32_FLOAT,
61 ISL_FORMAT_R32G32B32A32_FLOAT
62 };
63
64 static const GLuint half_float_types[5] = {
65 0,
66 ISL_FORMAT_R16_FLOAT,
67 ISL_FORMAT_R16G16_FLOAT,
68 ISL_FORMAT_R16G16B16_FLOAT,
69 ISL_FORMAT_R16G16B16A16_FLOAT
70 };
71
72 static const GLuint fixed_point_types[5] = {
73 0,
74 ISL_FORMAT_R32_SFIXED,
75 ISL_FORMAT_R32G32_SFIXED,
76 ISL_FORMAT_R32G32B32_SFIXED,
77 ISL_FORMAT_R32G32B32A32_SFIXED,
78 };
79
80 static const GLuint uint_types_direct[5] = {
81 0,
82 ISL_FORMAT_R32_UINT,
83 ISL_FORMAT_R32G32_UINT,
84 ISL_FORMAT_R32G32B32_UINT,
85 ISL_FORMAT_R32G32B32A32_UINT
86 };
87
88 static const GLuint uint_types_norm[5] = {
89 0,
90 ISL_FORMAT_R32_UNORM,
91 ISL_FORMAT_R32G32_UNORM,
92 ISL_FORMAT_R32G32B32_UNORM,
93 ISL_FORMAT_R32G32B32A32_UNORM
94 };
95
96 static const GLuint uint_types_scale[5] = {
97 0,
98 ISL_FORMAT_R32_USCALED,
99 ISL_FORMAT_R32G32_USCALED,
100 ISL_FORMAT_R32G32B32_USCALED,
101 ISL_FORMAT_R32G32B32A32_USCALED
102 };
103
104 static const GLuint int_types_direct[5] = {
105 0,
106 ISL_FORMAT_R32_SINT,
107 ISL_FORMAT_R32G32_SINT,
108 ISL_FORMAT_R32G32B32_SINT,
109 ISL_FORMAT_R32G32B32A32_SINT
110 };
111
112 static const GLuint int_types_norm[5] = {
113 0,
114 ISL_FORMAT_R32_SNORM,
115 ISL_FORMAT_R32G32_SNORM,
116 ISL_FORMAT_R32G32B32_SNORM,
117 ISL_FORMAT_R32G32B32A32_SNORM
118 };
119
120 static const GLuint int_types_scale[5] = {
121 0,
122 ISL_FORMAT_R32_SSCALED,
123 ISL_FORMAT_R32G32_SSCALED,
124 ISL_FORMAT_R32G32B32_SSCALED,
125 ISL_FORMAT_R32G32B32A32_SSCALED
126 };
127
128 static const GLuint ushort_types_direct[5] = {
129 0,
130 ISL_FORMAT_R16_UINT,
131 ISL_FORMAT_R16G16_UINT,
132 ISL_FORMAT_R16G16B16_UINT,
133 ISL_FORMAT_R16G16B16A16_UINT
134 };
135
136 static const GLuint ushort_types_norm[5] = {
137 0,
138 ISL_FORMAT_R16_UNORM,
139 ISL_FORMAT_R16G16_UNORM,
140 ISL_FORMAT_R16G16B16_UNORM,
141 ISL_FORMAT_R16G16B16A16_UNORM
142 };
143
144 static const GLuint ushort_types_scale[5] = {
145 0,
146 ISL_FORMAT_R16_USCALED,
147 ISL_FORMAT_R16G16_USCALED,
148 ISL_FORMAT_R16G16B16_USCALED,
149 ISL_FORMAT_R16G16B16A16_USCALED
150 };
151
152 static const GLuint short_types_direct[5] = {
153 0,
154 ISL_FORMAT_R16_SINT,
155 ISL_FORMAT_R16G16_SINT,
156 ISL_FORMAT_R16G16B16_SINT,
157 ISL_FORMAT_R16G16B16A16_SINT
158 };
159
160 static const GLuint short_types_norm[5] = {
161 0,
162 ISL_FORMAT_R16_SNORM,
163 ISL_FORMAT_R16G16_SNORM,
164 ISL_FORMAT_R16G16B16_SNORM,
165 ISL_FORMAT_R16G16B16A16_SNORM
166 };
167
168 static const GLuint short_types_scale[5] = {
169 0,
170 ISL_FORMAT_R16_SSCALED,
171 ISL_FORMAT_R16G16_SSCALED,
172 ISL_FORMAT_R16G16B16_SSCALED,
173 ISL_FORMAT_R16G16B16A16_SSCALED
174 };
175
176 static const GLuint ubyte_types_direct[5] = {
177 0,
178 ISL_FORMAT_R8_UINT,
179 ISL_FORMAT_R8G8_UINT,
180 ISL_FORMAT_R8G8B8_UINT,
181 ISL_FORMAT_R8G8B8A8_UINT
182 };
183
184 static const GLuint ubyte_types_norm[5] = {
185 0,
186 ISL_FORMAT_R8_UNORM,
187 ISL_FORMAT_R8G8_UNORM,
188 ISL_FORMAT_R8G8B8_UNORM,
189 ISL_FORMAT_R8G8B8A8_UNORM
190 };
191
192 static const GLuint ubyte_types_scale[5] = {
193 0,
194 ISL_FORMAT_R8_USCALED,
195 ISL_FORMAT_R8G8_USCALED,
196 ISL_FORMAT_R8G8B8_USCALED,
197 ISL_FORMAT_R8G8B8A8_USCALED
198 };
199
200 static const GLuint byte_types_direct[5] = {
201 0,
202 ISL_FORMAT_R8_SINT,
203 ISL_FORMAT_R8G8_SINT,
204 ISL_FORMAT_R8G8B8_SINT,
205 ISL_FORMAT_R8G8B8A8_SINT
206 };
207
208 static const GLuint byte_types_norm[5] = {
209 0,
210 ISL_FORMAT_R8_SNORM,
211 ISL_FORMAT_R8G8_SNORM,
212 ISL_FORMAT_R8G8B8_SNORM,
213 ISL_FORMAT_R8G8B8A8_SNORM
214 };
215
216 static const GLuint byte_types_scale[5] = {
217 0,
218 ISL_FORMAT_R8_SSCALED,
219 ISL_FORMAT_R8G8_SSCALED,
220 ISL_FORMAT_R8G8B8_SSCALED,
221 ISL_FORMAT_R8G8B8A8_SSCALED
222 };
223
224 static GLuint
225 double_types(int size, GLboolean doubles)
226 {
227 /* From the BDW PRM, Volume 2d, page 588 (VERTEX_ELEMENT_STATE):
228 * "When SourceElementFormat is set to one of the *64*_PASSTHRU formats,
229 * 64-bit components are stored in the URB without any conversion."
230 * Also included on BDW PRM, Volume 7, page 470, table "Source Element
231 * Formats Supported in VF Unit"
232 *
233 * Previous PRMs don't include those references, so for gen7 we can't use
234 * PASSTHRU formats directly. But in any case, we prefer to return passthru
235 * even in that case, because that reflects what we want to achieve, even
236 * if we would need to workaround on gen < 8.
237 */
238 return (doubles
239 ? double_types_passthru[size]
240 : double_types_float[size]);
241 }
242
243 /**
244 * Given vertex array type/size/format/normalized info, return
245 * the appopriate hardware surface type.
246 * Format will be GL_RGBA or possibly GL_BGRA for GLubyte[4] color arrays.
247 */
248 unsigned
249 brw_get_vertex_surface_type(struct brw_context *brw,
250 const struct gl_array_attributes *glattrib)
251 {
252 int size = glattrib->Size;
253 const struct gen_device_info *devinfo = &brw->screen->devinfo;
254 const bool is_ivybridge_or_older =
255 devinfo->gen <= 7 && !devinfo->is_baytrail && !devinfo->is_haswell;
256
257 if (unlikely(INTEL_DEBUG & DEBUG_VERTS))
258 fprintf(stderr, "type %s size %d normalized %d\n",
259 _mesa_enum_to_string(glattrib->Type),
260 glattrib->Size, glattrib->Normalized);
261
262 if (glattrib->Integer) {
263 assert(glattrib->Format == GL_RGBA); /* sanity check */
264 switch (glattrib->Type) {
265 case GL_INT: return int_types_direct[size];
266 case GL_SHORT:
267 if (is_ivybridge_or_older && size == 3)
268 return short_types_direct[4];
269 else
270 return short_types_direct[size];
271 case GL_BYTE:
272 if (is_ivybridge_or_older && size == 3)
273 return byte_types_direct[4];
274 else
275 return byte_types_direct[size];
276 case GL_UNSIGNED_INT: return uint_types_direct[size];
277 case GL_UNSIGNED_SHORT:
278 if (is_ivybridge_or_older && size == 3)
279 return ushort_types_direct[4];
280 else
281 return ushort_types_direct[size];
282 case GL_UNSIGNED_BYTE:
283 if (is_ivybridge_or_older && size == 3)
284 return ubyte_types_direct[4];
285 else
286 return ubyte_types_direct[size];
287 default: unreachable("not reached");
288 }
289 } else if (glattrib->Type == GL_UNSIGNED_INT_10F_11F_11F_REV) {
290 return ISL_FORMAT_R11G11B10_FLOAT;
291 } else if (glattrib->Normalized) {
292 switch (glattrib->Type) {
293 case GL_DOUBLE: return double_types(size, glattrib->Doubles);
294 case GL_FLOAT: return float_types[size];
295 case GL_HALF_FLOAT:
296 case GL_HALF_FLOAT_OES:
297 if (devinfo->gen < 6 && size == 3)
298 return half_float_types[4];
299 else
300 return half_float_types[size];
301 case GL_INT: return int_types_norm[size];
302 case GL_SHORT: return short_types_norm[size];
303 case GL_BYTE: return byte_types_norm[size];
304 case GL_UNSIGNED_INT: return uint_types_norm[size];
305 case GL_UNSIGNED_SHORT: return ushort_types_norm[size];
306 case GL_UNSIGNED_BYTE:
307 if (glattrib->Format == GL_BGRA) {
308 /* See GL_EXT_vertex_array_bgra */
309 assert(size == 4);
310 return ISL_FORMAT_B8G8R8A8_UNORM;
311 }
312 else {
313 return ubyte_types_norm[size];
314 }
315 case GL_FIXED:
316 if (devinfo->gen >= 8 || devinfo->is_haswell)
317 return fixed_point_types[size];
318
319 /* This produces GL_FIXED inputs as values between INT32_MIN and
320 * INT32_MAX, which will be scaled down by 1/65536 by the VS.
321 */
322 return int_types_scale[size];
323 /* See GL_ARB_vertex_type_2_10_10_10_rev.
324 * W/A: Pre-Haswell, the hardware doesn't really support the formats we'd
325 * like to use here, so upload everything as UINT and fix
326 * it in the shader
327 */
328 case GL_INT_2_10_10_10_REV:
329 assert(size == 4);
330 if (devinfo->gen >= 8 || devinfo->is_haswell) {
331 return glattrib->Format == GL_BGRA
332 ? ISL_FORMAT_B10G10R10A2_SNORM
333 : ISL_FORMAT_R10G10B10A2_SNORM;
334 }
335 return ISL_FORMAT_R10G10B10A2_UINT;
336 case GL_UNSIGNED_INT_2_10_10_10_REV:
337 assert(size == 4);
338 if (devinfo->gen >= 8 || devinfo->is_haswell) {
339 return glattrib->Format == GL_BGRA
340 ? ISL_FORMAT_B10G10R10A2_UNORM
341 : ISL_FORMAT_R10G10B10A2_UNORM;
342 }
343 return ISL_FORMAT_R10G10B10A2_UINT;
344 default: unreachable("not reached");
345 }
346 }
347 else {
348 /* See GL_ARB_vertex_type_2_10_10_10_rev.
349 * W/A: the hardware doesn't really support the formats we'd
350 * like to use here, so upload everything as UINT and fix
351 * it in the shader
352 */
353 if (glattrib->Type == GL_INT_2_10_10_10_REV) {
354 assert(size == 4);
355 if (devinfo->gen >= 8 || devinfo->is_haswell) {
356 return glattrib->Format == GL_BGRA
357 ? ISL_FORMAT_B10G10R10A2_SSCALED
358 : ISL_FORMAT_R10G10B10A2_SSCALED;
359 }
360 return ISL_FORMAT_R10G10B10A2_UINT;
361 } else if (glattrib->Type == GL_UNSIGNED_INT_2_10_10_10_REV) {
362 assert(size == 4);
363 if (devinfo->gen >= 8 || devinfo->is_haswell) {
364 return glattrib->Format == GL_BGRA
365 ? ISL_FORMAT_B10G10R10A2_USCALED
366 : ISL_FORMAT_R10G10B10A2_USCALED;
367 }
368 return ISL_FORMAT_R10G10B10A2_UINT;
369 }
370 assert(glattrib->Format == GL_RGBA); /* sanity check */
371 switch (glattrib->Type) {
372 case GL_DOUBLE: return double_types(size, glattrib->Doubles);
373 case GL_FLOAT: return float_types[size];
374 case GL_HALF_FLOAT:
375 case GL_HALF_FLOAT_OES:
376 if (devinfo->gen < 6 && size == 3)
377 return half_float_types[4];
378 else
379 return half_float_types[size];
380 case GL_INT: return int_types_scale[size];
381 case GL_SHORT: return short_types_scale[size];
382 case GL_BYTE: return byte_types_scale[size];
383 case GL_UNSIGNED_INT: return uint_types_scale[size];
384 case GL_UNSIGNED_SHORT: return ushort_types_scale[size];
385 case GL_UNSIGNED_BYTE: return ubyte_types_scale[size];
386 case GL_FIXED:
387 if (devinfo->gen >= 8 || devinfo->is_haswell)
388 return fixed_point_types[size];
389
390 /* This produces GL_FIXED inputs as values between INT32_MIN and
391 * INT32_MAX, which will be scaled down by 1/65536 by the VS.
392 */
393 return int_types_scale[size];
394 default: unreachable("not reached");
395 }
396 }
397 }
398
399 static void
400 copy_array_to_vbo_array(struct brw_context *brw,
401 struct brw_vertex_element *element,
402 int min, int max,
403 struct brw_vertex_buffer *buffer,
404 GLuint dst_stride)
405 {
406 const struct gl_vertex_array *glarray = element->glarray;
407 const struct gl_vertex_buffer_binding *glbinding = glarray->BufferBinding;
408 const struct gl_array_attributes *glattrib = glarray->VertexAttrib;
409 const int src_stride = glbinding->Stride;
410
411 /* If the source stride is zero, we just want to upload the current
412 * attribute once and set the buffer's stride to 0. There's no need
413 * to replicate it out.
414 */
415 if (src_stride == 0) {
416 brw_upload_data(&brw->upload, glattrib->Ptr, glattrib->_ElementSize,
417 glattrib->_ElementSize, &buffer->bo, &buffer->offset);
418
419 buffer->stride = 0;
420 buffer->size = glattrib->_ElementSize;
421 return;
422 }
423
424 const unsigned char *src = glattrib->Ptr + min * src_stride;
425 int count = max - min + 1;
426 GLuint size = count * dst_stride;
427 uint8_t *dst = brw_upload_space(&brw->upload, size, dst_stride,
428 &buffer->bo, &buffer->offset);
429
430 /* The GL 4.5 spec says:
431 * "If any enabled array’s buffer binding is zero when DrawArrays or
432 * one of the other drawing commands defined in section 10.4 is called,
433 * the result is undefined."
434 *
435 * In this case, let's the dst with undefined values
436 */
437 if (src != NULL) {
438 if (dst_stride == src_stride) {
439 memcpy(dst, src, size);
440 } else {
441 while (count--) {
442 memcpy(dst, src, dst_stride);
443 src += src_stride;
444 dst += dst_stride;
445 }
446 }
447 }
448 buffer->stride = dst_stride;
449 buffer->size = size;
450 }
451
452 void
453 brw_prepare_vertices(struct brw_context *brw)
454 {
455 const struct gen_device_info *devinfo = &brw->screen->devinfo;
456 struct gl_context *ctx = &brw->ctx;
457 /* BRW_NEW_VS_PROG_DATA */
458 const struct brw_vs_prog_data *vs_prog_data =
459 brw_vs_prog_data(brw->vs.base.prog_data);
460 GLbitfield64 vs_inputs = vs_prog_data->inputs_read;
461 const unsigned char *ptr = NULL;
462 GLuint interleaved = 0;
463 unsigned int min_index = brw->vb.min_index + brw->basevertex;
464 unsigned int max_index = brw->vb.max_index + brw->basevertex;
465 unsigned i;
466 int delta, j;
467
468 struct brw_vertex_element *upload[VERT_ATTRIB_MAX];
469 GLuint nr_uploads = 0;
470
471 /* _NEW_POLYGON
472 *
473 * On gen6+, edge flags don't end up in the VUE (either in or out of the
474 * VS). Instead, they're uploaded as the last vertex element, and the data
475 * is passed sideband through the fixed function units. So, we need to
476 * prepare the vertex buffer for it, but it's not present in inputs_read.
477 */
478 if (devinfo->gen >= 6 && (ctx->Polygon.FrontMode != GL_FILL ||
479 ctx->Polygon.BackMode != GL_FILL)) {
480 vs_inputs |= VERT_BIT_EDGEFLAG;
481 }
482
483 if (0)
484 fprintf(stderr, "%s %d..%d\n", __func__, min_index, max_index);
485
486 /* Accumulate the list of enabled arrays. */
487 brw->vb.nr_enabled = 0;
488 while (vs_inputs) {
489 GLuint first = ffsll(vs_inputs) - 1;
490 assert (first < 64);
491 GLuint index =
492 first - DIV_ROUND_UP(_mesa_bitcount_64(vs_prog_data->double_inputs_read &
493 BITFIELD64_MASK(first)), 2);
494 struct brw_vertex_element *input = &brw->vb.inputs[index];
495 input->is_dual_slot = (vs_prog_data->double_inputs_read & BITFIELD64_BIT(first)) != 0;
496 vs_inputs &= ~BITFIELD64_BIT(first);
497 if (input->is_dual_slot)
498 vs_inputs &= ~BITFIELD64_BIT(first + 1);
499 brw->vb.enabled[brw->vb.nr_enabled++] = input;
500 }
501
502 if (brw->vb.nr_enabled == 0)
503 return;
504
505 if (brw->vb.nr_buffers)
506 return;
507
508 /* The range of data in a given buffer represented as [min, max) */
509 struct intel_buffer_object *enabled_buffer[VERT_ATTRIB_MAX];
510 uint32_t buffer_range_start[VERT_ATTRIB_MAX];
511 uint32_t buffer_range_end[VERT_ATTRIB_MAX];
512
513 for (i = j = 0; i < brw->vb.nr_enabled; i++) {
514 struct brw_vertex_element *input = brw->vb.enabled[i];
515 const struct gl_vertex_array *glarray = input->glarray;
516 const struct gl_vertex_buffer_binding *glbinding = glarray->BufferBinding;
517 const struct gl_array_attributes *glattrib = glarray->VertexAttrib;
518
519 if (_mesa_is_bufferobj(glbinding->BufferObj)) {
520 struct intel_buffer_object *intel_buffer =
521 intel_buffer_object(glbinding->BufferObj);
522
523 const uint32_t offset = glbinding->Offset + glattrib->RelativeOffset;
524
525 /* Start with the worst case */
526 uint32_t start = 0;
527 uint32_t range = intel_buffer->Base.Size;
528 if (glbinding->InstanceDivisor) {
529 if (brw->num_instances) {
530 start = offset + glbinding->Stride * brw->baseinstance;
531 range = (glbinding->Stride * ((brw->num_instances - 1) /
532 glbinding->InstanceDivisor) +
533 glattrib->_ElementSize);
534 }
535 } else {
536 if (brw->vb.index_bounds_valid) {
537 start = offset + min_index * glbinding->Stride;
538 range = (glbinding->Stride * (max_index - min_index) +
539 glattrib->_ElementSize);
540 }
541 }
542
543 /* If we have a VB set to be uploaded for this buffer object
544 * already, reuse that VB state so that we emit fewer
545 * relocations.
546 */
547 unsigned k;
548 for (k = 0; k < i; k++) {
549 const struct gl_vertex_array *other = brw->vb.enabled[k]->glarray;
550 const struct gl_vertex_buffer_binding *obind = other->BufferBinding;
551 const struct gl_array_attributes *oattrib = other->VertexAttrib;
552 const uint32_t ooffset = obind->Offset + oattrib->RelativeOffset;
553 if (glbinding->BufferObj == obind->BufferObj &&
554 glbinding->Stride == obind->Stride &&
555 glbinding->InstanceDivisor == obind->InstanceDivisor &&
556 (offset - ooffset) < glbinding->Stride)
557 {
558 input->buffer = brw->vb.enabled[k]->buffer;
559 input->offset = offset - ooffset;
560
561 buffer_range_start[input->buffer] =
562 MIN2(buffer_range_start[input->buffer], start);
563 buffer_range_end[input->buffer] =
564 MAX2(buffer_range_end[input->buffer], start + range);
565 break;
566 }
567 }
568 if (k == i) {
569 struct brw_vertex_buffer *buffer = &brw->vb.buffers[j];
570
571 /* Named buffer object: Just reference its contents directly. */
572 buffer->offset = offset;
573 buffer->stride = glbinding->Stride;
574 buffer->step_rate = glbinding->InstanceDivisor;
575 buffer->size = glbinding->BufferObj->Size - offset;
576
577 enabled_buffer[j] = intel_buffer;
578 buffer_range_start[j] = start;
579 buffer_range_end[j] = start + range;
580
581 input->buffer = j++;
582 input->offset = 0;
583 }
584 } else {
585 /* Queue the buffer object up to be uploaded in the next pass,
586 * when we've decided if we're doing interleaved or not.
587 */
588 if (nr_uploads == 0) {
589 interleaved = glbinding->Stride;
590 ptr = glattrib->Ptr;
591 }
592 else if (interleaved != glbinding->Stride ||
593 glbinding->InstanceDivisor != 0 ||
594 glattrib->Ptr < ptr ||
595 (uintptr_t)(glattrib->Ptr - ptr) + glattrib->_ElementSize > interleaved)
596 {
597 /* If our stride is different from the first attribute's stride,
598 * or if we are using an instance divisor or if the first
599 * attribute's stride didn't cover our element, disable the
600 * interleaved upload optimization. The second case can most
601 * commonly occur in cases where there is a single vertex and, for
602 * example, the data is stored on the application's stack.
603 *
604 * NOTE: This will also disable the optimization in cases where
605 * the data is in a different order than the array indices.
606 * Something like:
607 *
608 * float data[...];
609 * glVertexAttribPointer(0, 4, GL_FLOAT, 32, &data[4]);
610 * glVertexAttribPointer(1, 4, GL_FLOAT, 32, &data[0]);
611 */
612 interleaved = 0;
613 }
614
615 upload[nr_uploads++] = input;
616 }
617 }
618
619 /* Now that we've set up all of the buffers, we walk through and reference
620 * each of them. We do this late so that we get the right size in each
621 * buffer and don't reference too little data.
622 */
623 for (i = 0; i < j; i++) {
624 struct brw_vertex_buffer *buffer = &brw->vb.buffers[i];
625 if (buffer->bo)
626 continue;
627
628 const uint32_t start = buffer_range_start[i];
629 const uint32_t range = buffer_range_end[i] - buffer_range_start[i];
630
631 buffer->bo = intel_bufferobj_buffer(brw, enabled_buffer[i], start,
632 range, false);
633 brw_bo_reference(buffer->bo);
634 }
635
636 /* If we need to upload all the arrays, then we can trim those arrays to
637 * only the used elements [min_index, max_index] so long as we adjust all
638 * the values used in the 3DPRIMITIVE i.e. by setting the vertex bias.
639 */
640 brw->vb.start_vertex_bias = 0;
641 delta = min_index;
642 if (nr_uploads == brw->vb.nr_enabled) {
643 brw->vb.start_vertex_bias = -delta;
644 delta = 0;
645 }
646
647 /* Handle any arrays to be uploaded. */
648 if (nr_uploads > 1) {
649 if (interleaved) {
650 struct brw_vertex_buffer *buffer = &brw->vb.buffers[j];
651 /* All uploads are interleaved, so upload the arrays together as
652 * interleaved. First, upload the contents and set up upload[0].
653 */
654 copy_array_to_vbo_array(brw, upload[0], min_index, max_index,
655 buffer, interleaved);
656 buffer->offset -= delta * interleaved;
657 buffer->size += delta * interleaved;
658 buffer->step_rate = 0;
659
660 for (i = 0; i < nr_uploads; i++) {
661 const struct gl_vertex_array *glarray = upload[i]->glarray;
662 const struct gl_array_attributes *glattrib = glarray->VertexAttrib;
663 /* Then, just point upload[i] at upload[0]'s buffer. */
664 upload[i]->offset = ((const unsigned char *)glattrib->Ptr - ptr);
665 upload[i]->buffer = j;
666 }
667 j++;
668
669 nr_uploads = 0;
670 }
671 }
672 /* Upload non-interleaved arrays */
673 for (i = 0; i < nr_uploads; i++) {
674 struct brw_vertex_buffer *buffer = &brw->vb.buffers[j];
675 const struct gl_vertex_array *glarray = upload[i]->glarray;
676 const struct gl_vertex_buffer_binding *glbinding = glarray->BufferBinding;
677 const struct gl_array_attributes *glattrib = glarray->VertexAttrib;
678 if (glbinding->InstanceDivisor == 0) {
679 copy_array_to_vbo_array(brw, upload[i], min_index, max_index,
680 buffer, glattrib->_ElementSize);
681 } else {
682 /* This is an instanced attribute, since its InstanceDivisor
683 * is not zero. Therefore, its data will be stepped after the
684 * instanced draw has been run InstanceDivisor times.
685 */
686 uint32_t instanced_attr_max_index =
687 (brw->num_instances - 1) / glbinding->InstanceDivisor;
688 copy_array_to_vbo_array(brw, upload[i], 0, instanced_attr_max_index,
689 buffer, glattrib->_ElementSize);
690 }
691 buffer->offset -= delta * buffer->stride;
692 buffer->size += delta * buffer->stride;
693 buffer->step_rate = glbinding->InstanceDivisor;
694 upload[i]->buffer = j++;
695 upload[i]->offset = 0;
696 }
697
698 brw->vb.nr_buffers = j;
699 }
700
701 void
702 brw_prepare_shader_draw_parameters(struct brw_context *brw)
703 {
704 const struct brw_vs_prog_data *vs_prog_data =
705 brw_vs_prog_data(brw->vs.base.prog_data);
706
707 const bool uses_firstvertex =
708 vs_prog_data->uses_basevertex || vs_prog_data->uses_firstvertex;
709
710 /* For non-indirect draws, upload the shader draw parameters */
711 if ((uses_firstvertex || vs_prog_data->uses_baseinstance) &&
712 brw->draw.draw_params_bo == NULL) {
713 brw_upload_data(&brw->upload,
714 &brw->draw.params, sizeof(brw->draw.params), 4,
715 &brw->draw.draw_params_bo,
716 &brw->draw.draw_params_offset);
717 }
718
719 if (vs_prog_data->uses_drawid || vs_prog_data->uses_is_indexed_draw) {
720 brw_upload_data(&brw->upload,
721 &brw->draw.derived_params, sizeof(brw->draw.derived_params), 4,
722 &brw->draw.derived_draw_params_bo,
723 &brw->draw.derived_draw_params_offset);
724 }
725 }
726
727 static void
728 brw_upload_indices(struct brw_context *brw)
729 {
730 const struct _mesa_index_buffer *index_buffer = brw->ib.ib;
731 GLuint ib_size;
732 struct brw_bo *old_bo = brw->ib.bo;
733 struct gl_buffer_object *bufferobj;
734 GLuint offset;
735 GLuint ib_type_size;
736
737 if (index_buffer == NULL)
738 return;
739
740 ib_type_size = index_buffer->index_size;
741 ib_size = index_buffer->count ? ib_type_size * index_buffer->count :
742 index_buffer->obj->Size;
743 bufferobj = index_buffer->obj;
744
745 /* Turn into a proper VBO:
746 */
747 if (!_mesa_is_bufferobj(bufferobj)) {
748 /* Get new bufferobj, offset:
749 */
750 brw_upload_data(&brw->upload, index_buffer->ptr, ib_size, ib_type_size,
751 &brw->ib.bo, &offset);
752 brw->ib.size = brw->ib.bo->size;
753 } else {
754 offset = (GLuint) (unsigned long) index_buffer->ptr;
755
756 struct brw_bo *bo =
757 intel_bufferobj_buffer(brw, intel_buffer_object(bufferobj),
758 offset, ib_size, false);
759 if (bo != brw->ib.bo) {
760 brw_bo_unreference(brw->ib.bo);
761 brw->ib.bo = bo;
762 brw->ib.size = bufferobj->Size;
763 brw_bo_reference(bo);
764 }
765 }
766
767 /* Use 3DPRIMITIVE's start_vertex_offset to avoid re-uploading
768 * the index buffer state when we're just moving the start index
769 * of our drawing.
770 */
771 brw->ib.start_vertex_offset = offset / ib_type_size;
772
773 if (brw->ib.bo != old_bo)
774 brw->ctx.NewDriverState |= BRW_NEW_INDEX_BUFFER;
775
776 if (index_buffer->index_size != brw->ib.index_size) {
777 brw->ib.index_size = index_buffer->index_size;
778 brw->ctx.NewDriverState |= BRW_NEW_INDEX_BUFFER;
779 }
780 }
781
782 const struct brw_tracked_state brw_indices = {
783 .dirty = {
784 .mesa = 0,
785 .brw = BRW_NEW_BLORP |
786 BRW_NEW_INDICES,
787 },
788 .emit = brw_upload_indices,
789 };