2 * Copyright 2008 Corbin Simpson <MostAwesomeDude@gmail.com>
3 * Copyright 2009 Marek Olšák <maraeo@gmail.com>
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * on the rights to use, copy, modify, merge, publish, distribute, sub
9 * license, and/or sell copies of the Software, and to permit persons to whom
10 * the Software is furnished to do so, subject to the following conditions:
12 * The above copyright notice and this permission notice (including the next
13 * paragraph) shall be included in all copies or substantial portions of the
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHOR(S) AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
20 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
21 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
22 * USE OR OTHER DEALINGS IN THE SOFTWARE. */
24 #include "draw/draw_context.h"
26 #include "util/u_math.h"
27 #include "util/u_memory.h"
28 #include "util/u_pack_color.h"
30 #include "tgsi/tgsi_parse.h"
32 #include "pipe/p_config.h"
34 #include "r300_context.h"
35 #include "r300_emit.h"
37 #include "r300_screen.h"
38 #include "r300_screen_buffer.h"
39 #include "r300_state.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
);
187 if (state
->rt
[0].blend_enable
)
189 unsigned eqRGB
= state
->rt
[0].rgb_func
;
190 unsigned srcRGB
= state
->rt
[0].rgb_src_factor
;
191 unsigned dstRGB
= state
->rt
[0].rgb_dst_factor
;
193 unsigned eqA
= state
->rt
[0].alpha_func
;
194 unsigned srcA
= state
->rt
[0].alpha_src_factor
;
195 unsigned dstA
= state
->rt
[0].alpha_dst_factor
;
197 /* despite the name, ALPHA_BLEND_ENABLE has nothing to do with alpha,
198 * this is just the crappy D3D naming */
199 blend
->blend_control
= R300_ALPHA_BLEND_ENABLE
|
200 r300_translate_blend_function(eqRGB
) |
201 ( r300_translate_blend_factor(srcRGB
) << R300_SRC_BLEND_SHIFT
) |
202 ( r300_translate_blend_factor(dstRGB
) << R300_DST_BLEND_SHIFT
);
204 /* Optimization: some operations do not require the destination color.
206 * When SRC_ALPHA_SATURATE is used, colorbuffer reads must be enabled,
207 * otherwise blending gives incorrect results. It seems to be
209 if (eqRGB
== PIPE_BLEND_MIN
|| eqA
== PIPE_BLEND_MIN
||
210 eqRGB
== PIPE_BLEND_MAX
|| eqA
== PIPE_BLEND_MAX
||
211 dstRGB
!= PIPE_BLENDFACTOR_ZERO
||
212 dstA
!= PIPE_BLENDFACTOR_ZERO
||
213 srcRGB
== PIPE_BLENDFACTOR_DST_COLOR
||
214 srcRGB
== PIPE_BLENDFACTOR_DST_ALPHA
||
215 srcRGB
== PIPE_BLENDFACTOR_INV_DST_COLOR
||
216 srcRGB
== PIPE_BLENDFACTOR_INV_DST_ALPHA
||
217 srcA
== PIPE_BLENDFACTOR_DST_COLOR
||
218 srcA
== PIPE_BLENDFACTOR_DST_ALPHA
||
219 srcA
== PIPE_BLENDFACTOR_INV_DST_COLOR
||
220 srcA
== PIPE_BLENDFACTOR_INV_DST_ALPHA
||
221 srcRGB
== PIPE_BLENDFACTOR_SRC_ALPHA_SATURATE
) {
222 /* Enable reading from the colorbuffer. */
223 blend
->blend_control
|= R300_READ_ENABLE
;
225 if (r300screen
->caps
.is_r500
) {
226 /* Optimization: Depending on incoming pixels, we can
227 * conditionally disable the reading in hardware... */
228 if (eqRGB
!= PIPE_BLEND_MIN
&& eqA
!= PIPE_BLEND_MIN
&&
229 eqRGB
!= PIPE_BLEND_MAX
&& eqA
!= PIPE_BLEND_MAX
) {
230 /* Disable reading if SRC_ALPHA == 0. */
231 if ((dstRGB
== PIPE_BLENDFACTOR_SRC_ALPHA
||
232 dstRGB
== PIPE_BLENDFACTOR_ZERO
) &&
233 (dstA
== PIPE_BLENDFACTOR_SRC_COLOR
||
234 dstA
== PIPE_BLENDFACTOR_SRC_ALPHA
||
235 dstA
== PIPE_BLENDFACTOR_ZERO
)) {
236 blend
->blend_control
|= R500_SRC_ALPHA_0_NO_READ
;
239 /* Disable reading if SRC_ALPHA == 1. */
240 if ((dstRGB
== PIPE_BLENDFACTOR_INV_SRC_ALPHA
||
241 dstRGB
== PIPE_BLENDFACTOR_ZERO
) &&
242 (dstA
== PIPE_BLENDFACTOR_INV_SRC_COLOR
||
243 dstA
== PIPE_BLENDFACTOR_INV_SRC_ALPHA
||
244 dstA
== PIPE_BLENDFACTOR_ZERO
)) {
245 blend
->blend_control
|= R500_SRC_ALPHA_1_NO_READ
;
251 /* Optimization: discard pixels which don't change the colorbuffer.
253 * The code below is non-trivial and some math is involved.
255 * Discarding pixels must be disabled when FP16 AA is enabled.
256 * This is a hardware bug. Also, this implementation wouldn't work
257 * with FP blending enabled and equation clamping disabled.
259 * Equations other than ADD are rarely used and therefore won't be
261 if ((eqRGB
== PIPE_BLEND_ADD
|| eqRGB
== PIPE_BLEND_REVERSE_SUBTRACT
) &&
262 (eqA
== PIPE_BLEND_ADD
|| eqA
== PIPE_BLEND_REVERSE_SUBTRACT
)) {
264 * REVERSE_SUBTRACT: Y-X
267 * If X = src*srcFactor = 0 and Y = dst*dstFactor = 1,
268 * then CB will not be changed.
270 * Given the srcFactor and dstFactor variables, we can derive
271 * what src and dst should be equal to and discard appropriate
274 if (blend_discard_if_src_alpha_0(srcRGB
, srcA
, dstRGB
, dstA
)) {
275 blend
->blend_control
|= R300_DISCARD_SRC_PIXELS_SRC_ALPHA_0
;
276 } else if (blend_discard_if_src_alpha_1(srcRGB
, srcA
,
278 blend
->blend_control
|= R300_DISCARD_SRC_PIXELS_SRC_ALPHA_1
;
279 } else if (blend_discard_if_src_color_0(srcRGB
, srcA
,
281 blend
->blend_control
|= R300_DISCARD_SRC_PIXELS_SRC_COLOR_0
;
282 } else if (blend_discard_if_src_color_1(srcRGB
, srcA
,
284 blend
->blend_control
|= R300_DISCARD_SRC_PIXELS_SRC_COLOR_1
;
285 } else if (blend_discard_if_src_alpha_color_0(srcRGB
, srcA
,
287 blend
->blend_control
|=
288 R300_DISCARD_SRC_PIXELS_SRC_ALPHA_COLOR_0
;
289 } else if (blend_discard_if_src_alpha_color_1(srcRGB
, srcA
,
291 blend
->blend_control
|=
292 R300_DISCARD_SRC_PIXELS_SRC_ALPHA_COLOR_1
;
297 if (srcA
!= srcRGB
|| dstA
!= dstRGB
|| eqA
!= eqRGB
) {
298 blend
->blend_control
|= R300_SEPARATE_ALPHA_ENABLE
;
299 blend
->alpha_blend_control
=
300 r300_translate_blend_function(eqA
) |
301 (r300_translate_blend_factor(srcA
) << R300_SRC_BLEND_SHIFT
) |
302 (r300_translate_blend_factor(dstA
) << R300_DST_BLEND_SHIFT
);
306 /* PIPE_LOGICOP_* don't need to be translated, fortunately. */
307 if (state
->logicop_enable
) {
308 blend
->rop
= R300_RB3D_ROPCNTL_ROP_ENABLE
|
309 (state
->logicop_func
) << R300_RB3D_ROPCNTL_ROP_SHIFT
;
312 /* Color channel masks for all MRTs. */
313 blend
->color_channel_mask
= bgra_cmask(state
->rt
[0].colormask
);
314 if (r300screen
->caps
.is_r500
&& state
->independent_blend_enable
) {
315 if (state
->rt
[1].blend_enable
) {
316 blend
->color_channel_mask
|= bgra_cmask(state
->rt
[1].colormask
) << 4;
318 if (state
->rt
[2].blend_enable
) {
319 blend
->color_channel_mask
|= bgra_cmask(state
->rt
[2].colormask
) << 8;
321 if (state
->rt
[3].blend_enable
) {
322 blend
->color_channel_mask
|= bgra_cmask(state
->rt
[3].colormask
) << 12;
326 /* Neither fglrx nor classic r300 ever set this, regardless of dithering
327 * state. Since it's an optional implementation detail, we can leave it
328 * out and never dither.
330 * This could be revisited if we ever get quality or conformance hints.
333 blend->dither = R300_RB3D_DITHER_CTL_DITHER_MODE_LUT |
334 R300_RB3D_DITHER_CTL_ALPHA_DITHER_MODE_LUT;
341 /* Bind blend state. */
342 static void r300_bind_blend_state(struct pipe_context
* pipe
,
345 struct r300_context
* r300
= r300_context(pipe
);
347 UPDATE_STATE(state
, r300
->blend_state
);
350 /* Free blend state. */
351 static void r300_delete_blend_state(struct pipe_context
* pipe
,
357 /* Convert float to 10bit integer */
358 static unsigned float_to_fixed10(float f
)
360 return CLAMP((unsigned)(f
* 1023.9f
), 0, 1023);
364 * Setup both R300 and R500 registers, figure out later which one to write. */
365 static void r300_set_blend_color(struct pipe_context
* pipe
,
366 const struct pipe_blend_color
* color
)
368 struct r300_context
* r300
= r300_context(pipe
);
369 struct r300_blend_color_state
* state
=
370 (struct r300_blend_color_state
*)r300
->blend_color_state
.state
;
373 util_pack_color(color
->color
, PIPE_FORMAT_B8G8R8A8_UNORM
, &uc
);
374 state
->blend_color
= uc
.ui
;
376 /* XXX if FP16 blending is enabled, we should use the FP16 format */
377 state
->blend_color_red_alpha
=
378 float_to_fixed10(color
->color
[0]) |
379 (float_to_fixed10(color
->color
[3]) << 16);
380 state
->blend_color_green_blue
=
381 float_to_fixed10(color
->color
[2]) |
382 (float_to_fixed10(color
->color
[1]) << 16);
384 r300
->blend_color_state
.size
= r300
->screen
->caps
.is_r500
? 3 : 2;
385 r300
->blend_color_state
.dirty
= TRUE
;
388 static void r300_set_clip_state(struct pipe_context
* pipe
,
389 const struct pipe_clip_state
* state
)
391 struct r300_context
* r300
= r300_context(pipe
);
395 if (r300
->screen
->caps
.has_tcl
) {
396 memcpy(r300
->clip_state
.state
, state
, sizeof(struct pipe_clip_state
));
397 r300
->clip_state
.size
= 29;
399 r300
->clip_state
.dirty
= TRUE
;
401 draw_flush(r300
->draw
);
402 draw_set_clip_state(r300
->draw
, state
);
403 r300
->clip_state
.size
= 2;
408 r300_set_sample_mask(struct pipe_context
*pipe
,
409 unsigned sample_mask
)
414 /* Create a new depth, stencil, and alpha state based on the CSO dsa state.
416 * This contains the depth buffer, stencil buffer, alpha test, and such.
417 * On the Radeon, depth and stencil buffer setup are intertwined, which is
418 * the reason for some of the strange-looking assignments across registers. */
420 r300_create_dsa_state(struct pipe_context
* pipe
,
421 const struct pipe_depth_stencil_alpha_state
* state
)
423 struct r300_capabilities
*caps
= &r300_screen(pipe
->screen
)->caps
;
424 struct r300_dsa_state
* dsa
= CALLOC_STRUCT(r300_dsa_state
);
426 /* Depth test setup. */
427 if (state
->depth
.enabled
) {
428 dsa
->z_buffer_control
|= R300_Z_ENABLE
;
430 if (state
->depth
.writemask
) {
431 dsa
->z_buffer_control
|= R300_Z_WRITE_ENABLE
;
434 dsa
->z_stencil_control
|=
435 (r300_translate_depth_stencil_function(state
->depth
.func
) <<
439 /* Stencil buffer setup. */
440 if (state
->stencil
[0].enabled
) {
441 dsa
->z_buffer_control
|= R300_STENCIL_ENABLE
;
442 dsa
->z_stencil_control
|=
443 (r300_translate_depth_stencil_function(state
->stencil
[0].func
) <<
444 R300_S_FRONT_FUNC_SHIFT
) |
445 (r300_translate_stencil_op(state
->stencil
[0].fail_op
) <<
446 R300_S_FRONT_SFAIL_OP_SHIFT
) |
447 (r300_translate_stencil_op(state
->stencil
[0].zpass_op
) <<
448 R300_S_FRONT_ZPASS_OP_SHIFT
) |
449 (r300_translate_stencil_op(state
->stencil
[0].zfail_op
) <<
450 R300_S_FRONT_ZFAIL_OP_SHIFT
);
452 dsa
->stencil_ref_mask
=
453 (state
->stencil
[0].valuemask
<< R300_STENCILMASK_SHIFT
) |
454 (state
->stencil
[0].writemask
<< R300_STENCILWRITEMASK_SHIFT
);
456 if (state
->stencil
[1].enabled
) {
457 dsa
->two_sided
= TRUE
;
459 dsa
->z_buffer_control
|= R300_STENCIL_FRONT_BACK
;
460 dsa
->z_stencil_control
|=
461 (r300_translate_depth_stencil_function(state
->stencil
[1].func
) <<
462 R300_S_BACK_FUNC_SHIFT
) |
463 (r300_translate_stencil_op(state
->stencil
[1].fail_op
) <<
464 R300_S_BACK_SFAIL_OP_SHIFT
) |
465 (r300_translate_stencil_op(state
->stencil
[1].zpass_op
) <<
466 R300_S_BACK_ZPASS_OP_SHIFT
) |
467 (r300_translate_stencil_op(state
->stencil
[1].zfail_op
) <<
468 R300_S_BACK_ZFAIL_OP_SHIFT
);
470 dsa
->stencil_ref_bf
=
471 (state
->stencil
[1].valuemask
<< R300_STENCILMASK_SHIFT
) |
472 (state
->stencil
[1].writemask
<< R300_STENCILWRITEMASK_SHIFT
);
475 dsa
->z_buffer_control
|= R500_STENCIL_REFMASK_FRONT_BACK
;
477 dsa
->stencil_ref_bf_fallback
=
478 (state
->stencil
[0].valuemask
!= state
->stencil
[1].valuemask
||
479 state
->stencil
[0].writemask
!= state
->stencil
[1].writemask
);
484 /* Alpha test setup. */
485 if (state
->alpha
.enabled
) {
486 dsa
->alpha_function
=
487 r300_translate_alpha_function(state
->alpha
.func
) |
488 R300_FG_ALPHA_FUNC_ENABLE
;
490 /* We could use 10bit alpha ref but who needs that? */
491 dsa
->alpha_function
|= float_to_ubyte(state
->alpha
.ref_value
);
494 dsa
->alpha_function
|= R500_FG_ALPHA_FUNC_8BIT
;
500 static void r300_update_stencil_ref_fallback_status(struct r300_context
*r300
)
502 struct r300_dsa_state
*dsa
= (struct r300_dsa_state
*)r300
->dsa_state
.state
;
504 if (r300
->screen
->caps
.is_r500
) {
508 r300
->stencil_ref_bf_fallback
=
509 dsa
->stencil_ref_bf_fallback
||
511 r300
->stencil_ref
.ref_value
[0] != r300
->stencil_ref
.ref_value
[1]);
514 /* Bind DSA state. */
515 static void r300_bind_dsa_state(struct pipe_context
* pipe
,
518 struct r300_context
* r300
= r300_context(pipe
);
524 UPDATE_STATE(state
, r300
->dsa_state
);
526 r300_update_stencil_ref_fallback_status(r300
);
529 /* Free DSA state. */
530 static void r300_delete_dsa_state(struct pipe_context
* pipe
,
536 static void r300_set_stencil_ref(struct pipe_context
* pipe
,
537 const struct pipe_stencil_ref
* sr
)
539 struct r300_context
* r300
= r300_context(pipe
);
541 r300
->stencil_ref
= *sr
;
542 r300
->dsa_state
.dirty
= TRUE
;
544 r300_update_stencil_ref_fallback_status(r300
);
547 /* This switcheroo is needed just because of goddamned MACRO_SWITCH. */
548 static void r300_fb_set_tiling_flags(struct r300_context
*r300
,
549 const struct pipe_framebuffer_state
*old_state
,
550 const struct pipe_framebuffer_state
*new_state
)
552 struct r300_texture
*tex
;
555 /* Set tiling flags for new surfaces. */
556 for (i
= 0; i
< new_state
->nr_cbufs
; i
++) {
557 tex
= r300_texture(new_state
->cbufs
[i
]->texture
);
558 level
= new_state
->cbufs
[i
]->level
;
560 r300
->rws
->buffer_set_tiling(r300
->rws
, tex
->buffer
,
563 tex
->mip_macrotile
[level
]);
565 if (new_state
->zsbuf
) {
566 tex
= r300_texture(new_state
->zsbuf
->texture
);
567 level
= new_state
->zsbuf
->level
;
569 r300
->rws
->buffer_set_tiling(r300
->rws
, tex
->buffer
,
572 tex
->mip_macrotile
[level
]);
576 static void r300_print_fb_surf_info(struct pipe_surface
*surf
, unsigned index
,
579 struct pipe_resource
*tex
= surf
->texture
;
580 struct r300_texture
*rtex
= r300_texture(tex
);
583 "r300: %s[%i] Dim: %ix%i, Offset: %i, ZSlice: %i, "
584 "Face: %i, Level: %i, Format: %s\n"
586 "r300: TEX: Macro: %s, Micro: %s, Pitch: %i, "
587 "Dim: %ix%ix%i, LastLevel: %i, Format: %s\n",
589 binding
, index
, surf
->width
, surf
->height
, surf
->offset
,
590 surf
->zslice
, surf
->face
, surf
->level
,
591 util_format_short_name(surf
->format
),
593 rtex
->macrotile
? "YES" : " NO", rtex
->microtile
? "YES" : " NO",
594 rtex
->hwpitch
[0], tex
->width0
, tex
->height0
, tex
->depth0
,
595 tex
->last_level
, util_format_short_name(tex
->format
));
599 r300_set_framebuffer_state(struct pipe_context
* pipe
,
600 const struct pipe_framebuffer_state
* state
)
602 struct r300_context
* r300
= r300_context(pipe
);
603 struct pipe_framebuffer_state
*old_state
= r300
->fb_state
.state
;
604 unsigned max_width
, max_height
, i
;
605 uint32_t zbuffer_bpp
= 0;
607 if (state
->nr_cbufs
> 4) {
608 fprintf(stderr
, "r300: Implementation error: Too many MRTs in %s, "
609 "refusing to bind framebuffer state!\n", __FUNCTION__
);
613 if (r300
->screen
->caps
.is_r500
) {
614 max_width
= max_height
= 4096;
615 } else if (r300
->screen
->caps
.is_r400
) {
616 max_width
= max_height
= 4021;
618 max_width
= max_height
= 2560;
621 if (state
->width
> max_width
|| state
->height
> max_height
) {
622 fprintf(stderr
, "r300: Implementation error: Render targets are too "
623 "big in %s, refusing to bind framebuffer state!\n", __FUNCTION__
);
628 draw_flush(r300
->draw
);
631 r300
->fb_state
.dirty
= TRUE
;
633 /* If nr_cbufs is changed from zero to non-zero or vice versa... */
634 if (!!old_state
->nr_cbufs
!= !!state
->nr_cbufs
) {
635 r300
->blend_state
.dirty
= TRUE
;
637 /* If zsbuf is set from NULL to non-NULL or vice versa.. */
638 if (!!old_state
->zsbuf
!= !!state
->zsbuf
) {
639 r300
->dsa_state
.dirty
= TRUE
;
642 /* The tiling flags are dependent on the surface miplevel, unfortunately. */
643 r300_fb_set_tiling_flags(r300
, r300
->fb_state
.state
, state
);
645 memcpy(r300
->fb_state
.state
, state
, sizeof(struct pipe_framebuffer_state
));
647 r300
->fb_state
.size
= (10 * state
->nr_cbufs
) + (2 * (4 - state
->nr_cbufs
)) +
648 (state
->zsbuf
? 10 : 0) + 9;
650 /* Polygon offset depends on the zbuffer bit depth. */
651 if (state
->zsbuf
&& r300
->polygon_offset_enabled
) {
652 switch (util_format_get_blocksize(state
->zsbuf
->texture
->format
)) {
661 if (r300
->zbuffer_bpp
!= zbuffer_bpp
) {
662 r300
->zbuffer_bpp
= zbuffer_bpp
;
663 r300
->rs_state
.dirty
= TRUE
;
667 if (DBG_ON(r300
, DBG_FB
)) {
668 fprintf(stderr
, "r300: set_framebuffer_state:\n");
669 for (i
= 0; i
< state
->nr_cbufs
; i
++) {
670 r300_print_fb_surf_info(state
->cbufs
[i
], i
, "CB");
673 r300_print_fb_surf_info(state
->zsbuf
, 0, "ZB");
678 /* Create fragment shader state. */
679 static void* r300_create_fs_state(struct pipe_context
* pipe
,
680 const struct pipe_shader_state
* shader
)
682 struct r300_fragment_shader
* fs
= NULL
;
684 fs
= (struct r300_fragment_shader
*)CALLOC_STRUCT(r300_fragment_shader
);
686 /* Copy state directly into shader. */
688 fs
->state
.tokens
= tgsi_dup_tokens(shader
->tokens
);
693 void r300_mark_fs_code_dirty(struct r300_context
*r300
)
695 struct r300_fragment_shader
* fs
= r300_fs(r300
);
697 r300
->fs
.dirty
= TRUE
;
698 r300
->fs_rc_constant_state
.dirty
= TRUE
;
699 r300
->fs_constants
.dirty
= TRUE
;
701 if (r300
->screen
->caps
.is_r500
) {
702 r300
->fs
.size
= r500_get_fs_atom_size(r300
);
703 r300
->fs_rc_constant_state
.size
= fs
->shader
->rc_state_count
* 7;
704 r300
->fs_constants
.size
= fs
->shader
->externals_count
* 4 + 3;
706 r300
->fs
.size
= r300_get_fs_atom_size(r300
);
707 r300
->fs_rc_constant_state
.size
= fs
->shader
->rc_state_count
* 5;
708 r300
->fs_constants
.size
= fs
->shader
->externals_count
* 4 + 1;
712 /* Bind fragment shader state. */
713 static void r300_bind_fs_state(struct pipe_context
* pipe
, void* shader
)
715 struct r300_context
* r300
= r300_context(pipe
);
716 struct r300_fragment_shader
* fs
= (struct r300_fragment_shader
*)shader
;
719 r300
->fs
.state
= NULL
;
724 r300_pick_fragment_shader(r300
);
725 r300_mark_fs_code_dirty(r300
);
727 r300
->rs_block_state
.dirty
= TRUE
; /* Will be updated before the emission. */
730 /* Delete fragment shader state. */
731 static void r300_delete_fs_state(struct pipe_context
* pipe
, void* shader
)
733 struct r300_fragment_shader
* fs
= (struct r300_fragment_shader
*)shader
;
734 struct r300_fragment_shader_code
*tmp
, *ptr
= fs
->first
;
739 rc_constants_destroy(&tmp
->code
.constants
);
742 FREE((void*)fs
->state
.tokens
);
746 static void r300_set_polygon_stipple(struct pipe_context
* pipe
,
747 const struct pipe_poly_stipple
* state
)
749 /* XXX no idea how to set this up, but not terribly important */
752 /* Create a new rasterizer state based on the CSO rasterizer state.
754 * This is a very large chunk of state, and covers most of the graphics
755 * backend (GB), geometry assembly (GA), and setup unit (SU) blocks.
757 * In a not entirely unironic sidenote, this state has nearly nothing to do
758 * with the actual block on the Radeon called the rasterizer (RS). */
759 static void* r300_create_rs_state(struct pipe_context
* pipe
,
760 const struct pipe_rasterizer_state
* state
)
762 struct r300_rs_state
* rs
= CALLOC_STRUCT(r300_rs_state
);
766 /* Copy rasterizer state. */
768 rs
->rs_draw
= *state
;
770 /* Override some states for Draw. */
771 rs
->rs_draw
.sprite_coord_enable
= 0; /* We can do this in HW. */
773 #ifdef PIPE_ARCH_LITTLE_ENDIAN
774 rs
->vap_control_status
= R300_VC_NO_SWAP
;
776 rs
->vap_control_status
= R300_VC_32BIT_SWAP
;
779 /* If no TCL engine is present, turn off the HW TCL. */
780 if (!r300_screen(pipe
->screen
)->caps
.has_tcl
) {
781 rs
->vap_control_status
|= R300_VAP_TCL_BYPASS
;
784 /* Point size width and height. */
786 pack_float_16_6x(state
->point_size
) |
787 (pack_float_16_6x(state
->point_size
) << R300_POINTSIZE_X_SHIFT
);
789 /* Point size clamping. */
790 if (state
->point_size_per_vertex
) {
791 /* Per-vertex point size.
792 * Clamp to [0, max FB size] */
793 psiz
= pipe
->screen
->get_paramf(pipe
->screen
,
794 PIPE_CAP_MAX_POINT_WIDTH
);
796 pack_float_16_6x(psiz
) << R300_GA_POINT_MINMAX_MAX_SHIFT
;
798 /* We cannot disable the point-size vertex output,
800 psiz
= state
->point_size
;
802 (pack_float_16_6x(psiz
) << R300_GA_POINT_MINMAX_MIN_SHIFT
) |
803 (pack_float_16_6x(psiz
) << R300_GA_POINT_MINMAX_MAX_SHIFT
);
807 rs
->line_control
= pack_float_16_6x(state
->line_width
) |
808 R300_GA_LINE_CNTL_END_TYPE_COMP
;
810 /* Enable polygon mode */
811 if (state
->fill_front
!= PIPE_POLYGON_MODE_FILL
||
812 state
->fill_back
!= PIPE_POLYGON_MODE_FILL
) {
813 rs
->polygon_mode
= R300_GA_POLY_MODE_DUAL
;
817 if (state
->front_ccw
)
818 rs
->cull_mode
= R300_FRONT_FACE_CCW
;
820 rs
->cull_mode
= R300_FRONT_FACE_CW
;
823 if (util_get_offset(state
, state
->fill_front
)) {
824 rs
->polygon_offset_enable
|= R300_FRONT_ENABLE
;
826 if (util_get_offset(state
, state
->fill_back
)) {
827 rs
->polygon_offset_enable
|= R300_BACK_ENABLE
;
831 if (rs
->polygon_mode
) {
833 r300_translate_polygon_mode_front(state
->fill_front
);
835 r300_translate_polygon_mode_back(state
->fill_back
);
838 if (state
->cull_face
& PIPE_FACE_FRONT
) {
839 rs
->cull_mode
|= R300_CULL_FRONT
;
841 if (state
->cull_face
& PIPE_FACE_BACK
) {
842 rs
->cull_mode
|= R300_CULL_BACK
;
845 if (rs
->polygon_offset_enable
) {
846 rs
->depth_offset
= state
->offset_units
;
847 rs
->depth_scale
= state
->offset_scale
;
850 if (state
->line_stipple_enable
) {
851 rs
->line_stipple_config
=
852 R300_GA_LINE_STIPPLE_CONFIG_LINE_RESET_LINE
|
853 (fui((float)state
->line_stipple_factor
) &
854 R300_GA_LINE_STIPPLE_CONFIG_STIPPLE_SCALE_MASK
);
855 /* XXX this might need to be scaled up */
856 rs
->line_stipple_value
= state
->line_stipple_pattern
;
859 if (state
->flatshade
) {
860 rs
->color_control
= R300_SHADE_MODEL_FLAT
;
862 rs
->color_control
= R300_SHADE_MODEL_SMOOTH
;
865 rs
->clip_rule
= state
->scissor
? 0xAAAA : 0xFFFF;
868 if (state
->sprite_coord_enable
) {
869 rs
->stuffing_enable
= R300_GB_POINT_STUFF_ENABLE
;
870 for (i
= 0; i
< 8; i
++) {
871 if (state
->sprite_coord_enable
& (1 << i
))
872 rs
->stuffing_enable
|=
873 R300_GB_TEX_STR
<< (R300_GB_TEX0_SOURCE_SHIFT
+ (i
*2));
876 rs
->point_texcoord_left
= 0.0f
;
877 rs
->point_texcoord_right
= 1.0f
;
879 switch (state
->sprite_coord_mode
) {
880 case PIPE_SPRITE_COORD_UPPER_LEFT
:
881 rs
->point_texcoord_top
= 0.0f
;
882 rs
->point_texcoord_bottom
= 1.0f
;
884 case PIPE_SPRITE_COORD_LOWER_LEFT
:
885 rs
->point_texcoord_top
= 1.0f
;
886 rs
->point_texcoord_bottom
= 0.0f
;
894 /* Bind rasterizer state. */
895 static void r300_bind_rs_state(struct pipe_context
* pipe
, void* state
)
897 struct r300_context
* r300
= r300_context(pipe
);
898 struct r300_rs_state
* rs
= (struct r300_rs_state
*)state
;
899 int last_sprite_coord_enable
= r300
->sprite_coord_enable
;
900 boolean last_two_sided_color
= r300
->two_sided_color
;
902 if (r300
->draw
&& rs
) {
903 draw_flush(r300
->draw
);
904 draw_set_rasterizer_state(r300
->draw
, &rs
->rs_draw
, state
);
908 r300
->polygon_offset_enabled
= (rs
->rs
.offset_point
||
909 rs
->rs
.offset_line
||
911 r300
->sprite_coord_enable
= rs
->rs
.sprite_coord_enable
;
912 r300
->two_sided_color
= rs
->rs
.light_twoside
;
914 r300
->polygon_offset_enabled
= FALSE
;
915 r300
->sprite_coord_enable
= 0;
916 r300
->two_sided_color
= FALSE
;
919 UPDATE_STATE(state
, r300
->rs_state
);
920 r300
->rs_state
.size
= 27 + (r300
->polygon_offset_enabled
? 5 : 0);
922 if (last_sprite_coord_enable
!= r300
->sprite_coord_enable
||
923 last_two_sided_color
!= r300
->two_sided_color
) {
924 r300
->rs_block_state
.dirty
= TRUE
;
928 /* Free rasterizer state. */
929 static void r300_delete_rs_state(struct pipe_context
* pipe
, void* state
)
935 r300_create_sampler_state(struct pipe_context
* pipe
,
936 const struct pipe_sampler_state
* state
)
938 struct r300_context
* r300
= r300_context(pipe
);
939 struct r300_sampler_state
* sampler
= CALLOC_STRUCT(r300_sampler_state
);
940 boolean is_r500
= r300
->screen
->caps
.is_r500
;
944 sampler
->state
= *state
;
947 (r300_translate_wrap(state
->wrap_s
) << R300_TX_WRAP_S_SHIFT
) |
948 (r300_translate_wrap(state
->wrap_t
) << R300_TX_WRAP_T_SHIFT
) |
949 (r300_translate_wrap(state
->wrap_r
) << R300_TX_WRAP_R_SHIFT
);
951 sampler
->filter0
|= r300_translate_tex_filters(state
->min_img_filter
,
952 state
->mag_img_filter
,
953 state
->min_mip_filter
,
954 state
->max_anisotropy
> 0);
956 sampler
->filter0
|= r300_anisotropy(state
->max_anisotropy
);
958 /* Unfortunately, r300-r500 don't support floating-point mipmap lods. */
959 /* We must pass these to the merge function to clamp them properly. */
960 sampler
->min_lod
= MAX2((unsigned)state
->min_lod
, 0);
961 sampler
->max_lod
= MAX2((unsigned)ceilf(state
->max_lod
), 0);
963 lod_bias
= CLAMP((int)(state
->lod_bias
* 32 + 1), -(1 << 9), (1 << 9) - 1);
965 sampler
->filter1
|= lod_bias
<< R300_LOD_BIAS_SHIFT
;
967 /* This is very high quality anisotropic filtering for R5xx.
968 * It's good for benchmarking the performance of texturing but
969 * in practice we don't want to slow down the driver because it's
970 * a pretty good performance killer. Feel free to play with it. */
971 if (DBG_ON(r300
, DBG_ANISOHQ
) && is_r500
) {
972 sampler
->filter1
|= r500_anisotropy(state
->max_anisotropy
);
975 util_pack_color(state
->border_color
, PIPE_FORMAT_B8G8R8A8_UNORM
, &uc
);
976 sampler
->border_color
= uc
.ui
;
978 /* R500-specific fixups and optimizations */
979 if (r300
->screen
->caps
.is_r500
) {
980 sampler
->filter1
|= R500_BORDER_FIX
;
983 return (void*)sampler
;
986 static void r300_bind_sampler_states(struct pipe_context
* pipe
,
990 struct r300_context
* r300
= r300_context(pipe
);
991 struct r300_textures_state
* state
=
992 (struct r300_textures_state
*)r300
->textures_state
.state
;
993 unsigned tex_units
= r300
->screen
->caps
.num_tex_units
;
995 if (count
> tex_units
) {
999 memcpy(state
->sampler_states
, states
, sizeof(void*) * count
);
1000 state
->sampler_state_count
= count
;
1002 r300
->textures_state
.dirty
= TRUE
;
1005 static void r300_lacks_vertex_textures(struct pipe_context
* pipe
,
1011 static void r300_delete_sampler_state(struct pipe_context
* pipe
, void* state
)
1016 static void r300_set_fragment_sampler_views(struct pipe_context
* pipe
,
1018 struct pipe_sampler_view
** views
)
1020 struct r300_context
* r300
= r300_context(pipe
);
1021 struct r300_textures_state
* state
=
1022 (struct r300_textures_state
*)r300
->textures_state
.state
;
1023 struct r300_texture
*texture
;
1025 unsigned tex_units
= r300
->screen
->caps
.num_tex_units
;
1026 boolean dirty_tex
= FALSE
;
1028 if (count
> tex_units
) {
1032 for (i
= 0; i
< count
; i
++) {
1033 if (&state
->sampler_views
[i
]->base
!= views
[i
]) {
1034 pipe_sampler_view_reference(
1035 (struct pipe_sampler_view
**)&state
->sampler_views
[i
],
1042 /* A new sampler view (= texture)... */
1045 /* Set the texrect factor in the fragment shader.
1046 * Needed for RECT and NPOT fallback. */
1047 texture
= r300_texture(views
[i
]->texture
);
1048 if (texture
->uses_pitch
) {
1049 r300
->fs_rc_constant_state
.dirty
= TRUE
;
1054 for (i
= count
; i
< tex_units
; i
++) {
1055 if (state
->sampler_views
[i
]) {
1056 pipe_sampler_view_reference(
1057 (struct pipe_sampler_view
**)&state
->sampler_views
[i
],
1062 state
->sampler_view_count
= count
;
1064 r300
->textures_state
.dirty
= TRUE
;
1067 r300
->texture_cache_inval
.dirty
= TRUE
;
1071 static struct pipe_sampler_view
*
1072 r300_create_sampler_view(struct pipe_context
*pipe
,
1073 struct pipe_resource
*texture
,
1074 const struct pipe_sampler_view
*templ
)
1076 struct r300_sampler_view
*view
= CALLOC_STRUCT(r300_sampler_view
);
1077 struct r300_texture
*tex
= r300_texture(texture
);
1080 view
->base
= *templ
;
1081 view
->base
.reference
.count
= 1;
1082 view
->base
.context
= pipe
;
1083 view
->base
.texture
= NULL
;
1084 pipe_resource_reference(&view
->base
.texture
, texture
);
1086 view
->swizzle
[0] = templ
->swizzle_r
;
1087 view
->swizzle
[1] = templ
->swizzle_g
;
1088 view
->swizzle
[2] = templ
->swizzle_b
;
1089 view
->swizzle
[3] = templ
->swizzle_a
;
1091 view
->format
= tex
->tx_format
;
1092 view
->format
.format1
|= r300_translate_texformat(templ
->format
,
1094 if (r300_screen(pipe
->screen
)->caps
.is_r500
) {
1095 view
->format
.format2
|= r500_tx_format_msb_bit(templ
->format
);
1099 return (struct pipe_sampler_view
*)view
;
1103 r300_sampler_view_destroy(struct pipe_context
*pipe
,
1104 struct pipe_sampler_view
*view
)
1106 pipe_resource_reference(&view
->texture
, NULL
);
1110 static void r300_set_scissor_state(struct pipe_context
* pipe
,
1111 const struct pipe_scissor_state
* state
)
1113 struct r300_context
* r300
= r300_context(pipe
);
1115 memcpy(r300
->scissor_state
.state
, state
,
1116 sizeof(struct pipe_scissor_state
));
1118 r300
->scissor_state
.dirty
= TRUE
;
1121 static void r300_set_viewport_state(struct pipe_context
* pipe
,
1122 const struct pipe_viewport_state
* state
)
1124 struct r300_context
* r300
= r300_context(pipe
);
1125 struct r300_viewport_state
* viewport
=
1126 (struct r300_viewport_state
*)r300
->viewport_state
.state
;
1128 r300
->viewport
= *state
;
1131 draw_flush(r300
->draw
);
1132 draw_set_viewport_state(r300
->draw
, state
);
1133 viewport
->vte_control
= R300_VTX_XY_FMT
| R300_VTX_Z_FMT
;
1137 /* Do the transform in HW. */
1138 viewport
->vte_control
= R300_VTX_W0_FMT
;
1140 if (state
->scale
[0] != 1.0f
) {
1141 viewport
->xscale
= state
->scale
[0];
1142 viewport
->vte_control
|= R300_VPORT_X_SCALE_ENA
;
1144 if (state
->scale
[1] != 1.0f
) {
1145 viewport
->yscale
= state
->scale
[1];
1146 viewport
->vte_control
|= R300_VPORT_Y_SCALE_ENA
;
1148 if (state
->scale
[2] != 1.0f
) {
1149 viewport
->zscale
= state
->scale
[2];
1150 viewport
->vte_control
|= R300_VPORT_Z_SCALE_ENA
;
1152 if (state
->translate
[0] != 0.0f
) {
1153 viewport
->xoffset
= state
->translate
[0];
1154 viewport
->vte_control
|= R300_VPORT_X_OFFSET_ENA
;
1156 if (state
->translate
[1] != 0.0f
) {
1157 viewport
->yoffset
= state
->translate
[1];
1158 viewport
->vte_control
|= R300_VPORT_Y_OFFSET_ENA
;
1160 if (state
->translate
[2] != 0.0f
) {
1161 viewport
->zoffset
= state
->translate
[2];
1162 viewport
->vte_control
|= R300_VPORT_Z_OFFSET_ENA
;
1165 r300
->viewport_state
.dirty
= TRUE
;
1166 if (r300
->fs
.state
&& r300_fs(r300
)->shader
->inputs
.wpos
!= ATTR_UNUSED
) {
1167 r300
->fs_rc_constant_state
.dirty
= TRUE
;
1171 static void r300_set_vertex_buffers(struct pipe_context
* pipe
,
1173 const struct pipe_vertex_buffer
* buffers
)
1175 struct r300_context
* r300
= r300_context(pipe
);
1176 struct pipe_vertex_buffer
*vbo
;
1177 unsigned i
, max_index
= (1 << 24) - 1;
1178 boolean any_user_buffer
= FALSE
;
1180 if (count
== r300
->vertex_buffer_count
&&
1181 memcmp(r300
->vertex_buffer
, buffers
,
1182 sizeof(struct pipe_vertex_buffer
) * count
) == 0) {
1186 if (r300
->screen
->caps
.has_tcl
) {
1188 /* Check if the stride is aligned to the size of DWORD. */
1189 for (i
= 0; i
< count
; i
++) {
1190 if (buffers
[i
].buffer
) {
1191 if (buffers
[i
].stride
% 4 != 0) {
1192 // XXX Shouldn't we align the buffer?
1193 fprintf(stderr
, "r300: set_vertex_buffers: "
1194 "Unaligned buffer stride %i isn't supported.\n",
1201 for (i
= 0; i
< count
; i
++) {
1202 /* Why, yes, I AM casting away constness. How did you know? */
1203 vbo
= (struct pipe_vertex_buffer
*)&buffers
[i
];
1205 /* Skip NULL buffers */
1206 if (!buffers
[i
].buffer
) {
1210 if (r300_buffer_is_user_buffer(vbo
->buffer
)) {
1211 any_user_buffer
= TRUE
;
1214 if (vbo
->max_index
== ~0) {
1215 /* if no VBO stride then only one vertex value so max index is 1 */
1216 /* should think about converting to VS constants like svga does */
1221 (vbo
->buffer
->width0
- vbo
->buffer_offset
) / vbo
->stride
;
1224 max_index
= MIN2(vbo
->max_index
, max_index
);
1227 r300
->any_user_vbs
= any_user_buffer
;
1228 r300
->vertex_buffer_max_index
= max_index
;
1232 draw_flush(r300
->draw
);
1233 draw_set_vertex_buffers(r300
->draw
, count
, buffers
);
1237 for (i
= 0; i
< count
; i
++) {
1238 /* Reference our buffer. */
1239 pipe_resource_reference(&r300
->vertex_buffer
[i
].buffer
, buffers
[i
].buffer
);
1241 for (; i
< r300
->vertex_buffer_count
; i
++) {
1242 /* Dereference any old buffers. */
1243 pipe_resource_reference(&r300
->vertex_buffer
[i
].buffer
, NULL
);
1246 memcpy(r300
->vertex_buffer
, buffers
,
1247 sizeof(struct pipe_vertex_buffer
) * count
);
1248 r300
->vertex_buffer_count
= count
;
1251 /* Initialize the PSC tables. */
1252 static void r300_vertex_psc(struct r300_vertex_element_state
*velems
)
1254 struct r300_vertex_stream_state
*vstream
= &velems
->vertex_stream
;
1255 uint16_t type
, swizzle
;
1256 enum pipe_format format
;
1259 if (velems
->count
> 16) {
1260 fprintf(stderr
, "r300: More than 16 vertex elements are not supported,"
1261 " requested %i, using 16.\n", velems
->count
);
1265 /* Vertex shaders have no semantics on their inputs,
1266 * so PSC should just route stuff based on the vertex elements,
1267 * and not on attrib information. */
1268 for (i
= 0; i
< velems
->count
; i
++) {
1269 format
= velems
->velem
[i
].src_format
;
1271 type
= r300_translate_vertex_data_type(format
) |
1272 (i
<< R300_DST_VEC_LOC_SHIFT
);
1273 swizzle
= r300_translate_vertex_data_swizzle(format
);
1276 vstream
->vap_prog_stream_cntl
[i
>> 1] |= type
<< 16;
1277 vstream
->vap_prog_stream_cntl_ext
[i
>> 1] |= swizzle
<< 16;
1279 vstream
->vap_prog_stream_cntl
[i
>> 1] |= type
;
1280 vstream
->vap_prog_stream_cntl_ext
[i
>> 1] |= swizzle
;
1284 /* Set the last vector in the PSC. */
1288 vstream
->vap_prog_stream_cntl
[i
>> 1] |=
1289 (R300_LAST_VEC
<< (i
& 1 ? 16 : 0));
1291 vstream
->count
= (i
>> 1) + 1;
1294 static void* r300_create_vertex_elements_state(struct pipe_context
* pipe
,
1296 const struct pipe_vertex_element
* attribs
)
1298 struct r300_vertex_element_state
*velems
;
1300 enum pipe_format
*format
;
1302 assert(count
<= PIPE_MAX_ATTRIBS
);
1303 velems
= CALLOC_STRUCT(r300_vertex_element_state
);
1304 if (velems
!= NULL
) {
1305 velems
->count
= count
;
1306 memcpy(velems
->velem
, attribs
, sizeof(struct pipe_vertex_element
) * count
);
1308 if (r300_screen(pipe
->screen
)->caps
.has_tcl
) {
1309 r300_vertex_psc(velems
);
1311 /* Check if the format is aligned to the size of DWORD.
1312 * We only care about the blocksizes of the formats since
1313 * swizzles are already set up. */
1314 for (i
= 0; i
< count
; i
++) {
1315 format
= &velems
->velem
[i
].src_format
;
1317 /* Replace some formats with their aligned counterparts,
1318 * this is OK because we check for aligned strides too. */
1320 /* Align to RGBA8. */
1321 case PIPE_FORMAT_R8_UNORM
:
1322 case PIPE_FORMAT_R8G8_UNORM
:
1323 case PIPE_FORMAT_R8G8B8_UNORM
:
1324 *format
= PIPE_FORMAT_R8G8B8A8_UNORM
;
1326 case PIPE_FORMAT_R8_SNORM
:
1327 case PIPE_FORMAT_R8G8_SNORM
:
1328 case PIPE_FORMAT_R8G8B8_SNORM
:
1329 *format
= PIPE_FORMAT_R8G8B8A8_SNORM
;
1331 case PIPE_FORMAT_R8_USCALED
:
1332 case PIPE_FORMAT_R8G8_USCALED
:
1333 case PIPE_FORMAT_R8G8B8_USCALED
:
1334 *format
= PIPE_FORMAT_R8G8B8A8_USCALED
;
1336 case PIPE_FORMAT_R8_SSCALED
:
1337 case PIPE_FORMAT_R8G8_SSCALED
:
1338 case PIPE_FORMAT_R8G8B8_SSCALED
:
1339 *format
= PIPE_FORMAT_R8G8B8A8_SSCALED
;
1342 /* Align to RG16. */
1343 case PIPE_FORMAT_R16_UNORM
:
1344 *format
= PIPE_FORMAT_R16G16_UNORM
;
1346 case PIPE_FORMAT_R16_SNORM
:
1347 *format
= PIPE_FORMAT_R16G16_SNORM
;
1349 case PIPE_FORMAT_R16_USCALED
:
1350 *format
= PIPE_FORMAT_R16G16_USCALED
;
1352 case PIPE_FORMAT_R16_SSCALED
:
1353 *format
= PIPE_FORMAT_R16G16_SSCALED
;
1355 case PIPE_FORMAT_R16_FLOAT
:
1356 *format
= PIPE_FORMAT_R16G16_FLOAT
;
1359 /* Align to RGBA16. */
1360 case PIPE_FORMAT_R16G16B16_UNORM
:
1361 *format
= PIPE_FORMAT_R16G16B16A16_UNORM
;
1363 case PIPE_FORMAT_R16G16B16_SNORM
:
1364 *format
= PIPE_FORMAT_R16G16B16A16_SNORM
;
1366 case PIPE_FORMAT_R16G16B16_USCALED
:
1367 *format
= PIPE_FORMAT_R16G16B16A16_USCALED
;
1369 case PIPE_FORMAT_R16G16B16_SSCALED
:
1370 *format
= PIPE_FORMAT_R16G16B16A16_SSCALED
;
1372 case PIPE_FORMAT_R16G16B16_FLOAT
:
1373 *format
= PIPE_FORMAT_R16G16B16A16_FLOAT
;
1379 size
= util_format_get_blocksize(*format
);
1381 if (size
% 4 != 0) {
1382 /* XXX Shouldn't we align the format? */
1383 fprintf(stderr
, "r300_create_vertex_elements_state: "
1384 "Unaligned format %s:%i isn't supported\n",
1385 util_format_short_name(*format
), size
);
1395 static void r300_bind_vertex_elements_state(struct pipe_context
*pipe
,
1398 struct r300_context
*r300
= r300_context(pipe
);
1399 struct r300_vertex_element_state
*velems
= state
;
1401 if (velems
== NULL
) {
1405 r300
->velems
= velems
;
1408 draw_flush(r300
->draw
);
1409 draw_set_vertex_elements(r300
->draw
, velems
->count
, velems
->velem
);
1413 UPDATE_STATE(&velems
->vertex_stream
, r300
->vertex_stream_state
);
1414 r300
->vertex_stream_state
.size
= (1 + velems
->vertex_stream
.count
) * 2;
1417 static void r300_delete_vertex_elements_state(struct pipe_context
*pipe
, void *state
)
1422 static void* r300_create_vs_state(struct pipe_context
* pipe
,
1423 const struct pipe_shader_state
* shader
)
1425 struct r300_context
* r300
= r300_context(pipe
);
1427 struct r300_vertex_shader
* vs
= CALLOC_STRUCT(r300_vertex_shader
);
1429 /* Copy state directly into shader. */
1430 vs
->state
= *shader
;
1431 vs
->state
.tokens
= tgsi_dup_tokens(shader
->tokens
);
1433 r300_init_vs_outputs(vs
);
1435 if (r300
->screen
->caps
.has_tcl
) {
1436 r300_translate_vertex_shader(r300
, vs
);
1438 vs
->draw_vs
= draw_create_vertex_shader(r300
->draw
, shader
);
1444 static void r300_bind_vs_state(struct pipe_context
* pipe
, void* shader
)
1446 struct r300_context
* r300
= r300_context(pipe
);
1447 struct r300_vertex_shader
* vs
= (struct r300_vertex_shader
*)shader
;
1450 r300
->vs_state
.state
= NULL
;
1453 if (vs
== r300
->vs_state
.state
) {
1456 r300
->vs_state
.state
= vs
;
1458 /* The majority of the RS block bits is dependent on the vertex shader. */
1459 r300
->rs_block_state
.dirty
= TRUE
; /* Will be updated before the emission. */
1461 if (r300
->screen
->caps
.has_tcl
) {
1462 r300
->vs_state
.dirty
= TRUE
;
1463 r300
->vs_state
.size
=
1464 vs
->code
.length
+ 9 +
1465 (vs
->immediates_count
? vs
->immediates_count
* 4 + 3 : 0);
1467 if (vs
->externals_count
) {
1468 r300
->vs_constants
.dirty
= TRUE
;
1469 r300
->vs_constants
.size
= vs
->externals_count
* 4 + 3;
1471 r300
->vs_constants
.size
= 0;
1474 r300
->pvs_flush
.dirty
= TRUE
;
1476 draw_flush(r300
->draw
);
1477 draw_bind_vertex_shader(r300
->draw
,
1478 (struct draw_vertex_shader
*)vs
->draw_vs
);
1482 static void r300_delete_vs_state(struct pipe_context
* pipe
, void* shader
)
1484 struct r300_context
* r300
= r300_context(pipe
);
1485 struct r300_vertex_shader
* vs
= (struct r300_vertex_shader
*)shader
;
1487 if (r300
->screen
->caps
.has_tcl
) {
1488 rc_constants_destroy(&vs
->code
.constants
);
1490 draw_delete_vertex_shader(r300
->draw
,
1491 (struct draw_vertex_shader
*)vs
->draw_vs
);
1494 FREE((void*)vs
->state
.tokens
);
1498 static void r300_set_constant_buffer(struct pipe_context
*pipe
,
1499 uint shader
, uint index
,
1500 struct pipe_resource
*buf
)
1502 struct r300_context
* r300
= r300_context(pipe
);
1503 struct r300_constant_buffer
*cbuf
;
1504 struct pipe_transfer
*tr
;
1506 int max_size
= 0, max_size_bytes
= 0, clamped_size
= 0;
1509 case PIPE_SHADER_VERTEX
:
1510 cbuf
= (struct r300_constant_buffer
*)r300
->vs_constants
.state
;
1513 case PIPE_SHADER_FRAGMENT
:
1514 cbuf
= (struct r300_constant_buffer
*)r300
->fs_constants
.state
;
1515 if (r300
->screen
->caps
.is_r500
) {
1525 max_size_bytes
= max_size
* 4 * sizeof(float);
1527 if (buf
== NULL
|| buf
->width0
== 0 ||
1528 (mapped
= pipe_buffer_map(pipe
, buf
, PIPE_TRANSFER_READ
, &tr
)) == NULL
)
1534 if (shader
== PIPE_SHADER_FRAGMENT
||
1535 (shader
== PIPE_SHADER_VERTEX
&& r300
->screen
->caps
.has_tcl
)) {
1536 assert((buf
->width0
% (4 * sizeof(float))) == 0);
1538 /* Check the size of the constant buffer. */
1539 /* XXX Subtract immediates and RC_STATE_* variables. */
1540 if (buf
->width0
> max_size_bytes
) {
1541 fprintf(stderr
, "r300: Max size of the constant buffer is "
1542 "%i*4 floats.\n", max_size
);
1544 clamped_size
= MIN2(buf
->width0
, max_size_bytes
);
1546 memcpy(cbuf
->constants
, mapped
, clamped_size
);
1547 cbuf
->count
= clamped_size
/ (4 * sizeof(float));
1550 if (shader
== PIPE_SHADER_VERTEX
) {
1551 if (r300
->screen
->caps
.has_tcl
) {
1552 if (r300
->vs_constants
.size
) {
1553 r300
->vs_constants
.dirty
= TRUE
;
1555 r300
->pvs_flush
.dirty
= TRUE
;
1556 } else if (r300
->draw
) {
1557 draw_set_mapped_constant_buffer(r300
->draw
, PIPE_SHADER_VERTEX
,
1558 0, mapped
, buf
->width0
);
1560 } else if (shader
== PIPE_SHADER_FRAGMENT
) {
1561 r300
->fs_constants
.dirty
= TRUE
;
1564 pipe_buffer_unmap(pipe
, buf
, tr
);
1567 void r300_init_state_functions(struct r300_context
* r300
)
1569 r300
->context
.create_blend_state
= r300_create_blend_state
;
1570 r300
->context
.bind_blend_state
= r300_bind_blend_state
;
1571 r300
->context
.delete_blend_state
= r300_delete_blend_state
;
1573 r300
->context
.set_blend_color
= r300_set_blend_color
;
1575 r300
->context
.set_clip_state
= r300_set_clip_state
;
1576 r300
->context
.set_sample_mask
= r300_set_sample_mask
;
1578 r300
->context
.set_constant_buffer
= r300_set_constant_buffer
;
1580 r300
->context
.create_depth_stencil_alpha_state
= r300_create_dsa_state
;
1581 r300
->context
.bind_depth_stencil_alpha_state
= r300_bind_dsa_state
;
1582 r300
->context
.delete_depth_stencil_alpha_state
= r300_delete_dsa_state
;
1584 r300
->context
.set_stencil_ref
= r300_set_stencil_ref
;
1586 r300
->context
.set_framebuffer_state
= r300_set_framebuffer_state
;
1588 r300
->context
.create_fs_state
= r300_create_fs_state
;
1589 r300
->context
.bind_fs_state
= r300_bind_fs_state
;
1590 r300
->context
.delete_fs_state
= r300_delete_fs_state
;
1592 r300
->context
.set_polygon_stipple
= r300_set_polygon_stipple
;
1594 r300
->context
.create_rasterizer_state
= r300_create_rs_state
;
1595 r300
->context
.bind_rasterizer_state
= r300_bind_rs_state
;
1596 r300
->context
.delete_rasterizer_state
= r300_delete_rs_state
;
1598 r300
->context
.create_sampler_state
= r300_create_sampler_state
;
1599 r300
->context
.bind_fragment_sampler_states
= r300_bind_sampler_states
;
1600 r300
->context
.bind_vertex_sampler_states
= r300_lacks_vertex_textures
;
1601 r300
->context
.delete_sampler_state
= r300_delete_sampler_state
;
1603 r300
->context
.set_fragment_sampler_views
= r300_set_fragment_sampler_views
;
1604 r300
->context
.create_sampler_view
= r300_create_sampler_view
;
1605 r300
->context
.sampler_view_destroy
= r300_sampler_view_destroy
;
1607 r300
->context
.set_scissor_state
= r300_set_scissor_state
;
1609 r300
->context
.set_viewport_state
= r300_set_viewport_state
;
1611 r300
->context
.set_vertex_buffers
= r300_set_vertex_buffers
;
1613 r300
->context
.create_vertex_elements_state
= r300_create_vertex_elements_state
;
1614 r300
->context
.bind_vertex_elements_state
= r300_bind_vertex_elements_state
;
1615 r300
->context
.delete_vertex_elements_state
= r300_delete_vertex_elements_state
;
1617 r300
->context
.create_vs_state
= r300_create_vs_state
;
1618 r300
->context
.bind_vs_state
= r300_bind_vs_state
;
1619 r300
->context
.delete_vs_state
= r300_delete_vs_state
;