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 **************************************************************************/
30 #include "main/glheader.h"
31 #include "main/context.h"
32 #include "main/state.h"
33 #include "main/api_validate.h"
34 #include "main/enums.h"
37 #include "brw_defines.h"
38 #include "brw_context.h"
39 #include "brw_state.h"
40 #include "brw_fallback.h"
42 #include "intel_batchbuffer.h"
43 #include "intel_buffer_objects.h"
46 #include "vbo/vbo_context.h"
47 #include "swrast/swrast.h"
48 #include "swrast_setup/swrast_setup.h"
50 #define FILE_DEBUG_FLAG DEBUG_BATCH
52 static GLuint prim_to_hw_prim
[GL_POLYGON
+1] = {
66 static const GLenum reduced_prim
[GL_POLYGON
+1] = {
80 /* When the primitive changes, set a state bit and re-validate. Not
81 * the nicest and would rather deal with this by having all the
82 * programs be immune to the active primitive (ie. cope with all
83 * possibilities). That may not be realistic however.
85 static GLuint
brw_set_prim(struct brw_context
*brw
, GLenum prim
)
87 GLcontext
*ctx
= &brw
->intel
.ctx
;
89 if (INTEL_DEBUG
& DEBUG_PRIMS
)
90 _mesa_printf("PRIM: %s\n", _mesa_lookup_enum_by_nr(prim
));
92 /* Slight optimization to avoid the GS program when not needed:
94 if (prim
== GL_QUAD_STRIP
&&
95 ctx
->Light
.ShadeModel
!= GL_FLAT
&&
96 ctx
->Polygon
.FrontMode
== GL_FILL
&&
97 ctx
->Polygon
.BackMode
== GL_FILL
)
98 prim
= GL_TRIANGLE_STRIP
;
100 if (prim
!= brw
->primitive
) {
101 brw
->primitive
= prim
;
102 brw
->state
.dirty
.brw
|= BRW_NEW_PRIMITIVE
;
104 if (reduced_prim
[prim
] != brw
->intel
.reduced_primitive
) {
105 brw
->intel
.reduced_primitive
= reduced_prim
[prim
];
106 brw
->state
.dirty
.brw
|= BRW_NEW_REDUCED_PRIMITIVE
;
110 return prim_to_hw_prim
[prim
];
114 static GLuint
trim(GLenum prim
, GLuint length
)
116 if (prim
== GL_QUAD_STRIP
)
117 return length
> 3 ? (length
- length
% 2) : 0;
118 else if (prim
== GL_QUADS
)
119 return length
- length
% 4;
125 static void brw_emit_prim(struct brw_context
*brw
,
126 const struct _mesa_prim
*prim
,
129 struct brw_3d_primitive prim_packet
;
130 struct intel_context
*intel
= &brw
->intel
;
132 if (INTEL_DEBUG
& DEBUG_PRIMS
)
133 _mesa_printf("PRIM: %s %d %d\n", _mesa_lookup_enum_by_nr(prim
->mode
),
134 prim
->start
, prim
->count
);
136 prim_packet
.header
.opcode
= CMD_3D_PRIM
;
137 prim_packet
.header
.length
= sizeof(prim_packet
)/4 - 2;
138 prim_packet
.header
.pad
= 0;
139 prim_packet
.header
.topology
= hw_prim
;
140 prim_packet
.header
.indexed
= prim
->indexed
;
142 prim_packet
.verts_per_instance
= trim(prim
->mode
, prim
->count
);
143 prim_packet
.start_vert_location
= prim
->start
;
145 prim_packet
.start_vert_location
+= brw
->ib
.start_vertex_offset
;
146 prim_packet
.instance_count
= 1;
147 prim_packet
.start_instance_location
= 0;
148 prim_packet
.base_vert_location
= 0;
150 /* Can't wrap here, since we rely on the validated state. */
151 brw
->no_batch_wrap
= GL_TRUE
;
153 /* If we're set to always flush, do it before and after the primitive emit.
154 * We want to catch both missed flushes that hurt instruction/state cache
155 * and missed flushes of the render cache as it heads to other parts of
156 * the besides the draw code.
158 if (intel
->always_flush_cache
) {
159 BEGIN_BATCH(1, IGNORE_CLIPRECTS
);
160 OUT_BATCH(intel
->vtbl
.flush_cmd());
163 if (prim_packet
.verts_per_instance
) {
164 intel_batchbuffer_data( brw
->intel
.batch
, &prim_packet
,
165 sizeof(prim_packet
), LOOP_CLIPRECTS
);
167 if (intel
->always_flush_cache
) {
168 BEGIN_BATCH(1, IGNORE_CLIPRECTS
);
169 OUT_BATCH(intel
->vtbl
.flush_cmd());
173 brw
->no_batch_wrap
= GL_FALSE
;
176 static void brw_merge_inputs( struct brw_context
*brw
,
177 const struct gl_client_array
*arrays
[])
179 struct brw_vertex_info old
= brw
->vb
.info
;
182 for (i
= 0; i
< VERT_ATTRIB_MAX
; i
++)
183 dri_bo_unreference(brw
->vb
.inputs
[i
].bo
);
185 memset(&brw
->vb
.inputs
, 0, sizeof(brw
->vb
.inputs
));
186 memset(&brw
->vb
.info
, 0, sizeof(brw
->vb
.info
));
188 for (i
= 0; i
< VERT_ATTRIB_MAX
; i
++) {
189 brw
->vb
.inputs
[i
].glarray
= arrays
[i
];
191 if (arrays
[i
]->StrideB
!= 0)
192 brw
->vb
.info
.sizes
[i
/16] |= (brw
->vb
.inputs
[i
].glarray
->Size
- 1) <<
196 /* Raise statechanges if input sizes have changed. */
197 if (memcmp(brw
->vb
.info
.sizes
, old
.sizes
, sizeof(old
.sizes
)) != 0)
198 brw
->state
.dirty
.brw
|= BRW_NEW_INPUT_DIMENSIONS
;
201 /* XXX: could split the primitive list to fallback only on the
202 * non-conformant primitives.
204 static GLboolean
check_fallbacks( struct brw_context
*brw
,
205 const struct _mesa_prim
*prim
,
208 GLcontext
*ctx
= &brw
->intel
.ctx
;
211 /* If we don't require strict OpenGL conformance, never
212 * use fallbacks. If we're forcing fallbacks, always
215 if (brw
->intel
.conformance_mode
== 0)
218 if (brw
->intel
.conformance_mode
== 2)
221 if (ctx
->Polygon
.SmoothFlag
) {
222 for (i
= 0; i
< nr_prims
; i
++)
223 if (reduced_prim
[prim
[i
].mode
] == GL_TRIANGLES
)
227 /* BRW hardware will do AA lines, but they are non-conformant it
228 * seems. TBD whether we keep this fallback:
230 if (ctx
->Line
.SmoothFlag
) {
231 for (i
= 0; i
< nr_prims
; i
++)
232 if (reduced_prim
[prim
[i
].mode
] == GL_LINES
)
236 /* Stipple -- these fallbacks could be resolved with a little
239 if (ctx
->Line
.StippleFlag
) {
240 for (i
= 0; i
< nr_prims
; i
++) {
241 /* GS doesn't get enough information to know when to reset
242 * the stipple counter?!?
244 if (prim
[i
].mode
== GL_LINE_LOOP
|| prim
[i
].mode
== GL_LINE_STRIP
)
247 if (prim
[i
].mode
== GL_POLYGON
&&
248 (ctx
->Polygon
.FrontMode
== GL_LINE
||
249 ctx
->Polygon
.BackMode
== GL_LINE
))
254 if (ctx
->Point
.SmoothFlag
) {
255 for (i
= 0; i
< nr_prims
; i
++)
256 if (prim
[i
].mode
== GL_POINTS
)
260 /* BRW hardware doesn't handle GL_CLAMP texturing correctly;
261 * brw_wm_sampler_state:translate_wrap_mode() treats GL_CLAMP
262 * as GL_CLAMP_TO_EDGE instead. If we're using GL_CLAMP, and
263 * we want strict conformance, force the fallback.
264 * Right now, we only do this for 2D textures.
268 for (u
= 0; u
< ctx
->Const
.MaxTextureCoordUnits
; u
++) {
269 struct gl_texture_unit
*texUnit
= &ctx
->Texture
.Unit
[u
];
270 if (texUnit
->Enabled
) {
271 if (texUnit
->Enabled
& TEXTURE_1D_BIT
) {
272 if (texUnit
->CurrentTex
[TEXTURE_1D_INDEX
]->WrapS
== GL_CLAMP
) {
276 if (texUnit
->Enabled
& TEXTURE_2D_BIT
) {
277 if (texUnit
->CurrentTex
[TEXTURE_2D_INDEX
]->WrapS
== GL_CLAMP
||
278 texUnit
->CurrentTex
[TEXTURE_2D_INDEX
]->WrapT
== GL_CLAMP
) {
282 if (texUnit
->Enabled
& TEXTURE_3D_BIT
) {
283 if (texUnit
->CurrentTex
[TEXTURE_3D_INDEX
]->WrapS
== GL_CLAMP
||
284 texUnit
->CurrentTex
[TEXTURE_3D_INDEX
]->WrapT
== GL_CLAMP
||
285 texUnit
->CurrentTex
[TEXTURE_3D_INDEX
]->WrapR
== GL_CLAMP
) {
293 /* Nothing stopping us from the fast path now */
297 /* May fail if out of video memory for texture or vbo upload, or on
298 * fallback conditions.
300 static GLboolean
brw_try_draw_prims( GLcontext
*ctx
,
301 const struct gl_client_array
*arrays
[],
302 const struct _mesa_prim
*prim
,
304 const struct _mesa_index_buffer
*ib
,
308 struct intel_context
*intel
= intel_context(ctx
);
309 struct brw_context
*brw
= brw_context(ctx
);
310 GLboolean retval
= GL_FALSE
;
311 GLboolean warn
= GL_FALSE
;
312 GLboolean first_time
= GL_TRUE
;
316 _mesa_update_state( ctx
);
318 /* We have to validate the textures *before* checking for fallbacks;
319 * otherwise, the software fallback won't be able to rely on the
320 * texture state, the firstLevel and lastLevel fields won't be
321 * set in the intel texture object (they'll both be 0), and the
322 * software fallback will segfault if it attempts to access any
323 * texture level other than level 0.
325 brw_validate_textures( brw
);
327 if (check_fallbacks(brw
, prim
, nr_prims
))
330 /* Bind all inputs, derive varying and size information:
332 brw_merge_inputs( brw
, arrays
);
335 brw
->state
.dirty
.brw
|= BRW_NEW_INDICES
;
337 brw
->vb
.min_index
= min_index
;
338 brw
->vb
.max_index
= max_index
;
339 brw
->state
.dirty
.brw
|= BRW_NEW_VERTICES
;
341 /* Have to validate state quite late. Will rebuild tnl_program,
342 * which depends on varying information.
344 * Note this is where brw->vs->prog_data.inputs_read is calculated,
345 * so can't access it earlier.
348 LOCK_HARDWARE(intel
);
350 if (!intel
->constant_cliprect
&& intel
->driDrawable
->numClipRects
== 0) {
351 UNLOCK_HARDWARE(intel
);
355 for (i
= 0; i
< nr_prims
; i
++) {
358 /* Flush the batch if it's approaching full, so that we don't wrap while
359 * we've got validated state that needs to be in the same batch as the
360 * primitives. This fraction is just a guess (minimal full state plus
361 * a primitive is around 512 bytes), and would be better if we had
362 * an upper bound of how much we might emit in a single
363 * brw_try_draw_prims().
365 intel_batchbuffer_require_space(intel
->batch
, intel
->batch
->size
/ 4,
368 hw_prim
= brw_set_prim(brw
, prim
[i
].mode
);
370 if (first_time
|| (brw
->state
.dirty
.brw
& BRW_NEW_PRIMITIVE
)) {
371 first_time
= GL_FALSE
;
373 brw_validate_state(brw
);
375 /* Various fallback checks: */
376 if (brw
->intel
.Fallback
)
379 /* Check that we can fit our state in with our existing batchbuffer, or
382 if (dri_bufmgr_check_aperture_space(brw
->state
.validated_bos
,
383 brw
->state
.validated_bo_count
)) {
384 static GLboolean warned
;
385 intel_batchbuffer_flush(intel
->batch
);
387 /* Validate the state after we flushed the batch (which would have
388 * changed the set of dirty state). If we still fail to
389 * check_aperture, warn of what's happening, but attempt to continue
390 * on since it may succeed anyway, and the user would probably rather
391 * see a failure and a warning than a fallback.
393 brw_validate_state(brw
);
395 dri_bufmgr_check_aperture_space(brw
->state
.validated_bos
,
396 brw
->state
.validated_bo_count
)) {
402 brw_upload_state(brw
);
405 brw_emit_prim(brw
, &prim
[i
], hw_prim
);
410 if (intel
->always_flush_batch
)
411 intel_batchbuffer_flush(intel
->batch
);
413 UNLOCK_HARDWARE(intel
);
415 brw_state_cache_check_size(brw
);
418 fprintf(stderr
, "i965: Single primitive emit potentially exceeded "
419 "available aperture space\n");
422 DBG("%s failed\n", __FUNCTION__
);
427 void brw_draw_prims( GLcontext
*ctx
,
428 const struct gl_client_array
*arrays
[],
429 const struct _mesa_prim
*prim
,
431 const struct _mesa_index_buffer
*ib
,
432 GLboolean index_bounds_valid
,
438 if (!vbo_all_varyings_in_vbos(arrays
)) {
439 if (!index_bounds_valid
)
440 vbo_get_minmax_index(ctx
, prim
, ib
, &min_index
, &max_index
);
442 /* Decide if we want to rebase. If so we end up recursing once
443 * only into this function.
445 if (min_index
!= 0) {
446 vbo_rebase_prims(ctx
, arrays
,
448 ib
, min_index
, max_index
,
454 /* Make a first attempt at drawing:
456 retval
= brw_try_draw_prims(ctx
, arrays
, prim
, nr_prims
, ib
, min_index
, max_index
);
458 /* Otherwise, we really are out of memory. Pass the drawing
459 * command to the software tnl module and which will in turn call
460 * swrast to do the drawing.
463 _swsetup_Wakeup(ctx
);
464 _tnl_draw_prims(ctx
, arrays
, prim
, nr_prims
, ib
, min_index
, max_index
);
469 void brw_draw_init( struct brw_context
*brw
)
471 GLcontext
*ctx
= &brw
->intel
.ctx
;
472 struct vbo_context
*vbo
= vbo_context(ctx
);
474 /* Register our drawing function:
476 vbo
->draw_prims
= brw_draw_prims
;
479 void brw_draw_destroy( struct brw_context
*brw
)
483 if (brw
->vb
.upload
.bo
!= NULL
) {
484 dri_bo_unreference(brw
->vb
.upload
.bo
);
485 brw
->vb
.upload
.bo
= NULL
;
488 for (i
= 0; i
< VERT_ATTRIB_MAX
; i
++) {
489 dri_bo_unreference(brw
->vb
.inputs
[i
].bo
);
490 brw
->vb
.inputs
[i
].bo
= NULL
;
493 dri_bo_unreference(brw
->ib
.bo
);