Add Intel i965G/Q DRI driver.
[mesa.git] / src / mesa / drivers / dri / i965 / brw_draw.c
1 /**************************************************************************
2 *
3 * Copyright 2003 Tungsten Graphics, Inc., Cedar Park, Texas.
4 * All Rights Reserved.
5 *
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:
13 *
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
16 * of the Software.
17 *
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.
25 *
26 **************************************************************************/
27
28 #include <stdlib.h>
29
30 #include "glheader.h"
31 #include "context.h"
32 #include "state.h"
33 #include "api_validate.h"
34 #include "enums.h"
35
36 #include "brw_draw.h"
37 #include "brw_defines.h"
38 #include "brw_attrib.h"
39 #include "brw_context.h"
40 #include "brw_aub.h"
41 #include "brw_state.h"
42 #include "brw_fallback.h"
43
44 #include "intel_ioctl.h"
45 #include "intel_batchbuffer.h"
46 #include "intel_buffer_objects.h"
47
48
49
50
51
52
53 static GLuint hw_prim[GL_POLYGON+1] = {
54 _3DPRIM_POINTLIST,
55 _3DPRIM_LINELIST,
56 _3DPRIM_LINELOOP,
57 _3DPRIM_LINESTRIP,
58 _3DPRIM_TRILIST,
59 _3DPRIM_TRISTRIP,
60 _3DPRIM_TRIFAN,
61 _3DPRIM_QUADLIST,
62 _3DPRIM_QUADSTRIP,
63 _3DPRIM_POLYGON
64 };
65
66
67 static const GLenum reduced_prim[GL_POLYGON+1] = {
68 GL_POINTS,
69 GL_LINES,
70 GL_LINES,
71 GL_LINES,
72 GL_TRIANGLES,
73 GL_TRIANGLES,
74 GL_TRIANGLES,
75 GL_TRIANGLES,
76 GL_TRIANGLES,
77 GL_TRIANGLES
78 };
79
80
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.
85 */
86 static GLuint brw_set_prim(struct brw_context *brw, GLenum prim)
87 {
88 if (INTEL_DEBUG & DEBUG_PRIMS)
89 _mesa_printf("PRIM: %s\n", _mesa_lookup_enum_by_nr(prim));
90
91 /* Slight optimization to avoid the GS program when not needed:
92 */
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;
98
99 if (prim != brw->primitive) {
100 brw->primitive = prim;
101 brw->state.dirty.brw |= BRW_NEW_PRIMITIVE;
102
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;
106 }
107
108 brw_validate_state(brw);
109 }
110
111 return hw_prim[prim];
112 }
113
114
115 static GLuint trim(GLenum prim, GLuint length)
116 {
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;
121 else
122 return length;
123 }
124
125
126
127
128 static void brw_emit_prim( struct brw_context *brw,
129 const struct brw_draw_prim *prim )
130
131 {
132 struct brw_3d_primitive prim_packet;
133
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);
137
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;
143
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;
149
150 if (prim_packet.verts_per_instance) {
151 intel_batchbuffer_data( brw->intel.batch, &prim_packet, sizeof(prim_packet),
152 INTEL_BATCH_CLIPRECTS);
153 }
154 }
155
156
157
158 static void update_current_size( struct gl_client_array *array)
159 {
160 const GLfloat *ptr = (const GLfloat *)array->Ptr;
161
162 assert(array->StrideB == 0);
163 assert(array->Type == GL_FLOAT);
164
165 if (ptr[3] != 1.0)
166 array->Size = 4;
167 else if (ptr[2] != 0.0)
168 array->Size = 3;
169 else if (ptr[1] != 0.0)
170 array->Size = 2;
171 else
172 array->Size = 1;
173 }
174
175
176
177 /* Fill in any gaps in passed arrays with pointers to current
178 * attributes:
179 */
180 static void brw_merge_inputs( struct brw_context *brw,
181 const struct gl_client_array *arrays[])
182 {
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;
186 GLuint i;
187
188 memset(inputs, 0, sizeof(*inputs));
189 memset(&brw->vb.info, 0, sizeof(brw->vb.info));
190
191 for (i = 0; i < BRW_ATTRIB_MAX; i++) {
192 if (arrays[i] && arrays[i]->Enabled)
193 {
194 brw->vb.inputs[i].glarray = arrays[i];
195 brw->vb.info.varying[i/32] |= 1 << (i%32);
196 }
197 else
198 {
199 brw->vb.inputs[i].glarray = &current_values[i];
200
201 /* XXX: This will change on the Mesa trunk as Brian has moved
202 * edgeflag, index into this range:
203 */
204 if (i < VERT_ATTRIB_GENERIC0)
205 update_current_size(&current_values[i]);
206 }
207
208 brw->vb.info.sizes[i/16] |= (inputs[i].glarray->Size - 1) << ((i%16) * 2);
209 }
210
211 /* Raise statechanges if input sizes and varying have changed:
212 */
213 if (memcmp(brw->vb.info.sizes, old.sizes, sizeof(old.sizes)) != 0)
214 brw->state.dirty.brw |= BRW_NEW_INPUT_DIMENSIONS;
215
216 if (memcmp(brw->vb.info.varying, old.varying, sizeof(old.varying)) != 0)
217 brw->state.dirty.brw |= BRW_NEW_INPUT_VARYING;
218 }
219
220 static GLboolean check_fallbacks( struct brw_context *brw,
221 const struct brw_draw_prim *prim,
222 GLuint nr_prims )
223 {
224 GLuint i;
225
226 if (!brw->intel.strict_conformance)
227 return GL_FALSE;
228
229 if (brw->attribs.Polygon->SmoothFlag) {
230 for (i = 0; i < nr_prims; i++)
231 if (reduced_prim[prim[i].mode] == GL_TRIANGLES)
232 return GL_TRUE;
233 }
234
235 /* BRW hardware will do AA lines, but they are non-conformant it
236 * seems. TBD whether we keep this fallback:
237 */
238 if (brw->attribs.Line->SmoothFlag) {
239 for (i = 0; i < nr_prims; i++)
240 if (reduced_prim[prim[i].mode] == GL_LINES)
241 return GL_TRUE;
242 }
243
244 /* Stipple -- these fallbacks could be resolved with a little
245 * bit of work?
246 */
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?!?
251 */
252 if (prim[i].mode == GL_LINE_LOOP)
253 return GL_TRUE;
254
255 if (prim[i].mode == GL_POLYGON &&
256 (brw->attribs.Polygon->FrontMode == GL_LINE ||
257 brw->attribs.Polygon->BackMode == GL_LINE))
258 return GL_TRUE;
259 }
260 }
261
262
263 if (brw->attribs.Point->SmoothFlag) {
264 for (i = 0; i < nr_prims; i++)
265 if (prim[i].mode == GL_POINTS)
266 return GL_TRUE;
267 }
268
269 return GL_FALSE;
270 }
271
272
273 GLboolean brw_draw_prims( GLcontext *ctx,
274 const struct gl_client_array *arrays[],
275 const struct brw_draw_prim *prim,
276 GLuint nr_prims,
277 const struct brw_draw_index_buffer *ib,
278 GLuint min_index,
279 GLuint max_index,
280 GLuint flags )
281 {
282 struct intel_context *intel = intel_context(ctx);
283 struct brw_context *brw = brw_context(ctx);
284 GLboolean retval = GL_FALSE;
285 GLuint i;
286
287 if (ctx->NewState)
288 _mesa_update_state( ctx );
289
290 /* Bind all inputs, derive varying and size information:
291 */
292 brw_merge_inputs( brw, arrays );
293
294 /* Have to validate state quite late. Will rebuild tnl_program,
295 * which depends on varying information.
296 *
297 * Note this is where brw->vs->prog_data.inputs_read is calculated,
298 * so can't access it earlier.
299 */
300
301 LOCK_HARDWARE(intel);
302 {
303 assert(intel->locked);
304
305 /* Set the first primitive early, ahead of validate_state:
306 */
307 brw_set_prim(brw, prim[0].mode);
308
309 /* XXX: Need to separate validate and upload of state.
310 */
311 brw_validate_state( brw );
312
313 /* Various fallback checks:
314 */
315 if (brw->intel.Fallback)
316 goto out;
317
318 if (check_fallbacks( brw, prim, nr_prims ))
319 goto out;
320
321 /* Upload index, vertex data:
322 */
323 if (ib)
324 brw_upload_indices( brw, ib );
325
326 if (!brw_upload_vertices( brw, min_index, max_index)) {
327 goto out;
328 }
329
330 /* Emit prims to batchbuffer:
331 */
332 for (i = 0; i < nr_prims; i++) {
333 brw_emit_prim(brw, &prim[i]);
334 }
335
336 retval = GL_TRUE;
337 }
338
339 out:
340
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
344 * up.
345 */
346 intel_batchbuffer_flush( brw->intel.batch );
347
348 if (intel->thrashing) {
349 bmSetFence(intel);
350 }
351
352 /* Free any old data so it doesn't clog up texture memory - we
353 * won't be referencing it again.
354 */
355 while (brw->vb.upload.wrap != brw->vb.upload.buf) {
356 ctx->Driver.BufferData(ctx,
357 GL_ARRAY_BUFFER_ARB,
358 BRW_UPLOAD_INIT_SIZE,
359 NULL,
360 GL_DYNAMIC_DRAW_ARB,
361 brw->vb.upload.vbo[brw->vb.upload.wrap]);
362 brw->vb.upload.wrap++;
363 brw->vb.upload.wrap %= BRW_NR_UPLOAD_BUFS;
364 }
365
366 UNLOCK_HARDWARE(intel);
367 return retval;
368 }
369
370 static void brw_invalidate_vbo_cb( struct intel_context *intel, void *ptr )
371 {
372 /* nothing to do, we don't rely on the contents being preserved */
373 }
374
375
376 void brw_draw_init( struct brw_context *brw )
377 {
378 GLcontext *ctx = &brw->intel.ctx;
379 GLuint i;
380
381 brw->vb.upload.size = BRW_UPLOAD_INIT_SIZE;
382
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);
385
386 /* XXX: Set these to no-backing-store
387 */
388 bmBufferSetInvalidateCB(&brw->intel,
389 intel_bufferobj_buffer(intel_buffer_object(brw->vb.upload.vbo[i])),
390 brw_invalidate_vbo_cb,
391 &brw->intel,
392 GL_TRUE);
393
394 }
395
396 ctx->Driver.BufferData( ctx,
397 GL_ARRAY_BUFFER_ARB,
398 BRW_UPLOAD_INIT_SIZE,
399 NULL,
400 GL_DYNAMIC_DRAW_ARB,
401 brw->vb.upload.vbo[0] );
402
403
404 brw_init_current_values(ctx, brw->vb.current_values);
405 }
406
407 void brw_draw_destroy( struct brw_context *brw )
408 {
409 GLcontext *ctx = &brw->intel.ctx;
410 GLuint i;
411
412 for (i = 0; i < BRW_NR_UPLOAD_BUFS; i++)
413 ctx->Driver.DeleteBuffer(ctx, brw->vb.upload.vbo[i]);
414 }