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 static dri_bo *array_buffer( struct intel_context *intel,
48 const struct gl_client_array *array )
50 return intel_bufferobj_buffer(intel, intel_buffer_object(array->BufferObj),
54 static GLuint double_types[5] = {
56 BRW_SURFACEFORMAT_R64_FLOAT,
57 BRW_SURFACEFORMAT_R64G64_FLOAT,
58 BRW_SURFACEFORMAT_R64G64B64_FLOAT,
59 BRW_SURFACEFORMAT_R64G64B64A64_FLOAT
62 static GLuint float_types[5] = {
64 BRW_SURFACEFORMAT_R32_FLOAT,
65 BRW_SURFACEFORMAT_R32G32_FLOAT,
66 BRW_SURFACEFORMAT_R32G32B32_FLOAT,
67 BRW_SURFACEFORMAT_R32G32B32A32_FLOAT
70 static GLuint uint_types_norm[5] = {
72 BRW_SURFACEFORMAT_R32_UNORM,
73 BRW_SURFACEFORMAT_R32G32_UNORM,
74 BRW_SURFACEFORMAT_R32G32B32_UNORM,
75 BRW_SURFACEFORMAT_R32G32B32A32_UNORM
78 static GLuint uint_types_scale[5] = {
80 BRW_SURFACEFORMAT_R32_USCALED,
81 BRW_SURFACEFORMAT_R32G32_USCALED,
82 BRW_SURFACEFORMAT_R32G32B32_USCALED,
83 BRW_SURFACEFORMAT_R32G32B32A32_USCALED
86 static GLuint int_types_norm[5] = {
88 BRW_SURFACEFORMAT_R32_SNORM,
89 BRW_SURFACEFORMAT_R32G32_SNORM,
90 BRW_SURFACEFORMAT_R32G32B32_SNORM,
91 BRW_SURFACEFORMAT_R32G32B32A32_SNORM
94 static GLuint int_types_scale[5] = {
96 BRW_SURFACEFORMAT_R32_SSCALED,
97 BRW_SURFACEFORMAT_R32G32_SSCALED,
98 BRW_SURFACEFORMAT_R32G32B32_SSCALED,
99 BRW_SURFACEFORMAT_R32G32B32A32_SSCALED
102 static GLuint ushort_types_norm[5] = {
104 BRW_SURFACEFORMAT_R16_UNORM,
105 BRW_SURFACEFORMAT_R16G16_UNORM,
106 BRW_SURFACEFORMAT_R16G16B16_UNORM,
107 BRW_SURFACEFORMAT_R16G16B16A16_UNORM
110 static GLuint ushort_types_scale[5] = {
112 BRW_SURFACEFORMAT_R16_USCALED,
113 BRW_SURFACEFORMAT_R16G16_USCALED,
114 BRW_SURFACEFORMAT_R16G16B16_USCALED,
115 BRW_SURFACEFORMAT_R16G16B16A16_USCALED
118 static GLuint short_types_norm[5] = {
120 BRW_SURFACEFORMAT_R16_SNORM,
121 BRW_SURFACEFORMAT_R16G16_SNORM,
122 BRW_SURFACEFORMAT_R16G16B16_SNORM,
123 BRW_SURFACEFORMAT_R16G16B16A16_SNORM
126 static GLuint short_types_scale[5] = {
128 BRW_SURFACEFORMAT_R16_SSCALED,
129 BRW_SURFACEFORMAT_R16G16_SSCALED,
130 BRW_SURFACEFORMAT_R16G16B16_SSCALED,
131 BRW_SURFACEFORMAT_R16G16B16A16_SSCALED
134 static GLuint ubyte_types_norm[5] = {
136 BRW_SURFACEFORMAT_R8_UNORM,
137 BRW_SURFACEFORMAT_R8G8_UNORM,
138 BRW_SURFACEFORMAT_R8G8B8_UNORM,
139 BRW_SURFACEFORMAT_R8G8B8A8_UNORM
142 static GLuint ubyte_types_scale[5] = {
144 BRW_SURFACEFORMAT_R8_USCALED,
145 BRW_SURFACEFORMAT_R8G8_USCALED,
146 BRW_SURFACEFORMAT_R8G8B8_USCALED,
147 BRW_SURFACEFORMAT_R8G8B8A8_USCALED
150 static GLuint byte_types_norm[5] = {
152 BRW_SURFACEFORMAT_R8_SNORM,
153 BRW_SURFACEFORMAT_R8G8_SNORM,
154 BRW_SURFACEFORMAT_R8G8B8_SNORM,
155 BRW_SURFACEFORMAT_R8G8B8A8_SNORM
158 static GLuint byte_types_scale[5] = {
160 BRW_SURFACEFORMAT_R8_SSCALED,
161 BRW_SURFACEFORMAT_R8G8_SSCALED,
162 BRW_SURFACEFORMAT_R8G8B8_SSCALED,
163 BRW_SURFACEFORMAT_R8G8B8A8_SSCALED
167 static GLuint get_surface_type( GLenum type, GLuint size, GLboolean normalized )
169 if (INTEL_DEBUG & DEBUG_VERTS)
170 _mesa_printf("type %s size %d normalized %d\n",
171 _mesa_lookup_enum_by_nr(type), size, normalized);
175 case GL_DOUBLE: return double_types[size];
176 case GL_FLOAT: return float_types[size];
177 case GL_INT: return int_types_norm[size];
178 case GL_SHORT: return short_types_norm[size];
179 case GL_BYTE: return byte_types_norm[size];
180 case GL_UNSIGNED_INT: return uint_types_norm[size];
181 case GL_UNSIGNED_SHORT: return ushort_types_norm[size];
182 case GL_UNSIGNED_BYTE: return ubyte_types_norm[size];
183 default: assert(0); return 0;
188 case GL_DOUBLE: return double_types[size];
189 case GL_FLOAT: return float_types[size];
190 case GL_INT: return int_types_scale[size];
191 case GL_SHORT: return short_types_scale[size];
192 case GL_BYTE: return byte_types_scale[size];
193 case GL_UNSIGNED_INT: return uint_types_scale[size];
194 case GL_UNSIGNED_SHORT: return ushort_types_scale[size];
195 case GL_UNSIGNED_BYTE: return ubyte_types_scale[size];
196 default: assert(0); return 0;
202 static GLuint get_size( GLenum type )
205 case GL_DOUBLE: return sizeof(GLdouble);
206 case GL_FLOAT: return sizeof(GLfloat);
207 case GL_INT: return sizeof(GLint);
208 case GL_SHORT: return sizeof(GLshort);
209 case GL_BYTE: return sizeof(GLbyte);
210 case GL_UNSIGNED_INT: return sizeof(GLuint);
211 case GL_UNSIGNED_SHORT: return sizeof(GLushort);
212 case GL_UNSIGNED_BYTE: return sizeof(GLubyte);
217 static GLuint get_index_type(GLenum type)
220 case GL_UNSIGNED_BYTE: return BRW_INDEX_BYTE;
221 case GL_UNSIGNED_SHORT: return BRW_INDEX_WORD;
222 case GL_UNSIGNED_INT: return BRW_INDEX_DWORD;
223 default: assert(0); return 0;
227 static void copy_strided_array( GLubyte *dest,
234 memcpy(dest, src, count * size);
238 for (i = 0; i < count; i++) {
239 memcpy(dest, src, size);
246 static void wrap_buffers( struct brw_context *brw,
249 GLcontext *ctx = &brw->intel.ctx;
251 if (size < BRW_UPLOAD_INIT_SIZE)
252 size = BRW_UPLOAD_INIT_SIZE;
254 brw->vb.upload.buf++;
255 brw->vb.upload.buf %= BRW_NR_UPLOAD_BUFS;
256 brw->vb.upload.offset = 0;
258 ctx->Driver.BufferData(ctx,
263 brw->vb.upload.vbo[brw->vb.upload.buf]);
266 static void get_space( struct brw_context *brw,
268 struct gl_buffer_object **vbo_return,
269 GLuint *offset_return )
271 size = ALIGN(size, 64);
273 if (brw->vb.upload.offset + size > BRW_UPLOAD_INIT_SIZE)
274 wrap_buffers(brw, size);
276 *vbo_return = brw->vb.upload.vbo[brw->vb.upload.buf];
277 *offset_return = brw->vb.upload.offset;
279 brw->vb.upload.offset += size;
283 copy_array_to_vbo_array( struct brw_context *brw,
284 struct gl_client_array *vbo_array,
285 const struct gl_client_array *array,
289 GLcontext *ctx = &brw->intel.ctx;
290 GLuint size = count * element_size;
291 struct gl_buffer_object *vbo;
295 get_space(brw, size, &vbo, &offset);
297 if (array->StrideB == 0) {
302 new_stride = element_size;
304 vbo_array->Size = array->Size;
305 vbo_array->Type = array->Type;
306 vbo_array->Stride = new_stride;
307 vbo_array->StrideB = new_stride;
308 vbo_array->Ptr = (const void *)offset;
309 vbo_array->Enabled = 1;
310 vbo_array->Normalized = array->Normalized;
311 vbo_array->_MaxElement = array->_MaxElement; /* ? */
312 vbo_array->BufferObj = vbo;
315 GLubyte *map = ctx->Driver.MapBuffer(ctx,
322 copy_strided_array( map,
328 ctx->Driver.UnmapBuffer(ctx, GL_ARRAY_BUFFER_ARB, vbo_array->BufferObj);
333 * Just a wrapper to highlight which cause of copy_array_to_vbo_array
334 * is happening in the profile.
337 interleaved_copy_array_to_vbo_array(struct brw_context *brw,
338 struct gl_client_array *vbo_array,
339 const struct gl_client_array *array,
343 copy_array_to_vbo_array(brw, vbo_array, array, element_size, count);
347 interleaved_vbo_array( struct brw_context *brw,
348 struct gl_client_array *vbo_array,
349 const struct gl_client_array *uploaded_array,
350 const struct gl_client_array *array,
353 vbo_array->Size = array->Size;
354 vbo_array->Type = array->Type;
355 vbo_array->Stride = array->Stride;
356 vbo_array->StrideB = array->StrideB;
357 vbo_array->Ptr = (const void *)((const char *)uploaded_array->Ptr +
358 ((const char *)array->Ptr - ptr));
359 vbo_array->Enabled = 1;
360 vbo_array->Normalized = array->Normalized;
361 vbo_array->_MaxElement = array->_MaxElement;
362 vbo_array->BufferObj = uploaded_array->BufferObj;
366 GLboolean brw_upload_vertices( struct brw_context *brw,
370 GLcontext *ctx = &brw->intel.ctx;
371 struct intel_context *intel = intel_context(ctx);
372 GLuint tmp = brw->vs.prog_data->inputs_read;
374 const void *ptr = NULL;
375 GLuint interleave = 0;
376 struct gl_client_array vbo_array_temp[VERT_ATTRIB_MAX];
378 struct brw_vertex_element *enabled[VERT_ATTRIB_MAX];
379 GLuint nr_enabled = 0;
381 struct brw_vertex_element *upload[VERT_ATTRIB_MAX];
382 GLuint nr_uploads = 0;
384 /* First build an array of pointers to ve's in vb.inputs_read
387 _mesa_printf("%s %d..%d\n", __FUNCTION__, min_index, max_index);
390 GLuint i = _mesa_ffsll(tmp)-1;
391 struct brw_vertex_element *input = &brw->vb.inputs[i];
394 enabled[nr_enabled++] = input;
396 input->element_size = get_size(input->glarray->Type) * input->glarray->Size;
397 input->count = input->glarray->StrideB ? max_index + 1 - min_index : 1;
399 if (!input->glarray->BufferObj->Name) {
401 /* Position array not properly enabled:
403 if (input->glarray->StrideB == 0)
406 interleave = input->glarray->StrideB;
407 ptr = input->glarray->Ptr;
409 else if (interleave != input->glarray->StrideB ||
410 (const char *)input->glarray->Ptr - (const char *)ptr < 0 ||
411 (const char *)input->glarray->Ptr - (const char *)ptr > interleave) {
415 upload[nr_uploads++] = input;
417 /* We rebase drawing to start at element zero only when
418 * varyings are not in vbos, which means we can end up
419 * uploading non-varying arrays (stride != 0) when min_index
420 * is zero. This doesn't matter as the amount to upload is
421 * the same for these arrays whether the draw call is rebased
422 * or not - we just have to upload the one element.
424 assert(min_index == 0 || input->glarray->StrideB == 0);
428 /* Upload interleaved arrays if all uploads are interleaved
430 if (nr_uploads > 1 && interleave && interleave <= 256) {
431 interleaved_copy_array_to_vbo_array(brw, &vbo_array_temp[0],
435 upload[0]->glarray = &vbo_array_temp[0];
437 for (i = 1; i < nr_uploads; i++) {
438 interleaved_vbo_array(brw,
443 upload[i]->glarray = &vbo_array_temp[i];
447 for (i = 0; i < nr_uploads; i++) {
448 copy_array_to_vbo_array(brw, &vbo_array_temp[i],
450 upload[i]->element_size,
452 upload[i]->glarray = &vbo_array_temp[i];
456 /* XXX: In the rare cases where this happens we fallback all
457 * the way to software rasterization, although a tnl fallback
458 * would be sufficient. I don't know of *any* real world
459 * cases with > 17 vertex attributes enabled, so it probably
460 * isn't an issue at this point.
462 if (nr_enabled >= BRW_VEP_MAX)
465 /* Now emit VB and VEP state packets.
467 * This still defines a hardware VB for each input, even if they
468 * are interleaved or from the same VBO. TBD if this makes a
469 * performance difference.
471 BEGIN_BATCH(1 + nr_enabled * 4, IGNORE_CLIPRECTS);
472 OUT_BATCH((CMD_VERTEX_BUFFER << 16) |
473 ((1 + nr_enabled * 4) - 2));
475 for (i = 0; i < nr_enabled; i++) {
476 struct brw_vertex_element *input = enabled[i];
478 OUT_BATCH((i << BRW_VB0_INDEX_SHIFT) |
479 BRW_VB0_ACCESS_VERTEXDATA |
480 (input->glarray->StrideB << BRW_VB0_PITCH_SHIFT));
481 OUT_RELOC(array_buffer(intel, input->glarray),
482 DRM_BO_FLAG_MEM_TT | DRM_BO_FLAG_READ,
483 (GLuint)input->glarray->Ptr);
484 OUT_BATCH(max_index);
485 OUT_BATCH(0); /* Instance data step rate */
489 BEGIN_BATCH(1 + nr_enabled * 2, IGNORE_CLIPRECTS);
490 OUT_BATCH((CMD_VERTEX_ELEMENT << 16) | ((1 + nr_enabled * 2) - 2));
491 for (i = 0; i < nr_enabled; i++) {
492 struct brw_vertex_element *input = enabled[i];
493 uint32_t format = get_surface_type(input->glarray->Type,
494 input->glarray->Size,
495 input->glarray->Normalized);
496 uint32_t comp0 = BRW_VE1_COMPONENT_STORE_SRC;
497 uint32_t comp1 = BRW_VE1_COMPONENT_STORE_SRC;
498 uint32_t comp2 = BRW_VE1_COMPONENT_STORE_SRC;
499 uint32_t comp3 = BRW_VE1_COMPONENT_STORE_SRC;
501 switch (input->glarray->Size) {
502 case 0: comp0 = BRW_VE1_COMPONENT_STORE_0;
503 case 1: comp1 = BRW_VE1_COMPONENT_STORE_0;
504 case 2: comp2 = BRW_VE1_COMPONENT_STORE_0;
505 case 3: comp3 = BRW_VE1_COMPONENT_STORE_1_FLT;
509 OUT_BATCH((i << BRW_VE0_INDEX_SHIFT) |
511 (format << BRW_VE0_FORMAT_SHIFT) |
512 (0 << BRW_VE0_SRC_OFFSET_SHIFT));
513 OUT_BATCH((comp0 << BRW_VE1_COMPONENT_0_SHIFT) |
514 (comp1 << BRW_VE1_COMPONENT_1_SHIFT) |
515 (comp2 << BRW_VE1_COMPONENT_2_SHIFT) |
516 (comp3 << BRW_VE1_COMPONENT_3_SHIFT) |
517 ((i * 4) << BRW_VE1_DST_OFFSET_SHIFT));
524 void brw_upload_indices( struct brw_context *brw,
525 const struct _mesa_index_buffer *index_buffer )
527 GLcontext *ctx = &brw->intel.ctx;
528 struct intel_context *intel = &brw->intel;
529 GLuint ib_size = get_size(index_buffer->type) * index_buffer->count;
530 struct gl_buffer_object *bufferobj = index_buffer->obj;
531 GLuint offset = (GLuint)index_buffer->ptr;
533 /* Turn into a proper VBO:
535 if (!bufferobj->Name) {
537 /* Get new bufferobj, offset:
539 get_space(brw, ib_size, &bufferobj, &offset);
543 ctx->Driver.BufferSubData( ctx,
544 GL_ELEMENT_ARRAY_BUFFER_ARB,
550 /* If the index buffer isn't aligned to its element size, we have to
551 * rebase it into a temporary.
553 if ((get_size(index_buffer->type) - 1) & offset) {
554 struct gl_buffer_object *vbo;
556 GLubyte *map = ctx->Driver.MapBuffer(ctx,
557 GL_ELEMENT_ARRAY_BUFFER_ARB,
561 get_space(brw, ib_size, &vbo, &voffset);
563 ctx->Driver.BufferSubData(ctx,
564 GL_ELEMENT_ARRAY_BUFFER_ARB,
569 ctx->Driver.UnmapBuffer(ctx, GL_ELEMENT_ARRAY_BUFFER_ARB, bufferobj);
576 /* Emit the indexbuffer packet:
579 struct brw_indexbuffer ib;
580 dri_bo *buffer = intel_bufferobj_buffer(intel,
581 intel_buffer_object(bufferobj),
584 memset(&ib, 0, sizeof(ib));
586 ib.header.bits.opcode = CMD_INDEX_BUFFER;
587 ib.header.bits.length = sizeof(ib)/4 - 2;
588 ib.header.bits.index_format = get_index_type(index_buffer->type);
589 ib.header.bits.cut_index_enable = 0;
592 BEGIN_BATCH(4, IGNORE_CLIPRECTS);
593 OUT_BATCH( ib.header.dword );
594 OUT_RELOC( buffer, DRM_BO_FLAG_MEM_TT | DRM_BO_FLAG_READ, offset);
595 OUT_RELOC( buffer, DRM_BO_FLAG_MEM_TT | DRM_BO_FLAG_READ,