1 /**************************************************************************
3 * Copyright 2003 Tungsten Graphics, Inc., Cedar Park, Texas.
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:
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
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 TUNGSTEN GRAPHICS 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.
26 **************************************************************************/
33 #include "api_validate.h"
37 #include "brw_defines.h"
38 #include "brw_context.h"
39 #include "brw_state.h"
40 #include "brw_fallback.h"
42 #include "intel_ioctl.h"
43 #include "intel_batchbuffer.h"
44 #include "intel_buffer_objects.h"
45 #include "intel_tex.h"
47 struct brw_array_state {
48 union header_union header;
65 GLuint instance_data_step_rate;
71 static dri_bo *array_buffer( struct intel_context *intel,
72 const struct gl_client_array *array )
74 return intel_bufferobj_buffer(intel, intel_buffer_object(array->BufferObj),
78 static GLuint double_types[5] = {
80 BRW_SURFACEFORMAT_R64_FLOAT,
81 BRW_SURFACEFORMAT_R64G64_FLOAT,
82 BRW_SURFACEFORMAT_R64G64B64_FLOAT,
83 BRW_SURFACEFORMAT_R64G64B64A64_FLOAT
86 static GLuint float_types[5] = {
88 BRW_SURFACEFORMAT_R32_FLOAT,
89 BRW_SURFACEFORMAT_R32G32_FLOAT,
90 BRW_SURFACEFORMAT_R32G32B32_FLOAT,
91 BRW_SURFACEFORMAT_R32G32B32A32_FLOAT
94 static GLuint uint_types_norm[5] = {
96 BRW_SURFACEFORMAT_R32_UNORM,
97 BRW_SURFACEFORMAT_R32G32_UNORM,
98 BRW_SURFACEFORMAT_R32G32B32_UNORM,
99 BRW_SURFACEFORMAT_R32G32B32A32_UNORM
102 static GLuint uint_types_scale[5] = {
104 BRW_SURFACEFORMAT_R32_USCALED,
105 BRW_SURFACEFORMAT_R32G32_USCALED,
106 BRW_SURFACEFORMAT_R32G32B32_USCALED,
107 BRW_SURFACEFORMAT_R32G32B32A32_USCALED
110 static GLuint int_types_norm[5] = {
112 BRW_SURFACEFORMAT_R32_SNORM,
113 BRW_SURFACEFORMAT_R32G32_SNORM,
114 BRW_SURFACEFORMAT_R32G32B32_SNORM,
115 BRW_SURFACEFORMAT_R32G32B32A32_SNORM
118 static GLuint int_types_scale[5] = {
120 BRW_SURFACEFORMAT_R32_SSCALED,
121 BRW_SURFACEFORMAT_R32G32_SSCALED,
122 BRW_SURFACEFORMAT_R32G32B32_SSCALED,
123 BRW_SURFACEFORMAT_R32G32B32A32_SSCALED
126 static GLuint ushort_types_norm[5] = {
128 BRW_SURFACEFORMAT_R16_UNORM,
129 BRW_SURFACEFORMAT_R16G16_UNORM,
130 BRW_SURFACEFORMAT_R16G16B16_UNORM,
131 BRW_SURFACEFORMAT_R16G16B16A16_UNORM
134 static GLuint ushort_types_scale[5] = {
136 BRW_SURFACEFORMAT_R16_USCALED,
137 BRW_SURFACEFORMAT_R16G16_USCALED,
138 BRW_SURFACEFORMAT_R16G16B16_USCALED,
139 BRW_SURFACEFORMAT_R16G16B16A16_USCALED
142 static GLuint short_types_norm[5] = {
144 BRW_SURFACEFORMAT_R16_SNORM,
145 BRW_SURFACEFORMAT_R16G16_SNORM,
146 BRW_SURFACEFORMAT_R16G16B16_SNORM,
147 BRW_SURFACEFORMAT_R16G16B16A16_SNORM
150 static GLuint short_types_scale[5] = {
152 BRW_SURFACEFORMAT_R16_SSCALED,
153 BRW_SURFACEFORMAT_R16G16_SSCALED,
154 BRW_SURFACEFORMAT_R16G16B16_SSCALED,
155 BRW_SURFACEFORMAT_R16G16B16A16_SSCALED
158 static GLuint ubyte_types_norm[5] = {
160 BRW_SURFACEFORMAT_R8_UNORM,
161 BRW_SURFACEFORMAT_R8G8_UNORM,
162 BRW_SURFACEFORMAT_R8G8B8_UNORM,
163 BRW_SURFACEFORMAT_R8G8B8A8_UNORM
166 static GLuint ubyte_types_scale[5] = {
168 BRW_SURFACEFORMAT_R8_USCALED,
169 BRW_SURFACEFORMAT_R8G8_USCALED,
170 BRW_SURFACEFORMAT_R8G8B8_USCALED,
171 BRW_SURFACEFORMAT_R8G8B8A8_USCALED
174 static GLuint byte_types_norm[5] = {
176 BRW_SURFACEFORMAT_R8_SNORM,
177 BRW_SURFACEFORMAT_R8G8_SNORM,
178 BRW_SURFACEFORMAT_R8G8B8_SNORM,
179 BRW_SURFACEFORMAT_R8G8B8A8_SNORM
182 static GLuint byte_types_scale[5] = {
184 BRW_SURFACEFORMAT_R8_SSCALED,
185 BRW_SURFACEFORMAT_R8G8_SSCALED,
186 BRW_SURFACEFORMAT_R8G8B8_SSCALED,
187 BRW_SURFACEFORMAT_R8G8B8A8_SSCALED
191 static GLuint get_surface_type( GLenum type, GLuint size, GLboolean normalized )
193 if (INTEL_DEBUG & DEBUG_VERTS)
194 _mesa_printf("type %s size %d normalized %d\n",
195 _mesa_lookup_enum_by_nr(type), size, normalized);
199 case GL_DOUBLE: return double_types[size];
200 case GL_FLOAT: return float_types[size];
201 case GL_INT: return int_types_norm[size];
202 case GL_SHORT: return short_types_norm[size];
203 case GL_BYTE: return byte_types_norm[size];
204 case GL_UNSIGNED_INT: return uint_types_norm[size];
205 case GL_UNSIGNED_SHORT: return ushort_types_norm[size];
206 case GL_UNSIGNED_BYTE: return ubyte_types_norm[size];
207 default: assert(0); return 0;
212 case GL_DOUBLE: return double_types[size];
213 case GL_FLOAT: return float_types[size];
214 case GL_INT: return int_types_scale[size];
215 case GL_SHORT: return short_types_scale[size];
216 case GL_BYTE: return byte_types_scale[size];
217 case GL_UNSIGNED_INT: return uint_types_scale[size];
218 case GL_UNSIGNED_SHORT: return ushort_types_scale[size];
219 case GL_UNSIGNED_BYTE: return ubyte_types_scale[size];
220 default: assert(0); return 0;
226 static GLuint get_size( GLenum type )
229 case GL_DOUBLE: return sizeof(GLdouble);
230 case GL_FLOAT: return sizeof(GLfloat);
231 case GL_INT: return sizeof(GLint);
232 case GL_SHORT: return sizeof(GLshort);
233 case GL_BYTE: return sizeof(GLbyte);
234 case GL_UNSIGNED_INT: return sizeof(GLuint);
235 case GL_UNSIGNED_SHORT: return sizeof(GLushort);
236 case GL_UNSIGNED_BYTE: return sizeof(GLubyte);
241 static GLuint get_index_type(GLenum type)
244 case GL_UNSIGNED_BYTE: return BRW_INDEX_BYTE;
245 case GL_UNSIGNED_SHORT: return BRW_INDEX_WORD;
246 case GL_UNSIGNED_INT: return BRW_INDEX_DWORD;
247 default: assert(0); return 0;
251 static void copy_strided_array( GLubyte *dest,
258 memcpy(dest, src, count * size);
262 for (i = 0; i < count; i++) {
263 memcpy(dest, src, size);
270 static void wrap_buffers( struct brw_context *brw,
273 GLcontext *ctx = &brw->intel.ctx;
275 if (size < BRW_UPLOAD_INIT_SIZE)
276 size = BRW_UPLOAD_INIT_SIZE;
278 brw->vb.upload.buf++;
279 brw->vb.upload.buf %= BRW_NR_UPLOAD_BUFS;
280 brw->vb.upload.offset = 0;
282 ctx->Driver.BufferData(ctx,
287 brw->vb.upload.vbo[brw->vb.upload.buf]);
290 static void get_space( struct brw_context *brw,
292 struct gl_buffer_object **vbo_return,
293 GLuint *offset_return )
295 size = ALIGN(size, 64);
297 if (brw->vb.upload.offset + size > BRW_UPLOAD_INIT_SIZE)
298 wrap_buffers(brw, size);
300 *vbo_return = brw->vb.upload.vbo[brw->vb.upload.buf];
301 *offset_return = brw->vb.upload.offset;
303 brw->vb.upload.offset += size;
307 copy_array_to_vbo_array( struct brw_context *brw,
308 struct gl_client_array *vbo_array,
309 const struct gl_client_array *array,
313 GLcontext *ctx = &brw->intel.ctx;
314 GLuint size = count * element_size;
315 struct gl_buffer_object *vbo;
319 get_space(brw, size, &vbo, &offset);
321 if (array->StrideB == 0) {
326 new_stride = element_size;
328 vbo_array->Size = array->Size;
329 vbo_array->Type = array->Type;
330 vbo_array->Stride = new_stride;
331 vbo_array->StrideB = new_stride;
332 vbo_array->Ptr = (const void *)offset;
333 vbo_array->Enabled = 1;
334 vbo_array->Normalized = array->Normalized;
335 vbo_array->_MaxElement = array->_MaxElement; /* ? */
336 vbo_array->BufferObj = vbo;
339 GLubyte *map = ctx->Driver.MapBuffer(ctx,
346 copy_strided_array( map,
352 ctx->Driver.UnmapBuffer(ctx, GL_ARRAY_BUFFER_ARB, vbo_array->BufferObj);
357 * Just a wrapper to highlight which cause of copy_array_to_vbo_array
358 * is happening in the profile.
361 interleaved_copy_array_to_vbo_array(struct brw_context *brw,
362 struct gl_client_array *vbo_array,
363 const struct gl_client_array *array,
367 copy_array_to_vbo_array(brw, vbo_array, array, element_size, count);
371 interleaved_vbo_array( struct brw_context *brw,
372 struct gl_client_array *vbo_array,
373 const struct gl_client_array *uploaded_array,
374 const struct gl_client_array *array,
377 vbo_array->Size = array->Size;
378 vbo_array->Type = array->Type;
379 vbo_array->Stride = array->Stride;
380 vbo_array->StrideB = array->StrideB;
381 vbo_array->Ptr = (const void *)((const char *)uploaded_array->Ptr +
382 ((const char *)array->Ptr - ptr));
383 vbo_array->Enabled = 1;
384 vbo_array->Normalized = array->Normalized;
385 vbo_array->_MaxElement = array->_MaxElement;
386 vbo_array->BufferObj = uploaded_array->BufferObj;
390 GLboolean brw_upload_vertices( struct brw_context *brw,
394 GLcontext *ctx = &brw->intel.ctx;
395 struct intel_context *intel = intel_context(ctx);
396 GLuint tmp = brw->vs.prog_data->inputs_read;
398 const void *ptr = NULL;
399 GLuint interleave = 0;
400 struct gl_client_array vbo_array_temp[VERT_ATTRIB_MAX];
402 struct brw_vertex_element *enabled[VERT_ATTRIB_MAX];
403 GLuint nr_enabled = 0;
405 struct brw_vertex_element *upload[VERT_ATTRIB_MAX];
406 GLuint nr_uploads = 0;
408 /* First build an array of pointers to ve's in vb.inputs_read
411 _mesa_printf("%s %d..%d\n", __FUNCTION__, min_index, max_index);
414 GLuint i = _mesa_ffsll(tmp)-1;
415 struct brw_vertex_element *input = &brw->vb.inputs[i];
418 enabled[nr_enabled++] = input;
420 input->element_size = get_size(input->glarray->Type) * input->glarray->Size;
421 input->count = input->glarray->StrideB ? max_index + 1 - min_index : 1;
423 if (!input->glarray->BufferObj->Name) {
425 /* Position array not properly enabled:
427 if (input->glarray->StrideB == 0)
430 interleave = input->glarray->StrideB;
431 ptr = input->glarray->Ptr;
433 else if (interleave != input->glarray->StrideB ||
434 (const char *)input->glarray->Ptr - (const char *)ptr < 0 ||
435 (const char *)input->glarray->Ptr - (const char *)ptr > interleave) {
439 upload[nr_uploads++] = input;
441 /* We rebase drawing to start at element zero only when
442 * varyings are not in vbos, which means we can end up
443 * uploading non-varying arrays (stride != 0) when min_index
444 * is zero. This doesn't matter as the amount to upload is
445 * the same for these arrays whether the draw call is rebased
446 * or not - we just have to upload the one element.
448 assert(min_index == 0 || input->glarray->StrideB == 0);
452 /* Upload interleaved arrays if all uploads are interleaved
454 if (nr_uploads > 1 && interleave && interleave <= 256) {
455 interleaved_copy_array_to_vbo_array(brw, &vbo_array_temp[0],
459 upload[0]->glarray = &vbo_array_temp[0];
461 for (i = 1; i < nr_uploads; i++) {
462 interleaved_vbo_array(brw,
467 upload[i]->glarray = &vbo_array_temp[i];
471 for (i = 0; i < nr_uploads; i++) {
472 copy_array_to_vbo_array(brw, &vbo_array_temp[i],
474 upload[i]->element_size,
476 upload[i]->glarray = &vbo_array_temp[i];
480 /* XXX: In the rare cases where this happens we fallback all
481 * the way to software rasterization, although a tnl fallback
482 * would be sufficient. I don't know of *any* real world
483 * cases with > 17 vertex attributes enabled, so it probably
484 * isn't an issue at this point.
486 if (nr_enabled >= BRW_VEP_MAX)
489 /* Now emit VB and VEP state packets.
491 * This still defines a hardware VB for each input, even if they
492 * are interleaved or from the same VBO. TBD if this makes a
493 * performance difference.
495 BEGIN_BATCH(1 + nr_enabled * 4, IGNORE_CLIPRECTS);
496 OUT_BATCH((CMD_VERTEX_BUFFER << 16) |
497 ((1 + nr_enabled * 4) - 2));
499 for (i = 0; i < nr_enabled; i++) {
500 struct brw_vertex_element *input = enabled[i];
502 OUT_BATCH((i << BRW_VB0_INDEX_SHIFT) |
503 BRW_VB0_ACCESS_VERTEXDATA |
504 (input->glarray->StrideB << BRW_VB0_PITCH_SHIFT));
505 OUT_RELOC(array_buffer(intel, input->glarray),
506 DRM_BO_FLAG_MEM_TT | DRM_BO_FLAG_READ,
507 (GLuint)input->glarray->Ptr);
508 OUT_BATCH(max_index);
509 OUT_BATCH(0); /* Instance data step rate */
513 BEGIN_BATCH(1 + nr_enabled * 2, IGNORE_CLIPRECTS);
514 OUT_BATCH((CMD_VERTEX_ELEMENT << 16) | ((1 + nr_enabled * 2) - 2));
515 for (i = 0; i < nr_enabled; i++) {
516 struct brw_vertex_element *input = enabled[i];
517 uint32_t format = get_surface_type(input->glarray->Type,
518 input->glarray->Size,
519 input->glarray->Normalized);
520 uint32_t comp0 = BRW_VE1_COMPONENT_STORE_SRC;
521 uint32_t comp1 = BRW_VE1_COMPONENT_STORE_SRC;
522 uint32_t comp2 = BRW_VE1_COMPONENT_STORE_SRC;
523 uint32_t comp3 = BRW_VE1_COMPONENT_STORE_SRC;
525 switch (input->glarray->Size) {
526 case 0: comp0 = BRW_VE1_COMPONENT_STORE_0;
527 case 1: comp1 = BRW_VE1_COMPONENT_STORE_0;
528 case 2: comp2 = BRW_VE1_COMPONENT_STORE_0;
529 case 3: comp3 = BRW_VE1_COMPONENT_STORE_1_FLT;
533 OUT_BATCH((i << BRW_VE0_INDEX_SHIFT) |
535 (format << BRW_VE0_FORMAT_SHIFT) |
536 (0 << BRW_VE0_SRC_OFFSET_SHIFT));
537 OUT_BATCH((comp0 << BRW_VE1_COMPONENT_0_SHIFT) |
538 (comp1 << BRW_VE1_COMPONENT_1_SHIFT) |
539 (comp2 << BRW_VE1_COMPONENT_2_SHIFT) |
540 (comp3 << BRW_VE1_COMPONENT_3_SHIFT) |
541 ((i * 4) << BRW_VE1_DST_OFFSET_SHIFT));
548 void brw_upload_indices( struct brw_context *brw,
549 const struct _mesa_index_buffer *index_buffer )
551 GLcontext *ctx = &brw->intel.ctx;
552 struct intel_context *intel = &brw->intel;
553 GLuint ib_size = get_size(index_buffer->type) * index_buffer->count;
554 struct gl_buffer_object *bufferobj = index_buffer->obj;
555 GLuint offset = (GLuint)index_buffer->ptr;
557 /* Turn into a proper VBO:
559 if (!bufferobj->Name) {
561 /* Get new bufferobj, offset:
563 get_space(brw, ib_size, &bufferobj, &offset);
567 ctx->Driver.BufferSubData( ctx,
568 GL_ELEMENT_ARRAY_BUFFER_ARB,
574 /* If the index buffer isn't aligned to its element size, we have to
575 * rebase it into a temporary.
577 if ((get_size(index_buffer->type) - 1) & offset) {
578 struct gl_buffer_object *vbo;
580 GLubyte *map = ctx->Driver.MapBuffer(ctx,
581 GL_ELEMENT_ARRAY_BUFFER_ARB,
585 get_space(brw, ib_size, &vbo, &voffset);
587 ctx->Driver.BufferSubData(ctx,
588 GL_ELEMENT_ARRAY_BUFFER_ARB,
593 ctx->Driver.UnmapBuffer(ctx, GL_ELEMENT_ARRAY_BUFFER_ARB, bufferobj);
600 /* Emit the indexbuffer packet:
603 struct brw_indexbuffer ib;
604 dri_bo *buffer = intel_bufferobj_buffer(intel,
605 intel_buffer_object(bufferobj),
608 memset(&ib, 0, sizeof(ib));
610 ib.header.bits.opcode = CMD_INDEX_BUFFER;
611 ib.header.bits.length = sizeof(ib)/4 - 2;
612 ib.header.bits.index_format = get_index_type(index_buffer->type);
613 ib.header.bits.cut_index_enable = 0;
616 BEGIN_BATCH(4, IGNORE_CLIPRECTS);
617 OUT_BATCH( ib.header.dword );
618 OUT_RELOC( buffer, DRM_BO_FLAG_MEM_TT | DRM_BO_FLAG_READ, offset);
619 OUT_RELOC( buffer, DRM_BO_FLAG_MEM_TT | DRM_BO_FLAG_READ,