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