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