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 "brw_defines.h"
31 #include "brw_context.h"
32 #include "brw_state.h"
34 #include "brw_batchbuffer.h"
35 #include "intel_buffer_objects.h"
37 #define FILE_DEBUG_FLAG DEBUG_BATCH
39 static uint32_t prim_to_hw_prim
[PIPE_PRIM_POLYGON
+1] = {
54 /* When the primitive changes, set a state bit and re-validate. Not
55 * the nicest and would rather deal with this by having all the
56 * programs be immune to the active primitive (ie. cope with all
57 * possibilities). That may not be realistic however.
59 static GLuint
brw_set_prim(struct brw_context
*brw
, GLenum prim
)
62 if (INTEL_DEBUG
& DEBUG_PRIMS
)
63 _mesa_printf("PRIM: %s\n", _mesa_lookup_enum_by_nr(prim
));
65 /* Slight optimization to avoid the GS program when not needed:
67 if (prim
== GL_QUAD_STRIP
&&
68 ctx
->Light
.ShadeModel
!= GL_FLAT
&&
69 ctx
->Polygon
.FrontMode
== GL_FILL
&&
70 ctx
->Polygon
.BackMode
== GL_FILL
)
71 prim
= GL_TRIANGLE_STRIP
;
73 if (prim
!= brw
->primitive
) {
74 brw
->primitive
= prim
;
75 brw
->state
.dirty
.brw
|= BRW_NEW_PRIMITIVE
;
77 if (reduced_prim
[prim
] != brw
->intel
.reduced_primitive
) {
78 brw
->intel
.reduced_primitive
= reduced_prim
[prim
];
79 brw
->state
.dirty
.brw
|= BRW_NEW_REDUCED_PRIMITIVE
;
83 return prim_to_hw_prim
[prim
];
87 static GLuint
trim(GLenum prim
, GLuint length
)
89 if (prim
== GL_QUAD_STRIP
)
90 return length
> 3 ? (length
- length
% 2) : 0;
91 else if (prim
== GL_QUADS
)
92 return length
- length
% 4;
98 static void brw_emit_prim(struct brw_context
*brw
,
99 const struct _mesa_prim
*prim
,
102 struct brw_3d_primitive prim_packet
;
104 if (INTEL_DEBUG
& DEBUG_PRIMS
)
105 _mesa_printf("PRIM: %s %d %d\n", _mesa_lookup_enum_by_nr(prim
->mode
),
106 prim
->start
, prim
->count
);
108 prim_packet
.header
.opcode
= CMD_3D_PRIM
;
109 prim_packet
.header
.length
= sizeof(prim_packet
)/4 - 2;
110 prim_packet
.header
.pad
= 0;
111 prim_packet
.header
.topology
= hw_prim
;
112 prim_packet
.header
.indexed
= prim
->indexed
;
114 prim_packet
.verts_per_instance
= trim(prim
->mode
, prim
->count
);
115 prim_packet
.start_vert_location
= prim
->start
;
117 prim_packet
.start_vert_location
+= brw
->ib
.start_vertex_offset
;
118 prim_packet
.instance_count
= 1;
119 prim_packet
.start_instance_location
= 0;
120 prim_packet
.base_vert_location
= prim
->basevertex
;
122 /* Can't wrap here, since we rely on the validated state. */
123 brw
->no_batch_wrap
= GL_TRUE
;
125 /* If we're set to always flush, do it before and after the primitive emit.
126 * We want to catch both missed flushes that hurt instruction/state cache
127 * and missed flushes of the render cache as it heads to other parts of
128 * the besides the draw code.
130 if (intel
->always_flush_cache
) {
131 BEGIN_BATCH(1, IGNORE_CLIPRECTS
);
132 OUT_BATCH(intel
->vtbl
.flush_cmd());
135 if (prim_packet
.verts_per_instance
) {
136 brw_batchbuffer_data( brw
->intel
.batch
, &prim_packet
,
137 sizeof(prim_packet
), LOOP_CLIPRECTS
);
139 if (intel
->always_flush_cache
) {
140 BEGIN_BATCH(1, IGNORE_CLIPRECTS
);
141 OUT_BATCH(intel
->vtbl
.flush_cmd());
145 brw
->no_batch_wrap
= GL_FALSE
;
148 static void brw_merge_inputs( struct brw_context
*brw
,
149 const struct gl_client_array
*arrays
[])
151 struct brw_vertex_info old
= brw
->vb
.info
;
154 for (i
= 0; i
< VERT_ATTRIB_MAX
; i
++)
155 brw
->sws
->bo_unreference(brw
->vb
.inputs
[i
].bo
);
157 memset(&brw
->vb
.inputs
, 0, sizeof(brw
->vb
.inputs
));
158 memset(&brw
->vb
.info
, 0, sizeof(brw
->vb
.info
));
160 for (i
= 0; i
< VERT_ATTRIB_MAX
; i
++) {
161 brw
->vb
.inputs
[i
].glarray
= arrays
[i
];
162 brw
->vb
.inputs
[i
].attrib
= (gl_vert_attrib
) i
;
164 if (arrays
[i
]->StrideB
!= 0)
165 brw
->vb
.info
.sizes
[i
/16] |= (brw
->vb
.inputs
[i
].glarray
->Size
- 1) <<
169 /* Raise statechanges if input sizes have changed. */
170 if (memcmp(brw
->vb
.info
.sizes
, old
.sizes
, sizeof(old
.sizes
)) != 0)
171 brw
->state
.dirty
.brw
|= BRW_NEW_INPUT_DIMENSIONS
;
174 /* May fail if out of video memory for texture or vbo upload, or on
175 * fallback conditions.
177 static GLboolean
brw_try_draw_prims( struct brw_context
*brw
,
178 const struct gl_client_array
*arrays
[],
179 const struct _mesa_prim
*prim
,
181 const struct _mesa_index_buffer
*ib
,
185 struct brw_context
*brw
= brw_context(ctx
);
186 GLboolean retval
= GL_FALSE
;
187 GLboolean warn
= GL_FALSE
;
188 GLboolean first_time
= GL_TRUE
;
193 _mesa_update_state( ctx
);
195 /* Bind all inputs, derive varying and size information:
197 brw_merge_inputs( brw
, arrays
);
200 brw
->state
.dirty
.brw
|= BRW_NEW_INDICES
;
202 brw
->vb
.min_index
= min_index
;
203 brw
->vb
.max_index
= max_index
;
204 brw
->state
.dirty
.brw
|= BRW_NEW_VERTICES
;
206 hw_prim
= brw_set_prim(brw
, prim
[i
].mode
);
208 brw_validate_state(brw
);
210 /* Check that we can fit our state in with our existing batchbuffer, or
213 ret
= dri_bufmgr_check_aperture_space(brw
->state
.validated_bos
,
214 brw
->state
.validated_bo_count
);
218 ret
= brw_upload_state(brw
);
222 ret
= brw_emit_prim(brw
, &prim
[i
], hw_prim
);
226 if (intel
->always_flush_batch
)
227 brw_batchbuffer_flush(intel
->batch
);
232 void brw_draw_prims( struct brw_context
*brw
,
233 const struct gl_client_array
*arrays
[],
234 const struct _mesa_prim
*prim
,
236 const struct _mesa_index_buffer
*ib
,
237 GLboolean index_bounds_valid
,
243 if (!vbo_all_varyings_in_vbos(arrays
)) {
244 if (!index_bounds_valid
)
245 vbo_get_minmax_index(ctx
, prim
, ib
, &min_index
, &max_index
);
248 /* Make a first attempt at drawing:
250 ret
= brw_try_draw_prims(ctx
, arrays
, prim
, nr_prims
, ib
, min_index
, max_index
);
252 /* Otherwise, flush and retry:
255 brw_batchbuffer_flush(intel
->batch
);
256 ret
= brw_try_draw_prims(ctx
, arrays
, prim
, nr_prims
, ib
, min_index
, max_index
);
261 void brw_draw_init( struct brw_context
*brw
)
263 struct vbo_context
*vbo
= vbo_context(ctx
);
265 /* Register our drawing function:
267 vbo
->draw_prims
= brw_draw_prims
;
270 void brw_draw_destroy( struct brw_context
*brw
)
274 if (brw
->vb
.upload
.bo
!= NULL
) {
275 brw
->sws
->bo_unreference(brw
->vb
.upload
.bo
);
276 brw
->vb
.upload
.bo
= NULL
;
279 for (i
= 0; i
< VERT_ATTRIB_MAX
; i
++) {
280 brw
->sws
->bo_unreference(brw
->vb
.inputs
[i
].bo
);
281 brw
->vb
.inputs
[i
].bo
= NULL
;
284 brw
->sws
->bo_unreference(brw
->ib
.bo
);