1 /**************************************************************************
3 * Copyright 2009 Maciej Cencora
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, sub license, 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:
13 * The above copyright notice and this permission notice (including the
14 * next paragraph) shall be included in all copies or substantial portions
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 NON-INFRINGEMENT.
20 * IN NO EVENT SHALL THE AUTHOR(S) 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.
25 **************************************************************************/
29 #include "main/glheader.h"
30 #include "main/context.h"
31 #include "main/state.h"
32 #include "main/api_validate.h"
33 #include "main/enums.h"
36 #include "r300_context.h"
37 #include "r300_emit.h"
38 #include "r300_render.h"
39 #include "r300_state.h"
42 #include "radeon_buffer_objects.h"
45 #include "tnl/t_vp_build.h"
46 #include "vbo/vbo_context.h"
47 #include "swrast/swrast.h"
48 #include "swrast_setup/swrast_setup.h"
51 static int getTypeSize(GLenum type)
55 return sizeof(GLdouble);
57 return sizeof(GLfloat);
61 return sizeof(GLuint);
63 return sizeof(GLshort);
64 case GL_UNSIGNED_SHORT:
65 return sizeof(GLushort);
67 return sizeof(GLbyte);
68 case GL_UNSIGNED_BYTE:
69 return sizeof(GLubyte);
76 static void r300FixupIndexBuffer(GLcontext *ctx, const struct _mesa_index_buffer *mesa_ind_buf)
78 r300ContextPtr r300 = R300_CONTEXT(ctx);
82 GLboolean mapped_named_bo = GL_FALSE;
84 if (mesa_ind_buf->obj->Name && !mesa_ind_buf->obj->Pointer) {
85 ctx->Driver.MapBuffer(ctx, GL_ELEMENT_ARRAY_BUFFER, GL_READ_ONLY_ARB, mesa_ind_buf->obj);
86 mapped_named_bo = GL_TRUE;
87 assert(mesa_ind_buf->obj->Pointer != NULL);
89 src_ptr = ADD_POINTERS(mesa_ind_buf->obj->Pointer, mesa_ind_buf->ptr);
91 if (mesa_ind_buf->type == GL_UNSIGNED_BYTE) {
92 GLuint size = sizeof(GLushort) * ((mesa_ind_buf->count + 1) & ~1);
93 GLubyte *in = (GLubyte *)src_ptr;
95 radeonAllocDmaRegion(&r300->radeon, &r300->ind_buf.bo, &r300->ind_buf.bo_offset, size, 4);
97 assert(r300->ind_buf.bo->ptr != NULL);
98 out = (GLuint *)ADD_POINTERS(r300->ind_buf.bo->ptr, r300->ind_buf.bo_offset);
100 for (i = 0; i + 1 < mesa_ind_buf->count; i += 2) {
101 *out++ = in[i] | in[i + 1] << 16;
104 if (i < mesa_ind_buf->count) {
109 } else { /* if (mesa_ind_buf->type == GL_UNSIGNED_SHORT) */
111 GLushort *in = (GLushort *)src_ptr;
112 size = sizeof(GLushort) * ((mesa_ind_buf->count + 1) & ~1);
114 radeonAllocDmaRegion(&r300->radeon, &r300->ind_buf.bo, &r300->ind_buf.bo_offet, size, 4);
116 assert(r300->ind_buf.bo->ptr != NULL);
117 out = (GLuint *)ADD_POINTERS(r300->ind_buf.bo->ptr, r300->ind_buf.bo_offset);
119 for (i = 0; i + 1 < mesa_ind_buf->count; i += 2) {
120 *out++ = in[i] | in[i + 1] << 16;
123 if (i < mesa_ind_buf->count) {
129 r300->ind_buf.is_32bit = GL_FALSE;
130 r300->ind_buf.count = mesa_ind_buf->count;
132 if (mapped_named_bo) {
133 ctx->Driver.UnmapBuffer(ctx, GL_ELEMENT_ARRAY_BUFFER, mesa_ind_buf->obj);
138 static void r300SetupIndexBuffer(GLcontext *ctx, const struct _mesa_index_buffer *mesa_ind_buf)
140 r300ContextPtr r300 = R300_CONTEXT(ctx);
143 r300->ind_buf.bo = NULL;
148 if (mesa_ind_buf->type == GL_UNSIGNED_INT) {
150 if (mesa_ind_buf->type != GL_UNSIGNED_BYTE) {
152 const GLvoid *src_ptr;
154 GLboolean mapped_named_bo = GL_FALSE;
156 if (mesa_ind_buf->obj->Name && !mesa_ind_buf->obj->Pointer) {
157 ctx->Driver.MapBuffer(ctx, GL_ELEMENT_ARRAY_BUFFER, GL_READ_ONLY_ARB, mesa_ind_buf->obj);
158 assert(mesa_ind_buf->obj->Pointer != NULL);
159 mapped_named_bo = GL_TRUE;
162 src_ptr = ADD_POINTERS(mesa_ind_buf->obj->Pointer, mesa_ind_buf->ptr);
164 const GLuint size = mesa_ind_buf->count * getTypeSize(mesa_ind_buf->type);
166 radeonAllocDmaRegion(&r300->radeon, &r300->ind_buf.bo, &r300->ind_buf.bo_offset, size, 4);
168 assert(r300->ind_buf.bo->ptr != NULL);
169 dst_ptr = ADD_POINTERS(r300->ind_buf.bo->ptr, r300->ind_buf.bo_offset);
170 _mesa_memcpy(dst_ptr, src_ptr, size);
172 r300->ind_buf.is_32bit = (mesa_ind_buf->type == GL_UNSIGNED_INT);
173 r300->ind_buf.count = mesa_ind_buf->count;
175 if (mapped_named_bo) {
176 ctx->Driver.UnmapBuffer(ctx, GL_ELEMENT_ARRAY_BUFFER, mesa_ind_buf->obj);
179 r300FixupIndexBuffer(ctx, mesa_ind_buf);
183 #define CONVERT( TYPE, MACRO ) do { \
186 if (input->Normalized) { \
187 for (i = 0; i < count; i++) { \
188 const TYPE *in = (TYPE *)src_ptr; \
189 for (j = 0; j < sz; j++) { \
190 *dst_ptr++ = MACRO(*in); \
196 for (i = 0; i < count; i++) { \
197 const TYPE *in = (TYPE *)src_ptr; \
198 for (j = 0; j < sz; j++) { \
199 *dst_ptr++ = (GLfloat)(*in); \
208 * Convert attribute data type to float
209 * If the attribute uses named buffer object replace the bo with newly allocated bo
211 static void r300ConvertAttrib(GLcontext *ctx, int count, const struct gl_client_array *input, struct vertex_attribute *attr)
213 r300ContextPtr r300 = R300_CONTEXT(ctx);
214 const GLvoid *src_ptr;
215 GLboolean mapped_named_bo = GL_FALSE;
219 stride = (input->StrideB == 0) ? getTypeSize(input->Type) * input->Size : input->StrideB;
221 /* Convert value for first element only */
222 if (input->StrideB == 0)
225 if (input->BufferObj->Name) {
226 if (!input->BufferObj->Pointer) {
227 ctx->Driver.MapBuffer(ctx, GL_ARRAY_BUFFER, GL_READ_ONLY_ARB, input->BufferObj);
228 mapped_named_bo = GL_TRUE;
231 src_ptr = ADD_POINTERS(input->BufferObj->Pointer, input->Ptr);
233 src_ptr = input->Ptr;
236 radeonAllocDmaRegion(&r300->radeon, &attr->bo, &attr->bo_offset, sizeof(GLfloat) * input->Size * count, 32);
237 dst_ptr = (GLfloat *)ADD_POINTERS(attr->bo->ptr, attr->bo_offset);
239 if (RADEON_DEBUG & DEBUG_FALLBACKS) {
240 fprintf(stderr, "%s: Converting vertex attributes, attribute data format %x,", __FUNCTION__, input->Type);
241 fprintf(stderr, "stride %d, components %d\n", stride, input->Size);
244 assert(src_ptr != NULL);
246 switch (input->Type) {
248 CONVERT(GLdouble, (GLfloat));
250 case GL_UNSIGNED_INT:
251 CONVERT(GLuint, UINT_TO_FLOAT);
254 CONVERT(GLint, INT_TO_FLOAT);
256 case GL_UNSIGNED_SHORT:
257 CONVERT(GLushort, USHORT_TO_FLOAT);
260 CONVERT(GLshort, SHORT_TO_FLOAT);
262 case GL_UNSIGNED_BYTE:
263 assert(input->Format != GL_BGRA);
264 CONVERT(GLubyte, UBYTE_TO_FLOAT);
267 CONVERT(GLbyte, BYTE_TO_FLOAT);
274 if (mapped_named_bo) {
275 ctx->Driver.UnmapBuffer(ctx, GL_ARRAY_BUFFER, input->BufferObj);
279 static void r300AlignDataToDword(GLcontext *ctx, const struct gl_client_array *input, int count, struct vertex_attribute *attr)
281 r300ContextPtr r300 = R300_CONTEXT(ctx);
282 const int dst_stride = (input->StrideB + 3) & ~3;
283 const int size = getTypeSize(input->Type) * input->Size * count;
284 GLboolean mapped_named_bo = GL_FALSE;
286 radeonAllocDmaRegion(&r300->radeon, &attr->bo, &attr->bo_offset, size, 32);
288 if (!input->BufferObj->Pointer) {
289 ctx->Driver.MapBuffer(ctx, GL_ARRAY_BUFFER, GL_READ_ONLY_ARB, input->BufferObj);
290 mapped_named_bo = GL_TRUE;
294 GLvoid *src_ptr = ADD_POINTERS(input->BufferObj->Pointer, input->Ptr);
295 GLvoid *dst_ptr = ADD_POINTERS(attr->bo->ptr, attr->bo_offset);
298 for (i = 0; i < count; ++i) {
299 _mesa_memcpy(dst_ptr, src_ptr, input->StrideB);
300 src_ptr += input->StrideB;
301 dst_ptr += dst_stride;
305 if (mapped_named_bo) {
306 ctx->Driver.UnmapBuffer(ctx, GL_ARRAY_BUFFER, input->BufferObj);
309 attr->stride = dst_stride;
312 static void r300TranslateAttrib(GLcontext *ctx, GLuint attr, int count, const struct gl_client_array *input)
314 r300ContextPtr r300 = R300_CONTEXT(ctx);
315 struct r300_vertex_buffer *vbuf = &r300->vbuf;
316 struct vertex_attribute r300_attr;
320 stride = (input->StrideB == 0) ? getTypeSize(input->Type) * input->Size : input->StrideB;
322 if (input->Type == GL_DOUBLE || input->Type == GL_UNSIGNED_INT || input->Type == GL_INT ||
324 getTypeSize(input->Type) != 4 ||
330 r300ConvertAttrib(ctx, count, input, &r300_attr);
331 if (input->StrideB == 0) {
332 r300_attr.stride = 0;
334 r300_attr.stride = sizeof(GLfloat) * input->Size;
336 r300_attr.dwords = input->Size;
337 r300_attr.is_named_bo = GL_FALSE;
340 r300_attr.dwords = (getTypeSize(type) * input->Size + 3)/ 4;
341 if (input->BufferObj->Name) {
342 if (stride % 4 != 0) {
343 assert(((int) input->Ptr) % input->StrideB == 0);
344 r300AlignDataToDword(ctx, input, count, &r300_attr);
345 r300_attr.is_named_bo = GL_FALSE;
347 r300_attr.stride = input->StrideB;
348 r300_attr.bo_offset = (GLuint) input->Ptr;
349 r300_attr.bo = get_radeon_buffer_object(input->BufferObj)->bo;
350 r300_attr.is_named_bo = GL_TRUE;
356 if (input->StrideB == 0) {
357 size = getTypeSize(input->Type) * input->Size;
359 r300_attr.stride = 0;
361 size = getTypeSize(input->Type) * input->Size * count;
362 r300_attr.stride = (getTypeSize(type) * input->Size + 3) & ~3;
365 radeonAllocDmaRegion(&r300->radeon, &r300_attr.bo, &r300_attr.bo_offset, size, 32);
366 assert(r300_attr.bo->ptr != NULL);
367 dst = (uint32_t *)ADD_POINTERS(r300_attr.bo->ptr, r300_attr.bo_offset);
368 switch (r300_attr.dwords) {
369 case 1: radeonEmitVec4(dst, input->Ptr, input->StrideB, count); break;
370 case 2: radeonEmitVec8(dst, input->Ptr, input->StrideB, count); break;
371 case 3: radeonEmitVec12(dst, input->Ptr, input->StrideB, count); break;
372 case 4: radeonEmitVec16(dst, input->Ptr, input->StrideB, count); break;
373 default: assert(0); break;
376 r300_attr.is_named_bo = GL_FALSE;
380 r300_attr.size = input->Size;
381 r300_attr.element = attr;
382 r300_attr.dst_loc = vbuf->num_attribs;
386 switch (input->Size) {
387 case 1: r300_attr.data_type = R300_DATA_TYPE_FLOAT_1; break;
388 case 2: r300_attr.data_type = R300_DATA_TYPE_FLOAT_2; break;
389 case 3: r300_attr.data_type = R300_DATA_TYPE_FLOAT_3; break;
390 case 4: r300_attr.data_type = R300_DATA_TYPE_FLOAT_4; break;
392 r300_attr._signed = 0;
393 r300_attr.normalize = 0;
396 r300_attr._signed = 1;
397 r300_attr.normalize = input->Normalized;
398 switch (input->Size) {
401 r300_attr.data_type = R300_DATA_TYPE_SHORT_2;
405 r300_attr.data_type = R300_DATA_TYPE_SHORT_4;
410 r300_attr._signed = 1;
411 r300_attr.normalize = input->Normalized;
412 r300_attr.data_type = R300_DATA_TYPE_BYTE;
414 case GL_UNSIGNED_SHORT:
415 r300_attr._signed = 0;
416 r300_attr.normalize = input->Normalized;
417 switch (input->Size) {
420 r300_attr.data_type = R300_DATA_TYPE_SHORT_2;
424 r300_attr.data_type = R300_DATA_TYPE_SHORT_4;
428 case GL_UNSIGNED_BYTE:
429 r300_attr._signed = 0;
430 r300_attr.normalize = input->Normalized;
431 if (input->Format == GL_BGRA)
432 r300_attr.data_type = R300_DATA_TYPE_D3DCOLOR;
434 r300_attr.data_type = R300_DATA_TYPE_BYTE;
440 case GL_UNSIGNED_INT:
445 switch (input->Size) {
447 r300_attr.swizzle = SWIZZLE_XYZW;
450 r300_attr.swizzle = MAKE_SWIZZLE4(SWIZZLE_X, SWIZZLE_Y, SWIZZLE_Z, SWIZZLE_ONE);
453 r300_attr.swizzle = MAKE_SWIZZLE4(SWIZZLE_X, SWIZZLE_Y, SWIZZLE_ZERO, SWIZZLE_ONE);
456 r300_attr.swizzle = MAKE_SWIZZLE4(SWIZZLE_X, SWIZZLE_ZERO, SWIZZLE_ZERO, SWIZZLE_ONE);
460 r300_attr.write_mask = MASK_XYZW;
462 vbuf->attribs[vbuf->num_attribs] = r300_attr;
466 static void r300SetVertexFormat(GLcontext *ctx, const struct gl_client_array *arrays[], int count)
468 r300ContextPtr r300 = R300_CONTEXT(ctx);
469 struct r300_vertex_buffer *vbuf = &r300->vbuf;
474 tmp = r300->selected_vp->code.InputsRead;
476 vbuf->num_attribs = 0;
478 /* find first enabled bit */
484 r300TranslateAttrib(ctx, i, count, arrays[i]);
491 r300SwitchFallback(ctx, R300_FALLBACK_AOS_LIMIT, vbuf->num_attribs > R300_MAX_AOS_ARRAYS);
498 for (i = 0; i < vbuf->num_attribs; i++) {
499 struct radeon_aos *aos = &r300->radeon.tcl.aos[i];
501 aos->count = vbuf->attribs[i].stride == 0 ? 1 : count;
502 aos->stride = vbuf->attribs[i].stride / sizeof(float);
503 aos->offset = vbuf->attribs[i].bo_offset;
504 aos->components = vbuf->attribs[i].dwords;
505 aos->bo = vbuf->attribs[i].bo;
507 if (vbuf->attribs[i].is_named_bo) {
508 radeon_cs_space_add_persistent_bo(r300->radeon.cmdbuf.cs, r300->vbuf.attribs[i].bo, RADEON_GEM_DOMAIN_GTT, 0);
512 r300->radeon.tcl.aos_count = vbuf->num_attribs;
513 ret = radeon_cs_space_check_with_bo(r300->radeon.cmdbuf.cs, r300->radeon.dma.current, RADEON_GEM_DOMAIN_GTT, 0);
515 r300SwitchFallback(ctx, R300_FALLBACK_INVALID_BUFFERS, GL_TRUE);
519 static void r300FreeData(GLcontext *ctx)
521 /* Need to zero tcl.aos[n].bo and tcl.elt_dma_bo
522 * to prevent double unref in radeonReleaseArrays
523 * called during context destroy
525 r300ContextPtr r300 = R300_CONTEXT(ctx);
529 for (i = 0; i < r300->vbuf.num_attribs; i++) {
530 if (!r300->vbuf.attribs[i].is_named_bo) {
531 radeon_bo_unref(r300->vbuf.attribs[i].bo);
533 r300->radeon.tcl.aos[i].bo = NULL;
538 if (r300->ind_buf.bo != NULL) {
539 radeon_bo_unref(r300->ind_buf.bo);
544 static GLboolean r300TryDrawPrims(GLcontext *ctx,
545 const struct gl_client_array *arrays[],
546 const struct _mesa_prim *prim,
548 const struct _mesa_index_buffer *ib,
552 struct r300_context *r300 = R300_CONTEXT(ctx);
556 _mesa_update_state( ctx );
558 if (r300->options.hw_tcl_enabled)
559 _tnl_UpdateFixedFunctionProgram(ctx);
561 r300UpdateShaders(r300);
563 r300SwitchFallback(ctx, R300_FALLBACK_INVALID_BUFFERS, !r300ValidateBuffers(ctx));
565 /* ensure we have the cmd buf space in advance to cover
566 * the state + DMA AOS pointers */
567 rcommonEnsureCmdBufSpace(&r300->radeon,
568 r300->radeon.hw.max_state_size + (60*sizeof(int)),
571 r300SetupIndexBuffer(ctx, ib);
573 r300SetVertexFormat(ctx, arrays, max_index + 1);
578 r300SetupVAP(ctx, r300->selected_vp->code.InputsRead, r300->selected_vp->code.OutputsWritten);
580 r300UpdateShaderStates(r300);
582 r300EmitCacheFlush(r300);
583 radeonEmitState(&r300->radeon);
585 for (i = 0; i < nr_prims; ++i) {
586 r300RunRenderPrimitive(ctx, prim[i].start, prim[i].start + prim[i].count, prim[i].mode);
589 r300EmitCacheFlush(r300);
596 static void r300DrawPrims(GLcontext *ctx,
597 const struct gl_client_array *arrays[],
598 const struct _mesa_prim *prim,
600 const struct _mesa_index_buffer *ib,
601 GLboolean index_bounds_valid,
607 /* This check should get folded into just the places that
608 * min/max index are really needed.
610 if (!index_bounds_valid) {
611 vbo_get_minmax_index(ctx, prim, ib, &min_index, &max_index);
615 vbo_rebase_prims( ctx, arrays, prim, nr_prims, ib, min_index, max_index, r300DrawPrims );
619 /* Make an attempt at drawing */
620 retval = r300TryDrawPrims(ctx, arrays, prim, nr_prims, ib, min_index, max_index);
622 /* If failed run tnl pipeline - it should take care of fallbacks */
624 _tnl_draw_prims(ctx, arrays, prim, nr_prims, ib, min_index, max_index);
627 void r300InitDraw(GLcontext *ctx)
629 struct vbo_context *vbo = vbo_context(ctx);
631 vbo->draw_prims = r300DrawPrims;