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