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 "vbo/vbo_context.h"
47 #include "r600_context.h"
48 #include "r600_cmdbuf.h"
52 #include "r700_vertprog.h"
53 #include "r700_fragprog.h"
54 #include "r700_state.h"
56 #include "radeon_buffer_objects.h"
57 #include "radeon_common_context.h"
59 void r700WaitForIdle(context_t
*context
);
60 void r700WaitForIdleClean(context_t
*context
);
61 static unsigned int r700PrimitiveType(int prim
);
62 GLboolean
r700SyncSurf(context_t
*context
,
63 struct radeon_bo
*pbo
,
65 uint32_t write_domain
,
68 void r700WaitForIdle(context_t
*context
)
70 BATCH_LOCALS(&context
->radeon
);
71 radeon_print(RADEON_RENDER
| RADEON_STATE
, RADEON_TRACE
, "%s\n", __func__
);
72 BEGIN_BATCH_NO_AUTOSTATE(3);
74 R600_OUT_BATCH(CP_PACKET3(R600_IT_SET_CONFIG_REG
, 1));
75 R600_OUT_BATCH(mmWAIT_UNTIL
- ASIC_CONFIG_BASE_INDEX
);
76 R600_OUT_BATCH(WAIT_3D_IDLE_bit
);
82 void r700WaitForIdleClean(context_t
*context
)
84 BATCH_LOCALS(&context
->radeon
);
85 radeon_print(RADEON_RENDER
| RADEON_STATE
, RADEON_TRACE
, "%s\n", __func__
);
86 BEGIN_BATCH_NO_AUTOSTATE(5);
88 R600_OUT_BATCH(CP_PACKET3(R600_IT_EVENT_WRITE
, 0));
89 R600_OUT_BATCH(CACHE_FLUSH_AND_INV_EVENT
);
91 R600_OUT_BATCH(CP_PACKET3(R600_IT_SET_CONFIG_REG
, 1));
92 R600_OUT_BATCH(mmWAIT_UNTIL
- ASIC_CONFIG_BASE_INDEX
);
93 R600_OUT_BATCH(WAIT_3D_IDLE_bit
| WAIT_3D_IDLECLEAN_bit
);
99 void r700Start3D(context_t
*context
)
101 BATCH_LOCALS(&context
->radeon
);
102 radeon_print(RADEON_RENDER
| RADEON_STATE
, RADEON_TRACE
, "%s\n", __func__
);
103 if (context
->radeon
.radeonScreen
->chip_family
< CHIP_FAMILY_RV770
)
105 BEGIN_BATCH_NO_AUTOSTATE(2);
106 R600_OUT_BATCH(CP_PACKET3(R600_IT_START_3D_CMDBUF
, 0));
111 BEGIN_BATCH_NO_AUTOSTATE(3);
112 R600_OUT_BATCH(CP_PACKET3(R600_IT_CONTEXT_CONTROL
, 1));
113 R600_OUT_BATCH(0x80000000);
114 R600_OUT_BATCH(0x80000000);
120 GLboolean
r700SyncSurf(context_t
*context
,
121 struct radeon_bo
*pbo
,
122 uint32_t read_domain
,
123 uint32_t write_domain
,
126 BATCH_LOCALS(&context
->radeon
);
127 radeon_print(RADEON_RENDER
| RADEON_STATE
, RADEON_TRACE
, "%s\n", __func__
);
128 uint32_t cp_coher_size
;
133 if (pbo
->size
== 0xffffffff)
134 cp_coher_size
= 0xffffffff;
136 cp_coher_size
= ((pbo
->size
+ 255) >> 8);
138 BEGIN_BATCH_NO_AUTOSTATE(5 + 2);
139 R600_OUT_BATCH(CP_PACKET3(R600_IT_SURFACE_SYNC
, 3));
140 R600_OUT_BATCH(sync_type
);
141 R600_OUT_BATCH(cp_coher_size
);
144 R600_OUT_BATCH_RELOC(0,
147 read_domain
, write_domain
, 0);
154 static unsigned int r700PrimitiveType(int prim
)
156 switch (prim
& PRIM_MODE_MASK
)
159 return DI_PT_POINTLIST
;
162 return DI_PT_LINELIST
;
165 return DI_PT_LINESTRIP
;
168 return DI_PT_LINELOOP
;
171 return DI_PT_TRILIST
;
173 case GL_TRIANGLE_STRIP
:
174 return DI_PT_TRISTRIP
;
176 case GL_TRIANGLE_FAN
:
180 return DI_PT_QUADLIST
;
183 return DI_PT_QUADSTRIP
;
186 return DI_PT_POLYGON
;
195 static int r700NumVerts(int num_verts
, int prim
)
199 switch (prim
& PRIM_MODE_MASK
) {
204 verts_off
= num_verts
% 2;
208 verts_off
= num_verts
;
212 verts_off
= num_verts
;
215 verts_off
= num_verts
% 3;
217 case GL_TRIANGLE_STRIP
:
219 verts_off
= num_verts
;
221 case GL_TRIANGLE_FAN
:
223 verts_off
= num_verts
;
226 verts_off
= num_verts
% 4;
230 verts_off
= num_verts
;
232 verts_off
= num_verts
% 2;
236 verts_off
= num_verts
;
244 return num_verts
- verts_off
;
247 static void r700RunRenderPrimitive(struct gl_context
* ctx
, int start
, int end
,
248 int prim
, GLint basevertex
)
250 context_t
*context
= R700_CONTEXT(ctx
);
251 BATCH_LOCALS(&context
->radeon
);
252 int type
, total_emit
;
254 uint32_t vgt_draw_initiator
= 0;
255 uint32_t vgt_index_type
= 0;
256 uint32_t vgt_primitive_type
= 0;
257 uint32_t vgt_num_indices
= 0;
259 type
= r700PrimitiveType(prim
);
260 num_indices
= r700NumVerts(end
- start
, prim
);
262 radeon_print(RADEON_RENDER
, RADEON_TRACE
,
263 "%s type %x num_indices %d\n",
264 __func__
, type
, num_indices
);
266 if (type
< 0 || num_indices
<= 0)
269 SETfield(vgt_primitive_type
, type
,
270 VGT_PRIMITIVE_TYPE__PRIM_TYPE_shift
, VGT_PRIMITIVE_TYPE__PRIM_TYPE_mask
);
272 SETfield(vgt_index_type
, DI_INDEX_SIZE_32_BIT
, INDEX_TYPE_shift
, INDEX_TYPE_mask
);
274 if(GL_TRUE
!= context
->ind_buf
.is_32bit
)
276 SETfield(vgt_index_type
, DI_INDEX_SIZE_16_BIT
, INDEX_TYPE_shift
, INDEX_TYPE_mask
);
279 /* 16-bit indexes are packed in a 32-bit value */
280 SETfield(vgt_index_type
,
286 SWAP_MODE_shift
, SWAP_MODE_mask
);
289 vgt_num_indices
= num_indices
;
290 SETfield(vgt_draw_initiator
, DI_SRC_SEL_DMA
, SOURCE_SELECT_shift
, SOURCE_SELECT_mask
);
291 SETfield(vgt_draw_initiator
, DI_MAJOR_MODE_0
, MAJOR_MODE_shift
, MAJOR_MODE_mask
);
293 total_emit
= 3 /* VGT_PRIMITIVE_TYPE */
294 + 2 /* VGT_INDEX_TYPE */
295 + 2 /* NUM_INSTANCES */
296 + 4 /* VTX_BASE_VTX_LOC + VTX_START_INST_LOC */
297 + 5 + 2; /* DRAW_INDEX */
299 BEGIN_BATCH_NO_AUTOSTATE(total_emit
);
301 R600_OUT_BATCH_REGSEQ(VGT_PRIMITIVE_TYPE
, 1);
302 R600_OUT_BATCH(vgt_primitive_type
);
304 R600_OUT_BATCH(CP_PACKET3(R600_IT_INDEX_TYPE
, 0));
305 R600_OUT_BATCH(vgt_index_type
);
307 R600_OUT_BATCH(CP_PACKET3(R600_IT_NUM_INSTANCES
, 0));
310 R600_OUT_BATCH(CP_PACKET3(R600_IT_SET_CTL_CONST
, 2));
311 R600_OUT_BATCH(mmSQ_VTX_BASE_VTX_LOC
- ASIC_CTL_CONST_BASE_INDEX
);
312 R600_OUT_BATCH(basevertex
); //VTX_BASE_VTX_LOC
313 R600_OUT_BATCH(0); //VTX_START_INST_LOC
315 R600_OUT_BATCH(CP_PACKET3(R600_IT_DRAW_INDEX
, 3));
316 R600_OUT_BATCH(context
->ind_buf
.bo_offset
);
318 R600_OUT_BATCH(vgt_num_indices
);
319 R600_OUT_BATCH(vgt_draw_initiator
);
320 R600_OUT_BATCH_RELOC(context
->ind_buf
.bo_offset
,
322 context
->ind_buf
.bo_offset
,
323 RADEON_GEM_DOMAIN_GTT
, 0, 0);
328 static void r700RunRenderPrimitiveImmediate(struct gl_context
* ctx
, int start
, int end
, int prim
)
330 context_t
*context
= R700_CONTEXT(ctx
);
331 BATCH_LOCALS(&context
->radeon
);
333 uint32_t num_indices
, total_emit
= 0;
334 uint32_t vgt_draw_initiator
= 0;
335 uint32_t vgt_index_type
= 0;
336 uint32_t vgt_primitive_type
= 0;
337 uint32_t vgt_num_indices
= 0;
339 type
= r700PrimitiveType(prim
);
340 num_indices
= r700NumVerts(end
- start
, prim
);
342 radeon_print(RADEON_RENDER
, RADEON_TRACE
,
343 "%s type %x num_indices %d\n",
344 __func__
, type
, num_indices
);
346 if (type
< 0 || num_indices
<= 0)
349 SETfield(vgt_primitive_type
, type
,
350 VGT_PRIMITIVE_TYPE__PRIM_TYPE_shift
, VGT_PRIMITIVE_TYPE__PRIM_TYPE_mask
);
352 if (num_indices
> 0xffff)
354 SETfield(vgt_index_type
, DI_INDEX_SIZE_32_BIT
, INDEX_TYPE_shift
, INDEX_TYPE_mask
);
358 SETfield(vgt_index_type
, DI_INDEX_SIZE_16_BIT
, INDEX_TYPE_shift
, INDEX_TYPE_mask
);
361 /* 16-bit indexes are packed in a 32-bit value */
362 SETfield(vgt_index_type
,
368 SWAP_MODE_shift
, SWAP_MODE_mask
);
370 vgt_num_indices
= num_indices
;
371 SETfield(vgt_draw_initiator
, DI_MAJOR_MODE_0
, MAJOR_MODE_shift
, MAJOR_MODE_mask
);
373 SETfield(vgt_draw_initiator
, DI_SRC_SEL_AUTO_INDEX
, SOURCE_SELECT_shift
, SOURCE_SELECT_mask
);
375 total_emit
+= 3 /* VGT_PRIMITIVE_TYPE */
376 + 2 /* VGT_INDEX_TYPE */
377 + 2 /* NUM_INSTANCES */
378 + 4 /* VTX_BASE_VTX_LOC + VTX_START_INST_LOC */
381 BEGIN_BATCH_NO_AUTOSTATE(total_emit
);
383 R600_OUT_BATCH_REGSEQ(VGT_PRIMITIVE_TYPE
, 1);
384 R600_OUT_BATCH(vgt_primitive_type
);
386 R600_OUT_BATCH(CP_PACKET3(R600_IT_INDEX_TYPE
, 0));
387 R600_OUT_BATCH(vgt_index_type
);
389 R600_OUT_BATCH(CP_PACKET3(R600_IT_NUM_INSTANCES
, 0));
392 R600_OUT_BATCH(CP_PACKET3(R600_IT_SET_CTL_CONST
, 2));
393 R600_OUT_BATCH(mmSQ_VTX_BASE_VTX_LOC
- ASIC_CTL_CONST_BASE_INDEX
);
394 R600_OUT_BATCH(start
); //VTX_BASE_VTX_LOC
395 R600_OUT_BATCH(0); //VTX_START_INST_LOC
398 R600_OUT_BATCH(CP_PACKET3(R600_IT_DRAW_INDEX_AUTO
, 1));
399 R600_OUT_BATCH(vgt_num_indices
);
400 R600_OUT_BATCH(vgt_draw_initiator
);
406 /* start 3d, idle, cb/db flush */
407 #define PRE_EMIT_STATE_BUFSZ 5 + 5 + 14
409 static GLuint
r700PredictRenderSize(struct gl_context
* ctx
,
410 const struct _mesa_prim
*prim
,
411 const struct _mesa_index_buffer
*ib
,
414 context_t
*context
= R700_CONTEXT(ctx
);
419 dwords
= PRE_EMIT_STATE_BUFSZ
;
421 dwords
+= nr_prims
* 18;
423 for (i
= 0; i
< nr_prims
; ++i
)
429 state_size
= radeonCountStateEmitSize(&context
->radeon
);
430 flushed
= rcommonEnsureCmdBufSpace(&context
->radeon
,
434 dwords
+= radeonCountStateEmitSize(&context
->radeon
);
436 dwords
+= state_size
;
438 radeon_print(RADEON_RENDER
, RADEON_VERBOSE
, "%s: total prediction size is %d.\n", __FUNCTION__
, dwords
);
443 #define CONVERT( TYPE, MACRO ) do { \
446 if (input->Normalized) { \
447 for (i = 0; i < count; i++) { \
448 const TYPE *in = (TYPE *)src_ptr; \
449 for (j = 0; j < sz; j++) { \
450 *dst_ptr++ = MACRO(*in); \
456 for (i = 0; i < count; i++) { \
457 const TYPE *in = (TYPE *)src_ptr; \
458 for (j = 0; j < sz; j++) { \
459 *dst_ptr++ = (GLfloat)(*in); \
468 * Convert attribute data type to float
469 * If the attribute uses named buffer object replace the bo with newly allocated bo
471 static void r700ConvertAttrib(struct gl_context
*ctx
, int count
,
472 const struct gl_client_array
*input
,
473 struct StreamDesc
*attr
)
475 context_t
*context
= R700_CONTEXT(ctx
);
476 const GLvoid
*src_ptr
;
477 GLboolean mapped_named_bo
= GL_FALSE
;
481 stride
= (input
->StrideB
== 0) ? getTypeSize(input
->Type
) * input
->Size
: input
->StrideB
;
483 /* Convert value for first element only */
484 if (input
->StrideB
== 0)
489 if (input
->BufferObj
->Name
)
491 if (!input
->BufferObj
->Pointer
)
493 ctx
->Driver
.MapBuffer(ctx
, GL_ARRAY_BUFFER
, GL_READ_ONLY_ARB
, input
->BufferObj
);
494 mapped_named_bo
= GL_TRUE
;
497 src_ptr
= ADD_POINTERS(input
->BufferObj
->Pointer
, input
->Ptr
);
501 src_ptr
= input
->Ptr
;
504 radeonAllocDmaRegion(&context
->radeon
, &attr
->bo
, &attr
->bo_offset
,
505 sizeof(GLfloat
) * input
->Size
* count
, 32);
507 radeon_bo_map(attr
->bo
, 1);
509 dst_ptr
= (GLfloat
*)ADD_POINTERS(attr
->bo
->ptr
, attr
->bo_offset
);
511 assert(src_ptr
!= NULL
);
516 CONVERT(GLdouble
, (GLfloat
));
518 case GL_UNSIGNED_INT
:
519 CONVERT(GLuint
, UINT_TO_FLOAT
);
522 CONVERT(GLint
, INT_TO_FLOAT
);
524 case GL_UNSIGNED_SHORT
:
525 CONVERT(GLushort
, USHORT_TO_FLOAT
);
528 CONVERT(GLshort
, SHORT_TO_FLOAT
);
530 case GL_UNSIGNED_BYTE
:
531 assert(input
->Format
!= GL_BGRA
);
532 CONVERT(GLubyte
, UBYTE_TO_FLOAT
);
535 CONVERT(GLbyte
, BYTE_TO_FLOAT
);
542 radeon_bo_unmap(attr
->bo
);
546 ctx
->Driver
.UnmapBuffer(ctx
, GL_ARRAY_BUFFER
, input
->BufferObj
);
551 static void r700AlignDataToDword(struct gl_context
*ctx
,
552 const struct gl_client_array
*input
,
554 struct StreamDesc
*attr
)
556 context_t
*context
= R700_CONTEXT(ctx
);
557 const int dst_stride
= (input
->StrideB
+ 3) & ~3;
558 const int size
= getTypeSize(input
->Type
) * input
->Size
* count
;
559 GLboolean mapped_named_bo
= GL_FALSE
;
561 radeonAllocDmaRegion(&context
->radeon
, &attr
->bo
, &attr
->bo_offset
, size
, 32);
563 radeon_bo_map(attr
->bo
, 1);
565 if (!input
->BufferObj
->Pointer
)
567 ctx
->Driver
.MapBuffer(ctx
, GL_ARRAY_BUFFER
, GL_READ_ONLY_ARB
, input
->BufferObj
);
568 mapped_named_bo
= GL_TRUE
;
572 GLvoid
*src_ptr
= ADD_POINTERS(input
->BufferObj
->Pointer
, input
->Ptr
);
573 GLvoid
*dst_ptr
= ADD_POINTERS(attr
->bo
->ptr
, attr
->bo_offset
);
576 for (i
= 0; i
< count
; ++i
)
578 memcpy(dst_ptr
, src_ptr
, input
->StrideB
);
579 src_ptr
+= input
->StrideB
;
580 dst_ptr
+= dst_stride
;
584 radeon_bo_unmap(attr
->bo
);
587 ctx
->Driver
.UnmapBuffer(ctx
, GL_ARRAY_BUFFER
, input
->BufferObj
);
590 attr
->stride
= dst_stride
;
594 static void r700SetupStreams(struct gl_context
*ctx
, const struct gl_client_array
*input
[], int count
)
596 context_t
*context
= R700_CONTEXT(ctx
);
601 R600_STATECHANGE(context
, vtx
);
603 for(index
= 0; index
< context
->nNumActiveAos
; index
++)
605 struct radeon_aos
*aos
= &context
->radeon
.tcl
.aos
[index
];
606 i
= context
->stream_desc
[index
].element
;
608 stride
= (input
[i
]->StrideB
== 0) ? getTypeSize(input
[i
]->Type
) * input
[i
]->Size
: input
[i
]->StrideB
;
610 if (input
[i
]->Type
== GL_DOUBLE
|| input
[i
]->Type
== GL_UNSIGNED_INT
|| input
[i
]->Type
== GL_INT
612 || getTypeSize(input
[i
]->Type
) != 4
617 r700ConvertAttrib(ctx
, count
, input
[i
], &context
->stream_desc
[index
]);
621 if (input
[i
]->BufferObj
->Name
)
623 context
->stream_desc
[index
].stride
= input
[i
]->StrideB
;
624 context
->stream_desc
[index
].bo_offset
= (intptr_t) input
[i
]->Ptr
;
625 context
->stream_desc
[index
].bo
= get_radeon_buffer_object(input
[i
]->BufferObj
)->bo
;
626 context
->stream_desc
[index
].is_named_bo
= GL_TRUE
;
631 int local_count
= count
;
634 if (input
[i
]->StrideB
== 0)
636 size
= getTypeSize(input
[i
]->Type
) * input
[i
]->Size
;
641 size
= getTypeSize(input
[i
]->Type
) * input
[i
]->Size
* local_count
;
644 radeonAllocDmaRegion(&context
->radeon
, &context
->stream_desc
[index
].bo
,
645 &context
->stream_desc
[index
].bo_offset
, size
, 32);
647 radeon_bo_map(context
->stream_desc
[index
].bo
, 1);
648 assert(context
->stream_desc
[index
].bo
->ptr
!= NULL
);
651 dst
= (uint32_t *)ADD_POINTERS(context
->stream_desc
[index
].bo
->ptr
,
652 context
->stream_desc
[index
].bo_offset
);
654 switch (context
->stream_desc
[index
].dwords
)
657 radeonEmitVec4(dst
, input
[i
]->Ptr
, input
[i
]->StrideB
, local_count
);
660 radeonEmitVec8(dst
, input
[i
]->Ptr
, input
[i
]->StrideB
, local_count
);
663 radeonEmitVec12(dst
, input
[i
]->Ptr
, input
[i
]->StrideB
, local_count
);
666 radeonEmitVec16(dst
, input
[i
]->Ptr
, input
[i
]->StrideB
, local_count
);
672 radeon_bo_unmap(context
->stream_desc
[index
].bo
);
676 aos
->count
= context
->stream_desc
[index
].stride
== 0 ? 1 : count
;
677 aos
->stride
= context
->stream_desc
[index
].stride
/ sizeof(float);
678 aos
->components
= context
->stream_desc
[index
].dwords
;
679 aos
->bo
= context
->stream_desc
[index
].bo
;
680 aos
->offset
= context
->stream_desc
[index
].bo_offset
;
682 if(context
->stream_desc
[index
].is_named_bo
)
684 radeon_cs_space_add_persistent_bo(context
->radeon
.cmdbuf
.cs
,
685 context
->stream_desc
[index
].bo
,
686 RADEON_GEM_DOMAIN_GTT
, 0);
690 ret
= radeon_cs_space_check_with_bo(context
->radeon
.cmdbuf
.cs
,
691 first_elem(&context
->radeon
.dma
.reserved
)->bo
,
692 RADEON_GEM_DOMAIN_GTT
, 0);
695 static void r700FreeData(struct gl_context
*ctx
)
697 /* Need to zero tcl.aos[n].bo and tcl.elt_dma_bo
698 * to prevent double unref in radeonReleaseArrays
699 * called during context destroy
701 context_t
*context
= R700_CONTEXT(ctx
);
705 for (i
= 0; i
< context
->nNumActiveAos
; i
++)
707 if (!context
->stream_desc
[i
].is_named_bo
)
709 radeon_bo_unref(context
->stream_desc
[i
].bo
);
711 context
->radeon
.tcl
.aos
[i
].bo
= NULL
;
714 if (context
->ind_buf
.bo
!= NULL
)
716 radeon_bo_unref(context
->ind_buf
.bo
);
720 static void r700FixupIndexBuffer(struct gl_context
*ctx
, const struct _mesa_index_buffer
*mesa_ind_buf
)
722 context_t
*context
= R700_CONTEXT(ctx
);
726 GLboolean mapped_named_bo
= GL_FALSE
;
728 if (mesa_ind_buf
->obj
->Name
&& !mesa_ind_buf
->obj
->Pointer
)
730 ctx
->Driver
.MapBuffer(ctx
, GL_ELEMENT_ARRAY_BUFFER
, GL_READ_ONLY_ARB
, mesa_ind_buf
->obj
);
731 mapped_named_bo
= GL_TRUE
;
732 assert(mesa_ind_buf
->obj
->Pointer
!= NULL
);
734 src_ptr
= ADD_POINTERS(mesa_ind_buf
->obj
->Pointer
, mesa_ind_buf
->ptr
);
736 if (mesa_ind_buf
->type
== GL_UNSIGNED_BYTE
)
738 GLuint size
= sizeof(GLushort
) * ((mesa_ind_buf
->count
+ 1) & ~1);
739 GLubyte
*in
= (GLubyte
*)src_ptr
;
741 radeonAllocDmaRegion(&context
->radeon
, &context
->ind_buf
.bo
,
742 &context
->ind_buf
.bo_offset
, size
, 4);
744 radeon_bo_map(context
->ind_buf
.bo
, 1);
745 assert(context
->ind_buf
.bo
->ptr
!= NULL
);
746 out
= (GLuint
*)ADD_POINTERS(context
->ind_buf
.bo
->ptr
, context
->ind_buf
.bo_offset
);
748 for (i
= 0; i
+ 1 < mesa_ind_buf
->count
; i
+= 2)
750 *out
++ = in
[i
] | in
[i
+ 1] << 16;
753 if (i
< mesa_ind_buf
->count
)
758 radeon_bo_unmap(context
->ind_buf
.bo
);
762 { /* if (mesa_ind_buf->type == GL_UNSIGNED_SHORT) */
763 GLushort
*in
= (GLushort
*)src_ptr
;
764 GLuint size
= sizeof(GLushort
) * ((mesa_ind_buf
->count
+ 1) & ~1);
766 radeonAllocDmaRegion(&context
->radeon
, &context
->ind_buf
.bo
,
767 &context
->ind_buf
.bo_offset
, size
, 4);
769 radeon_bo_map(context
->ind_buf
.bo
, 1);
770 assert(context
->ind_buf
.bo
->ptr
!= NULL
);
771 out
= (GLuint
*)ADD_POINTERS(context
->ind_buf
.bo
->ptr
, context
->ind_buf
.bo_offset
);
773 for (i
= 0; i
+ 1 < mesa_ind_buf
->count
; i
+= 2)
775 *out
++ = in
[i
] | in
[i
+ 1] << 16;
778 if (i
< mesa_ind_buf
->count
)
782 radeon_bo_unmap(context
->ind_buf
.bo
);
786 context
->ind_buf
.is_32bit
= GL_FALSE
;
787 context
->ind_buf
.count
= mesa_ind_buf
->count
;
791 ctx
->Driver
.UnmapBuffer(ctx
, GL_ELEMENT_ARRAY_BUFFER
, mesa_ind_buf
->obj
);
795 static void r700SetupIndexBuffer(struct gl_context
*ctx
, const struct _mesa_index_buffer
*mesa_ind_buf
)
797 context_t
*context
= R700_CONTEXT(ctx
);
800 context
->ind_buf
.bo
= NULL
;
805 if (mesa_ind_buf
->type
== GL_UNSIGNED_INT
)
807 if (mesa_ind_buf
->type
!= GL_UNSIGNED_BYTE
)
810 const GLvoid
*src_ptr
;
812 GLboolean mapped_named_bo
= GL_FALSE
;
814 if (mesa_ind_buf
->obj
->Name
&& !mesa_ind_buf
->obj
->Pointer
)
816 ctx
->Driver
.MapBuffer(ctx
, GL_ELEMENT_ARRAY_BUFFER
, GL_READ_ONLY_ARB
, mesa_ind_buf
->obj
);
817 assert(mesa_ind_buf
->obj
->Pointer
!= NULL
);
818 mapped_named_bo
= GL_TRUE
;
821 src_ptr
= ADD_POINTERS(mesa_ind_buf
->obj
->Pointer
, mesa_ind_buf
->ptr
);
823 const GLuint size
= mesa_ind_buf
->count
* getTypeSize(mesa_ind_buf
->type
);
825 radeonAllocDmaRegion(&context
->radeon
, &context
->ind_buf
.bo
,
826 &context
->ind_buf
.bo_offset
, size
, 4);
827 radeon_bo_map(context
->ind_buf
.bo
, 1);
828 assert(context
->ind_buf
.bo
->ptr
!= NULL
);
829 dst_ptr
= ADD_POINTERS(context
->ind_buf
.bo
->ptr
, context
->ind_buf
.bo_offset
);
831 memcpy(dst_ptr
, src_ptr
, size
);
833 radeon_bo_unmap(context
->ind_buf
.bo
);
834 context
->ind_buf
.is_32bit
= (mesa_ind_buf
->type
== GL_UNSIGNED_INT
);
835 context
->ind_buf
.count
= mesa_ind_buf
->count
;
839 ctx
->Driver
.UnmapBuffer(ctx
, GL_ELEMENT_ARRAY_BUFFER
, mesa_ind_buf
->obj
);
844 r700FixupIndexBuffer(ctx
, mesa_ind_buf
);
848 static GLboolean
check_fallbacks(struct gl_context
*ctx
)
850 if (ctx
->RenderMode
!= GL_RENDER
)
856 static GLboolean
r700TryDrawPrims(struct gl_context
*ctx
,
857 const struct gl_client_array
*arrays
[],
858 const struct _mesa_prim
*prim
,
860 const struct _mesa_index_buffer
*ib
,
864 context_t
*context
= R700_CONTEXT(ctx
);
865 radeonContextPtr radeon
= &context
->radeon
;
867 struct radeon_renderbuffer
*rrb
;
870 _mesa_update_state( ctx
);
872 if (check_fallbacks(ctx
))
875 _tnl_UpdateFixedFunctionProgram(ctx
);
876 r700SetVertexFormat(ctx
, arrays
, max_index
+ 1);
877 /* shaders need to be updated before buffers are validated */
878 r700UpdateShaders(ctx
);
879 if (!r600ValidateBuffers(ctx
))
882 /* always emit CB base to prevent
883 * lock ups on some chips.
885 R600_STATECHANGE(context
, cb_target
);
886 /* mark vtx as dirty since it changes per-draw */
887 R600_STATECHANGE(context
, vtx
);
889 r700SetScissor(context
);
890 r700SetupVertexProgram(ctx
);
891 r700SetupFragmentProgram(ctx
);
892 r700UpdateShaderStates(ctx
);
894 GLuint emit_end
= r700PredictRenderSize(ctx
, prim
, ib
, nr_prims
)
895 + context
->radeon
.cmdbuf
.cs
->cdw
;
897 r700SetupIndexBuffer(ctx
, ib
);
898 r700SetupStreams(ctx
, arrays
, max_index
+ 1);
900 radeonEmitState(radeon
);
902 radeon_debug_add_indent();
903 for (i
= 0; i
< nr_prims
; ++i
)
905 if (context
->ind_buf
.bo
)
906 r700RunRenderPrimitive(ctx
,
908 prim
[i
].start
+ prim
[i
].count
,
912 r700RunRenderPrimitiveImmediate(ctx
,
914 prim
[i
].start
+ prim
[i
].count
,
917 radeon_debug_remove_indent();
919 /* Flush render op cached for last several quads. */
920 /* XXX drm should handle this in fence submit */
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(struct gl_context
*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 context_t
*context
= R700_CONTEXT(ctx
);
956 radeonContextPtr radeon
= &context
->radeon
;
957 radeon_prepare_render(radeon
);
959 /* This check should get folded into just the places that
960 * min/max index are really needed.
963 if (!vbo_all_varyings_in_vbos(arrays
)) {
964 if (!index_bounds_valid
)
965 vbo_get_minmax_index(ctx
, prim
, ib
, &min_index
, &max_index
);
966 /* do we want to rebase, minimizes the
967 * amount of data to upload? */
969 vbo_rebase_prims( ctx
, arrays
, prim
, nr_prims
, ib
, min_index
, max_index
, r700DrawPrims
);
973 /* Make an attempt at drawing */
974 retval
= r700TryDrawPrims(ctx
, arrays
, prim
, nr_prims
, ib
, min_index
, max_index
);
976 /* If failed run tnl pipeline - it should take care of fallbacks */
978 _swsetup_Wakeup(ctx
);
979 _tnl_draw_prims(ctx
, arrays
, prim
, nr_prims
, ib
, min_index
, max_index
);
983 void r700InitDraw(struct gl_context
*ctx
)
985 struct vbo_context
*vbo
= vbo_context(ctx
);
988 vbo
->draw_prims
= r700DrawPrims
;