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"
48 #define FILE_DEBUG_FLAG DEBUG_PRIMS
50 static GLuint prim_to_hw_prim
[GL_POLYGON
+1] = {
64 static const GLenum reduced_prim
[GL_POLYGON
+1] = {
78 /* When the primitive changes, set a state bit and re-validate. Not
79 * the nicest and would rather deal with this by having all the
80 * programs be immune to the active primitive (ie. cope with all
81 * possibilities). That may not be realistic however.
83 static void brw_set_prim(struct brw_context
*brw
,
84 const struct _mesa_prim
*prim
)
86 struct gl_context
*ctx
= &brw
->intel
.ctx
;
87 uint32_t hw_prim
= prim_to_hw_prim
[prim
->mode
];
89 DBG("PRIM: %s\n", _mesa_lookup_enum_by_nr(prim
->mode
));
91 /* Slight optimization to avoid the GS program when not needed:
93 if (prim
->mode
== GL_QUAD_STRIP
&&
94 ctx
->Light
.ShadeModel
!= GL_FLAT
&&
95 ctx
->Polygon
.FrontMode
== GL_FILL
&&
96 ctx
->Polygon
.BackMode
== GL_FILL
)
97 hw_prim
= _3DPRIM_TRISTRIP
;
99 if (prim
->mode
== GL_QUADS
&& prim
->count
== 4 &&
100 ctx
->Light
.ShadeModel
!= GL_FLAT
&&
101 ctx
->Polygon
.FrontMode
== GL_FILL
&&
102 ctx
->Polygon
.BackMode
== GL_FILL
) {
103 hw_prim
= _3DPRIM_TRIFAN
;
106 if (hw_prim
!= brw
->primitive
) {
107 brw
->primitive
= hw_prim
;
108 brw
->state
.dirty
.brw
|= BRW_NEW_PRIMITIVE
;
110 if (reduced_prim
[prim
->mode
] != brw
->intel
.reduced_primitive
) {
111 brw
->intel
.reduced_primitive
= reduced_prim
[prim
->mode
];
112 brw
->state
.dirty
.brw
|= BRW_NEW_REDUCED_PRIMITIVE
;
117 static void gen6_set_prim(struct brw_context
*brw
,
118 const struct _mesa_prim
*prim
)
120 uint32_t hw_prim
= prim_to_hw_prim
[prim
->mode
];
122 DBG("PRIM: %s\n", _mesa_lookup_enum_by_nr(prim
->mode
));
124 if (hw_prim
!= brw
->primitive
) {
125 brw
->primitive
= hw_prim
;
126 brw
->state
.dirty
.brw
|= BRW_NEW_PRIMITIVE
;
131 static GLuint
trim(GLenum prim
, GLuint length
)
133 if (prim
== GL_QUAD_STRIP
)
134 return length
> 3 ? (length
- length
% 2) : 0;
135 else if (prim
== GL_QUADS
)
136 return length
- length
% 4;
142 static void brw_emit_prim(struct brw_context
*brw
,
143 const struct _mesa_prim
*prim
,
146 struct intel_context
*intel
= &brw
->intel
;
147 int verts_per_instance
;
148 int vertex_access_type
;
149 int start_vertex_location
;
150 int base_vertex_location
;
152 DBG("PRIM: %s %d %d\n", _mesa_lookup_enum_by_nr(prim
->mode
),
153 prim
->start
, prim
->count
);
155 start_vertex_location
= prim
->start
;
156 base_vertex_location
= prim
->basevertex
;
158 vertex_access_type
= GEN4_3DPRIM_VERTEXBUFFER_ACCESS_RANDOM
;
159 start_vertex_location
+= brw
->ib
.start_vertex_offset
;
160 base_vertex_location
+= brw
->vb
.start_vertex_bias
;
162 vertex_access_type
= GEN4_3DPRIM_VERTEXBUFFER_ACCESS_SEQUENTIAL
;
163 start_vertex_location
+= brw
->vb
.start_vertex_bias
;
166 verts_per_instance
= trim(prim
->mode
, prim
->count
);
168 /* If nothing to emit, just return. */
169 if (verts_per_instance
== 0)
172 /* If we're set to always flush, do it before and after the primitive emit.
173 * We want to catch both missed flushes that hurt instruction/state cache
174 * and missed flushes of the render cache as it heads to other parts of
175 * the besides the draw code.
177 if (intel
->always_flush_cache
) {
178 intel_batchbuffer_emit_mi_flush(intel
);
182 OUT_BATCH(CMD_3D_PRIM
<< 16 | (6 - 2) |
183 hw_prim
<< GEN4_3DPRIM_TOPOLOGY_TYPE_SHIFT
|
185 OUT_BATCH(verts_per_instance
);
186 OUT_BATCH(start_vertex_location
);
187 OUT_BATCH(1); // instance count
188 OUT_BATCH(0); // start instance location
189 OUT_BATCH(base_vertex_location
);
192 intel
->batch
.need_workaround_flush
= true;
194 if (intel
->always_flush_cache
) {
195 intel_batchbuffer_emit_mi_flush(intel
);
199 static void gen7_emit_prim(struct brw_context
*brw
,
200 const struct _mesa_prim
*prim
,
203 struct intel_context
*intel
= &brw
->intel
;
204 int verts_per_instance
;
205 int vertex_access_type
;
206 int start_vertex_location
;
207 int base_vertex_location
;
209 DBG("PRIM: %s %d %d\n", _mesa_lookup_enum_by_nr(prim
->mode
),
210 prim
->start
, prim
->count
);
212 start_vertex_location
= prim
->start
;
213 base_vertex_location
= prim
->basevertex
;
215 vertex_access_type
= GEN7_3DPRIM_VERTEXBUFFER_ACCESS_RANDOM
;
216 start_vertex_location
+= brw
->ib
.start_vertex_offset
;
217 base_vertex_location
+= brw
->vb
.start_vertex_bias
;
219 vertex_access_type
= GEN7_3DPRIM_VERTEXBUFFER_ACCESS_SEQUENTIAL
;
220 start_vertex_location
+= brw
->vb
.start_vertex_bias
;
223 verts_per_instance
= trim(prim
->mode
, prim
->count
);
225 /* If nothing to emit, just return. */
226 if (verts_per_instance
== 0)
229 /* If we're set to always flush, do it before and after the primitive emit.
230 * We want to catch both missed flushes that hurt instruction/state cache
231 * and missed flushes of the render cache as it heads to other parts of
232 * the besides the draw code.
234 if (intel
->always_flush_cache
) {
235 intel_batchbuffer_emit_mi_flush(intel
);
239 OUT_BATCH(CMD_3D_PRIM
<< 16 | (7 - 2));
240 OUT_BATCH(hw_prim
| vertex_access_type
);
241 OUT_BATCH(verts_per_instance
);
242 OUT_BATCH(start_vertex_location
);
243 OUT_BATCH(1); // instance count
244 OUT_BATCH(0); // start instance location
245 OUT_BATCH(base_vertex_location
);
248 if (intel
->always_flush_cache
) {
249 intel_batchbuffer_emit_mi_flush(intel
);
254 static void brw_merge_inputs( struct brw_context
*brw
,
255 const struct gl_client_array
*arrays
[])
257 struct brw_vertex_info old
= brw
->vb
.info
;
260 for (i
= 0; i
< brw
->vb
.nr_buffers
; i
++) {
261 drm_intel_bo_unreference(brw
->vb
.buffers
[i
].bo
);
262 brw
->vb
.buffers
[i
].bo
= NULL
;
264 brw
->vb
.nr_buffers
= 0;
266 memset(&brw
->vb
.info
, 0, sizeof(brw
->vb
.info
));
268 for (i
= 0; i
< VERT_ATTRIB_MAX
; i
++) {
269 brw
->vb
.inputs
[i
].buffer
= -1;
270 brw
->vb
.inputs
[i
].glarray
= arrays
[i
];
271 brw
->vb
.inputs
[i
].attrib
= (gl_vert_attrib
) i
;
273 if (arrays
[i
]->StrideB
!= 0)
274 brw
->vb
.info
.sizes
[i
/16] |= (brw
->vb
.inputs
[i
].glarray
->Size
- 1) <<
278 /* Raise statechanges if input sizes have changed. */
279 if (memcmp(brw
->vb
.info
.sizes
, old
.sizes
, sizeof(old
.sizes
)) != 0)
280 brw
->state
.dirty
.brw
|= BRW_NEW_INPUT_DIMENSIONS
;
283 /* May fail if out of video memory for texture or vbo upload, or on
284 * fallback conditions.
286 static bool brw_try_draw_prims( struct gl_context
*ctx
,
287 const struct gl_client_array
*arrays
[],
288 const struct _mesa_prim
*prim
,
290 const struct _mesa_index_buffer
*ib
,
294 struct intel_context
*intel
= intel_context(ctx
);
295 struct brw_context
*brw
= brw_context(ctx
);
298 bool fail_next
= false;
301 _mesa_update_state( ctx
);
303 /* We have to validate the textures *before* checking for fallbacks;
304 * otherwise, the software fallback won't be able to rely on the
305 * texture state, the firstLevel and lastLevel fields won't be
306 * set in the intel texture object (they'll both be 0), and the
307 * software fallback will segfault if it attempts to access any
308 * texture level other than level 0.
310 brw_validate_textures( brw
);
312 /* Bind all inputs, derive varying and size information:
314 brw_merge_inputs( brw
, arrays
);
317 brw
->state
.dirty
.brw
|= BRW_NEW_INDICES
;
319 brw
->vb
.min_index
= min_index
;
320 brw
->vb
.max_index
= max_index
;
321 brw
->state
.dirty
.brw
|= BRW_NEW_VERTICES
;
323 /* Have to validate state quite late. Will rebuild tnl_program,
324 * which depends on varying information.
326 * Note this is where brw->vs->prog_data.inputs_read is calculated,
327 * so can't access it earlier.
330 intel_prepare_render(intel
);
332 for (i
= 0; i
< nr_prims
; i
++) {
333 int estimated_max_prim_size
;
335 estimated_max_prim_size
= 512; /* batchbuffer commands */
336 estimated_max_prim_size
+= (BRW_MAX_TEX_UNIT
*
337 (sizeof(struct brw_sampler_state
) +
338 sizeof(struct gen5_sampler_default_color
)));
339 estimated_max_prim_size
+= 1024; /* gen6 VS push constants */
340 estimated_max_prim_size
+= 1024; /* gen6 WM push constants */
341 estimated_max_prim_size
+= 512; /* misc. pad */
343 /* Flush the batch if it's approaching full, so that we don't wrap while
344 * we've got validated state that needs to be in the same batch as the
347 intel_batchbuffer_require_space(intel
, estimated_max_prim_size
, false);
348 intel_batchbuffer_save_state(intel
);
351 brw_set_prim(brw
, &prim
[i
]);
353 gen6_set_prim(brw
, &prim
[i
]);
356 /* Note that before the loop, brw->state.dirty.brw was set to != 0, and
357 * that the state updated in the loop outside of this block is that in
358 * *_set_prim or intel_batchbuffer_flush(), which only impacts
359 * brw->state.dirty.brw.
361 if (brw
->state
.dirty
.brw
) {
362 brw_validate_state(brw
);
364 if (brw
->intel
.Fallback
) {
369 intel
->no_batch_wrap
= true;
370 brw_upload_state(brw
);
372 if (unlikely(brw
->intel
.Fallback
)) {
373 intel
->no_batch_wrap
= false;
380 gen7_emit_prim(brw
, &prim
[i
], brw
->primitive
);
382 brw_emit_prim(brw
, &prim
[i
], brw
->primitive
);
384 intel
->no_batch_wrap
= false;
386 if (dri_bufmgr_check_aperture_space(&intel
->batch
.bo
, 1)) {
388 intel_batchbuffer_reset_to_saved(intel
);
389 intel_batchbuffer_flush(intel
);
393 if (intel_batchbuffer_flush(intel
) == -ENOSPC
) {
394 static bool warned
= false;
397 fprintf(stderr
, "i965: Single primitive emit exceeded"
398 "available aperture space\n");
408 if (intel
->always_flush_batch
)
409 intel_batchbuffer_flush(intel
);
412 brw_state_cache_check_size(brw
);
417 void brw_draw_prims( struct gl_context
*ctx
,
418 const struct gl_client_array
*arrays
[],
419 const struct _mesa_prim
*prim
,
421 const struct _mesa_index_buffer
*ib
,
422 GLboolean index_bounds_valid
,
428 if (!_mesa_check_conditional_render(ctx
))
431 if (!vbo_all_varyings_in_vbos(arrays
)) {
432 if (!index_bounds_valid
)
433 vbo_get_minmax_index(ctx
, prim
, ib
, &min_index
, &max_index
);
435 /* Decide if we want to rebase. If so we end up recursing once
436 * only into this function.
438 if (min_index
!= 0 && !vbo_any_varyings_in_vbos(arrays
)) {
439 vbo_rebase_prims(ctx
, arrays
,
441 ib
, min_index
, max_index
,
447 /* Make a first attempt at drawing:
449 retval
= brw_try_draw_prims(ctx
, arrays
, prim
, nr_prims
, ib
, min_index
, max_index
);
451 /* Otherwise, we really are out of memory. Pass the drawing
452 * command to the software tnl module and which will in turn call
453 * swrast to do the drawing.
456 _swsetup_Wakeup(ctx
);
458 _tnl_draw_prims(ctx
, arrays
, prim
, nr_prims
, ib
, min_index
, max_index
);
463 void brw_draw_init( struct brw_context
*brw
)
465 struct gl_context
*ctx
= &brw
->intel
.ctx
;
466 struct vbo_context
*vbo
= vbo_context(ctx
);
469 /* Register our drawing function:
471 vbo
->draw_prims
= brw_draw_prims
;
473 for (i
= 0; i
< VERT_ATTRIB_MAX
; i
++)
474 brw
->vb
.inputs
[i
].buffer
= -1;
475 brw
->vb
.nr_buffers
= 0;
476 brw
->vb
.nr_enabled
= 0;
479 void brw_draw_destroy( struct brw_context
*brw
)
483 for (i
= 0; i
< brw
->vb
.nr_buffers
; i
++) {
484 drm_intel_bo_unreference(brw
->vb
.buffers
[i
].bo
);
485 brw
->vb
.buffers
[i
].bo
= NULL
;
487 brw
->vb
.nr_buffers
= 0;
489 for (i
= 0; i
< brw
->vb
.nr_enabled
; i
++) {
490 brw
->vb
.enabled
[i
]->buffer
= -1;
492 brw
->vb
.nr_enabled
= 0;
494 drm_intel_bo_unreference(brw
->ib
.bo
);