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_framebuffer.h"
27 #include "util/u_half.h"
28 #include "util/u_helpers.h"
29 #include "util/u_math.h"
30 #include "util/u_memory.h"
31 #include "util/u_pack_color.h"
32 #include "util/u_transfer.h"
34 #include "tgsi/tgsi_parse.h"
36 #include "pipe/p_config.h"
39 #include "r300_context.h"
40 #include "r300_emit.h"
42 #include "r300_screen.h"
43 #include "r300_screen_buffer.h"
44 #include "r300_state_inlines.h"
46 #include "r300_texture.h"
49 /* r300_state: Functions used to intialize state context by translating
50 * Gallium state objects into semi-native r300 state objects. */
52 #define UPDATE_STATE(cso, atom) \
53 if (cso != atom.state) { \
55 r300_mark_atom_dirty(r300, &(atom)); \
58 static boolean
blend_discard_if_src_alpha_0(unsigned srcRGB
, unsigned srcA
,
59 unsigned dstRGB
, unsigned dstA
)
61 /* If the blend equation is ADD or REVERSE_SUBTRACT,
62 * SRC_ALPHA == 0, and the following state is set, the colorbuffer
63 * will not be changed.
64 * Notice that the dst factors are the src factors inverted. */
65 return (srcRGB
== PIPE_BLENDFACTOR_SRC_ALPHA
||
66 srcRGB
== PIPE_BLENDFACTOR_SRC_ALPHA_SATURATE
||
67 srcRGB
== PIPE_BLENDFACTOR_ZERO
) &&
68 (srcA
== PIPE_BLENDFACTOR_SRC_COLOR
||
69 srcA
== PIPE_BLENDFACTOR_SRC_ALPHA
||
70 srcA
== PIPE_BLENDFACTOR_SRC_ALPHA_SATURATE
||
71 srcA
== PIPE_BLENDFACTOR_ZERO
) &&
72 (dstRGB
== PIPE_BLENDFACTOR_INV_SRC_ALPHA
||
73 dstRGB
== PIPE_BLENDFACTOR_ONE
) &&
74 (dstA
== PIPE_BLENDFACTOR_INV_SRC_COLOR
||
75 dstA
== PIPE_BLENDFACTOR_INV_SRC_ALPHA
||
76 dstA
== PIPE_BLENDFACTOR_ONE
);
79 static boolean
blend_discard_if_src_alpha_1(unsigned srcRGB
, unsigned srcA
,
80 unsigned dstRGB
, unsigned dstA
)
82 /* If the blend equation is ADD or REVERSE_SUBTRACT,
83 * SRC_ALPHA == 1, and the following state is set, the colorbuffer
84 * will not be changed.
85 * Notice that the dst factors are the src factors inverted. */
86 return (srcRGB
== PIPE_BLENDFACTOR_INV_SRC_ALPHA
||
87 srcRGB
== PIPE_BLENDFACTOR_ZERO
) &&
88 (srcA
== PIPE_BLENDFACTOR_INV_SRC_COLOR
||
89 srcA
== PIPE_BLENDFACTOR_INV_SRC_ALPHA
||
90 srcA
== PIPE_BLENDFACTOR_ZERO
) &&
91 (dstRGB
== PIPE_BLENDFACTOR_SRC_ALPHA
||
92 dstRGB
== PIPE_BLENDFACTOR_ONE
) &&
93 (dstA
== PIPE_BLENDFACTOR_SRC_COLOR
||
94 dstA
== PIPE_BLENDFACTOR_SRC_ALPHA
||
95 dstA
== PIPE_BLENDFACTOR_ONE
);
98 static boolean
blend_discard_if_src_color_0(unsigned srcRGB
, unsigned srcA
,
99 unsigned dstRGB
, unsigned dstA
)
101 /* If the blend equation is ADD or REVERSE_SUBTRACT,
102 * SRC_COLOR == (0,0,0), and the following state is set, the colorbuffer
103 * will not be changed.
104 * Notice that the dst factors are the src factors inverted. */
105 return (srcRGB
== PIPE_BLENDFACTOR_SRC_COLOR
||
106 srcRGB
== PIPE_BLENDFACTOR_ZERO
) &&
107 (srcA
== PIPE_BLENDFACTOR_ZERO
) &&
108 (dstRGB
== PIPE_BLENDFACTOR_INV_SRC_COLOR
||
109 dstRGB
== PIPE_BLENDFACTOR_ONE
) &&
110 (dstA
== PIPE_BLENDFACTOR_ONE
);
113 static boolean
blend_discard_if_src_color_1(unsigned srcRGB
, unsigned srcA
,
114 unsigned dstRGB
, unsigned dstA
)
116 /* If the blend equation is ADD or REVERSE_SUBTRACT,
117 * SRC_COLOR == (1,1,1), and the following state is set, the colorbuffer
118 * will not be changed.
119 * Notice that the dst factors are the src factors inverted. */
120 return (srcRGB
== PIPE_BLENDFACTOR_INV_SRC_COLOR
||
121 srcRGB
== PIPE_BLENDFACTOR_ZERO
) &&
122 (srcA
== PIPE_BLENDFACTOR_ZERO
) &&
123 (dstRGB
== PIPE_BLENDFACTOR_SRC_COLOR
||
124 dstRGB
== PIPE_BLENDFACTOR_ONE
) &&
125 (dstA
== PIPE_BLENDFACTOR_ONE
);
128 static boolean
blend_discard_if_src_alpha_color_0(unsigned srcRGB
, unsigned srcA
,
129 unsigned dstRGB
, unsigned dstA
)
131 /* If the blend equation is ADD or REVERSE_SUBTRACT,
132 * SRC_ALPHA_COLOR == (0,0,0,0), and the following state is set,
133 * the colorbuffer will not be changed.
134 * Notice that the dst factors are the src factors inverted. */
135 return (srcRGB
== PIPE_BLENDFACTOR_SRC_COLOR
||
136 srcRGB
== PIPE_BLENDFACTOR_SRC_ALPHA
||
137 srcRGB
== PIPE_BLENDFACTOR_SRC_ALPHA_SATURATE
||
138 srcRGB
== PIPE_BLENDFACTOR_ZERO
) &&
139 (srcA
== PIPE_BLENDFACTOR_SRC_COLOR
||
140 srcA
== PIPE_BLENDFACTOR_SRC_ALPHA
||
141 srcA
== PIPE_BLENDFACTOR_SRC_ALPHA_SATURATE
||
142 srcA
== PIPE_BLENDFACTOR_ZERO
) &&
143 (dstRGB
== PIPE_BLENDFACTOR_INV_SRC_COLOR
||
144 dstRGB
== PIPE_BLENDFACTOR_INV_SRC_ALPHA
||
145 dstRGB
== PIPE_BLENDFACTOR_ONE
) &&
146 (dstA
== PIPE_BLENDFACTOR_INV_SRC_COLOR
||
147 dstA
== PIPE_BLENDFACTOR_INV_SRC_ALPHA
||
148 dstA
== PIPE_BLENDFACTOR_ONE
);
151 static boolean
blend_discard_if_src_alpha_color_1(unsigned srcRGB
, unsigned srcA
,
152 unsigned dstRGB
, unsigned dstA
)
154 /* If the blend equation is ADD or REVERSE_SUBTRACT,
155 * SRC_ALPHA_COLOR == (1,1,1,1), and the following state is set,
156 * the colorbuffer will not be changed.
157 * Notice that the dst factors are the src factors inverted. */
158 return (srcRGB
== PIPE_BLENDFACTOR_INV_SRC_COLOR
||
159 srcRGB
== PIPE_BLENDFACTOR_INV_SRC_ALPHA
||
160 srcRGB
== PIPE_BLENDFACTOR_ZERO
) &&
161 (srcA
== PIPE_BLENDFACTOR_INV_SRC_COLOR
||
162 srcA
== PIPE_BLENDFACTOR_INV_SRC_ALPHA
||
163 srcA
== PIPE_BLENDFACTOR_ZERO
) &&
164 (dstRGB
== PIPE_BLENDFACTOR_SRC_COLOR
||
165 dstRGB
== PIPE_BLENDFACTOR_SRC_ALPHA
||
166 dstRGB
== PIPE_BLENDFACTOR_ONE
) &&
167 (dstA
== PIPE_BLENDFACTOR_SRC_COLOR
||
168 dstA
== PIPE_BLENDFACTOR_SRC_ALPHA
||
169 dstA
== PIPE_BLENDFACTOR_ONE
);
172 static unsigned blend_discard_conditionally(unsigned eqRGB
, unsigned eqA
,
173 unsigned dstRGB
, unsigned dstA
,
174 unsigned srcRGB
, unsigned srcA
)
176 unsigned blend_control
= 0;
178 /* Optimization: discard pixels which don't change the colorbuffer.
180 * The code below is non-trivial and some math is involved.
182 * Discarding pixels must be disabled when FP16 AA is enabled.
183 * This is a hardware bug. Also, this implementation wouldn't work
184 * with FP blending enabled and equation clamping disabled.
186 * Equations other than ADD are rarely used and therefore won't be
188 if ((eqRGB
== PIPE_BLEND_ADD
|| eqRGB
== PIPE_BLEND_REVERSE_SUBTRACT
) &&
189 (eqA
== PIPE_BLEND_ADD
|| eqA
== PIPE_BLEND_REVERSE_SUBTRACT
)) {
191 * REVERSE_SUBTRACT: Y-X
194 * If X = src*srcFactor = 0 and Y = dst*dstFactor = 1,
195 * then CB will not be changed.
197 * Given the srcFactor and dstFactor variables, we can derive
198 * what src and dst should be equal to and discard appropriate
201 if (blend_discard_if_src_alpha_0(srcRGB
, srcA
, dstRGB
, dstA
)) {
202 blend_control
|= R300_DISCARD_SRC_PIXELS_SRC_ALPHA_0
;
203 } else if (blend_discard_if_src_alpha_1(srcRGB
, srcA
,
205 blend_control
|= R300_DISCARD_SRC_PIXELS_SRC_ALPHA_1
;
206 } else if (blend_discard_if_src_color_0(srcRGB
, srcA
,
208 blend_control
|= R300_DISCARD_SRC_PIXELS_SRC_COLOR_0
;
209 } else if (blend_discard_if_src_color_1(srcRGB
, srcA
,
211 blend_control
|= R300_DISCARD_SRC_PIXELS_SRC_COLOR_1
;
212 } else if (blend_discard_if_src_alpha_color_0(srcRGB
, srcA
,
215 R300_DISCARD_SRC_PIXELS_SRC_ALPHA_COLOR_0
;
216 } else if (blend_discard_if_src_alpha_color_1(srcRGB
, srcA
,
219 R300_DISCARD_SRC_PIXELS_SRC_ALPHA_COLOR_1
;
222 return blend_control
;
225 /* The hardware colormask is clunky a must be swizzled depending on the format.
226 * This was figured out by trial-and-error. */
227 static unsigned bgra_cmask(unsigned mask
)
229 return ((mask
& PIPE_MASK_R
) << 2) |
230 ((mask
& PIPE_MASK_B
) >> 2) |
231 (mask
& (PIPE_MASK_G
| PIPE_MASK_A
));
234 static unsigned rgba_cmask(unsigned mask
)
236 return mask
& PIPE_MASK_RGBA
;
239 static unsigned rrrr_cmask(unsigned mask
)
241 return (mask
& PIPE_MASK_R
) |
242 ((mask
& PIPE_MASK_R
) << 1) |
243 ((mask
& PIPE_MASK_R
) << 2) |
244 ((mask
& PIPE_MASK_R
) << 3);
247 static unsigned aaaa_cmask(unsigned mask
)
249 return ((mask
& PIPE_MASK_A
) >> 3) |
250 ((mask
& PIPE_MASK_A
) >> 2) |
251 ((mask
& PIPE_MASK_A
) >> 1) |
252 (mask
& PIPE_MASK_A
);
255 static unsigned grrg_cmask(unsigned mask
)
257 return ((mask
& PIPE_MASK_R
) << 1) |
258 ((mask
& PIPE_MASK_R
) << 2) |
259 ((mask
& PIPE_MASK_G
) >> 1) |
260 ((mask
& PIPE_MASK_G
) << 2);
263 static unsigned arra_cmask(unsigned mask
)
265 return ((mask
& PIPE_MASK_R
) << 1) |
266 ((mask
& PIPE_MASK_R
) << 2) |
267 ((mask
& PIPE_MASK_A
) >> 3) |
268 (mask
& PIPE_MASK_A
);
271 static unsigned blend_read_enable(unsigned eqRGB
, unsigned eqA
,
272 unsigned dstRGB
, unsigned dstA
,
273 unsigned srcRGB
, unsigned srcA
,
274 boolean src_alpha_optz
)
276 unsigned blend_control
= 0;
278 /* Optimization: some operations do not require the destination color.
280 * When SRC_ALPHA_SATURATE is used, colorbuffer reads must be enabled,
281 * otherwise blending gives incorrect results. It seems to be
283 if (eqRGB
== PIPE_BLEND_MIN
|| eqA
== PIPE_BLEND_MIN
||
284 eqRGB
== PIPE_BLEND_MAX
|| eqA
== PIPE_BLEND_MAX
||
285 dstRGB
!= PIPE_BLENDFACTOR_ZERO
||
286 dstA
!= PIPE_BLENDFACTOR_ZERO
||
287 srcRGB
== PIPE_BLENDFACTOR_DST_COLOR
||
288 srcRGB
== PIPE_BLENDFACTOR_DST_ALPHA
||
289 srcRGB
== PIPE_BLENDFACTOR_INV_DST_COLOR
||
290 srcRGB
== PIPE_BLENDFACTOR_INV_DST_ALPHA
||
291 srcA
== PIPE_BLENDFACTOR_DST_COLOR
||
292 srcA
== PIPE_BLENDFACTOR_DST_ALPHA
||
293 srcA
== PIPE_BLENDFACTOR_INV_DST_COLOR
||
294 srcA
== PIPE_BLENDFACTOR_INV_DST_ALPHA
||
295 srcRGB
== PIPE_BLENDFACTOR_SRC_ALPHA_SATURATE
) {
296 /* Enable reading from the colorbuffer. */
297 blend_control
|= R300_READ_ENABLE
;
299 if (src_alpha_optz
) {
300 /* Optimization: Depending on incoming pixels, we can
301 * conditionally disable the reading in hardware... */
302 if (eqRGB
!= PIPE_BLEND_MIN
&& eqA
!= PIPE_BLEND_MIN
&&
303 eqRGB
!= PIPE_BLEND_MAX
&& eqA
!= PIPE_BLEND_MAX
) {
304 /* Disable reading if SRC_ALPHA == 0. */
305 if ((dstRGB
== PIPE_BLENDFACTOR_SRC_ALPHA
||
306 dstRGB
== PIPE_BLENDFACTOR_ZERO
) &&
307 (dstA
== PIPE_BLENDFACTOR_SRC_COLOR
||
308 dstA
== PIPE_BLENDFACTOR_SRC_ALPHA
||
309 dstA
== PIPE_BLENDFACTOR_ZERO
) &&
310 (srcRGB
!= PIPE_BLENDFACTOR_DST_COLOR
&&
311 srcRGB
!= PIPE_BLENDFACTOR_DST_ALPHA
&&
312 srcRGB
!= PIPE_BLENDFACTOR_INV_DST_COLOR
&&
313 srcRGB
!= PIPE_BLENDFACTOR_INV_DST_ALPHA
)) {
314 blend_control
|= R500_SRC_ALPHA_0_NO_READ
;
317 /* Disable reading if SRC_ALPHA == 1. */
318 if ((dstRGB
== PIPE_BLENDFACTOR_INV_SRC_ALPHA
||
319 dstRGB
== PIPE_BLENDFACTOR_ZERO
) &&
320 (dstA
== PIPE_BLENDFACTOR_INV_SRC_COLOR
||
321 dstA
== PIPE_BLENDFACTOR_INV_SRC_ALPHA
||
322 dstA
== PIPE_BLENDFACTOR_ZERO
) &&
323 (srcRGB
!= PIPE_BLENDFACTOR_DST_COLOR
&&
324 srcRGB
!= PIPE_BLENDFACTOR_DST_ALPHA
&&
325 srcRGB
!= PIPE_BLENDFACTOR_INV_DST_COLOR
&&
326 srcRGB
!= PIPE_BLENDFACTOR_INV_DST_ALPHA
)) {
327 blend_control
|= R500_SRC_ALPHA_1_NO_READ
;
332 return blend_control
;
335 /* Create a new blend state based on the CSO blend state.
337 * This encompasses alpha blending, logic/raster ops, and blend dithering. */
338 static void* r300_create_blend_state(struct pipe_context
* pipe
,
339 const struct pipe_blend_state
* state
)
341 struct r300_screen
* r300screen
= r300_screen(pipe
->screen
);
342 struct r300_blend_state
* blend
= CALLOC_STRUCT(r300_blend_state
);
343 uint32_t blend_control
= 0; /* R300_RB3D_CBLEND: 0x4e04 */
344 uint32_t blend_control_noclamp
= 0; /* R300_RB3D_CBLEND: 0x4e04 */
345 uint32_t blend_control_noalpha
= 0; /* R300_RB3D_CBLEND: 0x4e04 */
346 uint32_t blend_control_noalpha_noclamp
= 0; /* R300_RB3D_CBLEND: 0x4e04 */
347 uint32_t alpha_blend_control
= 0; /* R300_RB3D_ABLEND: 0x4e08 */
348 uint32_t alpha_blend_control_noclamp
= 0; /* R300_RB3D_ABLEND: 0x4e08 */
349 uint32_t alpha_blend_control_noalpha
= 0; /* R300_RB3D_ABLEND: 0x4e08 */
350 uint32_t alpha_blend_control_noalpha_noclamp
= 0; /* R300_RB3D_ABLEND: 0x4e08 */
351 uint32_t rop
= 0; /* R300_RB3D_ROPCNTL: 0x4e18 */
352 uint32_t dither
= 0; /* R300_RB3D_DITHER_CTL: 0x4e50 */
355 const unsigned eqRGB
= state
->rt
[0].rgb_func
;
356 const unsigned srcRGB
= state
->rt
[0].rgb_src_factor
;
357 const unsigned dstRGB
= state
->rt
[0].rgb_dst_factor
;
359 const unsigned eqA
= state
->rt
[0].alpha_func
;
360 const unsigned srcA
= state
->rt
[0].alpha_src_factor
;
361 const unsigned dstA
= state
->rt
[0].alpha_dst_factor
;
363 unsigned srcRGBX
= srcRGB
;
364 unsigned dstRGBX
= dstRGB
;
367 blend
->state
= *state
;
369 /* force DST_ALPHA to ONE where we can */
371 case PIPE_BLENDFACTOR_DST_ALPHA
:
372 srcRGBX
= PIPE_BLENDFACTOR_ONE
;
374 case PIPE_BLENDFACTOR_INV_DST_ALPHA
:
375 srcRGBX
= PIPE_BLENDFACTOR_ZERO
;
380 case PIPE_BLENDFACTOR_DST_ALPHA
:
381 dstRGBX
= PIPE_BLENDFACTOR_ONE
;
383 case PIPE_BLENDFACTOR_INV_DST_ALPHA
:
384 dstRGBX
= PIPE_BLENDFACTOR_ZERO
;
388 /* Get blending register values. */
389 if (state
->rt
[0].blend_enable
) {
390 unsigned blend_eq
, blend_eq_noclamp
;
392 /* despite the name, ALPHA_BLEND_ENABLE has nothing to do with alpha,
393 * this is just the crappy D3D naming */
394 blend_control
= blend_control_noclamp
=
395 R300_ALPHA_BLEND_ENABLE
|
396 ( r300_translate_blend_factor(srcRGB
) << R300_SRC_BLEND_SHIFT
) |
397 ( r300_translate_blend_factor(dstRGB
) << R300_DST_BLEND_SHIFT
);
399 blend_control_noalpha
= blend_control_noalpha_noclamp
=
400 R300_ALPHA_BLEND_ENABLE
|
401 ( r300_translate_blend_factor(srcRGBX
) << R300_SRC_BLEND_SHIFT
) |
402 ( r300_translate_blend_factor(dstRGBX
) << R300_DST_BLEND_SHIFT
);
404 blend_eq
= r300_translate_blend_function(eqRGB
, TRUE
);
405 blend_eq_noclamp
= r300_translate_blend_function(eqRGB
, FALSE
);
407 blend_control
|= blend_eq
;
408 blend_control_noalpha
|= blend_eq
;
409 blend_control_noclamp
|= blend_eq_noclamp
;
410 blend_control_noalpha_noclamp
|= blend_eq_noclamp
;
412 /* Optimization: some operations do not require the destination color. */
413 blend_control
|= blend_read_enable(eqRGB
, eqA
, dstRGB
, dstA
,
414 srcRGB
, srcA
, r300screen
->caps
.is_r500
);
415 blend_control_noclamp
|= blend_read_enable(eqRGB
, eqA
, dstRGB
, dstA
,
416 srcRGB
, srcA
, FALSE
);
417 blend_control_noalpha
|= blend_read_enable(eqRGB
, eqA
, dstRGBX
, dstA
,
418 srcRGBX
, srcA
, r300screen
->caps
.is_r500
);
419 blend_control_noalpha_noclamp
|= blend_read_enable(eqRGB
, eqA
, dstRGBX
, dstA
,
420 srcRGBX
, srcA
, FALSE
);
422 /* Optimization: discard pixels which don't change the colorbuffer.
423 * It cannot be used with FP16 AA. */
424 blend_control
|= blend_discard_conditionally(eqRGB
, eqA
, dstRGB
, dstA
,
426 blend_control_noalpha
|= blend_discard_conditionally(eqRGB
, eqA
, dstRGBX
, dstA
,
430 if (srcA
!= srcRGB
|| dstA
!= dstRGB
|| eqA
!= eqRGB
) {
431 blend_control
|= R300_SEPARATE_ALPHA_ENABLE
;
432 blend_control_noclamp
|= R300_SEPARATE_ALPHA_ENABLE
;
434 alpha_blend_control
= alpha_blend_control_noclamp
=
435 (r300_translate_blend_factor(srcA
) << R300_SRC_BLEND_SHIFT
) |
436 (r300_translate_blend_factor(dstA
) << R300_DST_BLEND_SHIFT
);
437 alpha_blend_control
|= r300_translate_blend_function(eqA
, TRUE
);
438 alpha_blend_control_noclamp
|= r300_translate_blend_function(eqA
, FALSE
);
440 if (srcA
!= srcRGBX
|| dstA
!= dstRGBX
|| eqA
!= eqRGB
) {
441 blend_control_noalpha
|= R300_SEPARATE_ALPHA_ENABLE
;
442 blend_control_noalpha_noclamp
|= R300_SEPARATE_ALPHA_ENABLE
;
444 alpha_blend_control_noalpha
= alpha_blend_control_noalpha_noclamp
=
445 (r300_translate_blend_factor(srcA
) << R300_SRC_BLEND_SHIFT
) |
446 (r300_translate_blend_factor(dstA
) << R300_DST_BLEND_SHIFT
);
447 alpha_blend_control_noalpha
|= r300_translate_blend_function(eqA
, TRUE
);
448 alpha_blend_control_noalpha_noclamp
|= r300_translate_blend_function(eqA
, FALSE
);
452 /* PIPE_LOGICOP_* don't need to be translated, fortunately. */
453 if (state
->logicop_enable
) {
454 rop
= R300_RB3D_ROPCNTL_ROP_ENABLE
|
455 (state
->logicop_func
) << R300_RB3D_ROPCNTL_ROP_SHIFT
;
458 /* Neither fglrx nor classic r300 ever set this, regardless of dithering
459 * state. Since it's an optional implementation detail, we can leave it
460 * out and never dither.
462 * This could be revisited if we ever get quality or conformance hints.
465 dither = R300_RB3D_DITHER_CTL_DITHER_MODE_LUT |
466 R300_RB3D_DITHER_CTL_ALPHA_DITHER_MODE_LUT;
470 /* Build a command buffer. */
472 unsigned (*func
[COLORMASK_NUM_SWIZZLES
])(unsigned) = {
483 for (i
= 0; i
< COLORMASK_NUM_SWIZZLES
; i
++) {
484 boolean has_alpha
= i
!= COLORMASK_RGBX
&& i
!= COLORMASK_BGRX
;
486 BEGIN_CB(blend
->cb_clamp
[i
], 8);
487 OUT_CB_REG(R300_RB3D_ROPCNTL
, rop
);
488 OUT_CB_REG_SEQ(R300_RB3D_CBLEND
, 3);
489 OUT_CB(has_alpha
? blend_control
: blend_control_noalpha
);
490 OUT_CB(has_alpha
? alpha_blend_control
: alpha_blend_control_noalpha
);
491 OUT_CB(func
[i
](state
->rt
[0].colormask
));
492 OUT_CB_REG(R300_RB3D_DITHER_CTL
, dither
);
497 /* Build a command buffer (for RGBA16F). */
498 BEGIN_CB(blend
->cb_noclamp
, 8);
499 OUT_CB_REG(R300_RB3D_ROPCNTL
, rop
);
500 OUT_CB_REG_SEQ(R300_RB3D_CBLEND
, 3);
501 OUT_CB(blend_control_noclamp
);
502 OUT_CB(alpha_blend_control_noclamp
);
503 OUT_CB(rgba_cmask(state
->rt
[0].colormask
));
504 OUT_CB_REG(R300_RB3D_DITHER_CTL
, dither
);
507 /* Build a command buffer (for RGB16F). */
508 BEGIN_CB(blend
->cb_noclamp_noalpha
, 8);
509 OUT_CB_REG(R300_RB3D_ROPCNTL
, rop
);
510 OUT_CB_REG_SEQ(R300_RB3D_CBLEND
, 3);
511 OUT_CB(blend_control_noalpha_noclamp
);
512 OUT_CB(alpha_blend_control_noalpha_noclamp
);
513 OUT_CB(rgba_cmask(state
->rt
[0].colormask
));
514 OUT_CB_REG(R300_RB3D_DITHER_CTL
, dither
);
517 /* The same as above, but with no colorbuffer reads and writes. */
518 BEGIN_CB(blend
->cb_no_readwrite
, 8);
519 OUT_CB_REG(R300_RB3D_ROPCNTL
, rop
);
520 OUT_CB_REG_SEQ(R300_RB3D_CBLEND
, 3);
524 OUT_CB_REG(R300_RB3D_DITHER_CTL
, dither
);
530 /* Bind blend state. */
531 static void r300_bind_blend_state(struct pipe_context
* pipe
,
534 struct r300_context
* r300
= r300_context(pipe
);
535 struct r300_blend_state
*blend
= (struct r300_blend_state
*)state
;
536 boolean last_alpha_to_one
= r300
->alpha_to_one
;
537 boolean last_alpha_to_coverage
= r300
->alpha_to_coverage
;
539 UPDATE_STATE(state
, r300
->blend_state
);
544 r300
->alpha_to_one
= blend
->state
.alpha_to_one
;
545 r300
->alpha_to_coverage
= blend
->state
.alpha_to_coverage
;
547 if (r300
->alpha_to_one
!= last_alpha_to_one
&& r300
->msaa_enable
&&
548 r300
->fs_status
== FRAGMENT_SHADER_VALID
) {
549 r300
->fs_status
= FRAGMENT_SHADER_MAYBE_DIRTY
;
552 if (r300
->alpha_to_coverage
!= last_alpha_to_coverage
&&
554 r300_mark_atom_dirty(r300
, &r300
->dsa_state
);
558 /* Free blend state. */
559 static void r300_delete_blend_state(struct pipe_context
* pipe
,
565 /* Convert float to 10bit integer */
566 static unsigned float_to_fixed10(float f
)
568 return CLAMP((unsigned)(f
* 1023.9f
), 0, 1023);
572 * Setup both R300 and R500 registers, figure out later which one to write. */
573 static void r300_set_blend_color(struct pipe_context
* pipe
,
574 const struct pipe_blend_color
* color
)
576 struct r300_context
* r300
= r300_context(pipe
);
577 struct pipe_framebuffer_state
*fb
= r300
->fb_state
.state
;
578 struct r300_blend_color_state
*state
=
579 (struct r300_blend_color_state
*)r300
->blend_color_state
.state
;
580 struct pipe_blend_color c
;
581 struct pipe_surface
*cb
;
585 state
->state
= *color
; /* Save it, so that we can reuse it in set_fb_state */
587 cb
= fb
->nr_cbufs
? r300_get_nonnull_cb(fb
, 0) : NULL
;
589 /* The blend color is dependent on the colorbuffer format. */
591 switch (cb
->format
) {
592 case PIPE_FORMAT_R8_UNORM
:
593 case PIPE_FORMAT_L8_UNORM
:
594 case PIPE_FORMAT_I8_UNORM
:
595 c
.color
[1] = c
.color
[0];
598 case PIPE_FORMAT_A8_UNORM
:
599 c
.color
[1] = c
.color
[3];
602 case PIPE_FORMAT_R8G8_UNORM
:
603 c
.color
[2] = c
.color
[1];
606 case PIPE_FORMAT_L8A8_UNORM
:
607 case PIPE_FORMAT_R8A8_UNORM
:
608 c
.color
[2] = c
.color
[3];
611 case PIPE_FORMAT_R8G8B8A8_UNORM
:
612 case PIPE_FORMAT_R8G8B8X8_UNORM
:
614 c
.color
[0] = c
.color
[2];
622 if (r300
->screen
->caps
.is_r500
) {
623 BEGIN_CB(state
->cb
, 3);
624 OUT_CB_REG_SEQ(R500_RB3D_CONSTANT_COLOR_AR
, 2);
626 switch (cb
? cb
->format
: 0) {
627 case PIPE_FORMAT_R16G16B16A16_FLOAT
:
628 case PIPE_FORMAT_R16G16B16X16_FLOAT
:
629 OUT_CB(util_float_to_half(c
.color
[2]) |
630 (util_float_to_half(c
.color
[3]) << 16));
631 OUT_CB(util_float_to_half(c
.color
[0]) |
632 (util_float_to_half(c
.color
[1]) << 16));
636 OUT_CB(float_to_fixed10(c
.color
[0]) |
637 (float_to_fixed10(c
.color
[3]) << 16));
638 OUT_CB(float_to_fixed10(c
.color
[2]) |
639 (float_to_fixed10(c
.color
[1]) << 16));
645 util_pack_color(c
.color
, PIPE_FORMAT_B8G8R8A8_UNORM
, &uc
);
647 BEGIN_CB(state
->cb
, 2);
648 OUT_CB_REG(R300_RB3D_BLEND_COLOR
, uc
.ui
[0]);
652 r300_mark_atom_dirty(r300
, &r300
->blend_color_state
);
655 static void r300_set_clip_state(struct pipe_context
* pipe
,
656 const struct pipe_clip_state
* state
)
658 struct r300_context
* r300
= r300_context(pipe
);
659 struct r300_clip_state
*clip
=
660 (struct r300_clip_state
*)r300
->clip_state
.state
;
663 if (r300
->screen
->caps
.has_tcl
) {
664 BEGIN_CB(clip
->cb
, r300
->clip_state
.size
);
665 OUT_CB_REG(R300_VAP_PVS_VECTOR_INDX_REG
,
666 (r300
->screen
->caps
.is_r500
?
667 R500_PVS_UCP_START
: R300_PVS_UCP_START
));
668 OUT_CB_ONE_REG(R300_VAP_PVS_UPLOAD_DATA
, 6 * 4);
669 OUT_CB_TABLE(state
->ucp
, 6 * 4);
672 r300_mark_atom_dirty(r300
, &r300
->clip_state
);
674 draw_set_clip_state(r300
->draw
, state
);
678 /* Create a new depth, stencil, and alpha state based on the CSO dsa state.
680 * This contains the depth buffer, stencil buffer, alpha test, and such.
681 * On the Radeon, depth and stencil buffer setup are intertwined, which is
682 * the reason for some of the strange-looking assignments across registers. */
683 static void* r300_create_dsa_state(struct pipe_context
* pipe
,
684 const struct pipe_depth_stencil_alpha_state
* state
)
686 boolean is_r500
= r300_screen(pipe
->screen
)->caps
.is_r500
;
687 struct r300_dsa_state
* dsa
= CALLOC_STRUCT(r300_dsa_state
);
689 uint32_t alpha_value_fp16
= 0;
690 uint32_t z_buffer_control
= 0;
691 uint32_t z_stencil_control
= 0;
692 uint32_t stencil_ref_mask
= 0;
693 uint32_t stencil_ref_bf
= 0;
697 /* Depth test setup. - separate write mask depth for decomp flush */
698 if (state
->depth
.writemask
) {
699 z_buffer_control
|= R300_Z_WRITE_ENABLE
;
702 if (state
->depth
.enabled
) {
703 z_buffer_control
|= R300_Z_ENABLE
;
706 (r300_translate_depth_stencil_function(state
->depth
.func
) <<
710 /* Stencil buffer setup. */
711 if (state
->stencil
[0].enabled
) {
712 z_buffer_control
|= R300_STENCIL_ENABLE
;
714 (r300_translate_depth_stencil_function(state
->stencil
[0].func
) <<
715 R300_S_FRONT_FUNC_SHIFT
) |
716 (r300_translate_stencil_op(state
->stencil
[0].fail_op
) <<
717 R300_S_FRONT_SFAIL_OP_SHIFT
) |
718 (r300_translate_stencil_op(state
->stencil
[0].zpass_op
) <<
719 R300_S_FRONT_ZPASS_OP_SHIFT
) |
720 (r300_translate_stencil_op(state
->stencil
[0].zfail_op
) <<
721 R300_S_FRONT_ZFAIL_OP_SHIFT
);
724 (state
->stencil
[0].valuemask
<< R300_STENCILMASK_SHIFT
) |
725 (state
->stencil
[0].writemask
<< R300_STENCILWRITEMASK_SHIFT
);
727 if (state
->stencil
[1].enabled
) {
728 dsa
->two_sided
= TRUE
;
730 z_buffer_control
|= R300_STENCIL_FRONT_BACK
;
732 (r300_translate_depth_stencil_function(state
->stencil
[1].func
) <<
733 R300_S_BACK_FUNC_SHIFT
) |
734 (r300_translate_stencil_op(state
->stencil
[1].fail_op
) <<
735 R300_S_BACK_SFAIL_OP_SHIFT
) |
736 (r300_translate_stencil_op(state
->stencil
[1].zpass_op
) <<
737 R300_S_BACK_ZPASS_OP_SHIFT
) |
738 (r300_translate_stencil_op(state
->stencil
[1].zfail_op
) <<
739 R300_S_BACK_ZFAIL_OP_SHIFT
);
742 (state
->stencil
[1].valuemask
<< R300_STENCILMASK_SHIFT
) |
743 (state
->stencil
[1].writemask
<< R300_STENCILWRITEMASK_SHIFT
);
746 z_buffer_control
|= R500_STENCIL_REFMASK_FRONT_BACK
;
748 dsa
->two_sided_stencil_ref
=
749 (state
->stencil
[0].valuemask
!= state
->stencil
[1].valuemask
||
750 state
->stencil
[0].writemask
!= state
->stencil
[1].writemask
);
755 /* Alpha test setup. */
756 if (state
->alpha
.enabled
) {
757 dsa
->alpha_function
=
758 r300_translate_alpha_function(state
->alpha
.func
) |
759 R300_FG_ALPHA_FUNC_ENABLE
;
761 dsa
->alpha_function
|= float_to_ubyte(state
->alpha
.ref_value
);
762 alpha_value_fp16
= util_float_to_half(state
->alpha
.ref_value
);
765 BEGIN_CB(&dsa
->cb_begin
, 8);
766 OUT_CB_REG_SEQ(R300_ZB_CNTL
, 3);
767 OUT_CB(z_buffer_control
);
768 OUT_CB(z_stencil_control
);
769 OUT_CB(stencil_ref_mask
);
770 OUT_CB_REG(R500_ZB_STENCILREFMASK_BF
, stencil_ref_bf
);
771 OUT_CB_REG(R500_FG_ALPHA_VALUE
, alpha_value_fp16
);
774 BEGIN_CB(dsa
->cb_zb_no_readwrite
, 8);
775 OUT_CB_REG_SEQ(R300_ZB_CNTL
, 3);
779 OUT_CB_REG(R500_ZB_STENCILREFMASK_BF
, 0);
780 OUT_CB_REG(R500_FG_ALPHA_VALUE
, alpha_value_fp16
);
786 static void r300_dsa_inject_stencilref(struct r300_context
*r300
)
788 struct r300_dsa_state
*dsa
=
789 (struct r300_dsa_state
*)r300
->dsa_state
.state
;
794 dsa
->stencil_ref_mask
=
795 (dsa
->stencil_ref_mask
& ~R300_STENCILREF_MASK
) |
796 r300
->stencil_ref
.ref_value
[0];
797 dsa
->stencil_ref_bf
=
798 (dsa
->stencil_ref_bf
& ~R300_STENCILREF_MASK
) |
799 r300
->stencil_ref
.ref_value
[1];
802 /* Bind DSA state. */
803 static void r300_bind_dsa_state(struct pipe_context
* pipe
,
806 struct r300_context
* r300
= r300_context(pipe
);
812 UPDATE_STATE(state
, r300
->dsa_state
);
814 r300_mark_atom_dirty(r300
, &r300
->hyperz_state
); /* Will be updated before the emission. */
815 r300_dsa_inject_stencilref(r300
);
818 /* Free DSA state. */
819 static void r300_delete_dsa_state(struct pipe_context
* pipe
,
825 static void r300_set_stencil_ref(struct pipe_context
* pipe
,
826 const struct pipe_stencil_ref
* sr
)
828 struct r300_context
* r300
= r300_context(pipe
);
830 r300
->stencil_ref
= *sr
;
832 r300_dsa_inject_stencilref(r300
);
833 r300_mark_atom_dirty(r300
, &r300
->dsa_state
);
836 static void r300_print_fb_surf_info(struct pipe_surface
*surf
, unsigned index
,
839 struct pipe_resource
*tex
= surf
->texture
;
840 struct r300_resource
*rtex
= r300_resource(tex
);
843 "r300: %s[%i] Dim: %ix%i, Firstlayer: %i, "
844 "Lastlayer: %i, Level: %i, Format: %s\n"
846 "r300: TEX: Macro: %s, Micro: %s, "
847 "Dim: %ix%ix%i, LastLevel: %i, Format: %s\n",
849 binding
, index
, surf
->width
, surf
->height
,
850 surf
->u
.tex
.first_layer
, surf
->u
.tex
.last_layer
, surf
->u
.tex
.level
,
851 util_format_short_name(surf
->format
),
853 rtex
->tex
.macrotile
[0] ? "YES" : " NO",
854 rtex
->tex
.microtile
? "YES" : " NO",
855 tex
->width0
, tex
->height0
, tex
->depth0
,
856 tex
->last_level
, util_format_short_name(surf
->format
));
859 void r300_mark_fb_state_dirty(struct r300_context
*r300
,
860 enum r300_fb_state_change change
)
862 struct pipe_framebuffer_state
*state
= r300
->fb_state
.state
;
864 r300_mark_atom_dirty(r300
, &r300
->gpu_flush
);
865 r300_mark_atom_dirty(r300
, &r300
->fb_state
);
867 /* What is marked as dirty depends on the enum r300_fb_state_change. */
868 if (change
== R300_CHANGED_FB_STATE
) {
869 r300_mark_atom_dirty(r300
, &r300
->aa_state
);
870 r300_mark_atom_dirty(r300
, &r300
->dsa_state
); /* for AlphaRef */
871 r300_set_blend_color(&r300
->context
, r300
->blend_color_state
.state
);
874 if (change
== R300_CHANGED_FB_STATE
||
875 change
== R300_CHANGED_HYPERZ_FLAG
) {
876 r300_mark_atom_dirty(r300
, &r300
->hyperz_state
);
879 if (change
== R300_CHANGED_FB_STATE
||
880 change
== R300_CHANGED_MULTIWRITE
) {
881 r300_mark_atom_dirty(r300
, &r300
->fb_state_pipelined
);
884 /* Now compute the fb_state atom size. */
885 r300
->fb_state
.size
= 2 + (8 * state
->nr_cbufs
);
887 if (r300
->cbzb_clear
)
888 r300
->fb_state
.size
+= 10;
889 else if (state
->zsbuf
) {
890 r300
->fb_state
.size
+= 10;
891 if (r300
->hyperz_enabled
)
892 r300
->fb_state
.size
+= 8;
895 if (r300
->cmask_in_use
) {
896 r300
->fb_state
.size
+= 6;
897 if (r300
->screen
->caps
.is_r500
&& r300
->screen
->info
.drm_minor
>= 29) {
898 r300
->fb_state
.size
+= 3;
902 /* The size of the rest of atoms stays the same. */
906 r300_set_framebuffer_state(struct pipe_context
* pipe
,
907 const struct pipe_framebuffer_state
* state
)
909 struct r300_context
* r300
= r300_context(pipe
);
910 struct r300_aa_state
*aa
= (struct r300_aa_state
*)r300
->aa_state
.state
;
911 struct pipe_framebuffer_state
*current_state
= r300
->fb_state
.state
;
912 unsigned max_width
, max_height
, i
;
913 uint32_t zbuffer_bpp
= 0;
914 boolean unlock_zbuffer
= FALSE
;
916 if (r300
->screen
->caps
.is_r500
) {
917 max_width
= max_height
= 4096;
918 } else if (r300
->screen
->caps
.is_r400
) {
919 max_width
= max_height
= 4021;
921 max_width
= max_height
= 2560;
924 if (state
->width
> max_width
|| state
->height
> max_height
) {
925 fprintf(stderr
, "r300: Implementation error: Render targets are too "
926 "big in %s, refusing to bind framebuffer state!\n", __FUNCTION__
);
930 if (current_state
->zsbuf
&& r300
->zmask_in_use
&& !r300
->locked_zbuffer
) {
931 /* There is a zmask in use, what are we gonna do? */
933 if (!pipe_surface_equal(current_state
->zsbuf
, state
->zsbuf
)) {
934 /* Decompress the currently bound zbuffer before we bind another one. */
935 r300_decompress_zmask(r300
);
936 r300
->hiz_in_use
= FALSE
;
939 /* We don't bind another zbuffer, so lock the current one. */
940 pipe_surface_reference(&r300
->locked_zbuffer
, current_state
->zsbuf
);
942 } else if (r300
->locked_zbuffer
) {
943 /* We have a locked zbuffer now, what are we gonna do? */
945 if (!pipe_surface_equal(r300
->locked_zbuffer
, state
->zsbuf
)) {
946 /* We are binding some other zbuffer, so decompress the locked one,
947 * it gets unlocked automatically. */
948 r300_decompress_zmask_locked_unsafe(r300
);
949 r300
->hiz_in_use
= FALSE
;
951 /* We are binding the locked zbuffer again, so unlock it. */
952 unlock_zbuffer
= TRUE
;
956 assert(state
->zsbuf
|| (r300
->locked_zbuffer
&& !unlock_zbuffer
) || !r300
->zmask_in_use
);
958 /* If zsbuf is set from NULL to non-NULL or vice versa.. */
959 if (!!current_state
->zsbuf
!= !!state
->zsbuf
) {
960 r300_mark_atom_dirty(r300
, &r300
->dsa_state
);
963 util_copy_framebuffer_state(r300
->fb_state
.state
, state
);
965 /* Remove trailing NULL colorbuffers. */
966 while (current_state
->nr_cbufs
&& !current_state
->cbufs
[current_state
->nr_cbufs
-1])
967 current_state
->nr_cbufs
--;
969 /* Set whether CMASK can be used. */
971 state
->nr_cbufs
== 1 && state
->cbufs
[0] &&
972 r300
->screen
->cmask_resource
== state
->cbufs
[0]->texture
;
974 /* Need to reset clamping or colormask. */
975 r300_mark_atom_dirty(r300
, &r300
->blend_state
);
977 /* Re-swizzle the blend color. */
978 r300_set_blend_color(pipe
, &((struct r300_blend_color_state
*)r300
->blend_color_state
.state
)->state
);
980 if (unlock_zbuffer
) {
981 pipe_surface_reference(&r300
->locked_zbuffer
, NULL
);
984 r300_mark_fb_state_dirty(r300
, R300_CHANGED_FB_STATE
);
987 switch (util_format_get_blocksize(state
->zsbuf
->format
)) {
996 /* Polygon offset depends on the zbuffer bit depth. */
997 if (r300
->zbuffer_bpp
!= zbuffer_bpp
) {
998 r300
->zbuffer_bpp
= zbuffer_bpp
;
1000 if (r300
->polygon_offset_enabled
)
1001 r300_mark_atom_dirty(r300
, &r300
->rs_state
);
1005 r300
->num_samples
= util_framebuffer_get_num_samples(state
);
1007 /* Set up AA config. */
1008 if (r300
->num_samples
> 1) {
1009 switch (r300
->num_samples
) {
1011 aa
->aa_config
= R300_GB_AA_CONFIG_AA_ENABLE
|
1012 R300_GB_AA_CONFIG_NUM_AA_SUBSAMPLES_2
;
1015 aa
->aa_config
= R300_GB_AA_CONFIG_AA_ENABLE
|
1016 R300_GB_AA_CONFIG_NUM_AA_SUBSAMPLES_4
;
1019 aa
->aa_config
= R300_GB_AA_CONFIG_AA_ENABLE
|
1020 R300_GB_AA_CONFIG_NUM_AA_SUBSAMPLES_6
;
1027 if (DBG_ON(r300
, DBG_FB
)) {
1028 fprintf(stderr
, "r300: set_framebuffer_state:\n");
1029 for (i
= 0; i
< state
->nr_cbufs
; i
++) {
1030 if (state
->cbufs
[i
])
1031 r300_print_fb_surf_info(state
->cbufs
[i
], i
, "CB");
1034 r300_print_fb_surf_info(state
->zsbuf
, 0, "ZB");
1039 /* Create fragment shader state. */
1040 static void* r300_create_fs_state(struct pipe_context
* pipe
,
1041 const struct pipe_shader_state
* shader
)
1043 struct r300_fragment_shader
* fs
= NULL
;
1045 fs
= (struct r300_fragment_shader
*)CALLOC_STRUCT(r300_fragment_shader
);
1047 /* Copy state directly into shader. */
1048 fs
->state
= *shader
;
1049 fs
->state
.tokens
= tgsi_dup_tokens(shader
->tokens
);
1054 void r300_mark_fs_code_dirty(struct r300_context
*r300
)
1056 struct r300_fragment_shader
* fs
= r300_fs(r300
);
1058 r300_mark_atom_dirty(r300
, &r300
->fs
);
1059 r300_mark_atom_dirty(r300
, &r300
->fs_rc_constant_state
);
1060 r300_mark_atom_dirty(r300
, &r300
->fs_constants
);
1061 r300
->fs
.size
= fs
->shader
->cb_code_size
;
1063 if (r300
->screen
->caps
.is_r500
) {
1064 r300
->fs_rc_constant_state
.size
= fs
->shader
->rc_state_count
* 7;
1065 r300
->fs_constants
.size
= fs
->shader
->externals_count
* 4 + 3;
1067 r300
->fs_rc_constant_state
.size
= fs
->shader
->rc_state_count
* 5;
1068 r300
->fs_constants
.size
= fs
->shader
->externals_count
* 4 + 1;
1071 ((struct r300_constant_buffer
*)r300
->fs_constants
.state
)->remap_table
=
1072 fs
->shader
->code
.constants_remap_table
;
1075 /* Bind fragment shader state. */
1076 static void r300_bind_fs_state(struct pipe_context
* pipe
, void* shader
)
1078 struct r300_context
* r300
= r300_context(pipe
);
1079 struct r300_fragment_shader
* fs
= (struct r300_fragment_shader
*)shader
;
1082 r300
->fs
.state
= NULL
;
1086 r300
->fs
.state
= fs
;
1087 r300
->fs_status
= FRAGMENT_SHADER_DIRTY
;
1089 r300_mark_atom_dirty(r300
, &r300
->rs_block_state
); /* Will be updated before the emission. */
1092 /* Delete fragment shader state. */
1093 static void r300_delete_fs_state(struct pipe_context
* pipe
, void* shader
)
1095 struct r300_fragment_shader
* fs
= (struct r300_fragment_shader
*)shader
;
1096 struct r300_fragment_shader_code
*tmp
, *ptr
= fs
->first
;
1101 rc_constants_destroy(&tmp
->code
.constants
);
1105 FREE((void*)fs
->state
.tokens
);
1109 static void r300_set_polygon_stipple(struct pipe_context
* pipe
,
1110 const struct pipe_poly_stipple
* state
)
1114 /* Create a new rasterizer state based on the CSO rasterizer state.
1116 * This is a very large chunk of state, and covers most of the graphics
1117 * backend (GB), geometry assembly (GA), and setup unit (SU) blocks.
1119 * In a not entirely unironic sidenote, this state has nearly nothing to do
1120 * with the actual block on the Radeon called the rasterizer (RS). */
1121 static void* r300_create_rs_state(struct pipe_context
* pipe
,
1122 const struct pipe_rasterizer_state
* state
)
1124 struct r300_rs_state
* rs
= CALLOC_STRUCT(r300_rs_state
);
1125 uint32_t vap_control_status
; /* R300_VAP_CNTL_STATUS: 0x2140 */
1126 uint32_t vap_clip_cntl
; /* R300_VAP_CLIP_CNTL: 0x221C */
1127 uint32_t point_size
; /* R300_GA_POINT_SIZE: 0x421c */
1128 uint32_t point_minmax
; /* R300_GA_POINT_MINMAX: 0x4230 */
1129 uint32_t line_control
; /* R300_GA_LINE_CNTL: 0x4234 */
1130 uint32_t polygon_offset_enable
; /* R300_SU_POLY_OFFSET_ENABLE: 0x42b4 */
1131 uint32_t cull_mode
; /* R300_SU_CULL_MODE: 0x42b8 */
1132 uint32_t line_stipple_config
; /* R300_GA_LINE_STIPPLE_CONFIG: 0x4328 */
1133 uint32_t line_stipple_value
; /* R300_GA_LINE_STIPPLE_VALUE: 0x4260 */
1134 uint32_t polygon_mode
; /* R300_GA_POLY_MODE: 0x4288 */
1135 uint32_t clip_rule
; /* R300_SC_CLIP_RULE: 0x43D0 */
1136 uint32_t round_mode
; /* R300_GA_ROUND_MODE: 0x428c */
1138 /* Point sprites texture coordinates, 0: lower left, 1: upper right */
1139 float point_texcoord_left
= 0; /* R300_GA_POINT_S0: 0x4200 */
1140 float point_texcoord_bottom
= 0;/* R300_GA_POINT_T0: 0x4204 */
1141 float point_texcoord_right
= 1; /* R300_GA_POINT_S1: 0x4208 */
1142 float point_texcoord_top
= 0; /* R300_GA_POINT_T1: 0x420c */
1143 boolean vclamp
= !r300_context(pipe
)->screen
->caps
.is_r500
;
1146 /* Copy rasterizer state. */
1148 rs
->rs_draw
= *state
;
1150 rs
->rs
.sprite_coord_enable
= state
->point_quad_rasterization
*
1151 state
->sprite_coord_enable
;
1153 /* Override some states for Draw. */
1154 rs
->rs_draw
.sprite_coord_enable
= 0; /* We can do this in HW. */
1155 rs
->rs_draw
.offset_point
= 0;
1156 rs
->rs_draw
.offset_line
= 0;
1157 rs
->rs_draw
.offset_tri
= 0;
1158 rs
->rs_draw
.offset_clamp
= 0;
1160 #if UTIL_ARCH_LITTLE_ENDIAN
1161 vap_control_status
= R300_VC_NO_SWAP
;
1163 vap_control_status
= R300_VC_32BIT_SWAP
;
1166 /* If no TCL engine is present, turn off the HW TCL. */
1167 if (!r300_screen(pipe
->screen
)->caps
.has_tcl
) {
1168 vap_control_status
|= R300_VAP_TCL_BYPASS
;
1171 /* Point size width and height. */
1173 pack_float_16_6x(state
->point_size
) |
1174 (pack_float_16_6x(state
->point_size
) << R300_POINTSIZE_X_SHIFT
);
1176 /* Point size clamping. */
1177 if (state
->point_size_per_vertex
) {
1178 /* Per-vertex point size.
1179 * Clamp to [0, max FB size] */
1180 float min_psiz
= util_get_min_point_size(state
);
1181 float max_psiz
= pipe
->screen
->get_paramf(pipe
->screen
,
1182 PIPE_CAPF_MAX_POINT_WIDTH
);
1184 (pack_float_16_6x(min_psiz
) << R300_GA_POINT_MINMAX_MIN_SHIFT
) |
1185 (pack_float_16_6x(max_psiz
) << R300_GA_POINT_MINMAX_MAX_SHIFT
);
1187 /* We cannot disable the point-size vertex output,
1189 float psiz
= state
->point_size
;
1191 (pack_float_16_6x(psiz
) << R300_GA_POINT_MINMAX_MIN_SHIFT
) |
1192 (pack_float_16_6x(psiz
) << R300_GA_POINT_MINMAX_MAX_SHIFT
);
1196 line_control
= pack_float_16_6x(state
->line_width
) |
1197 R300_GA_LINE_CNTL_END_TYPE_COMP
;
1199 /* Enable polygon mode */
1201 if (state
->fill_front
!= PIPE_POLYGON_MODE_FILL
||
1202 state
->fill_back
!= PIPE_POLYGON_MODE_FILL
) {
1203 polygon_mode
= R300_GA_POLY_MODE_DUAL
;
1207 if (state
->front_ccw
)
1208 cull_mode
= R300_FRONT_FACE_CCW
;
1210 cull_mode
= R300_FRONT_FACE_CW
;
1212 /* Polygon offset */
1213 polygon_offset_enable
= 0;
1214 if (util_get_offset(state
, state
->fill_front
)) {
1215 polygon_offset_enable
|= R300_FRONT_ENABLE
;
1217 if (util_get_offset(state
, state
->fill_back
)) {
1218 polygon_offset_enable
|= R300_BACK_ENABLE
;
1221 rs
->polygon_offset_enable
= polygon_offset_enable
!= 0;
1226 r300_translate_polygon_mode_front(state
->fill_front
);
1228 r300_translate_polygon_mode_back(state
->fill_back
);
1231 if (state
->cull_face
& PIPE_FACE_FRONT
) {
1232 cull_mode
|= R300_CULL_FRONT
;
1234 if (state
->cull_face
& PIPE_FACE_BACK
) {
1235 cull_mode
|= R300_CULL_BACK
;
1238 if (state
->line_stipple_enable
) {
1239 line_stipple_config
=
1240 R300_GA_LINE_STIPPLE_CONFIG_LINE_RESET_LINE
|
1241 (fui((float)state
->line_stipple_factor
) &
1242 R300_GA_LINE_STIPPLE_CONFIG_STIPPLE_SCALE_MASK
);
1243 /* XXX this might need to be scaled up */
1244 line_stipple_value
= state
->line_stipple_pattern
;
1246 line_stipple_config
= 0;
1247 line_stipple_value
= 0;
1250 if (state
->flatshade
) {
1251 rs
->color_control
= R300_SHADE_MODEL_FLAT
;
1253 rs
->color_control
= R300_SHADE_MODEL_SMOOTH
;
1256 clip_rule
= state
->scissor
? 0xAAAA : 0xFFFF;
1258 /* Point sprites coord mode */
1259 if (rs
->rs
.sprite_coord_enable
) {
1260 switch (state
->sprite_coord_mode
) {
1261 case PIPE_SPRITE_COORD_UPPER_LEFT
:
1262 point_texcoord_top
= 0.0f
;
1263 point_texcoord_bottom
= 1.0f
;
1265 case PIPE_SPRITE_COORD_LOWER_LEFT
:
1266 point_texcoord_top
= 1.0f
;
1267 point_texcoord_bottom
= 0.0f
;
1272 if (r300_screen(pipe
->screen
)->caps
.has_tcl
) {
1273 vap_clip_cntl
= (state
->clip_plane_enable
& 63) |
1274 R300_PS_UCP_MODE_CLIP_AS_TRIFAN
;
1276 vap_clip_cntl
= R300_CLIP_DISABLE
;
1279 /* Vertex color clamping. FP20 means no clamping. */
1281 R300_GA_ROUND_MODE_GEOMETRY_ROUND_NEAREST
|
1282 (!vclamp
? (R300_GA_ROUND_MODE_RGB_CLAMP_FP20
|
1283 R300_GA_ROUND_MODE_ALPHA_CLAMP_FP20
) : 0);
1285 /* Build the main command buffer. */
1286 BEGIN_CB(rs
->cb_main
, RS_STATE_MAIN_SIZE
);
1287 OUT_CB_REG(R300_VAP_CNTL_STATUS
, vap_control_status
);
1288 OUT_CB_REG(R300_VAP_CLIP_CNTL
, vap_clip_cntl
);
1289 OUT_CB_REG(R300_GA_POINT_SIZE
, point_size
);
1290 OUT_CB_REG_SEQ(R300_GA_POINT_MINMAX
, 2);
1291 OUT_CB(point_minmax
);
1292 OUT_CB(line_control
);
1293 OUT_CB_REG_SEQ(R300_SU_POLY_OFFSET_ENABLE
, 2);
1294 OUT_CB(polygon_offset_enable
);
1295 rs
->cull_mode_index
= 11;
1297 OUT_CB_REG(R300_GA_LINE_STIPPLE_CONFIG
, line_stipple_config
);
1298 OUT_CB_REG(R300_GA_LINE_STIPPLE_VALUE
, line_stipple_value
);
1299 OUT_CB_REG(R300_GA_POLY_MODE
, polygon_mode
);
1300 OUT_CB_REG(R300_GA_ROUND_MODE
, round_mode
);
1301 OUT_CB_REG(R300_SC_CLIP_RULE
, clip_rule
);
1302 OUT_CB_REG_SEQ(R300_GA_POINT_S0
, 4);
1303 OUT_CB_32F(point_texcoord_left
);
1304 OUT_CB_32F(point_texcoord_bottom
);
1305 OUT_CB_32F(point_texcoord_right
);
1306 OUT_CB_32F(point_texcoord_top
);
1309 /* Build the two command buffers for polygon offset setup. */
1310 if (polygon_offset_enable
) {
1311 float scale
= state
->offset_scale
* 12;
1312 float offset
= state
->offset_units
* 4;
1314 BEGIN_CB(rs
->cb_poly_offset_zb16
, 5);
1315 OUT_CB_REG_SEQ(R300_SU_POLY_OFFSET_FRONT_SCALE
, 4);
1322 offset
= state
->offset_units
* 2;
1324 BEGIN_CB(rs
->cb_poly_offset_zb24
, 5);
1325 OUT_CB_REG_SEQ(R300_SU_POLY_OFFSET_FRONT_SCALE
, 4);
1336 /* Bind rasterizer state. */
1337 static void r300_bind_rs_state(struct pipe_context
* pipe
, void* state
)
1339 struct r300_context
* r300
= r300_context(pipe
);
1340 struct r300_rs_state
* rs
= (struct r300_rs_state
*)state
;
1341 int last_sprite_coord_enable
= r300
->sprite_coord_enable
;
1342 boolean last_two_sided_color
= r300
->two_sided_color
;
1343 boolean last_msaa_enable
= r300
->msaa_enable
;
1344 boolean last_flatshade
= r300
->flatshade
;
1345 boolean last_clip_halfz
= r300
->clip_halfz
;
1347 if (r300
->draw
&& rs
) {
1348 draw_set_rasterizer_state(r300
->draw
, &rs
->rs_draw
, state
);
1352 r300
->polygon_offset_enabled
= rs
->polygon_offset_enable
;
1353 r300
->sprite_coord_enable
= rs
->rs
.sprite_coord_enable
;
1354 r300
->two_sided_color
= rs
->rs
.light_twoside
;
1355 r300
->msaa_enable
= rs
->rs
.multisample
;
1356 r300
->flatshade
= rs
->rs
.flatshade
;
1357 r300
->clip_halfz
= rs
->rs
.clip_halfz
;
1359 r300
->polygon_offset_enabled
= FALSE
;
1360 r300
->sprite_coord_enable
= 0;
1361 r300
->two_sided_color
= FALSE
;
1362 r300
->msaa_enable
= FALSE
;
1363 r300
->flatshade
= FALSE
;
1364 r300
->clip_halfz
= FALSE
;
1367 UPDATE_STATE(state
, r300
->rs_state
);
1368 r300
->rs_state
.size
= RS_STATE_MAIN_SIZE
+ (r300
->polygon_offset_enabled
? 5 : 0);
1370 if (last_sprite_coord_enable
!= r300
->sprite_coord_enable
||
1371 last_two_sided_color
!= r300
->two_sided_color
||
1372 last_flatshade
!= r300
->flatshade
) {
1373 r300_mark_atom_dirty(r300
, &r300
->rs_block_state
);
1376 if (last_msaa_enable
!= r300
->msaa_enable
) {
1377 if (r300
->alpha_to_coverage
) {
1378 r300_mark_atom_dirty(r300
, &r300
->dsa_state
);
1381 if (r300
->alpha_to_one
&&
1382 r300
->fs_status
== FRAGMENT_SHADER_VALID
) {
1383 r300
->fs_status
= FRAGMENT_SHADER_MAYBE_DIRTY
;
1387 if (r300
->screen
->caps
.has_tcl
&& last_clip_halfz
!= r300
->clip_halfz
) {
1388 r300_mark_atom_dirty(r300
, &r300
->vs_state
);
1392 /* Free rasterizer state. */
1393 static void r300_delete_rs_state(struct pipe_context
* pipe
, void* state
)
1399 r300_create_sampler_state(struct pipe_context
* pipe
,
1400 const struct pipe_sampler_state
* state
)
1402 struct r300_context
* r300
= r300_context(pipe
);
1403 struct r300_sampler_state
* sampler
= CALLOC_STRUCT(r300_sampler_state
);
1404 boolean is_r500
= r300
->screen
->caps
.is_r500
;
1407 sampler
->state
= *state
;
1409 /* r300 doesn't handle CLAMP and MIRROR_CLAMP correctly when either MAG
1410 * or MIN filter is NEAREST. Since texwrap produces same results
1411 * for CLAMP and CLAMP_TO_EDGE, we use them instead. */
1412 if (sampler
->state
.min_img_filter
== PIPE_TEX_FILTER_NEAREST
||
1413 sampler
->state
.mag_img_filter
== PIPE_TEX_FILTER_NEAREST
) {
1415 if (sampler
->state
.wrap_s
== PIPE_TEX_WRAP_CLAMP
)
1416 sampler
->state
.wrap_s
= PIPE_TEX_WRAP_CLAMP_TO_EDGE
;
1417 else if (sampler
->state
.wrap_s
== PIPE_TEX_WRAP_MIRROR_CLAMP
)
1418 sampler
->state
.wrap_s
= PIPE_TEX_WRAP_MIRROR_CLAMP_TO_EDGE
;
1421 if (sampler
->state
.wrap_t
== PIPE_TEX_WRAP_CLAMP
)
1422 sampler
->state
.wrap_t
= PIPE_TEX_WRAP_CLAMP_TO_EDGE
;
1423 else if (sampler
->state
.wrap_t
== PIPE_TEX_WRAP_MIRROR_CLAMP
)
1424 sampler
->state
.wrap_t
= PIPE_TEX_WRAP_MIRROR_CLAMP_TO_EDGE
;
1427 if (sampler
->state
.wrap_r
== PIPE_TEX_WRAP_CLAMP
)
1428 sampler
->state
.wrap_r
= PIPE_TEX_WRAP_CLAMP_TO_EDGE
;
1429 else if (sampler
->state
.wrap_r
== PIPE_TEX_WRAP_MIRROR_CLAMP
)
1430 sampler
->state
.wrap_r
= PIPE_TEX_WRAP_MIRROR_CLAMP_TO_EDGE
;
1434 (r300_translate_wrap(sampler
->state
.wrap_s
) << R300_TX_WRAP_S_SHIFT
) |
1435 (r300_translate_wrap(sampler
->state
.wrap_t
) << R300_TX_WRAP_T_SHIFT
) |
1436 (r300_translate_wrap(sampler
->state
.wrap_r
) << R300_TX_WRAP_R_SHIFT
);
1438 sampler
->filter0
|= r300_translate_tex_filters(state
->min_img_filter
,
1439 state
->mag_img_filter
,
1440 state
->min_mip_filter
,
1441 state
->max_anisotropy
> 1);
1443 sampler
->filter0
|= r300_anisotropy(state
->max_anisotropy
);
1445 /* Unfortunately, r300-r500 don't support floating-point mipmap lods. */
1446 /* We must pass these to the merge function to clamp them properly. */
1447 sampler
->min_lod
= (unsigned)MAX2(state
->min_lod
, 0);
1448 sampler
->max_lod
= (unsigned)MAX2(ceilf(state
->max_lod
), 0);
1450 lod_bias
= CLAMP((int)(state
->lod_bias
* 32 + 1), -(1 << 9), (1 << 9) - 1);
1452 sampler
->filter1
|= (lod_bias
<< R300_LOD_BIAS_SHIFT
) & R300_LOD_BIAS_MASK
;
1454 /* This is very high quality anisotropic filtering for R5xx.
1455 * It's good for benchmarking the performance of texturing but
1456 * in practice we don't want to slow down the driver because it's
1457 * a pretty good performance killer. Feel free to play with it. */
1458 if (DBG_ON(r300
, DBG_ANISOHQ
) && is_r500
) {
1459 sampler
->filter1
|= r500_anisotropy(state
->max_anisotropy
);
1462 /* R500-specific fixups and optimizations */
1463 if (r300
->screen
->caps
.is_r500
) {
1464 sampler
->filter1
|= R500_BORDER_FIX
;
1467 return (void*)sampler
;
1470 static void r300_bind_sampler_states(struct pipe_context
* pipe
,
1471 enum pipe_shader_type shader
,
1472 unsigned start
, unsigned count
,
1475 struct r300_context
* r300
= r300_context(pipe
);
1476 struct r300_textures_state
* state
=
1477 (struct r300_textures_state
*)r300
->textures_state
.state
;
1478 unsigned tex_units
= r300
->screen
->caps
.num_tex_units
;
1482 if (shader
!= PIPE_SHADER_FRAGMENT
)
1485 if (count
> tex_units
)
1488 memcpy(state
->sampler_states
, states
, sizeof(void*) * count
);
1489 state
->sampler_state_count
= count
;
1491 r300_mark_atom_dirty(r300
, &r300
->textures_state
);
1494 static void r300_delete_sampler_state(struct pipe_context
* pipe
, void* state
)
1499 static uint32_t r300_assign_texture_cache_region(unsigned index
, unsigned num
)
1501 /* This looks like a hack, but I believe it's suppose to work like
1502 * that. To illustrate how this works, let's assume you have 5 textures.
1503 * From docs, 5 and the successive numbers are:
1511 * First 3 textures will get 3/4 of size of the cache, divived evenly
1512 * between them. The last 1/4 of the cache must be divided between
1513 * the last 2 textures, each will therefore get 1/8 of the cache.
1514 * Why not just to use "5 + texture_index" ?
1516 * This simple trick works for all "num" <= 16.
1519 return R300_TX_CACHE(R300_TX_CACHE_WHOLE
);
1521 return R300_TX_CACHE(num
+ index
);
1524 static void r300_set_sampler_views(struct pipe_context
* pipe
,
1525 enum pipe_shader_type shader
,
1526 unsigned start
, unsigned count
,
1527 struct pipe_sampler_view
** views
)
1529 struct r300_context
* r300
= r300_context(pipe
);
1530 struct r300_textures_state
* state
=
1531 (struct r300_textures_state
*)r300
->textures_state
.state
;
1532 struct r300_resource
*texture
;
1533 unsigned i
, real_num_views
= 0, view_index
= 0;
1534 unsigned tex_units
= r300
->screen
->caps
.num_tex_units
;
1535 boolean dirty_tex
= FALSE
;
1537 if (shader
!= PIPE_SHADER_FRAGMENT
)
1540 assert(start
== 0); /* non-zero not handled yet */
1542 if (count
> tex_units
) {
1546 /* Calculate the real number of views. */
1547 for (i
= 0; i
< count
; i
++) {
1552 for (i
= 0; i
< count
; i
++) {
1553 pipe_sampler_view_reference(
1554 (struct pipe_sampler_view
**)&state
->sampler_views
[i
],
1561 /* A new sampler view (= texture)... */
1564 /* Set the texrect factor in the fragment shader.
1565 * Needed for RECT and NPOT fallback. */
1566 texture
= r300_resource(views
[i
]->texture
);
1567 if (texture
->tex
.is_npot
) {
1568 r300_mark_atom_dirty(r300
, &r300
->fs_rc_constant_state
);
1571 state
->sampler_views
[i
]->texcache_region
=
1572 r300_assign_texture_cache_region(view_index
, real_num_views
);
1576 for (i
= count
; i
< tex_units
; i
++) {
1577 if (state
->sampler_views
[i
]) {
1578 pipe_sampler_view_reference(
1579 (struct pipe_sampler_view
**)&state
->sampler_views
[i
],
1584 state
->sampler_view_count
= count
;
1586 r300_mark_atom_dirty(r300
, &r300
->textures_state
);
1589 r300_mark_atom_dirty(r300
, &r300
->texture_cache_inval
);
1593 struct pipe_sampler_view
*
1594 r300_create_sampler_view_custom(struct pipe_context
*pipe
,
1595 struct pipe_resource
*texture
,
1596 const struct pipe_sampler_view
*templ
,
1597 unsigned width0_override
,
1598 unsigned height0_override
)
1600 struct r300_sampler_view
*view
= CALLOC_STRUCT(r300_sampler_view
);
1601 struct r300_resource
*tex
= r300_resource(texture
);
1602 boolean is_r500
= r300_screen(pipe
->screen
)->caps
.is_r500
;
1603 boolean dxtc_swizzle
= r300_screen(pipe
->screen
)->caps
.dxtc_swizzle
;
1608 view
->base
= *templ
;
1609 view
->base
.reference
.count
= 1;
1610 view
->base
.context
= pipe
;
1611 view
->base
.texture
= NULL
;
1612 pipe_resource_reference(&view
->base
.texture
, texture
);
1614 view
->width0_override
= width0_override
;
1615 view
->height0_override
= height0_override
;
1616 view
->swizzle
[0] = templ
->swizzle_r
;
1617 view
->swizzle
[1] = templ
->swizzle_g
;
1618 view
->swizzle
[2] = templ
->swizzle_b
;
1619 view
->swizzle
[3] = templ
->swizzle_a
;
1621 hwformat
= r300_translate_texformat(templ
->format
,
1626 if (hwformat
== ~0) {
1627 fprintf(stderr
, "r300: Ooops. Got unsupported format %s in %s.\n",
1628 util_format_short_name(templ
->format
), __func__
);
1630 assert(hwformat
!= ~0);
1632 r300_texture_setup_format_state(r300_screen(pipe
->screen
), tex
,
1634 width0_override
, height0_override
,
1636 view
->format
.format1
|= hwformat
;
1638 view
->format
.format2
|= r500_tx_format_msb_bit(templ
->format
);
1642 return (struct pipe_sampler_view
*)view
;
1645 static struct pipe_sampler_view
*
1646 r300_create_sampler_view(struct pipe_context
*pipe
,
1647 struct pipe_resource
*texture
,
1648 const struct pipe_sampler_view
*templ
)
1650 return r300_create_sampler_view_custom(pipe
, texture
, templ
,
1651 r300_resource(texture
)->tex
.width0
,
1652 r300_resource(texture
)->tex
.height0
);
1657 r300_sampler_view_destroy(struct pipe_context
*pipe
,
1658 struct pipe_sampler_view
*view
)
1660 pipe_resource_reference(&view
->texture
, NULL
);
1664 static void r300_set_sample_mask(struct pipe_context
*pipe
,
1667 struct r300_context
* r300
= r300_context(pipe
);
1669 *((unsigned*)r300
->sample_mask
.state
) = mask
;
1671 r300_mark_atom_dirty(r300
, &r300
->sample_mask
);
1674 static void r300_set_scissor_states(struct pipe_context
* pipe
,
1675 unsigned start_slot
,
1676 unsigned num_scissors
,
1677 const struct pipe_scissor_state
* state
)
1679 struct r300_context
* r300
= r300_context(pipe
);
1681 memcpy(r300
->scissor_state
.state
, state
,
1682 sizeof(struct pipe_scissor_state
));
1684 r300_mark_atom_dirty(r300
, &r300
->scissor_state
);
1687 static void r300_set_viewport_states(struct pipe_context
* pipe
,
1688 unsigned start_slot
,
1689 unsigned num_viewports
,
1690 const struct pipe_viewport_state
* state
)
1692 struct r300_context
* r300
= r300_context(pipe
);
1693 struct r300_viewport_state
* viewport
=
1694 (struct r300_viewport_state
*)r300
->viewport_state
.state
;
1696 r300
->viewport
= *state
;
1699 draw_set_viewport_states(r300
->draw
, start_slot
, num_viewports
, state
);
1700 viewport
->vte_control
= R300_VTX_XY_FMT
| R300_VTX_Z_FMT
;
1704 /* Do the transform in HW. */
1705 viewport
->vte_control
= R300_VTX_W0_FMT
;
1707 if (state
->scale
[0] != 1.0f
) {
1708 viewport
->xscale
= state
->scale
[0];
1709 viewport
->vte_control
|= R300_VPORT_X_SCALE_ENA
;
1711 if (state
->scale
[1] != 1.0f
) {
1712 viewport
->yscale
= state
->scale
[1];
1713 viewport
->vte_control
|= R300_VPORT_Y_SCALE_ENA
;
1715 if (state
->scale
[2] != 1.0f
) {
1716 viewport
->zscale
= state
->scale
[2];
1717 viewport
->vte_control
|= R300_VPORT_Z_SCALE_ENA
;
1719 if (state
->translate
[0] != 0.0f
) {
1720 viewport
->xoffset
= state
->translate
[0];
1721 viewport
->vte_control
|= R300_VPORT_X_OFFSET_ENA
;
1723 if (state
->translate
[1] != 0.0f
) {
1724 viewport
->yoffset
= state
->translate
[1];
1725 viewport
->vte_control
|= R300_VPORT_Y_OFFSET_ENA
;
1727 if (state
->translate
[2] != 0.0f
) {
1728 viewport
->zoffset
= state
->translate
[2];
1729 viewport
->vte_control
|= R300_VPORT_Z_OFFSET_ENA
;
1732 r300_mark_atom_dirty(r300
, &r300
->viewport_state
);
1733 if (r300
->fs
.state
&& r300_fs(r300
)->shader
&&
1734 r300_fs(r300
)->shader
->inputs
.wpos
!= ATTR_UNUSED
) {
1735 r300_mark_atom_dirty(r300
, &r300
->fs_rc_constant_state
);
1739 static void r300_set_vertex_buffers_hwtcl(struct pipe_context
* pipe
,
1740 unsigned start_slot
, unsigned count
,
1741 const struct pipe_vertex_buffer
* buffers
)
1743 struct r300_context
* r300
= r300_context(pipe
);
1745 util_set_vertex_buffers_count(r300
->vertex_buffer
,
1746 &r300
->nr_vertex_buffers
,
1747 buffers
, start_slot
, count
);
1749 /* There must be at least one vertex buffer set, otherwise it locks up. */
1750 if (!r300
->nr_vertex_buffers
) {
1751 util_set_vertex_buffers_count(r300
->vertex_buffer
,
1752 &r300
->nr_vertex_buffers
,
1753 &r300
->dummy_vb
, 0, 1);
1756 r300
->vertex_arrays_dirty
= TRUE
;
1759 static void r300_set_vertex_buffers_swtcl(struct pipe_context
* pipe
,
1760 unsigned start_slot
, unsigned count
,
1761 const struct pipe_vertex_buffer
* buffers
)
1763 struct r300_context
* r300
= r300_context(pipe
);
1766 util_set_vertex_buffers_count(r300
->vertex_buffer
,
1767 &r300
->nr_vertex_buffers
,
1768 buffers
, start_slot
, count
);
1769 draw_set_vertex_buffers(r300
->draw
, start_slot
, count
, buffers
);
1774 for (i
= 0; i
< count
; i
++) {
1775 if (buffers
[i
].is_user_buffer
) {
1776 draw_set_mapped_vertex_buffer(r300
->draw
, start_slot
+ i
,
1777 buffers
[i
].buffer
.user
, ~0);
1778 } else if (buffers
[i
].buffer
.resource
) {
1779 draw_set_mapped_vertex_buffer(r300
->draw
, start_slot
+ i
,
1780 r300_resource(buffers
[i
].buffer
.resource
)->malloced_buffer
, ~0);
1785 /* Initialize the PSC tables. */
1786 static void r300_vertex_psc(struct r300_vertex_element_state
*velems
)
1788 struct r300_vertex_stream_state
*vstream
= &velems
->vertex_stream
;
1789 uint16_t type
, swizzle
;
1790 enum pipe_format format
;
1793 /* Vertex shaders have no semantics on their inputs,
1794 * so PSC should just route stuff based on the vertex elements,
1795 * and not on attrib information. */
1796 for (i
= 0; i
< velems
->count
; i
++) {
1797 format
= velems
->velem
[i
].src_format
;
1799 type
= r300_translate_vertex_data_type(format
);
1800 if (type
== R300_INVALID_FORMAT
) {
1801 fprintf(stderr
, "r300: Bad vertex format %s.\n",
1802 util_format_short_name(format
));
1807 type
|= i
<< R300_DST_VEC_LOC_SHIFT
;
1808 swizzle
= r300_translate_vertex_data_swizzle(format
);
1811 vstream
->vap_prog_stream_cntl
[i
>> 1] |= type
<< 16;
1812 vstream
->vap_prog_stream_cntl_ext
[i
>> 1] |= swizzle
<< 16;
1814 vstream
->vap_prog_stream_cntl
[i
>> 1] |= type
;
1815 vstream
->vap_prog_stream_cntl_ext
[i
>> 1] |= swizzle
;
1819 /* Set the last vector in the PSC. */
1823 vstream
->vap_prog_stream_cntl
[i
>> 1] |=
1824 (R300_LAST_VEC
<< (i
& 1 ? 16 : 0));
1826 vstream
->count
= (i
>> 1) + 1;
1829 static void* r300_create_vertex_elements_state(struct pipe_context
* pipe
,
1831 const struct pipe_vertex_element
* attribs
)
1833 struct r300_vertex_element_state
*velems
;
1835 struct pipe_vertex_element dummy_attrib
= {0};
1837 /* R300 Programmable Stream Control (PSC) doesn't support 0 vertex elements. */
1839 dummy_attrib
.src_format
= PIPE_FORMAT_R8G8B8A8_UNORM
;
1840 attribs
= &dummy_attrib
;
1842 } else if (count
> 16) {
1843 fprintf(stderr
, "r300: More than 16 vertex elements are not supported,"
1844 " requested %i, using 16.\n", count
);
1848 velems
= CALLOC_STRUCT(r300_vertex_element_state
);
1852 velems
->count
= count
;
1853 memcpy(velems
->velem
, attribs
, sizeof(struct pipe_vertex_element
) * count
);
1855 if (r300_screen(pipe
->screen
)->caps
.has_tcl
) {
1857 * The unused components will be replaced by (..., 0, 1). */
1858 r300_vertex_psc(velems
);
1860 for (i
= 0; i
< count
; i
++) {
1861 velems
->format_size
[i
] =
1862 align(util_format_get_blocksize(velems
->velem
[i
].src_format
), 4);
1863 velems
->vertex_size_dwords
+= velems
->format_size
[i
] / 4;
1870 static void r300_bind_vertex_elements_state(struct pipe_context
*pipe
,
1873 struct r300_context
*r300
= r300_context(pipe
);
1874 struct r300_vertex_element_state
*velems
= state
;
1880 r300
->velems
= velems
;
1883 draw_set_vertex_elements(r300
->draw
, velems
->count
, velems
->velem
);
1887 UPDATE_STATE(&velems
->vertex_stream
, r300
->vertex_stream_state
);
1888 r300
->vertex_stream_state
.size
= (1 + velems
->vertex_stream
.count
) * 2;
1889 r300
->vertex_arrays_dirty
= TRUE
;
1892 static void r300_delete_vertex_elements_state(struct pipe_context
*pipe
, void *state
)
1897 static void* r300_create_vs_state(struct pipe_context
* pipe
,
1898 const struct pipe_shader_state
* shader
)
1900 struct r300_context
* r300
= r300_context(pipe
);
1901 struct r300_vertex_shader
* vs
= CALLOC_STRUCT(r300_vertex_shader
);
1903 /* Copy state directly into shader. */
1904 vs
->state
= *shader
;
1905 vs
->state
.tokens
= tgsi_dup_tokens(shader
->tokens
);
1907 if (r300
->screen
->caps
.has_tcl
) {
1908 r300_init_vs_outputs(r300
, vs
);
1909 r300_translate_vertex_shader(r300
, vs
);
1911 r300_draw_init_vertex_shader(r300
, vs
);
1917 static void r300_bind_vs_state(struct pipe_context
* pipe
, void* shader
)
1919 struct r300_context
* r300
= r300_context(pipe
);
1920 struct r300_vertex_shader
* vs
= (struct r300_vertex_shader
*)shader
;
1923 r300
->vs_state
.state
= NULL
;
1926 if (vs
== r300
->vs_state
.state
) {
1929 r300
->vs_state
.state
= vs
;
1931 /* The majority of the RS block bits is dependent on the vertex shader. */
1932 r300_mark_atom_dirty(r300
, &r300
->rs_block_state
); /* Will be updated before the emission. */
1934 if (r300
->screen
->caps
.has_tcl
) {
1935 unsigned fc_op_dwords
= r300
->screen
->caps
.is_r500
? 3 : 2;
1936 r300_mark_atom_dirty(r300
, &r300
->vs_state
);
1937 r300
->vs_state
.size
= vs
->code
.length
+ 9 +
1938 (R300_VS_MAX_FC_OPS
* fc_op_dwords
+ 4);
1940 r300_mark_atom_dirty(r300
, &r300
->vs_constants
);
1941 r300
->vs_constants
.size
=
1943 (vs
->externals_count
? vs
->externals_count
* 4 + 3 : 0) +
1944 (vs
->immediates_count
? vs
->immediates_count
* 4 + 3 : 0);
1946 ((struct r300_constant_buffer
*)r300
->vs_constants
.state
)->remap_table
=
1947 vs
->code
.constants_remap_table
;
1949 r300_mark_atom_dirty(r300
, &r300
->pvs_flush
);
1951 draw_bind_vertex_shader(r300
->draw
,
1952 (struct draw_vertex_shader
*)vs
->draw_vs
);
1956 static void r300_delete_vs_state(struct pipe_context
* pipe
, void* shader
)
1958 struct r300_context
* r300
= r300_context(pipe
);
1959 struct r300_vertex_shader
* vs
= (struct r300_vertex_shader
*)shader
;
1961 if (r300
->screen
->caps
.has_tcl
) {
1962 rc_constants_destroy(&vs
->code
.constants
);
1963 FREE(vs
->code
.constants_remap_table
);
1965 draw_delete_vertex_shader(r300
->draw
,
1966 (struct draw_vertex_shader
*)vs
->draw_vs
);
1969 FREE((void*)vs
->state
.tokens
);
1973 static void r300_set_constant_buffer(struct pipe_context
*pipe
,
1974 enum pipe_shader_type shader
, uint index
,
1975 const struct pipe_constant_buffer
*cb
)
1977 struct r300_context
* r300
= r300_context(pipe
);
1978 struct r300_constant_buffer
*cbuf
;
1981 if (!cb
|| (!cb
->buffer
&& !cb
->user_buffer
))
1985 case PIPE_SHADER_VERTEX
:
1986 cbuf
= (struct r300_constant_buffer
*)r300
->vs_constants
.state
;
1988 case PIPE_SHADER_FRAGMENT
:
1989 cbuf
= (struct r300_constant_buffer
*)r300
->fs_constants
.state
;
1996 if (cb
->user_buffer
)
1997 mapped
= (uint32_t*)cb
->user_buffer
;
1999 struct r300_resource
*rbuf
= r300_resource(cb
->buffer
);
2001 if (rbuf
&& rbuf
->malloced_buffer
)
2002 mapped
= (uint32_t*)rbuf
->malloced_buffer
;
2007 if (shader
== PIPE_SHADER_FRAGMENT
||
2008 (shader
== PIPE_SHADER_VERTEX
&& r300
->screen
->caps
.has_tcl
)) {
2012 if (shader
== PIPE_SHADER_VERTEX
) {
2013 if (r300
->screen
->caps
.has_tcl
) {
2014 struct r300_vertex_shader
*vs
=
2015 (struct r300_vertex_shader
*)r300
->vs_state
.state
;
2018 cbuf
->buffer_base
= 0;
2022 cbuf
->buffer_base
= r300
->vs_const_base
;
2023 r300
->vs_const_base
+= vs
->code
.constants
.Count
;
2024 if (r300
->vs_const_base
> R500_MAX_PVS_CONST_VECS
) {
2025 r300
->vs_const_base
= vs
->code
.constants
.Count
;
2026 cbuf
->buffer_base
= 0;
2027 r300_mark_atom_dirty(r300
, &r300
->pvs_flush
);
2029 r300_mark_atom_dirty(r300
, &r300
->vs_constants
);
2030 } else if (r300
->draw
) {
2031 draw_set_mapped_constant_buffer(r300
->draw
, PIPE_SHADER_VERTEX
,
2032 0, mapped
, cb
->buffer_size
);
2034 } else if (shader
== PIPE_SHADER_FRAGMENT
) {
2035 r300_mark_atom_dirty(r300
, &r300
->fs_constants
);
2039 static void r300_texture_barrier(struct pipe_context
*pipe
, unsigned flags
)
2041 struct r300_context
*r300
= r300_context(pipe
);
2043 r300_mark_atom_dirty(r300
, &r300
->gpu_flush
);
2044 r300_mark_atom_dirty(r300
, &r300
->texture_cache_inval
);
2047 static void r300_memory_barrier(struct pipe_context
*pipe
, unsigned flags
)
2051 void r300_init_state_functions(struct r300_context
* r300
)
2053 r300
->context
.create_blend_state
= r300_create_blend_state
;
2054 r300
->context
.bind_blend_state
= r300_bind_blend_state
;
2055 r300
->context
.delete_blend_state
= r300_delete_blend_state
;
2057 r300
->context
.set_blend_color
= r300_set_blend_color
;
2059 r300
->context
.set_clip_state
= r300_set_clip_state
;
2060 r300
->context
.set_sample_mask
= r300_set_sample_mask
;
2062 r300
->context
.set_constant_buffer
= r300_set_constant_buffer
;
2064 r300
->context
.create_depth_stencil_alpha_state
= r300_create_dsa_state
;
2065 r300
->context
.bind_depth_stencil_alpha_state
= r300_bind_dsa_state
;
2066 r300
->context
.delete_depth_stencil_alpha_state
= r300_delete_dsa_state
;
2068 r300
->context
.set_stencil_ref
= r300_set_stencil_ref
;
2070 r300
->context
.set_framebuffer_state
= r300_set_framebuffer_state
;
2072 r300
->context
.create_fs_state
= r300_create_fs_state
;
2073 r300
->context
.bind_fs_state
= r300_bind_fs_state
;
2074 r300
->context
.delete_fs_state
= r300_delete_fs_state
;
2076 r300
->context
.set_polygon_stipple
= r300_set_polygon_stipple
;
2078 r300
->context
.create_rasterizer_state
= r300_create_rs_state
;
2079 r300
->context
.bind_rasterizer_state
= r300_bind_rs_state
;
2080 r300
->context
.delete_rasterizer_state
= r300_delete_rs_state
;
2082 r300
->context
.create_sampler_state
= r300_create_sampler_state
;
2083 r300
->context
.bind_sampler_states
= r300_bind_sampler_states
;
2084 r300
->context
.delete_sampler_state
= r300_delete_sampler_state
;
2086 r300
->context
.set_sampler_views
= r300_set_sampler_views
;
2087 r300
->context
.create_sampler_view
= r300_create_sampler_view
;
2088 r300
->context
.sampler_view_destroy
= r300_sampler_view_destroy
;
2090 r300
->context
.set_scissor_states
= r300_set_scissor_states
;
2092 r300
->context
.set_viewport_states
= r300_set_viewport_states
;
2094 if (r300
->screen
->caps
.has_tcl
) {
2095 r300
->context
.set_vertex_buffers
= r300_set_vertex_buffers_hwtcl
;
2097 r300
->context
.set_vertex_buffers
= r300_set_vertex_buffers_swtcl
;
2100 r300
->context
.create_vertex_elements_state
= r300_create_vertex_elements_state
;
2101 r300
->context
.bind_vertex_elements_state
= r300_bind_vertex_elements_state
;
2102 r300
->context
.delete_vertex_elements_state
= r300_delete_vertex_elements_state
;
2104 r300
->context
.create_vs_state
= r300_create_vs_state
;
2105 r300
->context
.bind_vs_state
= r300_bind_vs_state
;
2106 r300
->context
.delete_vs_state
= r300_delete_vs_state
;
2108 r300
->context
.texture_barrier
= r300_texture_barrier
;
2109 r300
->context
.memory_barrier
= r300_memory_barrier
;