r300: do front/back color selection in HW for software TCL path
[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 * Maciej Cencora <m.cencora@gmail.com>
32 */
33
34 #include "tnl/tnl.h"
35 #include "tnl/t_pipeline.h"
36
37 #include "r300_state.h"
38 #include "r300_swtcl.h"
39 #include "r300_emit.h"
40 #include "r300_tex.h"
41
42 #define EMIT_ATTR( ATTR, STYLE ) \
43 do { \
44 rmesa->radeon.swtcl.vertex_attrs[rmesa->radeon.swtcl.vertex_attr_count].attrib = (ATTR); \
45 rmesa->radeon.swtcl.vertex_attrs[rmesa->radeon.swtcl.vertex_attr_count].format = (STYLE); \
46 rmesa->radeon.swtcl.vertex_attr_count++; \
47 } while (0)
48
49 #define EMIT_PAD( N ) \
50 do { \
51 rmesa->radeon.swtcl.vertex_attrs[rmesa->radeon.swtcl.vertex_attr_count].attrib = 0; \
52 rmesa->radeon.swtcl.vertex_attrs[rmesa->radeon.swtcl.vertex_attr_count].format = EMIT_PAD; \
53 rmesa->radeon.swtcl.vertex_attrs[rmesa->radeon.swtcl.vertex_attr_count].offset = (N); \
54 rmesa->radeon.swtcl.vertex_attr_count++; \
55 } while (0)
56
57 #define ADD_ATTR(_attr, _format, _dst_loc, _swizzle, _write_mask) \
58 do { \
59 attrs[num_attrs].attr = (_attr); \
60 attrs[num_attrs].format = (_format); \
61 attrs[num_attrs].dst_loc = (_dst_loc); \
62 attrs[num_attrs].swizzle = (_swizzle); \
63 attrs[num_attrs].write_mask = (_write_mask); \
64 ++num_attrs; \
65 } while (0)
66
67 static void r300SwtclVAPSetup(GLcontext *ctx, GLuint InputsRead, GLuint OutputsWritten, GLuint vap_out_fmt_1)
68 {
69 r300ContextPtr rmesa = R300_CONTEXT( ctx );
70 struct vertex_attribute *attrs = rmesa->swtcl.vert_attrs;
71 int i, j, reg_count;
72 uint32_t *vir0 = &rmesa->hw.vir[0].cmd[1];
73 uint32_t *vir1 = &rmesa->hw.vir[1].cmd[1];
74
75 for (i = 0; i < R300_VIR_CMDSIZE-1; ++i)
76 vir0[i] = vir1[i] = 0;
77
78 for (i = 0, j = 0; i < rmesa->radeon.swtcl.vertex_attr_count; ++i) {
79 int tmp, data_format;
80 switch (attrs[i].format) {
81 case EMIT_1F:
82 data_format = R300_DATA_TYPE_FLOAT_1;
83 break;
84 case EMIT_2F:
85 data_format = R300_DATA_TYPE_FLOAT_2;
86 break;
87 case EMIT_3F:
88 data_format = R300_DATA_TYPE_FLOAT_3;
89 break;
90 case EMIT_4F:
91 data_format = R300_DATA_TYPE_FLOAT_4;
92 break;
93 case EMIT_4UB_4F_RGBA:
94 case EMIT_4UB_4F_ABGR:
95 data_format = R300_DATA_TYPE_BYTE | R300_NORMALIZE;
96 break;
97 default:
98 fprintf(stderr, "%s: Invalid data format type", __FUNCTION__);
99 _mesa_exit(-1);
100 break;
101 }
102
103 tmp = data_format | (attrs[i].dst_loc << R300_DST_VEC_LOC_SHIFT);
104 if (i % 2 == 0) {
105 vir0[j] = tmp << R300_DATA_TYPE_0_SHIFT;
106 vir1[j] = attrs[i].swizzle | (attrs[i].write_mask << R300_WRITE_ENA_SHIFT);
107 } else {
108 vir0[j] |= tmp << R300_DATA_TYPE_1_SHIFT;
109 vir1[j] |= (attrs[i].swizzle | (attrs[i].write_mask << R300_WRITE_ENA_SHIFT)) << R300_SWIZZLE1_SHIFT;
110 ++j;
111 }
112 }
113
114 reg_count = (rmesa->radeon.swtcl.vertex_attr_count + 1) >> 1;
115 if (rmesa->radeon.swtcl.vertex_attr_count % 2 != 0) {
116 vir0[reg_count-1] |= R300_LAST_VEC << R300_DATA_TYPE_0_SHIFT;
117 } else {
118 vir0[reg_count-1] |= R300_LAST_VEC << R300_DATA_TYPE_1_SHIFT;
119 }
120
121 R300_STATECHANGE(rmesa, vir[0]);
122 R300_STATECHANGE(rmesa, vir[1]);
123 R300_STATECHANGE(rmesa, vof);
124 R300_STATECHANGE(rmesa, vic);
125
126 if (rmesa->radeon.radeonScreen->kernel_mm) {
127 rmesa->hw.vir[0].cmd[0] &= 0xC000FFFF;
128 rmesa->hw.vir[1].cmd[0] &= 0xC000FFFF;
129 rmesa->hw.vir[0].cmd[0] |= (reg_count & 0x3FFF) << 16;
130 rmesa->hw.vir[1].cmd[0] |= (reg_count & 0x3FFF) << 16;
131 } else {
132 ((drm_r300_cmd_header_t *) rmesa->hw.vir[0].cmd)->packet0.count = reg_count;
133 ((drm_r300_cmd_header_t *) rmesa->hw.vir[1].cmd)->packet0.count = reg_count;
134 }
135
136 rmesa->hw.vic.cmd[R300_VIC_CNTL_0] = r300VAPInputCntl0(ctx, InputsRead);
137 rmesa->hw.vic.cmd[R300_VIC_CNTL_1] = r300VAPInputCntl1(ctx, InputsRead);
138 rmesa->hw.vof.cmd[R300_VOF_CNTL_0] = r300VAPOutputCntl0(ctx, OutputsWritten);
139 /**
140 * Can't use r300VAPOutputCntl1 function because it assumes
141 * that all texture coords have 4 components and that's the case
142 * for HW TCL path, but not for SW TCL.
143 */
144 rmesa->hw.vof.cmd[R300_VOF_CNTL_1] = vap_out_fmt_1;
145 }
146
147
148 static void r300SetVertexFormat( GLcontext *ctx )
149 {
150 r300ContextPtr rmesa = R300_CONTEXT( ctx );
151 TNLcontext *tnl = TNL_CONTEXT(ctx);
152 struct vertex_buffer *VB = &tnl->vb;
153 int first_free_tex = 0, vap_out_fmt_1 = 0;
154 GLuint InputsRead = 0;
155 GLuint OutputsWritten = 0;
156 int num_attrs = 0;
157 struct vertex_attribute *attrs = rmesa->swtcl.vert_attrs;
158
159 rmesa->swtcl.coloroffset = rmesa->swtcl.specoffset = 0;
160 rmesa->radeon.swtcl.vertex_attr_count = 0;
161
162 /* We always want non Ndc coords format */
163 VB->AttribPtr[VERT_ATTRIB_POS] = VB->ClipPtr;
164
165 if (RENDERINPUTS_TEST(tnl->render_inputs_bitset, _TNL_ATTRIB_POS)) {
166 InputsRead |= 1 << VERT_ATTRIB_POS;
167 OutputsWritten |= 1 << VERT_RESULT_HPOS;
168 EMIT_ATTR( _TNL_ATTRIB_POS, EMIT_4F );
169 ADD_ATTR(VERT_ATTRIB_POS, EMIT_4F, SWTCL_OVM_POS, SWIZZLE_XYZW, MASK_XYZW);
170 rmesa->swtcl.coloroffset = 4;
171 }
172
173 if (RENDERINPUTS_TEST(tnl->render_inputs_bitset, _TNL_ATTRIB_COLOR0)) {
174 InputsRead |= 1 << VERT_ATTRIB_COLOR0;
175 OutputsWritten |= 1 << VERT_RESULT_COL0;
176 #if MESA_LITTLE_ENDIAN
177 EMIT_ATTR( _TNL_ATTRIB_COLOR0, EMIT_4UB_4F_RGBA );
178 ADD_ATTR(VERT_ATTRIB_COLOR0, EMIT_4UB_4F_RGBA, SWTCL_OVM_COLOR0, SWIZZLE_XYZW, MASK_XYZW);
179 #else
180 EMIT_ATTR( _TNL_ATTRIB_COLOR0, EMIT_4UB_4F_ABGR );
181 ADD_ATTR(VERT_ATTRIB_COLOR0, EMIT_4UB_4F_ABGR, SWTCL_OVM_COLOR0, SWIZZLE_XYZW, MASK_XYZW);
182 #endif
183 }
184
185 if (RENDERINPUTS_TEST(tnl->render_inputs_bitset, _TNL_ATTRIB_COLOR1 )) {
186 GLuint swiz = MAKE_SWIZZLE4(SWIZZLE_X, SWIZZLE_Y, SWIZZLE_Z, SWIZZLE_ONE);
187 InputsRead |= 1 << VERT_ATTRIB_COLOR1;
188 OutputsWritten |= 1 << VERT_RESULT_COL1;
189 #if MESA_LITTLE_ENDIAN
190 EMIT_ATTR( _TNL_ATTRIB_COLOR1, EMIT_4UB_4F_RGBA );
191 ADD_ATTR(VERT_ATTRIB_COLOR1, EMIT_4UB_4F_RGBA, SWTCL_OVM_COLOR1, swiz, MASK_XYZW);
192 #else
193 EMIT_ATTR( _TNL_ATTRIB_COLOR1, EMIT_4UB_4F_ABGR );
194 ADD_ATTR(VERT_ATTRIB_COLOR1, EMIT_4UB_4F_ABGR, SWTCL_OVM_COLOR1, swiz, MASK_XYZW);
195 #endif
196 rmesa->swtcl.specoffset = rmesa->swtcl.coloroffset + 1;
197 }
198
199 if (ctx->Light.Enabled && ctx->Light.Model.TwoSide) {
200 VB->AttribPtr[VERT_ATTRIB_GENERIC0] = VB->ColorPtr[1];
201 OutputsWritten |= 1 << VERT_RESULT_BFC0;
202 #if MESA_LITTLE_ENDIAN
203 EMIT_ATTR( _TNL_ATTRIB_GENERIC0, EMIT_4UB_4F_RGBA );
204 ADD_ATTR(VERT_ATTRIB_GENERIC0, EMIT_4UB_4F_RGBA, SWTCL_OVM_COLOR2, SWIZZLE_XYZW, MASK_XYZW);
205 #else
206 EMIT_ATTR( _TNL_ATTRIB_GENERIC0, EMIT_4UB_4F_ABGR );
207 ADD_ATTR(VERT_ATTRIB_GENERIC0, EMIT_4UB_4F_ABGR, SWTCL_OVM_COLOR2, SWIZZLE_XYZW, MASK_XYZW);
208 #endif
209 if (RENDERINPUTS_TEST(tnl->render_inputs_bitset, _TNL_ATTRIB_COLOR1 )) {
210 GLuint swiz = MAKE_SWIZZLE4(SWIZZLE_X, SWIZZLE_Y, SWIZZLE_Z, SWIZZLE_ONE);
211 OutputsWritten |= 1 << VERT_RESULT_BFC1;
212 #if MESA_LITTLE_ENDIAN
213 EMIT_ATTR( _TNL_ATTRIB_GENERIC1, EMIT_4UB_4F_RGBA );
214 ADD_ATTR(VERT_ATTRIB_GENERIC1, EMIT_4UB_4F_RGBA, SWTCL_OVM_COLOR3, swiz, MASK_XYZW);
215 #else
216 EMIT_ATTR( _TNL_ATTRIB_GENERIC1, EMIT_4UB_4F_ABGR );
217 ADD_ATTR(VERT_ATTRIB_GENERIC1, EMIT_4UB_4F_ABGR, SWTCL_OVM_COLOR3, swiz, MASK_XYZW);
218 #endif
219 }
220 }
221
222 if (RENDERINPUTS_TEST(tnl->render_inputs_bitset, _TNL_ATTRIB_POINTSIZE )) {
223 GLuint swiz = MAKE_SWIZZLE4(SWIZZLE_X, SWIZZLE_ZERO, SWIZZLE_ZERO, SWIZZLE_ZERO);
224 InputsRead |= 1 << VERT_ATTRIB_POINT_SIZE;
225 OutputsWritten |= 1 << VERT_RESULT_PSIZ;
226 EMIT_ATTR( _TNL_ATTRIB_POINTSIZE, EMIT_1F );
227 ADD_ATTR(VERT_ATTRIB_POINT_SIZE, EMIT_1F, SWTCL_OVM_POINT_SIZE, swiz, MASK_X);
228 }
229
230 /**
231 * Sending only one texcoord component may lead to lock up,
232 * so for all textures always output 4 texcoord components to RS.
233 */
234 if (RENDERINPUTS_TEST_RANGE(tnl->render_inputs_bitset, _TNL_FIRST_TEX, _TNL_LAST_TEX )) {
235 int i;
236 GLuint swiz, format;
237 for (i = 0; i < ctx->Const.MaxTextureUnits; i++) {
238 if (RENDERINPUTS_TEST(tnl->render_inputs_bitset, _TNL_ATTRIB_TEX(i) )) {
239 switch (VB->TexCoordPtr[i]->size) {
240 case 1:
241 format = EMIT_1F;
242 swiz = MAKE_SWIZZLE4(SWIZZLE_X, SWIZZLE_ZERO, SWIZZLE_ZERO, SWIZZLE_ONE);
243 break;
244 case 2:
245 format = EMIT_2F;
246 swiz = MAKE_SWIZZLE4(SWIZZLE_X, SWIZZLE_Y, SWIZZLE_ZERO, SWIZZLE_ONE);
247 break;
248 case 3:
249 format = EMIT_3F;
250 swiz = MAKE_SWIZZLE4(SWIZZLE_X, SWIZZLE_Y, SWIZZLE_Z, SWIZZLE_ONE);
251 break;
252 case 4:
253 format = EMIT_4F;
254 swiz = SWIZZLE_XYZW;
255 break;
256 default:
257 continue;
258 }
259 InputsRead |= 1 << (VERT_ATTRIB_TEX0 + i);
260 OutputsWritten |= 1 << (VERT_RESULT_TEX0 + i);
261 EMIT_ATTR(_TNL_ATTRIB_TEX(i), format);
262 ADD_ATTR(VERT_ATTRIB_TEX0 + i, format, SWTCL_OVM_TEX(i), swiz, MASK_XYZW);
263 vap_out_fmt_1 |= 4 << (i * 3);
264 ++first_free_tex;
265 }
266 }
267 }
268
269 /* RS can't put fragment position on the pixel stack, so stuff it in texcoord if needed */
270 if (RENDERINPUTS_TEST(tnl->render_inputs_bitset, _TNL_ATTRIB_POS) && (ctx->FragmentProgram._Current->Base.InputsRead & FRAG_BIT_WPOS)) {
271 if (first_free_tex >= ctx->Const.MaxTextureUnits) {
272 fprintf(stderr, "\tout of free texcoords to write w pos\n");
273 _mesa_exit(-1);
274 }
275
276 InputsRead |= 1 << (VERT_ATTRIB_TEX0 + first_free_tex);
277 OutputsWritten |= 1 << (VERT_RESULT_TEX0 + first_free_tex);
278 EMIT_ATTR( _TNL_ATTRIB_POS, EMIT_4F );
279 ADD_ATTR(VERT_ATTRIB_POS, EMIT_4F, SWTCL_OVM_TEX(first_free_tex), SWIZZLE_XYZW, MASK_XYZW);
280 vap_out_fmt_1 |= 4 << (first_free_tex * 3);
281 ++first_free_tex;
282 }
283
284 if (RENDERINPUTS_TEST(tnl->render_inputs_bitset, _TNL_ATTRIB_FOG)) {
285 if (first_free_tex >= ctx->Const.MaxTextureUnits) {
286 fprintf(stderr, "\tout of free texcoords to write fog coordinate\n");
287 _mesa_exit(-1);
288 }
289
290 InputsRead |= 1 << VERT_ATTRIB_FOG;
291 OutputsWritten |= 1 << VERT_RESULT_FOGC;
292 GLuint swiz = MAKE_SWIZZLE4(SWIZZLE_X, SWIZZLE_ZERO, SWIZZLE_ZERO, SWIZZLE_ZERO);
293 EMIT_ATTR( _TNL_ATTRIB_FOG, EMIT_1F );
294 ADD_ATTR(VERT_ATTRIB_FOG, EMIT_1F, SWTCL_OVM_TEX(first_free_tex), swiz, MASK_X);
295 vap_out_fmt_1 |= 1 << (first_free_tex * 3);
296 }
297
298 R300_NEWPRIM(rmesa);
299 r300SwtclVAPSetup(ctx, InputsRead, OutputsWritten, vap_out_fmt_1);
300
301 rmesa->radeon.swtcl.vertex_size =
302 _tnl_install_attrs( ctx,
303 rmesa->radeon.swtcl.vertex_attrs,
304 rmesa->radeon.swtcl.vertex_attr_count,
305 NULL, 0 );
306
307 rmesa->radeon.swtcl.vertex_size /= 4;
308
309 RENDERINPUTS_COPY(rmesa->render_inputs_bitset, tnl->render_inputs_bitset);
310 }
311
312
313 static GLuint reduced_prim[] = {
314 GL_POINTS,
315 GL_LINES,
316 GL_LINES,
317 GL_LINES,
318 GL_TRIANGLES,
319 GL_TRIANGLES,
320 GL_TRIANGLES,
321 GL_TRIANGLES,
322 GL_TRIANGLES,
323 GL_TRIANGLES,
324 };
325
326 static void r300RasterPrimitive( GLcontext *ctx, GLuint prim );
327 static void r300RenderPrimitive( GLcontext *ctx, GLenum prim );
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 #undef TAG
357 #define TAG(x) r300_##x
358 #include "tnl_dd/t_dd_triemit.h"
359
360
361
362 /***********************************************************************
363 * Macros for t_dd_tritmp.h to draw basic primitives *
364 ***********************************************************************/
365
366 #define QUAD( a, b, c, d ) r300_quad( rmesa, a, b, c, d )
367 #define TRI( a, b, c ) r300_triangle( rmesa, a, b, c )
368 #define LINE( a, b ) r300_line( rmesa, a, b )
369 #define POINT( a ) r300_point( rmesa, a )
370
371 /***********************************************************************
372 * Build render functions from dd templates *
373 ***********************************************************************/
374
375 #define R300_UNFILLED_BIT 0x01
376 #define R300_MAX_TRIFUNC 0x02
377
378 static struct {
379 tnl_points_func points;
380 tnl_line_func line;
381 tnl_triangle_func triangle;
382 tnl_quad_func quad;
383 } rast_tab[R300_MAX_TRIFUNC];
384
385 #define DO_FALLBACK 0
386 #define DO_UNFILLED (IND & R300_UNFILLED_BIT)
387 #define DO_TWOSIDE 0
388 #define DO_FLAT 0
389 #define DO_OFFSET 0
390 #define DO_TRI 1
391 #define DO_QUAD 1
392 #define DO_LINE 1
393 #define DO_POINTS 1
394 #define DO_FULL_QUAD 1
395
396 #define HAVE_RGBA 1
397 #define HAVE_SPEC 1
398 #define HAVE_BACK_COLORS 0
399 #define HAVE_HW_FLATSHADE 1
400 #define TAB rast_tab
401
402 #define DEPTH_SCALE 1.0
403 #define UNFILLED_TRI unfilled_tri
404 #define UNFILLED_QUAD unfilled_quad
405 #define VERT_X(_v) _v->v.x
406 #define VERT_Y(_v) _v->v.y
407 #define VERT_Z(_v) _v->v.z
408 #define AREA_IS_CCW( a ) (a < 0)
409 #define GET_VERTEX(e) (rmesa->radeon.swtcl.verts + (e*rmesa->radeon.swtcl.vertex_size*sizeof(int)))
410
411 #define VERT_SET_RGBA( v, c ) \
412 do { \
413 r300_color_t *color = (r300_color_t *)&((v)->ui[coloroffset]); \
414 UNCLAMPED_FLOAT_TO_UBYTE(color->red, (c)[0]); \
415 UNCLAMPED_FLOAT_TO_UBYTE(color->green, (c)[1]); \
416 UNCLAMPED_FLOAT_TO_UBYTE(color->blue, (c)[2]); \
417 UNCLAMPED_FLOAT_TO_UBYTE(color->alpha, (c)[3]); \
418 } while (0)
419
420 #define VERT_COPY_RGBA( v0, v1 ) v0->ui[coloroffset] = v1->ui[coloroffset]
421
422 #define VERT_SET_SPEC( v0, c ) \
423 do { \
424 if (specoffset) { \
425 UNCLAMPED_FLOAT_TO_UBYTE(v0->v.specular.red, (c)[0]); \
426 UNCLAMPED_FLOAT_TO_UBYTE(v0->v.specular.green, (c)[1]); \
427 UNCLAMPED_FLOAT_TO_UBYTE(v0->v.specular.blue, (c)[2]); \
428 } \
429 } while (0)
430
431 #define VERT_COPY_SPEC( v0, v1 ) \
432 do { \
433 if (specoffset) { \
434 v0->v.specular.red = v1->v.specular.red; \
435 v0->v.specular.green = v1->v.specular.green; \
436 v0->v.specular.blue = v1->v.specular.blue; \
437 } \
438 } while (0)
439
440 #define VERT_SAVE_RGBA( idx ) color[idx] = v[idx]->ui[coloroffset]
441 #define VERT_RESTORE_RGBA( idx ) v[idx]->ui[coloroffset] = color[idx]
442 #define VERT_SAVE_SPEC( idx ) if (specoffset) spec[idx] = v[idx]->ui[specoffset]
443 #define VERT_RESTORE_SPEC( idx ) if (specoffset) v[idx]->ui[specoffset] = spec[idx]
444
445 #undef LOCAL_VARS
446 #undef TAG
447 #undef INIT
448
449 #define LOCAL_VARS(n) \
450 r300ContextPtr rmesa = R300_CONTEXT(ctx); \
451 GLuint color[n] = { 0, }, spec[n] = { 0, }; \
452 GLuint coloroffset = rmesa->swtcl.coloroffset; \
453 GLuint specoffset = rmesa->swtcl.specoffset; \
454 (void) color; (void) spec; (void) coloroffset; (void) specoffset;
455
456 /***********************************************************************
457 * Helpers for rendering unfilled primitives *
458 ***********************************************************************/
459
460 #define RASTERIZE(x) r300RasterPrimitive( ctx, reduced_prim[x] )
461 #define RENDER_PRIMITIVE rmesa->radeon.swtcl.render_primitive
462 #undef TAG
463 #define TAG(x) x
464 #include "tnl_dd/t_dd_unfilled.h"
465 #undef IND
466
467
468 /***********************************************************************
469 * Generate GL render functions *
470 ***********************************************************************/
471
472
473 #define IND (0)
474 #define TAG(x) x
475 #include "tnl_dd/t_dd_tritmp.h"
476
477 #define IND (R300_UNFILLED_BIT)
478 #define TAG(x) x##_unfilled
479 #include "tnl_dd/t_dd_tritmp.h"
480
481
482 static void init_rast_tab( void )
483 {
484 init();
485 init_unfilled();
486 }
487
488 /**********************************************************************/
489 /* Render unclipped begin/end objects */
490 /**********************************************************************/
491
492 #define RENDER_POINTS( start, count ) \
493 for ( ; start < count ; start++) \
494 r300_point( rmesa, VERT(start) )
495 #define RENDER_LINE( v0, v1 ) \
496 r300_line( rmesa, VERT(v0), VERT(v1) )
497 #define RENDER_TRI( v0, v1, v2 ) \
498 r300_triangle( rmesa, VERT(v0), VERT(v1), VERT(v2) )
499 #define RENDER_QUAD( v0, v1, v2, v3 ) \
500 r300_quad( rmesa, VERT(v0), VERT(v1), VERT(v2), VERT(v3) )
501 #define INIT(x) do { \
502 r300RenderPrimitive( ctx, x ); \
503 } while (0)
504 #undef LOCAL_VARS
505 #define LOCAL_VARS \
506 r300ContextPtr rmesa = R300_CONTEXT(ctx); \
507 const GLuint vertsize = rmesa->radeon.swtcl.vertex_size; \
508 const char *r300verts = (char *)rmesa->radeon.swtcl.verts; \
509 const GLuint * const elt = TNL_CONTEXT(ctx)->vb.Elts; \
510 const GLboolean stipple = ctx->Line.StippleFlag; \
511 (void) elt; (void) stipple;
512 #define RESET_STIPPLE //if ( stipple ) r200ResetLineStipple( ctx );
513 #define RESET_OCCLUSION
514 #define PRESERVE_VB_DEFS
515 #define ELT(x) (x)
516 #define TAG(x) r300_##x##_verts
517 #include "tnl/t_vb_rendertmp.h"
518 #undef ELT
519 #undef TAG
520 #define TAG(x) r300_##x##_elts
521 #define ELT(x) elt[x]
522 #include "tnl/t_vb_rendertmp.h"
523
524
525
526
527 /**********************************************************************/
528 /* Choose render functions */
529 /**********************************************************************/
530 static void r300ChooseRenderState( GLcontext *ctx )
531 {
532 TNLcontext *tnl = TNL_CONTEXT(ctx);
533 r300ContextPtr rmesa = R300_CONTEXT(ctx);
534 GLuint index = 0;
535 GLuint flags = ctx->_TriangleCaps;
536
537 if (flags & DD_TRI_UNFILLED) index |= R300_UNFILLED_BIT;
538
539 if (index != rmesa->radeon.swtcl.RenderIndex) {
540 tnl->Driver.Render.Points = rast_tab[index].points;
541 tnl->Driver.Render.Line = rast_tab[index].line;
542 tnl->Driver.Render.ClippedLine = rast_tab[index].line;
543 tnl->Driver.Render.Triangle = rast_tab[index].triangle;
544 tnl->Driver.Render.Quad = rast_tab[index].quad;
545
546 if (index == 0) {
547 tnl->Driver.Render.PrimTabVerts = r300_render_tab_verts;
548 tnl->Driver.Render.PrimTabElts = r300_render_tab_elts;
549 tnl->Driver.Render.ClippedPolygon = r300_fast_clipped_poly;
550 } else {
551 tnl->Driver.Render.PrimTabVerts = _tnl_render_tab_verts;
552 tnl->Driver.Render.PrimTabElts = _tnl_render_tab_elts;
553 tnl->Driver.Render.ClippedPolygon = _tnl_RenderClippedPolygon;
554 }
555
556 rmesa->radeon.swtcl.RenderIndex = index;
557 }
558 }
559
560
561 static void r300RenderStart(GLcontext *ctx)
562 {
563 r300ContextPtr rmesa = R300_CONTEXT( ctx );
564
565 r300ChooseRenderState(ctx);
566 r300SetVertexFormat(ctx);
567
568 r300ValidateBuffers(ctx);
569
570 r300UpdateShaders(rmesa);
571 r300UpdateShaderStates(rmesa);
572
573 r300EmitCacheFlush(rmesa);
574
575 /* investigate if we can put back flush optimisation if needed */
576 if (rmesa->radeon.dma.flush != NULL) {
577 rmesa->radeon.dma.flush(ctx);
578 }
579 }
580
581 static void r300RenderFinish(GLcontext *ctx)
582 {
583 }
584
585 static void r300RasterPrimitive( GLcontext *ctx, GLuint hwprim )
586 {
587 r300ContextPtr rmesa = R300_CONTEXT(ctx);
588
589 if (rmesa->radeon.swtcl.hw_primitive != hwprim) {
590 R300_NEWPRIM( rmesa );
591 rmesa->radeon.swtcl.hw_primitive = hwprim;
592 }
593 }
594
595 static void r300RenderPrimitive(GLcontext *ctx, GLenum prim)
596 {
597
598 r300ContextPtr rmesa = R300_CONTEXT(ctx);
599 rmesa->radeon.swtcl.render_primitive = prim;
600
601 if ((prim == GL_TRIANGLES) && (ctx->_TriangleCaps & DD_TRI_UNFILLED))
602 return;
603
604 r300RasterPrimitive( ctx, reduced_prim[prim] );
605 }
606
607 static void r300ResetLineStipple(GLcontext *ctx)
608 {
609 }
610
611 void r300InitSwtcl(GLcontext *ctx)
612 {
613 TNLcontext *tnl = TNL_CONTEXT(ctx);
614 r300ContextPtr rmesa = R300_CONTEXT(ctx);
615 static int firsttime = 1;
616
617 if (firsttime) {
618 init_rast_tab();
619 firsttime = 0;
620 }
621
622 tnl->Driver.Render.Start = r300RenderStart;
623 tnl->Driver.Render.Finish = r300RenderFinish;
624 tnl->Driver.Render.PrimitiveNotify = r300RenderPrimitive;
625 tnl->Driver.Render.ResetLineStipple = r300ResetLineStipple;
626 tnl->Driver.Render.BuildVertices = _tnl_build_vertices;
627 tnl->Driver.Render.CopyPV = _tnl_copy_pv;
628 tnl->Driver.Render.Interp = _tnl_interp;
629
630 /* FIXME: what are these numbers? */
631 _tnl_init_vertices( ctx, ctx->Const.MaxArrayLockSize + 12,
632 48 * sizeof(GLfloat) );
633
634 rmesa->radeon.swtcl.verts = (GLubyte *)tnl->clipspace.vertex_buf;
635 rmesa->radeon.swtcl.RenderIndex = ~0;
636 rmesa->radeon.swtcl.render_primitive = GL_TRIANGLES;
637 rmesa->radeon.swtcl.hw_primitive = 0;
638
639 _tnl_invalidate_vertex_state( ctx, ~0 );
640 _tnl_invalidate_vertices( ctx, ~0 );
641
642 _tnl_need_projected_coords( ctx, GL_FALSE );
643 r300ChooseRenderState(ctx);
644 }
645
646 void r300DestroySwtcl(GLcontext *ctx)
647 {
648 }
649
650 static void r300EmitVertexAOS(r300ContextPtr rmesa, GLuint vertex_size, struct radeon_bo *bo, GLuint offset)
651 {
652 BATCH_LOCALS(&rmesa->radeon);
653
654 if (RADEON_DEBUG & DEBUG_VERTS)
655 fprintf(stderr, "%s: vertex_size %d, offset 0x%x \n",
656 __FUNCTION__, vertex_size, offset);
657
658 BEGIN_BATCH(7);
659 OUT_BATCH_PACKET3(R300_PACKET3_3D_LOAD_VBPNTR, 2);
660 OUT_BATCH(1);
661 OUT_BATCH(vertex_size | (vertex_size << 8));
662 OUT_BATCH_RELOC(offset, bo, offset, RADEON_GEM_DOMAIN_GTT, 0, 0);
663 END_BATCH();
664 }
665
666 static void r300EmitVbufPrim(r300ContextPtr rmesa, GLuint primitive, GLuint vertex_nr)
667 {
668 BATCH_LOCALS(&rmesa->radeon);
669 int type, num_verts;
670
671 type = r300PrimitiveType(rmesa, primitive);
672 num_verts = r300NumVerts(rmesa, vertex_nr, primitive);
673
674 BEGIN_BATCH(3);
675 OUT_BATCH_PACKET3(R300_PACKET3_3D_DRAW_VBUF_2, 0);
676 OUT_BATCH(R300_VAP_VF_CNTL__PRIM_WALK_VERTEX_LIST | (num_verts << 16) | type);
677 END_BATCH();
678 }
679
680 void r300_swtcl_flush(GLcontext *ctx, uint32_t current_offset)
681 {
682 r300ContextPtr rmesa = R300_CONTEXT(ctx);
683
684 rcommonEnsureCmdBufSpace(&rmesa->radeon,
685 rmesa->radeon.hw.max_state_size + (12*sizeof(int)),
686 __FUNCTION__);
687 radeonEmitState(&rmesa->radeon);
688 r300EmitVertexAOS(rmesa,
689 rmesa->radeon.swtcl.vertex_size,
690 rmesa->radeon.dma.current,
691 current_offset);
692
693 r300EmitVbufPrim(rmesa,
694 rmesa->radeon.swtcl.hw_primitive,
695 rmesa->radeon.swtcl.numverts);
696 r300EmitCacheFlush(rmesa);
697 COMMIT_BATCH();
698 }