a790acacfef1365e5b5a85a08c8177bfff2c3d7c
[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 /**
315 * Update our tracked culling state based on Mesa's state.
316 */
317 static void r300UpdateCulling(GLcontext * ctx)
318 {
319 r300ContextPtr r300 = R300_CONTEXT(ctx);
320 uint32_t val = 0;
321
322 if (ctx->Polygon.CullFlag) {
323 switch (ctx->Polygon.CullFaceMode) {
324 case GL_FRONT:
325 val = R300_CULL_FRONT;
326 break;
327 case GL_BACK:
328 val = R300_CULL_BACK;
329 break;
330 case GL_FRONT_AND_BACK:
331 val = R300_CULL_FRONT | R300_CULL_BACK;
332 break;
333 default:
334 break;
335 }
336 }
337
338 switch (ctx->Polygon.FrontFace) {
339 case GL_CW:
340 val |= R300_FRONT_FACE_CW;
341 break;
342 case GL_CCW:
343 val |= R300_FRONT_FACE_CCW;
344 break;
345 default:
346 break;
347 }
348
349 R300_STATECHANGE(r300, cul);
350 r300->hw.cul.cmd[R300_CUL_CULL] = val;
351 }
352
353 static void r300SetEarlyZState(GLcontext * ctx)
354 {
355 /* updates register R300_RB3D_EARLY_Z (0x4F14)
356 if depth test is not enabled it should be R300_EARLY_Z_DISABLE
357 if depth is enabled and alpha not it should be R300_EARLY_Z_ENABLE
358 if depth and alpha is enabled it should be R300_EARLY_Z_DISABLE
359 */
360 r300ContextPtr r300 = R300_CONTEXT(ctx);
361
362 R300_STATECHANGE(r300, zstencil_format);
363 switch (ctx->Visual.depthBits) {
364 case 16:
365 r300->hw.zstencil_format.cmd[1] = R300_DEPTH_FORMAT_16BIT_INT_Z;
366 break;
367 case 24:
368 r300->hw.zstencil_format.cmd[1] = R300_DEPTH_FORMAT_24BIT_INT_Z;
369 break;
370 default:
371 fprintf(stderr, "Error: Unsupported depth %d... exiting\n", ctx->Visual.depthBits);
372 _mesa_exit(-1);
373 }
374
375 // r300->hw.zstencil_format.cmd[1] |= R300_DEPTH_FORMAT_UNK32;
376
377 if (ctx->Color.AlphaEnabled && ctx->Color.AlphaFunc != GL_ALWAYS)
378 /* disable early Z */
379 r300->hw.zstencil_format.cmd[2] = R300_EARLY_Z_DISABLE;
380 else {
381 if (ctx->Depth.Test && ctx->Depth.Func != GL_NEVER)
382 /* enable early Z */
383 r300->hw.zstencil_format.cmd[2] = R300_EARLY_Z_ENABLE;
384 else
385 /* disable early Z */
386 r300->hw.zstencil_format.cmd[2] = R300_EARLY_Z_DISABLE;
387 }
388
389 r300->hw.zstencil_format.cmd[3] = 0x00000003;
390 r300->hw.zstencil_format.cmd[4] = 0x00000000;
391 }
392
393 static void r300SetAlphaState(GLcontext * ctx)
394 {
395 r300ContextPtr r300 = R300_CONTEXT(ctx);
396 GLubyte refByte;
397 uint32_t pp_misc = 0x0;
398 GLboolean really_enabled = ctx->Color.AlphaEnabled;
399
400 CLAMPED_FLOAT_TO_UBYTE(refByte, ctx->Color.AlphaRef);
401
402 switch (ctx->Color.AlphaFunc) {
403 case GL_NEVER:
404 pp_misc |= R300_ALPHA_TEST_FAIL;
405 break;
406 case GL_LESS:
407 pp_misc |= R300_ALPHA_TEST_LESS;
408 break;
409 case GL_EQUAL:
410 pp_misc |= R300_ALPHA_TEST_EQUAL;
411 break;
412 case GL_LEQUAL:
413 pp_misc |= R300_ALPHA_TEST_LEQUAL;
414 break;
415 case GL_GREATER:
416 pp_misc |= R300_ALPHA_TEST_GREATER;
417 break;
418 case GL_NOTEQUAL:
419 pp_misc |= R300_ALPHA_TEST_NEQUAL;
420 break;
421 case GL_GEQUAL:
422 pp_misc |= R300_ALPHA_TEST_GEQUAL;
423 break;
424 case GL_ALWAYS:
425 /*pp_misc |= R300_ALPHA_TEST_PASS; */
426 really_enabled = GL_FALSE;
427 break;
428 }
429
430 if (really_enabled) {
431 pp_misc |= R300_ALPHA_TEST_ENABLE;
432 pp_misc |= (refByte & R300_REF_ALPHA_MASK);
433 } else {
434 pp_misc = 0x0;
435 }
436
437 R300_STATECHANGE(r300, at);
438 r300->hw.at.cmd[R300_AT_ALPHA_TEST] = pp_misc;
439 r300->hw.at.cmd[R300_AT_UNKNOWN] = 0;
440
441 r300SetEarlyZState(ctx);
442 }
443
444 static void r300AlphaFunc(GLcontext * ctx, GLenum func, GLfloat ref)
445 {
446 (void)func;
447 (void)ref;
448 r300SetAlphaState(ctx);
449 }
450
451 static int translate_func(int func)
452 {
453 switch (func) {
454 case GL_NEVER:
455 return R300_ZS_NEVER;
456 case GL_LESS:
457 return R300_ZS_LESS;
458 case GL_EQUAL:
459 return R300_ZS_EQUAL;
460 case GL_LEQUAL:
461 return R300_ZS_LEQUAL;
462 case GL_GREATER:
463 return R300_ZS_GREATER;
464 case GL_NOTEQUAL:
465 return R300_ZS_NOTEQUAL;
466 case GL_GEQUAL:
467 return R300_ZS_GEQUAL;
468 case GL_ALWAYS:
469 return R300_ZS_ALWAYS;
470 }
471 return 0;
472 }
473
474 static void r300SetDepthState(GLcontext * ctx)
475 {
476 r300ContextPtr r300 = R300_CONTEXT(ctx);
477
478 R300_STATECHANGE(r300, zs);
479 r300->hw.zs.cmd[R300_ZS_CNTL_0] &= R300_RB3D_STENCIL_ENABLE;
480 r300->hw.zs.cmd[R300_ZS_CNTL_1] &=
481 ~(R300_ZS_MASK << R300_RB3D_ZS1_DEPTH_FUNC_SHIFT);
482
483 if (ctx->Depth.Test && ctx->Depth.Func != GL_NEVER) {
484 if (ctx->Depth.Mask)
485 r300->hw.zs.cmd[R300_ZS_CNTL_0] |=
486 R300_RB3D_Z_TEST_AND_WRITE;
487 else
488 r300->hw.zs.cmd[R300_ZS_CNTL_0] |= R300_RB3D_Z_TEST;
489
490 r300->hw.zs.cmd[R300_ZS_CNTL_1] |=
491 translate_func(ctx->Depth.
492 Func) << R300_RB3D_ZS1_DEPTH_FUNC_SHIFT;
493 } else {
494 r300->hw.zs.cmd[R300_ZS_CNTL_0] |= R300_RB3D_Z_DISABLED_1;
495 r300->hw.zs.cmd[R300_ZS_CNTL_1] |=
496 translate_func(GL_NEVER) << R300_RB3D_ZS1_DEPTH_FUNC_SHIFT;
497 }
498
499 r300SetEarlyZState(ctx);
500 }
501
502 static void r300UpdatePolygonMode(GLcontext * ctx)
503 {
504 r300ContextPtr r300 = R300_CONTEXT(ctx);
505 uint32_t hw_mode = 0;
506
507 if (ctx->Polygon.FrontMode != GL_FILL ||
508 ctx->Polygon.BackMode != GL_FILL) {
509 GLenum f, b;
510
511 if (ctx->Polygon.FrontFace == GL_CCW) {
512 f = ctx->Polygon.FrontMode;
513 b = ctx->Polygon.BackMode;
514 } else {
515 f = ctx->Polygon.BackMode;
516 b = ctx->Polygon.FrontMode;
517 }
518
519 hw_mode |= R300_PM_ENABLED;
520
521 switch (f) {
522 case GL_LINE:
523 hw_mode |= R300_PM_FRONT_LINE;
524 break;
525 case GL_POINT: /* noop */
526 hw_mode |= R300_PM_FRONT_POINT;
527 break;
528 case GL_FILL:
529 hw_mode |= R300_PM_FRONT_FILL;
530 break;
531 }
532
533 switch (b) {
534 case GL_LINE:
535 hw_mode |= R300_PM_BACK_LINE;
536 break;
537 case GL_POINT: /* noop */
538 hw_mode |= R300_PM_BACK_POINT;
539 break;
540 case GL_FILL:
541 hw_mode |= R300_PM_BACK_FILL;
542 break;
543 }
544 }
545
546 if (r300->hw.polygon_mode.cmd[1] != hw_mode) {
547 R300_STATECHANGE(r300, polygon_mode);
548 r300->hw.polygon_mode.cmd[1] = hw_mode;
549 }
550
551 r300->hw.polygon_mode.cmd[2] = 0x00000001;
552 r300->hw.polygon_mode.cmd[3] = 0x00000000;
553 }
554
555 /**
556 * Change the culling mode.
557 *
558 * \note Mesa already filters redundant calls to this function.
559 */
560 static void r300CullFace(GLcontext * ctx, GLenum mode)
561 {
562 (void)mode;
563
564 r300UpdateCulling(ctx);
565 }
566
567 /**
568 * Change the polygon orientation.
569 *
570 * \note Mesa already filters redundant calls to this function.
571 */
572 static void r300FrontFace(GLcontext * ctx, GLenum mode)
573 {
574 (void)mode;
575
576 r300UpdateCulling(ctx);
577 r300UpdatePolygonMode(ctx);
578 }
579
580 /**
581 * Change the depth testing function.
582 *
583 * \note Mesa already filters redundant calls to this function.
584 */
585 static void r300DepthFunc(GLcontext * ctx, GLenum func)
586 {
587 (void)func;
588 r300SetDepthState(ctx);
589 }
590
591 /**
592 * Enable/Disable depth writing.
593 *
594 * \note Mesa already filters redundant calls to this function.
595 */
596 static void r300DepthMask(GLcontext * ctx, GLboolean mask)
597 {
598 (void)mask;
599 r300SetDepthState(ctx);
600 }
601
602 /**
603 * Handle glColorMask()
604 */
605 static void r300ColorMask(GLcontext * ctx,
606 GLboolean r, GLboolean g, GLboolean b, GLboolean a)
607 {
608 r300ContextPtr r300 = R300_CONTEXT(ctx);
609 int mask = (r ? R300_COLORMASK0_R : 0) |
610 (g ? R300_COLORMASK0_G : 0) |
611 (b ? R300_COLORMASK0_B : 0) | (a ? R300_COLORMASK0_A : 0);
612
613 if (mask != r300->hw.cmk.cmd[R300_CMK_COLORMASK]) {
614 R300_STATECHANGE(r300, cmk);
615 r300->hw.cmk.cmd[R300_CMK_COLORMASK] = mask;
616 }
617 }
618
619 /* =============================================================
620 * Fog
621 */
622 static void r300Fogfv(GLcontext * ctx, GLenum pname, const GLfloat * param)
623 {
624 r300ContextPtr r300 = R300_CONTEXT(ctx);
625 union {
626 int i;
627 float f;
628 } fogScale, fogStart;
629
630 (void)param;
631
632 fogScale.i = r300->hw.fogp.cmd[R300_FOGP_SCALE];
633 fogStart.i = r300->hw.fogp.cmd[R300_FOGP_START];
634
635 switch (pname) {
636 case GL_FOG_MODE:
637 if (!ctx->Fog.Enabled)
638 return;
639 switch (ctx->Fog.Mode) {
640 case GL_LINEAR:
641 R300_STATECHANGE(r300, fogs);
642 r300->hw.fogs.cmd[R300_FOGS_STATE] =
643 (r300->hw.fogs.
644 cmd[R300_FOGS_STATE] & ~R300_FOG_MODE_MASK) |
645 R300_FOG_MODE_LINEAR;
646
647 if (ctx->Fog.Start == ctx->Fog.End) {
648 fogScale.f = -1.0;
649 fogStart.f = 1.0;
650 } else {
651 fogScale.f =
652 1.0 / (ctx->Fog.End - ctx->Fog.Start);
653 fogStart.f =
654 -ctx->Fog.Start / (ctx->Fog.End -
655 ctx->Fog.Start);
656 }
657 break;
658 case GL_EXP:
659 R300_STATECHANGE(r300, fogs);
660 r300->hw.fogs.cmd[R300_FOGS_STATE] =
661 (r300->hw.fogs.
662 cmd[R300_FOGS_STATE] & ~R300_FOG_MODE_MASK) |
663 R300_FOG_MODE_EXP;
664 fogScale.f = 0.0933 * ctx->Fog.Density;
665 fogStart.f = 0.0;
666 break;
667 case GL_EXP2:
668 R300_STATECHANGE(r300, fogs);
669 r300->hw.fogs.cmd[R300_FOGS_STATE] =
670 (r300->hw.fogs.
671 cmd[R300_FOGS_STATE] & ~R300_FOG_MODE_MASK) |
672 R300_FOG_MODE_EXP2;
673 fogScale.f = 0.3 * ctx->Fog.Density;
674 fogStart.f = 0.0;
675 default:
676 return;
677 }
678 break;
679 case GL_FOG_DENSITY:
680 switch (ctx->Fog.Mode) {
681 case GL_EXP:
682 fogScale.f = 0.0933 * ctx->Fog.Density;
683 fogStart.f = 0.0;
684 break;
685 case GL_EXP2:
686 fogScale.f = 0.3 * ctx->Fog.Density;
687 fogStart.f = 0.0;
688 default:
689 break;
690 }
691 break;
692 case GL_FOG_START:
693 case GL_FOG_END:
694 if (ctx->Fog.Mode == GL_LINEAR) {
695 if (ctx->Fog.Start == ctx->Fog.End) {
696 fogScale.f = -1.0;
697 fogStart.f = 1.0;
698 } else {
699 fogScale.f =
700 1.0 / (ctx->Fog.End - ctx->Fog.Start);
701 fogStart.f =
702 -ctx->Fog.Start / (ctx->Fog.End -
703 ctx->Fog.Start);
704 }
705 }
706 break;
707 case GL_FOG_COLOR:
708 R300_STATECHANGE(r300, fogc);
709 r300->hw.fogc.cmd[R300_FOGC_R] =
710 (GLuint) (ctx->Fog.Color[0] * 1023.0F) & 0x3FF;
711 r300->hw.fogc.cmd[R300_FOGC_G] =
712 (GLuint) (ctx->Fog.Color[1] * 1023.0F) & 0x3FF;
713 r300->hw.fogc.cmd[R300_FOGC_B] =
714 (GLuint) (ctx->Fog.Color[2] * 1023.0F) & 0x3FF;
715 break;
716 case GL_FOG_COORD_SRC:
717 break;
718 default:
719 return;
720 }
721
722 if (fogScale.i != r300->hw.fogp.cmd[R300_FOGP_SCALE] ||
723 fogStart.i != r300->hw.fogp.cmd[R300_FOGP_START]) {
724 R300_STATECHANGE(r300, fogp);
725 r300->hw.fogp.cmd[R300_FOGP_SCALE] = fogScale.i;
726 r300->hw.fogp.cmd[R300_FOGP_START] = fogStart.i;
727 }
728 }
729
730 /* =============================================================
731 * Point state
732 */
733 static void r300PointSize(GLcontext * ctx, GLfloat size)
734 {
735 r300ContextPtr r300 = R300_CONTEXT(ctx);
736
737 size = ctx->Point._Size;
738
739 R300_STATECHANGE(r300, ps);
740 r300->hw.ps.cmd[R300_PS_POINTSIZE] =
741 ((int)(size * 6) << R300_POINTSIZE_X_SHIFT) |
742 ((int)(size * 6) << R300_POINTSIZE_Y_SHIFT);
743 }
744
745 /* =============================================================
746 * Line state
747 */
748 static void r300LineWidth(GLcontext * ctx, GLfloat widthf)
749 {
750 r300ContextPtr r300 = R300_CONTEXT(ctx);
751
752 widthf = ctx->Line._Width;
753
754 R300_STATECHANGE(r300, lcntl);
755 r300->hw.lcntl.cmd[1] =
756 R300_LINE_CNT_HO | R300_LINE_CNT_VE | (int)(widthf * 6.0);
757 }
758
759 static void r300PolygonMode(GLcontext * ctx, GLenum face, GLenum mode)
760 {
761 (void)face;
762 (void)mode;
763
764 r300UpdatePolygonMode(ctx);
765 }
766
767 /* =============================================================
768 * Stencil
769 */
770
771 static int translate_stencil_op(int op)
772 {
773 switch (op) {
774 case GL_KEEP:
775 return R300_ZS_KEEP;
776 case GL_ZERO:
777 return R300_ZS_ZERO;
778 case GL_REPLACE:
779 return R300_ZS_REPLACE;
780 case GL_INCR:
781 return R300_ZS_INCR;
782 case GL_DECR:
783 return R300_ZS_DECR;
784 case GL_INCR_WRAP_EXT:
785 return R300_ZS_INCR_WRAP;
786 case GL_DECR_WRAP_EXT:
787 return R300_ZS_DECR_WRAP;
788 case GL_INVERT:
789 return R300_ZS_INVERT;
790 default:
791 WARN_ONCE("Do not know how to translate stencil op");
792 return R300_ZS_KEEP;
793 }
794 return 0;
795 }
796
797 static void r300ShadeModel(GLcontext * ctx, GLenum mode)
798 {
799 r300ContextPtr rmesa = R300_CONTEXT(ctx);
800
801 R300_STATECHANGE(rmesa, shade);
802 rmesa->hw.shade.cmd[1] = 0x00000002;
803 switch (mode) {
804 case GL_FLAT:
805 rmesa->hw.shade.cmd[2] = R300_RE_SHADE_MODEL_FLAT;
806 break;
807 case GL_SMOOTH:
808 rmesa->hw.shade.cmd[2] = R300_RE_SHADE_MODEL_SMOOTH;
809 break;
810 default:
811 return;
812 }
813 rmesa->hw.shade.cmd[3] = 0x00000000;
814 rmesa->hw.shade.cmd[4] = 0x00000000;
815 }
816
817 static void r300StencilFuncSeparate(GLcontext * ctx, GLenum face,
818 GLenum func, GLint ref, GLuint mask)
819 {
820 r300ContextPtr rmesa = R300_CONTEXT(ctx);
821 GLuint refmask =
822 (((ctx->Stencil.
823 Ref[0] & 0xff) << R300_RB3D_ZS2_STENCIL_REF_SHIFT) | ((ctx->
824 Stencil.
825 ValueMask
826 [0] &
827 0xff)
828 <<
829 R300_RB3D_ZS2_STENCIL_MASK_SHIFT));
830
831 GLuint flag;
832
833 R300_STATECHANGE(rmesa, zs);
834
835 rmesa->hw.zs.cmd[R300_ZS_CNTL_1] &= ~((R300_ZS_MASK <<
836 R300_RB3D_ZS1_FRONT_FUNC_SHIFT)
837 | (R300_ZS_MASK <<
838 R300_RB3D_ZS1_BACK_FUNC_SHIFT));
839
840 rmesa->hw.zs.cmd[R300_ZS_CNTL_2] &=
841 ~((R300_RB3D_ZS2_STENCIL_MASK <<
842 R300_RB3D_ZS2_STENCIL_REF_SHIFT) |
843 (R300_RB3D_ZS2_STENCIL_MASK << R300_RB3D_ZS2_STENCIL_MASK_SHIFT));
844
845 flag = translate_func(ctx->Stencil.Function[0]);
846 rmesa->hw.zs.cmd[R300_ZS_CNTL_1] |=
847 (flag << R300_RB3D_ZS1_FRONT_FUNC_SHIFT);
848
849 if (ctx->Stencil._TestTwoSide)
850 flag = translate_func(ctx->Stencil.Function[1]);
851
852 rmesa->hw.zs.cmd[R300_ZS_CNTL_1] |=
853 (flag << R300_RB3D_ZS1_BACK_FUNC_SHIFT);
854 rmesa->hw.zs.cmd[R300_ZS_CNTL_2] |= refmask;
855 }
856
857 static void r300StencilMaskSeparate(GLcontext * ctx, GLenum face, GLuint mask)
858 {
859 r300ContextPtr rmesa = R300_CONTEXT(ctx);
860
861 R300_STATECHANGE(rmesa, zs);
862 rmesa->hw.zs.cmd[R300_ZS_CNTL_2] &=
863 ~(R300_RB3D_ZS2_STENCIL_MASK <<
864 R300_RB3D_ZS2_STENCIL_WRITE_MASK_SHIFT);
865 rmesa->hw.zs.cmd[R300_ZS_CNTL_2] |=
866 (ctx->Stencil.
867 WriteMask[0] & 0xff) << R300_RB3D_ZS2_STENCIL_WRITE_MASK_SHIFT;
868 }
869
870 static void r300StencilOpSeparate(GLcontext * ctx, GLenum face,
871 GLenum fail, GLenum zfail, GLenum zpass)
872 {
873 r300ContextPtr rmesa = R300_CONTEXT(ctx);
874
875 R300_STATECHANGE(rmesa, zs);
876 /* It is easier to mask what's left.. */
877 rmesa->hw.zs.cmd[R300_ZS_CNTL_1] &=
878 (R300_ZS_MASK << R300_RB3D_ZS1_DEPTH_FUNC_SHIFT) |
879 (R300_ZS_MASK << R300_RB3D_ZS1_FRONT_FUNC_SHIFT) |
880 (R300_ZS_MASK << R300_RB3D_ZS1_BACK_FUNC_SHIFT);
881
882 rmesa->hw.zs.cmd[R300_ZS_CNTL_1] |=
883 (translate_stencil_op(ctx->Stencil.FailFunc[0]) <<
884 R300_RB3D_ZS1_FRONT_FAIL_OP_SHIFT)
885 | (translate_stencil_op(ctx->Stencil.ZFailFunc[0]) <<
886 R300_RB3D_ZS1_FRONT_ZFAIL_OP_SHIFT)
887 | (translate_stencil_op(ctx->Stencil.ZPassFunc[0]) <<
888 R300_RB3D_ZS1_FRONT_ZPASS_OP_SHIFT);
889
890 if (ctx->Stencil._TestTwoSide) {
891 rmesa->hw.zs.cmd[R300_ZS_CNTL_1] |=
892 (translate_stencil_op(ctx->Stencil.FailFunc[1]) <<
893 R300_RB3D_ZS1_BACK_FAIL_OP_SHIFT)
894 | (translate_stencil_op(ctx->Stencil.ZFailFunc[1]) <<
895 R300_RB3D_ZS1_BACK_ZFAIL_OP_SHIFT)
896 | (translate_stencil_op(ctx->Stencil.ZPassFunc[1]) <<
897 R300_RB3D_ZS1_BACK_ZPASS_OP_SHIFT);
898 } else {
899 rmesa->hw.zs.cmd[R300_ZS_CNTL_1] |=
900 (translate_stencil_op(ctx->Stencil.FailFunc[0]) <<
901 R300_RB3D_ZS1_BACK_FAIL_OP_SHIFT)
902 | (translate_stencil_op(ctx->Stencil.ZFailFunc[0]) <<
903 R300_RB3D_ZS1_BACK_ZFAIL_OP_SHIFT)
904 | (translate_stencil_op(ctx->Stencil.ZPassFunc[0]) <<
905 R300_RB3D_ZS1_BACK_ZPASS_OP_SHIFT);
906 }
907 }
908
909 static void r300ClearStencil(GLcontext * ctx, GLint s)
910 {
911 r300ContextPtr rmesa = R300_CONTEXT(ctx);
912
913 rmesa->state.stencil.clear =
914 ((GLuint) (ctx->Stencil.Clear & 0xff) |
915 (R300_RB3D_ZS2_STENCIL_MASK <<
916 R300_RB3D_ZS2_STENCIL_MASK_SHIFT) | ((ctx->Stencil.
917 WriteMask[0] & 0xff) <<
918 R300_RB3D_ZS2_STENCIL_WRITE_MASK_SHIFT));
919 }
920
921 /* =============================================================
922 * Window position and viewport transformation
923 */
924
925 /*
926 * To correctly position primitives:
927 */
928 #define SUBPIXEL_X 0.125
929 #define SUBPIXEL_Y 0.125
930
931 static void r300UpdateWindow(GLcontext * ctx)
932 {
933 r300ContextPtr rmesa = R300_CONTEXT(ctx);
934 __DRIdrawablePrivate *dPriv = rmesa->radeon.dri.drawable;
935 GLfloat xoffset = dPriv ? (GLfloat) dPriv->x : 0;
936 GLfloat yoffset = dPriv ? (GLfloat) dPriv->y + dPriv->h : 0;
937 const GLfloat *v = ctx->Viewport._WindowMap.m;
938
939 GLfloat sx = v[MAT_SX];
940 GLfloat tx = v[MAT_TX] + xoffset + SUBPIXEL_X;
941 GLfloat sy = -v[MAT_SY];
942 GLfloat ty = (-v[MAT_TY]) + yoffset + SUBPIXEL_Y;
943 GLfloat sz = v[MAT_SZ] * rmesa->state.depth.scale;
944 GLfloat tz = v[MAT_TZ] * rmesa->state.depth.scale;
945
946 R300_FIREVERTICES(rmesa);
947 R300_STATECHANGE(rmesa, vpt);
948
949 rmesa->hw.vpt.cmd[R300_VPT_XSCALE] = r300PackFloat32(sx);
950 rmesa->hw.vpt.cmd[R300_VPT_XOFFSET] = r300PackFloat32(tx);
951 rmesa->hw.vpt.cmd[R300_VPT_YSCALE] = r300PackFloat32(sy);
952 rmesa->hw.vpt.cmd[R300_VPT_YOFFSET] = r300PackFloat32(ty);
953 rmesa->hw.vpt.cmd[R300_VPT_ZSCALE] = r300PackFloat32(sz);
954 rmesa->hw.vpt.cmd[R300_VPT_ZOFFSET] = r300PackFloat32(tz);
955 }
956
957 static void r300Viewport(GLcontext * ctx, GLint x, GLint y,
958 GLsizei width, GLsizei height)
959 {
960 /* Don't pipeline viewport changes, conflict with window offset
961 * setting below. Could apply deltas to rescue pipelined viewport
962 * values, or keep the originals hanging around.
963 */
964 r300UpdateWindow(ctx);
965 }
966
967 static void r300DepthRange(GLcontext * ctx, GLclampd nearval, GLclampd farval)
968 {
969 r300UpdateWindow(ctx);
970 }
971
972 void r300UpdateViewportOffset(GLcontext * ctx)
973 {
974 r300ContextPtr rmesa = R300_CONTEXT(ctx);
975 __DRIdrawablePrivate *dPriv = ((radeonContextPtr) rmesa)->dri.drawable;
976 GLfloat xoffset = (GLfloat) dPriv->x;
977 GLfloat yoffset = (GLfloat) dPriv->y + dPriv->h;
978 const GLfloat *v = ctx->Viewport._WindowMap.m;
979
980 GLfloat tx = v[MAT_TX] + xoffset + SUBPIXEL_X;
981 GLfloat ty = (-v[MAT_TY]) + yoffset + SUBPIXEL_Y;
982
983 if (rmesa->hw.vpt.cmd[R300_VPT_XOFFSET] != r300PackFloat32(tx) ||
984 rmesa->hw.vpt.cmd[R300_VPT_YOFFSET] != r300PackFloat32(ty)) {
985 /* Note: this should also modify whatever data the context reset
986 * code uses...
987 */
988 R300_STATECHANGE(rmesa, vpt);
989 rmesa->hw.vpt.cmd[R300_VPT_XOFFSET] = r300PackFloat32(tx);
990 rmesa->hw.vpt.cmd[R300_VPT_YOFFSET] = r300PackFloat32(ty);
991
992 }
993
994 radeonUpdateScissor(ctx);
995 }
996
997 /**
998 * Tell the card where to render (offset, pitch).
999 * Effected by glDrawBuffer, etc
1000 */
1001 void r300UpdateDrawBuffer(GLcontext * ctx)
1002 {
1003 r300ContextPtr rmesa = R300_CONTEXT(ctx);
1004 r300ContextPtr r300 = rmesa;
1005 struct gl_framebuffer *fb = ctx->DrawBuffer;
1006 driRenderbuffer *drb;
1007
1008 if (fb->_ColorDrawBufferMask[0] == BUFFER_BIT_FRONT_LEFT) {
1009 /* draw to front */
1010 drb =
1011 (driRenderbuffer *) fb->Attachment[BUFFER_FRONT_LEFT].
1012 Renderbuffer;
1013 } else if (fb->_ColorDrawBufferMask[0] == BUFFER_BIT_BACK_LEFT) {
1014 /* draw to back */
1015 drb =
1016 (driRenderbuffer *) fb->Attachment[BUFFER_BACK_LEFT].
1017 Renderbuffer;
1018 } else {
1019 /* drawing to multiple buffers, or none */
1020 return;
1021 }
1022
1023 assert(drb);
1024 assert(drb->flippedPitch);
1025
1026 R300_STATECHANGE(rmesa, cb);
1027
1028 r300->hw.cb.cmd[R300_CB_OFFSET] = drb->flippedOffset + //r300->radeon.state.color.drawOffset +
1029 r300->radeon.radeonScreen->fbLocation;
1030 r300->hw.cb.cmd[R300_CB_PITCH] = drb->flippedPitch; //r300->radeon.state.color.drawPitch;
1031
1032 if (r300->radeon.radeonScreen->cpp == 4)
1033 r300->hw.cb.cmd[R300_CB_PITCH] |= R300_COLOR_FORMAT_ARGB8888;
1034 else
1035 r300->hw.cb.cmd[R300_CB_PITCH] |= R300_COLOR_FORMAT_RGB565;
1036
1037 if (r300->radeon.sarea->tiling_enabled)
1038 r300->hw.cb.cmd[R300_CB_PITCH] |= R300_COLOR_TILE_ENABLE;
1039 #if 0
1040 R200_STATECHANGE(rmesa, ctx);
1041
1042 /* Note: we used the (possibly) page-flipped values */
1043 rmesa->hw.ctx.cmd[CTX_RB3D_COLOROFFSET]
1044 = ((drb->flippedOffset + rmesa->r200Screen->fbLocation)
1045 & R200_COLOROFFSET_MASK);
1046 rmesa->hw.ctx.cmd[CTX_RB3D_COLORPITCH] = drb->flippedPitch;
1047
1048 if (rmesa->sarea->tiling_enabled) {
1049 rmesa->hw.ctx.cmd[CTX_RB3D_COLORPITCH] |=
1050 R200_COLOR_TILE_ENABLE;
1051 }
1052 #endif
1053 }
1054
1055 static void
1056 r300FetchStateParameter(GLcontext * ctx,
1057 const gl_state_index state[STATE_LENGTH],
1058 GLfloat * value)
1059 {
1060 r300ContextPtr r300 = R300_CONTEXT(ctx);
1061
1062 switch (state[0]) {
1063 case STATE_INTERNAL:
1064 switch (state[1]) {
1065 case STATE_R300_WINDOW_DIMENSION:
1066 value[0] = r300->radeon.dri.drawable->w * 0.5f; /* width*0.5 */
1067 value[1] = r300->radeon.dri.drawable->h * 0.5f; /* height*0.5 */
1068 value[2] = 0.5F; /* for moving range [-1 1] -> [0 1] */
1069 value[3] = 1.0F; /* not used */
1070 break;
1071
1072 case STATE_R300_TEXRECT_FACTOR:{
1073 struct gl_texture_object *t =
1074 ctx->Texture.Unit[state[2]].CurrentRect;
1075
1076 if (t && t->Image[0][t->BaseLevel]) {
1077 struct gl_texture_image *image =
1078 t->Image[0][t->BaseLevel];
1079 value[0] = 1.0 / image->Width2;
1080 value[1] = 1.0 / image->Height2;
1081 } else {
1082 value[0] = 1.0;
1083 value[1] = 1.0;
1084 }
1085 value[2] = 1.0;
1086 value[3] = 1.0;
1087 break;
1088 }
1089
1090 default:
1091 break;
1092 }
1093 break;
1094
1095 default:
1096 break;
1097 }
1098 }
1099
1100 /**
1101 * Update R300's own internal state parameters.
1102 * For now just STATE_R300_WINDOW_DIMENSION
1103 */
1104 void r300UpdateStateParameters(GLcontext * ctx, GLuint new_state)
1105 {
1106 struct r300_fragment_program *fp;
1107 struct gl_program_parameter_list *paramList;
1108 GLuint i;
1109
1110 if (!(new_state & (_NEW_BUFFERS | _NEW_PROGRAM)))
1111 return;
1112
1113 fp = (struct r300_fragment_program *)ctx->FragmentProgram._Current;
1114 if (!fp)
1115 return;
1116
1117 paramList = fp->mesa_program.Base.Parameters;
1118
1119 if (!paramList)
1120 return;
1121
1122 for (i = 0; i < paramList->NumParameters; i++) {
1123 if (paramList->Parameters[i].Type == PROGRAM_STATE_VAR) {
1124 r300FetchStateParameter(ctx,
1125 paramList->Parameters[i].
1126 StateIndexes,
1127 paramList->ParameterValues[i]);
1128 }
1129 }
1130 }
1131
1132 /* =============================================================
1133 * Polygon state
1134 */
1135 static void r300PolygonOffset(GLcontext * ctx, GLfloat factor, GLfloat units)
1136 {
1137 r300ContextPtr rmesa = R300_CONTEXT(ctx);
1138 GLfloat constant = units;
1139
1140 switch (ctx->Visual.depthBits) {
1141 case 16:
1142 constant *= 4.0;
1143 break;
1144 case 24:
1145 constant *= 2.0;
1146 break;
1147 }
1148
1149 factor *= 12.0;
1150
1151 /* fprintf(stderr, "%s f:%f u:%f\n", __FUNCTION__, factor, constant); */
1152
1153 R300_STATECHANGE(rmesa, zbs);
1154 rmesa->hw.zbs.cmd[R300_ZBS_T_FACTOR] = r300PackFloat32(factor);
1155 rmesa->hw.zbs.cmd[R300_ZBS_T_CONSTANT] = r300PackFloat32(constant);
1156 rmesa->hw.zbs.cmd[R300_ZBS_W_FACTOR] = r300PackFloat32(factor);
1157 rmesa->hw.zbs.cmd[R300_ZBS_W_CONSTANT] = r300PackFloat32(constant);
1158 }
1159
1160 /* Routing and texture-related */
1161
1162 /* r300 doesnt handle GL_CLAMP and GL_MIRROR_CLAMP_EXT correctly when filter is NEAREST.
1163 * Since texwrap produces same results for GL_CLAMP and GL_CLAMP_TO_EDGE we use them instead.
1164 * We need to recalculate wrap modes whenever filter mode is changed because someone might do:
1165 * glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
1166 * glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
1167 * glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
1168 * Since r300 completely ignores R300_TX_CLAMP when either min or mag is nearest it cant handle
1169 * combinations where only one of them is nearest.
1170 */
1171 static unsigned long gen_fixed_filter(unsigned long f)
1172 {
1173 unsigned long mag, min, needs_fixing = 0;
1174 //return f;
1175
1176 /* We ignore MIRROR bit so we dont have to do everything twice */
1177 if ((f & ((7 - 1) << R300_TX_WRAP_S_SHIFT)) ==
1178 (R300_TX_CLAMP << R300_TX_WRAP_S_SHIFT)) {
1179 needs_fixing |= 1;
1180 }
1181 if ((f & ((7 - 1) << R300_TX_WRAP_T_SHIFT)) ==
1182 (R300_TX_CLAMP << R300_TX_WRAP_T_SHIFT)) {
1183 needs_fixing |= 2;
1184 }
1185 if ((f & ((7 - 1) << R300_TX_WRAP_Q_SHIFT)) ==
1186 (R300_TX_CLAMP << R300_TX_WRAP_Q_SHIFT)) {
1187 needs_fixing |= 4;
1188 }
1189
1190 if (!needs_fixing)
1191 return f;
1192
1193 mag = f & R300_TX_MAG_FILTER_MASK;
1194 min = f & R300_TX_MIN_FILTER_MASK;
1195
1196 /* TODO: Check for anisto filters too */
1197 if ((mag != R300_TX_MAG_FILTER_NEAREST)
1198 && (min != R300_TX_MIN_FILTER_NEAREST))
1199 return f;
1200
1201 /* r300 cant handle these modes hence we force nearest to linear */
1202 if ((mag == R300_TX_MAG_FILTER_NEAREST)
1203 && (min != R300_TX_MIN_FILTER_NEAREST)) {
1204 f &= ~R300_TX_MAG_FILTER_NEAREST;
1205 f |= R300_TX_MAG_FILTER_LINEAR;
1206 return f;
1207 }
1208
1209 if ((min == R300_TX_MIN_FILTER_NEAREST)
1210 && (mag != R300_TX_MAG_FILTER_NEAREST)) {
1211 f &= ~R300_TX_MIN_FILTER_NEAREST;
1212 f |= R300_TX_MIN_FILTER_LINEAR;
1213 return f;
1214 }
1215
1216 /* Both are nearest */
1217 if (needs_fixing & 1) {
1218 f &= ~((7 - 1) << R300_TX_WRAP_S_SHIFT);
1219 f |= R300_TX_CLAMP_TO_EDGE << R300_TX_WRAP_S_SHIFT;
1220 }
1221 if (needs_fixing & 2) {
1222 f &= ~((7 - 1) << R300_TX_WRAP_T_SHIFT);
1223 f |= R300_TX_CLAMP_TO_EDGE << R300_TX_WRAP_T_SHIFT;
1224 }
1225 if (needs_fixing & 4) {
1226 f &= ~((7 - 1) << R300_TX_WRAP_Q_SHIFT);
1227 f |= R300_TX_CLAMP_TO_EDGE << R300_TX_WRAP_Q_SHIFT;
1228 }
1229 return f;
1230 }
1231
1232 static void r300SetupTextures(GLcontext * ctx)
1233 {
1234 int i, mtu;
1235 struct r300_tex_obj *t;
1236 r300ContextPtr r300 = R300_CONTEXT(ctx);
1237 int hw_tmu = 0;
1238 int last_hw_tmu = -1; /* -1 translates into no setup costs for fields */
1239 int tmu_mappings[R300_MAX_TEXTURE_UNITS] = { -1, };
1240 struct r300_fragment_program *fp = (struct r300_fragment_program *)
1241 (char *)ctx->FragmentProgram._Current;
1242
1243 R300_STATECHANGE(r300, txe);
1244 R300_STATECHANGE(r300, tex.filter);
1245 R300_STATECHANGE(r300, tex.filter_1);
1246 R300_STATECHANGE(r300, tex.size);
1247 R300_STATECHANGE(r300, tex.format);
1248 R300_STATECHANGE(r300, tex.pitch);
1249 R300_STATECHANGE(r300, tex.offset);
1250 R300_STATECHANGE(r300, tex.chroma_key);
1251 R300_STATECHANGE(r300, tex.border_color);
1252
1253 r300->hw.txe.cmd[R300_TXE_ENABLE] = 0x0;
1254
1255 mtu = r300->radeon.glCtx->Const.MaxTextureUnits;
1256 if (RADEON_DEBUG & DEBUG_STATE)
1257 fprintf(stderr, "mtu=%d\n", mtu);
1258
1259 if (mtu > R300_MAX_TEXTURE_UNITS) {
1260 fprintf(stderr,
1261 "Aiiee ! mtu=%d is greater than R300_MAX_TEXTURE_UNITS=%d\n",
1262 mtu, R300_MAX_TEXTURE_UNITS);
1263 _mesa_exit(-1);
1264 }
1265
1266 /* We cannot let disabled tmu offsets pass DRM */
1267 for (i = 0; i < mtu; i++) {
1268 if (ctx->Texture.Unit[i]._ReallyEnabled) {
1269
1270 #if 0 /* Enables old behaviour */
1271 hw_tmu = i;
1272 #endif
1273 tmu_mappings[i] = hw_tmu;
1274
1275 t = r300->state.texture.unit[i].texobj;
1276 /* XXX questionable fix for bug 9170: */
1277 if (!t)
1278 continue;
1279
1280 if ((t->format & 0xffffff00) == 0xffffff00) {
1281 WARN_ONCE
1282 ("unknown texture format (entry %x) encountered. Help me !\n",
1283 t->format & 0xff);
1284 }
1285
1286 if (RADEON_DEBUG & DEBUG_STATE)
1287 fprintf(stderr,
1288 "Activating texture unit %d\n", i);
1289
1290 r300->hw.txe.cmd[R300_TXE_ENABLE] |= (1 << hw_tmu);
1291
1292 r300->hw.tex.filter.cmd[R300_TEX_VALUE_0 +
1293 hw_tmu] =
1294 gen_fixed_filter(t->filter) | (hw_tmu << 28);
1295 /* Currently disabled! */
1296 r300->hw.tex.filter_1.cmd[R300_TEX_VALUE_0 + hw_tmu] = 0x0; //0x20501f80;
1297 r300->hw.tex.size.cmd[R300_TEX_VALUE_0 + hw_tmu] =
1298 t->size;
1299 r300->hw.tex.format.cmd[R300_TEX_VALUE_0 +
1300 hw_tmu] = t->format;
1301 r300->hw.tex.pitch.cmd[R300_TEX_VALUE_0 + hw_tmu] =
1302 t->pitch_reg;
1303 r300->hw.tex.offset.cmd[R300_TEX_VALUE_0 +
1304 hw_tmu] = t->offset;
1305
1306 if (t->offset & R300_TXO_MACRO_TILE) {
1307 WARN_ONCE("macro tiling enabled!\n");
1308 }
1309
1310 if (t->offset & R300_TXO_MICRO_TILE) {
1311 WARN_ONCE("micro tiling enabled!\n");
1312 }
1313
1314 r300->hw.tex.chroma_key.cmd[R300_TEX_VALUE_0 +
1315 hw_tmu] = 0x0;
1316 r300->hw.tex.border_color.cmd[R300_TEX_VALUE_0 +
1317 hw_tmu] =
1318 t->pp_border_color;
1319
1320 last_hw_tmu = hw_tmu;
1321
1322 hw_tmu++;
1323 }
1324 }
1325
1326 r300->hw.tex.filter.cmd[R300_TEX_CMD_0] =
1327 cmdpacket0(R300_TX_FILTER_0, last_hw_tmu + 1);
1328 r300->hw.tex.filter_1.cmd[R300_TEX_CMD_0] =
1329 cmdpacket0(R300_TX_FILTER1_0, last_hw_tmu + 1);
1330 r300->hw.tex.size.cmd[R300_TEX_CMD_0] =
1331 cmdpacket0(R300_TX_SIZE_0, last_hw_tmu + 1);
1332 r300->hw.tex.format.cmd[R300_TEX_CMD_0] =
1333 cmdpacket0(R300_TX_FORMAT_0, last_hw_tmu + 1);
1334 r300->hw.tex.pitch.cmd[R300_TEX_CMD_0] =
1335 cmdpacket0(R300_TX_PITCH_0, last_hw_tmu + 1);
1336 r300->hw.tex.offset.cmd[R300_TEX_CMD_0] =
1337 cmdpacket0(R300_TX_OFFSET_0, last_hw_tmu + 1);
1338 r300->hw.tex.chroma_key.cmd[R300_TEX_CMD_0] =
1339 cmdpacket0(R300_TX_CHROMA_KEY_0, last_hw_tmu + 1);
1340 r300->hw.tex.border_color.cmd[R300_TEX_CMD_0] =
1341 cmdpacket0(R300_TX_BORDER_COLOR_0, last_hw_tmu + 1);
1342
1343 if (!fp) /* should only happenen once, just after context is created */
1344 return;
1345
1346 R300_STATECHANGE(r300, fpt);
1347
1348 for (i = 0; i < fp->tex.length; i++) {
1349 int unit;
1350 int opcode;
1351 unsigned long val;
1352
1353 unit = fp->tex.inst[i] >> R300_FPITX_IMAGE_SHIFT;
1354 unit &= 15;
1355
1356 val = fp->tex.inst[i];
1357 val &= ~R300_FPITX_IMAGE_MASK;
1358
1359 opcode =
1360 (val & R300_FPITX_OPCODE_MASK) >> R300_FPITX_OPCODE_SHIFT;
1361 if (opcode == R300_FPITX_OP_KIL) {
1362 r300->hw.fpt.cmd[R300_FPT_INSTR_0 + i] = val;
1363 } else {
1364 if (tmu_mappings[unit] >= 0) {
1365 val |=
1366 tmu_mappings[unit] <<
1367 R300_FPITX_IMAGE_SHIFT;
1368 r300->hw.fpt.cmd[R300_FPT_INSTR_0 + i] = val;
1369 } else {
1370 // We get here when the corresponding texture image is incomplete
1371 // (e.g. incomplete mipmaps etc.)
1372 r300->hw.fpt.cmd[R300_FPT_INSTR_0 + i] = val;
1373 }
1374 }
1375 }
1376
1377 r300->hw.fpt.cmd[R300_FPT_CMD_0] =
1378 cmdpacket0(R300_PFS_TEXI_0, fp->tex.length);
1379
1380 if (RADEON_DEBUG & DEBUG_STATE)
1381 fprintf(stderr, "TX_ENABLE: %08x last_hw_tmu=%d\n",
1382 r300->hw.txe.cmd[R300_TXE_ENABLE], last_hw_tmu);
1383 }
1384
1385 union r300_outputs_written {
1386 GLuint vp_outputs; /* hw_tcl_on */
1387 DECLARE_RENDERINPUTS(index_bitset); /* !hw_tcl_on */
1388 };
1389
1390 #define R300_OUTPUTS_WRITTEN_TEST(ow, vp_result, tnl_attrib) \
1391 ((hw_tcl_on) ? (ow).vp_outputs & (1 << (vp_result)) : \
1392 RENDERINPUTS_TEST( (ow.index_bitset), (tnl_attrib) ))
1393
1394 static void r300SetupRSUnit(GLcontext * ctx)
1395 {
1396 r300ContextPtr r300 = R300_CONTEXT(ctx);
1397 /* I'm still unsure if these are needed */
1398 GLuint interp_magic[8] = {
1399 0x00,
1400 R300_RS_INTERP_1_UNKNOWN,
1401 R300_RS_INTERP_2_UNKNOWN,
1402 R300_RS_INTERP_3_UNKNOWN,
1403 0x00,
1404 0x00,
1405 0x00,
1406 0x00
1407 };
1408 union r300_outputs_written OutputsWritten;
1409 GLuint InputsRead;
1410 int fp_reg, high_rr;
1411 int in_texcoords, col_interp_nr;
1412 int i;
1413
1414 if (hw_tcl_on)
1415 OutputsWritten.vp_outputs = CURRENT_VERTEX_SHADER(ctx)->key.OutputsWritten;
1416 else
1417 RENDERINPUTS_COPY(OutputsWritten.index_bitset, r300->state.render_inputs_bitset);
1418
1419 if (ctx->FragmentProgram._Current)
1420 InputsRead = ctx->FragmentProgram._Current->Base.InputsRead;
1421 else {
1422 fprintf(stderr, "No ctx->FragmentProgram._Current!!\n");
1423 return; /* This should only ever happen once.. */
1424 }
1425
1426 R300_STATECHANGE(r300, ri);
1427 R300_STATECHANGE(r300, rc);
1428 R300_STATECHANGE(r300, rr);
1429
1430 fp_reg = in_texcoords = col_interp_nr = high_rr = 0;
1431
1432 r300->hw.rr.cmd[R300_RR_ROUTE_1] = 0;
1433
1434 if (InputsRead & FRAG_BIT_WPOS) {
1435 for (i = 0; i < ctx->Const.MaxTextureUnits; i++)
1436 if (!(InputsRead & (FRAG_BIT_TEX0 << i)))
1437 break;
1438
1439 if (i == ctx->Const.MaxTextureUnits) {
1440 fprintf(stderr, "\tno free texcoord found...\n");
1441 _mesa_exit(-1);
1442 }
1443
1444 InputsRead |= (FRAG_BIT_TEX0 << i);
1445 InputsRead &= ~FRAG_BIT_WPOS;
1446 }
1447
1448 for (i = 0; i < ctx->Const.MaxTextureUnits; i++) {
1449 r300->hw.ri.cmd[R300_RI_INTERP_0 + i] = 0 | R300_RS_INTERP_USED | (in_texcoords << R300_RS_INTERP_SRC_SHIFT)
1450 | interp_magic[i];
1451
1452 r300->hw.rr.cmd[R300_RR_ROUTE_0 + fp_reg] = 0;
1453 if (InputsRead & (FRAG_BIT_TEX0 << i)) {
1454 //assert(r300->state.texture.tc_count != 0);
1455 r300->hw.rr.cmd[R300_RR_ROUTE_0 + fp_reg] |= R300_RS_ROUTE_ENABLE | i /* source INTERP */
1456 | (fp_reg << R300_RS_ROUTE_DEST_SHIFT);
1457 high_rr = fp_reg;
1458
1459 /* Passing invalid data here can lock the GPU. */
1460 if (R300_OUTPUTS_WRITTEN_TEST(OutputsWritten, VERT_RESULT_TEX0 + i, _TNL_ATTRIB_TEX(i))) {
1461 InputsRead &= ~(FRAG_BIT_TEX0 << i);
1462 fp_reg++;
1463 } else {
1464 WARN_ONCE("fragprog wants coords for tex%d, vp doesn't provide them!\n", i);
1465 }
1466 }
1467 /* Need to count all coords enabled at vof */
1468 if (R300_OUTPUTS_WRITTEN_TEST(OutputsWritten, VERT_RESULT_TEX0 + i, _TNL_ATTRIB_TEX(i))) {
1469 in_texcoords++;
1470 }
1471 }
1472
1473 if (InputsRead & FRAG_BIT_COL0) {
1474 if (R300_OUTPUTS_WRITTEN_TEST(OutputsWritten, VERT_RESULT_COL0, _TNL_ATTRIB_COLOR0)) {
1475 r300->hw.rr.cmd[R300_RR_ROUTE_0] |= 0 | R300_RS_ROUTE_0_COLOR | (fp_reg++ << R300_RS_ROUTE_0_COLOR_DEST_SHIFT);
1476 InputsRead &= ~FRAG_BIT_COL0;
1477 col_interp_nr++;
1478 } else {
1479 WARN_ONCE("fragprog wants col0, vp doesn't provide it\n");
1480 }
1481 }
1482
1483 if (InputsRead & FRAG_BIT_COL1) {
1484 if (R300_OUTPUTS_WRITTEN_TEST(OutputsWritten, VERT_RESULT_COL1, _TNL_ATTRIB_COLOR1)) {
1485 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);
1486 InputsRead &= ~FRAG_BIT_COL1;
1487 if (high_rr < 1)
1488 high_rr = 1;
1489 col_interp_nr++;
1490 } else {
1491 WARN_ONCE("fragprog wants col1, vp doesn't provide it\n");
1492 }
1493 }
1494
1495 /* Need at least one. This might still lock as the values are undefined... */
1496 if (in_texcoords == 0 && col_interp_nr == 0) {
1497 r300->hw.rr.cmd[R300_RR_ROUTE_0] |= 0 | R300_RS_ROUTE_0_COLOR | (fp_reg++ << R300_RS_ROUTE_0_COLOR_DEST_SHIFT);
1498 col_interp_nr++;
1499 }
1500
1501 r300->hw.rc.cmd[1] = 0 | (in_texcoords << R300_RS_CNTL_TC_CNT_SHIFT)
1502 | (col_interp_nr << R300_RS_CNTL_CI_CNT_SHIFT)
1503 | R300_RS_CNTL_0_UNKNOWN_18;
1504
1505 assert(high_rr >= 0);
1506 r300->hw.rr.cmd[R300_RR_CMD_0] = cmdpacket0(R300_RS_ROUTE_0, high_rr + 1);
1507 r300->hw.rc.cmd[2] = 0xC0 | high_rr;
1508
1509 if (InputsRead)
1510 WARN_ONCE("Don't know how to satisfy InputsRead=0x%08x\n", InputsRead);
1511 }
1512
1513 #define bump_vpu_count(ptr, new_count) do{\
1514 drm_r300_cmd_header_t* _p=((drm_r300_cmd_header_t*)(ptr));\
1515 int _nc=(new_count)/4; \
1516 assert(_nc < 256); \
1517 if(_nc>_p->vpu.count)_p->vpu.count=_nc;\
1518 }while(0)
1519
1520 static inline void r300SetupVertexProgramFragment(r300ContextPtr r300, int dest, struct r300_vertex_shader_fragment *vsf)
1521 {
1522 int i;
1523
1524 if (vsf->length == 0)
1525 return;
1526
1527 if (vsf->length & 0x3) {
1528 fprintf(stderr, "VERTEX_SHADER_FRAGMENT must have length divisible by 4\n");
1529 _mesa_exit(-1);
1530 }
1531
1532 switch ((dest >> 8) & 0xf) {
1533 case 0:
1534 R300_STATECHANGE(r300, vpi);
1535 for (i = 0; i < vsf->length; i++)
1536 r300->hw.vpi.cmd[R300_VPI_INSTR_0 + i + 4 * (dest & 0xff)] = (vsf->body.d[i]);
1537 bump_vpu_count(r300->hw.vpi.cmd, vsf->length + 4 * (dest & 0xff));
1538 break;
1539
1540 case 2:
1541 R300_STATECHANGE(r300, vpp);
1542 for (i = 0; i < vsf->length; i++)
1543 r300->hw.vpp.cmd[R300_VPP_PARAM_0 + i + 4 * (dest & 0xff)] = (vsf->body.d[i]);
1544 bump_vpu_count(r300->hw.vpp.cmd, vsf->length + 4 * (dest & 0xff));
1545 break;
1546 case 4:
1547 R300_STATECHANGE(r300, vps);
1548 for (i = 0; i < vsf->length; i++)
1549 r300->hw.vps.cmd[1 + i + 4 * (dest & 0xff)] = (vsf->body.d[i]);
1550 bump_vpu_count(r300->hw.vps.cmd, vsf->length + 4 * (dest & 0xff));
1551 break;
1552 default:
1553 fprintf(stderr, "%s:%s don't know how to handle dest %04x\n", __FILE__, __FUNCTION__, dest);
1554 _mesa_exit(-1);
1555 }
1556 }
1557
1558 static void r300SetupDefaultVertexProgram(r300ContextPtr rmesa)
1559 {
1560 struct r300_vertex_shader_state *prog = &(rmesa->state.vertex_shader);
1561 GLuint o_reg = 0;
1562 int i;
1563 int inst_count = 0;
1564 int param_count = 0;
1565 int program_end = 0;
1566
1567 for (i = VERT_ATTRIB_POS; i < VERT_ATTRIB_MAX; i++) {
1568 if (rmesa->state.sw_tcl_inputs[i] != -1) {
1569 prog->program.body.i[program_end + 0] = EASY_VSF_OP(MUL, o_reg++, ALL, RESULT);
1570 prog->program.body.i[program_end + 1] = VSF_REG(rmesa->state.sw_tcl_inputs[i]);
1571 prog->program.body.i[program_end + 2] = VSF_ATTR_UNITY(rmesa->state.sw_tcl_inputs[i]);
1572 prog->program.body.i[program_end + 3] = VSF_UNITY(rmesa->state.sw_tcl_inputs[i]);
1573 program_end += 4;
1574 }
1575 }
1576
1577 prog->program.length = program_end;
1578
1579 r300SetupVertexProgramFragment(rmesa, R300_PVS_UPLOAD_PROGRAM,
1580 &(prog->program));
1581 inst_count = (prog->program.length / 4) - 1;
1582
1583 R300_STATECHANGE(rmesa, pvs);
1584 rmesa->hw.pvs.cmd[R300_PVS_CNTL_1] =
1585 (0 << R300_PVS_CNTL_1_PROGRAM_START_SHIFT) |
1586 (inst_count << R300_PVS_CNTL_1_POS_END_SHIFT) |
1587 (inst_count << R300_PVS_CNTL_1_PROGRAM_END_SHIFT);
1588 rmesa->hw.pvs.cmd[R300_PVS_CNTL_2] =
1589 (0 << R300_PVS_CNTL_2_PARAM_OFFSET_SHIFT) |
1590 (param_count << R300_PVS_CNTL_2_PARAM_COUNT_SHIFT);
1591 rmesa->hw.pvs.cmd[R300_PVS_CNTL_3] =
1592 (inst_count << R300_PVS_CNTL_3_PROGRAM_UNKNOWN_SHIFT) |
1593 (inst_count << R300_PVS_CNTL_3_PROGRAM_UNKNOWN2_SHIFT);
1594 }
1595
1596 static void r300SetupRealVertexProgram(r300ContextPtr rmesa)
1597 {
1598 GLcontext *ctx = rmesa->radeon.glCtx;
1599 struct r300_vertex_program *prog = (struct r300_vertex_program *)CURRENT_VERTEX_SHADER(ctx);
1600 int inst_count = 0;
1601 int param_count = 0;
1602
1603 /* FIXME: r300SetupVertexProgramFragment */
1604 R300_STATECHANGE(rmesa, vpp);
1605 param_count =
1606 r300VertexProgUpdateParams(ctx,
1607 (struct r300_vertex_program_cont *)
1608 ctx->VertexProgram._Current,
1609 (float *)&rmesa->hw.vpp.
1610 cmd[R300_VPP_PARAM_0]);
1611 bump_vpu_count(rmesa->hw.vpp.cmd, param_count);
1612 param_count /= 4;
1613
1614 r300SetupVertexProgramFragment(rmesa, R300_PVS_UPLOAD_PROGRAM, &(prog->program));
1615 inst_count = (prog->program.length / 4) - 1;
1616
1617 R300_STATECHANGE(rmesa, pvs);
1618 rmesa->hw.pvs.cmd[R300_PVS_CNTL_1] =
1619 (0 << R300_PVS_CNTL_1_PROGRAM_START_SHIFT) |
1620 (inst_count << R300_PVS_CNTL_1_POS_END_SHIFT) |
1621 (inst_count << R300_PVS_CNTL_1_PROGRAM_END_SHIFT);
1622 rmesa->hw.pvs.cmd[R300_PVS_CNTL_2] =
1623 (0 << R300_PVS_CNTL_2_PARAM_OFFSET_SHIFT) |
1624 (param_count << R300_PVS_CNTL_2_PARAM_COUNT_SHIFT);
1625 rmesa->hw.pvs.cmd[R300_PVS_CNTL_3] =
1626 (inst_count << R300_PVS_CNTL_3_PROGRAM_UNKNOWN_SHIFT) |
1627 (inst_count << R300_PVS_CNTL_3_PROGRAM_UNKNOWN2_SHIFT);
1628 }
1629
1630 static void r300SetupVertexProgram(r300ContextPtr rmesa)
1631 {
1632 GLcontext *ctx = rmesa->radeon.glCtx;
1633
1634 /* Reset state, in case we don't use something */
1635 ((drm_r300_cmd_header_t *) rmesa->hw.vpp.cmd)->vpu.count = 0;
1636 ((drm_r300_cmd_header_t *) rmesa->hw.vpi.cmd)->vpu.count = 0;
1637 ((drm_r300_cmd_header_t *) rmesa->hw.vps.cmd)->vpu.count = 0;
1638
1639 /* Not sure why this doesnt work...
1640 0x400 area might have something to do with pixel shaders as it appears right after pfs programming.
1641 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. */
1642 //setup_vertex_shader_fragment(rmesa, 0x406, &unk4);
1643 if (hw_tcl_on && ((struct r300_vertex_program *)CURRENT_VERTEX_SHADER(ctx))->translated) {
1644 r300SetupRealVertexProgram(rmesa);
1645 } else {
1646 /* FIXME: This needs to be replaced by vertex shader generation code. */
1647 r300SetupDefaultVertexProgram(rmesa);
1648 }
1649
1650
1651 /* FIXME: This is done for vertex shader fragments, but also needs to be
1652 * done for vap_pvs, so I leave it as a reminder. */
1653 #if 0
1654 reg_start(R300_VAP_PVS_WAITIDLE, 0);
1655 e32(0x00000000);
1656 #endif
1657 }
1658
1659 /**
1660 * Enable/Disable states.
1661 *
1662 * \note Mesa already filters redundant calls to this function.
1663 */
1664 static void r300Enable(GLcontext * ctx, GLenum cap, GLboolean state)
1665 {
1666 r300ContextPtr r300 = R300_CONTEXT(ctx);
1667
1668 if (RADEON_DEBUG & DEBUG_STATE)
1669 fprintf(stderr, "%s( %s = %s )\n", __FUNCTION__,
1670 _mesa_lookup_enum_by_nr(cap),
1671 state ? "GL_TRUE" : "GL_FALSE");
1672
1673 switch (cap) {
1674 /* Fast track this one...
1675 */
1676 case GL_TEXTURE_1D:
1677 case GL_TEXTURE_2D:
1678 case GL_TEXTURE_3D:
1679 break;
1680
1681 case GL_FOG:
1682 R300_STATECHANGE(r300, fogs);
1683 if (state) {
1684 r300->hw.fogs.cmd[R300_FOGS_STATE] |= R300_FOG_ENABLE;
1685
1686 r300Fogfv(ctx, GL_FOG_MODE, NULL);
1687 r300Fogfv(ctx, GL_FOG_DENSITY, &ctx->Fog.Density);
1688 r300Fogfv(ctx, GL_FOG_START, &ctx->Fog.Start);
1689 r300Fogfv(ctx, GL_FOG_END, &ctx->Fog.End);
1690 r300Fogfv(ctx, GL_FOG_COLOR, ctx->Fog.Color);
1691 } else {
1692 r300->hw.fogs.cmd[R300_FOGS_STATE] &= ~R300_FOG_ENABLE;
1693 }
1694
1695 break;
1696
1697 case GL_ALPHA_TEST:
1698 r300SetAlphaState(ctx);
1699 break;
1700
1701 case GL_BLEND:
1702 case GL_COLOR_LOGIC_OP:
1703 r300SetBlendState(ctx);
1704 break;
1705
1706 case GL_DEPTH_TEST:
1707 r300SetDepthState(ctx);
1708 break;
1709
1710 case GL_STENCIL_TEST:
1711 if (r300->state.stencil.hw_stencil) {
1712 R300_STATECHANGE(r300, zs);
1713 if (state) {
1714 r300->hw.zs.cmd[R300_ZS_CNTL_0] |=
1715 R300_RB3D_STENCIL_ENABLE;
1716 } else {
1717 r300->hw.zs.cmd[R300_ZS_CNTL_0] &=
1718 ~R300_RB3D_STENCIL_ENABLE;
1719 }
1720 } else {
1721 #if R200_MERGED
1722 FALLBACK(&r300->radeon, RADEON_FALLBACK_STENCIL, state);
1723 #endif
1724 }
1725 break;
1726
1727 case GL_CULL_FACE:
1728 r300UpdateCulling(ctx);
1729 break;
1730
1731 case GL_POLYGON_OFFSET_POINT:
1732 case GL_POLYGON_OFFSET_LINE:
1733 case GL_POLYGON_OFFSET_FILL:
1734 R300_STATECHANGE(r300, occlusion_cntl);
1735 if (state) {
1736 r300->hw.occlusion_cntl.cmd[1] |= (3 << 0);
1737 } else {
1738 r300->hw.occlusion_cntl.cmd[1] &= ~(3 << 0);
1739 }
1740 break;
1741 default:
1742 radeonEnable(ctx, cap, state);
1743 return;
1744 }
1745 }
1746
1747 /**
1748 * Completely recalculates hardware state based on the Mesa state.
1749 */
1750 static void r300ResetHwState(r300ContextPtr r300)
1751 {
1752 GLcontext *ctx = r300->radeon.glCtx;
1753 int has_tcl = 1;
1754
1755 if (!(r300->radeon.radeonScreen->chip_flags & RADEON_CHIPSET_TCL))
1756 has_tcl = 0;
1757
1758 if (RADEON_DEBUG & DEBUG_STATE)
1759 fprintf(stderr, "%s\n", __FUNCTION__);
1760
1761 r300UpdateWindow(ctx);
1762
1763 r300ColorMask(ctx,
1764 ctx->Color.ColorMask[RCOMP],
1765 ctx->Color.ColorMask[GCOMP],
1766 ctx->Color.ColorMask[BCOMP], ctx->Color.ColorMask[ACOMP]);
1767
1768 r300Enable(ctx, GL_DEPTH_TEST, ctx->Depth.Test);
1769 r300DepthMask(ctx, ctx->Depth.Mask);
1770 r300DepthFunc(ctx, ctx->Depth.Func);
1771
1772 /* stencil */
1773 r300Enable(ctx, GL_STENCIL_TEST, ctx->Stencil.Enabled);
1774 r300StencilMaskSeparate(ctx, 0, ctx->Stencil.WriteMask[0]);
1775 r300StencilFuncSeparate(ctx, 0, ctx->Stencil.Function[0],
1776 ctx->Stencil.Ref[0], ctx->Stencil.ValueMask[0]);
1777 r300StencilOpSeparate(ctx, 0, ctx->Stencil.FailFunc[0],
1778 ctx->Stencil.ZFailFunc[0],
1779 ctx->Stencil.ZPassFunc[0]);
1780
1781 r300UpdateCulling(ctx);
1782
1783 r300UpdateTextureState(ctx);
1784
1785 r300SetBlendState(ctx);
1786
1787 r300AlphaFunc(ctx, ctx->Color.AlphaFunc, ctx->Color.AlphaRef);
1788 r300Enable(ctx, GL_ALPHA_TEST, ctx->Color.AlphaEnabled);
1789
1790 if (!has_tcl)
1791 r300->hw.vap_cntl.cmd[1] = 0x0014045a;
1792 else
1793 r300->hw.vap_cntl.cmd[1] = 0x0030045A; //0x0030065a /* Dangerous */
1794
1795 r300->hw.vte.cmd[1] = R300_VPORT_X_SCALE_ENA
1796 | R300_VPORT_X_OFFSET_ENA
1797 | R300_VPORT_Y_SCALE_ENA
1798 | R300_VPORT_Y_OFFSET_ENA
1799 | R300_VPORT_Z_SCALE_ENA
1800 | R300_VPORT_Z_OFFSET_ENA | R300_VTX_W0_FMT;
1801 r300->hw.vte.cmd[2] = 0x00000008;
1802
1803 r300->hw.unk2134.cmd[1] = 0x00FFFFFF;
1804 r300->hw.unk2134.cmd[2] = 0x00000000;
1805
1806 #ifdef MESA_LITTLE_ENDIAN
1807 r300->hw.vap_cntl_status.cmd[1] = R300_VC_NO_SWAP;
1808 #else
1809 r300->hw.vap_cntl_status.cmd[1] = R300_VC_32BIT_SWAP;
1810 #endif
1811
1812 /* disable VAP/TCL on non-TCL capable chips */
1813 if (!has_tcl)
1814 r300->hw.vap_cntl_status.cmd[1] |= R300_VAP_TCL_BYPASS;
1815
1816 r300->hw.unk21DC.cmd[1] = 0xAAAAAAAA;
1817
1818 r300->hw.unk221C.cmd[1] = R300_221C_NORMAL;
1819
1820 r300->hw.vap_clip.cmd[1] = r300PackFloat32(1.0); /* X */
1821 r300->hw.vap_clip.cmd[2] = r300PackFloat32(1.0); /* X */
1822 r300->hw.vap_clip.cmd[3] = r300PackFloat32(1.0); /* Y */
1823 r300->hw.vap_clip.cmd[4] = r300PackFloat32(1.0); /* Y */
1824
1825 /* XXX: Other families? */
1826 if (has_tcl) {
1827 switch (r300->radeon.radeonScreen->chip_family) {
1828 case CHIP_FAMILY_R300:
1829 r300->hw.unk2288.cmd[1] = R300_2288_R300;
1830 break;
1831 default:
1832 r300->hw.unk2288.cmd[1] = R300_2288_RV350;
1833 break;
1834 }
1835 }
1836
1837 r300->hw.gb_enable.cmd[1] = R300_GB_POINT_STUFF_ENABLE
1838 | R300_GB_LINE_STUFF_ENABLE
1839 | R300_GB_TRIANGLE_STUFF_ENABLE /*| R300_GB_UNK31 */ ;
1840
1841 r300->hw.gb_misc.cmd[R300_GB_MISC_MSPOS_0] = 0x66666666;
1842 r300->hw.gb_misc.cmd[R300_GB_MISC_MSPOS_1] = 0x06666666;
1843
1844 /* XXX: Other families? */
1845 r300->hw.gb_misc.cmd[R300_GB_MISC_TILE_CONFIG] =
1846 R300_GB_TILE_ENABLE | R300_GB_TILE_SIZE_16;
1847 switch (r300->radeon.radeonScreen->chip_family) {
1848 case CHIP_FAMILY_R300:
1849 case CHIP_FAMILY_R350:
1850 r300->hw.gb_misc.cmd[R300_GB_MISC_TILE_CONFIG] |=
1851 R300_GB_TILE_PIPE_COUNT_R300;
1852 break;
1853 case CHIP_FAMILY_RV410:
1854 r300->hw.gb_misc.cmd[R300_GB_MISC_TILE_CONFIG] |=
1855 R300_GB_TILE_PIPE_COUNT_RV410;
1856 break;
1857 case CHIP_FAMILY_R420:
1858 r300->hw.gb_misc.cmd[R300_GB_MISC_TILE_CONFIG] |=
1859 R300_GB_TILE_PIPE_COUNT_R420;
1860 break;
1861 default:
1862 r300->hw.gb_misc.cmd[R300_GB_MISC_TILE_CONFIG] |=
1863 R300_GB_TILE_PIPE_COUNT_RV300;
1864 break;
1865 }
1866
1867 /* XXX: set to 0 when fog is disabled? */
1868 r300->hw.gb_misc.cmd[R300_GB_MISC_SELECT] = R300_GB_FOG_SELECT_1_1_W;
1869
1870 /* XXX: Enable anti-aliasing? */
1871 r300->hw.gb_misc.cmd[R300_GB_MISC_AA_CONFIG] = R300_AA_DISABLE;
1872
1873 r300->hw.unk4200.cmd[1] = r300PackFloat32(0.0);
1874 r300->hw.unk4200.cmd[2] = r300PackFloat32(0.0);
1875 r300->hw.unk4200.cmd[3] = r300PackFloat32(1.0);
1876 r300->hw.unk4200.cmd[4] = r300PackFloat32(1.0);
1877
1878 r300->hw.unk4214.cmd[1] = 0x00050005;
1879
1880 r300PointSize(ctx, 1.0);
1881
1882 r300->hw.unk4230.cmd[1] = 0x18000006;
1883 r300->hw.unk4230.cmd[2] = 0x00020006;
1884 r300->hw.unk4230.cmd[3] = r300PackFloat32(1.0 / 192.0);
1885
1886 r300LineWidth(ctx, 1.0);
1887
1888 r300->hw.unk4260.cmd[1] = 0;
1889 r300->hw.unk4260.cmd[2] = r300PackFloat32(0.0);
1890 r300->hw.unk4260.cmd[3] = r300PackFloat32(1.0);
1891
1892 r300ShadeModel(ctx, ctx->Light.ShadeModel);
1893
1894 r300PolygonMode(ctx, GL_FRONT, ctx->Polygon.FrontMode);
1895 r300PolygonMode(ctx, GL_BACK, ctx->Polygon.BackMode);
1896 r300->hw.zbias_cntl.cmd[1] = 0x00000000;
1897
1898 r300PolygonOffset(ctx, ctx->Polygon.OffsetFactor,
1899 ctx->Polygon.OffsetUnits);
1900 r300Enable(ctx, GL_POLYGON_OFFSET_POINT, ctx->Polygon.OffsetPoint);
1901 r300Enable(ctx, GL_POLYGON_OFFSET_LINE, ctx->Polygon.OffsetLine);
1902 r300Enable(ctx, GL_POLYGON_OFFSET_FILL, ctx->Polygon.OffsetFill);
1903
1904 r300->hw.unk42C0.cmd[1] = 0x4B7FFFFF;
1905 r300->hw.unk42C0.cmd[2] = 0x00000000;
1906
1907 r300->hw.unk43A4.cmd[1] = 0x0000001C;
1908 r300->hw.unk43A4.cmd[2] = 0x2DA49525;
1909
1910 r300->hw.unk43E8.cmd[1] = 0x00FFFFFF;
1911
1912 r300->hw.unk46A4.cmd[1] = 0x00001B01;
1913 r300->hw.unk46A4.cmd[2] = 0x00001B0F;
1914 r300->hw.unk46A4.cmd[3] = 0x00001B0F;
1915 r300->hw.unk46A4.cmd[4] = 0x00001B0F;
1916 r300->hw.unk46A4.cmd[5] = 0x00000001;
1917
1918 r300Enable(ctx, GL_FOG, ctx->Fog.Enabled);
1919 r300Fogfv(ctx, GL_FOG_MODE, NULL);
1920 r300Fogfv(ctx, GL_FOG_DENSITY, &ctx->Fog.Density);
1921 r300Fogfv(ctx, GL_FOG_START, &ctx->Fog.Start);
1922 r300Fogfv(ctx, GL_FOG_END, &ctx->Fog.End);
1923 r300Fogfv(ctx, GL_FOG_COLOR, ctx->Fog.Color);
1924 r300Fogfv(ctx, GL_FOG_COORDINATE_SOURCE_EXT, NULL);
1925
1926 r300->hw.unk4BD8.cmd[1] = 0;
1927
1928 r300->hw.unk4E00.cmd[1] = 0;
1929
1930 r300BlendColor(ctx, ctx->Color.BlendColor);
1931
1932 /* Again, r300ClearBuffer uses this */
1933 r300->hw.cb.cmd[R300_CB_OFFSET] =
1934 r300->radeon.state.color.drawOffset +
1935 r300->radeon.radeonScreen->fbLocation;
1936 r300->hw.cb.cmd[R300_CB_PITCH] = r300->radeon.state.color.drawPitch;
1937
1938 if (r300->radeon.radeonScreen->cpp == 4)
1939 r300->hw.cb.cmd[R300_CB_PITCH] |= R300_COLOR_FORMAT_ARGB8888;
1940 else
1941 r300->hw.cb.cmd[R300_CB_PITCH] |= R300_COLOR_FORMAT_RGB565;
1942
1943 if (r300->radeon.sarea->tiling_enabled)
1944 r300->hw.cb.cmd[R300_CB_PITCH] |= R300_COLOR_TILE_ENABLE;
1945
1946 r300->hw.unk4E50.cmd[1] = 0;
1947 r300->hw.unk4E50.cmd[2] = 0;
1948 r300->hw.unk4E50.cmd[3] = 0;
1949 r300->hw.unk4E50.cmd[4] = 0;
1950 r300->hw.unk4E50.cmd[5] = 0;
1951 r300->hw.unk4E50.cmd[6] = 0;
1952 r300->hw.unk4E50.cmd[7] = 0;
1953 r300->hw.unk4E50.cmd[8] = 0;
1954 r300->hw.unk4E50.cmd[9] = 0;
1955
1956 r300->hw.unk4E88.cmd[1] = 0;
1957
1958 r300->hw.unk4EA0.cmd[1] = 0x00000000;
1959 r300->hw.unk4EA0.cmd[2] = 0xffffffff;
1960
1961 r300->hw.zb.cmd[R300_ZB_OFFSET] =
1962 r300->radeon.radeonScreen->depthOffset +
1963 r300->radeon.radeonScreen->fbLocation;
1964 r300->hw.zb.cmd[R300_ZB_PITCH] = r300->radeon.radeonScreen->depthPitch;
1965
1966 if (r300->radeon.sarea->tiling_enabled) {
1967 /* XXX: Turn off when clearing buffers ? */
1968 r300->hw.zb.cmd[R300_ZB_PITCH] |= R300_DEPTH_TILE_ENABLE;
1969
1970 if (ctx->Visual.depthBits == 24)
1971 r300->hw.zb.cmd[R300_ZB_PITCH] |=
1972 R300_DEPTH_MICROTILE_ENABLE;
1973 }
1974
1975 r300->hw.unk4F28.cmd[1] = 0;
1976
1977 r300->hw.unk4F30.cmd[1] = 0;
1978 r300->hw.unk4F30.cmd[2] = 0;
1979
1980 r300->hw.unk4F44.cmd[1] = 0;
1981
1982 r300->hw.unk4F54.cmd[1] = 0;
1983
1984 if (has_tcl) {
1985 r300->hw.vps.cmd[R300_VPS_ZERO_0] = 0;
1986 r300->hw.vps.cmd[R300_VPS_ZERO_1] = 0;
1987 r300->hw.vps.cmd[R300_VPS_POINTSIZE] = r300PackFloat32(1.0);
1988 r300->hw.vps.cmd[R300_VPS_ZERO_3] = 0;
1989 }
1990
1991 r300->hw.all_dirty = GL_TRUE;
1992 }
1993
1994 void r300UpdateShaders(r300ContextPtr rmesa)
1995 {
1996 GLcontext *ctx;
1997 struct r300_vertex_program *vp;
1998 int i;
1999
2000 ctx = rmesa->radeon.glCtx;
2001
2002 if (rmesa->NewGLState && hw_tcl_on) {
2003 rmesa->NewGLState = 0;
2004
2005 for (i = _TNL_FIRST_MAT; i <= _TNL_LAST_MAT; i++) {
2006 rmesa->temp_attrib[i] =
2007 TNL_CONTEXT(ctx)->vb.AttribPtr[i];
2008 TNL_CONTEXT(ctx)->vb.AttribPtr[i] =
2009 &rmesa->dummy_attrib[i];
2010 }
2011
2012 _tnl_UpdateFixedFunctionProgram(ctx);
2013
2014 for (i = _TNL_FIRST_MAT; i <= _TNL_LAST_MAT; i++) {
2015 TNL_CONTEXT(ctx)->vb.AttribPtr[i] =
2016 rmesa->temp_attrib[i];
2017 }
2018
2019 r300SelectVertexShader(rmesa);
2020 vp = (struct r300_vertex_program *)
2021 CURRENT_VERTEX_SHADER(ctx);
2022 /*if (vp->translated == GL_FALSE)
2023 r300TranslateVertexShader(vp); */
2024 if (vp->translated == GL_FALSE) {
2025 fprintf(stderr, "Failing back to sw-tcl\n");
2026 hw_tcl_on = future_hw_tcl_on = 0;
2027 r300ResetHwState(rmesa);
2028
2029 return;
2030 }
2031 r300UpdateStateParameters(ctx, _NEW_PROGRAM);
2032 }
2033 }
2034
2035 static void r300SetupPixelShader(r300ContextPtr rmesa)
2036 {
2037 GLcontext *ctx = rmesa->radeon.glCtx;
2038 struct r300_fragment_program *fp = (struct r300_fragment_program *)
2039 (char *)ctx->FragmentProgram._Current;
2040 int i, k;
2041
2042 if (!fp) /* should only happenen once, just after context is created */
2043 return;
2044
2045 r300TranslateFragmentShader(rmesa, fp);
2046 if (!fp->translated) {
2047 fprintf(stderr, "%s: No valid fragment shader, exiting\n",
2048 __FUNCTION__);
2049 return;
2050 }
2051
2052 R300_STATECHANGE(rmesa, fpi[0]);
2053 rmesa->hw.fpi[0].cmd[R300_FPI_CMD_0] = cmdpacket0(R300_PFS_INSTR0_0, fp->alu_end + 1);
2054 for (i = 0; i <= fp->alu_end; i++) {
2055 rmesa->hw.fpi[0].cmd[R300_FPI_INSTR_0 + i] = fp->alu.inst[i].inst0;
2056 }
2057
2058 R300_STATECHANGE(rmesa, fpi[1]);
2059 rmesa->hw.fpi[1].cmd[R300_FPI_CMD_0] = cmdpacket0(R300_PFS_INSTR1_0, fp->alu_end + 1);
2060 for (i = 0; i <= fp->alu_end; i++) {
2061 rmesa->hw.fpi[1].cmd[R300_FPI_INSTR_0 + i] = fp->alu.inst[i].inst1;
2062 }
2063
2064 R300_STATECHANGE(rmesa, fpi[2]);
2065 rmesa->hw.fpi[2].cmd[R300_FPI_CMD_0] = cmdpacket0(R300_PFS_INSTR2_0, fp->alu_end + 1);
2066 for (i = 0; i <= fp->alu_end; i++) {
2067 rmesa->hw.fpi[2].cmd[R300_FPI_INSTR_0 + i] = fp->alu.inst[i].inst2;
2068 }
2069
2070 R300_STATECHANGE(rmesa, fpi[3]);
2071 rmesa->hw.fpi[3].cmd[R300_FPI_CMD_0] = cmdpacket0(R300_PFS_INSTR3_0, fp->alu_end + 1);
2072 for (i = 0; i <= fp->alu_end; i++) {
2073 rmesa->hw.fpi[3].cmd[R300_FPI_INSTR_0 + i] = fp->alu.inst[i].inst3;
2074 }
2075
2076 R300_STATECHANGE(rmesa, fp);
2077 rmesa->hw.fp.cmd[R300_FP_CNTL0] = fp->cur_node | (fp->first_node_has_tex << 3);
2078 rmesa->hw.fp.cmd[R300_FP_CNTL1] = fp->max_temp_idx;
2079 rmesa->hw.fp.cmd[R300_FP_CNTL2] =
2080 (fp->alu_offset << R300_PFS_CNTL_ALU_OFFSET_SHIFT) |
2081 (fp->alu_end << R300_PFS_CNTL_ALU_END_SHIFT) |
2082 (fp->tex_offset << R300_PFS_CNTL_TEX_OFFSET_SHIFT) |
2083 (fp->tex_end << R300_PFS_CNTL_TEX_END_SHIFT);
2084 /* I just want to say, the way these nodes are stored.. weird.. */
2085 for (i = 0, k = (4 - (fp->cur_node + 1)); i < 4; i++, k++) {
2086 if (i < (fp->cur_node + 1)) {
2087 rmesa->hw.fp.cmd[R300_FP_NODE0 + k] =
2088 (fp->node[i].alu_offset << R300_PFS_NODE_ALU_OFFSET_SHIFT) |
2089 (fp->node[i].alu_end << R300_PFS_NODE_ALU_END_SHIFT) |
2090 (fp->node[i].tex_offset << R300_PFS_NODE_TEX_OFFSET_SHIFT) |
2091 (fp->node[i].tex_end << R300_PFS_NODE_TEX_END_SHIFT) |
2092 fp->node[i].flags;
2093 } else {
2094 rmesa->hw.fp.cmd[R300_FP_NODE0 + (3 - i)] = 0;
2095 }
2096 }
2097
2098 R300_STATECHANGE(rmesa, fpp);
2099 rmesa->hw.fpp.cmd[R300_FPP_CMD_0] = cmdpacket0(R300_PFS_PARAM_0_X, fp->const_nr * 4);
2100 for (i = 0; i < fp->const_nr; i++) {
2101 rmesa->hw.fpp.cmd[R300_FPP_PARAM_0 + 4 * i + 0] = r300PackFloat24(fp->constant[i][0]);
2102 rmesa->hw.fpp.cmd[R300_FPP_PARAM_0 + 4 * i + 1] = r300PackFloat24(fp->constant[i][1]);
2103 rmesa->hw.fpp.cmd[R300_FPP_PARAM_0 + 4 * i + 2] = r300PackFloat24(fp->constant[i][2]);
2104 rmesa->hw.fpp.cmd[R300_FPP_PARAM_0 + 4 * i + 3] = r300PackFloat24(fp->constant[i][3]);
2105 }
2106 }
2107
2108 void r300UpdateShaderStates(r300ContextPtr rmesa)
2109 {
2110 GLcontext *ctx;
2111 ctx = rmesa->radeon.glCtx;
2112
2113 r300UpdateTextureState(ctx);
2114
2115 r300SetupPixelShader(rmesa);
2116 r300SetupTextures(ctx);
2117
2118 if ((rmesa->radeon.radeonScreen->chip_flags & RADEON_CHIPSET_TCL))
2119 r300SetupVertexProgram(rmesa);
2120 r300SetupRSUnit(ctx);
2121 }
2122
2123 /**
2124 * Called by Mesa after an internal state update.
2125 */
2126 static void r300InvalidateState(GLcontext * ctx, GLuint new_state)
2127 {
2128 r300ContextPtr r300 = R300_CONTEXT(ctx);
2129
2130 _swrast_InvalidateState(ctx, new_state);
2131 _swsetup_InvalidateState(ctx, new_state);
2132 _vbo_InvalidateState(ctx, new_state);
2133 _tnl_InvalidateState(ctx, new_state);
2134 _ae_invalidate_state(ctx, new_state);
2135
2136 if (new_state & (_NEW_BUFFERS | _NEW_COLOR | _NEW_PIXEL)) {
2137 r300UpdateDrawBuffer(ctx);
2138 }
2139
2140 r300UpdateStateParameters(ctx, new_state);
2141
2142 r300->NewGLState |= new_state;
2143 }
2144
2145 /**
2146 * Calculate initial hardware state and register state functions.
2147 * Assumes that the command buffer and state atoms have been
2148 * initialized already.
2149 */
2150 void r300InitState(r300ContextPtr r300)
2151 {
2152 GLcontext *ctx = r300->radeon.glCtx;
2153 GLuint depth_fmt;
2154
2155 radeonInitState(&r300->radeon);
2156
2157 switch (ctx->Visual.depthBits) {
2158 case 16:
2159 r300->state.depth.scale = 1.0 / (GLfloat) 0xffff;
2160 depth_fmt = R300_DEPTH_FORMAT_16BIT_INT_Z;
2161 r300->state.stencil.clear = 0x00000000;
2162 break;
2163 case 24:
2164 r300->state.depth.scale = 1.0 / (GLfloat) 0xffffff;
2165 depth_fmt = R300_DEPTH_FORMAT_24BIT_INT_Z;
2166 r300->state.stencil.clear = 0x00ff0000;
2167 break;
2168 default:
2169 fprintf(stderr, "Error: Unsupported depth %d... exiting\n",
2170 ctx->Visual.depthBits);
2171 _mesa_exit(-1);
2172 }
2173
2174 /* Only have hw stencil when depth buffer is 24 bits deep */
2175 r300->state.stencil.hw_stencil = (ctx->Visual.stencilBits > 0 &&
2176 ctx->Visual.depthBits == 24);
2177
2178 memset(&(r300->state.texture), 0, sizeof(r300->state.texture));
2179
2180 r300ResetHwState(r300);
2181 }
2182
2183 static void r300RenderMode(GLcontext * ctx, GLenum mode)
2184 {
2185 r300ContextPtr rmesa = R300_CONTEXT(ctx);
2186 (void)rmesa;
2187 (void)mode;
2188 }
2189
2190 /**
2191 * Initialize driver's state callback functions
2192 */
2193 void r300InitStateFuncs(struct dd_function_table *functions)
2194 {
2195 radeonInitStateFuncs(functions);
2196
2197 functions->UpdateState = r300InvalidateState;
2198 functions->AlphaFunc = r300AlphaFunc;
2199 functions->BlendColor = r300BlendColor;
2200 functions->BlendEquationSeparate = r300BlendEquationSeparate;
2201 functions->BlendFuncSeparate = r300BlendFuncSeparate;
2202 functions->Enable = r300Enable;
2203 functions->ColorMask = r300ColorMask;
2204 functions->DepthFunc = r300DepthFunc;
2205 functions->DepthMask = r300DepthMask;
2206 functions->CullFace = r300CullFace;
2207 functions->Fogfv = r300Fogfv;
2208 functions->FrontFace = r300FrontFace;
2209 functions->ShadeModel = r300ShadeModel;
2210
2211 /* Stencil related */
2212 functions->ClearStencil = r300ClearStencil;
2213 functions->StencilFuncSeparate = r300StencilFuncSeparate;
2214 functions->StencilMaskSeparate = r300StencilMaskSeparate;
2215 functions->StencilOpSeparate = r300StencilOpSeparate;
2216
2217 /* Viewport related */
2218 functions->Viewport = r300Viewport;
2219 functions->DepthRange = r300DepthRange;
2220 functions->PointSize = r300PointSize;
2221 functions->LineWidth = r300LineWidth;
2222
2223 functions->PolygonOffset = r300PolygonOffset;
2224 functions->PolygonMode = r300PolygonMode;
2225
2226 functions->RenderMode = r300RenderMode;
2227 }