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) */
110 GLushort
*in
= (GLushort
*)src_ptr
;
111 size
= sizeof(GLushort
) * ((mesa_ind_buf
->count
+ 1) & ~1);
113 radeonAllocDmaRegion(&r300
->radeon
, &r300
->ind_buf
.bo
, &r300
->ind_buf
.bo_offet
, size
, 4);
115 assert(r300
->ind_buf
.bo
->ptr
!= NULL
)
116 out
= (GLuint
*)ADD_POINTERS(r300
->ind_buf
.bo
->ptr
, r300
->ind_buf
.bo_offset
);
118 for (i
= 0; i
+ 1 < mesa_ind_buf
->count
; i
+= 2) {
119 *out
++ = in
[i
] | in
[i
+ 1] << 16;
122 if (i
< mesa_ind_buf
->count
) {
128 r300
->ind_buf
.is_32bit
= GL_FALSE
;
129 r300
->ind_buf
.count
= mesa_ind_buf
->count
;
131 if (mapped_named_bo
) {
132 ctx
->Driver
.UnmapBuffer(ctx
, GL_ELEMENT_ARRAY_BUFFER
, mesa_ind_buf
->obj
);
137 static void r300SetupIndexBuffer(GLcontext
*ctx
, const struct _mesa_index_buffer
*mesa_ind_buf
)
139 r300ContextPtr r300
= R300_CONTEXT(ctx
);
142 r300
->ind_buf
.bo
= NULL
;
147 if (mesa_ind_buf
->type
== GL_UNSIGNED_INT
) {
149 if (mesa_ind_buf
->type
!= GL_UNSIGNED_BYTE
) {
151 const GLvoid
*src_ptr
;
153 GLboolean mapped_named_bo
= GL_FALSE
;
155 if (mesa_ind_buf
->obj
->Name
&& !mesa_ind_buf
->obj
->Pointer
) {
156 ctx
->Driver
.MapBuffer(ctx
, GL_ELEMENT_ARRAY_BUFFER
, GL_READ_ONLY_ARB
, mesa_ind_buf
->obj
);
157 assert(mesa_ind_buf
->obj
->Pointer
!= NULL
);
158 mapped_named_bo
= GL_TRUE
;
161 src_ptr
= ADD_POINTERS(mesa_ind_buf
->obj
->Pointer
, mesa_ind_buf
->ptr
);
163 const GLuint size
= mesa_ind_buf
->count
* getTypeSize(mesa_ind_buf
->type
);
165 radeonAllocDmaRegion(&r300
->radeon
, &r300
->ind_buf
.bo
, &r300
->ind_buf
.bo_offset
, size
, 4);
167 assert(r300
->ind_buf
.bo
->ptr
!= NULL
);
168 dst_ptr
= ADD_POINTERS(r300
->ind_buf
.bo
->ptr
, r300
->ind_buf
.bo_offset
);
169 _mesa_memcpy(dst_ptr
, src_ptr
, size
);
171 r300
->ind_buf
.is_32bit
= (mesa_ind_buf
->type
== GL_UNSIGNED_INT
);
172 r300
->ind_buf
.count
= mesa_ind_buf
->count
;
174 if (mapped_named_bo
) {
175 ctx
->Driver
.UnmapBuffer(ctx
, GL_ELEMENT_ARRAY_BUFFER
, mesa_ind_buf
->obj
);
178 r300FixupIndexBuffer(ctx
, mesa_ind_buf
);
182 #define CONVERT( TYPE, MACRO ) do { \
185 if (input->Normalized) { \
186 for (i = 0; i < count; i++) { \
187 const TYPE *in = (TYPE *)src_ptr; \
188 for (j = 0; j < sz; j++) { \
189 *dst_ptr++ = MACRO(*in); \
195 for (i = 0; i < count; i++) { \
196 const TYPE *in = (TYPE *)src_ptr; \
197 for (j = 0; j < sz; j++) { \
198 *dst_ptr++ = (GLfloat)(*in); \
207 * Convert attribute data type to float
208 * If the attribute uses named buffer object replace the bo with newly allocated bo
210 static void r300ConvertAttrib(GLcontext
*ctx
, int count
, const struct gl_client_array
*input
, struct vertex_attribute
*attr
)
212 r300ContextPtr r300
= R300_CONTEXT(ctx
);
213 const GLvoid
*src_ptr
;
214 GLboolean mapped_named_bo
= GL_FALSE
;
218 stride
= (input
->StrideB
== 0) ? getTypeSize(input
->Type
) * input
->Size
: input
->StrideB
;
220 /* Convert value for first element only */
221 if (input
->StrideB
== 0)
224 if (input
->BufferObj
->Name
) {
225 if (!input
->BufferObj
->Pointer
) {
226 ctx
->Driver
.MapBuffer(ctx
, GL_ARRAY_BUFFER
, GL_READ_ONLY_ARB
, input
->BufferObj
);
227 mapped_named_bo
= GL_TRUE
;
230 src_ptr
= ADD_POINTERS(input
->BufferObj
->Pointer
, input
->Ptr
);
232 src_ptr
= input
->Ptr
;
235 radeonAllocDmaRegion(&r300
->radeon
, &attr
->bo
, &attr
->bo_offset
, sizeof(GLfloat
) * input
->Size
* count
, 32);
236 dst_ptr
= (GLfloat
*)ADD_POINTERS(attr
->bo
->ptr
, attr
->bo_offset
);
238 if (RADEON_DEBUG
& DEBUG_FALLBACKS
) {
239 fprintf(stderr
, "%s: Converting vertex attributes, attribute data format %x,", __FUNCTION__
, input
->Type
);
240 fprintf(stderr
, "stride %d, components %d\n", stride
, input
->Size
);
243 assert(src_ptr
!= NULL
);
245 switch (input
->Type
) {
247 CONVERT(GLdouble
, (GLfloat
));
249 case GL_UNSIGNED_INT
:
250 CONVERT(GLuint
, UINT_TO_FLOAT
);
253 CONVERT(GLint
, INT_TO_FLOAT
);
255 case GL_UNSIGNED_SHORT
:
256 CONVERT(GLushort
, USHORT_TO_FLOAT
);
259 CONVERT(GLshort
, SHORT_TO_FLOAT
);
261 case GL_UNSIGNED_BYTE
:
262 assert(input
->Format
!= GL_BGRA
);
263 CONVERT(GLubyte
, UBYTE_TO_FLOAT
);
266 CONVERT(GLbyte
, BYTE_TO_FLOAT
);
273 if (mapped_named_bo
) {
274 ctx
->Driver
.UnmapBuffer(ctx
, GL_ARRAY_BUFFER
, input
->BufferObj
);
278 static void r300AlignDataToDword(GLcontext
*ctx
, const struct gl_client_array
*input
, int count
, struct vertex_attribute
*attr
)
280 r300ContextPtr r300
= R300_CONTEXT(ctx
);
281 const int dst_stride
= (input
->StrideB
+ 3) & ~3;
282 const int size
= getTypeSize(input
->Type
) * input
->Size
* count
;
283 GLboolean mapped_named_bo
= GL_FALSE
;
285 radeonAllocDmaRegion(&r300
->radeon
, &attr
->bo
, &attr
->bo_offset
, size
, 32);
287 if (!input
->BufferObj
->Pointer
) {
288 ctx
->Driver
.MapBuffer(ctx
, GL_ARRAY_BUFFER
, GL_READ_ONLY_ARB
, input
->BufferObj
);
289 mapped_named_bo
= GL_TRUE
;
293 GLvoid
*src_ptr
= ADD_POINTERS(input
->BufferObj
->Pointer
, input
->Ptr
);
294 GLvoid
*dst_ptr
= ADD_POINTERS(attr
->bo
->ptr
, attr
->bo_offset
);
297 for (i
= 0; i
< count
; ++i
) {
298 _mesa_memcpy(dst_ptr
, src_ptr
, input
->StrideB
);
299 src_ptr
+= input
->StrideB
;
300 dst_ptr
+= dst_stride
;
304 if (mapped_named_bo
) {
305 ctx
->Driver
.UnmapBuffer(ctx
, GL_ARRAY_BUFFER
, input
->BufferObj
);
308 attr
->stride
= dst_stride
;
311 static void r300TranslateAttrib(GLcontext
*ctx
, GLuint attr
, int count
, const struct gl_client_array
*input
)
313 r300ContextPtr r300
= R300_CONTEXT(ctx
);
314 struct r300_vertex_buffer
*vbuf
= &r300
->vbuf
;
315 struct vertex_attribute r300_attr
;
319 stride
= (input
->StrideB
== 0) ? getTypeSize(input
->Type
) * input
->Size
: input
->StrideB
;
321 if (input
->Type
== GL_DOUBLE
|| input
->Type
== GL_UNSIGNED_INT
|| input
->Type
== GL_INT
||
323 getTypeSize(input
->Type
) != 4 ||
329 r300ConvertAttrib(ctx
, count
, input
, &r300_attr
);
330 if (input
->StrideB
== 0) {
331 r300_attr
.stride
= 0;
333 r300_attr
.stride
= sizeof(GLfloat
) * input
->Size
;
335 r300_attr
.dwords
= input
->Size
;
336 r300_attr
.is_named_bo
= GL_FALSE
;
339 r300_attr
.dwords
= (getTypeSize(type
) * input
->Size
+ 3)/ 4;
340 if (input
->BufferObj
->Name
) {
341 if (stride
% 4 != 0) {
342 assert(((int) input
->Ptr
) % input
->StrideB
== 0);
343 r300AlignDataToDword(ctx
, input
, count
, &r300_attr
);
344 r300_attr
.is_named_bo
= GL_FALSE
;
346 r300_attr
.stride
= input
->StrideB
;
347 r300_attr
.bo_offset
= (GLuint
) input
->Ptr
;
348 r300_attr
.bo
= get_radeon_buffer_object(input
->BufferObj
)->bo
;
349 r300_attr
.is_named_bo
= GL_TRUE
;
355 if (input
->StrideB
== 0) {
356 size
= getTypeSize(input
->Type
) * input
->Size
;
358 r300_attr
.stride
= 0;
360 size
= getTypeSize(input
->Type
) * input
->Size
* count
;
361 r300_attr
.stride
= (getTypeSize(type
) * input
->Size
+ 3) & ~3;
364 radeonAllocDmaRegion(&r300
->radeon
, &r300_attr
.bo
, &r300_attr
.bo_offset
, size
, 32);
365 assert(r300_attr
.bo
->ptr
!= NULL
);
366 dst
= (uint32_t *)ADD_POINTERS(r300_attr
.bo
->ptr
, r300_attr
.bo_offset
);
367 switch (r300_attr
.dwords
) {
368 case 1: radeonEmitVec4(dst
, input
->Ptr
, input
->StrideB
, count
); break;
369 case 2: radeonEmitVec8(dst
, input
->Ptr
, input
->StrideB
, count
); break;
370 case 3: radeonEmitVec12(dst
, input
->Ptr
, input
->StrideB
, count
); break;
371 case 4: radeonEmitVec16(dst
, input
->Ptr
, input
->StrideB
, count
); break;
372 default: assert(0); break;
375 r300_attr
.is_named_bo
= GL_FALSE
;
379 r300_attr
.size
= input
->Size
;
380 r300_attr
.element
= attr
;
381 r300_attr
.dst_loc
= vbuf
->num_attribs
;
385 switch (input
->Size
) {
386 case 1: r300_attr
.data_type
= R300_DATA_TYPE_FLOAT_1
; break;
387 case 2: r300_attr
.data_type
= R300_DATA_TYPE_FLOAT_2
; break;
388 case 3: r300_attr
.data_type
= R300_DATA_TYPE_FLOAT_3
; break;
389 case 4: r300_attr
.data_type
= R300_DATA_TYPE_FLOAT_4
; break;
391 r300_attr
._signed
= 0;
392 r300_attr
.normalize
= 0;
395 r300_attr
._signed
= 1;
396 r300_attr
.normalize
= input
->Normalized
;
397 switch (input
->Size
) {
400 r300_attr
.data_type
= R300_DATA_TYPE_SHORT_2
;
404 r300_attr
.data_type
= R300_DATA_TYPE_SHORT_4
;
409 r300_attr
._signed
= 1;
410 r300_attr
.normalize
= input
->Normalized
;
411 r300_attr
.data_type
= R300_DATA_TYPE_BYTE
;
413 case GL_UNSIGNED_SHORT
:
414 r300_attr
._signed
= 0;
415 r300_attr
.normalize
= input
->Normalized
;
416 switch (input
->Size
) {
419 r300_attr
.data_type
= R300_DATA_TYPE_SHORT_2
;
423 r300_attr
.data_type
= R300_DATA_TYPE_SHORT_4
;
427 case GL_UNSIGNED_BYTE
:
428 r300_attr
._signed
= 0;
429 r300_attr
.normalize
= input
->Normalized
;
430 if (input
->Format
== GL_BGRA
)
431 r300_attr
.data_type
= R300_DATA_TYPE_D3DCOLOR
;
433 r300_attr
.data_type
= R300_DATA_TYPE_BYTE
;
439 case GL_UNSIGNED_INT
:
444 switch (input
->Size
) {
446 r300_attr
.swizzle
= SWIZZLE_XYZW
;
449 r300_attr
.swizzle
= MAKE_SWIZZLE4(SWIZZLE_X
, SWIZZLE_Y
, SWIZZLE_Z
, SWIZZLE_ONE
);
452 r300_attr
.swizzle
= MAKE_SWIZZLE4(SWIZZLE_X
, SWIZZLE_Y
, SWIZZLE_ZERO
, SWIZZLE_ONE
);
455 r300_attr
.swizzle
= MAKE_SWIZZLE4(SWIZZLE_X
, SWIZZLE_ZERO
, SWIZZLE_ZERO
, SWIZZLE_ONE
);
459 r300_attr
.write_mask
= MASK_XYZW
;
461 vbuf
->attribs
[vbuf
->num_attribs
] = r300_attr
;
465 static void r300SetVertexFormat(GLcontext
*ctx
, const struct gl_client_array
*arrays
[], int count
)
467 r300ContextPtr r300
= R300_CONTEXT(ctx
);
468 struct r300_vertex_buffer
*vbuf
= &r300
->vbuf
;
473 tmp
= r300
->selected_vp
->code
.InputsRead
;
475 vbuf
->num_attribs
= 0;
477 /* find first enabled bit */
483 r300TranslateAttrib(ctx
, i
, count
, arrays
[i
]);
490 r300SwitchFallback(ctx
, R300_FALLBACK_AOS_LIMIT
, vbuf
->num_attribs
> R300_MAX_AOS_ARRAYS
);
497 for (i
= 0; i
< vbuf
->num_attribs
; i
++) {
498 struct radeon_aos
*aos
= &r300
->radeon
.tcl
.aos
[i
];
500 aos
->count
= vbuf
->attribs
[i
].stride
== 0 ? 1 : count
;
501 aos
->stride
= vbuf
->attribs
[i
].stride
/ sizeof(float);
502 aos
->offset
= vbuf
->attribs
[i
].bo_offset
;
503 aos
->components
= vbuf
->attribs
[i
].dwords
;
504 aos
->bo
= vbuf
->attribs
[i
].bo
;
506 if (vbuf
->attribs
[i
].is_named_bo
) {
507 radeon_cs_space_add_persistent_bo(r300
->radeon
.cmdbuf
.cs
, r300
->vbuf
.attribs
[i
].bo
, RADEON_GEM_DOMAIN_GTT
, 0);
511 r300
->radeon
.tcl
.aos_count
= vbuf
->num_attribs
;
512 ret
= radeon_cs_space_check_with_bo(r300
->radeon
.cmdbuf
.cs
, r300
->radeon
.dma
.current
, RADEON_GEM_DOMAIN_GTT
, 0);
514 r300SwitchFallback(ctx
, R300_FALLBACK_INVALID_BUFFERS
, GL_TRUE
);
518 static void r300FreeData(GLcontext
*ctx
)
520 /* Need to zero tcl.aos[n].bo and tcl.elt_dma_bo
521 * to prevent double unref in radeonReleaseArrays
522 * called during context destroy
524 r300ContextPtr r300
= R300_CONTEXT(ctx
);
528 for (i
= 0; i
< r300
->vbuf
.num_attribs
; i
++) {
529 if (!r300
->vbuf
.attribs
[i
].is_named_bo
) {
530 radeon_bo_unref(r300
->vbuf
.attribs
[i
].bo
);
532 r300
->radeon
.tcl
.aos
[i
].bo
= NULL
;
537 if (r300
->ind_buf
.bo
!= NULL
) {
538 radeon_bo_unref(r300
->ind_buf
.bo
);
543 static GLboolean
r300TryDrawPrims(GLcontext
*ctx
,
544 const struct gl_client_array
*arrays
[],
545 const struct _mesa_prim
*prim
,
547 const struct _mesa_index_buffer
*ib
,
551 struct r300_context
*r300
= R300_CONTEXT(ctx
);
555 _mesa_update_state( ctx
);
557 if (r300
->options
.hw_tcl_enabled
)
558 _tnl_UpdateFixedFunctionProgram(ctx
);
560 r300UpdateShaders(r300
);
562 r300SwitchFallback(ctx
, R300_FALLBACK_INVALID_BUFFERS
, !r300ValidateBuffers(ctx
));
564 /* ensure we have the cmd buf space in advance to cover
565 * the state + DMA AOS pointers */
566 rcommonEnsureCmdBufSpace(&r300
->radeon
,
567 r300
->radeon
.hw
.max_state_size
+ (60*sizeof(int)),
570 r300SetupIndexBuffer(ctx
, ib
);
572 r300SetVertexFormat(ctx
, arrays
, max_index
+ 1);
577 r300SetupVAP(ctx
, r300
->selected_vp
->code
.InputsRead
, r300
->selected_vp
->code
.OutputsWritten
);
579 r300UpdateShaderStates(r300
);
581 r300EmitCacheFlush(r300
);
582 radeonEmitState(&r300
->radeon
);
584 for (i
= 0; i
< nr_prims
; ++i
) {
585 r300RunRenderPrimitive(ctx
, prim
[i
].start
, prim
[i
].start
+ prim
[i
].count
, prim
[i
].mode
);
588 r300EmitCacheFlush(r300
);
595 static void r300DrawPrims(GLcontext
*ctx
,
596 const struct gl_client_array
*arrays
[],
597 const struct _mesa_prim
*prim
,
599 const struct _mesa_index_buffer
*ib
,
600 GLboolean index_bounds_valid
,
606 /* This check should get folded into just the places that
607 * min/max index are really needed.
609 if (!index_bounds_valid
) {
610 vbo_get_minmax_index(ctx
, prim
, ib
, &min_index
, &max_index
);
614 vbo_rebase_prims( ctx
, arrays
, prim
, nr_prims
, ib
, min_index
, max_index
, r300DrawPrims
);
618 /* Make an attempt at drawing */
619 retval
= r300TryDrawPrims(ctx
, arrays
, prim
, nr_prims
, ib
, min_index
, max_index
);
621 /* If failed run tnl pipeline - it should take care of fallbacks */
623 _tnl_draw_prims(ctx
, arrays
, prim
, nr_prims
, ib
, min_index
, max_index
);
626 void r300InitDraw(GLcontext
*ctx
)
628 struct vbo_context
*vbo
= vbo_context(ctx
);
630 vbo
->draw_prims
= r300DrawPrims
;