radeon: Invert front face winding when rendering to FBO.
[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 "main/glheader.h"
39 #include "main/state.h"
40 #include "main/imports.h"
41 #include "main/enums.h"
42 #include "main/macros.h"
43 #include "main/context.h"
44 #include "main/dd.h"
45 #include "main/framebuffer.h"
46 #include "main/simple_list.h"
47 #include "main/api_arrayelt.h"
48 #include "main/texformat.h"
49
50 #include "swrast/swrast.h"
51 #include "swrast_setup/swrast_setup.h"
52 #include "shader/prog_parameter.h"
53 #include "shader/prog_statevars.h"
54 #include "vbo/vbo.h"
55 #include "tnl/tnl.h"
56 #include "tnl/t_vp_build.h"
57
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_tex.h"
64 #include "r300_fragprog_common.h"
65 #include "r300_fragprog.h"
66 #include "r500_fragprog.h"
67 #include "r300_render.h"
68 #include "r300_vertprog.h"
69
70 #include "drirenderbuffer.h"
71
72 static void r300BlendColor(GLcontext * ctx, const GLfloat cf[4])
73 {
74 r300ContextPtr rmesa = R300_CONTEXT(ctx);
75
76 R300_STATECHANGE(rmesa, blend_color);
77
78 if (rmesa->radeon.radeonScreen->chip_family >= CHIP_FAMILY_RV515) {
79 GLuint r = IROUND(cf[0]*1023.0f);
80 GLuint g = IROUND(cf[1]*1023.0f);
81 GLuint b = IROUND(cf[2]*1023.0f);
82 GLuint a = IROUND(cf[3]*1023.0f);
83
84 rmesa->hw.blend_color.cmd[1] = r | (a << 16);
85 rmesa->hw.blend_color.cmd[2] = b | (g << 16);
86 } else {
87 GLubyte color[4];
88 CLAMPED_FLOAT_TO_UBYTE(color[0], cf[0]);
89 CLAMPED_FLOAT_TO_UBYTE(color[1], cf[1]);
90 CLAMPED_FLOAT_TO_UBYTE(color[2], cf[2]);
91 CLAMPED_FLOAT_TO_UBYTE(color[3], cf[3]);
92
93 rmesa->hw.blend_color.cmd[1] = PACK_COLOR_8888(color[3], color[0],
94 color[1], color[2]);
95 }
96 }
97
98 /**
99 * Calculate the hardware blend factor setting. This same function is used
100 * for source and destination of both alpha and RGB.
101 *
102 * \returns
103 * The hardware register value for the specified blend factor. This value
104 * will need to be shifted into the correct position for either source or
105 * destination factor.
106 *
107 * \todo
108 * Since the two cases where source and destination are handled differently
109 * are essentially error cases, they should never happen. Determine if these
110 * cases can be removed.
111 */
112 static int blend_factor(GLenum factor, GLboolean is_src)
113 {
114 switch (factor) {
115 case GL_ZERO:
116 return R300_BLEND_GL_ZERO;
117 break;
118 case GL_ONE:
119 return R300_BLEND_GL_ONE;
120 break;
121 case GL_DST_COLOR:
122 return R300_BLEND_GL_DST_COLOR;
123 break;
124 case GL_ONE_MINUS_DST_COLOR:
125 return R300_BLEND_GL_ONE_MINUS_DST_COLOR;
126 break;
127 case GL_SRC_COLOR:
128 return R300_BLEND_GL_SRC_COLOR;
129 break;
130 case GL_ONE_MINUS_SRC_COLOR:
131 return R300_BLEND_GL_ONE_MINUS_SRC_COLOR;
132 break;
133 case GL_SRC_ALPHA:
134 return R300_BLEND_GL_SRC_ALPHA;
135 break;
136 case GL_ONE_MINUS_SRC_ALPHA:
137 return R300_BLEND_GL_ONE_MINUS_SRC_ALPHA;
138 break;
139 case GL_DST_ALPHA:
140 return R300_BLEND_GL_DST_ALPHA;
141 break;
142 case GL_ONE_MINUS_DST_ALPHA:
143 return R300_BLEND_GL_ONE_MINUS_DST_ALPHA;
144 break;
145 case GL_SRC_ALPHA_SATURATE:
146 return (is_src) ? R300_BLEND_GL_SRC_ALPHA_SATURATE :
147 R300_BLEND_GL_ZERO;
148 break;
149 case GL_CONSTANT_COLOR:
150 return R300_BLEND_GL_CONST_COLOR;
151 break;
152 case GL_ONE_MINUS_CONSTANT_COLOR:
153 return R300_BLEND_GL_ONE_MINUS_CONST_COLOR;
154 break;
155 case GL_CONSTANT_ALPHA:
156 return R300_BLEND_GL_CONST_ALPHA;
157 break;
158 case GL_ONE_MINUS_CONSTANT_ALPHA:
159 return R300_BLEND_GL_ONE_MINUS_CONST_ALPHA;
160 break;
161 default:
162 fprintf(stderr, "unknown blend factor %x\n", factor);
163 return (is_src) ? R300_BLEND_GL_ONE : R300_BLEND_GL_ZERO;
164 break;
165 }
166 }
167
168 /**
169 * Sets both the blend equation and the blend function.
170 * This is done in a single
171 * function because some blend equations (i.e., \c GL_MIN and \c GL_MAX)
172 * change the interpretation of the blend function.
173 * Also, make sure that blend function and blend equation are set to their
174 * default value if color blending is not enabled, since at least blend
175 * equations GL_MIN and GL_FUNC_REVERSE_SUBTRACT will cause wrong results
176 * otherwise for unknown reasons.
177 */
178
179 /* helper function */
180 static void r300SetBlendCntl(r300ContextPtr r300, int func, int eqn,
181 int cbits, int funcA, int eqnA)
182 {
183 GLuint new_ablend, new_cblend;
184
185 #if 0
186 fprintf(stderr,
187 "eqnA=%08x funcA=%08x eqn=%08x func=%08x cbits=%08x\n",
188 eqnA, funcA, eqn, func, cbits);
189 #endif
190 new_ablend = eqnA | funcA;
191 new_cblend = eqn | func;
192
193 /* Some blend factor combinations don't seem to work when the
194 * BLEND_NO_SEPARATE bit is set.
195 *
196 * Especially problematic candidates are the ONE_MINUS_* flags,
197 * but I can't see a real pattern.
198 */
199 #if 0
200 if (new_ablend == new_cblend) {
201 new_cblend |= R300_DISCARD_SRC_PIXELS_SRC_ALPHA_0;
202 }
203 #endif
204 new_cblend |= cbits;
205
206 if ((new_ablend != r300->hw.bld.cmd[R300_BLD_ABLEND]) ||
207 (new_cblend != r300->hw.bld.cmd[R300_BLD_CBLEND])) {
208 R300_STATECHANGE(r300, bld);
209 r300->hw.bld.cmd[R300_BLD_ABLEND] = new_ablend;
210 r300->hw.bld.cmd[R300_BLD_CBLEND] = new_cblend;
211 }
212 }
213
214 static void r300SetBlendState(GLcontext * ctx)
215 {
216 r300ContextPtr r300 = R300_CONTEXT(ctx);
217 int func = (R300_BLEND_GL_ONE << R300_SRC_BLEND_SHIFT) |
218 (R300_BLEND_GL_ZERO << R300_DST_BLEND_SHIFT);
219 int eqn = R300_COMB_FCN_ADD_CLAMP;
220 int funcA = (R300_BLEND_GL_ONE << R300_SRC_BLEND_SHIFT) |
221 (R300_BLEND_GL_ZERO << R300_DST_BLEND_SHIFT);
222 int eqnA = R300_COMB_FCN_ADD_CLAMP;
223
224 if (RGBA_LOGICOP_ENABLED(ctx) || !ctx->Color.BlendEnabled) {
225 r300SetBlendCntl(r300, func, eqn, 0, func, eqn);
226 return;
227 }
228
229 func =
230 (blend_factor(ctx->Color.BlendSrcRGB, GL_TRUE) <<
231 R300_SRC_BLEND_SHIFT) | (blend_factor(ctx->Color.BlendDstRGB,
232 GL_FALSE) <<
233 R300_DST_BLEND_SHIFT);
234
235 switch (ctx->Color.BlendEquationRGB) {
236 case GL_FUNC_ADD:
237 eqn = R300_COMB_FCN_ADD_CLAMP;
238 break;
239
240 case GL_FUNC_SUBTRACT:
241 eqn = R300_COMB_FCN_SUB_CLAMP;
242 break;
243
244 case GL_FUNC_REVERSE_SUBTRACT:
245 eqn = R300_COMB_FCN_RSUB_CLAMP;
246 break;
247
248 case GL_MIN:
249 eqn = R300_COMB_FCN_MIN;
250 func = (R300_BLEND_GL_ONE << R300_SRC_BLEND_SHIFT) |
251 (R300_BLEND_GL_ONE << R300_DST_BLEND_SHIFT);
252 break;
253
254 case GL_MAX:
255 eqn = R300_COMB_FCN_MAX;
256 func = (R300_BLEND_GL_ONE << R300_SRC_BLEND_SHIFT) |
257 (R300_BLEND_GL_ONE << R300_DST_BLEND_SHIFT);
258 break;
259
260 default:
261 fprintf(stderr,
262 "[%s:%u] Invalid RGB blend equation (0x%04x).\n",
263 __FUNCTION__, __LINE__, ctx->Color.BlendEquationRGB);
264 return;
265 }
266
267 funcA =
268 (blend_factor(ctx->Color.BlendSrcA, GL_TRUE) <<
269 R300_SRC_BLEND_SHIFT) | (blend_factor(ctx->Color.BlendDstA,
270 GL_FALSE) <<
271 R300_DST_BLEND_SHIFT);
272
273 switch (ctx->Color.BlendEquationA) {
274 case GL_FUNC_ADD:
275 eqnA = R300_COMB_FCN_ADD_CLAMP;
276 break;
277
278 case GL_FUNC_SUBTRACT:
279 eqnA = R300_COMB_FCN_SUB_CLAMP;
280 break;
281
282 case GL_FUNC_REVERSE_SUBTRACT:
283 eqnA = R300_COMB_FCN_RSUB_CLAMP;
284 break;
285
286 case GL_MIN:
287 eqnA = R300_COMB_FCN_MIN;
288 funcA = (R300_BLEND_GL_ONE << R300_SRC_BLEND_SHIFT) |
289 (R300_BLEND_GL_ONE << R300_DST_BLEND_SHIFT);
290 break;
291
292 case GL_MAX:
293 eqnA = R300_COMB_FCN_MAX;
294 funcA = (R300_BLEND_GL_ONE << R300_SRC_BLEND_SHIFT) |
295 (R300_BLEND_GL_ONE << R300_DST_BLEND_SHIFT);
296 break;
297
298 default:
299 fprintf(stderr,
300 "[%s:%u] Invalid A blend equation (0x%04x).\n",
301 __FUNCTION__, __LINE__, ctx->Color.BlendEquationA);
302 return;
303 }
304
305 r300SetBlendCntl(r300,
306 func, eqn,
307 (R300_SEPARATE_ALPHA_ENABLE |
308 R300_READ_ENABLE |
309 R300_ALPHA_BLEND_ENABLE), funcA, eqnA);
310 }
311
312 static void r300BlendEquationSeparate(GLcontext * ctx,
313 GLenum modeRGB, GLenum modeA)
314 {
315 r300SetBlendState(ctx);
316 }
317
318 static void r300BlendFuncSeparate(GLcontext * ctx,
319 GLenum sfactorRGB, GLenum dfactorRGB,
320 GLenum sfactorA, GLenum dfactorA)
321 {
322 r300SetBlendState(ctx);
323 }
324
325 /**
326 * Translate LogicOp enums into hardware representation.
327 * Both use a very logical bit-wise layout, but unfortunately the order
328 * of bits is reversed.
329 */
330 static GLuint translate_logicop(GLenum logicop)
331 {
332 GLuint bits = logicop - GL_CLEAR;
333 bits = ((bits & 1) << 3) | ((bits & 2) << 1) | ((bits & 4) >> 1) | ((bits & 8) >> 3);
334 return bits << R300_RB3D_ROPCNTL_ROP_SHIFT;
335 }
336
337 /**
338 * Used internally to update the r300->hw hardware state to match the
339 * current OpenGL state.
340 */
341 static void r300SetLogicOpState(GLcontext *ctx)
342 {
343 r300ContextPtr r300 = R300_CONTEXT(ctx);
344 R300_STATECHANGE(r300, rop);
345 if (RGBA_LOGICOP_ENABLED(ctx)) {
346 r300->hw.rop.cmd[1] = R300_RB3D_ROPCNTL_ROP_ENABLE |
347 translate_logicop(ctx->Color.LogicOp);
348 } else {
349 r300->hw.rop.cmd[1] = 0;
350 }
351 }
352
353 /**
354 * Called by Mesa when an application program changes the LogicOp state
355 * via glLogicOp.
356 */
357 static void r300LogicOpcode(GLcontext *ctx, GLenum logicop)
358 {
359 if (RGBA_LOGICOP_ENABLED(ctx))
360 r300SetLogicOpState(ctx);
361 }
362
363 static void r300ClipPlane( GLcontext *ctx, GLenum plane, const GLfloat *eq )
364 {
365 r300ContextPtr rmesa = R300_CONTEXT(ctx);
366 GLint p;
367 GLint *ip;
368
369 /* no VAP UCP on non-TCL chipsets */
370 if (!rmesa->options.hw_tcl_enabled)
371 return;
372
373 p = (GLint) plane - (GLint) GL_CLIP_PLANE0;
374 ip = (GLint *)ctx->Transform._ClipUserPlane[p];
375
376 R300_STATECHANGE( rmesa, vpucp[p] );
377 rmesa->hw.vpucp[p].cmd[R300_VPUCP_X] = ip[0];
378 rmesa->hw.vpucp[p].cmd[R300_VPUCP_Y] = ip[1];
379 rmesa->hw.vpucp[p].cmd[R300_VPUCP_Z] = ip[2];
380 rmesa->hw.vpucp[p].cmd[R300_VPUCP_W] = ip[3];
381 }
382
383 static void r300SetClipPlaneState(GLcontext * ctx, GLenum cap, GLboolean state)
384 {
385 r300ContextPtr r300 = R300_CONTEXT(ctx);
386 GLuint p;
387
388 /* no VAP UCP on non-TCL chipsets */
389 if (!r300->options.hw_tcl_enabled)
390 return;
391
392 p = cap - GL_CLIP_PLANE0;
393 R300_STATECHANGE(r300, vap_clip_cntl);
394 if (state) {
395 r300->hw.vap_clip_cntl.cmd[1] |= (R300_VAP_UCP_ENABLE_0 << p);
396 r300ClipPlane(ctx, cap, NULL);
397 } else {
398 r300->hw.vap_clip_cntl.cmd[1] &= ~(R300_VAP_UCP_ENABLE_0 << p);
399 }
400 }
401
402 /**
403 * Update our tracked culling state based on Mesa's state.
404 */
405 static void r300UpdateCulling(GLcontext * ctx)
406 {
407 r300ContextPtr r300 = R300_CONTEXT(ctx);
408 uint32_t val = 0;
409
410 if (ctx->Polygon.CullFlag) {
411 switch (ctx->Polygon.CullFaceMode) {
412 case GL_FRONT:
413 val = R300_CULL_FRONT;
414 break;
415 case GL_BACK:
416 val = R300_CULL_BACK;
417 break;
418 case GL_FRONT_AND_BACK:
419 val = R300_CULL_FRONT | R300_CULL_BACK;
420 break;
421 default:
422 break;
423 }
424 }
425
426 switch (ctx->Polygon.FrontFace) {
427 case GL_CW:
428 val |= R300_FRONT_FACE_CW;
429 break;
430 case GL_CCW:
431 val |= R300_FRONT_FACE_CCW;
432 break;
433 default:
434 break;
435 }
436
437 /* Winding is inverted when rendering to FBO */
438 if (ctx->DrawBuffer && ctx->DrawBuffer->Name)
439 val ^= R300_FRONT_FACE_CW;
440
441 R300_STATECHANGE(r300, cul);
442 r300->hw.cul.cmd[R300_CUL_CULL] = val;
443 }
444
445 static void r300SetPolygonOffsetState(GLcontext * ctx, GLboolean state)
446 {
447 r300ContextPtr r300 = R300_CONTEXT(ctx);
448
449 R300_STATECHANGE(r300, occlusion_cntl);
450 if (state) {
451 r300->hw.occlusion_cntl.cmd[1] |= (3 << 0);
452 } else {
453 r300->hw.occlusion_cntl.cmd[1] &= ~(3 << 0);
454 }
455 }
456
457 static GLboolean current_fragment_program_writes_depth(GLcontext* ctx)
458 {
459 r300ContextPtr r300 = R300_CONTEXT(ctx);
460
461 return ctx->FragmentProgram._Current && r300->selected_fp->writes_depth;
462 }
463
464 static void r300SetEarlyZState(GLcontext * ctx)
465 {
466 r300ContextPtr r300 = R300_CONTEXT(ctx);
467 GLuint topZ = R300_ZTOP_ENABLE;
468 GLuint w_fmt, fgdepthsrc;
469
470 if (ctx->Color.AlphaEnabled && ctx->Color.AlphaFunc != GL_ALWAYS)
471 topZ = R300_ZTOP_DISABLE;
472 else if (current_fragment_program_writes_depth(ctx))
473 topZ = R300_ZTOP_DISABLE;
474 else if (ctx->FragmentProgram._Current && ctx->FragmentProgram._Current->UsesKill)
475 topZ = R300_ZTOP_DISABLE;
476
477 if (topZ != r300->hw.zstencil_format.cmd[2]) {
478 /* Note: This completely reemits the stencil format.
479 * I have not tested whether this is strictly necessary,
480 * or if emitting a write to ZB_ZTOP is enough.
481 */
482 R300_STATECHANGE(r300, zstencil_format);
483 r300->hw.zstencil_format.cmd[2] = topZ;
484 }
485
486 /* w_fmt value is set to get best performance
487 * see p.130 R5xx 3D acceleration guide v1.3 */
488 if (current_fragment_program_writes_depth(ctx)) {
489 fgdepthsrc = R300_FG_DEPTH_SRC_SHADER;
490 w_fmt = R300_W_FMT_W24 | R300_W_SRC_US;
491 } else {
492 fgdepthsrc = R300_FG_DEPTH_SRC_SCAN;
493 w_fmt = R300_W_FMT_W0 | R300_W_SRC_US;
494 }
495
496 if (w_fmt != r300->hw.us_out_fmt.cmd[5]) {
497 R300_STATECHANGE(r300, us_out_fmt);
498 r300->hw.us_out_fmt.cmd[5] = w_fmt;
499 }
500
501 if (fgdepthsrc != r300->hw.fg_depth_src.cmd[1]) {
502 R300_STATECHANGE(r300, fg_depth_src);
503 r300->hw.fg_depth_src.cmd[1] = fgdepthsrc;
504 }
505 }
506
507 static void r300SetAlphaState(GLcontext * ctx)
508 {
509 r300ContextPtr r300 = R300_CONTEXT(ctx);
510 GLubyte refByte;
511 uint32_t pp_misc = 0x0;
512 GLboolean really_enabled = ctx->Color.AlphaEnabled;
513
514 CLAMPED_FLOAT_TO_UBYTE(refByte, ctx->Color.AlphaRef);
515
516 switch (ctx->Color.AlphaFunc) {
517 case GL_NEVER:
518 pp_misc |= R300_FG_ALPHA_FUNC_NEVER;
519 break;
520 case GL_LESS:
521 pp_misc |= R300_FG_ALPHA_FUNC_LESS;
522 break;
523 case GL_EQUAL:
524 pp_misc |= R300_FG_ALPHA_FUNC_EQUAL;
525 break;
526 case GL_LEQUAL:
527 pp_misc |= R300_FG_ALPHA_FUNC_LE;
528 break;
529 case GL_GREATER:
530 pp_misc |= R300_FG_ALPHA_FUNC_GREATER;
531 break;
532 case GL_NOTEQUAL:
533 pp_misc |= R300_FG_ALPHA_FUNC_NOTEQUAL;
534 break;
535 case GL_GEQUAL:
536 pp_misc |= R300_FG_ALPHA_FUNC_GE;
537 break;
538 case GL_ALWAYS:
539 /*pp_misc |= FG_ALPHA_FUNC_ALWAYS; */
540 really_enabled = GL_FALSE;
541 break;
542 }
543
544 if (really_enabled) {
545 pp_misc |= R300_FG_ALPHA_FUNC_ENABLE;
546 pp_misc |= R500_FG_ALPHA_FUNC_8BIT;
547 pp_misc |= (refByte & R300_FG_ALPHA_FUNC_VAL_MASK);
548 } else {
549 pp_misc = 0x0;
550 }
551
552 R300_STATECHANGE(r300, at);
553 r300->hw.at.cmd[R300_AT_ALPHA_TEST] = pp_misc;
554 r300->hw.at.cmd[R300_AT_UNKNOWN] = 0;
555 }
556
557 static void r300AlphaFunc(GLcontext * ctx, GLenum func, GLfloat ref)
558 {
559 (void)func;
560 (void)ref;
561 r300SetAlphaState(ctx);
562 }
563
564 static int translate_func(int func)
565 {
566 switch (func) {
567 case GL_NEVER:
568 return R300_ZS_NEVER;
569 case GL_LESS:
570 return R300_ZS_LESS;
571 case GL_EQUAL:
572 return R300_ZS_EQUAL;
573 case GL_LEQUAL:
574 return R300_ZS_LEQUAL;
575 case GL_GREATER:
576 return R300_ZS_GREATER;
577 case GL_NOTEQUAL:
578 return R300_ZS_NOTEQUAL;
579 case GL_GEQUAL:
580 return R300_ZS_GEQUAL;
581 case GL_ALWAYS:
582 return R300_ZS_ALWAYS;
583 }
584 return 0;
585 }
586
587 static void r300SetDepthState(GLcontext * ctx)
588 {
589 r300ContextPtr r300 = R300_CONTEXT(ctx);
590
591 R300_STATECHANGE(r300, zs);
592 r300->hw.zs.cmd[R300_ZS_CNTL_0] &= R300_STENCIL_ENABLE|R300_STENCIL_FRONT_BACK;
593 r300->hw.zs.cmd[R300_ZS_CNTL_1] &= ~(R300_ZS_MASK << R300_Z_FUNC_SHIFT);
594
595 if (ctx->Depth.Test) {
596 r300->hw.zs.cmd[R300_ZS_CNTL_0] |= R300_Z_ENABLE;
597 if (ctx->Depth.Mask)
598 r300->hw.zs.cmd[R300_ZS_CNTL_0] |= R300_Z_WRITE_ENABLE;
599 r300->hw.zs.cmd[R300_ZS_CNTL_1] |=
600 translate_func(ctx->Depth.Func) << R300_Z_FUNC_SHIFT;
601 }
602 }
603
604 static void r300CatchStencilFallback(GLcontext *ctx)
605 {
606 const unsigned back = ctx->Stencil._BackFace;
607
608 if (ctx->Stencil._Enabled && (ctx->Stencil.Ref[0] != ctx->Stencil.Ref[back]
609 || ctx->Stencil.ValueMask[0] != ctx->Stencil.ValueMask[back]
610 || ctx->Stencil.WriteMask[0] != ctx->Stencil.WriteMask[back])) {
611 r300SwitchFallback(ctx, R300_FALLBACK_STENCIL_TWOSIDE, GL_TRUE);
612 } else {
613 r300SwitchFallback(ctx, R300_FALLBACK_STENCIL_TWOSIDE, GL_FALSE);
614 }
615 }
616
617 static void r300SetStencilState(GLcontext * ctx, GLboolean state)
618 {
619 r300ContextPtr r300 = R300_CONTEXT(ctx);
620 GLboolean hw_stencil = GL_FALSE;
621
622 r300CatchStencilFallback(ctx);
623
624 if (ctx->DrawBuffer) {
625 struct radeon_renderbuffer *rrbStencil
626 = radeon_get_renderbuffer(ctx->DrawBuffer, BUFFER_STENCIL);
627 hw_stencil = (rrbStencil && rrbStencil->bo);
628 }
629
630 if (hw_stencil) {
631 R300_STATECHANGE(r300, zs);
632 if (state) {
633 r300->hw.zs.cmd[R300_ZS_CNTL_0] |=
634 R300_STENCIL_ENABLE;
635 } else {
636 r300->hw.zs.cmd[R300_ZS_CNTL_0] &=
637 ~R300_STENCIL_ENABLE;
638 }
639 }
640 }
641
642 static void r300UpdatePolygonMode(GLcontext * ctx)
643 {
644 r300ContextPtr r300 = R300_CONTEXT(ctx);
645 uint32_t hw_mode = R300_GA_POLY_MODE_DISABLE;
646
647 /* Only do something if a polygon mode is wanted, default is GL_FILL */
648 if (ctx->Polygon.FrontMode != GL_FILL ||
649 ctx->Polygon.BackMode != GL_FILL) {
650 GLenum f, b;
651
652 /* Handle GL_CW (clock wise and GL_CCW (counter clock wise)
653 * correctly by selecting the correct front and back face
654 */
655 if (ctx->Polygon.FrontFace == GL_CCW) {
656 f = ctx->Polygon.FrontMode;
657 b = ctx->Polygon.BackMode;
658 } else {
659 f = ctx->Polygon.BackMode;
660 b = ctx->Polygon.FrontMode;
661 }
662
663 /* Enable polygon mode */
664 hw_mode |= R300_GA_POLY_MODE_DUAL;
665
666 switch (f) {
667 case GL_LINE:
668 hw_mode |= R300_GA_POLY_MODE_FRONT_PTYPE_LINE;
669 break;
670 case GL_POINT:
671 hw_mode |= R300_GA_POLY_MODE_FRONT_PTYPE_POINT;
672 break;
673 case GL_FILL:
674 hw_mode |= R300_GA_POLY_MODE_FRONT_PTYPE_TRI;
675 break;
676 }
677
678 switch (b) {
679 case GL_LINE:
680 hw_mode |= R300_GA_POLY_MODE_BACK_PTYPE_LINE;
681 break;
682 case GL_POINT:
683 hw_mode |= R300_GA_POLY_MODE_BACK_PTYPE_POINT;
684 break;
685 case GL_FILL:
686 hw_mode |= R300_GA_POLY_MODE_BACK_PTYPE_TRI;
687 break;
688 }
689 }
690
691 if (r300->hw.polygon_mode.cmd[1] != hw_mode) {
692 R300_STATECHANGE(r300, polygon_mode);
693 r300->hw.polygon_mode.cmd[1] = hw_mode;
694 }
695
696 r300->hw.polygon_mode.cmd[2] = 0x00000001;
697 r300->hw.polygon_mode.cmd[3] = 0x00000000;
698 }
699
700 /**
701 * Change the culling mode.
702 *
703 * \note Mesa already filters redundant calls to this function.
704 */
705 static void r300CullFace(GLcontext * ctx, GLenum mode)
706 {
707 (void)mode;
708
709 r300UpdateCulling(ctx);
710 }
711
712 /**
713 * Change the polygon orientation.
714 *
715 * \note Mesa already filters redundant calls to this function.
716 */
717 static void r300FrontFace(GLcontext * ctx, GLenum mode)
718 {
719 (void)mode;
720
721 r300UpdateCulling(ctx);
722 r300UpdatePolygonMode(ctx);
723 }
724
725 /**
726 * Change the depth testing function.
727 *
728 * \note Mesa already filters redundant calls to this function.
729 */
730 static void r300DepthFunc(GLcontext * ctx, GLenum func)
731 {
732 (void)func;
733 r300SetDepthState(ctx);
734 }
735
736 /**
737 * Enable/Disable depth writing.
738 *
739 * \note Mesa already filters redundant calls to this function.
740 */
741 static void r300DepthMask(GLcontext * ctx, GLboolean mask)
742 {
743 (void)mask;
744 r300SetDepthState(ctx);
745 }
746
747 /**
748 * Handle glColorMask()
749 */
750 static void r300ColorMask(GLcontext * ctx,
751 GLboolean r, GLboolean g, GLboolean b, GLboolean a)
752 {
753 r300ContextPtr r300 = R300_CONTEXT(ctx);
754 int mask = (r ? RB3D_COLOR_CHANNEL_MASK_RED_MASK0 : 0) |
755 (g ? RB3D_COLOR_CHANNEL_MASK_GREEN_MASK0 : 0) |
756 (b ? RB3D_COLOR_CHANNEL_MASK_BLUE_MASK0 : 0) |
757 (a ? RB3D_COLOR_CHANNEL_MASK_ALPHA_MASK0 : 0);
758
759 if (mask != r300->hw.cmk.cmd[R300_CMK_COLORMASK]) {
760 R300_STATECHANGE(r300, cmk);
761 r300->hw.cmk.cmd[R300_CMK_COLORMASK] = mask;
762 }
763 }
764
765 /* =============================================================
766 * Point state
767 */
768 static void r300PointSize(GLcontext * ctx, GLfloat size)
769 {
770 r300ContextPtr r300 = R300_CONTEXT(ctx);
771
772 /* We need to clamp to user defined range here, because
773 * the HW clamping happens only for per vertex point size. */
774 size = CLAMP(size, ctx->Point.MinSize, ctx->Point.MaxSize);
775
776 /* same size limits for AA, non-AA points */
777 size = CLAMP(size, ctx->Const.MinPointSize, ctx->Const.MaxPointSize);
778
779 R300_STATECHANGE(r300, ps);
780 r300->hw.ps.cmd[R300_PS_POINTSIZE] =
781 ((int)(size * 6) << R300_POINTSIZE_X_SHIFT) |
782 ((int)(size * 6) << R300_POINTSIZE_Y_SHIFT);
783 }
784
785 static void r300PointParameter(GLcontext * ctx, GLenum pname, const GLfloat * param)
786 {
787 r300ContextPtr r300 = R300_CONTEXT(ctx);
788
789 switch (pname) {
790 case GL_POINT_SIZE_MIN:
791 R300_STATECHANGE(r300, ga_point_minmax);
792 r300->hw.ga_point_minmax.cmd[1] &= ~R300_GA_POINT_MINMAX_MIN_MASK;
793 r300->hw.ga_point_minmax.cmd[1] |= (GLuint)(ctx->Point.MinSize * 6.0);
794 break;
795 case GL_POINT_SIZE_MAX:
796 R300_STATECHANGE(r300, ga_point_minmax);
797 r300->hw.ga_point_minmax.cmd[1] &= ~R300_GA_POINT_MINMAX_MAX_MASK;
798 r300->hw.ga_point_minmax.cmd[1] |= (GLuint)(ctx->Point.MaxSize * 6.0)
799 << R300_GA_POINT_MINMAX_MAX_SHIFT;
800 break;
801 case GL_POINT_DISTANCE_ATTENUATION:
802 break;
803 case GL_POINT_FADE_THRESHOLD_SIZE:
804 break;
805 default:
806 break;
807 }
808 }
809
810 /* =============================================================
811 * Line state
812 */
813 static void r300LineWidth(GLcontext * ctx, GLfloat widthf)
814 {
815 r300ContextPtr r300 = R300_CONTEXT(ctx);
816
817 widthf = CLAMP(widthf,
818 ctx->Const.MinPointSize,
819 ctx->Const.MaxPointSize);
820 R300_STATECHANGE(r300, lcntl);
821 r300->hw.lcntl.cmd[1] =
822 R300_LINE_CNT_HO | R300_LINE_CNT_VE | (int)(widthf * 6.0);
823 }
824
825 static void r300PolygonMode(GLcontext * ctx, GLenum face, GLenum mode)
826 {
827 (void)face;
828 (void)mode;
829
830 r300UpdatePolygonMode(ctx);
831 }
832
833 /* =============================================================
834 * Stencil
835 */
836
837 static int translate_stencil_op(int op)
838 {
839 switch (op) {
840 case GL_KEEP:
841 return R300_ZS_KEEP;
842 case GL_ZERO:
843 return R300_ZS_ZERO;
844 case GL_REPLACE:
845 return R300_ZS_REPLACE;
846 case GL_INCR:
847 return R300_ZS_INCR;
848 case GL_DECR:
849 return R300_ZS_DECR;
850 case GL_INCR_WRAP_EXT:
851 return R300_ZS_INCR_WRAP;
852 case GL_DECR_WRAP_EXT:
853 return R300_ZS_DECR_WRAP;
854 case GL_INVERT:
855 return R300_ZS_INVERT;
856 default:
857 WARN_ONCE("Do not know how to translate stencil op");
858 return R300_ZS_KEEP;
859 }
860 return 0;
861 }
862
863 static void r300ShadeModel(GLcontext * ctx, GLenum mode)
864 {
865 r300ContextPtr rmesa = R300_CONTEXT(ctx);
866
867 R300_STATECHANGE(rmesa, shade);
868 rmesa->hw.shade.cmd[1] = 0x00000002;
869 R300_STATECHANGE(rmesa, shade2);
870 switch (mode) {
871 case GL_FLAT:
872 rmesa->hw.shade2.cmd[1] = R300_RE_SHADE_MODEL_FLAT;
873 break;
874 case GL_SMOOTH:
875 rmesa->hw.shade2.cmd[1] = R300_RE_SHADE_MODEL_SMOOTH;
876 break;
877 default:
878 return;
879 }
880 rmesa->hw.shade2.cmd[2] = 0x00000000;
881 rmesa->hw.shade2.cmd[3] = 0x00000000;
882 }
883
884 static void r300StencilFuncSeparate(GLcontext * ctx, GLenum face,
885 GLenum func, GLint ref, GLuint mask)
886 {
887 r300ContextPtr rmesa = R300_CONTEXT(ctx);
888 GLuint refmask;
889 GLuint flag;
890 const unsigned back = ctx->Stencil._BackFace;
891
892 r300CatchStencilFallback(ctx);
893
894 refmask = ((ctx->Stencil.Ref[0] & 0xff) << R300_STENCILREF_SHIFT)
895 | ((ctx->Stencil.ValueMask[0] & 0xff) << R300_STENCILMASK_SHIFT);
896
897 R300_STATECHANGE(rmesa, zs);
898 rmesa->hw.zs.cmd[R300_ZS_CNTL_0] |= R300_STENCIL_FRONT_BACK;
899 rmesa->hw.zs.cmd[R300_ZS_CNTL_1] &= ~((R300_ZS_MASK <<
900 R300_S_FRONT_FUNC_SHIFT)
901 | (R300_ZS_MASK <<
902 R300_S_BACK_FUNC_SHIFT));
903
904 rmesa->hw.zs.cmd[R300_ZS_CNTL_2] &=
905 ~((R300_STENCILREF_MASK << R300_STENCILREF_SHIFT) |
906 (R300_STENCILREF_MASK << R300_STENCILMASK_SHIFT));
907
908 flag = translate_func(ctx->Stencil.Function[0]);
909 rmesa->hw.zs.cmd[R300_ZS_CNTL_1] |=
910 (flag << R300_S_FRONT_FUNC_SHIFT);
911
912 flag = translate_func(ctx->Stencil.Function[back]);
913
914 rmesa->hw.zs.cmd[R300_ZS_CNTL_1] |=
915 (flag << R300_S_BACK_FUNC_SHIFT);
916 rmesa->hw.zs.cmd[R300_ZS_CNTL_2] |= refmask;
917 }
918
919 static void r300StencilMaskSeparate(GLcontext * ctx, GLenum face, GLuint mask)
920 {
921 r300ContextPtr rmesa = R300_CONTEXT(ctx);
922
923 r300CatchStencilFallback(ctx);
924
925 R300_STATECHANGE(rmesa, zs);
926 rmesa->hw.zs.cmd[R300_ZS_CNTL_2] &=
927 ~(R300_STENCILREF_MASK <<
928 R300_STENCILWRITEMASK_SHIFT);
929 rmesa->hw.zs.cmd[R300_ZS_CNTL_2] |=
930 (ctx->Stencil.
931 WriteMask[0] & R300_STENCILREF_MASK) <<
932 R300_STENCILWRITEMASK_SHIFT;
933 }
934
935 static void r300StencilOpSeparate(GLcontext * ctx, GLenum face,
936 GLenum fail, GLenum zfail, GLenum zpass)
937 {
938 r300ContextPtr rmesa = R300_CONTEXT(ctx);
939 const unsigned back = ctx->Stencil._BackFace;
940
941 r300CatchStencilFallback(ctx);
942
943 R300_STATECHANGE(rmesa, zs);
944 /* It is easier to mask what's left.. */
945 rmesa->hw.zs.cmd[R300_ZS_CNTL_1] &=
946 (R300_ZS_MASK << R300_Z_FUNC_SHIFT) |
947 (R300_ZS_MASK << R300_S_FRONT_FUNC_SHIFT) |
948 (R300_ZS_MASK << R300_S_BACK_FUNC_SHIFT);
949
950 rmesa->hw.zs.cmd[R300_ZS_CNTL_1] |=
951 (translate_stencil_op(ctx->Stencil.FailFunc[0]) <<
952 R300_S_FRONT_SFAIL_OP_SHIFT)
953 | (translate_stencil_op(ctx->Stencil.ZFailFunc[0]) <<
954 R300_S_FRONT_ZFAIL_OP_SHIFT)
955 | (translate_stencil_op(ctx->Stencil.ZPassFunc[0]) <<
956 R300_S_FRONT_ZPASS_OP_SHIFT);
957
958 rmesa->hw.zs.cmd[R300_ZS_CNTL_1] |=
959 (translate_stencil_op(ctx->Stencil.FailFunc[back]) <<
960 R300_S_BACK_SFAIL_OP_SHIFT)
961 | (translate_stencil_op(ctx->Stencil.ZFailFunc[back]) <<
962 R300_S_BACK_ZFAIL_OP_SHIFT)
963 | (translate_stencil_op(ctx->Stencil.ZPassFunc[back]) <<
964 R300_S_BACK_ZPASS_OP_SHIFT);
965 }
966
967 /* =============================================================
968 * Window position and viewport transformation
969 */
970
971 static void r300UpdateWindow(GLcontext * ctx)
972 {
973 r300ContextPtr rmesa = R300_CONTEXT(ctx);
974 __DRIdrawablePrivate *dPriv = radeon_get_drawable(&rmesa->radeon);
975 GLfloat xoffset = dPriv ? (GLfloat) dPriv->x : 0;
976 GLfloat yoffset = dPriv ? (GLfloat) dPriv->y + dPriv->h : 0;
977 const GLfloat *v = ctx->Viewport._WindowMap.m;
978 const GLfloat depthScale = 1.0F / ctx->DrawBuffer->_DepthMaxF;
979 const GLboolean render_to_fbo = (ctx->DrawBuffer->Name != 0);
980 GLfloat y_scale, y_bias;
981
982 if (render_to_fbo) {
983 y_scale = 1.0;
984 y_bias = 0;
985 } else {
986 y_scale = -1.0;
987 y_bias = yoffset;
988 }
989
990 GLfloat sx = v[MAT_SX];
991 GLfloat tx = v[MAT_TX] + xoffset;
992 GLfloat sy = v[MAT_SY] * y_scale;
993 GLfloat ty = (v[MAT_TY] * y_scale) + y_bias;
994 GLfloat sz = v[MAT_SZ] * depthScale;
995 GLfloat tz = v[MAT_TZ] * depthScale;
996
997 R300_STATECHANGE(rmesa, vpt);
998
999 rmesa->hw.vpt.cmd[R300_VPT_XSCALE] = r300PackFloat32(sx);
1000 rmesa->hw.vpt.cmd[R300_VPT_XOFFSET] = r300PackFloat32(tx);
1001 rmesa->hw.vpt.cmd[R300_VPT_YSCALE] = r300PackFloat32(sy);
1002 rmesa->hw.vpt.cmd[R300_VPT_YOFFSET] = r300PackFloat32(ty);
1003 rmesa->hw.vpt.cmd[R300_VPT_ZSCALE] = r300PackFloat32(sz);
1004 rmesa->hw.vpt.cmd[R300_VPT_ZOFFSET] = r300PackFloat32(tz);
1005 }
1006
1007 static void r300Viewport(GLcontext * ctx, GLint x, GLint y,
1008 GLsizei width, GLsizei height)
1009 {
1010 /* Don't pipeline viewport changes, conflict with window offset
1011 * setting below. Could apply deltas to rescue pipelined viewport
1012 * values, or keep the originals hanging around.
1013 */
1014 r300UpdateWindow(ctx);
1015
1016 radeon_viewport(ctx, x, y, width, height);
1017 }
1018
1019 static void r300DepthRange(GLcontext * ctx, GLclampd nearval, GLclampd farval)
1020 {
1021 r300UpdateWindow(ctx);
1022 }
1023
1024 void r300UpdateViewportOffset(GLcontext * ctx)
1025 {
1026 r300ContextPtr rmesa = R300_CONTEXT(ctx);
1027 __DRIdrawablePrivate *dPriv = radeon_get_drawable(&rmesa->radeon);
1028 GLfloat xoffset = (GLfloat) dPriv->x;
1029 GLfloat yoffset = (GLfloat) dPriv->y + dPriv->h;
1030 const GLfloat *v = ctx->Viewport._WindowMap.m;
1031
1032 GLfloat tx = v[MAT_TX] + xoffset;
1033 GLfloat ty = (-v[MAT_TY]) + yoffset;
1034
1035 if (rmesa->hw.vpt.cmd[R300_VPT_XOFFSET] != r300PackFloat32(tx) ||
1036 rmesa->hw.vpt.cmd[R300_VPT_YOFFSET] != r300PackFloat32(ty)) {
1037 /* Note: this should also modify whatever data the context reset
1038 * code uses...
1039 */
1040 R300_STATECHANGE(rmesa, vpt);
1041 rmesa->hw.vpt.cmd[R300_VPT_XOFFSET] = r300PackFloat32(tx);
1042 rmesa->hw.vpt.cmd[R300_VPT_YOFFSET] = r300PackFloat32(ty);
1043
1044 }
1045
1046 radeonUpdateScissor(ctx);
1047 }
1048
1049 static void
1050 r300FetchStateParameter(GLcontext * ctx,
1051 const gl_state_index state[STATE_LENGTH],
1052 GLfloat * value)
1053 {
1054 r300ContextPtr r300 = R300_CONTEXT(ctx);
1055
1056 switch (state[0]) {
1057 case STATE_INTERNAL:
1058 switch (state[1]) {
1059 case STATE_R300_WINDOW_DIMENSION: {
1060 __DRIdrawablePrivate * drawable = radeon_get_drawable(&r300->radeon);
1061 value[0] = drawable->w * 0.5f; /* width*0.5 */
1062 value[1] = drawable->h * 0.5f; /* height*0.5 */
1063 value[2] = 0.5F; /* for moving range [-1 1] -> [0 1] */
1064 value[3] = 1.0F; /* not used */
1065 break;
1066 }
1067
1068 case STATE_R300_TEXRECT_FACTOR:{
1069 struct gl_texture_object *t =
1070 ctx->Texture.Unit[state[2]].CurrentTex[TEXTURE_RECT_INDEX];
1071
1072 if (t && t->Image[0][t->BaseLevel]) {
1073 struct gl_texture_image *image =
1074 t->Image[0][t->BaseLevel];
1075 value[0] = 1.0 / image->Width2;
1076 value[1] = 1.0 / image->Height2;
1077 } else {
1078 value[0] = 1.0;
1079 value[1] = 1.0;
1080 }
1081 value[2] = 1.0;
1082 value[3] = 1.0;
1083 break;
1084 }
1085
1086 default:
1087 break;
1088 }
1089 break;
1090
1091 default:
1092 break;
1093 }
1094 }
1095
1096 /**
1097 * Update R300's own internal state parameters.
1098 * For now just STATE_R300_WINDOW_DIMENSION
1099 */
1100 static void r300UpdateStateParameters(GLcontext * ctx, GLuint new_state)
1101 {
1102 r300ContextPtr rmesa = R300_CONTEXT(ctx);
1103 struct gl_program_parameter_list *paramList;
1104 GLuint i;
1105
1106 if (!(new_state & (_NEW_BUFFERS | _NEW_PROGRAM | _NEW_PROGRAM_CONSTANTS)))
1107 return;
1108
1109 if (!ctx->FragmentProgram._Current || !rmesa->selected_fp)
1110 return;
1111
1112 paramList = rmesa->selected_fp->Base->Parameters;
1113
1114 if (!paramList)
1115 return;
1116
1117 _mesa_load_state_parameters(ctx, paramList);
1118
1119 for (i = 0; i < paramList->NumParameters; i++) {
1120 if (paramList->Parameters[i].Type == PROGRAM_STATE_VAR) {
1121 r300FetchStateParameter(ctx,
1122 paramList->Parameters[i].
1123 StateIndexes,
1124 paramList->ParameterValues[i]);
1125 }
1126 }
1127 }
1128
1129 /* =============================================================
1130 * Polygon state
1131 */
1132 static void r300PolygonOffset(GLcontext * ctx, GLfloat factor, GLfloat units)
1133 {
1134 r300ContextPtr rmesa = R300_CONTEXT(ctx);
1135 GLfloat constant = units;
1136
1137 switch (ctx->Visual.depthBits) {
1138 case 16:
1139 constant *= 4.0;
1140 break;
1141 case 24:
1142 constant *= 2.0;
1143 break;
1144 }
1145
1146 factor *= 12.0;
1147
1148 /* fprintf(stderr, "%s f:%f u:%f\n", __FUNCTION__, factor, constant); */
1149
1150 R300_STATECHANGE(rmesa, zbs);
1151 rmesa->hw.zbs.cmd[R300_ZBS_T_FACTOR] = r300PackFloat32(factor);
1152 rmesa->hw.zbs.cmd[R300_ZBS_T_CONSTANT] = r300PackFloat32(constant);
1153 rmesa->hw.zbs.cmd[R300_ZBS_W_FACTOR] = r300PackFloat32(factor);
1154 rmesa->hw.zbs.cmd[R300_ZBS_W_CONSTANT] = r300PackFloat32(constant);
1155 }
1156
1157 /* Routing and texture-related */
1158
1159 /* r300 doesnt handle GL_CLAMP and GL_MIRROR_CLAMP_EXT correctly when filter is NEAREST.
1160 * Since texwrap produces same results for GL_CLAMP and GL_CLAMP_TO_EDGE we use them instead.
1161 * We need to recalculate wrap modes whenever filter mode is changed because someone might do:
1162 * glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
1163 * glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
1164 * glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
1165 * Since r300 completely ignores R300_TX_CLAMP when either min or mag is nearest it cant handle
1166 * combinations where only one of them is nearest.
1167 */
1168 static unsigned long gen_fixed_filter(unsigned long f)
1169 {
1170 unsigned long mag, min, needs_fixing = 0;
1171 //return f;
1172
1173 /* We ignore MIRROR bit so we dont have to do everything twice */
1174 if ((f & ((7 - 1) << R300_TX_WRAP_S_SHIFT)) ==
1175 (R300_TX_CLAMP << R300_TX_WRAP_S_SHIFT)) {
1176 needs_fixing |= 1;
1177 }
1178 if ((f & ((7 - 1) << R300_TX_WRAP_T_SHIFT)) ==
1179 (R300_TX_CLAMP << R300_TX_WRAP_T_SHIFT)) {
1180 needs_fixing |= 2;
1181 }
1182 if ((f & ((7 - 1) << R300_TX_WRAP_R_SHIFT)) ==
1183 (R300_TX_CLAMP << R300_TX_WRAP_R_SHIFT)) {
1184 needs_fixing |= 4;
1185 }
1186
1187 if (!needs_fixing)
1188 return f;
1189
1190 mag = f & R300_TX_MAG_FILTER_MASK;
1191 min = f & (R300_TX_MIN_FILTER_MASK|R300_TX_MIN_FILTER_MIP_MASK);
1192
1193 /* TODO: Check for anisto filters too */
1194 if ((mag != R300_TX_MAG_FILTER_NEAREST)
1195 && (min != R300_TX_MIN_FILTER_NEAREST))
1196 return f;
1197
1198 /* r300 cant handle these modes hence we force nearest to linear */
1199 if ((mag == R300_TX_MAG_FILTER_NEAREST)
1200 && (min != R300_TX_MIN_FILTER_NEAREST)) {
1201 f &= ~R300_TX_MAG_FILTER_NEAREST;
1202 f |= R300_TX_MAG_FILTER_LINEAR;
1203 return f;
1204 }
1205
1206 if ((min == R300_TX_MIN_FILTER_NEAREST)
1207 && (mag != R300_TX_MAG_FILTER_NEAREST)) {
1208 f &= ~R300_TX_MIN_FILTER_NEAREST;
1209 f |= R300_TX_MIN_FILTER_LINEAR;
1210 return f;
1211 }
1212
1213 /* Both are nearest */
1214 if (needs_fixing & 1) {
1215 f &= ~((7 - 1) << R300_TX_WRAP_S_SHIFT);
1216 f |= R300_TX_CLAMP_TO_EDGE << R300_TX_WRAP_S_SHIFT;
1217 }
1218 if (needs_fixing & 2) {
1219 f &= ~((7 - 1) << R300_TX_WRAP_T_SHIFT);
1220 f |= R300_TX_CLAMP_TO_EDGE << R300_TX_WRAP_T_SHIFT;
1221 }
1222 if (needs_fixing & 4) {
1223 f &= ~((7 - 1) << R300_TX_WRAP_R_SHIFT);
1224 f |= R300_TX_CLAMP_TO_EDGE << R300_TX_WRAP_R_SHIFT;
1225 }
1226 return f;
1227 }
1228
1229 static void r300SetupFragmentShaderTextures(GLcontext *ctx, int *tmu_mappings)
1230 {
1231 r300ContextPtr r300 = R300_CONTEXT(ctx);
1232 int i;
1233 struct r300_fragment_program_code *code = &r300->selected_fp->code.r300;
1234
1235 R300_STATECHANGE(r300, fpt);
1236
1237 for (i = 0; i < code->tex.length; i++) {
1238 int unit;
1239 int opcode;
1240 unsigned long val;
1241
1242 unit = code->tex.inst[i] >> R300_TEX_ID_SHIFT;
1243 unit &= 15;
1244
1245 val = code->tex.inst[i];
1246 val &= ~R300_TEX_ID_MASK;
1247
1248 opcode =
1249 (val & R300_TEX_INST_MASK) >> R300_TEX_INST_SHIFT;
1250 if (opcode == R300_TEX_OP_KIL) {
1251 r300->hw.fpt.cmd[R300_FPT_INSTR_0 + i] = val;
1252 } else {
1253 if (tmu_mappings[unit] >= 0) {
1254 val |=
1255 tmu_mappings[unit] <<
1256 R300_TEX_ID_SHIFT;
1257 r300->hw.fpt.cmd[R300_FPT_INSTR_0 + i] = val;
1258 } else {
1259 // We get here when the corresponding texture image is incomplete
1260 // (e.g. incomplete mipmaps etc.)
1261 r300->hw.fpt.cmd[R300_FPT_INSTR_0 + i] = val;
1262 }
1263 }
1264 }
1265
1266 r300->hw.fpt.cmd[R300_FPT_CMD_0] =
1267 cmdpacket0(r300->radeon.radeonScreen,
1268 R300_US_TEX_INST_0, code->tex.length);
1269 }
1270
1271 static void r500SetupFragmentShaderTextures(GLcontext *ctx, int *tmu_mappings)
1272 {
1273 r300ContextPtr r300 = R300_CONTEXT(ctx);
1274 int i;
1275 struct r500_fragment_program_code *code = &r300->selected_fp->code.r500;
1276
1277 /* find all the texture instructions and relocate the texture units */
1278 for (i = 0; i < code->inst_end + 1; i++) {
1279 if ((code->inst[i].inst0 & 0x3) == R500_INST_TYPE_TEX) {
1280 uint32_t val;
1281 int unit, opcode, new_unit;
1282
1283 val = code->inst[i].inst1;
1284
1285 unit = (val >> 16) & 0xf;
1286
1287 val &= ~(0xf << 16);
1288
1289 opcode = val & (0x7 << 22);
1290 if (opcode == R500_TEX_INST_TEXKILL) {
1291 new_unit = 0;
1292 } else {
1293 if (tmu_mappings[unit] >= 0) {
1294 new_unit = tmu_mappings[unit];
1295 } else {
1296 new_unit = 0;
1297 }
1298 }
1299 val |= R500_TEX_ID(new_unit);
1300 code->inst[i].inst1 = val;
1301 }
1302 }
1303 }
1304
1305 static GLuint translate_lod_bias(GLfloat bias)
1306 {
1307 GLint b = (int)(bias*32);
1308 if (b >= (1 << 9))
1309 b = (1 << 9)-1;
1310 else if (b < -(1 << 9))
1311 b = -(1 << 9);
1312 return (((GLuint)b) << R300_LOD_BIAS_SHIFT) & R300_LOD_BIAS_MASK;
1313 }
1314
1315 static void r300SetupTextures(GLcontext * ctx)
1316 {
1317 int i, mtu;
1318 struct radeon_tex_obj *t;
1319 r300ContextPtr r300 = R300_CONTEXT(ctx);
1320 int hw_tmu = 0;
1321 int last_hw_tmu = -1; /* -1 translates into no setup costs for fields */
1322 int tmu_mappings[R300_MAX_TEXTURE_UNITS] = { -1, };
1323
1324 R300_STATECHANGE(r300, txe);
1325 R300_STATECHANGE(r300, tex.filter);
1326 R300_STATECHANGE(r300, tex.filter_1);
1327 R300_STATECHANGE(r300, tex.size);
1328 R300_STATECHANGE(r300, tex.format);
1329 R300_STATECHANGE(r300, tex.pitch);
1330 R300_STATECHANGE(r300, tex.offset);
1331 R300_STATECHANGE(r300, tex.chroma_key);
1332 R300_STATECHANGE(r300, tex.border_color);
1333
1334 r300->hw.txe.cmd[R300_TXE_ENABLE] = 0x0;
1335
1336 mtu = r300->radeon.glCtx->Const.MaxTextureUnits;
1337 if (RADEON_DEBUG & DEBUG_STATE)
1338 fprintf(stderr, "mtu=%d\n", mtu);
1339
1340 if (mtu > R300_MAX_TEXTURE_UNITS) {
1341 fprintf(stderr,
1342 "Aiiee ! mtu=%d is greater than R300_MAX_TEXTURE_UNITS=%d\n",
1343 mtu, R300_MAX_TEXTURE_UNITS);
1344 _mesa_exit(-1);
1345 }
1346
1347 /* We cannot let disabled tmu offsets pass DRM */
1348 for (i = 0; i < mtu; i++) {
1349 if (ctx->Texture.Unit[i]._ReallyEnabled) {
1350 tmu_mappings[i] = hw_tmu;
1351
1352 t = radeon_tex_obj(ctx->Texture.Unit[i]._Current);
1353 if (!t)
1354 continue;
1355
1356 if ((t->pp_txformat & 0xffffff00) == 0xffffff00) {
1357 WARN_ONCE
1358 ("unknown texture format (entry %x) encountered. Help me !\n",
1359 t->pp_txformat & 0xff);
1360 }
1361
1362 if (RADEON_DEBUG & DEBUG_STATE)
1363 fprintf(stderr,
1364 "Activating texture unit %d\n", i);
1365
1366 r300->hw.txe.cmd[R300_TXE_ENABLE] |= (1 << hw_tmu);
1367
1368 r300->hw.tex.filter.cmd[R300_TEX_VALUE_0 +
1369 hw_tmu] =
1370 gen_fixed_filter(t->pp_txfilter) | (hw_tmu << 28);
1371 /* Note: There is a LOD bias per texture unit and a LOD bias
1372 * per texture object. We add them here to get the correct behaviour.
1373 * (The per-texture object LOD bias was introduced in OpenGL 1.4
1374 * and is not present in the EXT_texture_object extension).
1375 */
1376 r300->hw.tex.filter_1.cmd[R300_TEX_VALUE_0 + hw_tmu] =
1377 t->pp_txfilter_1 |
1378 translate_lod_bias(ctx->Texture.Unit[i].LodBias + t->base.LodBias);
1379 r300->hw.tex.size.cmd[R300_TEX_VALUE_0 + hw_tmu] =
1380 t->pp_txsize;
1381 r300->hw.tex.format.cmd[R300_TEX_VALUE_0 +
1382 hw_tmu] = t->pp_txformat;
1383 r300->hw.tex.pitch.cmd[R300_TEX_VALUE_0 + hw_tmu] =
1384 t->pp_txpitch;
1385 r300->hw.textures[hw_tmu] = t;
1386
1387 if (t->tile_bits & R300_TXO_MACRO_TILE) {
1388 WARN_ONCE("macro tiling enabled!\n");
1389 }
1390
1391 if (t->tile_bits & R300_TXO_MICRO_TILE) {
1392 WARN_ONCE("micro tiling enabled!\n");
1393 }
1394
1395 r300->hw.tex.chroma_key.cmd[R300_TEX_VALUE_0 +
1396 hw_tmu] = 0x0;
1397 r300->hw.tex.border_color.cmd[R300_TEX_VALUE_0 +
1398 hw_tmu] =
1399 t->pp_border_color;
1400
1401 last_hw_tmu = hw_tmu;
1402
1403 hw_tmu++;
1404 }
1405 }
1406
1407 r300->hw.tex.filter.cmd[R300_TEX_CMD_0] =
1408 cmdpacket0(r300->radeon.radeonScreen, R300_TX_FILTER0_0, last_hw_tmu + 1);
1409 r300->hw.tex.filter_1.cmd[R300_TEX_CMD_0] =
1410 cmdpacket0(r300->radeon.radeonScreen, R300_TX_FILTER1_0, last_hw_tmu + 1);
1411 r300->hw.tex.size.cmd[R300_TEX_CMD_0] =
1412 cmdpacket0(r300->radeon.radeonScreen, R300_TX_SIZE_0, last_hw_tmu + 1);
1413 r300->hw.tex.format.cmd[R300_TEX_CMD_0] =
1414 cmdpacket0(r300->radeon.radeonScreen, R300_TX_FORMAT_0, last_hw_tmu + 1);
1415 r300->hw.tex.pitch.cmd[R300_TEX_CMD_0] =
1416 cmdpacket0(r300->radeon.radeonScreen, R300_TX_FORMAT2_0, last_hw_tmu + 1);
1417 r300->hw.tex.offset.cmd[R300_TEX_CMD_0] =
1418 cmdpacket0(r300->radeon.radeonScreen, R300_TX_OFFSET_0, last_hw_tmu + 1);
1419 r300->hw.tex.chroma_key.cmd[R300_TEX_CMD_0] =
1420 cmdpacket0(r300->radeon.radeonScreen, R300_TX_CHROMA_KEY_0, last_hw_tmu + 1);
1421 r300->hw.tex.border_color.cmd[R300_TEX_CMD_0] =
1422 cmdpacket0(r300->radeon.radeonScreen, R300_TX_BORDER_COLOR_0, last_hw_tmu + 1);
1423
1424 if (r300->radeon.radeonScreen->chip_family < CHIP_FAMILY_RV515) {
1425 if (ctx->FragmentProgram._Current->UsesKill && last_hw_tmu < 0) {
1426 // The KILL operation requires the first texture unit
1427 // to be enabled.
1428 r300->hw.txe.cmd[R300_TXE_ENABLE] |= 1;
1429 r300->hw.tex.filter.cmd[R300_TEX_VALUE_0] = 0;
1430 r300->hw.tex.filter.cmd[R300_TEX_CMD_0] =
1431 cmdpacket0(r300->radeon.radeonScreen, R300_TX_FILTER0_0, 1);
1432 }
1433 }
1434 r300->vtbl.SetupFragmentShaderTextures(ctx, tmu_mappings);
1435
1436 if (RADEON_DEBUG & DEBUG_STATE)
1437 fprintf(stderr, "TX_ENABLE: %08x last_hw_tmu=%d\n",
1438 r300->hw.txe.cmd[R300_TXE_ENABLE], last_hw_tmu);
1439 }
1440
1441 union r300_outputs_written {
1442 GLuint vp_outputs; /* hw_tcl_on */
1443 DECLARE_RENDERINPUTS(index_bitset); /* !hw_tcl_on */
1444 };
1445
1446 #define R300_OUTPUTS_WRITTEN_TEST(ow, vp_result, tnl_attrib) \
1447 ((hw_tcl_on) ? (ow).vp_outputs & (1 << (vp_result)) : \
1448 RENDERINPUTS_TEST( (ow.index_bitset), (tnl_attrib) ))
1449
1450 static void r300SetupRSUnit(GLcontext * ctx)
1451 {
1452 r300ContextPtr r300 = R300_CONTEXT(ctx);
1453 union r300_outputs_written OutputsWritten;
1454 GLuint InputsRead;
1455 int fp_reg, high_rr;
1456 int col_ip, tex_ip;
1457 int rs_tex_count = 0;
1458 int i, col_fmt, hw_tcl_on;
1459
1460 hw_tcl_on = r300->options.hw_tcl_enabled;
1461
1462 if (hw_tcl_on)
1463 OutputsWritten.vp_outputs = r300->selected_vp->Base->Base.OutputsWritten;
1464 else
1465 RENDERINPUTS_COPY(OutputsWritten.index_bitset, r300->render_inputs_bitset);
1466
1467 InputsRead = r300->selected_fp->Base->InputsRead;
1468
1469 R300_STATECHANGE(r300, ri);
1470 R300_STATECHANGE(r300, rc);
1471 R300_STATECHANGE(r300, rr);
1472
1473 fp_reg = col_ip = tex_ip = col_fmt = 0;
1474
1475 r300->hw.rc.cmd[1] = 0;
1476 r300->hw.rc.cmd[2] = 0;
1477 for (i=0; i<R300_RR_CMDSIZE-1; ++i)
1478 r300->hw.rr.cmd[R300_RR_INST_0 + i] = 0;
1479
1480 for (i=0; i<R300_RI_CMDSIZE-1; ++i)
1481 r300->hw.ri.cmd[R300_RI_INTERP_0 + i] = 0;
1482
1483
1484 if (InputsRead & FRAG_BIT_COL0) {
1485 if (R300_OUTPUTS_WRITTEN_TEST(OutputsWritten, VERT_RESULT_COL0, _TNL_ATTRIB_COLOR0)) {
1486 r300->hw.ri.cmd[R300_RI_INTERP_0 + col_ip] = R300_RS_COL_PTR(col_ip) | R300_RS_COL_FMT(R300_RS_COL_FMT_RGBA);
1487 r300->hw.rr.cmd[R300_RR_INST_0 + col_ip] = R300_RS_INST_COL_ID(col_ip) | R300_RS_INST_COL_CN_WRITE | R300_RS_INST_COL_ADDR(fp_reg);
1488 InputsRead &= ~FRAG_BIT_COL0;
1489 ++col_ip;
1490 ++fp_reg;
1491 } else {
1492 WARN_ONCE("fragprog wants col0, vp doesn't provide it\n");
1493 }
1494 }
1495
1496 if (InputsRead & FRAG_BIT_COL1) {
1497 if (R300_OUTPUTS_WRITTEN_TEST(OutputsWritten, VERT_RESULT_COL1, _TNL_ATTRIB_COLOR1)) {
1498 r300->hw.ri.cmd[R300_RI_INTERP_0 + col_ip] = R300_RS_COL_PTR(col_ip) | R300_RS_COL_FMT(R300_RS_COL_FMT_RGBA);
1499 r300->hw.rr.cmd[R300_RR_INST_0 + col_ip] = R300_RS_INST_COL_ID(col_ip) | R300_RS_INST_COL_CN_WRITE | R300_RS_INST_COL_ADDR(fp_reg);
1500 InputsRead &= ~FRAG_BIT_COL1;
1501 ++col_ip;
1502 ++fp_reg;
1503 } else {
1504 WARN_ONCE("fragprog wants col1, vp doesn't provide it\n");
1505 }
1506 }
1507
1508 /* We always route 4 texcoord components */
1509 for (i = 0; i < ctx->Const.MaxTextureUnits; i++) {
1510 if (! ( InputsRead & FRAG_BIT_TEX(i) ) )
1511 continue;
1512
1513 if (!R300_OUTPUTS_WRITTEN_TEST(OutputsWritten, VERT_RESULT_TEX0 + i, _TNL_ATTRIB_TEX(i))) {
1514 WARN_ONCE("fragprog wants coords for tex%d, vp doesn't provide them!\n", i);
1515 continue;
1516 }
1517
1518 r300->hw.ri.cmd[R300_RI_INTERP_0 + tex_ip] |= R300_RS_SEL_S(0) | R300_RS_SEL_T(1) | R300_RS_SEL_R(2) | R300_RS_SEL_Q(3) | R300_RS_TEX_PTR(rs_tex_count);
1519 r300->hw.rr.cmd[R300_RR_INST_0 + tex_ip] |= R300_RS_INST_TEX_ID(tex_ip) | R300_RS_INST_TEX_CN_WRITE | R300_RS_INST_TEX_ADDR(fp_reg);
1520 InputsRead &= ~(FRAG_BIT_TEX0 << i);
1521 rs_tex_count += 4;
1522 ++tex_ip;
1523 ++fp_reg;
1524 }
1525
1526 /* Setup default color if no color or tex was set */
1527 if (rs_tex_count == 0 && col_ip == 0) {
1528 r300->hw.rr.cmd[R300_RR_INST_0] = R300_RS_INST_COL_ID(0) | R300_RS_INST_COL_ADDR(0);
1529 r300->hw.ri.cmd[R300_RI_INTERP_0] = R300_RS_COL_PTR(0) | R300_RS_COL_FMT(R300_RS_COL_FMT_0001);
1530 ++col_ip;
1531 }
1532
1533 high_rr = (col_ip > tex_ip) ? col_ip : tex_ip;
1534 r300->hw.rc.cmd[1] |= (rs_tex_count << R300_IT_COUNT_SHIFT) | (col_ip << R300_IC_COUNT_SHIFT) | R300_HIRES_EN;
1535 r300->hw.rc.cmd[2] |= high_rr - 1;
1536
1537 r300->hw.rr.cmd[R300_RR_CMD_0] = cmdpacket0(r300->radeon.radeonScreen, R300_RS_INST_0, high_rr);
1538 r300->hw.ri.cmd[R300_RI_CMD_0] = cmdpacket0(r300->radeon.radeonScreen, R300_RS_IP_0, high_rr);
1539
1540 if (InputsRead)
1541 WARN_ONCE("Don't know how to satisfy InputsRead=0x%08x\n", InputsRead);
1542 }
1543
1544 static void r500SetupRSUnit(GLcontext * ctx)
1545 {
1546 r300ContextPtr r300 = R300_CONTEXT(ctx);
1547 union r300_outputs_written OutputsWritten;
1548 GLuint InputsRead;
1549 int fp_reg, high_rr;
1550 int col_ip, tex_ip;
1551 int rs_tex_count = 0;
1552 int i, col_fmt, hw_tcl_on;
1553
1554 hw_tcl_on = r300->options.hw_tcl_enabled;
1555
1556 if (hw_tcl_on)
1557 OutputsWritten.vp_outputs = r300->selected_vp->Base->Base.OutputsWritten;
1558 else
1559 RENDERINPUTS_COPY(OutputsWritten.index_bitset, r300->render_inputs_bitset);
1560
1561 InputsRead = r300->selected_fp->Base->InputsRead;
1562
1563 R300_STATECHANGE(r300, ri);
1564 R300_STATECHANGE(r300, rc);
1565 R300_STATECHANGE(r300, rr);
1566
1567 fp_reg = col_ip = tex_ip = col_fmt = 0;
1568
1569 r300->hw.rc.cmd[1] = 0;
1570 r300->hw.rc.cmd[2] = 0;
1571 for (i=0; i<R300_RR_CMDSIZE-1; ++i)
1572 r300->hw.rr.cmd[R300_RR_INST_0 + i] = 0;
1573
1574 for (i=0; i<R500_RI_CMDSIZE-1; ++i)
1575 r300->hw.ri.cmd[R300_RI_INTERP_0 + i] = 0;
1576
1577
1578 if (InputsRead & FRAG_BIT_COL0) {
1579 if (R300_OUTPUTS_WRITTEN_TEST(OutputsWritten, VERT_RESULT_COL0, _TNL_ATTRIB_COLOR0)) {
1580 r300->hw.ri.cmd[R300_RI_INTERP_0 + col_ip] = R500_RS_COL_PTR(col_ip) | R500_RS_COL_FMT(R300_RS_COL_FMT_RGBA);
1581 r300->hw.rr.cmd[R300_RR_INST_0 + col_ip] = R500_RS_INST_COL_ID(col_ip) | R500_RS_INST_COL_CN_WRITE | R500_RS_INST_COL_ADDR(fp_reg);
1582 InputsRead &= ~FRAG_BIT_COL0;
1583 ++col_ip;
1584 ++fp_reg;
1585 } else {
1586 WARN_ONCE("fragprog wants col0, vp doesn't provide it\n");
1587 }
1588 }
1589
1590 if (InputsRead & FRAG_BIT_COL1) {
1591 if (R300_OUTPUTS_WRITTEN_TEST(OutputsWritten, VERT_RESULT_COL1, _TNL_ATTRIB_COLOR1)) {
1592 r300->hw.ri.cmd[R300_RI_INTERP_0 + col_ip] = R500_RS_COL_PTR(col_ip) | R500_RS_COL_FMT(R300_RS_COL_FMT_RGBA);
1593 r300->hw.rr.cmd[R300_RR_INST_0 + col_ip] = R500_RS_INST_COL_ID(col_ip) | R500_RS_INST_COL_CN_WRITE | R500_RS_INST_COL_ADDR(fp_reg);
1594 InputsRead &= ~FRAG_BIT_COL1;
1595 ++col_ip;
1596 ++fp_reg;
1597 } else {
1598 WARN_ONCE("fragprog wants col1, vp doesn't provide it\n");
1599 }
1600 }
1601
1602 /* We always route 4 texcoord components */
1603 for (i = 0; i < ctx->Const.MaxTextureUnits; i++) {
1604 if (! ( InputsRead & FRAG_BIT_TEX(i) ) )
1605 continue;
1606
1607 if (!R300_OUTPUTS_WRITTEN_TEST(OutputsWritten, VERT_RESULT_TEX0 + i, _TNL_ATTRIB_TEX(i))) {
1608 WARN_ONCE("fragprog wants coords for tex%d, vp doesn't provide them!\n", i);
1609 continue;
1610 }
1611
1612 r300->hw.ri.cmd[R300_RI_INTERP_0 + tex_ip] |= ((rs_tex_count + 0) << R500_RS_IP_TEX_PTR_S_SHIFT) |
1613 ((rs_tex_count + 1) << R500_RS_IP_TEX_PTR_T_SHIFT) |
1614 ((rs_tex_count + 2) << R500_RS_IP_TEX_PTR_R_SHIFT) |
1615 ((rs_tex_count + 3) << R500_RS_IP_TEX_PTR_Q_SHIFT);
1616
1617 r300->hw.rr.cmd[R300_RR_INST_0 + tex_ip] |= R500_RS_INST_TEX_ID(tex_ip) | R500_RS_INST_TEX_CN_WRITE | R500_RS_INST_TEX_ADDR(fp_reg);
1618 InputsRead &= ~(FRAG_BIT_TEX0 << i);
1619 rs_tex_count += 4;
1620 ++tex_ip;
1621 ++fp_reg;
1622 }
1623
1624 /* Setup default color if no color or tex was set */
1625 if (rs_tex_count == 0 && col_ip == 0) {
1626 r300->hw.rr.cmd[R300_RR_INST_0] = R500_RS_INST_COL_ID(0) | R500_RS_INST_COL_ADDR(0);
1627 r300->hw.ri.cmd[R300_RI_INTERP_0] = R500_RS_COL_PTR(0) | R500_RS_COL_FMT(R300_RS_COL_FMT_0001);
1628 ++col_ip;
1629 }
1630
1631 high_rr = (col_ip > tex_ip) ? col_ip : tex_ip;
1632 r300->hw.rc.cmd[1] = (rs_tex_count << R300_IT_COUNT_SHIFT) | (col_ip << R300_IC_COUNT_SHIFT) | R300_HIRES_EN;
1633 r300->hw.rc.cmd[2] = 0xC0 | (high_rr - 1);
1634
1635 r300->hw.rr.cmd[R300_RR_CMD_0] = cmdpacket0(r300->radeon.radeonScreen, R500_RS_INST_0, high_rr);
1636 r300->hw.ri.cmd[R300_RI_CMD_0] = cmdpacket0(r300->radeon.radeonScreen, R500_RS_IP_0, high_rr);
1637
1638 if (InputsRead)
1639 WARN_ONCE("Don't know how to satisfy InputsRead=0x%08x\n", InputsRead);
1640 }
1641
1642 #define MIN3(a, b, c) ((a) < (b) ? MIN2(a, c) : MIN2(b, c))
1643
1644 void r300VapCntl(r300ContextPtr rmesa, GLuint input_count,
1645 GLuint output_count, GLuint temp_count)
1646 {
1647 int vtx_mem_size;
1648 int pvs_num_slots;
1649 int pvs_num_cntrls;
1650
1651 /* Flush PVS engine before changing PVS_NUM_SLOTS, PVS_NUM_CNTRLS.
1652 * See r500 docs 6.5.2 - done in emit */
1653
1654 /* avoid division by zero */
1655 if (input_count == 0) input_count = 1;
1656 if (output_count == 0) output_count = 1;
1657 if (temp_count == 0) temp_count = 1;
1658
1659 if (rmesa->radeon.radeonScreen->chip_family >= CHIP_FAMILY_RV515)
1660 vtx_mem_size = 128;
1661 else
1662 vtx_mem_size = 72;
1663
1664 pvs_num_slots = MIN3(10, vtx_mem_size/input_count, vtx_mem_size/output_count);
1665 pvs_num_cntrls = MIN2(6, vtx_mem_size/temp_count);
1666
1667 R300_STATECHANGE(rmesa, vap_cntl);
1668 if (rmesa->options.hw_tcl_enabled) {
1669 rmesa->hw.vap_cntl.cmd[R300_VAP_CNTL_INSTR] =
1670 (pvs_num_slots << R300_PVS_NUM_SLOTS_SHIFT) |
1671 (pvs_num_cntrls << R300_PVS_NUM_CNTLRS_SHIFT) |
1672 (12 << R300_VF_MAX_VTX_NUM_SHIFT);
1673 if (rmesa->radeon.radeonScreen->chip_family >= CHIP_FAMILY_RV515)
1674 rmesa->hw.vap_cntl.cmd[R300_VAP_CNTL_INSTR] |= R500_TCL_STATE_OPTIMIZATION;
1675 } else
1676 /* not sure about non-tcl */
1677 rmesa->hw.vap_cntl.cmd[R300_VAP_CNTL_INSTR] = ((10 << R300_PVS_NUM_SLOTS_SHIFT) |
1678 (5 << R300_PVS_NUM_CNTLRS_SHIFT) |
1679 (5 << R300_VF_MAX_VTX_NUM_SHIFT));
1680
1681 if (rmesa->radeon.radeonScreen->chip_family == CHIP_FAMILY_RV515)
1682 rmesa->hw.vap_cntl.cmd[R300_VAP_CNTL_INSTR] |= (2 << R300_PVS_NUM_FPUS_SHIFT);
1683 else if ((rmesa->radeon.radeonScreen->chip_family == CHIP_FAMILY_RV530) ||
1684 (rmesa->radeon.radeonScreen->chip_family == CHIP_FAMILY_RV560) ||
1685 (rmesa->radeon.radeonScreen->chip_family == CHIP_FAMILY_RV570))
1686 rmesa->hw.vap_cntl.cmd[R300_VAP_CNTL_INSTR] |= (5 << R300_PVS_NUM_FPUS_SHIFT);
1687 else if ((rmesa->radeon.radeonScreen->chip_family == CHIP_FAMILY_RV410) ||
1688 (rmesa->radeon.radeonScreen->chip_family == CHIP_FAMILY_R420))
1689 rmesa->hw.vap_cntl.cmd[R300_VAP_CNTL_INSTR] |= (6 << R300_PVS_NUM_FPUS_SHIFT);
1690 else if ((rmesa->radeon.radeonScreen->chip_family == CHIP_FAMILY_R520) ||
1691 (rmesa->radeon.radeonScreen->chip_family == CHIP_FAMILY_R580))
1692 rmesa->hw.vap_cntl.cmd[R300_VAP_CNTL_INSTR] |= (8 << R300_PVS_NUM_FPUS_SHIFT);
1693 else
1694 rmesa->hw.vap_cntl.cmd[R300_VAP_CNTL_INSTR] |= (4 << R300_PVS_NUM_FPUS_SHIFT);
1695
1696 }
1697
1698 /**
1699 * Enable/Disable states.
1700 *
1701 * \note Mesa already filters redundant calls to this function.
1702 */
1703 static void r300Enable(GLcontext * ctx, GLenum cap, GLboolean state)
1704 {
1705 r300ContextPtr rmesa = R300_CONTEXT(ctx);
1706 if (RADEON_DEBUG & DEBUG_STATE)
1707 fprintf(stderr, "%s( %s = %s )\n", __FUNCTION__,
1708 _mesa_lookup_enum_by_nr(cap),
1709 state ? "GL_TRUE" : "GL_FALSE");
1710
1711 switch (cap) {
1712 case GL_ALPHA_TEST:
1713 r300SetAlphaState(ctx);
1714 break;
1715 case GL_COLOR_LOGIC_OP:
1716 r300SetLogicOpState(ctx);
1717 /* fall-through, because logic op overrides blending */
1718 case GL_BLEND:
1719 r300SetBlendState(ctx);
1720 break;
1721 case GL_CLIP_PLANE0:
1722 case GL_CLIP_PLANE1:
1723 case GL_CLIP_PLANE2:
1724 case GL_CLIP_PLANE3:
1725 case GL_CLIP_PLANE4:
1726 case GL_CLIP_PLANE5:
1727 r300SetClipPlaneState(ctx, cap, state);
1728 break;
1729 case GL_CULL_FACE:
1730 r300UpdateCulling(ctx);
1731 break;
1732 case GL_DEPTH_TEST:
1733 r300SetDepthState(ctx);
1734 break;
1735 case GL_LINE_SMOOTH:
1736 if (rmesa->options.conformance_mode)
1737 r300SwitchFallback(ctx, R300_FALLBACK_LINE_SMOOTH, ctx->Line.SmoothFlag);
1738 break;
1739 case GL_LINE_STIPPLE:
1740 if (rmesa->options.conformance_mode)
1741 r300SwitchFallback(ctx, R300_FALLBACK_LINE_STIPPLE, ctx->Line.StippleFlag);
1742 break;
1743 case GL_POINT_SMOOTH:
1744 if (rmesa->options.conformance_mode)
1745 r300SwitchFallback(ctx, R300_FALLBACK_POINT_SMOOTH, ctx->Point.SmoothFlag);
1746 break;
1747 case GL_POLYGON_SMOOTH:
1748 if (rmesa->options.conformance_mode)
1749 r300SwitchFallback(ctx, R300_FALLBACK_POLYGON_SMOOTH, ctx->Polygon.SmoothFlag);
1750 break;
1751 case GL_POLYGON_STIPPLE:
1752 if (rmesa->options.conformance_mode)
1753 r300SwitchFallback(ctx, R300_FALLBACK_POLYGON_STIPPLE, ctx->Polygon.StippleFlag);
1754 break;
1755 case GL_POLYGON_OFFSET_POINT:
1756 case GL_POLYGON_OFFSET_LINE:
1757 case GL_POLYGON_OFFSET_FILL:
1758 r300SetPolygonOffsetState(ctx, state);
1759 break;
1760 case GL_SCISSOR_TEST:
1761 radeon_firevertices(&rmesa->radeon);
1762 rmesa->radeon.state.scissor.enabled = state;
1763 radeonUpdateScissor( ctx );
1764 break;
1765 case GL_STENCIL_TEST:
1766 r300SetStencilState(ctx, state);
1767 break;
1768 default:
1769 break;
1770 }
1771 }
1772
1773 /**
1774 * Completely recalculates hardware state based on the Mesa state.
1775 */
1776 static void r300ResetHwState(r300ContextPtr r300)
1777 {
1778 GLcontext *ctx = r300->radeon.glCtx;
1779 int has_tcl;
1780
1781 has_tcl = r300->options.hw_tcl_enabled;
1782
1783 if (RADEON_DEBUG & DEBUG_STATE)
1784 fprintf(stderr, "%s\n", __FUNCTION__);
1785
1786 radeon_firevertices(&r300->radeon);
1787
1788 r300ColorMask(ctx,
1789 ctx->Color.ColorMask[RCOMP],
1790 ctx->Color.ColorMask[GCOMP],
1791 ctx->Color.ColorMask[BCOMP], ctx->Color.ColorMask[ACOMP]);
1792
1793 r300Enable(ctx, GL_DEPTH_TEST, ctx->Depth.Test);
1794 r300DepthMask(ctx, ctx->Depth.Mask);
1795 r300DepthFunc(ctx, ctx->Depth.Func);
1796
1797 /* stencil */
1798 r300Enable(ctx, GL_STENCIL_TEST, ctx->Stencil._Enabled);
1799 r300StencilMaskSeparate(ctx, 0, ctx->Stencil.WriteMask[0]);
1800 r300StencilFuncSeparate(ctx, 0, ctx->Stencil.Function[0],
1801 ctx->Stencil.Ref[0], ctx->Stencil.ValueMask[0]);
1802 r300StencilOpSeparate(ctx, 0, ctx->Stencil.FailFunc[0],
1803 ctx->Stencil.ZFailFunc[0],
1804 ctx->Stencil.ZPassFunc[0]);
1805
1806 r300UpdateCulling(ctx);
1807
1808 r300SetBlendState(ctx);
1809 r300SetLogicOpState(ctx);
1810
1811 r300AlphaFunc(ctx, ctx->Color.AlphaFunc, ctx->Color.AlphaRef);
1812 r300Enable(ctx, GL_ALPHA_TEST, ctx->Color.AlphaEnabled);
1813
1814 r300->hw.vte.cmd[1] = R300_VPORT_X_SCALE_ENA
1815 | R300_VPORT_X_OFFSET_ENA
1816 | R300_VPORT_Y_SCALE_ENA
1817 | R300_VPORT_Y_OFFSET_ENA
1818 | R300_VPORT_Z_SCALE_ENA
1819 | R300_VPORT_Z_OFFSET_ENA | R300_VTX_W0_FMT;
1820 r300->hw.vte.cmd[2] = 0x00000008;
1821
1822 r300->hw.vap_vf_max_vtx_indx.cmd[1] = 0x00FFFFFF;
1823 r300->hw.vap_vf_max_vtx_indx.cmd[2] = 0x00000000;
1824
1825 #ifdef MESA_LITTLE_ENDIAN
1826 r300->hw.vap_cntl_status.cmd[1] = R300_VC_NO_SWAP;
1827 #else
1828 r300->hw.vap_cntl_status.cmd[1] = R300_VC_32BIT_SWAP;
1829 #endif
1830
1831 /* disable VAP/TCL on non-TCL capable chips */
1832 if (!has_tcl)
1833 r300->hw.vap_cntl_status.cmd[1] |= R300_VAP_TCL_BYPASS;
1834
1835 r300->hw.vap_psc_sgn_norm_cntl.cmd[1] = 0xAAAAAAAA;
1836
1837 /* XXX: Other families? */
1838 if (has_tcl) {
1839 r300->hw.vap_clip_cntl.cmd[1] = R300_PS_UCP_MODE_DIST_COP;
1840
1841 r300->hw.vap_clip.cmd[1] = r300PackFloat32(1.0); /* X */
1842 r300->hw.vap_clip.cmd[2] = r300PackFloat32(1.0); /* X */
1843 r300->hw.vap_clip.cmd[3] = r300PackFloat32(1.0); /* Y */
1844 r300->hw.vap_clip.cmd[4] = r300PackFloat32(1.0); /* Y */
1845
1846 switch (r300->radeon.radeonScreen->chip_family) {
1847 case CHIP_FAMILY_R300:
1848 r300->hw.vap_pvs_vtx_timeout_reg.cmd[1] = R300_2288_R300;
1849 break;
1850 default:
1851 r300->hw.vap_pvs_vtx_timeout_reg.cmd[1] = R300_2288_RV350;
1852 break;
1853 }
1854 }
1855
1856 r300->hw.gb_enable.cmd[1] = R300_GB_POINT_STUFF_ENABLE
1857 | R300_GB_LINE_STUFF_ENABLE
1858 | R300_GB_TRIANGLE_STUFF_ENABLE;
1859
1860 r300->hw.gb_misc.cmd[R300_GB_MISC_MSPOS_0] = 0x66666666;
1861 r300->hw.gb_misc.cmd[R300_GB_MISC_MSPOS_1] = 0x06666666;
1862
1863 r300->hw.gb_misc.cmd[R300_GB_MISC_TILE_CONFIG] =
1864 R300_GB_TILE_ENABLE | R300_GB_TILE_SIZE_16 /*| R300_GB_SUBPIXEL_1_16*/;
1865 switch (r300->radeon.radeonScreen->num_gb_pipes) {
1866 case 1:
1867 default:
1868 r300->hw.gb_misc.cmd[R300_GB_MISC_TILE_CONFIG] |=
1869 R300_GB_TILE_PIPE_COUNT_RV300;
1870 break;
1871 case 2:
1872 r300->hw.gb_misc.cmd[R300_GB_MISC_TILE_CONFIG] |=
1873 R300_GB_TILE_PIPE_COUNT_R300;
1874 break;
1875 case 3:
1876 r300->hw.gb_misc.cmd[R300_GB_MISC_TILE_CONFIG] |=
1877 R300_GB_TILE_PIPE_COUNT_R420_3P;
1878 break;
1879 case 4:
1880 r300->hw.gb_misc.cmd[R300_GB_MISC_TILE_CONFIG] |=
1881 R300_GB_TILE_PIPE_COUNT_R420;
1882 break;
1883 }
1884
1885 /* XXX: Enable anti-aliasing? */
1886 r300->hw.gb_misc2.cmd[R300_GB_MISC2_AA_CONFIG] = GB_AA_CONFIG_AA_DISABLE;
1887 r300->hw.gb_misc2.cmd[R300_GB_MISC2_SELECT] = 0;
1888
1889 r300->hw.ga_point_s0.cmd[1] = r300PackFloat32(0.0);
1890 r300->hw.ga_point_s0.cmd[2] = r300PackFloat32(0.0);
1891 r300->hw.ga_point_s0.cmd[3] = r300PackFloat32(1.0);
1892 r300->hw.ga_point_s0.cmd[4] = r300PackFloat32(1.0);
1893
1894 r300->hw.ga_triangle_stipple.cmd[1] = 0x00050005;
1895
1896 r300PointSize(ctx, 1.0);
1897
1898 r300->hw.ga_point_minmax.cmd[1] = 0x18000006;
1899 r300->hw.ga_point_minmax.cmd[2] = 0x00020006;
1900 r300->hw.ga_point_minmax.cmd[3] = r300PackFloat32(1.0 / 192.0);
1901
1902 r300LineWidth(ctx, 1.0);
1903
1904 r300->hw.ga_line_stipple.cmd[1] = 0;
1905 r300->hw.ga_line_stipple.cmd[2] = r300PackFloat32(0.0);
1906 r300->hw.ga_line_stipple.cmd[3] = r300PackFloat32(1.0);
1907
1908 r300ShadeModel(ctx, ctx->Light.ShadeModel);
1909
1910 r300PolygonMode(ctx, GL_FRONT, ctx->Polygon.FrontMode);
1911 r300PolygonMode(ctx, GL_BACK, ctx->Polygon.BackMode);
1912 r300->hw.zbias_cntl.cmd[1] = 0x00000000;
1913
1914 r300PolygonOffset(ctx, ctx->Polygon.OffsetFactor,
1915 ctx->Polygon.OffsetUnits);
1916 r300Enable(ctx, GL_POLYGON_OFFSET_POINT, ctx->Polygon.OffsetPoint);
1917 r300Enable(ctx, GL_POLYGON_OFFSET_LINE, ctx->Polygon.OffsetLine);
1918 r300Enable(ctx, GL_POLYGON_OFFSET_FILL, ctx->Polygon.OffsetFill);
1919
1920 r300->hw.su_depth_scale.cmd[1] = 0x4B7FFFFF;
1921 r300->hw.su_depth_scale.cmd[2] = 0x00000000;
1922
1923 r300->hw.sc_hyperz.cmd[1] = 0x0000001C;
1924 r300->hw.sc_hyperz.cmd[2] = 0x2DA49525;
1925
1926 r300->hw.sc_screendoor.cmd[1] = 0x00FFFFFF;
1927
1928 r300->hw.us_out_fmt.cmd[1] = R500_OUT_FMT_C4_8 |
1929 R500_C0_SEL_B | R500_C1_SEL_G | R500_C2_SEL_R | R500_C3_SEL_A;
1930 r300->hw.us_out_fmt.cmd[2] = R500_OUT_FMT_UNUSED |
1931 R500_C0_SEL_B | R500_C1_SEL_G | R500_C2_SEL_R | R500_C3_SEL_A;
1932 r300->hw.us_out_fmt.cmd[3] = R500_OUT_FMT_UNUSED |
1933 R500_C0_SEL_B | R500_C1_SEL_G | R500_C2_SEL_R | R500_C3_SEL_A;
1934 r300->hw.us_out_fmt.cmd[4] = R500_OUT_FMT_UNUSED |
1935 R500_C0_SEL_B | R500_C1_SEL_G | R500_C2_SEL_R | R500_C3_SEL_A;
1936 r300->hw.us_out_fmt.cmd[5] = R300_W_FMT_W0 | R300_W_SRC_US;
1937
1938 /* disable fog unit */
1939 r300->hw.fogs.cmd[R300_FOGS_STATE] = 0;
1940 r300->hw.fg_depth_src.cmd[1] = R300_FG_DEPTH_SRC_SCAN;
1941
1942 r300->hw.rb3d_cctl.cmd[1] = 0;
1943
1944 r300BlendColor(ctx, ctx->Color.BlendColor);
1945
1946 r300->hw.rb3d_dither_ctl.cmd[1] = 0;
1947 r300->hw.rb3d_dither_ctl.cmd[2] = 0;
1948 r300->hw.rb3d_dither_ctl.cmd[3] = 0;
1949 r300->hw.rb3d_dither_ctl.cmd[4] = 0;
1950 r300->hw.rb3d_dither_ctl.cmd[5] = 0;
1951 r300->hw.rb3d_dither_ctl.cmd[6] = 0;
1952 r300->hw.rb3d_dither_ctl.cmd[7] = 0;
1953 r300->hw.rb3d_dither_ctl.cmd[8] = 0;
1954 r300->hw.rb3d_dither_ctl.cmd[9] = 0;
1955
1956 r300->hw.rb3d_aaresolve_ctl.cmd[1] = 0;
1957
1958 r300->hw.rb3d_discard_src_pixel_lte_threshold.cmd[1] = 0x00000000;
1959 r300->hw.rb3d_discard_src_pixel_lte_threshold.cmd[2] = 0xffffffff;
1960
1961 r300->hw.zb_depthclearvalue.cmd[1] = 0;
1962
1963 r300->hw.zstencil_format.cmd[2] = R300_ZTOP_DISABLE;
1964 r300->hw.zstencil_format.cmd[3] = 0x00000003;
1965 r300->hw.zstencil_format.cmd[4] = 0x00000000;
1966 r300SetEarlyZState(ctx);
1967
1968 r300->hw.zb_zmask.cmd[1] = 0;
1969 r300->hw.zb_zmask.cmd[2] = 0;
1970
1971 r300->hw.zb_hiz_offset.cmd[1] = 0;
1972
1973 r300->hw.zb_hiz_pitch.cmd[1] = 0;
1974
1975 r300VapCntl(r300, 0, 0, 0);
1976 if (has_tcl) {
1977 r300->hw.vps.cmd[R300_VPS_ZERO_0] = 0;
1978 r300->hw.vps.cmd[R300_VPS_ZERO_1] = 0;
1979 r300->hw.vps.cmd[R300_VPS_POINTSIZE] = r300PackFloat32(1.0);
1980 r300->hw.vps.cmd[R300_VPS_ZERO_3] = 0;
1981 }
1982
1983 r300->radeon.hw.all_dirty = GL_TRUE;
1984 }
1985
1986 void r300UpdateShaders(r300ContextPtr rmesa)
1987 {
1988 GLcontext *ctx = rmesa->radeon.glCtx;
1989
1990 /* should only happenen once, just after context is created */
1991 /* TODO: shouldn't we fallback to sw here? */
1992 if (!ctx->FragmentProgram._Current) {
1993 _mesa_fprintf(stderr, "No ctx->FragmentProgram._Current!!\n");
1994 return;
1995 }
1996
1997 {
1998 struct r300_fragment_program *fp;
1999
2000 fp = r300SelectFragmentShader(ctx);
2001 if (!fp->translated)
2002 r300TranslateFragmentShader(ctx, fp);
2003
2004 r300SwitchFallback(ctx, R300_FALLBACK_FRAGMENT_PROGRAM, fp->error);
2005 }
2006
2007 if (rmesa->options.hw_tcl_enabled) {
2008 struct r300_vertex_program *vp;
2009
2010 if (rmesa->radeon.NewGLState) {
2011 int i;
2012 for (i = _TNL_FIRST_MAT; i <= _TNL_LAST_MAT; i++) {
2013 rmesa->temp_attrib[i] =
2014 TNL_CONTEXT(ctx)->vb.AttribPtr[i];
2015 TNL_CONTEXT(ctx)->vb.AttribPtr[i] =
2016 &rmesa->dummy_attrib[i];
2017 }
2018
2019 _tnl_UpdateFixedFunctionProgram(ctx);
2020
2021 for (i = _TNL_FIRST_MAT; i <= _TNL_LAST_MAT; i++) {
2022 TNL_CONTEXT(ctx)->vb.AttribPtr[i] =
2023 rmesa->temp_attrib[i];
2024 }
2025 }
2026
2027 vp = r300SelectVertexShader(ctx);
2028 if (!vp->translated)
2029 r300TranslateVertexShader(vp);
2030
2031 r300SwitchFallback(ctx, R300_FALLBACK_VERTEX_PROGRAM, vp->error);
2032 }
2033
2034 r300UpdateStateParameters(ctx, _NEW_PROGRAM | _NEW_PROGRAM_CONSTANTS);
2035 rmesa->radeon.NewGLState = 0;
2036 }
2037
2038 static const GLfloat *get_fragmentprogram_constant(GLcontext *ctx,
2039 struct gl_program *program, struct prog_src_register srcreg)
2040 {
2041 static const GLfloat dummy[4] = { 0, 0, 0, 0 };
2042
2043 switch(srcreg.File) {
2044 case PROGRAM_LOCAL_PARAM:
2045 return program->LocalParams[srcreg.Index];
2046 case PROGRAM_ENV_PARAM:
2047 return ctx->FragmentProgram.Parameters[srcreg.Index];
2048 case PROGRAM_STATE_VAR:
2049 case PROGRAM_NAMED_PARAM:
2050 case PROGRAM_CONSTANT:
2051 return program->Parameters->ParameterValues[srcreg.Index];
2052 default:
2053 _mesa_problem(ctx, "get_fragmentprogram_constant: Unknown\n");
2054 return dummy;
2055 }
2056 }
2057
2058
2059 static void r300SetupPixelShader(GLcontext *ctx)
2060 {
2061 r300ContextPtr rmesa = R300_CONTEXT(ctx);
2062 struct r300_fragment_program *fp = rmesa->selected_fp;
2063 struct r300_fragment_program_code *code;
2064 int i, k;
2065
2066 code = &fp->code.r300;
2067
2068 R300_STATECHANGE(rmesa, fpi[0]);
2069 R300_STATECHANGE(rmesa, fpi[1]);
2070 R300_STATECHANGE(rmesa, fpi[2]);
2071 R300_STATECHANGE(rmesa, fpi[3]);
2072 rmesa->hw.fpi[0].cmd[R300_FPI_CMD_0] = cmdpacket0(rmesa->radeon.radeonScreen, R300_US_ALU_RGB_INST_0, code->alu.length);
2073 rmesa->hw.fpi[1].cmd[R300_FPI_CMD_0] = cmdpacket0(rmesa->radeon.radeonScreen, R300_US_ALU_RGB_ADDR_0, code->alu.length);
2074 rmesa->hw.fpi[2].cmd[R300_FPI_CMD_0] = cmdpacket0(rmesa->radeon.radeonScreen, R300_US_ALU_ALPHA_INST_0, code->alu.length);
2075 rmesa->hw.fpi[3].cmd[R300_FPI_CMD_0] = cmdpacket0(rmesa->radeon.radeonScreen, R300_US_ALU_ALPHA_ADDR_0, code->alu.length);
2076 for (i = 0; i < code->alu.length; i++) {
2077 rmesa->hw.fpi[0].cmd[R300_FPI_INSTR_0 + i] = code->alu.inst[i].inst0;
2078 rmesa->hw.fpi[1].cmd[R300_FPI_INSTR_0 + i] = code->alu.inst[i].inst1;
2079 rmesa->hw.fpi[2].cmd[R300_FPI_INSTR_0 + i] = code->alu.inst[i].inst2;
2080 rmesa->hw.fpi[3].cmd[R300_FPI_INSTR_0 + i] = code->alu.inst[i].inst3;
2081 }
2082
2083 R300_STATECHANGE(rmesa, fp);
2084 rmesa->hw.fp.cmd[R300_FP_CNTL0] = code->cur_node | (code->first_node_has_tex << 3);
2085 rmesa->hw.fp.cmd[R300_FP_CNTL1] = code->max_temp_idx;
2086 rmesa->hw.fp.cmd[R300_FP_CNTL2] =
2087 (0 << R300_PFS_CNTL_ALU_OFFSET_SHIFT) |
2088 ((code->alu.length-1) << R300_PFS_CNTL_ALU_END_SHIFT) |
2089 (0 << R300_PFS_CNTL_TEX_OFFSET_SHIFT) |
2090 ((code->tex.length ? code->tex.length-1 : 0) << R300_PFS_CNTL_TEX_END_SHIFT);
2091 /* I just want to say, the way these nodes are stored.. weird.. */
2092 for (i = 0, k = (4 - (code->cur_node + 1)); i < 4; i++, k++) {
2093 if (i < (code->cur_node + 1)) {
2094 rmesa->hw.fp.cmd[R300_FP_NODE0 + k] =
2095 (code->node[i].alu_offset << R300_ALU_START_SHIFT) |
2096 (code->node[i].alu_end << R300_ALU_SIZE_SHIFT) |
2097 (code->node[i].tex_offset << R300_TEX_START_SHIFT) |
2098 (code->node[i].tex_end << R300_TEX_SIZE_SHIFT) |
2099 code->node[i].flags;
2100 } else {
2101 rmesa->hw.fp.cmd[R300_FP_NODE0 + (3 - i)] = 0;
2102 }
2103 }
2104
2105 R300_STATECHANGE(rmesa, fpp);
2106 rmesa->hw.fpp.cmd[R300_FPP_CMD_0] = cmdpacket0(rmesa->radeon.radeonScreen, R300_PFS_PARAM_0_X, code->const_nr * 4);
2107 for (i = 0; i < code->const_nr; i++) {
2108 const GLfloat *constant = get_fragmentprogram_constant(ctx, fp->Base, code->constant[i]);
2109 rmesa->hw.fpp.cmd[R300_FPP_PARAM_0 + 4 * i + 0] = r300PackFloat24(constant[0]);
2110 rmesa->hw.fpp.cmd[R300_FPP_PARAM_0 + 4 * i + 1] = r300PackFloat24(constant[1]);
2111 rmesa->hw.fpp.cmd[R300_FPP_PARAM_0 + 4 * i + 2] = r300PackFloat24(constant[2]);
2112 rmesa->hw.fpp.cmd[R300_FPP_PARAM_0 + 4 * i + 3] = r300PackFloat24(constant[3]);
2113 }
2114 }
2115
2116 #define bump_r500fp_count(ptr, new_count) do{\
2117 drm_r300_cmd_header_t* _p=((drm_r300_cmd_header_t*)(ptr));\
2118 int _nc=(new_count)/6; \
2119 assert(_nc < 256); \
2120 if(_nc>_p->r500fp.count)_p->r500fp.count=_nc;\
2121 } while(0)
2122
2123 #define bump_r500fp_const_count(ptr, new_count) do{\
2124 drm_r300_cmd_header_t* _p=((drm_r300_cmd_header_t*)(ptr));\
2125 int _nc=(new_count)/4; \
2126 assert(_nc < 256); \
2127 if(_nc>_p->r500fp.count)_p->r500fp.count=_nc;\
2128 } while(0)
2129
2130 static void r500SetupPixelShader(GLcontext *ctx)
2131 {
2132 r300ContextPtr rmesa = R300_CONTEXT(ctx);
2133 struct r300_fragment_program *fp = rmesa->selected_fp;
2134 int i;
2135 struct r500_fragment_program_code *code;
2136
2137 ((drm_r300_cmd_header_t *) rmesa->hw.r500fp.cmd)->r500fp.count = 0;
2138 ((drm_r300_cmd_header_t *) rmesa->hw.r500fp_const.cmd)->r500fp.count = 0;
2139
2140 code = &fp->code.r500;
2141
2142 R300_STATECHANGE(rmesa, fp);
2143 rmesa->hw.fp.cmd[R500_FP_PIXSIZE] = code->max_temp_idx;
2144
2145 rmesa->hw.fp.cmd[R500_FP_CODE_ADDR] =
2146 R500_US_CODE_START_ADDR(code->inst_offset) |
2147 R500_US_CODE_END_ADDR(code->inst_end);
2148 rmesa->hw.fp.cmd[R500_FP_CODE_RANGE] =
2149 R500_US_CODE_RANGE_ADDR(code->inst_offset) |
2150 R500_US_CODE_RANGE_SIZE(code->inst_end);
2151 rmesa->hw.fp.cmd[R500_FP_CODE_OFFSET] =
2152 R500_US_CODE_OFFSET_ADDR(0); /* FIXME when we add flow control */
2153
2154 R300_STATECHANGE(rmesa, r500fp);
2155 /* Emit our shader... */
2156 for (i = 0; i < code->inst_end+1; i++) {
2157 rmesa->hw.r500fp.cmd[i*6+1] = code->inst[i].inst0;
2158 rmesa->hw.r500fp.cmd[i*6+2] = code->inst[i].inst1;
2159 rmesa->hw.r500fp.cmd[i*6+3] = code->inst[i].inst2;
2160 rmesa->hw.r500fp.cmd[i*6+4] = code->inst[i].inst3;
2161 rmesa->hw.r500fp.cmd[i*6+5] = code->inst[i].inst4;
2162 rmesa->hw.r500fp.cmd[i*6+6] = code->inst[i].inst5;
2163 }
2164
2165 bump_r500fp_count(rmesa->hw.r500fp.cmd, (code->inst_end + 1) * 6);
2166
2167 R300_STATECHANGE(rmesa, r500fp_const);
2168 for (i = 0; i < code->const_nr; i++) {
2169 const GLfloat *constant = get_fragmentprogram_constant(ctx, fp->Base, code->constant[i]);
2170 rmesa->hw.r500fp_const.cmd[R300_FPP_PARAM_0 + 4 * i + 0] = r300PackFloat32(constant[0]);
2171 rmesa->hw.r500fp_const.cmd[R300_FPP_PARAM_0 + 4 * i + 1] = r300PackFloat32(constant[1]);
2172 rmesa->hw.r500fp_const.cmd[R300_FPP_PARAM_0 + 4 * i + 2] = r300PackFloat32(constant[2]);
2173 rmesa->hw.r500fp_const.cmd[R300_FPP_PARAM_0 + 4 * i + 3] = r300PackFloat32(constant[3]);
2174 }
2175 bump_r500fp_const_count(rmesa->hw.r500fp_const.cmd, code->const_nr * 4);
2176 }
2177
2178 void r300SetupVAP(GLcontext *ctx, GLuint InputsRead, GLuint OutputsWritten)
2179 {
2180 r300ContextPtr rmesa = R300_CONTEXT( ctx );
2181 struct vertex_attribute *attrs = rmesa->vbuf.attribs;
2182 int i, j, reg_count;
2183 uint32_t *vir0 = &rmesa->hw.vir[0].cmd[1];
2184 uint32_t *vir1 = &rmesa->hw.vir[1].cmd[1];
2185
2186 for (i = 0; i < R300_VIR_CMDSIZE-1; ++i)
2187 vir0[i] = vir1[i] = 0;
2188
2189 for (i = 0, j = 0; i < rmesa->vbuf.num_attribs; ++i) {
2190 int tmp;
2191
2192 tmp = attrs[i].data_type | (attrs[i].dst_loc << R300_DST_VEC_LOC_SHIFT);
2193 if (attrs[i]._signed)
2194 tmp |= R300_SIGNED;
2195 if (attrs[i].normalize)
2196 tmp |= R300_NORMALIZE;
2197
2198 if (i % 2 == 0) {
2199 vir0[j] = tmp << R300_DATA_TYPE_0_SHIFT;
2200 vir1[j] = attrs[i].swizzle | (attrs[i].write_mask << R300_WRITE_ENA_SHIFT);
2201 } else {
2202 vir0[j] |= tmp << R300_DATA_TYPE_1_SHIFT;
2203 vir1[j] |= (attrs[i].swizzle | (attrs[i].write_mask << R300_WRITE_ENA_SHIFT)) << R300_SWIZZLE1_SHIFT;
2204 ++j;
2205 }
2206 }
2207
2208 reg_count = (rmesa->vbuf.num_attribs + 1) >> 1;
2209 if (rmesa->vbuf.num_attribs % 2 != 0) {
2210 vir0[reg_count-1] |= R300_LAST_VEC << R300_DATA_TYPE_0_SHIFT;
2211 } else {
2212 vir0[reg_count-1] |= R300_LAST_VEC << R300_DATA_TYPE_1_SHIFT;
2213 }
2214
2215 R300_STATECHANGE(rmesa, vir[0]);
2216 R300_STATECHANGE(rmesa, vir[1]);
2217 R300_STATECHANGE(rmesa, vof);
2218 R300_STATECHANGE(rmesa, vic);
2219
2220 if (rmesa->radeon.radeonScreen->kernel_mm) {
2221 rmesa->hw.vir[0].cmd[0] &= 0xC000FFFF;
2222 rmesa->hw.vir[1].cmd[0] &= 0xC000FFFF;
2223 rmesa->hw.vir[0].cmd[0] |= (reg_count & 0x3FFF) << 16;
2224 rmesa->hw.vir[1].cmd[0] |= (reg_count & 0x3FFF) << 16;
2225 } else {
2226 ((drm_r300_cmd_header_t *) rmesa->hw.vir[0].cmd)->packet0.count = reg_count;
2227 ((drm_r300_cmd_header_t *) rmesa->hw.vir[1].cmd)->packet0.count = reg_count;
2228 }
2229
2230 rmesa->hw.vic.cmd[R300_VIC_CNTL_0] = r300VAPInputCntl0(ctx, InputsRead);
2231 rmesa->hw.vic.cmd[R300_VIC_CNTL_1] = r300VAPInputCntl1(ctx, InputsRead);
2232 rmesa->hw.vof.cmd[R300_VOF_CNTL_0] = r300VAPOutputCntl0(ctx, OutputsWritten);
2233 rmesa->hw.vof.cmd[R300_VOF_CNTL_1] = r300VAPOutputCntl1(ctx, OutputsWritten);
2234 }
2235
2236 void r300UpdateShaderStates(r300ContextPtr rmesa)
2237 {
2238 GLcontext *ctx;
2239 ctx = rmesa->radeon.glCtx;
2240
2241 /* should only happenen once, just after context is created */
2242 if (!ctx->FragmentProgram._Current)
2243 return;
2244
2245 r300SetEarlyZState(ctx);
2246
2247 r300SetupTextures(ctx);
2248
2249 rmesa->vtbl.SetupPixelShader(ctx);
2250
2251 rmesa->vtbl.SetupRSUnit(ctx);
2252
2253 if (rmesa->options.hw_tcl_enabled) {
2254 r300SetupVertexProgram(rmesa);
2255 }
2256 }
2257
2258 /**
2259 * Called by Mesa after an internal state update.
2260 */
2261 static void r300InvalidateState(GLcontext * ctx, GLuint new_state)
2262 {
2263 r300ContextPtr r300 = R300_CONTEXT(ctx);
2264
2265 _swrast_InvalidateState(ctx, new_state);
2266 _swsetup_InvalidateState(ctx, new_state);
2267 _vbo_InvalidateState(ctx, new_state);
2268 _tnl_InvalidateState(ctx, new_state);
2269
2270 if (new_state & _NEW_BUFFERS) {
2271 _mesa_update_framebuffer(ctx);
2272 /* this updates the DrawBuffer's Width/Height if it's a FBO */
2273 _mesa_update_draw_buffer_bounds(ctx);
2274
2275 R300_STATECHANGE(r300, cb);
2276 }
2277
2278 r300->radeon.NewGLState |= new_state;
2279 }
2280
2281 /**
2282 * Calculate initial hardware state and register state functions.
2283 * Assumes that the command buffer and state atoms have been
2284 * initialized already.
2285 */
2286 void r300InitState(r300ContextPtr r300)
2287 {
2288 r300ResetHwState(r300);
2289 }
2290
2291 static void r300RenderMode(GLcontext * ctx, GLenum mode)
2292 {
2293 r300SwitchFallback(ctx, R300_FALLBACK_RENDER_MODE, ctx->RenderMode != GL_RENDER);
2294 }
2295
2296 /**
2297 * Initialize driver's state callback functions
2298 */
2299 void r300InitStateFuncs(struct dd_function_table *functions)
2300 {
2301
2302 functions->UpdateState = r300InvalidateState;
2303 functions->AlphaFunc = r300AlphaFunc;
2304 functions->BlendColor = r300BlendColor;
2305 functions->BlendEquationSeparate = r300BlendEquationSeparate;
2306 functions->BlendFuncSeparate = r300BlendFuncSeparate;
2307 functions->Enable = r300Enable;
2308 functions->ColorMask = r300ColorMask;
2309 functions->DepthFunc = r300DepthFunc;
2310 functions->DepthMask = r300DepthMask;
2311 functions->CullFace = r300CullFace;
2312 functions->FrontFace = r300FrontFace;
2313 functions->ShadeModel = r300ShadeModel;
2314 functions->LogicOpcode = r300LogicOpcode;
2315
2316 /* ARB_point_parameters */
2317 functions->PointParameterfv = r300PointParameter;
2318
2319 /* Stencil related */
2320 functions->StencilFuncSeparate = r300StencilFuncSeparate;
2321 functions->StencilMaskSeparate = r300StencilMaskSeparate;
2322 functions->StencilOpSeparate = r300StencilOpSeparate;
2323
2324 /* Viewport related */
2325 functions->Viewport = r300Viewport;
2326 functions->DepthRange = r300DepthRange;
2327 functions->PointSize = r300PointSize;
2328 functions->LineWidth = r300LineWidth;
2329
2330 functions->PolygonOffset = r300PolygonOffset;
2331 functions->PolygonMode = r300PolygonMode;
2332
2333 functions->RenderMode = r300RenderMode;
2334
2335 functions->ClipPlane = r300ClipPlane;
2336 functions->Scissor = radeonScissor;
2337
2338 functions->DrawBuffer = radeonDrawBuffer;
2339 functions->ReadBuffer = radeonReadBuffer;
2340 }
2341
2342 void r300InitShaderFunctions(r300ContextPtr r300)
2343 {
2344 if (r300->radeon.radeonScreen->chip_family >= CHIP_FAMILY_RV515) {
2345 r300->vtbl.SetupRSUnit = r500SetupRSUnit;
2346 r300->vtbl.SetupPixelShader = r500SetupPixelShader;
2347 r300->vtbl.SetupFragmentShaderTextures = r500SetupFragmentShaderTextures;
2348 r300->vtbl.BuildFragmentProgramHwCode = r500BuildFragmentProgramHwCode;
2349 r300->vtbl.FragmentProgramDump = r500FragmentProgramDump;
2350 } else {
2351 r300->vtbl.SetupRSUnit = r300SetupRSUnit;
2352 r300->vtbl.SetupPixelShader = r300SetupPixelShader;
2353 r300->vtbl.SetupFragmentShaderTextures = r300SetupFragmentShaderTextures;
2354 r300->vtbl.BuildFragmentProgramHwCode = r300BuildFragmentProgramHwCode;
2355 r300->vtbl.FragmentProgramDump = r300FragmentProgramDump;
2356 }
2357 }