OSDN Git Service

[965] Remove dead structure in brw_draw_upload.c.
[android-x86/external-mesa.git] / src / mesa / drivers / dri / i965 / brw_draw_upload.c
1 /**************************************************************************
2  * 
3  * Copyright 2003 Tungsten Graphics, Inc., Cedar Park, Texas.
4  * All Rights Reserved.
5  * 
6  * Permission is hereby granted, free of charge, to any person obtaining a
7  * copy of this software and associated documentation files (the
8  * "Software"), to deal in the Software without restriction, including
9  * without limitation the rights to use, copy, modify, merge, publish,
10  * distribute, sub license, and/or sell copies of the Software, and to
11  * permit persons to whom the Software is furnished to do so, subject to
12  * the following conditions:
13  * 
14  * The above copyright notice and this permission notice (including the
15  * next paragraph) shall be included in all copies or substantial portions
16  * of the Software.
17  * 
18  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21  * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
22  * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23  * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24  * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25  * 
26  **************************************************************************/
27
28 #include <stdlib.h>
29
30 #include "glheader.h"
31 #include "context.h"
32 #include "state.h"
33 #include "api_validate.h"
34 #include "enums.h"
35
36 #include "brw_draw.h"
37 #include "brw_defines.h"
38 #include "brw_context.h"
39 #include "brw_state.h"
40 #include "brw_fallback.h"
41
42 #include "intel_ioctl.h"
43 #include "intel_batchbuffer.h"
44 #include "intel_buffer_objects.h"
45 #include "intel_tex.h"
46
47 static dri_bo *array_buffer( struct intel_context *intel,
48                              const struct gl_client_array *array )
49 {
50    return intel_bufferobj_buffer(intel, intel_buffer_object(array->BufferObj),
51                                  INTEL_WRITE_PART);
52 }
53
54 static GLuint double_types[5] = {
55    0,
56    BRW_SURFACEFORMAT_R64_FLOAT,
57    BRW_SURFACEFORMAT_R64G64_FLOAT,
58    BRW_SURFACEFORMAT_R64G64B64_FLOAT,
59    BRW_SURFACEFORMAT_R64G64B64A64_FLOAT
60 };
61
62 static GLuint float_types[5] = {
63    0,
64    BRW_SURFACEFORMAT_R32_FLOAT,
65    BRW_SURFACEFORMAT_R32G32_FLOAT,
66    BRW_SURFACEFORMAT_R32G32B32_FLOAT,
67    BRW_SURFACEFORMAT_R32G32B32A32_FLOAT
68 };
69
70 static GLuint uint_types_norm[5] = {
71    0,
72    BRW_SURFACEFORMAT_R32_UNORM,
73    BRW_SURFACEFORMAT_R32G32_UNORM,
74    BRW_SURFACEFORMAT_R32G32B32_UNORM,
75    BRW_SURFACEFORMAT_R32G32B32A32_UNORM
76 };
77
78 static GLuint uint_types_scale[5] = {
79    0,
80    BRW_SURFACEFORMAT_R32_USCALED,
81    BRW_SURFACEFORMAT_R32G32_USCALED,
82    BRW_SURFACEFORMAT_R32G32B32_USCALED,
83    BRW_SURFACEFORMAT_R32G32B32A32_USCALED
84 };
85
86 static GLuint int_types_norm[5] = {
87    0,
88    BRW_SURFACEFORMAT_R32_SNORM,
89    BRW_SURFACEFORMAT_R32G32_SNORM,
90    BRW_SURFACEFORMAT_R32G32B32_SNORM,
91    BRW_SURFACEFORMAT_R32G32B32A32_SNORM
92 };
93
94 static GLuint int_types_scale[5] = {
95    0,
96    BRW_SURFACEFORMAT_R32_SSCALED,
97    BRW_SURFACEFORMAT_R32G32_SSCALED,
98    BRW_SURFACEFORMAT_R32G32B32_SSCALED,
99    BRW_SURFACEFORMAT_R32G32B32A32_SSCALED
100 };
101
102 static GLuint ushort_types_norm[5] = {
103    0,
104    BRW_SURFACEFORMAT_R16_UNORM,
105    BRW_SURFACEFORMAT_R16G16_UNORM,
106    BRW_SURFACEFORMAT_R16G16B16_UNORM,
107    BRW_SURFACEFORMAT_R16G16B16A16_UNORM
108 };
109
110 static GLuint ushort_types_scale[5] = {
111    0,
112    BRW_SURFACEFORMAT_R16_USCALED,
113    BRW_SURFACEFORMAT_R16G16_USCALED,
114    BRW_SURFACEFORMAT_R16G16B16_USCALED,
115    BRW_SURFACEFORMAT_R16G16B16A16_USCALED
116 };
117
118 static GLuint short_types_norm[5] = {
119    0,
120    BRW_SURFACEFORMAT_R16_SNORM,
121    BRW_SURFACEFORMAT_R16G16_SNORM,
122    BRW_SURFACEFORMAT_R16G16B16_SNORM,
123    BRW_SURFACEFORMAT_R16G16B16A16_SNORM
124 };
125
126 static GLuint short_types_scale[5] = {
127    0,
128    BRW_SURFACEFORMAT_R16_SSCALED,
129    BRW_SURFACEFORMAT_R16G16_SSCALED,
130    BRW_SURFACEFORMAT_R16G16B16_SSCALED,
131    BRW_SURFACEFORMAT_R16G16B16A16_SSCALED
132 };
133
134 static GLuint ubyte_types_norm[5] = {
135    0,
136    BRW_SURFACEFORMAT_R8_UNORM,
137    BRW_SURFACEFORMAT_R8G8_UNORM,
138    BRW_SURFACEFORMAT_R8G8B8_UNORM,
139    BRW_SURFACEFORMAT_R8G8B8A8_UNORM
140 };
141
142 static GLuint ubyte_types_scale[5] = {
143    0,
144    BRW_SURFACEFORMAT_R8_USCALED,
145    BRW_SURFACEFORMAT_R8G8_USCALED,
146    BRW_SURFACEFORMAT_R8G8B8_USCALED,
147    BRW_SURFACEFORMAT_R8G8B8A8_USCALED
148 };
149
150 static GLuint byte_types_norm[5] = {
151    0,
152    BRW_SURFACEFORMAT_R8_SNORM,
153    BRW_SURFACEFORMAT_R8G8_SNORM,
154    BRW_SURFACEFORMAT_R8G8B8_SNORM,
155    BRW_SURFACEFORMAT_R8G8B8A8_SNORM
156 };
157
158 static GLuint byte_types_scale[5] = {
159    0,
160    BRW_SURFACEFORMAT_R8_SSCALED,
161    BRW_SURFACEFORMAT_R8G8_SSCALED,
162    BRW_SURFACEFORMAT_R8G8B8_SSCALED,
163    BRW_SURFACEFORMAT_R8G8B8A8_SSCALED
164 };
165
166
167 static GLuint get_surface_type( GLenum type, GLuint size, GLboolean normalized )
168 {
169    if (INTEL_DEBUG & DEBUG_VERTS)
170       _mesa_printf("type %s size %d normalized %d\n", 
171                    _mesa_lookup_enum_by_nr(type), size, normalized);
172
173    if (normalized) {
174       switch (type) {
175       case GL_DOUBLE: return double_types[size];
176       case GL_FLOAT: return float_types[size];
177       case GL_INT: return int_types_norm[size];
178       case GL_SHORT: return short_types_norm[size];
179       case GL_BYTE: return byte_types_norm[size];
180       case GL_UNSIGNED_INT: return uint_types_norm[size];
181       case GL_UNSIGNED_SHORT: return ushort_types_norm[size];
182       case GL_UNSIGNED_BYTE: return ubyte_types_norm[size];
183       default: assert(0); return 0;
184       }      
185    }
186    else {
187       switch (type) {
188       case GL_DOUBLE: return double_types[size];
189       case GL_FLOAT: return float_types[size];
190       case GL_INT: return int_types_scale[size];
191       case GL_SHORT: return short_types_scale[size];
192       case GL_BYTE: return byte_types_scale[size];
193       case GL_UNSIGNED_INT: return uint_types_scale[size];
194       case GL_UNSIGNED_SHORT: return ushort_types_scale[size];
195       case GL_UNSIGNED_BYTE: return ubyte_types_scale[size];
196       default: assert(0); return 0;
197       }      
198    }
199 }
200
201
202 static GLuint get_size( GLenum type )
203 {
204    switch (type) {
205    case GL_DOUBLE: return sizeof(GLdouble);
206    case GL_FLOAT: return sizeof(GLfloat);
207    case GL_INT: return sizeof(GLint);
208    case GL_SHORT: return sizeof(GLshort);
209    case GL_BYTE: return sizeof(GLbyte);
210    case GL_UNSIGNED_INT: return sizeof(GLuint);
211    case GL_UNSIGNED_SHORT: return sizeof(GLushort);
212    case GL_UNSIGNED_BYTE: return sizeof(GLubyte);
213    default: return 0;
214    }      
215 }
216
217 static GLuint get_index_type(GLenum type) 
218 {
219    switch (type) {
220    case GL_UNSIGNED_BYTE:  return BRW_INDEX_BYTE;
221    case GL_UNSIGNED_SHORT: return BRW_INDEX_WORD;
222    case GL_UNSIGNED_INT:   return BRW_INDEX_DWORD;
223    default: assert(0); return 0;
224    }
225 }
226
227 static void copy_strided_array( GLubyte *dest, 
228                                 const GLubyte *src, 
229                                 GLuint size, 
230                                 GLuint stride,
231                                 GLuint count )
232 {
233    if (size == stride) 
234       memcpy(dest, src, count * size);
235    else {
236       GLuint i;
237    
238       for (i = 0; i < count; i++) {
239          memcpy(dest, src, size);
240          src += stride;
241          dest += size;
242       }
243    }
244 }
245
246 static void wrap_buffers( struct brw_context *brw,
247                           GLuint size )
248 {
249    GLcontext *ctx = &brw->intel.ctx;
250
251    if (size < BRW_UPLOAD_INIT_SIZE)
252       size = BRW_UPLOAD_INIT_SIZE;
253
254    brw->vb.upload.buf++;
255    brw->vb.upload.buf %= BRW_NR_UPLOAD_BUFS;
256    brw->vb.upload.offset = 0;
257
258    ctx->Driver.BufferData(ctx,
259                           GL_ARRAY_BUFFER_ARB,
260                           size,
261                           NULL,
262                           GL_DYNAMIC_DRAW_ARB,
263                           brw->vb.upload.vbo[brw->vb.upload.buf]);
264 }
265
266 static void get_space( struct brw_context *brw,
267                        GLuint size,
268                        struct gl_buffer_object **vbo_return,
269                        GLuint *offset_return )
270 {
271    size = ALIGN(size, 64);
272    
273    if (brw->vb.upload.offset + size > BRW_UPLOAD_INIT_SIZE)
274       wrap_buffers(brw, size);
275
276    *vbo_return = brw->vb.upload.vbo[brw->vb.upload.buf];
277    *offset_return = brw->vb.upload.offset;
278
279    brw->vb.upload.offset += size;
280 }
281
282 static void
283 copy_array_to_vbo_array( struct brw_context *brw,
284                          struct gl_client_array *vbo_array,
285                          const struct gl_client_array *array,
286                          GLuint element_size,
287                          GLuint count)
288 {
289    GLcontext *ctx = &brw->intel.ctx;
290    GLuint size = count * element_size;
291    struct gl_buffer_object *vbo;
292    GLuint offset;
293    GLuint new_stride;
294
295    get_space(brw, size, &vbo, &offset);
296
297    if (array->StrideB == 0) {
298       assert(count == 1);
299       new_stride = 0;
300    }
301    else 
302       new_stride = element_size;
303
304    vbo_array->Size = array->Size;
305    vbo_array->Type = array->Type;
306    vbo_array->Stride = new_stride;
307    vbo_array->StrideB = new_stride;   
308    vbo_array->Ptr = (const void *)offset;
309    vbo_array->Enabled = 1;
310    vbo_array->Normalized = array->Normalized;
311    vbo_array->_MaxElement = array->_MaxElement; /* ? */
312    vbo_array->BufferObj = vbo;
313
314    {
315       GLubyte *map = ctx->Driver.MapBuffer(ctx,
316                                            GL_ARRAY_BUFFER_ARB,
317                                            GL_DYNAMIC_DRAW_ARB,
318                                            vbo);
319    
320       map += offset;
321
322       copy_strided_array( map, 
323                           array->Ptr,
324                           element_size,
325                           array->StrideB,
326                           count);
327
328       ctx->Driver.UnmapBuffer(ctx, GL_ARRAY_BUFFER_ARB, vbo_array->BufferObj);
329    }
330 }
331
332 /**
333  * Just a wrapper to highlight which cause of copy_array_to_vbo_array
334  * is happening in the profile.
335  */
336 static void
337 interleaved_copy_array_to_vbo_array(struct brw_context *brw,
338                                     struct gl_client_array *vbo_array,
339                                     const struct gl_client_array *array,
340                                     GLuint element_size,
341                                     GLuint count)
342 {
343    copy_array_to_vbo_array(brw, vbo_array, array, element_size, count);
344 }
345
346 static void
347 interleaved_vbo_array( struct brw_context *brw,
348                        struct gl_client_array *vbo_array,
349                        const struct gl_client_array *uploaded_array,
350                        const struct gl_client_array *array,
351                        const char *ptr)
352 {
353    vbo_array->Size = array->Size;
354    vbo_array->Type = array->Type;
355    vbo_array->Stride = array->Stride;
356    vbo_array->StrideB = array->StrideB;   
357    vbo_array->Ptr = (const void *)((const char *)uploaded_array->Ptr + 
358                                    ((const char *)array->Ptr - ptr));
359    vbo_array->Enabled = 1;
360    vbo_array->Normalized = array->Normalized;
361    vbo_array->_MaxElement = array->_MaxElement; 
362    vbo_array->BufferObj = uploaded_array->BufferObj;
363 }
364
365
366 GLboolean brw_upload_vertices( struct brw_context *brw,
367                                GLuint min_index,
368                                GLuint max_index )
369 {
370    GLcontext *ctx = &brw->intel.ctx;
371    struct intel_context *intel = intel_context(ctx);
372    GLuint tmp = brw->vs.prog_data->inputs_read; 
373    GLuint i;
374    const void *ptr = NULL;
375    GLuint interleave = 0;
376    struct gl_client_array vbo_array_temp[VERT_ATTRIB_MAX];
377
378    struct brw_vertex_element *enabled[VERT_ATTRIB_MAX];
379    GLuint nr_enabled = 0;
380
381    struct brw_vertex_element *upload[VERT_ATTRIB_MAX];
382    GLuint nr_uploads = 0;
383
384    /* First build an array of pointers to ve's in vb.inputs_read
385     */
386    if (0)
387       _mesa_printf("%s %d..%d\n", __FUNCTION__, min_index, max_index);
388    
389    while (tmp) {
390       GLuint i = _mesa_ffsll(tmp)-1;
391       struct brw_vertex_element *input = &brw->vb.inputs[i];
392
393       tmp &= ~(1<<i);
394       enabled[nr_enabled++] = input;
395
396       input->element_size = get_size(input->glarray->Type) * input->glarray->Size;
397       input->count = input->glarray->StrideB ? max_index + 1 - min_index : 1;
398
399       if (!input->glarray->BufferObj->Name) {
400          if (i == 0) {
401             /* Position array not properly enabled:
402              */
403             if (input->glarray->StrideB == 0)
404                return GL_FALSE;
405
406             interleave = input->glarray->StrideB;
407             ptr = input->glarray->Ptr;
408          }
409          else if (interleave != input->glarray->StrideB ||
410                   (const char *)input->glarray->Ptr - (const char *)ptr < 0 ||
411                   (const char *)input->glarray->Ptr - (const char *)ptr > interleave) {
412             interleave = 0;
413          }
414
415          upload[nr_uploads++] = input;
416          
417          /* We rebase drawing to start at element zero only when
418           * varyings are not in vbos, which means we can end up
419           * uploading non-varying arrays (stride != 0) when min_index
420           * is zero.  This doesn't matter as the amount to upload is
421           * the same for these arrays whether the draw call is rebased
422           * or not - we just have to upload the one element.
423           */
424          assert(min_index == 0 || input->glarray->StrideB == 0);
425       }
426    }
427
428    /* Upload interleaved arrays if all uploads are interleaved
429     */
430    if (nr_uploads > 1 && interleave && interleave <= 256) {
431       interleaved_copy_array_to_vbo_array(brw, &vbo_array_temp[0],
432                                           upload[0]->glarray,
433                                           interleave,
434                                           upload[0]->count);
435       upload[0]->glarray = &vbo_array_temp[0];
436
437       for (i = 1; i < nr_uploads; i++) {
438          interleaved_vbo_array(brw,
439                                &vbo_array_temp[i],
440                                upload[0]->glarray,
441                                upload[i]->glarray,
442                                ptr);
443          upload[i]->glarray = &vbo_array_temp[i];
444       }
445    }
446    else {
447       for (i = 0; i < nr_uploads; i++) {
448          copy_array_to_vbo_array(brw, &vbo_array_temp[i],
449                                  upload[i]->glarray,
450                                  upload[i]->element_size,
451                                  upload[i]->count);
452          upload[i]->glarray = &vbo_array_temp[i];
453       }
454    }
455
456    /* XXX: In the rare cases where this happens we fallback all
457     * the way to software rasterization, although a tnl fallback
458     * would be sufficient.  I don't know of *any* real world
459     * cases with > 17 vertex attributes enabled, so it probably
460     * isn't an issue at this point.
461     */
462    if (nr_enabled >= BRW_VEP_MAX)
463          return GL_FALSE;
464
465    /* Now emit VB and VEP state packets.
466     *
467     * This still defines a hardware VB for each input, even if they
468     * are interleaved or from the same VBO.  TBD if this makes a
469     * performance difference.
470     */
471    BEGIN_BATCH(1 + nr_enabled * 4, IGNORE_CLIPRECTS);
472    OUT_BATCH((CMD_VERTEX_BUFFER << 16) |
473              ((1 + nr_enabled * 4) - 2));
474
475    for (i = 0; i < nr_enabled; i++) {
476       struct brw_vertex_element *input = enabled[i];
477
478       OUT_BATCH((i << BRW_VB0_INDEX_SHIFT) |
479                 BRW_VB0_ACCESS_VERTEXDATA |
480                 (input->glarray->StrideB << BRW_VB0_PITCH_SHIFT));
481       OUT_RELOC(array_buffer(intel, input->glarray),
482                 DRM_BO_FLAG_MEM_TT | DRM_BO_FLAG_READ,
483                 (GLuint)input->glarray->Ptr);
484       OUT_BATCH(max_index);
485       OUT_BATCH(0); /* Instance data step rate */
486    }
487    ADVANCE_BATCH();
488
489    BEGIN_BATCH(1 + nr_enabled * 2, IGNORE_CLIPRECTS);
490    OUT_BATCH((CMD_VERTEX_ELEMENT << 16) | ((1 + nr_enabled * 2) - 2));
491    for (i = 0; i < nr_enabled; i++) {
492       struct brw_vertex_element *input = enabled[i];
493       uint32_t format = get_surface_type(input->glarray->Type,
494                                          input->glarray->Size,
495                                          input->glarray->Normalized);
496       uint32_t comp0 = BRW_VE1_COMPONENT_STORE_SRC;
497       uint32_t comp1 = BRW_VE1_COMPONENT_STORE_SRC;
498       uint32_t comp2 = BRW_VE1_COMPONENT_STORE_SRC;
499       uint32_t comp3 = BRW_VE1_COMPONENT_STORE_SRC;
500
501       switch (input->glarray->Size) {
502       case 0: comp0 = BRW_VE1_COMPONENT_STORE_0;
503       case 1: comp1 = BRW_VE1_COMPONENT_STORE_0;
504       case 2: comp2 = BRW_VE1_COMPONENT_STORE_0;
505       case 3: comp3 = BRW_VE1_COMPONENT_STORE_1_FLT;
506          break;
507       }
508
509       OUT_BATCH((i << BRW_VE0_INDEX_SHIFT) |
510                 BRW_VE0_VALID |
511                 (format << BRW_VE0_FORMAT_SHIFT) |
512                 (0 << BRW_VE0_SRC_OFFSET_SHIFT));
513       OUT_BATCH((comp0 << BRW_VE1_COMPONENT_0_SHIFT) |
514                 (comp1 << BRW_VE1_COMPONENT_1_SHIFT) |
515                 (comp2 << BRW_VE1_COMPONENT_2_SHIFT) |
516                 (comp3 << BRW_VE1_COMPONENT_3_SHIFT) |
517                 ((i * 4) << BRW_VE1_DST_OFFSET_SHIFT));
518    }
519    ADVANCE_BATCH();
520
521    return GL_TRUE;
522 }
523
524 void brw_upload_indices( struct brw_context *brw,
525                          const struct _mesa_index_buffer *index_buffer )
526 {
527    GLcontext *ctx = &brw->intel.ctx;
528    struct intel_context *intel = &brw->intel;
529    GLuint ib_size = get_size(index_buffer->type) * index_buffer->count;
530    struct gl_buffer_object *bufferobj = index_buffer->obj;
531    GLuint offset = (GLuint)index_buffer->ptr;
532
533    /* Turn into a proper VBO:
534     */
535    if (!bufferobj->Name) {
536      
537       /* Get new bufferobj, offset:
538        */
539       get_space(brw, ib_size, &bufferobj, &offset);
540
541       /* Straight upload
542        */
543       ctx->Driver.BufferSubData( ctx,
544                                  GL_ELEMENT_ARRAY_BUFFER_ARB,
545                                  offset, 
546                                  ib_size,
547                                  index_buffer->ptr,
548                                  bufferobj);
549    } else {
550       /* If the index buffer isn't aligned to its element size, we have to
551        * rebase it into a temporary.
552        */
553        if ((get_size(index_buffer->type) - 1) & offset) {
554            struct gl_buffer_object *vbo;
555            GLuint voffset;
556            GLubyte *map = ctx->Driver.MapBuffer(ctx,
557                                                 GL_ELEMENT_ARRAY_BUFFER_ARB,
558                                                 GL_DYNAMIC_DRAW_ARB,
559                                                 bufferobj);
560            map += offset;
561            get_space(brw, ib_size, &vbo, &voffset);
562            
563            ctx->Driver.BufferSubData(ctx,
564                                      GL_ELEMENT_ARRAY_BUFFER_ARB,
565                                      voffset,
566                                      ib_size,
567                                      map,
568                                      vbo);
569            ctx->Driver.UnmapBuffer(ctx, GL_ELEMENT_ARRAY_BUFFER_ARB, bufferobj);
570
571            bufferobj = vbo;
572            offset = voffset;
573        }
574    }
575
576    /* Emit the indexbuffer packet:
577     */
578    {
579       struct brw_indexbuffer ib;
580       dri_bo *buffer = intel_bufferobj_buffer(intel,
581                                               intel_buffer_object(bufferobj),
582                                               INTEL_READ);
583
584       memset(&ib, 0, sizeof(ib));
585    
586       ib.header.bits.opcode = CMD_INDEX_BUFFER;
587       ib.header.bits.length = sizeof(ib)/4 - 2;
588       ib.header.bits.index_format = get_index_type(index_buffer->type);
589       ib.header.bits.cut_index_enable = 0;
590    
591
592       BEGIN_BATCH(4, IGNORE_CLIPRECTS);
593       OUT_BATCH( ib.header.dword );
594       OUT_RELOC( buffer, DRM_BO_FLAG_MEM_TT | DRM_BO_FLAG_READ, offset);
595       OUT_RELOC( buffer, DRM_BO_FLAG_MEM_TT | DRM_BO_FLAG_READ,
596                  offset + ib_size);
597       OUT_BATCH( 0 );
598       ADVANCE_BATCH();
599    }
600 }