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 **************************************************************************/
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"
36 #include "vbo/vbo_context.h"
37 #include "swrast/swrast.h"
38 #include "swrast_setup/swrast_setup.h"
41 #include "brw_defines.h"
42 #include "brw_context.h"
43 #include "brw_state.h"
45 #include "intel_batchbuffer.h"
47 #define FILE_DEBUG_FLAG DEBUG_PRIMS
49 static GLuint prim_to_hw_prim
[GL_POLYGON
+1] = {
63 static const GLenum reduced_prim
[GL_POLYGON
+1] = {
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.
82 static GLuint
brw_set_prim(struct brw_context
*brw
,
83 const struct _mesa_prim
*prim
)
85 struct gl_context
*ctx
= &brw
->intel
.ctx
;
86 GLenum mode
= prim
->mode
;
88 DBG("PRIM: %s\n", _mesa_lookup_enum_by_nr(prim
->mode
));
90 /* Slight optimization to avoid the GS program when not needed:
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
;
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
;
105 if (mode
!= brw
->primitive
) {
106 brw
->primitive
= mode
;
107 brw
->state
.dirty
.brw
|= BRW_NEW_PRIMITIVE
;
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
;
115 return prim_to_hw_prim
[mode
];
119 static GLuint
trim(GLenum prim
, GLuint length
)
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;
130 static void brw_emit_prim(struct brw_context
*brw
,
131 const struct _mesa_prim
*prim
,
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
;
140 DBG("PRIM: %s %d %d\n", _mesa_lookup_enum_by_nr(prim
->mode
),
141 prim
->start
, prim
->count
);
143 start_vertex_location
= prim
->start
;
144 base_vertex_location
= prim
->basevertex
;
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
;
150 vertex_access_type
= GEN4_3DPRIM_VERTEXBUFFER_ACCESS_SEQUENTIAL
;
151 start_vertex_location
+= brw
->vb
.start_vertex_bias
;
154 verts_per_instance
= trim(prim
->mode
, prim
->count
);
156 /* If nothing to emit, just return. */
157 if (verts_per_instance
== 0)
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.
165 if (intel
->always_flush_cache
) {
166 intel_batchbuffer_emit_mi_flush(intel
);
170 OUT_BATCH(CMD_3D_PRIM
<< 16 | (6 - 2) |
171 hw_prim
<< GEN4_3DPRIM_TOPOLOGY_TYPE_SHIFT
|
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
);
180 if (intel
->always_flush_cache
) {
181 intel_batchbuffer_emit_mi_flush(intel
);
185 static void gen7_emit_prim(struct brw_context
*brw
,
186 const struct _mesa_prim
*prim
,
189 struct intel_context
*intel
= &brw
->intel
;
190 int verts_per_instance
;
191 int vertex_access_type
;
192 int start_vertex_location
;
193 int base_vertex_location
;
195 DBG("PRIM: %s %d %d\n", _mesa_lookup_enum_by_nr(prim
->mode
),
196 prim
->start
, prim
->count
);
198 start_vertex_location
= prim
->start
;
199 base_vertex_location
= prim
->basevertex
;
201 vertex_access_type
= GEN7_3DPRIM_VERTEXBUFFER_ACCESS_RANDOM
;
202 start_vertex_location
+= brw
->ib
.start_vertex_offset
;
203 base_vertex_location
+= brw
->vb
.start_vertex_bias
;
205 vertex_access_type
= GEN7_3DPRIM_VERTEXBUFFER_ACCESS_SEQUENTIAL
;
206 start_vertex_location
+= brw
->vb
.start_vertex_bias
;
209 verts_per_instance
= trim(prim
->mode
, prim
->count
);
211 /* If nothing to emit, just return. */
212 if (verts_per_instance
== 0)
215 /* If we're set to always flush, do it before and after the primitive emit.
216 * We want to catch both missed flushes that hurt instruction/state cache
217 * and missed flushes of the render cache as it heads to other parts of
218 * the besides the draw code.
220 if (intel
->always_flush_cache
) {
221 intel_batchbuffer_emit_mi_flush(intel
);
225 OUT_BATCH(CMD_3D_PRIM
<< 16 | (7 - 2));
226 OUT_BATCH(hw_prim
| vertex_access_type
);
227 OUT_BATCH(verts_per_instance
);
228 OUT_BATCH(start_vertex_location
);
229 OUT_BATCH(1); // instance count
230 OUT_BATCH(0); // start instance location
231 OUT_BATCH(base_vertex_location
);
234 if (intel
->always_flush_cache
) {
235 intel_batchbuffer_emit_mi_flush(intel
);
240 static void brw_merge_inputs( struct brw_context
*brw
,
241 const struct gl_client_array
*arrays
[])
243 struct brw_vertex_info old
= brw
->vb
.info
;
246 for (i
= 0; i
< brw
->vb
.nr_buffers
; i
++) {
247 drm_intel_bo_unreference(brw
->vb
.buffers
[i
].bo
);
248 brw
->vb
.buffers
[i
].bo
= NULL
;
250 brw
->vb
.nr_buffers
= 0;
252 memset(&brw
->vb
.info
, 0, sizeof(brw
->vb
.info
));
254 for (i
= 0; i
< VERT_ATTRIB_MAX
; i
++) {
255 brw
->vb
.inputs
[i
].buffer
= -1;
256 brw
->vb
.inputs
[i
].glarray
= arrays
[i
];
257 brw
->vb
.inputs
[i
].attrib
= (gl_vert_attrib
) i
;
259 if (arrays
[i
]->StrideB
!= 0)
260 brw
->vb
.info
.sizes
[i
/16] |= (brw
->vb
.inputs
[i
].glarray
->Size
- 1) <<
264 /* Raise statechanges if input sizes have changed. */
265 if (memcmp(brw
->vb
.info
.sizes
, old
.sizes
, sizeof(old
.sizes
)) != 0)
266 brw
->state
.dirty
.brw
|= BRW_NEW_INPUT_DIMENSIONS
;
269 /* XXX: could split the primitive list to fallback only on the
270 * non-conformant primitives.
272 static GLboolean
check_fallbacks( struct brw_context
*brw
,
273 const struct _mesa_prim
*prim
,
276 struct gl_context
*ctx
= &brw
->intel
.ctx
;
279 /* If we don't require strict OpenGL conformance, never
280 * use fallbacks. If we're forcing fallbacks, always
283 if (brw
->intel
.conformance_mode
== 0)
286 /* Stipple -- these fallbacks could be resolved with a little
289 if (ctx
->Line
.StippleFlag
) {
290 for (i
= 0; i
< nr_prims
; i
++) {
291 /* GS doesn't get enough information to know when to reset
292 * the stipple counter?!?
294 if (prim
[i
].mode
== GL_LINE_LOOP
|| prim
[i
].mode
== GL_LINE_STRIP
)
297 if (prim
[i
].mode
== GL_POLYGON
&&
298 (ctx
->Polygon
.FrontMode
== GL_LINE
||
299 ctx
->Polygon
.BackMode
== GL_LINE
))
304 if (ctx
->Point
.SmoothFlag
) {
305 for (i
= 0; i
< nr_prims
; i
++)
306 if (prim
[i
].mode
== GL_POINTS
)
310 /* Nothing stopping us from the fast path now */
314 /* May fail if out of video memory for texture or vbo upload, or on
315 * fallback conditions.
317 static GLboolean
brw_try_draw_prims( struct gl_context
*ctx
,
318 const struct gl_client_array
*arrays
[],
319 const struct _mesa_prim
*prim
,
321 const struct _mesa_index_buffer
*ib
,
325 struct intel_context
*intel
= intel_context(ctx
);
326 struct brw_context
*brw
= brw_context(ctx
);
327 GLboolean retval
= GL_FALSE
;
328 GLboolean warn
= GL_FALSE
;
332 _mesa_update_state( ctx
);
334 /* We have to validate the textures *before* checking for fallbacks;
335 * otherwise, the software fallback won't be able to rely on the
336 * texture state, the firstLevel and lastLevel fields won't be
337 * set in the intel texture object (they'll both be 0), and the
338 * software fallback will segfault if it attempts to access any
339 * texture level other than level 0.
341 brw_validate_textures( brw
);
343 if (check_fallbacks(brw
, prim
, nr_prims
))
346 /* Bind all inputs, derive varying and size information:
348 brw_merge_inputs( brw
, arrays
);
351 brw
->state
.dirty
.brw
|= BRW_NEW_INDICES
;
353 brw
->vb
.min_index
= min_index
;
354 brw
->vb
.max_index
= max_index
;
355 brw
->state
.dirty
.brw
|= BRW_NEW_VERTICES
;
357 /* Have to validate state quite late. Will rebuild tnl_program,
358 * which depends on varying information.
360 * Note this is where brw->vs->prog_data.inputs_read is calculated,
361 * so can't access it earlier.
364 intel_prepare_render(intel
);
366 for (i
= 0; i
< nr_prims
; i
++) {
368 int estimated_max_prim_size
;
370 estimated_max_prim_size
= 512; /* batchbuffer commands */
371 estimated_max_prim_size
+= (BRW_MAX_TEX_UNIT
*
372 (sizeof(struct brw_sampler_state
) +
373 sizeof(struct gen5_sampler_default_color
)));
374 estimated_max_prim_size
+= 1024; /* gen6 VS push constants */
375 estimated_max_prim_size
+= 1024; /* gen6 WM push constants */
376 estimated_max_prim_size
+= 512; /* misc. pad */
378 /* Flush the batch if it's approaching full, so that we don't wrap while
379 * we've got validated state that needs to be in the same batch as the
382 intel_batchbuffer_require_space(intel
, estimated_max_prim_size
, false);
384 hw_prim
= brw_set_prim(brw
, &prim
[i
]);
385 if (brw
->state
.dirty
.brw
) {
386 brw_validate_state(brw
);
388 /* Various fallback checks: */
389 if (brw
->intel
.Fallback
)
392 /* Check that we can fit our state in with our existing batchbuffer, or
395 if (dri_bufmgr_check_aperture_space(brw
->state
.validated_bos
,
396 brw
->state
.validated_bo_count
)) {
397 static GLboolean warned
;
398 intel_batchbuffer_flush(intel
);
400 /* Validate the state after we flushed the batch (which would have
401 * changed the set of dirty state). If we still fail to
402 * check_aperture, warn of what's happening, but attempt to continue
403 * on since it may succeed anyway, and the user would probably rather
404 * see a failure and a warning than a fallback.
406 brw_validate_state(brw
);
408 dri_bufmgr_check_aperture_space(brw
->state
.validated_bos
,
409 brw
->state
.validated_bo_count
)) {
415 intel
->no_batch_wrap
= GL_TRUE
;
416 brw_upload_state(brw
);
420 gen7_emit_prim(brw
, &prim
[i
], hw_prim
);
422 brw_emit_prim(brw
, &prim
[i
], hw_prim
);
424 intel
->no_batch_wrap
= GL_FALSE
;
429 if (intel
->always_flush_batch
)
430 intel_batchbuffer_flush(intel
);
433 brw_state_cache_check_size(brw
);
436 fprintf(stderr
, "i965: Single primitive emit potentially exceeded "
437 "available aperture space\n");
440 DBG("%s failed\n", __FUNCTION__
);
445 void brw_draw_prims( struct gl_context
*ctx
,
446 const struct gl_client_array
*arrays
[],
447 const struct _mesa_prim
*prim
,
449 const struct _mesa_index_buffer
*ib
,
450 GLboolean index_bounds_valid
,
456 if (!_mesa_check_conditional_render(ctx
))
459 if (!vbo_all_varyings_in_vbos(arrays
)) {
460 if (!index_bounds_valid
)
461 vbo_get_minmax_index(ctx
, prim
, ib
, &min_index
, &max_index
);
463 /* Decide if we want to rebase. If so we end up recursing once
464 * only into this function.
466 if (min_index
!= 0 && !vbo_any_varyings_in_vbos(arrays
)) {
467 vbo_rebase_prims(ctx
, arrays
,
469 ib
, min_index
, max_index
,
475 /* Make a first attempt at drawing:
477 retval
= brw_try_draw_prims(ctx
, arrays
, prim
, nr_prims
, ib
, min_index
, max_index
);
479 /* Otherwise, we really are out of memory. Pass the drawing
480 * command to the software tnl module and which will in turn call
481 * swrast to do the drawing.
484 _swsetup_Wakeup(ctx
);
485 _tnl_draw_prims(ctx
, arrays
, prim
, nr_prims
, ib
, min_index
, max_index
);
490 void brw_draw_init( struct brw_context
*brw
)
492 struct gl_context
*ctx
= &brw
->intel
.ctx
;
493 struct vbo_context
*vbo
= vbo_context(ctx
);
496 /* Register our drawing function:
498 vbo
->draw_prims
= brw_draw_prims
;
500 for (i
= 0; i
< VERT_ATTRIB_MAX
; i
++)
501 brw
->vb
.inputs
[i
].buffer
= -1;
502 brw
->vb
.nr_buffers
= 0;
503 brw
->vb
.nr_enabled
= 0;
506 void brw_draw_destroy( struct brw_context
*brw
)
510 for (i
= 0; i
< brw
->vb
.nr_buffers
; i
++) {
511 drm_intel_bo_unreference(brw
->vb
.buffers
[i
].bo
);
512 brw
->vb
.buffers
[i
].bo
= NULL
;
514 brw
->vb
.nr_buffers
= 0;
516 for (i
= 0; i
< brw
->vb
.nr_enabled
; i
++) {
517 brw
->vb
.enabled
[i
]->buffer
= -1;
519 brw
->vb
.nr_enabled
= 0;
521 drm_intel_bo_unreference(brw
->ib
.bo
);