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