2 * Copyright (C) 2008-2009 Advanced Micro Devices, Inc.
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice shall be included
12 * in all copies or substantial portions of the Software.
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17 * THE COPYRIGHT HOLDER(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
18 * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
19 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 * Richard Li <RichardZ.Li@amd.com>, <richardradeon@gmail.com>
25 * CooperYuan <cooper.yuan@amd.com>, <cooperyuan@gmail.com>
28 #include "main/glheader.h"
29 #include "main/state.h"
30 #include "main/imports.h"
31 #include "main/enums.h"
32 #include "main/macros.h"
33 #include "main/context.h"
35 #include "main/simple_list.h"
36 #include "main/api_arrayelt.h"
37 #include "swrast/swrast.h"
38 #include "swrast_setup/swrast_setup.h"
42 #include "tnl/t_vp_build.h"
43 #include "tnl/t_context.h"
44 #include "tnl/t_vertex.h"
45 #include "tnl/t_pipeline.h"
46 #include "vbo/vbo_context.h"
48 #include "r600_context.h"
49 #include "r600_cmdbuf.h"
53 #include "r700_vertprog.h"
54 #include "r700_fragprog.h"
55 #include "r700_state.h"
57 #include "radeon_buffer_objects.h"
58 #include "radeon_common_context.h"
60 void r700WaitForIdle(context_t
*context
);
61 void r700WaitForIdleClean(context_t
*context
);
62 GLboolean
r700SendTextureState(context_t
*context
);
63 static unsigned int r700PrimitiveType(int prim
);
64 void r600UpdateTextureState(GLcontext
* ctx
);
65 GLboolean
r700SyncSurf(context_t
*context
,
66 struct radeon_bo
*pbo
,
68 uint32_t write_domain
,
71 void r700WaitForIdle(context_t
*context
)
73 BATCH_LOCALS(&context
->radeon
);
74 radeon_print(RADEON_RENDER
| RADEON_STATE
, RADEON_TRACE
, "%s\n", __func__
);
75 BEGIN_BATCH_NO_AUTOSTATE(3);
77 R600_OUT_BATCH(CP_PACKET3(R600_IT_SET_CONFIG_REG
, 1));
78 R600_OUT_BATCH(mmWAIT_UNTIL
- ASIC_CONFIG_BASE_INDEX
);
79 R600_OUT_BATCH(WAIT_3D_IDLE_bit
);
85 void r700WaitForIdleClean(context_t
*context
)
87 BATCH_LOCALS(&context
->radeon
);
88 radeon_print(RADEON_RENDER
| RADEON_STATE
, RADEON_TRACE
, "%s\n", __func__
);
89 BEGIN_BATCH_NO_AUTOSTATE(5);
91 R600_OUT_BATCH(CP_PACKET3(R600_IT_EVENT_WRITE
, 0));
92 R600_OUT_BATCH(CACHE_FLUSH_AND_INV_EVENT
);
94 R600_OUT_BATCH(CP_PACKET3(R600_IT_SET_CONFIG_REG
, 1));
95 R600_OUT_BATCH(mmWAIT_UNTIL
- ASIC_CONFIG_BASE_INDEX
);
96 R600_OUT_BATCH(WAIT_3D_IDLE_bit
| WAIT_3D_IDLECLEAN_bit
);
102 void r700Start3D(context_t
*context
)
104 BATCH_LOCALS(&context
->radeon
);
105 radeon_print(RADEON_RENDER
| RADEON_STATE
, RADEON_TRACE
, "%s\n", __func__
);
106 if (context
->radeon
.radeonScreen
->chip_family
< CHIP_FAMILY_RV770
)
108 BEGIN_BATCH_NO_AUTOSTATE(2);
109 R600_OUT_BATCH(CP_PACKET3(R600_IT_START_3D_CMDBUF
, 0));
114 BEGIN_BATCH_NO_AUTOSTATE(3);
115 R600_OUT_BATCH(CP_PACKET3(R600_IT_CONTEXT_CONTROL
, 1));
116 R600_OUT_BATCH(0x80000000);
117 R600_OUT_BATCH(0x80000000);
122 r700WaitForIdleClean(context
);
125 GLboolean
r700SyncSurf(context_t
*context
,
126 struct radeon_bo
*pbo
,
127 uint32_t read_domain
,
128 uint32_t write_domain
,
131 BATCH_LOCALS(&context
->radeon
);
132 radeon_print(RADEON_RENDER
| RADEON_STATE
, RADEON_TRACE
, "%s\n", __func__
);
133 uint32_t cp_coher_size
;
138 if (pbo
->size
== 0xffffffff)
139 cp_coher_size
= 0xffffffff;
141 cp_coher_size
= ((pbo
->size
+ 255) >> 8);
143 BEGIN_BATCH_NO_AUTOSTATE(5 + 2);
144 R600_OUT_BATCH(CP_PACKET3(R600_IT_SURFACE_SYNC
, 3));
145 R600_OUT_BATCH(sync_type
);
146 R600_OUT_BATCH(cp_coher_size
);
149 R600_OUT_BATCH_RELOC(0,
152 read_domain
, write_domain
, 0);
159 static unsigned int r700PrimitiveType(int prim
)
161 switch (prim
& PRIM_MODE_MASK
)
164 return DI_PT_POINTLIST
;
167 return DI_PT_LINELIST
;
170 return DI_PT_LINESTRIP
;
173 return DI_PT_LINELOOP
;
176 return DI_PT_TRILIST
;
178 case GL_TRIANGLE_STRIP
:
179 return DI_PT_TRISTRIP
;
181 case GL_TRIANGLE_FAN
:
185 return DI_PT_QUADLIST
;
188 return DI_PT_QUADSTRIP
;
191 return DI_PT_POLYGON
;
200 static int r700NumVerts(int num_verts
, int prim
)
204 switch (prim
& PRIM_MODE_MASK
) {
209 verts_off
= num_verts
% 2;
213 verts_off
= num_verts
;
217 verts_off
= num_verts
;
220 verts_off
= num_verts
% 3;
222 case GL_TRIANGLE_STRIP
:
224 verts_off
= num_verts
;
226 case GL_TRIANGLE_FAN
:
228 verts_off
= num_verts
;
231 verts_off
= num_verts
% 4;
235 verts_off
= num_verts
;
237 verts_off
= num_verts
% 2;
241 verts_off
= num_verts
;
249 return num_verts
- verts_off
;
252 static void r700RunRenderPrimitive(GLcontext
* ctx
, int start
, int end
, int prim
)
254 context_t
*context
= R700_CONTEXT(ctx
);
255 BATCH_LOCALS(&context
->radeon
);
256 int type
, total_emit
;
258 uint32_t vgt_draw_initiator
= 0;
259 uint32_t vgt_index_type
= 0;
260 uint32_t vgt_primitive_type
= 0;
261 uint32_t vgt_num_indices
= 0;
263 type
= r700PrimitiveType(prim
);
264 num_indices
= r700NumVerts(end
- start
, prim
);
266 radeon_print(RADEON_RENDER
, RADEON_TRACE
,
267 "%s type %x num_indices %d\n",
268 __func__
, type
, num_indices
);
270 if (type
< 0 || num_indices
<= 0)
273 SETfield(vgt_primitive_type
, type
,
274 VGT_PRIMITIVE_TYPE__PRIM_TYPE_shift
, VGT_PRIMITIVE_TYPE__PRIM_TYPE_mask
);
276 SETfield(vgt_index_type
, DI_INDEX_SIZE_32_BIT
, INDEX_TYPE_shift
, INDEX_TYPE_mask
);
278 if(GL_TRUE
!= context
->ind_buf
.is_32bit
)
280 SETfield(vgt_index_type
, DI_INDEX_SIZE_16_BIT
, INDEX_TYPE_shift
, INDEX_TYPE_mask
);
283 vgt_num_indices
= num_indices
;
284 SETfield(vgt_draw_initiator
, DI_SRC_SEL_DMA
, SOURCE_SELECT_shift
, SOURCE_SELECT_mask
);
285 SETfield(vgt_draw_initiator
, DI_MAJOR_MODE_0
, MAJOR_MODE_shift
, MAJOR_MODE_mask
);
287 total_emit
= 3 /* VGT_PRIMITIVE_TYPE */
288 + 2 /* VGT_INDEX_TYPE */
289 + 2 /* NUM_INSTANCES */
290 + 5 + 2; /* DRAW_INDEX */
292 BEGIN_BATCH_NO_AUTOSTATE(total_emit
);
294 R600_OUT_BATCH_REGSEQ(VGT_PRIMITIVE_TYPE
, 1);
295 R600_OUT_BATCH(vgt_primitive_type
);
297 R600_OUT_BATCH(CP_PACKET3(R600_IT_INDEX_TYPE
, 0));
298 R600_OUT_BATCH(vgt_index_type
);
300 R600_OUT_BATCH(CP_PACKET3(R600_IT_NUM_INSTANCES
, 0));
303 R600_OUT_BATCH(CP_PACKET3(R600_IT_DRAW_INDEX
, 3));
304 R600_OUT_BATCH(context
->ind_buf
.bo_offset
);
306 R600_OUT_BATCH(vgt_num_indices
);
307 R600_OUT_BATCH(vgt_draw_initiator
);
308 R600_OUT_BATCH_RELOC(context
->ind_buf
.bo_offset
,
310 context
->ind_buf
.bo_offset
,
311 RADEON_GEM_DOMAIN_GTT
, 0, 0);
316 static void r700RunRenderPrimitiveImmediate(GLcontext
* ctx
, int start
, int end
, int prim
)
318 context_t
*context
= R700_CONTEXT(ctx
);
319 BATCH_LOCALS(&context
->radeon
);
321 uint32_t num_indices
, total_emit
= 0;
322 uint32_t vgt_draw_initiator
= 0;
323 uint32_t vgt_index_type
= 0;
324 uint32_t vgt_primitive_type
= 0;
325 uint32_t vgt_num_indices
= 0;
327 type
= r700PrimitiveType(prim
);
328 num_indices
= r700NumVerts(end
- start
, prim
);
330 radeon_print(RADEON_RENDER
, RADEON_TRACE
,
331 "%s type %x num_indices %d\n",
332 __func__
, type
, num_indices
);
334 if (type
< 0 || num_indices
<= 0)
337 SETfield(vgt_primitive_type
, type
,
338 VGT_PRIMITIVE_TYPE__PRIM_TYPE_shift
, VGT_PRIMITIVE_TYPE__PRIM_TYPE_mask
);
340 if (num_indices
> 0xffff)
342 SETfield(vgt_index_type
, DI_INDEX_SIZE_32_BIT
, INDEX_TYPE_shift
, INDEX_TYPE_mask
);
346 SETfield(vgt_index_type
, DI_INDEX_SIZE_16_BIT
, INDEX_TYPE_shift
, INDEX_TYPE_mask
);
349 vgt_num_indices
= num_indices
;
350 SETfield(vgt_draw_initiator
, DI_MAJOR_MODE_0
, MAJOR_MODE_shift
, MAJOR_MODE_mask
);
354 SETfield(vgt_draw_initiator
, DI_SRC_SEL_AUTO_INDEX
, SOURCE_SELECT_shift
, SOURCE_SELECT_mask
);
358 if (num_indices
> 0xffff)
360 total_emit
+= num_indices
;
364 total_emit
+= (num_indices
+ 1) / 2;
366 SETfield(vgt_draw_initiator
, DI_SRC_SEL_IMMEDIATE
, SOURCE_SELECT_shift
, SOURCE_SELECT_mask
);
369 total_emit
+= 3 /* VGT_PRIMITIVE_TYPE */
370 + 2 /* VGT_INDEX_TYPE */
371 + 2 /* NUM_INSTANCES */
374 BEGIN_BATCH_NO_AUTOSTATE(total_emit
);
376 R600_OUT_BATCH_REGSEQ(VGT_PRIMITIVE_TYPE
, 1);
377 R600_OUT_BATCH(vgt_primitive_type
);
379 R600_OUT_BATCH(CP_PACKET3(R600_IT_INDEX_TYPE
, 0));
380 R600_OUT_BATCH(vgt_index_type
);
382 R600_OUT_BATCH(CP_PACKET3(R600_IT_NUM_INSTANCES
, 0));
387 R600_OUT_BATCH(CP_PACKET3(R600_IT_DRAW_INDEX_AUTO
, 1));
388 R600_OUT_BATCH(vgt_num_indices
);
389 R600_OUT_BATCH(vgt_draw_initiator
);
393 if (num_indices
> 0xffff)
395 R600_OUT_BATCH(CP_PACKET3(R600_IT_DRAW_INDEX_IMMD
, (num_indices
+ 1)));
396 R600_OUT_BATCH(vgt_num_indices
);
397 R600_OUT_BATCH(vgt_draw_initiator
);
398 for (i
= start
; i
< (start
+ num_indices
); i
++)
405 R600_OUT_BATCH(CP_PACKET3(R600_IT_DRAW_INDEX_IMMD
, (((num_indices
+ 1) / 2) + 1)));
406 R600_OUT_BATCH(vgt_num_indices
);
407 R600_OUT_BATCH(vgt_draw_initiator
);
408 for (i
= start
; i
< (start
+ num_indices
); i
+= 2)
410 if ((i
+ 1) == (start
+ num_indices
))
416 R600_OUT_BATCH(((i
+ 1) << 16) | (i
));
426 /* start 3d, idle, cb/db flush */
427 #define PRE_EMIT_STATE_BUFSZ 10 + 5 + 14
429 static GLuint
r700PredictRenderSize(GLcontext
* ctx
,
430 const struct _mesa_prim
*prim
,
431 const struct _mesa_index_buffer
*ib
,
434 context_t
*context
= R700_CONTEXT(ctx
);
439 dwords
= PRE_EMIT_STATE_BUFSZ
;
441 dwords
+= nr_prims
* 14;
443 for (i
= 0; i
< nr_prims
; ++i
)
445 if (prim
[i
].start
== 0)
447 else if (prim
[i
].count
> 0xffff)
448 dwords
+= prim
[i
].count
+ 10;
450 dwords
+= ((prim
[i
].count
+ 1) / 2) + 10;
454 state_size
= radeonCountStateEmitSize(&context
->radeon
);
455 flushed
= rcommonEnsureCmdBufSpace(&context
->radeon
,
459 dwords
+= radeonCountStateEmitSize(&context
->radeon
);
461 dwords
+= state_size
;
463 radeon_print(RADEON_RENDER
, RADEON_VERBOSE
, "%s: total prediction size is %d.\n", __FUNCTION__
, dwords
);
468 #define CONVERT( TYPE, MACRO ) do { \
471 if (input->Normalized) { \
472 for (i = 0; i < count; i++) { \
473 const TYPE *in = (TYPE *)src_ptr; \
474 for (j = 0; j < sz; j++) { \
475 *dst_ptr++ = MACRO(*in); \
481 for (i = 0; i < count; i++) { \
482 const TYPE *in = (TYPE *)src_ptr; \
483 for (j = 0; j < sz; j++) { \
484 *dst_ptr++ = (GLfloat)(*in); \
493 * Convert attribute data type to float
494 * If the attribute uses named buffer object replace the bo with newly allocated bo
496 static void r700ConvertAttrib(GLcontext
*ctx
, int count
,
497 const struct gl_client_array
*input
,
498 struct StreamDesc
*attr
)
500 context_t
*context
= R700_CONTEXT(ctx
);
501 const GLvoid
*src_ptr
;
502 GLboolean mapped_named_bo
= GL_FALSE
;
506 stride
= (input
->StrideB
== 0) ? getTypeSize(input
->Type
) * input
->Size
: input
->StrideB
;
508 /* Convert value for first element only */
509 if (input
->StrideB
== 0)
514 if (input
->BufferObj
->Name
)
516 if (!input
->BufferObj
->Pointer
)
518 ctx
->Driver
.MapBuffer(ctx
, GL_ARRAY_BUFFER
, GL_READ_ONLY_ARB
, input
->BufferObj
);
519 mapped_named_bo
= GL_TRUE
;
522 src_ptr
= ADD_POINTERS(input
->BufferObj
->Pointer
, input
->Ptr
);
526 src_ptr
= input
->Ptr
;
529 radeonAllocDmaRegion(&context
->radeon
, &attr
->bo
, &attr
->bo_offset
,
530 sizeof(GLfloat
) * input
->Size
* count
, 32);
531 dst_ptr
= (GLfloat
*)ADD_POINTERS(attr
->bo
->ptr
, attr
->bo_offset
);
533 assert(src_ptr
!= NULL
);
538 CONVERT(GLdouble
, (GLfloat
));
540 case GL_UNSIGNED_INT
:
541 CONVERT(GLuint
, UINT_TO_FLOAT
);
544 CONVERT(GLint
, INT_TO_FLOAT
);
546 case GL_UNSIGNED_SHORT
:
547 CONVERT(GLushort
, USHORT_TO_FLOAT
);
550 CONVERT(GLshort
, SHORT_TO_FLOAT
);
552 case GL_UNSIGNED_BYTE
:
553 assert(input
->Format
!= GL_BGRA
);
554 CONVERT(GLubyte
, UBYTE_TO_FLOAT
);
557 CONVERT(GLbyte
, BYTE_TO_FLOAT
);
566 ctx
->Driver
.UnmapBuffer(ctx
, GL_ARRAY_BUFFER
, input
->BufferObj
);
570 static void r700AlignDataToDword(GLcontext
*ctx
,
571 const struct gl_client_array
*input
,
573 struct StreamDesc
*attr
)
575 context_t
*context
= R700_CONTEXT(ctx
);
576 const int dst_stride
= (input
->StrideB
+ 3) & ~3;
577 const int size
= getTypeSize(input
->Type
) * input
->Size
* count
;
578 GLboolean mapped_named_bo
= GL_FALSE
;
580 radeonAllocDmaRegion(&context
->radeon
, &attr
->bo
, &attr
->bo_offset
, size
, 32);
582 if (!input
->BufferObj
->Pointer
)
584 ctx
->Driver
.MapBuffer(ctx
, GL_ARRAY_BUFFER
, GL_READ_ONLY_ARB
, input
->BufferObj
);
585 mapped_named_bo
= GL_TRUE
;
589 GLvoid
*src_ptr
= ADD_POINTERS(input
->BufferObj
->Pointer
, input
->Ptr
);
590 GLvoid
*dst_ptr
= ADD_POINTERS(attr
->bo
->ptr
, attr
->bo_offset
);
593 for (i
= 0; i
< count
; ++i
)
595 _mesa_memcpy(dst_ptr
, src_ptr
, input
->StrideB
);
596 src_ptr
+= input
->StrideB
;
597 dst_ptr
+= dst_stride
;
603 ctx
->Driver
.UnmapBuffer(ctx
, GL_ARRAY_BUFFER
, input
->BufferObj
);
606 attr
->stride
= dst_stride
;
609 static void r700SetupStreams(GLcontext
*ctx
, const struct gl_client_array
*input
[], int count
)
611 context_t
*context
= R700_CONTEXT(ctx
);
616 R600_STATECHANGE(context
, vtx
);
618 for(index
= 0; index
< context
->nNumActiveAos
; index
++)
620 struct radeon_aos
*aos
= &context
->radeon
.tcl
.aos
[index
];
621 i
= context
->stream_desc
[index
].element
;
623 stride
= (input
[i
]->StrideB
== 0) ? getTypeSize(input
[i
]->Type
) * input
[i
]->Size
: input
[i
]->StrideB
;
625 if (input
[i
]->Type
== GL_DOUBLE
|| input
[i
]->Type
== GL_UNSIGNED_INT
|| input
[i
]->Type
== GL_INT
||
627 getTypeSize(input
[i
]->Type
) != 4 ||
631 r700ConvertAttrib(ctx
, count
, input
[i
], &context
->stream_desc
[index
]);
635 if (input
[i
]->BufferObj
->Name
)
639 assert(((intptr_t) input
[i
]->Ptr
) % input
[i
]->StrideB
== 0);
640 r700AlignDataToDword(ctx
, input
[i
], count
, &context
->stream_desc
[index
]);
641 context
->stream_desc
[index
].is_named_bo
= GL_FALSE
;
645 context
->stream_desc
[index
].stride
= input
[i
]->StrideB
;
646 context
->stream_desc
[index
].bo_offset
= (intptr_t) input
[i
]->Ptr
;
647 context
->stream_desc
[index
].bo
= get_radeon_buffer_object(input
[i
]->BufferObj
)->bo
;
648 context
->stream_desc
[index
].is_named_bo
= GL_TRUE
;
654 int local_count
= count
;
657 if (input
[i
]->StrideB
== 0)
659 size
= getTypeSize(input
[i
]->Type
) * input
[i
]->Size
;
664 size
= getTypeSize(input
[i
]->Type
) * input
[i
]->Size
* local_count
;
667 radeonAllocDmaRegion(&context
->radeon
, &context
->stream_desc
[index
].bo
,
668 &context
->stream_desc
[index
].bo_offset
, size
, 32);
669 assert(context
->stream_desc
[index
].bo
->ptr
!= NULL
);
670 dst
= (uint32_t *)ADD_POINTERS(context
->stream_desc
[index
].bo
->ptr
,
671 context
->stream_desc
[index
].bo_offset
);
673 switch (context
->stream_desc
[index
].dwords
)
676 radeonEmitVec4(dst
, input
[i
]->Ptr
, input
[i
]->StrideB
, local_count
);
679 radeonEmitVec8(dst
, input
[i
]->Ptr
, input
[i
]->StrideB
, local_count
);
682 radeonEmitVec12(dst
, input
[i
]->Ptr
, input
[i
]->StrideB
, local_count
);
685 radeonEmitVec16(dst
, input
[i
]->Ptr
, input
[i
]->StrideB
, local_count
);
694 aos
->count
= context
->stream_desc
[index
].stride
== 0 ? 1 : count
;
695 aos
->stride
= context
->stream_desc
[index
].stride
/ sizeof(float);
696 aos
->components
= context
->stream_desc
[index
].dwords
;
697 aos
->bo
= context
->stream_desc
[index
].bo
;
698 aos
->offset
= context
->stream_desc
[index
].bo_offset
;
700 if(context
->stream_desc
[index
].is_named_bo
)
702 radeon_cs_space_add_persistent_bo(context
->radeon
.cmdbuf
.cs
,
703 context
->stream_desc
[index
].bo
,
704 RADEON_GEM_DOMAIN_GTT
, 0);
708 ret
= radeon_cs_space_check_with_bo(context
->radeon
.cmdbuf
.cs
,
709 first_elem(&context
->radeon
.dma
.reserved
)->bo
,
710 RADEON_GEM_DOMAIN_GTT
, 0);
713 static void r700FreeData(GLcontext
*ctx
)
715 /* Need to zero tcl.aos[n].bo and tcl.elt_dma_bo
716 * to prevent double unref in radeonReleaseArrays
717 * called during context destroy
719 context_t
*context
= R700_CONTEXT(ctx
);
723 for (i
= 0; i
< context
->nNumActiveAos
; i
++)
725 if (!context
->stream_desc
[i
].is_named_bo
)
727 radeon_bo_unref(context
->stream_desc
[i
].bo
);
729 context
->radeon
.tcl
.aos
[i
].bo
= NULL
;
732 if (context
->ind_buf
.bo
!= NULL
)
734 radeon_bo_unref(context
->ind_buf
.bo
);
738 static void r700FixupIndexBuffer(GLcontext
*ctx
, const struct _mesa_index_buffer
*mesa_ind_buf
)
740 context_t
*context
= R700_CONTEXT(ctx
);
744 GLboolean mapped_named_bo
= GL_FALSE
;
746 if (mesa_ind_buf
->obj
->Name
&& !mesa_ind_buf
->obj
->Pointer
)
748 ctx
->Driver
.MapBuffer(ctx
, GL_ELEMENT_ARRAY_BUFFER
, GL_READ_ONLY_ARB
, mesa_ind_buf
->obj
);
749 mapped_named_bo
= GL_TRUE
;
750 assert(mesa_ind_buf
->obj
->Pointer
!= NULL
);
752 src_ptr
= ADD_POINTERS(mesa_ind_buf
->obj
->Pointer
, mesa_ind_buf
->ptr
);
754 if (mesa_ind_buf
->type
== GL_UNSIGNED_BYTE
)
756 GLuint size
= sizeof(GLushort
) * ((mesa_ind_buf
->count
+ 1) & ~1);
757 GLubyte
*in
= (GLubyte
*)src_ptr
;
759 radeonAllocDmaRegion(&context
->radeon
, &context
->ind_buf
.bo
,
760 &context
->ind_buf
.bo_offset
, size
, 4);
762 assert(context
->ind_buf
.bo
->ptr
!= NULL
);
763 out
= (GLuint
*)ADD_POINTERS(context
->ind_buf
.bo
->ptr
, context
->ind_buf
.bo_offset
);
765 for (i
= 0; i
+ 1 < mesa_ind_buf
->count
; i
+= 2)
767 *out
++ = in
[i
] | in
[i
+ 1] << 16;
770 if (i
< mesa_ind_buf
->count
)
778 { /* if (mesa_ind_buf->type == GL_UNSIGNED_SHORT) */
779 GLushort
*in
= (GLushort
*)src_ptr
;
780 GLuint size
= sizeof(GLushort
) * ((mesa_ind_buf
->count
+ 1) & ~1);
782 radeonAllocDmaRegion(&context
->radeon
, &context
->ind_buf
.bo
,
783 &context
->ind_buf
.bo_offset
, size
, 4);
785 assert(context
->ind_buf
.bo
->ptr
!= NULL
);
786 out
= (GLuint
*)ADD_POINTERS(context
->ind_buf
.bo
->ptr
, context
->ind_buf
.bo_offset
);
788 for (i
= 0; i
+ 1 < mesa_ind_buf
->count
; i
+= 2)
790 *out
++ = in
[i
] | in
[i
+ 1] << 16;
793 if (i
< mesa_ind_buf
->count
)
800 context
->ind_buf
.is_32bit
= GL_FALSE
;
801 context
->ind_buf
.count
= mesa_ind_buf
->count
;
805 ctx
->Driver
.UnmapBuffer(ctx
, GL_ELEMENT_ARRAY_BUFFER
, mesa_ind_buf
->obj
);
809 static void r700SetupIndexBuffer(GLcontext
*ctx
, const struct _mesa_index_buffer
*mesa_ind_buf
)
811 context_t
*context
= R700_CONTEXT(ctx
);
814 context
->ind_buf
.bo
= NULL
;
819 if (mesa_ind_buf
->type
== GL_UNSIGNED_INT
)
822 if (mesa_ind_buf
->type
!= GL_UNSIGNED_BYTE
)
825 const GLvoid
*src_ptr
;
827 GLboolean mapped_named_bo
= GL_FALSE
;
829 if (mesa_ind_buf
->obj
->Name
&& !mesa_ind_buf
->obj
->Pointer
)
831 ctx
->Driver
.MapBuffer(ctx
, GL_ELEMENT_ARRAY_BUFFER
, GL_READ_ONLY_ARB
, mesa_ind_buf
->obj
);
832 assert(mesa_ind_buf
->obj
->Pointer
!= NULL
);
833 mapped_named_bo
= GL_TRUE
;
836 src_ptr
= ADD_POINTERS(mesa_ind_buf
->obj
->Pointer
, mesa_ind_buf
->ptr
);
838 const GLuint size
= mesa_ind_buf
->count
* getTypeSize(mesa_ind_buf
->type
);
840 radeonAllocDmaRegion(&context
->radeon
, &context
->ind_buf
.bo
,
841 &context
->ind_buf
.bo_offset
, size
, 4);
842 assert(context
->ind_buf
.bo
->ptr
!= NULL
);
843 dst_ptr
= ADD_POINTERS(context
->ind_buf
.bo
->ptr
, context
->ind_buf
.bo_offset
);
845 _mesa_memcpy(dst_ptr
, src_ptr
, size
);
847 context
->ind_buf
.is_32bit
= (mesa_ind_buf
->type
== GL_UNSIGNED_INT
);
848 context
->ind_buf
.count
= mesa_ind_buf
->count
;
852 ctx
->Driver
.UnmapBuffer(ctx
, GL_ELEMENT_ARRAY_BUFFER
, mesa_ind_buf
->obj
);
857 r700FixupIndexBuffer(ctx
, mesa_ind_buf
);
861 static GLboolean
r700TryDrawPrims(GLcontext
*ctx
,
862 const struct gl_client_array
*arrays
[],
863 const struct _mesa_prim
*prim
,
865 const struct _mesa_index_buffer
*ib
,
869 context_t
*context
= R700_CONTEXT(ctx
);
870 radeonContextPtr radeon
= &context
->radeon
;
872 struct radeon_renderbuffer
*rrb
;
875 _mesa_update_state( ctx
);
877 _tnl_UpdateFixedFunctionProgram(ctx
);
878 r700SetVertexFormat(ctx
, arrays
, max_index
+ 1);
879 /* shaders need to be updated before buffers are validated */
880 r700UpdateShaders(ctx
);
881 if (!r600ValidateBuffers(ctx
))
884 /* always emit CB base to prevent
885 * lock ups on some chips.
887 R600_STATECHANGE(context
, cb_target
);
888 /* mark vtx as dirty since it changes per-draw */
889 R600_STATECHANGE(context
, vtx
);
891 r700SetScissor(context
);
892 r700SetupVertexProgram(ctx
);
893 r700SetupFragmentProgram(ctx
);
894 r600UpdateTextureState(ctx
);
896 GLuint emit_end
= r700PredictRenderSize(ctx
, prim
, ib
, nr_prims
)
897 + context
->radeon
.cmdbuf
.cs
->cdw
;
899 r700SetupIndexBuffer(ctx
, ib
);
900 r700SetupStreams(ctx
, arrays
, max_index
+ 1);
902 radeonEmitState(radeon
);
904 radeon_debug_add_indent();
905 for (i
= 0; i
< nr_prims
; ++i
)
907 if (context
->ind_buf
.bo
)
908 r700RunRenderPrimitive(ctx
,
910 prim
[i
].start
+ prim
[i
].count
,
913 r700RunRenderPrimitiveImmediate(ctx
,
915 prim
[i
].start
+ prim
[i
].count
,
918 radeon_debug_remove_indent();
920 /* Flush render op cached for last several quads. */
921 r700WaitForIdleClean(context
);
923 rrb
= radeon_get_colorbuffer(&context
->radeon
);
925 r700SyncSurf(context
, rrb
->bo
, 0, RADEON_GEM_DOMAIN_VRAM
,
926 CB_ACTION_ENA_bit
| (1 << (id
+ 6)));
928 rrb
= radeon_get_depthbuffer(&context
->radeon
);
930 r700SyncSurf(context
, rrb
->bo
, 0, RADEON_GEM_DOMAIN_VRAM
,
931 DB_ACTION_ENA_bit
| DB_DEST_BASE_ENA_bit
);
935 if (emit_end
< context
->radeon
.cmdbuf
.cs
->cdw
)
937 WARN_ONCE("Rendering was %d commands larger than predicted size."
938 " We might overflow command buffer.\n", context
->radeon
.cmdbuf
.cs
->cdw
- emit_end
);
944 static void r700DrawPrims(GLcontext
*ctx
,
945 const struct gl_client_array
*arrays
[],
946 const struct _mesa_prim
*prim
,
948 const struct _mesa_index_buffer
*ib
,
949 GLboolean index_bounds_valid
,
953 GLboolean retval
= GL_FALSE
;
955 /* This check should get folded into just the places that
956 * min/max index are really needed.
958 if (!index_bounds_valid
) {
959 vbo_get_minmax_index(ctx
, prim
, ib
, &min_index
, &max_index
);
963 vbo_rebase_prims( ctx
, arrays
, prim
, nr_prims
, ib
, min_index
, max_index
, r700DrawPrims
);
967 /* Make an attempt at drawing */
968 retval
= r700TryDrawPrims(ctx
, arrays
, prim
, nr_prims
, ib
, min_index
, max_index
);
970 /* If failed run tnl pipeline - it should take care of fallbacks */
972 _tnl_draw_prims(ctx
, arrays
, prim
, nr_prims
, ib
, min_index
, max_index
);
975 void r700InitDraw(GLcontext
*ctx
)
977 struct vbo_context
*vbo
= vbo_context(ctx
);
980 vbo
->draw_prims
= r700DrawPrims
;