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"
33 #include "pipe/internal/p_winsys_screen.h"
35 #include "r300_context.h"
37 #include "r300_screen.h"
38 #include "r300_state_inlines.h"
42 /* r300_state: Functions used to intialize state context by translating
43 * Gallium state objects into semi-native r300 state objects. */
45 static boolean
blend_discard_if_src_alpha_0(unsigned srcRGB
, unsigned srcA
,
46 unsigned dstRGB
, unsigned dstA
)
48 /* If the blend equation is ADD or REVERSE_SUBTRACT,
49 * SRC_ALPHA == 0, and the following state is set, the colorbuffer
50 * will not be changed.
51 * Notice that the dst factors are the src factors inverted. */
52 return (srcRGB
== PIPE_BLENDFACTOR_SRC_ALPHA
||
53 srcRGB
== PIPE_BLENDFACTOR_SRC_ALPHA_SATURATE
||
54 srcRGB
== PIPE_BLENDFACTOR_ZERO
) &&
55 (srcA
== PIPE_BLENDFACTOR_SRC_COLOR
||
56 srcA
== PIPE_BLENDFACTOR_SRC_ALPHA
||
57 srcA
== PIPE_BLENDFACTOR_SRC_ALPHA_SATURATE
||
58 srcA
== PIPE_BLENDFACTOR_ZERO
) &&
59 (dstRGB
== PIPE_BLENDFACTOR_INV_SRC_ALPHA
||
60 dstRGB
== PIPE_BLENDFACTOR_ONE
) &&
61 (dstA
== PIPE_BLENDFACTOR_INV_SRC_COLOR
||
62 dstA
== PIPE_BLENDFACTOR_INV_SRC_ALPHA
||
63 dstA
== PIPE_BLENDFACTOR_ONE
);
66 static boolean
blend_discard_if_src_alpha_1(unsigned srcRGB
, unsigned srcA
,
67 unsigned dstRGB
, unsigned dstA
)
69 /* If the blend equation is ADD or REVERSE_SUBTRACT,
70 * SRC_ALPHA == 1, and the following state is set, the colorbuffer
71 * will not be changed.
72 * Notice that the dst factors are the src factors inverted. */
73 return (srcRGB
== PIPE_BLENDFACTOR_INV_SRC_ALPHA
||
74 srcRGB
== PIPE_BLENDFACTOR_ZERO
) &&
75 (srcA
== PIPE_BLENDFACTOR_INV_SRC_COLOR
||
76 srcA
== PIPE_BLENDFACTOR_INV_SRC_ALPHA
||
77 srcA
== PIPE_BLENDFACTOR_ZERO
) &&
78 (dstRGB
== PIPE_BLENDFACTOR_SRC_ALPHA
||
79 dstRGB
== PIPE_BLENDFACTOR_ONE
) &&
80 (dstA
== PIPE_BLENDFACTOR_SRC_COLOR
||
81 dstA
== PIPE_BLENDFACTOR_SRC_ALPHA
||
82 dstA
== PIPE_BLENDFACTOR_ONE
);
85 static boolean
blend_discard_if_src_color_0(unsigned srcRGB
, unsigned srcA
,
86 unsigned dstRGB
, unsigned dstA
)
88 /* If the blend equation is ADD or REVERSE_SUBTRACT,
89 * SRC_COLOR == (0,0,0), and the following state is set, the colorbuffer
90 * will not be changed.
91 * Notice that the dst factors are the src factors inverted. */
92 return (srcRGB
== PIPE_BLENDFACTOR_SRC_COLOR
||
93 srcRGB
== PIPE_BLENDFACTOR_ZERO
) &&
94 (srcA
== PIPE_BLENDFACTOR_ZERO
) &&
95 (dstRGB
== PIPE_BLENDFACTOR_INV_SRC_COLOR
||
96 dstRGB
== PIPE_BLENDFACTOR_ONE
) &&
97 (dstA
== PIPE_BLENDFACTOR_ONE
);
100 static boolean
blend_discard_if_src_color_1(unsigned srcRGB
, unsigned srcA
,
101 unsigned dstRGB
, unsigned dstA
)
103 /* If the blend equation is ADD or REVERSE_SUBTRACT,
104 * SRC_COLOR == (1,1,1), and the following state is set, the colorbuffer
105 * will not be changed.
106 * Notice that the dst factors are the src factors inverted. */
107 return (srcRGB
== PIPE_BLENDFACTOR_INV_SRC_COLOR
||
108 srcRGB
== PIPE_BLENDFACTOR_ZERO
) &&
109 (srcA
== PIPE_BLENDFACTOR_ZERO
) &&
110 (dstRGB
== PIPE_BLENDFACTOR_SRC_COLOR
||
111 dstRGB
== PIPE_BLENDFACTOR_ONE
) &&
112 (dstA
== PIPE_BLENDFACTOR_ONE
);
115 static boolean
blend_discard_if_src_alpha_color_0(unsigned srcRGB
, unsigned srcA
,
116 unsigned dstRGB
, unsigned dstA
)
118 /* If the blend equation is ADD or REVERSE_SUBTRACT,
119 * SRC_ALPHA_COLOR == (0,0,0,0), and the following state is set,
120 * the colorbuffer will not be changed.
121 * Notice that the dst factors are the src factors inverted. */
122 return (srcRGB
== PIPE_BLENDFACTOR_SRC_COLOR
||
123 srcRGB
== PIPE_BLENDFACTOR_SRC_ALPHA
||
124 srcRGB
== PIPE_BLENDFACTOR_SRC_ALPHA_SATURATE
||
125 srcRGB
== PIPE_BLENDFACTOR_ZERO
) &&
126 (srcA
== PIPE_BLENDFACTOR_SRC_COLOR
||
127 srcA
== PIPE_BLENDFACTOR_SRC_ALPHA
||
128 srcA
== PIPE_BLENDFACTOR_SRC_ALPHA_SATURATE
||
129 srcA
== PIPE_BLENDFACTOR_ZERO
) &&
130 (dstRGB
== PIPE_BLENDFACTOR_INV_SRC_COLOR
||
131 dstRGB
== PIPE_BLENDFACTOR_INV_SRC_ALPHA
||
132 dstRGB
== PIPE_BLENDFACTOR_ONE
) &&
133 (dstA
== PIPE_BLENDFACTOR_INV_SRC_COLOR
||
134 dstA
== PIPE_BLENDFACTOR_INV_SRC_ALPHA
||
135 dstA
== PIPE_BLENDFACTOR_ONE
);
138 static boolean
blend_discard_if_src_alpha_color_1(unsigned srcRGB
, unsigned srcA
,
139 unsigned dstRGB
, unsigned dstA
)
141 /* If the blend equation is ADD or REVERSE_SUBTRACT,
142 * SRC_ALPHA_COLOR == (1,1,1,1), and the following state is set,
143 * the colorbuffer will not be changed.
144 * Notice that the dst factors are the src factors inverted. */
145 return (srcRGB
== PIPE_BLENDFACTOR_INV_SRC_COLOR
||
146 srcRGB
== PIPE_BLENDFACTOR_INV_SRC_ALPHA
||
147 srcRGB
== PIPE_BLENDFACTOR_ZERO
) &&
148 (srcA
== PIPE_BLENDFACTOR_INV_SRC_COLOR
||
149 srcA
== PIPE_BLENDFACTOR_INV_SRC_ALPHA
||
150 srcA
== PIPE_BLENDFACTOR_ZERO
) &&
151 (dstRGB
== PIPE_BLENDFACTOR_SRC_COLOR
||
152 dstRGB
== PIPE_BLENDFACTOR_SRC_ALPHA
||
153 dstRGB
== PIPE_BLENDFACTOR_ONE
) &&
154 (dstA
== PIPE_BLENDFACTOR_SRC_COLOR
||
155 dstA
== PIPE_BLENDFACTOR_SRC_ALPHA
||
156 dstA
== PIPE_BLENDFACTOR_ONE
);
159 /* Create a new blend state based on the CSO blend state.
161 * This encompasses alpha blending, logic/raster ops, and blend dithering. */
162 static void* r300_create_blend_state(struct pipe_context
* pipe
,
163 const struct pipe_blend_state
* state
)
165 struct r300_blend_state
* blend
= CALLOC_STRUCT(r300_blend_state
);
167 if (state
->blend_enable
)
169 unsigned eqRGB
= state
->rgb_func
;
170 unsigned srcRGB
= state
->rgb_src_factor
;
171 unsigned dstRGB
= state
->rgb_dst_factor
;
173 unsigned eqA
= state
->alpha_func
;
174 unsigned srcA
= state
->alpha_src_factor
;
175 unsigned dstA
= state
->alpha_dst_factor
;
177 /* despite the name, ALPHA_BLEND_ENABLE has nothing to do with alpha,
178 * this is just the crappy D3D naming */
179 blend
->blend_control
= R300_ALPHA_BLEND_ENABLE
|
180 r300_translate_blend_function(eqRGB
) |
181 ( r300_translate_blend_factor(srcRGB
) << R300_SRC_BLEND_SHIFT
) |
182 ( r300_translate_blend_factor(dstRGB
) << R300_DST_BLEND_SHIFT
);
184 /* Optimization: some operations do not require the destination color.
186 * When SRC_ALPHA_SATURATE is used, colorbuffer reads must be enabled,
187 * otherwise blending gives incorrect results. It seems to be
189 if (eqRGB
== PIPE_BLEND_MIN
|| eqA
== PIPE_BLEND_MIN
||
190 eqRGB
== PIPE_BLEND_MAX
|| eqA
== PIPE_BLEND_MAX
||
191 dstRGB
!= PIPE_BLENDFACTOR_ZERO
||
192 dstA
!= PIPE_BLENDFACTOR_ZERO
||
193 srcRGB
== PIPE_BLENDFACTOR_DST_COLOR
||
194 srcRGB
== PIPE_BLENDFACTOR_DST_ALPHA
||
195 srcRGB
== PIPE_BLENDFACTOR_INV_DST_COLOR
||
196 srcRGB
== PIPE_BLENDFACTOR_INV_DST_ALPHA
||
197 srcA
== PIPE_BLENDFACTOR_DST_COLOR
||
198 srcA
== PIPE_BLENDFACTOR_DST_ALPHA
||
199 srcA
== PIPE_BLENDFACTOR_INV_DST_COLOR
||
200 srcA
== PIPE_BLENDFACTOR_INV_DST_ALPHA
||
201 srcRGB
== PIPE_BLENDFACTOR_SRC_ALPHA_SATURATE
) {
202 /* Enable reading from the colorbuffer. */
203 blend
->blend_control
|= R300_READ_ENABLE
;
205 if (r300_screen(r300_context(pipe
)->context
.screen
)->caps
->is_r500
) {
206 /* Optimization: Depending on incoming pixels, we can
207 * conditionally disable the reading in hardware... */
208 if (eqRGB
!= PIPE_BLEND_MIN
&& eqA
!= PIPE_BLEND_MIN
&&
209 eqRGB
!= PIPE_BLEND_MAX
&& eqA
!= PIPE_BLEND_MAX
) {
210 /* Disable reading if SRC_ALPHA == 0. */
211 if ((dstRGB
== PIPE_BLENDFACTOR_SRC_ALPHA
||
212 dstRGB
== PIPE_BLENDFACTOR_ZERO
) &&
213 (dstA
== PIPE_BLENDFACTOR_SRC_COLOR
||
214 dstA
== PIPE_BLENDFACTOR_SRC_ALPHA
||
215 dstA
== PIPE_BLENDFACTOR_ZERO
)) {
216 blend
->blend_control
|= R500_SRC_ALPHA_0_NO_READ
;
219 /* Disable reading if SRC_ALPHA == 1. */
220 if ((dstRGB
== PIPE_BLENDFACTOR_INV_SRC_ALPHA
||
221 dstRGB
== PIPE_BLENDFACTOR_ZERO
) &&
222 (dstA
== PIPE_BLENDFACTOR_INV_SRC_COLOR
||
223 dstA
== PIPE_BLENDFACTOR_INV_SRC_ALPHA
||
224 dstA
== PIPE_BLENDFACTOR_ZERO
)) {
225 blend
->blend_control
|= R500_SRC_ALPHA_1_NO_READ
;
231 /* Optimization: discard pixels which don't change the colorbuffer.
233 * The code below is non-trivial and some math is involved.
235 * Discarding pixels must be disabled when FP16 AA is enabled.
236 * This is a hardware bug. Also, this implementation wouldn't work
237 * with FP blending enabled and equation clamping disabled.
239 * Equations other than ADD are rarely used and therefore won't be
241 if ((eqRGB
== PIPE_BLEND_ADD
|| eqRGB
== PIPE_BLEND_REVERSE_SUBTRACT
) &&
242 (eqA
== PIPE_BLEND_ADD
|| eqA
== PIPE_BLEND_REVERSE_SUBTRACT
)) {
244 * REVERSE_SUBTRACT: Y-X
247 * If X = src*srcFactor = 0 and Y = dst*dstFactor = 1,
248 * then CB will not be changed.
250 * Given the srcFactor and dstFactor variables, we can derive
251 * what src and dst should be equal to and discard appropriate
254 if (blend_discard_if_src_alpha_0(srcRGB
, srcA
, dstRGB
, dstA
)) {
255 blend
->blend_control
|= R300_DISCARD_SRC_PIXELS_SRC_ALPHA_0
;
256 } else if (blend_discard_if_src_alpha_1(srcRGB
, srcA
,
258 blend
->blend_control
|= R300_DISCARD_SRC_PIXELS_SRC_ALPHA_1
;
259 } else if (blend_discard_if_src_color_0(srcRGB
, srcA
,
261 blend
->blend_control
|= R300_DISCARD_SRC_PIXELS_SRC_COLOR_0
;
262 } else if (blend_discard_if_src_color_1(srcRGB
, srcA
,
264 blend
->blend_control
|= R300_DISCARD_SRC_PIXELS_SRC_COLOR_1
;
265 } else if (blend_discard_if_src_alpha_color_0(srcRGB
, srcA
,
267 blend
->blend_control
|=
268 R300_DISCARD_SRC_PIXELS_SRC_ALPHA_COLOR_0
;
269 } else if (blend_discard_if_src_alpha_color_1(srcRGB
, srcA
,
271 blend
->blend_control
|=
272 R300_DISCARD_SRC_PIXELS_SRC_ALPHA_COLOR_1
;
277 if (srcA
!= srcRGB
|| dstA
!= dstRGB
|| eqA
!= eqRGB
) {
278 blend
->blend_control
|= R300_SEPARATE_ALPHA_ENABLE
;
279 blend
->alpha_blend_control
=
280 r300_translate_blend_function(eqA
) |
281 (r300_translate_blend_factor(srcA
) << R300_SRC_BLEND_SHIFT
) |
282 (r300_translate_blend_factor(dstA
) << R300_DST_BLEND_SHIFT
);
286 /* PIPE_LOGICOP_* don't need to be translated, fortunately. */
287 if (state
->logicop_enable
) {
288 blend
->rop
= R300_RB3D_ROPCNTL_ROP_ENABLE
|
289 (state
->logicop_func
) << R300_RB3D_ROPCNTL_ROP_SHIFT
;
292 /* Color Channel Mask */
293 if (state
->colormask
& PIPE_MASK_R
) {
294 blend
->color_channel_mask
|= RB3D_COLOR_CHANNEL_MASK_RED_MASK0
;
296 if (state
->colormask
& PIPE_MASK_G
) {
297 blend
->color_channel_mask
|= RB3D_COLOR_CHANNEL_MASK_GREEN_MASK0
;
299 if (state
->colormask
& PIPE_MASK_B
) {
300 blend
->color_channel_mask
|= RB3D_COLOR_CHANNEL_MASK_BLUE_MASK0
;
302 if (state
->colormask
& PIPE_MASK_A
) {
303 blend
->color_channel_mask
|= RB3D_COLOR_CHANNEL_MASK_ALPHA_MASK0
;
307 blend
->dither
= R300_RB3D_DITHER_CTL_DITHER_MODE_LUT
|
308 R300_RB3D_DITHER_CTL_ALPHA_DITHER_MODE_LUT
;
314 /* Bind blend state. */
315 static void r300_bind_blend_state(struct pipe_context
* pipe
,
318 struct r300_context
* r300
= r300_context(pipe
);
320 r300
->blend_state
.state
= state
;
321 r300
->blend_state
.dirty
= TRUE
;
324 /* Free blend state. */
325 static void r300_delete_blend_state(struct pipe_context
* pipe
,
331 /* Convert float to 10bit integer */
332 static unsigned float_to_fixed10(float f
)
334 return CLAMP((unsigned)(f
* 1023.9f
), 0, 1023);
338 * Setup both R300 and R500 registers, figure out later which one to write. */
339 static void r300_set_blend_color(struct pipe_context
* pipe
,
340 const struct pipe_blend_color
* color
)
342 struct r300_context
* r300
= r300_context(pipe
);
343 struct r300_screen
* r300screen
= r300_screen(pipe
->screen
);
344 struct r300_blend_color_state
* state
=
345 (struct r300_blend_color_state
*)r300
->blend_color_state
.state
;
348 util_pack_color(color
->color
, PIPE_FORMAT_A8R8G8B8_UNORM
, &uc
);
349 state
->blend_color
= uc
.ui
;
351 /* XXX if FP16 blending is enabled, we should use the FP16 format */
352 state
->blend_color_red_alpha
=
353 float_to_fixed10(color
->color
[0]) |
354 (float_to_fixed10(color
->color
[3]) << 16);
355 state
->blend_color_green_blue
=
356 float_to_fixed10(color
->color
[2]) |
357 (float_to_fixed10(color
->color
[1]) << 16);
359 r300
->blend_color_state
.size
= r300screen
->caps
->is_r500
? 3 : 2;
360 r300
->blend_color_state
.dirty
= TRUE
;
363 static void r300_set_clip_state(struct pipe_context
* pipe
,
364 const struct pipe_clip_state
* state
)
366 struct r300_context
* r300
= r300_context(pipe
);
368 if (r300_screen(pipe
->screen
)->caps
->has_tcl
) {
369 memcpy(r300
->clip_state
.state
, state
, sizeof(struct pipe_clip_state
));
370 r300
->clip_state
.size
= 29;
372 draw_flush(r300
->draw
);
373 draw_set_clip_state(r300
->draw
, state
);
374 r300
->clip_state
.size
= 2;
377 r300
->clip_state
.dirty
= TRUE
;
380 /* Create a new depth, stencil, and alpha state based on the CSO dsa state.
382 * This contains the depth buffer, stencil buffer, alpha test, and such.
383 * On the Radeon, depth and stencil buffer setup are intertwined, which is
384 * the reason for some of the strange-looking assignments across registers. */
386 r300_create_dsa_state(struct pipe_context
* pipe
,
387 const struct pipe_depth_stencil_alpha_state
* state
)
389 struct r300_capabilities
*caps
=
390 r300_screen(r300_context(pipe
)->context
.screen
)->caps
;
391 struct r300_dsa_state
* dsa
= CALLOC_STRUCT(r300_dsa_state
);
393 /* Depth test setup. */
394 if (state
->depth
.enabled
) {
395 dsa
->z_buffer_control
|= R300_Z_ENABLE
;
397 if (state
->depth
.writemask
) {
398 dsa
->z_buffer_control
|= R300_Z_WRITE_ENABLE
;
401 dsa
->z_stencil_control
|=
402 (r300_translate_depth_stencil_function(state
->depth
.func
) <<
406 /* Stencil buffer setup. */
407 if (state
->stencil
[0].enabled
) {
408 dsa
->z_buffer_control
|= R300_STENCIL_ENABLE
;
409 dsa
->z_stencil_control
|=
410 (r300_translate_depth_stencil_function(state
->stencil
[0].func
) <<
411 R300_S_FRONT_FUNC_SHIFT
) |
412 (r300_translate_stencil_op(state
->stencil
[0].fail_op
) <<
413 R300_S_FRONT_SFAIL_OP_SHIFT
) |
414 (r300_translate_stencil_op(state
->stencil
[0].zpass_op
) <<
415 R300_S_FRONT_ZPASS_OP_SHIFT
) |
416 (r300_translate_stencil_op(state
->stencil
[0].zfail_op
) <<
417 R300_S_FRONT_ZFAIL_OP_SHIFT
);
419 dsa
->stencil_ref_mask
= (state
->stencil
[0].ref_value
) |
420 (state
->stencil
[0].valuemask
<< R300_STENCILMASK_SHIFT
) |
421 (state
->stencil
[0].writemask
<< R300_STENCILWRITEMASK_SHIFT
);
423 if (state
->stencil
[1].enabled
) {
424 dsa
->z_buffer_control
|= R300_STENCIL_FRONT_BACK
;
425 dsa
->z_stencil_control
|=
426 (r300_translate_depth_stencil_function(state
->stencil
[1].func
) <<
427 R300_S_BACK_FUNC_SHIFT
) |
428 (r300_translate_stencil_op(state
->stencil
[1].fail_op
) <<
429 R300_S_BACK_SFAIL_OP_SHIFT
) |
430 (r300_translate_stencil_op(state
->stencil
[1].zpass_op
) <<
431 R300_S_BACK_ZPASS_OP_SHIFT
) |
432 (r300_translate_stencil_op(state
->stencil
[1].zfail_op
) <<
433 R300_S_BACK_ZFAIL_OP_SHIFT
);
435 /* XXX it seems r3xx doesn't support STENCILREFMASK_BF */
438 dsa
->z_buffer_control
|= R500_STENCIL_REFMASK_FRONT_BACK
;
439 dsa
->stencil_ref_bf
= (state
->stencil
[1].ref_value
) |
440 (state
->stencil
[1].valuemask
<<
441 R300_STENCILMASK_SHIFT
) |
442 (state
->stencil
[1].writemask
<<
443 R300_STENCILWRITEMASK_SHIFT
);
448 /* Alpha test setup. */
449 if (state
->alpha
.enabled
) {
450 dsa
->alpha_function
=
451 r300_translate_alpha_function(state
->alpha
.func
) |
452 R300_FG_ALPHA_FUNC_ENABLE
;
454 /* XXX figure out why emitting 10bit alpha ref causes CS to dump */
455 /* always use 8bit alpha ref */
456 dsa
->alpha_function
|= float_to_ubyte(state
->alpha
.ref_value
);
459 dsa
->alpha_function
|= R500_FG_ALPHA_FUNC_8BIT
;
465 /* Bind DSA state. */
466 static void r300_bind_dsa_state(struct pipe_context
* pipe
,
469 struct r300_context
* r300
= r300_context(pipe
);
470 struct r300_screen
* r300screen
= r300_screen(pipe
->screen
);
472 r300
->dsa_state
.state
= state
;
473 r300
->dsa_state
.size
= r300screen
->caps
->is_r500
? 8 : 6;
474 r300
->dsa_state
.dirty
= TRUE
;
477 /* Free DSA state. */
478 static void r300_delete_dsa_state(struct pipe_context
* pipe
,
485 r300_set_framebuffer_state(struct pipe_context
* pipe
,
486 const struct pipe_framebuffer_state
* state
)
488 struct r300_context
* r300
= r300_context(pipe
);
489 uint32_t zbuffer_bpp
= 0;
492 draw_flush(r300
->draw
);
495 r300
->framebuffer_state
= *state
;
497 /* Don't rely on the order of states being set for the first time. */
498 r300
->dirty_state
|= R300_NEW_FRAMEBUFFERS
;
500 r300
->blend_state
.dirty
= TRUE
;
501 r300
->dsa_state
.dirty
= TRUE
;
502 r300
->scissor_state
.dirty
= TRUE
;
504 /* Polyfon offset depends on the zbuffer bit depth. */
505 if (state
->zsbuf
&& r300
->polygon_offset_enabled
) {
506 switch (util_format_get_blocksize(state
->zsbuf
->texture
->format
)) {
515 if (r300
->zbuffer_bpp
!= zbuffer_bpp
) {
516 r300
->zbuffer_bpp
= zbuffer_bpp
;
517 r300
->rs_state
.dirty
= TRUE
;
522 /* Create fragment shader state. */
523 static void* r300_create_fs_state(struct pipe_context
* pipe
,
524 const struct pipe_shader_state
* shader
)
526 struct r300_fragment_shader
* fs
= NULL
;
528 fs
= (struct r300_fragment_shader
*)CALLOC_STRUCT(r300_fragment_shader
);
530 /* Copy state directly into shader. */
532 fs
->state
.tokens
= tgsi_dup_tokens(shader
->tokens
);
534 tgsi_scan_shader(shader
->tokens
, &fs
->info
);
535 r300_shader_read_fs_inputs(&fs
->info
, &fs
->inputs
);
540 /* Bind fragment shader state. */
541 static void r300_bind_fs_state(struct pipe_context
* pipe
, void* shader
)
543 struct r300_context
* r300
= r300_context(pipe
);
544 struct r300_fragment_shader
* fs
= (struct r300_fragment_shader
*)shader
;
552 r300_pick_fragment_shader(r300
);
554 if (r300
->vs
&& r300_vertex_shader_setup_wpos(r300
)) {
555 r300
->dirty_state
|= R300_NEW_VERTEX_FORMAT
;
558 r300
->dirty_state
|= R300_NEW_FRAGMENT_SHADER
| R300_NEW_FRAGMENT_SHADER_CONSTANTS
;
561 /* Delete fragment shader state. */
562 static void r300_delete_fs_state(struct pipe_context
* pipe
, void* shader
)
564 struct r300_fragment_shader
* fs
= (struct r300_fragment_shader
*)shader
;
565 struct r300_fragment_shader_code
*tmp
, *ptr
= fs
->first
;
570 rc_constants_destroy(&tmp
->code
.constants
);
573 FREE((void*)fs
->state
.tokens
);
577 static void r300_set_polygon_stipple(struct pipe_context
* pipe
,
578 const struct pipe_poly_stipple
* state
)
580 /* XXX no idea how to set this up, but not terribly important */
583 /* Create a new rasterizer state based on the CSO rasterizer state.
585 * This is a very large chunk of state, and covers most of the graphics
586 * backend (GB), geometry assembly (GA), and setup unit (SU) blocks.
588 * In a not entirely unironic sidenote, this state has nearly nothing to do
589 * with the actual block on the Radeon called the rasterizer (RS). */
590 static void* r300_create_rs_state(struct pipe_context
* pipe
,
591 const struct pipe_rasterizer_state
* state
)
593 struct r300_rs_state
* rs
= CALLOC_STRUCT(r300_rs_state
);
595 /* Copy rasterizer state for Draw. */
598 #ifdef PIPE_ARCH_LITTLE_ENDIAN
599 rs
->vap_control_status
= R300_VC_NO_SWAP
;
601 rs
->vap_control_status
= R300_VC_32BIT_SWAP
;
604 /* If bypassing TCL, or if no TCL engine is present, turn off the HW TCL.
605 * Else, enable HW TCL and force Draw's TCL off. */
606 if (state
->bypass_vs_clip_and_viewport
||
607 !r300_screen(pipe
->screen
)->caps
->has_tcl
) {
608 rs
->vap_control_status
|= R300_VAP_TCL_BYPASS
;
611 rs
->point_size
= pack_float_16_6x(state
->point_size
) |
612 (pack_float_16_6x(state
->point_size
) << R300_POINTSIZE_X_SHIFT
);
615 ((int)(state
->point_size_min
* 6.0) <<
616 R300_GA_POINT_MINMAX_MIN_SHIFT
) |
617 ((int)(state
->point_size_max
* 6.0) <<
618 R300_GA_POINT_MINMAX_MAX_SHIFT
);
620 rs
->line_control
= pack_float_16_6x(state
->line_width
) |
621 R300_GA_LINE_CNTL_END_TYPE_COMP
;
623 /* Enable polygon mode */
624 if (state
->fill_cw
!= PIPE_POLYGON_MODE_FILL
||
625 state
->fill_ccw
!= PIPE_POLYGON_MODE_FILL
) {
626 rs
->polygon_mode
= R300_GA_POLY_MODE_DUAL
;
629 /* Radeons don't think in "CW/CCW", they think in "front/back". */
630 if (state
->front_winding
== PIPE_WINDING_CW
) {
631 rs
->cull_mode
= R300_FRONT_FACE_CW
;
634 if (state
->offset_cw
) {
635 rs
->polygon_offset_enable
|= R300_FRONT_ENABLE
;
637 if (state
->offset_ccw
) {
638 rs
->polygon_offset_enable
|= R300_BACK_ENABLE
;
642 if (rs
->polygon_mode
) {
644 r300_translate_polygon_mode_front(state
->fill_cw
);
646 r300_translate_polygon_mode_back(state
->fill_ccw
);
649 rs
->cull_mode
= R300_FRONT_FACE_CCW
;
652 if (state
->offset_ccw
) {
653 rs
->polygon_offset_enable
|= R300_FRONT_ENABLE
;
655 if (state
->offset_cw
) {
656 rs
->polygon_offset_enable
|= R300_BACK_ENABLE
;
660 if (rs
->polygon_mode
) {
662 r300_translate_polygon_mode_front(state
->fill_ccw
);
664 r300_translate_polygon_mode_back(state
->fill_cw
);
667 if (state
->front_winding
& state
->cull_mode
) {
668 rs
->cull_mode
|= R300_CULL_FRONT
;
670 if (~(state
->front_winding
) & state
->cull_mode
) {
671 rs
->cull_mode
|= R300_CULL_BACK
;
674 if (rs
->polygon_offset_enable
) {
675 rs
->depth_offset
= state
->offset_units
;
676 rs
->depth_scale
= state
->offset_scale
;
679 if (state
->line_stipple_enable
) {
680 rs
->line_stipple_config
=
681 R300_GA_LINE_STIPPLE_CONFIG_LINE_RESET_LINE
|
682 (fui((float)state
->line_stipple_factor
) &
683 R300_GA_LINE_STIPPLE_CONFIG_STIPPLE_SCALE_MASK
);
684 /* XXX this might need to be scaled up */
685 rs
->line_stipple_value
= state
->line_stipple_pattern
;
688 if (state
->flatshade
) {
689 rs
->color_control
= R300_SHADE_MODEL_FLAT
;
691 rs
->color_control
= R300_SHADE_MODEL_SMOOTH
;
697 /* Bind rasterizer state. */
698 static void r300_bind_rs_state(struct pipe_context
* pipe
, void* state
)
700 struct r300_context
* r300
= r300_context(pipe
);
701 struct r300_rs_state
* rs
= (struct r300_rs_state
*)state
;
704 draw_flush(r300
->draw
);
705 draw_set_rasterizer_state(r300
->draw
, &rs
->rs
);
709 r300
->tcl_bypass
= rs
->rs
.bypass_vs_clip_and_viewport
;
710 r300
->polygon_offset_enabled
= rs
->rs
.offset_cw
|| rs
->rs
.offset_ccw
;
712 r300
->tcl_bypass
= FALSE
;
713 r300
->polygon_offset_enabled
= FALSE
;
716 r300
->rs_state
.state
= rs
;
717 r300
->rs_state
.dirty
= TRUE
;
718 /* XXX Why is this still needed, dammit!? */
719 r300
->scissor_state
.dirty
= TRUE
;
720 r300
->viewport_state
.dirty
= TRUE
;
722 /* XXX Clean these up when we move to atom emits */
723 r300
->dirty_state
|= R300_NEW_RS_BLOCK
;
724 if (r300
->fs
&& r300
->fs
->inputs
.wpos
!= ATTR_UNUSED
) {
725 r300
->dirty_state
|= R300_NEW_FRAGMENT_SHADER_CONSTANTS
;
729 /* Free rasterizer state. */
730 static void r300_delete_rs_state(struct pipe_context
* pipe
, void* state
)
736 r300_create_sampler_state(struct pipe_context
* pipe
,
737 const struct pipe_sampler_state
* state
)
739 struct r300_context
* r300
= r300_context(pipe
);
740 struct r300_sampler_state
* sampler
= CALLOC_STRUCT(r300_sampler_state
);
744 sampler
->state
= *state
;
747 (r300_translate_wrap(state
->wrap_s
) << R300_TX_WRAP_S_SHIFT
) |
748 (r300_translate_wrap(state
->wrap_t
) << R300_TX_WRAP_T_SHIFT
) |
749 (r300_translate_wrap(state
->wrap_r
) << R300_TX_WRAP_R_SHIFT
);
751 sampler
->filter0
|= r300_translate_tex_filters(state
->min_img_filter
,
752 state
->mag_img_filter
,
753 state
->min_mip_filter
,
754 state
->max_anisotropy
> 1.0);
756 /* Unfortunately, r300-r500 don't support floating-point mipmap lods. */
757 /* We must pass these to the emit function to clamp them properly. */
758 sampler
->min_lod
= MAX2((unsigned)state
->min_lod
, 0);
759 sampler
->max_lod
= MAX2((unsigned)ceilf(state
->max_lod
), 0);
761 lod_bias
= CLAMP((int)(state
->lod_bias
* 32), -(1 << 9), (1 << 9) - 1);
763 sampler
->filter1
|= lod_bias
<< R300_LOD_BIAS_SHIFT
;
765 sampler
->filter1
|= r300_anisotropy(state
->max_anisotropy
);
767 util_pack_color(state
->border_color
, PIPE_FORMAT_A8R8G8B8_UNORM
, &uc
);
768 sampler
->border_color
= uc
.ui
;
770 /* R500-specific fixups and optimizations */
771 if (r300_screen(r300
->context
.screen
)->caps
->is_r500
) {
772 sampler
->filter1
|= R500_BORDER_FIX
;
775 return (void*)sampler
;
778 static void r300_bind_sampler_states(struct pipe_context
* pipe
,
782 struct r300_context
* r300
= r300_context(pipe
);
789 for (i
= 0; i
< count
; i
++) {
790 if (r300
->sampler_states
[i
] != states
[i
]) {
791 r300
->sampler_states
[i
] = (struct r300_sampler_state
*)states
[i
];
792 r300
->dirty_state
|= (R300_NEW_SAMPLER
<< i
);
796 r300
->sampler_count
= count
;
798 /* Pick a fragment shader based on the texture compare state. */
799 if (r300
->fs
&& (r300
->dirty_state
& R300_ANY_NEW_SAMPLERS
)) {
800 if (r300_pick_fragment_shader(r300
)) {
801 r300
->dirty_state
|= R300_NEW_FRAGMENT_SHADER
|
802 R300_NEW_FRAGMENT_SHADER_CONSTANTS
;
807 static void r300_lacks_vertex_textures(struct pipe_context
* pipe
,
813 static void r300_delete_sampler_state(struct pipe_context
* pipe
, void* state
)
818 static void r300_set_sampler_textures(struct pipe_context
* pipe
,
820 struct pipe_texture
** texture
)
822 struct r300_context
* r300
= r300_context(pipe
);
823 boolean is_r500
= r300_screen(r300
->context
.screen
)->caps
->is_r500
;
831 for (i
= 0; i
< count
; i
++) {
832 if (r300
->textures
[i
] != (struct r300_texture
*)texture
[i
]) {
833 pipe_texture_reference((struct pipe_texture
**)&r300
->textures
[i
],
835 r300
->dirty_state
|= (R300_NEW_TEXTURE
<< i
);
837 /* R300-specific - set the texrect factor in a fragment shader */
838 if (!is_r500
&& r300
->textures
[i
]->is_npot
) {
839 /* XXX It would be nice to re-emit just 1 constant,
840 * XXX not all of them */
841 r300
->dirty_state
|= R300_NEW_FRAGMENT_SHADER_CONSTANTS
;
846 for (i
= count
; i
< 8; i
++) {
847 if (r300
->textures
[i
]) {
848 pipe_texture_reference((struct pipe_texture
**)&r300
->textures
[i
],
850 r300
->dirty_state
|= (R300_NEW_TEXTURE
<< i
);
854 r300
->texture_count
= count
;
857 static void r300_set_scissor_state(struct pipe_context
* pipe
,
858 const struct pipe_scissor_state
* state
)
860 struct r300_context
* r300
= r300_context(pipe
);
862 memcpy(r300
->scissor_state
.state
, state
,
863 sizeof(struct pipe_scissor_state
));
865 r300
->scissor_state
.dirty
= TRUE
;
868 static void r300_set_viewport_state(struct pipe_context
* pipe
,
869 const struct pipe_viewport_state
* state
)
871 struct r300_context
* r300
= r300_context(pipe
);
872 struct r300_viewport_state
* viewport
=
873 (struct r300_viewport_state
*)r300
->viewport_state
.state
;
875 /* Do the transform in HW. */
876 viewport
->vte_control
= R300_VTX_W0_FMT
;
878 if (state
->scale
[0] != 1.0f
) {
879 viewport
->xscale
= state
->scale
[0];
880 viewport
->vte_control
|= R300_VPORT_X_SCALE_ENA
;
882 if (state
->scale
[1] != 1.0f
) {
883 viewport
->yscale
= state
->scale
[1];
884 viewport
->vte_control
|= R300_VPORT_Y_SCALE_ENA
;
886 if (state
->scale
[2] != 1.0f
) {
887 viewport
->zscale
= state
->scale
[2];
888 viewport
->vte_control
|= R300_VPORT_Z_SCALE_ENA
;
890 if (state
->translate
[0] != 0.0f
) {
891 viewport
->xoffset
= state
->translate
[0];
892 viewport
->vte_control
|= R300_VPORT_X_OFFSET_ENA
;
894 if (state
->translate
[1] != 0.0f
) {
895 viewport
->yoffset
= state
->translate
[1];
896 viewport
->vte_control
|= R300_VPORT_Y_OFFSET_ENA
;
898 if (state
->translate
[2] != 0.0f
) {
899 viewport
->zoffset
= state
->translate
[2];
900 viewport
->vte_control
|= R300_VPORT_Z_OFFSET_ENA
;
903 r300
->viewport_state
.dirty
= TRUE
;
904 if (r300
->fs
&& r300
->fs
->inputs
.wpos
!= ATTR_UNUSED
) {
905 r300
->dirty_state
|= R300_NEW_FRAGMENT_SHADER_CONSTANTS
;
909 static void r300_set_vertex_buffers(struct pipe_context
* pipe
,
911 const struct pipe_vertex_buffer
* buffers
)
913 struct r300_context
* r300
= r300_context(pipe
);
915 memcpy(r300
->vertex_buffer
, buffers
,
916 sizeof(struct pipe_vertex_buffer
) * count
);
917 r300
->vertex_buffer_count
= count
;
920 draw_flush(r300
->draw
);
921 draw_set_vertex_buffers(r300
->draw
, count
, buffers
);
924 r300
->dirty_state
|= R300_NEW_VERTEX_FORMAT
;
927 static boolean
r300_validate_aos(struct r300_context
*r300
)
929 struct pipe_vertex_buffer
*vbuf
= r300
->vertex_buffer
;
930 struct pipe_vertex_element
*velem
= r300
->vertex_element
;
933 /* Check if formats and strides are aligned to the size of DWORD. */
934 for (i
= 0; i
< r300
->vertex_element_count
; i
++) {
935 if (vbuf
[velem
[i
].vertex_buffer_index
].stride
% 4 != 0 ||
936 util_format_get_blocksize(velem
[i
].src_format
) % 4 != 0) {
943 static void r300_set_vertex_elements(struct pipe_context
* pipe
,
945 const struct pipe_vertex_element
* elements
)
947 struct r300_context
* r300
= r300_context(pipe
);
949 memcpy(r300
->vertex_element
,
951 sizeof(struct pipe_vertex_element
) * count
);
952 r300
->vertex_element_count
= count
;
955 draw_flush(r300
->draw
);
956 draw_set_vertex_elements(r300
->draw
, count
, elements
);
959 if (!r300_validate_aos(r300
)) {
960 /* XXX We should fallback using draw. */
966 static void* r300_create_vs_state(struct pipe_context
* pipe
,
967 const struct pipe_shader_state
* shader
)
969 struct r300_context
* r300
= r300_context(pipe
);
971 if (r300_screen(pipe
->screen
)->caps
->has_tcl
) {
972 struct r300_vertex_shader
* vs
= CALLOC_STRUCT(r300_vertex_shader
);
973 /* Copy state directly into shader. */
975 vs
->state
.tokens
= tgsi_dup_tokens(shader
->tokens
);
977 tgsi_scan_shader(shader
->tokens
, &vs
->info
);
981 return draw_create_vertex_shader(r300
->draw
, shader
);
985 static void r300_bind_vs_state(struct pipe_context
* pipe
, void* shader
)
987 struct r300_context
* r300
= r300_context(pipe
);
989 if (r300_screen(pipe
->screen
)->caps
->has_tcl
) {
990 struct r300_vertex_shader
* vs
= (struct r300_vertex_shader
*)shader
;
995 } else if (!vs
->translated
) {
996 r300_translate_vertex_shader(r300
, vs
);
1001 r300_vertex_shader_setup_wpos(r300
);
1004 r300
->dirty_state
|=
1005 R300_NEW_VERTEX_SHADER
| R300_NEW_VERTEX_SHADER_CONSTANTS
|
1006 R300_NEW_VERTEX_FORMAT
;
1008 draw_flush(r300
->draw
);
1009 draw_bind_vertex_shader(r300
->draw
,
1010 (struct draw_vertex_shader
*)shader
);
1014 static void r300_delete_vs_state(struct pipe_context
* pipe
, void* shader
)
1016 struct r300_context
* r300
= r300_context(pipe
);
1018 if (r300_screen(pipe
->screen
)->caps
->has_tcl
) {
1019 struct r300_vertex_shader
* vs
= (struct r300_vertex_shader
*)shader
;
1021 rc_constants_destroy(&vs
->code
.constants
);
1022 FREE((void*)vs
->state
.tokens
);
1025 draw_delete_vertex_shader(r300
->draw
,
1026 (struct draw_vertex_shader
*)shader
);
1030 static void r300_set_constant_buffer(struct pipe_context
*pipe
,
1031 uint shader
, uint index
,
1032 struct pipe_buffer
*buf
)
1034 struct r300_context
* r300
= r300_context(pipe
);
1037 if (buf
== NULL
|| buf
->size
== 0 ||
1038 (mapped
= pipe_buffer_map(pipe
->screen
, buf
, PIPE_BUFFER_USAGE_CPU_READ
)) == NULL
)
1040 r300
->shader_constants
[shader
].count
= 0;
1044 assert((buf
->size
% 4 * sizeof(float)) == 0);
1045 memcpy(r300
->shader_constants
[shader
].constants
, mapped
, buf
->size
);
1046 r300
->shader_constants
[shader
].count
= buf
->size
/ (4 * sizeof(float));
1047 pipe_buffer_unmap(pipe
->screen
, buf
);
1049 if (shader
== PIPE_SHADER_VERTEX
)
1050 r300
->dirty_state
|= R300_NEW_VERTEX_SHADER_CONSTANTS
;
1051 else if (shader
== PIPE_SHADER_FRAGMENT
)
1052 r300
->dirty_state
|= R300_NEW_FRAGMENT_SHADER_CONSTANTS
;
1055 void r300_init_state_functions(struct r300_context
* r300
)
1057 r300
->context
.create_blend_state
= r300_create_blend_state
;
1058 r300
->context
.bind_blend_state
= r300_bind_blend_state
;
1059 r300
->context
.delete_blend_state
= r300_delete_blend_state
;
1061 r300
->context
.set_blend_color
= r300_set_blend_color
;
1063 r300
->context
.set_clip_state
= r300_set_clip_state
;
1065 r300
->context
.set_constant_buffer
= r300_set_constant_buffer
;
1067 r300
->context
.create_depth_stencil_alpha_state
= r300_create_dsa_state
;
1068 r300
->context
.bind_depth_stencil_alpha_state
= r300_bind_dsa_state
;
1069 r300
->context
.delete_depth_stencil_alpha_state
= r300_delete_dsa_state
;
1071 r300
->context
.set_framebuffer_state
= r300_set_framebuffer_state
;
1073 r300
->context
.create_fs_state
= r300_create_fs_state
;
1074 r300
->context
.bind_fs_state
= r300_bind_fs_state
;
1075 r300
->context
.delete_fs_state
= r300_delete_fs_state
;
1077 r300
->context
.set_polygon_stipple
= r300_set_polygon_stipple
;
1079 r300
->context
.create_rasterizer_state
= r300_create_rs_state
;
1080 r300
->context
.bind_rasterizer_state
= r300_bind_rs_state
;
1081 r300
->context
.delete_rasterizer_state
= r300_delete_rs_state
;
1083 r300
->context
.create_sampler_state
= r300_create_sampler_state
;
1084 r300
->context
.bind_fragment_sampler_states
= r300_bind_sampler_states
;
1085 r300
->context
.bind_vertex_sampler_states
= r300_lacks_vertex_textures
;
1086 r300
->context
.delete_sampler_state
= r300_delete_sampler_state
;
1088 r300
->context
.set_fragment_sampler_textures
= r300_set_sampler_textures
;
1090 r300
->context
.set_scissor_state
= r300_set_scissor_state
;
1092 r300
->context
.set_viewport_state
= r300_set_viewport_state
;
1094 r300
->context
.set_vertex_buffers
= r300_set_vertex_buffers
;
1095 r300
->context
.set_vertex_elements
= r300_set_vertex_elements
;
1097 r300
->context
.create_vs_state
= r300_create_vs_state
;
1098 r300
->context
.bind_vs_state
= r300_bind_vs_state
;
1099 r300
->context
.delete_vs_state
= r300_delete_vs_state
;