1 /**************************************************************************
3 * Copyright 2003 Tungsten Graphics, Inc., Cedar Park, Texas.
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:
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
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.
26 **************************************************************************/
28 #include <sys/errno.h>
30 #include "main/glheader.h"
31 #include "main/context.h"
32 #include "main/condrender.h"
33 #include "main/samplerobj.h"
34 #include "main/state.h"
35 #include "main/enums.h"
37 #include "vbo/vbo_context.h"
38 #include "swrast/swrast.h"
39 #include "swrast_setup/swrast_setup.h"
42 #include "brw_defines.h"
43 #include "brw_context.h"
44 #include "brw_state.h"
46 #include "intel_batchbuffer.h"
47 #include "intel_fbo.h"
48 #include "intel_mipmap_tree.h"
49 #include "intel_regions.h"
51 #define FILE_DEBUG_FLAG DEBUG_PRIMS
53 static GLuint prim_to_hw_prim[GL_POLYGON+1] = {
67 static const GLenum reduced_prim[GL_POLYGON+1] = {
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.
86 static void brw_set_prim(struct brw_context *brw,
87 const struct _mesa_prim *prim)
89 struct gl_context *ctx = &brw->intel.ctx;
90 uint32_t hw_prim = prim_to_hw_prim[prim->mode];
92 DBG("PRIM: %s\n", _mesa_lookup_enum_by_nr(prim->mode));
94 /* Slight optimization to avoid the GS program when not needed:
96 if (prim->mode == GL_QUAD_STRIP &&
97 ctx->Light.ShadeModel != GL_FLAT &&
98 ctx->Polygon.FrontMode == GL_FILL &&
99 ctx->Polygon.BackMode == GL_FILL)
100 hw_prim = _3DPRIM_TRISTRIP;
102 if (prim->mode == GL_QUADS && prim->count == 4 &&
103 ctx->Light.ShadeModel != GL_FLAT &&
104 ctx->Polygon.FrontMode == GL_FILL &&
105 ctx->Polygon.BackMode == GL_FILL) {
106 hw_prim = _3DPRIM_TRIFAN;
109 if (hw_prim != brw->primitive) {
110 brw->primitive = hw_prim;
111 brw->state.dirty.brw |= BRW_NEW_PRIMITIVE;
113 if (reduced_prim[prim->mode] != brw->intel.reduced_primitive) {
114 brw->intel.reduced_primitive = reduced_prim[prim->mode];
115 brw->state.dirty.brw |= BRW_NEW_REDUCED_PRIMITIVE;
120 static void gen6_set_prim(struct brw_context *brw,
121 const struct _mesa_prim *prim)
125 DBG("PRIM: %s\n", _mesa_lookup_enum_by_nr(prim->mode));
128 assert(prim->mode == GL_TRIANGLES);
129 hw_prim = _3DPRIM_RECTLIST;
131 hw_prim = prim_to_hw_prim[prim->mode];
134 if (hw_prim != brw->primitive) {
135 brw->primitive = hw_prim;
136 brw->state.dirty.brw |= BRW_NEW_PRIMITIVE;
141 static GLuint trim(GLenum prim, GLuint length)
143 if (prim == GL_QUAD_STRIP)
144 return length > 3 ? (length - length % 2) : 0;
145 else if (prim == GL_QUADS)
146 return length - length % 4;
152 static void brw_emit_prim(struct brw_context *brw,
153 const struct _mesa_prim *prim,
156 struct intel_context *intel = &brw->intel;
157 int verts_per_instance;
158 int vertex_access_type;
159 int start_vertex_location;
160 int base_vertex_location;
162 DBG("PRIM: %s %d %d\n", _mesa_lookup_enum_by_nr(prim->mode),
163 prim->start, prim->count);
165 start_vertex_location = prim->start;
166 base_vertex_location = prim->basevertex;
168 vertex_access_type = GEN4_3DPRIM_VERTEXBUFFER_ACCESS_RANDOM;
169 start_vertex_location += brw->ib.start_vertex_offset;
170 base_vertex_location += brw->vb.start_vertex_bias;
172 vertex_access_type = GEN4_3DPRIM_VERTEXBUFFER_ACCESS_SEQUENTIAL;
173 start_vertex_location += brw->vb.start_vertex_bias;
176 verts_per_instance = trim(prim->mode, prim->count);
178 /* If nothing to emit, just return. */
179 if (verts_per_instance == 0)
182 /* If we're set to always flush, do it before and after the primitive emit.
183 * We want to catch both missed flushes that hurt instruction/state cache
184 * and missed flushes of the render cache as it heads to other parts of
185 * the besides the draw code.
187 if (intel->always_flush_cache) {
188 intel_batchbuffer_emit_mi_flush(intel);
192 OUT_BATCH(CMD_3D_PRIM << 16 | (6 - 2) |
193 hw_prim << GEN4_3DPRIM_TOPOLOGY_TYPE_SHIFT |
195 OUT_BATCH(verts_per_instance);
196 OUT_BATCH(start_vertex_location);
197 OUT_BATCH(1); // instance count
198 OUT_BATCH(0); // start instance location
199 OUT_BATCH(base_vertex_location);
202 intel->batch.need_workaround_flush = true;
204 if (intel->always_flush_cache) {
205 intel_batchbuffer_emit_mi_flush(intel);
209 static void gen7_emit_prim(struct brw_context *brw,
210 const struct _mesa_prim *prim,
213 struct intel_context *intel = &brw->intel;
214 int verts_per_instance;
215 int vertex_access_type;
216 int start_vertex_location;
217 int base_vertex_location;
219 DBG("PRIM: %s %d %d\n", _mesa_lookup_enum_by_nr(prim->mode),
220 prim->start, prim->count);
222 start_vertex_location = prim->start;
223 base_vertex_location = prim->basevertex;
225 vertex_access_type = GEN7_3DPRIM_VERTEXBUFFER_ACCESS_RANDOM;
226 start_vertex_location += brw->ib.start_vertex_offset;
227 base_vertex_location += brw->vb.start_vertex_bias;
229 vertex_access_type = GEN7_3DPRIM_VERTEXBUFFER_ACCESS_SEQUENTIAL;
230 start_vertex_location += brw->vb.start_vertex_bias;
233 verts_per_instance = trim(prim->mode, prim->count);
235 /* If nothing to emit, just return. */
236 if (verts_per_instance == 0)
239 /* If we're set to always flush, do it before and after the primitive emit.
240 * We want to catch both missed flushes that hurt instruction/state cache
241 * and missed flushes of the render cache as it heads to other parts of
242 * the besides the draw code.
244 if (intel->always_flush_cache) {
245 intel_batchbuffer_emit_mi_flush(intel);
249 OUT_BATCH(CMD_3D_PRIM << 16 | (7 - 2));
250 OUT_BATCH(hw_prim | vertex_access_type);
251 OUT_BATCH(verts_per_instance);
252 OUT_BATCH(start_vertex_location);
253 OUT_BATCH(1); // instance count
254 OUT_BATCH(0); // start instance location
255 OUT_BATCH(base_vertex_location);
258 if (intel->always_flush_cache) {
259 intel_batchbuffer_emit_mi_flush(intel);
264 static void brw_merge_inputs( struct brw_context *brw,
265 const struct gl_client_array *arrays[])
267 struct brw_vertex_info old = brw->vb.info;
270 for (i = 0; i < brw->vb.nr_buffers; i++) {
271 drm_intel_bo_unreference(brw->vb.buffers[i].bo);
272 brw->vb.buffers[i].bo = NULL;
274 brw->vb.nr_buffers = 0;
276 memset(&brw->vb.info, 0, sizeof(brw->vb.info));
278 for (i = 0; i < VERT_ATTRIB_MAX; i++) {
279 brw->vb.inputs[i].buffer = -1;
280 brw->vb.inputs[i].glarray = arrays[i];
281 brw->vb.inputs[i].attrib = (gl_vert_attrib) i;
283 if (arrays[i]->StrideB != 0)
284 brw->vb.info.sizes[i/16] |= (brw->vb.inputs[i].glarray->Size - 1) <<
288 /* Raise statechanges if input sizes have changed. */
289 if (memcmp(brw->vb.info.sizes, old.sizes, sizeof(old.sizes)) != 0)
290 brw->state.dirty.brw |= BRW_NEW_INPUT_DIMENSIONS;
294 * \brief Resolve buffers before drawing.
296 * Resolve the depth buffer's HiZ buffer and resolve the depth buffer of each
297 * enabled depth texture.
299 * (In the future, this will also perform MSAA resolves).
302 brw_predraw_resolve_buffers(struct brw_context *brw)
304 struct gl_context *ctx = &brw->intel.ctx;
305 struct intel_context *intel = &brw->intel;
306 struct intel_renderbuffer *depth_irb;
307 struct intel_texture_object *tex_obj;
308 bool did_resolve = false;
310 /* Avoid recursive HiZ op. */
315 /* Resolve the depth buffer's HiZ buffer. */
316 depth_irb = intel_get_renderbuffer(ctx->DrawBuffer, BUFFER_DEPTH);
317 if (depth_irb && depth_irb->mt) {
318 did_resolve |= intel_renderbuffer_resolve_hiz(intel, depth_irb);
321 /* Resolve depth buffer of each enabled depth texture. */
322 for (int i = 0; i < BRW_MAX_TEX_UNIT; i++) {
323 if (!ctx->Texture.Unit[i]._ReallyEnabled)
325 tex_obj = intel_texture_object(ctx->Texture.Unit[i]._Current);
326 if (!tex_obj || !tex_obj->mt)
328 did_resolve |= intel_miptree_all_slices_resolve_depth(intel, tex_obj->mt);
332 /* Call vbo_bind_array() to synchronize the vbo module's vertex
333 * attributes to the gl_context's.
337 * The vbo module tracks vertex attributes separately from the
338 * gl_context. Specifically, the vbo module maintins vertex attributes
339 * in vbo_exec_context::array::inputs, which is synchronized with
340 * gl_context::Array::ArrayObj::VertexAttrib by vbo_bind_array().
341 * vbo_draw_arrays() calls vbo_bind_array() to perform the
342 * synchronization before calling the real draw call,
343 * vbo_context::draw_arrays.
345 * At this point (after performing a resolve meta-op but before calling
346 * vbo_bind_array), the gl_context's vertex attributes have been
347 * restored to their original state (that is, their state before the
348 * meta-op began), but the vbo module's vertex attribute are those used
349 * in the last meta-op. Therefore we must manually synchronize the two with
350 * vbo_bind_array() before continuing with the original draw command.
352 _mesa_update_state(ctx);
353 vbo_bind_arrays(ctx);
354 _mesa_update_state(ctx);
359 * \brief Call this after drawing to mark which buffers need resolving
361 * If the depth buffer was written to and if it has an accompanying HiZ
362 * buffer, then mark that it needs a depth resolve.
364 * (In the future, this will also mark needed MSAA resolves).
366 static void brw_postdraw_set_buffers_need_resolve(struct brw_context *brw)
368 struct gl_context *ctx = &brw->intel.ctx;
369 struct gl_framebuffer *fb = ctx->DrawBuffer;
370 struct intel_renderbuffer *depth_irb =
371 intel_get_renderbuffer(fb, BUFFER_DEPTH);
376 intel_renderbuffer_set_needs_depth_resolve(depth_irb);
380 /* May fail if out of video memory for texture or vbo upload, or on
381 * fallback conditions.
383 static bool brw_try_draw_prims( struct gl_context *ctx,
384 const struct gl_client_array *arrays[],
385 const struct _mesa_prim *prim,
387 const struct _mesa_index_buffer *ib,
391 struct intel_context *intel = intel_context(ctx);
392 struct brw_context *brw = brw_context(ctx);
395 bool fail_next = false;
398 _mesa_update_state( ctx );
400 /* We have to validate the textures *before* checking for fallbacks;
401 * otherwise, the software fallback won't be able to rely on the
402 * texture state, the firstLevel and lastLevel fields won't be
403 * set in the intel texture object (they'll both be 0), and the
404 * software fallback will segfault if it attempts to access any
405 * texture level other than level 0.
407 brw_validate_textures( brw );
409 /* Resolves must occur after updating state and finalizing textures but
410 * before setting up any hardware state for this draw call.
412 brw_predraw_resolve_buffers(brw);
414 /* Bind all inputs, derive varying and size information:
416 brw_merge_inputs( brw, arrays );
419 brw->state.dirty.brw |= BRW_NEW_INDICES;
421 brw->vb.min_index = min_index;
422 brw->vb.max_index = max_index;
423 brw->state.dirty.brw |= BRW_NEW_VERTICES;
425 /* Have to validate state quite late. Will rebuild tnl_program,
426 * which depends on varying information.
428 * Note this is where brw->vs->prog_data.inputs_read is calculated,
429 * so can't access it earlier.
432 intel_prepare_render(intel);
434 for (i = 0; i < nr_prims; i++) {
435 int estimated_max_prim_size;
437 estimated_max_prim_size = 512; /* batchbuffer commands */
438 estimated_max_prim_size += (BRW_MAX_TEX_UNIT *
439 (sizeof(struct brw_sampler_state) +
440 sizeof(struct gen5_sampler_default_color)));
441 estimated_max_prim_size += 1024; /* gen6 VS push constants */
442 estimated_max_prim_size += 1024; /* gen6 WM push constants */
443 estimated_max_prim_size += 512; /* misc. pad */
445 /* Flush the batch if it's approaching full, so that we don't wrap while
446 * we've got validated state that needs to be in the same batch as the
449 intel_batchbuffer_require_space(intel, estimated_max_prim_size, false);
450 intel_batchbuffer_save_state(intel);
453 brw_set_prim(brw, &prim[i]);
455 gen6_set_prim(brw, &prim[i]);
458 /* Note that before the loop, brw->state.dirty.brw was set to != 0, and
459 * that the state updated in the loop outside of this block is that in
460 * *_set_prim or intel_batchbuffer_flush(), which only impacts
461 * brw->state.dirty.brw.
463 if (brw->state.dirty.brw) {
464 intel->no_batch_wrap = true;
465 brw_upload_state(brw);
467 if (unlikely(brw->intel.Fallback)) {
468 intel->no_batch_wrap = false;
475 gen7_emit_prim(brw, &prim[i], brw->primitive);
477 brw_emit_prim(brw, &prim[i], brw->primitive);
479 intel->no_batch_wrap = false;
481 if (dri_bufmgr_check_aperture_space(&intel->batch.bo, 1)) {
483 intel_batchbuffer_reset_to_saved(intel);
484 intel_batchbuffer_flush(intel);
488 if (intel_batchbuffer_flush(intel) == -ENOSPC) {
489 static bool warned = false;
492 fprintf(stderr, "i965: Single primitive emit exceeded"
493 "available aperture space\n");
503 if (intel->always_flush_batch)
504 intel_batchbuffer_flush(intel);
507 brw_state_cache_check_size(brw);
508 brw_postdraw_set_buffers_need_resolve(brw);
513 void brw_draw_prims( struct gl_context *ctx,
514 const struct gl_client_array *arrays[],
515 const struct _mesa_prim *prim,
517 const struct _mesa_index_buffer *ib,
518 GLboolean index_bounds_valid,
524 if (!_mesa_check_conditional_render(ctx))
527 if (!vbo_all_varyings_in_vbos(arrays)) {
528 if (!index_bounds_valid)
529 vbo_get_minmax_index(ctx, prim, ib, &min_index, &max_index);
531 /* Decide if we want to rebase. If so we end up recursing once
532 * only into this function.
534 if (min_index != 0 && !vbo_any_varyings_in_vbos(arrays)) {
535 vbo_rebase_prims(ctx, arrays,
537 ib, min_index, max_index,
543 /* Make a first attempt at drawing:
545 retval = brw_try_draw_prims(ctx, arrays, prim, nr_prims, ib, min_index, max_index);
547 /* Otherwise, we really are out of memory. Pass the drawing
548 * command to the software tnl module and which will in turn call
549 * swrast to do the drawing.
552 _swsetup_Wakeup(ctx);
554 _tnl_draw_prims(ctx, arrays, prim, nr_prims, ib, min_index, max_index);
559 void brw_draw_init( struct brw_context *brw )
561 struct gl_context *ctx = &brw->intel.ctx;
562 struct vbo_context *vbo = vbo_context(ctx);
565 /* Register our drawing function:
567 vbo->draw_prims = brw_draw_prims;
569 for (i = 0; i < VERT_ATTRIB_MAX; i++)
570 brw->vb.inputs[i].buffer = -1;
571 brw->vb.nr_buffers = 0;
572 brw->vb.nr_enabled = 0;
575 void brw_draw_destroy( struct brw_context *brw )
579 for (i = 0; i < brw->vb.nr_buffers; i++) {
580 drm_intel_bo_unreference(brw->vb.buffers[i].bo);
581 brw->vb.buffers[i].bo = NULL;
583 brw->vb.nr_buffers = 0;
585 for (i = 0; i < brw->vb.nr_enabled; i++) {
586 brw->vb.enabled[i]->buffer = -1;
588 brw->vb.nr_enabled = 0;
590 drm_intel_bo_unreference(brw->ib.bo);