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