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