r600: relax stride/alignment requirements for vertices
[mesa.git] / src / mesa / drivers / dri / r600 / r700_render.c
1 /*
2 * Copyright (C) 2008-2009 Advanced Micro Devices, Inc.
3 *
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:
10 *
11 * The above copyright notice and this permission notice shall be included
12 * in all copies or substantial portions of the Software.
13 *
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.
20 */
21
22 /*
23 * Authors:
24 * Richard Li <RichardZ.Li@amd.com>, <richardradeon@gmail.com>
25 * CooperYuan <cooper.yuan@amd.com>, <cooperyuan@gmail.com>
26 */
27
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"
34 #include "main/dd.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"
39 #include "vbo/vbo.h"
40
41 #include "tnl/tnl.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"
46
47 #include "r600_context.h"
48 #include "r600_cmdbuf.h"
49
50 #include "r600_tex.h"
51
52 #include "r700_vertprog.h"
53 #include "r700_fragprog.h"
54 #include "r700_state.h"
55
56 #include "radeon_buffer_objects.h"
57 #include "radeon_common_context.h"
58
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,
64 uint32_t read_domain,
65 uint32_t write_domain,
66 uint32_t sync_type);
67
68 void r700WaitForIdle(context_t *context)
69 {
70 BATCH_LOCALS(&context->radeon);
71 radeon_print(RADEON_RENDER | RADEON_STATE, RADEON_TRACE, "%s\n", __func__);
72 BEGIN_BATCH_NO_AUTOSTATE(3);
73
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);
77
78 END_BATCH();
79 COMMIT_BATCH();
80 }
81
82 void r700WaitForIdleClean(context_t *context)
83 {
84 BATCH_LOCALS(&context->radeon);
85 radeon_print(RADEON_RENDER | RADEON_STATE, RADEON_TRACE, "%s\n", __func__);
86 BEGIN_BATCH_NO_AUTOSTATE(5);
87
88 R600_OUT_BATCH(CP_PACKET3(R600_IT_EVENT_WRITE, 0));
89 R600_OUT_BATCH(CACHE_FLUSH_AND_INV_EVENT);
90
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);
94
95 END_BATCH();
96 COMMIT_BATCH();
97 }
98
99 void r700Start3D(context_t *context)
100 {
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)
104 {
105 BEGIN_BATCH_NO_AUTOSTATE(2);
106 R600_OUT_BATCH(CP_PACKET3(R600_IT_START_3D_CMDBUF, 0));
107 R600_OUT_BATCH(0);
108 END_BATCH();
109 }
110
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);
115 END_BATCH();
116
117 COMMIT_BATCH();
118 }
119
120 GLboolean r700SyncSurf(context_t *context,
121 struct radeon_bo *pbo,
122 uint32_t read_domain,
123 uint32_t write_domain,
124 uint32_t sync_type)
125 {
126 BATCH_LOCALS(&context->radeon);
127 radeon_print(RADEON_RENDER | RADEON_STATE, RADEON_TRACE, "%s\n", __func__);
128 uint32_t cp_coher_size;
129
130 if (!pbo)
131 return GL_FALSE;
132
133 if (pbo->size == 0xffffffff)
134 cp_coher_size = 0xffffffff;
135 else
136 cp_coher_size = ((pbo->size + 255) >> 8);
137
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);
142 R600_OUT_BATCH(0);
143 R600_OUT_BATCH(10);
144 R600_OUT_BATCH_RELOC(0,
145 pbo,
146 0,
147 read_domain, write_domain, 0);
148 END_BATCH();
149 COMMIT_BATCH();
150
151 return GL_TRUE;
152 }
153
154 static unsigned int r700PrimitiveType(int prim)
155 {
156 switch (prim & PRIM_MODE_MASK)
157 {
158 case GL_POINTS:
159 return DI_PT_POINTLIST;
160 break;
161 case GL_LINES:
162 return DI_PT_LINELIST;
163 break;
164 case GL_LINE_STRIP:
165 return DI_PT_LINESTRIP;
166 break;
167 case GL_LINE_LOOP:
168 return DI_PT_LINELOOP;
169 break;
170 case GL_TRIANGLES:
171 return DI_PT_TRILIST;
172 break;
173 case GL_TRIANGLE_STRIP:
174 return DI_PT_TRISTRIP;
175 break;
176 case GL_TRIANGLE_FAN:
177 return DI_PT_TRIFAN;
178 break;
179 case GL_QUADS:
180 return DI_PT_QUADLIST;
181 break;
182 case GL_QUAD_STRIP:
183 return DI_PT_QUADSTRIP;
184 break;
185 case GL_POLYGON:
186 return DI_PT_POLYGON;
187 break;
188 default:
189 assert(0);
190 return -1;
191 break;
192 }
193 }
194
195 static int r700NumVerts(int num_verts, int prim)
196 {
197 int verts_off = 0;
198
199 switch (prim & PRIM_MODE_MASK) {
200 case GL_POINTS:
201 verts_off = 0;
202 break;
203 case GL_LINES:
204 verts_off = num_verts % 2;
205 break;
206 case GL_LINE_STRIP:
207 if (num_verts < 2)
208 verts_off = num_verts;
209 break;
210 case GL_LINE_LOOP:
211 if (num_verts < 2)
212 verts_off = num_verts;
213 break;
214 case GL_TRIANGLES:
215 verts_off = num_verts % 3;
216 break;
217 case GL_TRIANGLE_STRIP:
218 if (num_verts < 3)
219 verts_off = num_verts;
220 break;
221 case GL_TRIANGLE_FAN:
222 if (num_verts < 3)
223 verts_off = num_verts;
224 break;
225 case GL_QUADS:
226 verts_off = num_verts % 4;
227 break;
228 case GL_QUAD_STRIP:
229 if (num_verts < 4)
230 verts_off = num_verts;
231 else
232 verts_off = num_verts % 2;
233 break;
234 case GL_POLYGON:
235 if (num_verts < 3)
236 verts_off = num_verts;
237 break;
238 default:
239 assert(0);
240 return -1;
241 break;
242 }
243
244 return num_verts - verts_off;
245 }
246
247 static void r700RunRenderPrimitive(GLcontext * ctx, int start, int end, int prim)
248 {
249 context_t *context = R700_CONTEXT(ctx);
250 BATCH_LOCALS(&context->radeon);
251 int type, total_emit;
252 int num_indices;
253 uint32_t vgt_draw_initiator = 0;
254 uint32_t vgt_index_type = 0;
255 uint32_t vgt_primitive_type = 0;
256 uint32_t vgt_num_indices = 0;
257
258 type = r700PrimitiveType(prim);
259 num_indices = r700NumVerts(end - start, prim);
260
261 radeon_print(RADEON_RENDER, RADEON_TRACE,
262 "%s type %x num_indices %d\n",
263 __func__, type, num_indices);
264
265 if (type < 0 || num_indices <= 0)
266 return;
267
268 SETfield(vgt_primitive_type, type,
269 VGT_PRIMITIVE_TYPE__PRIM_TYPE_shift, VGT_PRIMITIVE_TYPE__PRIM_TYPE_mask);
270
271 SETfield(vgt_index_type, DI_INDEX_SIZE_32_BIT, INDEX_TYPE_shift, INDEX_TYPE_mask);
272
273 if(GL_TRUE != context->ind_buf.is_32bit)
274 {
275 SETfield(vgt_index_type, DI_INDEX_SIZE_16_BIT, INDEX_TYPE_shift, INDEX_TYPE_mask);
276 }
277
278 vgt_num_indices = num_indices;
279 SETfield(vgt_draw_initiator, DI_SRC_SEL_DMA, SOURCE_SELECT_shift, SOURCE_SELECT_mask);
280 SETfield(vgt_draw_initiator, DI_MAJOR_MODE_0, MAJOR_MODE_shift, MAJOR_MODE_mask);
281
282 total_emit = 3 /* VGT_PRIMITIVE_TYPE */
283 + 2 /* VGT_INDEX_TYPE */
284 + 2 /* NUM_INSTANCES */
285 + 5 + 2; /* DRAW_INDEX */
286
287 BEGIN_BATCH_NO_AUTOSTATE(total_emit);
288 // prim
289 R600_OUT_BATCH_REGSEQ(VGT_PRIMITIVE_TYPE, 1);
290 R600_OUT_BATCH(vgt_primitive_type);
291 // index type
292 R600_OUT_BATCH(CP_PACKET3(R600_IT_INDEX_TYPE, 0));
293 R600_OUT_BATCH(vgt_index_type);
294 // num instances
295 R600_OUT_BATCH(CP_PACKET3(R600_IT_NUM_INSTANCES, 0));
296 R600_OUT_BATCH(1);
297 // draw packet
298 R600_OUT_BATCH(CP_PACKET3(R600_IT_DRAW_INDEX, 3));
299 R600_OUT_BATCH(context->ind_buf.bo_offset);
300 R600_OUT_BATCH(0);
301 R600_OUT_BATCH(vgt_num_indices);
302 R600_OUT_BATCH(vgt_draw_initiator);
303 R600_OUT_BATCH_RELOC(context->ind_buf.bo_offset,
304 context->ind_buf.bo,
305 context->ind_buf.bo_offset,
306 RADEON_GEM_DOMAIN_GTT, 0, 0);
307 END_BATCH();
308 COMMIT_BATCH();
309 }
310
311 static void r700RunRenderPrimitiveImmediate(GLcontext * ctx, int start, int end, int prim)
312 {
313 context_t *context = R700_CONTEXT(ctx);
314 BATCH_LOCALS(&context->radeon);
315 int type, i;
316 uint32_t num_indices, total_emit = 0;
317 uint32_t vgt_draw_initiator = 0;
318 uint32_t vgt_index_type = 0;
319 uint32_t vgt_primitive_type = 0;
320 uint32_t vgt_num_indices = 0;
321
322 type = r700PrimitiveType(prim);
323 num_indices = r700NumVerts(end - start, prim);
324
325 radeon_print(RADEON_RENDER, RADEON_TRACE,
326 "%s type %x num_indices %d\n",
327 __func__, type, num_indices);
328
329 if (type < 0 || num_indices <= 0)
330 return;
331
332 SETfield(vgt_primitive_type, type,
333 VGT_PRIMITIVE_TYPE__PRIM_TYPE_shift, VGT_PRIMITIVE_TYPE__PRIM_TYPE_mask);
334
335 if (num_indices > 0xffff)
336 {
337 SETfield(vgt_index_type, DI_INDEX_SIZE_32_BIT, INDEX_TYPE_shift, INDEX_TYPE_mask);
338 }
339 else
340 {
341 SETfield(vgt_index_type, DI_INDEX_SIZE_16_BIT, INDEX_TYPE_shift, INDEX_TYPE_mask);
342 }
343
344 vgt_num_indices = num_indices;
345 SETfield(vgt_draw_initiator, DI_MAJOR_MODE_0, MAJOR_MODE_shift, MAJOR_MODE_mask);
346
347 if (start == 0)
348 {
349 SETfield(vgt_draw_initiator, DI_SRC_SEL_AUTO_INDEX, SOURCE_SELECT_shift, SOURCE_SELECT_mask);
350 }
351 else
352 {
353 if (num_indices > 0xffff)
354 {
355 total_emit += num_indices;
356 }
357 else
358 {
359 total_emit += (num_indices + 1) / 2;
360 }
361 SETfield(vgt_draw_initiator, DI_SRC_SEL_IMMEDIATE, SOURCE_SELECT_shift, SOURCE_SELECT_mask);
362 }
363
364 total_emit += 3 /* VGT_PRIMITIVE_TYPE */
365 + 2 /* VGT_INDEX_TYPE */
366 + 2 /* NUM_INSTANCES */
367 + 3; /* DRAW */
368
369 BEGIN_BATCH_NO_AUTOSTATE(total_emit);
370 // prim
371 R600_OUT_BATCH_REGSEQ(VGT_PRIMITIVE_TYPE, 1);
372 R600_OUT_BATCH(vgt_primitive_type);
373 // index type
374 R600_OUT_BATCH(CP_PACKET3(R600_IT_INDEX_TYPE, 0));
375 R600_OUT_BATCH(vgt_index_type);
376 // num instances
377 R600_OUT_BATCH(CP_PACKET3(R600_IT_NUM_INSTANCES, 0));
378 R600_OUT_BATCH(1);
379 // draw packet
380 if(start == 0)
381 {
382 R600_OUT_BATCH(CP_PACKET3(R600_IT_DRAW_INDEX_AUTO, 1));
383 R600_OUT_BATCH(vgt_num_indices);
384 R600_OUT_BATCH(vgt_draw_initiator);
385 }
386 else
387 {
388 if (num_indices > 0xffff)
389 {
390 R600_OUT_BATCH(CP_PACKET3(R600_IT_DRAW_INDEX_IMMD, (num_indices + 1)));
391 R600_OUT_BATCH(vgt_num_indices);
392 R600_OUT_BATCH(vgt_draw_initiator);
393 for (i = start; i < (start + num_indices); i++)
394 {
395 R600_OUT_BATCH(i);
396 }
397 }
398 else
399 {
400 R600_OUT_BATCH(CP_PACKET3(R600_IT_DRAW_INDEX_IMMD, (((num_indices + 1) / 2) + 1)));
401 R600_OUT_BATCH(vgt_num_indices);
402 R600_OUT_BATCH(vgt_draw_initiator);
403 for (i = start; i < (start + num_indices); i += 2)
404 {
405 if ((i + 1) == (start + num_indices))
406 {
407 R600_OUT_BATCH(i);
408 }
409 else
410 {
411 R600_OUT_BATCH(((i + 1) << 16) | (i));
412 }
413 }
414 }
415 }
416
417 END_BATCH();
418 COMMIT_BATCH();
419 }
420
421 /* start 3d, idle, cb/db flush */
422 #define PRE_EMIT_STATE_BUFSZ 5 + 5 + 14
423
424 static GLuint r700PredictRenderSize(GLcontext* ctx,
425 const struct _mesa_prim *prim,
426 const struct _mesa_index_buffer *ib,
427 GLuint nr_prims)
428 {
429 context_t *context = R700_CONTEXT(ctx);
430 GLboolean flushed;
431 GLuint dwords, i;
432 GLuint state_size;
433
434 dwords = PRE_EMIT_STATE_BUFSZ;
435 if (ib)
436 dwords += nr_prims * 14;
437 else {
438 for (i = 0; i < nr_prims; ++i)
439 {
440 if (prim[i].start == 0)
441 dwords += 10;
442 else if (prim[i].count > 0xffff)
443 dwords += prim[i].count + 10;
444 else
445 dwords += ((prim[i].count + 1) / 2) + 10;
446 }
447 }
448
449 state_size = radeonCountStateEmitSize(&context->radeon);
450 flushed = rcommonEnsureCmdBufSpace(&context->radeon,
451 dwords + state_size,
452 __FUNCTION__);
453 if (flushed)
454 dwords += radeonCountStateEmitSize(&context->radeon);
455 else
456 dwords += state_size;
457
458 radeon_print(RADEON_RENDER, RADEON_VERBOSE, "%s: total prediction size is %d.\n", __FUNCTION__, dwords);
459 return dwords;
460
461 }
462
463 #define CONVERT( TYPE, MACRO ) do { \
464 GLuint i, j, sz; \
465 sz = input->Size; \
466 if (input->Normalized) { \
467 for (i = 0; i < count; i++) { \
468 const TYPE *in = (TYPE *)src_ptr; \
469 for (j = 0; j < sz; j++) { \
470 *dst_ptr++ = MACRO(*in); \
471 in++; \
472 } \
473 src_ptr += stride; \
474 } \
475 } else { \
476 for (i = 0; i < count; i++) { \
477 const TYPE *in = (TYPE *)src_ptr; \
478 for (j = 0; j < sz; j++) { \
479 *dst_ptr++ = (GLfloat)(*in); \
480 in++; \
481 } \
482 src_ptr += stride; \
483 } \
484 } \
485 } while (0)
486
487 /**
488 * Convert attribute data type to float
489 * If the attribute uses named buffer object replace the bo with newly allocated bo
490 */
491 static void r700ConvertAttrib(GLcontext *ctx, int count,
492 const struct gl_client_array *input,
493 struct StreamDesc *attr)
494 {
495 context_t *context = R700_CONTEXT(ctx);
496 const GLvoid *src_ptr;
497 GLboolean mapped_named_bo = GL_FALSE;
498 GLfloat *dst_ptr;
499 GLuint stride;
500
501 stride = (input->StrideB == 0) ? getTypeSize(input->Type) * input->Size : input->StrideB;
502
503 /* Convert value for first element only */
504 if (input->StrideB == 0)
505 {
506 count = 1;
507 }
508
509 if (input->BufferObj->Name)
510 {
511 if (!input->BufferObj->Pointer)
512 {
513 ctx->Driver.MapBuffer(ctx, GL_ARRAY_BUFFER, GL_READ_ONLY_ARB, input->BufferObj);
514 mapped_named_bo = GL_TRUE;
515 }
516
517 src_ptr = ADD_POINTERS(input->BufferObj->Pointer, input->Ptr);
518 }
519 else
520 {
521 src_ptr = input->Ptr;
522 }
523
524 radeonAllocDmaRegion(&context->radeon, &attr->bo, &attr->bo_offset,
525 sizeof(GLfloat) * input->Size * count, 32);
526
527 radeon_bo_map(attr->bo, 1);
528
529 dst_ptr = (GLfloat *)ADD_POINTERS(attr->bo->ptr, attr->bo_offset);
530
531 assert(src_ptr != NULL);
532
533 switch (input->Type)
534 {
535 case GL_DOUBLE:
536 CONVERT(GLdouble, (GLfloat));
537 break;
538 case GL_UNSIGNED_INT:
539 CONVERT(GLuint, UINT_TO_FLOAT);
540 break;
541 case GL_INT:
542 CONVERT(GLint, INT_TO_FLOAT);
543 break;
544 case GL_UNSIGNED_SHORT:
545 CONVERT(GLushort, USHORT_TO_FLOAT);
546 break;
547 case GL_SHORT:
548 CONVERT(GLshort, SHORT_TO_FLOAT);
549 break;
550 case GL_UNSIGNED_BYTE:
551 assert(input->Format != GL_BGRA);
552 CONVERT(GLubyte, UBYTE_TO_FLOAT);
553 break;
554 case GL_BYTE:
555 CONVERT(GLbyte, BYTE_TO_FLOAT);
556 break;
557 default:
558 assert(0);
559 break;
560 }
561
562 radeon_bo_unmap(attr->bo);
563
564 if (mapped_named_bo)
565 {
566 ctx->Driver.UnmapBuffer(ctx, GL_ARRAY_BUFFER, input->BufferObj);
567 }
568 }
569
570 static void r700AlignDataToDword(GLcontext *ctx,
571 const struct gl_client_array *input,
572 int count,
573 struct StreamDesc *attr)
574 {
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;
579
580 radeonAllocDmaRegion(&context->radeon, &attr->bo, &attr->bo_offset, size, 32);
581
582 radeon_bo_map(attr->bo, 1);
583
584 if (!input->BufferObj->Pointer)
585 {
586 ctx->Driver.MapBuffer(ctx, GL_ARRAY_BUFFER, GL_READ_ONLY_ARB, input->BufferObj);
587 mapped_named_bo = GL_TRUE;
588 }
589
590 {
591 GLvoid *src_ptr = ADD_POINTERS(input->BufferObj->Pointer, input->Ptr);
592 GLvoid *dst_ptr = ADD_POINTERS(attr->bo->ptr, attr->bo_offset);
593 int i;
594
595 for (i = 0; i < count; ++i)
596 {
597 memcpy(dst_ptr, src_ptr, input->StrideB);
598 src_ptr += input->StrideB;
599 dst_ptr += dst_stride;
600 }
601 }
602
603 radeon_bo_unmap(attr->bo);
604 if (mapped_named_bo)
605 {
606 ctx->Driver.UnmapBuffer(ctx, GL_ARRAY_BUFFER, input->BufferObj);
607 }
608
609 attr->stride = dst_stride;
610 }
611
612 static void r700SetupStreams(GLcontext *ctx, const struct gl_client_array *input[], int count)
613 {
614 context_t *context = R700_CONTEXT(ctx);
615 GLuint stride;
616 int ret;
617 int i, index;
618
619 R600_STATECHANGE(context, vtx);
620
621 for(index = 0; index < context->nNumActiveAos; index++)
622 {
623 struct radeon_aos *aos = &context->radeon.tcl.aos[index];
624 i = context->stream_desc[index].element;
625
626 stride = (input[i]->StrideB == 0) ? getTypeSize(input[i]->Type) * input[i]->Size : input[i]->StrideB;
627
628 if (input[i]->Type == GL_DOUBLE || input[i]->Type == GL_UNSIGNED_INT || input[i]->Type == GL_INT
629 #if MESA_BIG_ENDIAN
630 || getTypeSize(input[i]->Type) != 4
631 #endif
632 )
633 {
634 r700ConvertAttrib(ctx, count, input[i], &context->stream_desc[index]);
635 }
636 else
637 {
638 if (input[i]->BufferObj->Name)
639 {
640 context->stream_desc[index].stride = input[i]->StrideB;
641 context->stream_desc[index].bo_offset = (intptr_t) input[i]->Ptr;
642 context->stream_desc[index].bo = get_radeon_buffer_object(input[i]->BufferObj)->bo;
643 context->stream_desc[index].is_named_bo = GL_TRUE;
644 }
645 else
646 {
647 int size;
648 int local_count = count;
649 uint32_t *dst;
650
651 if (input[i]->StrideB == 0)
652 {
653 size = getTypeSize(input[i]->Type) * input[i]->Size;
654 local_count = 1;
655 }
656 else
657 {
658 size = getTypeSize(input[i]->Type) * input[i]->Size * local_count;
659 }
660
661 radeonAllocDmaRegion(&context->radeon, &context->stream_desc[index].bo,
662 &context->stream_desc[index].bo_offset, size, 32);
663
664 radeon_bo_map(context->stream_desc[index].bo, 1);
665 assert(context->stream_desc[index].bo->ptr != NULL);
666
667
668 dst = (uint32_t *)ADD_POINTERS(context->stream_desc[index].bo->ptr,
669 context->stream_desc[index].bo_offset);
670
671 switch (context->stream_desc[index].dwords)
672 {
673 case 1:
674 radeonEmitVec4(dst, input[i]->Ptr, input[i]->StrideB, local_count);
675 break;
676 case 2:
677 radeonEmitVec8(dst, input[i]->Ptr, input[i]->StrideB, local_count);
678 break;
679 case 3:
680 radeonEmitVec12(dst, input[i]->Ptr, input[i]->StrideB, local_count);
681 break;
682 case 4:
683 radeonEmitVec16(dst, input[i]->Ptr, input[i]->StrideB, local_count);
684 break;
685 default:
686 assert(0);
687 break;
688 }
689 radeon_bo_unmap(context->stream_desc[index].bo);
690 }
691 }
692
693 aos->count = context->stream_desc[index].stride == 0 ? 1 : count;
694 aos->stride = context->stream_desc[index].stride / sizeof(float);
695 aos->components = context->stream_desc[index].dwords;
696 aos->bo = context->stream_desc[index].bo;
697 aos->offset = context->stream_desc[index].bo_offset;
698
699 if(context->stream_desc[index].is_named_bo)
700 {
701 radeon_cs_space_add_persistent_bo(context->radeon.cmdbuf.cs,
702 context->stream_desc[index].bo,
703 RADEON_GEM_DOMAIN_GTT, 0);
704 }
705 }
706
707 ret = radeon_cs_space_check_with_bo(context->radeon.cmdbuf.cs,
708 first_elem(&context->radeon.dma.reserved)->bo,
709 RADEON_GEM_DOMAIN_GTT, 0);
710 }
711
712 static void r700FreeData(GLcontext *ctx)
713 {
714 /* Need to zero tcl.aos[n].bo and tcl.elt_dma_bo
715 * to prevent double unref in radeonReleaseArrays
716 * called during context destroy
717 */
718 context_t *context = R700_CONTEXT(ctx);
719
720 int i;
721
722 for (i = 0; i < context->nNumActiveAos; i++)
723 {
724 if (!context->stream_desc[i].is_named_bo)
725 {
726 radeon_bo_unref(context->stream_desc[i].bo);
727 }
728 context->radeon.tcl.aos[i].bo = NULL;
729 }
730
731 if (context->ind_buf.bo != NULL)
732 {
733 radeon_bo_unref(context->ind_buf.bo);
734 }
735 }
736
737 static void r700FixupIndexBuffer(GLcontext *ctx, const struct _mesa_index_buffer *mesa_ind_buf)
738 {
739 context_t *context = R700_CONTEXT(ctx);
740 GLvoid *src_ptr;
741 GLuint *out;
742 int i;
743 GLboolean mapped_named_bo = GL_FALSE;
744
745 if (mesa_ind_buf->obj->Name && !mesa_ind_buf->obj->Pointer)
746 {
747 ctx->Driver.MapBuffer(ctx, GL_ELEMENT_ARRAY_BUFFER, GL_READ_ONLY_ARB, mesa_ind_buf->obj);
748 mapped_named_bo = GL_TRUE;
749 assert(mesa_ind_buf->obj->Pointer != NULL);
750 }
751 src_ptr = ADD_POINTERS(mesa_ind_buf->obj->Pointer, mesa_ind_buf->ptr);
752
753 if (mesa_ind_buf->type == GL_UNSIGNED_BYTE)
754 {
755 GLuint size = sizeof(GLushort) * ((mesa_ind_buf->count + 1) & ~1);
756 GLubyte *in = (GLubyte *)src_ptr;
757
758 radeonAllocDmaRegion(&context->radeon, &context->ind_buf.bo,
759 &context->ind_buf.bo_offset, size, 4);
760
761 radeon_bo_map(context->ind_buf.bo, 1);
762 assert(context->ind_buf.bo->ptr != NULL);
763 out = (GLuint *)ADD_POINTERS(context->ind_buf.bo->ptr, context->ind_buf.bo_offset);
764
765 for (i = 0; i + 1 < mesa_ind_buf->count; i += 2)
766 {
767 *out++ = in[i] | in[i + 1] << 16;
768 }
769
770 if (i < mesa_ind_buf->count)
771 {
772 *out++ = in[i];
773 }
774
775 radeon_bo_unmap(context->ind_buf.bo);
776 #if MESA_BIG_ENDIAN
777 }
778 else
779 { /* if (mesa_ind_buf->type == GL_UNSIGNED_SHORT) */
780 GLushort *in = (GLushort *)src_ptr;
781 GLuint size = sizeof(GLushort) * ((mesa_ind_buf->count + 1) & ~1);
782
783 radeonAllocDmaRegion(&context->radeon, &context->ind_buf.bo,
784 &context->ind_buf.bo_offset, size, 4);
785
786 radeon_bo_map(context->ind_buf.bo, 1);
787 assert(context->ind_buf.bo->ptr != NULL);
788 out = (GLuint *)ADD_POINTERS(context->ind_buf.bo->ptr, context->ind_buf.bo_offset);
789
790 for (i = 0; i + 1 < mesa_ind_buf->count; i += 2)
791 {
792 *out++ = in[i] | in[i + 1] << 16;
793 }
794
795 if (i < mesa_ind_buf->count)
796 {
797 *out++ = in[i];
798 }
799 radeon_bo_unmap(context->ind_buf.bo);
800 #endif
801 }
802
803 context->ind_buf.is_32bit = GL_FALSE;
804 context->ind_buf.count = mesa_ind_buf->count;
805
806 if (mapped_named_bo)
807 {
808 ctx->Driver.UnmapBuffer(ctx, GL_ELEMENT_ARRAY_BUFFER, mesa_ind_buf->obj);
809 }
810 }
811
812 static void r700SetupIndexBuffer(GLcontext *ctx, const struct _mesa_index_buffer *mesa_ind_buf)
813 {
814 context_t *context = R700_CONTEXT(ctx);
815
816 if (!mesa_ind_buf) {
817 context->ind_buf.bo = NULL;
818 return;
819 }
820
821 #if MESA_BIG_ENDIAN
822 if (mesa_ind_buf->type == GL_UNSIGNED_INT)
823 #else
824 if (mesa_ind_buf->type != GL_UNSIGNED_BYTE)
825 #endif
826 {
827 const GLvoid *src_ptr;
828 GLvoid *dst_ptr;
829 GLboolean mapped_named_bo = GL_FALSE;
830
831 if (mesa_ind_buf->obj->Name && !mesa_ind_buf->obj->Pointer)
832 {
833 ctx->Driver.MapBuffer(ctx, GL_ELEMENT_ARRAY_BUFFER, GL_READ_ONLY_ARB, mesa_ind_buf->obj);
834 assert(mesa_ind_buf->obj->Pointer != NULL);
835 mapped_named_bo = GL_TRUE;
836 }
837
838 src_ptr = ADD_POINTERS(mesa_ind_buf->obj->Pointer, mesa_ind_buf->ptr);
839
840 const GLuint size = mesa_ind_buf->count * getTypeSize(mesa_ind_buf->type);
841
842 radeonAllocDmaRegion(&context->radeon, &context->ind_buf.bo,
843 &context->ind_buf.bo_offset, size, 4);
844 radeon_bo_map(context->ind_buf.bo, 1);
845 assert(context->ind_buf.bo->ptr != NULL);
846 dst_ptr = ADD_POINTERS(context->ind_buf.bo->ptr, context->ind_buf.bo_offset);
847
848 memcpy(dst_ptr, src_ptr, size);
849
850 radeon_bo_unmap(context->ind_buf.bo);
851 context->ind_buf.is_32bit = (mesa_ind_buf->type == GL_UNSIGNED_INT);
852 context->ind_buf.count = mesa_ind_buf->count;
853
854 if (mapped_named_bo)
855 {
856 ctx->Driver.UnmapBuffer(ctx, GL_ELEMENT_ARRAY_BUFFER, mesa_ind_buf->obj);
857 }
858 }
859 else
860 {
861 r700FixupIndexBuffer(ctx, mesa_ind_buf);
862 }
863 }
864
865 static GLboolean check_fallbacks(GLcontext *ctx)
866 {
867 if (ctx->RenderMode != GL_RENDER)
868 return GL_TRUE;
869
870 return GL_FALSE;
871 }
872
873 static GLboolean r700TryDrawPrims(GLcontext *ctx,
874 const struct gl_client_array *arrays[],
875 const struct _mesa_prim *prim,
876 GLuint nr_prims,
877 const struct _mesa_index_buffer *ib,
878 GLuint min_index,
879 GLuint max_index )
880 {
881 context_t *context = R700_CONTEXT(ctx);
882 radeonContextPtr radeon = &context->radeon;
883 GLuint i, id = 0;
884 struct radeon_renderbuffer *rrb;
885
886 if (ctx->NewState)
887 _mesa_update_state( ctx );
888
889 if (check_fallbacks(ctx))
890 return GL_FALSE;
891
892 _tnl_UpdateFixedFunctionProgram(ctx);
893 r700SetVertexFormat(ctx, arrays, max_index + 1);
894 /* shaders need to be updated before buffers are validated */
895 r700UpdateShaders(ctx);
896 if (!r600ValidateBuffers(ctx))
897 return GL_FALSE;
898
899 /* always emit CB base to prevent
900 * lock ups on some chips.
901 */
902 R600_STATECHANGE(context, cb_target);
903 /* mark vtx as dirty since it changes per-draw */
904 R600_STATECHANGE(context, vtx);
905
906 r700SetScissor(context);
907 r700SetupVertexProgram(ctx);
908 r700SetupFragmentProgram(ctx);
909 r700UpdateShaderStates(ctx);
910
911 GLuint emit_end = r700PredictRenderSize(ctx, prim, ib, nr_prims)
912 + context->radeon.cmdbuf.cs->cdw;
913
914 r700SetupIndexBuffer(ctx, ib);
915 r700SetupStreams(ctx, arrays, max_index + 1);
916
917 radeonEmitState(radeon);
918
919 radeon_debug_add_indent();
920 for (i = 0; i < nr_prims; ++i)
921 {
922 if (context->ind_buf.bo)
923 r700RunRenderPrimitive(ctx,
924 prim[i].start,
925 prim[i].start + prim[i].count,
926 prim[i].mode);
927 else
928 r700RunRenderPrimitiveImmediate(ctx,
929 prim[i].start,
930 prim[i].start + prim[i].count,
931 prim[i].mode);
932 }
933 radeon_debug_remove_indent();
934
935 /* Flush render op cached for last several quads. */
936 /* XXX drm should handle this in fence submit */
937 r700WaitForIdleClean(context);
938
939 rrb = radeon_get_colorbuffer(&context->radeon);
940 if (rrb && rrb->bo)
941 r700SyncSurf(context, rrb->bo, 0, RADEON_GEM_DOMAIN_VRAM,
942 CB_ACTION_ENA_bit | (1 << (id + 6)));
943
944 rrb = radeon_get_depthbuffer(&context->radeon);
945 if (rrb && rrb->bo)
946 r700SyncSurf(context, rrb->bo, 0, RADEON_GEM_DOMAIN_VRAM,
947 DB_ACTION_ENA_bit | DB_DEST_BASE_ENA_bit);
948
949 r700FreeData(ctx);
950
951 if (emit_end < context->radeon.cmdbuf.cs->cdw)
952 {
953 WARN_ONCE("Rendering was %d commands larger than predicted size."
954 " We might overflow command buffer.\n", context->radeon.cmdbuf.cs->cdw - emit_end);
955 }
956
957 return GL_TRUE;
958 }
959
960 static void r700DrawPrims(GLcontext *ctx,
961 const struct gl_client_array *arrays[],
962 const struct _mesa_prim *prim,
963 GLuint nr_prims,
964 const struct _mesa_index_buffer *ib,
965 GLboolean index_bounds_valid,
966 GLuint min_index,
967 GLuint max_index)
968 {
969 GLboolean retval = GL_FALSE;
970
971 context_t *context = R700_CONTEXT(ctx);
972 radeonContextPtr radeon = &context->radeon;
973 radeon_prepare_render(radeon);
974
975 /* This check should get folded into just the places that
976 * min/max index are really needed.
977 */
978 if (!index_bounds_valid) {
979 vbo_get_minmax_index(ctx, prim, ib, &min_index, &max_index);
980 }
981
982 if (min_index) {
983 vbo_rebase_prims( ctx, arrays, prim, nr_prims, ib, min_index, max_index, r700DrawPrims );
984 return;
985 }
986
987 /* Make an attempt at drawing */
988 retval = r700TryDrawPrims(ctx, arrays, prim, nr_prims, ib, min_index, max_index);
989
990 /* If failed run tnl pipeline - it should take care of fallbacks */
991 if (!retval) {
992 _swsetup_Wakeup(ctx);
993 _tnl_draw_prims(ctx, arrays, prim, nr_prims, ib, min_index, max_index);
994 }
995 }
996
997 void r700InitDraw(GLcontext *ctx)
998 {
999 struct vbo_context *vbo = vbo_context(ctx);
1000
1001 /* to be enabled */
1002 vbo->draw_prims = r700DrawPrims;
1003 }
1004
1005