Revert "Revert "Merge branch 'drm-gem'""
[mesa.git] / src / mesa / drivers / dri / i965 / brw_draw.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
46 #include "tnl/tnl.h"
47 #include "vbo/vbo_context.h"
48 #include "swrast/swrast.h"
49 #include "swrast_setup/swrast_setup.h"
50
51 #define FILE_DEBUG_FLAG DEBUG_BATCH
52
53 static GLuint hw_prim[GL_POLYGON+1] = {
54 _3DPRIM_POINTLIST,
55 _3DPRIM_LINELIST,
56 _3DPRIM_LINELOOP,
57 _3DPRIM_LINESTRIP,
58 _3DPRIM_TRILIST,
59 _3DPRIM_TRISTRIP,
60 _3DPRIM_TRIFAN,
61 _3DPRIM_QUADLIST,
62 _3DPRIM_QUADSTRIP,
63 _3DPRIM_POLYGON
64 };
65
66
67 static const GLenum reduced_prim[GL_POLYGON+1] = {
68 GL_POINTS,
69 GL_LINES,
70 GL_LINES,
71 GL_LINES,
72 GL_TRIANGLES,
73 GL_TRIANGLES,
74 GL_TRIANGLES,
75 GL_TRIANGLES,
76 GL_TRIANGLES,
77 GL_TRIANGLES
78 };
79
80
81 /* When the primitive changes, set a state bit and re-validate. Not
82 * the nicest and would rather deal with this by having all the
83 * programs be immune to the active primitive (ie. cope with all
84 * possibilities). That may not be realistic however.
85 */
86 static GLuint brw_set_prim(struct brw_context *brw, GLenum prim)
87 {
88 if (INTEL_DEBUG & DEBUG_PRIMS)
89 _mesa_printf("PRIM: %s\n", _mesa_lookup_enum_by_nr(prim));
90
91 /* Slight optimization to avoid the GS program when not needed:
92 */
93 if (prim == GL_QUAD_STRIP &&
94 brw->attribs.Light->ShadeModel != GL_FLAT &&
95 brw->attribs.Polygon->FrontMode == GL_FILL &&
96 brw->attribs.Polygon->BackMode == GL_FILL)
97 prim = GL_TRIANGLE_STRIP;
98
99 if (prim != brw->primitive) {
100 brw->primitive = prim;
101 brw->state.dirty.brw |= BRW_NEW_PRIMITIVE;
102
103 if (reduced_prim[prim] != brw->intel.reduced_primitive) {
104 brw->intel.reduced_primitive = reduced_prim[prim];
105 brw->state.dirty.brw |= BRW_NEW_REDUCED_PRIMITIVE;
106 }
107
108 brw_validate_state(brw);
109 }
110
111 return hw_prim[prim];
112 }
113
114
115 static GLuint trim(GLenum prim, GLuint length)
116 {
117 if (prim == GL_QUAD_STRIP)
118 return length > 3 ? (length - length % 2) : 0;
119 else if (prim == GL_QUADS)
120 return length - length % 4;
121 else
122 return length;
123 }
124
125
126 static void brw_emit_prim( struct brw_context *brw,
127 const struct _mesa_prim *prim )
128
129 {
130 struct brw_3d_primitive prim_packet;
131
132 if (INTEL_DEBUG & DEBUG_PRIMS)
133 _mesa_printf("PRIM: %s %d %d\n", _mesa_lookup_enum_by_nr(prim->mode),
134 prim->start, prim->count);
135
136 prim_packet.header.opcode = CMD_3D_PRIM;
137 prim_packet.header.length = sizeof(prim_packet)/4 - 2;
138 prim_packet.header.pad = 0;
139 prim_packet.header.topology = brw_set_prim(brw, prim->mode);
140 prim_packet.header.indexed = prim->indexed;
141
142 prim_packet.verts_per_instance = trim(prim->mode, prim->count);
143 prim_packet.start_vert_location = prim->start;
144 prim_packet.instance_count = 1;
145 prim_packet.start_instance_location = 0;
146 prim_packet.base_vert_location = 0;
147
148 /* Can't wrap here, since we rely on the validated state. */
149 brw->no_batch_wrap = GL_TRUE;
150 if (prim_packet.verts_per_instance) {
151 intel_batchbuffer_data( brw->intel.batch, &prim_packet,
152 sizeof(prim_packet), LOOP_CLIPRECTS);
153 }
154 brw->no_batch_wrap = GL_FALSE;
155 }
156
157 static void brw_merge_inputs( struct brw_context *brw,
158 const struct gl_client_array *arrays[])
159 {
160 struct brw_vertex_element *inputs = brw->vb.inputs;
161 struct brw_vertex_info old = brw->vb.info;
162 GLuint i;
163
164 memset(inputs, 0, sizeof(*inputs));
165 memset(&brw->vb.info, 0, sizeof(brw->vb.info));
166
167 for (i = 0; i < VERT_ATTRIB_MAX; i++) {
168 brw->vb.inputs[i].glarray = arrays[i];
169
170 /* XXX: metaops passes null arrays */
171 if (arrays[i]) {
172 if (arrays[i]->StrideB != 0)
173 brw->vb.info.varying |= 1 << i;
174
175 brw->vb.info.sizes[i/16] |= (inputs[i].glarray->Size - 1) << ((i%16) * 2);
176 }
177 }
178
179 /* Raise statechanges if input sizes and varying have changed:
180 */
181 if (memcmp(brw->vb.info.sizes, old.sizes, sizeof(old.sizes)) != 0)
182 brw->state.dirty.brw |= BRW_NEW_INPUT_DIMENSIONS;
183
184 if (brw->vb.info.varying != old.varying)
185 brw->state.dirty.brw |= BRW_NEW_INPUT_VARYING;
186 }
187
188 /* XXX: could split the primitive list to fallback only on the
189 * non-conformant primitives.
190 */
191 static GLboolean check_fallbacks( struct brw_context *brw,
192 const struct _mesa_prim *prim,
193 GLuint nr_prims )
194 {
195 GLuint i;
196
197 if (!brw->intel.strict_conformance)
198 return GL_FALSE;
199
200 if (brw->attribs.Polygon->SmoothFlag) {
201 for (i = 0; i < nr_prims; i++)
202 if (reduced_prim[prim[i].mode] == GL_TRIANGLES)
203 return GL_TRUE;
204 }
205
206 /* BRW hardware will do AA lines, but they are non-conformant it
207 * seems. TBD whether we keep this fallback:
208 */
209 if (brw->attribs.Line->SmoothFlag) {
210 for (i = 0; i < nr_prims; i++)
211 if (reduced_prim[prim[i].mode] == GL_LINES)
212 return GL_TRUE;
213 }
214
215 /* Stipple -- these fallbacks could be resolved with a little
216 * bit of work?
217 */
218 if (brw->attribs.Line->StippleFlag) {
219 for (i = 0; i < nr_prims; i++) {
220 /* GS doesn't get enough information to know when to reset
221 * the stipple counter?!?
222 */
223 if (prim[i].mode == GL_LINE_LOOP)
224 return GL_TRUE;
225
226 if (prim[i].mode == GL_POLYGON &&
227 (brw->attribs.Polygon->FrontMode == GL_LINE ||
228 brw->attribs.Polygon->BackMode == GL_LINE))
229 return GL_TRUE;
230 }
231 }
232
233
234 if (brw->attribs.Point->SmoothFlag) {
235 for (i = 0; i < nr_prims; i++)
236 if (prim[i].mode == GL_POINTS)
237 return GL_TRUE;
238 }
239
240 return GL_FALSE;
241 }
242
243 /* May fail if out of video memory for texture or vbo upload, or on
244 * fallback conditions.
245 */
246 static GLboolean brw_try_draw_prims( GLcontext *ctx,
247 const struct gl_client_array *arrays[],
248 const struct _mesa_prim *prim,
249 GLuint nr_prims,
250 const struct _mesa_index_buffer *ib,
251 GLuint min_index,
252 GLuint max_index )
253 {
254 struct intel_context *intel = intel_context(ctx);
255 struct brw_context *brw = brw_context(ctx);
256 GLboolean retval = GL_FALSE;
257 GLuint i;
258
259 if (ctx->NewState)
260 _mesa_update_state( ctx );
261
262 brw_validate_textures( brw );
263
264 /* Bind all inputs, derive varying and size information:
265 */
266 brw_merge_inputs( brw, arrays );
267
268 brw->ib.ib = ib;
269 brw->state.dirty.brw |= BRW_NEW_INDICES;
270
271 brw->vb.min_index = min_index;
272 brw->vb.max_index = max_index;
273 brw->state.dirty.brw |= BRW_NEW_VERTICES;
274 /* Have to validate state quite late. Will rebuild tnl_program,
275 * which depends on varying information.
276 *
277 * Note this is where brw->vs->prog_data.inputs_read is calculated,
278 * so can't access it earlier.
279 */
280
281 LOCK_HARDWARE(intel);
282
283 if (brw->intel.numClipRects == 0) {
284 UNLOCK_HARDWARE(intel);
285 return GL_TRUE;
286 }
287
288 {
289 /* Flush the batch if it's approaching full, so that we don't wrap while
290 * we've got validated state that needs to be in the same batch as the
291 * primitives. This fraction is just a guess (minimal full state plus
292 * a primitive is around 512 bytes), and would be better if we had
293 * an upper bound of how much we might emit in a single
294 * brw_try_draw_prims().
295 */
296 if (intel->batch->ptr - intel->batch->map > intel->batch->size * 3 / 4
297 /* brw_emit_prim may change the cliprect_mode to LOOP_CLIPRECTS */
298 || intel->batch->cliprect_mode != LOOP_CLIPRECTS)
299 intel_batchbuffer_flush(intel->batch);
300
301 /* Set the first primitive early, ahead of validate_state:
302 */
303 brw_set_prim(brw, prim[0].mode);
304
305 /* XXX: Need to separate validate and upload of state.
306 */
307 brw_validate_state( brw );
308
309 /* Various fallback checks:
310 */
311 if (brw->intel.Fallback)
312 goto out;
313
314 if (check_fallbacks( brw, prim, nr_prims ))
315 goto out;
316
317 for (i = 0; i < nr_prims; i++) {
318 brw_emit_prim(brw, &prim[i]);
319 }
320
321 retval = GL_TRUE;
322 }
323
324 out:
325 UNLOCK_HARDWARE(intel);
326
327 if (!retval)
328 DBG("%s failed\n", __FUNCTION__);
329
330 return retval;
331 }
332
333 static GLboolean brw_need_rebase( GLcontext *ctx,
334 const struct gl_client_array *arrays[],
335 const struct _mesa_index_buffer *ib,
336 GLuint min_index )
337 {
338 if (min_index == 0)
339 return GL_FALSE;
340
341 if (ib) {
342 if (!vbo_all_varyings_in_vbos(arrays))
343 return GL_TRUE;
344 else
345 return GL_FALSE;
346 }
347 else {
348 /* Hmm. This isn't quite what I wanted. BRW can actually
349 * handle the mixed case well enough that we shouldn't need to
350 * rebase. However, it's probably not very common, nor hugely
351 * expensive to do it this way:
352 */
353 if (!vbo_all_varyings_in_vbos(arrays))
354 return GL_TRUE;
355 else
356 return GL_FALSE;
357 }
358 }
359
360
361 void brw_draw_prims( GLcontext *ctx,
362 const struct gl_client_array *arrays[],
363 const struct _mesa_prim *prim,
364 GLuint nr_prims,
365 const struct _mesa_index_buffer *ib,
366 GLuint min_index,
367 GLuint max_index )
368 {
369 GLboolean retval;
370
371 /* Decide if we want to rebase. If so we end up recursing once
372 * only into this function.
373 */
374 if (brw_need_rebase( ctx, arrays, ib, min_index )) {
375 vbo_rebase_prims( ctx, arrays,
376 prim, nr_prims,
377 ib, min_index, max_index,
378 brw_draw_prims );
379
380 return;
381 }
382
383
384 /* Make a first attempt at drawing:
385 */
386 retval = brw_try_draw_prims(ctx, arrays, prim, nr_prims, ib, min_index, max_index);
387
388 /* Otherwise, we really are out of memory. Pass the drawing
389 * command to the software tnl module and which will in turn call
390 * swrast to do the drawing.
391 */
392 if (!retval) {
393 _swsetup_Wakeup(ctx);
394 _tnl_draw_prims(ctx, arrays, prim, nr_prims, ib, min_index, max_index);
395 }
396 }
397
398 void brw_draw_init( struct brw_context *brw )
399 {
400 GLcontext *ctx = &brw->intel.ctx;
401 struct vbo_context *vbo = vbo_context(ctx);
402
403 /* Register our drawing function:
404 */
405 vbo->draw_prims = brw_draw_prims;
406 }
407
408 void brw_draw_destroy( struct brw_context *brw )
409 {
410 if (brw->vb.upload.bo != NULL) {
411 dri_bo_unreference(brw->vb.upload.bo);
412 brw->vb.upload.bo = NULL;
413 }
414 }