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