2 Copyright (C) The Weather Channel, Inc. 2002.
3 Copyright (C) 2004 Nicolai Haehnle.
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.
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:
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.
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.
30 **************************************************************************/
35 * \author Nicolai Haehnle <prefect_@gmx.net>
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"
45 #include "main/framebuffer.h"
46 #include "main/simple_list.h"
47 #include "main/api_arrayelt.h"
49 #include "drivers/common/meta.h"
50 #include "swrast/swrast.h"
51 #include "swrast_setup/swrast_setup.h"
52 #include "shader/prog_parameter.h"
53 #include "shader/prog_statevars.h"
56 #include "tnl/t_vp_build.h"
58 #include "r300_context.h"
59 #include "r300_state.h"
61 #include "r300_emit.h"
62 #include "r300_fragprog_common.h"
63 #include "r300_render.h"
64 #include "r300_vertprog.h"
66 static void r300BlendColor(GLcontext
* ctx
, const GLfloat cf
[4])
68 r300ContextPtr rmesa
= R300_CONTEXT(ctx
);
70 R300_STATECHANGE(rmesa
, blend_color
);
72 if (rmesa
->radeon
.radeonScreen
->chip_family
>= CHIP_FAMILY_RV515
) {
73 GLuint r
= IROUND(cf
[0]*1023.0f
);
74 GLuint g
= IROUND(cf
[1]*1023.0f
);
75 GLuint b
= IROUND(cf
[2]*1023.0f
);
76 GLuint a
= IROUND(cf
[3]*1023.0f
);
78 rmesa
->hw
.blend_color
.cmd
[1] = r
| (a
<< 16);
79 rmesa
->hw
.blend_color
.cmd
[2] = b
| (g
<< 16);
82 CLAMPED_FLOAT_TO_UBYTE(color
[0], cf
[0]);
83 CLAMPED_FLOAT_TO_UBYTE(color
[1], cf
[1]);
84 CLAMPED_FLOAT_TO_UBYTE(color
[2], cf
[2]);
85 CLAMPED_FLOAT_TO_UBYTE(color
[3], cf
[3]);
87 rmesa
->hw
.blend_color
.cmd
[1] = PACK_COLOR_8888(color
[3], color
[0],
93 * Calculate the hardware blend factor setting. This same function is used
94 * for source and destination of both alpha and RGB.
97 * The hardware register value for the specified blend factor. This value
98 * will need to be shifted into the correct position for either source or
102 * Since the two cases where source and destination are handled differently
103 * are essentially error cases, they should never happen. Determine if these
104 * cases can be removed.
106 static int blend_factor(GLenum factor
, GLboolean is_src
)
110 return R300_BLEND_GL_ZERO
;
113 return R300_BLEND_GL_ONE
;
116 return R300_BLEND_GL_DST_COLOR
;
118 case GL_ONE_MINUS_DST_COLOR
:
119 return R300_BLEND_GL_ONE_MINUS_DST_COLOR
;
122 return R300_BLEND_GL_SRC_COLOR
;
124 case GL_ONE_MINUS_SRC_COLOR
:
125 return R300_BLEND_GL_ONE_MINUS_SRC_COLOR
;
128 return R300_BLEND_GL_SRC_ALPHA
;
130 case GL_ONE_MINUS_SRC_ALPHA
:
131 return R300_BLEND_GL_ONE_MINUS_SRC_ALPHA
;
134 return R300_BLEND_GL_DST_ALPHA
;
136 case GL_ONE_MINUS_DST_ALPHA
:
137 return R300_BLEND_GL_ONE_MINUS_DST_ALPHA
;
139 case GL_SRC_ALPHA_SATURATE
:
140 return (is_src
) ? R300_BLEND_GL_SRC_ALPHA_SATURATE
:
143 case GL_CONSTANT_COLOR
:
144 return R300_BLEND_GL_CONST_COLOR
;
146 case GL_ONE_MINUS_CONSTANT_COLOR
:
147 return R300_BLEND_GL_ONE_MINUS_CONST_COLOR
;
149 case GL_CONSTANT_ALPHA
:
150 return R300_BLEND_GL_CONST_ALPHA
;
152 case GL_ONE_MINUS_CONSTANT_ALPHA
:
153 return R300_BLEND_GL_ONE_MINUS_CONST_ALPHA
;
156 fprintf(stderr
, "unknown blend factor %x\n", factor
);
157 return (is_src
) ? R300_BLEND_GL_ONE
: R300_BLEND_GL_ZERO
;
163 * Sets both the blend equation and the blend function.
164 * This is done in a single
165 * function because some blend equations (i.e., \c GL_MIN and \c GL_MAX)
166 * change the interpretation of the blend function.
167 * Also, make sure that blend function and blend equation are set to their
168 * default value if color blending is not enabled, since at least blend
169 * equations GL_MIN and GL_FUNC_REVERSE_SUBTRACT will cause wrong results
170 * otherwise for unknown reasons.
173 /* helper function */
174 static void r300SetBlendCntl(r300ContextPtr r300
, int func
, int eqn
,
175 int cbits
, int funcA
, int eqnA
)
177 GLuint new_ablend
, new_cblend
;
181 "eqnA=%08x funcA=%08x eqn=%08x func=%08x cbits=%08x\n",
182 eqnA
, funcA
, eqn
, func
, cbits
);
184 new_ablend
= eqnA
| funcA
;
185 new_cblend
= eqn
| func
;
187 /* Some blend factor combinations don't seem to work when the
188 * BLEND_NO_SEPARATE bit is set.
190 * Especially problematic candidates are the ONE_MINUS_* flags,
191 * but I can't see a real pattern.
194 if (new_ablend
== new_cblend
) {
195 new_cblend
|= R300_DISCARD_SRC_PIXELS_SRC_ALPHA_0
;
200 if ((new_ablend
!= r300
->hw
.bld
.cmd
[R300_BLD_ABLEND
]) ||
201 (new_cblend
!= r300
->hw
.bld
.cmd
[R300_BLD_CBLEND
])) {
202 R300_STATECHANGE(r300
, bld
);
203 r300
->hw
.bld
.cmd
[R300_BLD_ABLEND
] = new_ablend
;
204 r300
->hw
.bld
.cmd
[R300_BLD_CBLEND
] = new_cblend
;
208 static void r300SetBlendState(GLcontext
* ctx
)
210 r300ContextPtr r300
= R300_CONTEXT(ctx
);
211 int func
= (R300_BLEND_GL_ONE
<< R300_SRC_BLEND_SHIFT
) |
212 (R300_BLEND_GL_ZERO
<< R300_DST_BLEND_SHIFT
);
213 int eqn
= R300_COMB_FCN_ADD_CLAMP
;
214 int funcA
= (R300_BLEND_GL_ONE
<< R300_SRC_BLEND_SHIFT
) |
215 (R300_BLEND_GL_ZERO
<< R300_DST_BLEND_SHIFT
);
216 int eqnA
= R300_COMB_FCN_ADD_CLAMP
;
218 if (RGBA_LOGICOP_ENABLED(ctx
) || !ctx
->Color
.BlendEnabled
) {
219 r300SetBlendCntl(r300
, func
, eqn
, 0, func
, eqn
);
224 (blend_factor(ctx
->Color
.BlendSrcRGB
, GL_TRUE
) <<
225 R300_SRC_BLEND_SHIFT
) | (blend_factor(ctx
->Color
.BlendDstRGB
,
227 R300_DST_BLEND_SHIFT
);
229 switch (ctx
->Color
.BlendEquationRGB
) {
231 eqn
= R300_COMB_FCN_ADD_CLAMP
;
234 case GL_FUNC_SUBTRACT
:
235 eqn
= R300_COMB_FCN_SUB_CLAMP
;
238 case GL_FUNC_REVERSE_SUBTRACT
:
239 eqn
= R300_COMB_FCN_RSUB_CLAMP
;
243 eqn
= R300_COMB_FCN_MIN
;
244 func
= (R300_BLEND_GL_ONE
<< R300_SRC_BLEND_SHIFT
) |
245 (R300_BLEND_GL_ONE
<< R300_DST_BLEND_SHIFT
);
249 eqn
= R300_COMB_FCN_MAX
;
250 func
= (R300_BLEND_GL_ONE
<< R300_SRC_BLEND_SHIFT
) |
251 (R300_BLEND_GL_ONE
<< R300_DST_BLEND_SHIFT
);
256 "[%s:%u] Invalid RGB blend equation (0x%04x).\n",
257 __FUNCTION__
, __LINE__
, ctx
->Color
.BlendEquationRGB
);
262 (blend_factor(ctx
->Color
.BlendSrcA
, GL_TRUE
) <<
263 R300_SRC_BLEND_SHIFT
) | (blend_factor(ctx
->Color
.BlendDstA
,
265 R300_DST_BLEND_SHIFT
);
267 switch (ctx
->Color
.BlendEquationA
) {
269 eqnA
= R300_COMB_FCN_ADD_CLAMP
;
272 case GL_FUNC_SUBTRACT
:
273 eqnA
= R300_COMB_FCN_SUB_CLAMP
;
276 case GL_FUNC_REVERSE_SUBTRACT
:
277 eqnA
= R300_COMB_FCN_RSUB_CLAMP
;
281 eqnA
= R300_COMB_FCN_MIN
;
282 funcA
= (R300_BLEND_GL_ONE
<< R300_SRC_BLEND_SHIFT
) |
283 (R300_BLEND_GL_ONE
<< R300_DST_BLEND_SHIFT
);
287 eqnA
= R300_COMB_FCN_MAX
;
288 funcA
= (R300_BLEND_GL_ONE
<< R300_SRC_BLEND_SHIFT
) |
289 (R300_BLEND_GL_ONE
<< R300_DST_BLEND_SHIFT
);
294 "[%s:%u] Invalid A blend equation (0x%04x).\n",
295 __FUNCTION__
, __LINE__
, ctx
->Color
.BlendEquationA
);
299 r300SetBlendCntl(r300
,
301 (R300_SEPARATE_ALPHA_ENABLE
|
303 R300_ALPHA_BLEND_ENABLE
), funcA
, eqnA
);
306 static void r300BlendEquationSeparate(GLcontext
* ctx
,
307 GLenum modeRGB
, GLenum modeA
)
309 r300SetBlendState(ctx
);
312 static void r300BlendFuncSeparate(GLcontext
* ctx
,
313 GLenum sfactorRGB
, GLenum dfactorRGB
,
314 GLenum sfactorA
, GLenum dfactorA
)
316 r300SetBlendState(ctx
);
320 * Translate LogicOp enums into hardware representation.
321 * Both use a very logical bit-wise layout, but unfortunately the order
322 * of bits is reversed.
324 static GLuint
translate_logicop(GLenum logicop
)
326 GLuint bits
= logicop
- GL_CLEAR
;
327 bits
= ((bits
& 1) << 3) | ((bits
& 2) << 1) | ((bits
& 4) >> 1) | ((bits
& 8) >> 3);
328 return bits
<< R300_RB3D_ROPCNTL_ROP_SHIFT
;
332 * Used internally to update the r300->hw hardware state to match the
333 * current OpenGL state.
335 static void r300SetLogicOpState(GLcontext
*ctx
)
337 r300ContextPtr r300
= R300_CONTEXT(ctx
);
338 R300_STATECHANGE(r300
, rop
);
339 if (RGBA_LOGICOP_ENABLED(ctx
)) {
340 r300
->hw
.rop
.cmd
[1] = R300_RB3D_ROPCNTL_ROP_ENABLE
|
341 translate_logicop(ctx
->Color
.LogicOp
);
343 r300
->hw
.rop
.cmd
[1] = 0;
348 * Called by Mesa when an application program changes the LogicOp state
351 static void r300LogicOpcode(GLcontext
*ctx
, GLenum logicop
)
353 if (RGBA_LOGICOP_ENABLED(ctx
))
354 r300SetLogicOpState(ctx
);
357 static void r300ClipPlane( GLcontext
*ctx
, GLenum plane
, const GLfloat
*eq
)
359 r300ContextPtr rmesa
= R300_CONTEXT(ctx
);
363 /* no VAP UCP on non-TCL chipsets */
364 if (!rmesa
->options
.hw_tcl_enabled
)
367 p
= (GLint
) plane
- (GLint
) GL_CLIP_PLANE0
;
368 ip
= (GLint
*)ctx
->Transform
._ClipUserPlane
[p
];
370 R300_STATECHANGE( rmesa
, vpucp
[p
] );
371 rmesa
->hw
.vpucp
[p
].cmd
[R300_VPUCP_X
] = ip
[0];
372 rmesa
->hw
.vpucp
[p
].cmd
[R300_VPUCP_Y
] = ip
[1];
373 rmesa
->hw
.vpucp
[p
].cmd
[R300_VPUCP_Z
] = ip
[2];
374 rmesa
->hw
.vpucp
[p
].cmd
[R300_VPUCP_W
] = ip
[3];
377 static void r300SetClipPlaneState(GLcontext
* ctx
, GLenum cap
, GLboolean state
)
379 r300ContextPtr r300
= R300_CONTEXT(ctx
);
382 /* no VAP UCP on non-TCL chipsets */
383 if (!r300
->options
.hw_tcl_enabled
)
386 p
= cap
- GL_CLIP_PLANE0
;
387 R300_STATECHANGE(r300
, vap_clip_cntl
);
389 r300
->hw
.vap_clip_cntl
.cmd
[1] |= (R300_VAP_UCP_ENABLE_0
<< p
);
390 r300ClipPlane(ctx
, cap
, NULL
);
392 r300
->hw
.vap_clip_cntl
.cmd
[1] &= ~(R300_VAP_UCP_ENABLE_0
<< p
);
397 * Update our tracked culling state based on Mesa's state.
399 static void r300UpdateCulling(GLcontext
* ctx
)
401 r300ContextPtr r300
= R300_CONTEXT(ctx
);
404 if (ctx
->Polygon
.CullFlag
) {
405 switch (ctx
->Polygon
.CullFaceMode
) {
407 val
= R300_CULL_FRONT
;
410 val
= R300_CULL_BACK
;
412 case GL_FRONT_AND_BACK
:
413 val
= R300_CULL_FRONT
| R300_CULL_BACK
;
420 switch (ctx
->Polygon
.FrontFace
) {
422 val
|= R300_FRONT_FACE_CW
;
425 val
|= R300_FRONT_FACE_CCW
;
431 /* Winding is inverted when rendering to FBO */
432 if (ctx
->DrawBuffer
&& ctx
->DrawBuffer
->Name
)
433 val
^= R300_FRONT_FACE_CW
;
435 R300_STATECHANGE(r300
, cul
);
436 r300
->hw
.cul
.cmd
[R300_CUL_CULL
] = val
;
439 static void r300SetPolygonOffsetState(GLcontext
* ctx
, GLboolean state
)
441 r300ContextPtr r300
= R300_CONTEXT(ctx
);
443 R300_STATECHANGE(r300
, occlusion_cntl
);
445 r300
->hw
.occlusion_cntl
.cmd
[1] |= (3 << 0);
447 r300
->hw
.occlusion_cntl
.cmd
[1] &= ~(3 << 0);
451 static GLboolean
current_fragment_program_writes_depth(GLcontext
* ctx
)
453 r300ContextPtr r300
= R300_CONTEXT(ctx
);
455 return ctx
->FragmentProgram
._Current
&& r300
->selected_fp
->code
.writes_depth
;
458 static void r300SetEarlyZState(GLcontext
* ctx
)
460 r300ContextPtr r300
= R300_CONTEXT(ctx
);
461 GLuint topZ
= R300_ZTOP_ENABLE
;
462 GLuint w_fmt
, fgdepthsrc
;
464 if (ctx
->Color
.AlphaEnabled
&& ctx
->Color
.AlphaFunc
!= GL_ALWAYS
)
465 topZ
= R300_ZTOP_DISABLE
;
466 else if (current_fragment_program_writes_depth(ctx
))
467 topZ
= R300_ZTOP_DISABLE
;
468 else if (ctx
->FragmentProgram
._Current
&& ctx
->FragmentProgram
._Current
->UsesKill
)
469 topZ
= R300_ZTOP_DISABLE
;
470 else if (r300
->radeon
.query
.current
)
471 topZ
= R300_ZTOP_DISABLE
;
473 if (topZ
!= r300
->hw
.zstencil_format
.cmd
[2]) {
474 /* Note: This completely reemits the stencil format.
475 * I have not tested whether this is strictly necessary,
476 * or if emitting a write to ZB_ZTOP is enough.
478 R300_STATECHANGE(r300
, zstencil_format
);
479 r300
->hw
.zstencil_format
.cmd
[2] = topZ
;
482 /* w_fmt value is set to get best performance
483 * see p.130 R5xx 3D acceleration guide v1.3 */
484 if (current_fragment_program_writes_depth(ctx
)) {
485 fgdepthsrc
= R300_FG_DEPTH_SRC_SHADER
;
486 w_fmt
= R300_W_FMT_W24
| R300_W_SRC_US
;
488 fgdepthsrc
= R300_FG_DEPTH_SRC_SCAN
;
489 w_fmt
= R300_W_FMT_W0
| R300_W_SRC_US
;
492 if (w_fmt
!= r300
->hw
.us_out_fmt
.cmd
[5]) {
493 R300_STATECHANGE(r300
, us_out_fmt
);
494 r300
->hw
.us_out_fmt
.cmd
[5] = w_fmt
;
497 if (fgdepthsrc
!= r300
->hw
.fg_depth_src
.cmd
[1]) {
498 R300_STATECHANGE(r300
, fg_depth_src
);
499 r300
->hw
.fg_depth_src
.cmd
[1] = fgdepthsrc
;
503 static void r300SetAlphaState(GLcontext
* ctx
)
505 r300ContextPtr r300
= R300_CONTEXT(ctx
);
507 uint32_t pp_misc
= 0x0;
508 GLboolean really_enabled
= ctx
->Color
.AlphaEnabled
;
510 CLAMPED_FLOAT_TO_UBYTE(refByte
, ctx
->Color
.AlphaRef
);
512 switch (ctx
->Color
.AlphaFunc
) {
514 pp_misc
|= R300_FG_ALPHA_FUNC_NEVER
;
517 pp_misc
|= R300_FG_ALPHA_FUNC_LESS
;
520 pp_misc
|= R300_FG_ALPHA_FUNC_EQUAL
;
523 pp_misc
|= R300_FG_ALPHA_FUNC_LE
;
526 pp_misc
|= R300_FG_ALPHA_FUNC_GREATER
;
529 pp_misc
|= R300_FG_ALPHA_FUNC_NOTEQUAL
;
532 pp_misc
|= R300_FG_ALPHA_FUNC_GE
;
535 /*pp_misc |= FG_ALPHA_FUNC_ALWAYS; */
536 really_enabled
= GL_FALSE
;
540 if (really_enabled
) {
541 pp_misc
|= R300_FG_ALPHA_FUNC_ENABLE
;
542 pp_misc
|= R500_FG_ALPHA_FUNC_8BIT
;
543 pp_misc
|= (refByte
& R300_FG_ALPHA_FUNC_VAL_MASK
);
548 R300_STATECHANGE(r300
, at
);
549 r300
->hw
.at
.cmd
[R300_AT_ALPHA_TEST
] = pp_misc
;
550 r300
->hw
.at
.cmd
[R300_AT_UNKNOWN
] = 0;
553 static void r300AlphaFunc(GLcontext
* ctx
, GLenum func
, GLfloat ref
)
557 r300SetAlphaState(ctx
);
560 static int translate_func(int func
)
564 return R300_ZS_NEVER
;
568 return R300_ZS_EQUAL
;
570 return R300_ZS_LEQUAL
;
572 return R300_ZS_GREATER
;
574 return R300_ZS_NOTEQUAL
;
576 return R300_ZS_GEQUAL
;
578 return R300_ZS_ALWAYS
;
583 static void r300SetDepthState(GLcontext
* ctx
)
585 r300ContextPtr r300
= R300_CONTEXT(ctx
);
587 R300_STATECHANGE(r300
, zs
);
588 r300
->hw
.zs
.cmd
[R300_ZS_CNTL_0
] &= (R300_STENCIL_ENABLE
|
589 R300_STENCIL_FRONT_BACK
|
590 R500_STENCIL_REFMASK_FRONT_BACK
);
591 r300
->hw
.zs
.cmd
[R300_ZS_CNTL_1
] &= ~(R300_ZS_MASK
<< R300_Z_FUNC_SHIFT
);
593 if (ctx
->Depth
.Test
&& ctx
->DrawBuffer
->_DepthBuffer
) {
594 r300
->hw
.zs
.cmd
[R300_ZS_CNTL_0
] |= R300_Z_ENABLE
;
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
;
602 static void r300CatchStencilFallback(GLcontext
*ctx
)
604 r300ContextPtr rmesa
= R300_CONTEXT(ctx
);
605 const unsigned back
= ctx
->Stencil
._BackFace
;
607 if (rmesa
->radeon
.radeonScreen
->kernel_mm
&&
608 (rmesa
->radeon
.radeonScreen
->chip_family
>= CHIP_FAMILY_RV515
)) {
609 r300SwitchFallback(ctx
, R300_FALLBACK_STENCIL_TWOSIDE
, GL_FALSE
);
610 } else if (ctx
->Stencil
._Enabled
&&
611 (ctx
->Stencil
.Ref
[0] != ctx
->Stencil
.Ref
[back
]
612 || ctx
->Stencil
.ValueMask
[0] != ctx
->Stencil
.ValueMask
[back
]
613 || ctx
->Stencil
.WriteMask
[0] != ctx
->Stencil
.WriteMask
[back
])) {
614 r300SwitchFallback(ctx
, R300_FALLBACK_STENCIL_TWOSIDE
, GL_TRUE
);
616 r300SwitchFallback(ctx
, R300_FALLBACK_STENCIL_TWOSIDE
, GL_FALSE
);
620 static void r300SetStencilState(GLcontext
* ctx
, GLboolean state
)
622 r300ContextPtr r300
= R300_CONTEXT(ctx
);
623 GLboolean hw_stencil
= GL_FALSE
;
625 r300CatchStencilFallback(ctx
);
627 if (ctx
->DrawBuffer
) {
628 struct radeon_renderbuffer
*rrbStencil
629 = radeon_get_renderbuffer(ctx
->DrawBuffer
, BUFFER_STENCIL
);
630 hw_stencil
= (rrbStencil
&& rrbStencil
->bo
);
634 R300_STATECHANGE(r300
, zs
);
636 r300
->hw
.zs
.cmd
[R300_ZS_CNTL_0
] |=
639 r300
->hw
.zs
.cmd
[R300_ZS_CNTL_0
] &=
640 ~R300_STENCIL_ENABLE
;
645 static void r300UpdatePolygonMode(GLcontext
* ctx
)
647 r300ContextPtr r300
= R300_CONTEXT(ctx
);
648 uint32_t hw_mode
= R300_GA_POLY_MODE_DISABLE
;
650 /* Only do something if a polygon mode is wanted, default is GL_FILL */
651 if (ctx
->Polygon
.FrontMode
!= GL_FILL
||
652 ctx
->Polygon
.BackMode
!= GL_FILL
) {
655 /* Handle GL_CW (clock wise and GL_CCW (counter clock wise)
656 * correctly by selecting the correct front and back face
658 if (ctx
->Polygon
.FrontFace
== GL_CCW
) {
659 f
= ctx
->Polygon
.FrontMode
;
660 b
= ctx
->Polygon
.BackMode
;
662 f
= ctx
->Polygon
.BackMode
;
663 b
= ctx
->Polygon
.FrontMode
;
666 /* Enable polygon mode */
667 hw_mode
|= R300_GA_POLY_MODE_DUAL
;
671 hw_mode
|= R300_GA_POLY_MODE_FRONT_PTYPE_LINE
;
674 hw_mode
|= R300_GA_POLY_MODE_FRONT_PTYPE_POINT
;
677 hw_mode
|= R300_GA_POLY_MODE_FRONT_PTYPE_TRI
;
683 hw_mode
|= R300_GA_POLY_MODE_BACK_PTYPE_LINE
;
686 hw_mode
|= R300_GA_POLY_MODE_BACK_PTYPE_POINT
;
689 hw_mode
|= R300_GA_POLY_MODE_BACK_PTYPE_TRI
;
694 if (r300
->hw
.polygon_mode
.cmd
[1] != hw_mode
) {
695 R300_STATECHANGE(r300
, polygon_mode
);
696 r300
->hw
.polygon_mode
.cmd
[1] = hw_mode
;
699 r300
->hw
.polygon_mode
.cmd
[2] = 0x00000001;
700 r300
->hw
.polygon_mode
.cmd
[3] = 0x00000000;
704 * Change the culling mode.
706 * \note Mesa already filters redundant calls to this function.
708 static void r300CullFace(GLcontext
* ctx
, GLenum mode
)
712 r300UpdateCulling(ctx
);
716 * Change the polygon orientation.
718 * \note Mesa already filters redundant calls to this function.
720 static void r300FrontFace(GLcontext
* ctx
, GLenum mode
)
724 r300UpdateCulling(ctx
);
725 r300UpdatePolygonMode(ctx
);
729 * Change the depth testing function.
731 * \note Mesa already filters redundant calls to this function.
733 static void r300DepthFunc(GLcontext
* ctx
, GLenum func
)
736 r300SetDepthState(ctx
);
740 * Enable/Disable depth writing.
742 * \note Mesa already filters redundant calls to this function.
744 static void r300DepthMask(GLcontext
* ctx
, GLboolean mask
)
747 r300SetDepthState(ctx
);
751 * Handle glColorMask()
753 static void r300ColorMask(GLcontext
* ctx
,
754 GLboolean r
, GLboolean g
, GLboolean b
, GLboolean a
)
756 r300ContextPtr r300
= R300_CONTEXT(ctx
);
757 int mask
= (r
? RB3D_COLOR_CHANNEL_MASK_RED_MASK0
: 0) |
758 (g
? RB3D_COLOR_CHANNEL_MASK_GREEN_MASK0
: 0) |
759 (b
? RB3D_COLOR_CHANNEL_MASK_BLUE_MASK0
: 0) |
760 (a
? RB3D_COLOR_CHANNEL_MASK_ALPHA_MASK0
: 0);
762 if (mask
!= r300
->hw
.cmk
.cmd
[R300_CMK_COLORMASK
]) {
763 R300_STATECHANGE(r300
, cmk
);
764 r300
->hw
.cmk
.cmd
[R300_CMK_COLORMASK
] = mask
;
768 /* =============================================================
771 static void r300PointSize(GLcontext
* ctx
, GLfloat size
)
773 r300ContextPtr r300
= R300_CONTEXT(ctx
);
775 /* We need to clamp to user defined range here, because
776 * the HW clamping happens only for per vertex point size. */
777 size
= CLAMP(size
, ctx
->Point
.MinSize
, ctx
->Point
.MaxSize
);
779 /* same size limits for AA, non-AA points */
780 size
= CLAMP(size
, ctx
->Const
.MinPointSize
, ctx
->Const
.MaxPointSize
);
782 R300_STATECHANGE(r300
, ps
);
783 r300
->hw
.ps
.cmd
[R300_PS_POINTSIZE
] =
784 ((int)(size
* 6) << R300_POINTSIZE_X_SHIFT
) |
785 ((int)(size
* 6) << R300_POINTSIZE_Y_SHIFT
);
788 static void r300PointParameter(GLcontext
* ctx
, GLenum pname
, const GLfloat
* param
)
790 r300ContextPtr r300
= R300_CONTEXT(ctx
);
793 case GL_POINT_SIZE_MIN
:
794 R300_STATECHANGE(r300
, ga_point_minmax
);
795 r300
->hw
.ga_point_minmax
.cmd
[1] &= ~R300_GA_POINT_MINMAX_MIN_MASK
;
796 r300
->hw
.ga_point_minmax
.cmd
[1] |= (GLuint
)(ctx
->Point
.MinSize
* 6.0);
797 r300PointSize(ctx
, ctx
->Point
.Size
);
799 case GL_POINT_SIZE_MAX
:
800 R300_STATECHANGE(r300
, ga_point_minmax
);
801 r300
->hw
.ga_point_minmax
.cmd
[1] &= ~R300_GA_POINT_MINMAX_MAX_MASK
;
802 r300
->hw
.ga_point_minmax
.cmd
[1] |= (GLuint
)(ctx
->Point
.MaxSize
* 6.0)
803 << R300_GA_POINT_MINMAX_MAX_SHIFT
;
804 r300PointSize(ctx
, ctx
->Point
.Size
);
806 case GL_POINT_DISTANCE_ATTENUATION
:
808 case GL_POINT_FADE_THRESHOLD_SIZE
:
815 /* =============================================================
818 static void r300LineWidth(GLcontext
* ctx
, GLfloat widthf
)
820 r300ContextPtr r300
= R300_CONTEXT(ctx
);
822 widthf
= CLAMP(widthf
,
823 ctx
->Const
.MinPointSize
,
824 ctx
->Const
.MaxPointSize
);
825 R300_STATECHANGE(r300
, lcntl
);
826 r300
->hw
.lcntl
.cmd
[1] =
827 R300_LINE_CNT_HO
| R300_LINE_CNT_VE
| (int)(widthf
* 6.0);
830 static void r300PolygonMode(GLcontext
* ctx
, GLenum face
, GLenum mode
)
835 r300UpdatePolygonMode(ctx
);
838 /* =============================================================
842 static int translate_stencil_op(int op
)
850 return R300_ZS_REPLACE
;
855 case GL_INCR_WRAP_EXT
:
856 return R300_ZS_INCR_WRAP
;
857 case GL_DECR_WRAP_EXT
:
858 return R300_ZS_DECR_WRAP
;
860 return R300_ZS_INVERT
;
862 WARN_ONCE("Do not know how to translate stencil op");
868 static void r300ShadeModel(GLcontext
* ctx
, GLenum mode
)
870 r300ContextPtr rmesa
= R300_CONTEXT(ctx
);
872 R300_STATECHANGE(rmesa
, shade
);
873 rmesa
->hw
.shade
.cmd
[1] = 0x00000002;
874 R300_STATECHANGE(rmesa
, shade2
);
877 rmesa
->hw
.shade2
.cmd
[1] = R300_RE_SHADE_MODEL_FLAT
;
880 rmesa
->hw
.shade2
.cmd
[1] = R300_RE_SHADE_MODEL_SMOOTH
;
885 rmesa
->hw
.shade2
.cmd
[2] = 0x00000000;
886 rmesa
->hw
.shade2
.cmd
[3] = 0x00000000;
889 static void r300StencilFuncSeparate(GLcontext
* ctx
, GLenum face
,
890 GLenum func
, GLint ref
, GLuint mask
)
892 r300ContextPtr rmesa
= R300_CONTEXT(ctx
);
895 const unsigned back
= ctx
->Stencil
._BackFace
;
897 r300CatchStencilFallback(ctx
);
899 refmask
= ((ctx
->Stencil
.Ref
[0] & 0xff) << R300_STENCILREF_SHIFT
)
900 | ((ctx
->Stencil
.ValueMask
[0] & 0xff) << R300_STENCILMASK_SHIFT
);
902 R300_STATECHANGE(rmesa
, zs
);
903 rmesa
->hw
.zs
.cmd
[R300_ZS_CNTL_0
] |= R300_STENCIL_FRONT_BACK
;
904 rmesa
->hw
.zs
.cmd
[R300_ZS_CNTL_1
] &= ~((R300_ZS_MASK
<<
905 R300_S_FRONT_FUNC_SHIFT
)
907 R300_S_BACK_FUNC_SHIFT
));
909 rmesa
->hw
.zs
.cmd
[R300_ZS_CNTL_2
] &=
910 ~((R300_STENCILREF_MASK
<< R300_STENCILREF_SHIFT
) |
911 (R300_STENCILREF_MASK
<< R300_STENCILMASK_SHIFT
));
913 flag
= translate_func(ctx
->Stencil
.Function
[0]);
914 rmesa
->hw
.zs
.cmd
[R300_ZS_CNTL_1
] |=
915 (flag
<< R300_S_FRONT_FUNC_SHIFT
);
917 flag
= translate_func(ctx
->Stencil
.Function
[back
]);
919 rmesa
->hw
.zs
.cmd
[R300_ZS_CNTL_1
] |=
920 (flag
<< R300_S_BACK_FUNC_SHIFT
);
921 rmesa
->hw
.zs
.cmd
[R300_ZS_CNTL_2
] |= refmask
;
923 if (rmesa
->radeon
.radeonScreen
->chip_family
>= CHIP_FAMILY_RV515
) {
924 rmesa
->hw
.zs
.cmd
[R300_ZS_CNTL_0
] |= R500_STENCIL_REFMASK_FRONT_BACK
;
925 R300_STATECHANGE(rmesa
, zsb
);
926 refmask
= ((ctx
->Stencil
.Ref
[back
] & 0xff) << R300_STENCILREF_SHIFT
)
927 | ((ctx
->Stencil
.ValueMask
[back
] & 0xff) << R300_STENCILMASK_SHIFT
);
929 rmesa
->hw
.zsb
.cmd
[R300_ZSB_CNTL_0
] &=
930 ~((R300_STENCILREF_MASK
<< R300_STENCILREF_SHIFT
) |
931 (R300_STENCILREF_MASK
<< R300_STENCILMASK_SHIFT
));
932 rmesa
->hw
.zsb
.cmd
[R300_ZSB_CNTL_0
] |= refmask
;
936 static void r300StencilMaskSeparate(GLcontext
* ctx
, GLenum face
, GLuint mask
)
938 r300ContextPtr rmesa
= R300_CONTEXT(ctx
);
939 const unsigned back
= ctx
->Stencil
._BackFace
;
941 r300CatchStencilFallback(ctx
);
943 R300_STATECHANGE(rmesa
, zs
);
944 rmesa
->hw
.zs
.cmd
[R300_ZS_CNTL_2
] &=
945 ~(R300_STENCILREF_MASK
<<
946 R300_STENCILWRITEMASK_SHIFT
);
947 rmesa
->hw
.zs
.cmd
[R300_ZS_CNTL_2
] |=
949 WriteMask
[0] & R300_STENCILREF_MASK
) <<
950 R300_STENCILWRITEMASK_SHIFT
;
951 if (rmesa
->radeon
.radeonScreen
->chip_family
>= CHIP_FAMILY_RV515
) {
952 R300_STATECHANGE(rmesa
, zsb
);
953 rmesa
->hw
.zsb
.cmd
[R300_ZSB_CNTL_0
] |=
955 WriteMask
[back
] & R300_STENCILREF_MASK
) <<
956 R300_STENCILWRITEMASK_SHIFT
;
960 static void r300StencilOpSeparate(GLcontext
* ctx
, GLenum face
,
961 GLenum fail
, GLenum zfail
, GLenum zpass
)
963 r300ContextPtr rmesa
= R300_CONTEXT(ctx
);
964 const unsigned back
= ctx
->Stencil
._BackFace
;
966 r300CatchStencilFallback(ctx
);
968 R300_STATECHANGE(rmesa
, zs
);
969 /* It is easier to mask what's left.. */
970 rmesa
->hw
.zs
.cmd
[R300_ZS_CNTL_1
] &=
971 (R300_ZS_MASK
<< R300_Z_FUNC_SHIFT
) |
972 (R300_ZS_MASK
<< R300_S_FRONT_FUNC_SHIFT
) |
973 (R300_ZS_MASK
<< R300_S_BACK_FUNC_SHIFT
);
975 rmesa
->hw
.zs
.cmd
[R300_ZS_CNTL_1
] |=
976 (translate_stencil_op(ctx
->Stencil
.FailFunc
[0]) <<
977 R300_S_FRONT_SFAIL_OP_SHIFT
)
978 | (translate_stencil_op(ctx
->Stencil
.ZFailFunc
[0]) <<
979 R300_S_FRONT_ZFAIL_OP_SHIFT
)
980 | (translate_stencil_op(ctx
->Stencil
.ZPassFunc
[0]) <<
981 R300_S_FRONT_ZPASS_OP_SHIFT
);
983 rmesa
->hw
.zs
.cmd
[R300_ZS_CNTL_1
] |=
984 (translate_stencil_op(ctx
->Stencil
.FailFunc
[back
]) <<
985 R300_S_BACK_SFAIL_OP_SHIFT
)
986 | (translate_stencil_op(ctx
->Stencil
.ZFailFunc
[back
]) <<
987 R300_S_BACK_ZFAIL_OP_SHIFT
)
988 | (translate_stencil_op(ctx
->Stencil
.ZPassFunc
[back
]) <<
989 R300_S_BACK_ZPASS_OP_SHIFT
);
992 /* =============================================================
993 * Window position and viewport transformation
996 static void r300UpdateWindow(GLcontext
* ctx
)
998 r300ContextPtr rmesa
= R300_CONTEXT(ctx
);
999 __DRIdrawable
*dPriv
= radeon_get_drawable(&rmesa
->radeon
);
1000 GLfloat xoffset
= dPriv
? (GLfloat
) dPriv
->x
: 0;
1001 GLfloat yoffset
= dPriv
? (GLfloat
) dPriv
->y
+ dPriv
->h
: 0;
1002 const GLfloat
*v
= ctx
->Viewport
._WindowMap
.m
;
1003 const GLfloat depthScale
= 1.0F
/ ctx
->DrawBuffer
->_DepthMaxF
;
1004 const GLboolean render_to_fbo
= (ctx
->DrawBuffer
->Name
!= 0);
1005 GLfloat y_scale
, y_bias
;
1007 if (render_to_fbo
) {
1015 GLfloat sx
= v
[MAT_SX
];
1016 GLfloat tx
= v
[MAT_TX
] + xoffset
;
1017 GLfloat sy
= v
[MAT_SY
] * y_scale
;
1018 GLfloat ty
= (v
[MAT_TY
] * y_scale
) + y_bias
;
1019 GLfloat sz
= v
[MAT_SZ
] * depthScale
;
1020 GLfloat tz
= v
[MAT_TZ
] * depthScale
;
1022 R300_STATECHANGE(rmesa
, vpt
);
1024 rmesa
->hw
.vpt
.cmd
[R300_VPT_XSCALE
] = r300PackFloat32(sx
);
1025 rmesa
->hw
.vpt
.cmd
[R300_VPT_XOFFSET
] = r300PackFloat32(tx
);
1026 rmesa
->hw
.vpt
.cmd
[R300_VPT_YSCALE
] = r300PackFloat32(sy
);
1027 rmesa
->hw
.vpt
.cmd
[R300_VPT_YOFFSET
] = r300PackFloat32(ty
);
1028 rmesa
->hw
.vpt
.cmd
[R300_VPT_ZSCALE
] = r300PackFloat32(sz
);
1029 rmesa
->hw
.vpt
.cmd
[R300_VPT_ZOFFSET
] = r300PackFloat32(tz
);
1032 static void r300Viewport(GLcontext
* ctx
, GLint x
, GLint y
,
1033 GLsizei width
, GLsizei height
)
1035 /* Don't pipeline viewport changes, conflict with window offset
1036 * setting below. Could apply deltas to rescue pipelined viewport
1037 * values, or keep the originals hanging around.
1039 r300UpdateWindow(ctx
);
1041 radeon_viewport(ctx
, x
, y
, width
, height
);
1044 static void r300DepthRange(GLcontext
* ctx
, GLclampd nearval
, GLclampd farval
)
1046 r300UpdateWindow(ctx
);
1049 void r300UpdateViewportOffset(GLcontext
* ctx
)
1051 r300ContextPtr rmesa
= R300_CONTEXT(ctx
);
1052 __DRIdrawable
*dPriv
= radeon_get_drawable(&rmesa
->radeon
);
1053 GLfloat xoffset
= (GLfloat
) dPriv
->x
;
1054 GLfloat yoffset
= (GLfloat
) dPriv
->y
+ dPriv
->h
;
1055 const GLfloat
*v
= ctx
->Viewport
._WindowMap
.m
;
1057 GLfloat tx
= v
[MAT_TX
] + xoffset
;
1058 GLfloat ty
= (-v
[MAT_TY
]) + yoffset
;
1060 if (rmesa
->hw
.vpt
.cmd
[R300_VPT_XOFFSET
] != r300PackFloat32(tx
) ||
1061 rmesa
->hw
.vpt
.cmd
[R300_VPT_YOFFSET
] != r300PackFloat32(ty
)) {
1062 /* Note: this should also modify whatever data the context reset
1065 R300_STATECHANGE(rmesa
, vpt
);
1066 rmesa
->hw
.vpt
.cmd
[R300_VPT_XOFFSET
] = r300PackFloat32(tx
);
1067 rmesa
->hw
.vpt
.cmd
[R300_VPT_YOFFSET
] = r300PackFloat32(ty
);
1071 radeonUpdateScissor(ctx
);
1075 * Update R300's own internal state parameters.
1076 * For now just STATE_R300_WINDOW_DIMENSION
1078 static void r300UpdateStateParameters(GLcontext
* ctx
, GLuint new_state
)
1080 r300ContextPtr rmesa
= R300_CONTEXT(ctx
);
1081 struct gl_program_parameter_list
*paramList
;
1083 if (!(new_state
& (_NEW_BUFFERS
| _NEW_PROGRAM
| _NEW_PROGRAM_CONSTANTS
)))
1086 if (!ctx
->FragmentProgram
._Current
|| !rmesa
->selected_fp
)
1089 paramList
= ctx
->FragmentProgram
._Current
->Base
.Parameters
;
1094 _mesa_load_state_parameters(ctx
, paramList
);
1097 /* =============================================================
1100 static void r300PolygonOffset(GLcontext
* ctx
, GLfloat factor
, GLfloat units
)
1102 r300ContextPtr rmesa
= R300_CONTEXT(ctx
);
1103 GLfloat constant
= units
;
1105 switch (ctx
->Visual
.depthBits
) {
1116 /* fprintf(stderr, "%s f:%f u:%f\n", __FUNCTION__, factor, constant); */
1118 R300_STATECHANGE(rmesa
, zbs
);
1119 rmesa
->hw
.zbs
.cmd
[R300_ZBS_T_FACTOR
] = r300PackFloat32(factor
);
1120 rmesa
->hw
.zbs
.cmd
[R300_ZBS_T_CONSTANT
] = r300PackFloat32(constant
);
1121 rmesa
->hw
.zbs
.cmd
[R300_ZBS_W_FACTOR
] = r300PackFloat32(factor
);
1122 rmesa
->hw
.zbs
.cmd
[R300_ZBS_W_CONSTANT
] = r300PackFloat32(constant
);
1125 /* Routing and texture-related */
1127 /* r300 doesnt handle GL_CLAMP and GL_MIRROR_CLAMP_EXT correctly when filter is NEAREST.
1128 * Since texwrap produces same results for GL_CLAMP and GL_CLAMP_TO_EDGE we use them instead.
1129 * We need to recalculate wrap modes whenever filter mode is changed because someone might do:
1130 * glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
1131 * glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
1132 * glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
1133 * Since r300 completely ignores R300_TX_CLAMP when either min or mag is nearest it cant handle
1134 * combinations where only one of them is nearest.
1136 static unsigned long gen_fixed_filter(unsigned long f
)
1138 unsigned long mag
, min
, needs_fixing
= 0;
1141 /* We ignore MIRROR bit so we dont have to do everything twice */
1142 if ((f
& ((7 - 1) << R300_TX_WRAP_S_SHIFT
)) ==
1143 (R300_TX_CLAMP
<< R300_TX_WRAP_S_SHIFT
)) {
1146 if ((f
& ((7 - 1) << R300_TX_WRAP_T_SHIFT
)) ==
1147 (R300_TX_CLAMP
<< R300_TX_WRAP_T_SHIFT
)) {
1150 if ((f
& ((7 - 1) << R300_TX_WRAP_R_SHIFT
)) ==
1151 (R300_TX_CLAMP
<< R300_TX_WRAP_R_SHIFT
)) {
1158 mag
= f
& R300_TX_MAG_FILTER_MASK
;
1159 min
= f
& (R300_TX_MIN_FILTER_MASK
|R300_TX_MIN_FILTER_MIP_MASK
);
1161 /* TODO: Check for anisto filters too */
1162 if ((mag
!= R300_TX_MAG_FILTER_NEAREST
)
1163 && (min
!= R300_TX_MIN_FILTER_NEAREST
))
1166 /* r300 cant handle these modes hence we force nearest to linear */
1167 if ((mag
== R300_TX_MAG_FILTER_NEAREST
)
1168 && (min
!= R300_TX_MIN_FILTER_NEAREST
)) {
1169 f
&= ~R300_TX_MAG_FILTER_NEAREST
;
1170 f
|= R300_TX_MAG_FILTER_LINEAR
;
1174 if ((min
== R300_TX_MIN_FILTER_NEAREST
)
1175 && (mag
!= R300_TX_MAG_FILTER_NEAREST
)) {
1176 f
&= ~R300_TX_MIN_FILTER_NEAREST
;
1177 f
|= R300_TX_MIN_FILTER_LINEAR
;
1181 /* Both are nearest */
1182 if (needs_fixing
& 1) {
1183 f
&= ~((7 - 1) << R300_TX_WRAP_S_SHIFT
);
1184 f
|= R300_TX_CLAMP_TO_EDGE
<< R300_TX_WRAP_S_SHIFT
;
1186 if (needs_fixing
& 2) {
1187 f
&= ~((7 - 1) << R300_TX_WRAP_T_SHIFT
);
1188 f
|= R300_TX_CLAMP_TO_EDGE
<< R300_TX_WRAP_T_SHIFT
;
1190 if (needs_fixing
& 4) {
1191 f
&= ~((7 - 1) << R300_TX_WRAP_R_SHIFT
);
1192 f
|= R300_TX_CLAMP_TO_EDGE
<< R300_TX_WRAP_R_SHIFT
;
1197 static void r300SetupFragmentShaderTextures(GLcontext
*ctx
, int *tmu_mappings
)
1199 r300ContextPtr r300
= R300_CONTEXT(ctx
);
1201 struct r300_fragment_program_code
*code
= &r300
->selected_fp
->code
.code
.r300
;
1203 R300_STATECHANGE(r300
, fpt
);
1205 for (i
= 0; i
< code
->tex
.length
; i
++) {
1210 unit
= code
->tex
.inst
[i
] >> R300_TEX_ID_SHIFT
;
1213 val
= code
->tex
.inst
[i
];
1214 val
&= ~R300_TEX_ID_MASK
;
1217 (val
& R300_TEX_INST_MASK
) >> R300_TEX_INST_SHIFT
;
1218 if (opcode
== R300_TEX_OP_KIL
) {
1219 r300
->hw
.fpt
.cmd
[R300_FPT_INSTR_0
+ i
] = val
;
1221 if (tmu_mappings
[unit
] >= 0) {
1223 tmu_mappings
[unit
] <<
1225 r300
->hw
.fpt
.cmd
[R300_FPT_INSTR_0
+ i
] = val
;
1227 // We get here when the corresponding texture image is incomplete
1228 // (e.g. incomplete mipmaps etc.)
1229 r300
->hw
.fpt
.cmd
[R300_FPT_INSTR_0
+ i
] = val
;
1234 r300
->hw
.fpt
.cmd
[R300_FPT_CMD_0
] =
1235 cmdpacket0(r300
->radeon
.radeonScreen
,
1236 R300_US_TEX_INST_0
, code
->tex
.length
);
1239 static void r500SetupFragmentShaderTextures(GLcontext
*ctx
, int *tmu_mappings
)
1241 r300ContextPtr r300
= R300_CONTEXT(ctx
);
1243 struct r500_fragment_program_code
*code
= &r300
->selected_fp
->code
.code
.r500
;
1245 /* find all the texture instructions and relocate the texture units */
1246 for (i
= 0; i
< code
->inst_end
+ 1; i
++) {
1247 if ((code
->inst
[i
].inst0
& 0x3) == R500_INST_TYPE_TEX
) {
1249 int unit
, opcode
, new_unit
;
1251 val
= code
->inst
[i
].inst1
;
1253 unit
= (val
>> 16) & 0xf;
1255 val
&= ~(0xf << 16);
1257 opcode
= val
& (0x7 << 22);
1258 if (opcode
== R500_TEX_INST_TEXKILL
) {
1261 if (tmu_mappings
[unit
] >= 0) {
1262 new_unit
= tmu_mappings
[unit
];
1267 val
|= R500_TEX_ID(new_unit
);
1268 code
->inst
[i
].inst1
= val
;
1273 static GLuint
translate_lod_bias(GLfloat bias
)
1275 GLint b
= (int)(bias
*32);
1278 else if (b
< -(1 << 9))
1280 return (((GLuint
)b
) << R300_LOD_BIAS_SHIFT
) & R300_LOD_BIAS_MASK
;
1284 static void r300SetupTextures(GLcontext
* ctx
)
1287 struct radeon_tex_obj
*t
;
1288 r300ContextPtr r300
= R300_CONTEXT(ctx
);
1290 int last_hw_tmu
= -1; /* -1 translates into no setup costs for fields */
1291 int tmu_mappings
[R300_MAX_TEXTURE_UNITS
] = { -1, };
1293 R300_STATECHANGE(r300
, txe
);
1294 R300_STATECHANGE(r300
, tex
.filter
);
1295 R300_STATECHANGE(r300
, tex
.filter_1
);
1296 R300_STATECHANGE(r300
, tex
.size
);
1297 R300_STATECHANGE(r300
, tex
.format
);
1298 R300_STATECHANGE(r300
, tex
.pitch
);
1299 R300_STATECHANGE(r300
, tex
.offset
);
1300 R300_STATECHANGE(r300
, tex
.chroma_key
);
1301 R300_STATECHANGE(r300
, tex
.border_color
);
1303 r300
->hw
.txe
.cmd
[R300_TXE_ENABLE
] = 0x0;
1305 mtu
= r300
->radeon
.glCtx
->Const
.MaxTextureUnits
;
1306 if (RADEON_DEBUG
& RADEON_STATE
)
1307 fprintf(stderr
, "mtu=%d\n", mtu
);
1309 if (mtu
> R300_MAX_TEXTURE_UNITS
) {
1311 "Aiiee ! mtu=%d is greater than R300_MAX_TEXTURE_UNITS=%d\n",
1312 mtu
, R300_MAX_TEXTURE_UNITS
);
1316 /* We cannot let disabled tmu offsets pass DRM */
1317 for (i
= 0; i
< mtu
; i
++) {
1318 if (ctx
->Texture
.Unit
[i
]._ReallyEnabled
) {
1319 tmu_mappings
[i
] = hw_tmu
;
1321 t
= radeon_tex_obj(ctx
->Texture
.Unit
[i
]._Current
);
1325 if ((t
->pp_txformat
& 0xffffff00) == 0xffffff00) {
1327 ("unknown texture format (entry %x) encountered. Help me !\n",
1328 t
->pp_txformat
& 0xff);
1331 if (RADEON_DEBUG
& RADEON_STATE
)
1333 "Activating texture unit %d\n", i
);
1335 r300
->hw
.txe
.cmd
[R300_TXE_ENABLE
] |= (1 << hw_tmu
);
1337 r300
->hw
.tex
.filter
.cmd
[R300_TEX_VALUE_0
+
1339 gen_fixed_filter(t
->pp_txfilter
) | (hw_tmu
<< 28);
1340 /* Note: There is a LOD bias per texture unit and a LOD bias
1341 * per texture object. We add them here to get the correct behaviour.
1342 * (The per-texture object LOD bias was introduced in OpenGL 1.4
1343 * and is not present in the EXT_texture_object extension).
1345 r300
->hw
.tex
.filter_1
.cmd
[R300_TEX_VALUE_0
+ hw_tmu
] =
1347 translate_lod_bias(ctx
->Texture
.Unit
[i
].LodBias
+ t
->base
.LodBias
);
1348 r300
->hw
.tex
.size
.cmd
[R300_TEX_VALUE_0
+ hw_tmu
] =
1350 r300
->hw
.tex
.format
.cmd
[R300_TEX_VALUE_0
+
1351 hw_tmu
] = t
->pp_txformat
;
1352 r300
->hw
.tex
.pitch
.cmd
[R300_TEX_VALUE_0
+ hw_tmu
] =
1354 r300
->hw
.textures
[hw_tmu
] = t
;
1356 if (t
->tile_bits
& R300_TXO_MACRO_TILE
) {
1357 WARN_ONCE("macro tiling enabled!\n");
1360 if (t
->tile_bits
& R300_TXO_MICRO_TILE
) {
1361 WARN_ONCE("micro tiling enabled!\n");
1364 r300
->hw
.tex
.chroma_key
.cmd
[R300_TEX_VALUE_0
+
1366 r300
->hw
.tex
.border_color
.cmd
[R300_TEX_VALUE_0
+
1370 last_hw_tmu
= hw_tmu
;
1376 /* R3xx and R4xx chips require that the texture unit corresponding to
1377 * KIL instructions is really enabled.
1379 * We do some fakery here and in the state atom emit logic to enable
1380 * the texture without tripping up the CS checker in the kernel.
1382 if (r300
->radeon
.radeonScreen
->chip_family
< CHIP_FAMILY_RV515
) {
1383 if (ctx
->FragmentProgram
._Current
->UsesKill
&& last_hw_tmu
< 0) {
1386 r300
->hw
.txe
.cmd
[R300_TXE_ENABLE
] |= 1;
1388 r300
->hw
.tex
.border_color
.cmd
[R300_TEX_VALUE_0
] = 0;
1389 r300
->hw
.tex
.chroma_key
.cmd
[R300_TEX_VALUE_0
] = 0;
1390 r300
->hw
.tex
.filter
.cmd
[R300_TEX_VALUE_0
] = 0;
1391 r300
->hw
.tex
.filter_1
.cmd
[R300_TEX_VALUE_0
] = 0;
1392 r300
->hw
.tex
.size
.cmd
[R300_TEX_VALUE_0
] = 0; /* 1x1 texture */
1393 r300
->hw
.tex
.format
.cmd
[R300_TEX_VALUE_0
] = 0; /* A8 format */
1394 r300
->hw
.tex
.pitch
.cmd
[R300_TEX_VALUE_0
] = 0;
1398 r300
->hw
.tex
.filter
.cmd
[R300_TEX_CMD_0
] =
1399 cmdpacket0(r300
->radeon
.radeonScreen
, R300_TX_FILTER0_0
, last_hw_tmu
+ 1);
1400 r300
->hw
.tex
.filter_1
.cmd
[R300_TEX_CMD_0
] =
1401 cmdpacket0(r300
->radeon
.radeonScreen
, R300_TX_FILTER1_0
, last_hw_tmu
+ 1);
1402 r300
->hw
.tex
.size
.cmd
[R300_TEX_CMD_0
] =
1403 cmdpacket0(r300
->radeon
.radeonScreen
, R300_TX_SIZE_0
, last_hw_tmu
+ 1);
1404 r300
->hw
.tex
.format
.cmd
[R300_TEX_CMD_0
] =
1405 cmdpacket0(r300
->radeon
.radeonScreen
, R300_TX_FORMAT_0
, last_hw_tmu
+ 1);
1406 r300
->hw
.tex
.pitch
.cmd
[R300_TEX_CMD_0
] =
1407 cmdpacket0(r300
->radeon
.radeonScreen
, R300_TX_FORMAT2_0
, last_hw_tmu
+ 1);
1408 r300
->hw
.tex
.offset
.cmd
[R300_TEX_CMD_0
] =
1409 cmdpacket0(r300
->radeon
.radeonScreen
, R300_TX_OFFSET_0
, last_hw_tmu
+ 1);
1410 r300
->hw
.tex
.chroma_key
.cmd
[R300_TEX_CMD_0
] =
1411 cmdpacket0(r300
->radeon
.radeonScreen
, R300_TX_CHROMA_KEY_0
, last_hw_tmu
+ 1);
1412 r300
->hw
.tex
.border_color
.cmd
[R300_TEX_CMD_0
] =
1413 cmdpacket0(r300
->radeon
.radeonScreen
, R300_TX_BORDER_COLOR_0
, last_hw_tmu
+ 1);
1415 r300
->vtbl
.SetupFragmentShaderTextures(ctx
, tmu_mappings
);
1417 if (RADEON_DEBUG
& RADEON_STATE
)
1418 fprintf(stderr
, "TX_ENABLE: %08x last_hw_tmu=%d\n",
1419 r300
->hw
.txe
.cmd
[R300_TXE_ENABLE
], last_hw_tmu
);
1422 union r300_outputs_written
{
1423 GLuint vp_outputs
; /* hw_tcl_on */
1424 DECLARE_RENDERINPUTS(index_bitset
); /* !hw_tcl_on */
1427 #define R300_OUTPUTS_WRITTEN_TEST(ow, vp_result, tnl_attrib) \
1428 ((hw_tcl_on) ? (ow).vp_outputs & (1 << (vp_result)) : \
1429 RENDERINPUTS_TEST( (ow.index_bitset), (tnl_attrib) ))
1431 static void r300SetupRSUnit(GLcontext
* ctx
)
1433 r300ContextPtr r300
= R300_CONTEXT(ctx
);
1434 union r300_outputs_written OutputsWritten
;
1436 int fp_reg
, high_rr
;
1438 int rs_tex_count
= 0;
1439 int i
, col_fmt
, hw_tcl_on
;
1441 hw_tcl_on
= r300
->options
.hw_tcl_enabled
;
1444 OutputsWritten
.vp_outputs
= r300
->selected_vp
->code
.OutputsWritten
;
1446 RENDERINPUTS_COPY(OutputsWritten
.index_bitset
, r300
->render_inputs_bitset
);
1448 InputsRead
= r300
->selected_fp
->InputsRead
;
1450 R300_STATECHANGE(r300
, ri
);
1451 R300_STATECHANGE(r300
, rc
);
1452 R300_STATECHANGE(r300
, rr
);
1454 fp_reg
= col_ip
= tex_ip
= col_fmt
= 0;
1456 r300
->hw
.rc
.cmd
[1] = 0;
1457 r300
->hw
.rc
.cmd
[2] = 0;
1458 for (i
=0; i
<R300_RR_CMDSIZE
-1; ++i
)
1459 r300
->hw
.rr
.cmd
[R300_RR_INST_0
+ i
] = 0;
1461 for (i
=0; i
<R300_RI_CMDSIZE
-1; ++i
)
1462 r300
->hw
.ri
.cmd
[R300_RI_INTERP_0
+ i
] = 0;
1465 if (InputsRead
& FRAG_BIT_COL0
) {
1466 if (R300_OUTPUTS_WRITTEN_TEST(OutputsWritten
, VERT_RESULT_COL0
, _TNL_ATTRIB_COLOR0
)) {
1467 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
);
1468 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
);
1469 InputsRead
&= ~FRAG_BIT_COL0
;
1473 WARN_ONCE("fragprog wants col0, vp doesn't provide it\n");
1477 if (InputsRead
& FRAG_BIT_COL1
) {
1478 if (R300_OUTPUTS_WRITTEN_TEST(OutputsWritten
, VERT_RESULT_COL1
, _TNL_ATTRIB_COLOR1
)) {
1479 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
);
1480 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
);
1481 InputsRead
&= ~FRAG_BIT_COL1
;
1485 WARN_ONCE("fragprog wants col1, vp doesn't provide it\n");
1489 /* We always route 4 texcoord components */
1490 for (i
= 0; i
< ctx
->Const
.MaxTextureUnits
; i
++) {
1491 if (! ( InputsRead
& FRAG_BIT_TEX(i
) ) )
1494 if (!R300_OUTPUTS_WRITTEN_TEST(OutputsWritten
, VERT_RESULT_TEX0
+ i
, _TNL_ATTRIB_TEX(i
))) {
1495 WARN_ONCE("fragprog wants coords for tex%d, vp doesn't provide them!\n", i
);
1499 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
);
1500 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
);
1501 InputsRead
&= ~(FRAG_BIT_TEX0
<< i
);
1507 /* Setup default color if no color or tex was set */
1508 if (rs_tex_count
== 0 && col_ip
== 0) {
1509 r300
->hw
.rr
.cmd
[R300_RR_INST_0
] = R300_RS_INST_COL_ID(0) | R300_RS_INST_COL_ADDR(0);
1510 r300
->hw
.ri
.cmd
[R300_RI_INTERP_0
] = R300_RS_COL_PTR(0) | R300_RS_COL_FMT(R300_RS_COL_FMT_0001
);
1514 high_rr
= (col_ip
> tex_ip
) ? col_ip
: tex_ip
;
1515 r300
->hw
.rc
.cmd
[1] |= (rs_tex_count
<< R300_IT_COUNT_SHIFT
) | (col_ip
<< R300_IC_COUNT_SHIFT
) | R300_HIRES_EN
;
1516 r300
->hw
.rc
.cmd
[2] |= high_rr
- 1;
1518 r300
->hw
.rr
.cmd
[R300_RR_CMD_0
] = cmdpacket0(r300
->radeon
.radeonScreen
, R300_RS_INST_0
, high_rr
);
1519 r300
->hw
.ri
.cmd
[R300_RI_CMD_0
] = cmdpacket0(r300
->radeon
.radeonScreen
, R300_RS_IP_0
, high_rr
);
1522 WARN_ONCE("Don't know how to satisfy InputsRead=0x%08x\n", InputsRead
);
1525 static void r500SetupRSUnit(GLcontext
* ctx
)
1527 r300ContextPtr r300
= R300_CONTEXT(ctx
);
1528 union r300_outputs_written OutputsWritten
;
1530 int fp_reg
, high_rr
;
1532 int rs_tex_count
= 0;
1533 int i
, col_fmt
, hw_tcl_on
;
1535 hw_tcl_on
= r300
->options
.hw_tcl_enabled
;
1538 OutputsWritten
.vp_outputs
= r300
->selected_vp
->code
.OutputsWritten
;
1540 RENDERINPUTS_COPY(OutputsWritten
.index_bitset
, r300
->render_inputs_bitset
);
1542 InputsRead
= r300
->selected_fp
->InputsRead
;
1544 R300_STATECHANGE(r300
, ri
);
1545 R300_STATECHANGE(r300
, rc
);
1546 R300_STATECHANGE(r300
, rr
);
1548 fp_reg
= col_ip
= tex_ip
= col_fmt
= 0;
1550 r300
->hw
.rc
.cmd
[1] = 0;
1551 r300
->hw
.rc
.cmd
[2] = 0;
1552 for (i
=0; i
<R300_RR_CMDSIZE
-1; ++i
)
1553 r300
->hw
.rr
.cmd
[R300_RR_INST_0
+ i
] = 0;
1555 for (i
=0; i
<R500_RI_CMDSIZE
-1; ++i
)
1556 r300
->hw
.ri
.cmd
[R300_RI_INTERP_0
+ i
] = 0;
1559 if (InputsRead
& FRAG_BIT_COL0
) {
1560 if (R300_OUTPUTS_WRITTEN_TEST(OutputsWritten
, VERT_RESULT_COL0
, _TNL_ATTRIB_COLOR0
)) {
1561 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
);
1562 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
);
1563 InputsRead
&= ~FRAG_BIT_COL0
;
1567 WARN_ONCE("fragprog wants col0, vp doesn't provide it\n");
1571 if (InputsRead
& FRAG_BIT_COL1
) {
1572 if (R300_OUTPUTS_WRITTEN_TEST(OutputsWritten
, VERT_RESULT_COL1
, _TNL_ATTRIB_COLOR1
)) {
1573 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
);
1574 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
);
1575 InputsRead
&= ~FRAG_BIT_COL1
;
1579 WARN_ONCE("fragprog wants col1, vp doesn't provide it\n");
1583 /* We always route 4 texcoord components */
1584 for (i
= 0; i
< ctx
->Const
.MaxTextureUnits
; i
++) {
1585 if (! ( InputsRead
& FRAG_BIT_TEX(i
) ) )
1588 if (!R300_OUTPUTS_WRITTEN_TEST(OutputsWritten
, VERT_RESULT_TEX0
+ i
, _TNL_ATTRIB_TEX(i
))) {
1589 WARN_ONCE("fragprog wants coords for tex%d, vp doesn't provide them!\n", i
);
1593 r300
->hw
.ri
.cmd
[R300_RI_INTERP_0
+ tex_ip
] |= ((rs_tex_count
+ 0) << R500_RS_IP_TEX_PTR_S_SHIFT
) |
1594 ((rs_tex_count
+ 1) << R500_RS_IP_TEX_PTR_T_SHIFT
) |
1595 ((rs_tex_count
+ 2) << R500_RS_IP_TEX_PTR_R_SHIFT
) |
1596 ((rs_tex_count
+ 3) << R500_RS_IP_TEX_PTR_Q_SHIFT
);
1598 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
);
1599 InputsRead
&= ~(FRAG_BIT_TEX0
<< i
);
1605 /* Setup default color if no color or tex was set */
1606 if (rs_tex_count
== 0 && col_ip
== 0) {
1607 r300
->hw
.rr
.cmd
[R300_RR_INST_0
] = R500_RS_INST_COL_ID(0) | R500_RS_INST_COL_ADDR(0);
1608 r300
->hw
.ri
.cmd
[R300_RI_INTERP_0
] = R500_RS_COL_PTR(0) | R500_RS_COL_FMT(R300_RS_COL_FMT_0001
);
1612 high_rr
= (col_ip
> tex_ip
) ? col_ip
: tex_ip
;
1613 r300
->hw
.rc
.cmd
[1] = (rs_tex_count
<< R300_IT_COUNT_SHIFT
) | (col_ip
<< R300_IC_COUNT_SHIFT
) | R300_HIRES_EN
;
1614 r300
->hw
.rc
.cmd
[2] = 0xC0 | (high_rr
- 1);
1616 r300
->hw
.rr
.cmd
[R300_RR_CMD_0
] = cmdpacket0(r300
->radeon
.radeonScreen
, R500_RS_INST_0
, high_rr
);
1617 r300
->hw
.ri
.cmd
[R300_RI_CMD_0
] = cmdpacket0(r300
->radeon
.radeonScreen
, R500_RS_IP_0
, high_rr
);
1620 WARN_ONCE("Don't know how to satisfy InputsRead=0x%08x\n", InputsRead
);
1623 #define MIN3(a, b, c) ((a) < (b) ? MIN2(a, c) : MIN2(b, c))
1625 void r300VapCntl(r300ContextPtr rmesa
, GLuint input_count
,
1626 GLuint output_count
, GLuint temp_count
)
1632 /* Flush PVS engine before changing PVS_NUM_SLOTS, PVS_NUM_CNTRLS.
1633 * See r500 docs 6.5.2 - done in emit */
1635 /* avoid division by zero */
1636 if (input_count
== 0) input_count
= 1;
1637 if (output_count
== 0) output_count
= 1;
1638 if (temp_count
== 0) temp_count
= 1;
1640 if (rmesa
->radeon
.radeonScreen
->chip_family
>= CHIP_FAMILY_RV515
)
1645 pvs_num_slots
= MIN3(10, vtx_mem_size
/input_count
, vtx_mem_size
/output_count
);
1646 pvs_num_cntrls
= MIN2(6, vtx_mem_size
/temp_count
);
1648 R300_STATECHANGE(rmesa
, vap_cntl
);
1649 if (rmesa
->options
.hw_tcl_enabled
) {
1650 rmesa
->hw
.vap_cntl
.cmd
[R300_VAP_CNTL_INSTR
] =
1651 (pvs_num_slots
<< R300_PVS_NUM_SLOTS_SHIFT
) |
1652 (pvs_num_cntrls
<< R300_PVS_NUM_CNTLRS_SHIFT
) |
1653 (12 << R300_VF_MAX_VTX_NUM_SHIFT
);
1654 if (rmesa
->radeon
.radeonScreen
->chip_family
>= CHIP_FAMILY_RV515
)
1655 rmesa
->hw
.vap_cntl
.cmd
[R300_VAP_CNTL_INSTR
] |= R500_TCL_STATE_OPTIMIZATION
;
1657 /* not sure about non-tcl */
1658 rmesa
->hw
.vap_cntl
.cmd
[R300_VAP_CNTL_INSTR
] = ((10 << R300_PVS_NUM_SLOTS_SHIFT
) |
1659 (5 << R300_PVS_NUM_CNTLRS_SHIFT
) |
1660 (5 << R300_VF_MAX_VTX_NUM_SHIFT
));
1662 if (rmesa
->radeon
.radeonScreen
->chip_family
== CHIP_FAMILY_RV515
)
1663 rmesa
->hw
.vap_cntl
.cmd
[R300_VAP_CNTL_INSTR
] |= (2 << R300_PVS_NUM_FPUS_SHIFT
);
1664 else if ((rmesa
->radeon
.radeonScreen
->chip_family
== CHIP_FAMILY_RV530
) ||
1665 (rmesa
->radeon
.radeonScreen
->chip_family
== CHIP_FAMILY_RV560
) ||
1666 (rmesa
->radeon
.radeonScreen
->chip_family
== CHIP_FAMILY_RV570
))
1667 rmesa
->hw
.vap_cntl
.cmd
[R300_VAP_CNTL_INSTR
] |= (5 << R300_PVS_NUM_FPUS_SHIFT
);
1668 else if ((rmesa
->radeon
.radeonScreen
->chip_family
== CHIP_FAMILY_RV410
) ||
1669 (rmesa
->radeon
.radeonScreen
->chip_family
== CHIP_FAMILY_R420
))
1670 rmesa
->hw
.vap_cntl
.cmd
[R300_VAP_CNTL_INSTR
] |= (6 << R300_PVS_NUM_FPUS_SHIFT
);
1671 else if ((rmesa
->radeon
.radeonScreen
->chip_family
== CHIP_FAMILY_R520
) ||
1672 (rmesa
->radeon
.radeonScreen
->chip_family
== CHIP_FAMILY_R580
))
1673 rmesa
->hw
.vap_cntl
.cmd
[R300_VAP_CNTL_INSTR
] |= (8 << R300_PVS_NUM_FPUS_SHIFT
);
1675 rmesa
->hw
.vap_cntl
.cmd
[R300_VAP_CNTL_INSTR
] |= (4 << R300_PVS_NUM_FPUS_SHIFT
);
1680 * Enable/Disable states.
1682 * \note Mesa already filters redundant calls to this function.
1684 static void r300Enable(GLcontext
* ctx
, GLenum cap
, GLboolean state
)
1686 r300ContextPtr rmesa
= R300_CONTEXT(ctx
);
1687 if (RADEON_DEBUG
& RADEON_STATE
)
1688 fprintf(stderr
, "%s( %s = %s )\n", __FUNCTION__
,
1689 _mesa_lookup_enum_by_nr(cap
),
1690 state
? "GL_TRUE" : "GL_FALSE");
1694 r300SetAlphaState(ctx
);
1696 case GL_COLOR_LOGIC_OP
:
1697 r300SetLogicOpState(ctx
);
1698 /* fall-through, because logic op overrides blending */
1700 r300SetBlendState(ctx
);
1702 case GL_CLIP_PLANE0
:
1703 case GL_CLIP_PLANE1
:
1704 case GL_CLIP_PLANE2
:
1705 case GL_CLIP_PLANE3
:
1706 case GL_CLIP_PLANE4
:
1707 case GL_CLIP_PLANE5
:
1708 r300SetClipPlaneState(ctx
, cap
, state
);
1711 r300UpdateCulling(ctx
);
1714 r300SetDepthState(ctx
);
1716 case GL_LINE_SMOOTH
:
1717 if (rmesa
->options
.conformance_mode
)
1718 r300SwitchFallback(ctx
, R300_FALLBACK_LINE_SMOOTH
, ctx
->Line
.SmoothFlag
);
1720 case GL_LINE_STIPPLE
:
1721 if (rmesa
->options
.conformance_mode
)
1722 r300SwitchFallback(ctx
, R300_FALLBACK_LINE_STIPPLE
, ctx
->Line
.StippleFlag
);
1724 case GL_POINT_SMOOTH
:
1725 if (rmesa
->options
.conformance_mode
)
1726 r300SwitchFallback(ctx
, R300_FALLBACK_POINT_SMOOTH
, ctx
->Point
.SmoothFlag
);
1728 case GL_POLYGON_SMOOTH
:
1729 if (rmesa
->options
.conformance_mode
)
1730 r300SwitchFallback(ctx
, R300_FALLBACK_POLYGON_SMOOTH
, ctx
->Polygon
.SmoothFlag
);
1732 case GL_POLYGON_STIPPLE
:
1733 if (rmesa
->options
.conformance_mode
)
1734 r300SwitchFallback(ctx
, R300_FALLBACK_POLYGON_STIPPLE
, ctx
->Polygon
.StippleFlag
);
1736 case GL_POLYGON_OFFSET_POINT
:
1737 case GL_POLYGON_OFFSET_LINE
:
1738 case GL_POLYGON_OFFSET_FILL
:
1739 r300SetPolygonOffsetState(ctx
, state
);
1741 case GL_SCISSOR_TEST
:
1742 radeon_firevertices(&rmesa
->radeon
);
1743 rmesa
->radeon
.state
.scissor
.enabled
= state
;
1744 radeonUpdateScissor( ctx
);
1746 case GL_STENCIL_TEST
:
1747 r300SetStencilState(ctx
, state
);
1755 * Completely recalculates hardware state based on the Mesa state.
1757 static void r300ResetHwState(r300ContextPtr r300
)
1759 GLcontext
*ctx
= r300
->radeon
.glCtx
;
1762 has_tcl
= r300
->options
.hw_tcl_enabled
;
1764 if (RADEON_DEBUG
& RADEON_STATE
)
1765 fprintf(stderr
, "%s\n", __FUNCTION__
);
1768 ctx
->Color
.ColorMask
[0][RCOMP
],
1769 ctx
->Color
.ColorMask
[0][GCOMP
],
1770 ctx
->Color
.ColorMask
[0][BCOMP
],
1771 ctx
->Color
.ColorMask
[0][ACOMP
]);
1773 r300Enable(ctx
, GL_DEPTH_TEST
, ctx
->Depth
.Test
);
1774 r300DepthMask(ctx
, ctx
->Depth
.Mask
);
1775 r300DepthFunc(ctx
, ctx
->Depth
.Func
);
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]);
1786 r300UpdateCulling(ctx
);
1788 r300SetBlendState(ctx
);
1789 r300SetLogicOpState(ctx
);
1791 r300AlphaFunc(ctx
, ctx
->Color
.AlphaFunc
, ctx
->Color
.AlphaRef
);
1792 r300Enable(ctx
, GL_ALPHA_TEST
, ctx
->Color
.AlphaEnabled
);
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;
1802 r300
->hw
.vap_vf_max_vtx_indx
.cmd
[1] = 0x00FFFFFF;
1803 r300
->hw
.vap_vf_max_vtx_indx
.cmd
[2] = 0x00000000;
1805 #ifdef MESA_LITTLE_ENDIAN
1806 r300
->hw
.vap_cntl_status
.cmd
[1] = R300_VC_NO_SWAP
;
1808 r300
->hw
.vap_cntl_status
.cmd
[1] = R300_VC_32BIT_SWAP
;
1811 /* disable VAP/TCL on non-TCL capable chips */
1813 r300
->hw
.vap_cntl_status
.cmd
[1] |= R300_VAP_TCL_BYPASS
;
1815 r300
->hw
.vap_psc_sgn_norm_cntl
.cmd
[1] = 0xAAAAAAAA;
1817 /* XXX: Other families? */
1819 r300
->hw
.vap_clip_cntl
.cmd
[1] = R300_PS_UCP_MODE_DIST_COP
;
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 */
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
;
1831 r300
->hw
.vap_pvs_vtx_timeout_reg
.cmd
[1] = R300_2288_RV350
;
1836 r300
->hw
.gb_enable
.cmd
[1] = R300_GB_POINT_STUFF_ENABLE
1837 | R300_GB_LINE_STUFF_ENABLE
1838 | R300_GB_TRIANGLE_STUFF_ENABLE
;
1840 r300
->hw
.gb_misc
.cmd
[R300_GB_MISC_MSPOS_0
] = 0x66666666;
1841 r300
->hw
.gb_misc
.cmd
[R300_GB_MISC_MSPOS_1
] = 0x06666666;
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
) {
1848 r300
->hw
.gb_misc
.cmd
[R300_GB_MISC_TILE_CONFIG
] |=
1849 R300_GB_TILE_PIPE_COUNT_RV300
;
1852 r300
->hw
.gb_misc
.cmd
[R300_GB_MISC_TILE_CONFIG
] |=
1853 R300_GB_TILE_PIPE_COUNT_R300
;
1856 r300
->hw
.gb_misc
.cmd
[R300_GB_MISC_TILE_CONFIG
] |=
1857 R300_GB_TILE_PIPE_COUNT_R420_3P
;
1860 r300
->hw
.gb_misc
.cmd
[R300_GB_MISC_TILE_CONFIG
] |=
1861 R300_GB_TILE_PIPE_COUNT_R420
;
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;
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);
1874 r300
->hw
.ga_triangle_stipple
.cmd
[1] = 0x00050005;
1876 r300PointSize(ctx
, 1.0);
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);
1882 r300LineWidth(ctx
, 1.0);
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);
1888 r300ShadeModel(ctx
, ctx
->Light
.ShadeModel
);
1890 r300PolygonMode(ctx
, GL_FRONT
, ctx
->Polygon
.FrontMode
);
1891 r300PolygonMode(ctx
, GL_BACK
, ctx
->Polygon
.BackMode
);
1892 r300
->hw
.zbias_cntl
.cmd
[1] = 0x00000000;
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
);
1900 r300
->hw
.su_depth_scale
.cmd
[1] = 0x4B7FFFFF;
1901 r300
->hw
.su_depth_scale
.cmd
[2] = 0x00000000;
1903 r300
->hw
.sc_hyperz
.cmd
[1] = 0x0000001C;
1904 r300
->hw
.sc_hyperz
.cmd
[2] = 0x2DA49525;
1906 r300
->hw
.sc_screendoor
.cmd
[1] = 0x00FFFFFF;
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
;
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
;
1922 r300
->hw
.rb3d_cctl
.cmd
[1] = 0;
1924 r300BlendColor(ctx
, ctx
->Color
.BlendColor
);
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;
1936 r300
->hw
.rb3d_aaresolve_ctl
.cmd
[1] = 0;
1938 r300
->hw
.rb3d_discard_src_pixel_lte_threshold
.cmd
[1] = 0x00000000;
1939 r300
->hw
.rb3d_discard_src_pixel_lte_threshold
.cmd
[2] = 0xffffffff;
1941 r300
->hw
.zb_depthclearvalue
.cmd
[1] = 0;
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
);
1948 r300
->hw
.zb_zmask
.cmd
[1] = 0;
1949 r300
->hw
.zb_zmask
.cmd
[2] = 0;
1951 r300
->hw
.zb_hiz_offset
.cmd
[1] = 0;
1953 r300
->hw
.zb_hiz_pitch
.cmd
[1] = 0;
1955 r300VapCntl(r300
, 0, 0, 0);
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;
1963 r300
->radeon
.hw
.all_dirty
= GL_TRUE
;
1966 void r300UpdateShaders(r300ContextPtr rmesa
)
1968 GLcontext
*ctx
= rmesa
->radeon
.glCtx
;
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 fprintf(stderr
, "No ctx->FragmentProgram._Current!!\n");
1978 struct r300_fragment_program
*fp
;
1980 fp
= r300SelectAndTranslateFragmentShader(ctx
);
1982 r300SwitchFallback(ctx
, R300_FALLBACK_FRAGMENT_PROGRAM
, fp
->error
);
1985 if (rmesa
->options
.hw_tcl_enabled
) {
1986 struct r300_vertex_program
*vp
;
1988 vp
= r300SelectAndTranslateVertexShader(ctx
);
1990 r300SwitchFallback(ctx
, R300_FALLBACK_VERTEX_PROGRAM
, vp
->error
);
1993 r300UpdateStateParameters(ctx
, _NEW_PROGRAM
| _NEW_PROGRAM_CONSTANTS
);
1994 rmesa
->radeon
.NewGLState
= 0;
1997 static const GLfloat
*get_fragmentprogram_constant(GLcontext
*ctx
, GLuint index
, GLfloat
* buffer
)
1999 static const GLfloat dummy
[4] = { 0, 0, 0, 0 };
2000 r300ContextPtr rmesa
= R300_CONTEXT(ctx
);
2001 struct rc_constant
* rcc
= &rmesa
->selected_fp
->code
.constants
.Constants
[index
];
2004 case RC_CONSTANT_EXTERNAL
:
2005 return ctx
->FragmentProgram
._Current
->Base
.Parameters
->ParameterValues
[rcc
->u
.External
];
2006 case RC_CONSTANT_IMMEDIATE
:
2007 return rcc
->u
.Immediate
;
2008 case RC_CONSTANT_STATE
:
2009 switch(rcc
->u
.State
[0]) {
2010 case RC_STATE_SHADOW_AMBIENT
: {
2011 const int unit
= (int) rcc
->u
.State
[1];
2012 const struct gl_texture_object
*texObj
= ctx
->Texture
.Unit
[unit
]._Current
;
2017 buffer
[3] = texObj
->CompareFailValue
;
2022 case RC_STATE_R300_WINDOW_DIMENSION
: {
2023 __DRIdrawable
* drawable
= radeon_get_drawable(&rmesa
->radeon
);
2024 buffer
[0] = drawable
->w
* 0.5f
; /* width*0.5 */
2025 buffer
[1] = drawable
->h
* 0.5f
; /* height*0.5 */
2026 buffer
[2] = 0.5F
; /* for moving range [-1 1] -> [0 1] */
2027 buffer
[3] = 1.0F
; /* not used */
2031 case RC_STATE_R300_TEXRECT_FACTOR
: {
2032 struct gl_texture_object
*t
=
2033 ctx
->Texture
.Unit
[rcc
->u
.State
[1]].CurrentTex
[TEXTURE_RECT_INDEX
];
2035 if (t
&& t
->Image
[0][t
->BaseLevel
]) {
2036 struct gl_texture_image
*image
=
2037 t
->Image
[0][t
->BaseLevel
];
2038 buffer
[0] = 1.0 / image
->Width2
;
2039 buffer
[1] = 1.0 / image
->Height2
;
2055 static void r300SetupPixelShader(GLcontext
*ctx
)
2057 r300ContextPtr rmesa
= R300_CONTEXT(ctx
);
2058 struct r300_fragment_program
*fp
= rmesa
->selected_fp
;
2059 struct r300_fragment_program_code
*code
;
2062 code
= &fp
->code
.code
.r300
;
2064 R300_STATECHANGE(rmesa
, fpi
[0]);
2065 R300_STATECHANGE(rmesa
, fpi
[1]);
2066 R300_STATECHANGE(rmesa
, fpi
[2]);
2067 R300_STATECHANGE(rmesa
, fpi
[3]);
2068 rmesa
->hw
.fpi
[0].cmd
[R300_FPI_CMD_0
] = cmdpacket0(rmesa
->radeon
.radeonScreen
, R300_US_ALU_RGB_INST_0
, code
->alu
.length
);
2069 rmesa
->hw
.fpi
[1].cmd
[R300_FPI_CMD_0
] = cmdpacket0(rmesa
->radeon
.radeonScreen
, R300_US_ALU_RGB_ADDR_0
, code
->alu
.length
);
2070 rmesa
->hw
.fpi
[2].cmd
[R300_FPI_CMD_0
] = cmdpacket0(rmesa
->radeon
.radeonScreen
, R300_US_ALU_ALPHA_INST_0
, code
->alu
.length
);
2071 rmesa
->hw
.fpi
[3].cmd
[R300_FPI_CMD_0
] = cmdpacket0(rmesa
->radeon
.radeonScreen
, R300_US_ALU_ALPHA_ADDR_0
, code
->alu
.length
);
2072 for (i
= 0; i
< code
->alu
.length
; i
++) {
2073 rmesa
->hw
.fpi
[0].cmd
[R300_FPI_INSTR_0
+ i
] = code
->alu
.inst
[i
].rgb_inst
;
2074 rmesa
->hw
.fpi
[1].cmd
[R300_FPI_INSTR_0
+ i
] = code
->alu
.inst
[i
].rgb_addr
;
2075 rmesa
->hw
.fpi
[2].cmd
[R300_FPI_INSTR_0
+ i
] = code
->alu
.inst
[i
].alpha_inst
;
2076 rmesa
->hw
.fpi
[3].cmd
[R300_FPI_INSTR_0
+ i
] = code
->alu
.inst
[i
].alpha_addr
;
2079 R300_STATECHANGE(rmesa
, fp
);
2080 rmesa
->hw
.fp
.cmd
[R300_FP_CNTL0
] = code
->config
;
2081 rmesa
->hw
.fp
.cmd
[R300_FP_CNTL1
] = code
->pixsize
;
2082 rmesa
->hw
.fp
.cmd
[R300_FP_CNTL2
] = code
->code_offset
;
2083 for (i
= 0; i
< 4; i
++)
2084 rmesa
->hw
.fp
.cmd
[R300_FP_NODE0
+ i
] = code
->code_addr
[i
];
2086 R300_STATECHANGE(rmesa
, fpp
);
2087 rmesa
->hw
.fpp
.cmd
[R300_FPP_CMD_0
] = cmdpacket0(rmesa
->radeon
.radeonScreen
, R300_PFS_PARAM_0_X
, fp
->code
.constants
.Count
* 4);
2088 for (i
= 0; i
< fp
->code
.constants
.Count
; i
++) {
2090 const GLfloat
*constant
= get_fragmentprogram_constant(ctx
, i
, buffer
);
2091 rmesa
->hw
.fpp
.cmd
[R300_FPP_PARAM_0
+ 4 * i
+ 0] = r300PackFloat24(constant
[0]);
2092 rmesa
->hw
.fpp
.cmd
[R300_FPP_PARAM_0
+ 4 * i
+ 1] = r300PackFloat24(constant
[1]);
2093 rmesa
->hw
.fpp
.cmd
[R300_FPP_PARAM_0
+ 4 * i
+ 2] = r300PackFloat24(constant
[2]);
2094 rmesa
->hw
.fpp
.cmd
[R300_FPP_PARAM_0
+ 4 * i
+ 3] = r300PackFloat24(constant
[3]);
2098 #define bump_r500fp_count(ptr, new_count) do{\
2099 drm_r300_cmd_header_t* _p=((drm_r300_cmd_header_t*)(ptr));\
2100 int _nc=(new_count)/6; \
2101 assert(_nc < 256); \
2102 if(_nc>_p->r500fp.count)_p->r500fp.count=_nc;\
2105 #define bump_r500fp_const_count(ptr, new_count) do{\
2106 drm_r300_cmd_header_t* _p=((drm_r300_cmd_header_t*)(ptr));\
2107 int _nc=(new_count)/4; \
2108 assert(_nc < 256); \
2109 if(_nc>_p->r500fp.count)_p->r500fp.count=_nc;\
2112 static void r500SetupPixelShader(GLcontext
*ctx
)
2114 r300ContextPtr rmesa
= R300_CONTEXT(ctx
);
2115 struct r300_fragment_program
*fp
= rmesa
->selected_fp
;
2117 struct r500_fragment_program_code
*code
;
2119 ((drm_r300_cmd_header_t
*) rmesa
->hw
.r500fp
.cmd
)->r500fp
.count
= 0;
2120 ((drm_r300_cmd_header_t
*) rmesa
->hw
.r500fp_const
.cmd
)->r500fp
.count
= 0;
2122 code
= &fp
->code
.code
.r500
;
2124 R300_STATECHANGE(rmesa
, fp
);
2125 rmesa
->hw
.fp
.cmd
[R500_FP_PIXSIZE
] = code
->max_temp_idx
;
2127 rmesa
->hw
.fp
.cmd
[R500_FP_CODE_ADDR
] =
2128 R500_US_CODE_START_ADDR(0) |
2129 R500_US_CODE_END_ADDR(code
->inst_end
);
2130 rmesa
->hw
.fp
.cmd
[R500_FP_CODE_RANGE
] =
2131 R500_US_CODE_RANGE_ADDR(0) |
2132 R500_US_CODE_RANGE_SIZE(code
->inst_end
);
2133 rmesa
->hw
.fp
.cmd
[R500_FP_CODE_OFFSET
] =
2134 R500_US_CODE_OFFSET_ADDR(0);
2136 R300_STATECHANGE(rmesa
, r500fp
);
2137 /* Emit our shader... */
2138 for (i
= 0; i
< code
->inst_end
+1; i
++) {
2139 rmesa
->hw
.r500fp
.cmd
[i
*6+1] = code
->inst
[i
].inst0
;
2140 rmesa
->hw
.r500fp
.cmd
[i
*6+2] = code
->inst
[i
].inst1
;
2141 rmesa
->hw
.r500fp
.cmd
[i
*6+3] = code
->inst
[i
].inst2
;
2142 rmesa
->hw
.r500fp
.cmd
[i
*6+4] = code
->inst
[i
].inst3
;
2143 rmesa
->hw
.r500fp
.cmd
[i
*6+5] = code
->inst
[i
].inst4
;
2144 rmesa
->hw
.r500fp
.cmd
[i
*6+6] = code
->inst
[i
].inst5
;
2147 bump_r500fp_count(rmesa
->hw
.r500fp
.cmd
, (code
->inst_end
+ 1) * 6);
2149 R300_STATECHANGE(rmesa
, r500fp_const
);
2150 for (i
= 0; i
< fp
->code
.constants
.Count
; i
++) {
2152 const GLfloat
*constant
= get_fragmentprogram_constant(ctx
, i
, buffer
);
2153 rmesa
->hw
.r500fp_const
.cmd
[R300_FPP_PARAM_0
+ 4 * i
+ 0] = r300PackFloat32(constant
[0]);
2154 rmesa
->hw
.r500fp_const
.cmd
[R300_FPP_PARAM_0
+ 4 * i
+ 1] = r300PackFloat32(constant
[1]);
2155 rmesa
->hw
.r500fp_const
.cmd
[R300_FPP_PARAM_0
+ 4 * i
+ 2] = r300PackFloat32(constant
[2]);
2156 rmesa
->hw
.r500fp_const
.cmd
[R300_FPP_PARAM_0
+ 4 * i
+ 3] = r300PackFloat32(constant
[3]);
2158 bump_r500fp_const_count(rmesa
->hw
.r500fp_const
.cmd
, fp
->code
.constants
.Count
* 4);
2161 void r300SetupVAP(GLcontext
*ctx
, GLuint InputsRead
, GLuint OutputsWritten
)
2163 r300ContextPtr rmesa
= R300_CONTEXT( ctx
);
2164 struct vertex_attribute
*attrs
= rmesa
->vbuf
.attribs
;
2165 int i
, j
, reg_count
;
2166 uint32_t *vir0
= &rmesa
->hw
.vir
[0].cmd
[1];
2167 uint32_t *vir1
= &rmesa
->hw
.vir
[1].cmd
[1];
2169 for (i
= 0; i
< R300_VIR_CMDSIZE
-1; ++i
)
2170 vir0
[i
] = vir1
[i
] = 0;
2172 for (i
= 0, j
= 0; i
< rmesa
->vbuf
.num_attribs
; ++i
) {
2175 tmp
= attrs
[i
].data_type
| (attrs
[i
].dst_loc
<< R300_DST_VEC_LOC_SHIFT
);
2176 if (attrs
[i
]._signed
)
2178 if (attrs
[i
].normalize
)
2179 tmp
|= R300_NORMALIZE
;
2182 vir0
[j
] = tmp
<< R300_DATA_TYPE_0_SHIFT
;
2183 vir1
[j
] = attrs
[i
].swizzle
| (attrs
[i
].write_mask
<< R300_WRITE_ENA_SHIFT
);
2185 vir0
[j
] |= tmp
<< R300_DATA_TYPE_1_SHIFT
;
2186 vir1
[j
] |= (attrs
[i
].swizzle
| (attrs
[i
].write_mask
<< R300_WRITE_ENA_SHIFT
)) << R300_SWIZZLE1_SHIFT
;
2191 reg_count
= (rmesa
->vbuf
.num_attribs
+ 1) >> 1;
2192 if (rmesa
->vbuf
.num_attribs
% 2 != 0) {
2193 vir0
[reg_count
-1] |= R300_LAST_VEC
<< R300_DATA_TYPE_0_SHIFT
;
2195 vir0
[reg_count
-1] |= R300_LAST_VEC
<< R300_DATA_TYPE_1_SHIFT
;
2198 R300_STATECHANGE(rmesa
, vir
[0]);
2199 R300_STATECHANGE(rmesa
, vir
[1]);
2200 R300_STATECHANGE(rmesa
, vof
);
2201 R300_STATECHANGE(rmesa
, vic
);
2203 if (rmesa
->radeon
.radeonScreen
->kernel_mm
) {
2204 rmesa
->hw
.vir
[0].cmd
[0] &= 0xC000FFFF;
2205 rmesa
->hw
.vir
[1].cmd
[0] &= 0xC000FFFF;
2206 rmesa
->hw
.vir
[0].cmd
[0] |= (reg_count
& 0x3FFF) << 16;
2207 rmesa
->hw
.vir
[1].cmd
[0] |= (reg_count
& 0x3FFF) << 16;
2209 ((drm_r300_cmd_header_t
*) rmesa
->hw
.vir
[0].cmd
)->packet0
.count
= reg_count
;
2210 ((drm_r300_cmd_header_t
*) rmesa
->hw
.vir
[1].cmd
)->packet0
.count
= reg_count
;
2213 rmesa
->hw
.vic
.cmd
[R300_VIC_CNTL_0
] = r300VAPInputCntl0(ctx
, InputsRead
);
2214 rmesa
->hw
.vic
.cmd
[R300_VIC_CNTL_1
] = r300VAPInputCntl1(ctx
, InputsRead
);
2215 rmesa
->hw
.vof
.cmd
[R300_VOF_CNTL_0
] = r300VAPOutputCntl0(ctx
, OutputsWritten
);
2216 rmesa
->hw
.vof
.cmd
[R300_VOF_CNTL_1
] = r300VAPOutputCntl1(ctx
, OutputsWritten
);
2219 void r300UpdateShaderStates(r300ContextPtr rmesa
)
2222 ctx
= rmesa
->radeon
.glCtx
;
2224 /* should only happenen once, just after context is created */
2225 if (!ctx
->FragmentProgram
._Current
)
2228 r300SetEarlyZState(ctx
);
2230 r300SetupTextures(ctx
);
2232 rmesa
->vtbl
.SetupPixelShader(ctx
);
2234 rmesa
->vtbl
.SetupRSUnit(ctx
);
2236 if (rmesa
->options
.hw_tcl_enabled
) {
2237 r300SetupVertexProgram(rmesa
);
2241 #define EASY_US_OUT_FMT(comps, c0, c1, c2, c3) \
2242 (R500_OUT_FMT_##comps | R500_C0_SEL_##c0 | R500_C1_SEL_##c1 | \
2243 R500_C2_SEL_##c2 | R500_C3_SEL_##c3)
2244 static void r300SetupUsOutputFormat(GLcontext
*ctx
)
2246 r300ContextPtr rmesa
= R300_CONTEXT(ctx
);
2248 struct radeon_renderbuffer
*rrb
= radeon_get_colorbuffer(&rmesa
->radeon
);
2254 switch (rrb
->base
.Format
)
2256 case MESA_FORMAT_RGBA5551
:
2257 case MESA_FORMAT_RGBA8888
:
2258 hw_format
= EASY_US_OUT_FMT(C4_8
, A
, B
, G
, R
);
2260 case MESA_FORMAT_RGB565_REV
:
2261 case MESA_FORMAT_RGBA8888_REV
:
2262 hw_format
= EASY_US_OUT_FMT(C4_8
, R
, G
, B
, A
);
2264 case MESA_FORMAT_RGB565
:
2265 case MESA_FORMAT_ARGB4444
:
2266 case MESA_FORMAT_ARGB1555
:
2267 case MESA_FORMAT_XRGB8888
:
2268 case MESA_FORMAT_ARGB8888
:
2269 hw_format
= EASY_US_OUT_FMT(C4_8
, B
, G
, R
, A
);
2271 case MESA_FORMAT_ARGB4444_REV
:
2272 case MESA_FORMAT_ARGB1555_REV
:
2273 case MESA_FORMAT_XRGB8888_REV
:
2274 case MESA_FORMAT_ARGB8888_REV
:
2275 hw_format
= EASY_US_OUT_FMT(C4_8
, A
, R
, G
, B
);
2277 case MESA_FORMAT_SRGBA8
:
2278 hw_format
= EASY_US_OUT_FMT(C4_10_GAMMA
, A
, B
, G
, R
);
2280 case MESA_FORMAT_SARGB8
:
2281 hw_format
= EASY_US_OUT_FMT(C4_10_GAMMA
, B
, G
, R
, A
);
2283 case MESA_FORMAT_SL8
:
2284 hw_format
= EASY_US_OUT_FMT(C4_10_GAMMA
, A
, A
, R
, A
);
2286 case MESA_FORMAT_A8
:
2287 hw_format
= EASY_US_OUT_FMT(C4_8
, A
, A
, A
, A
);
2289 case MESA_FORMAT_L8
:
2290 case MESA_FORMAT_I8
:
2291 hw_format
= EASY_US_OUT_FMT(C4_8
, A
, A
, R
, A
);
2294 assert(!"Unsupported format");
2298 R300_STATECHANGE(rmesa
, us_out_fmt
);
2299 rmesa
->hw
.us_out_fmt
.cmd
[1] = hw_format
;
2301 #undef EASY_US_OUT_FMT
2304 * Called by Mesa after an internal state update.
2306 static void r300InvalidateState(GLcontext
* ctx
, GLuint new_state
)
2308 r300ContextPtr r300
= R300_CONTEXT(ctx
);
2310 _swrast_InvalidateState(ctx
, new_state
);
2311 _swsetup_InvalidateState(ctx
, new_state
);
2312 _vbo_InvalidateState(ctx
, new_state
);
2313 _tnl_InvalidateState(ctx
, new_state
);
2315 if (new_state
& _NEW_BUFFERS
) {
2316 _mesa_update_framebuffer(ctx
);
2317 /* this updates the DrawBuffer's Width/Height if it's a FBO */
2318 _mesa_update_draw_buffer_bounds(ctx
);
2320 R300_STATECHANGE(r300
, cb
);
2321 R300_STATECHANGE(r300
, zb
);
2324 if (new_state
& (_NEW_LIGHT
)) {
2325 R300_STATECHANGE(r300
, shade2
);
2326 if (ctx
->Light
.ProvokingVertex
== GL_LAST_VERTEX_CONVENTION
)
2327 r300
->hw
.shade2
.cmd
[1] |= R300_GA_COLOR_CONTROL_PROVOKING_VERTEX_LAST
;
2329 r300
->hw
.shade2
.cmd
[1] &= ~R300_GA_COLOR_CONTROL_PROVOKING_VERTEX_LAST
;
2332 if (new_state
& _NEW_BUFFERS
) {
2333 r300SetupUsOutputFormat(ctx
);
2336 r300
->radeon
.NewGLState
|= new_state
;
2340 * Calculate initial hardware state and register state functions.
2341 * Assumes that the command buffer and state atoms have been
2342 * initialized already.
2344 void r300InitState(r300ContextPtr r300
)
2346 r300ResetHwState(r300
);
2349 static void r300RenderMode(GLcontext
* ctx
, GLenum mode
)
2351 r300SwitchFallback(ctx
, R300_FALLBACK_RENDER_MODE
, ctx
->RenderMode
!= GL_RENDER
);
2355 * Initialize driver's state callback functions
2357 void r300InitStateFuncs(struct dd_function_table
*functions
)
2360 functions
->UpdateState
= r300InvalidateState
;
2361 functions
->AlphaFunc
= r300AlphaFunc
;
2362 functions
->BlendColor
= r300BlendColor
;
2363 functions
->BlendEquationSeparate
= r300BlendEquationSeparate
;
2364 functions
->BlendFuncSeparate
= r300BlendFuncSeparate
;
2365 functions
->Enable
= r300Enable
;
2366 functions
->ColorMask
= r300ColorMask
;
2367 functions
->DepthFunc
= r300DepthFunc
;
2368 functions
->DepthMask
= r300DepthMask
;
2369 functions
->CullFace
= r300CullFace
;
2370 functions
->FrontFace
= r300FrontFace
;
2371 functions
->ShadeModel
= r300ShadeModel
;
2372 functions
->LogicOpcode
= r300LogicOpcode
;
2374 /* ARB_point_parameters */
2375 functions
->PointParameterfv
= r300PointParameter
;
2377 /* Stencil related */
2378 functions
->StencilFuncSeparate
= r300StencilFuncSeparate
;
2379 functions
->StencilMaskSeparate
= r300StencilMaskSeparate
;
2380 functions
->StencilOpSeparate
= r300StencilOpSeparate
;
2382 /* Viewport related */
2383 functions
->Viewport
= r300Viewport
;
2384 functions
->DepthRange
= r300DepthRange
;
2385 functions
->PointSize
= r300PointSize
;
2386 functions
->LineWidth
= r300LineWidth
;
2388 functions
->PolygonOffset
= r300PolygonOffset
;
2389 functions
->PolygonMode
= r300PolygonMode
;
2391 functions
->RenderMode
= r300RenderMode
;
2393 functions
->ClipPlane
= r300ClipPlane
;
2394 functions
->Scissor
= radeonScissor
;
2396 functions
->DrawBuffer
= radeonDrawBuffer
;
2397 functions
->ReadBuffer
= radeonReadBuffer
;
2399 functions
->CopyPixels
= _mesa_meta_CopyPixels
;
2400 functions
->DrawPixels
= _mesa_meta_DrawPixels
;
2401 functions
->ReadPixels
= radeonReadPixels
;
2404 void r300InitShaderFunctions(r300ContextPtr r300
)
2406 if (r300
->radeon
.radeonScreen
->chip_family
>= CHIP_FAMILY_RV515
) {
2407 r300
->vtbl
.SetupRSUnit
= r500SetupRSUnit
;
2408 r300
->vtbl
.SetupPixelShader
= r500SetupPixelShader
;
2409 r300
->vtbl
.SetupFragmentShaderTextures
= r500SetupFragmentShaderTextures
;
2411 r300
->vtbl
.SetupRSUnit
= r300SetupRSUnit
;
2412 r300
->vtbl
.SetupPixelShader
= r300SetupPixelShader
;
2413 r300
->vtbl
.SetupFragmentShaderTextures
= r300SetupFragmentShaderTextures
;