Tizen 2.0 Release
[profile/ivi/osmesa.git] / src / gallium / drivers / 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 "pipe/p_context.h"
29 #include "util/u_inlines.h"
30
31 #include "util/u_upload_mgr.h"
32 #include "util/u_math.h"
33
34 #include "brw_draw.h"
35 #include "brw_defines.h"
36 #include "brw_context.h"
37 #include "brw_state.h"
38 #include "brw_screen.h"
39 #include "brw_batchbuffer.h"
40 #include "brw_debug.h"
41 #include "brw_resource.h"
42
43
44
45
46 static unsigned get_index_type(int type)
47 {
48    switch (type) {
49    case 1: return BRW_INDEX_BYTE;
50    case 2: return BRW_INDEX_WORD;
51    case 4: return BRW_INDEX_DWORD;
52    default: assert(0); return 0;
53    }
54 }
55
56
57 static int brw_prepare_vertices(struct brw_context *brw)
58 {
59    unsigned int min_index = brw->curr.min_index;
60    unsigned int max_index = brw->curr.max_index;
61    GLuint i;
62    int ret;
63
64    if (BRW_DEBUG & DEBUG_VERTS)
65       debug_printf("%s %d..%d\n", __FUNCTION__, min_index, max_index);
66
67
68    for (i = 0; i < brw->curr.num_vertex_buffers; i++) {
69       struct pipe_vertex_buffer *vb = &brw->curr.vertex_buffer[i];
70       struct brw_winsys_buffer *bo;
71       struct pipe_resource *upload_buf = NULL;
72       unsigned offset;
73       
74       if (BRW_DEBUG & DEBUG_VERTS)
75          debug_printf("%s vb[%d] user:%d offset:0x%x sz:0x%x stride:0x%x\n",
76                       __FUNCTION__, i,
77                       brw_buffer_is_user_buffer(vb->buffer),
78                       vb->buffer_offset,
79                       vb->buffer->width0,
80                       vb->stride);
81
82       if (brw_buffer_is_user_buffer(vb->buffer)) {
83
84          /* XXX: simplify this.  Stop the state trackers from generating
85           * zero-stride buffers & have them use additional constants (or
86           * add support for >1 constant buffer) instead.
87           */
88          unsigned size = (vb->stride == 0 ? 
89                           vb->buffer->width0 - vb->buffer_offset :
90                           MAX2(vb->buffer->width0 - vb->buffer_offset,
91                                vb->stride * (max_index + 1 - min_index)));
92          boolean flushed;
93
94          ret = u_upload_buffer( brw->vb.upload_vertex,
95                                 0,
96                                 vb->buffer_offset + min_index * vb->stride,
97                                 size,
98                                 vb->buffer,
99                                 &offset,
100                                 &upload_buf,
101                                 &flushed );
102          if (ret)
103             return ret;
104
105          bo = brw_buffer(upload_buf)->bo;
106          
107          assert(offset + size <= bo->size);
108       }
109       else
110       {
111          offset = vb->buffer_offset;
112          bo = brw_buffer(vb->buffer)->bo;
113       }
114
115       assert(offset < bo->size);
116       
117       /* Set up post-upload info about this vertex buffer:
118        */
119       brw->vb.vb[i].offset = offset;
120       brw->vb.vb[i].stride = vb->stride;
121       brw->vb.vb[i].vertex_count = (vb->stride == 0 ?
122                                     1 :
123                                     (bo->size - offset) / vb->stride);
124
125       bo_reference( &brw->vb.vb[i].bo,  bo );
126
127       /* Don't need to retain this reference.  We have a reference on
128        * the underlying winsys buffer:
129        */
130       pipe_resource_reference( &upload_buf, NULL );
131    }
132
133    brw->vb.nr_vb = i;
134    brw_prepare_query_begin(brw);
135
136    for (i = 0; i < brw->vb.nr_vb; i++) {
137       brw_add_validated_bo(brw, brw->vb.vb[i].bo);
138    }
139
140    return 0;
141 }
142
143 static int brw_emit_vertex_buffers( struct brw_context *brw )
144 {
145    int i;
146
147    /* If the VS doesn't read any inputs (calculating vertex position from
148     * a state variable for some reason, for example), just bail.
149     *
150     * The stale VB state stays in place, but they don't do anything unless
151     * a VE loads from them.
152     */
153    if (brw->vb.nr_vb == 0) {
154       if (BRW_DEBUG & DEBUG_VERTS)
155          debug_printf("%s: no active vertex buffers\n", __FUNCTION__);
156
157       return 0;
158    }
159
160    /* Emit VB state packets.
161     */
162    BEGIN_BATCH(1 + brw->vb.nr_vb * 4, IGNORE_CLIPRECTS);
163    OUT_BATCH((CMD_VERTEX_BUFFER << 16) |
164              ((1 + brw->vb.nr_vb * 4) - 2));
165
166    for (i = 0; i < brw->vb.nr_vb; i++) {
167       OUT_BATCH((i << BRW_VB0_INDEX_SHIFT) |
168                 BRW_VB0_ACCESS_VERTEXDATA |
169                 (brw->vb.vb[i].stride << BRW_VB0_PITCH_SHIFT));
170       OUT_RELOC(brw->vb.vb[i].bo,
171                 BRW_USAGE_VERTEX,
172                 brw->vb.vb[i].offset);
173       if (brw->gen == 5) {
174          OUT_RELOC(brw->vb.vb[i].bo,
175                    BRW_USAGE_VERTEX,
176                    brw->vb.vb[i].bo->size - 1);
177       } else
178          OUT_BATCH(brw->vb.vb[i].stride ? brw->vb.vb[i].vertex_count : 0);
179       OUT_BATCH(0); /* Instance data step rate */
180    }
181    ADVANCE_BATCH();
182    return 0;
183 }
184
185
186
187 static int brw_emit_vertex_elements(struct brw_context *brw)
188 {
189    const struct brw_vertex_element_packet *brw_velems = brw->curr.velems;
190    unsigned size = brw_velems->header.length + 2;
191
192    /* why is this here */
193    brw_emit_query_begin(brw);
194
195    brw_batchbuffer_data(brw->batch, brw_velems, size * 4, IGNORE_CLIPRECTS);
196
197    return 0;
198 }
199
200
201 static int brw_emit_vertices( struct brw_context *brw )
202 {
203    int ret;
204
205    ret = brw_emit_vertex_buffers( brw );
206    if (ret)
207       return ret;
208
209    /* XXX should separate this? */
210    ret = brw_emit_vertex_elements( brw );
211    if (ret)
212       return ret;
213
214    return 0;
215 }
216
217
218 const struct brw_tracked_state brw_vertices = {
219    .dirty = {
220       .mesa = (PIPE_NEW_INDEX_RANGE |
221                PIPE_NEW_VERTEX_BUFFER |
222                PIPE_NEW_VERTEX_ELEMENT),
223       .brw = BRW_NEW_BATCH,
224       .cache = 0,
225    },
226    .prepare = brw_prepare_vertices,
227    .emit = brw_emit_vertices,
228 };
229
230
231 static int brw_prepare_indices(struct brw_context *brw)
232 {
233    struct pipe_resource *index_buffer = brw->curr.index_buffer;
234    struct pipe_resource *upload_buf = NULL;
235    struct brw_winsys_buffer *bo = NULL;
236    GLuint offset;
237    GLuint index_size, index_offset;
238    GLuint ib_size;
239    int ret;
240
241    if (index_buffer == NULL)
242       return 0;
243
244    if (BRW_DEBUG & DEBUG_VERTS)
245       debug_printf("%s: index_size:%d index_buffer->size:%d\n",
246                    __FUNCTION__,
247                    brw->curr.index_size,
248                    brw->curr.index_buffer->width0);
249
250    ib_size = index_buffer->width0;
251    index_size = brw->curr.index_size;
252    index_offset = brw->curr.index_offset;
253
254    /* Turn userbuffer into a proper hardware buffer?
255     */
256    if (brw_buffer_is_user_buffer(index_buffer)) {
257       boolean flushed;
258
259       ret = u_upload_buffer( brw->vb.upload_index,
260                              0,
261                              index_offset,
262                              ib_size,
263                              index_buffer,
264                              &offset,
265                              &upload_buf,
266                              &flushed );
267       if (ret)
268          return ret;
269
270       bo = brw_buffer(upload_buf)->bo;
271
272       /* XXX: annotate the userbuffer with the upload information so
273        * that successive calls don't get re-uploaded.
274        */
275    }
276    else {
277       bo = brw_buffer(index_buffer)->bo;
278       ib_size = bo->size;
279       offset = index_offset;
280    }
281
282    /* Use CMD_3D_PRIM's start_vertex_offset to avoid re-uploading the
283     * index buffer state when we're just moving the start index of our
284     * drawing.
285     *
286     * In gallium this will happen in the case where successive draw
287     * calls are made with (distinct?) userbuffers, but the upload_mgr
288     * places the data into a single winsys buffer.
289     * 
290     * This statechange doesn't raise any state flags and is always
291     * just merged into the final draw packet:
292     */
293    if (1) {
294       assert((offset & (index_size - 1)) == 0);
295       brw->ib.start_vertex_offset = offset / index_size;
296    }
297
298    /* These statechanges trigger a new CMD_INDEX_BUFFER packet:
299     */
300    if (brw->ib.bo != bo ||
301        brw->ib.size != ib_size)
302    {
303       bo_reference(&brw->ib.bo, bo);
304       brw->ib.size = ib_size;
305       brw->state.dirty.brw |= BRW_NEW_INDEX_BUFFER;
306    }
307
308    pipe_resource_reference( &upload_buf, NULL );
309    brw_add_validated_bo(brw, brw->ib.bo);
310    return 0;
311 }
312
313 const struct brw_tracked_state brw_indices = {
314    .dirty = {
315       .mesa = PIPE_NEW_INDEX_BUFFER,
316       .brw = 0,
317       .cache = 0,
318    },
319    .prepare = brw_prepare_indices,
320 };
321
322 static int brw_emit_index_buffer(struct brw_context *brw)
323 {
324    /* Emit the indexbuffer packet:
325     */
326    if (brw->ib.bo)
327    {
328       struct brw_indexbuffer ib;
329
330       memset(&ib, 0, sizeof(ib));
331
332       ib.header.bits.opcode = CMD_INDEX_BUFFER;
333       ib.header.bits.length = sizeof(ib)/4 - 2;
334       ib.header.bits.index_format = get_index_type(brw->ib.size);
335       ib.header.bits.cut_index_enable = 0;
336
337       BEGIN_BATCH(4, IGNORE_CLIPRECTS);
338       OUT_BATCH( ib.header.dword );
339       OUT_RELOC(brw->ib.bo,
340                 BRW_USAGE_VERTEX,
341                 brw->ib.offset);
342       OUT_RELOC(brw->ib.bo,
343                 BRW_USAGE_VERTEX,
344                 brw->ib.offset + brw->ib.size - 1);
345       OUT_BATCH( 0 );
346       ADVANCE_BATCH();
347    }
348
349    return 0;
350 }
351
352 const struct brw_tracked_state brw_index_buffer = {
353    .dirty = {
354       .mesa = 0,
355       .brw = BRW_NEW_BATCH | BRW_NEW_INDEX_BUFFER,
356       .cache = 0,
357    },
358    .emit = brw_emit_index_buffer,
359 };