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
;
308 static struct gl_client_array
*
309 copy_array_to_vbo_array( struct brw_context
*brw
,
311 const struct gl_client_array
*array
,
315 GLcontext
*ctx
= &brw
->intel
.ctx
;
316 struct gl_client_array
*vbo_array
= &brw
->vb
.vbo_array
[i
];
317 GLuint size
= count
* element_size
;
318 struct gl_buffer_object
*vbo
;
322 get_space(brw
, size
, &vbo
, &offset
);
324 if (array
->StrideB
== 0) {
329 new_stride
= element_size
;
331 vbo_array
->Size
= array
->Size
;
332 vbo_array
->Type
= array
->Type
;
333 vbo_array
->Stride
= new_stride
;
334 vbo_array
->StrideB
= new_stride
;
335 vbo_array
->Ptr
= (const void *)offset
;
336 vbo_array
->Enabled
= 1;
337 vbo_array
->Normalized
= array
->Normalized
;
338 vbo_array
->_MaxElement
= array
->_MaxElement
; /* ? */
339 vbo_array
->BufferObj
= vbo
;
342 GLubyte
*map
= ctx
->Driver
.MapBuffer(ctx
,
349 copy_strided_array( map
,
355 ctx
->Driver
.UnmapBuffer(ctx
, GL_ARRAY_BUFFER_ARB
, vbo_array
->BufferObj
);
363 static struct gl_client_array
*
364 interleaved_vbo_array( struct brw_context
*brw
,
366 const struct gl_client_array
*uploaded_array
,
367 const struct gl_client_array
*array
,
370 struct gl_client_array
*vbo_array
= &brw
->vb
.vbo_array
[i
];
372 vbo_array
->Size
= array
->Size
;
373 vbo_array
->Type
= array
->Type
;
374 vbo_array
->Stride
= array
->Stride
;
375 vbo_array
->StrideB
= array
->StrideB
;
376 vbo_array
->Ptr
= (const void *)((const char *)uploaded_array
->Ptr
+
377 ((const char *)array
->Ptr
- ptr
));
378 vbo_array
->Enabled
= 1;
379 vbo_array
->Normalized
= array
->Normalized
;
380 vbo_array
->_MaxElement
= array
->_MaxElement
;
381 vbo_array
->BufferObj
= uploaded_array
->BufferObj
;
387 GLboolean
brw_upload_vertices( struct brw_context
*brw
,
391 GLcontext
*ctx
= &brw
->intel
.ctx
;
392 struct intel_context
*intel
= intel_context(ctx
);
393 GLuint tmp
= brw
->vs
.prog_data
->inputs_read
;
395 const void *ptr
= NULL
;
396 GLuint interleave
= 0;
398 struct brw_vertex_element
*enabled
[VERT_ATTRIB_MAX
];
399 GLuint nr_enabled
= 0;
401 struct brw_vertex_element
*upload
[VERT_ATTRIB_MAX
];
402 GLuint nr_uploads
= 0;
404 /* First build an array of pointers to ve's in vb.inputs_read
407 _mesa_printf("%s %d..%d\n", __FUNCTION__
, min_index
, max_index
);
410 GLuint i
= _mesa_ffsll(tmp
)-1;
411 struct brw_vertex_element
*input
= &brw
->vb
.inputs
[i
];
414 enabled
[nr_enabled
++] = input
;
417 input
->element_size
= get_size(input
->glarray
->Type
) * input
->glarray
->Size
;
418 input
->count
= input
->glarray
->StrideB
? max_index
+ 1 - min_index
: 1;
420 if (!input
->glarray
->BufferObj
->Name
) {
422 /* Position array not properly enabled:
424 if (input
->glarray
->StrideB
== 0)
427 interleave
= input
->glarray
->StrideB
;
428 ptr
= input
->glarray
->Ptr
;
430 else if (interleave
!= input
->glarray
->StrideB
||
431 (const char *)input
->glarray
->Ptr
- (const char *)ptr
< 0 ||
432 (const char *)input
->glarray
->Ptr
- (const char *)ptr
> interleave
) {
436 upload
[nr_uploads
++] = input
;
438 /* We rebase drawing to start at element zero only when
439 * varyings are not in vbos, which means we can end up
440 * uploading non-varying arrays (stride != 0) when min_index
441 * is zero. This doesn't matter as the amount to upload is
442 * the same for these arrays whether the draw call is rebased
443 * or not - we just have to upload the one element.
445 assert(min_index
== 0 || input
->glarray
->StrideB
== 0);
449 /* Upload interleaved arrays if all uploads are interleaved
451 if (nr_uploads
> 1 &&
454 upload
[0]->glarray
= copy_array_to_vbo_array(brw
, 0,
459 for (i
= 1; i
< nr_uploads
; i
++) {
460 upload
[i
]->glarray
= interleaved_vbo_array(brw
,
468 for (i
= 0; i
< nr_uploads
; i
++) {
469 upload
[i
]->glarray
= copy_array_to_vbo_array(brw
, i
,
471 upload
[i
]->element_size
,
477 /* XXX: In the rare cases where this happens we fallback all
478 * the way to software rasterization, although a tnl fallback
479 * would be sufficient. I don't know of *any* real world
480 * cases with > 17 vertex attributes enabled, so it probably
481 * isn't an issue at this point.
483 if (nr_enabled
>= BRW_VEP_MAX
)
486 /* Now emit VB and VEP state packets.
488 * This still defines a hardware VB for each input, even if they
489 * are interleaved or from the same VBO. TBD if this makes a
490 * performance difference.
492 BEGIN_BATCH(1 + nr_enabled
* 4, IGNORE_CLIPRECTS
);
493 OUT_BATCH((CMD_VERTEX_BUFFER
<< 16) |
494 ((1 + nr_enabled
* 4) - 2));
496 for (i
= 0; i
< nr_enabled
; i
++) {
497 struct brw_vertex_element
*input
= enabled
[i
];
499 OUT_BATCH((i
<< BRW_VB0_INDEX_SHIFT
) |
500 BRW_VB0_ACCESS_VERTEXDATA
|
501 (input
->glarray
->StrideB
<< BRW_VB0_PITCH_SHIFT
));
502 OUT_RELOC(array_buffer(intel
, input
->glarray
),
503 DRM_BO_FLAG_MEM_TT
| DRM_BO_FLAG_READ
,
504 (GLuint
)input
->glarray
->Ptr
);
505 OUT_BATCH(max_index
);
506 OUT_BATCH(0); /* Instance data step rate */
510 BEGIN_BATCH(1 + nr_enabled
* 2, IGNORE_CLIPRECTS
);
511 OUT_BATCH((CMD_VERTEX_ELEMENT
<< 16) | ((1 + nr_enabled
* 2) - 2));
512 for (i
= 0; i
< nr_enabled
; i
++) {
513 struct brw_vertex_element
*input
= enabled
[i
];
514 uint32_t format
= get_surface_type(input
->glarray
->Type
,
515 input
->glarray
->Size
,
516 input
->glarray
->Normalized
);
517 uint32_t comp0
= BRW_VE1_COMPONENT_STORE_SRC
;
518 uint32_t comp1
= BRW_VE1_COMPONENT_STORE_SRC
;
519 uint32_t comp2
= BRW_VE1_COMPONENT_STORE_SRC
;
520 uint32_t comp3
= BRW_VE1_COMPONENT_STORE_SRC
;
522 switch (input
->glarray
->Size
) {
523 case 0: comp0
= BRW_VE1_COMPONENT_STORE_0
;
524 case 1: comp1
= BRW_VE1_COMPONENT_STORE_0
;
525 case 2: comp2
= BRW_VE1_COMPONENT_STORE_0
;
526 case 3: comp3
= BRW_VE1_COMPONENT_STORE_1_FLT
;
530 OUT_BATCH((i
<< BRW_VE0_INDEX_SHIFT
) |
532 (format
<< BRW_VE0_FORMAT_SHIFT
) |
533 (0 << BRW_VE0_SRC_OFFSET_SHIFT
));
534 OUT_BATCH((comp0
<< BRW_VE1_COMPONENT_0_SHIFT
) |
535 (comp1
<< BRW_VE1_COMPONENT_1_SHIFT
) |
536 (comp2
<< BRW_VE1_COMPONENT_2_SHIFT
) |
537 (comp3
<< BRW_VE1_COMPONENT_3_SHIFT
) |
538 ((i
* 4) << BRW_VE1_DST_OFFSET_SHIFT
));
545 void brw_upload_indices( struct brw_context
*brw
,
546 const struct _mesa_index_buffer
*index_buffer
)
548 GLcontext
*ctx
= &brw
->intel
.ctx
;
549 struct intel_context
*intel
= &brw
->intel
;
550 GLuint ib_size
= get_size(index_buffer
->type
) * index_buffer
->count
;
551 struct gl_buffer_object
*bufferobj
= index_buffer
->obj
;
552 GLuint offset
= (GLuint
)index_buffer
->ptr
;
554 /* Turn into a proper VBO:
556 if (!bufferobj
->Name
) {
558 /* Get new bufferobj, offset:
560 get_space(brw
, ib_size
, &bufferobj
, &offset
);
564 ctx
->Driver
.BufferSubData( ctx
,
565 GL_ELEMENT_ARRAY_BUFFER_ARB
,
571 /* If the index buffer isn't aligned to its element size, we have to
572 * rebase it into a temporary.
574 if ((get_size(index_buffer
->type
) - 1) & offset
) {
575 struct gl_buffer_object
*vbo
;
577 GLubyte
*map
= ctx
->Driver
.MapBuffer(ctx
,
578 GL_ELEMENT_ARRAY_BUFFER_ARB
,
582 get_space(brw
, ib_size
, &vbo
, &voffset
);
584 ctx
->Driver
.BufferSubData(ctx
,
585 GL_ELEMENT_ARRAY_BUFFER_ARB
,
590 ctx
->Driver
.UnmapBuffer(ctx
, GL_ELEMENT_ARRAY_BUFFER_ARB
, bufferobj
);
597 /* Emit the indexbuffer packet:
600 struct brw_indexbuffer ib
;
601 dri_bo
*buffer
= intel_bufferobj_buffer(intel
,
602 intel_buffer_object(bufferobj
),
605 memset(&ib
, 0, sizeof(ib
));
607 ib
.header
.bits
.opcode
= CMD_INDEX_BUFFER
;
608 ib
.header
.bits
.length
= sizeof(ib
)/4 - 2;
609 ib
.header
.bits
.index_format
= get_index_type(index_buffer
->type
);
610 ib
.header
.bits
.cut_index_enable
= 0;
613 BEGIN_BATCH(4, IGNORE_CLIPRECTS
);
614 OUT_BATCH( ib
.header
.dword
);
615 OUT_RELOC( buffer
, DRM_BO_FLAG_MEM_TT
| DRM_BO_FLAG_READ
, offset
);
616 OUT_RELOC( buffer
, DRM_BO_FLAG_MEM_TT
| DRM_BO_FLAG_READ
,