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 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 if (INTEL_DEBUG
& DEBUG_PRIMS
)
88 _mesa_printf("PRIM: %s\n", _mesa_lookup_enum_by_nr(prim
));
90 /* Slight optimization to avoid the GS program when not needed:
92 if (prim
== GL_QUAD_STRIP
&&
93 brw
->attribs
.Light
->ShadeModel
!= GL_FLAT
&&
94 brw
->attribs
.Polygon
->FrontMode
== GL_FILL
&&
95 brw
->attribs
.Polygon
->BackMode
== GL_FILL
)
96 prim
= GL_TRIANGLE_STRIP
;
98 if (prim
!= brw
->primitive
) {
99 brw
->primitive
= prim
;
100 brw
->state
.dirty
.brw
|= BRW_NEW_PRIMITIVE
;
102 if (reduced_prim
[prim
] != brw
->intel
.reduced_primitive
) {
103 brw
->intel
.reduced_primitive
= reduced_prim
[prim
];
104 brw
->state
.dirty
.brw
|= BRW_NEW_REDUCED_PRIMITIVE
;
107 brw_validate_state(brw
);
110 return 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
;
131 if (INTEL_DEBUG
& DEBUG_PRIMS
)
132 _mesa_printf("PRIM: %s %d %d\n", _mesa_lookup_enum_by_nr(prim
->mode
),
133 prim
->start
, prim
->count
);
135 prim_packet
.header
.opcode
= CMD_3D_PRIM
;
136 prim_packet
.header
.length
= sizeof(prim_packet
)/4 - 2;
137 prim_packet
.header
.pad
= 0;
138 prim_packet
.header
.topology
= brw_set_prim(brw
, prim
->mode
);
139 prim_packet
.header
.indexed
= prim
->indexed
;
141 prim_packet
.verts_per_instance
= trim(prim
->mode
, prim
->count
);
142 prim_packet
.start_vert_location
= prim
->start
;
143 prim_packet
.instance_count
= 1;
144 prim_packet
.start_instance_location
= 0;
145 prim_packet
.base_vert_location
= 0;
147 /* Can't wrap here, since we rely on the validated state. */
148 brw
->no_batch_wrap
= GL_TRUE
;
149 if (prim_packet
.verts_per_instance
) {
150 intel_batchbuffer_data( brw
->intel
.batch
, &prim_packet
,
151 sizeof(prim_packet
), LOOP_CLIPRECTS
);
153 brw
->no_batch_wrap
= GL_FALSE
;
156 static void brw_merge_inputs( struct brw_context
*brw
,
157 const struct gl_client_array
*arrays
[])
159 struct brw_vertex_info old
= brw
->vb
.info
;
162 for (i
= 0; i
< VERT_ATTRIB_MAX
; i
++)
163 dri_bo_unreference(brw
->vb
.inputs
[i
].bo
);
165 memset(&brw
->vb
.inputs
, 0, sizeof(brw
->vb
.inputs
));
166 memset(&brw
->vb
.info
, 0, sizeof(brw
->vb
.info
));
168 for (i
= 0; i
< VERT_ATTRIB_MAX
; i
++) {
169 brw
->vb
.inputs
[i
].glarray
= arrays
[i
];
171 /* XXX: metaops passes null arrays */
173 if (arrays
[i
]->StrideB
!= 0)
174 brw
->vb
.info
.varying
|= 1 << i
;
176 brw
->vb
.info
.sizes
[i
/16] |= (brw
->vb
.inputs
[i
].glarray
->Size
- 1) <<
181 /* Raise statechanges if input sizes and varying have changed:
183 if (memcmp(brw
->vb
.info
.sizes
, old
.sizes
, sizeof(old
.sizes
)) != 0)
184 brw
->state
.dirty
.brw
|= BRW_NEW_INPUT_DIMENSIONS
;
186 if (brw
->vb
.info
.varying
!= old
.varying
)
187 brw
->state
.dirty
.brw
|= BRW_NEW_INPUT_VARYING
;
190 /* XXX: could split the primitive list to fallback only on the
191 * non-conformant primitives.
193 static GLboolean
check_fallbacks( struct brw_context
*brw
,
194 const struct _mesa_prim
*prim
,
199 if (!brw
->intel
.strict_conformance
)
202 if (brw
->attribs
.Polygon
->SmoothFlag
) {
203 for (i
= 0; i
< nr_prims
; i
++)
204 if (reduced_prim
[prim
[i
].mode
] == GL_TRIANGLES
)
208 /* BRW hardware will do AA lines, but they are non-conformant it
209 * seems. TBD whether we keep this fallback:
211 if (brw
->attribs
.Line
->SmoothFlag
) {
212 for (i
= 0; i
< nr_prims
; i
++)
213 if (reduced_prim
[prim
[i
].mode
] == GL_LINES
)
217 /* Stipple -- these fallbacks could be resolved with a little
220 if (brw
->attribs
.Line
->StippleFlag
) {
221 for (i
= 0; i
< nr_prims
; i
++) {
222 /* GS doesn't get enough information to know when to reset
223 * the stipple counter?!?
225 if (prim
[i
].mode
== GL_LINE_LOOP
)
228 if (prim
[i
].mode
== GL_POLYGON
&&
229 (brw
->attribs
.Polygon
->FrontMode
== GL_LINE
||
230 brw
->attribs
.Polygon
->BackMode
== GL_LINE
))
236 if (brw
->attribs
.Point
->SmoothFlag
) {
237 for (i
= 0; i
< nr_prims
; i
++)
238 if (prim
[i
].mode
== GL_POINTS
)
245 /* May fail if out of video memory for texture or vbo upload, or on
246 * fallback conditions.
248 static GLboolean
brw_try_draw_prims( GLcontext
*ctx
,
249 const struct gl_client_array
*arrays
[],
250 const struct _mesa_prim
*prim
,
252 const struct _mesa_index_buffer
*ib
,
256 struct intel_context
*intel
= intel_context(ctx
);
257 struct brw_context
*brw
= brw_context(ctx
);
258 GLboolean retval
= GL_FALSE
;
262 _mesa_update_state( ctx
);
264 brw_validate_textures( brw
);
266 /* Bind all inputs, derive varying and size information:
268 brw_merge_inputs( brw
, arrays
);
271 brw
->state
.dirty
.brw
|= BRW_NEW_INDICES
;
273 brw
->vb
.min_index
= min_index
;
274 brw
->vb
.max_index
= max_index
;
275 brw
->state
.dirty
.brw
|= BRW_NEW_VERTICES
;
276 /* Have to validate state quite late. Will rebuild tnl_program,
277 * which depends on varying information.
279 * Note this is where brw->vs->prog_data.inputs_read is calculated,
280 * so can't access it earlier.
283 LOCK_HARDWARE(intel
);
285 if (brw
->intel
.numClipRects
== 0) {
286 UNLOCK_HARDWARE(intel
);
291 /* Flush the batch if it's approaching full, so that we don't wrap while
292 * we've got validated state that needs to be in the same batch as the
293 * primitives. This fraction is just a guess (minimal full state plus
294 * a primitive is around 512 bytes), and would be better if we had
295 * an upper bound of how much we might emit in a single
296 * brw_try_draw_prims().
298 if (intel
->batch
->ptr
- intel
->batch
->map
> intel
->batch
->size
* 3 / 4
299 /* brw_emit_prim may change the cliprect_mode to LOOP_CLIPRECTS */
300 || intel
->batch
->cliprect_mode
!= LOOP_CLIPRECTS
)
301 intel_batchbuffer_flush(intel
->batch
);
303 /* Set the first primitive early, ahead of validate_state:
305 brw_set_prim(brw
, prim
[0].mode
);
307 /* XXX: Need to separate validate and upload of state.
309 brw_validate_state( brw
);
311 /* Various fallback checks:
313 if (brw
->intel
.Fallback
)
316 if (check_fallbacks( brw
, prim
, nr_prims
))
319 for (i
= 0; i
< nr_prims
; i
++) {
320 brw_emit_prim(brw
, &prim
[i
]);
327 UNLOCK_HARDWARE(intel
);
330 DBG("%s failed\n", __FUNCTION__
);
335 static GLboolean
brw_need_rebase( GLcontext
*ctx
,
336 const struct gl_client_array
*arrays
[],
337 const struct _mesa_index_buffer
*ib
,
344 if (!vbo_all_varyings_in_vbos(arrays
))
350 /* Hmm. This isn't quite what I wanted. BRW can actually
351 * handle the mixed case well enough that we shouldn't need to
352 * rebase. However, it's probably not very common, nor hugely
353 * expensive to do it this way:
355 if (!vbo_all_varyings_in_vbos(arrays
))
363 void brw_draw_prims( GLcontext
*ctx
,
364 const struct gl_client_array
*arrays
[],
365 const struct _mesa_prim
*prim
,
367 const struct _mesa_index_buffer
*ib
,
373 /* Decide if we want to rebase. If so we end up recursing once
374 * only into this function.
376 if (brw_need_rebase( ctx
, arrays
, ib
, min_index
)) {
377 vbo_rebase_prims( ctx
, arrays
,
379 ib
, min_index
, max_index
,
386 /* Make a first attempt at drawing:
388 retval
= brw_try_draw_prims(ctx
, arrays
, prim
, nr_prims
, ib
, min_index
, max_index
);
390 /* Otherwise, we really are out of memory. Pass the drawing
391 * command to the software tnl module and which will in turn call
392 * swrast to do the drawing.
395 _swsetup_Wakeup(ctx
);
396 _tnl_draw_prims(ctx
, arrays
, prim
, nr_prims
, ib
, min_index
, max_index
);
400 void brw_draw_init( struct brw_context
*brw
)
402 GLcontext
*ctx
= &brw
->intel
.ctx
;
403 struct vbo_context
*vbo
= vbo_context(ctx
);
405 /* Register our drawing function:
407 vbo
->draw_prims
= brw_draw_prims
;
410 void brw_draw_destroy( struct brw_context
*brw
)
412 if (brw
->vb
.upload
.bo
!= NULL
) {
413 dri_bo_unreference(brw
->vb
.upload
.bo
);
414 brw
->vb
.upload
.bo
= NULL
;