Merge commit 'origin/7.8'
[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
29 #include "main/glheader.h"
30 #include "main/context.h"
31 #include "main/state.h"
32 #include "main/enums.h"
33 #include "tnl/tnl.h"
34 #include "vbo/vbo_context.h"
35 #include "swrast/swrast.h"
36 #include "swrast_setup/swrast_setup.h"
37
38 #include "brw_draw.h"
39 #include "brw_defines.h"
40 #include "brw_context.h"
41 #include "brw_state.h"
42
43 #include "intel_batchbuffer.h"
44
45 #define FILE_DEBUG_FLAG DEBUG_BATCH
46
47 static GLuint prim_to_hw_prim[GL_POLYGON+1] = {
48 _3DPRIM_POINTLIST,
49 _3DPRIM_LINELIST,
50 _3DPRIM_LINELOOP,
51 _3DPRIM_LINESTRIP,
52 _3DPRIM_TRILIST,
53 _3DPRIM_TRISTRIP,
54 _3DPRIM_TRIFAN,
55 _3DPRIM_QUADLIST,
56 _3DPRIM_QUADSTRIP,
57 _3DPRIM_POLYGON
58 };
59
60
61 static const GLenum reduced_prim[GL_POLYGON+1] = {
62 GL_POINTS,
63 GL_LINES,
64 GL_LINES,
65 GL_LINES,
66 GL_TRIANGLES,
67 GL_TRIANGLES,
68 GL_TRIANGLES,
69 GL_TRIANGLES,
70 GL_TRIANGLES,
71 GL_TRIANGLES
72 };
73
74
75 /* When the primitive changes, set a state bit and re-validate. Not
76 * the nicest and would rather deal with this by having all the
77 * programs be immune to the active primitive (ie. cope with all
78 * possibilities). That may not be realistic however.
79 */
80 static GLuint brw_set_prim(struct brw_context *brw, GLenum prim)
81 {
82 GLcontext *ctx = &brw->intel.ctx;
83
84 if (INTEL_DEBUG & DEBUG_PRIMS)
85 printf("PRIM: %s\n", _mesa_lookup_enum_by_nr(prim));
86
87 /* Slight optimization to avoid the GS program when not needed:
88 */
89 if (prim == GL_QUAD_STRIP &&
90 ctx->Light.ShadeModel != GL_FLAT &&
91 ctx->Polygon.FrontMode == GL_FILL &&
92 ctx->Polygon.BackMode == GL_FILL)
93 prim = GL_TRIANGLE_STRIP;
94
95 if (prim != brw->primitive) {
96 brw->primitive = prim;
97 brw->state.dirty.brw |= BRW_NEW_PRIMITIVE;
98
99 if (reduced_prim[prim] != brw->intel.reduced_primitive) {
100 brw->intel.reduced_primitive = reduced_prim[prim];
101 brw->state.dirty.brw |= BRW_NEW_REDUCED_PRIMITIVE;
102 }
103 }
104
105 return prim_to_hw_prim[prim];
106 }
107
108
109 static GLuint trim(GLenum prim, GLuint length)
110 {
111 if (prim == GL_QUAD_STRIP)
112 return length > 3 ? (length - length % 2) : 0;
113 else if (prim == GL_QUADS)
114 return length - length % 4;
115 else
116 return length;
117 }
118
119
120 static void brw_emit_prim(struct brw_context *brw,
121 const struct _mesa_prim *prim,
122 uint32_t hw_prim)
123 {
124 struct brw_3d_primitive prim_packet;
125 struct intel_context *intel = &brw->intel;
126
127 if (INTEL_DEBUG & DEBUG_PRIMS)
128 printf("PRIM: %s %d %d\n", _mesa_lookup_enum_by_nr(prim->mode),
129 prim->start, prim->count);
130
131 prim_packet.header.opcode = CMD_3D_PRIM;
132 prim_packet.header.length = sizeof(prim_packet)/4 - 2;
133 prim_packet.header.pad = 0;
134 prim_packet.header.topology = hw_prim;
135 prim_packet.header.indexed = prim->indexed;
136
137 prim_packet.verts_per_instance = trim(prim->mode, prim->count);
138 prim_packet.start_vert_location = prim->start;
139 if (prim->indexed)
140 prim_packet.start_vert_location += brw->ib.start_vertex_offset;
141 prim_packet.instance_count = 1;
142 prim_packet.start_instance_location = 0;
143 prim_packet.base_vert_location = prim->basevertex;
144
145 /* Can't wrap here, since we rely on the validated state. */
146 intel->no_batch_wrap = GL_TRUE;
147
148 /* If we're set to always flush, do it before and after the primitive emit.
149 * We want to catch both missed flushes that hurt instruction/state cache
150 * and missed flushes of the render cache as it heads to other parts of
151 * the besides the draw code.
152 */
153 if (intel->always_flush_cache) {
154 intel_batchbuffer_emit_mi_flush(intel->batch);
155 }
156 if (prim_packet.verts_per_instance) {
157 intel_batchbuffer_data( brw->intel.batch, &prim_packet,
158 sizeof(prim_packet));
159 }
160 if (intel->always_flush_cache) {
161 intel_batchbuffer_emit_mi_flush(intel->batch);
162 }
163
164 intel->no_batch_wrap = GL_FALSE;
165 }
166
167 static void brw_merge_inputs( struct brw_context *brw,
168 const struct gl_client_array *arrays[])
169 {
170 struct brw_vertex_info old = brw->vb.info;
171 GLuint i;
172
173 for (i = 0; i < VERT_ATTRIB_MAX; i++)
174 dri_bo_unreference(brw->vb.inputs[i].bo);
175
176 memset(&brw->vb.inputs, 0, sizeof(brw->vb.inputs));
177 memset(&brw->vb.info, 0, sizeof(brw->vb.info));
178
179 for (i = 0; i < VERT_ATTRIB_MAX; i++) {
180 brw->vb.inputs[i].glarray = arrays[i];
181 brw->vb.inputs[i].attrib = (gl_vert_attrib) i;
182
183 if (arrays[i]->StrideB != 0)
184 brw->vb.info.sizes[i/16] |= (brw->vb.inputs[i].glarray->Size - 1) <<
185 ((i%16) * 2);
186 }
187
188 /* Raise statechanges if input sizes have changed. */
189 if (memcmp(brw->vb.info.sizes, old.sizes, sizeof(old.sizes)) != 0)
190 brw->state.dirty.brw |= BRW_NEW_INPUT_DIMENSIONS;
191 }
192
193 /* XXX: could split the primitive list to fallback only on the
194 * non-conformant primitives.
195 */
196 static GLboolean check_fallbacks( struct brw_context *brw,
197 const struct _mesa_prim *prim,
198 GLuint nr_prims )
199 {
200 GLcontext *ctx = &brw->intel.ctx;
201 GLuint i;
202
203 /* If we don't require strict OpenGL conformance, never
204 * use fallbacks. If we're forcing fallbacks, always
205 * use fallfacks.
206 */
207 if (brw->intel.conformance_mode == 0)
208 return GL_FALSE;
209
210 if (brw->intel.conformance_mode == 2)
211 return GL_TRUE;
212
213 if (ctx->Polygon.SmoothFlag) {
214 for (i = 0; i < nr_prims; i++)
215 if (reduced_prim[prim[i].mode] == GL_TRIANGLES)
216 return GL_TRUE;
217 }
218
219 /* BRW hardware will do AA lines, but they are non-conformant it
220 * seems. TBD whether we keep this fallback:
221 */
222 if (ctx->Line.SmoothFlag) {
223 for (i = 0; i < nr_prims; i++)
224 if (reduced_prim[prim[i].mode] == GL_LINES)
225 return GL_TRUE;
226 }
227
228 /* Stipple -- these fallbacks could be resolved with a little
229 * bit of work?
230 */
231 if (ctx->Line.StippleFlag) {
232 for (i = 0; i < nr_prims; i++) {
233 /* GS doesn't get enough information to know when to reset
234 * the stipple counter?!?
235 */
236 if (prim[i].mode == GL_LINE_LOOP || prim[i].mode == GL_LINE_STRIP)
237 return GL_TRUE;
238
239 if (prim[i].mode == GL_POLYGON &&
240 (ctx->Polygon.FrontMode == GL_LINE ||
241 ctx->Polygon.BackMode == GL_LINE))
242 return GL_TRUE;
243 }
244 }
245
246 if (ctx->Point.SmoothFlag) {
247 for (i = 0; i < nr_prims; i++)
248 if (prim[i].mode == GL_POINTS)
249 return GL_TRUE;
250 }
251
252 /* BRW hardware doesn't handle GL_CLAMP texturing correctly;
253 * brw_wm_sampler_state:translate_wrap_mode() treats GL_CLAMP
254 * as GL_CLAMP_TO_EDGE instead. If we're using GL_CLAMP, and
255 * we want strict conformance, force the fallback.
256 * Right now, we only do this for 2D textures.
257 */
258 {
259 int u;
260 for (u = 0; u < ctx->Const.MaxTextureCoordUnits; u++) {
261 struct gl_texture_unit *texUnit = &ctx->Texture.Unit[u];
262 if (texUnit->Enabled) {
263 if (texUnit->Enabled & TEXTURE_1D_BIT) {
264 if (texUnit->CurrentTex[TEXTURE_1D_INDEX]->WrapS == GL_CLAMP) {
265 return GL_TRUE;
266 }
267 }
268 if (texUnit->Enabled & TEXTURE_2D_BIT) {
269 if (texUnit->CurrentTex[TEXTURE_2D_INDEX]->WrapS == GL_CLAMP ||
270 texUnit->CurrentTex[TEXTURE_2D_INDEX]->WrapT == GL_CLAMP) {
271 return GL_TRUE;
272 }
273 }
274 if (texUnit->Enabled & TEXTURE_3D_BIT) {
275 if (texUnit->CurrentTex[TEXTURE_3D_INDEX]->WrapS == GL_CLAMP ||
276 texUnit->CurrentTex[TEXTURE_3D_INDEX]->WrapT == GL_CLAMP ||
277 texUnit->CurrentTex[TEXTURE_3D_INDEX]->WrapR == GL_CLAMP) {
278 return GL_TRUE;
279 }
280 }
281 }
282 }
283 }
284
285 /* Nothing stopping us from the fast path now */
286 return GL_FALSE;
287 }
288
289 /* May fail if out of video memory for texture or vbo upload, or on
290 * fallback conditions.
291 */
292 static GLboolean brw_try_draw_prims( GLcontext *ctx,
293 const struct gl_client_array *arrays[],
294 const struct _mesa_prim *prim,
295 GLuint nr_prims,
296 const struct _mesa_index_buffer *ib,
297 GLuint min_index,
298 GLuint max_index )
299 {
300 struct intel_context *intel = intel_context(ctx);
301 struct brw_context *brw = brw_context(ctx);
302 GLboolean retval = GL_FALSE;
303 GLboolean warn = GL_FALSE;
304 GLboolean first_time = GL_TRUE;
305 GLuint i;
306
307 if (ctx->NewState)
308 _mesa_update_state( ctx );
309
310 /* We have to validate the textures *before* checking for fallbacks;
311 * otherwise, the software fallback won't be able to rely on the
312 * texture state, the firstLevel and lastLevel fields won't be
313 * set in the intel texture object (they'll both be 0), and the
314 * software fallback will segfault if it attempts to access any
315 * texture level other than level 0.
316 */
317 brw_validate_textures( brw );
318
319 if (check_fallbacks(brw, prim, nr_prims))
320 return GL_FALSE;
321
322 /* Bind all inputs, derive varying and size information:
323 */
324 brw_merge_inputs( brw, arrays );
325
326 brw->ib.ib = ib;
327 brw->state.dirty.brw |= BRW_NEW_INDICES;
328
329 brw->vb.min_index = min_index;
330 brw->vb.max_index = max_index;
331 brw->state.dirty.brw |= BRW_NEW_VERTICES;
332
333 /* Have to validate state quite late. Will rebuild tnl_program,
334 * which depends on varying information.
335 *
336 * Note this is where brw->vs->prog_data.inputs_read is calculated,
337 * so can't access it earlier.
338 */
339
340 intel_prepare_render(intel);
341
342 for (i = 0; i < nr_prims; i++) {
343 uint32_t hw_prim;
344
345 /* Flush the batch if it's approaching full, so that we don't wrap while
346 * we've got validated state that needs to be in the same batch as the
347 * primitives. This fraction is just a guess (minimal full state plus
348 * a primitive is around 512 bytes), and would be better if we had
349 * an upper bound of how much we might emit in a single
350 * brw_try_draw_prims().
351 */
352 intel_batchbuffer_require_space(intel->batch, intel->batch->size / 4);
353
354 hw_prim = brw_set_prim(brw, prim[i].mode);
355
356 if (first_time || (brw->state.dirty.brw & BRW_NEW_PRIMITIVE)) {
357 first_time = GL_FALSE;
358
359 brw_validate_state(brw);
360
361 /* Various fallback checks: */
362 if (brw->intel.Fallback)
363 goto out;
364
365 /* Check that we can fit our state in with our existing batchbuffer, or
366 * flush otherwise.
367 */
368 if (dri_bufmgr_check_aperture_space(brw->state.validated_bos,
369 brw->state.validated_bo_count)) {
370 static GLboolean warned;
371 intel_batchbuffer_flush(intel->batch);
372
373 /* Validate the state after we flushed the batch (which would have
374 * changed the set of dirty state). If we still fail to
375 * check_aperture, warn of what's happening, but attempt to continue
376 * on since it may succeed anyway, and the user would probably rather
377 * see a failure and a warning than a fallback.
378 */
379 brw_validate_state(brw);
380 if (!warned &&
381 dri_bufmgr_check_aperture_space(brw->state.validated_bos,
382 brw->state.validated_bo_count)) {
383 warn = GL_TRUE;
384 warned = GL_TRUE;
385 }
386 }
387
388 brw_upload_state(brw);
389 }
390
391 brw_emit_prim(brw, &prim[i], hw_prim);
392
393 retval = GL_TRUE;
394 }
395
396 if (intel->always_flush_batch)
397 intel_batchbuffer_flush(intel->batch);
398 out:
399
400 brw_state_cache_check_size(brw);
401
402 if (warn)
403 fprintf(stderr, "i965: Single primitive emit potentially exceeded "
404 "available aperture space\n");
405
406 if (!retval)
407 DBG("%s failed\n", __FUNCTION__);
408
409 return retval;
410 }
411
412 void brw_draw_prims( GLcontext *ctx,
413 const struct gl_client_array *arrays[],
414 const struct _mesa_prim *prim,
415 GLuint nr_prims,
416 const struct _mesa_index_buffer *ib,
417 GLboolean index_bounds_valid,
418 GLuint min_index,
419 GLuint max_index )
420 {
421 GLboolean retval;
422
423 if (!vbo_all_varyings_in_vbos(arrays)) {
424 if (!index_bounds_valid)
425 vbo_get_minmax_index(ctx, prim, ib, &min_index, &max_index);
426
427 /* Decide if we want to rebase. If so we end up recursing once
428 * only into this function.
429 */
430 if (min_index != 0) {
431 vbo_rebase_prims(ctx, arrays,
432 prim, nr_prims,
433 ib, min_index, max_index,
434 brw_draw_prims );
435 return;
436 }
437 }
438
439 /* Make a first attempt at drawing:
440 */
441 retval = brw_try_draw_prims(ctx, arrays, prim, nr_prims, ib, min_index, max_index);
442
443 /* Otherwise, we really are out of memory. Pass the drawing
444 * command to the software tnl module and which will in turn call
445 * swrast to do the drawing.
446 */
447 if (!retval) {
448 _swsetup_Wakeup(ctx);
449 _tnl_draw_prims(ctx, arrays, prim, nr_prims, ib, min_index, max_index);
450 }
451
452 }
453
454 void brw_draw_init( struct brw_context *brw )
455 {
456 GLcontext *ctx = &brw->intel.ctx;
457 struct vbo_context *vbo = vbo_context(ctx);
458
459 /* Register our drawing function:
460 */
461 vbo->draw_prims = brw_draw_prims;
462 }
463
464 void brw_draw_destroy( struct brw_context *brw )
465 {
466 int i;
467
468 if (brw->vb.upload.bo != NULL) {
469 dri_bo_unreference(brw->vb.upload.bo);
470 brw->vb.upload.bo = NULL;
471 }
472
473 for (i = 0; i < VERT_ATTRIB_MAX; i++) {
474 dri_bo_unreference(brw->vb.inputs[i].bo);
475 brw->vb.inputs[i].bo = NULL;
476 }
477
478 dri_bo_unreference(brw->ib.bo);
479 brw->ib.bo = NULL;
480 }