OSDN Git Service

vbo: Avoid extra validation of DrawElements.
[android-x86/external-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
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
42 #include "tnl/tnl.h"
43 #include "tnl/t_vp_build.h"
44 #include "vbo/vbo_context.h"
45 #include "swrast/swrast.h"
46 #include "swrast_setup/swrast_setup.h"
47
48 static void r300FixupIndexBuffer(GLcontext *ctx, const struct _mesa_index_buffer *mesa_ind_buf, struct gl_buffer_object **bo, GLuint *nr_bo)
49 {
50         r300ContextPtr r300 = R300_CONTEXT(ctx);
51         struct r300_index_buffer *ind_buf = &r300->ind_buf;
52         GLvoid *src_ptr;
53
54         if (!mesa_ind_buf) {
55                 ind_buf->ptr = NULL;
56                 return;
57         }
58
59         ind_buf->count = mesa_ind_buf->count;
60         if (mesa_ind_buf->obj->Name && !mesa_ind_buf->obj->Pointer) {
61                 bo[*nr_bo] = mesa_ind_buf->obj;
62                 (*nr_bo)++;
63                 ctx->Driver.MapBuffer(ctx, GL_ELEMENT_ARRAY_BUFFER, GL_READ_ONLY_ARB, mesa_ind_buf->obj);
64                 assert(mesa_ind_buf->obj->Pointer != NULL);
65         }
66         src_ptr = ADD_POINTERS(mesa_ind_buf->obj->Pointer, mesa_ind_buf->ptr);
67
68         if (mesa_ind_buf->type == GL_UNSIGNED_BYTE) {
69                 GLubyte *in = (GLubyte *)src_ptr;
70                 GLuint *out = _mesa_malloc(sizeof(GLushort) * ((mesa_ind_buf->count + 1) & ~1));
71                 int i;
72
73                 ind_buf->ptr = out;
74
75                 for (i = 0; i + 1 < mesa_ind_buf->count; i += 2) {
76                         *out++ = in[i] | in[i + 1] << 16;
77                 }
78
79                 if (i < mesa_ind_buf->count) {
80                         *out++ = in[i];
81                 }
82
83                 ind_buf->free_needed = GL_TRUE;
84                 ind_buf->is_32bit = GL_FALSE;
85         } else if (mesa_ind_buf->type == GL_UNSIGNED_SHORT) {
86 #if MESA_BIG_ENDIAN
87                 GLushort *in = (GLushort *)src_ptr;
88                 GLuint *out = _mesa_malloc(sizeof(GLushort) *
89                                            ((mesa_ind_buf->count + 1) & ~1));
90                 int i;
91
92                 ind_buf->ptr = out;
93
94                 for (i = 0; i + 1 < mesa_ind_buf->count; i += 2) {
95                         *out++ = in[i] | in[i + 1] << 16;
96                 }
97
98                 if (i < mesa_ind_buf->count) {
99                         *out++ = in[i];
100                 }
101
102                 ind_buf->free_needed = GL_TRUE;
103 #else
104                 ind_buf->ptr = src_ptr;
105                 ind_buf->free_needed = GL_FALSE;
106 #endif
107                 ind_buf->is_32bit = GL_FALSE;
108         } else {
109                 ind_buf->ptr = src_ptr;
110                 ind_buf->free_needed = GL_FALSE;
111                 ind_buf->is_32bit = GL_TRUE;
112         }
113 }
114
115 static int getTypeSize(GLenum type)
116 {
117         switch (type) {
118                 case GL_DOUBLE:
119                         return sizeof(GLdouble);
120                 case GL_FLOAT:
121                         return sizeof(GLfloat);
122                 case GL_INT:
123                         return sizeof(GLint);
124                 case GL_UNSIGNED_INT:
125                         return sizeof(GLuint);
126                 case GL_SHORT:
127                         return sizeof(GLshort);
128                 case GL_UNSIGNED_SHORT:
129                         return sizeof(GLushort);
130                 case GL_BYTE:
131                         return sizeof(GLbyte);
132                 case GL_UNSIGNED_BYTE:
133                         return sizeof(GLubyte);
134                 default:
135                         assert(0);
136                         return 0;
137         }
138 }
139
140 #define CONVERT( TYPE, MACRO ) do {             \
141         GLuint i, j, sz;                                \
142         sz = input->Size;                               \
143         if (input->Normalized) {                        \
144                 for (i = 0; i < count; i++) {           \
145                         const TYPE *in = (TYPE *)src_ptr;               \
146                         for (j = 0; j < sz; j++) {              \
147                                 *dst_ptr++ = MACRO(*in);                \
148                                 in++;                           \
149                         }                                       \
150                         src_ptr += stride;                      \
151                 }                                               \
152         } else {                                        \
153                 for (i = 0; i < count; i++) {           \
154                         const TYPE *in = (TYPE *)src_ptr;               \
155                         for (j = 0; j < sz; j++) {              \
156                                 *dst_ptr++ = (GLfloat)(*in);            \
157                                 in++;                           \
158                         }                                       \
159                         src_ptr += stride;                      \
160                 }                                               \
161         }                                               \
162 } while (0)
163
164 static void r300TranslateAttrib(GLcontext *ctx, GLuint attr, int count, const struct gl_client_array *input, struct gl_buffer_object **bo, GLuint *nr_bo)
165 {
166         r300ContextPtr r300 = R300_CONTEXT(ctx);
167         struct r300_vertex_buffer *vbuf = &r300->vbuf;
168         struct vertex_attribute r300_attr;
169         const void *src_ptr;
170         GLenum type;
171         GLuint stride;
172
173         if (input->BufferObj->Name) {
174                 if (!input->BufferObj->Pointer) {
175                         bo[*nr_bo] = input->BufferObj;
176                         (*nr_bo)++;
177                         ctx->Driver.MapBuffer(ctx, GL_ARRAY_BUFFER, GL_READ_ONLY_ARB, input->BufferObj);
178                         assert(input->BufferObj->Pointer != NULL);
179                 }
180
181                 src_ptr = ADD_POINTERS(input->BufferObj->Pointer, input->Ptr);
182         } else
183                 src_ptr = input->Ptr;
184
185         stride = (input->StrideB == 0) ? getTypeSize(input->Type) * input->Size : input->StrideB;
186
187         if (input->Type == GL_DOUBLE || input->Type == GL_UNSIGNED_INT || input->Type == GL_INT ||
188 #if MESA_BIG_ENDIAN
189             getTypeSize(input->Type) != 4 ||
190 #endif
191             stride < 4) {
192                 if (RADEON_DEBUG & DEBUG_FALLBACKS) {
193                         fprintf(stderr, "%s: Converting vertex attributes, attribute data format %x,", __FUNCTION__, input->Type);
194                         fprintf(stderr, "stride %d, components %d\n", stride, input->Size);
195                 }
196
197                 GLfloat *dst_ptr, *tmp;
198
199                 /* Convert value for first element only */
200                 if (input->StrideB == 0)
201                         count = 1;
202
203                 tmp = dst_ptr = _mesa_malloc(sizeof(GLfloat) * input->Size * count);
204
205                 switch (input->Type) {
206                         case GL_DOUBLE:
207                                 CONVERT(GLdouble, (GLfloat));
208                                 break;
209                         case GL_UNSIGNED_INT:
210                                 CONVERT(GLuint, UINT_TO_FLOAT);
211                                 break;
212                         case GL_INT:
213                                 CONVERT(GLint, INT_TO_FLOAT);
214                                 break;
215                         case GL_UNSIGNED_SHORT:
216                                 CONVERT(GLushort, USHORT_TO_FLOAT);
217                                 break;
218                         case GL_SHORT:
219                                 CONVERT(GLshort, SHORT_TO_FLOAT);
220                                 break;
221                         case GL_UNSIGNED_BYTE:
222                                 assert(input->Format != GL_BGRA);
223                                 CONVERT(GLubyte, UBYTE_TO_FLOAT);
224                                 break;
225                         case GL_BYTE:
226                                 CONVERT(GLbyte, BYTE_TO_FLOAT);
227                                 break;
228                         default:
229                                 assert(0);
230                                 break;
231                 }
232
233                 type = GL_FLOAT;
234                 r300_attr.free_needed = GL_TRUE;
235                 r300_attr.data = tmp;
236                 if (input->StrideB == 0) {
237                         r300_attr.stride = 0;
238                 } else {
239                         r300_attr.stride = sizeof(GLfloat) * input->Size;
240                 }
241                 r300_attr.dwords = input->Size;
242         } else {
243                 type = input->Type;
244                 r300_attr.free_needed = GL_FALSE;
245                 r300_attr.data = (GLvoid *)src_ptr;
246                 r300_attr.stride = input->StrideB;
247                 r300_attr.dwords = (getTypeSize(type) * input->Size  + 3)/ 4;
248         }
249
250         r300_attr.size = input->Size;
251         r300_attr.element = attr;
252         r300_attr.dst_loc = vbuf->num_attribs;
253
254         switch (type) {
255                 case GL_FLOAT:
256                         switch (input->Size) {
257                                 case 1: r300_attr.data_type = R300_DATA_TYPE_FLOAT_1; break;
258                                 case 2: r300_attr.data_type = R300_DATA_TYPE_FLOAT_2; break;
259                                 case 3: r300_attr.data_type = R300_DATA_TYPE_FLOAT_3; break;
260                                 case 4: r300_attr.data_type = R300_DATA_TYPE_FLOAT_4; break;
261                         }
262                         r300_attr._signed = 0;
263                         r300_attr.normalize = 0;
264                         break;
265                 case GL_SHORT:
266                         r300_attr._signed = 1;
267                         r300_attr.normalize = input->Normalized;
268                         switch (input->Size) {
269                                 case 1:
270                                 case 2:
271                                         r300_attr.data_type = R300_DATA_TYPE_SHORT_2;
272                                         break;
273                                 case 3:
274                                 case 4:
275                                         r300_attr.data_type = R300_DATA_TYPE_SHORT_4;
276                                         break;
277                         }
278                         break;
279                 case GL_BYTE:
280                         r300_attr._signed = 1;
281                         r300_attr.normalize = input->Normalized;
282                         r300_attr.data_type = R300_DATA_TYPE_BYTE;
283                         break;
284                 case GL_UNSIGNED_SHORT:
285                         r300_attr._signed = 0;
286                         r300_attr.normalize = input->Normalized;
287                         switch (input->Size) {
288                                 case 1:
289                                 case 2:
290                                         r300_attr.data_type = R300_DATA_TYPE_SHORT_2;
291                                         break;
292                                 case 3:
293                                 case 4:
294                                         r300_attr.data_type = R300_DATA_TYPE_SHORT_4;
295                                         break;
296                         }
297                         break;
298                 case GL_UNSIGNED_BYTE:
299                         r300_attr._signed = 0;
300                         r300_attr.normalize = input->Normalized;
301                         if (input->Format == GL_BGRA)
302                                 r300_attr.data_type = R300_DATA_TYPE_D3DCOLOR;
303                         else
304                                 r300_attr.data_type = R300_DATA_TYPE_BYTE;
305                         break;
306
307                 default:
308                 case GL_DOUBLE:
309                 case GL_INT:
310                 case GL_UNSIGNED_INT:
311                         assert(0);
312                         break;
313         }
314
315         switch (input->Size) {
316                 case 4:
317                         r300_attr.swizzle = SWIZZLE_XYZW;
318                         break;
319                 case 3:
320                         r300_attr.swizzle = MAKE_SWIZZLE4(SWIZZLE_X, SWIZZLE_Y, SWIZZLE_Z, SWIZZLE_ONE);
321                         break;
322                 case 2:
323                         r300_attr.swizzle = MAKE_SWIZZLE4(SWIZZLE_X, SWIZZLE_Y, SWIZZLE_ZERO, SWIZZLE_ONE);
324                         break;
325                 case 1:
326                         r300_attr.swizzle = MAKE_SWIZZLE4(SWIZZLE_X, SWIZZLE_ZERO, SWIZZLE_ZERO, SWIZZLE_ONE);
327                         break;
328         }
329
330         r300_attr.write_mask = MASK_XYZW;
331
332         vbuf->attribs[vbuf->num_attribs] = r300_attr;
333         ++vbuf->num_attribs;
334 }
335
336 static void r300SetVertexFormat(GLcontext *ctx, const struct gl_client_array *arrays[], int count, struct gl_buffer_object **bo, GLuint *nr_bo)
337 {
338         r300ContextPtr r300 = R300_CONTEXT(ctx);
339         struct r300_vertex_buffer *vbuf = &r300->vbuf;
340
341         {
342                 int i, tmp;
343
344                 tmp = r300->selected_vp->code.InputsRead;
345                 i = 0;
346                 vbuf->num_attribs = 0;
347                 while (tmp) {
348                         /* find first enabled bit */
349                         while (!(tmp & 1)) {
350                                 tmp >>= 1;
351                                 ++i;
352                         }
353
354                         r300TranslateAttrib(ctx, i, count, arrays[i], bo, nr_bo);
355
356                         tmp >>= 1;
357                         ++i;
358                 }
359         }
360
361         r300SwitchFallback(ctx, R300_FALLBACK_AOS_LIMIT, vbuf->num_attribs > R300_MAX_AOS_ARRAYS);
362         if (r300->fallback)
363                 return;
364
365         {
366                 int i;
367
368                 for (i = 0; i < vbuf->num_attribs; i++) {
369                         rcommon_emit_vector(ctx, &r300->radeon.tcl.aos[i],
370                                                 vbuf->attribs[i].data, vbuf->attribs[i].dwords,
371                                                 vbuf->attribs[i].stride, count);
372                 }
373
374                 r300->radeon.tcl.aos_count = vbuf->num_attribs;
375         }
376 }
377
378 static void r300FreeData(GLcontext *ctx, struct gl_buffer_object **bo, GLuint nr_bo)
379 {
380         {
381                 struct r300_vertex_buffer *vbuf = &R300_CONTEXT(ctx)->vbuf;
382                 int i;
383
384                 for (i = 0; i < vbuf->num_attribs; i++) {
385                         if (vbuf->attribs[i].free_needed)
386                                 _mesa_free(vbuf->attribs[i].data);
387                 }
388         }
389
390         {
391                 struct r300_index_buffer *ind_buf = &R300_CONTEXT(ctx)->ind_buf;
392                 if (ind_buf->free_needed)
393                         _mesa_free(ind_buf->ptr);
394         }
395
396         {
397                 int i;
398
399                 for (i = 0; i < nr_bo; ++i) {
400                         ctx->Driver.UnmapBuffer(ctx, 0, bo[i]);
401                 }
402         }
403 }
404
405 static GLboolean r300TryDrawPrims(GLcontext *ctx,
406                                          const struct gl_client_array *arrays[],
407                                          const struct _mesa_prim *prim,
408                                          GLuint nr_prims,
409                                          const struct _mesa_index_buffer *ib,
410                                          GLuint min_index,
411                                          GLuint max_index )
412 {
413         struct r300_context *r300 = R300_CONTEXT(ctx);
414         struct gl_buffer_object *bo[VERT_ATTRIB_MAX+1];
415         GLuint i, nr_bo = 0;
416
417         if (ctx->NewState)
418                 _mesa_update_state( ctx );
419
420         if (r300->options.hw_tcl_enabled)
421                 _tnl_UpdateFixedFunctionProgram(ctx);
422
423         r300UpdateShaders(r300);
424
425         r300SwitchFallback(ctx, R300_FALLBACK_INVALID_BUFFERS, !r300ValidateBuffers(ctx));
426
427         r300FixupIndexBuffer(ctx, ib, bo, &nr_bo);
428
429         /* ensure we have the cmd buf space in advance to cover
430          * the state + DMA AOS pointers */
431         rcommonEnsureCmdBufSpace(&r300->radeon,
432                            r300->radeon.hw.max_state_size + (50*sizeof(int)),
433                            __FUNCTION__);
434
435         r300SetVertexFormat(ctx, arrays, max_index + 1, bo, &nr_bo);
436
437         if (r300->fallback)
438                 return GL_FALSE;
439
440         r300SetupVAP(ctx, r300->selected_vp->code.InputsRead, r300->selected_vp->code.OutputsWritten);
441
442         r300UpdateShaderStates(r300);
443
444         r300EmitCacheFlush(r300);
445         radeonEmitState(&r300->radeon);
446
447         for (i = 0; i < nr_prims; ++i) {
448                 r300RunRenderPrimitive(ctx, prim[i].start, prim[i].start + prim[i].count, prim[i].mode);
449         }
450
451         r300EmitCacheFlush(r300);
452
453         radeonReleaseArrays(ctx, ~0);
454
455         r300FreeData(ctx, bo, nr_bo);
456
457         return GL_TRUE;
458 }
459
460 static void r300DrawPrims(GLcontext *ctx,
461                          const struct gl_client_array *arrays[],
462                          const struct _mesa_prim *prim,
463                          GLuint nr_prims,
464                          const struct _mesa_index_buffer *ib,
465                          GLboolean index_bounds_valid,
466                          GLuint min_index,
467                          GLuint max_index)
468 {
469         struct split_limits limits;
470         GLboolean retval;
471
472         if (ib)
473                 limits.max_verts = 0xffffffff;
474         else
475                 limits.max_verts = 65535;
476
477         limits.max_indices = 65535;
478         limits.max_vb_size = 1024*1024;
479
480         /* This check should get folded into just the places that
481          * min/max index are really needed.
482          */
483         if (!index_bounds_valid)
484            vbo_get_minmax_index(ctx, prim, ib, &min_index, &max_index);
485
486         if (min_index) {
487                 vbo_rebase_prims( ctx, arrays, prim, nr_prims, ib, min_index, max_index, r300DrawPrims );
488                 return;
489         }
490         if ((ib && ib->count > 65535)) {
491                 vbo_split_prims (ctx, arrays, prim, nr_prims, ib, min_index, max_index, r300DrawPrims, &limits);
492                 return;
493         }
494
495         /* Make an attempt at drawing */
496         retval = r300TryDrawPrims(ctx, arrays, prim, nr_prims, ib, min_index, max_index);
497
498         /* If failed run tnl pipeline - it should take care of fallbacks */
499         if (!retval)
500                 _tnl_draw_prims(ctx, arrays, prim, nr_prims, ib, min_index, max_index);
501 }
502
503 void r300InitDraw(GLcontext *ctx)
504 {
505         struct vbo_context *vbo = vbo_context(ctx);
506
507         vbo->draw_prims = r300DrawPrims;
508 }