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