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