2 * Copyright 2008 Corbin Simpson <MostAwesomeDude@gmail.com>
3 * Copyright 2009 Marek Olšák <maraeo@gmail.com>
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * on the rights to use, copy, modify, merge, publish, distribute, sub
9 * license, and/or sell copies of the Software, and to permit persons to whom
10 * the Software is furnished to do so, subject to the following conditions:
12 * The above copyright notice and this permission notice (including the next
13 * paragraph) shall be included in all copies or substantial portions of the
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHOR(S) AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
20 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
21 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
22 * USE OR OTHER DEALINGS IN THE SOFTWARE. */
24 #include "draw/draw_context.h"
26 #include "util/u_math.h"
27 #include "util/u_memory.h"
28 #include "util/u_pack_color.h"
30 #include "tgsi/tgsi_parse.h"
32 #include "pipe/p_config.h"
34 #include "r300_context.h"
36 #include "r300_screen.h"
37 #include "r300_state_inlines.h"
41 /* r300_state: Functions used to intialize state context by translating
42 * Gallium state objects into semi-native r300 state objects. */
44 static boolean
blend_discard_if_src_alpha_0(unsigned srcRGB
, unsigned srcA
,
45 unsigned dstRGB
, unsigned dstA
)
47 /* If the blend equation is ADD or REVERSE_SUBTRACT,
48 * SRC_ALPHA == 0, and the following state is set, the colorbuffer
49 * will not be changed.
50 * Notice that the dst factors are the src factors inverted. */
51 return (srcRGB
== PIPE_BLENDFACTOR_SRC_ALPHA
||
52 srcRGB
== PIPE_BLENDFACTOR_SRC_ALPHA_SATURATE
||
53 srcRGB
== PIPE_BLENDFACTOR_ZERO
) &&
54 (srcA
== PIPE_BLENDFACTOR_SRC_COLOR
||
55 srcA
== PIPE_BLENDFACTOR_SRC_ALPHA
||
56 srcA
== PIPE_BLENDFACTOR_SRC_ALPHA_SATURATE
||
57 srcA
== PIPE_BLENDFACTOR_ZERO
) &&
58 (dstRGB
== PIPE_BLENDFACTOR_INV_SRC_ALPHA
||
59 dstRGB
== PIPE_BLENDFACTOR_ONE
) &&
60 (dstA
== PIPE_BLENDFACTOR_INV_SRC_COLOR
||
61 dstA
== PIPE_BLENDFACTOR_INV_SRC_ALPHA
||
62 dstA
== PIPE_BLENDFACTOR_ONE
);
65 static boolean
blend_discard_if_src_alpha_1(unsigned srcRGB
, unsigned srcA
,
66 unsigned dstRGB
, unsigned dstA
)
68 /* If the blend equation is ADD or REVERSE_SUBTRACT,
69 * SRC_ALPHA == 1, and the following state is set, the colorbuffer
70 * will not be changed.
71 * Notice that the dst factors are the src factors inverted. */
72 return (srcRGB
== PIPE_BLENDFACTOR_INV_SRC_ALPHA
||
73 srcRGB
== PIPE_BLENDFACTOR_ZERO
) &&
74 (srcA
== PIPE_BLENDFACTOR_INV_SRC_COLOR
||
75 srcA
== PIPE_BLENDFACTOR_INV_SRC_ALPHA
||
76 srcA
== PIPE_BLENDFACTOR_ZERO
) &&
77 (dstRGB
== PIPE_BLENDFACTOR_SRC_ALPHA
||
78 dstRGB
== PIPE_BLENDFACTOR_ONE
) &&
79 (dstA
== PIPE_BLENDFACTOR_SRC_COLOR
||
80 dstA
== PIPE_BLENDFACTOR_SRC_ALPHA
||
81 dstA
== PIPE_BLENDFACTOR_ONE
);
84 static boolean
blend_discard_if_src_color_0(unsigned srcRGB
, unsigned srcA
,
85 unsigned dstRGB
, unsigned dstA
)
87 /* If the blend equation is ADD or REVERSE_SUBTRACT,
88 * SRC_COLOR == (0,0,0), and the following state is set, the colorbuffer
89 * will not be changed.
90 * Notice that the dst factors are the src factors inverted. */
91 return (srcRGB
== PIPE_BLENDFACTOR_SRC_COLOR
||
92 srcRGB
== PIPE_BLENDFACTOR_ZERO
) &&
93 (srcA
== PIPE_BLENDFACTOR_ZERO
) &&
94 (dstRGB
== PIPE_BLENDFACTOR_INV_SRC_COLOR
||
95 dstRGB
== PIPE_BLENDFACTOR_ONE
) &&
96 (dstA
== PIPE_BLENDFACTOR_ONE
);
99 static boolean
blend_discard_if_src_color_1(unsigned srcRGB
, unsigned srcA
,
100 unsigned dstRGB
, unsigned dstA
)
102 /* If the blend equation is ADD or REVERSE_SUBTRACT,
103 * SRC_COLOR == (1,1,1), and the following state is set, the colorbuffer
104 * will not be changed.
105 * Notice that the dst factors are the src factors inverted. */
106 return (srcRGB
== PIPE_BLENDFACTOR_INV_SRC_COLOR
||
107 srcRGB
== PIPE_BLENDFACTOR_ZERO
) &&
108 (srcA
== PIPE_BLENDFACTOR_ZERO
) &&
109 (dstRGB
== PIPE_BLENDFACTOR_SRC_COLOR
||
110 dstRGB
== PIPE_BLENDFACTOR_ONE
) &&
111 (dstA
== PIPE_BLENDFACTOR_ONE
);
114 static boolean
blend_discard_if_src_alpha_color_0(unsigned srcRGB
, unsigned srcA
,
115 unsigned dstRGB
, unsigned dstA
)
117 /* If the blend equation is ADD or REVERSE_SUBTRACT,
118 * SRC_ALPHA_COLOR == (0,0,0,0), and the following state is set,
119 * the colorbuffer will not be changed.
120 * Notice that the dst factors are the src factors inverted. */
121 return (srcRGB
== PIPE_BLENDFACTOR_SRC_COLOR
||
122 srcRGB
== PIPE_BLENDFACTOR_SRC_ALPHA
||
123 srcRGB
== PIPE_BLENDFACTOR_SRC_ALPHA_SATURATE
||
124 srcRGB
== PIPE_BLENDFACTOR_ZERO
) &&
125 (srcA
== PIPE_BLENDFACTOR_SRC_COLOR
||
126 srcA
== PIPE_BLENDFACTOR_SRC_ALPHA
||
127 srcA
== PIPE_BLENDFACTOR_SRC_ALPHA_SATURATE
||
128 srcA
== PIPE_BLENDFACTOR_ZERO
) &&
129 (dstRGB
== PIPE_BLENDFACTOR_INV_SRC_COLOR
||
130 dstRGB
== PIPE_BLENDFACTOR_INV_SRC_ALPHA
||
131 dstRGB
== PIPE_BLENDFACTOR_ONE
) &&
132 (dstA
== PIPE_BLENDFACTOR_INV_SRC_COLOR
||
133 dstA
== PIPE_BLENDFACTOR_INV_SRC_ALPHA
||
134 dstA
== PIPE_BLENDFACTOR_ONE
);
137 static boolean
blend_discard_if_src_alpha_color_1(unsigned srcRGB
, unsigned srcA
,
138 unsigned dstRGB
, unsigned dstA
)
140 /* If the blend equation is ADD or REVERSE_SUBTRACT,
141 * SRC_ALPHA_COLOR == (1,1,1,1), and the following state is set,
142 * the colorbuffer will not be changed.
143 * Notice that the dst factors are the src factors inverted. */
144 return (srcRGB
== PIPE_BLENDFACTOR_INV_SRC_COLOR
||
145 srcRGB
== PIPE_BLENDFACTOR_INV_SRC_ALPHA
||
146 srcRGB
== PIPE_BLENDFACTOR_ZERO
) &&
147 (srcA
== PIPE_BLENDFACTOR_INV_SRC_COLOR
||
148 srcA
== PIPE_BLENDFACTOR_INV_SRC_ALPHA
||
149 srcA
== PIPE_BLENDFACTOR_ZERO
) &&
150 (dstRGB
== PIPE_BLENDFACTOR_SRC_COLOR
||
151 dstRGB
== PIPE_BLENDFACTOR_SRC_ALPHA
||
152 dstRGB
== PIPE_BLENDFACTOR_ONE
) &&
153 (dstA
== PIPE_BLENDFACTOR_SRC_COLOR
||
154 dstA
== PIPE_BLENDFACTOR_SRC_ALPHA
||
155 dstA
== PIPE_BLENDFACTOR_ONE
);
158 /* Create a new blend state based on the CSO blend state.
160 * This encompasses alpha blending, logic/raster ops, and blend dithering. */
161 static void* r300_create_blend_state(struct pipe_context
* pipe
,
162 const struct pipe_blend_state
* state
)
164 struct r300_blend_state
* blend
= CALLOC_STRUCT(r300_blend_state
);
166 if (state
->rt
[0].blend_enable
)
168 unsigned eqRGB
= state
->rt
[0].rgb_func
;
169 unsigned srcRGB
= state
->rt
[0].rgb_src_factor
;
170 unsigned dstRGB
= state
->rt
[0].rgb_dst_factor
;
172 unsigned eqA
= state
->rt
[0].alpha_func
;
173 unsigned srcA
= state
->rt
[0].alpha_src_factor
;
174 unsigned dstA
= state
->rt
[0].alpha_dst_factor
;
176 /* despite the name, ALPHA_BLEND_ENABLE has nothing to do with alpha,
177 * this is just the crappy D3D naming */
178 blend
->blend_control
= R300_ALPHA_BLEND_ENABLE
|
179 r300_translate_blend_function(eqRGB
) |
180 ( r300_translate_blend_factor(srcRGB
) << R300_SRC_BLEND_SHIFT
) |
181 ( r300_translate_blend_factor(dstRGB
) << R300_DST_BLEND_SHIFT
);
183 /* Optimization: some operations do not require the destination color.
185 * When SRC_ALPHA_SATURATE is used, colorbuffer reads must be enabled,
186 * otherwise blending gives incorrect results. It seems to be
188 if (eqRGB
== PIPE_BLEND_MIN
|| eqA
== PIPE_BLEND_MIN
||
189 eqRGB
== PIPE_BLEND_MAX
|| eqA
== PIPE_BLEND_MAX
||
190 dstRGB
!= PIPE_BLENDFACTOR_ZERO
||
191 dstA
!= PIPE_BLENDFACTOR_ZERO
||
192 srcRGB
== PIPE_BLENDFACTOR_DST_COLOR
||
193 srcRGB
== PIPE_BLENDFACTOR_DST_ALPHA
||
194 srcRGB
== PIPE_BLENDFACTOR_INV_DST_COLOR
||
195 srcRGB
== PIPE_BLENDFACTOR_INV_DST_ALPHA
||
196 srcA
== PIPE_BLENDFACTOR_DST_COLOR
||
197 srcA
== PIPE_BLENDFACTOR_DST_ALPHA
||
198 srcA
== PIPE_BLENDFACTOR_INV_DST_COLOR
||
199 srcA
== PIPE_BLENDFACTOR_INV_DST_ALPHA
||
200 srcRGB
== PIPE_BLENDFACTOR_SRC_ALPHA_SATURATE
) {
201 /* Enable reading from the colorbuffer. */
202 blend
->blend_control
|= R300_READ_ENABLE
;
204 if (r300_screen(r300_context(pipe
)->context
.screen
)->caps
->is_r500
) {
205 /* Optimization: Depending on incoming pixels, we can
206 * conditionally disable the reading in hardware... */
207 if (eqRGB
!= PIPE_BLEND_MIN
&& eqA
!= PIPE_BLEND_MIN
&&
208 eqRGB
!= PIPE_BLEND_MAX
&& eqA
!= PIPE_BLEND_MAX
) {
209 /* Disable reading if SRC_ALPHA == 0. */
210 if ((dstRGB
== PIPE_BLENDFACTOR_SRC_ALPHA
||
211 dstRGB
== PIPE_BLENDFACTOR_ZERO
) &&
212 (dstA
== PIPE_BLENDFACTOR_SRC_COLOR
||
213 dstA
== PIPE_BLENDFACTOR_SRC_ALPHA
||
214 dstA
== PIPE_BLENDFACTOR_ZERO
)) {
215 blend
->blend_control
|= R500_SRC_ALPHA_0_NO_READ
;
218 /* Disable reading if SRC_ALPHA == 1. */
219 if ((dstRGB
== PIPE_BLENDFACTOR_INV_SRC_ALPHA
||
220 dstRGB
== PIPE_BLENDFACTOR_ZERO
) &&
221 (dstA
== PIPE_BLENDFACTOR_INV_SRC_COLOR
||
222 dstA
== PIPE_BLENDFACTOR_INV_SRC_ALPHA
||
223 dstA
== PIPE_BLENDFACTOR_ZERO
)) {
224 blend
->blend_control
|= R500_SRC_ALPHA_1_NO_READ
;
230 /* Optimization: discard pixels which don't change the colorbuffer.
232 * The code below is non-trivial and some math is involved.
234 * Discarding pixels must be disabled when FP16 AA is enabled.
235 * This is a hardware bug. Also, this implementation wouldn't work
236 * with FP blending enabled and equation clamping disabled.
238 * Equations other than ADD are rarely used and therefore won't be
240 if ((eqRGB
== PIPE_BLEND_ADD
|| eqRGB
== PIPE_BLEND_REVERSE_SUBTRACT
) &&
241 (eqA
== PIPE_BLEND_ADD
|| eqA
== PIPE_BLEND_REVERSE_SUBTRACT
)) {
243 * REVERSE_SUBTRACT: Y-X
246 * If X = src*srcFactor = 0 and Y = dst*dstFactor = 1,
247 * then CB will not be changed.
249 * Given the srcFactor and dstFactor variables, we can derive
250 * what src and dst should be equal to and discard appropriate
253 if (blend_discard_if_src_alpha_0(srcRGB
, srcA
, dstRGB
, dstA
)) {
254 blend
->blend_control
|= R300_DISCARD_SRC_PIXELS_SRC_ALPHA_0
;
255 } else if (blend_discard_if_src_alpha_1(srcRGB
, srcA
,
257 blend
->blend_control
|= R300_DISCARD_SRC_PIXELS_SRC_ALPHA_1
;
258 } else if (blend_discard_if_src_color_0(srcRGB
, srcA
,
260 blend
->blend_control
|= R300_DISCARD_SRC_PIXELS_SRC_COLOR_0
;
261 } else if (blend_discard_if_src_color_1(srcRGB
, srcA
,
263 blend
->blend_control
|= R300_DISCARD_SRC_PIXELS_SRC_COLOR_1
;
264 } else if (blend_discard_if_src_alpha_color_0(srcRGB
, srcA
,
266 blend
->blend_control
|=
267 R300_DISCARD_SRC_PIXELS_SRC_ALPHA_COLOR_0
;
268 } else if (blend_discard_if_src_alpha_color_1(srcRGB
, srcA
,
270 blend
->blend_control
|=
271 R300_DISCARD_SRC_PIXELS_SRC_ALPHA_COLOR_1
;
276 if (srcA
!= srcRGB
|| dstA
!= dstRGB
|| eqA
!= eqRGB
) {
277 blend
->blend_control
|= R300_SEPARATE_ALPHA_ENABLE
;
278 blend
->alpha_blend_control
=
279 r300_translate_blend_function(eqA
) |
280 (r300_translate_blend_factor(srcA
) << R300_SRC_BLEND_SHIFT
) |
281 (r300_translate_blend_factor(dstA
) << R300_DST_BLEND_SHIFT
);
285 /* PIPE_LOGICOP_* don't need to be translated, fortunately. */
286 if (state
->logicop_enable
) {
287 blend
->rop
= R300_RB3D_ROPCNTL_ROP_ENABLE
|
288 (state
->logicop_func
) << R300_RB3D_ROPCNTL_ROP_SHIFT
;
291 /* Color channel masks for all MRTs. */
292 blend
->color_channel_mask
= state
->rt
[0].colormask
;
293 if (state
->independent_blend_enable
) {
294 blend
->color_channel_mask
|= (state
->rt
[1].colormask
<< 4);
295 blend
->color_channel_mask
|= (state
->rt
[2].colormask
<< 8);
296 blend
->color_channel_mask
|= (state
->rt
[3].colormask
<< 12);
300 blend
->dither
= R300_RB3D_DITHER_CTL_DITHER_MODE_LUT
|
301 R300_RB3D_DITHER_CTL_ALPHA_DITHER_MODE_LUT
;
307 /* Bind blend state. */
308 static void r300_bind_blend_state(struct pipe_context
* pipe
,
311 struct r300_context
* r300
= r300_context(pipe
);
313 r300
->blend_state
.state
= state
;
314 r300
->blend_state
.dirty
= TRUE
;
317 /* Free blend state. */
318 static void r300_delete_blend_state(struct pipe_context
* pipe
,
324 /* Convert float to 10bit integer */
325 static unsigned float_to_fixed10(float f
)
327 return CLAMP((unsigned)(f
* 1023.9f
), 0, 1023);
331 * Setup both R300 and R500 registers, figure out later which one to write. */
332 static void r300_set_blend_color(struct pipe_context
* pipe
,
333 const struct pipe_blend_color
* color
)
335 struct r300_context
* r300
= r300_context(pipe
);
336 struct r300_screen
* r300screen
= r300_screen(pipe
->screen
);
337 struct r300_blend_color_state
* state
=
338 (struct r300_blend_color_state
*)r300
->blend_color_state
.state
;
341 util_pack_color(color
->color
, PIPE_FORMAT_A8R8G8B8_UNORM
, &uc
);
342 state
->blend_color
= uc
.ui
;
344 /* XXX if FP16 blending is enabled, we should use the FP16 format */
345 state
->blend_color_red_alpha
=
346 float_to_fixed10(color
->color
[0]) |
347 (float_to_fixed10(color
->color
[3]) << 16);
348 state
->blend_color_green_blue
=
349 float_to_fixed10(color
->color
[2]) |
350 (float_to_fixed10(color
->color
[1]) << 16);
352 r300
->blend_color_state
.size
= r300screen
->caps
->is_r500
? 3 : 2;
353 r300
->blend_color_state
.dirty
= TRUE
;
356 static void r300_set_clip_state(struct pipe_context
* pipe
,
357 const struct pipe_clip_state
* state
)
359 struct r300_context
* r300
= r300_context(pipe
);
361 if (r300_screen(pipe
->screen
)->caps
->has_tcl
) {
362 memcpy(r300
->clip_state
.state
, state
, sizeof(struct pipe_clip_state
));
363 r300
->clip_state
.size
= 29;
365 draw_flush(r300
->draw
);
366 draw_set_clip_state(r300
->draw
, state
);
367 r300
->clip_state
.size
= 2;
370 r300
->clip_state
.dirty
= TRUE
;
373 /* Create a new depth, stencil, and alpha state based on the CSO dsa state.
375 * This contains the depth buffer, stencil buffer, alpha test, and such.
376 * On the Radeon, depth and stencil buffer setup are intertwined, which is
377 * the reason for some of the strange-looking assignments across registers. */
379 r300_create_dsa_state(struct pipe_context
* pipe
,
380 const struct pipe_depth_stencil_alpha_state
* state
)
382 struct r300_capabilities
*caps
=
383 r300_screen(r300_context(pipe
)->context
.screen
)->caps
;
384 struct r300_dsa_state
* dsa
= CALLOC_STRUCT(r300_dsa_state
);
386 /* Depth test setup. */
387 if (state
->depth
.enabled
) {
388 dsa
->z_buffer_control
|= R300_Z_ENABLE
;
390 if (state
->depth
.writemask
) {
391 dsa
->z_buffer_control
|= R300_Z_WRITE_ENABLE
;
394 dsa
->z_stencil_control
|=
395 (r300_translate_depth_stencil_function(state
->depth
.func
) <<
399 /* Stencil buffer setup. */
400 if (state
->stencil
[0].enabled
) {
401 dsa
->z_buffer_control
|= R300_STENCIL_ENABLE
;
402 dsa
->z_stencil_control
|=
403 (r300_translate_depth_stencil_function(state
->stencil
[0].func
) <<
404 R300_S_FRONT_FUNC_SHIFT
) |
405 (r300_translate_stencil_op(state
->stencil
[0].fail_op
) <<
406 R300_S_FRONT_SFAIL_OP_SHIFT
) |
407 (r300_translate_stencil_op(state
->stencil
[0].zpass_op
) <<
408 R300_S_FRONT_ZPASS_OP_SHIFT
) |
409 (r300_translate_stencil_op(state
->stencil
[0].zfail_op
) <<
410 R300_S_FRONT_ZFAIL_OP_SHIFT
);
412 dsa
->stencil_ref_mask
= (state
->stencil
[0].ref_value
) |
413 (state
->stencil
[0].valuemask
<< R300_STENCILMASK_SHIFT
) |
414 (state
->stencil
[0].writemask
<< R300_STENCILWRITEMASK_SHIFT
);
416 if (state
->stencil
[1].enabled
) {
417 dsa
->z_buffer_control
|= R300_STENCIL_FRONT_BACK
;
418 dsa
->z_stencil_control
|=
419 (r300_translate_depth_stencil_function(state
->stencil
[1].func
) <<
420 R300_S_BACK_FUNC_SHIFT
) |
421 (r300_translate_stencil_op(state
->stencil
[1].fail_op
) <<
422 R300_S_BACK_SFAIL_OP_SHIFT
) |
423 (r300_translate_stencil_op(state
->stencil
[1].zpass_op
) <<
424 R300_S_BACK_ZPASS_OP_SHIFT
) |
425 (r300_translate_stencil_op(state
->stencil
[1].zfail_op
) <<
426 R300_S_BACK_ZFAIL_OP_SHIFT
);
428 /* XXX it seems r3xx doesn't support STENCILREFMASK_BF */
431 dsa
->z_buffer_control
|= R500_STENCIL_REFMASK_FRONT_BACK
;
432 dsa
->stencil_ref_bf
= (state
->stencil
[1].ref_value
) |
433 (state
->stencil
[1].valuemask
<<
434 R300_STENCILMASK_SHIFT
) |
435 (state
->stencil
[1].writemask
<<
436 R300_STENCILWRITEMASK_SHIFT
);
441 /* Alpha test setup. */
442 if (state
->alpha
.enabled
) {
443 dsa
->alpha_function
=
444 r300_translate_alpha_function(state
->alpha
.func
) |
445 R300_FG_ALPHA_FUNC_ENABLE
;
447 /* XXX figure out why emitting 10bit alpha ref causes CS to dump */
448 /* always use 8bit alpha ref */
449 dsa
->alpha_function
|= float_to_ubyte(state
->alpha
.ref_value
);
452 dsa
->alpha_function
|= R500_FG_ALPHA_FUNC_8BIT
;
458 /* Bind DSA state. */
459 static void r300_bind_dsa_state(struct pipe_context
* pipe
,
462 struct r300_context
* r300
= r300_context(pipe
);
463 struct r300_screen
* r300screen
= r300_screen(pipe
->screen
);
465 r300
->dsa_state
.state
= state
;
466 r300
->dsa_state
.size
= r300screen
->caps
->is_r500
? 8 : 6;
467 r300
->dsa_state
.dirty
= TRUE
;
470 /* Free DSA state. */
471 static void r300_delete_dsa_state(struct pipe_context
* pipe
,
478 r300_set_framebuffer_state(struct pipe_context
* pipe
,
479 const struct pipe_framebuffer_state
* state
)
481 struct r300_context
* r300
= r300_context(pipe
);
482 uint32_t zbuffer_bpp
= 0;
484 r300
->fb_state
.size
= (10 * state
->nr_cbufs
) +
485 (2 * (4 - state
->nr_cbufs
)) +
486 (state
->zsbuf
? 10 : 0) + 6;
488 if (state
->nr_cbufs
> 4) {
489 debug_printf("r300: Implementation error: Too many MRTs in %s, "
490 "refusing to bind framebuffer state!\n", __FUNCTION__
);
495 draw_flush(r300
->draw
);
498 r300
->fb_state
.state
= state
;
500 /* Don't rely on the order of states being set for the first time. */
502 r300
->blend_state
.dirty
= TRUE
;
503 r300
->dsa_state
.dirty
= TRUE
;
504 r300
->fb_state
.dirty
= TRUE
;
505 r300
->scissor_state
.dirty
= TRUE
;
507 /* Polygon offset depends on the zbuffer bit depth. */
508 if (state
->zsbuf
&& r300
->polygon_offset_enabled
) {
509 switch (util_format_get_blocksize(state
->zsbuf
->texture
->format
)) {
518 if (r300
->zbuffer_bpp
!= zbuffer_bpp
) {
519 r300
->zbuffer_bpp
= zbuffer_bpp
;
520 r300
->rs_state
.dirty
= TRUE
;
525 /* Create fragment shader state. */
526 static void* r300_create_fs_state(struct pipe_context
* pipe
,
527 const struct pipe_shader_state
* shader
)
529 struct r300_fragment_shader
* fs
= NULL
;
531 fs
= (struct r300_fragment_shader
*)CALLOC_STRUCT(r300_fragment_shader
);
533 /* Copy state directly into shader. */
535 fs
->state
.tokens
= tgsi_dup_tokens(shader
->tokens
);
537 tgsi_scan_shader(shader
->tokens
, &fs
->info
);
538 r300_shader_read_fs_inputs(&fs
->info
, &fs
->inputs
);
543 /* Bind fragment shader state. */
544 static void r300_bind_fs_state(struct pipe_context
* pipe
, void* shader
)
546 struct r300_context
* r300
= r300_context(pipe
);
547 struct r300_fragment_shader
* fs
= (struct r300_fragment_shader
*)shader
;
555 r300_pick_fragment_shader(r300
);
557 if (r300
->vs
&& r300_vertex_shader_setup_wpos(r300
)) {
558 r300
->vertex_format_state
.dirty
= TRUE
;
561 r300
->dirty_state
|= R300_NEW_FRAGMENT_SHADER
| R300_NEW_FRAGMENT_SHADER_CONSTANTS
;
564 /* Delete fragment shader state. */
565 static void r300_delete_fs_state(struct pipe_context
* pipe
, void* shader
)
567 struct r300_fragment_shader
* fs
= (struct r300_fragment_shader
*)shader
;
568 struct r300_fragment_shader_code
*tmp
, *ptr
= fs
->first
;
573 rc_constants_destroy(&tmp
->code
.constants
);
576 FREE((void*)fs
->state
.tokens
);
580 static void r300_set_polygon_stipple(struct pipe_context
* pipe
,
581 const struct pipe_poly_stipple
* state
)
583 /* XXX no idea how to set this up, but not terribly important */
586 /* Create a new rasterizer state based on the CSO rasterizer state.
588 * This is a very large chunk of state, and covers most of the graphics
589 * backend (GB), geometry assembly (GA), and setup unit (SU) blocks.
591 * In a not entirely unironic sidenote, this state has nearly nothing to do
592 * with the actual block on the Radeon called the rasterizer (RS). */
593 static void* r300_create_rs_state(struct pipe_context
* pipe
,
594 const struct pipe_rasterizer_state
* state
)
596 struct r300_rs_state
* rs
= CALLOC_STRUCT(r300_rs_state
);
598 /* Copy rasterizer state for Draw. */
601 #ifdef PIPE_ARCH_LITTLE_ENDIAN
602 rs
->vap_control_status
= R300_VC_NO_SWAP
;
604 rs
->vap_control_status
= R300_VC_32BIT_SWAP
;
607 /* If bypassing TCL, or if no TCL engine is present, turn off the HW TCL.
608 * Else, enable HW TCL and force Draw's TCL off. */
609 if (state
->bypass_vs_clip_and_viewport
||
610 !r300_screen(pipe
->screen
)->caps
->has_tcl
) {
611 rs
->vap_control_status
|= R300_VAP_TCL_BYPASS
;
614 rs
->point_size
= pack_float_16_6x(state
->point_size
) |
615 (pack_float_16_6x(state
->point_size
) << R300_POINTSIZE_X_SHIFT
);
618 ((int)(state
->point_size_min
* 6.0) <<
619 R300_GA_POINT_MINMAX_MIN_SHIFT
) |
620 ((int)(state
->point_size_max
* 6.0) <<
621 R300_GA_POINT_MINMAX_MAX_SHIFT
);
623 rs
->line_control
= pack_float_16_6x(state
->line_width
) |
624 R300_GA_LINE_CNTL_END_TYPE_COMP
;
626 /* Enable polygon mode */
627 if (state
->fill_cw
!= PIPE_POLYGON_MODE_FILL
||
628 state
->fill_ccw
!= PIPE_POLYGON_MODE_FILL
) {
629 rs
->polygon_mode
= R300_GA_POLY_MODE_DUAL
;
632 /* Radeons don't think in "CW/CCW", they think in "front/back". */
633 if (state
->front_winding
== PIPE_WINDING_CW
) {
634 rs
->cull_mode
= R300_FRONT_FACE_CW
;
637 if (state
->offset_cw
) {
638 rs
->polygon_offset_enable
|= R300_FRONT_ENABLE
;
640 if (state
->offset_ccw
) {
641 rs
->polygon_offset_enable
|= R300_BACK_ENABLE
;
645 if (rs
->polygon_mode
) {
647 r300_translate_polygon_mode_front(state
->fill_cw
);
649 r300_translate_polygon_mode_back(state
->fill_ccw
);
652 rs
->cull_mode
= R300_FRONT_FACE_CCW
;
655 if (state
->offset_ccw
) {
656 rs
->polygon_offset_enable
|= R300_FRONT_ENABLE
;
658 if (state
->offset_cw
) {
659 rs
->polygon_offset_enable
|= R300_BACK_ENABLE
;
663 if (rs
->polygon_mode
) {
665 r300_translate_polygon_mode_front(state
->fill_ccw
);
667 r300_translate_polygon_mode_back(state
->fill_cw
);
670 if (state
->front_winding
& state
->cull_mode
) {
671 rs
->cull_mode
|= R300_CULL_FRONT
;
673 if (~(state
->front_winding
) & state
->cull_mode
) {
674 rs
->cull_mode
|= R300_CULL_BACK
;
677 if (rs
->polygon_offset_enable
) {
678 rs
->depth_offset
= state
->offset_units
;
679 rs
->depth_scale
= state
->offset_scale
;
682 if (state
->line_stipple_enable
) {
683 rs
->line_stipple_config
=
684 R300_GA_LINE_STIPPLE_CONFIG_LINE_RESET_LINE
|
685 (fui((float)state
->line_stipple_factor
) &
686 R300_GA_LINE_STIPPLE_CONFIG_STIPPLE_SCALE_MASK
);
687 /* XXX this might need to be scaled up */
688 rs
->line_stipple_value
= state
->line_stipple_pattern
;
691 if (state
->flatshade
) {
692 rs
->color_control
= R300_SHADE_MODEL_FLAT
;
694 rs
->color_control
= R300_SHADE_MODEL_SMOOTH
;
700 /* Bind rasterizer state. */
701 static void r300_bind_rs_state(struct pipe_context
* pipe
, void* state
)
703 struct r300_context
* r300
= r300_context(pipe
);
704 struct r300_rs_state
* rs
= (struct r300_rs_state
*)state
;
707 draw_flush(r300
->draw
);
708 draw_set_rasterizer_state(r300
->draw
, &rs
->rs
);
712 r300
->tcl_bypass
= rs
->rs
.bypass_vs_clip_and_viewport
;
713 r300
->polygon_offset_enabled
= rs
->rs
.offset_cw
|| rs
->rs
.offset_ccw
;
715 r300
->tcl_bypass
= FALSE
;
716 r300
->polygon_offset_enabled
= FALSE
;
719 r300
->rs_state
.state
= rs
;
720 r300
->rs_state
.dirty
= TRUE
;
721 /* XXX Why is this still needed, dammit!? */
722 r300
->scissor_state
.dirty
= TRUE
;
723 r300
->viewport_state
.dirty
= TRUE
;
725 /* XXX Clean these up when we move to atom emits */
726 if (r300
->fs
&& r300
->fs
->inputs
.wpos
!= ATTR_UNUSED
) {
727 r300
->dirty_state
|= R300_NEW_FRAGMENT_SHADER_CONSTANTS
;
731 /* Free rasterizer state. */
732 static void r300_delete_rs_state(struct pipe_context
* pipe
, void* state
)
738 r300_create_sampler_state(struct pipe_context
* pipe
,
739 const struct pipe_sampler_state
* state
)
741 struct r300_context
* r300
= r300_context(pipe
);
742 struct r300_sampler_state
* sampler
= CALLOC_STRUCT(r300_sampler_state
);
746 sampler
->state
= *state
;
749 (r300_translate_wrap(state
->wrap_s
) << R300_TX_WRAP_S_SHIFT
) |
750 (r300_translate_wrap(state
->wrap_t
) << R300_TX_WRAP_T_SHIFT
) |
751 (r300_translate_wrap(state
->wrap_r
) << R300_TX_WRAP_R_SHIFT
);
753 sampler
->filter0
|= r300_translate_tex_filters(state
->min_img_filter
,
754 state
->mag_img_filter
,
755 state
->min_mip_filter
,
756 state
->max_anisotropy
> 1.0);
758 /* Unfortunately, r300-r500 don't support floating-point mipmap lods. */
759 /* We must pass these to the emit function to clamp them properly. */
760 sampler
->min_lod
= MAX2((unsigned)state
->min_lod
, 0);
761 sampler
->max_lod
= MAX2((unsigned)ceilf(state
->max_lod
), 0);
763 lod_bias
= CLAMP((int)(state
->lod_bias
* 32), -(1 << 9), (1 << 9) - 1);
765 sampler
->filter1
|= lod_bias
<< R300_LOD_BIAS_SHIFT
;
767 sampler
->filter1
|= r300_anisotropy(state
->max_anisotropy
);
769 util_pack_color(state
->border_color
, PIPE_FORMAT_A8R8G8B8_UNORM
, &uc
);
770 sampler
->border_color
= uc
.ui
;
772 /* R500-specific fixups and optimizations */
773 if (r300_screen(r300
->context
.screen
)->caps
->is_r500
) {
774 sampler
->filter1
|= R500_BORDER_FIX
;
777 return (void*)sampler
;
780 static void r300_bind_sampler_states(struct pipe_context
* pipe
,
784 struct r300_context
* r300
= r300_context(pipe
);
791 for (i
= 0; i
< count
; i
++) {
792 if (r300
->sampler_states
[i
] != states
[i
]) {
793 r300
->sampler_states
[i
] = (struct r300_sampler_state
*)states
[i
];
794 r300
->dirty_state
|= (R300_NEW_SAMPLER
<< i
);
798 r300
->sampler_count
= count
;
800 /* Pick a fragment shader based on the texture compare state. */
801 if (r300
->fs
&& (r300
->dirty_state
& R300_ANY_NEW_SAMPLERS
)) {
802 if (r300_pick_fragment_shader(r300
)) {
803 r300
->dirty_state
|= R300_NEW_FRAGMENT_SHADER
|
804 R300_NEW_FRAGMENT_SHADER_CONSTANTS
;
809 static void r300_lacks_vertex_textures(struct pipe_context
* pipe
,
815 static void r300_delete_sampler_state(struct pipe_context
* pipe
, void* state
)
820 static void r300_set_sampler_textures(struct pipe_context
* pipe
,
822 struct pipe_texture
** texture
)
824 struct r300_context
* r300
= r300_context(pipe
);
825 boolean is_r500
= r300_screen(r300
->context
.screen
)->caps
->is_r500
;
833 for (i
= 0; i
< count
; i
++) {
834 if (r300
->textures
[i
] != (struct r300_texture
*)texture
[i
]) {
835 pipe_texture_reference((struct pipe_texture
**)&r300
->textures
[i
],
837 r300
->dirty_state
|= (R300_NEW_TEXTURE
<< i
);
839 /* R300-specific - set the texrect factor in a fragment shader */
840 if (!is_r500
&& r300
->textures
[i
]->is_npot
) {
841 /* XXX It would be nice to re-emit just 1 constant,
842 * XXX not all of them */
843 r300
->dirty_state
|= R300_NEW_FRAGMENT_SHADER_CONSTANTS
;
848 for (i
= count
; i
< 8; i
++) {
849 if (r300
->textures
[i
]) {
850 pipe_texture_reference((struct pipe_texture
**)&r300
->textures
[i
],
852 r300
->dirty_state
|= (R300_NEW_TEXTURE
<< i
);
856 r300
->texture_count
= count
;
859 static void r300_set_scissor_state(struct pipe_context
* pipe
,
860 const struct pipe_scissor_state
* state
)
862 struct r300_context
* r300
= r300_context(pipe
);
864 memcpy(r300
->scissor_state
.state
, state
,
865 sizeof(struct pipe_scissor_state
));
867 r300
->scissor_state
.dirty
= TRUE
;
870 static void r300_set_viewport_state(struct pipe_context
* pipe
,
871 const struct pipe_viewport_state
* state
)
873 struct r300_context
* r300
= r300_context(pipe
);
874 struct r300_viewport_state
* viewport
=
875 (struct r300_viewport_state
*)r300
->viewport_state
.state
;
877 /* Do the transform in HW. */
878 viewport
->vte_control
= R300_VTX_W0_FMT
;
880 if (state
->scale
[0] != 1.0f
) {
881 viewport
->xscale
= state
->scale
[0];
882 viewport
->vte_control
|= R300_VPORT_X_SCALE_ENA
;
884 if (state
->scale
[1] != 1.0f
) {
885 viewport
->yscale
= state
->scale
[1];
886 viewport
->vte_control
|= R300_VPORT_Y_SCALE_ENA
;
888 if (state
->scale
[2] != 1.0f
) {
889 viewport
->zscale
= state
->scale
[2];
890 viewport
->vte_control
|= R300_VPORT_Z_SCALE_ENA
;
892 if (state
->translate
[0] != 0.0f
) {
893 viewport
->xoffset
= state
->translate
[0];
894 viewport
->vte_control
|= R300_VPORT_X_OFFSET_ENA
;
896 if (state
->translate
[1] != 0.0f
) {
897 viewport
->yoffset
= state
->translate
[1];
898 viewport
->vte_control
|= R300_VPORT_Y_OFFSET_ENA
;
900 if (state
->translate
[2] != 0.0f
) {
901 viewport
->zoffset
= state
->translate
[2];
902 viewport
->vte_control
|= R300_VPORT_Z_OFFSET_ENA
;
905 r300
->viewport_state
.dirty
= TRUE
;
906 if (r300
->fs
&& r300
->fs
->inputs
.wpos
!= ATTR_UNUSED
) {
907 r300
->dirty_state
|= R300_NEW_FRAGMENT_SHADER_CONSTANTS
;
911 static void r300_set_vertex_buffers(struct pipe_context
* pipe
,
913 const struct pipe_vertex_buffer
* buffers
)
915 struct r300_context
* r300
= r300_context(pipe
);
917 memcpy(r300
->vertex_buffer
, buffers
,
918 sizeof(struct pipe_vertex_buffer
) * count
);
919 r300
->vertex_buffer_count
= count
;
922 draw_flush(r300
->draw
);
923 draw_set_vertex_buffers(r300
->draw
, count
, buffers
);
926 r300
->vertex_format_state
.dirty
= TRUE
;
929 static boolean
r300_validate_aos(struct r300_context
*r300
)
931 struct pipe_vertex_buffer
*vbuf
= r300
->vertex_buffer
;
932 struct pipe_vertex_element
*velem
= r300
->vertex_element
;
935 /* Check if formats and strides are aligned to the size of DWORD. */
936 for (i
= 0; i
< r300
->vertex_element_count
; i
++) {
937 if (vbuf
[velem
[i
].vertex_buffer_index
].stride
% 4 != 0 ||
938 util_format_get_blocksize(velem
[i
].src_format
) % 4 != 0) {
945 static void r300_set_vertex_elements(struct pipe_context
* pipe
,
947 const struct pipe_vertex_element
* elements
)
949 struct r300_context
* r300
= r300_context(pipe
);
951 memcpy(r300
->vertex_element
,
953 sizeof(struct pipe_vertex_element
) * count
);
954 r300
->vertex_element_count
= count
;
957 draw_flush(r300
->draw
);
958 draw_set_vertex_elements(r300
->draw
, count
, elements
);
961 if (!r300_validate_aos(r300
)) {
962 /* XXX We should fallback using draw. */
968 static void* r300_create_vs_state(struct pipe_context
* pipe
,
969 const struct pipe_shader_state
* shader
)
971 struct r300_context
* r300
= r300_context(pipe
);
973 if (r300_screen(pipe
->screen
)->caps
->has_tcl
) {
974 struct r300_vertex_shader
* vs
= CALLOC_STRUCT(r300_vertex_shader
);
975 /* Copy state directly into shader. */
977 vs
->state
.tokens
= tgsi_dup_tokens(shader
->tokens
);
979 tgsi_scan_shader(shader
->tokens
, &vs
->info
);
983 return draw_create_vertex_shader(r300
->draw
, shader
);
987 static void r300_bind_vs_state(struct pipe_context
* pipe
, void* shader
)
989 struct r300_context
* r300
= r300_context(pipe
);
991 if (r300_screen(pipe
->screen
)->caps
->has_tcl
) {
992 struct r300_vertex_shader
* vs
= (struct r300_vertex_shader
*)shader
;
997 } else if (!vs
->translated
) {
998 r300_translate_vertex_shader(r300
, vs
);
1003 r300_vertex_shader_setup_wpos(r300
);
1006 r300
->vertex_format_state
.dirty
= TRUE
;
1008 r300
->dirty_state
|=
1009 R300_NEW_VERTEX_SHADER
| R300_NEW_VERTEX_SHADER_CONSTANTS
;
1011 draw_flush(r300
->draw
);
1012 draw_bind_vertex_shader(r300
->draw
,
1013 (struct draw_vertex_shader
*)shader
);
1017 static void r300_delete_vs_state(struct pipe_context
* pipe
, void* shader
)
1019 struct r300_context
* r300
= r300_context(pipe
);
1021 if (r300_screen(pipe
->screen
)->caps
->has_tcl
) {
1022 struct r300_vertex_shader
* vs
= (struct r300_vertex_shader
*)shader
;
1024 rc_constants_destroy(&vs
->code
.constants
);
1025 FREE((void*)vs
->state
.tokens
);
1028 draw_delete_vertex_shader(r300
->draw
,
1029 (struct draw_vertex_shader
*)shader
);
1033 static void r300_set_constant_buffer(struct pipe_context
*pipe
,
1034 uint shader
, uint index
,
1035 struct pipe_buffer
*buf
)
1037 struct r300_context
* r300
= r300_context(pipe
);
1040 if (buf
== NULL
|| buf
->size
== 0 ||
1041 (mapped
= pipe_buffer_map(pipe
->screen
, buf
, PIPE_BUFFER_USAGE_CPU_READ
)) == NULL
)
1043 r300
->shader_constants
[shader
].count
= 0;
1047 assert((buf
->size
% 4 * sizeof(float)) == 0);
1048 memcpy(r300
->shader_constants
[shader
].constants
, mapped
, buf
->size
);
1049 r300
->shader_constants
[shader
].count
= buf
->size
/ (4 * sizeof(float));
1050 pipe_buffer_unmap(pipe
->screen
, buf
);
1052 if (shader
== PIPE_SHADER_VERTEX
)
1053 r300
->dirty_state
|= R300_NEW_VERTEX_SHADER_CONSTANTS
;
1054 else if (shader
== PIPE_SHADER_FRAGMENT
)
1055 r300
->dirty_state
|= R300_NEW_FRAGMENT_SHADER_CONSTANTS
;
1058 void r300_init_state_functions(struct r300_context
* r300
)
1060 r300
->context
.create_blend_state
= r300_create_blend_state
;
1061 r300
->context
.bind_blend_state
= r300_bind_blend_state
;
1062 r300
->context
.delete_blend_state
= r300_delete_blend_state
;
1064 r300
->context
.set_blend_color
= r300_set_blend_color
;
1066 r300
->context
.set_clip_state
= r300_set_clip_state
;
1068 r300
->context
.set_constant_buffer
= r300_set_constant_buffer
;
1070 r300
->context
.create_depth_stencil_alpha_state
= r300_create_dsa_state
;
1071 r300
->context
.bind_depth_stencil_alpha_state
= r300_bind_dsa_state
;
1072 r300
->context
.delete_depth_stencil_alpha_state
= r300_delete_dsa_state
;
1074 r300
->context
.set_framebuffer_state
= r300_set_framebuffer_state
;
1076 r300
->context
.create_fs_state
= r300_create_fs_state
;
1077 r300
->context
.bind_fs_state
= r300_bind_fs_state
;
1078 r300
->context
.delete_fs_state
= r300_delete_fs_state
;
1080 r300
->context
.set_polygon_stipple
= r300_set_polygon_stipple
;
1082 r300
->context
.create_rasterizer_state
= r300_create_rs_state
;
1083 r300
->context
.bind_rasterizer_state
= r300_bind_rs_state
;
1084 r300
->context
.delete_rasterizer_state
= r300_delete_rs_state
;
1086 r300
->context
.create_sampler_state
= r300_create_sampler_state
;
1087 r300
->context
.bind_fragment_sampler_states
= r300_bind_sampler_states
;
1088 r300
->context
.bind_vertex_sampler_states
= r300_lacks_vertex_textures
;
1089 r300
->context
.delete_sampler_state
= r300_delete_sampler_state
;
1091 r300
->context
.set_fragment_sampler_textures
= r300_set_sampler_textures
;
1093 r300
->context
.set_scissor_state
= r300_set_scissor_state
;
1095 r300
->context
.set_viewport_state
= r300_set_viewport_state
;
1097 r300
->context
.set_vertex_buffers
= r300_set_vertex_buffers
;
1098 r300
->context
.set_vertex_elements
= r300_set_vertex_elements
;
1100 r300
->context
.create_vs_state
= r300_create_vs_state
;
1101 r300
->context
.bind_vs_state
= r300_bind_vs_state
;
1102 r300
->context
.delete_vs_state
= r300_delete_vs_state
;