r500: Set Saturate correctly in radeon_program_pair
[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 r300ContextPtr rmesa = R300_CONTEXT(ctx);
74
75 R300_STATECHANGE(rmesa, blend_color);
76
77 if (rmesa->radeon.radeonScreen->chip_family >= CHIP_FAMILY_RV515) {
78 GLuint r = IROUND(cf[0]*1023.0f);
79 GLuint g = IROUND(cf[1]*1023.0f);
80 GLuint b = IROUND(cf[2]*1023.0f);
81 GLuint a = IROUND(cf[3]*1023.0f);
82
83 rmesa->hw.blend_color.cmd[1] = r | (a << 16);
84 rmesa->hw.blend_color.cmd[2] = b | (g << 16);
85 } else {
86 GLubyte color[4];
87 CLAMPED_FLOAT_TO_UBYTE(color[0], cf[0]);
88 CLAMPED_FLOAT_TO_UBYTE(color[1], cf[1]);
89 CLAMPED_FLOAT_TO_UBYTE(color[2], cf[2]);
90 CLAMPED_FLOAT_TO_UBYTE(color[3], cf[3]);
91
92 rmesa->hw.blend_color.cmd[1] = PACK_COLOR_8888(color[3], color[0],
93 color[1], color[2]);
94 }
95 }
96
97 /**
98 * Calculate the hardware blend factor setting. This same function is used
99 * for source and destination of both alpha and RGB.
100 *
101 * \returns
102 * The hardware register value for the specified blend factor. This value
103 * will need to be shifted into the correct position for either source or
104 * destination factor.
105 *
106 * \todo
107 * Since the two cases where source and destination are handled differently
108 * are essentially error cases, they should never happen. Determine if these
109 * cases can be removed.
110 */
111 static int blend_factor(GLenum factor, GLboolean is_src)
112 {
113 switch (factor) {
114 case GL_ZERO:
115 return R300_BLEND_GL_ZERO;
116 break;
117 case GL_ONE:
118 return R300_BLEND_GL_ONE;
119 break;
120 case GL_DST_COLOR:
121 return R300_BLEND_GL_DST_COLOR;
122 break;
123 case GL_ONE_MINUS_DST_COLOR:
124 return R300_BLEND_GL_ONE_MINUS_DST_COLOR;
125 break;
126 case GL_SRC_COLOR:
127 return R300_BLEND_GL_SRC_COLOR;
128 break;
129 case GL_ONE_MINUS_SRC_COLOR:
130 return R300_BLEND_GL_ONE_MINUS_SRC_COLOR;
131 break;
132 case GL_SRC_ALPHA:
133 return R300_BLEND_GL_SRC_ALPHA;
134 break;
135 case GL_ONE_MINUS_SRC_ALPHA:
136 return R300_BLEND_GL_ONE_MINUS_SRC_ALPHA;
137 break;
138 case GL_DST_ALPHA:
139 return R300_BLEND_GL_DST_ALPHA;
140 break;
141 case GL_ONE_MINUS_DST_ALPHA:
142 return R300_BLEND_GL_ONE_MINUS_DST_ALPHA;
143 break;
144 case GL_SRC_ALPHA_SATURATE:
145 return (is_src) ? R300_BLEND_GL_SRC_ALPHA_SATURATE :
146 R300_BLEND_GL_ZERO;
147 break;
148 case GL_CONSTANT_COLOR:
149 return R300_BLEND_GL_CONST_COLOR;
150 break;
151 case GL_ONE_MINUS_CONSTANT_COLOR:
152 return R300_BLEND_GL_ONE_MINUS_CONST_COLOR;
153 break;
154 case GL_CONSTANT_ALPHA:
155 return R300_BLEND_GL_CONST_ALPHA;
156 break;
157 case GL_ONE_MINUS_CONSTANT_ALPHA:
158 return R300_BLEND_GL_ONE_MINUS_CONST_ALPHA;
159 break;
160 default:
161 fprintf(stderr, "unknown blend factor %x\n", factor);
162 return (is_src) ? R300_BLEND_GL_ONE : R300_BLEND_GL_ZERO;
163 break;
164 }
165 }
166
167 /**
168 * Sets both the blend equation and the blend function.
169 * This is done in a single
170 * function because some blend equations (i.e., \c GL_MIN and \c GL_MAX)
171 * change the interpretation of the blend function.
172 * Also, make sure that blend function and blend equation are set to their
173 * default value if color blending is not enabled, since at least blend
174 * equations GL_MIN and GL_FUNC_REVERSE_SUBTRACT will cause wrong results
175 * otherwise for unknown reasons.
176 */
177
178 /* helper function */
179 static void r300SetBlendCntl(r300ContextPtr r300, int func, int eqn,
180 int cbits, int funcA, int eqnA)
181 {
182 GLuint new_ablend, new_cblend;
183
184 #if 0
185 fprintf(stderr,
186 "eqnA=%08x funcA=%08x eqn=%08x func=%08x cbits=%08x\n",
187 eqnA, funcA, eqn, func, cbits);
188 #endif
189 new_ablend = eqnA | funcA;
190 new_cblend = eqn | func;
191
192 /* Some blend factor combinations don't seem to work when the
193 * BLEND_NO_SEPARATE bit is set.
194 *
195 * Especially problematic candidates are the ONE_MINUS_* flags,
196 * but I can't see a real pattern.
197 */
198 #if 0
199 if (new_ablend == new_cblend) {
200 new_cblend |= R300_DISCARD_SRC_PIXELS_SRC_ALPHA_0;
201 }
202 #endif
203 new_cblend |= cbits;
204
205 if ((new_ablend != r300->hw.bld.cmd[R300_BLD_ABLEND]) ||
206 (new_cblend != r300->hw.bld.cmd[R300_BLD_CBLEND])) {
207 R300_STATECHANGE(r300, bld);
208 r300->hw.bld.cmd[R300_BLD_ABLEND] = new_ablend;
209 r300->hw.bld.cmd[R300_BLD_CBLEND] = new_cblend;
210 }
211 }
212
213 static void r300SetBlendState(GLcontext * ctx)
214 {
215 r300ContextPtr r300 = R300_CONTEXT(ctx);
216 int func = (R300_BLEND_GL_ONE << R300_SRC_BLEND_SHIFT) |
217 (R300_BLEND_GL_ZERO << R300_DST_BLEND_SHIFT);
218 int eqn = R300_COMB_FCN_ADD_CLAMP;
219 int funcA = (R300_BLEND_GL_ONE << R300_SRC_BLEND_SHIFT) |
220 (R300_BLEND_GL_ZERO << R300_DST_BLEND_SHIFT);
221 int eqnA = R300_COMB_FCN_ADD_CLAMP;
222
223 if (RGBA_LOGICOP_ENABLED(ctx) || !ctx->Color.BlendEnabled) {
224 r300SetBlendCntl(r300, func, eqn, 0, func, eqn);
225 return;
226 }
227
228 func =
229 (blend_factor(ctx->Color.BlendSrcRGB, GL_TRUE) <<
230 R300_SRC_BLEND_SHIFT) | (blend_factor(ctx->Color.BlendDstRGB,
231 GL_FALSE) <<
232 R300_DST_BLEND_SHIFT);
233
234 switch (ctx->Color.BlendEquationRGB) {
235 case GL_FUNC_ADD:
236 eqn = R300_COMB_FCN_ADD_CLAMP;
237 break;
238
239 case GL_FUNC_SUBTRACT:
240 eqn = R300_COMB_FCN_SUB_CLAMP;
241 break;
242
243 case GL_FUNC_REVERSE_SUBTRACT:
244 eqn = R300_COMB_FCN_RSUB_CLAMP;
245 break;
246
247 case GL_MIN:
248 eqn = R300_COMB_FCN_MIN;
249 func = (R300_BLEND_GL_ONE << R300_SRC_BLEND_SHIFT) |
250 (R300_BLEND_GL_ONE << R300_DST_BLEND_SHIFT);
251 break;
252
253 case GL_MAX:
254 eqn = R300_COMB_FCN_MAX;
255 func = (R300_BLEND_GL_ONE << R300_SRC_BLEND_SHIFT) |
256 (R300_BLEND_GL_ONE << R300_DST_BLEND_SHIFT);
257 break;
258
259 default:
260 fprintf(stderr,
261 "[%s:%u] Invalid RGB blend equation (0x%04x).\n",
262 __FUNCTION__, __LINE__, ctx->Color.BlendEquationRGB);
263 return;
264 }
265
266 funcA =
267 (blend_factor(ctx->Color.BlendSrcA, GL_TRUE) <<
268 R300_SRC_BLEND_SHIFT) | (blend_factor(ctx->Color.BlendDstA,
269 GL_FALSE) <<
270 R300_DST_BLEND_SHIFT);
271
272 switch (ctx->Color.BlendEquationA) {
273 case GL_FUNC_ADD:
274 eqnA = R300_COMB_FCN_ADD_CLAMP;
275 break;
276
277 case GL_FUNC_SUBTRACT:
278 eqnA = R300_COMB_FCN_SUB_CLAMP;
279 break;
280
281 case GL_FUNC_REVERSE_SUBTRACT:
282 eqnA = R300_COMB_FCN_RSUB_CLAMP;
283 break;
284
285 case GL_MIN:
286 eqnA = R300_COMB_FCN_MIN;
287 funcA = (R300_BLEND_GL_ONE << R300_SRC_BLEND_SHIFT) |
288 (R300_BLEND_GL_ONE << R300_DST_BLEND_SHIFT);
289 break;
290
291 case GL_MAX:
292 eqnA = R300_COMB_FCN_MAX;
293 funcA = (R300_BLEND_GL_ONE << R300_SRC_BLEND_SHIFT) |
294 (R300_BLEND_GL_ONE << R300_DST_BLEND_SHIFT);
295 break;
296
297 default:
298 fprintf(stderr,
299 "[%s:%u] Invalid A blend equation (0x%04x).\n",
300 __FUNCTION__, __LINE__, ctx->Color.BlendEquationA);
301 return;
302 }
303
304 r300SetBlendCntl(r300,
305 func, eqn,
306 (R300_SEPARATE_ALPHA_ENABLE |
307 R300_READ_ENABLE |
308 R300_ALPHA_BLEND_ENABLE), funcA, eqnA);
309 }
310
311 static void r300BlendEquationSeparate(GLcontext * ctx,
312 GLenum modeRGB, GLenum modeA)
313 {
314 r300SetBlendState(ctx);
315 }
316
317 static void r300BlendFuncSeparate(GLcontext * ctx,
318 GLenum sfactorRGB, GLenum dfactorRGB,
319 GLenum sfactorA, GLenum dfactorA)
320 {
321 r300SetBlendState(ctx);
322 }
323
324 static void r300ClipPlane( GLcontext *ctx, GLenum plane, const GLfloat *eq )
325 {
326 r300ContextPtr rmesa = R300_CONTEXT(ctx);
327 GLint p;
328 GLint *ip;
329
330 /* no VAP UCP on non-TCL chipsets */
331 if (!(rmesa->radeon.radeonScreen->chip_flags & RADEON_CHIPSET_TCL))
332 return;
333
334 p = (GLint) plane - (GLint) GL_CLIP_PLANE0;
335 ip = (GLint *)ctx->Transform._ClipUserPlane[p];
336
337 R300_STATECHANGE( rmesa, vpucp[p] );
338 rmesa->hw.vpucp[p].cmd[R300_VPUCP_X] = ip[0];
339 rmesa->hw.vpucp[p].cmd[R300_VPUCP_Y] = ip[1];
340 rmesa->hw.vpucp[p].cmd[R300_VPUCP_Z] = ip[2];
341 rmesa->hw.vpucp[p].cmd[R300_VPUCP_W] = ip[3];
342 }
343
344 static void r300SetClipPlaneState(GLcontext * ctx, GLenum cap, GLboolean state)
345 {
346 r300ContextPtr r300 = R300_CONTEXT(ctx);
347 GLuint p;
348
349 /* no VAP UCP on non-TCL chipsets */
350 if (!(r300->radeon.radeonScreen->chip_flags & RADEON_CHIPSET_TCL))
351 return;
352
353 p = cap - GL_CLIP_PLANE0;
354 R300_STATECHANGE(r300, vap_clip_cntl);
355 if (state) {
356 r300->hw.vap_clip_cntl.cmd[1] |= (R300_VAP_UCP_ENABLE_0 << p);
357 r300ClipPlane(ctx, cap, NULL);
358 } else {
359 r300->hw.vap_clip_cntl.cmd[1] &= ~(R300_VAP_UCP_ENABLE_0 << p);
360 }
361 }
362
363 /**
364 * Update our tracked culling state based on Mesa's state.
365 */
366 static void r300UpdateCulling(GLcontext * ctx)
367 {
368 r300ContextPtr r300 = R300_CONTEXT(ctx);
369 uint32_t val = 0;
370
371 if (ctx->Polygon.CullFlag) {
372 switch (ctx->Polygon.CullFaceMode) {
373 case GL_FRONT:
374 val = R300_CULL_FRONT;
375 break;
376 case GL_BACK:
377 val = R300_CULL_BACK;
378 break;
379 case GL_FRONT_AND_BACK:
380 val = R300_CULL_FRONT | R300_CULL_BACK;
381 break;
382 default:
383 break;
384 }
385 }
386
387 switch (ctx->Polygon.FrontFace) {
388 case GL_CW:
389 val |= R300_FRONT_FACE_CW;
390 break;
391 case GL_CCW:
392 val |= R300_FRONT_FACE_CCW;
393 break;
394 default:
395 break;
396 }
397
398 R300_STATECHANGE(r300, cul);
399 r300->hw.cul.cmd[R300_CUL_CULL] = val;
400 }
401
402 static void r300SetPolygonOffsetState(GLcontext * ctx, GLboolean state)
403 {
404 r300ContextPtr r300 = R300_CONTEXT(ctx);
405
406 R300_STATECHANGE(r300, occlusion_cntl);
407 if (state) {
408 r300->hw.occlusion_cntl.cmd[1] |= (3 << 0);
409 } else {
410 r300->hw.occlusion_cntl.cmd[1] &= ~(3 << 0);
411 }
412 }
413
414 static GLboolean current_fragment_program_writes_depth(GLcontext* ctx)
415 {
416 r300ContextPtr r300 = R300_CONTEXT(ctx);
417
418 if (r300->radeon.radeonScreen->chip_family < CHIP_FAMILY_RV515) {
419 struct r300_fragment_program *fp = (struct r300_fragment_program *)
420 (char *)ctx->FragmentProgram._Current;
421 return (fp && fp->WritesDepth);
422 } else {
423 struct r500_fragment_program* fp =
424 (struct r500_fragment_program*)(char*)
425 ctx->FragmentProgram._Current;
426 return (fp && fp->writes_depth);
427 }
428 }
429
430 static void r300SetEarlyZState(GLcontext * ctx)
431 {
432 r300ContextPtr r300 = R300_CONTEXT(ctx);
433 GLuint topZ = R300_ZTOP_ENABLE;
434
435 if (ctx->Color.AlphaEnabled && ctx->Color.AlphaFunc != GL_ALWAYS)
436 topZ = R300_ZTOP_DISABLE;
437 if (current_fragment_program_writes_depth(ctx))
438 topZ = R300_ZTOP_DISABLE;
439
440 if (topZ != r300->hw.zstencil_format.cmd[2]) {
441 /* Note: This completely reemits the stencil format.
442 * I have not tested whether this is strictly necessary,
443 * or if emitting a write to ZB_ZTOP is enough.
444 */
445 R300_STATECHANGE(r300, zstencil_format);
446 r300->hw.zstencil_format.cmd[2] = topZ;
447 }
448 }
449
450 static void r300SetAlphaState(GLcontext * ctx)
451 {
452 r300ContextPtr r300 = R300_CONTEXT(ctx);
453 GLubyte refByte;
454 uint32_t pp_misc = 0x0;
455 GLboolean really_enabled = ctx->Color.AlphaEnabled;
456
457 CLAMPED_FLOAT_TO_UBYTE(refByte, ctx->Color.AlphaRef);
458
459 switch (ctx->Color.AlphaFunc) {
460 case GL_NEVER:
461 pp_misc |= R300_FG_ALPHA_FUNC_NEVER;
462 break;
463 case GL_LESS:
464 pp_misc |= R300_FG_ALPHA_FUNC_LESS;
465 break;
466 case GL_EQUAL:
467 pp_misc |= R300_FG_ALPHA_FUNC_EQUAL;
468 break;
469 case GL_LEQUAL:
470 pp_misc |= R300_FG_ALPHA_FUNC_LE;
471 break;
472 case GL_GREATER:
473 pp_misc |= R300_FG_ALPHA_FUNC_GREATER;
474 break;
475 case GL_NOTEQUAL:
476 pp_misc |= R300_FG_ALPHA_FUNC_NOTEQUAL;
477 break;
478 case GL_GEQUAL:
479 pp_misc |= R300_FG_ALPHA_FUNC_GE;
480 break;
481 case GL_ALWAYS:
482 /*pp_misc |= FG_ALPHA_FUNC_ALWAYS; */
483 really_enabled = GL_FALSE;
484 break;
485 }
486
487 if (really_enabled) {
488 pp_misc |= R300_FG_ALPHA_FUNC_ENABLE;
489 pp_misc |= R500_FG_ALPHA_FUNC_8BIT;
490 pp_misc |= (refByte & R300_FG_ALPHA_FUNC_VAL_MASK);
491 } else {
492 pp_misc = 0x0;
493 }
494
495 R300_STATECHANGE(r300, at);
496 r300->hw.at.cmd[R300_AT_ALPHA_TEST] = pp_misc;
497 r300->hw.at.cmd[R300_AT_UNKNOWN] = 0;
498
499 r300SetEarlyZState(ctx);
500 }
501
502 static void r300AlphaFunc(GLcontext * ctx, GLenum func, GLfloat ref)
503 {
504 (void)func;
505 (void)ref;
506 r300SetAlphaState(ctx);
507 }
508
509 static int translate_func(int func)
510 {
511 switch (func) {
512 case GL_NEVER:
513 return R300_ZS_NEVER;
514 case GL_LESS:
515 return R300_ZS_LESS;
516 case GL_EQUAL:
517 return R300_ZS_EQUAL;
518 case GL_LEQUAL:
519 return R300_ZS_LEQUAL;
520 case GL_GREATER:
521 return R300_ZS_GREATER;
522 case GL_NOTEQUAL:
523 return R300_ZS_NOTEQUAL;
524 case GL_GEQUAL:
525 return R300_ZS_GEQUAL;
526 case GL_ALWAYS:
527 return R300_ZS_ALWAYS;
528 }
529 return 0;
530 }
531
532 static void r300SetDepthState(GLcontext * ctx)
533 {
534 r300ContextPtr r300 = R300_CONTEXT(ctx);
535
536 R300_STATECHANGE(r300, zs);
537 r300->hw.zs.cmd[R300_ZS_CNTL_0] &= R300_STENCIL_ENABLE|R300_STENCIL_FRONT_BACK;
538 r300->hw.zs.cmd[R300_ZS_CNTL_1] &= ~(R300_ZS_MASK << R300_Z_FUNC_SHIFT);
539
540 if (ctx->Depth.Test) {
541 r300->hw.zs.cmd[R300_ZS_CNTL_0] |= R300_Z_ENABLE;
542 if (ctx->Depth.Mask)
543 r300->hw.zs.cmd[R300_ZS_CNTL_0] |= R300_Z_WRITE_ENABLE;
544 r300->hw.zs.cmd[R300_ZS_CNTL_1] |=
545 translate_func(ctx->Depth.Func) << R300_Z_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_STENCIL_ENABLE;
560 } else {
561 r300->hw.zs.cmd[R300_ZS_CNTL_0] &=
562 ~R300_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 = R300_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 |= R300_GA_POLY_MODE_DUAL;
594
595 switch (f) {
596 case GL_LINE:
597 hw_mode |= R300_GA_POLY_MODE_FRONT_PTYPE_LINE;
598 break;
599 case GL_POINT:
600 hw_mode |= R300_GA_POLY_MODE_FRONT_PTYPE_POINT;
601 break;
602 case GL_FILL:
603 hw_mode |= R300_GA_POLY_MODE_FRONT_PTYPE_TRI;
604 break;
605 }
606
607 switch (b) {
608 case GL_LINE:
609 hw_mode |= R300_GA_POLY_MODE_BACK_PTYPE_LINE;
610 break;
611 case GL_POINT:
612 hw_mode |= R300_GA_POLY_MODE_BACK_PTYPE_POINT;
613 break;
614 case GL_FILL:
615 hw_mode |= R300_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] & ~R300_FG_FOG_BLEND_FN_MASK) |
720 R300_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] & ~R300_FG_FOG_BLEND_FN_MASK) |
738 R300_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] & ~R300_FG_FOG_BLEND_FN_MASK) |
747 R300_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] |= R300_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] &= ~R300_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 static void r300PointParameter(GLcontext * ctx, GLenum pname, const GLfloat * param)
839 {
840 r300ContextPtr r300 = R300_CONTEXT(ctx);
841
842 switch (pname) {
843 case GL_POINT_SIZE_MIN:
844 R300_STATECHANGE(r300, ga_point_minmax);
845 r300->hw.ga_point_minmax.cmd[1] &= ~R300_GA_POINT_MINMAX_MIN_MASK;
846 r300->hw.ga_point_minmax.cmd[1] |= (GLuint)(ctx->Point.MinSize * 16.0);
847 break;
848 case GL_POINT_SIZE_MAX:
849 R300_STATECHANGE(r300, ga_point_minmax);
850 r300->hw.ga_point_minmax.cmd[1] &= ~R300_GA_POINT_MINMAX_MAX_MASK;
851 r300->hw.ga_point_minmax.cmd[1] |= (GLuint)(ctx->Point.MaxSize * 16.0)
852 << R300_GA_POINT_MINMAX_MAX_SHIFT;
853 break;
854 case GL_POINT_DISTANCE_ATTENUATION:
855 break;
856 case GL_POINT_FADE_THRESHOLD_SIZE:
857 break;
858 default:
859 break;
860 }
861 }
862
863 /* =============================================================
864 * Line state
865 */
866 static void r300LineWidth(GLcontext * ctx, GLfloat widthf)
867 {
868 r300ContextPtr r300 = R300_CONTEXT(ctx);
869
870 widthf = CLAMP(widthf,
871 ctx->Const.MinPointSize,
872 ctx->Const.MaxPointSize);
873 R300_STATECHANGE(r300, lcntl);
874 r300->hw.lcntl.cmd[1] =
875 R300_LINE_CNT_HO | R300_LINE_CNT_VE | (int)(widthf * 6.0);
876 }
877
878 static void r300PolygonMode(GLcontext * ctx, GLenum face, GLenum mode)
879 {
880 (void)face;
881 (void)mode;
882
883 r300UpdatePolygonMode(ctx);
884 }
885
886 /* =============================================================
887 * Stencil
888 */
889
890 static int translate_stencil_op(int op)
891 {
892 switch (op) {
893 case GL_KEEP:
894 return R300_ZS_KEEP;
895 case GL_ZERO:
896 return R300_ZS_ZERO;
897 case GL_REPLACE:
898 return R300_ZS_REPLACE;
899 case GL_INCR:
900 return R300_ZS_INCR;
901 case GL_DECR:
902 return R300_ZS_DECR;
903 case GL_INCR_WRAP_EXT:
904 return R300_ZS_INCR_WRAP;
905 case GL_DECR_WRAP_EXT:
906 return R300_ZS_DECR_WRAP;
907 case GL_INVERT:
908 return R300_ZS_INVERT;
909 default:
910 WARN_ONCE("Do not know how to translate stencil op");
911 return R300_ZS_KEEP;
912 }
913 return 0;
914 }
915
916 static void r300ShadeModel(GLcontext * ctx, GLenum mode)
917 {
918 r300ContextPtr rmesa = R300_CONTEXT(ctx);
919
920 R300_STATECHANGE(rmesa, shade);
921 rmesa->hw.shade.cmd[1] = 0x00000002;
922 switch (mode) {
923 case GL_FLAT:
924 rmesa->hw.shade.cmd[2] = R300_RE_SHADE_MODEL_FLAT;
925 break;
926 case GL_SMOOTH:
927 rmesa->hw.shade.cmd[2] = R300_RE_SHADE_MODEL_SMOOTH;
928 break;
929 default:
930 return;
931 }
932 rmesa->hw.shade.cmd[3] = 0x00000000;
933 rmesa->hw.shade.cmd[4] = 0x00000000;
934 }
935
936 static void r300StencilFuncSeparate(GLcontext * ctx, GLenum face,
937 GLenum func, GLint ref, GLuint mask)
938 {
939 r300ContextPtr rmesa = R300_CONTEXT(ctx);
940 GLuint refmask =
941 (((ctx->Stencil.
942 Ref[0] & 0xff) << R300_STENCILREF_SHIFT) | ((ctx->
943 Stencil.
944 ValueMask
945 [0] &
946 0xff)
947 <<
948 R300_STENCILMASK_SHIFT));
949
950 GLuint flag;
951
952 R300_STATECHANGE(rmesa, zs);
953 rmesa->hw.zs.cmd[R300_ZS_CNTL_0] |= R300_STENCIL_FRONT_BACK;
954 rmesa->hw.zs.cmd[R300_ZS_CNTL_1] &= ~((R300_ZS_MASK <<
955 R300_S_FRONT_FUNC_SHIFT)
956 | (R300_ZS_MASK <<
957 R300_S_BACK_FUNC_SHIFT));
958
959 rmesa->hw.zs.cmd[R300_ZS_CNTL_2] &=
960 ~((R300_STENCILREF_MASK << R300_STENCILREF_SHIFT) |
961 (R300_STENCILREF_MASK << R300_STENCILMASK_SHIFT));
962
963 flag = translate_func(ctx->Stencil.Function[0]);
964 rmesa->hw.zs.cmd[R300_ZS_CNTL_1] |=
965 (flag << R300_S_FRONT_FUNC_SHIFT);
966
967 if (ctx->Stencil._TestTwoSide)
968 flag = translate_func(ctx->Stencil.Function[1]);
969
970 rmesa->hw.zs.cmd[R300_ZS_CNTL_1] |=
971 (flag << R300_S_BACK_FUNC_SHIFT);
972 rmesa->hw.zs.cmd[R300_ZS_CNTL_2] |= refmask;
973 }
974
975 static void r300StencilMaskSeparate(GLcontext * ctx, GLenum face, GLuint mask)
976 {
977 r300ContextPtr rmesa = R300_CONTEXT(ctx);
978
979 R300_STATECHANGE(rmesa, zs);
980 rmesa->hw.zs.cmd[R300_ZS_CNTL_2] &=
981 ~(R300_STENCILREF_MASK <<
982 R300_STENCILWRITEMASK_SHIFT);
983 rmesa->hw.zs.cmd[R300_ZS_CNTL_2] |=
984 (ctx->Stencil.
985 WriteMask[0] & R300_STENCILREF_MASK) <<
986 R300_STENCILWRITEMASK_SHIFT;
987 }
988
989 static void r300StencilOpSeparate(GLcontext * ctx, GLenum face,
990 GLenum fail, GLenum zfail, GLenum zpass)
991 {
992 r300ContextPtr rmesa = R300_CONTEXT(ctx);
993
994 R300_STATECHANGE(rmesa, zs);
995 /* It is easier to mask what's left.. */
996 rmesa->hw.zs.cmd[R300_ZS_CNTL_1] &=
997 (R300_ZS_MASK << R300_Z_FUNC_SHIFT) |
998 (R300_ZS_MASK << R300_S_FRONT_FUNC_SHIFT) |
999 (R300_ZS_MASK << R300_S_BACK_FUNC_SHIFT);
1000
1001 rmesa->hw.zs.cmd[R300_ZS_CNTL_1] |=
1002 (translate_stencil_op(ctx->Stencil.FailFunc[0]) <<
1003 R300_S_FRONT_SFAIL_OP_SHIFT)
1004 | (translate_stencil_op(ctx->Stencil.ZFailFunc[0]) <<
1005 R300_S_FRONT_ZFAIL_OP_SHIFT)
1006 | (translate_stencil_op(ctx->Stencil.ZPassFunc[0]) <<
1007 R300_S_FRONT_ZPASS_OP_SHIFT);
1008
1009 if (ctx->Stencil._TestTwoSide) {
1010 rmesa->hw.zs.cmd[R300_ZS_CNTL_1] |=
1011 (translate_stencil_op(ctx->Stencil.FailFunc[1]) <<
1012 R300_S_BACK_SFAIL_OP_SHIFT)
1013 | (translate_stencil_op(ctx->Stencil.ZFailFunc[1]) <<
1014 R300_S_BACK_ZFAIL_OP_SHIFT)
1015 | (translate_stencil_op(ctx->Stencil.ZPassFunc[1]) <<
1016 R300_S_BACK_ZPASS_OP_SHIFT);
1017 } else {
1018 rmesa->hw.zs.cmd[R300_ZS_CNTL_1] |=
1019 (translate_stencil_op(ctx->Stencil.FailFunc[0]) <<
1020 R300_S_BACK_SFAIL_OP_SHIFT)
1021 | (translate_stencil_op(ctx->Stencil.ZFailFunc[0]) <<
1022 R300_S_BACK_ZFAIL_OP_SHIFT)
1023 | (translate_stencil_op(ctx->Stencil.ZPassFunc[0]) <<
1024 R300_S_BACK_ZPASS_OP_SHIFT);
1025 }
1026 }
1027
1028 /* =============================================================
1029 * Window position and viewport transformation
1030 */
1031
1032 /*
1033 * To correctly position primitives:
1034 */
1035 #define SUBPIXEL_X 0.125
1036 #define SUBPIXEL_Y 0.125
1037
1038 static void r300UpdateWindow(GLcontext * ctx)
1039 {
1040 r300ContextPtr rmesa = R300_CONTEXT(ctx);
1041 __DRIdrawablePrivate *dPriv = rmesa->radeon.dri.drawable;
1042 GLfloat xoffset = dPriv ? (GLfloat) dPriv->x : 0;
1043 GLfloat yoffset = dPriv ? (GLfloat) dPriv->y + dPriv->h : 0;
1044 const GLfloat *v = ctx->Viewport._WindowMap.m;
1045
1046 GLfloat sx = v[MAT_SX];
1047 GLfloat tx = v[MAT_TX] + xoffset + SUBPIXEL_X;
1048 GLfloat sy = -v[MAT_SY];
1049 GLfloat ty = (-v[MAT_TY]) + yoffset + SUBPIXEL_Y;
1050 GLfloat sz = v[MAT_SZ] * rmesa->state.depth.scale;
1051 GLfloat tz = v[MAT_TZ] * rmesa->state.depth.scale;
1052
1053 R300_FIREVERTICES(rmesa);
1054 R300_STATECHANGE(rmesa, vpt);
1055
1056 rmesa->hw.vpt.cmd[R300_VPT_XSCALE] = r300PackFloat32(sx);
1057 rmesa->hw.vpt.cmd[R300_VPT_XOFFSET] = r300PackFloat32(tx);
1058 rmesa->hw.vpt.cmd[R300_VPT_YSCALE] = r300PackFloat32(sy);
1059 rmesa->hw.vpt.cmd[R300_VPT_YOFFSET] = r300PackFloat32(ty);
1060 rmesa->hw.vpt.cmd[R300_VPT_ZSCALE] = r300PackFloat32(sz);
1061 rmesa->hw.vpt.cmd[R300_VPT_ZOFFSET] = r300PackFloat32(tz);
1062 }
1063
1064 static void r300Viewport(GLcontext * ctx, GLint x, GLint y,
1065 GLsizei width, GLsizei height)
1066 {
1067 /* Don't pipeline viewport changes, conflict with window offset
1068 * setting below. Could apply deltas to rescue pipelined viewport
1069 * values, or keep the originals hanging around.
1070 */
1071 r300UpdateWindow(ctx);
1072 }
1073
1074 static void r300DepthRange(GLcontext * ctx, GLclampd nearval, GLclampd farval)
1075 {
1076 r300UpdateWindow(ctx);
1077 }
1078
1079 void r300UpdateViewportOffset(GLcontext * ctx)
1080 {
1081 r300ContextPtr rmesa = R300_CONTEXT(ctx);
1082 __DRIdrawablePrivate *dPriv = ((radeonContextPtr) rmesa)->dri.drawable;
1083 GLfloat xoffset = (GLfloat) dPriv->x;
1084 GLfloat yoffset = (GLfloat) dPriv->y + dPriv->h;
1085 const GLfloat *v = ctx->Viewport._WindowMap.m;
1086
1087 GLfloat tx = v[MAT_TX] + xoffset + SUBPIXEL_X;
1088 GLfloat ty = (-v[MAT_TY]) + yoffset + SUBPIXEL_Y;
1089
1090 if (rmesa->hw.vpt.cmd[R300_VPT_XOFFSET] != r300PackFloat32(tx) ||
1091 rmesa->hw.vpt.cmd[R300_VPT_YOFFSET] != r300PackFloat32(ty)) {
1092 /* Note: this should also modify whatever data the context reset
1093 * code uses...
1094 */
1095 R300_STATECHANGE(rmesa, vpt);
1096 rmesa->hw.vpt.cmd[R300_VPT_XOFFSET] = r300PackFloat32(tx);
1097 rmesa->hw.vpt.cmd[R300_VPT_YOFFSET] = r300PackFloat32(ty);
1098
1099 }
1100
1101 radeonUpdateScissor(ctx);
1102 }
1103
1104 /**
1105 * Tell the card where to render (offset, pitch).
1106 * Effected by glDrawBuffer, etc
1107 */
1108 void r300UpdateDrawBuffer(GLcontext * ctx)
1109 {
1110 r300ContextPtr rmesa = R300_CONTEXT(ctx);
1111 r300ContextPtr r300 = rmesa;
1112 struct gl_framebuffer *fb = ctx->DrawBuffer;
1113 driRenderbuffer *drb;
1114
1115 if (fb->_ColorDrawBufferIndexes[0] == BUFFER_FRONT_LEFT) {
1116 /* draw to front */
1117 drb =
1118 (driRenderbuffer *) fb->Attachment[BUFFER_FRONT_LEFT].
1119 Renderbuffer;
1120 } else if (fb->_ColorDrawBufferIndexes[0] == BUFFER_BACK_LEFT) {
1121 /* draw to back */
1122 drb =
1123 (driRenderbuffer *) fb->Attachment[BUFFER_BACK_LEFT].
1124 Renderbuffer;
1125 } else {
1126 /* drawing to multiple buffers, or none */
1127 return;
1128 }
1129
1130 assert(drb);
1131 assert(drb->flippedPitch);
1132
1133 R300_STATECHANGE(rmesa, cb);
1134
1135 r300->hw.cb.cmd[R300_CB_OFFSET] = drb->flippedOffset + //r300->radeon.state.color.drawOffset +
1136 r300->radeon.radeonScreen->fbLocation;
1137 r300->hw.cb.cmd[R300_CB_PITCH] = drb->flippedPitch; //r300->radeon.state.color.drawPitch;
1138
1139 if (r300->radeon.radeonScreen->cpp == 4)
1140 r300->hw.cb.cmd[R300_CB_PITCH] |= R300_COLOR_FORMAT_ARGB8888;
1141 else
1142 r300->hw.cb.cmd[R300_CB_PITCH] |= R300_COLOR_FORMAT_RGB565;
1143
1144 if (r300->radeon.sarea->tiling_enabled)
1145 r300->hw.cb.cmd[R300_CB_PITCH] |= R300_COLOR_TILE_ENABLE;
1146 #if 0
1147 R200_STATECHANGE(rmesa, ctx);
1148
1149 /* Note: we used the (possibly) page-flipped values */
1150 rmesa->hw.ctx.cmd[CTX_RB3D_COLOROFFSET]
1151 = ((drb->flippedOffset + rmesa->r200Screen->fbLocation)
1152 & R200_COLOROFFSET_MASK);
1153 rmesa->hw.ctx.cmd[CTX_RB3D_COLORPITCH] = drb->flippedPitch;
1154
1155 if (rmesa->sarea->tiling_enabled) {
1156 rmesa->hw.ctx.cmd[CTX_RB3D_COLORPITCH] |=
1157 R200_COLOR_TILE_ENABLE;
1158 }
1159 #endif
1160 }
1161
1162 static void
1163 r300FetchStateParameter(GLcontext * ctx,
1164 const gl_state_index state[STATE_LENGTH],
1165 GLfloat * value)
1166 {
1167 r300ContextPtr r300 = R300_CONTEXT(ctx);
1168
1169 switch (state[0]) {
1170 case STATE_INTERNAL:
1171 switch (state[1]) {
1172 case STATE_R300_WINDOW_DIMENSION:
1173 value[0] = r300->radeon.dri.drawable->w * 0.5f; /* width*0.5 */
1174 value[1] = r300->radeon.dri.drawable->h * 0.5f; /* height*0.5 */
1175 value[2] = 0.5F; /* for moving range [-1 1] -> [0 1] */
1176 value[3] = 1.0F; /* not used */
1177 break;
1178
1179 case STATE_R300_TEXRECT_FACTOR:{
1180 struct gl_texture_object *t =
1181 ctx->Texture.Unit[state[2]].CurrentRect;
1182
1183 if (t && t->Image[0][t->BaseLevel]) {
1184 struct gl_texture_image *image =
1185 t->Image[0][t->BaseLevel];
1186 value[0] = 1.0 / image->Width2;
1187 value[1] = 1.0 / image->Height2;
1188 } else {
1189 value[0] = 1.0;
1190 value[1] = 1.0;
1191 }
1192 value[2] = 1.0;
1193 value[3] = 1.0;
1194 break;
1195 }
1196
1197 default:
1198 break;
1199 }
1200 break;
1201
1202 default:
1203 break;
1204 }
1205 }
1206
1207 /**
1208 * Update R300's own internal state parameters.
1209 * For now just STATE_R300_WINDOW_DIMENSION
1210 */
1211 void r300UpdateStateParameters(GLcontext * ctx, GLuint new_state)
1212 {
1213 struct r300_fragment_program *fp;
1214 struct gl_program_parameter_list *paramList;
1215 GLuint i;
1216
1217 if (!(new_state & (_NEW_BUFFERS | _NEW_PROGRAM)))
1218 return;
1219
1220 fp = (struct r300_fragment_program *)ctx->FragmentProgram._Current;
1221 if (!fp)
1222 return;
1223
1224 paramList = fp->mesa_program.Base.Parameters;
1225
1226 if (!paramList)
1227 return;
1228
1229 for (i = 0; i < paramList->NumParameters; i++) {
1230 if (paramList->Parameters[i].Type == PROGRAM_STATE_VAR) {
1231 r300FetchStateParameter(ctx,
1232 paramList->Parameters[i].
1233 StateIndexes,
1234 paramList->ParameterValues[i]);
1235 }
1236 }
1237 }
1238
1239 /* =============================================================
1240 * Polygon state
1241 */
1242 static void r300PolygonOffset(GLcontext * ctx, GLfloat factor, GLfloat units)
1243 {
1244 r300ContextPtr rmesa = R300_CONTEXT(ctx);
1245 GLfloat constant = units;
1246
1247 switch (ctx->Visual.depthBits) {
1248 case 16:
1249 constant *= 4.0;
1250 break;
1251 case 24:
1252 constant *= 2.0;
1253 break;
1254 }
1255
1256 factor *= 12.0;
1257
1258 /* fprintf(stderr, "%s f:%f u:%f\n", __FUNCTION__, factor, constant); */
1259
1260 R300_STATECHANGE(rmesa, zbs);
1261 rmesa->hw.zbs.cmd[R300_ZBS_T_FACTOR] = r300PackFloat32(factor);
1262 rmesa->hw.zbs.cmd[R300_ZBS_T_CONSTANT] = r300PackFloat32(constant);
1263 rmesa->hw.zbs.cmd[R300_ZBS_W_FACTOR] = r300PackFloat32(factor);
1264 rmesa->hw.zbs.cmd[R300_ZBS_W_CONSTANT] = r300PackFloat32(constant);
1265 }
1266
1267 /* Routing and texture-related */
1268
1269 /* r300 doesnt handle GL_CLAMP and GL_MIRROR_CLAMP_EXT correctly when filter is NEAREST.
1270 * Since texwrap produces same results for GL_CLAMP and GL_CLAMP_TO_EDGE we use them instead.
1271 * We need to recalculate wrap modes whenever filter mode is changed because someone might do:
1272 * glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
1273 * glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
1274 * glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
1275 * Since r300 completely ignores R300_TX_CLAMP when either min or mag is nearest it cant handle
1276 * combinations where only one of them is nearest.
1277 */
1278 static unsigned long gen_fixed_filter(unsigned long f)
1279 {
1280 unsigned long mag, min, needs_fixing = 0;
1281 //return f;
1282
1283 /* We ignore MIRROR bit so we dont have to do everything twice */
1284 if ((f & ((7 - 1) << R300_TX_WRAP_S_SHIFT)) ==
1285 (R300_TX_CLAMP << R300_TX_WRAP_S_SHIFT)) {
1286 needs_fixing |= 1;
1287 }
1288 if ((f & ((7 - 1) << R300_TX_WRAP_T_SHIFT)) ==
1289 (R300_TX_CLAMP << R300_TX_WRAP_T_SHIFT)) {
1290 needs_fixing |= 2;
1291 }
1292 if ((f & ((7 - 1) << R300_TX_WRAP_R_SHIFT)) ==
1293 (R300_TX_CLAMP << R300_TX_WRAP_R_SHIFT)) {
1294 needs_fixing |= 4;
1295 }
1296
1297 if (!needs_fixing)
1298 return f;
1299
1300 mag = f & R300_TX_MAG_FILTER_MASK;
1301 min = f & (R300_TX_MIN_FILTER_MASK|R300_TX_MIN_FILTER_MIP_MASK);
1302
1303 /* TODO: Check for anisto filters too */
1304 if ((mag != R300_TX_MAG_FILTER_NEAREST)
1305 && (min != R300_TX_MIN_FILTER_NEAREST))
1306 return f;
1307
1308 /* r300 cant handle these modes hence we force nearest to linear */
1309 if ((mag == R300_TX_MAG_FILTER_NEAREST)
1310 && (min != R300_TX_MIN_FILTER_NEAREST)) {
1311 f &= ~R300_TX_MAG_FILTER_NEAREST;
1312 f |= R300_TX_MAG_FILTER_LINEAR;
1313 return f;
1314 }
1315
1316 if ((min == R300_TX_MIN_FILTER_NEAREST)
1317 && (mag != R300_TX_MAG_FILTER_NEAREST)) {
1318 f &= ~R300_TX_MIN_FILTER_NEAREST;
1319 f |= R300_TX_MIN_FILTER_LINEAR;
1320 return f;
1321 }
1322
1323 /* Both are nearest */
1324 if (needs_fixing & 1) {
1325 f &= ~((7 - 1) << R300_TX_WRAP_S_SHIFT);
1326 f |= R300_TX_CLAMP_TO_EDGE << R300_TX_WRAP_S_SHIFT;
1327 }
1328 if (needs_fixing & 2) {
1329 f &= ~((7 - 1) << R300_TX_WRAP_T_SHIFT);
1330 f |= R300_TX_CLAMP_TO_EDGE << R300_TX_WRAP_T_SHIFT;
1331 }
1332 if (needs_fixing & 4) {
1333 f &= ~((7 - 1) << R300_TX_WRAP_R_SHIFT);
1334 f |= R300_TX_CLAMP_TO_EDGE << R300_TX_WRAP_R_SHIFT;
1335 }
1336 return f;
1337 }
1338
1339 static void r300SetupFragmentShaderTextures(GLcontext *ctx, int *tmu_mappings)
1340 {
1341 r300ContextPtr r300 = R300_CONTEXT(ctx);
1342 int i;
1343 struct r300_fragment_program *fp = (struct r300_fragment_program *)
1344 (char *)ctx->FragmentProgram._Current;
1345 struct r300_fragment_program_code *code = &fp->code;
1346
1347 R300_STATECHANGE(r300, fpt);
1348
1349 for (i = 0; i < code->tex.length; i++) {
1350 int unit;
1351 int opcode;
1352 unsigned long val;
1353
1354 unit = code->tex.inst[i] >> R300_TEX_ID_SHIFT;
1355 unit &= 15;
1356
1357 val = code->tex.inst[i];
1358 val &= ~R300_TEX_ID_MASK;
1359
1360 opcode =
1361 (val & R300_TEX_INST_MASK) >> R300_TEX_INST_SHIFT;
1362 if (opcode == R300_TEX_OP_KIL) {
1363 r300->hw.fpt.cmd[R300_FPT_INSTR_0 + i] = val;
1364 } else {
1365 if (tmu_mappings[unit] >= 0) {
1366 val |=
1367 tmu_mappings[unit] <<
1368 R300_TEX_ID_SHIFT;
1369 r300->hw.fpt.cmd[R300_FPT_INSTR_0 + i] = val;
1370 } else {
1371 // We get here when the corresponding texture image is incomplete
1372 // (e.g. incomplete mipmaps etc.)
1373 r300->hw.fpt.cmd[R300_FPT_INSTR_0 + i] = val;
1374 }
1375 }
1376 }
1377
1378 r300->hw.fpt.cmd[R300_FPT_CMD_0] =
1379 cmdpacket0(R300_US_TEX_INST_0, code->tex.length);
1380 }
1381
1382 static void r500SetupFragmentShaderTextures(GLcontext *ctx, int *tmu_mappings)
1383 {
1384 int i;
1385 struct r500_fragment_program *fp = (struct r500_fragment_program *)
1386 (char *)ctx->FragmentProgram._Current;
1387 struct r500_fragment_program_code *code = &fp->code;
1388
1389 /* find all the texture instructions and relocate the texture units */
1390 for (i = 0; i < code->inst_end + 1; i++) {
1391 if ((code->inst[i].inst0 & 0x3) == R500_INST_TYPE_TEX) {
1392 uint32_t val;
1393 int unit, opcode, new_unit;
1394
1395 val = code->inst[i].inst1;
1396
1397 unit = (val >> 16) & 0xf;
1398
1399 val &= ~(0xf << 16);
1400
1401 opcode = val & (0x7 << 22);
1402 if (opcode == R500_TEX_INST_TEXKILL) {
1403 new_unit = 0;
1404 } else {
1405 if (tmu_mappings[unit] >= 0) {
1406 new_unit = tmu_mappings[unit];
1407 } else {
1408 new_unit = 0;
1409 }
1410 }
1411 val |= R500_TEX_ID(new_unit);
1412 code->inst[i].inst1 = val;
1413 }
1414 }
1415 }
1416
1417 static GLuint translate_lod_bias(GLfloat bias)
1418 {
1419 GLint b = (int)(bias*32);
1420 if (b >= (1 << 9))
1421 b = (1 << 9)-1;
1422 else if (b < -(1 << 9))
1423 b = -(1 << 9);
1424 return (((GLuint)b) << R300_LOD_BIAS_SHIFT) & R300_LOD_BIAS_MASK;
1425 }
1426
1427 static void r300SetupTextures(GLcontext * ctx)
1428 {
1429 int i, mtu;
1430 struct r300_tex_obj *t;
1431 r300ContextPtr r300 = R300_CONTEXT(ctx);
1432 int hw_tmu = 0;
1433 int last_hw_tmu = -1; /* -1 translates into no setup costs for fields */
1434 int tmu_mappings[R300_MAX_TEXTURE_UNITS] = { -1, };
1435 struct r300_fragment_program *fp = (struct r300_fragment_program *)
1436 (char *)ctx->FragmentProgram._Current;
1437
1438 R300_STATECHANGE(r300, txe);
1439 R300_STATECHANGE(r300, tex.filter);
1440 R300_STATECHANGE(r300, tex.filter_1);
1441 R300_STATECHANGE(r300, tex.size);
1442 R300_STATECHANGE(r300, tex.format);
1443 R300_STATECHANGE(r300, tex.pitch);
1444 R300_STATECHANGE(r300, tex.offset);
1445 R300_STATECHANGE(r300, tex.chroma_key);
1446 R300_STATECHANGE(r300, tex.border_color);
1447
1448 r300->hw.txe.cmd[R300_TXE_ENABLE] = 0x0;
1449
1450 mtu = r300->radeon.glCtx->Const.MaxTextureUnits;
1451 if (RADEON_DEBUG & DEBUG_STATE)
1452 fprintf(stderr, "mtu=%d\n", mtu);
1453
1454 if (mtu > R300_MAX_TEXTURE_UNITS) {
1455 fprintf(stderr,
1456 "Aiiee ! mtu=%d is greater than R300_MAX_TEXTURE_UNITS=%d\n",
1457 mtu, R300_MAX_TEXTURE_UNITS);
1458 _mesa_exit(-1);
1459 }
1460
1461 /* We cannot let disabled tmu offsets pass DRM */
1462 for (i = 0; i < mtu; i++) {
1463 if (ctx->Texture.Unit[i]._ReallyEnabled) {
1464
1465 #if 0 /* Enables old behaviour */
1466 hw_tmu = i;
1467 #endif
1468 tmu_mappings[i] = hw_tmu;
1469
1470 t = r300->state.texture.unit[i].texobj;
1471 /* XXX questionable fix for bug 9170: */
1472 if (!t)
1473 continue;
1474
1475 if ((t->format & 0xffffff00) == 0xffffff00) {
1476 WARN_ONCE
1477 ("unknown texture format (entry %x) encountered. Help me !\n",
1478 t->format & 0xff);
1479 }
1480
1481 if (RADEON_DEBUG & DEBUG_STATE)
1482 fprintf(stderr,
1483 "Activating texture unit %d\n", i);
1484
1485 r300->hw.txe.cmd[R300_TXE_ENABLE] |= (1 << hw_tmu);
1486
1487 r300->hw.tex.filter.cmd[R300_TEX_VALUE_0 +
1488 hw_tmu] =
1489 gen_fixed_filter(t->filter) | (hw_tmu << 28);
1490 /* Note: There is a LOD bias per texture unit and a LOD bias
1491 * per texture object. We add them here to get the correct behaviour.
1492 * (The per-texture object LOD bias was introduced in OpenGL 1.4
1493 * and is not present in the EXT_texture_object extension).
1494 */
1495 r300->hw.tex.filter_1.cmd[R300_TEX_VALUE_0 + hw_tmu] =
1496 t->filter_1 |
1497 translate_lod_bias(ctx->Texture.Unit[i].LodBias + t->base.tObj->LodBias);
1498 r300->hw.tex.size.cmd[R300_TEX_VALUE_0 + hw_tmu] =
1499 t->size;
1500 r300->hw.tex.format.cmd[R300_TEX_VALUE_0 +
1501 hw_tmu] = t->format;
1502 r300->hw.tex.pitch.cmd[R300_TEX_VALUE_0 + hw_tmu] =
1503 t->pitch_reg;
1504 r300->hw.tex.offset.cmd[R300_TEX_VALUE_0 +
1505 hw_tmu] = t->offset;
1506
1507 if (t->offset & R300_TXO_MACRO_TILE) {
1508 WARN_ONCE("macro tiling enabled!\n");
1509 }
1510
1511 if (t->offset & R300_TXO_MICRO_TILE) {
1512 WARN_ONCE("micro tiling enabled!\n");
1513 }
1514
1515 r300->hw.tex.chroma_key.cmd[R300_TEX_VALUE_0 +
1516 hw_tmu] = 0x0;
1517 r300->hw.tex.border_color.cmd[R300_TEX_VALUE_0 +
1518 hw_tmu] =
1519 t->pp_border_color;
1520
1521 last_hw_tmu = hw_tmu;
1522
1523 hw_tmu++;
1524 }
1525 }
1526
1527 r300->hw.tex.filter.cmd[R300_TEX_CMD_0] =
1528 cmdpacket0(R300_TX_FILTER0_0, last_hw_tmu + 1);
1529 r300->hw.tex.filter_1.cmd[R300_TEX_CMD_0] =
1530 cmdpacket0(R300_TX_FILTER1_0, last_hw_tmu + 1);
1531 r300->hw.tex.size.cmd[R300_TEX_CMD_0] =
1532 cmdpacket0(R300_TX_SIZE_0, last_hw_tmu + 1);
1533 r300->hw.tex.format.cmd[R300_TEX_CMD_0] =
1534 cmdpacket0(R300_TX_FORMAT_0, last_hw_tmu + 1);
1535 r300->hw.tex.pitch.cmd[R300_TEX_CMD_0] =
1536 cmdpacket0(R300_TX_FORMAT2_0, last_hw_tmu + 1);
1537 r300->hw.tex.offset.cmd[R300_TEX_CMD_0] =
1538 cmdpacket0(R300_TX_OFFSET_0, last_hw_tmu + 1);
1539 r300->hw.tex.chroma_key.cmd[R300_TEX_CMD_0] =
1540 cmdpacket0(R300_TX_CHROMA_KEY_0, last_hw_tmu + 1);
1541 r300->hw.tex.border_color.cmd[R300_TEX_CMD_0] =
1542 cmdpacket0(R300_TX_BORDER_COLOR_0, last_hw_tmu + 1);
1543
1544 if (!fp) /* should only happenen once, just after context is created */
1545 return;
1546
1547 if (r300->radeon.radeonScreen->chip_family < CHIP_FAMILY_RV515) {
1548 if (fp->mesa_program.UsesKill && last_hw_tmu < 0) {
1549 // The KILL operation requires the first texture unit
1550 // to be enabled.
1551 r300->hw.txe.cmd[R300_TXE_ENABLE] |= 1;
1552 r300->hw.tex.filter.cmd[R300_TEX_VALUE_0] = 0;
1553 r300->hw.tex.filter.cmd[R300_TEX_CMD_0] =
1554 cmdpacket0(R300_TX_FILTER0_0, 1);
1555 }
1556 r300SetupFragmentShaderTextures(ctx, tmu_mappings);
1557 } else
1558 r500SetupFragmentShaderTextures(ctx, tmu_mappings);
1559
1560 if (RADEON_DEBUG & DEBUG_STATE)
1561 fprintf(stderr, "TX_ENABLE: %08x last_hw_tmu=%d\n",
1562 r300->hw.txe.cmd[R300_TXE_ENABLE], last_hw_tmu);
1563 }
1564
1565 union r300_outputs_written {
1566 GLuint vp_outputs; /* hw_tcl_on */
1567 DECLARE_RENDERINPUTS(index_bitset); /* !hw_tcl_on */
1568 };
1569
1570 #define R300_OUTPUTS_WRITTEN_TEST(ow, vp_result, tnl_attrib) \
1571 ((hw_tcl_on) ? (ow).vp_outputs & (1 << (vp_result)) : \
1572 RENDERINPUTS_TEST( (ow.index_bitset), (tnl_attrib) ))
1573
1574 static void r300SetupRSUnit(GLcontext * ctx)
1575 {
1576 r300ContextPtr r300 = R300_CONTEXT(ctx);
1577 /* I'm still unsure if these are needed */
1578 GLuint interp_col[8];
1579 TNLcontext *tnl = TNL_CONTEXT(ctx);
1580 struct vertex_buffer *VB = &tnl->vb;
1581 union r300_outputs_written OutputsWritten;
1582 GLuint InputsRead;
1583 int fp_reg, high_rr;
1584 int col_interp_nr;
1585 int rs_tex_count = 0, rs_col_count = 0;
1586 int i, count;
1587
1588 memset(interp_col, 0, sizeof(interp_col));
1589
1590 if (hw_tcl_on)
1591 OutputsWritten.vp_outputs = CURRENT_VERTEX_SHADER(ctx)->key.OutputsWritten;
1592 else
1593 RENDERINPUTS_COPY(OutputsWritten.index_bitset, r300->state.render_inputs_bitset);
1594
1595 if (ctx->FragmentProgram._Current)
1596 InputsRead = ctx->FragmentProgram._Current->Base.InputsRead;
1597 else {
1598 fprintf(stderr, "No ctx->FragmentProgram._Current!!\n");
1599 return; /* This should only ever happen once.. */
1600 }
1601
1602 R300_STATECHANGE(r300, ri);
1603 R300_STATECHANGE(r300, rc);
1604 R300_STATECHANGE(r300, rr);
1605
1606 fp_reg = col_interp_nr = high_rr = 0;
1607
1608 r300->hw.rr.cmd[R300_RR_INST_1] = 0;
1609
1610 if (InputsRead & FRAG_BIT_WPOS) {
1611 for (i = 0; i < ctx->Const.MaxTextureUnits; i++)
1612 if (!(InputsRead & (FRAG_BIT_TEX0 << i)))
1613 break;
1614
1615 if (i == ctx->Const.MaxTextureUnits) {
1616 fprintf(stderr, "\tno free texcoord found...\n");
1617 _mesa_exit(-1);
1618 }
1619
1620 InputsRead |= (FRAG_BIT_TEX0 << i);
1621 InputsRead &= ~FRAG_BIT_WPOS;
1622 }
1623
1624 if (InputsRead & FRAG_BIT_COL0) {
1625 count = VB->AttribPtr[_TNL_ATTRIB_COLOR0]->size;
1626 interp_col[0] |= R300_RS_COL_PTR(rs_col_count);
1627 if (count == 3)
1628 interp_col[0] |= R300_RS_COL_FMT(R300_RS_COL_FMT_RGB1);
1629 rs_col_count += count;
1630 }
1631 else
1632 interp_col[0] = R300_RS_COL_FMT(R300_RS_COL_FMT_0001);
1633
1634 if (InputsRead & FRAG_BIT_COL1) {
1635 count = VB->AttribPtr[_TNL_ATTRIB_COLOR1]->size;
1636 if (count == 3)
1637 interp_col[1] |= R300_RS_COL_FMT(R300_RS_COL_FMT_RGB0);
1638 interp_col[1] |= R300_RS_COL_PTR(1);
1639 rs_col_count += count;
1640 }
1641
1642
1643 for (i = 0; i < ctx->Const.MaxTextureUnits; i++) {
1644 int swiz;
1645
1646 /* with TCL we always seem to route 4 components */
1647 if (hw_tcl_on)
1648 count = 4;
1649 else
1650 count = VB->AttribPtr[_TNL_ATTRIB_TEX(i)]->size;
1651
1652 r300->hw.ri.cmd[R300_RI_INTERP_0 + i] = interp_col[i] | rs_tex_count;
1653 switch(count) {
1654 case 4: swiz = R300_RS_SEL_S(0) | R300_RS_SEL_T(1) | R300_RS_SEL_R(2) | R300_RS_SEL_Q(3); break;
1655 case 3: swiz = R300_RS_SEL_S(0) | R300_RS_SEL_T(1) | R300_RS_SEL_R(2) | R300_RS_SEL_Q(R300_RS_SEL_K1); break;
1656 default:
1657 case 1:
1658 case 2: swiz = R300_RS_SEL_S(0) | R300_RS_SEL_T(1) | R300_RS_SEL_R(R300_RS_SEL_K0) | R300_RS_SEL_Q(R300_RS_SEL_K1); break;
1659 };
1660
1661 r300->hw.ri.cmd[R300_RI_INTERP_0 + i] |= swiz;
1662
1663 r300->hw.rr.cmd[R300_RR_INST_0 + fp_reg] = 0;
1664 if (InputsRead & (FRAG_BIT_TEX0 << i)) {
1665
1666 rs_tex_count += count;
1667
1668 //assert(r300->state.texture.tc_count != 0);
1669 r300->hw.rr.cmd[R300_RR_INST_0 + fp_reg] |= R300_RS_INST_TEX_CN_WRITE | i /* source INTERP */
1670 | (fp_reg << R300_RS_INST_TEX_ADDR_SHIFT);
1671 high_rr = fp_reg;
1672
1673 /* Passing invalid data here can lock the GPU. */
1674 if (R300_OUTPUTS_WRITTEN_TEST(OutputsWritten, VERT_RESULT_TEX0 + i, _TNL_ATTRIB_TEX(i))) {
1675 InputsRead &= ~(FRAG_BIT_TEX0 << i);
1676 fp_reg++;
1677 } else {
1678 WARN_ONCE("fragprog wants coords for tex%d, vp doesn't provide them!\n", i);
1679 }
1680 }
1681 }
1682
1683 if (InputsRead & FRAG_BIT_COL0) {
1684 if (R300_OUTPUTS_WRITTEN_TEST(OutputsWritten, VERT_RESULT_COL0, _TNL_ATTRIB_COLOR0)) {
1685 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);
1686 InputsRead &= ~FRAG_BIT_COL0;
1687 col_interp_nr++;
1688 } else {
1689 WARN_ONCE("fragprog wants col0, vp doesn't provide it\n");
1690 }
1691 }
1692
1693 if (InputsRead & FRAG_BIT_COL1) {
1694 if (R300_OUTPUTS_WRITTEN_TEST(OutputsWritten, VERT_RESULT_COL1, _TNL_ATTRIB_COLOR1)) {
1695 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);
1696 InputsRead &= ~FRAG_BIT_COL1;
1697 if (high_rr < 1)
1698 high_rr = 1;
1699 col_interp_nr++;
1700 } else {
1701 WARN_ONCE("fragprog wants col1, vp doesn't provide it\n");
1702 }
1703 }
1704
1705 /* Need at least one. This might still lock as the values are undefined... */
1706 if (rs_tex_count == 0 && col_interp_nr == 0) {
1707 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);
1708 col_interp_nr++;
1709 }
1710
1711 r300->hw.rc.cmd[1] = 0 | (rs_tex_count << R300_IT_COUNT_SHIFT)
1712 | (col_interp_nr << R300_IC_COUNT_SHIFT)
1713 | R300_HIRES_EN;
1714
1715 assert(high_rr >= 0);
1716 r300->hw.rr.cmd[R300_RR_CMD_0] = cmdpacket0(R300_RS_INST_0, high_rr + 1);
1717 r300->hw.rc.cmd[2] = high_rr;
1718
1719 if (InputsRead)
1720 WARN_ONCE("Don't know how to satisfy InputsRead=0x%08x\n", InputsRead);
1721 }
1722
1723 static void r500SetupRSUnit(GLcontext * ctx)
1724 {
1725 r300ContextPtr r300 = R300_CONTEXT(ctx);
1726 /* I'm still unsure if these are needed */
1727 GLuint interp_col[8];
1728 union r300_outputs_written OutputsWritten;
1729 TNLcontext *tnl = TNL_CONTEXT(ctx);
1730 struct vertex_buffer *VB = &tnl->vb;
1731 GLuint InputsRead;
1732 int fp_reg, high_rr;
1733 int rs_col_count = 0;
1734 int in_texcoords, col_interp_nr;
1735 int i, count;
1736
1737 memset(interp_col, 0, sizeof(interp_col));
1738 if (hw_tcl_on)
1739 OutputsWritten.vp_outputs = CURRENT_VERTEX_SHADER(ctx)->key.OutputsWritten;
1740 else
1741 RENDERINPUTS_COPY(OutputsWritten.index_bitset, r300->state.render_inputs_bitset);
1742
1743 if (ctx->FragmentProgram._Current)
1744 InputsRead = ctx->FragmentProgram._Current->Base.InputsRead;
1745 else {
1746 fprintf(stderr, "No ctx->FragmentProgram._Current!!\n");
1747 return; /* This should only ever happen once.. */
1748 }
1749
1750 R300_STATECHANGE(r300, ri);
1751 R300_STATECHANGE(r300, rc);
1752 R300_STATECHANGE(r300, rr);
1753
1754 fp_reg = col_interp_nr = high_rr = in_texcoords = 0;
1755
1756 r300->hw.rr.cmd[R300_RR_INST_1] = 0;
1757
1758 if (InputsRead & FRAG_BIT_WPOS) {
1759 for (i = 0; i < ctx->Const.MaxTextureUnits; i++)
1760 if (!(InputsRead & (FRAG_BIT_TEX0 << i)))
1761 break;
1762
1763 if (i == ctx->Const.MaxTextureUnits) {
1764 fprintf(stderr, "\tno free texcoord found...\n");
1765 _mesa_exit(-1);
1766 }
1767
1768 InputsRead |= (FRAG_BIT_TEX0 << i);
1769 InputsRead &= ~FRAG_BIT_WPOS;
1770 }
1771
1772 if (InputsRead & FRAG_BIT_COL0) {
1773 count = VB->AttribPtr[_TNL_ATTRIB_COLOR0]->size;
1774 interp_col[0] |= R500_RS_COL_PTR(rs_col_count);
1775 if (count == 3)
1776 interp_col[0] |= R500_RS_COL_FMT(R300_RS_COL_FMT_RGB1);
1777 rs_col_count += count;
1778 }
1779 else
1780 interp_col[0] = R500_RS_COL_FMT(R300_RS_COL_FMT_0001);
1781
1782 if (InputsRead & FRAG_BIT_COL1) {
1783 count = VB->AttribPtr[_TNL_ATTRIB_COLOR1]->size;
1784 interp_col[1] |= R500_RS_COL_PTR(1);
1785 if (count == 3)
1786 interp_col[1] |= R500_RS_COL_FMT(R300_RS_COL_FMT_RGB0);
1787 rs_col_count += count;
1788 }
1789
1790 for (i = 0; i < ctx->Const.MaxTextureUnits; i++) {
1791 GLuint swiz = 0;
1792
1793 /* with TCL we always seem to route 4 components */
1794 if (InputsRead & (FRAG_BIT_TEX0 << i)) {
1795
1796 if (hw_tcl_on)
1797 count = 4;
1798 else
1799 count = VB->AttribPtr[_TNL_ATTRIB_TEX(i)]->size;
1800
1801 /* always have on texcoord */
1802 swiz |= in_texcoords++ << R500_RS_IP_TEX_PTR_S_SHIFT;
1803 if (count >= 2)
1804 swiz |= in_texcoords++ << R500_RS_IP_TEX_PTR_T_SHIFT;
1805 else
1806 swiz |= R500_RS_IP_PTR_K0 << R500_RS_IP_TEX_PTR_T_SHIFT;
1807
1808 if (count >= 3)
1809 swiz |= in_texcoords++ << R500_RS_IP_TEX_PTR_R_SHIFT;
1810 else
1811 swiz |= R500_RS_IP_PTR_K0 << R500_RS_IP_TEX_PTR_R_SHIFT;
1812
1813 if (count == 4)
1814 swiz |= in_texcoords++ << R500_RS_IP_TEX_PTR_Q_SHIFT;
1815 else
1816 swiz |= R500_RS_IP_PTR_K1 << R500_RS_IP_TEX_PTR_Q_SHIFT;
1817
1818 } else
1819 swiz = (R500_RS_IP_PTR_K0 << R500_RS_IP_TEX_PTR_S_SHIFT) |
1820 (R500_RS_IP_PTR_K0 << R500_RS_IP_TEX_PTR_T_SHIFT) |
1821 (R500_RS_IP_PTR_K0 << R500_RS_IP_TEX_PTR_R_SHIFT) |
1822 (R500_RS_IP_PTR_K1 << R500_RS_IP_TEX_PTR_Q_SHIFT);
1823
1824 r300->hw.ri.cmd[R300_RI_INTERP_0 + i] = interp_col[i] | swiz;
1825
1826 r300->hw.rr.cmd[R300_RR_INST_0 + fp_reg] = 0;
1827 if (InputsRead & (FRAG_BIT_TEX0 << i)) {
1828 //assert(r300->state.texture.tc_count != 0);
1829 r300->hw.rr.cmd[R300_RR_INST_0 + fp_reg] |= R500_RS_INST_TEX_CN_WRITE | i /* source INTERP */
1830 | (fp_reg << R500_RS_INST_TEX_ADDR_SHIFT);
1831 high_rr = fp_reg;
1832
1833 /* Passing invalid data here can lock the GPU. */
1834 if (R300_OUTPUTS_WRITTEN_TEST(OutputsWritten, VERT_RESULT_TEX0 + i, _TNL_ATTRIB_TEX(i))) {
1835 InputsRead &= ~(FRAG_BIT_TEX0 << i);
1836 fp_reg++;
1837 } else {
1838 WARN_ONCE("fragprog wants coords for tex%d, vp doesn't provide them!\n", i);
1839 }
1840 }
1841 }
1842
1843 if (InputsRead & FRAG_BIT_COL0) {
1844 if (R300_OUTPUTS_WRITTEN_TEST(OutputsWritten, VERT_RESULT_COL0, _TNL_ATTRIB_COLOR0)) {
1845 r300->hw.rr.cmd[R300_RR_INST_0] |= R500_RS_INST_COL_CN_WRITE | (fp_reg++ << R500_RS_INST_COL_ADDR_SHIFT);
1846 InputsRead &= ~FRAG_BIT_COL0;
1847 col_interp_nr++;
1848 } else {
1849 WARN_ONCE("fragprog wants col0, vp doesn't provide it\n");
1850 }
1851 }
1852
1853 if (InputsRead & FRAG_BIT_COL1) {
1854 if (R300_OUTPUTS_WRITTEN_TEST(OutputsWritten, VERT_RESULT_COL1, _TNL_ATTRIB_COLOR1)) {
1855 r300->hw.rr.cmd[R300_RR_INST_1] |= (1 << 12) | R500_RS_INST_COL_CN_WRITE | (fp_reg++ << R500_RS_INST_COL_ADDR_SHIFT);
1856 InputsRead &= ~FRAG_BIT_COL1;
1857 if (high_rr < 1)
1858 high_rr = 1;
1859 col_interp_nr++;
1860 } else {
1861 WARN_ONCE("fragprog wants col1, vp doesn't provide it\n");
1862 }
1863 }
1864
1865 /* Need at least one. This might still lock as the values are undefined... */
1866 if (in_texcoords == 0 && col_interp_nr == 0) {
1867 r300->hw.rr.cmd[R300_RR_INST_0] |= 0 | R500_RS_INST_COL_CN_WRITE | (fp_reg++ << R500_RS_INST_COL_ADDR_SHIFT);
1868 col_interp_nr++;
1869 }
1870
1871 r300->hw.rc.cmd[1] = 0 | (in_texcoords << R300_IT_COUNT_SHIFT)
1872 | (col_interp_nr << R300_IC_COUNT_SHIFT)
1873 | R300_HIRES_EN;
1874
1875 assert(high_rr >= 0);
1876 r300->hw.rr.cmd[R300_RR_CMD_0] = cmdpacket0(R500_RS_INST_0, high_rr + 1);
1877 r300->hw.rc.cmd[2] = 0xC0 | high_rr;
1878
1879 if (InputsRead)
1880 WARN_ONCE("Don't know how to satisfy InputsRead=0x%08x\n", InputsRead);
1881 }
1882
1883
1884
1885
1886 #define bump_vpu_count(ptr, new_count) do{\
1887 drm_r300_cmd_header_t* _p=((drm_r300_cmd_header_t*)(ptr));\
1888 int _nc=(new_count)/4; \
1889 assert(_nc < 256); \
1890 if(_nc>_p->vpu.count)_p->vpu.count=_nc;\
1891 }while(0)
1892
1893 static INLINE void r300SetupVertexProgramFragment(r300ContextPtr r300, int dest, struct r300_vertex_shader_fragment *vsf)
1894 {
1895 int i;
1896
1897 if (vsf->length == 0)
1898 return;
1899
1900 if (vsf->length & 0x3) {
1901 fprintf(stderr, "VERTEX_SHADER_FRAGMENT must have length divisible by 4\n");
1902 _mesa_exit(-1);
1903 }
1904
1905 switch ((dest >> 8) & 0xf) {
1906 case 0:
1907 R300_STATECHANGE(r300, vpi);
1908 for (i = 0; i < vsf->length; i++)
1909 r300->hw.vpi.cmd[R300_VPI_INSTR_0 + i + 4 * (dest & 0xff)] = (vsf->body.d[i]);
1910 bump_vpu_count(r300->hw.vpi.cmd, vsf->length + 4 * (dest & 0xff));
1911 break;
1912
1913 case 2:
1914 R300_STATECHANGE(r300, vpp);
1915 for (i = 0; i < vsf->length; i++)
1916 r300->hw.vpp.cmd[R300_VPP_PARAM_0 + i + 4 * (dest & 0xff)] = (vsf->body.d[i]);
1917 bump_vpu_count(r300->hw.vpp.cmd, vsf->length + 4 * (dest & 0xff));
1918 break;
1919 case 4:
1920 R300_STATECHANGE(r300, vps);
1921 for (i = 0; i < vsf->length; i++)
1922 r300->hw.vps.cmd[1 + i + 4 * (dest & 0xff)] = (vsf->body.d[i]);
1923 bump_vpu_count(r300->hw.vps.cmd, vsf->length + 4 * (dest & 0xff));
1924 break;
1925 default:
1926 fprintf(stderr, "%s:%s don't know how to handle dest %04x\n", __FILE__, __FUNCTION__, dest);
1927 _mesa_exit(-1);
1928 }
1929 }
1930
1931 #define MIN3(a, b, c) ((a) < (b) ? MIN2(a, c) : MIN2(b, c))
1932
1933
1934 static void r300VapCntl(r300ContextPtr rmesa, GLuint input_count,
1935 GLuint output_count, GLuint temp_count)
1936 {
1937 int vtx_mem_size;
1938 int pvs_num_slots;
1939 int pvs_num_cntrls;
1940
1941 /* Flush PVS engine before changing PVS_NUM_SLOTS, PVS_NUM_CNTRLS.
1942 * See r500 docs 6.5.2 - done in emit */
1943
1944 /* avoid division by zero */
1945 if (input_count == 0) input_count = 1;
1946 if (output_count == 0) output_count = 1;
1947 if (temp_count == 0) temp_count = 1;
1948
1949 if (rmesa->radeon.radeonScreen->chip_family >= CHIP_FAMILY_RV515)
1950 vtx_mem_size = 128;
1951 else
1952 vtx_mem_size = 72;
1953
1954 pvs_num_slots = MIN3(10, vtx_mem_size/input_count, vtx_mem_size/output_count);
1955 pvs_num_cntrls = MIN2(6, vtx_mem_size/temp_count);
1956
1957 R300_STATECHANGE(rmesa, vap_cntl);
1958 if (rmesa->radeon.radeonScreen->chip_flags & RADEON_CHIPSET_TCL) {
1959 rmesa->hw.vap_cntl.cmd[R300_VAP_CNTL_INSTR] =
1960 (pvs_num_slots << R300_PVS_NUM_SLOTS_SHIFT) |
1961 (pvs_num_cntrls << R300_PVS_NUM_CNTLRS_SHIFT) |
1962 (12 << R300_VF_MAX_VTX_NUM_SHIFT);
1963 if (rmesa->radeon.radeonScreen->chip_family >= CHIP_FAMILY_RV515)
1964 rmesa->hw.vap_cntl.cmd[R300_VAP_CNTL_INSTR] |= R500_TCL_STATE_OPTIMIZATION;
1965 } else
1966 /* not sure about non-tcl */
1967 rmesa->hw.vap_cntl.cmd[R300_VAP_CNTL_INSTR] = ((10 << R300_PVS_NUM_SLOTS_SHIFT) |
1968 (5 << R300_PVS_NUM_CNTLRS_SHIFT) |
1969 (5 << R300_VF_MAX_VTX_NUM_SHIFT));
1970
1971 if (rmesa->radeon.radeonScreen->chip_family == CHIP_FAMILY_RV515)
1972 rmesa->hw.vap_cntl.cmd[R300_VAP_CNTL_INSTR] |= (2 << R300_PVS_NUM_FPUS_SHIFT);
1973 else if ((rmesa->radeon.radeonScreen->chip_family == CHIP_FAMILY_RV530) ||
1974 (rmesa->radeon.radeonScreen->chip_family == CHIP_FAMILY_RV560) ||
1975 (rmesa->radeon.radeonScreen->chip_family == CHIP_FAMILY_RV570))
1976 rmesa->hw.vap_cntl.cmd[R300_VAP_CNTL_INSTR] |= (5 << R300_PVS_NUM_FPUS_SHIFT);
1977 else if ((rmesa->radeon.radeonScreen->chip_family == CHIP_FAMILY_RV410) ||
1978 (rmesa->radeon.radeonScreen->chip_family == CHIP_FAMILY_R420))
1979 rmesa->hw.vap_cntl.cmd[R300_VAP_CNTL_INSTR] |= (6 << R300_PVS_NUM_FPUS_SHIFT);
1980 else if ((rmesa->radeon.radeonScreen->chip_family == CHIP_FAMILY_R520) ||
1981 (rmesa->radeon.radeonScreen->chip_family == CHIP_FAMILY_R580))
1982 rmesa->hw.vap_cntl.cmd[R300_VAP_CNTL_INSTR] |= (8 << R300_PVS_NUM_FPUS_SHIFT);
1983 else
1984 rmesa->hw.vap_cntl.cmd[R300_VAP_CNTL_INSTR] |= (4 << R300_PVS_NUM_FPUS_SHIFT);
1985
1986 }
1987
1988 static void r300SetupDefaultVertexProgram(r300ContextPtr rmesa)
1989 {
1990 struct r300_vertex_shader_state *prog = &(rmesa->state.vertex_shader);
1991 GLuint o_reg = 0;
1992 GLuint i_reg = 0;
1993 int i;
1994 int inst_count = 0;
1995 int param_count = 0;
1996 int program_end = 0;
1997
1998 for (i = VERT_ATTRIB_POS; i < VERT_ATTRIB_MAX; i++) {
1999 if (rmesa->state.sw_tcl_inputs[i] != -1) {
2000 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);
2001 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);
2002 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);
2003 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);
2004 program_end += 4;
2005 i_reg++;
2006 }
2007 }
2008
2009 prog->program.length = program_end;
2010
2011 r300SetupVertexProgramFragment(rmesa, R300_PVS_CODE_START,
2012 &(prog->program));
2013 inst_count = (prog->program.length / 4) - 1;
2014
2015 r300VapCntl(rmesa, i_reg, o_reg, 0);
2016
2017 R300_STATECHANGE(rmesa, pvs);
2018 rmesa->hw.pvs.cmd[R300_PVS_CNTL_1] =
2019 (0 << R300_PVS_FIRST_INST_SHIFT) |
2020 (inst_count << R300_PVS_XYZW_VALID_INST_SHIFT) |
2021 (inst_count << R300_PVS_LAST_INST_SHIFT);
2022 rmesa->hw.pvs.cmd[R300_PVS_CNTL_2] =
2023 (0 << R300_PVS_CONST_BASE_OFFSET_SHIFT) |
2024 (param_count << R300_PVS_MAX_CONST_ADDR_SHIFT);
2025 rmesa->hw.pvs.cmd[R300_PVS_CNTL_3] =
2026 (inst_count << R300_PVS_LAST_VTX_SRC_INST_SHIFT);
2027 }
2028
2029 static int bit_count (int x)
2030 {
2031 x = ((x & 0xaaaaaaaaU) >> 1) + (x & 0x55555555U);
2032 x = ((x & 0xccccccccU) >> 2) + (x & 0x33333333U);
2033 x = (x >> 16) + (x & 0xffff);
2034 x = ((x & 0xf0f0) >> 4) + (x & 0x0f0f);
2035 return (x >> 8) + (x & 0x00ff);
2036 }
2037
2038 static void r300SetupRealVertexProgram(r300ContextPtr rmesa)
2039 {
2040 GLcontext *ctx = rmesa->radeon.glCtx;
2041 struct r300_vertex_program *prog = (struct r300_vertex_program *)CURRENT_VERTEX_SHADER(ctx);
2042 int inst_count = 0;
2043 int param_count = 0;
2044
2045 /* FIXME: r300SetupVertexProgramFragment */
2046 R300_STATECHANGE(rmesa, vpp);
2047 param_count =
2048 r300VertexProgUpdateParams(ctx,
2049 (struct r300_vertex_program_cont *)
2050 ctx->VertexProgram._Current,
2051 (float *)&rmesa->hw.vpp.
2052 cmd[R300_VPP_PARAM_0]);
2053 bump_vpu_count(rmesa->hw.vpp.cmd, param_count);
2054 param_count /= 4;
2055
2056 r300SetupVertexProgramFragment(rmesa, R300_PVS_CODE_START, &(prog->program));
2057 inst_count = (prog->program.length / 4) - 1;
2058
2059 r300VapCntl(rmesa, bit_count(prog->key.InputsRead),
2060 bit_count(prog->key.OutputsWritten), prog->num_temporaries);
2061
2062 R300_STATECHANGE(rmesa, pvs);
2063 rmesa->hw.pvs.cmd[R300_PVS_CNTL_1] =
2064 (0 << R300_PVS_FIRST_INST_SHIFT) |
2065 (inst_count << R300_PVS_XYZW_VALID_INST_SHIFT) |
2066 (inst_count << R300_PVS_LAST_INST_SHIFT);
2067 rmesa->hw.pvs.cmd[R300_PVS_CNTL_2] =
2068 (0 << R300_PVS_CONST_BASE_OFFSET_SHIFT) |
2069 (param_count << R300_PVS_MAX_CONST_ADDR_SHIFT);
2070 rmesa->hw.pvs.cmd[R300_PVS_CNTL_3] =
2071 (inst_count << R300_PVS_LAST_VTX_SRC_INST_SHIFT);
2072 }
2073
2074 static void r300SetupVertexProgram(r300ContextPtr rmesa)
2075 {
2076 GLcontext *ctx = rmesa->radeon.glCtx;
2077
2078 /* Reset state, in case we don't use something */
2079 ((drm_r300_cmd_header_t *) rmesa->hw.vpp.cmd)->vpu.count = 0;
2080 ((drm_r300_cmd_header_t *) rmesa->hw.vpi.cmd)->vpu.count = 0;
2081 ((drm_r300_cmd_header_t *) rmesa->hw.vps.cmd)->vpu.count = 0;
2082
2083 /* Not sure why this doesnt work...
2084 0x400 area might have something to do with pixel shaders as it appears right after pfs programming.
2085 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. */
2086 //setup_vertex_shader_fragment(rmesa, 0x406, &unk4);
2087 if (hw_tcl_on && ((struct r300_vertex_program *)CURRENT_VERTEX_SHADER(ctx))->translated) {
2088 r300SetupRealVertexProgram(rmesa);
2089 } else {
2090 /* FIXME: This needs to be replaced by vertex shader generation code. */
2091 r300SetupDefaultVertexProgram(rmesa);
2092 }
2093
2094 }
2095
2096 /**
2097 * Enable/Disable states.
2098 *
2099 * \note Mesa already filters redundant calls to this function.
2100 */
2101 static void r300Enable(GLcontext * ctx, GLenum cap, GLboolean state)
2102 {
2103 if (RADEON_DEBUG & DEBUG_STATE)
2104 fprintf(stderr, "%s( %s = %s )\n", __FUNCTION__,
2105 _mesa_lookup_enum_by_nr(cap),
2106 state ? "GL_TRUE" : "GL_FALSE");
2107
2108 switch (cap) {
2109 case GL_TEXTURE_1D:
2110 case GL_TEXTURE_2D:
2111 case GL_TEXTURE_3D:
2112 /* empty */
2113 break;
2114 case GL_FOG:
2115 r300SetFogState(ctx, state);
2116 break;
2117 case GL_ALPHA_TEST:
2118 r300SetAlphaState(ctx);
2119 break;
2120 case GL_BLEND:
2121 case GL_COLOR_LOGIC_OP:
2122 r300SetBlendState(ctx);
2123 break;
2124 case GL_CLIP_PLANE0:
2125 case GL_CLIP_PLANE1:
2126 case GL_CLIP_PLANE2:
2127 case GL_CLIP_PLANE3:
2128 case GL_CLIP_PLANE4:
2129 case GL_CLIP_PLANE5:
2130 r300SetClipPlaneState(ctx, cap, state);
2131 break;
2132 case GL_DEPTH_TEST:
2133 r300SetDepthState(ctx);
2134 break;
2135 case GL_STENCIL_TEST:
2136 r300SetStencilState(ctx, state);
2137 break;
2138 case GL_CULL_FACE:
2139 r300UpdateCulling(ctx);
2140 break;
2141 case GL_POLYGON_OFFSET_POINT:
2142 case GL_POLYGON_OFFSET_LINE:
2143 case GL_POLYGON_OFFSET_FILL:
2144 r300SetPolygonOffsetState(ctx, state);
2145 break;
2146 default:
2147 radeonEnable(ctx, cap, state);
2148 break;
2149 }
2150 }
2151
2152 /**
2153 * Completely recalculates hardware state based on the Mesa state.
2154 */
2155 static void r300ResetHwState(r300ContextPtr r300)
2156 {
2157 GLcontext *ctx = r300->radeon.glCtx;
2158 int has_tcl = 1;
2159
2160 if (!(r300->radeon.radeonScreen->chip_flags & RADEON_CHIPSET_TCL))
2161 has_tcl = 0;
2162
2163 if (RADEON_DEBUG & DEBUG_STATE)
2164 fprintf(stderr, "%s\n", __FUNCTION__);
2165
2166 r300UpdateWindow(ctx);
2167
2168 r300ColorMask(ctx,
2169 ctx->Color.ColorMask[RCOMP],
2170 ctx->Color.ColorMask[GCOMP],
2171 ctx->Color.ColorMask[BCOMP], ctx->Color.ColorMask[ACOMP]);
2172
2173 r300Enable(ctx, GL_DEPTH_TEST, ctx->Depth.Test);
2174 r300DepthMask(ctx, ctx->Depth.Mask);
2175 r300DepthFunc(ctx, ctx->Depth.Func);
2176
2177 /* stencil */
2178 r300Enable(ctx, GL_STENCIL_TEST, ctx->Stencil.Enabled);
2179 r300StencilMaskSeparate(ctx, 0, ctx->Stencil.WriteMask[0]);
2180 r300StencilFuncSeparate(ctx, 0, ctx->Stencil.Function[0],
2181 ctx->Stencil.Ref[0], ctx->Stencil.ValueMask[0]);
2182 r300StencilOpSeparate(ctx, 0, ctx->Stencil.FailFunc[0],
2183 ctx->Stencil.ZFailFunc[0],
2184 ctx->Stencil.ZPassFunc[0]);
2185
2186 r300UpdateCulling(ctx);
2187
2188 r300UpdateTextureState(ctx);
2189
2190 r300SetBlendState(ctx);
2191
2192 r300AlphaFunc(ctx, ctx->Color.AlphaFunc, ctx->Color.AlphaRef);
2193 r300Enable(ctx, GL_ALPHA_TEST, ctx->Color.AlphaEnabled);
2194
2195 r300->hw.vte.cmd[1] = R300_VPORT_X_SCALE_ENA
2196 | R300_VPORT_X_OFFSET_ENA
2197 | R300_VPORT_Y_SCALE_ENA
2198 | R300_VPORT_Y_OFFSET_ENA
2199 | R300_VPORT_Z_SCALE_ENA
2200 | R300_VPORT_Z_OFFSET_ENA | R300_VTX_W0_FMT;
2201 r300->hw.vte.cmd[2] = 0x00000008;
2202
2203 r300->hw.vap_vf_max_vtx_indx.cmd[1] = 0x00FFFFFF;
2204 r300->hw.vap_vf_max_vtx_indx.cmd[2] = 0x00000000;
2205
2206 #ifdef MESA_LITTLE_ENDIAN
2207 r300->hw.vap_cntl_status.cmd[1] = R300_VC_NO_SWAP;
2208 #else
2209 r300->hw.vap_cntl_status.cmd[1] = R300_VC_32BIT_SWAP;
2210 #endif
2211
2212 /* disable VAP/TCL on non-TCL capable chips */
2213 if (!has_tcl)
2214 r300->hw.vap_cntl_status.cmd[1] |= R300_VAP_TCL_BYPASS;
2215
2216 r300->hw.vap_psc_sgn_norm_cntl.cmd[1] = 0xAAAAAAAA;
2217
2218 /* XXX: Other families? */
2219 if (has_tcl) {
2220 r300->hw.vap_clip_cntl.cmd[1] = R300_PS_UCP_MODE_DIST_COP;
2221
2222 r300->hw.vap_clip.cmd[1] = r300PackFloat32(1.0); /* X */
2223 r300->hw.vap_clip.cmd[2] = r300PackFloat32(1.0); /* X */
2224 r300->hw.vap_clip.cmd[3] = r300PackFloat32(1.0); /* Y */
2225 r300->hw.vap_clip.cmd[4] = r300PackFloat32(1.0); /* Y */
2226
2227 switch (r300->radeon.radeonScreen->chip_family) {
2228 case CHIP_FAMILY_R300:
2229 r300->hw.vap_pvs_vtx_timeout_reg.cmd[1] = R300_2288_R300;
2230 break;
2231 default:
2232 r300->hw.vap_pvs_vtx_timeout_reg.cmd[1] = R300_2288_RV350;
2233 break;
2234 }
2235 }
2236
2237 r300->hw.gb_enable.cmd[1] = R300_GB_POINT_STUFF_ENABLE
2238 | R300_GB_LINE_STUFF_ENABLE
2239 | R300_GB_TRIANGLE_STUFF_ENABLE;
2240
2241 r300->hw.gb_misc.cmd[R300_GB_MISC_MSPOS_0] = 0x66666666;
2242 r300->hw.gb_misc.cmd[R300_GB_MISC_MSPOS_1] = 0x06666666;
2243
2244 r300->hw.gb_misc.cmd[R300_GB_MISC_TILE_CONFIG] =
2245 R300_GB_TILE_ENABLE | R300_GB_TILE_SIZE_16 /*| R300_GB_SUBPIXEL_1_16*/;
2246 switch (r300->radeon.radeonScreen->num_gb_pipes) {
2247 case 1:
2248 default:
2249 r300->hw.gb_misc.cmd[R300_GB_MISC_TILE_CONFIG] |=
2250 R300_GB_TILE_PIPE_COUNT_RV300;
2251 break;
2252 case 2:
2253 r300->hw.gb_misc.cmd[R300_GB_MISC_TILE_CONFIG] |=
2254 R300_GB_TILE_PIPE_COUNT_R300;
2255 break;
2256 case 3:
2257 r300->hw.gb_misc.cmd[R300_GB_MISC_TILE_CONFIG] |=
2258 R300_GB_TILE_PIPE_COUNT_R420_3P;
2259 break;
2260 case 4:
2261 r300->hw.gb_misc.cmd[R300_GB_MISC_TILE_CONFIG] |=
2262 R300_GB_TILE_PIPE_COUNT_R420;
2263 break;
2264 }
2265
2266 /* XXX: set to 0 when fog is disabled? */
2267 r300->hw.gb_misc.cmd[R300_GB_MISC_SELECT] = R300_GB_FOG_SELECT_1_1_W;
2268
2269 /* XXX: Enable anti-aliasing? */
2270 r300->hw.gb_misc.cmd[R300_GB_MISC_AA_CONFIG] = GB_AA_CONFIG_AA_DISABLE;
2271
2272 r300->hw.ga_point_s0.cmd[1] = r300PackFloat32(0.0);
2273 r300->hw.ga_point_s0.cmd[2] = r300PackFloat32(0.0);
2274 r300->hw.ga_point_s0.cmd[3] = r300PackFloat32(1.0);
2275 r300->hw.ga_point_s0.cmd[4] = r300PackFloat32(1.0);
2276
2277 r300->hw.ga_triangle_stipple.cmd[1] = 0x00050005;
2278
2279 r300PointSize(ctx, 1.0);
2280
2281 r300->hw.ga_point_minmax.cmd[1] = 0x18000006;
2282 r300->hw.ga_point_minmax.cmd[2] = 0x00020006;
2283 r300->hw.ga_point_minmax.cmd[3] = r300PackFloat32(1.0 / 192.0);
2284
2285 r300LineWidth(ctx, 1.0);
2286
2287 r300->hw.ga_line_stipple.cmd[1] = 0;
2288 r300->hw.ga_line_stipple.cmd[2] = r300PackFloat32(0.0);
2289 r300->hw.ga_line_stipple.cmd[3] = r300PackFloat32(1.0);
2290
2291 r300ShadeModel(ctx, ctx->Light.ShadeModel);
2292
2293 r300PolygonMode(ctx, GL_FRONT, ctx->Polygon.FrontMode);
2294 r300PolygonMode(ctx, GL_BACK, ctx->Polygon.BackMode);
2295 r300->hw.zbias_cntl.cmd[1] = 0x00000000;
2296
2297 r300PolygonOffset(ctx, ctx->Polygon.OffsetFactor,
2298 ctx->Polygon.OffsetUnits);
2299 r300Enable(ctx, GL_POLYGON_OFFSET_POINT, ctx->Polygon.OffsetPoint);
2300 r300Enable(ctx, GL_POLYGON_OFFSET_LINE, ctx->Polygon.OffsetLine);
2301 r300Enable(ctx, GL_POLYGON_OFFSET_FILL, ctx->Polygon.OffsetFill);
2302
2303 r300->hw.su_depth_scale.cmd[1] = 0x4B7FFFFF;
2304 r300->hw.su_depth_scale.cmd[2] = 0x00000000;
2305
2306 r300->hw.sc_hyperz.cmd[1] = 0x0000001C;
2307 r300->hw.sc_hyperz.cmd[2] = 0x2DA49525;
2308
2309 r300->hw.sc_screendoor.cmd[1] = 0x00FFFFFF;
2310
2311 r300->hw.us_out_fmt.cmd[1] = R500_OUT_FMT_C4_8 |
2312 R500_C0_SEL_B | R500_C1_SEL_G | R500_C2_SEL_R | R500_C3_SEL_A;
2313 r300->hw.us_out_fmt.cmd[2] = R500_OUT_FMT_UNUSED |
2314 R500_C0_SEL_B | R500_C1_SEL_G | R500_C2_SEL_R | R500_C3_SEL_A;
2315 r300->hw.us_out_fmt.cmd[3] = R500_OUT_FMT_UNUSED |
2316 R500_C0_SEL_B | R500_C1_SEL_G | R500_C2_SEL_R | R500_C3_SEL_A;
2317 r300->hw.us_out_fmt.cmd[4] = R500_OUT_FMT_UNUSED |
2318 R500_C0_SEL_B | R500_C1_SEL_G | R500_C2_SEL_R | R500_C3_SEL_A;
2319 r300->hw.us_out_fmt.cmd[5] = R300_W_FMT_W24;
2320
2321 r300Enable(ctx, GL_FOG, ctx->Fog.Enabled);
2322 r300Fogfv(ctx, GL_FOG_MODE, NULL);
2323 r300Fogfv(ctx, GL_FOG_DENSITY, &ctx->Fog.Density);
2324 r300Fogfv(ctx, GL_FOG_START, &ctx->Fog.Start);
2325 r300Fogfv(ctx, GL_FOG_END, &ctx->Fog.End);
2326 r300Fogfv(ctx, GL_FOG_COLOR, ctx->Fog.Color);
2327 r300Fogfv(ctx, GL_FOG_COORDINATE_SOURCE_EXT, NULL);
2328
2329 r300->hw.fg_depth_src.cmd[1] = 0;
2330
2331 r300->hw.rb3d_cctl.cmd[1] = 0;
2332
2333 r300BlendColor(ctx, ctx->Color.BlendColor);
2334
2335 /* Again, r300ClearBuffer uses this */
2336 r300->hw.cb.cmd[R300_CB_OFFSET] =
2337 r300->radeon.state.color.drawOffset +
2338 r300->radeon.radeonScreen->fbLocation;
2339 r300->hw.cb.cmd[R300_CB_PITCH] = r300->radeon.state.color.drawPitch;
2340
2341 if (r300->radeon.radeonScreen->cpp == 4)
2342 r300->hw.cb.cmd[R300_CB_PITCH] |= R300_COLOR_FORMAT_ARGB8888;
2343 else
2344 r300->hw.cb.cmd[R300_CB_PITCH] |= R300_COLOR_FORMAT_RGB565;
2345
2346 if (r300->radeon.sarea->tiling_enabled)
2347 r300->hw.cb.cmd[R300_CB_PITCH] |= R300_COLOR_TILE_ENABLE;
2348
2349 r300->hw.rb3d_dither_ctl.cmd[1] = 0;
2350 r300->hw.rb3d_dither_ctl.cmd[2] = 0;
2351 r300->hw.rb3d_dither_ctl.cmd[3] = 0;
2352 r300->hw.rb3d_dither_ctl.cmd[4] = 0;
2353 r300->hw.rb3d_dither_ctl.cmd[5] = 0;
2354 r300->hw.rb3d_dither_ctl.cmd[6] = 0;
2355 r300->hw.rb3d_dither_ctl.cmd[7] = 0;
2356 r300->hw.rb3d_dither_ctl.cmd[8] = 0;
2357 r300->hw.rb3d_dither_ctl.cmd[9] = 0;
2358
2359 r300->hw.rb3d_aaresolve_ctl.cmd[1] = 0;
2360
2361 r300->hw.rb3d_discard_src_pixel_lte_threshold.cmd[1] = 0x00000000;
2362 r300->hw.rb3d_discard_src_pixel_lte_threshold.cmd[2] = 0xffffffff;
2363
2364 r300->hw.zb.cmd[R300_ZB_OFFSET] =
2365 r300->radeon.radeonScreen->depthOffset +
2366 r300->radeon.radeonScreen->fbLocation;
2367 r300->hw.zb.cmd[R300_ZB_PITCH] = r300->radeon.radeonScreen->depthPitch;
2368
2369 if (r300->radeon.sarea->tiling_enabled) {
2370 /* XXX: Turn off when clearing buffers ? */
2371 r300->hw.zb.cmd[R300_ZB_PITCH] |= R300_DEPTHMACROTILE_ENABLE;
2372
2373 if (ctx->Visual.depthBits == 24)
2374 r300->hw.zb.cmd[R300_ZB_PITCH] |=
2375 R300_DEPTHMICROTILE_TILED;
2376 }
2377
2378 r300->hw.zb_depthclearvalue.cmd[1] = 0;
2379
2380 switch (ctx->Visual.depthBits) {
2381 case 16:
2382 r300->hw.zstencil_format.cmd[1] = R300_DEPTHFORMAT_16BIT_INT_Z;
2383 break;
2384 case 24:
2385 r300->hw.zstencil_format.cmd[1] = R300_DEPTHFORMAT_24BIT_INT_Z_8BIT_STENCIL;
2386 break;
2387 default:
2388 fprintf(stderr, "Error: Unsupported depth %d... exiting\n", ctx->Visual.depthBits);
2389 _mesa_exit(-1);
2390 }
2391
2392 r300->hw.zstencil_format.cmd[2] = R300_ZTOP_DISABLE;
2393 r300->hw.zstencil_format.cmd[3] = 0x00000003;
2394 r300->hw.zstencil_format.cmd[4] = 0x00000000;
2395 r300SetEarlyZState(ctx);
2396
2397 r300->hw.unk4F30.cmd[1] = 0;
2398 r300->hw.unk4F30.cmd[2] = 0;
2399
2400 r300->hw.zb_hiz_offset.cmd[1] = 0;
2401
2402 r300->hw.zb_hiz_pitch.cmd[1] = 0;
2403
2404 r300VapCntl(r300, 0, 0, 0);
2405 if (has_tcl) {
2406 r300->hw.vps.cmd[R300_VPS_ZERO_0] = 0;
2407 r300->hw.vps.cmd[R300_VPS_ZERO_1] = 0;
2408 r300->hw.vps.cmd[R300_VPS_POINTSIZE] = r300PackFloat32(1.0);
2409 r300->hw.vps.cmd[R300_VPS_ZERO_3] = 0;
2410 }
2411
2412 r300->hw.all_dirty = GL_TRUE;
2413 }
2414
2415 void r300UpdateShaders(r300ContextPtr rmesa)
2416 {
2417 GLcontext *ctx;
2418 struct r300_vertex_program *vp;
2419 int i;
2420
2421 ctx = rmesa->radeon.glCtx;
2422
2423 if (rmesa->NewGLState && hw_tcl_on) {
2424 rmesa->NewGLState = 0;
2425
2426 for (i = _TNL_FIRST_MAT; i <= _TNL_LAST_MAT; i++) {
2427 rmesa->temp_attrib[i] =
2428 TNL_CONTEXT(ctx)->vb.AttribPtr[i];
2429 TNL_CONTEXT(ctx)->vb.AttribPtr[i] =
2430 &rmesa->dummy_attrib[i];
2431 }
2432
2433 _tnl_UpdateFixedFunctionProgram(ctx);
2434
2435 for (i = _TNL_FIRST_MAT; i <= _TNL_LAST_MAT; i++) {
2436 TNL_CONTEXT(ctx)->vb.AttribPtr[i] =
2437 rmesa->temp_attrib[i];
2438 }
2439
2440 r300SelectVertexShader(rmesa);
2441 vp = (struct r300_vertex_program *)
2442 CURRENT_VERTEX_SHADER(ctx);
2443 /*if (vp->translated == GL_FALSE)
2444 r300TranslateVertexShader(vp); */
2445 if (vp->translated == GL_FALSE) {
2446 fprintf(stderr, "Failing back to sw-tcl\n");
2447 hw_tcl_on = future_hw_tcl_on = 0;
2448 r300ResetHwState(rmesa);
2449
2450 r300UpdateStateParameters(ctx, _NEW_PROGRAM);
2451 return;
2452 }
2453 }
2454 r300UpdateStateParameters(ctx, _NEW_PROGRAM);
2455 }
2456
2457 static const GLfloat *get_fragmentprogram_constant(GLcontext *ctx,
2458 struct gl_program *program, struct prog_src_register srcreg)
2459 {
2460 static const GLfloat dummy[4] = { 0, 0, 0, 0 };
2461
2462 switch(srcreg.File) {
2463 case PROGRAM_LOCAL_PARAM:
2464 return program->LocalParams[srcreg.Index];
2465 case PROGRAM_ENV_PARAM:
2466 return ctx->FragmentProgram.Parameters[srcreg.Index];
2467 case PROGRAM_STATE_VAR:
2468 case PROGRAM_NAMED_PARAM:
2469 case PROGRAM_CONSTANT:
2470 return program->Parameters->ParameterValues[srcreg.Index];
2471 default:
2472 _mesa_problem(ctx, "get_fragmentprogram_constant: Unknown\n");
2473 return dummy;
2474 }
2475 }
2476
2477
2478 static void r300SetupPixelShader(r300ContextPtr rmesa)
2479 {
2480 GLcontext *ctx = rmesa->radeon.glCtx;
2481 struct r300_fragment_program *fp = (struct r300_fragment_program *)
2482 (char *)ctx->FragmentProgram._Current;
2483 struct r300_fragment_program_code *code;
2484 int i, k;
2485
2486 if (!fp) /* should only happenen once, just after context is created */
2487 return;
2488
2489 r300TranslateFragmentShader(rmesa, fp);
2490 if (!fp->translated) {
2491 fprintf(stderr, "%s: No valid fragment shader, exiting\n",
2492 __FUNCTION__);
2493 return;
2494 }
2495 code = &fp->code;
2496
2497 r300SetupTextures(ctx);
2498
2499 R300_STATECHANGE(rmesa, fpi[0]);
2500 rmesa->hw.fpi[0].cmd[R300_FPI_CMD_0] = cmdpacket0(R300_US_ALU_RGB_INST_0, code->alu_end + 1);
2501 for (i = 0; i <= code->alu_end; i++) {
2502 rmesa->hw.fpi[0].cmd[R300_FPI_INSTR_0 + i] = code->alu.inst[i].inst0;
2503 }
2504
2505 R300_STATECHANGE(rmesa, fpi[1]);
2506 rmesa->hw.fpi[1].cmd[R300_FPI_CMD_0] = cmdpacket0(R300_US_ALU_RGB_ADDR_0, code->alu_end + 1);
2507 for (i = 0; i <= code->alu_end; i++) {
2508 rmesa->hw.fpi[1].cmd[R300_FPI_INSTR_0 + i] = code->alu.inst[i].inst1;
2509 }
2510
2511 R300_STATECHANGE(rmesa, fpi[2]);
2512 rmesa->hw.fpi[2].cmd[R300_FPI_CMD_0] = cmdpacket0(R300_US_ALU_ALPHA_INST_0, code->alu_end + 1);
2513 for (i = 0; i <= code->alu_end; i++) {
2514 rmesa->hw.fpi[2].cmd[R300_FPI_INSTR_0 + i] = code->alu.inst[i].inst2;
2515 }
2516
2517 R300_STATECHANGE(rmesa, fpi[3]);
2518 rmesa->hw.fpi[3].cmd[R300_FPI_CMD_0] = cmdpacket0(R300_US_ALU_ALPHA_ADDR_0, code->alu_end + 1);
2519 for (i = 0; i <= code->alu_end; i++) {
2520 rmesa->hw.fpi[3].cmd[R300_FPI_INSTR_0 + i] = code->alu.inst[i].inst3;
2521 }
2522
2523 R300_STATECHANGE(rmesa, fp);
2524 rmesa->hw.fp.cmd[R300_FP_CNTL0] = code->cur_node | (code->first_node_has_tex << 3);
2525 rmesa->hw.fp.cmd[R300_FP_CNTL1] = code->max_temp_idx;
2526 rmesa->hw.fp.cmd[R300_FP_CNTL2] =
2527 (code->alu_offset << R300_PFS_CNTL_ALU_OFFSET_SHIFT) |
2528 (code->alu_end << R300_PFS_CNTL_ALU_END_SHIFT) |
2529 (code->tex_offset << R300_PFS_CNTL_TEX_OFFSET_SHIFT) |
2530 (code->tex_end << R300_PFS_CNTL_TEX_END_SHIFT);
2531 /* I just want to say, the way these nodes are stored.. weird.. */
2532 for (i = 0, k = (4 - (code->cur_node + 1)); i < 4; i++, k++) {
2533 if (i < (code->cur_node + 1)) {
2534 rmesa->hw.fp.cmd[R300_FP_NODE0 + k] =
2535 (code->node[i].alu_offset << R300_ALU_START_SHIFT) |
2536 (code->node[i].alu_end << R300_ALU_SIZE_SHIFT) |
2537 (code->node[i].tex_offset << R300_TEX_START_SHIFT) |
2538 (code->node[i].tex_end << R300_TEX_SIZE_SHIFT) |
2539 code->node[i].flags;
2540 } else {
2541 rmesa->hw.fp.cmd[R300_FP_NODE0 + (3 - i)] = 0;
2542 }
2543 }
2544
2545 R300_STATECHANGE(rmesa, fpp);
2546 rmesa->hw.fpp.cmd[R300_FPP_CMD_0] = cmdpacket0(R300_PFS_PARAM_0_X, code->const_nr * 4);
2547 for (i = 0; i < code->const_nr; i++) {
2548 const GLfloat *constant = get_fragmentprogram_constant(ctx,
2549 &fp->mesa_program.Base, code->constant[i]);
2550 rmesa->hw.fpp.cmd[R300_FPP_PARAM_0 + 4 * i + 0] = r300PackFloat24(constant[0]);
2551 rmesa->hw.fpp.cmd[R300_FPP_PARAM_0 + 4 * i + 1] = r300PackFloat24(constant[1]);
2552 rmesa->hw.fpp.cmd[R300_FPP_PARAM_0 + 4 * i + 2] = r300PackFloat24(constant[2]);
2553 rmesa->hw.fpp.cmd[R300_FPP_PARAM_0 + 4 * i + 3] = r300PackFloat24(constant[3]);
2554 }
2555 }
2556
2557 #define bump_r500fp_count(ptr, new_count) do{\
2558 drm_r300_cmd_header_t* _p=((drm_r300_cmd_header_t*)(ptr));\
2559 int _nc=(new_count)/6; \
2560 assert(_nc < 256); \
2561 if(_nc>_p->r500fp.count)_p->r500fp.count=_nc;\
2562 } while(0)
2563
2564 #define bump_r500fp_const_count(ptr, new_count) do{\
2565 drm_r300_cmd_header_t* _p=((drm_r300_cmd_header_t*)(ptr));\
2566 int _nc=(new_count)/4; \
2567 assert(_nc < 256); \
2568 if(_nc>_p->r500fp.count)_p->r500fp.count=_nc;\
2569 } while(0)
2570
2571 static void r500SetupPixelShader(r300ContextPtr rmesa)
2572 {
2573 GLcontext *ctx = rmesa->radeon.glCtx;
2574 struct r500_fragment_program *fp = (struct r500_fragment_program *)
2575 (char *)ctx->FragmentProgram._Current;
2576 int i;
2577 struct r500_fragment_program_code *code;
2578
2579 if (!fp) /* should only happenen once, just after context is created */
2580 return;
2581
2582 ((drm_r300_cmd_header_t *) rmesa->hw.r500fp.cmd)->r500fp.count = 0;
2583 ((drm_r300_cmd_header_t *) rmesa->hw.r500fp_const.cmd)->r500fp.count = 0;
2584
2585 r500TranslateFragmentShader(rmesa, fp);
2586 if (!fp->translated) {
2587 fprintf(stderr, "%s: No valid fragment shader, exiting\n",
2588 __FUNCTION__);
2589 return;
2590 }
2591 code = &fp->code;
2592
2593 r300SetupTextures(ctx);
2594
2595 R300_STATECHANGE(rmesa, fp);
2596 rmesa->hw.fp.cmd[R500_FP_PIXSIZE] = code->max_temp_idx;
2597
2598 rmesa->hw.fp.cmd[R500_FP_CODE_ADDR] =
2599 R500_US_CODE_START_ADDR(code->inst_offset) |
2600 R500_US_CODE_END_ADDR(code->inst_end);
2601 rmesa->hw.fp.cmd[R500_FP_CODE_RANGE] =
2602 R500_US_CODE_RANGE_ADDR(code->inst_offset) |
2603 R500_US_CODE_RANGE_SIZE(code->inst_end);
2604 rmesa->hw.fp.cmd[R500_FP_CODE_OFFSET] =
2605 R500_US_CODE_OFFSET_ADDR(0); /* FIXME when we add flow control */
2606
2607 R300_STATECHANGE(rmesa, r500fp);
2608 /* Emit our shader... */
2609 for (i = 0; i < code->inst_end+1; i++) {
2610 rmesa->hw.r500fp.cmd[i*6+1] = code->inst[i].inst0;
2611 rmesa->hw.r500fp.cmd[i*6+2] = code->inst[i].inst1;
2612 rmesa->hw.r500fp.cmd[i*6+3] = code->inst[i].inst2;
2613 rmesa->hw.r500fp.cmd[i*6+4] = code->inst[i].inst3;
2614 rmesa->hw.r500fp.cmd[i*6+5] = code->inst[i].inst4;
2615 rmesa->hw.r500fp.cmd[i*6+6] = code->inst[i].inst5;
2616 }
2617
2618 bump_r500fp_count(rmesa->hw.r500fp.cmd, (code->inst_end + 1) * 6);
2619
2620 R300_STATECHANGE(rmesa, r500fp_const);
2621 for (i = 0; i < code->const_nr; i++) {
2622 const GLfloat *constant = get_fragmentprogram_constant(ctx,
2623 &fp->mesa_program.Base, code->constant[i]);
2624 rmesa->hw.r500fp_const.cmd[R300_FPP_PARAM_0 + 4 * i + 0] = r300PackFloat32(constant[0]);
2625 rmesa->hw.r500fp_const.cmd[R300_FPP_PARAM_0 + 4 * i + 1] = r300PackFloat32(constant[1]);
2626 rmesa->hw.r500fp_const.cmd[R300_FPP_PARAM_0 + 4 * i + 2] = r300PackFloat32(constant[2]);
2627 rmesa->hw.r500fp_const.cmd[R300_FPP_PARAM_0 + 4 * i + 3] = r300PackFloat32(constant[3]);
2628 }
2629 bump_r500fp_const_count(rmesa->hw.r500fp_const.cmd, code->const_nr * 4);
2630
2631 }
2632
2633 void r300UpdateShaderStates(r300ContextPtr rmesa)
2634 {
2635 GLcontext *ctx;
2636 ctx = rmesa->radeon.glCtx;
2637
2638 r300UpdateTextureState(ctx);
2639 r300SetEarlyZState(ctx);
2640
2641 GLuint fgdepthsrc = R300_FG_DEPTH_SRC_SCAN;
2642 if (current_fragment_program_writes_depth(ctx))
2643 fgdepthsrc = R300_FG_DEPTH_SRC_SHADER;
2644 if (fgdepthsrc != rmesa->hw.fg_depth_src.cmd[1]) {
2645 R300_STATECHANGE(rmesa, fg_depth_src);
2646 rmesa->hw.fg_depth_src.cmd[1] = fgdepthsrc;
2647 }
2648
2649 if (rmesa->radeon.radeonScreen->chip_family >= CHIP_FAMILY_RV515)
2650 r500SetupPixelShader(rmesa);
2651 else
2652 r300SetupPixelShader(rmesa);
2653
2654 if (rmesa->radeon.radeonScreen->chip_family >= CHIP_FAMILY_RV515)
2655 r500SetupRSUnit(ctx);
2656 else
2657 r300SetupRSUnit(ctx);
2658
2659 if ((rmesa->radeon.radeonScreen->chip_flags & RADEON_CHIPSET_TCL))
2660 r300SetupVertexProgram(rmesa);
2661
2662 }
2663
2664 /**
2665 * Called by Mesa after an internal state update.
2666 */
2667 static void r300InvalidateState(GLcontext * ctx, GLuint new_state)
2668 {
2669 r300ContextPtr r300 = R300_CONTEXT(ctx);
2670
2671 _swrast_InvalidateState(ctx, new_state);
2672 _swsetup_InvalidateState(ctx, new_state);
2673 _vbo_InvalidateState(ctx, new_state);
2674 _tnl_InvalidateState(ctx, new_state);
2675 _ae_invalidate_state(ctx, new_state);
2676
2677 if (new_state & (_NEW_BUFFERS | _NEW_COLOR | _NEW_PIXEL)) {
2678 r300UpdateDrawBuffer(ctx);
2679 }
2680
2681 r300UpdateStateParameters(ctx, new_state);
2682
2683 r300->NewGLState |= new_state;
2684 }
2685
2686 /**
2687 * Calculate initial hardware state and register state functions.
2688 * Assumes that the command buffer and state atoms have been
2689 * initialized already.
2690 */
2691 void r300InitState(r300ContextPtr r300)
2692 {
2693 GLcontext *ctx = r300->radeon.glCtx;
2694 GLuint depth_fmt;
2695
2696 radeonInitState(&r300->radeon);
2697
2698 switch (ctx->Visual.depthBits) {
2699 case 16:
2700 r300->state.depth.scale = 1.0 / (GLfloat) 0xffff;
2701 depth_fmt = R300_DEPTHFORMAT_16BIT_INT_Z;
2702 break;
2703 case 24:
2704 r300->state.depth.scale = 1.0 / (GLfloat) 0xffffff;
2705 depth_fmt = R300_DEPTHFORMAT_24BIT_INT_Z_8BIT_STENCIL;
2706 break;
2707 default:
2708 fprintf(stderr, "Error: Unsupported depth %d... exiting\n",
2709 ctx->Visual.depthBits);
2710 _mesa_exit(-1);
2711 }
2712
2713 /* Only have hw stencil when depth buffer is 24 bits deep */
2714 r300->state.stencil.hw_stencil = (ctx->Visual.stencilBits > 0 &&
2715 ctx->Visual.depthBits == 24);
2716
2717 memset(&(r300->state.texture), 0, sizeof(r300->state.texture));
2718
2719 r300ResetHwState(r300);
2720 }
2721
2722 static void r300RenderMode(GLcontext * ctx, GLenum mode)
2723 {
2724 r300ContextPtr rmesa = R300_CONTEXT(ctx);
2725 (void)rmesa;
2726 (void)mode;
2727 }
2728
2729 void r300UpdateClipPlanes( GLcontext *ctx )
2730 {
2731 r300ContextPtr rmesa = R300_CONTEXT(ctx);
2732 GLuint p;
2733
2734 for (p = 0; p < ctx->Const.MaxClipPlanes; p++) {
2735 if (ctx->Transform.ClipPlanesEnabled & (1 << p)) {
2736 GLint *ip = (GLint *)ctx->Transform._ClipUserPlane[p];
2737
2738 R300_STATECHANGE( rmesa, vpucp[p] );
2739 rmesa->hw.vpucp[p].cmd[R300_VPUCP_X] = ip[0];
2740 rmesa->hw.vpucp[p].cmd[R300_VPUCP_Y] = ip[1];
2741 rmesa->hw.vpucp[p].cmd[R300_VPUCP_Z] = ip[2];
2742 rmesa->hw.vpucp[p].cmd[R300_VPUCP_W] = ip[3];
2743 }
2744 }
2745 }
2746
2747 /**
2748 * Initialize driver's state callback functions
2749 */
2750 void r300InitStateFuncs(struct dd_function_table *functions)
2751 {
2752 radeonInitStateFuncs(functions);
2753
2754 functions->UpdateState = r300InvalidateState;
2755 functions->AlphaFunc = r300AlphaFunc;
2756 functions->BlendColor = r300BlendColor;
2757 functions->BlendEquationSeparate = r300BlendEquationSeparate;
2758 functions->BlendFuncSeparate = r300BlendFuncSeparate;
2759 functions->Enable = r300Enable;
2760 functions->ColorMask = r300ColorMask;
2761 functions->DepthFunc = r300DepthFunc;
2762 functions->DepthMask = r300DepthMask;
2763 functions->CullFace = r300CullFace;
2764 functions->Fogfv = r300Fogfv;
2765 functions->FrontFace = r300FrontFace;
2766 functions->ShadeModel = r300ShadeModel;
2767
2768 /* ARB_point_parameters */
2769 functions->PointParameterfv = r300PointParameter;
2770
2771 /* Stencil related */
2772 functions->StencilFuncSeparate = r300StencilFuncSeparate;
2773 functions->StencilMaskSeparate = r300StencilMaskSeparate;
2774 functions->StencilOpSeparate = r300StencilOpSeparate;
2775
2776 /* Viewport related */
2777 functions->Viewport = r300Viewport;
2778 functions->DepthRange = r300DepthRange;
2779 functions->PointSize = r300PointSize;
2780 functions->LineWidth = r300LineWidth;
2781
2782 functions->PolygonOffset = r300PolygonOffset;
2783 functions->PolygonMode = r300PolygonMode;
2784
2785 functions->RenderMode = r300RenderMode;
2786
2787 functions->ClipPlane = r300ClipPlane;
2788 }