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