r500: fixup support for emitting fragment program to hardware.
[mesa.git] / src / mesa / drivers / dri / r300 / r300_state.c
1 /*
2 Copyright (C) The Weather Channel, Inc. 2002.
3 Copyright (C) 2004 Nicolai Haehnle.
4 All Rights Reserved.
5
6 The Weather Channel (TM) funded Tungsten Graphics to develop the
7 initial release of the Radeon 8500 driver under the XFree86 license.
8 This notice must be preserved.
9
10 Permission is hereby granted, free of charge, to any person obtaining
11 a copy of this software and associated documentation files (the
12 "Software"), to deal in the Software without restriction, including
13 without limitation the rights to use, copy, modify, merge, publish,
14 distribute, sublicense, and/or sell copies of the Software, and to
15 permit persons to whom the Software is furnished to do so, subject to
16 the following conditions:
17
18 The above copyright notice and this permission notice (including the
19 next paragraph) shall be included in all copies or substantial
20 portions of the Software.
21
22 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
25 IN NO EVENT SHALL THE COPYRIGHT OWNER(S) AND/OR ITS SUPPLIERS BE
26 LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27 OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29
30 **************************************************************************/
31
32 /**
33 * \file
34 *
35 * \author Nicolai Haehnle <prefect_@gmx.net>
36 */
37
38 #include "glheader.h"
39 #include "state.h"
40 #include "imports.h"
41 #include "enums.h"
42 #include "macros.h"
43 #include "context.h"
44 #include "dd.h"
45 #include "simple_list.h"
46
47 #include "api_arrayelt.h"
48 #include "swrast/swrast.h"
49 #include "swrast_setup/swrast_setup.h"
50 #include "shader/prog_parameter.h"
51 #include "shader/prog_statevars.h"
52 #include "vbo/vbo.h"
53 #include "tnl/tnl.h"
54 #include "texformat.h"
55
56 #include "radeon_ioctl.h"
57 #include "radeon_state.h"
58 #include "r300_context.h"
59 #include "r300_ioctl.h"
60 #include "r300_state.h"
61 #include "r300_reg.h"
62 #include "r300_emit.h"
63 #include "r300_fragprog.h"
64 #include "r300_tex.h"
65
66 #include "drirenderbuffer.h"
67
68 extern int future_hw_tcl_on;
69 extern void _tnl_UpdateFixedFunctionProgram(GLcontext * ctx);
70
71 static void r300BlendColor(GLcontext * ctx, const GLfloat cf[4])
72 {
73 GLubyte color[4];
74 r300ContextPtr rmesa = R300_CONTEXT(ctx);
75
76 R300_STATECHANGE(rmesa, blend_color);
77
78 CLAMPED_FLOAT_TO_UBYTE(color[0], cf[0]);
79 CLAMPED_FLOAT_TO_UBYTE(color[1], cf[1]);
80 CLAMPED_FLOAT_TO_UBYTE(color[2], cf[2]);
81 CLAMPED_FLOAT_TO_UBYTE(color[3], cf[3]);
82
83 rmesa->hw.blend_color.cmd[1] = PACK_COLOR_8888(color[3], color[0],
84 color[1], color[2]);
85 rmesa->hw.blend_color.cmd[2] = 0;
86 rmesa->hw.blend_color.cmd[3] = 0;
87 }
88
89 /**
90 * Calculate the hardware blend factor setting. This same function is used
91 * for source and destination of both alpha and RGB.
92 *
93 * \returns
94 * The hardware register value for the specified blend factor. This value
95 * will need to be shifted into the correct position for either source or
96 * destination factor.
97 *
98 * \todo
99 * Since the two cases where source and destination are handled differently
100 * are essentially error cases, they should never happen. Determine if these
101 * cases can be removed.
102 */
103 static int blend_factor(GLenum factor, GLboolean is_src)
104 {
105 switch (factor) {
106 case GL_ZERO:
107 return R300_BLEND_GL_ZERO;
108 break;
109 case GL_ONE:
110 return R300_BLEND_GL_ONE;
111 break;
112 case GL_DST_COLOR:
113 return R300_BLEND_GL_DST_COLOR;
114 break;
115 case GL_ONE_MINUS_DST_COLOR:
116 return R300_BLEND_GL_ONE_MINUS_DST_COLOR;
117 break;
118 case GL_SRC_COLOR:
119 return R300_BLEND_GL_SRC_COLOR;
120 break;
121 case GL_ONE_MINUS_SRC_COLOR:
122 return R300_BLEND_GL_ONE_MINUS_SRC_COLOR;
123 break;
124 case GL_SRC_ALPHA:
125 return R300_BLEND_GL_SRC_ALPHA;
126 break;
127 case GL_ONE_MINUS_SRC_ALPHA:
128 return R300_BLEND_GL_ONE_MINUS_SRC_ALPHA;
129 break;
130 case GL_DST_ALPHA:
131 return R300_BLEND_GL_DST_ALPHA;
132 break;
133 case GL_ONE_MINUS_DST_ALPHA:
134 return R300_BLEND_GL_ONE_MINUS_DST_ALPHA;
135 break;
136 case GL_SRC_ALPHA_SATURATE:
137 return (is_src) ? R300_BLEND_GL_SRC_ALPHA_SATURATE :
138 R300_BLEND_GL_ZERO;
139 break;
140 case GL_CONSTANT_COLOR:
141 return R300_BLEND_GL_CONST_COLOR;
142 break;
143 case GL_ONE_MINUS_CONSTANT_COLOR:
144 return R300_BLEND_GL_ONE_MINUS_CONST_COLOR;
145 break;
146 case GL_CONSTANT_ALPHA:
147 return R300_BLEND_GL_CONST_ALPHA;
148 break;
149 case GL_ONE_MINUS_CONSTANT_ALPHA:
150 return R300_BLEND_GL_ONE_MINUS_CONST_ALPHA;
151 break;
152 default:
153 fprintf(stderr, "unknown blend factor %x\n", factor);
154 return (is_src) ? R300_BLEND_GL_ONE : R300_BLEND_GL_ZERO;
155 break;
156 }
157 }
158
159 /**
160 * Sets both the blend equation and the blend function.
161 * This is done in a single
162 * function because some blend equations (i.e., \c GL_MIN and \c GL_MAX)
163 * change the interpretation of the blend function.
164 * Also, make sure that blend function and blend equation are set to their
165 * default value if color blending is not enabled, since at least blend
166 * equations GL_MIN and GL_FUNC_REVERSE_SUBTRACT will cause wrong results
167 * otherwise for unknown reasons.
168 */
169
170 /* helper function */
171 static void r300SetBlendCntl(r300ContextPtr r300, int func, int eqn,
172 int cbits, int funcA, int eqnA)
173 {
174 GLuint new_ablend, new_cblend;
175
176 #if 0
177 fprintf(stderr,
178 "eqnA=%08x funcA=%08x eqn=%08x func=%08x cbits=%08x\n",
179 eqnA, funcA, eqn, func, cbits);
180 #endif
181 new_ablend = eqnA | funcA;
182 new_cblend = eqn | func;
183
184 /* Some blend factor combinations don't seem to work when the
185 * BLEND_NO_SEPARATE bit is set.
186 *
187 * Especially problematic candidates are the ONE_MINUS_* flags,
188 * but I can't see a real pattern.
189 */
190 #if 0
191 if (new_ablend == new_cblend) {
192 new_cblend |= R300_BLEND_NO_SEPARATE;
193 }
194 #endif
195 new_cblend |= cbits;
196
197 if ((new_ablend != r300->hw.bld.cmd[R300_BLD_ABLEND]) ||
198 (new_cblend != r300->hw.bld.cmd[R300_BLD_CBLEND])) {
199 R300_STATECHANGE(r300, bld);
200 r300->hw.bld.cmd[R300_BLD_ABLEND] = new_ablend;
201 r300->hw.bld.cmd[R300_BLD_CBLEND] = new_cblend;
202 }
203 }
204
205 static void r300SetBlendState(GLcontext * ctx)
206 {
207 r300ContextPtr r300 = R300_CONTEXT(ctx);
208 int func = (R300_BLEND_GL_ONE << R300_SRC_BLEND_SHIFT) |
209 (R300_BLEND_GL_ZERO << R300_DST_BLEND_SHIFT);
210 int eqn = R300_COMB_FCN_ADD_CLAMP;
211 int funcA = (R300_BLEND_GL_ONE << R300_SRC_BLEND_SHIFT) |
212 (R300_BLEND_GL_ZERO << R300_DST_BLEND_SHIFT);
213 int eqnA = R300_COMB_FCN_ADD_CLAMP;
214
215 if (RGBA_LOGICOP_ENABLED(ctx) || !ctx->Color.BlendEnabled) {
216 r300SetBlendCntl(r300, func, eqn, 0, func, eqn);
217 return;
218 }
219
220 func =
221 (blend_factor(ctx->Color.BlendSrcRGB, GL_TRUE) <<
222 R300_SRC_BLEND_SHIFT) | (blend_factor(ctx->Color.BlendDstRGB,
223 GL_FALSE) <<
224 R300_DST_BLEND_SHIFT);
225
226 switch (ctx->Color.BlendEquationRGB) {
227 case GL_FUNC_ADD:
228 eqn = R300_COMB_FCN_ADD_CLAMP;
229 break;
230
231 case GL_FUNC_SUBTRACT:
232 eqn = R300_COMB_FCN_SUB_CLAMP;
233 break;
234
235 case GL_FUNC_REVERSE_SUBTRACT:
236 eqn = R300_COMB_FCN_RSUB_CLAMP;
237 break;
238
239 case GL_MIN:
240 eqn = R300_COMB_FCN_MIN;
241 func = (R300_BLEND_GL_ONE << R300_SRC_BLEND_SHIFT) |
242 (R300_BLEND_GL_ONE << R300_DST_BLEND_SHIFT);
243 break;
244
245 case GL_MAX:
246 eqn = R300_COMB_FCN_MAX;
247 func = (R300_BLEND_GL_ONE << R300_SRC_BLEND_SHIFT) |
248 (R300_BLEND_GL_ONE << R300_DST_BLEND_SHIFT);
249 break;
250
251 default:
252 fprintf(stderr,
253 "[%s:%u] Invalid RGB blend equation (0x%04x).\n",
254 __FUNCTION__, __LINE__, ctx->Color.BlendEquationRGB);
255 return;
256 }
257
258 funcA =
259 (blend_factor(ctx->Color.BlendSrcA, GL_TRUE) <<
260 R300_SRC_BLEND_SHIFT) | (blend_factor(ctx->Color.BlendDstA,
261 GL_FALSE) <<
262 R300_DST_BLEND_SHIFT);
263
264 switch (ctx->Color.BlendEquationA) {
265 case GL_FUNC_ADD:
266 eqnA = R300_COMB_FCN_ADD_CLAMP;
267 break;
268
269 case GL_FUNC_SUBTRACT:
270 eqnA = R300_COMB_FCN_SUB_CLAMP;
271 break;
272
273 case GL_FUNC_REVERSE_SUBTRACT:
274 eqnA = R300_COMB_FCN_RSUB_CLAMP;
275 break;
276
277 case GL_MIN:
278 eqnA = R300_COMB_FCN_MIN;
279 funcA = (R300_BLEND_GL_ONE << R300_SRC_BLEND_SHIFT) |
280 (R300_BLEND_GL_ONE << R300_DST_BLEND_SHIFT);
281 break;
282
283 case GL_MAX:
284 eqnA = R300_COMB_FCN_MAX;
285 funcA = (R300_BLEND_GL_ONE << R300_SRC_BLEND_SHIFT) |
286 (R300_BLEND_GL_ONE << R300_DST_BLEND_SHIFT);
287 break;
288
289 default:
290 fprintf(stderr,
291 "[%s:%u] Invalid A blend equation (0x%04x).\n",
292 __FUNCTION__, __LINE__, ctx->Color.BlendEquationA);
293 return;
294 }
295
296 r300SetBlendCntl(r300,
297 func, eqn,
298 R300_BLEND_UNKNOWN | R300_BLEND_ENABLE, funcA, eqnA);
299 }
300
301 static void r300BlendEquationSeparate(GLcontext * ctx,
302 GLenum modeRGB, GLenum modeA)
303 {
304 r300SetBlendState(ctx);
305 }
306
307 static void r300BlendFuncSeparate(GLcontext * ctx,
308 GLenum sfactorRGB, GLenum dfactorRGB,
309 GLenum sfactorA, GLenum dfactorA)
310 {
311 r300SetBlendState(ctx);
312 }
313
314 static void r300ClipPlane( GLcontext *ctx, GLenum plane, const GLfloat *eq )
315 {
316 r300ContextPtr rmesa = R300_CONTEXT(ctx);
317 GLint p;
318 GLint *ip;
319
320 /* no VAP UCP on non-TCL chipsets */
321 if (!(rmesa->radeon.radeonScreen->chip_flags & RADEON_CHIPSET_TCL))
322 return;
323
324 p = (GLint) plane - (GLint) GL_CLIP_PLANE0;
325 ip = (GLint *)ctx->Transform._ClipUserPlane[p];
326
327 R300_STATECHANGE( rmesa, vpucp[p] );
328 rmesa->hw.vpucp[p].cmd[R300_VPUCP_X] = ip[0];
329 rmesa->hw.vpucp[p].cmd[R300_VPUCP_Y] = ip[1];
330 rmesa->hw.vpucp[p].cmd[R300_VPUCP_Z] = ip[2];
331 rmesa->hw.vpucp[p].cmd[R300_VPUCP_W] = ip[3];
332 }
333
334 static void r300SetClipPlaneState(GLcontext * ctx, GLenum cap, GLboolean state)
335 {
336 r300ContextPtr r300 = R300_CONTEXT(ctx);
337 GLuint p;
338
339 /* no VAP UCP on non-TCL chipsets */
340 if (!(r300->radeon.radeonScreen->chip_flags & RADEON_CHIPSET_TCL))
341 return;
342
343 p = cap - GL_CLIP_PLANE0;
344 R300_STATECHANGE(r300, vap_clip_cntl);
345 if (state) {
346 r300->hw.vap_clip_cntl.cmd[1] |= (R300_VAP_UCP_ENABLE_0 << p);
347 r300ClipPlane(ctx, cap, NULL);
348 } else {
349 r300->hw.vap_clip_cntl.cmd[1] &= ~(R300_VAP_UCP_ENABLE_0 << p);
350 }
351 }
352
353 /**
354 * Update our tracked culling state based on Mesa's state.
355 */
356 static void r300UpdateCulling(GLcontext * ctx)
357 {
358 r300ContextPtr r300 = R300_CONTEXT(ctx);
359 uint32_t val = 0;
360
361 if (ctx->Polygon.CullFlag) {
362 switch (ctx->Polygon.CullFaceMode) {
363 case GL_FRONT:
364 val = R300_CULL_FRONT;
365 break;
366 case GL_BACK:
367 val = R300_CULL_BACK;
368 break;
369 case GL_FRONT_AND_BACK:
370 val = R300_CULL_FRONT | R300_CULL_BACK;
371 break;
372 default:
373 break;
374 }
375 }
376
377 switch (ctx->Polygon.FrontFace) {
378 case GL_CW:
379 val |= R300_FRONT_FACE_CW;
380 break;
381 case GL_CCW:
382 val |= R300_FRONT_FACE_CCW;
383 break;
384 default:
385 break;
386 }
387
388 R300_STATECHANGE(r300, cul);
389 r300->hw.cul.cmd[R300_CUL_CULL] = val;
390 }
391
392 static void r300SetPolygonOffsetState(GLcontext * ctx, GLboolean state)
393 {
394 r300ContextPtr r300 = R300_CONTEXT(ctx);
395
396 R300_STATECHANGE(r300, occlusion_cntl);
397 if (state) {
398 r300->hw.occlusion_cntl.cmd[1] |= (3 << 0);
399 } else {
400 r300->hw.occlusion_cntl.cmd[1] &= ~(3 << 0);
401 }
402 }
403
404 static void r300SetEarlyZState(GLcontext * ctx)
405 {
406 /* updates register R300_RB3D_EARLY_Z (0x4F14)
407 if depth test is not enabled it should be R300_EARLY_Z_DISABLE
408 if depth is enabled and alpha not it should be R300_EARLY_Z_ENABLE
409 if depth and alpha is enabled it should be R300_EARLY_Z_DISABLE
410 */
411 r300ContextPtr r300 = R300_CONTEXT(ctx);
412
413 R300_STATECHANGE(r300, zstencil_format);
414 switch (ctx->Visual.depthBits) {
415 case 16:
416 r300->hw.zstencil_format.cmd[1] = ZB_FORMAR_DEPTHFORMAT_16BIT_INT_Z;
417 break;
418 case 24:
419 r300->hw.zstencil_format.cmd[1] = ZB_FORMAR_DEPTHFORMAT_24BIT_INT_Z;
420 break;
421 default:
422 fprintf(stderr, "Error: Unsupported depth %d... exiting\n", ctx->Visual.depthBits);
423 _mesa_exit(-1);
424 }
425
426 if (ctx->Color.AlphaEnabled && ctx->Color.AlphaFunc != GL_ALWAYS)
427 /* disable early Z */
428 r300->hw.zstencil_format.cmd[2] = R300_EARLY_Z_DISABLE;
429 else {
430 if (ctx->Depth.Test && ctx->Depth.Func != GL_NEVER)
431 /* enable early Z */
432 r300->hw.zstencil_format.cmd[2] = R300_EARLY_Z_ENABLE;
433 else
434 /* disable early Z */
435 r300->hw.zstencil_format.cmd[2] = R300_EARLY_Z_DISABLE;
436 }
437
438 r300->hw.zstencil_format.cmd[3] = 0x00000003;
439 r300->hw.zstencil_format.cmd[4] = 0x00000000;
440 }
441
442 static void r300SetAlphaState(GLcontext * ctx)
443 {
444 r300ContextPtr r300 = R300_CONTEXT(ctx);
445 GLubyte refByte;
446 uint32_t pp_misc = 0x0;
447 GLboolean really_enabled = ctx->Color.AlphaEnabled;
448
449 CLAMPED_FLOAT_TO_UBYTE(refByte, ctx->Color.AlphaRef);
450
451 switch (ctx->Color.AlphaFunc) {
452 case GL_NEVER:
453 pp_misc |= FG_ALPHA_FUNC_NEVER;
454 break;
455 case GL_LESS:
456 pp_misc |= FG_ALPHA_FUNC_LESS;
457 break;
458 case GL_EQUAL:
459 pp_misc |= FG_ALPHA_FUNC_EQUAL;
460 break;
461 case GL_LEQUAL:
462 pp_misc |= FG_ALPHA_FUNC_LE;
463 break;
464 case GL_GREATER:
465 pp_misc |= FG_ALPHA_FUNC_GREATER;
466 break;
467 case GL_NOTEQUAL:
468 pp_misc |= FG_ALPHA_FUNC_NOTEQUAL;
469 break;
470 case GL_GEQUAL:
471 pp_misc |= FG_ALPHA_FUNC_GE;
472 break;
473 case GL_ALWAYS:
474 /*pp_misc |= FG_ALPHA_FUNC_ALWAYS; */
475 really_enabled = GL_FALSE;
476 break;
477 }
478
479 if (really_enabled) {
480 pp_misc |= FG_ALPHA_FUNC_ENABLE;
481 pp_misc |= (refByte & R300_REF_ALPHA_MASK);
482 } else {
483 pp_misc = 0x0;
484 }
485
486 R300_STATECHANGE(r300, at);
487 r300->hw.at.cmd[R300_AT_ALPHA_TEST] = pp_misc;
488 r300->hw.at.cmd[R300_AT_UNKNOWN] = 0;
489
490 r300SetEarlyZState(ctx);
491 }
492
493 static void r300AlphaFunc(GLcontext * ctx, GLenum func, GLfloat ref)
494 {
495 (void)func;
496 (void)ref;
497 r300SetAlphaState(ctx);
498 }
499
500 static int translate_func(int func)
501 {
502 switch (func) {
503 case GL_NEVER:
504 return R300_ZS_NEVER;
505 case GL_LESS:
506 return R300_ZS_LESS;
507 case GL_EQUAL:
508 return R300_ZS_EQUAL;
509 case GL_LEQUAL:
510 return R300_ZS_LEQUAL;
511 case GL_GREATER:
512 return R300_ZS_GREATER;
513 case GL_NOTEQUAL:
514 return R300_ZS_NOTEQUAL;
515 case GL_GEQUAL:
516 return R300_ZS_GEQUAL;
517 case GL_ALWAYS:
518 return R300_ZS_ALWAYS;
519 }
520 return 0;
521 }
522
523 static void r300SetDepthState(GLcontext * ctx)
524 {
525 r300ContextPtr r300 = R300_CONTEXT(ctx);
526
527 R300_STATECHANGE(r300, zs);
528 r300->hw.zs.cmd[R300_ZS_CNTL_0] &= R300_RB3D_STENCIL_ENABLE;
529 r300->hw.zs.cmd[R300_ZS_CNTL_1] &=
530 ~(R300_ZS_MASK << R300_RB3D_ZS1_DEPTH_FUNC_SHIFT);
531
532 if (ctx->Depth.Test && ctx->Depth.Func != GL_NEVER) {
533 if (ctx->Depth.Mask)
534 r300->hw.zs.cmd[R300_ZS_CNTL_0] |=
535 R300_RB3D_Z_TEST_AND_WRITE;
536 else
537 r300->hw.zs.cmd[R300_ZS_CNTL_0] |= R300_RB3D_Z_TEST;
538
539 r300->hw.zs.cmd[R300_ZS_CNTL_1] |=
540 translate_func(ctx->Depth.
541 Func) << R300_RB3D_ZS1_DEPTH_FUNC_SHIFT;
542 } else {
543 r300->hw.zs.cmd[R300_ZS_CNTL_0] |= R300_RB3D_Z_DISABLED_1;
544 r300->hw.zs.cmd[R300_ZS_CNTL_1] |=
545 translate_func(GL_NEVER) << R300_RB3D_ZS1_DEPTH_FUNC_SHIFT;
546 }
547
548 r300SetEarlyZState(ctx);
549 }
550
551 static void r300SetStencilState(GLcontext * ctx, GLboolean state)
552 {
553 r300ContextPtr r300 = R300_CONTEXT(ctx);
554
555 if (r300->state.stencil.hw_stencil) {
556 R300_STATECHANGE(r300, zs);
557 if (state) {
558 r300->hw.zs.cmd[R300_ZS_CNTL_0] |=
559 R300_RB3D_STENCIL_ENABLE;
560 } else {
561 r300->hw.zs.cmd[R300_ZS_CNTL_0] &=
562 ~R300_RB3D_STENCIL_ENABLE;
563 }
564 } else {
565 #if R200_MERGED
566 FALLBACK(&r300->radeon, RADEON_FALLBACK_STENCIL, state);
567 #endif
568 }
569 }
570
571 static void r300UpdatePolygonMode(GLcontext * ctx)
572 {
573 r300ContextPtr r300 = R300_CONTEXT(ctx);
574 uint32_t hw_mode = GA_POLY_MODE_DISABLE;
575
576 /* Only do something if a polygon mode is wanted, default is GL_FILL */
577 if (ctx->Polygon.FrontMode != GL_FILL ||
578 ctx->Polygon.BackMode != GL_FILL) {
579 GLenum f, b;
580
581 /* Handle GL_CW (clock wise and GL_CCW (counter clock wise)
582 * correctly by selecting the correct front and back face
583 */
584 if (ctx->Polygon.FrontFace == GL_CCW) {
585 f = ctx->Polygon.FrontMode;
586 b = ctx->Polygon.BackMode;
587 } else {
588 f = ctx->Polygon.BackMode;
589 b = ctx->Polygon.FrontMode;
590 }
591
592 /* Enable polygon mode */
593 hw_mode |= GA_POLY_MODE_DUAL;
594
595 switch (f) {
596 case GL_LINE:
597 hw_mode |= GA_POLY_MODE_FRONT_PTYPE_LINE;
598 break;
599 case GL_POINT:
600 hw_mode |= GA_POLY_MODE_FRONT_PTYPE_POINT;
601 break;
602 case GL_FILL:
603 hw_mode |= GA_POLY_MODE_FRONT_PTYPE_TRI;
604 break;
605 }
606
607 switch (b) {
608 case GL_LINE:
609 hw_mode |= GA_POLY_MODE_BACK_PTYPE_LINE;
610 break;
611 case GL_POINT:
612 hw_mode |= GA_POLY_MODE_BACK_PTYPE_POINT;
613 break;
614 case GL_FILL:
615 hw_mode |= GA_POLY_MODE_BACK_PTYPE_TRI;
616 break;
617 }
618 }
619
620 if (r300->hw.polygon_mode.cmd[1] != hw_mode) {
621 R300_STATECHANGE(r300, polygon_mode);
622 r300->hw.polygon_mode.cmd[1] = hw_mode;
623 }
624
625 r300->hw.polygon_mode.cmd[2] = 0x00000001;
626 r300->hw.polygon_mode.cmd[3] = 0x00000000;
627 }
628
629 /**
630 * Change the culling mode.
631 *
632 * \note Mesa already filters redundant calls to this function.
633 */
634 static void r300CullFace(GLcontext * ctx, GLenum mode)
635 {
636 (void)mode;
637
638 r300UpdateCulling(ctx);
639 }
640
641 /**
642 * Change the polygon orientation.
643 *
644 * \note Mesa already filters redundant calls to this function.
645 */
646 static void r300FrontFace(GLcontext * ctx, GLenum mode)
647 {
648 (void)mode;
649
650 r300UpdateCulling(ctx);
651 r300UpdatePolygonMode(ctx);
652 }
653
654 /**
655 * Change the depth testing function.
656 *
657 * \note Mesa already filters redundant calls to this function.
658 */
659 static void r300DepthFunc(GLcontext * ctx, GLenum func)
660 {
661 (void)func;
662 r300SetDepthState(ctx);
663 }
664
665 /**
666 * Enable/Disable depth writing.
667 *
668 * \note Mesa already filters redundant calls to this function.
669 */
670 static void r300DepthMask(GLcontext * ctx, GLboolean mask)
671 {
672 (void)mask;
673 r300SetDepthState(ctx);
674 }
675
676 /**
677 * Handle glColorMask()
678 */
679 static void r300ColorMask(GLcontext * ctx,
680 GLboolean r, GLboolean g, GLboolean b, GLboolean a)
681 {
682 r300ContextPtr r300 = R300_CONTEXT(ctx);
683 int mask = (r ? RB3D_COLOR_CHANNEL_MASK_RED_MASK0 : 0) |
684 (g ? RB3D_COLOR_CHANNEL_MASK_GREEN_MASK0 : 0) |
685 (b ? RB3D_COLOR_CHANNEL_MASK_BLUE_MASK0 : 0) |
686 (a ? RB3D_COLOR_CHANNEL_MASK_ALPHA_MASK0 : 0);
687
688 if (mask != r300->hw.cmk.cmd[R300_CMK_COLORMASK]) {
689 R300_STATECHANGE(r300, cmk);
690 r300->hw.cmk.cmd[R300_CMK_COLORMASK] = mask;
691 }
692 }
693
694 /* =============================================================
695 * Fog
696 */
697 static void r300Fogfv(GLcontext * ctx, GLenum pname, const GLfloat * param)
698 {
699 r300ContextPtr r300 = R300_CONTEXT(ctx);
700 union {
701 int i;
702 float f;
703 } fogScale, fogStart;
704
705 (void)param;
706
707 fogScale.i = r300->hw.fogp.cmd[R300_FOGP_SCALE];
708 fogStart.i = r300->hw.fogp.cmd[R300_FOGP_START];
709
710 switch (pname) {
711 case GL_FOG_MODE:
712 if (!ctx->Fog.Enabled)
713 return;
714 switch (ctx->Fog.Mode) {
715 case GL_LINEAR:
716 R300_STATECHANGE(r300, fogs);
717 r300->hw.fogs.cmd[R300_FOGS_STATE] =
718 (r300->hw.fogs.
719 cmd[R300_FOGS_STATE] & ~FG_FOG_BLEND_FN_MASK) |
720 FG_FOG_BLEND_FN_LINEAR;
721
722 if (ctx->Fog.Start == ctx->Fog.End) {
723 fogScale.f = -1.0;
724 fogStart.f = 1.0;
725 } else {
726 fogScale.f =
727 1.0 / (ctx->Fog.End - ctx->Fog.Start);
728 fogStart.f =
729 -ctx->Fog.Start / (ctx->Fog.End -
730 ctx->Fog.Start);
731 }
732 break;
733 case GL_EXP:
734 R300_STATECHANGE(r300, fogs);
735 r300->hw.fogs.cmd[R300_FOGS_STATE] =
736 (r300->hw.fogs.
737 cmd[R300_FOGS_STATE] & ~FG_FOG_BLEND_FN_MASK) |
738 FG_FOG_BLEND_FN_EXP;
739 fogScale.f = 0.0933 * ctx->Fog.Density;
740 fogStart.f = 0.0;
741 break;
742 case GL_EXP2:
743 R300_STATECHANGE(r300, fogs);
744 r300->hw.fogs.cmd[R300_FOGS_STATE] =
745 (r300->hw.fogs.
746 cmd[R300_FOGS_STATE] & ~FG_FOG_BLEND_FN_MASK) |
747 FG_FOG_BLEND_FN_EXP2;
748 fogScale.f = 0.3 * ctx->Fog.Density;
749 fogStart.f = 0.0;
750 default:
751 return;
752 }
753 break;
754 case GL_FOG_DENSITY:
755 switch (ctx->Fog.Mode) {
756 case GL_EXP:
757 fogScale.f = 0.0933 * ctx->Fog.Density;
758 fogStart.f = 0.0;
759 break;
760 case GL_EXP2:
761 fogScale.f = 0.3 * ctx->Fog.Density;
762 fogStart.f = 0.0;
763 default:
764 break;
765 }
766 break;
767 case GL_FOG_START:
768 case GL_FOG_END:
769 if (ctx->Fog.Mode == GL_LINEAR) {
770 if (ctx->Fog.Start == ctx->Fog.End) {
771 fogScale.f = -1.0;
772 fogStart.f = 1.0;
773 } else {
774 fogScale.f =
775 1.0 / (ctx->Fog.End - ctx->Fog.Start);
776 fogStart.f =
777 -ctx->Fog.Start / (ctx->Fog.End -
778 ctx->Fog.Start);
779 }
780 }
781 break;
782 case GL_FOG_COLOR:
783 R300_STATECHANGE(r300, fogc);
784 r300->hw.fogc.cmd[R300_FOGC_R] =
785 (GLuint) (ctx->Fog.Color[0] * 1023.0F) & 0x3FF;
786 r300->hw.fogc.cmd[R300_FOGC_G] =
787 (GLuint) (ctx->Fog.Color[1] * 1023.0F) & 0x3FF;
788 r300->hw.fogc.cmd[R300_FOGC_B] =
789 (GLuint) (ctx->Fog.Color[2] * 1023.0F) & 0x3FF;
790 break;
791 case GL_FOG_COORD_SRC:
792 break;
793 default:
794 return;
795 }
796
797 if (fogScale.i != r300->hw.fogp.cmd[R300_FOGP_SCALE] ||
798 fogStart.i != r300->hw.fogp.cmd[R300_FOGP_START]) {
799 R300_STATECHANGE(r300, fogp);
800 r300->hw.fogp.cmd[R300_FOGP_SCALE] = fogScale.i;
801 r300->hw.fogp.cmd[R300_FOGP_START] = fogStart.i;
802 }
803 }
804
805 static void r300SetFogState(GLcontext * ctx, GLboolean state)
806 {
807 r300ContextPtr r300 = R300_CONTEXT(ctx);
808
809 R300_STATECHANGE(r300, fogs);
810 if (state) {
811 r300->hw.fogs.cmd[R300_FOGS_STATE] |= FG_FOG_BLEND_ENABLE;
812
813 r300Fogfv(ctx, GL_FOG_MODE, NULL);
814 r300Fogfv(ctx, GL_FOG_DENSITY, &ctx->Fog.Density);
815 r300Fogfv(ctx, GL_FOG_START, &ctx->Fog.Start);
816 r300Fogfv(ctx, GL_FOG_END, &ctx->Fog.End);
817 r300Fogfv(ctx, GL_FOG_COLOR, ctx->Fog.Color);
818 } else {
819 r300->hw.fogs.cmd[R300_FOGS_STATE] &= ~FG_FOG_BLEND_ENABLE;
820 }
821 }
822
823 /* =============================================================
824 * Point state
825 */
826 static void r300PointSize(GLcontext * ctx, GLfloat size)
827 {
828 r300ContextPtr r300 = R300_CONTEXT(ctx);
829 /* same size limits for AA, non-AA points */
830 size = CLAMP(size, ctx->Const.MinPointSize, ctx->Const.MaxPointSize);
831
832 R300_STATECHANGE(r300, ps);
833 r300->hw.ps.cmd[R300_PS_POINTSIZE] =
834 ((int)(size * 6) << R300_POINTSIZE_X_SHIFT) |
835 ((int)(size * 6) << R300_POINTSIZE_Y_SHIFT);
836 }
837
838 /* =============================================================
839 * Line state
840 */
841 static void r300LineWidth(GLcontext * ctx, GLfloat widthf)
842 {
843 r300ContextPtr r300 = R300_CONTEXT(ctx);
844
845 widthf = CLAMP(widthf,
846 ctx->Const.MinPointSize,
847 ctx->Const.MaxPointSize);
848 R300_STATECHANGE(r300, lcntl);
849 r300->hw.lcntl.cmd[1] =
850 R300_LINE_CNT_HO | R300_LINE_CNT_VE | (int)(widthf * 6.0);
851 }
852
853 static void r300PolygonMode(GLcontext * ctx, GLenum face, GLenum mode)
854 {
855 (void)face;
856 (void)mode;
857
858 r300UpdatePolygonMode(ctx);
859 }
860
861 /* =============================================================
862 * Stencil
863 */
864
865 static int translate_stencil_op(int op)
866 {
867 switch (op) {
868 case GL_KEEP:
869 return R300_ZS_KEEP;
870 case GL_ZERO:
871 return R300_ZS_ZERO;
872 case GL_REPLACE:
873 return R300_ZS_REPLACE;
874 case GL_INCR:
875 return R300_ZS_INCR;
876 case GL_DECR:
877 return R300_ZS_DECR;
878 case GL_INCR_WRAP_EXT:
879 return R300_ZS_INCR_WRAP;
880 case GL_DECR_WRAP_EXT:
881 return R300_ZS_DECR_WRAP;
882 case GL_INVERT:
883 return R300_ZS_INVERT;
884 default:
885 WARN_ONCE("Do not know how to translate stencil op");
886 return R300_ZS_KEEP;
887 }
888 return 0;
889 }
890
891 static void r300ShadeModel(GLcontext * ctx, GLenum mode)
892 {
893 r300ContextPtr rmesa = R300_CONTEXT(ctx);
894
895 R300_STATECHANGE(rmesa, shade);
896 rmesa->hw.shade.cmd[1] = 0x00000002;
897 switch (mode) {
898 case GL_FLAT:
899 rmesa->hw.shade.cmd[2] = R300_RE_SHADE_MODEL_FLAT;
900 break;
901 case GL_SMOOTH:
902 rmesa->hw.shade.cmd[2] = R300_RE_SHADE_MODEL_SMOOTH;
903 break;
904 default:
905 return;
906 }
907 rmesa->hw.shade.cmd[3] = 0x00000000;
908 rmesa->hw.shade.cmd[4] = 0x00000000;
909 }
910
911 static void r300StencilFuncSeparate(GLcontext * ctx, GLenum face,
912 GLenum func, GLint ref, GLuint mask)
913 {
914 r300ContextPtr rmesa = R300_CONTEXT(ctx);
915 GLuint refmask =
916 (((ctx->Stencil.
917 Ref[0] & 0xff) << ZB_STENCILREFMASK_STENCILREF_SHIFT) | ((ctx->
918 Stencil.
919 ValueMask
920 [0] &
921 0xff)
922 <<
923 ZB_STENCILREFMASK_STENCILMASK_SHIFT));
924
925 GLuint flag;
926
927 R300_STATECHANGE(rmesa, zs);
928
929 rmesa->hw.zs.cmd[R300_ZS_CNTL_1] &= ~((R300_ZS_MASK <<
930 R300_RB3D_ZS1_FRONT_FUNC_SHIFT)
931 | (R300_ZS_MASK <<
932 R300_RB3D_ZS1_BACK_FUNC_SHIFT));
933
934 rmesa->hw.zs.cmd[R300_ZS_CNTL_2] &=
935 ~((ZB_STENCILREFMASK_STENCIL_MASK << ZB_STENCILREFMASK_STENCILREF_SHIFT) |
936 (ZB_STENCILREFMASK_STENCIL_MASK << ZB_STENCILREFMASK_STENCILMASK_SHIFT));
937
938 flag = translate_func(ctx->Stencil.Function[0]);
939 rmesa->hw.zs.cmd[R300_ZS_CNTL_1] |=
940 (flag << R300_RB3D_ZS1_FRONT_FUNC_SHIFT);
941
942 if (ctx->Stencil._TestTwoSide)
943 flag = translate_func(ctx->Stencil.Function[1]);
944
945 rmesa->hw.zs.cmd[R300_ZS_CNTL_1] |=
946 (flag << R300_RB3D_ZS1_BACK_FUNC_SHIFT);
947 rmesa->hw.zs.cmd[R300_ZS_CNTL_2] |= refmask;
948 }
949
950 static void r300StencilMaskSeparate(GLcontext * ctx, GLenum face, GLuint mask)
951 {
952 r300ContextPtr rmesa = R300_CONTEXT(ctx);
953
954 R300_STATECHANGE(rmesa, zs);
955 rmesa->hw.zs.cmd[R300_ZS_CNTL_2] &=
956 ~(ZB_STENCILREFMASK_STENCIL_MASK <<
957 ZB_STENCILREFMASK_STENCILWRITEMASK_SHIFT);
958 rmesa->hw.zs.cmd[R300_ZS_CNTL_2] |=
959 (ctx->Stencil.
960 WriteMask[0] & ZB_STENCILREFMASK_STENCIL_MASK) <<
961 ZB_STENCILREFMASK_STENCILWRITEMASK_SHIFT;
962 }
963
964 static void r300StencilOpSeparate(GLcontext * ctx, GLenum face,
965 GLenum fail, GLenum zfail, GLenum zpass)
966 {
967 r300ContextPtr rmesa = R300_CONTEXT(ctx);
968
969 R300_STATECHANGE(rmesa, zs);
970 /* It is easier to mask what's left.. */
971 rmesa->hw.zs.cmd[R300_ZS_CNTL_1] &=
972 (R300_ZS_MASK << R300_RB3D_ZS1_DEPTH_FUNC_SHIFT) |
973 (R300_ZS_MASK << R300_RB3D_ZS1_FRONT_FUNC_SHIFT) |
974 (R300_ZS_MASK << R300_RB3D_ZS1_BACK_FUNC_SHIFT);
975
976 rmesa->hw.zs.cmd[R300_ZS_CNTL_1] |=
977 (translate_stencil_op(ctx->Stencil.FailFunc[0]) <<
978 R300_RB3D_ZS1_FRONT_FAIL_OP_SHIFT)
979 | (translate_stencil_op(ctx->Stencil.ZFailFunc[0]) <<
980 R300_RB3D_ZS1_FRONT_ZFAIL_OP_SHIFT)
981 | (translate_stencil_op(ctx->Stencil.ZPassFunc[0]) <<
982 R300_RB3D_ZS1_FRONT_ZPASS_OP_SHIFT);
983
984 if (ctx->Stencil._TestTwoSide) {
985 rmesa->hw.zs.cmd[R300_ZS_CNTL_1] |=
986 (translate_stencil_op(ctx->Stencil.FailFunc[1]) <<
987 R300_RB3D_ZS1_BACK_FAIL_OP_SHIFT)
988 | (translate_stencil_op(ctx->Stencil.ZFailFunc[1]) <<
989 R300_RB3D_ZS1_BACK_ZFAIL_OP_SHIFT)
990 | (translate_stencil_op(ctx->Stencil.ZPassFunc[1]) <<
991 R300_RB3D_ZS1_BACK_ZPASS_OP_SHIFT);
992 } else {
993 rmesa->hw.zs.cmd[R300_ZS_CNTL_1] |=
994 (translate_stencil_op(ctx->Stencil.FailFunc[0]) <<
995 R300_RB3D_ZS1_BACK_FAIL_OP_SHIFT)
996 | (translate_stencil_op(ctx->Stencil.ZFailFunc[0]) <<
997 R300_RB3D_ZS1_BACK_ZFAIL_OP_SHIFT)
998 | (translate_stencil_op(ctx->Stencil.ZPassFunc[0]) <<
999 R300_RB3D_ZS1_BACK_ZPASS_OP_SHIFT);
1000 }
1001 }
1002
1003 static void r300ClearStencil(GLcontext * ctx, GLint s)
1004 {
1005 r300ContextPtr rmesa = R300_CONTEXT(ctx);
1006
1007 rmesa->state.stencil.clear =
1008 ((GLuint) (ctx->Stencil.Clear & ZB_STENCILREFMASK_STENCIL_MASK) |
1009 (ZB_STENCILREFMASK_STENCIL_MASK << ZB_STENCILREFMASK_STENCILMASK_SHIFT) |
1010 ((ctx->Stencil.WriteMask[0] & ZB_STENCILREFMASK_STENCIL_MASK) <<
1011 ZB_STENCILREFMASK_STENCILMASK_SHIFT));
1012 }
1013
1014 /* =============================================================
1015 * Window position and viewport transformation
1016 */
1017
1018 /*
1019 * To correctly position primitives:
1020 */
1021 #define SUBPIXEL_X 0.125
1022 #define SUBPIXEL_Y 0.125
1023
1024 static void r300UpdateWindow(GLcontext * ctx)
1025 {
1026 r300ContextPtr rmesa = R300_CONTEXT(ctx);
1027 __DRIdrawablePrivate *dPriv = rmesa->radeon.dri.drawable;
1028 GLfloat xoffset = dPriv ? (GLfloat) dPriv->x : 0;
1029 GLfloat yoffset = dPriv ? (GLfloat) dPriv->y + dPriv->h : 0;
1030 const GLfloat *v = ctx->Viewport._WindowMap.m;
1031
1032 GLfloat sx = v[MAT_SX];
1033 GLfloat tx = v[MAT_TX] + xoffset + SUBPIXEL_X;
1034 GLfloat sy = -v[MAT_SY];
1035 GLfloat ty = (-v[MAT_TY]) + yoffset + SUBPIXEL_Y;
1036 GLfloat sz = v[MAT_SZ] * rmesa->state.depth.scale;
1037 GLfloat tz = v[MAT_TZ] * rmesa->state.depth.scale;
1038
1039 R300_FIREVERTICES(rmesa);
1040 R300_STATECHANGE(rmesa, vpt);
1041
1042 rmesa->hw.vpt.cmd[R300_VPT_XSCALE] = r300PackFloat32(sx);
1043 rmesa->hw.vpt.cmd[R300_VPT_XOFFSET] = r300PackFloat32(tx);
1044 rmesa->hw.vpt.cmd[R300_VPT_YSCALE] = r300PackFloat32(sy);
1045 rmesa->hw.vpt.cmd[R300_VPT_YOFFSET] = r300PackFloat32(ty);
1046 rmesa->hw.vpt.cmd[R300_VPT_ZSCALE] = r300PackFloat32(sz);
1047 rmesa->hw.vpt.cmd[R300_VPT_ZOFFSET] = r300PackFloat32(tz);
1048 }
1049
1050 static void r300Viewport(GLcontext * ctx, GLint x, GLint y,
1051 GLsizei width, GLsizei height)
1052 {
1053 /* Don't pipeline viewport changes, conflict with window offset
1054 * setting below. Could apply deltas to rescue pipelined viewport
1055 * values, or keep the originals hanging around.
1056 */
1057 r300UpdateWindow(ctx);
1058 }
1059
1060 static void r300DepthRange(GLcontext * ctx, GLclampd nearval, GLclampd farval)
1061 {
1062 r300UpdateWindow(ctx);
1063 }
1064
1065 void r300UpdateViewportOffset(GLcontext * ctx)
1066 {
1067 r300ContextPtr rmesa = R300_CONTEXT(ctx);
1068 __DRIdrawablePrivate *dPriv = ((radeonContextPtr) rmesa)->dri.drawable;
1069 GLfloat xoffset = (GLfloat) dPriv->x;
1070 GLfloat yoffset = (GLfloat) dPriv->y + dPriv->h;
1071 const GLfloat *v = ctx->Viewport._WindowMap.m;
1072
1073 GLfloat tx = v[MAT_TX] + xoffset + SUBPIXEL_X;
1074 GLfloat ty = (-v[MAT_TY]) + yoffset + SUBPIXEL_Y;
1075
1076 if (rmesa->hw.vpt.cmd[R300_VPT_XOFFSET] != r300PackFloat32(tx) ||
1077 rmesa->hw.vpt.cmd[R300_VPT_YOFFSET] != r300PackFloat32(ty)) {
1078 /* Note: this should also modify whatever data the context reset
1079 * code uses...
1080 */
1081 R300_STATECHANGE(rmesa, vpt);
1082 rmesa->hw.vpt.cmd[R300_VPT_XOFFSET] = r300PackFloat32(tx);
1083 rmesa->hw.vpt.cmd[R300_VPT_YOFFSET] = r300PackFloat32(ty);
1084
1085 }
1086
1087 radeonUpdateScissor(ctx);
1088 }
1089
1090 /**
1091 * Tell the card where to render (offset, pitch).
1092 * Effected by glDrawBuffer, etc
1093 */
1094 void r300UpdateDrawBuffer(GLcontext * ctx)
1095 {
1096 r300ContextPtr rmesa = R300_CONTEXT(ctx);
1097 r300ContextPtr r300 = rmesa;
1098 struct gl_framebuffer *fb = ctx->DrawBuffer;
1099 driRenderbuffer *drb;
1100
1101 if (fb->_ColorDrawBufferIndexes[0] == BUFFER_FRONT_LEFT) {
1102 /* draw to front */
1103 drb =
1104 (driRenderbuffer *) fb->Attachment[BUFFER_FRONT_LEFT].
1105 Renderbuffer;
1106 } else if (fb->_ColorDrawBufferIndexes[0] == BUFFER_BACK_LEFT) {
1107 /* draw to back */
1108 drb =
1109 (driRenderbuffer *) fb->Attachment[BUFFER_BACK_LEFT].
1110 Renderbuffer;
1111 } else {
1112 /* drawing to multiple buffers, or none */
1113 return;
1114 }
1115
1116 assert(drb);
1117 assert(drb->flippedPitch);
1118
1119 R300_STATECHANGE(rmesa, cb);
1120
1121 r300->hw.cb.cmd[R300_CB_OFFSET] = drb->flippedOffset + //r300->radeon.state.color.drawOffset +
1122 r300->radeon.radeonScreen->fbLocation;
1123 r300->hw.cb.cmd[R300_CB_PITCH] = drb->flippedPitch; //r300->radeon.state.color.drawPitch;
1124
1125 if (r300->radeon.radeonScreen->cpp == 4)
1126 r300->hw.cb.cmd[R300_CB_PITCH] |= R300_COLOR_FORMAT_ARGB8888;
1127 else
1128 r300->hw.cb.cmd[R300_CB_PITCH] |= R300_COLOR_FORMAT_RGB565;
1129
1130 if (r300->radeon.sarea->tiling_enabled)
1131 r300->hw.cb.cmd[R300_CB_PITCH] |= R300_COLOR_TILE_ENABLE;
1132 #if 0
1133 R200_STATECHANGE(rmesa, ctx);
1134
1135 /* Note: we used the (possibly) page-flipped values */
1136 rmesa->hw.ctx.cmd[CTX_RB3D_COLOROFFSET]
1137 = ((drb->flippedOffset + rmesa->r200Screen->fbLocation)
1138 & R200_COLOROFFSET_MASK);
1139 rmesa->hw.ctx.cmd[CTX_RB3D_COLORPITCH] = drb->flippedPitch;
1140
1141 if (rmesa->sarea->tiling_enabled) {
1142 rmesa->hw.ctx.cmd[CTX_RB3D_COLORPITCH] |=
1143 R200_COLOR_TILE_ENABLE;
1144 }
1145 #endif
1146 }
1147
1148 static void
1149 r300FetchStateParameter(GLcontext * ctx,
1150 const gl_state_index state[STATE_LENGTH],
1151 GLfloat * value)
1152 {
1153 r300ContextPtr r300 = R300_CONTEXT(ctx);
1154
1155 switch (state[0]) {
1156 case STATE_INTERNAL:
1157 switch (state[1]) {
1158 case STATE_R300_WINDOW_DIMENSION:
1159 value[0] = r300->radeon.dri.drawable->w * 0.5f; /* width*0.5 */
1160 value[1] = r300->radeon.dri.drawable->h * 0.5f; /* height*0.5 */
1161 value[2] = 0.5F; /* for moving range [-1 1] -> [0 1] */
1162 value[3] = 1.0F; /* not used */
1163 break;
1164
1165 case STATE_R300_TEXRECT_FACTOR:{
1166 struct gl_texture_object *t =
1167 ctx->Texture.Unit[state[2]].CurrentRect;
1168
1169 if (t && t->Image[0][t->BaseLevel]) {
1170 struct gl_texture_image *image =
1171 t->Image[0][t->BaseLevel];
1172 value[0] = 1.0 / image->Width2;
1173 value[1] = 1.0 / image->Height2;
1174 } else {
1175 value[0] = 1.0;
1176 value[1] = 1.0;
1177 }
1178 value[2] = 1.0;
1179 value[3] = 1.0;
1180 break;
1181 }
1182
1183 default:
1184 break;
1185 }
1186 break;
1187
1188 default:
1189 break;
1190 }
1191 }
1192
1193 /**
1194 * Update R300's own internal state parameters.
1195 * For now just STATE_R300_WINDOW_DIMENSION
1196 */
1197 void r300UpdateStateParameters(GLcontext * ctx, GLuint new_state)
1198 {
1199 struct r300_fragment_program *fp;
1200 struct gl_program_parameter_list *paramList;
1201 GLuint i;
1202
1203 if (!(new_state & (_NEW_BUFFERS | _NEW_PROGRAM)))
1204 return;
1205
1206 fp = (struct r300_fragment_program *)ctx->FragmentProgram._Current;
1207 if (!fp)
1208 return;
1209
1210 paramList = fp->mesa_program.Base.Parameters;
1211
1212 if (!paramList)
1213 return;
1214
1215 for (i = 0; i < paramList->NumParameters; i++) {
1216 if (paramList->Parameters[i].Type == PROGRAM_STATE_VAR) {
1217 r300FetchStateParameter(ctx,
1218 paramList->Parameters[i].
1219 StateIndexes,
1220 paramList->ParameterValues[i]);
1221 }
1222 }
1223 }
1224
1225 /* =============================================================
1226 * Polygon state
1227 */
1228 static void r300PolygonOffset(GLcontext * ctx, GLfloat factor, GLfloat units)
1229 {
1230 r300ContextPtr rmesa = R300_CONTEXT(ctx);
1231 GLfloat constant = units;
1232
1233 switch (ctx->Visual.depthBits) {
1234 case 16:
1235 constant *= 4.0;
1236 break;
1237 case 24:
1238 constant *= 2.0;
1239 break;
1240 }
1241
1242 factor *= 12.0;
1243
1244 /* fprintf(stderr, "%s f:%f u:%f\n", __FUNCTION__, factor, constant); */
1245
1246 R300_STATECHANGE(rmesa, zbs);
1247 rmesa->hw.zbs.cmd[R300_ZBS_T_FACTOR] = r300PackFloat32(factor);
1248 rmesa->hw.zbs.cmd[R300_ZBS_T_CONSTANT] = r300PackFloat32(constant);
1249 rmesa->hw.zbs.cmd[R300_ZBS_W_FACTOR] = r300PackFloat32(factor);
1250 rmesa->hw.zbs.cmd[R300_ZBS_W_CONSTANT] = r300PackFloat32(constant);
1251 }
1252
1253 /* Routing and texture-related */
1254
1255 /* r300 doesnt handle GL_CLAMP and GL_MIRROR_CLAMP_EXT correctly when filter is NEAREST.
1256 * Since texwrap produces same results for GL_CLAMP and GL_CLAMP_TO_EDGE we use them instead.
1257 * We need to recalculate wrap modes whenever filter mode is changed because someone might do:
1258 * glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
1259 * glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
1260 * glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
1261 * Since r300 completely ignores R300_TX_CLAMP when either min or mag is nearest it cant handle
1262 * combinations where only one of them is nearest.
1263 */
1264 static unsigned long gen_fixed_filter(unsigned long f)
1265 {
1266 unsigned long mag, min, needs_fixing = 0;
1267 //return f;
1268
1269 /* We ignore MIRROR bit so we dont have to do everything twice */
1270 if ((f & ((7 - 1) << R300_TX_WRAP_S_SHIFT)) ==
1271 (R300_TX_CLAMP << R300_TX_WRAP_S_SHIFT)) {
1272 needs_fixing |= 1;
1273 }
1274 if ((f & ((7 - 1) << R300_TX_WRAP_T_SHIFT)) ==
1275 (R300_TX_CLAMP << R300_TX_WRAP_T_SHIFT)) {
1276 needs_fixing |= 2;
1277 }
1278 if ((f & ((7 - 1) << R300_TX_WRAP_Q_SHIFT)) ==
1279 (R300_TX_CLAMP << R300_TX_WRAP_Q_SHIFT)) {
1280 needs_fixing |= 4;
1281 }
1282
1283 if (!needs_fixing)
1284 return f;
1285
1286 mag = f & R300_TX_MAG_FILTER_MASK;
1287 min = f & R300_TX_MIN_FILTER_MASK;
1288
1289 /* TODO: Check for anisto filters too */
1290 if ((mag != R300_TX_MAG_FILTER_NEAREST)
1291 && (min != R300_TX_MIN_FILTER_NEAREST))
1292 return f;
1293
1294 /* r300 cant handle these modes hence we force nearest to linear */
1295 if ((mag == R300_TX_MAG_FILTER_NEAREST)
1296 && (min != R300_TX_MIN_FILTER_NEAREST)) {
1297 f &= ~R300_TX_MAG_FILTER_NEAREST;
1298 f |= R300_TX_MAG_FILTER_LINEAR;
1299 return f;
1300 }
1301
1302 if ((min == R300_TX_MIN_FILTER_NEAREST)
1303 && (mag != R300_TX_MAG_FILTER_NEAREST)) {
1304 f &= ~R300_TX_MIN_FILTER_NEAREST;
1305 f |= R300_TX_MIN_FILTER_LINEAR;
1306 return f;
1307 }
1308
1309 /* Both are nearest */
1310 if (needs_fixing & 1) {
1311 f &= ~((7 - 1) << R300_TX_WRAP_S_SHIFT);
1312 f |= R300_TX_CLAMP_TO_EDGE << R300_TX_WRAP_S_SHIFT;
1313 }
1314 if (needs_fixing & 2) {
1315 f &= ~((7 - 1) << R300_TX_WRAP_T_SHIFT);
1316 f |= R300_TX_CLAMP_TO_EDGE << R300_TX_WRAP_T_SHIFT;
1317 }
1318 if (needs_fixing & 4) {
1319 f &= ~((7 - 1) << R300_TX_WRAP_Q_SHIFT);
1320 f |= R300_TX_CLAMP_TO_EDGE << R300_TX_WRAP_Q_SHIFT;
1321 }
1322 return f;
1323 }
1324
1325 static void r300SetupTextures(GLcontext * ctx)
1326 {
1327 int i, mtu;
1328 struct r300_tex_obj *t;
1329 r300ContextPtr r300 = R300_CONTEXT(ctx);
1330 int hw_tmu = 0;
1331 int last_hw_tmu = -1; /* -1 translates into no setup costs for fields */
1332 int tmu_mappings[R300_MAX_TEXTURE_UNITS] = { -1, };
1333 struct r300_fragment_program *fp = (struct r300_fragment_program *)
1334 (char *)ctx->FragmentProgram._Current;
1335
1336 R300_STATECHANGE(r300, txe);
1337 R300_STATECHANGE(r300, tex.filter);
1338 R300_STATECHANGE(r300, tex.filter_1);
1339 R300_STATECHANGE(r300, tex.size);
1340 R300_STATECHANGE(r300, tex.format);
1341 R300_STATECHANGE(r300, tex.pitch);
1342 R300_STATECHANGE(r300, tex.offset);
1343 R300_STATECHANGE(r300, tex.chroma_key);
1344 R300_STATECHANGE(r300, tex.border_color);
1345
1346 r300->hw.txe.cmd[R300_TXE_ENABLE] = 0x0;
1347
1348 mtu = r300->radeon.glCtx->Const.MaxTextureUnits;
1349 if (RADEON_DEBUG & DEBUG_STATE)
1350 fprintf(stderr, "mtu=%d\n", mtu);
1351
1352 if (mtu > R300_MAX_TEXTURE_UNITS) {
1353 fprintf(stderr,
1354 "Aiiee ! mtu=%d is greater than R300_MAX_TEXTURE_UNITS=%d\n",
1355 mtu, R300_MAX_TEXTURE_UNITS);
1356 _mesa_exit(-1);
1357 }
1358
1359 /* We cannot let disabled tmu offsets pass DRM */
1360 for (i = 0; i < mtu; i++) {
1361 if (ctx->Texture.Unit[i]._ReallyEnabled) {
1362
1363 #if 0 /* Enables old behaviour */
1364 hw_tmu = i;
1365 #endif
1366 tmu_mappings[i] = hw_tmu;
1367
1368 t = r300->state.texture.unit[i].texobj;
1369 /* XXX questionable fix for bug 9170: */
1370 if (!t)
1371 continue;
1372
1373 if ((t->format & 0xffffff00) == 0xffffff00) {
1374 WARN_ONCE
1375 ("unknown texture format (entry %x) encountered. Help me !\n",
1376 t->format & 0xff);
1377 }
1378
1379 if (RADEON_DEBUG & DEBUG_STATE)
1380 fprintf(stderr,
1381 "Activating texture unit %d\n", i);
1382
1383 r300->hw.txe.cmd[R300_TXE_ENABLE] |= (1 << hw_tmu);
1384
1385 r300->hw.tex.filter.cmd[R300_TEX_VALUE_0 +
1386 hw_tmu] =
1387 gen_fixed_filter(t->filter) | (hw_tmu << 28);
1388 /* Currently disabled! */
1389 r300->hw.tex.filter_1.cmd[R300_TEX_VALUE_0 + hw_tmu] = 0x0; //0x20501f80;
1390 r300->hw.tex.size.cmd[R300_TEX_VALUE_0 + hw_tmu] =
1391 t->size;
1392 r300->hw.tex.format.cmd[R300_TEX_VALUE_0 +
1393 hw_tmu] = t->format;
1394 r300->hw.tex.pitch.cmd[R300_TEX_VALUE_0 + hw_tmu] =
1395 t->pitch_reg;
1396 r300->hw.tex.offset.cmd[R300_TEX_VALUE_0 +
1397 hw_tmu] = t->offset;
1398
1399 if (t->offset & R300_TXO_MACRO_TILE) {
1400 WARN_ONCE("macro tiling enabled!\n");
1401 }
1402
1403 if (t->offset & R300_TXO_MICRO_TILE) {
1404 WARN_ONCE("micro tiling enabled!\n");
1405 }
1406
1407 r300->hw.tex.chroma_key.cmd[R300_TEX_VALUE_0 +
1408 hw_tmu] = 0x0;
1409 r300->hw.tex.border_color.cmd[R300_TEX_VALUE_0 +
1410 hw_tmu] =
1411 t->pp_border_color;
1412
1413 last_hw_tmu = hw_tmu;
1414
1415 hw_tmu++;
1416 }
1417 }
1418
1419 r300->hw.tex.filter.cmd[R300_TEX_CMD_0] =
1420 cmdpacket0(R300_TX_FILTER0_0, last_hw_tmu + 1);
1421 r300->hw.tex.filter_1.cmd[R300_TEX_CMD_0] =
1422 cmdpacket0(R300_TX_FILTER1_0, last_hw_tmu + 1);
1423 r300->hw.tex.size.cmd[R300_TEX_CMD_0] =
1424 cmdpacket0(R300_TX_SIZE_0, last_hw_tmu + 1);
1425 r300->hw.tex.format.cmd[R300_TEX_CMD_0] =
1426 cmdpacket0(R300_TX_FORMAT_0, last_hw_tmu + 1);
1427 r300->hw.tex.pitch.cmd[R300_TEX_CMD_0] =
1428 cmdpacket0(R300_TX_FORMAT2_0, last_hw_tmu + 1);
1429 r300->hw.tex.offset.cmd[R300_TEX_CMD_0] =
1430 cmdpacket0(R300_TX_OFFSET_0, last_hw_tmu + 1);
1431 r300->hw.tex.chroma_key.cmd[R300_TEX_CMD_0] =
1432 cmdpacket0(R300_TX_CHROMA_KEY_0, last_hw_tmu + 1);
1433 r300->hw.tex.border_color.cmd[R300_TEX_CMD_0] =
1434 cmdpacket0(R300_TX_BORDER_COLOR_0, last_hw_tmu + 1);
1435
1436 if (!fp) /* should only happenen once, just after context is created */
1437 return;
1438
1439
1440 if (r300->radeon.radeonScreen->chip_family < CHIP_FAMILY_RV515) {
1441 R300_STATECHANGE(r300, fpt);
1442
1443 for (i = 0; i < fp->tex.length; i++) {
1444 int unit;
1445 int opcode;
1446 unsigned long val;
1447
1448 unit = fp->tex.inst[i] >> R300_FPITX_IMAGE_SHIFT;
1449 unit &= 15;
1450
1451 val = fp->tex.inst[i];
1452 val &= ~R300_FPITX_IMAGE_MASK;
1453
1454 opcode =
1455 (val & R300_FPITX_OPCODE_MASK) >> R300_FPITX_OPCODE_SHIFT;
1456 if (opcode == R300_FPITX_OP_KIL) {
1457 r300->hw.fpt.cmd[R300_FPT_INSTR_0 + i] = val;
1458 } else {
1459 if (tmu_mappings[unit] >= 0) {
1460 val |=
1461 tmu_mappings[unit] <<
1462 R300_FPITX_IMAGE_SHIFT;
1463 r300->hw.fpt.cmd[R300_FPT_INSTR_0 + i] = val;
1464 } else {
1465 // We get here when the corresponding texture image is incomplete
1466 // (e.g. incomplete mipmaps etc.)
1467 r300->hw.fpt.cmd[R300_FPT_INSTR_0 + i] = val;
1468 }
1469 }
1470 }
1471
1472 r300->hw.fpt.cmd[R300_FPT_CMD_0] =
1473 cmdpacket0(R300_PFS_TEXI_0, fp->tex.length);
1474 }
1475
1476 if (RADEON_DEBUG & DEBUG_STATE)
1477 fprintf(stderr, "TX_ENABLE: %08x last_hw_tmu=%d\n",
1478 r300->hw.txe.cmd[R300_TXE_ENABLE], last_hw_tmu);
1479 }
1480
1481 union r300_outputs_written {
1482 GLuint vp_outputs; /* hw_tcl_on */
1483 DECLARE_RENDERINPUTS(index_bitset); /* !hw_tcl_on */
1484 };
1485
1486 #define R300_OUTPUTS_WRITTEN_TEST(ow, vp_result, tnl_attrib) \
1487 ((hw_tcl_on) ? (ow).vp_outputs & (1 << (vp_result)) : \
1488 RENDERINPUTS_TEST( (ow.index_bitset), (tnl_attrib) ))
1489
1490 static void r300SetupRSUnit(GLcontext * ctx)
1491 {
1492 r300ContextPtr r300 = R300_CONTEXT(ctx);
1493 /* I'm still unsure if these are needed */
1494 GLuint interp_magic[8] = {
1495 0x00,
1496 R300_RS_COL_PTR(1),
1497 R300_RS_COL_PTR(2),
1498 R300_RS_COL_PTR(3),
1499 0x00,
1500 0x00,
1501 0x00,
1502 0x00
1503 };
1504 union r300_outputs_written OutputsWritten;
1505 GLuint InputsRead;
1506 int fp_reg, high_rr;
1507 int in_texcoords, col_interp_nr;
1508 int i;
1509
1510 if (hw_tcl_on)
1511 OutputsWritten.vp_outputs = CURRENT_VERTEX_SHADER(ctx)->key.OutputsWritten;
1512 else
1513 RENDERINPUTS_COPY(OutputsWritten.index_bitset, r300->state.render_inputs_bitset);
1514
1515 if (ctx->FragmentProgram._Current)
1516 InputsRead = ctx->FragmentProgram._Current->Base.InputsRead;
1517 else {
1518 fprintf(stderr, "No ctx->FragmentProgram._Current!!\n");
1519 return; /* This should only ever happen once.. */
1520 }
1521
1522 R300_STATECHANGE(r300, ri);
1523 R300_STATECHANGE(r300, rc);
1524 R300_STATECHANGE(r300, rr);
1525
1526 fp_reg = in_texcoords = col_interp_nr = high_rr = 0;
1527
1528 r300->hw.rr.cmd[R300_RR_INST_1] = 0;
1529
1530 if (InputsRead & FRAG_BIT_WPOS) {
1531 for (i = 0; i < ctx->Const.MaxTextureUnits; i++)
1532 if (!(InputsRead & (FRAG_BIT_TEX0 << i)))
1533 break;
1534
1535 if (i == ctx->Const.MaxTextureUnits) {
1536 fprintf(stderr, "\tno free texcoord found...\n");
1537 _mesa_exit(-1);
1538 }
1539
1540 InputsRead |= (FRAG_BIT_TEX0 << i);
1541 InputsRead &= ~FRAG_BIT_WPOS;
1542 }
1543
1544 for (i = 0; i < ctx->Const.MaxTextureUnits; i++) {
1545 r300->hw.ri.cmd[R300_RI_INTERP_0 + i] = 0 | R300_RS_SEL_T(1) | R300_RS_SEL_R(2) | R300_RS_SEL_Q(3) | (in_texcoords << R300_RS_INTERP_SRC_SHIFT)
1546 | interp_magic[i];
1547
1548 r300->hw.rr.cmd[R300_RR_INST_0 + fp_reg] = 0;
1549 if (InputsRead & (FRAG_BIT_TEX0 << i)) {
1550 //assert(r300->state.texture.tc_count != 0);
1551 r300->hw.rr.cmd[R300_RR_INST_0 + fp_reg] |= R300_RS_INST_TEX_CN_WRITE | i /* source INTERP */
1552 | (fp_reg << R300_RS_INST_TEX_ADDR_SHIFT);
1553 high_rr = fp_reg;
1554
1555 /* Passing invalid data here can lock the GPU. */
1556 if (R300_OUTPUTS_WRITTEN_TEST(OutputsWritten, VERT_RESULT_TEX0 + i, _TNL_ATTRIB_TEX(i))) {
1557 InputsRead &= ~(FRAG_BIT_TEX0 << i);
1558 fp_reg++;
1559 } else {
1560 WARN_ONCE("fragprog wants coords for tex%d, vp doesn't provide them!\n", i);
1561 }
1562 }
1563 /* Need to count all coords enabled at vof */
1564 if (R300_OUTPUTS_WRITTEN_TEST(OutputsWritten, VERT_RESULT_TEX0 + i, _TNL_ATTRIB_TEX(i))) {
1565 in_texcoords++;
1566 }
1567 }
1568
1569 if (InputsRead & FRAG_BIT_COL0) {
1570 if (R300_OUTPUTS_WRITTEN_TEST(OutputsWritten, VERT_RESULT_COL0, _TNL_ATTRIB_COLOR0)) {
1571 r300->hw.rr.cmd[R300_RR_INST_0] |= R300_RS_INST_COL_ID(0) | R300_RS_INST_COL_CN_WRITE | (fp_reg++ << R300_RS_INST_COL_ADDR_SHIFT);
1572 InputsRead &= ~FRAG_BIT_COL0;
1573 col_interp_nr++;
1574 } else {
1575 WARN_ONCE("fragprog wants col0, vp doesn't provide it\n");
1576 }
1577 }
1578
1579 if (InputsRead & FRAG_BIT_COL1) {
1580 if (R300_OUTPUTS_WRITTEN_TEST(OutputsWritten, VERT_RESULT_COL1, _TNL_ATTRIB_COLOR1)) {
1581 r300->hw.rr.cmd[R300_RR_INST_1] |= R300_RS_INST_COL_ID(1) | R300_RS_INST_COL_CN_WRITE | (fp_reg++ << R300_RS_INST_COL_ADDR_SHIFT);
1582 InputsRead &= ~FRAG_BIT_COL1;
1583 if (high_rr < 1)
1584 high_rr = 1;
1585 col_interp_nr++;
1586 } else {
1587 WARN_ONCE("fragprog wants col1, vp doesn't provide it\n");
1588 }
1589 }
1590
1591 /* Need at least one. This might still lock as the values are undefined... */
1592 if (in_texcoords == 0 && col_interp_nr == 0) {
1593 r300->hw.rr.cmd[R300_RR_INST_0] |= R300_RS_INST_COL_ID(0) | R300_RS_INST_COL_CN_WRITE | (fp_reg++ << R300_RS_INST_COL_ADDR_SHIFT);
1594 col_interp_nr++;
1595 }
1596
1597 r300->hw.rc.cmd[1] = 0 | ((in_texcoords << 2) << R300_IT_COUNT_SHIFT)
1598 | (col_interp_nr << R300_IC_COUNT_SHIFT)
1599 | R300_HIRES_EN;
1600
1601 assert(high_rr >= 0);
1602 r300->hw.rr.cmd[R300_RR_CMD_0] = cmdpacket0(R300_RS_INST_0, high_rr + 1);
1603 r300->hw.rc.cmd[2] = 0xC0 | high_rr;
1604
1605 if (InputsRead)
1606 WARN_ONCE("Don't know how to satisfy InputsRead=0x%08x\n", InputsRead);
1607 }
1608
1609 static void r500SetupRSUnit(GLcontext * ctx)
1610 {
1611 r300ContextPtr r300 = R300_CONTEXT(ctx);
1612 /* I'm still unsure if these are needed */
1613 GLuint interp_magic[8] = {
1614 0x00,
1615 1 << 24,
1616 2 << 24,
1617 3 << 24,
1618 0x00,
1619 0x00,
1620 0x00,
1621 0x00
1622 };
1623 union r300_outputs_written OutputsWritten;
1624 GLuint InputsRead;
1625 int fp_reg, high_rr;
1626 int in_texcoords, col_interp_nr;
1627 int i;
1628
1629 if (hw_tcl_on)
1630 OutputsWritten.vp_outputs = CURRENT_VERTEX_SHADER(ctx)->key.OutputsWritten;
1631 else
1632 RENDERINPUTS_COPY(OutputsWritten.index_bitset, r300->state.render_inputs_bitset);
1633
1634 if (ctx->FragmentProgram._Current)
1635 InputsRead = ctx->FragmentProgram._Current->Base.InputsRead;
1636 else {
1637 fprintf(stderr, "No ctx->FragmentProgram._Current!!\n");
1638 return; /* This should only ever happen once.. */
1639 }
1640
1641 R300_STATECHANGE(r300, ri);
1642 R300_STATECHANGE(r300, rc);
1643 R300_STATECHANGE(r300, rr);
1644
1645 fp_reg = in_texcoords = col_interp_nr = high_rr = 0;
1646
1647 r300->hw.rr.cmd[R300_RR_ROUTE_1] = 0;
1648
1649 if (InputsRead & FRAG_BIT_WPOS) {
1650 for (i = 0; i < ctx->Const.MaxTextureUnits; i++)
1651 if (!(InputsRead & (FRAG_BIT_TEX0 << i)))
1652 break;
1653
1654 if (i == ctx->Const.MaxTextureUnits) {
1655 fprintf(stderr, "\tno free texcoord found...\n");
1656 _mesa_exit(-1);
1657 }
1658
1659 InputsRead |= (FRAG_BIT_TEX0 << i);
1660 InputsRead &= ~FRAG_BIT_WPOS;
1661 }
1662
1663 for (i = 0; i < ctx->Const.MaxTextureUnits; i++) {
1664
1665 // r300->hw.ri.cmd[R300_RI_INTERP_0 + i] = 0 | R300_RS_SEL_T(1) | R300_RS_SEL_R(2) | R300_RS_SEL_Q(3) | (in_texcoords << R300_RS_INTERP_SRC_SHIFT)
1666
1667 r300->hw.ri.cmd[R300_RI_INTERP_0 + i] = (0 << R500_RS_IP_TEX_PTR_S_SHIFT) |
1668 (1 << R500_RS_IP_TEX_PTR_T_SHIFT) |
1669 (2 << R500_RS_IP_TEX_PTR_R_SHIFT) |
1670 (3 << R500_RS_IP_TEX_PTR_Q_SHIFT) |
1671 (in_texcoords << 0) | interp_magic[i];
1672
1673 r300->hw.rr.cmd[R300_RR_ROUTE_0 + fp_reg] = 0;
1674 if (InputsRead & (FRAG_BIT_TEX0 << i)) {
1675 //assert(r300->state.texture.tc_count != 0);
1676 r300->hw.rr.cmd[R300_RR_ROUTE_0 + fp_reg] |= R500_RS_INST_TEX_CN_WRITE | i /* source INTERP */
1677 | (fp_reg << R500_RS_INST_TEX_ADDR_SHIFT);
1678 high_rr = fp_reg;
1679
1680 /* Passing invalid data here can lock the GPU. */
1681 if (R300_OUTPUTS_WRITTEN_TEST(OutputsWritten, VERT_RESULT_TEX0 + i, _TNL_ATTRIB_TEX(i))) {
1682 InputsRead &= ~(FRAG_BIT_TEX0 << i);
1683 fp_reg++;
1684 } else {
1685 WARN_ONCE("fragprog wants coords for tex%d, vp doesn't provide them!\n", i);
1686 }
1687 }
1688 /* Need to count all coords enabled at vof */
1689 if (R300_OUTPUTS_WRITTEN_TEST(OutputsWritten, VERT_RESULT_TEX0 + i, _TNL_ATTRIB_TEX(i))) {
1690 in_texcoords++;
1691 }
1692 }
1693
1694 if (InputsRead & FRAG_BIT_COL0) {
1695 if (R300_OUTPUTS_WRITTEN_TEST(OutputsWritten, VERT_RESULT_COL0, _TNL_ATTRIB_COLOR0)) {
1696 // r300->hw.rr.cmd[R300_RR_ROUTE_0] |= 0 | R300_RS_ROUTE_0_COLOR | (fp_reg++ << R300_RS_ROUTE_0_COLOR_DEST_SHIFT);
1697 r300->hw.rr.cmd[R300_RR_ROUTE_0] |= 0 | R500_RS_INST_COL_CN_WRITE | (fp_reg++ << R500_RS_INST_COL_COL_ADDR_SHIFT);
1698 InputsRead &= ~FRAG_BIT_COL0;
1699 col_interp_nr++;
1700 } else {
1701 WARN_ONCE("fragprog wants col0, vp doesn't provide it\n");
1702 }
1703 }
1704
1705 if (InputsRead & FRAG_BIT_COL1) {
1706 if (R300_OUTPUTS_WRITTEN_TEST(OutputsWritten, VERT_RESULT_COL1, _TNL_ATTRIB_COLOR1)) {
1707 // r300->hw.rr.cmd[R300_RR_ROUTE_1] |= R300_RS_ROUTE_1_UNKNOWN11 | R300_RS_ROUTE_1_COLOR1 | (fp_reg++ << R300_RS_ROUTE_1_COLOR1_DEST_SHIFT);
1708 r300->hw.rr.cmd[R300_RR_ROUTE_1] |= (1 << 12) | R500_RS_INST_COL_CN_WRITE | (fp_reg++ << R500_RS_INST_COL_COL_ADDR_SHIFT);
1709 InputsRead &= ~FRAG_BIT_COL1;
1710 if (high_rr < 1)
1711 high_rr = 1;
1712 col_interp_nr++;
1713 } else {
1714 WARN_ONCE("fragprog wants col1, vp doesn't provide it\n");
1715 }
1716 }
1717
1718 /* Need at least one. This might still lock as the values are undefined... */
1719 if (in_texcoords == 0 && col_interp_nr == 0) {
1720 r300->hw.rr.cmd[R300_RR_ROUTE_0] |= 0 | R500_RS_INST_COL_CN_WRITE | (fp_reg++ << R500_RS_INST_COL_COL_ADDR_SHIFT);
1721 col_interp_nr++;
1722 }
1723
1724 r300->hw.rc.cmd[1] = 0 | ((in_texcoords << 2) << R300_IT_COUNT_SHIFT)
1725 | (col_interp_nr << R300_IC_COUNT_SHIFT)
1726 | R300_HIRES_EN;
1727
1728 assert(high_rr >= 0);
1729 r300->hw.rr.cmd[R300_RR_CMD_0] = cmdpacket0(R300_RS_ROUTE_0, high_rr + 1);
1730 r300->hw.rc.cmd[2] = 0xC0 | high_rr;
1731
1732 if (InputsRead)
1733 WARN_ONCE("Don't know how to satisfy InputsRead=0x%08x\n", InputsRead);
1734 }
1735
1736
1737
1738
1739 #define bump_vpu_count(ptr, new_count) do{\
1740 drm_r300_cmd_header_t* _p=((drm_r300_cmd_header_t*)(ptr));\
1741 int _nc=(new_count)/4; \
1742 assert(_nc < 256); \
1743 if(_nc>_p->vpu.count)_p->vpu.count=_nc;\
1744 }while(0)
1745
1746 static inline void r300SetupVertexProgramFragment(r300ContextPtr r300, int dest, struct r300_vertex_shader_fragment *vsf)
1747 {
1748 int i;
1749
1750 if (vsf->length == 0)
1751 return;
1752
1753 if (vsf->length & 0x3) {
1754 fprintf(stderr, "VERTEX_SHADER_FRAGMENT must have length divisible by 4\n");
1755 _mesa_exit(-1);
1756 }
1757
1758 switch ((dest >> 8) & 0xf) {
1759 case 0:
1760 R300_STATECHANGE(r300, vpi);
1761 for (i = 0; i < vsf->length; i++)
1762 r300->hw.vpi.cmd[R300_VPI_INSTR_0 + i + 4 * (dest & 0xff)] = (vsf->body.d[i]);
1763 bump_vpu_count(r300->hw.vpi.cmd, vsf->length + 4 * (dest & 0xff));
1764 break;
1765
1766 case 2:
1767 R300_STATECHANGE(r300, vpp);
1768 for (i = 0; i < vsf->length; i++)
1769 r300->hw.vpp.cmd[R300_VPP_PARAM_0 + i + 4 * (dest & 0xff)] = (vsf->body.d[i]);
1770 bump_vpu_count(r300->hw.vpp.cmd, vsf->length + 4 * (dest & 0xff));
1771 break;
1772 case 4:
1773 R300_STATECHANGE(r300, vps);
1774 for (i = 0; i < vsf->length; i++)
1775 r300->hw.vps.cmd[1 + i + 4 * (dest & 0xff)] = (vsf->body.d[i]);
1776 bump_vpu_count(r300->hw.vps.cmd, vsf->length + 4 * (dest & 0xff));
1777 break;
1778 default:
1779 fprintf(stderr, "%s:%s don't know how to handle dest %04x\n", __FILE__, __FUNCTION__, dest);
1780 _mesa_exit(-1);
1781 }
1782 }
1783
1784 static void r300SetupDefaultVertexProgram(r300ContextPtr rmesa)
1785 {
1786 struct r300_vertex_shader_state *prog = &(rmesa->state.vertex_shader);
1787 GLuint o_reg = 0;
1788 int i;
1789 int inst_count = 0;
1790 int param_count = 0;
1791 int program_end = 0;
1792
1793 for (i = VERT_ATTRIB_POS; i < VERT_ATTRIB_MAX; i++) {
1794 if (rmesa->state.sw_tcl_inputs[i] != -1) {
1795 prog->program.body.i[program_end + 0] = PVS_OP_DST_OPERAND(VE_MULTIPLY, GL_FALSE, GL_FALSE, o_reg++, VSF_FLAG_ALL, PVS_DST_REG_OUT);
1796 prog->program.body.i[program_end + 1] = PVS_SRC_OPERAND(rmesa->state.sw_tcl_inputs[i], PVS_SRC_SELECT_X, PVS_SRC_SELECT_Y, PVS_SRC_SELECT_Z, PVS_SRC_SELECT_W, PVS_SRC_REG_INPUT, VSF_FLAG_NONE);
1797 prog->program.body.i[program_end + 2] = PVS_SRC_OPERAND(rmesa->state.sw_tcl_inputs[i], PVS_SRC_SELECT_FORCE_1, PVS_SRC_SELECT_FORCE_1, PVS_SRC_SELECT_FORCE_1, PVS_SRC_SELECT_FORCE_1, PVS_SRC_REG_INPUT, VSF_FLAG_NONE);
1798 prog->program.body.i[program_end + 3] = PVS_SRC_OPERAND(rmesa->state.sw_tcl_inputs[i], PVS_SRC_SELECT_FORCE_1, PVS_SRC_SELECT_FORCE_1, PVS_SRC_SELECT_FORCE_1, PVS_SRC_SELECT_FORCE_1, PVS_SRC_REG_INPUT, VSF_FLAG_NONE);
1799 program_end += 4;
1800 }
1801 }
1802
1803 prog->program.length = program_end;
1804
1805 r300SetupVertexProgramFragment(rmesa, R300_PVS_UPLOAD_PROGRAM,
1806 &(prog->program));
1807 inst_count = (prog->program.length / 4) - 1;
1808
1809 R300_STATECHANGE(rmesa, pvs);
1810 rmesa->hw.pvs.cmd[R300_PVS_CNTL_1] =
1811 (0 << R300_PVS_CNTL_1_PROGRAM_START_SHIFT) |
1812 (inst_count << R300_PVS_CNTL_1_POS_END_SHIFT) |
1813 (inst_count << R300_PVS_CNTL_1_PROGRAM_END_SHIFT);
1814 rmesa->hw.pvs.cmd[R300_PVS_CNTL_2] =
1815 (0 << R300_PVS_CNTL_2_PARAM_OFFSET_SHIFT) |
1816 (param_count << R300_PVS_CNTL_2_PARAM_COUNT_SHIFT);
1817 rmesa->hw.pvs.cmd[R300_PVS_CNTL_3] =
1818 (inst_count << R300_PVS_CNTL_3_PROGRAM_UNKNOWN_SHIFT) |
1819 (inst_count << R300_PVS_CNTL_3_PROGRAM_UNKNOWN2_SHIFT);
1820 }
1821
1822 static void r300SetupRealVertexProgram(r300ContextPtr rmesa)
1823 {
1824 GLcontext *ctx = rmesa->radeon.glCtx;
1825 struct r300_vertex_program *prog = (struct r300_vertex_program *)CURRENT_VERTEX_SHADER(ctx);
1826 int inst_count = 0;
1827 int param_count = 0;
1828
1829 /* FIXME: r300SetupVertexProgramFragment */
1830 R300_STATECHANGE(rmesa, vpp);
1831 param_count =
1832 r300VertexProgUpdateParams(ctx,
1833 (struct r300_vertex_program_cont *)
1834 ctx->VertexProgram._Current,
1835 (float *)&rmesa->hw.vpp.
1836 cmd[R300_VPP_PARAM_0]);
1837 bump_vpu_count(rmesa->hw.vpp.cmd, param_count);
1838 param_count /= 4;
1839
1840 r300SetupVertexProgramFragment(rmesa, R300_PVS_UPLOAD_PROGRAM, &(prog->program));
1841 inst_count = (prog->program.length / 4) - 1;
1842
1843 R300_STATECHANGE(rmesa, pvs);
1844 rmesa->hw.pvs.cmd[R300_PVS_CNTL_1] =
1845 (0 << R300_PVS_CNTL_1_PROGRAM_START_SHIFT) |
1846 (inst_count << R300_PVS_CNTL_1_POS_END_SHIFT) |
1847 (inst_count << R300_PVS_CNTL_1_PROGRAM_END_SHIFT);
1848 rmesa->hw.pvs.cmd[R300_PVS_CNTL_2] =
1849 (0 << R300_PVS_CNTL_2_PARAM_OFFSET_SHIFT) |
1850 (param_count << R300_PVS_CNTL_2_PARAM_COUNT_SHIFT);
1851 rmesa->hw.pvs.cmd[R300_PVS_CNTL_3] =
1852 (inst_count << R300_PVS_CNTL_3_PROGRAM_UNKNOWN_SHIFT) |
1853 (inst_count << R300_PVS_CNTL_3_PROGRAM_UNKNOWN2_SHIFT);
1854 }
1855
1856 static void r300SetupVertexProgram(r300ContextPtr rmesa)
1857 {
1858 GLcontext *ctx = rmesa->radeon.glCtx;
1859
1860 /* Reset state, in case we don't use something */
1861 ((drm_r300_cmd_header_t *) rmesa->hw.vpp.cmd)->vpu.count = 0;
1862 ((drm_r300_cmd_header_t *) rmesa->hw.vpi.cmd)->vpu.count = 0;
1863 ((drm_r300_cmd_header_t *) rmesa->hw.vps.cmd)->vpu.count = 0;
1864
1865 /* Not sure why this doesnt work...
1866 0x400 area might have something to do with pixel shaders as it appears right after pfs programming.
1867 0x406 is set to { 0.0, 0.0, 1.0, 0.0 } most of the time but should change with smooth points and in other rare cases. */
1868 //setup_vertex_shader_fragment(rmesa, 0x406, &unk4);
1869 if (hw_tcl_on && ((struct r300_vertex_program *)CURRENT_VERTEX_SHADER(ctx))->translated) {
1870 r300SetupRealVertexProgram(rmesa);
1871 } else {
1872 /* FIXME: This needs to be replaced by vertex shader generation code. */
1873 r300SetupDefaultVertexProgram(rmesa);
1874 }
1875
1876
1877 /* FIXME: This is done for vertex shader fragments, but also needs to be
1878 * done for vap_pvs, so I leave it as a reminder. */
1879 #if 0
1880 reg_start(R300_VAP_PVS_WAITIDLE, 0);
1881 e32(0x00000000);
1882 #endif
1883 }
1884
1885 /**
1886 * Enable/Disable states.
1887 *
1888 * \note Mesa already filters redundant calls to this function.
1889 */
1890 static void r300Enable(GLcontext * ctx, GLenum cap, GLboolean state)
1891 {
1892 if (RADEON_DEBUG & DEBUG_STATE)
1893 fprintf(stderr, "%s( %s = %s )\n", __FUNCTION__,
1894 _mesa_lookup_enum_by_nr(cap),
1895 state ? "GL_TRUE" : "GL_FALSE");
1896
1897 switch (cap) {
1898 case GL_TEXTURE_1D:
1899 case GL_TEXTURE_2D:
1900 case GL_TEXTURE_3D:
1901 /* empty */
1902 break;
1903 case GL_FOG:
1904 r300SetFogState(ctx, state);
1905 break;
1906 case GL_ALPHA_TEST:
1907 r300SetAlphaState(ctx);
1908 break;
1909 case GL_BLEND:
1910 case GL_COLOR_LOGIC_OP:
1911 r300SetBlendState(ctx);
1912 break;
1913 case GL_CLIP_PLANE0:
1914 case GL_CLIP_PLANE1:
1915 case GL_CLIP_PLANE2:
1916 case GL_CLIP_PLANE3:
1917 case GL_CLIP_PLANE4:
1918 case GL_CLIP_PLANE5:
1919 r300SetClipPlaneState(ctx, cap, state);
1920 break;
1921 case GL_DEPTH_TEST:
1922 r300SetDepthState(ctx);
1923 break;
1924 case GL_STENCIL_TEST:
1925 r300SetStencilState(ctx, state);
1926 break;
1927 case GL_CULL_FACE:
1928 r300UpdateCulling(ctx);
1929 break;
1930 case GL_POLYGON_OFFSET_POINT:
1931 case GL_POLYGON_OFFSET_LINE:
1932 case GL_POLYGON_OFFSET_FILL:
1933 r300SetPolygonOffsetState(ctx, state);
1934 break;
1935 default:
1936 radeonEnable(ctx, cap, state);
1937 break;
1938 }
1939 }
1940
1941 /**
1942 * Completely recalculates hardware state based on the Mesa state.
1943 */
1944 static void r300ResetHwState(r300ContextPtr r300)
1945 {
1946 GLcontext *ctx = r300->radeon.glCtx;
1947 int has_tcl = 1;
1948
1949 if (!(r300->radeon.radeonScreen->chip_flags & RADEON_CHIPSET_TCL))
1950 has_tcl = 0;
1951
1952 if (RADEON_DEBUG & DEBUG_STATE)
1953 fprintf(stderr, "%s\n", __FUNCTION__);
1954
1955 r300UpdateWindow(ctx);
1956
1957 r300ColorMask(ctx,
1958 ctx->Color.ColorMask[RCOMP],
1959 ctx->Color.ColorMask[GCOMP],
1960 ctx->Color.ColorMask[BCOMP], ctx->Color.ColorMask[ACOMP]);
1961
1962 r300Enable(ctx, GL_DEPTH_TEST, ctx->Depth.Test);
1963 r300DepthMask(ctx, ctx->Depth.Mask);
1964 r300DepthFunc(ctx, ctx->Depth.Func);
1965
1966 /* stencil */
1967 r300Enable(ctx, GL_STENCIL_TEST, ctx->Stencil.Enabled);
1968 r300StencilMaskSeparate(ctx, 0, ctx->Stencil.WriteMask[0]);
1969 r300StencilFuncSeparate(ctx, 0, ctx->Stencil.Function[0],
1970 ctx->Stencil.Ref[0], ctx->Stencil.ValueMask[0]);
1971 r300StencilOpSeparate(ctx, 0, ctx->Stencil.FailFunc[0],
1972 ctx->Stencil.ZFailFunc[0],
1973 ctx->Stencil.ZPassFunc[0]);
1974
1975 r300UpdateCulling(ctx);
1976
1977 r300UpdateTextureState(ctx);
1978
1979 r300SetBlendState(ctx);
1980
1981 r300AlphaFunc(ctx, ctx->Color.AlphaFunc, ctx->Color.AlphaRef);
1982 r300Enable(ctx, GL_ALPHA_TEST, ctx->Color.AlphaEnabled);
1983
1984 if (!has_tcl)
1985 r300->hw.vap_cntl.cmd[1] = 0x0014045a;
1986 else
1987 r300->hw.vap_cntl.cmd[1] = 0x0030045A; //0x0030065a /* Dangerous */
1988
1989 r300->hw.vte.cmd[1] = R300_VPORT_X_SCALE_ENA
1990 | R300_VPORT_X_OFFSET_ENA
1991 | R300_VPORT_Y_SCALE_ENA
1992 | R300_VPORT_Y_OFFSET_ENA
1993 | R300_VPORT_Z_SCALE_ENA
1994 | R300_VPORT_Z_OFFSET_ENA | R300_VTX_W0_FMT;
1995 r300->hw.vte.cmd[2] = 0x00000008;
1996
1997 r300->hw.vap_vf_max_vtx_indx.cmd[1] = 0x00FFFFFF;
1998 r300->hw.vap_vf_max_vtx_indx.cmd[2] = 0x00000000;
1999
2000 #ifdef MESA_LITTLE_ENDIAN
2001 r300->hw.vap_cntl_status.cmd[1] = R300_VC_NO_SWAP;
2002 #else
2003 r300->hw.vap_cntl_status.cmd[1] = R300_VC_32BIT_SWAP;
2004 #endif
2005
2006 /* disable VAP/TCL on non-TCL capable chips */
2007 if (!has_tcl)
2008 r300->hw.vap_cntl_status.cmd[1] |= R300_VAP_TCL_BYPASS;
2009
2010 r300->hw.vap_psc_sgn_norm_cntl.cmd[1] = 0xAAAAAAAA;
2011
2012 /* XXX: Other families? */
2013 if (has_tcl) {
2014 r300->hw.vap_clip_cntl.cmd[1] = R300_221C_NORMAL;
2015
2016 r300->hw.vap_clip.cmd[1] = r300PackFloat32(1.0); /* X */
2017 r300->hw.vap_clip.cmd[2] = r300PackFloat32(1.0); /* X */
2018 r300->hw.vap_clip.cmd[3] = r300PackFloat32(1.0); /* Y */
2019 r300->hw.vap_clip.cmd[4] = r300PackFloat32(1.0); /* Y */
2020
2021 switch (r300->radeon.radeonScreen->chip_family) {
2022 case CHIP_FAMILY_R300:
2023 r300->hw.vap_pvs_vtx_timeout_reg.cmd[1] = R300_2288_R300;
2024 break;
2025 default:
2026 r300->hw.vap_pvs_vtx_timeout_reg.cmd[1] = R300_2288_RV350;
2027 break;
2028 }
2029 }
2030
2031 r300->hw.gb_enable.cmd[1] = R300_GB_POINT_STUFF_ENABLE
2032 | R300_GB_LINE_STUFF_ENABLE
2033 | R300_GB_TRIANGLE_STUFF_ENABLE;
2034
2035 r300->hw.gb_misc.cmd[R300_GB_MISC_MSPOS_0] = 0x66666666;
2036 r300->hw.gb_misc.cmd[R300_GB_MISC_MSPOS_1] = 0x06666666;
2037
2038 /* XXX: Other families? */
2039 r300->hw.gb_misc.cmd[R300_GB_MISC_TILE_CONFIG] =
2040 R300_GB_TILE_ENABLE | R300_GB_TILE_SIZE_16;
2041 switch (r300->radeon.radeonScreen->chip_family) {
2042 case CHIP_FAMILY_R300:
2043 case CHIP_FAMILY_R350:
2044 case CHIP_FAMILY_RV410:
2045 r300->hw.gb_misc.cmd[R300_GB_MISC_TILE_CONFIG] |=
2046 R300_GB_TILE_PIPE_COUNT_R300;
2047 break;
2048 case CHIP_FAMILY_R420:
2049 r300->hw.gb_misc.cmd[R300_GB_MISC_TILE_CONFIG] |=
2050 R300_GB_TILE_PIPE_COUNT_R420;
2051 break;
2052 default:
2053 r300->hw.gb_misc.cmd[R300_GB_MISC_TILE_CONFIG] |=
2054 R300_GB_TILE_DISABLE; /* TODO: This disables tiling totally. I guess it happened accidentially. */
2055 break;
2056 }
2057
2058 /* XXX: set to 0 when fog is disabled? */
2059 r300->hw.gb_misc.cmd[R300_GB_MISC_SELECT] = R300_GB_FOG_SELECT_1_1_W;
2060
2061 /* XXX: Enable anti-aliasing? */
2062 r300->hw.gb_misc.cmd[R300_GB_MISC_AA_CONFIG] = GB_AA_CONFIG_AA_DISABLE;
2063
2064 r300->hw.ga_point_s0.cmd[1] = r300PackFloat32(0.0);
2065 r300->hw.ga_point_s0.cmd[2] = r300PackFloat32(0.0);
2066 r300->hw.ga_point_s0.cmd[3] = r300PackFloat32(1.0);
2067 r300->hw.ga_point_s0.cmd[4] = r300PackFloat32(1.0);
2068
2069 r300->hw.ga_triangle_stipple.cmd[1] = 0x00050005;
2070
2071 r300PointSize(ctx, 1.0);
2072
2073 r300->hw.ga_point_minmax.cmd[1] = 0x18000006;
2074 r300->hw.ga_point_minmax.cmd[2] = 0x00020006;
2075 r300->hw.ga_point_minmax.cmd[3] = r300PackFloat32(1.0 / 192.0);
2076
2077 r300LineWidth(ctx, 1.0);
2078
2079 r300->hw.ga_line_stipple.cmd[1] = 0;
2080 r300->hw.ga_line_stipple.cmd[2] = r300PackFloat32(0.0);
2081 r300->hw.ga_line_stipple.cmd[3] = r300PackFloat32(1.0);
2082
2083 r300ShadeModel(ctx, ctx->Light.ShadeModel);
2084
2085 r300PolygonMode(ctx, GL_FRONT, ctx->Polygon.FrontMode);
2086 r300PolygonMode(ctx, GL_BACK, ctx->Polygon.BackMode);
2087 r300->hw.zbias_cntl.cmd[1] = 0x00000000;
2088
2089 r300PolygonOffset(ctx, ctx->Polygon.OffsetFactor,
2090 ctx->Polygon.OffsetUnits);
2091 r300Enable(ctx, GL_POLYGON_OFFSET_POINT, ctx->Polygon.OffsetPoint);
2092 r300Enable(ctx, GL_POLYGON_OFFSET_LINE, ctx->Polygon.OffsetLine);
2093 r300Enable(ctx, GL_POLYGON_OFFSET_FILL, ctx->Polygon.OffsetFill);
2094
2095 r300->hw.su_depth_scale.cmd[1] = 0x4B7FFFFF;
2096 r300->hw.su_depth_scale.cmd[2] = 0x00000000;
2097
2098 r300->hw.sc_hyperz.cmd[1] = 0x0000001C;
2099 r300->hw.sc_hyperz.cmd[2] = 0x2DA49525;
2100
2101 r300->hw.sc_screendoor.cmd[1] = 0x00FFFFFF;
2102
2103 r300->hw.us_out_fmt.cmd[1] = 0x00001B01;
2104 r300->hw.us_out_fmt.cmd[2] = 0x00001B0F;
2105 r300->hw.us_out_fmt.cmd[3] = 0x00001B0F;
2106 r300->hw.us_out_fmt.cmd[4] = 0x00001B0F;
2107 r300->hw.us_out_fmt.cmd[5] = 0x00000001;
2108
2109 r300Enable(ctx, GL_FOG, ctx->Fog.Enabled);
2110 r300Fogfv(ctx, GL_FOG_MODE, NULL);
2111 r300Fogfv(ctx, GL_FOG_DENSITY, &ctx->Fog.Density);
2112 r300Fogfv(ctx, GL_FOG_START, &ctx->Fog.Start);
2113 r300Fogfv(ctx, GL_FOG_END, &ctx->Fog.End);
2114 r300Fogfv(ctx, GL_FOG_COLOR, ctx->Fog.Color);
2115 r300Fogfv(ctx, GL_FOG_COORDINATE_SOURCE_EXT, NULL);
2116
2117 r300->hw.fg_depth_src.cmd[1] = 0;
2118
2119 r300->hw.rb3d_cctl.cmd[1] = 0;
2120
2121 r300BlendColor(ctx, ctx->Color.BlendColor);
2122
2123 /* Again, r300ClearBuffer uses this */
2124 r300->hw.cb.cmd[R300_CB_OFFSET] =
2125 r300->radeon.state.color.drawOffset +
2126 r300->radeon.radeonScreen->fbLocation;
2127 r300->hw.cb.cmd[R300_CB_PITCH] = r300->radeon.state.color.drawPitch;
2128
2129 if (r300->radeon.radeonScreen->cpp == 4)
2130 r300->hw.cb.cmd[R300_CB_PITCH] |= R300_COLOR_FORMAT_ARGB8888;
2131 else
2132 r300->hw.cb.cmd[R300_CB_PITCH] |= R300_COLOR_FORMAT_RGB565;
2133
2134 if (r300->radeon.sarea->tiling_enabled)
2135 r300->hw.cb.cmd[R300_CB_PITCH] |= R300_COLOR_TILE_ENABLE;
2136
2137 r300->hw.rb3d_dither_ctl.cmd[1] = 0;
2138 r300->hw.rb3d_dither_ctl.cmd[2] = 0;
2139 r300->hw.rb3d_dither_ctl.cmd[3] = 0;
2140 r300->hw.rb3d_dither_ctl.cmd[4] = 0;
2141 r300->hw.rb3d_dither_ctl.cmd[5] = 0;
2142 r300->hw.rb3d_dither_ctl.cmd[6] = 0;
2143 r300->hw.rb3d_dither_ctl.cmd[7] = 0;
2144 r300->hw.rb3d_dither_ctl.cmd[8] = 0;
2145 r300->hw.rb3d_dither_ctl.cmd[9] = 0;
2146
2147 r300->hw.rb3d_aaresolve_ctl.cmd[1] = 0;
2148
2149 r300->hw.rb3d_discard_src_pixel_lte_threshold.cmd[1] = 0x00000000;
2150 r300->hw.rb3d_discard_src_pixel_lte_threshold.cmd[2] = 0xffffffff;
2151
2152 r300->hw.zb.cmd[R300_ZB_OFFSET] =
2153 r300->radeon.radeonScreen->depthOffset +
2154 r300->radeon.radeonScreen->fbLocation;
2155 r300->hw.zb.cmd[R300_ZB_PITCH] = r300->radeon.radeonScreen->depthPitch;
2156
2157 if (r300->radeon.sarea->tiling_enabled) {
2158 /* XXX: Turn off when clearing buffers ? */
2159 r300->hw.zb.cmd[R300_ZB_PITCH] |= ZB_DEPTHPITCH_DEPTHMACROTILE_ENABLE;
2160
2161 if (ctx->Visual.depthBits == 24)
2162 r300->hw.zb.cmd[R300_ZB_PITCH] |=
2163 ZB_DEPTHPITCH_DEPTHMICROTILE_TILED;
2164 }
2165
2166 r300->hw.zb_depthclearvalue.cmd[1] = 0;
2167
2168 r300->hw.unk4F30.cmd[1] = 0;
2169 r300->hw.unk4F30.cmd[2] = 0;
2170
2171 r300->hw.zb_hiz_offset.cmd[1] = 0;
2172
2173 r300->hw.zb_hiz_pitch.cmd[1] = 0;
2174
2175 if (has_tcl) {
2176 r300->hw.vps.cmd[R300_VPS_ZERO_0] = 0;
2177 r300->hw.vps.cmd[R300_VPS_ZERO_1] = 0;
2178 r300->hw.vps.cmd[R300_VPS_POINTSIZE] = r300PackFloat32(1.0);
2179 r300->hw.vps.cmd[R300_VPS_ZERO_3] = 0;
2180 }
2181
2182 r300->hw.all_dirty = GL_TRUE;
2183 }
2184
2185 void r300UpdateShaders(r300ContextPtr rmesa)
2186 {
2187 GLcontext *ctx;
2188 struct r300_vertex_program *vp;
2189 int i;
2190
2191 ctx = rmesa->radeon.glCtx;
2192
2193 if (rmesa->NewGLState && hw_tcl_on) {
2194 rmesa->NewGLState = 0;
2195
2196 for (i = _TNL_FIRST_MAT; i <= _TNL_LAST_MAT; i++) {
2197 rmesa->temp_attrib[i] =
2198 TNL_CONTEXT(ctx)->vb.AttribPtr[i];
2199 TNL_CONTEXT(ctx)->vb.AttribPtr[i] =
2200 &rmesa->dummy_attrib[i];
2201 }
2202
2203 _tnl_UpdateFixedFunctionProgram(ctx);
2204
2205 for (i = _TNL_FIRST_MAT; i <= _TNL_LAST_MAT; i++) {
2206 TNL_CONTEXT(ctx)->vb.AttribPtr[i] =
2207 rmesa->temp_attrib[i];
2208 }
2209
2210 r300SelectVertexShader(rmesa);
2211 vp = (struct r300_vertex_program *)
2212 CURRENT_VERTEX_SHADER(ctx);
2213 /*if (vp->translated == GL_FALSE)
2214 r300TranslateVertexShader(vp); */
2215 if (vp->translated == GL_FALSE) {
2216 fprintf(stderr, "Failing back to sw-tcl\n");
2217 hw_tcl_on = future_hw_tcl_on = 0;
2218 r300ResetHwState(rmesa);
2219
2220 return;
2221 }
2222 r300UpdateStateParameters(ctx, _NEW_PROGRAM);
2223 }
2224 }
2225
2226 static void r300SetupPixelShader(r300ContextPtr rmesa)
2227 {
2228 GLcontext *ctx = rmesa->radeon.glCtx;
2229 struct r300_fragment_program *fp = (struct r300_fragment_program *)
2230 (char *)ctx->FragmentProgram._Current;
2231 int i, k;
2232
2233 if (!fp) /* should only happenen once, just after context is created */
2234 return;
2235
2236 r300TranslateFragmentShader(rmesa, fp);
2237 if (!fp->translated) {
2238 fprintf(stderr, "%s: No valid fragment shader, exiting\n",
2239 __FUNCTION__);
2240 return;
2241 }
2242
2243 R300_STATECHANGE(rmesa, fpi[0]);
2244 rmesa->hw.fpi[0].cmd[R300_FPI_CMD_0] = cmdpacket0(R300_PFS_INSTR0_0, fp->alu_end + 1);
2245 for (i = 0; i <= fp->alu_end; i++) {
2246 rmesa->hw.fpi[0].cmd[R300_FPI_INSTR_0 + i] = fp->alu.inst[i].inst0;
2247 }
2248
2249 R300_STATECHANGE(rmesa, fpi[1]);
2250 rmesa->hw.fpi[1].cmd[R300_FPI_CMD_0] = cmdpacket0(R300_PFS_INSTR1_0, fp->alu_end + 1);
2251 for (i = 0; i <= fp->alu_end; i++) {
2252 rmesa->hw.fpi[1].cmd[R300_FPI_INSTR_0 + i] = fp->alu.inst[i].inst1;
2253 }
2254
2255 R300_STATECHANGE(rmesa, fpi[2]);
2256 rmesa->hw.fpi[2].cmd[R300_FPI_CMD_0] = cmdpacket0(R300_PFS_INSTR2_0, fp->alu_end + 1);
2257 for (i = 0; i <= fp->alu_end; i++) {
2258 rmesa->hw.fpi[2].cmd[R300_FPI_INSTR_0 + i] = fp->alu.inst[i].inst2;
2259 }
2260
2261 R300_STATECHANGE(rmesa, fpi[3]);
2262 rmesa->hw.fpi[3].cmd[R300_FPI_CMD_0] = cmdpacket0(R300_PFS_INSTR3_0, fp->alu_end + 1);
2263 for (i = 0; i <= fp->alu_end; i++) {
2264 rmesa->hw.fpi[3].cmd[R300_FPI_INSTR_0 + i] = fp->alu.inst[i].inst3;
2265 }
2266
2267 R300_STATECHANGE(rmesa, fp);
2268 rmesa->hw.fp.cmd[R300_FP_CNTL0] = fp->cur_node | (fp->first_node_has_tex << 3);
2269 rmesa->hw.fp.cmd[R300_FP_CNTL1] = fp->max_temp_idx;
2270 rmesa->hw.fp.cmd[R300_FP_CNTL2] =
2271 (fp->alu_offset << R300_PFS_CNTL_ALU_OFFSET_SHIFT) |
2272 (fp->alu_end << R300_PFS_CNTL_ALU_END_SHIFT) |
2273 (fp->tex_offset << R300_PFS_CNTL_TEX_OFFSET_SHIFT) |
2274 (fp->tex_end << R300_PFS_CNTL_TEX_END_SHIFT);
2275 /* I just want to say, the way these nodes are stored.. weird.. */
2276 for (i = 0, k = (4 - (fp->cur_node + 1)); i < 4; i++, k++) {
2277 if (i < (fp->cur_node + 1)) {
2278 rmesa->hw.fp.cmd[R300_FP_NODE0 + k] =
2279 (fp->node[i].alu_offset << R300_PFS_NODE_ALU_OFFSET_SHIFT) |
2280 (fp->node[i].alu_end << R300_PFS_NODE_ALU_END_SHIFT) |
2281 (fp->node[i].tex_offset << R300_PFS_NODE_TEX_OFFSET_SHIFT) |
2282 (fp->node[i].tex_end << R300_PFS_NODE_TEX_END_SHIFT) |
2283 fp->node[i].flags;
2284 } else {
2285 rmesa->hw.fp.cmd[R300_FP_NODE0 + (3 - i)] = 0;
2286 }
2287 }
2288
2289 R300_STATECHANGE(rmesa, fpp);
2290 rmesa->hw.fpp.cmd[R300_FPP_CMD_0] = cmdpacket0(R300_PFS_PARAM_0_X, fp->const_nr * 4);
2291 for (i = 0; i < fp->const_nr; i++) {
2292 rmesa->hw.fpp.cmd[R300_FPP_PARAM_0 + 4 * i + 0] = r300PackFloat24(fp->constant[i][0]);
2293 rmesa->hw.fpp.cmd[R300_FPP_PARAM_0 + 4 * i + 1] = r300PackFloat24(fp->constant[i][1]);
2294 rmesa->hw.fpp.cmd[R300_FPP_PARAM_0 + 4 * i + 2] = r300PackFloat24(fp->constant[i][2]);
2295 rmesa->hw.fpp.cmd[R300_FPP_PARAM_0 + 4 * i + 3] = r300PackFloat24(fp->constant[i][3]);
2296 }
2297 }
2298
2299 #define bump_r500fp_count(ptr, new_count) do{\
2300 drm_r300_cmd_header_t* _p=((drm_r300_cmd_header_t*)(ptr));\
2301 int _nc=(new_count)/6; \
2302 assert(_nc < 256); \
2303 if(_nc>_p->r500fp.count)_p->r500fp.count=_nc;\
2304 } while(0)
2305
2306 #define bump_r500fp_const_count(ptr, new_count) do{\
2307 drm_r300_cmd_header_t* _p=((drm_r300_cmd_header_t*)(ptr));\
2308 int _nc=(new_count)/4; \
2309 assert(_nc < 256); \
2310 if(_nc>_p->r500fp.count)_p->r500fp.count=_nc;\
2311 } while(0)
2312
2313 static void r500SetupPixelShader(r300ContextPtr rmesa)
2314 {
2315 GLcontext *ctx = rmesa->radeon.glCtx;
2316 struct r300_fragment_program *fp = (struct r300_fragment_program *)
2317 (char *)ctx->FragmentProgram._Current;
2318 int i, k;
2319
2320 if (!fp) /* should only happenen once, just after context is created */
2321 return;
2322
2323 /* emit the standard zero shader */
2324 R300_STATECHANGE(rmesa, r500fp);
2325 i = 1;
2326 rmesa->hw.r500fp.cmd[i++] = 0x7807;
2327 rmesa->hw.r500fp.cmd[i++] = R500_TEX_ID(0) | R500_TEX_INST_LD | R500_TEX_SEM_ACQUIRE | R500_TEX_IGNORE_UNCOVERED;
2328 rmesa->hw.r500fp.cmd[i++] = R500_TEX_SRC_ADDR(0) | R500_TEX_SRC_S_SWIZ_R |
2329 R500_TEX_SRC_T_SWIZ_G |
2330 R500_TEX_DST_ADDR(0) |
2331 R500_TEX_DST_R_SWIZ_R |
2332 R500_TEX_DST_G_SWIZ_G |
2333 R500_TEX_DST_B_SWIZ_B |
2334 R500_TEX_DST_A_SWIZ_A;
2335 rmesa->hw.r500fp.cmd[i++] = R500_DX_ADDR(0) |
2336 R500_DX_S_SWIZ_R |
2337 R500_DX_T_SWIZ_R |
2338 R500_DX_R_SWIZ_R |
2339 R500_DX_Q_SWIZ_R |
2340 R500_DY_ADDR(0) |
2341 R500_DY_S_SWIZ_R |
2342 R500_DY_T_SWIZ_R |
2343 R500_DY_R_SWIZ_R |
2344 R500_DY_Q_SWIZ_R;
2345 rmesa->hw.r500fp.cmd[i++] = 0x0;
2346 rmesa->hw.r500fp.cmd[i++] = 0x0;
2347
2348 rmesa->hw.r500fp.cmd[i++] = R500_INST_TYPE_OUT |
2349 R500_INST_TEX_SEM_WAIT |
2350 R500_INST_LAST |
2351 R500_INST_RGB_OMASK_R |
2352 R500_INST_RGB_OMASK_G |
2353 R500_INST_RGB_OMASK_B |
2354 R500_INST_ALPHA_OMASK;
2355
2356 rmesa->hw.r500fp.cmd[i++] = R500_RGB_ADDR0(0) |
2357 R500_RGB_ADDR1(0) |
2358 R500_RGB_ADDR1_CONST |
2359 R500_RGB_ADDR2(0) |
2360 R500_RGB_ADDR2_CONST |
2361 R500_RGB_SRCP_OP_1_MINUS_2RGB0;
2362 rmesa->hw.r500fp.cmd[i++] = R500_ALPHA_ADDR0(0) |
2363 R500_ALPHA_ADDR1(0) |
2364 R500_ALPHA_ADDR1_CONST |
2365 R500_ALPHA_ADDR2(0) |
2366 R500_ALPHA_ADDR2_CONST |
2367 R500_ALPHA_SRCP_OP_1_MINUS_2A0;
2368 rmesa->hw.r500fp.cmd[i++] = R500_ALU_RGB_SEL_A_SRC0 |
2369 R500_ALU_RGB_R_SWIZ_A_R |
2370 R500_ALU_RGB_G_SWIZ_A_G |
2371 R500_ALU_RGB_B_SWIZ_A_B |
2372 R500_ALU_RGB_SEL_B_SRC0 |
2373 R500_ALU_RGB_R_SWIZ_B_1 |
2374 R500_ALU_RGB_B_SWIZ_B_1 |
2375 R500_ALU_RGB_G_SWIZ_B_1;
2376 rmesa->hw.r500fp.cmd[i++] = R500_ALPHA_OP_MAD |
2377 R500_ALPHA_SWIZ_A_A |
2378 R500_ALPHA_SWIZ_B_1;
2379 rmesa->hw.r500fp.cmd[i++] = R500_ALU_RGBA_OP_MAD |
2380 R500_ALU_RGBA_R_SWIZ_0 |
2381 R500_ALU_RGBA_G_SWIZ_0 |
2382 R500_ALU_RGBA_B_SWIZ_0 |
2383 R500_ALU_RGBA_A_SWIZ_0;
2384
2385 bump_r500fp_count(rmesa->hw.r500fp.cmd, 12);
2386
2387 R300_STATECHANGE(rmesa, r500fp_const);
2388 for (i = 0; i < fp->const_nr; i++) {
2389 rmesa->hw.r500fp_const.cmd[R300_FPP_PARAM_0 + 4 * i + 0] = r300PackFloat24(fp->constant[i][0]);
2390 rmesa->hw.r500fp_const.cmd[R300_FPP_PARAM_0 + 4 * i + 1] = r300PackFloat24(fp->constant[i][1]);
2391 rmesa->hw.r500fp_const.cmd[R300_FPP_PARAM_0 + 4 * i + 2] = r300PackFloat24(fp->constant[i][2]);
2392 rmesa->hw.r500fp_const.cmd[R300_FPP_PARAM_0 + 4 * i + 3] = r300PackFloat24(fp->constant[i][3]);
2393 }
2394 bump_r500fp_const_count(rmesa->hw.r500fp_const.cmd, fp->const_nr * 4);
2395
2396 }
2397
2398 void r300UpdateShaderStates(r300ContextPtr rmesa)
2399 {
2400 GLcontext *ctx;
2401 ctx = rmesa->radeon.glCtx;
2402
2403 r300UpdateTextureState(ctx);
2404
2405 if (rmesa->radeon.radeonScreen->chip_family >= CHIP_FAMILY_RV515)
2406 r500SetupPixelShader(rmesa);
2407 else
2408 r300SetupPixelShader(rmesa);
2409 r300SetupTextures(ctx);
2410
2411 if ((rmesa->radeon.radeonScreen->chip_flags & RADEON_CHIPSET_TCL))
2412 r300SetupVertexProgram(rmesa);
2413
2414 if (rmesa->radeon.radeonScreen->chip_family >= CHIP_FAMILY_RV515)
2415 r500SetupRSUnit(ctx);
2416 else
2417 r300SetupRSUnit(ctx);
2418 }
2419
2420 /**
2421 * Called by Mesa after an internal state update.
2422 */
2423 static void r300InvalidateState(GLcontext * ctx, GLuint new_state)
2424 {
2425 r300ContextPtr r300 = R300_CONTEXT(ctx);
2426
2427 _swrast_InvalidateState(ctx, new_state);
2428 _swsetup_InvalidateState(ctx, new_state);
2429 _vbo_InvalidateState(ctx, new_state);
2430 _tnl_InvalidateState(ctx, new_state);
2431 _ae_invalidate_state(ctx, new_state);
2432
2433 if (new_state & (_NEW_BUFFERS | _NEW_COLOR | _NEW_PIXEL)) {
2434 r300UpdateDrawBuffer(ctx);
2435 }
2436
2437 r300UpdateStateParameters(ctx, new_state);
2438
2439 r300->NewGLState |= new_state;
2440 }
2441
2442 /**
2443 * Calculate initial hardware state and register state functions.
2444 * Assumes that the command buffer and state atoms have been
2445 * initialized already.
2446 */
2447 void r300InitState(r300ContextPtr r300)
2448 {
2449 GLcontext *ctx = r300->radeon.glCtx;
2450 GLuint depth_fmt;
2451
2452 radeonInitState(&r300->radeon);
2453
2454 switch (ctx->Visual.depthBits) {
2455 case 16:
2456 r300->state.depth.scale = 1.0 / (GLfloat) 0xffff;
2457 depth_fmt = ZB_FORMAR_DEPTHFORMAT_16BIT_INT_Z;
2458 r300->state.stencil.clear = 0x00000000;
2459 break;
2460 case 24:
2461 r300->state.depth.scale = 1.0 / (GLfloat) 0xffffff;
2462 depth_fmt = ZB_FORMAR_DEPTHFORMAT_24BIT_INT_Z;
2463 r300->state.stencil.clear = 0x00ff0000;
2464 break;
2465 default:
2466 fprintf(stderr, "Error: Unsupported depth %d... exiting\n",
2467 ctx->Visual.depthBits);
2468 _mesa_exit(-1);
2469 }
2470
2471 /* Only have hw stencil when depth buffer is 24 bits deep */
2472 r300->state.stencil.hw_stencil = (ctx->Visual.stencilBits > 0 &&
2473 ctx->Visual.depthBits == 24);
2474
2475 memset(&(r300->state.texture), 0, sizeof(r300->state.texture));
2476
2477 r300ResetHwState(r300);
2478 }
2479
2480 static void r300RenderMode(GLcontext * ctx, GLenum mode)
2481 {
2482 r300ContextPtr rmesa = R300_CONTEXT(ctx);
2483 (void)rmesa;
2484 (void)mode;
2485 }
2486
2487 void r300UpdateClipPlanes( GLcontext *ctx )
2488 {
2489 r300ContextPtr rmesa = R300_CONTEXT(ctx);
2490 GLuint p;
2491
2492 for (p = 0; p < ctx->Const.MaxClipPlanes; p++) {
2493 if (ctx->Transform.ClipPlanesEnabled & (1 << p)) {
2494 GLint *ip = (GLint *)ctx->Transform._ClipUserPlane[p];
2495
2496 R300_STATECHANGE( rmesa, vpucp[p] );
2497 rmesa->hw.vpucp[p].cmd[R300_VPUCP_X] = ip[0];
2498 rmesa->hw.vpucp[p].cmd[R300_VPUCP_Y] = ip[1];
2499 rmesa->hw.vpucp[p].cmd[R300_VPUCP_Z] = ip[2];
2500 rmesa->hw.vpucp[p].cmd[R300_VPUCP_W] = ip[3];
2501 }
2502 }
2503 }
2504
2505 /**
2506 * Initialize driver's state callback functions
2507 */
2508 void r300InitStateFuncs(struct dd_function_table *functions)
2509 {
2510 radeonInitStateFuncs(functions);
2511
2512 functions->UpdateState = r300InvalidateState;
2513 functions->AlphaFunc = r300AlphaFunc;
2514 functions->BlendColor = r300BlendColor;
2515 functions->BlendEquationSeparate = r300BlendEquationSeparate;
2516 functions->BlendFuncSeparate = r300BlendFuncSeparate;
2517 functions->Enable = r300Enable;
2518 functions->ColorMask = r300ColorMask;
2519 functions->DepthFunc = r300DepthFunc;
2520 functions->DepthMask = r300DepthMask;
2521 functions->CullFace = r300CullFace;
2522 functions->Fogfv = r300Fogfv;
2523 functions->FrontFace = r300FrontFace;
2524 functions->ShadeModel = r300ShadeModel;
2525
2526 /* Stencil related */
2527 functions->ClearStencil = r300ClearStencil;
2528 functions->StencilFuncSeparate = r300StencilFuncSeparate;
2529 functions->StencilMaskSeparate = r300StencilMaskSeparate;
2530 functions->StencilOpSeparate = r300StencilOpSeparate;
2531
2532 /* Viewport related */
2533 functions->Viewport = r300Viewport;
2534 functions->DepthRange = r300DepthRange;
2535 functions->PointSize = r300PointSize;
2536 functions->LineWidth = r300LineWidth;
2537
2538 functions->PolygonOffset = r300PolygonOffset;
2539 functions->PolygonMode = r300PolygonMode;
2540
2541 functions->RenderMode = r300RenderMode;
2542
2543 functions->ClipPlane = r300ClipPlane;
2544 }