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