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"
35 #include "r300_context.h"
36 #include "r300_emit.h"
38 #include "r300_screen.h"
39 #include "r300_screen_buffer.h"
40 #include "r300_state_inlines.h"
42 #include "r300_texture.h"
44 #include "r300_winsys.h"
46 /* r300_state: Functions used to intialize state context by translating
47 * Gallium state objects into semi-native r300 state objects. */
49 #define UPDATE_STATE(cso, atom) \
50 if (cso != atom.state) { \
55 static boolean
blend_discard_if_src_alpha_0(unsigned srcRGB
, unsigned srcA
,
56 unsigned dstRGB
, unsigned dstA
)
58 /* If the blend equation is ADD or REVERSE_SUBTRACT,
59 * SRC_ALPHA == 0, and the following state is set, the colorbuffer
60 * will not be changed.
61 * Notice that the dst factors are the src factors inverted. */
62 return (srcRGB
== PIPE_BLENDFACTOR_SRC_ALPHA
||
63 srcRGB
== PIPE_BLENDFACTOR_SRC_ALPHA_SATURATE
||
64 srcRGB
== PIPE_BLENDFACTOR_ZERO
) &&
65 (srcA
== PIPE_BLENDFACTOR_SRC_COLOR
||
66 srcA
== PIPE_BLENDFACTOR_SRC_ALPHA
||
67 srcA
== PIPE_BLENDFACTOR_SRC_ALPHA_SATURATE
||
68 srcA
== PIPE_BLENDFACTOR_ZERO
) &&
69 (dstRGB
== PIPE_BLENDFACTOR_INV_SRC_ALPHA
||
70 dstRGB
== PIPE_BLENDFACTOR_ONE
) &&
71 (dstA
== PIPE_BLENDFACTOR_INV_SRC_COLOR
||
72 dstA
== PIPE_BLENDFACTOR_INV_SRC_ALPHA
||
73 dstA
== PIPE_BLENDFACTOR_ONE
);
76 static boolean
blend_discard_if_src_alpha_1(unsigned srcRGB
, unsigned srcA
,
77 unsigned dstRGB
, unsigned dstA
)
79 /* If the blend equation is ADD or REVERSE_SUBTRACT,
80 * SRC_ALPHA == 1, and the following state is set, the colorbuffer
81 * will not be changed.
82 * Notice that the dst factors are the src factors inverted. */
83 return (srcRGB
== PIPE_BLENDFACTOR_INV_SRC_ALPHA
||
84 srcRGB
== PIPE_BLENDFACTOR_ZERO
) &&
85 (srcA
== PIPE_BLENDFACTOR_INV_SRC_COLOR
||
86 srcA
== PIPE_BLENDFACTOR_INV_SRC_ALPHA
||
87 srcA
== PIPE_BLENDFACTOR_ZERO
) &&
88 (dstRGB
== PIPE_BLENDFACTOR_SRC_ALPHA
||
89 dstRGB
== PIPE_BLENDFACTOR_ONE
) &&
90 (dstA
== PIPE_BLENDFACTOR_SRC_COLOR
||
91 dstA
== PIPE_BLENDFACTOR_SRC_ALPHA
||
92 dstA
== PIPE_BLENDFACTOR_ONE
);
95 static boolean
blend_discard_if_src_color_0(unsigned srcRGB
, unsigned srcA
,
96 unsigned dstRGB
, unsigned dstA
)
98 /* If the blend equation is ADD or REVERSE_SUBTRACT,
99 * SRC_COLOR == (0,0,0), and the following state is set, the colorbuffer
100 * will not be changed.
101 * Notice that the dst factors are the src factors inverted. */
102 return (srcRGB
== PIPE_BLENDFACTOR_SRC_COLOR
||
103 srcRGB
== PIPE_BLENDFACTOR_ZERO
) &&
104 (srcA
== PIPE_BLENDFACTOR_ZERO
) &&
105 (dstRGB
== PIPE_BLENDFACTOR_INV_SRC_COLOR
||
106 dstRGB
== PIPE_BLENDFACTOR_ONE
) &&
107 (dstA
== PIPE_BLENDFACTOR_ONE
);
110 static boolean
blend_discard_if_src_color_1(unsigned srcRGB
, unsigned srcA
,
111 unsigned dstRGB
, unsigned dstA
)
113 /* If the blend equation is ADD or REVERSE_SUBTRACT,
114 * SRC_COLOR == (1,1,1), and the following state is set, the colorbuffer
115 * will not be changed.
116 * Notice that the dst factors are the src factors inverted. */
117 return (srcRGB
== PIPE_BLENDFACTOR_INV_SRC_COLOR
||
118 srcRGB
== PIPE_BLENDFACTOR_ZERO
) &&
119 (srcA
== PIPE_BLENDFACTOR_ZERO
) &&
120 (dstRGB
== PIPE_BLENDFACTOR_SRC_COLOR
||
121 dstRGB
== PIPE_BLENDFACTOR_ONE
) &&
122 (dstA
== PIPE_BLENDFACTOR_ONE
);
125 static boolean
blend_discard_if_src_alpha_color_0(unsigned srcRGB
, unsigned srcA
,
126 unsigned dstRGB
, unsigned dstA
)
128 /* If the blend equation is ADD or REVERSE_SUBTRACT,
129 * SRC_ALPHA_COLOR == (0,0,0,0), and the following state is set,
130 * the colorbuffer will not be changed.
131 * Notice that the dst factors are the src factors inverted. */
132 return (srcRGB
== PIPE_BLENDFACTOR_SRC_COLOR
||
133 srcRGB
== PIPE_BLENDFACTOR_SRC_ALPHA
||
134 srcRGB
== PIPE_BLENDFACTOR_SRC_ALPHA_SATURATE
||
135 srcRGB
== PIPE_BLENDFACTOR_ZERO
) &&
136 (srcA
== PIPE_BLENDFACTOR_SRC_COLOR
||
137 srcA
== PIPE_BLENDFACTOR_SRC_ALPHA
||
138 srcA
== PIPE_BLENDFACTOR_SRC_ALPHA_SATURATE
||
139 srcA
== PIPE_BLENDFACTOR_ZERO
) &&
140 (dstRGB
== PIPE_BLENDFACTOR_INV_SRC_COLOR
||
141 dstRGB
== PIPE_BLENDFACTOR_INV_SRC_ALPHA
||
142 dstRGB
== PIPE_BLENDFACTOR_ONE
) &&
143 (dstA
== PIPE_BLENDFACTOR_INV_SRC_COLOR
||
144 dstA
== PIPE_BLENDFACTOR_INV_SRC_ALPHA
||
145 dstA
== PIPE_BLENDFACTOR_ONE
);
148 static boolean
blend_discard_if_src_alpha_color_1(unsigned srcRGB
, unsigned srcA
,
149 unsigned dstRGB
, unsigned dstA
)
151 /* If the blend equation is ADD or REVERSE_SUBTRACT,
152 * SRC_ALPHA_COLOR == (1,1,1,1), and the following state is set,
153 * the colorbuffer will not be changed.
154 * Notice that the dst factors are the src factors inverted. */
155 return (srcRGB
== PIPE_BLENDFACTOR_INV_SRC_COLOR
||
156 srcRGB
== PIPE_BLENDFACTOR_INV_SRC_ALPHA
||
157 srcRGB
== PIPE_BLENDFACTOR_ZERO
) &&
158 (srcA
== PIPE_BLENDFACTOR_INV_SRC_COLOR
||
159 srcA
== PIPE_BLENDFACTOR_INV_SRC_ALPHA
||
160 srcA
== PIPE_BLENDFACTOR_ZERO
) &&
161 (dstRGB
== PIPE_BLENDFACTOR_SRC_COLOR
||
162 dstRGB
== PIPE_BLENDFACTOR_SRC_ALPHA
||
163 dstRGB
== PIPE_BLENDFACTOR_ONE
) &&
164 (dstA
== PIPE_BLENDFACTOR_SRC_COLOR
||
165 dstA
== PIPE_BLENDFACTOR_SRC_ALPHA
||
166 dstA
== PIPE_BLENDFACTOR_ONE
);
169 static unsigned bgra_cmask(unsigned mask
)
171 /* Gallium uses RGBA color ordering while R300 expects BGRA. */
173 return ((mask
& PIPE_MASK_R
) << 2) |
174 ((mask
& PIPE_MASK_B
) >> 2) |
175 (mask
& (PIPE_MASK_G
| PIPE_MASK_A
));
178 /* Create a new blend state based on the CSO blend state.
180 * This encompasses alpha blending, logic/raster ops, and blend dithering. */
181 static void* r300_create_blend_state(struct pipe_context
* pipe
,
182 const struct pipe_blend_state
* state
)
184 struct r300_screen
* r300screen
= r300_screen(pipe
->screen
);
185 struct r300_blend_state
* blend
= CALLOC_STRUCT(r300_blend_state
);
186 uint32_t blend_control
= 0; /* R300_RB3D_CBLEND: 0x4e04 */
187 uint32_t alpha_blend_control
= 0; /* R300_RB3D_ABLEND: 0x4e08 */
188 uint32_t color_channel_mask
= 0; /* R300_RB3D_COLOR_CHANNEL_MASK: 0x4e0c */
189 uint32_t rop
= 0; /* R300_RB3D_ROPCNTL: 0x4e18 */
190 uint32_t dither
= 0; /* R300_RB3D_DITHER_CTL: 0x4e50 */
193 if (state
->rt
[0].blend_enable
)
195 unsigned eqRGB
= state
->rt
[0].rgb_func
;
196 unsigned srcRGB
= state
->rt
[0].rgb_src_factor
;
197 unsigned dstRGB
= state
->rt
[0].rgb_dst_factor
;
199 unsigned eqA
= state
->rt
[0].alpha_func
;
200 unsigned srcA
= state
->rt
[0].alpha_src_factor
;
201 unsigned dstA
= state
->rt
[0].alpha_dst_factor
;
203 /* despite the name, ALPHA_BLEND_ENABLE has nothing to do with alpha,
204 * this is just the crappy D3D naming */
205 blend_control
= R300_ALPHA_BLEND_ENABLE
|
206 r300_translate_blend_function(eqRGB
) |
207 ( r300_translate_blend_factor(srcRGB
) << R300_SRC_BLEND_SHIFT
) |
208 ( r300_translate_blend_factor(dstRGB
) << R300_DST_BLEND_SHIFT
);
210 /* Optimization: some operations do not require the destination color.
212 * When SRC_ALPHA_SATURATE is used, colorbuffer reads must be enabled,
213 * otherwise blending gives incorrect results. It seems to be
215 if (eqRGB
== PIPE_BLEND_MIN
|| eqA
== PIPE_BLEND_MIN
||
216 eqRGB
== PIPE_BLEND_MAX
|| eqA
== PIPE_BLEND_MAX
||
217 dstRGB
!= PIPE_BLENDFACTOR_ZERO
||
218 dstA
!= PIPE_BLENDFACTOR_ZERO
||
219 srcRGB
== PIPE_BLENDFACTOR_DST_COLOR
||
220 srcRGB
== PIPE_BLENDFACTOR_DST_ALPHA
||
221 srcRGB
== PIPE_BLENDFACTOR_INV_DST_COLOR
||
222 srcRGB
== PIPE_BLENDFACTOR_INV_DST_ALPHA
||
223 srcA
== PIPE_BLENDFACTOR_DST_COLOR
||
224 srcA
== PIPE_BLENDFACTOR_DST_ALPHA
||
225 srcA
== PIPE_BLENDFACTOR_INV_DST_COLOR
||
226 srcA
== PIPE_BLENDFACTOR_INV_DST_ALPHA
||
227 srcRGB
== PIPE_BLENDFACTOR_SRC_ALPHA_SATURATE
) {
228 /* Enable reading from the colorbuffer. */
229 blend_control
|= R300_READ_ENABLE
;
231 if (r300screen
->caps
.is_r500
) {
232 /* Optimization: Depending on incoming pixels, we can
233 * conditionally disable the reading in hardware... */
234 if (eqRGB
!= PIPE_BLEND_MIN
&& eqA
!= PIPE_BLEND_MIN
&&
235 eqRGB
!= PIPE_BLEND_MAX
&& eqA
!= PIPE_BLEND_MAX
) {
236 /* Disable reading if SRC_ALPHA == 0. */
237 if ((dstRGB
== PIPE_BLENDFACTOR_SRC_ALPHA
||
238 dstRGB
== PIPE_BLENDFACTOR_ZERO
) &&
239 (dstA
== PIPE_BLENDFACTOR_SRC_COLOR
||
240 dstA
== PIPE_BLENDFACTOR_SRC_ALPHA
||
241 dstA
== PIPE_BLENDFACTOR_ZERO
)) {
242 blend_control
|= R500_SRC_ALPHA_0_NO_READ
;
245 /* Disable reading if SRC_ALPHA == 1. */
246 if ((dstRGB
== PIPE_BLENDFACTOR_INV_SRC_ALPHA
||
247 dstRGB
== PIPE_BLENDFACTOR_ZERO
) &&
248 (dstA
== PIPE_BLENDFACTOR_INV_SRC_COLOR
||
249 dstA
== PIPE_BLENDFACTOR_INV_SRC_ALPHA
||
250 dstA
== PIPE_BLENDFACTOR_ZERO
)) {
251 blend_control
|= R500_SRC_ALPHA_1_NO_READ
;
257 /* Optimization: discard pixels which don't change the colorbuffer.
259 * The code below is non-trivial and some math is involved.
261 * Discarding pixels must be disabled when FP16 AA is enabled.
262 * This is a hardware bug. Also, this implementation wouldn't work
263 * with FP blending enabled and equation clamping disabled.
265 * Equations other than ADD are rarely used and therefore won't be
267 if ((eqRGB
== PIPE_BLEND_ADD
|| eqRGB
== PIPE_BLEND_REVERSE_SUBTRACT
) &&
268 (eqA
== PIPE_BLEND_ADD
|| eqA
== PIPE_BLEND_REVERSE_SUBTRACT
)) {
270 * REVERSE_SUBTRACT: Y-X
273 * If X = src*srcFactor = 0 and Y = dst*dstFactor = 1,
274 * then CB will not be changed.
276 * Given the srcFactor and dstFactor variables, we can derive
277 * what src and dst should be equal to and discard appropriate
280 if (blend_discard_if_src_alpha_0(srcRGB
, srcA
, dstRGB
, dstA
)) {
281 blend_control
|= R300_DISCARD_SRC_PIXELS_SRC_ALPHA_0
;
282 } else if (blend_discard_if_src_alpha_1(srcRGB
, srcA
,
284 blend_control
|= R300_DISCARD_SRC_PIXELS_SRC_ALPHA_1
;
285 } else if (blend_discard_if_src_color_0(srcRGB
, srcA
,
287 blend_control
|= R300_DISCARD_SRC_PIXELS_SRC_COLOR_0
;
288 } else if (blend_discard_if_src_color_1(srcRGB
, srcA
,
290 blend_control
|= R300_DISCARD_SRC_PIXELS_SRC_COLOR_1
;
291 } else if (blend_discard_if_src_alpha_color_0(srcRGB
, srcA
,
294 R300_DISCARD_SRC_PIXELS_SRC_ALPHA_COLOR_0
;
295 } else if (blend_discard_if_src_alpha_color_1(srcRGB
, srcA
,
298 R300_DISCARD_SRC_PIXELS_SRC_ALPHA_COLOR_1
;
303 if (srcA
!= srcRGB
|| dstA
!= dstRGB
|| eqA
!= eqRGB
) {
304 blend_control
|= R300_SEPARATE_ALPHA_ENABLE
;
305 alpha_blend_control
=
306 r300_translate_blend_function(eqA
) |
307 (r300_translate_blend_factor(srcA
) << R300_SRC_BLEND_SHIFT
) |
308 (r300_translate_blend_factor(dstA
) << R300_DST_BLEND_SHIFT
);
312 /* PIPE_LOGICOP_* don't need to be translated, fortunately. */
313 if (state
->logicop_enable
) {
314 rop
= R300_RB3D_ROPCNTL_ROP_ENABLE
|
315 (state
->logicop_func
) << R300_RB3D_ROPCNTL_ROP_SHIFT
;
318 /* Color channel masks for all MRTs. */
319 color_channel_mask
= bgra_cmask(state
->rt
[0].colormask
);
320 if (r300screen
->caps
.is_r500
&& state
->independent_blend_enable
) {
321 if (state
->rt
[1].blend_enable
) {
322 color_channel_mask
|= bgra_cmask(state
->rt
[1].colormask
) << 4;
324 if (state
->rt
[2].blend_enable
) {
325 color_channel_mask
|= bgra_cmask(state
->rt
[2].colormask
) << 8;
327 if (state
->rt
[3].blend_enable
) {
328 color_channel_mask
|= bgra_cmask(state
->rt
[3].colormask
) << 12;
332 /* Neither fglrx nor classic r300 ever set this, regardless of dithering
333 * state. Since it's an optional implementation detail, we can leave it
334 * out and never dither.
336 * This could be revisited if we ever get quality or conformance hints.
339 dither = R300_RB3D_DITHER_CTL_DITHER_MODE_LUT |
340 R300_RB3D_DITHER_CTL_ALPHA_DITHER_MODE_LUT;
344 /* Build a command buffer. */
345 BEGIN_CB(blend
->cb
, 8);
346 OUT_CB_REG(R300_RB3D_ROPCNTL
, rop
);
347 OUT_CB_REG_SEQ(R300_RB3D_CBLEND
, 3);
348 OUT_CB(blend_control
);
349 OUT_CB(alpha_blend_control
);
350 OUT_CB(color_channel_mask
);
351 OUT_CB_REG(R300_RB3D_DITHER_CTL
, dither
);
354 /* The same as above, but with no colorbuffer reads and writes. */
355 BEGIN_CB(blend
->cb_no_readwrite
, 8);
356 OUT_CB_REG(R300_RB3D_ROPCNTL
, rop
);
357 OUT_CB_REG_SEQ(R300_RB3D_CBLEND
, 3);
361 OUT_CB_REG(R300_RB3D_DITHER_CTL
, dither
);
367 /* Bind blend state. */
368 static void r300_bind_blend_state(struct pipe_context
* pipe
,
371 struct r300_context
* r300
= r300_context(pipe
);
373 UPDATE_STATE(state
, r300
->blend_state
);
376 /* Free blend state. */
377 static void r300_delete_blend_state(struct pipe_context
* pipe
,
383 /* Convert float to 10bit integer */
384 static unsigned float_to_fixed10(float f
)
386 return CLAMP((unsigned)(f
* 1023.9f
), 0, 1023);
390 * Setup both R300 and R500 registers, figure out later which one to write. */
391 static void r300_set_blend_color(struct pipe_context
* pipe
,
392 const struct pipe_blend_color
* color
)
394 struct r300_context
* r300
= r300_context(pipe
);
395 struct r300_blend_color_state
* state
=
396 (struct r300_blend_color_state
*)r300
->blend_color_state
.state
;
399 if (r300
->screen
->caps
.is_r500
) {
400 /* XXX if FP16 blending is enabled, we should use the FP16 format */
401 BEGIN_CB(state
->cb
, 3);
402 OUT_CB_REG_SEQ(R500_RB3D_CONSTANT_COLOR_AR
, 2);
403 OUT_CB(float_to_fixed10(color
->color
[0]) |
404 (float_to_fixed10(color
->color
[3]) << 16));
405 OUT_CB(float_to_fixed10(color
->color
[2]) |
406 (float_to_fixed10(color
->color
[1]) << 16));
410 util_pack_color(color
->color
, PIPE_FORMAT_B8G8R8A8_UNORM
, &uc
);
412 BEGIN_CB(state
->cb
, 2);
413 OUT_CB_REG(R300_RB3D_BLEND_COLOR
, uc
.ui
);
417 r300
->blend_color_state
.dirty
= TRUE
;
420 static void r300_set_clip_state(struct pipe_context
* pipe
,
421 const struct pipe_clip_state
* state
)
423 struct r300_context
* r300
= r300_context(pipe
);
424 struct r300_clip_state
*clip
=
425 (struct r300_clip_state
*)r300
->clip_state
.state
;
430 if (r300
->screen
->caps
.has_tcl
) {
431 BEGIN_CB(clip
->cb
, 29);
432 OUT_CB_REG(R300_VAP_PVS_VECTOR_INDX_REG
,
433 (r300
->screen
->caps
.is_r500
?
434 R500_PVS_UCP_START
: R300_PVS_UCP_START
));
435 OUT_CB_ONE_REG(R300_VAP_PVS_UPLOAD_DATA
, 6 * 4);
436 OUT_CB_TABLE(state
->ucp
, 6 * 4);
437 OUT_CB_REG(R300_VAP_CLIP_CNTL
, ((1 << state
->nr
) - 1) |
438 R300_PS_UCP_MODE_CLIP_AS_TRIFAN
);
441 r300
->clip_state
.dirty
= TRUE
;
443 draw_flush(r300
->draw
);
444 draw_set_clip_state(r300
->draw
, state
);
449 r300_set_sample_mask(struct pipe_context
*pipe
,
450 unsigned sample_mask
)
455 /* Create a new depth, stencil, and alpha state based on the CSO dsa state.
457 * This contains the depth buffer, stencil buffer, alpha test, and such.
458 * On the Radeon, depth and stencil buffer setup are intertwined, which is
459 * the reason for some of the strange-looking assignments across registers. */
461 r300_create_dsa_state(struct pipe_context
* pipe
,
462 const struct pipe_depth_stencil_alpha_state
* state
)
464 struct r300_capabilities
*caps
= &r300_screen(pipe
->screen
)->caps
;
465 struct r300_dsa_state
* dsa
= CALLOC_STRUCT(r300_dsa_state
);
468 /* Depth test setup. */
469 if (state
->depth
.enabled
) {
470 dsa
->z_buffer_control
|= R300_Z_ENABLE
;
472 if (state
->depth
.writemask
) {
473 dsa
->z_buffer_control
|= R300_Z_WRITE_ENABLE
;
476 dsa
->z_stencil_control
|=
477 (r300_translate_depth_stencil_function(state
->depth
.func
) <<
481 /* Stencil buffer setup. */
482 if (state
->stencil
[0].enabled
) {
483 dsa
->z_buffer_control
|= R300_STENCIL_ENABLE
;
484 dsa
->z_stencil_control
|=
485 (r300_translate_depth_stencil_function(state
->stencil
[0].func
) <<
486 R300_S_FRONT_FUNC_SHIFT
) |
487 (r300_translate_stencil_op(state
->stencil
[0].fail_op
) <<
488 R300_S_FRONT_SFAIL_OP_SHIFT
) |
489 (r300_translate_stencil_op(state
->stencil
[0].zpass_op
) <<
490 R300_S_FRONT_ZPASS_OP_SHIFT
) |
491 (r300_translate_stencil_op(state
->stencil
[0].zfail_op
) <<
492 R300_S_FRONT_ZFAIL_OP_SHIFT
);
494 dsa
->stencil_ref_mask
=
495 (state
->stencil
[0].valuemask
<< R300_STENCILMASK_SHIFT
) |
496 (state
->stencil
[0].writemask
<< R300_STENCILWRITEMASK_SHIFT
);
498 if (state
->stencil
[1].enabled
) {
499 dsa
->two_sided
= TRUE
;
501 dsa
->z_buffer_control
|= R300_STENCIL_FRONT_BACK
;
502 dsa
->z_stencil_control
|=
503 (r300_translate_depth_stencil_function(state
->stencil
[1].func
) <<
504 R300_S_BACK_FUNC_SHIFT
) |
505 (r300_translate_stencil_op(state
->stencil
[1].fail_op
) <<
506 R300_S_BACK_SFAIL_OP_SHIFT
) |
507 (r300_translate_stencil_op(state
->stencil
[1].zpass_op
) <<
508 R300_S_BACK_ZPASS_OP_SHIFT
) |
509 (r300_translate_stencil_op(state
->stencil
[1].zfail_op
) <<
510 R300_S_BACK_ZFAIL_OP_SHIFT
);
512 dsa
->stencil_ref_bf
=
513 (state
->stencil
[1].valuemask
<< R300_STENCILMASK_SHIFT
) |
514 (state
->stencil
[1].writemask
<< R300_STENCILWRITEMASK_SHIFT
);
517 dsa
->z_buffer_control
|= R500_STENCIL_REFMASK_FRONT_BACK
;
519 dsa
->two_sided_stencil_ref
=
520 (state
->stencil
[0].valuemask
!= state
->stencil
[1].valuemask
||
521 state
->stencil
[0].writemask
!= state
->stencil
[1].writemask
);
526 /* Alpha test setup. */
527 if (state
->alpha
.enabled
) {
528 dsa
->alpha_function
=
529 r300_translate_alpha_function(state
->alpha
.func
) |
530 R300_FG_ALPHA_FUNC_ENABLE
;
532 /* We could use 10bit alpha ref but who needs that? */
533 dsa
->alpha_function
|= float_to_ubyte(state
->alpha
.ref_value
);
536 dsa
->alpha_function
|= R500_FG_ALPHA_FUNC_8BIT
;
539 BEGIN_CB(&dsa
->cb_begin
, 8);
540 OUT_CB_REG(R300_FG_ALPHA_FUNC
, dsa
->alpha_function
);
541 OUT_CB_REG_SEQ(R300_ZB_CNTL
, 3);
542 OUT_CB(dsa
->z_buffer_control
);
543 OUT_CB(dsa
->z_stencil_control
);
544 OUT_CB(dsa
->stencil_ref_mask
);
545 OUT_CB_REG(R500_ZB_STENCILREFMASK_BF
, dsa
->stencil_ref_bf
);
548 BEGIN_CB(dsa
->cb_no_readwrite
, 8);
549 OUT_CB_REG(R300_FG_ALPHA_FUNC
, dsa
->alpha_function
);
550 OUT_CB_REG_SEQ(R300_ZB_CNTL
, 3);
554 OUT_CB_REG(R500_ZB_STENCILREFMASK_BF
, 0);
560 static void r300_dsa_inject_stencilref(struct r300_context
*r300
)
562 struct r300_dsa_state
*dsa
=
563 (struct r300_dsa_state
*)r300
->dsa_state
.state
;
568 dsa
->stencil_ref_mask
=
569 (dsa
->stencil_ref_mask
& ~R300_STENCILREF_MASK
) |
570 r300
->stencil_ref
.ref_value
[0];
571 dsa
->stencil_ref_bf
=
572 (dsa
->stencil_ref_bf
& ~R300_STENCILREF_MASK
) |
573 r300
->stencil_ref
.ref_value
[1];
576 /* Bind DSA state. */
577 static void r300_bind_dsa_state(struct pipe_context
* pipe
,
580 struct r300_context
* r300
= r300_context(pipe
);
586 UPDATE_STATE(state
, r300
->dsa_state
);
588 r300_dsa_inject_stencilref(r300
);
591 /* Free DSA state. */
592 static void r300_delete_dsa_state(struct pipe_context
* pipe
,
598 static void r300_set_stencil_ref(struct pipe_context
* pipe
,
599 const struct pipe_stencil_ref
* sr
)
601 struct r300_context
* r300
= r300_context(pipe
);
603 r300
->stencil_ref
= *sr
;
605 r300_dsa_inject_stencilref(r300
);
606 r300
->dsa_state
.dirty
= TRUE
;
609 /* This switcheroo is needed just because of goddamned MACRO_SWITCH. */
610 static void r300_fb_set_tiling_flags(struct r300_context
*r300
,
611 const struct pipe_framebuffer_state
*old_state
,
612 const struct pipe_framebuffer_state
*new_state
)
614 struct r300_texture
*tex
;
617 /* Set tiling flags for new surfaces. */
618 for (i
= 0; i
< new_state
->nr_cbufs
; i
++) {
619 tex
= r300_texture(new_state
->cbufs
[i
]->texture
);
620 level
= new_state
->cbufs
[i
]->level
;
622 r300
->rws
->buffer_set_tiling(r300
->rws
, tex
->buffer
,
623 tex
->pitch
[0] * util_format_get_blocksize(tex
->b
.b
.format
),
625 tex
->mip_macrotile
[level
]);
627 if (new_state
->zsbuf
) {
628 tex
= r300_texture(new_state
->zsbuf
->texture
);
629 level
= new_state
->zsbuf
->level
;
631 r300
->rws
->buffer_set_tiling(r300
->rws
, tex
->buffer
,
632 tex
->pitch
[0] * util_format_get_blocksize(tex
->b
.b
.format
),
634 tex
->mip_macrotile
[level
]);
638 static void r300_print_fb_surf_info(struct pipe_surface
*surf
, unsigned index
,
641 struct pipe_resource
*tex
= surf
->texture
;
642 struct r300_texture
*rtex
= r300_texture(tex
);
645 "r300: %s[%i] Dim: %ix%i, Offset: %i, ZSlice: %i, "
646 "Face: %i, Level: %i, Format: %s\n"
648 "r300: TEX: Macro: %s, Micro: %s, Pitch: %i, "
649 "Dim: %ix%ix%i, LastLevel: %i, Format: %s\n",
651 binding
, index
, surf
->width
, surf
->height
, surf
->offset
,
652 surf
->zslice
, surf
->face
, surf
->level
,
653 util_format_short_name(surf
->format
),
655 rtex
->macrotile
? "YES" : " NO", rtex
->microtile
? "YES" : " NO",
656 rtex
->hwpitch
[0], tex
->width0
, tex
->height0
, tex
->depth0
,
657 tex
->last_level
, util_format_short_name(tex
->format
));
661 r300_set_framebuffer_state(struct pipe_context
* pipe
,
662 const struct pipe_framebuffer_state
* state
)
664 struct r300_context
* r300
= r300_context(pipe
);
665 struct pipe_framebuffer_state
*old_state
= r300
->fb_state
.state
;
666 unsigned max_width
, max_height
, i
;
667 uint32_t zbuffer_bpp
= 0;
669 if (state
->nr_cbufs
> 4) {
670 fprintf(stderr
, "r300: Implementation error: Too many MRTs in %s, "
671 "refusing to bind framebuffer state!\n", __FUNCTION__
);
675 if (r300
->screen
->caps
.is_r500
) {
676 max_width
= max_height
= 4096;
677 } else if (r300
->screen
->caps
.is_r400
) {
678 max_width
= max_height
= 4021;
680 max_width
= max_height
= 2560;
683 if (state
->width
> max_width
|| state
->height
> max_height
) {
684 fprintf(stderr
, "r300: Implementation error: Render targets are too "
685 "big in %s, refusing to bind framebuffer state!\n", __FUNCTION__
);
690 draw_flush(r300
->draw
);
693 r300
->fb_state
.dirty
= TRUE
;
695 /* If nr_cbufs is changed from zero to non-zero or vice versa... */
696 if (!!old_state
->nr_cbufs
!= !!state
->nr_cbufs
) {
697 r300
->blend_state
.dirty
= TRUE
;
699 /* If zsbuf is set from NULL to non-NULL or vice versa.. */
700 if (!!old_state
->zsbuf
!= !!state
->zsbuf
) {
701 r300
->dsa_state
.dirty
= TRUE
;
704 /* The tiling flags are dependent on the surface miplevel, unfortunately. */
705 r300_fb_set_tiling_flags(r300
, r300
->fb_state
.state
, state
);
707 memcpy(r300
->fb_state
.state
, state
, sizeof(struct pipe_framebuffer_state
));
709 r300
->fb_state
.size
= (10 * state
->nr_cbufs
) + (2 * (4 - state
->nr_cbufs
)) +
710 (state
->zsbuf
? 10 : 0) + 9;
712 /* Polygon offset depends on the zbuffer bit depth. */
713 if (state
->zsbuf
&& r300
->polygon_offset_enabled
) {
714 switch (util_format_get_blocksize(state
->zsbuf
->texture
->format
)) {
723 if (r300
->zbuffer_bpp
!= zbuffer_bpp
) {
724 r300
->zbuffer_bpp
= zbuffer_bpp
;
725 r300
->rs_state
.dirty
= TRUE
;
729 if (DBG_ON(r300
, DBG_FB
)) {
730 fprintf(stderr
, "r300: set_framebuffer_state:\n");
731 for (i
= 0; i
< state
->nr_cbufs
; i
++) {
732 r300_print_fb_surf_info(state
->cbufs
[i
], i
, "CB");
735 r300_print_fb_surf_info(state
->zsbuf
, 0, "ZB");
740 /* Create fragment shader state. */
741 static void* r300_create_fs_state(struct pipe_context
* pipe
,
742 const struct pipe_shader_state
* shader
)
744 struct r300_fragment_shader
* fs
= NULL
;
746 fs
= (struct r300_fragment_shader
*)CALLOC_STRUCT(r300_fragment_shader
);
748 /* Copy state directly into shader. */
750 fs
->state
.tokens
= tgsi_dup_tokens(shader
->tokens
);
755 void r300_mark_fs_code_dirty(struct r300_context
*r300
)
757 struct r300_fragment_shader
* fs
= r300_fs(r300
);
759 r300
->fs
.dirty
= TRUE
;
760 r300
->fs_rc_constant_state
.dirty
= TRUE
;
761 r300
->fs_constants
.dirty
= TRUE
;
762 r300
->fs
.size
= fs
->shader
->cb_code_size
;
764 if (r300
->screen
->caps
.is_r500
) {
765 r300
->fs_rc_constant_state
.size
= fs
->shader
->rc_state_count
* 7;
766 r300
->fs_constants
.size
= fs
->shader
->externals_count
* 4 + 3;
768 r300
->fs_rc_constant_state
.size
= fs
->shader
->rc_state_count
* 5;
769 r300
->fs_constants
.size
= fs
->shader
->externals_count
* 4 + 1;
773 /* Bind fragment shader state. */
774 static void r300_bind_fs_state(struct pipe_context
* pipe
, void* shader
)
776 struct r300_context
* r300
= r300_context(pipe
);
777 struct r300_fragment_shader
* fs
= (struct r300_fragment_shader
*)shader
;
780 r300
->fs
.state
= NULL
;
785 r300_pick_fragment_shader(r300
);
786 r300_mark_fs_code_dirty(r300
);
788 r300
->rs_block_state
.dirty
= TRUE
; /* Will be updated before the emission. */
791 /* Delete fragment shader state. */
792 static void r300_delete_fs_state(struct pipe_context
* pipe
, void* shader
)
794 struct r300_fragment_shader
* fs
= (struct r300_fragment_shader
*)shader
;
795 struct r300_fragment_shader_code
*tmp
, *ptr
= fs
->first
;
800 rc_constants_destroy(&tmp
->code
.constants
);
804 FREE((void*)fs
->state
.tokens
);
808 static void r300_set_polygon_stipple(struct pipe_context
* pipe
,
809 const struct pipe_poly_stipple
* state
)
811 /* XXX no idea how to set this up, but not terribly important */
814 /* Create a new rasterizer state based on the CSO rasterizer state.
816 * This is a very large chunk of state, and covers most of the graphics
817 * backend (GB), geometry assembly (GA), and setup unit (SU) blocks.
819 * In a not entirely unironic sidenote, this state has nearly nothing to do
820 * with the actual block on the Radeon called the rasterizer (RS). */
821 static void* r300_create_rs_state(struct pipe_context
* pipe
,
822 const struct pipe_rasterizer_state
* state
)
824 struct r300_rs_state
* rs
= CALLOC_STRUCT(r300_rs_state
);
828 /* Copy rasterizer state. */
830 rs
->rs_draw
= *state
;
832 /* Override some states for Draw. */
833 rs
->rs_draw
.sprite_coord_enable
= 0; /* We can do this in HW. */
835 #ifdef PIPE_ARCH_LITTLE_ENDIAN
836 rs
->vap_control_status
= R300_VC_NO_SWAP
;
838 rs
->vap_control_status
= R300_VC_32BIT_SWAP
;
841 /* If no TCL engine is present, turn off the HW TCL. */
842 if (!r300_screen(pipe
->screen
)->caps
.has_tcl
) {
843 rs
->vap_control_status
|= R300_VAP_TCL_BYPASS
;
846 /* Point size width and height. */
848 pack_float_16_6x(state
->point_size
) |
849 (pack_float_16_6x(state
->point_size
) << R300_POINTSIZE_X_SHIFT
);
851 /* Point size clamping. */
852 if (state
->point_size_per_vertex
) {
853 /* Per-vertex point size.
854 * Clamp to [0, max FB size] */
855 psiz
= pipe
->screen
->get_paramf(pipe
->screen
,
856 PIPE_CAP_MAX_POINT_WIDTH
);
858 pack_float_16_6x(psiz
) << R300_GA_POINT_MINMAX_MAX_SHIFT
;
860 /* We cannot disable the point-size vertex output,
862 psiz
= state
->point_size
;
864 (pack_float_16_6x(psiz
) << R300_GA_POINT_MINMAX_MIN_SHIFT
) |
865 (pack_float_16_6x(psiz
) << R300_GA_POINT_MINMAX_MAX_SHIFT
);
869 rs
->line_control
= pack_float_16_6x(state
->line_width
) |
870 R300_GA_LINE_CNTL_END_TYPE_COMP
;
872 /* Enable polygon mode */
873 if (state
->fill_front
!= PIPE_POLYGON_MODE_FILL
||
874 state
->fill_back
!= PIPE_POLYGON_MODE_FILL
) {
875 rs
->polygon_mode
= R300_GA_POLY_MODE_DUAL
;
879 if (state
->front_ccw
)
880 rs
->cull_mode
= R300_FRONT_FACE_CCW
;
882 rs
->cull_mode
= R300_FRONT_FACE_CW
;
885 if (util_get_offset(state
, state
->fill_front
)) {
886 rs
->polygon_offset_enable
|= R300_FRONT_ENABLE
;
888 if (util_get_offset(state
, state
->fill_back
)) {
889 rs
->polygon_offset_enable
|= R300_BACK_ENABLE
;
893 if (rs
->polygon_mode
) {
895 r300_translate_polygon_mode_front(state
->fill_front
);
897 r300_translate_polygon_mode_back(state
->fill_back
);
900 if (state
->cull_face
& PIPE_FACE_FRONT
) {
901 rs
->cull_mode
|= R300_CULL_FRONT
;
903 if (state
->cull_face
& PIPE_FACE_BACK
) {
904 rs
->cull_mode
|= R300_CULL_BACK
;
907 if (rs
->polygon_offset_enable
) {
908 rs
->depth_offset
= state
->offset_units
;
909 rs
->depth_scale
= state
->offset_scale
;
912 if (state
->line_stipple_enable
) {
913 rs
->line_stipple_config
=
914 R300_GA_LINE_STIPPLE_CONFIG_LINE_RESET_LINE
|
915 (fui((float)state
->line_stipple_factor
) &
916 R300_GA_LINE_STIPPLE_CONFIG_STIPPLE_SCALE_MASK
);
917 /* XXX this might need to be scaled up */
918 rs
->line_stipple_value
= state
->line_stipple_pattern
;
921 if (state
->flatshade
) {
922 rs
->color_control
= R300_SHADE_MODEL_FLAT
;
924 rs
->color_control
= R300_SHADE_MODEL_SMOOTH
;
927 rs
->clip_rule
= state
->scissor
? 0xAAAA : 0xFFFF;
930 if (state
->sprite_coord_enable
) {
931 rs
->stuffing_enable
= R300_GB_POINT_STUFF_ENABLE
;
932 for (i
= 0; i
< 8; i
++) {
933 if (state
->sprite_coord_enable
& (1 << i
))
934 rs
->stuffing_enable
|=
935 R300_GB_TEX_STR
<< (R300_GB_TEX0_SOURCE_SHIFT
+ (i
*2));
938 rs
->point_texcoord_left
= 0.0f
;
939 rs
->point_texcoord_right
= 1.0f
;
941 switch (state
->sprite_coord_mode
) {
942 case PIPE_SPRITE_COORD_UPPER_LEFT
:
943 rs
->point_texcoord_top
= 0.0f
;
944 rs
->point_texcoord_bottom
= 1.0f
;
946 case PIPE_SPRITE_COORD_LOWER_LEFT
:
947 rs
->point_texcoord_top
= 1.0f
;
948 rs
->point_texcoord_bottom
= 0.0f
;
956 /* Bind rasterizer state. */
957 static void r300_bind_rs_state(struct pipe_context
* pipe
, void* state
)
959 struct r300_context
* r300
= r300_context(pipe
);
960 struct r300_rs_state
* rs
= (struct r300_rs_state
*)state
;
961 int last_sprite_coord_enable
= r300
->sprite_coord_enable
;
962 boolean last_two_sided_color
= r300
->two_sided_color
;
964 if (r300
->draw
&& rs
) {
965 draw_flush(r300
->draw
);
966 draw_set_rasterizer_state(r300
->draw
, &rs
->rs_draw
, state
);
970 r300
->polygon_offset_enabled
= (rs
->rs
.offset_point
||
971 rs
->rs
.offset_line
||
973 r300
->sprite_coord_enable
= rs
->rs
.sprite_coord_enable
;
974 r300
->two_sided_color
= rs
->rs
.light_twoside
;
976 r300
->polygon_offset_enabled
= FALSE
;
977 r300
->sprite_coord_enable
= 0;
978 r300
->two_sided_color
= FALSE
;
981 UPDATE_STATE(state
, r300
->rs_state
);
982 r300
->rs_state
.size
= 27 + (r300
->polygon_offset_enabled
? 5 : 0);
984 if (last_sprite_coord_enable
!= r300
->sprite_coord_enable
||
985 last_two_sided_color
!= r300
->two_sided_color
) {
986 r300
->rs_block_state
.dirty
= TRUE
;
990 /* Free rasterizer state. */
991 static void r300_delete_rs_state(struct pipe_context
* pipe
, void* state
)
997 r300_create_sampler_state(struct pipe_context
* pipe
,
998 const struct pipe_sampler_state
* state
)
1000 struct r300_context
* r300
= r300_context(pipe
);
1001 struct r300_sampler_state
* sampler
= CALLOC_STRUCT(r300_sampler_state
);
1002 boolean is_r500
= r300
->screen
->caps
.is_r500
;
1004 union util_color uc
;
1006 sampler
->state
= *state
;
1009 (r300_translate_wrap(state
->wrap_s
) << R300_TX_WRAP_S_SHIFT
) |
1010 (r300_translate_wrap(state
->wrap_t
) << R300_TX_WRAP_T_SHIFT
) |
1011 (r300_translate_wrap(state
->wrap_r
) << R300_TX_WRAP_R_SHIFT
);
1013 sampler
->filter0
|= r300_translate_tex_filters(state
->min_img_filter
,
1014 state
->mag_img_filter
,
1015 state
->min_mip_filter
,
1016 state
->max_anisotropy
> 0);
1018 sampler
->filter0
|= r300_anisotropy(state
->max_anisotropy
);
1020 /* Unfortunately, r300-r500 don't support floating-point mipmap lods. */
1021 /* We must pass these to the merge function to clamp them properly. */
1022 sampler
->min_lod
= MAX2((unsigned)state
->min_lod
, 0);
1023 sampler
->max_lod
= MAX2((unsigned)ceilf(state
->max_lod
), 0);
1025 lod_bias
= CLAMP((int)(state
->lod_bias
* 32 + 1), -(1 << 9), (1 << 9) - 1);
1027 sampler
->filter1
|= lod_bias
<< R300_LOD_BIAS_SHIFT
;
1029 /* This is very high quality anisotropic filtering for R5xx.
1030 * It's good for benchmarking the performance of texturing but
1031 * in practice we don't want to slow down the driver because it's
1032 * a pretty good performance killer. Feel free to play with it. */
1033 if (DBG_ON(r300
, DBG_ANISOHQ
) && is_r500
) {
1034 sampler
->filter1
|= r500_anisotropy(state
->max_anisotropy
);
1037 util_pack_color(state
->border_color
, PIPE_FORMAT_B8G8R8A8_UNORM
, &uc
);
1038 sampler
->border_color
= uc
.ui
;
1040 /* R500-specific fixups and optimizations */
1041 if (r300
->screen
->caps
.is_r500
) {
1042 sampler
->filter1
|= R500_BORDER_FIX
;
1045 return (void*)sampler
;
1048 static void r300_bind_sampler_states(struct pipe_context
* pipe
,
1052 struct r300_context
* r300
= r300_context(pipe
);
1053 struct r300_textures_state
* state
=
1054 (struct r300_textures_state
*)r300
->textures_state
.state
;
1055 unsigned tex_units
= r300
->screen
->caps
.num_tex_units
;
1057 if (count
> tex_units
) {
1061 memcpy(state
->sampler_states
, states
, sizeof(void*) * count
);
1062 state
->sampler_state_count
= count
;
1064 r300
->textures_state
.dirty
= TRUE
;
1067 static void r300_lacks_vertex_textures(struct pipe_context
* pipe
,
1073 static void r300_delete_sampler_state(struct pipe_context
* pipe
, void* state
)
1078 static void r300_set_fragment_sampler_views(struct pipe_context
* pipe
,
1080 struct pipe_sampler_view
** views
)
1082 struct r300_context
* r300
= r300_context(pipe
);
1083 struct r300_textures_state
* state
=
1084 (struct r300_textures_state
*)r300
->textures_state
.state
;
1085 struct r300_texture
*texture
;
1087 unsigned tex_units
= r300
->screen
->caps
.num_tex_units
;
1088 boolean dirty_tex
= FALSE
;
1090 if (count
> tex_units
) {
1094 for (i
= 0; i
< count
; i
++) {
1095 if (&state
->sampler_views
[i
]->base
!= views
[i
]) {
1096 pipe_sampler_view_reference(
1097 (struct pipe_sampler_view
**)&state
->sampler_views
[i
],
1104 /* A new sampler view (= texture)... */
1107 /* Set the texrect factor in the fragment shader.
1108 * Needed for RECT and NPOT fallback. */
1109 texture
= r300_texture(views
[i
]->texture
);
1110 if (texture
->uses_pitch
) {
1111 r300
->fs_rc_constant_state
.dirty
= TRUE
;
1116 for (i
= count
; i
< tex_units
; i
++) {
1117 if (state
->sampler_views
[i
]) {
1118 pipe_sampler_view_reference(
1119 (struct pipe_sampler_view
**)&state
->sampler_views
[i
],
1124 state
->sampler_view_count
= count
;
1126 r300
->textures_state
.dirty
= TRUE
;
1129 r300
->texture_cache_inval
.dirty
= TRUE
;
1133 static struct pipe_sampler_view
*
1134 r300_create_sampler_view(struct pipe_context
*pipe
,
1135 struct pipe_resource
*texture
,
1136 const struct pipe_sampler_view
*templ
)
1138 struct r300_sampler_view
*view
= CALLOC_STRUCT(r300_sampler_view
);
1139 struct r300_texture
*tex
= r300_texture(texture
);
1142 view
->base
= *templ
;
1143 view
->base
.reference
.count
= 1;
1144 view
->base
.context
= pipe
;
1145 view
->base
.texture
= NULL
;
1146 pipe_resource_reference(&view
->base
.texture
, texture
);
1148 view
->swizzle
[0] = templ
->swizzle_r
;
1149 view
->swizzle
[1] = templ
->swizzle_g
;
1150 view
->swizzle
[2] = templ
->swizzle_b
;
1151 view
->swizzle
[3] = templ
->swizzle_a
;
1153 view
->format
= tex
->tx_format
;
1154 view
->format
.format1
|= r300_translate_texformat(templ
->format
,
1156 if (r300_screen(pipe
->screen
)->caps
.is_r500
) {
1157 view
->format
.format2
|= r500_tx_format_msb_bit(templ
->format
);
1161 return (struct pipe_sampler_view
*)view
;
1165 r300_sampler_view_destroy(struct pipe_context
*pipe
,
1166 struct pipe_sampler_view
*view
)
1168 pipe_resource_reference(&view
->texture
, NULL
);
1172 static void r300_set_scissor_state(struct pipe_context
* pipe
,
1173 const struct pipe_scissor_state
* state
)
1175 struct r300_context
* r300
= r300_context(pipe
);
1177 memcpy(r300
->scissor_state
.state
, state
,
1178 sizeof(struct pipe_scissor_state
));
1180 r300
->scissor_state
.dirty
= TRUE
;
1183 static void r300_set_viewport_state(struct pipe_context
* pipe
,
1184 const struct pipe_viewport_state
* state
)
1186 struct r300_context
* r300
= r300_context(pipe
);
1187 struct r300_viewport_state
* viewport
=
1188 (struct r300_viewport_state
*)r300
->viewport_state
.state
;
1190 r300
->viewport
= *state
;
1193 draw_flush(r300
->draw
);
1194 draw_set_viewport_state(r300
->draw
, state
);
1195 viewport
->vte_control
= R300_VTX_XY_FMT
| R300_VTX_Z_FMT
;
1199 /* Do the transform in HW. */
1200 viewport
->vte_control
= R300_VTX_W0_FMT
;
1202 if (state
->scale
[0] != 1.0f
) {
1203 viewport
->xscale
= state
->scale
[0];
1204 viewport
->vte_control
|= R300_VPORT_X_SCALE_ENA
;
1206 if (state
->scale
[1] != 1.0f
) {
1207 viewport
->yscale
= state
->scale
[1];
1208 viewport
->vte_control
|= R300_VPORT_Y_SCALE_ENA
;
1210 if (state
->scale
[2] != 1.0f
) {
1211 viewport
->zscale
= state
->scale
[2];
1212 viewport
->vte_control
|= R300_VPORT_Z_SCALE_ENA
;
1214 if (state
->translate
[0] != 0.0f
) {
1215 viewport
->xoffset
= state
->translate
[0];
1216 viewport
->vte_control
|= R300_VPORT_X_OFFSET_ENA
;
1218 if (state
->translate
[1] != 0.0f
) {
1219 viewport
->yoffset
= state
->translate
[1];
1220 viewport
->vte_control
|= R300_VPORT_Y_OFFSET_ENA
;
1222 if (state
->translate
[2] != 0.0f
) {
1223 viewport
->zoffset
= state
->translate
[2];
1224 viewport
->vte_control
|= R300_VPORT_Z_OFFSET_ENA
;
1227 r300
->viewport_state
.dirty
= TRUE
;
1228 if (r300
->fs
.state
&& r300_fs(r300
)->shader
->inputs
.wpos
!= ATTR_UNUSED
) {
1229 r300
->fs_rc_constant_state
.dirty
= TRUE
;
1233 static void r300_set_vertex_buffers(struct pipe_context
* pipe
,
1235 const struct pipe_vertex_buffer
* buffers
)
1237 struct r300_context
* r300
= r300_context(pipe
);
1238 struct pipe_vertex_buffer
*vbo
;
1239 unsigned i
, max_index
= (1 << 24) - 1;
1240 boolean any_user_buffer
= FALSE
;
1242 if (count
== r300
->vertex_buffer_count
&&
1243 memcmp(r300
->vertex_buffer
, buffers
,
1244 sizeof(struct pipe_vertex_buffer
) * count
) == 0) {
1248 if (r300
->screen
->caps
.has_tcl
) {
1250 r300
->incompatible_vb_layout
= FALSE
;
1252 /* Check if the strides and offsets are aligned to the size of DWORD. */
1253 for (i
= 0; i
< count
; i
++) {
1254 if (buffers
[i
].buffer
) {
1255 if (buffers
[i
].stride
% 4 != 0 ||
1256 buffers
[i
].buffer_offset
% 4 != 0) {
1257 r300
->incompatible_vb_layout
= TRUE
;
1263 for (i
= 0; i
< count
; i
++) {
1264 /* Why, yes, I AM casting away constness. How did you know? */
1265 vbo
= (struct pipe_vertex_buffer
*)&buffers
[i
];
1267 /* Skip NULL buffers */
1268 if (!buffers
[i
].buffer
) {
1272 if (r300_buffer_is_user_buffer(vbo
->buffer
)) {
1273 any_user_buffer
= TRUE
;
1276 if (vbo
->max_index
== ~0) {
1277 /* if no VBO stride then only one vertex value so max index is 1 */
1278 /* should think about converting to VS constants like svga does */
1283 (vbo
->buffer
->width0
- vbo
->buffer_offset
) / vbo
->stride
;
1286 max_index
= MIN2(vbo
->max_index
, max_index
);
1289 r300
->any_user_vbs
= any_user_buffer
;
1290 r300
->vertex_buffer_max_index
= max_index
;
1294 draw_flush(r300
->draw
);
1295 draw_set_vertex_buffers(r300
->draw
, count
, buffers
);
1299 for (i
= 0; i
< count
; i
++) {
1300 /* Reference our buffer. */
1301 pipe_resource_reference(&r300
->vertex_buffer
[i
].buffer
, buffers
[i
].buffer
);
1303 for (; i
< r300
->vertex_buffer_count
; i
++) {
1304 /* Dereference any old buffers. */
1305 pipe_resource_reference(&r300
->vertex_buffer
[i
].buffer
, NULL
);
1308 memcpy(r300
->vertex_buffer
, buffers
,
1309 sizeof(struct pipe_vertex_buffer
) * count
);
1310 r300
->vertex_buffer_count
= count
;
1313 /* Initialize the PSC tables. */
1314 static void r300_vertex_psc(struct r300_vertex_element_state
*velems
)
1316 struct r300_vertex_stream_state
*vstream
= &velems
->vertex_stream
;
1317 uint16_t type
, swizzle
;
1318 enum pipe_format format
;
1321 if (velems
->count
> 16) {
1322 fprintf(stderr
, "r300: More than 16 vertex elements are not supported,"
1323 " requested %i, using 16.\n", velems
->count
);
1327 /* Vertex shaders have no semantics on their inputs,
1328 * so PSC should just route stuff based on the vertex elements,
1329 * and not on attrib information. */
1330 for (i
= 0; i
< velems
->count
; i
++) {
1331 format
= velems
->hw_format
[i
];
1333 type
= r300_translate_vertex_data_type(format
);
1334 if (type
== R300_INVALID_FORMAT
) {
1335 fprintf(stderr
, "r300: Bad vertex format %s.\n",
1336 util_format_short_name(format
));
1341 type
|= i
<< R300_DST_VEC_LOC_SHIFT
;
1342 swizzle
= r300_translate_vertex_data_swizzle(format
);
1345 vstream
->vap_prog_stream_cntl
[i
>> 1] |= type
<< 16;
1346 vstream
->vap_prog_stream_cntl_ext
[i
>> 1] |= swizzle
<< 16;
1348 vstream
->vap_prog_stream_cntl
[i
>> 1] |= type
;
1349 vstream
->vap_prog_stream_cntl_ext
[i
>> 1] |= swizzle
;
1353 /* Set the last vector in the PSC. */
1357 vstream
->vap_prog_stream_cntl
[i
>> 1] |=
1358 (R300_LAST_VEC
<< (i
& 1 ? 16 : 0));
1360 vstream
->count
= (i
>> 1) + 1;
1363 #define FORMAT_REPLACE(what, withwhat) \
1364 case PIPE_FORMAT_##what: *format = PIPE_FORMAT_##withwhat; break
1366 static void* r300_create_vertex_elements_state(struct pipe_context
* pipe
,
1368 const struct pipe_vertex_element
* attribs
)
1370 struct r300_vertex_element_state
*velems
;
1372 enum pipe_format
*format
;
1374 assert(count
<= PIPE_MAX_ATTRIBS
);
1375 velems
= CALLOC_STRUCT(r300_vertex_element_state
);
1376 if (velems
!= NULL
) {
1377 velems
->count
= count
;
1378 memcpy(velems
->velem
, attribs
, sizeof(struct pipe_vertex_element
) * count
);
1380 if (r300_screen(pipe
->screen
)->caps
.has_tcl
) {
1381 /* Set the best hw format in case the original format is not
1382 * supported by hw. */
1383 for (i
= 0; i
< count
; i
++) {
1384 velems
->hw_format
[i
] = velems
->velem
[i
].src_format
;
1385 format
= &velems
->hw_format
[i
];
1387 /* This is basically the list of unsupported formats.
1388 * For now we don't care about the alignment, that's going to
1389 * be sorted out after the PSC setup. */
1391 FORMAT_REPLACE(R64_FLOAT
, R32_FLOAT
);
1392 FORMAT_REPLACE(R64G64_FLOAT
, R32G32_FLOAT
);
1393 FORMAT_REPLACE(R64G64B64_FLOAT
, R32G32B32_FLOAT
);
1394 FORMAT_REPLACE(R64G64B64A64_FLOAT
, R32G32B32A32_FLOAT
);
1396 FORMAT_REPLACE(R32_UNORM
, R32_FLOAT
);
1397 FORMAT_REPLACE(R32G32_UNORM
, R32G32_FLOAT
);
1398 FORMAT_REPLACE(R32G32B32_UNORM
, R32G32B32_FLOAT
);
1399 FORMAT_REPLACE(R32G32B32A32_UNORM
, R32G32B32A32_FLOAT
);
1401 FORMAT_REPLACE(R32_USCALED
, R32_FLOAT
);
1402 FORMAT_REPLACE(R32G32_USCALED
, R32G32_FLOAT
);
1403 FORMAT_REPLACE(R32G32B32_USCALED
, R32G32B32_FLOAT
);
1404 FORMAT_REPLACE(R32G32B32A32_USCALED
,R32G32B32A32_FLOAT
);
1406 FORMAT_REPLACE(R32_SNORM
, R32_FLOAT
);
1407 FORMAT_REPLACE(R32G32_SNORM
, R32G32_FLOAT
);
1408 FORMAT_REPLACE(R32G32B32_SNORM
, R32G32B32_FLOAT
);
1409 FORMAT_REPLACE(R32G32B32A32_SNORM
, R32G32B32A32_FLOAT
);
1411 FORMAT_REPLACE(R32_SSCALED
, R32_FLOAT
);
1412 FORMAT_REPLACE(R32G32_SSCALED
, R32G32_FLOAT
);
1413 FORMAT_REPLACE(R32G32B32_SSCALED
, R32G32B32_FLOAT
);
1414 FORMAT_REPLACE(R32G32B32A32_SSCALED
,R32G32B32A32_FLOAT
);
1416 FORMAT_REPLACE(R32_FIXED
, R32_FLOAT
);
1417 FORMAT_REPLACE(R32G32_FIXED
, R32G32_FLOAT
);
1418 FORMAT_REPLACE(R32G32B32_FIXED
, R32G32B32_FLOAT
);
1419 FORMAT_REPLACE(R32G32B32A32_FIXED
, R32G32B32A32_FLOAT
);
1424 velems
->incompatible_layout
=
1425 velems
->incompatible_layout
||
1426 velems
->velem
[i
].src_format
!= velems
->hw_format
[i
] ||
1427 velems
->velem
[i
].src_offset
% 4 != 0;
1431 * The unused components will be replaced by (..., 0, 1). */
1432 r300_vertex_psc(velems
);
1434 /* Align the formats to the size of DWORD.
1435 * We only care about the blocksizes of the formats since
1436 * swizzles are already set up. */
1437 for (i
= 0; i
< count
; i
++) {
1438 /* This is OK because we check for aligned strides too. */
1439 velems
->hw_format_size
[i
] =
1440 align(util_format_get_blocksize(velems
->hw_format
[i
]), 4);
1447 static void r300_bind_vertex_elements_state(struct pipe_context
*pipe
,
1450 struct r300_context
*r300
= r300_context(pipe
);
1451 struct r300_vertex_element_state
*velems
= state
;
1453 if (velems
== NULL
) {
1457 r300
->velems
= velems
;
1460 draw_flush(r300
->draw
);
1461 draw_set_vertex_elements(r300
->draw
, velems
->count
, velems
->velem
);
1465 UPDATE_STATE(&velems
->vertex_stream
, r300
->vertex_stream_state
);
1466 r300
->vertex_stream_state
.size
= (1 + velems
->vertex_stream
.count
) * 2;
1469 static void r300_delete_vertex_elements_state(struct pipe_context
*pipe
, void *state
)
1474 static void* r300_create_vs_state(struct pipe_context
* pipe
,
1475 const struct pipe_shader_state
* shader
)
1477 struct r300_context
* r300
= r300_context(pipe
);
1479 struct r300_vertex_shader
* vs
= CALLOC_STRUCT(r300_vertex_shader
);
1481 /* Copy state directly into shader. */
1482 vs
->state
= *shader
;
1483 vs
->state
.tokens
= tgsi_dup_tokens(shader
->tokens
);
1485 if (r300
->screen
->caps
.has_tcl
) {
1486 r300_init_vs_outputs(vs
);
1487 r300_translate_vertex_shader(r300
, vs
);
1489 r300_draw_init_vertex_shader(r300
->draw
, vs
);
1495 static void r300_bind_vs_state(struct pipe_context
* pipe
, void* shader
)
1497 struct r300_context
* r300
= r300_context(pipe
);
1498 struct r300_vertex_shader
* vs
= (struct r300_vertex_shader
*)shader
;
1501 r300
->vs_state
.state
= NULL
;
1504 if (vs
== r300
->vs_state
.state
) {
1507 r300
->vs_state
.state
= vs
;
1509 /* The majority of the RS block bits is dependent on the vertex shader. */
1510 r300
->rs_block_state
.dirty
= TRUE
; /* Will be updated before the emission. */
1512 if (r300
->screen
->caps
.has_tcl
) {
1513 r300
->vs_state
.dirty
= TRUE
;
1514 r300
->vs_state
.size
=
1515 vs
->code
.length
+ 9 +
1516 (vs
->immediates_count
? vs
->immediates_count
* 4 + 3 : 0);
1518 if (vs
->externals_count
) {
1519 r300
->vs_constants
.dirty
= TRUE
;
1520 r300
->vs_constants
.size
= vs
->externals_count
* 4 + 3;
1522 r300
->vs_constants
.size
= 0;
1525 r300
->pvs_flush
.dirty
= TRUE
;
1527 draw_flush(r300
->draw
);
1528 draw_bind_vertex_shader(r300
->draw
,
1529 (struct draw_vertex_shader
*)vs
->draw_vs
);
1533 static void r300_delete_vs_state(struct pipe_context
* pipe
, void* shader
)
1535 struct r300_context
* r300
= r300_context(pipe
);
1536 struct r300_vertex_shader
* vs
= (struct r300_vertex_shader
*)shader
;
1538 if (r300
->screen
->caps
.has_tcl
) {
1539 rc_constants_destroy(&vs
->code
.constants
);
1541 draw_delete_vertex_shader(r300
->draw
,
1542 (struct draw_vertex_shader
*)vs
->draw_vs
);
1545 FREE((void*)vs
->state
.tokens
);
1549 static void r300_set_constant_buffer(struct pipe_context
*pipe
,
1550 uint shader
, uint index
,
1551 struct pipe_resource
*buf
)
1553 struct r300_context
* r300
= r300_context(pipe
);
1554 struct r300_constant_buffer
*cbuf
;
1555 struct pipe_transfer
*tr
;
1557 int max_size
= 0, max_size_bytes
= 0, clamped_size
= 0;
1560 case PIPE_SHADER_VERTEX
:
1561 cbuf
= (struct r300_constant_buffer
*)r300
->vs_constants
.state
;
1564 case PIPE_SHADER_FRAGMENT
:
1565 cbuf
= (struct r300_constant_buffer
*)r300
->fs_constants
.state
;
1566 if (r300
->screen
->caps
.is_r500
) {
1576 max_size_bytes
= max_size
* 4 * sizeof(float);
1578 if (buf
== NULL
|| buf
->width0
== 0 ||
1579 (mapped
= pipe_buffer_map(pipe
, buf
, PIPE_TRANSFER_READ
, &tr
)) == NULL
)
1585 if (shader
== PIPE_SHADER_FRAGMENT
||
1586 (shader
== PIPE_SHADER_VERTEX
&& r300
->screen
->caps
.has_tcl
)) {
1587 assert((buf
->width0
% (4 * sizeof(float))) == 0);
1589 /* Check the size of the constant buffer. */
1590 /* XXX Subtract immediates and RC_STATE_* variables. */
1591 if (buf
->width0
> max_size_bytes
) {
1592 fprintf(stderr
, "r300: Max size of the constant buffer is "
1593 "%i*4 floats.\n", max_size
);
1596 clamped_size
= MIN2(buf
->width0
, max_size_bytes
);
1597 cbuf
->count
= clamped_size
/ (4 * sizeof(float));
1599 if (shader
== PIPE_SHADER_FRAGMENT
&& !r300
->screen
->caps
.is_r500
) {
1602 /* Convert constants to float24. */
1603 for (i
= 0; i
< cbuf
->count
; i
++)
1604 for (j
= 0; j
< 4; j
++)
1605 cbuf
->constants
[i
][j
] = pack_float24(mapped
[i
*4+j
]);
1607 memcpy(cbuf
->constants
, mapped
, clamped_size
);
1611 if (shader
== PIPE_SHADER_VERTEX
) {
1612 if (r300
->screen
->caps
.has_tcl
) {
1613 if (r300
->vs_constants
.size
) {
1614 r300
->vs_constants
.dirty
= TRUE
;
1616 r300
->pvs_flush
.dirty
= TRUE
;
1617 } else if (r300
->draw
) {
1618 draw_set_mapped_constant_buffer(r300
->draw
, PIPE_SHADER_VERTEX
,
1619 0, mapped
, buf
->width0
);
1621 } else if (shader
== PIPE_SHADER_FRAGMENT
) {
1622 r300
->fs_constants
.dirty
= TRUE
;
1625 pipe_buffer_unmap(pipe
, buf
, tr
);
1628 void r300_init_state_functions(struct r300_context
* r300
)
1630 r300
->context
.create_blend_state
= r300_create_blend_state
;
1631 r300
->context
.bind_blend_state
= r300_bind_blend_state
;
1632 r300
->context
.delete_blend_state
= r300_delete_blend_state
;
1634 r300
->context
.set_blend_color
= r300_set_blend_color
;
1636 r300
->context
.set_clip_state
= r300_set_clip_state
;
1637 r300
->context
.set_sample_mask
= r300_set_sample_mask
;
1639 r300
->context
.set_constant_buffer
= r300_set_constant_buffer
;
1641 r300
->context
.create_depth_stencil_alpha_state
= r300_create_dsa_state
;
1642 r300
->context
.bind_depth_stencil_alpha_state
= r300_bind_dsa_state
;
1643 r300
->context
.delete_depth_stencil_alpha_state
= r300_delete_dsa_state
;
1645 r300
->context
.set_stencil_ref
= r300_set_stencil_ref
;
1647 r300
->context
.set_framebuffer_state
= r300_set_framebuffer_state
;
1649 r300
->context
.create_fs_state
= r300_create_fs_state
;
1650 r300
->context
.bind_fs_state
= r300_bind_fs_state
;
1651 r300
->context
.delete_fs_state
= r300_delete_fs_state
;
1653 r300
->context
.set_polygon_stipple
= r300_set_polygon_stipple
;
1655 r300
->context
.create_rasterizer_state
= r300_create_rs_state
;
1656 r300
->context
.bind_rasterizer_state
= r300_bind_rs_state
;
1657 r300
->context
.delete_rasterizer_state
= r300_delete_rs_state
;
1659 r300
->context
.create_sampler_state
= r300_create_sampler_state
;
1660 r300
->context
.bind_fragment_sampler_states
= r300_bind_sampler_states
;
1661 r300
->context
.bind_vertex_sampler_states
= r300_lacks_vertex_textures
;
1662 r300
->context
.delete_sampler_state
= r300_delete_sampler_state
;
1664 r300
->context
.set_fragment_sampler_views
= r300_set_fragment_sampler_views
;
1665 r300
->context
.create_sampler_view
= r300_create_sampler_view
;
1666 r300
->context
.sampler_view_destroy
= r300_sampler_view_destroy
;
1668 r300
->context
.set_scissor_state
= r300_set_scissor_state
;
1670 r300
->context
.set_viewport_state
= r300_set_viewport_state
;
1672 r300
->context
.set_vertex_buffers
= r300_set_vertex_buffers
;
1674 r300
->context
.create_vertex_elements_state
= r300_create_vertex_elements_state
;
1675 r300
->context
.bind_vertex_elements_state
= r300_bind_vertex_elements_state
;
1676 r300
->context
.delete_vertex_elements_state
= r300_delete_vertex_elements_state
;
1678 r300
->context
.create_vs_state
= r300_create_vs_state
;
1679 r300
->context
.bind_vs_state
= r300_bind_vs_state
;
1680 r300
->context
.delete_vs_state
= r300_delete_vs_state
;