WIP comit
[mesa.git] / src / mesa / drivers / dri / r300 / r300_swtcl.c
1 /**************************************************************************
2
3 Copyright (C) 2007 Dave Airlie
4
5 All Rights Reserved.
6
7 Permission is hereby granted, free of charge, to any person obtaining a
8 copy of this software and associated documentation files (the "Software"),
9 to deal in the Software without restriction, including without limitation
10 on the rights to use, copy, modify, merge, publish, distribute, sub
11 license, and/or sell copies of the Software, and to permit persons to whom
12 the Software is furnished to do so, subject to the following conditions:
13
14 The above copyright notice and this permission notice (including the next
15 paragraph) shall be included in all copies or substantial portions of the
16 Software.
17
18 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
21 THE COPYRIGHT OWNER(S) AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
22 DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
23 OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
24 USE OR OTHER DEALINGS IN THE SOFTWARE.
25
26 **************************************************************************/
27
28 /*
29 * Authors:
30 * Dave Airlie <airlied@linux.ie>
31 */
32
33 /* derived from r200 swtcl path */
34
35
36
37 #include "main/glheader.h"
38 #include "main/mtypes.h"
39 #include "main/colormac.h"
40 #include "main/enums.h"
41 #include "main/image.h"
42 #include "main/imports.h"
43 #include "main/light.h"
44 #include "main/macros.h"
45
46 #include "swrast/s_context.h"
47 #include "swrast/s_fog.h"
48 #include "swrast_setup/swrast_setup.h"
49 #include "math/m_translate.h"
50 #include "tnl/tnl.h"
51 #include "tnl/t_context.h"
52 #include "tnl/t_pipeline.h"
53
54 #include "r300_context.h"
55 #include "r300_swtcl.h"
56 #include "r300_state.h"
57 #include "r300_ioctl.h"
58 #include "r300_emit.h"
59
60 static void flush_last_swtcl_prim( GLcontext *ctx);
61
62
63 void r300EmitVertexAOS(r300ContextPtr rmesa, GLuint vertex_size, struct radeon_bo *bo, GLuint offset);
64 void r300EmitVbufPrim(r300ContextPtr rmesa, GLuint primitive, GLuint vertex_nr);
65 #define EMIT_ATTR( ATTR, STYLE ) \
66 do { \
67 rmesa->swtcl.vertex_attrs[rmesa->swtcl.vertex_attr_count].attrib = (ATTR); \
68 rmesa->swtcl.vertex_attrs[rmesa->swtcl.vertex_attr_count].format = (STYLE); \
69 rmesa->swtcl.vertex_attr_count++; \
70 } while (0)
71
72 #define EMIT_PAD( N ) \
73 do { \
74 rmesa->swtcl.vertex_attrs[rmesa->swtcl.vertex_attr_count].attrib = 0; \
75 rmesa->swtcl.vertex_attrs[rmesa->swtcl.vertex_attr_count].format = EMIT_PAD; \
76 rmesa->swtcl.vertex_attrs[rmesa->swtcl.vertex_attr_count].offset = (N); \
77 rmesa->swtcl.vertex_attr_count++; \
78 } while (0)
79
80 static void r300SetVertexFormat( GLcontext *ctx )
81 {
82 r300ContextPtr rmesa = R300_CONTEXT( ctx );
83 TNLcontext *tnl = TNL_CONTEXT(ctx);
84 struct vertex_buffer *VB = &tnl->vb;
85 DECLARE_RENDERINPUTS(index_bitset);
86 GLuint InputsRead = 0, OutputsWritten = 0;
87 int vap_fmt_0 = 0;
88 int offset = 0;
89 int vte = 0;
90 GLint inputs[VERT_ATTRIB_MAX];
91 GLint tab[VERT_ATTRIB_MAX];
92 int swizzle[VERT_ATTRIB_MAX][4];
93 GLuint i, nr;
94 GLuint sz, vap_fmt_1 = 0;
95
96 DECLARE_RENDERINPUTS(render_inputs_bitset);
97 RENDERINPUTS_COPY(render_inputs_bitset, tnl->render_inputs_bitset);
98 RENDERINPUTS_COPY( index_bitset, tnl->render_inputs_bitset );
99 RENDERINPUTS_COPY(rmesa->state.render_inputs_bitset, render_inputs_bitset);
100
101 vte = rmesa->hw.vte.cmd[1];
102 vte &= ~(R300_VTX_XY_FMT | R300_VTX_Z_FMT | R300_VTX_W0_FMT);
103 /* Important:
104 */
105 if ( VB->NdcPtr != NULL ) {
106 VB->AttribPtr[VERT_ATTRIB_POS] = VB->NdcPtr;
107 vte |= R300_VTX_XY_FMT | R300_VTX_Z_FMT;
108 }
109 else {
110 VB->AttribPtr[VERT_ATTRIB_POS] = VB->ClipPtr;
111 vte |= R300_VTX_W0_FMT;
112 }
113
114 assert( VB->AttribPtr[VERT_ATTRIB_POS] != NULL );
115 rmesa->swtcl.vertex_attr_count = 0;
116
117 /* EMIT_ATTR's must be in order as they tell t_vertex.c how to
118 * build up a hardware vertex.
119 */
120 if (RENDERINPUTS_TEST( index_bitset, _TNL_ATTRIB_POS)) {
121 sz = VB->AttribPtr[VERT_ATTRIB_POS]->size;
122 InputsRead |= 1 << VERT_ATTRIB_POS;
123 OutputsWritten |= 1 << VERT_RESULT_HPOS;
124 EMIT_ATTR( _TNL_ATTRIB_POS, EMIT_1F + sz - 1 );
125 offset = sz;
126 } else {
127 offset = 4;
128 EMIT_PAD(4 * sizeof(float));
129 }
130
131 if (RENDERINPUTS_TEST( index_bitset, _TNL_ATTRIB_POINTSIZE )) {
132 EMIT_ATTR( _TNL_ATTRIB_POINTSIZE, EMIT_1F );
133 vap_fmt_0 |= R300_VAP_OUTPUT_VTX_FMT_0__PT_SIZE_PRESENT;
134 offset += 1;
135 }
136
137 if (RENDERINPUTS_TEST(index_bitset, _TNL_ATTRIB_COLOR0)) {
138 sz = VB->AttribPtr[VERT_ATTRIB_COLOR0]->size;
139 rmesa->swtcl.coloroffset = offset;
140 InputsRead |= 1 << VERT_ATTRIB_COLOR0;
141 OutputsWritten |= 1 << VERT_RESULT_COL0;
142 EMIT_ATTR( _TNL_ATTRIB_COLOR0, EMIT_1F + sz - 1 );
143 offset += sz;
144 }
145
146 rmesa->swtcl.specoffset = 0;
147 if (RENDERINPUTS_TEST( index_bitset, _TNL_ATTRIB_COLOR1 )) {
148 sz = VB->AttribPtr[VERT_ATTRIB_COLOR1]->size;
149 rmesa->swtcl.specoffset = offset;
150 EMIT_ATTR( _TNL_ATTRIB_COLOR1, EMIT_1F + sz - 1 );
151 InputsRead |= 1 << VERT_ATTRIB_COLOR1;
152 OutputsWritten |= 1 << VERT_RESULT_COL1;
153 }
154
155 if (RENDERINPUTS_TEST_RANGE( index_bitset, _TNL_FIRST_TEX, _TNL_LAST_TEX )) {
156 int i;
157
158 for (i = 0; i < ctx->Const.MaxTextureUnits; i++) {
159 if (RENDERINPUTS_TEST( index_bitset, _TNL_ATTRIB_TEX(i) )) {
160 sz = VB->TexCoordPtr[i]->size;
161 InputsRead |= 1 << (VERT_ATTRIB_TEX0 + i);
162 OutputsWritten |= 1 << (VERT_RESULT_TEX0 + i);
163 EMIT_ATTR( _TNL_ATTRIB_TEX0+i, EMIT_1F + sz - 1 );
164 vap_fmt_1 |= sz << (3 * i);
165 }
166 }
167 }
168
169 for (i = 0, nr = 0; i < VERT_ATTRIB_MAX; i++) {
170 if (InputsRead & (1 << i)) {
171 inputs[i] = nr++;
172 } else {
173 inputs[i] = -1;
174 }
175 }
176
177 /* Fixed, apply to vir0 only */
178 if (InputsRead & (1 << VERT_ATTRIB_POS))
179 inputs[VERT_ATTRIB_POS] = 0;
180 if (InputsRead & (1 << VERT_ATTRIB_COLOR0))
181 inputs[VERT_ATTRIB_COLOR0] = 2;
182 if (InputsRead & (1 << VERT_ATTRIB_COLOR1))
183 inputs[VERT_ATTRIB_COLOR1] = 3;
184 for (i = VERT_ATTRIB_TEX0; i <= VERT_ATTRIB_TEX7; i++)
185 if (InputsRead & (1 << i))
186 inputs[i] = 6 + (i - VERT_ATTRIB_TEX0);
187
188 for (i = 0, nr = 0; i < VERT_ATTRIB_MAX; i++) {
189 if (InputsRead & (1 << i)) {
190 tab[nr++] = i;
191 }
192 }
193
194 for (i = 0; i < nr; i++) {
195 int ci;
196
197 swizzle[i][0] = SWIZZLE_ZERO;
198 swizzle[i][1] = SWIZZLE_ZERO;
199 swizzle[i][2] = SWIZZLE_ZERO;
200 swizzle[i][3] = SWIZZLE_ONE;
201
202 for (ci = 0; ci < VB->AttribPtr[tab[i]]->size; ci++) {
203 swizzle[i][ci] = ci;
204 }
205 }
206
207 R300_NEWPRIM(rmesa);
208 R300_STATECHANGE(rmesa, vir[0]);
209 ((drm_r300_cmd_header_t *) rmesa->hw.vir[0].cmd)->packet0.count =
210 r300VAPInputRoute0(&rmesa->hw.vir[0].cmd[R300_VIR_CNTL_0],
211 VB->AttribPtr, inputs, tab, nr);
212 R300_STATECHANGE(rmesa, vir[1]);
213 ((drm_r300_cmd_header_t *) rmesa->hw.vir[1].cmd)->packet0.count =
214 r300VAPInputRoute1(&rmesa->hw.vir[1].cmd[R300_VIR_CNTL_0], swizzle,
215 nr);
216
217 R300_STATECHANGE(rmesa, vic);
218 rmesa->hw.vic.cmd[R300_VIC_CNTL_0] = r300VAPInputCntl0(ctx, InputsRead);
219 rmesa->hw.vic.cmd[R300_VIC_CNTL_1] = r300VAPInputCntl1(ctx, InputsRead);
220
221 R300_STATECHANGE(rmesa, vof);
222 rmesa->hw.vof.cmd[R300_VOF_CNTL_0] = r300VAPOutputCntl0(ctx, OutputsWritten);
223 rmesa->hw.vof.cmd[R300_VOF_CNTL_1] = vap_fmt_1;
224
225 rmesa->swtcl.vertex_size =
226 _tnl_install_attrs( ctx,
227 rmesa->swtcl.vertex_attrs,
228 rmesa->swtcl.vertex_attr_count,
229 NULL, 0 );
230
231 rmesa->swtcl.vertex_size /= 4;
232
233 RENDERINPUTS_COPY( rmesa->tnl_index_bitset, index_bitset );
234
235
236 R300_STATECHANGE(rmesa, vte);
237 rmesa->hw.vte.cmd[1] = vte;
238 rmesa->hw.vte.cmd[2] = rmesa->swtcl.vertex_size;
239 }
240
241
242 /* Flush vertices in the current dma region.
243 */
244 static void flush_last_swtcl_prim( GLcontext *ctx )
245 {
246 r300ContextPtr rmesa = R300_CONTEXT(ctx);
247 struct radeon_dma *dma = &rmesa->radeon.dma;
248
249
250 if (RADEON_DEBUG & DEBUG_IOCTL)
251 fprintf(stderr, "%s\n", __FUNCTION__);
252 dma->flush = NULL;
253
254 if (dma->current) {
255 GLuint current_offset = dma->current_used;
256
257 assert (dma->current_used +
258 rmesa->swtcl.numverts * rmesa->swtcl.vertex_size * 4 ==
259 dma->current_vertexptr);
260
261 radeon_bo_unmap(dma->current);
262 if (dma->current_used != dma->current_vertexptr) {
263 dma->current_used = dma->current_vertexptr;
264
265 rcommonEnsureCmdBufSpace(rmesa,
266 rmesa->hw.max_state_size + (12*sizeof(int)),
267 __FUNCTION__);
268 r300EmitState(rmesa);
269 r300EmitVertexAOS(rmesa,
270 rmesa->swtcl.vertex_size,
271 dma->current,
272 current_offset);
273
274 r300EmitVbufPrim(rmesa,
275 rmesa->swtcl.hw_primitive,
276 rmesa->swtcl.numverts);
277 r300EmitCacheFlush(rmesa);
278 COMMIT_BATCH();
279 }
280 radeonReleaseDmaRegion(&rmesa->radeon);
281 rmesa->swtcl.numverts = 0;
282 }
283 }
284
285 /* Alloc space in the current dma region.
286 */
287 static void *
288 r300AllocDmaLowVerts( r300ContextPtr rmesa, int nverts, int vsize )
289 {
290 GLuint bytes = vsize * nverts;
291 void *head;
292
293 if (!rmesa->radeon.dma.current || rmesa->radeon.dma.current_vertexptr + bytes > rmesa->radeon.dma.current->size) {
294 radeonRefillCurrentDmaRegion( &rmesa->radeon, bytes);
295 }
296
297 if (!rmesa->radeon.dma.flush) {
298 rmesa->radeon.glCtx->Driver.NeedFlush |= FLUSH_STORED_VERTICES;
299 rmesa->radeon.dma.flush = flush_last_swtcl_prim;
300 }
301
302 ASSERT( vsize == rmesa->swtcl.vertex_size * 4 );
303 ASSERT( rmesa->radeon.dma.flush == flush_last_swtcl_prim );
304 ASSERT( rmesa->radeon.dma.current_used +
305 rmesa->swtcl.numverts * rmesa->swtcl.vertex_size * 4 ==
306 rmesa->radeon.dma.current_vertexptr );
307
308 // fprintf(stderr,"current %p %x\n", rmesa->radeon.dma.current->ptr,
309 // rmesa->radeon.dma.current_vertexptr);
310 head = (rmesa->radeon.dma.current->ptr + rmesa->radeon.dma.current_vertexptr);
311 rmesa->radeon.dma.current_vertexptr += bytes;
312 rmesa->swtcl.numverts += nverts;
313 return head;
314 }
315
316 static GLuint reduced_prim[] = {
317 GL_POINTS,
318 GL_LINES,
319 GL_LINES,
320 GL_LINES,
321 GL_TRIANGLES,
322 GL_TRIANGLES,
323 GL_TRIANGLES,
324 GL_TRIANGLES,
325 GL_TRIANGLES,
326 GL_TRIANGLES,
327 };
328
329 static void r300RasterPrimitive( GLcontext *ctx, GLuint prim );
330 static void r300RenderPrimitive( GLcontext *ctx, GLenum prim );
331 //static void r300ResetLineStipple( GLcontext *ctx );
332
333 /***********************************************************************
334 * Emit primitives as inline vertices *
335 ***********************************************************************/
336
337
338 #define HAVE_POINTS 1
339 #define HAVE_LINES 1
340 #define HAVE_LINE_STRIPS 1
341 #define HAVE_TRIANGLES 1
342 #define HAVE_TRI_STRIPS 1
343 #define HAVE_TRI_STRIP_1 0
344 #define HAVE_TRI_FANS 1
345 #define HAVE_QUADS 0
346 #define HAVE_QUAD_STRIPS 0
347 #define HAVE_POLYGONS 1
348 #define HAVE_ELTS 1
349
350 #undef LOCAL_VARS
351 #undef ALLOC_VERTS
352 #define CTX_ARG r300ContextPtr rmesa
353 #define GET_VERTEX_DWORDS() rmesa->swtcl.vertex_size
354 #define ALLOC_VERTS( n, size ) r300AllocDmaLowVerts( rmesa, n, size * 4 )
355 #define LOCAL_VARS \
356 r300ContextPtr rmesa = R300_CONTEXT(ctx); \
357 const char *r300verts = (char *)rmesa->swtcl.verts;
358 #define VERT(x) (r300Vertex *)(r300verts + ((x) * vertsize * sizeof(int)))
359 #define VERTEX r300Vertex
360 #define DO_DEBUG_VERTS (1 && (RADEON_DEBUG & DEBUG_VERTS))
361 #define PRINT_VERTEX(x)
362 #undef TAG
363 #define TAG(x) r300_##x
364 #include "tnl_dd/t_dd_triemit.h"
365
366
367
368 /***********************************************************************
369 * Macros for t_dd_tritmp.h to draw basic primitives *
370 ***********************************************************************/
371
372 #define QUAD( a, b, c, d ) r300_quad( rmesa, a, b, c, d )
373 #define TRI( a, b, c ) r300_triangle( rmesa, a, b, c )
374 #define LINE( a, b ) r300_line( rmesa, a, b )
375 #define POINT( a ) r300_point( rmesa, a )
376
377 /***********************************************************************
378 * Build render functions from dd templates *
379 ***********************************************************************/
380
381 #define R300_TWOSIDE_BIT 0x01
382 #define R300_UNFILLED_BIT 0x02
383 #define R300_MAX_TRIFUNC 0x04
384
385 static struct {
386 tnl_points_func points;
387 tnl_line_func line;
388 tnl_triangle_func triangle;
389 tnl_quad_func quad;
390 } rast_tab[R300_MAX_TRIFUNC];
391
392 #define DO_FALLBACK 0
393 #define DO_UNFILLED (IND & R300_UNFILLED_BIT)
394 #define DO_TWOSIDE (IND & R300_TWOSIDE_BIT)
395 #define DO_FLAT 0
396 #define DO_OFFSET 0
397 #define DO_TRI 1
398 #define DO_QUAD 1
399 #define DO_LINE 1
400 #define DO_POINTS 1
401 #define DO_FULL_QUAD 1
402
403 #define HAVE_RGBA 1
404 #define HAVE_SPEC 1
405 #define HAVE_BACK_COLORS 0
406 #define HAVE_HW_FLATSHADE 1
407 #define TAB rast_tab
408
409 #define DEPTH_SCALE 1.0
410 #define UNFILLED_TRI unfilled_tri
411 #define UNFILLED_QUAD unfilled_quad
412 #define VERT_X(_v) _v->v.x
413 #define VERT_Y(_v) _v->v.y
414 #define VERT_Z(_v) _v->v.z
415 #define AREA_IS_CCW( a ) (a < 0)
416 #define GET_VERTEX(e) (rmesa->swtcl.verts + (e*rmesa->swtcl.vertex_size*sizeof(int)))
417
418 /* Only used to pull back colors into vertices (ie, we know color is
419 * floating point).
420 */
421 #define R300_COLOR( dst, src ) \
422 do { \
423 UNCLAMPED_FLOAT_TO_UBYTE((dst)[0], (src)[2]); \
424 UNCLAMPED_FLOAT_TO_UBYTE((dst)[1], (src)[1]); \
425 UNCLAMPED_FLOAT_TO_UBYTE((dst)[2], (src)[0]); \
426 UNCLAMPED_FLOAT_TO_UBYTE((dst)[3], (src)[3]); \
427 } while (0)
428
429 #define VERT_SET_RGBA( v, c ) if (coloroffset) R300_COLOR( v->ub4[coloroffset], c )
430 #define VERT_COPY_RGBA( v0, v1 ) if (coloroffset) v0->ui[coloroffset] = v1->ui[coloroffset]
431 #define VERT_SAVE_RGBA( idx ) if (coloroffset) color[idx] = v[idx]->ui[coloroffset]
432 #define VERT_RESTORE_RGBA( idx ) if (coloroffset) v[idx]->ui[coloroffset] = color[idx]
433
434 #define R300_SPEC( dst, src ) \
435 do { \
436 UNCLAMPED_FLOAT_TO_UBYTE((dst)[0], (src)[2]); \
437 UNCLAMPED_FLOAT_TO_UBYTE((dst)[1], (src)[1]); \
438 UNCLAMPED_FLOAT_TO_UBYTE((dst)[2], (src)[0]); \
439 } while (0)
440
441 #define VERT_SET_SPEC( v, c ) if (specoffset) R300_SPEC( v->ub4[specoffset], c )
442 #define VERT_COPY_SPEC( v0, v1 ) if (specoffset) COPY_3V(v0->ub4[specoffset], v1->ub4[specoffset])
443 #define VERT_SAVE_SPEC( idx ) if (specoffset) spec[idx] = v[idx]->ui[specoffset]
444 #define VERT_RESTORE_SPEC( idx ) if (specoffset) v[idx]->ui[specoffset] = spec[idx]
445
446 #undef LOCAL_VARS
447 #undef TAG
448 #undef INIT
449
450 #define LOCAL_VARS(n) \
451 r300ContextPtr rmesa = R300_CONTEXT(ctx); \
452 GLuint color[n], spec[n]; \
453 GLuint coloroffset = rmesa->swtcl.coloroffset; \
454 GLuint specoffset = rmesa->swtcl.specoffset; \
455 (void) color; (void) spec; (void) coloroffset; (void) specoffset;
456
457 /***********************************************************************
458 * Helpers for rendering unfilled primitives *
459 ***********************************************************************/
460
461 #define RASTERIZE(x) r300RasterPrimitive( ctx, reduced_prim[x] )
462 #define RENDER_PRIMITIVE rmesa->swtcl.render_primitive
463 #undef TAG
464 #define TAG(x) x
465 #include "tnl_dd/t_dd_unfilled.h"
466 #undef IND
467
468
469 /***********************************************************************
470 * Generate GL render functions *
471 ***********************************************************************/
472
473
474 #define IND (0)
475 #define TAG(x) x
476 #include "tnl_dd/t_dd_tritmp.h"
477
478 #define IND (R300_TWOSIDE_BIT)
479 #define TAG(x) x##_twoside
480 #include "tnl_dd/t_dd_tritmp.h"
481
482 #define IND (R300_UNFILLED_BIT)
483 #define TAG(x) x##_unfilled
484 #include "tnl_dd/t_dd_tritmp.h"
485
486 #define IND (R300_TWOSIDE_BIT|R300_UNFILLED_BIT)
487 #define TAG(x) x##_twoside_unfilled
488 #include "tnl_dd/t_dd_tritmp.h"
489
490
491
492 static void init_rast_tab( void )
493 {
494 init();
495 init_twoside();
496 init_unfilled();
497 init_twoside_unfilled();
498 }
499
500 /**********************************************************************/
501 /* Render unclipped begin/end objects */
502 /**********************************************************************/
503
504 #define RENDER_POINTS( start, count ) \
505 for ( ; start < count ; start++) \
506 r300_point( rmesa, VERT(start) )
507 #define RENDER_LINE( v0, v1 ) \
508 r300_line( rmesa, VERT(v0), VERT(v1) )
509 #define RENDER_TRI( v0, v1, v2 ) \
510 r300_triangle( rmesa, VERT(v0), VERT(v1), VERT(v2) )
511 #define RENDER_QUAD( v0, v1, v2, v3 ) \
512 r300_quad( rmesa, VERT(v0), VERT(v1), VERT(v2), VERT(v3) )
513 #define INIT(x) do { \
514 r300RenderPrimitive( ctx, x ); \
515 } while (0)
516 #undef LOCAL_VARS
517 #define LOCAL_VARS \
518 r300ContextPtr rmesa = R300_CONTEXT(ctx); \
519 const GLuint vertsize = rmesa->swtcl.vertex_size; \
520 const char *r300verts = (char *)rmesa->swtcl.verts; \
521 const GLuint * const elt = TNL_CONTEXT(ctx)->vb.Elts; \
522 const GLboolean stipple = ctx->Line.StippleFlag; \
523 (void) elt; (void) stipple;
524 #define RESET_STIPPLE //if ( stipple ) r200ResetLineStipple( ctx );
525 #define RESET_OCCLUSION
526 #define PRESERVE_VB_DEFS
527 #define ELT(x) (x)
528 #define TAG(x) r300_##x##_verts
529 #include "tnl/t_vb_rendertmp.h"
530 #undef ELT
531 #undef TAG
532 #define TAG(x) r300_##x##_elts
533 #define ELT(x) elt[x]
534 #include "tnl/t_vb_rendertmp.h"
535
536
537
538
539 /**********************************************************************/
540 /* Choose render functions */
541 /**********************************************************************/
542 static void r300ChooseRenderState( GLcontext *ctx )
543 {
544 TNLcontext *tnl = TNL_CONTEXT(ctx);
545 r300ContextPtr rmesa = R300_CONTEXT(ctx);
546 GLuint index = 0;
547 GLuint flags = ctx->_TriangleCaps;
548
549 if (flags & DD_TRI_LIGHT_TWOSIDE) index |= R300_TWOSIDE_BIT;
550 if (flags & DD_TRI_UNFILLED) index |= R300_UNFILLED_BIT;
551
552 if (index != rmesa->swtcl.RenderIndex) {
553 tnl->Driver.Render.Points = rast_tab[index].points;
554 tnl->Driver.Render.Line = rast_tab[index].line;
555 tnl->Driver.Render.ClippedLine = rast_tab[index].line;
556 tnl->Driver.Render.Triangle = rast_tab[index].triangle;
557 tnl->Driver.Render.Quad = rast_tab[index].quad;
558
559 if (index == 0) {
560 tnl->Driver.Render.PrimTabVerts = r300_render_tab_verts;
561 tnl->Driver.Render.PrimTabElts = r300_render_tab_elts;
562 tnl->Driver.Render.ClippedPolygon = r300_fast_clipped_poly;
563 } else {
564 tnl->Driver.Render.PrimTabVerts = _tnl_render_tab_verts;
565 tnl->Driver.Render.PrimTabElts = _tnl_render_tab_elts;
566 tnl->Driver.Render.ClippedPolygon = _tnl_RenderClippedPolygon;
567 }
568
569 rmesa->swtcl.RenderIndex = index;
570 }
571 }
572
573
574 static void r300RenderStart(GLcontext *ctx)
575 {
576 r300ContextPtr rmesa = R300_CONTEXT( ctx );
577 // fprintf(stderr, "%s\n", __FUNCTION__);
578
579 r300ChooseRenderState(ctx);
580 r300SetVertexFormat(ctx);
581
582 r300UpdateShaders(rmesa);
583 r300UpdateShaderStates(rmesa);
584
585 r300EmitCacheFlush(rmesa);
586 if (rmesa->radeon.dma.flush != NULL) {
587 rmesa->radeon.dma.flush(ctx);
588 }
589 }
590
591 static void r300RenderFinish(GLcontext *ctx)
592 {
593 }
594
595 static void r300RasterPrimitive( GLcontext *ctx, GLuint hwprim )
596 {
597 r300ContextPtr rmesa = R300_CONTEXT(ctx);
598
599 if (rmesa->swtcl.hw_primitive != hwprim) {
600 R300_NEWPRIM( rmesa );
601 rmesa->swtcl.hw_primitive = hwprim;
602 }
603 }
604
605 static void r300RenderPrimitive(GLcontext *ctx, GLenum prim)
606 {
607
608 r300ContextPtr rmesa = R300_CONTEXT(ctx);
609 rmesa->swtcl.render_primitive = prim;
610
611 if ((prim == GL_TRIANGLES) && (ctx->_TriangleCaps & DD_TRI_UNFILLED))
612 return;
613
614 r300RasterPrimitive( ctx, reduced_prim[prim] );
615 // fprintf(stderr, "%s\n", __FUNCTION__);
616
617 }
618
619 static void r300ResetLineStipple(GLcontext *ctx)
620 {
621
622
623 }
624
625 void r300InitSwtcl(GLcontext *ctx)
626 {
627 TNLcontext *tnl = TNL_CONTEXT(ctx);
628 r300ContextPtr rmesa = R300_CONTEXT(ctx);
629 static int firsttime = 1;
630
631 if (firsttime) {
632 init_rast_tab();
633 firsttime = 0;
634 }
635
636 tnl->Driver.Render.Start = r300RenderStart;
637 tnl->Driver.Render.Finish = r300RenderFinish;
638 tnl->Driver.Render.PrimitiveNotify = r300RenderPrimitive;
639 tnl->Driver.Render.ResetLineStipple = r300ResetLineStipple;
640 tnl->Driver.Render.BuildVertices = _tnl_build_vertices;
641 tnl->Driver.Render.CopyPV = _tnl_copy_pv;
642 tnl->Driver.Render.Interp = _tnl_interp;
643
644 /* FIXME: what are these numbers? */
645 _tnl_init_vertices( ctx, ctx->Const.MaxArrayLockSize + 12,
646 48 * sizeof(GLfloat) );
647
648 rmesa->swtcl.verts = (GLubyte *)tnl->clipspace.vertex_buf;
649 rmesa->swtcl.RenderIndex = ~0;
650 rmesa->swtcl.render_primitive = GL_TRIANGLES;
651 rmesa->swtcl.hw_primitive = 0;
652
653 _tnl_invalidate_vertex_state( ctx, ~0 );
654 _tnl_invalidate_vertices( ctx, ~0 );
655 RENDERINPUTS_ZERO( rmesa->tnl_index_bitset );
656
657 _tnl_need_projected_coords( ctx, GL_FALSE );
658 r300ChooseRenderState(ctx);
659
660 _mesa_validate_all_lighting_tables( ctx );
661
662 tnl->Driver.NotifyMaterialChange =
663 _mesa_validate_all_lighting_tables;
664 }
665
666 void r300DestroySwtcl(GLcontext *ctx)
667 {
668 }
669
670 void r300EmitVertexAOS(r300ContextPtr rmesa, GLuint vertex_size, struct radeon_bo *bo, GLuint offset)
671 {
672 BATCH_LOCALS(&rmesa->radeon);
673
674 if (RADEON_DEBUG & DEBUG_VERTS)
675 fprintf(stderr, "%s: vertex_size %d, offset 0x%x \n",
676 __FUNCTION__, vertex_size, offset);
677
678 BEGIN_BATCH(5);
679 OUT_BATCH_PACKET3(R300_PACKET3_3D_LOAD_VBPNTR, 2);
680 OUT_BATCH(1);
681 OUT_BATCH(vertex_size | (vertex_size << 8));
682 OUT_BATCH_RELOC(offset, bo, offset, RADEON_GEM_DOMAIN_GTT, 0, 0);
683 END_BATCH();
684 }
685
686 void r300EmitVbufPrim(r300ContextPtr rmesa, GLuint primitive, GLuint vertex_nr)
687 {
688 BATCH_LOCALS(&rmesa->radeon);
689 int type, num_verts;
690
691 type = r300PrimitiveType(rmesa, primitive);
692 num_verts = r300NumVerts(rmesa, vertex_nr, primitive);
693
694 BEGIN_BATCH(3);
695 OUT_BATCH_PACKET3(R300_PACKET3_3D_DRAW_VBUF_2, 0);
696 OUT_BATCH(R300_VAP_VF_CNTL__PRIM_WALK_VERTEX_LIST | (num_verts << 16) | type);
697 END_BATCH();
698 }