Merge remote branch 'main/master' into radeon-rewrite
[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 #include "r300_tex.h"
60
61 void r300EmitVertexAOS(r300ContextPtr rmesa, GLuint vertex_size, struct radeon_bo *bo, GLuint offset);
62 void r300EmitVbufPrim(r300ContextPtr rmesa, GLuint primitive, GLuint vertex_nr);
63 #define EMIT_ATTR( ATTR, STYLE ) \
64 do { \
65 rmesa->radeon.swtcl.vertex_attrs[rmesa->radeon.swtcl.vertex_attr_count].attrib = (ATTR); \
66 rmesa->radeon.swtcl.vertex_attrs[rmesa->radeon.swtcl.vertex_attr_count].format = (STYLE); \
67 rmesa->radeon.swtcl.vertex_attr_count++; \
68 } while (0)
69
70 #define EMIT_PAD( N ) \
71 do { \
72 rmesa->radeon.swtcl.vertex_attrs[rmesa->radeon.swtcl.vertex_attr_count].attrib = 0; \
73 rmesa->radeon.swtcl.vertex_attrs[rmesa->radeon.swtcl.vertex_attr_count].format = EMIT_PAD; \
74 rmesa->radeon.swtcl.vertex_attrs[rmesa->radeon.swtcl.vertex_attr_count].offset = (N); \
75 rmesa->radeon.swtcl.vertex_attr_count++; \
76 } while (0)
77
78 static void r300SetVertexFormat( GLcontext *ctx )
79 {
80 r300ContextPtr rmesa = R300_CONTEXT( ctx );
81 TNLcontext *tnl = TNL_CONTEXT(ctx);
82 struct vertex_buffer *VB = &tnl->vb;
83 DECLARE_RENDERINPUTS(index_bitset);
84 GLuint InputsRead = 0, OutputsWritten = 0;
85 int vap_fmt_1 = 0;
86 int offset = 0;
87 int vte = 0;
88 int fog_id;
89 GLint inputs[VERT_ATTRIB_MAX];
90 GLint tab[VERT_ATTRIB_MAX];
91 int swizzle[VERT_ATTRIB_MAX][4];
92 GLuint i, nr;
93 GLuint sz;
94
95 DECLARE_RENDERINPUTS(render_inputs_bitset);
96 RENDERINPUTS_COPY(render_inputs_bitset, tnl->render_inputs_bitset);
97 RENDERINPUTS_COPY( index_bitset, tnl->render_inputs_bitset );
98 RENDERINPUTS_COPY(rmesa->state.render_inputs_bitset, render_inputs_bitset);
99
100 vte = rmesa->hw.vte.cmd[1];
101 vte &= ~(R300_VTX_XY_FMT | R300_VTX_Z_FMT | R300_VTX_W0_FMT);
102 /* Important:
103 */
104 if ( VB->NdcPtr != NULL ) {
105 VB->AttribPtr[VERT_ATTRIB_POS] = VB->NdcPtr;
106 vte |= R300_VTX_XY_FMT | R300_VTX_Z_FMT;
107 }
108 else {
109 VB->AttribPtr[VERT_ATTRIB_POS] = VB->ClipPtr;
110 vte |= R300_VTX_W0_FMT;
111 }
112
113 assert( VB->AttribPtr[VERT_ATTRIB_POS] != NULL );
114 rmesa->radeon.swtcl.vertex_attr_count = 0;
115
116 /* EMIT_ATTR's must be in order as they tell t_vertex.c how to
117 * build up a hardware vertex.
118 */
119 if (RENDERINPUTS_TEST( index_bitset, _TNL_ATTRIB_POS)) {
120 sz = VB->AttribPtr[VERT_ATTRIB_POS]->size;
121 InputsRead |= 1 << VERT_ATTRIB_POS;
122 OutputsWritten |= 1 << VERT_RESULT_HPOS;
123 EMIT_ATTR( _TNL_ATTRIB_POS, EMIT_1F + sz - 1 );
124 offset = sz;
125 } else {
126 offset = 4;
127 EMIT_PAD(4 * sizeof(float));
128 }
129 /*
130 if (RENDERINPUTS_TEST( index_bitset, _TNL_ATTRIB_POINTSIZE )) {
131 EMIT_ATTR( _TNL_ATTRIB_POINTSIZE, EMIT_1F );
132 offset += 1;
133 }
134 */
135 if (RENDERINPUTS_TEST(index_bitset, _TNL_ATTRIB_COLOR0)) {
136 sz = VB->AttribPtr[VERT_ATTRIB_COLOR0]->size;
137 rmesa->swtcl.coloroffset = offset;
138 InputsRead |= 1 << VERT_ATTRIB_COLOR0;
139 OutputsWritten |= 1 << VERT_RESULT_COL0;
140 EMIT_ATTR( _TNL_ATTRIB_COLOR0, EMIT_1F + sz - 1 );
141 offset += sz;
142 }
143
144 rmesa->swtcl.specoffset = 0;
145 if (RENDERINPUTS_TEST( index_bitset, _TNL_ATTRIB_COLOR1 )) {
146 sz = VB->AttribPtr[VERT_ATTRIB_COLOR1]->size;
147 rmesa->swtcl.specoffset = offset;
148 EMIT_ATTR( _TNL_ATTRIB_COLOR1, EMIT_1F + sz - 1 );
149 InputsRead |= 1 << VERT_ATTRIB_COLOR1;
150 OutputsWritten |= 1 << VERT_RESULT_COL1;
151 }
152
153 fog_id = -1;
154 if (RENDERINPUTS_TEST(index_bitset, _TNL_ATTRIB_FOG)) {
155 /* find first free tex coord slot */
156 if (RENDERINPUTS_TEST_RANGE( index_bitset, _TNL_FIRST_TEX, _TNL_LAST_TEX )) {
157 int i;
158 for (i = 0; i < ctx->Const.MaxTextureUnits; i++) {
159 if (!RENDERINPUTS_TEST( index_bitset, _TNL_ATTRIB_TEX(i) )) {
160 fog_id = i;
161 break;
162 }
163 }
164 } else {
165 fog_id = 0;
166 }
167
168 if (fog_id == -1) {
169 fprintf(stderr, "\tout of free texcoords to do fog\n");
170 _mesa_exit(-1);
171 }
172
173 sz = VB->AttribPtr[VERT_ATTRIB_FOG]->size;
174 EMIT_ATTR( _TNL_ATTRIB_FOG, EMIT_1F + sz - 1);
175 InputsRead |= 1 << VERT_ATTRIB_FOG;
176 OutputsWritten |= 1 << VERT_RESULT_FOGC;
177 vap_fmt_1 |= sz << (3 * fog_id);
178 }
179
180 if (RENDERINPUTS_TEST_RANGE( index_bitset, _TNL_FIRST_TEX, _TNL_LAST_TEX )) {
181 int i;
182
183 for (i = 0; i < ctx->Const.MaxTextureUnits; i++) {
184 if (RENDERINPUTS_TEST( index_bitset, _TNL_ATTRIB_TEX(i) )) {
185 sz = VB->TexCoordPtr[i]->size;
186 InputsRead |= 1 << (VERT_ATTRIB_TEX0 + i);
187 OutputsWritten |= 1 << (VERT_RESULT_TEX0 + i);
188 EMIT_ATTR( _TNL_ATTRIB_TEX0+i, EMIT_1F + sz - 1 );
189 vap_fmt_1 |= sz << (3 * i);
190 }
191 }
192 }
193
194 /* RS can't put fragment position on the pixel stack, so stuff it in texcoord if needed */
195 if (RENDERINPUTS_TEST( index_bitset, _TNL_ATTRIB_POS) && (ctx->FragmentProgram._Current->Base.InputsRead & FRAG_BIT_WPOS)) {
196 int first_free_tex = -1;
197 if (fog_id >= 0) {
198 first_free_tex = fog_id+1;
199 } else {
200 if (RENDERINPUTS_TEST_RANGE( index_bitset, _TNL_FIRST_TEX, _TNL_LAST_TEX )) {
201 int i;
202 for (i = 0; i < ctx->Const.MaxTextureUnits; i++) {
203 if (!RENDERINPUTS_TEST( index_bitset, _TNL_ATTRIB_TEX(i) )) {
204 first_free_tex = i;
205 break;
206 }
207 }
208 } else {
209 first_free_tex = 0;
210 }
211 }
212
213 if (first_free_tex == -1) {
214 fprintf(stderr, "\tout of free texcoords to write w pos\n");
215 _mesa_exit(-1);
216 }
217
218 sz = VB->AttribPtr[VERT_ATTRIB_POS]->size;
219 InputsRead |= 1 << (VERT_ATTRIB_TEX0 + first_free_tex);
220 OutputsWritten |= 1 << (VERT_RESULT_TEX0 + first_free_tex);
221 EMIT_ATTR( _TNL_ATTRIB_POS, EMIT_1F + sz - 1 );
222 vap_fmt_1 |= sz << (3 * first_free_tex);
223 }
224
225 for (i = 0, nr = 0; i < VERT_ATTRIB_MAX; i++) {
226 if (InputsRead & (1 << i)) {
227 inputs[i] = nr++;
228 } else {
229 inputs[i] = -1;
230 }
231 }
232
233 /* Fixed, apply to vir0 only */
234 if (InputsRead & (1 << VERT_ATTRIB_POS))
235 inputs[VERT_ATTRIB_POS] = 0;
236 if (InputsRead & (1 << VERT_ATTRIB_COLOR0))
237 inputs[VERT_ATTRIB_COLOR0] = 2;
238 if (InputsRead & (1 << VERT_ATTRIB_COLOR1))
239 inputs[VERT_ATTRIB_COLOR1] = 3;
240 if (InputsRead & (1 << VERT_ATTRIB_FOG))
241 inputs[VERT_ATTRIB_FOG] = 6 + fog_id;
242 for (i = VERT_ATTRIB_TEX0; i <= VERT_ATTRIB_TEX7; i++)
243 if (InputsRead & (1 << i))
244 inputs[i] = 6 + (i - VERT_ATTRIB_TEX0);
245
246 for (i = 0, nr = 0; i < VERT_ATTRIB_MAX; i++) {
247 if (InputsRead & (1 << i)) {
248 tab[nr++] = i;
249 }
250 }
251
252 for (i = 0; i < nr; i++) {
253 int ci;
254
255 swizzle[i][0] = SWIZZLE_ZERO;
256 swizzle[i][1] = SWIZZLE_ZERO;
257 swizzle[i][2] = SWIZZLE_ZERO;
258 swizzle[i][3] = SWIZZLE_ONE;
259
260 for (ci = 0; ci < VB->AttribPtr[tab[i]]->size; ci++) {
261 swizzle[i][ci] = ci;
262 }
263 }
264
265 R300_NEWPRIM(rmesa);
266 if (rmesa->radeon.radeonScreen->kernel_mm) {
267 R300_STATECHANGE(rmesa, vir[0]);
268 rmesa->hw.vir[0].cmd[0] &= 0xC000FFFF;
269 rmesa->hw.vir[1].cmd[0] &= 0xC000FFFF;
270 rmesa->hw.vir[0].cmd[0] |=
271 (r300VAPInputRoute0(&rmesa->hw.vir[0].cmd[R300_VIR_CNTL_0],
272 VB->AttribPtr, inputs, tab, nr) & 0x3FFF) << 16;
273 R300_STATECHANGE(rmesa, vir[1]);
274 rmesa->hw.vir[1].cmd[0] |=
275 (r300VAPInputRoute1(&rmesa->hw.vir[1].cmd[R300_VIR_CNTL_0], swizzle,
276 nr) & 0x3FFF) << 16;
277 } else {
278 R300_STATECHANGE(rmesa, vir[0]);
279 ((drm_r300_cmd_header_t *) rmesa->hw.vir[0].cmd)->packet0.count =
280 r300VAPInputRoute0(&rmesa->hw.vir[0].cmd[R300_VIR_CNTL_0],
281 VB->AttribPtr, inputs, tab, nr);
282 R300_STATECHANGE(rmesa, vir[1]);
283 ((drm_r300_cmd_header_t *) rmesa->hw.vir[1].cmd)->packet0.count =
284 r300VAPInputRoute1(&rmesa->hw.vir[1].cmd[R300_VIR_CNTL_0], swizzle,
285 nr);
286 }
287
288 R300_STATECHANGE(rmesa, vic);
289 rmesa->hw.vic.cmd[R300_VIC_CNTL_0] = r300VAPInputCntl0(ctx, InputsRead);
290 rmesa->hw.vic.cmd[R300_VIC_CNTL_1] = r300VAPInputCntl1(ctx, InputsRead);
291
292 R300_STATECHANGE(rmesa, vof);
293 rmesa->hw.vof.cmd[R300_VOF_CNTL_0] = r300VAPOutputCntl0(ctx, OutputsWritten);
294 rmesa->hw.vof.cmd[R300_VOF_CNTL_1] = vap_fmt_1;
295
296 rmesa->radeon.swtcl.vertex_size =
297 _tnl_install_attrs( ctx,
298 rmesa->radeon.swtcl.vertex_attrs,
299 rmesa->radeon.swtcl.vertex_attr_count,
300 NULL, 0 );
301
302 rmesa->radeon.swtcl.vertex_size /= 4;
303
304 RENDERINPUTS_COPY( rmesa->tnl_index_bitset, index_bitset );
305
306
307 R300_STATECHANGE(rmesa, vte);
308 rmesa->hw.vte.cmd[1] = vte;
309 rmesa->hw.vte.cmd[2] = rmesa->radeon.swtcl.vertex_size;
310 }
311
312 static GLuint reduced_prim[] = {
313 GL_POINTS,
314 GL_LINES,
315 GL_LINES,
316 GL_LINES,
317 GL_TRIANGLES,
318 GL_TRIANGLES,
319 GL_TRIANGLES,
320 GL_TRIANGLES,
321 GL_TRIANGLES,
322 GL_TRIANGLES,
323 };
324
325 static void r300RasterPrimitive( GLcontext *ctx, GLuint prim );
326 static void r300RenderPrimitive( GLcontext *ctx, GLenum prim );
327 //static void r300ResetLineStipple( GLcontext *ctx );
328
329 /***********************************************************************
330 * Emit primitives as inline vertices *
331 ***********************************************************************/
332
333
334 #define HAVE_POINTS 1
335 #define HAVE_LINES 1
336 #define HAVE_LINE_STRIPS 1
337 #define HAVE_TRIANGLES 1
338 #define HAVE_TRI_STRIPS 1
339 #define HAVE_TRI_STRIP_1 0
340 #define HAVE_TRI_FANS 1
341 #define HAVE_QUADS 0
342 #define HAVE_QUAD_STRIPS 0
343 #define HAVE_POLYGONS 1
344 #define HAVE_ELTS 1
345
346 #undef LOCAL_VARS
347 #undef ALLOC_VERTS
348 #define CTX_ARG r300ContextPtr rmesa
349 #define GET_VERTEX_DWORDS() rmesa->radeon.swtcl.vertex_size
350 #define ALLOC_VERTS( n, size ) rcommonAllocDmaLowVerts( &rmesa->radeon, n, size * 4 )
351 #define LOCAL_VARS \
352 r300ContextPtr rmesa = R300_CONTEXT(ctx); \
353 const char *r300verts = (char *)rmesa->radeon.swtcl.verts;
354 #define VERT(x) (r300Vertex *)(r300verts + ((x) * vertsize * sizeof(int)))
355 #define VERTEX r300Vertex
356 #define DO_DEBUG_VERTS (1 && (RADEON_DEBUG & DEBUG_VERTS))
357 #define PRINT_VERTEX(x)
358 #undef TAG
359 #define TAG(x) r300_##x
360 #include "tnl_dd/t_dd_triemit.h"
361
362
363
364 /***********************************************************************
365 * Macros for t_dd_tritmp.h to draw basic primitives *
366 ***********************************************************************/
367
368 #define QUAD( a, b, c, d ) r300_quad( rmesa, a, b, c, d )
369 #define TRI( a, b, c ) r300_triangle( rmesa, a, b, c )
370 #define LINE( a, b ) r300_line( rmesa, a, b )
371 #define POINT( a ) r300_point( rmesa, a )
372
373 /***********************************************************************
374 * Build render functions from dd templates *
375 ***********************************************************************/
376
377 #define R300_TWOSIDE_BIT 0x01
378 #define R300_UNFILLED_BIT 0x02
379 #define R300_MAX_TRIFUNC 0x04
380
381 static struct {
382 tnl_points_func points;
383 tnl_line_func line;
384 tnl_triangle_func triangle;
385 tnl_quad_func quad;
386 } rast_tab[R300_MAX_TRIFUNC];
387
388 #define DO_FALLBACK 0
389 #define DO_UNFILLED (IND & R300_UNFILLED_BIT)
390 #define DO_TWOSIDE (IND & R300_TWOSIDE_BIT)
391 #define DO_FLAT 0
392 #define DO_OFFSET 0
393 #define DO_TRI 1
394 #define DO_QUAD 1
395 #define DO_LINE 1
396 #define DO_POINTS 1
397 #define DO_FULL_QUAD 1
398
399 #define HAVE_RGBA 1
400 #define HAVE_SPEC 1
401 #define HAVE_BACK_COLORS 0
402 #define HAVE_HW_FLATSHADE 1
403 #define TAB rast_tab
404
405 #define DEPTH_SCALE 1.0
406 #define UNFILLED_TRI unfilled_tri
407 #define UNFILLED_QUAD unfilled_quad
408 #define VERT_X(_v) _v->v.x
409 #define VERT_Y(_v) _v->v.y
410 #define VERT_Z(_v) _v->v.z
411 #define AREA_IS_CCW( a ) (a < 0)
412 #define GET_VERTEX(e) (rmesa->radeon.swtcl.verts + (e*rmesa->radeon.swtcl.vertex_size*sizeof(int)))
413
414 /* Only used to pull back colors into vertices (ie, we know color is
415 * floating point).
416 */
417 #define R300_COLOR( dst, src ) \
418 do { \
419 UNCLAMPED_FLOAT_TO_UBYTE((dst)[0], (src)[2]); \
420 UNCLAMPED_FLOAT_TO_UBYTE((dst)[1], (src)[1]); \
421 UNCLAMPED_FLOAT_TO_UBYTE((dst)[2], (src)[0]); \
422 UNCLAMPED_FLOAT_TO_UBYTE((dst)[3], (src)[3]); \
423 } while (0)
424
425 #define VERT_SET_RGBA( v, c ) if (coloroffset) R300_COLOR( v->ub4[coloroffset], c )
426 #define VERT_COPY_RGBA( v0, v1 ) if (coloroffset) v0->ui[coloroffset] = v1->ui[coloroffset]
427 #define VERT_SAVE_RGBA( idx ) if (coloroffset) color[idx] = v[idx]->ui[coloroffset]
428 #define VERT_RESTORE_RGBA( idx ) if (coloroffset) v[idx]->ui[coloroffset] = color[idx]
429
430 #define R300_SPEC( dst, src ) \
431 do { \
432 UNCLAMPED_FLOAT_TO_UBYTE((dst)[0], (src)[2]); \
433 UNCLAMPED_FLOAT_TO_UBYTE((dst)[1], (src)[1]); \
434 UNCLAMPED_FLOAT_TO_UBYTE((dst)[2], (src)[0]); \
435 } while (0)
436
437 #define VERT_SET_SPEC( v, c ) if (specoffset) R300_SPEC( v->ub4[specoffset], c )
438 #define VERT_COPY_SPEC( v0, v1 ) if (specoffset) COPY_3V(v0->ub4[specoffset], v1->ub4[specoffset])
439 #define VERT_SAVE_SPEC( idx ) if (specoffset) spec[idx] = v[idx]->ui[specoffset]
440 #define VERT_RESTORE_SPEC( idx ) if (specoffset) v[idx]->ui[specoffset] = spec[idx]
441
442 #undef LOCAL_VARS
443 #undef TAG
444 #undef INIT
445
446 #define LOCAL_VARS(n) \
447 r300ContextPtr rmesa = R300_CONTEXT(ctx); \
448 GLuint color[n], spec[n]; \
449 GLuint coloroffset = rmesa->swtcl.coloroffset; \
450 GLuint specoffset = rmesa->swtcl.specoffset; \
451 (void) color; (void) spec; (void) coloroffset; (void) specoffset;
452
453 /***********************************************************************
454 * Helpers for rendering unfilled primitives *
455 ***********************************************************************/
456
457 #define RASTERIZE(x) r300RasterPrimitive( ctx, reduced_prim[x] )
458 #define RENDER_PRIMITIVE rmesa->radeon.swtcl.render_primitive
459 #undef TAG
460 #define TAG(x) x
461 #include "tnl_dd/t_dd_unfilled.h"
462 #undef IND
463
464
465 /***********************************************************************
466 * Generate GL render functions *
467 ***********************************************************************/
468
469
470 #define IND (0)
471 #define TAG(x) x
472 #include "tnl_dd/t_dd_tritmp.h"
473
474 #define IND (R300_TWOSIDE_BIT)
475 #define TAG(x) x##_twoside
476 #include "tnl_dd/t_dd_tritmp.h"
477
478 #define IND (R300_UNFILLED_BIT)
479 #define TAG(x) x##_unfilled
480 #include "tnl_dd/t_dd_tritmp.h"
481
482 #define IND (R300_TWOSIDE_BIT|R300_UNFILLED_BIT)
483 #define TAG(x) x##_twoside_unfilled
484 #include "tnl_dd/t_dd_tritmp.h"
485
486
487
488 static void init_rast_tab( void )
489 {
490 init();
491 init_twoside();
492 init_unfilled();
493 init_twoside_unfilled();
494 }
495
496 /**********************************************************************/
497 /* Render unclipped begin/end objects */
498 /**********************************************************************/
499
500 #define RENDER_POINTS( start, count ) \
501 for ( ; start < count ; start++) \
502 r300_point( rmesa, VERT(start) )
503 #define RENDER_LINE( v0, v1 ) \
504 r300_line( rmesa, VERT(v0), VERT(v1) )
505 #define RENDER_TRI( v0, v1, v2 ) \
506 r300_triangle( rmesa, VERT(v0), VERT(v1), VERT(v2) )
507 #define RENDER_QUAD( v0, v1, v2, v3 ) \
508 r300_quad( rmesa, VERT(v0), VERT(v1), VERT(v2), VERT(v3) )
509 #define INIT(x) do { \
510 r300RenderPrimitive( ctx, x ); \
511 } while (0)
512 #undef LOCAL_VARS
513 #define LOCAL_VARS \
514 r300ContextPtr rmesa = R300_CONTEXT(ctx); \
515 const GLuint vertsize = rmesa->radeon.swtcl.vertex_size; \
516 const char *r300verts = (char *)rmesa->radeon.swtcl.verts; \
517 const GLuint * const elt = TNL_CONTEXT(ctx)->vb.Elts; \
518 const GLboolean stipple = ctx->Line.StippleFlag; \
519 (void) elt; (void) stipple;
520 #define RESET_STIPPLE //if ( stipple ) r200ResetLineStipple( ctx );
521 #define RESET_OCCLUSION
522 #define PRESERVE_VB_DEFS
523 #define ELT(x) (x)
524 #define TAG(x) r300_##x##_verts
525 #include "tnl/t_vb_rendertmp.h"
526 #undef ELT
527 #undef TAG
528 #define TAG(x) r300_##x##_elts
529 #define ELT(x) elt[x]
530 #include "tnl/t_vb_rendertmp.h"
531
532
533
534
535 /**********************************************************************/
536 /* Choose render functions */
537 /**********************************************************************/
538 static void r300ChooseRenderState( GLcontext *ctx )
539 {
540 TNLcontext *tnl = TNL_CONTEXT(ctx);
541 r300ContextPtr rmesa = R300_CONTEXT(ctx);
542 GLuint index = 0;
543 GLuint flags = ctx->_TriangleCaps;
544
545 if (flags & DD_TRI_LIGHT_TWOSIDE) index |= R300_TWOSIDE_BIT;
546 if (flags & DD_TRI_UNFILLED) index |= R300_UNFILLED_BIT;
547
548 if (index != rmesa->radeon.swtcl.RenderIndex) {
549 tnl->Driver.Render.Points = rast_tab[index].points;
550 tnl->Driver.Render.Line = rast_tab[index].line;
551 tnl->Driver.Render.ClippedLine = rast_tab[index].line;
552 tnl->Driver.Render.Triangle = rast_tab[index].triangle;
553 tnl->Driver.Render.Quad = rast_tab[index].quad;
554
555 if (index == 0) {
556 tnl->Driver.Render.PrimTabVerts = r300_render_tab_verts;
557 tnl->Driver.Render.PrimTabElts = r300_render_tab_elts;
558 tnl->Driver.Render.ClippedPolygon = r300_fast_clipped_poly;
559 } else {
560 tnl->Driver.Render.PrimTabVerts = _tnl_render_tab_verts;
561 tnl->Driver.Render.PrimTabElts = _tnl_render_tab_elts;
562 tnl->Driver.Render.ClippedPolygon = _tnl_RenderClippedPolygon;
563 }
564
565 rmesa->radeon.swtcl.RenderIndex = index;
566 }
567 }
568
569
570 static void r300RenderStart(GLcontext *ctx)
571 {
572 r300ContextPtr rmesa = R300_CONTEXT( ctx );
573 // fprintf(stderr, "%s\n", __FUNCTION__);
574
575 r300ChooseRenderState(ctx);
576 r300SetVertexFormat(ctx);
577
578 r300ValidateBuffers(ctx);
579
580 r300UpdateShaders(rmesa);
581 r300UpdateShaderStates(rmesa);
582
583 r300EmitCacheFlush(rmesa);
584
585 /* investigate if we can put back flush optimisation if needed */
586 if (rmesa->radeon.dma.flush != NULL) {
587 rmesa->radeon.dma.flush(ctx);
588 }
589
590 }
591
592 static void r300RenderFinish(GLcontext *ctx)
593 {
594 }
595
596 static void r300RasterPrimitive( GLcontext *ctx, GLuint hwprim )
597 {
598 r300ContextPtr rmesa = R300_CONTEXT(ctx);
599
600 if (rmesa->radeon.swtcl.hw_primitive != hwprim) {
601 R300_NEWPRIM( rmesa );
602 rmesa->radeon.swtcl.hw_primitive = hwprim;
603 }
604 }
605
606 static void r300RenderPrimitive(GLcontext *ctx, GLenum prim)
607 {
608
609 r300ContextPtr rmesa = R300_CONTEXT(ctx);
610 rmesa->radeon.swtcl.render_primitive = prim;
611
612 if ((prim == GL_TRIANGLES) && (ctx->_TriangleCaps & DD_TRI_UNFILLED))
613 return;
614
615 r300RasterPrimitive( ctx, reduced_prim[prim] );
616 // fprintf(stderr, "%s\n", __FUNCTION__);
617
618 }
619
620 static void r300ResetLineStipple(GLcontext *ctx)
621 {
622
623
624 }
625
626 void r300InitSwtcl(GLcontext *ctx)
627 {
628 TNLcontext *tnl = TNL_CONTEXT(ctx);
629 r300ContextPtr rmesa = R300_CONTEXT(ctx);
630 static int firsttime = 1;
631
632 if (firsttime) {
633 init_rast_tab();
634 firsttime = 0;
635 }
636
637 tnl->Driver.Render.Start = r300RenderStart;
638 tnl->Driver.Render.Finish = r300RenderFinish;
639 tnl->Driver.Render.PrimitiveNotify = r300RenderPrimitive;
640 tnl->Driver.Render.ResetLineStipple = r300ResetLineStipple;
641 tnl->Driver.Render.BuildVertices = _tnl_build_vertices;
642 tnl->Driver.Render.CopyPV = _tnl_copy_pv;
643 tnl->Driver.Render.Interp = _tnl_interp;
644
645 /* FIXME: what are these numbers? */
646 _tnl_init_vertices( ctx, ctx->Const.MaxArrayLockSize + 12,
647 48 * sizeof(GLfloat) );
648
649 rmesa->radeon.swtcl.verts = (GLubyte *)tnl->clipspace.vertex_buf;
650 rmesa->radeon.swtcl.RenderIndex = ~0;
651 rmesa->radeon.swtcl.render_primitive = GL_TRIANGLES;
652 rmesa->radeon.swtcl.hw_primitive = 0;
653
654 _tnl_invalidate_vertex_state( ctx, ~0 );
655 _tnl_invalidate_vertices( ctx, ~0 );
656 RENDERINPUTS_ZERO( rmesa->tnl_index_bitset );
657
658 _tnl_need_projected_coords( ctx, GL_FALSE );
659 r300ChooseRenderState(ctx);
660
661 _mesa_validate_all_lighting_tables( ctx );
662
663 tnl->Driver.NotifyMaterialChange =
664 _mesa_validate_all_lighting_tables;
665 }
666
667 void r300DestroySwtcl(GLcontext *ctx)
668 {
669 }
670
671 void r300EmitVertexAOS(r300ContextPtr rmesa, GLuint vertex_size, struct radeon_bo *bo, GLuint offset)
672 {
673 BATCH_LOCALS(&rmesa->radeon);
674
675 if (RADEON_DEBUG & DEBUG_VERTS)
676 fprintf(stderr, "%s: vertex_size %d, offset 0x%x \n",
677 __FUNCTION__, vertex_size, offset);
678
679 BEGIN_BATCH(7);
680 OUT_BATCH_PACKET3(R300_PACKET3_3D_LOAD_VBPNTR, 2);
681 OUT_BATCH(1);
682 OUT_BATCH(vertex_size | (vertex_size << 8));
683 OUT_BATCH_RELOC(offset, bo, offset, RADEON_GEM_DOMAIN_GTT, 0, 0);
684 END_BATCH();
685 }
686
687 void r300EmitVbufPrim(r300ContextPtr rmesa, GLuint primitive, GLuint vertex_nr)
688 {
689 BATCH_LOCALS(&rmesa->radeon);
690 int type, num_verts;
691
692 type = r300PrimitiveType(rmesa, primitive);
693 num_verts = r300NumVerts(rmesa, vertex_nr, primitive);
694
695 BEGIN_BATCH(3);
696 OUT_BATCH_PACKET3(R300_PACKET3_3D_DRAW_VBUF_2, 0);
697 OUT_BATCH(R300_VAP_VF_CNTL__PRIM_WALK_VERTEX_LIST | (num_verts << 16) | type);
698 END_BATCH();
699 }
700
701 void r300_swtcl_flush(GLcontext *ctx, uint32_t current_offset)
702 {
703 r300ContextPtr rmesa = R300_CONTEXT(ctx);
704
705 rcommonEnsureCmdBufSpace(&rmesa->radeon,
706 rmesa->radeon.hw.max_state_size + (12*sizeof(int)),
707 __FUNCTION__);
708 radeonEmitState(&rmesa->radeon);
709 r300EmitVertexAOS(rmesa,
710 rmesa->radeon.swtcl.vertex_size,
711 rmesa->radeon.dma.current,
712 current_offset);
713
714 r300EmitVbufPrim(rmesa,
715 rmesa->radeon.swtcl.hw_primitive,
716 rmesa->radeon.swtcl.numverts);
717 r300EmitCacheFlush(rmesa);
718 COMMIT_BATCH();
719 }