Merge branch 'mesa_7_6_branch'
[mesa.git] / src / mesa / drivers / dri / r300 / r300_draw.c
1 /**************************************************************************
2 *
3 * Copyright 2009 Maciej Cencora
4 *
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:
12 *
13 * The above copyright notice and this permission notice (including the
14 * next paragraph) shall be included in all copies or substantial portions
15 * of the Software.
16 *
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.
24 *
25 **************************************************************************/
26
27 #include <stdlib.h>
28
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"
34 #include "main/simple_list.h"
35
36 #include "r300_reg.h"
37 #include "r300_context.h"
38 #include "r300_emit.h"
39 #include "r300_render.h"
40 #include "r300_state.h"
41 #include "r300_tex.h"
42 #include "r300_cmdbuf.h"
43
44 #include "radeon_buffer_objects.h"
45 #include "radeon_common_context.h"
46
47 #include "tnl/tnl.h"
48 #include "tnl/t_vp_build.h"
49 #include "vbo/vbo_context.h"
50 #include "swrast/swrast.h"
51 #include "swrast_setup/swrast_setup.h"
52
53
54 static int getTypeSize(GLenum type)
55 {
56 switch (type) {
57 case GL_DOUBLE:
58 return sizeof(GLdouble);
59 case GL_FLOAT:
60 return sizeof(GLfloat);
61 case GL_INT:
62 return sizeof(GLint);
63 case GL_UNSIGNED_INT:
64 return sizeof(GLuint);
65 case GL_SHORT:
66 return sizeof(GLshort);
67 case GL_UNSIGNED_SHORT:
68 return sizeof(GLushort);
69 case GL_BYTE:
70 return sizeof(GLbyte);
71 case GL_UNSIGNED_BYTE:
72 return sizeof(GLubyte);
73 default:
74 assert(0);
75 return 0;
76 }
77 }
78
79 static void r300FixupIndexBuffer(GLcontext *ctx, const struct _mesa_index_buffer *mesa_ind_buf)
80 {
81 r300ContextPtr r300 = R300_CONTEXT(ctx);
82 GLvoid *src_ptr;
83 GLuint *out;
84 int i;
85 GLboolean mapped_named_bo = GL_FALSE;
86
87 if (mesa_ind_buf->obj->Name && !mesa_ind_buf->obj->Pointer) {
88 ctx->Driver.MapBuffer(ctx, GL_ELEMENT_ARRAY_BUFFER, GL_READ_ONLY_ARB, mesa_ind_buf->obj);
89 mapped_named_bo = GL_TRUE;
90 assert(mesa_ind_buf->obj->Pointer != NULL);
91 }
92 src_ptr = ADD_POINTERS(mesa_ind_buf->obj->Pointer, mesa_ind_buf->ptr);
93
94 radeon_print(RADEON_FALLBACKS, RADEON_IMPORTANT,
95 "%s: Fixing index buffer format. type %d\n",
96 __func__, mesa_ind_buf->type);
97
98 if (mesa_ind_buf->type == GL_UNSIGNED_BYTE) {
99 GLuint size = sizeof(GLushort) * ((mesa_ind_buf->count + 1) & ~1);
100 GLubyte *in = (GLubyte *)src_ptr;
101
102 radeonAllocDmaRegion(&r300->radeon, &r300->ind_buf.bo, &r300->ind_buf.bo_offset, size, 4);
103
104 assert(r300->ind_buf.bo->ptr != NULL);
105 out = (GLuint *)ADD_POINTERS(r300->ind_buf.bo->ptr, r300->ind_buf.bo_offset);
106
107 for (i = 0; i + 1 < mesa_ind_buf->count; i += 2) {
108 *out++ = in[i] | in[i + 1] << 16;
109 }
110
111 if (i < mesa_ind_buf->count) {
112 *out++ = in[i];
113 }
114
115 #if MESA_BIG_ENDIAN
116 } else { /* if (mesa_ind_buf->type == GL_UNSIGNED_SHORT) */
117 GLushort *in = (GLushort *)src_ptr;
118 GLuint size = sizeof(GLushort) * ((mesa_ind_buf->count + 1) & ~1);
119
120 radeonAllocDmaRegion(&r300->radeon, &r300->ind_buf.bo,
121 &r300->ind_buf.bo_offset, size, 4);
122
123 assert(r300->ind_buf.bo->ptr != NULL);
124 out = (GLuint *)ADD_POINTERS(r300->ind_buf.bo->ptr, r300->ind_buf.bo_offset);
125
126 for (i = 0; i + 1 < mesa_ind_buf->count; i += 2) {
127 *out++ = in[i] | in[i + 1] << 16;
128 }
129
130 if (i < mesa_ind_buf->count) {
131 *out++ = in[i];
132 }
133 #endif
134 }
135
136 r300->ind_buf.is_32bit = GL_FALSE;
137 r300->ind_buf.count = mesa_ind_buf->count;
138
139 if (mapped_named_bo) {
140 ctx->Driver.UnmapBuffer(ctx, GL_ELEMENT_ARRAY_BUFFER, mesa_ind_buf->obj);
141 }
142 }
143
144
145 static void r300SetupIndexBuffer(GLcontext *ctx, const struct _mesa_index_buffer *mesa_ind_buf)
146 {
147 r300ContextPtr r300 = R300_CONTEXT(ctx);
148
149 if (!mesa_ind_buf) {
150 r300->ind_buf.bo = NULL;
151 return;
152 }
153 radeon_print(RADEON_RENDER, RADEON_TRACE, "%s\n", __func__);
154
155 #if MESA_BIG_ENDIAN
156 if (mesa_ind_buf->type == GL_UNSIGNED_INT) {
157 #else
158 if (mesa_ind_buf->type != GL_UNSIGNED_BYTE) {
159 #endif
160 const GLvoid *src_ptr;
161 GLvoid *dst_ptr;
162 GLboolean mapped_named_bo = GL_FALSE;
163
164 if (mesa_ind_buf->obj->Name && !mesa_ind_buf->obj->Pointer) {
165 ctx->Driver.MapBuffer(ctx, GL_ELEMENT_ARRAY_BUFFER, GL_READ_ONLY_ARB, mesa_ind_buf->obj);
166 assert(mesa_ind_buf->obj->Pointer != NULL);
167 mapped_named_bo = GL_TRUE;
168 }
169
170 src_ptr = ADD_POINTERS(mesa_ind_buf->obj->Pointer, mesa_ind_buf->ptr);
171
172 const GLuint size = mesa_ind_buf->count * getTypeSize(mesa_ind_buf->type);
173
174 radeonAllocDmaRegion(&r300->radeon, &r300->ind_buf.bo, &r300->ind_buf.bo_offset, size, 4);
175
176 assert(r300->ind_buf.bo->ptr != NULL);
177 dst_ptr = ADD_POINTERS(r300->ind_buf.bo->ptr, r300->ind_buf.bo_offset);
178 _mesa_memcpy(dst_ptr, src_ptr, size);
179
180 r300->ind_buf.is_32bit = (mesa_ind_buf->type == GL_UNSIGNED_INT);
181 r300->ind_buf.count = mesa_ind_buf->count;
182
183 if (mapped_named_bo) {
184 ctx->Driver.UnmapBuffer(ctx, GL_ELEMENT_ARRAY_BUFFER, mesa_ind_buf->obj);
185 }
186 } else {
187 r300FixupIndexBuffer(ctx, mesa_ind_buf);
188 }
189 }
190
191 #define CONVERT( TYPE, MACRO ) do { \
192 GLuint i, j, sz; \
193 sz = input->Size; \
194 if (input->Normalized) { \
195 for (i = 0; i < count; i++) { \
196 const TYPE *in = (TYPE *)src_ptr; \
197 for (j = 0; j < sz; j++) { \
198 *dst_ptr++ = MACRO(*in); \
199 in++; \
200 } \
201 src_ptr += stride; \
202 } \
203 } else { \
204 for (i = 0; i < count; i++) { \
205 const TYPE *in = (TYPE *)src_ptr; \
206 for (j = 0; j < sz; j++) { \
207 *dst_ptr++ = (GLfloat)(*in); \
208 in++; \
209 } \
210 src_ptr += stride; \
211 } \
212 } \
213 } while (0)
214
215 /**
216 * Convert attribute data type to float
217 * If the attribute uses named buffer object replace the bo with newly allocated bo
218 */
219 static void r300ConvertAttrib(GLcontext *ctx, int count, const struct gl_client_array *input, struct vertex_attribute *attr)
220 {
221 r300ContextPtr r300 = R300_CONTEXT(ctx);
222 const GLvoid *src_ptr;
223 GLboolean mapped_named_bo = GL_FALSE;
224 GLfloat *dst_ptr;
225 GLuint stride;
226
227 stride = (input->StrideB == 0) ? getTypeSize(input->Type) * input->Size : input->StrideB;
228
229 /* Convert value for first element only */
230 if (input->StrideB == 0)
231 count = 1;
232
233 if (input->BufferObj->Name) {
234 if (!input->BufferObj->Pointer) {
235 ctx->Driver.MapBuffer(ctx, GL_ARRAY_BUFFER, GL_READ_ONLY_ARB, input->BufferObj);
236 mapped_named_bo = GL_TRUE;
237 }
238
239 src_ptr = ADD_POINTERS(input->BufferObj->Pointer, input->Ptr);
240 } else {
241 src_ptr = input->Ptr;
242 }
243
244 radeonAllocDmaRegion(&r300->radeon, &attr->bo, &attr->bo_offset, sizeof(GLfloat) * input->Size * count, 32);
245 dst_ptr = (GLfloat *)ADD_POINTERS(attr->bo->ptr, attr->bo_offset);
246
247 radeon_print(RADEON_FALLBACKS, RADEON_IMPORTANT,
248 "%s: Converting vertex attributes, attribute data format %x,"
249 "stride %d, components %d\n"
250 , __FUNCTION__, input->Type
251 , stride, input->Size);
252
253 assert(src_ptr != NULL);
254
255 switch (input->Type) {
256 case GL_DOUBLE:
257 CONVERT(GLdouble, (GLfloat));
258 break;
259 case GL_UNSIGNED_INT:
260 CONVERT(GLuint, UINT_TO_FLOAT);
261 break;
262 case GL_INT:
263 CONVERT(GLint, INT_TO_FLOAT);
264 break;
265 case GL_UNSIGNED_SHORT:
266 CONVERT(GLushort, USHORT_TO_FLOAT);
267 break;
268 case GL_SHORT:
269 CONVERT(GLshort, SHORT_TO_FLOAT);
270 break;
271 case GL_UNSIGNED_BYTE:
272 assert(input->Format != GL_BGRA);
273 CONVERT(GLubyte, UBYTE_TO_FLOAT);
274 break;
275 case GL_BYTE:
276 CONVERT(GLbyte, BYTE_TO_FLOAT);
277 break;
278 default:
279 assert(0);
280 break;
281 }
282
283 if (mapped_named_bo) {
284 ctx->Driver.UnmapBuffer(ctx, GL_ARRAY_BUFFER, input->BufferObj);
285 }
286 }
287
288 static void r300AlignDataToDword(GLcontext *ctx, const struct gl_client_array *input, int count, struct vertex_attribute *attr)
289 {
290 r300ContextPtr r300 = R300_CONTEXT(ctx);
291 const int dst_stride = (input->StrideB + 3) & ~3;
292 const int size = getTypeSize(input->Type) * input->Size * count;
293 GLboolean mapped_named_bo = GL_FALSE;
294
295 radeonAllocDmaRegion(&r300->radeon, &attr->bo, &attr->bo_offset, size, 32);
296
297 if (!input->BufferObj->Pointer) {
298 ctx->Driver.MapBuffer(ctx, GL_ARRAY_BUFFER, GL_READ_ONLY_ARB, input->BufferObj);
299 mapped_named_bo = GL_TRUE;
300 }
301
302 radeon_print(RADEON_FALLBACKS, RADEON_IMPORTANT, "%s. Vertex alignment doesn't match hw requirements.\n", __func__);
303
304 {
305 GLvoid *src_ptr = ADD_POINTERS(input->BufferObj->Pointer, input->Ptr);
306 GLvoid *dst_ptr = ADD_POINTERS(attr->bo->ptr, attr->bo_offset);
307 int i;
308
309 for (i = 0; i < count; ++i) {
310 _mesa_memcpy(dst_ptr, src_ptr, input->StrideB);
311 src_ptr += input->StrideB;
312 dst_ptr += dst_stride;
313 }
314 }
315
316 if (mapped_named_bo) {
317 ctx->Driver.UnmapBuffer(ctx, GL_ARRAY_BUFFER, input->BufferObj);
318 }
319
320 attr->stride = dst_stride;
321 }
322
323 static void r300TranslateAttrib(GLcontext *ctx, GLuint attr, int count, const struct gl_client_array *input)
324 {
325 r300ContextPtr r300 = R300_CONTEXT(ctx);
326 struct r300_vertex_buffer *vbuf = &r300->vbuf;
327 struct vertex_attribute r300_attr;
328 GLenum type;
329 GLuint stride;
330
331 radeon_print(RADEON_RENDER, RADEON_TRACE, "%s\n", __func__);
332 stride = (input->StrideB == 0) ? getTypeSize(input->Type) * input->Size : input->StrideB;
333
334 if (input->Type == GL_DOUBLE || input->Type == GL_UNSIGNED_INT || input->Type == GL_INT ||
335 #if MESA_BIG_ENDIAN
336 getTypeSize(input->Type) != 4 ||
337 #endif
338 stride < 4) {
339
340 type = GL_FLOAT;
341
342 if (input->StrideB == 0) {
343 r300_attr.stride = 0;
344 } else {
345 r300_attr.stride = sizeof(GLfloat) * input->Size;
346 }
347 r300_attr.dwords = input->Size;
348 r300_attr.is_named_bo = GL_FALSE;
349 } else {
350 type = input->Type;
351 r300_attr.dwords = (getTypeSize(type) * input->Size + 3)/ 4;
352 if (!input->BufferObj->Name) {
353
354 if (input->StrideB == 0) {
355 r300_attr.stride = 0;
356 } else {
357 r300_attr.stride = (getTypeSize(type) * input->Size + 3) & ~3;
358 }
359
360 r300_attr.is_named_bo = GL_FALSE;
361 }
362 }
363
364 r300_attr.size = input->Size;
365 r300_attr.element = attr;
366 r300_attr.dst_loc = vbuf->num_attribs;
367
368 switch (type) {
369 case GL_FLOAT:
370 switch (input->Size) {
371 case 1: r300_attr.data_type = R300_DATA_TYPE_FLOAT_1; break;
372 case 2: r300_attr.data_type = R300_DATA_TYPE_FLOAT_2; break;
373 case 3: r300_attr.data_type = R300_DATA_TYPE_FLOAT_3; break;
374 case 4: r300_attr.data_type = R300_DATA_TYPE_FLOAT_4; break;
375 }
376 r300_attr._signed = 0;
377 r300_attr.normalize = 0;
378 break;
379 case GL_SHORT:
380 r300_attr._signed = 1;
381 r300_attr.normalize = input->Normalized;
382 switch (input->Size) {
383 case 1:
384 case 2:
385 r300_attr.data_type = R300_DATA_TYPE_SHORT_2;
386 break;
387 case 3:
388 case 4:
389 r300_attr.data_type = R300_DATA_TYPE_SHORT_4;
390 break;
391 }
392 break;
393 case GL_BYTE:
394 r300_attr._signed = 1;
395 r300_attr.normalize = input->Normalized;
396 r300_attr.data_type = R300_DATA_TYPE_BYTE;
397 break;
398 case GL_UNSIGNED_SHORT:
399 r300_attr._signed = 0;
400 r300_attr.normalize = input->Normalized;
401 switch (input->Size) {
402 case 1:
403 case 2:
404 r300_attr.data_type = R300_DATA_TYPE_SHORT_2;
405 break;
406 case 3:
407 case 4:
408 r300_attr.data_type = R300_DATA_TYPE_SHORT_4;
409 break;
410 }
411 break;
412 case GL_UNSIGNED_BYTE:
413 r300_attr._signed = 0;
414 r300_attr.normalize = input->Normalized;
415 if (input->Format == GL_BGRA)
416 r300_attr.data_type = R300_DATA_TYPE_D3DCOLOR;
417 else
418 r300_attr.data_type = R300_DATA_TYPE_BYTE;
419 break;
420
421 default:
422 case GL_DOUBLE:
423 case GL_INT:
424 case GL_UNSIGNED_INT:
425 assert(0);
426 break;
427 }
428
429 switch (input->Size) {
430 case 4:
431 r300_attr.swizzle = SWIZZLE_XYZW;
432 break;
433 case 3:
434 r300_attr.swizzle = MAKE_SWIZZLE4(SWIZZLE_X, SWIZZLE_Y, SWIZZLE_Z, SWIZZLE_ONE);
435 break;
436 case 2:
437 r300_attr.swizzle = MAKE_SWIZZLE4(SWIZZLE_X, SWIZZLE_Y, SWIZZLE_ZERO, SWIZZLE_ONE);
438 break;
439 case 1:
440 r300_attr.swizzle = MAKE_SWIZZLE4(SWIZZLE_X, SWIZZLE_ZERO, SWIZZLE_ZERO, SWIZZLE_ONE);
441 break;
442 }
443
444 r300_attr.write_mask = MASK_XYZW;
445
446 vbuf->attribs[vbuf->num_attribs] = r300_attr;
447 ++vbuf->num_attribs;
448 }
449
450 static void r300SetVertexFormat(GLcontext *ctx, const struct gl_client_array *arrays[], int count)
451 {
452 r300ContextPtr r300 = R300_CONTEXT(ctx);
453 struct r300_vertex_buffer *vbuf = &r300->vbuf;
454 radeon_print(RADEON_RENDER, RADEON_VERBOSE, "%s\n", __func__);
455 {
456 int i, tmp;
457
458 tmp = r300->selected_vp->code.InputsRead;
459 i = 0;
460 vbuf->num_attribs = 0;
461 while (tmp) {
462 /* find first enabled bit */
463 while (!(tmp & 1)) {
464 tmp >>= 1;
465 ++i;
466 }
467
468 r300TranslateAttrib(ctx, i, count, arrays[i]);
469
470 tmp >>= 1;
471 ++i;
472 }
473 }
474
475 r300SwitchFallback(ctx, R300_FALLBACK_AOS_LIMIT, vbuf->num_attribs > R300_MAX_AOS_ARRAYS);
476 if (r300->fallback)
477 return;
478 }
479
480 static void r300AllocDmaRegions(GLcontext *ctx, const struct gl_client_array *input[], int count)
481 {
482 r300ContextPtr r300 = R300_CONTEXT(ctx);
483 struct r300_vertex_buffer *vbuf = &r300->vbuf;
484 GLuint stride;
485 int ret;
486 int i, index;
487 radeon_print(RADEON_RENDER, RADEON_VERBOSE,
488 "%s: count %d num_attribs %d\n",
489 __func__, count, vbuf->num_attribs);
490
491 for (index = 0; index < vbuf->num_attribs; index++) {
492 struct radeon_aos *aos = &r300->radeon.tcl.aos[index];
493 i = vbuf->attribs[index].element;
494
495 stride = (input[i]->StrideB == 0) ? getTypeSize(input[i]->Type) * input[i]->Size : input[i]->StrideB;
496
497 if (input[i]->Type == GL_DOUBLE || input[i]->Type == GL_UNSIGNED_INT || input[i]->Type == GL_INT ||
498 #if MESA_BIG_ENDIAN
499 getTypeSize(input[i]->Type) != 4 ||
500 #endif
501 stride < 4) {
502
503 r300ConvertAttrib(ctx, count, input[i], &vbuf->attribs[index]);
504 } else {
505 if (input[i]->BufferObj->Name) {
506 if (stride % 4 != 0) {
507 assert(((intptr_t) input[i]->Ptr) % input[i]->StrideB == 0);
508 r300AlignDataToDword(ctx, input[i], count, &vbuf->attribs[index]);
509 vbuf->attribs[index].is_named_bo = GL_FALSE;
510 } else {
511 vbuf->attribs[index].stride = input[i]->StrideB;
512 vbuf->attribs[index].bo_offset = (intptr_t) input[i]->Ptr;
513 vbuf->attribs[index].bo = get_radeon_buffer_object(input[i]->BufferObj)->bo;
514 vbuf->attribs[index].is_named_bo = GL_TRUE;
515 }
516 } else {
517
518 int size;
519 int local_count = count;
520 uint32_t *dst;
521
522 if (input[i]->StrideB == 0) {
523 size = getTypeSize(input[i]->Type) * input[i]->Size;
524 local_count = 1;
525 } else {
526 size = getTypeSize(input[i]->Type) * input[i]->Size * local_count;
527 }
528
529 radeonAllocDmaRegion(&r300->radeon, &vbuf->attribs[index].bo, &vbuf->attribs[index].bo_offset, size, 32);
530 assert(vbuf->attribs[index].bo->ptr != NULL);
531 dst = (uint32_t *)ADD_POINTERS(vbuf->attribs[index].bo->ptr, vbuf->attribs[index].bo_offset);
532 switch (vbuf->attribs[index].dwords) {
533 case 1: radeonEmitVec4(dst, input[i]->Ptr, input[i]->StrideB, local_count); break;
534 case 2: radeonEmitVec8(dst, input[i]->Ptr, input[i]->StrideB, local_count); break;
535 case 3: radeonEmitVec12(dst, input[i]->Ptr, input[i]->StrideB, local_count); break;
536 case 4: radeonEmitVec16(dst, input[i]->Ptr, input[i]->StrideB, local_count); break;
537 default: assert(0); break;
538 }
539
540 }
541 }
542
543 aos->count = vbuf->attribs[index].stride == 0 ? 1 : count;
544 aos->stride = vbuf->attribs[index].stride / sizeof(float);
545 aos->components = vbuf->attribs[index].dwords;
546 aos->bo = vbuf->attribs[index].bo;
547 aos->offset = vbuf->attribs[index].bo_offset;
548
549 if (vbuf->attribs[index].is_named_bo) {
550 radeon_cs_space_add_persistent_bo(r300->radeon.cmdbuf.cs, r300->vbuf.attribs[index].bo, RADEON_GEM_DOMAIN_GTT, 0);
551 }
552 }
553
554 r300->radeon.tcl.aos_count = vbuf->num_attribs;
555 ret = radeon_cs_space_check_with_bo(r300->radeon.cmdbuf.cs, first_elem(&r300->radeon.dma.reserved)->bo, RADEON_GEM_DOMAIN_GTT, 0);
556 r300SwitchFallback(ctx, R300_FALLBACK_INVALID_BUFFERS, ret);
557
558 }
559
560 static void r300FreeData(GLcontext *ctx)
561 {
562 /* Need to zero tcl.aos[n].bo and tcl.elt_dma_bo
563 * to prevent double unref in radeonReleaseArrays
564 * called during context destroy
565 */
566 radeon_print(RADEON_RENDER, RADEON_VERBOSE, "%s\n", __func__);
567 r300ContextPtr r300 = R300_CONTEXT(ctx);
568 {
569 int i;
570
571 for (i = 0; i < r300->vbuf.num_attribs; i++) {
572 if (!r300->vbuf.attribs[i].is_named_bo) {
573 radeon_bo_unref(r300->vbuf.attribs[i].bo);
574 }
575 r300->radeon.tcl.aos[i].bo = NULL;
576 }
577 }
578
579 {
580 if (r300->ind_buf.bo != NULL) {
581 radeon_bo_unref(r300->ind_buf.bo);
582 }
583 }
584 }
585
586 static GLuint r300PredictTryDrawPrimsSize(GLcontext *ctx, GLuint nr_prims)
587 {
588 struct r300_context *r300 = R300_CONTEXT(ctx);
589 struct r300_vertex_buffer *vbuf = &r300->vbuf;
590 GLboolean flushed;
591 GLuint dwords;
592 GLuint state_size;
593
594 dwords = 2*CACHE_FLUSH_BUFSZ;
595 dwords += PRE_EMIT_STATE_BUFSZ;
596 dwords += (AOS_BUFSZ(vbuf->num_attribs)
597 + SCISSORS_BUFSZ*2
598 + FIREAOS_BUFSZ )*nr_prims;
599
600 state_size = radeonCountStateEmitSize(&r300->radeon);
601 flushed = rcommonEnsureCmdBufSpace(&r300->radeon,
602 dwords + state_size,
603 __FUNCTION__);
604 if (flushed)
605 dwords += radeonCountStateEmitSize(&r300->radeon);
606 else
607 dwords += state_size;
608
609 radeon_print(RADEON_RENDER, RADEON_VERBOSE, "%s: total prediction size is %d.\n", __FUNCTION__, dwords);
610 return dwords;
611 }
612
613 static GLboolean r300TryDrawPrims(GLcontext *ctx,
614 const struct gl_client_array *arrays[],
615 const struct _mesa_prim *prim,
616 GLuint nr_prims,
617 const struct _mesa_index_buffer *ib,
618 GLuint min_index,
619 GLuint max_index )
620 {
621 struct r300_context *r300 = R300_CONTEXT(ctx);
622 GLuint i;
623
624 radeon_print(RADEON_RENDER, RADEON_NORMAL, "%s: %u (%d-%d) cs begin at %d\n",
625 __FUNCTION__, nr_prims, min_index, max_index, r300->radeon.cmdbuf.cs->cdw );
626
627 if (ctx->NewState)
628 _mesa_update_state( ctx );
629
630 if (r300->options.hw_tcl_enabled)
631 _tnl_UpdateFixedFunctionProgram(ctx);
632
633 r300UpdateShaders(r300);
634
635 r300SwitchFallback(ctx, R300_FALLBACK_INVALID_BUFFERS, !r300ValidateBuffers(ctx));
636
637 r300SetVertexFormat(ctx, arrays, max_index + 1);
638
639 if (r300->fallback)
640 return GL_FALSE;
641
642 r300SetupVAP(ctx, r300->selected_vp->code.InputsRead, r300->selected_vp->code.OutputsWritten);
643
644 r300UpdateShaderStates(r300);
645
646 /* ensure we have the cmd buf space in advance to cover
647 * the state + DMA AOS pointers */
648 GLuint emit_end = r300PredictTryDrawPrimsSize(ctx, nr_prims)
649 + r300->radeon.cmdbuf.cs->cdw;
650
651 r300SetupIndexBuffer(ctx, ib);
652
653 r300AllocDmaRegions(ctx, arrays, max_index + 1);
654
655 if (r300->fallback)
656 return GL_FALSE;
657
658 r300EmitCacheFlush(r300);
659 radeonEmitState(&r300->radeon);
660
661 for (i = 0; i < nr_prims; ++i) {
662 r300RunRenderPrimitive(ctx, prim[i].start, prim[i].start + prim[i].count, prim[i].mode);
663 }
664
665 r300EmitCacheFlush(r300);
666
667 r300FreeData(ctx);
668
669 radeon_print(RADEON_RENDER, RADEON_VERBOSE, "%s: %u (%d-%d) cs ending at %d\n",
670 __FUNCTION__, nr_prims, min_index, max_index, r300->radeon.cmdbuf.cs->cdw );
671
672 if (emit_end < r300->radeon.cmdbuf.cs->cdw)
673 WARN_ONCE("Rendering was %d commands larger than predicted size."
674 " We might overflow command buffer.\n", r300->radeon.cmdbuf.cs->cdw - emit_end);
675
676 return GL_TRUE;
677 }
678
679 static void r300DrawPrims(GLcontext *ctx,
680 const struct gl_client_array *arrays[],
681 const struct _mesa_prim *prim,
682 GLuint nr_prims,
683 const struct _mesa_index_buffer *ib,
684 GLboolean index_bounds_valid,
685 GLuint min_index,
686 GLuint max_index)
687 {
688 GLboolean retval;
689
690 /* This check should get folded into just the places that
691 * min/max index are really needed.
692 */
693 if (!index_bounds_valid) {
694 vbo_get_minmax_index(ctx, prim, ib, &min_index, &max_index);
695 }
696
697 if (min_index) {
698 radeon_print(RADEON_FALLBACKS, RADEON_IMPORTANT,
699 "%s: Rebasing primitives. %p nr_prims %d min_index %u max_index %u\n",
700 __func__, prim, nr_prims, min_index, max_index);
701 vbo_rebase_prims( ctx, arrays, prim, nr_prims, ib, min_index, max_index, r300DrawPrims );
702 return;
703 }
704
705 /* Make an attempt at drawing */
706 retval = r300TryDrawPrims(ctx, arrays, prim, nr_prims, ib, min_index, max_index);
707
708 /* If failed run tnl pipeline - it should take care of fallbacks */
709 if (!retval)
710 _tnl_draw_prims(ctx, arrays, prim, nr_prims, ib, min_index, max_index);
711 }
712
713 void r300InitDraw(GLcontext *ctx)
714 {
715 struct vbo_context *vbo = vbo_context(ctx);
716
717 vbo->draw_prims = r300DrawPrims;
718 }