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 **************************************************************************/
33 #include "api_validate.h"
37 #include "brw_defines.h"
38 #include "brw_attrib.h"
39 #include "brw_context.h"
41 #include "brw_state.h"
42 #include "brw_fallback.h"
44 #include "intel_ioctl.h"
45 #include "intel_batchbuffer.h"
46 #include "intel_buffer_objects.h"
53 static GLuint hw_prim
[GL_POLYGON
+1] = {
67 static const GLenum reduced_prim
[GL_POLYGON
+1] = {
81 /* When the primitive changes, set a state bit and re-validate. Not
82 * the nicest and would rather deal with this by having all the
83 * programs be immune to the active primitive (ie. cope with all
84 * possibilities). That may not be realistic however.
86 static GLuint
brw_set_prim(struct brw_context
*brw
, GLenum prim
)
88 if (INTEL_DEBUG
& DEBUG_PRIMS
)
89 _mesa_printf("PRIM: %s\n", _mesa_lookup_enum_by_nr(prim
));
91 /* Slight optimization to avoid the GS program when not needed:
93 if (prim
== GL_QUAD_STRIP
&&
94 brw
->attribs
.Light
->ShadeModel
!= GL_FLAT
&&
95 brw
->attribs
.Polygon
->FrontMode
== GL_FILL
&&
96 brw
->attribs
.Polygon
->BackMode
== GL_FILL
)
97 prim
= GL_TRIANGLE_STRIP
;
99 if (prim
!= brw
->primitive
) {
100 brw
->primitive
= prim
;
101 brw
->state
.dirty
.brw
|= BRW_NEW_PRIMITIVE
;
103 if (reduced_prim
[prim
] != brw
->intel
.reduced_primitive
) {
104 brw
->intel
.reduced_primitive
= reduced_prim
[prim
];
105 brw
->state
.dirty
.brw
|= BRW_NEW_REDUCED_PRIMITIVE
;
108 brw_validate_state(brw
);
111 return hw_prim
[prim
];
115 static GLuint
trim(GLenum prim
, GLuint length
)
117 if (prim
== GL_QUAD_STRIP
)
118 return length
> 3 ? (length
- length
% 2) : 0;
119 else if (prim
== GL_QUADS
)
120 return length
- length
% 4;
128 static void brw_emit_prim( struct brw_context
*brw
,
129 const struct brw_draw_prim
*prim
)
132 struct brw_3d_primitive prim_packet
;
134 if (INTEL_DEBUG
& DEBUG_PRIMS
)
135 _mesa_printf("PRIM: %s %d %d\n", _mesa_lookup_enum_by_nr(prim
->mode
),
136 prim
->start
, prim
->count
);
138 prim_packet
.header
.opcode
= CMD_3D_PRIM
;
139 prim_packet
.header
.length
= sizeof(prim_packet
)/4 - 2;
140 prim_packet
.header
.pad
= 0;
141 prim_packet
.header
.topology
= brw_set_prim(brw
, prim
->mode
);
142 prim_packet
.header
.indexed
= prim
->indexed
;
144 prim_packet
.verts_per_instance
= trim(prim
->mode
, prim
->count
);
145 prim_packet
.start_vert_location
= prim
->start
;
146 prim_packet
.instance_count
= 1;
147 prim_packet
.start_instance_location
= 0;
148 prim_packet
.base_vert_location
= 0;
150 if (prim_packet
.verts_per_instance
) {
151 intel_batchbuffer_data( brw
->intel
.batch
, &prim_packet
, sizeof(prim_packet
),
152 INTEL_BATCH_CLIPRECTS
);
158 static void update_current_size( struct gl_client_array
*array
)
160 const GLfloat
*ptr
= (const GLfloat
*)array
->Ptr
;
162 assert(array
->StrideB
== 0);
163 assert(array
->Type
== GL_FLOAT
);
167 else if (ptr
[2] != 0.0)
169 else if (ptr
[1] != 0.0)
177 /* Fill in any gaps in passed arrays with pointers to current
180 static void brw_merge_inputs( struct brw_context
*brw
,
181 const struct gl_client_array
*arrays
[])
183 struct gl_client_array
*current_values
= brw
->vb
.current_values
;
184 struct brw_vertex_element
*inputs
= brw
->vb
.inputs
;
185 struct brw_vertex_info old
= brw
->vb
.info
;
188 memset(inputs
, 0, sizeof(*inputs
));
189 memset(&brw
->vb
.info
, 0, sizeof(brw
->vb
.info
));
191 for (i
= 0; i
< BRW_ATTRIB_MAX
; i
++) {
192 if (arrays
[i
] && arrays
[i
]->Enabled
)
194 brw
->vb
.inputs
[i
].glarray
= arrays
[i
];
195 brw
->vb
.info
.varying
[i
/32] |= 1 << (i
%32);
199 brw
->vb
.inputs
[i
].glarray
= ¤t_values
[i
];
201 /* XXX: This will change on the Mesa trunk as Brian has moved
202 * edgeflag, index into this range:
204 if (i
< VERT_ATTRIB_GENERIC0
)
205 update_current_size(¤t_values
[i
]);
208 brw
->vb
.info
.sizes
[i
/16] |= (inputs
[i
].glarray
->Size
- 1) << ((i
%16) * 2);
211 /* Raise statechanges if input sizes and varying have changed:
213 if (memcmp(brw
->vb
.info
.sizes
, old
.sizes
, sizeof(old
.sizes
)) != 0)
214 brw
->state
.dirty
.brw
|= BRW_NEW_INPUT_DIMENSIONS
;
216 if (memcmp(brw
->vb
.info
.varying
, old
.varying
, sizeof(old
.varying
)) != 0)
217 brw
->state
.dirty
.brw
|= BRW_NEW_INPUT_VARYING
;
220 static GLboolean
check_fallbacks( struct brw_context
*brw
,
221 const struct brw_draw_prim
*prim
,
226 if (!brw
->intel
.strict_conformance
)
229 if (brw
->attribs
.Polygon
->SmoothFlag
) {
230 for (i
= 0; i
< nr_prims
; i
++)
231 if (reduced_prim
[prim
[i
].mode
] == GL_TRIANGLES
)
235 /* BRW hardware will do AA lines, but they are non-conformant it
236 * seems. TBD whether we keep this fallback:
238 if (brw
->attribs
.Line
->SmoothFlag
) {
239 for (i
= 0; i
< nr_prims
; i
++)
240 if (reduced_prim
[prim
[i
].mode
] == GL_LINES
)
244 /* Stipple -- these fallbacks could be resolved with a little
247 if (brw
->attribs
.Line
->StippleFlag
) {
248 for (i
= 0; i
< nr_prims
; i
++) {
249 /* GS doesn't get enough information to know when to reset
250 * the stipple counter?!?
252 if (prim
[i
].mode
== GL_LINE_LOOP
)
255 if (prim
[i
].mode
== GL_POLYGON
&&
256 (brw
->attribs
.Polygon
->FrontMode
== GL_LINE
||
257 brw
->attribs
.Polygon
->BackMode
== GL_LINE
))
263 if (brw
->attribs
.Point
->SmoothFlag
) {
264 for (i
= 0; i
< nr_prims
; i
++)
265 if (prim
[i
].mode
== GL_POINTS
)
273 GLboolean
brw_draw_prims( GLcontext
*ctx
,
274 const struct gl_client_array
*arrays
[],
275 const struct brw_draw_prim
*prim
,
277 const struct brw_draw_index_buffer
*ib
,
282 struct intel_context
*intel
= intel_context(ctx
);
283 struct brw_context
*brw
= brw_context(ctx
);
284 GLboolean retval
= GL_FALSE
;
288 _mesa_update_state( ctx
);
290 /* Bind all inputs, derive varying and size information:
292 brw_merge_inputs( brw
, arrays
);
294 /* Have to validate state quite late. Will rebuild tnl_program,
295 * which depends on varying information.
297 * Note this is where brw->vs->prog_data.inputs_read is calculated,
298 * so can't access it earlier.
301 LOCK_HARDWARE(intel
);
303 assert(intel
->locked
);
305 /* Set the first primitive early, ahead of validate_state:
307 brw_set_prim(brw
, prim
[0].mode
);
309 /* XXX: Need to separate validate and upload of state.
311 brw_validate_state( brw
);
313 /* Various fallback checks:
315 if (brw
->intel
.Fallback
)
318 if (check_fallbacks( brw
, prim
, nr_prims
))
321 /* Upload index, vertex data:
324 brw_upload_indices( brw
, ib
);
326 if (!brw_upload_vertices( brw
, min_index
, max_index
)) {
330 /* Emit prims to batchbuffer:
332 for (i
= 0; i
< nr_prims
; i
++) {
333 brw_emit_prim(brw
, &prim
[i
]);
341 /* Currently have to do this to synchronize with the map/unmap of
342 * the vertex buffer in brw_exec_api.c. Not sure if there is any
343 * way around this, as not every flush is due to a buffer filling
346 intel_batchbuffer_flush( brw
->intel
.batch
);
348 if (intel
->thrashing
) {
352 /* Free any old data so it doesn't clog up texture memory - we
353 * won't be referencing it again.
355 while (brw
->vb
.upload
.wrap
!= brw
->vb
.upload
.buf
) {
356 ctx
->Driver
.BufferData(ctx
,
358 BRW_UPLOAD_INIT_SIZE
,
361 brw
->vb
.upload
.vbo
[brw
->vb
.upload
.wrap
]);
362 brw
->vb
.upload
.wrap
++;
363 brw
->vb
.upload
.wrap
%= BRW_NR_UPLOAD_BUFS
;
366 UNLOCK_HARDWARE(intel
);
370 static void brw_invalidate_vbo_cb( struct intel_context
*intel
, void *ptr
)
372 /* nothing to do, we don't rely on the contents being preserved */
376 void brw_draw_init( struct brw_context
*brw
)
378 GLcontext
*ctx
= &brw
->intel
.ctx
;
381 brw
->vb
.upload
.size
= BRW_UPLOAD_INIT_SIZE
;
383 for (i
= 0; i
< BRW_NR_UPLOAD_BUFS
; i
++) {
384 brw
->vb
.upload
.vbo
[i
] = ctx
->Driver
.NewBufferObject(ctx
, 1, GL_ARRAY_BUFFER_ARB
);
386 /* XXX: Set these to no-backing-store
388 bmBufferSetInvalidateCB(&brw
->intel
,
389 intel_bufferobj_buffer(intel_buffer_object(brw
->vb
.upload
.vbo
[i
])),
390 brw_invalidate_vbo_cb
,
396 ctx
->Driver
.BufferData( ctx
,
398 BRW_UPLOAD_INIT_SIZE
,
401 brw
->vb
.upload
.vbo
[0] );
404 brw_init_current_values(ctx
, brw
->vb
.current_values
);
407 void brw_draw_destroy( struct brw_context
*brw
)
409 GLcontext
*ctx
= &brw
->intel
.ctx
;
412 for (i
= 0; i
< BRW_NR_UPLOAD_BUFS
; i
++)
413 ctx
->Driver
.DeleteBuffer(ctx
, brw
->vb
.upload
.vbo
[i
]);