2 * Copyright 2008 Corbin Simpson <MostAwesomeDude@gmail.com>
3 * Copyright 2009 Marek Olšák <maraeo@gmail.com>
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * on the rights to use, copy, modify, merge, publish, distribute, sub
9 * license, and/or sell copies of the Software, and to permit persons to whom
10 * the Software is furnished to do so, subject to the following conditions:
12 * The above copyright notice and this permission notice (including the next
13 * paragraph) shall be included in all copies or substantial portions of the
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHOR(S) AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
20 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
21 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
22 * USE OR OTHER DEALINGS IN THE SOFTWARE. */
24 #include "draw/draw_context.h"
26 #include "util/u_math.h"
27 #include "util/u_memory.h"
28 #include "util/u_pack_color.h"
30 #include "tgsi/tgsi_parse.h"
32 #include "pipe/p_config.h"
34 #include "r300_context.h"
36 #include "r300_screen.h"
37 #include "r300_screen_buffer.h"
38 #include "r300_state_inlines.h"
42 #include "radeon_winsys.h"
44 /* r300_state: Functions used to intialize state context by translating
45 * Gallium state objects into semi-native r300 state objects. */
47 #define UPDATE_STATE(cso, atom) \
48 if (cso != atom.state) { \
53 static boolean
blend_discard_if_src_alpha_0(unsigned srcRGB
, unsigned srcA
,
54 unsigned dstRGB
, unsigned dstA
)
56 /* If the blend equation is ADD or REVERSE_SUBTRACT,
57 * SRC_ALPHA == 0, and the following state is set, the colorbuffer
58 * will not be changed.
59 * Notice that the dst factors are the src factors inverted. */
60 return (srcRGB
== PIPE_BLENDFACTOR_SRC_ALPHA
||
61 srcRGB
== PIPE_BLENDFACTOR_SRC_ALPHA_SATURATE
||
62 srcRGB
== PIPE_BLENDFACTOR_ZERO
) &&
63 (srcA
== PIPE_BLENDFACTOR_SRC_COLOR
||
64 srcA
== PIPE_BLENDFACTOR_SRC_ALPHA
||
65 srcA
== PIPE_BLENDFACTOR_SRC_ALPHA_SATURATE
||
66 srcA
== PIPE_BLENDFACTOR_ZERO
) &&
67 (dstRGB
== PIPE_BLENDFACTOR_INV_SRC_ALPHA
||
68 dstRGB
== PIPE_BLENDFACTOR_ONE
) &&
69 (dstA
== PIPE_BLENDFACTOR_INV_SRC_COLOR
||
70 dstA
== PIPE_BLENDFACTOR_INV_SRC_ALPHA
||
71 dstA
== PIPE_BLENDFACTOR_ONE
);
74 static boolean
blend_discard_if_src_alpha_1(unsigned srcRGB
, unsigned srcA
,
75 unsigned dstRGB
, unsigned dstA
)
77 /* If the blend equation is ADD or REVERSE_SUBTRACT,
78 * SRC_ALPHA == 1, and the following state is set, the colorbuffer
79 * will not be changed.
80 * Notice that the dst factors are the src factors inverted. */
81 return (srcRGB
== PIPE_BLENDFACTOR_INV_SRC_ALPHA
||
82 srcRGB
== PIPE_BLENDFACTOR_ZERO
) &&
83 (srcA
== PIPE_BLENDFACTOR_INV_SRC_COLOR
||
84 srcA
== PIPE_BLENDFACTOR_INV_SRC_ALPHA
||
85 srcA
== PIPE_BLENDFACTOR_ZERO
) &&
86 (dstRGB
== PIPE_BLENDFACTOR_SRC_ALPHA
||
87 dstRGB
== PIPE_BLENDFACTOR_ONE
) &&
88 (dstA
== PIPE_BLENDFACTOR_SRC_COLOR
||
89 dstA
== PIPE_BLENDFACTOR_SRC_ALPHA
||
90 dstA
== PIPE_BLENDFACTOR_ONE
);
93 static boolean
blend_discard_if_src_color_0(unsigned srcRGB
, unsigned srcA
,
94 unsigned dstRGB
, unsigned dstA
)
96 /* If the blend equation is ADD or REVERSE_SUBTRACT,
97 * SRC_COLOR == (0,0,0), and the following state is set, the colorbuffer
98 * will not be changed.
99 * Notice that the dst factors are the src factors inverted. */
100 return (srcRGB
== PIPE_BLENDFACTOR_SRC_COLOR
||
101 srcRGB
== PIPE_BLENDFACTOR_ZERO
) &&
102 (srcA
== PIPE_BLENDFACTOR_ZERO
) &&
103 (dstRGB
== PIPE_BLENDFACTOR_INV_SRC_COLOR
||
104 dstRGB
== PIPE_BLENDFACTOR_ONE
) &&
105 (dstA
== PIPE_BLENDFACTOR_ONE
);
108 static boolean
blend_discard_if_src_color_1(unsigned srcRGB
, unsigned srcA
,
109 unsigned dstRGB
, unsigned dstA
)
111 /* If the blend equation is ADD or REVERSE_SUBTRACT,
112 * SRC_COLOR == (1,1,1), and the following state is set, the colorbuffer
113 * will not be changed.
114 * Notice that the dst factors are the src factors inverted. */
115 return (srcRGB
== PIPE_BLENDFACTOR_INV_SRC_COLOR
||
116 srcRGB
== PIPE_BLENDFACTOR_ZERO
) &&
117 (srcA
== PIPE_BLENDFACTOR_ZERO
) &&
118 (dstRGB
== PIPE_BLENDFACTOR_SRC_COLOR
||
119 dstRGB
== PIPE_BLENDFACTOR_ONE
) &&
120 (dstA
== PIPE_BLENDFACTOR_ONE
);
123 static boolean
blend_discard_if_src_alpha_color_0(unsigned srcRGB
, unsigned srcA
,
124 unsigned dstRGB
, unsigned dstA
)
126 /* If the blend equation is ADD or REVERSE_SUBTRACT,
127 * SRC_ALPHA_COLOR == (0,0,0,0), and the following state is set,
128 * the colorbuffer will not be changed.
129 * Notice that the dst factors are the src factors inverted. */
130 return (srcRGB
== PIPE_BLENDFACTOR_SRC_COLOR
||
131 srcRGB
== PIPE_BLENDFACTOR_SRC_ALPHA
||
132 srcRGB
== PIPE_BLENDFACTOR_SRC_ALPHA_SATURATE
||
133 srcRGB
== PIPE_BLENDFACTOR_ZERO
) &&
134 (srcA
== PIPE_BLENDFACTOR_SRC_COLOR
||
135 srcA
== PIPE_BLENDFACTOR_SRC_ALPHA
||
136 srcA
== PIPE_BLENDFACTOR_SRC_ALPHA_SATURATE
||
137 srcA
== PIPE_BLENDFACTOR_ZERO
) &&
138 (dstRGB
== PIPE_BLENDFACTOR_INV_SRC_COLOR
||
139 dstRGB
== PIPE_BLENDFACTOR_INV_SRC_ALPHA
||
140 dstRGB
== PIPE_BLENDFACTOR_ONE
) &&
141 (dstA
== PIPE_BLENDFACTOR_INV_SRC_COLOR
||
142 dstA
== PIPE_BLENDFACTOR_INV_SRC_ALPHA
||
143 dstA
== PIPE_BLENDFACTOR_ONE
);
146 static boolean
blend_discard_if_src_alpha_color_1(unsigned srcRGB
, unsigned srcA
,
147 unsigned dstRGB
, unsigned dstA
)
149 /* If the blend equation is ADD or REVERSE_SUBTRACT,
150 * SRC_ALPHA_COLOR == (1,1,1,1), and the following state is set,
151 * the colorbuffer will not be changed.
152 * Notice that the dst factors are the src factors inverted. */
153 return (srcRGB
== PIPE_BLENDFACTOR_INV_SRC_COLOR
||
154 srcRGB
== PIPE_BLENDFACTOR_INV_SRC_ALPHA
||
155 srcRGB
== PIPE_BLENDFACTOR_ZERO
) &&
156 (srcA
== PIPE_BLENDFACTOR_INV_SRC_COLOR
||
157 srcA
== PIPE_BLENDFACTOR_INV_SRC_ALPHA
||
158 srcA
== PIPE_BLENDFACTOR_ZERO
) &&
159 (dstRGB
== PIPE_BLENDFACTOR_SRC_COLOR
||
160 dstRGB
== PIPE_BLENDFACTOR_SRC_ALPHA
||
161 dstRGB
== PIPE_BLENDFACTOR_ONE
) &&
162 (dstA
== PIPE_BLENDFACTOR_SRC_COLOR
||
163 dstA
== PIPE_BLENDFACTOR_SRC_ALPHA
||
164 dstA
== PIPE_BLENDFACTOR_ONE
);
167 static unsigned bgra_cmask(unsigned mask
)
169 /* Gallium uses RGBA color ordering while R300 expects BGRA. */
171 return ((mask
& PIPE_MASK_R
) << 2) |
172 ((mask
& PIPE_MASK_B
) >> 2) |
173 (mask
& (PIPE_MASK_G
| PIPE_MASK_A
));
176 /* Create a new blend state based on the CSO blend state.
178 * This encompasses alpha blending, logic/raster ops, and blend dithering. */
179 static void* r300_create_blend_state(struct pipe_context
* pipe
,
180 const struct pipe_blend_state
* state
)
182 struct r300_screen
* r300screen
= r300_screen(pipe
->screen
);
183 struct r300_blend_state
* blend
= CALLOC_STRUCT(r300_blend_state
);
185 if (state
->rt
[0].blend_enable
)
187 unsigned eqRGB
= state
->rt
[0].rgb_func
;
188 unsigned srcRGB
= state
->rt
[0].rgb_src_factor
;
189 unsigned dstRGB
= state
->rt
[0].rgb_dst_factor
;
191 unsigned eqA
= state
->rt
[0].alpha_func
;
192 unsigned srcA
= state
->rt
[0].alpha_src_factor
;
193 unsigned dstA
= state
->rt
[0].alpha_dst_factor
;
195 /* despite the name, ALPHA_BLEND_ENABLE has nothing to do with alpha,
196 * this is just the crappy D3D naming */
197 blend
->blend_control
= R300_ALPHA_BLEND_ENABLE
|
198 r300_translate_blend_function(eqRGB
) |
199 ( r300_translate_blend_factor(srcRGB
) << R300_SRC_BLEND_SHIFT
) |
200 ( r300_translate_blend_factor(dstRGB
) << R300_DST_BLEND_SHIFT
);
202 /* Optimization: some operations do not require the destination color.
204 * When SRC_ALPHA_SATURATE is used, colorbuffer reads must be enabled,
205 * otherwise blending gives incorrect results. It seems to be
207 if (eqRGB
== PIPE_BLEND_MIN
|| eqA
== PIPE_BLEND_MIN
||
208 eqRGB
== PIPE_BLEND_MAX
|| eqA
== PIPE_BLEND_MAX
||
209 dstRGB
!= PIPE_BLENDFACTOR_ZERO
||
210 dstA
!= PIPE_BLENDFACTOR_ZERO
||
211 srcRGB
== PIPE_BLENDFACTOR_DST_COLOR
||
212 srcRGB
== PIPE_BLENDFACTOR_DST_ALPHA
||
213 srcRGB
== PIPE_BLENDFACTOR_INV_DST_COLOR
||
214 srcRGB
== PIPE_BLENDFACTOR_INV_DST_ALPHA
||
215 srcA
== PIPE_BLENDFACTOR_DST_COLOR
||
216 srcA
== PIPE_BLENDFACTOR_DST_ALPHA
||
217 srcA
== PIPE_BLENDFACTOR_INV_DST_COLOR
||
218 srcA
== PIPE_BLENDFACTOR_INV_DST_ALPHA
||
219 srcRGB
== PIPE_BLENDFACTOR_SRC_ALPHA_SATURATE
) {
220 /* Enable reading from the colorbuffer. */
221 blend
->blend_control
|= R300_READ_ENABLE
;
223 if (r300_screen(r300_context(pipe
)->context
.screen
)->caps
->is_r500
) {
224 /* Optimization: Depending on incoming pixels, we can
225 * conditionally disable the reading in hardware... */
226 if (eqRGB
!= PIPE_BLEND_MIN
&& eqA
!= PIPE_BLEND_MIN
&&
227 eqRGB
!= PIPE_BLEND_MAX
&& eqA
!= PIPE_BLEND_MAX
) {
228 /* Disable reading if SRC_ALPHA == 0. */
229 if ((dstRGB
== PIPE_BLENDFACTOR_SRC_ALPHA
||
230 dstRGB
== PIPE_BLENDFACTOR_ZERO
) &&
231 (dstA
== PIPE_BLENDFACTOR_SRC_COLOR
||
232 dstA
== PIPE_BLENDFACTOR_SRC_ALPHA
||
233 dstA
== PIPE_BLENDFACTOR_ZERO
)) {
234 blend
->blend_control
|= R500_SRC_ALPHA_0_NO_READ
;
237 /* Disable reading if SRC_ALPHA == 1. */
238 if ((dstRGB
== PIPE_BLENDFACTOR_INV_SRC_ALPHA
||
239 dstRGB
== PIPE_BLENDFACTOR_ZERO
) &&
240 (dstA
== PIPE_BLENDFACTOR_INV_SRC_COLOR
||
241 dstA
== PIPE_BLENDFACTOR_INV_SRC_ALPHA
||
242 dstA
== PIPE_BLENDFACTOR_ZERO
)) {
243 blend
->blend_control
|= R500_SRC_ALPHA_1_NO_READ
;
249 /* Optimization: discard pixels which don't change the colorbuffer.
251 * The code below is non-trivial and some math is involved.
253 * Discarding pixels must be disabled when FP16 AA is enabled.
254 * This is a hardware bug. Also, this implementation wouldn't work
255 * with FP blending enabled and equation clamping disabled.
257 * Equations other than ADD are rarely used and therefore won't be
259 if ((eqRGB
== PIPE_BLEND_ADD
|| eqRGB
== PIPE_BLEND_REVERSE_SUBTRACT
) &&
260 (eqA
== PIPE_BLEND_ADD
|| eqA
== PIPE_BLEND_REVERSE_SUBTRACT
)) {
262 * REVERSE_SUBTRACT: Y-X
265 * If X = src*srcFactor = 0 and Y = dst*dstFactor = 1,
266 * then CB will not be changed.
268 * Given the srcFactor and dstFactor variables, we can derive
269 * what src and dst should be equal to and discard appropriate
272 if (blend_discard_if_src_alpha_0(srcRGB
, srcA
, dstRGB
, dstA
)) {
273 blend
->blend_control
|= R300_DISCARD_SRC_PIXELS_SRC_ALPHA_0
;
274 } else if (blend_discard_if_src_alpha_1(srcRGB
, srcA
,
276 blend
->blend_control
|= R300_DISCARD_SRC_PIXELS_SRC_ALPHA_1
;
277 } else if (blend_discard_if_src_color_0(srcRGB
, srcA
,
279 blend
->blend_control
|= R300_DISCARD_SRC_PIXELS_SRC_COLOR_0
;
280 } else if (blend_discard_if_src_color_1(srcRGB
, srcA
,
282 blend
->blend_control
|= R300_DISCARD_SRC_PIXELS_SRC_COLOR_1
;
283 } else if (blend_discard_if_src_alpha_color_0(srcRGB
, srcA
,
285 blend
->blend_control
|=
286 R300_DISCARD_SRC_PIXELS_SRC_ALPHA_COLOR_0
;
287 } else if (blend_discard_if_src_alpha_color_1(srcRGB
, srcA
,
289 blend
->blend_control
|=
290 R300_DISCARD_SRC_PIXELS_SRC_ALPHA_COLOR_1
;
295 if (srcA
!= srcRGB
|| dstA
!= dstRGB
|| eqA
!= eqRGB
) {
296 blend
->blend_control
|= R300_SEPARATE_ALPHA_ENABLE
;
297 blend
->alpha_blend_control
=
298 r300_translate_blend_function(eqA
) |
299 (r300_translate_blend_factor(srcA
) << R300_SRC_BLEND_SHIFT
) |
300 (r300_translate_blend_factor(dstA
) << R300_DST_BLEND_SHIFT
);
304 /* PIPE_LOGICOP_* don't need to be translated, fortunately. */
305 if (state
->logicop_enable
) {
306 blend
->rop
= R300_RB3D_ROPCNTL_ROP_ENABLE
|
307 (state
->logicop_func
) << R300_RB3D_ROPCNTL_ROP_SHIFT
;
310 /* Color channel masks for all MRTs. */
311 blend
->color_channel_mask
= bgra_cmask(state
->rt
[0].colormask
);
312 if (r300screen
->caps
->is_r500
&& state
->independent_blend_enable
) {
313 if (state
->rt
[1].blend_enable
) {
314 blend
->color_channel_mask
|= bgra_cmask(state
->rt
[1].colormask
) << 4;
316 if (state
->rt
[2].blend_enable
) {
317 blend
->color_channel_mask
|= bgra_cmask(state
->rt
[2].colormask
) << 8;
319 if (state
->rt
[3].blend_enable
) {
320 blend
->color_channel_mask
|= bgra_cmask(state
->rt
[3].colormask
) << 12;
325 blend
->dither
= R300_RB3D_DITHER_CTL_DITHER_MODE_LUT
|
326 R300_RB3D_DITHER_CTL_ALPHA_DITHER_MODE_LUT
;
332 /* Bind blend state. */
333 static void r300_bind_blend_state(struct pipe_context
* pipe
,
336 struct r300_context
* r300
= r300_context(pipe
);
338 UPDATE_STATE(state
, r300
->blend_state
);
341 /* Free blend state. */
342 static void r300_delete_blend_state(struct pipe_context
* pipe
,
348 /* Convert float to 10bit integer */
349 static unsigned float_to_fixed10(float f
)
351 return CLAMP((unsigned)(f
* 1023.9f
), 0, 1023);
355 * Setup both R300 and R500 registers, figure out later which one to write. */
356 static void r300_set_blend_color(struct pipe_context
* pipe
,
357 const struct pipe_blend_color
* color
)
359 struct r300_context
* r300
= r300_context(pipe
);
360 struct r300_screen
* r300screen
= r300_screen(pipe
->screen
);
361 struct r300_blend_color_state
* state
=
362 (struct r300_blend_color_state
*)r300
->blend_color_state
.state
;
365 util_pack_color(color
->color
, PIPE_FORMAT_B8G8R8A8_UNORM
, &uc
);
366 state
->blend_color
= uc
.ui
;
368 /* XXX if FP16 blending is enabled, we should use the FP16 format */
369 state
->blend_color_red_alpha
=
370 float_to_fixed10(color
->color
[0]) |
371 (float_to_fixed10(color
->color
[3]) << 16);
372 state
->blend_color_green_blue
=
373 float_to_fixed10(color
->color
[2]) |
374 (float_to_fixed10(color
->color
[1]) << 16);
376 r300
->blend_color_state
.size
= r300screen
->caps
->is_r500
? 3 : 2;
377 r300
->blend_color_state
.dirty
= TRUE
;
380 static void r300_set_clip_state(struct pipe_context
* pipe
,
381 const struct pipe_clip_state
* state
)
383 struct r300_context
* r300
= r300_context(pipe
);
387 if (r300_screen(pipe
->screen
)->caps
->has_tcl
) {
388 memcpy(r300
->clip_state
.state
, state
, sizeof(struct pipe_clip_state
));
389 r300
->clip_state
.size
= 29;
391 draw_flush(r300
->draw
);
392 draw_set_clip_state(r300
->draw
, state
);
393 r300
->clip_state
.size
= 2;
396 r300
->clip_state
.dirty
= TRUE
;
399 /* Create a new depth, stencil, and alpha state based on the CSO dsa state.
401 * This contains the depth buffer, stencil buffer, alpha test, and such.
402 * On the Radeon, depth and stencil buffer setup are intertwined, which is
403 * the reason for some of the strange-looking assignments across registers. */
405 r300_create_dsa_state(struct pipe_context
* pipe
,
406 const struct pipe_depth_stencil_alpha_state
* state
)
408 struct r300_capabilities
*caps
=
409 r300_screen(r300_context(pipe
)->context
.screen
)->caps
;
410 struct r300_dsa_state
* dsa
= CALLOC_STRUCT(r300_dsa_state
);
412 /* Depth test setup. */
413 if (state
->depth
.enabled
) {
414 dsa
->z_buffer_control
|= R300_Z_ENABLE
;
416 if (state
->depth
.writemask
) {
417 dsa
->z_buffer_control
|= R300_Z_WRITE_ENABLE
;
420 dsa
->z_stencil_control
|=
421 (r300_translate_depth_stencil_function(state
->depth
.func
) <<
425 /* Stencil buffer setup. */
426 if (state
->stencil
[0].enabled
) {
427 dsa
->z_buffer_control
|= R300_STENCIL_ENABLE
;
428 dsa
->z_stencil_control
|=
429 (r300_translate_depth_stencil_function(state
->stencil
[0].func
) <<
430 R300_S_FRONT_FUNC_SHIFT
) |
431 (r300_translate_stencil_op(state
->stencil
[0].fail_op
) <<
432 R300_S_FRONT_SFAIL_OP_SHIFT
) |
433 (r300_translate_stencil_op(state
->stencil
[0].zpass_op
) <<
434 R300_S_FRONT_ZPASS_OP_SHIFT
) |
435 (r300_translate_stencil_op(state
->stencil
[0].zfail_op
) <<
436 R300_S_FRONT_ZFAIL_OP_SHIFT
);
438 dsa
->stencil_ref_mask
=
439 (state
->stencil
[0].valuemask
<< R300_STENCILMASK_SHIFT
) |
440 (state
->stencil
[0].writemask
<< R300_STENCILWRITEMASK_SHIFT
);
442 if (state
->stencil
[1].enabled
) {
443 dsa
->z_buffer_control
|= R300_STENCIL_FRONT_BACK
;
444 dsa
->z_stencil_control
|=
445 (r300_translate_depth_stencil_function(state
->stencil
[1].func
) <<
446 R300_S_BACK_FUNC_SHIFT
) |
447 (r300_translate_stencil_op(state
->stencil
[1].fail_op
) <<
448 R300_S_BACK_SFAIL_OP_SHIFT
) |
449 (r300_translate_stencil_op(state
->stencil
[1].zpass_op
) <<
450 R300_S_BACK_ZPASS_OP_SHIFT
) |
451 (r300_translate_stencil_op(state
->stencil
[1].zfail_op
) <<
452 R300_S_BACK_ZFAIL_OP_SHIFT
);
456 dsa
->z_buffer_control
|= R500_STENCIL_REFMASK_FRONT_BACK
;
457 dsa
->stencil_ref_bf
=
458 (state
->stencil
[1].valuemask
<<
459 R300_STENCILMASK_SHIFT
) |
460 (state
->stencil
[1].writemask
<<
461 R300_STENCILWRITEMASK_SHIFT
);
466 /* Alpha test setup. */
467 if (state
->alpha
.enabled
) {
468 dsa
->alpha_function
=
469 r300_translate_alpha_function(state
->alpha
.func
) |
470 R300_FG_ALPHA_FUNC_ENABLE
;
472 /* We could use 10bit alpha ref but who needs that? */
473 dsa
->alpha_function
|= float_to_ubyte(state
->alpha
.ref_value
);
476 dsa
->alpha_function
|= R500_FG_ALPHA_FUNC_8BIT
;
482 /* Bind DSA state. */
483 static void r300_bind_dsa_state(struct pipe_context
* pipe
,
486 struct r300_context
* r300
= r300_context(pipe
);
488 UPDATE_STATE(state
, r300
->dsa_state
);
491 /* Free DSA state. */
492 static void r300_delete_dsa_state(struct pipe_context
* pipe
,
498 static void r300_set_stencil_ref(struct pipe_context
* pipe
,
499 const struct pipe_stencil_ref
* sr
)
501 struct r300_context
* r300
= r300_context(pipe
);
502 r300
->stencil_ref
= *sr
;
503 r300
->dsa_state
.dirty
= TRUE
;
506 /* This switcheroo is needed just because of goddamned MACRO_SWITCH. */
507 static void r300_fb_update_tiling_flags(struct r300_context
*r300
,
508 const struct pipe_framebuffer_state
*old_state
,
509 const struct pipe_framebuffer_state
*new_state
)
511 struct r300_texture
*tex
;
512 unsigned i
, j
, level
;
514 /* Reset tiling flags for old surfaces to default values. */
515 for (i
= 0; i
< old_state
->nr_cbufs
; i
++) {
516 for (j
= 0; j
< new_state
->nr_cbufs
; j
++) {
517 if (old_state
->cbufs
[i
]->texture
== new_state
->cbufs
[j
]->texture
) {
521 /* If not binding the surface again... */
522 if (j
!= new_state
->nr_cbufs
) {
526 tex
= (struct r300_texture
*)old_state
->cbufs
[i
]->texture
;
529 r300
->rws
->buffer_set_tiling(r300
->rws
, tex
->buffer
,
532 tex
->macrotile
!= 0);
535 if (old_state
->zsbuf
&&
536 (!new_state
->zsbuf
||
537 old_state
->zsbuf
->texture
!= new_state
->zsbuf
->texture
)) {
538 tex
= (struct r300_texture
*)old_state
->zsbuf
->texture
;
541 r300
->rws
->buffer_set_tiling(r300
->rws
, tex
->buffer
,
544 tex
->macrotile
!= 0);
548 /* Set tiling flags for new surfaces. */
549 for (i
= 0; i
< new_state
->nr_cbufs
; i
++) {
550 tex
= (struct r300_texture
*)new_state
->cbufs
[i
]->texture
;
551 level
= new_state
->cbufs
[i
]->level
;
553 r300
->rws
->buffer_set_tiling(r300
->rws
, tex
->buffer
,
556 tex
->mip_macrotile
[level
] != 0);
558 if (new_state
->zsbuf
) {
559 tex
= (struct r300_texture
*)new_state
->zsbuf
->texture
;
560 level
= new_state
->zsbuf
->level
;
562 r300
->rws
->buffer_set_tiling(r300
->rws
, tex
->buffer
,
565 tex
->mip_macrotile
[level
] != 0);
570 r300_set_framebuffer_state(struct pipe_context
* pipe
,
571 const struct pipe_framebuffer_state
* state
)
573 struct r300_context
* r300
= r300_context(pipe
);
574 struct r300_screen
* r300screen
= r300_screen(pipe
->screen
);
575 struct pipe_framebuffer_state
*old_state
= r300
->fb_state
.state
;
576 unsigned max_width
, max_height
;
577 uint32_t zbuffer_bpp
= 0;
580 if (state
->nr_cbufs
> 4) {
581 debug_printf("r300: Implementation error: Too many MRTs in %s, "
582 "refusing to bind framebuffer state!\n", __FUNCTION__
);
586 if (r300screen
->caps
->is_r500
) {
587 max_width
= max_height
= 4096;
588 } else if (r300screen
->caps
->is_r400
) {
589 max_width
= max_height
= 4021;
591 max_width
= max_height
= 2560;
594 if (state
->width
> max_width
|| state
->height
> max_height
) {
595 debug_printf("r300: Implementation error: Render targets are too "
596 "big in %s, refusing to bind framebuffer state!\n", __FUNCTION__
);
601 draw_flush(r300
->draw
);
604 r300
->fb_state
.dirty
= TRUE
;
606 /* If nr_cbufs is changed from zero to non-zero or vice versa... */
607 if (!!old_state
->nr_cbufs
!= !!state
->nr_cbufs
) {
608 r300
->blend_state
.dirty
= TRUE
;
610 /* If zsbuf is set from NULL to non-NULL or vice versa.. */
611 if (!!old_state
->zsbuf
!= !!state
->zsbuf
) {
612 r300
->dsa_state
.dirty
= TRUE
;
614 if (!r300
->scissor_enabled
) {
615 r300
->scissor_state
.dirty
= TRUE
;
618 r300_fb_update_tiling_flags(r300
, r300
->fb_state
.state
, state
);
620 memcpy(r300
->fb_state
.state
, state
, sizeof(struct pipe_framebuffer_state
));
622 r300
->fb_state
.size
= (10 * state
->nr_cbufs
) + (2 * (4 - state
->nr_cbufs
)) +
623 (state
->zsbuf
? 10 : 0) + 8;
625 /* Polygon offset depends on the zbuffer bit depth. */
626 if (state
->zsbuf
&& r300
->polygon_offset_enabled
) {
627 switch (util_format_get_blocksize(state
->zsbuf
->texture
->format
)) {
636 if (r300
->zbuffer_bpp
!= zbuffer_bpp
) {
637 r300
->zbuffer_bpp
= zbuffer_bpp
;
638 r300
->rs_state
.dirty
= TRUE
;
643 /* Create fragment shader state. */
644 static void* r300_create_fs_state(struct pipe_context
* pipe
,
645 const struct pipe_shader_state
* shader
)
647 struct r300_fragment_shader
* fs
= NULL
;
649 fs
= (struct r300_fragment_shader
*)CALLOC_STRUCT(r300_fragment_shader
);
651 /* Copy state directly into shader. */
653 fs
->state
.tokens
= tgsi_dup_tokens(shader
->tokens
);
655 tgsi_scan_shader(shader
->tokens
, &fs
->info
);
656 r300_shader_read_fs_inputs(&fs
->info
, &fs
->inputs
);
661 /* Bind fragment shader state. */
662 static void r300_bind_fs_state(struct pipe_context
* pipe
, void* shader
)
664 struct r300_context
* r300
= r300_context(pipe
);
665 struct r300_fragment_shader
* fs
= (struct r300_fragment_shader
*)shader
;
673 r300_pick_fragment_shader(r300
);
675 r300
->rs_block_state
.dirty
= TRUE
; /* Will be updated before the emission. */
677 if (r300
->vs_state
.state
&& r300_vertex_shader_setup_wpos(r300
)) {
678 r300
->vap_output_state
.dirty
= TRUE
;
681 r300
->dirty_state
|= R300_NEW_FRAGMENT_SHADER
| R300_NEW_FRAGMENT_SHADER_CONSTANTS
;
684 /* Delete fragment shader state. */
685 static void r300_delete_fs_state(struct pipe_context
* pipe
, void* shader
)
687 struct r300_fragment_shader
* fs
= (struct r300_fragment_shader
*)shader
;
688 struct r300_fragment_shader_code
*tmp
, *ptr
= fs
->first
;
693 rc_constants_destroy(&tmp
->code
.constants
);
696 FREE((void*)fs
->state
.tokens
);
700 static void r300_set_polygon_stipple(struct pipe_context
* pipe
,
701 const struct pipe_poly_stipple
* state
)
703 /* XXX no idea how to set this up, but not terribly important */
706 /* Create a new rasterizer state based on the CSO rasterizer state.
708 * This is a very large chunk of state, and covers most of the graphics
709 * backend (GB), geometry assembly (GA), and setup unit (SU) blocks.
711 * In a not entirely unironic sidenote, this state has nearly nothing to do
712 * with the actual block on the Radeon called the rasterizer (RS). */
713 static void* r300_create_rs_state(struct pipe_context
* pipe
,
714 const struct pipe_rasterizer_state
* state
)
716 struct r300_screen
* r300screen
= r300_screen(pipe
->screen
);
717 struct r300_rs_state
* rs
= CALLOC_STRUCT(r300_rs_state
);
719 /* Copy rasterizer state for Draw. */
722 #ifdef PIPE_ARCH_LITTLE_ENDIAN
723 rs
->vap_control_status
= R300_VC_NO_SWAP
;
725 rs
->vap_control_status
= R300_VC_32BIT_SWAP
;
728 /* If no TCL engine is present, turn off the HW TCL. */
729 if (!r300screen
->caps
->has_tcl
) {
730 rs
->vap_control_status
|= R300_VAP_TCL_BYPASS
;
733 rs
->point_size
= pack_float_16_6x(state
->point_size
) |
734 (pack_float_16_6x(state
->point_size
) << R300_POINTSIZE_X_SHIFT
);
736 rs
->line_control
= pack_float_16_6x(state
->line_width
) |
737 R300_GA_LINE_CNTL_END_TYPE_COMP
;
739 /* Enable polygon mode */
740 if (state
->fill_cw
!= PIPE_POLYGON_MODE_FILL
||
741 state
->fill_ccw
!= PIPE_POLYGON_MODE_FILL
) {
742 rs
->polygon_mode
= R300_GA_POLY_MODE_DUAL
;
745 /* Radeons don't think in "CW/CCW", they think in "front/back". */
746 if (state
->front_winding
== PIPE_WINDING_CW
) {
747 rs
->cull_mode
= R300_FRONT_FACE_CW
;
750 if (state
->offset_cw
) {
751 rs
->polygon_offset_enable
|= R300_FRONT_ENABLE
;
753 if (state
->offset_ccw
) {
754 rs
->polygon_offset_enable
|= R300_BACK_ENABLE
;
758 if (rs
->polygon_mode
) {
760 r300_translate_polygon_mode_front(state
->fill_cw
);
762 r300_translate_polygon_mode_back(state
->fill_ccw
);
765 rs
->cull_mode
= R300_FRONT_FACE_CCW
;
768 if (state
->offset_ccw
) {
769 rs
->polygon_offset_enable
|= R300_FRONT_ENABLE
;
771 if (state
->offset_cw
) {
772 rs
->polygon_offset_enable
|= R300_BACK_ENABLE
;
776 if (rs
->polygon_mode
) {
778 r300_translate_polygon_mode_front(state
->fill_ccw
);
780 r300_translate_polygon_mode_back(state
->fill_cw
);
783 if (state
->front_winding
& state
->cull_mode
) {
784 rs
->cull_mode
|= R300_CULL_FRONT
;
786 if (~(state
->front_winding
) & state
->cull_mode
) {
787 rs
->cull_mode
|= R300_CULL_BACK
;
790 if (rs
->polygon_offset_enable
) {
791 rs
->depth_offset
= state
->offset_units
;
792 rs
->depth_scale
= state
->offset_scale
;
795 if (state
->line_stipple_enable
) {
796 rs
->line_stipple_config
=
797 R300_GA_LINE_STIPPLE_CONFIG_LINE_RESET_LINE
|
798 (fui((float)state
->line_stipple_factor
) &
799 R300_GA_LINE_STIPPLE_CONFIG_STIPPLE_SCALE_MASK
);
800 /* XXX this might need to be scaled up */
801 rs
->line_stipple_value
= state
->line_stipple_pattern
;
804 if (state
->flatshade
) {
805 rs
->color_control
= R300_SHADE_MODEL_FLAT
;
807 rs
->color_control
= R300_SHADE_MODEL_SMOOTH
;
813 /* Bind rasterizer state. */
814 static void r300_bind_rs_state(struct pipe_context
* pipe
, void* state
)
816 struct r300_context
* r300
= r300_context(pipe
);
817 struct r300_rs_state
* rs
= (struct r300_rs_state
*)state
;
818 boolean scissor_was_enabled
= r300
->scissor_enabled
;
821 draw_flush(r300
->draw
);
822 draw_set_rasterizer_state(r300
->draw
, &rs
->rs
);
826 r300
->polygon_offset_enabled
= rs
->rs
.offset_cw
|| rs
->rs
.offset_ccw
;
827 r300
->scissor_enabled
= rs
->rs
.scissor
;
829 r300
->polygon_offset_enabled
= FALSE
;
830 r300
->scissor_enabled
= FALSE
;
833 UPDATE_STATE(state
, r300
->rs_state
);
834 r300
->rs_state
.size
= 17 + (r300
->polygon_offset_enabled
? 5 : 0);
836 if (scissor_was_enabled
!= r300
->scissor_enabled
) {
837 r300
->scissor_state
.dirty
= TRUE
;
841 /* Free rasterizer state. */
842 static void r300_delete_rs_state(struct pipe_context
* pipe
, void* state
)
848 r300_create_sampler_state(struct pipe_context
* pipe
,
849 const struct pipe_sampler_state
* state
)
851 struct r300_context
* r300
= r300_context(pipe
);
852 struct r300_sampler_state
* sampler
= CALLOC_STRUCT(r300_sampler_state
);
853 boolean is_r500
= r300_screen(pipe
->screen
)->caps
->is_r500
;
857 sampler
->state
= *state
;
860 (r300_translate_wrap(state
->wrap_s
) << R300_TX_WRAP_S_SHIFT
) |
861 (r300_translate_wrap(state
->wrap_t
) << R300_TX_WRAP_T_SHIFT
) |
862 (r300_translate_wrap(state
->wrap_r
) << R300_TX_WRAP_R_SHIFT
);
864 sampler
->filter0
|= r300_translate_tex_filters(state
->min_img_filter
,
865 state
->mag_img_filter
,
866 state
->min_mip_filter
,
867 state
->max_anisotropy
> 0);
869 sampler
->filter0
|= r300_anisotropy(state
->max_anisotropy
);
871 /* Unfortunately, r300-r500 don't support floating-point mipmap lods. */
872 /* We must pass these to the merge function to clamp them properly. */
873 sampler
->min_lod
= MAX2((unsigned)state
->min_lod
, 0);
874 sampler
->max_lod
= MAX2((unsigned)ceilf(state
->max_lod
), 0);
876 lod_bias
= CLAMP((int)(state
->lod_bias
* 32), -(1 << 9), (1 << 9) - 1);
878 sampler
->filter1
|= lod_bias
<< R300_LOD_BIAS_SHIFT
;
880 /* This is very high quality anisotropic filtering for R5xx.
881 * It's good for benchmarking the performance of texturing but
882 * in practice we don't want to slow down the driver because it's
883 * a pretty good performance killer. Feel free to play with it. */
884 if (DBG_ON(r300
, DBG_ANISOHQ
) && is_r500
) {
885 sampler
->filter1
|= r500_anisotropy(state
->max_anisotropy
);
888 util_pack_color(state
->border_color
, PIPE_FORMAT_B8G8R8A8_UNORM
, &uc
);
889 sampler
->border_color
= uc
.ui
;
891 /* R500-specific fixups and optimizations */
892 if (r300_screen(r300
->context
.screen
)->caps
->is_r500
) {
893 sampler
->filter1
|= R500_BORDER_FIX
;
896 return (void*)sampler
;
899 static void r300_bind_sampler_states(struct pipe_context
* pipe
,
903 struct r300_context
* r300
= r300_context(pipe
);
904 struct r300_textures_state
* state
=
905 (struct r300_textures_state
*)r300
->textures_state
.state
;
911 memcpy(state
->sampler_states
, states
, sizeof(void*) * count
);
912 state
->sampler_count
= count
;
914 r300
->textures_state
.dirty
= TRUE
;
916 /* Pick a fragment shader based on the texture compare state. */
917 if (r300
->fs
&& count
) {
918 if (r300_pick_fragment_shader(r300
)) {
919 r300
->dirty_state
|= R300_NEW_FRAGMENT_SHADER
|
920 R300_NEW_FRAGMENT_SHADER_CONSTANTS
;
925 static void r300_lacks_vertex_textures(struct pipe_context
* pipe
,
931 static void r300_delete_sampler_state(struct pipe_context
* pipe
, void* state
)
936 static void r300_set_fragment_sampler_views(struct pipe_context
* pipe
,
938 struct pipe_sampler_view
** views
)
940 struct r300_context
* r300
= r300_context(pipe
);
942 boolean is_r500
= r300_screen(r300
->context
.screen
)->caps
->is_r500
;
943 boolean dirty_tex
= FALSE
;
950 for (i
= 0; i
< count
; i
++) {
951 if (r300
->fragment_sampler_views
[i
] != views
[i
]) {
952 struct r300_texture
*texture
;
954 pipe_sampler_view_reference(&r300
->fragment_sampler_views
[i
],
958 texture
= (struct r300_texture
*)views
[i
]->texture
;
960 /* R300-specific - set the texrect factor in a fragment shader */
961 if (!is_r500
&& texture
->is_npot
) {
962 /* XXX It would be nice to re-emit just 1 constant,
963 * XXX not all of them */
964 r300
->dirty_state
|= R300_NEW_FRAGMENT_SHADER_CONSTANTS
;
969 for (i
= count
; i
< 8; i
++) {
970 if (r300
->fragment_sampler_views
[i
]) {
971 pipe_sampler_view_reference(&r300
->fragment_sampler_views
[i
],
976 r300
->fragment_sampler_view_count
= count
;
978 r300
->textures_state
.dirty
= TRUE
;
981 r300
->texture_cache_inval
.dirty
= TRUE
;
985 static struct pipe_sampler_view
*
986 r300_create_sampler_view(struct pipe_context
*pipe
,
987 struct pipe_texture
*texture
,
988 const struct pipe_sampler_view
*templ
)
990 struct r300_context
*r300
= r300_context(pipe
);
991 struct pipe_sampler_view
*view
= CALLOC_STRUCT(pipe_sampler_view
);
995 view
->reference
.count
= 1;
996 view
->texture
= NULL
;
997 pipe_texture_reference(&view
->texture
, texture
);
998 view
->context
= pipe
;
1006 r300_sampler_view_destroy(struct pipe_context
*pipe
,
1007 struct pipe_sampler_view
*view
)
1009 pipe_texture_reference(&view
->texture
, NULL
);
1013 static void r300_set_scissor_state(struct pipe_context
* pipe
,
1014 const struct pipe_scissor_state
* state
)
1016 struct r300_context
* r300
= r300_context(pipe
);
1018 memcpy(r300
->scissor_state
.state
, state
,
1019 sizeof(struct pipe_scissor_state
));
1021 if (r300
->scissor_enabled
) {
1022 r300
->scissor_state
.dirty
= TRUE
;
1026 static void r300_set_viewport_state(struct pipe_context
* pipe
,
1027 const struct pipe_viewport_state
* state
)
1029 struct r300_context
* r300
= r300_context(pipe
);
1030 struct r300_viewport_state
* viewport
=
1031 (struct r300_viewport_state
*)r300
->viewport_state
.state
;
1033 r300
->viewport
= *state
;
1035 /* Do the transform in HW. */
1036 viewport
->vte_control
= R300_VTX_W0_FMT
;
1038 if (state
->scale
[0] != 1.0f
) {
1039 viewport
->xscale
= state
->scale
[0];
1040 viewport
->vte_control
|= R300_VPORT_X_SCALE_ENA
;
1042 if (state
->scale
[1] != 1.0f
) {
1043 viewport
->yscale
= state
->scale
[1];
1044 viewport
->vte_control
|= R300_VPORT_Y_SCALE_ENA
;
1046 if (state
->scale
[2] != 1.0f
) {
1047 viewport
->zscale
= state
->scale
[2];
1048 viewport
->vte_control
|= R300_VPORT_Z_SCALE_ENA
;
1050 if (state
->translate
[0] != 0.0f
) {
1051 viewport
->xoffset
= state
->translate
[0];
1052 viewport
->vte_control
|= R300_VPORT_X_OFFSET_ENA
;
1054 if (state
->translate
[1] != 0.0f
) {
1055 viewport
->yoffset
= state
->translate
[1];
1056 viewport
->vte_control
|= R300_VPORT_Y_OFFSET_ENA
;
1058 if (state
->translate
[2] != 0.0f
) {
1059 viewport
->zoffset
= state
->translate
[2];
1060 viewport
->vte_control
|= R300_VPORT_Z_OFFSET_ENA
;
1063 r300
->viewport_state
.dirty
= TRUE
;
1064 if (r300
->fs
&& r300
->fs
->inputs
.wpos
!= ATTR_UNUSED
) {
1065 r300
->dirty_state
|= R300_NEW_FRAGMENT_SHADER_CONSTANTS
;
1069 static void r300_set_vertex_buffers(struct pipe_context
* pipe
,
1071 const struct pipe_vertex_buffer
* buffers
)
1073 struct r300_context
* r300
= r300_context(pipe
);
1075 unsigned max_index
= (1 << 24) - 1;
1076 boolean any_user_buffer
= false;
1078 if (count
== r300
->vertex_buffer_count
&&
1079 memcmp(r300
->vertex_buffer
, buffers
, count
* sizeof(buffers
[0])) == 0)
1082 for (i
= 0; i
< count
; i
++) {
1083 pipe_buffer_reference(&r300
->vertex_buffer
[i
].buffer
, buffers
[i
].buffer
);
1084 if (r300_buffer_is_user_buffer(buffers
[i
].buffer
))
1085 any_user_buffer
= true;
1086 max_index
= MIN2(buffers
[i
].max_index
, max_index
);
1089 for ( ; i
< r300
->vertex_buffer_count
; i
++)
1090 pipe_buffer_reference(&r300
->vertex_buffer
[i
].buffer
, NULL
);
1092 memcpy(r300
->vertex_buffer
, buffers
,
1093 sizeof(struct pipe_vertex_buffer
) * count
);
1095 r300
->vertex_buffer_count
= count
;
1096 r300
->vertex_buffer_max_index
= max_index
;
1097 r300
->any_user_vbs
= any_user_buffer
;
1100 draw_flush(r300
->draw
);
1101 draw_set_vertex_buffers(r300
->draw
, count
, buffers
);
1105 static boolean
r300_validate_aos(struct r300_context
*r300
)
1107 struct pipe_vertex_buffer
*vbuf
= r300
->vertex_buffer
;
1108 struct pipe_vertex_element
*velem
= r300
->velems
->velem
;
1111 /* Check if formats and strides are aligned to the size of DWORD. */
1112 for (i
= 0; i
< r300
->velems
->count
; i
++) {
1113 if (vbuf
[velem
[i
].vertex_buffer_index
].stride
% 4 != 0 ||
1114 util_format_get_blocksize(velem
[i
].src_format
) % 4 != 0) {
1121 static void r300_draw_emit_attrib(struct r300_context
* r300
,
1122 enum attrib_emit emit
,
1123 enum interp_mode interp
,
1126 struct r300_vertex_shader
* vs
= r300
->vs_state
.state
;
1127 struct tgsi_shader_info
* info
= &vs
->info
;
1130 output
= draw_find_shader_output(r300
->draw
,
1131 info
->output_semantic_name
[index
],
1132 info
->output_semantic_index
[index
]);
1133 draw_emit_vertex_attr(&r300
->vertex_info
, emit
, interp
, output
);
1136 static void r300_draw_emit_all_attribs(struct r300_context
* r300
)
1138 struct r300_vertex_shader
* vs
= r300
->vs_state
.state
;
1139 struct r300_shader_semantics
* vs_outputs
= &vs
->outputs
;
1143 if (vs_outputs
->pos
!= ATTR_UNUSED
) {
1144 r300_draw_emit_attrib(r300
, EMIT_4F
, INTERP_PERSPECTIVE
,
1151 if (vs_outputs
->psize
!= ATTR_UNUSED
) {
1152 r300_draw_emit_attrib(r300
, EMIT_1F_PSIZE
, INTERP_POS
,
1157 for (i
= 0; i
< ATTR_COLOR_COUNT
; i
++) {
1158 if (vs_outputs
->color
[i
] != ATTR_UNUSED
) {
1159 r300_draw_emit_attrib(r300
, EMIT_4F
, INTERP_LINEAR
,
1160 vs_outputs
->color
[i
]);
1164 /* XXX Back-face colors. */
1166 /* Texture coordinates. */
1168 for (i
= 0; i
< ATTR_GENERIC_COUNT
; i
++) {
1169 if (vs_outputs
->generic
[i
] != ATTR_UNUSED
) {
1170 r300_draw_emit_attrib(r300
, EMIT_4F
, INTERP_PERSPECTIVE
,
1171 vs_outputs
->generic
[i
]);
1176 /* Fog coordinates. */
1177 if (vs_outputs
->fog
!= ATTR_UNUSED
) {
1178 r300_draw_emit_attrib(r300
, EMIT_4F
, INTERP_PERSPECTIVE
,
1184 assert(gen_count
<= 8);
1187 /* Update the PSC tables. */
1188 static void r300_vertex_psc(struct r300_vertex_element_state
*velems
)
1190 struct r300_vertex_stream_state
*vstream
= &velems
->vertex_stream
;
1191 uint16_t type
, swizzle
;
1192 enum pipe_format format
;
1195 assert(velems
->count
<= 16);
1197 /* Vertex shaders have no semantics on their inputs,
1198 * so PSC should just route stuff based on the vertex elements,
1199 * and not on attrib information. */
1200 for (i
= 0; i
< velems
->count
; i
++) {
1201 format
= velems
->velem
[i
].src_format
;
1203 type
= r300_translate_vertex_data_type(format
) |
1204 (i
<< R300_DST_VEC_LOC_SHIFT
);
1205 swizzle
= r300_translate_vertex_data_swizzle(format
);
1208 vstream
->vap_prog_stream_cntl
[i
>> 1] |= type
<< 16;
1209 vstream
->vap_prog_stream_cntl_ext
[i
>> 1] |= swizzle
<< 16;
1211 vstream
->vap_prog_stream_cntl
[i
>> 1] |= type
;
1212 vstream
->vap_prog_stream_cntl_ext
[i
>> 1] |= swizzle
;
1216 /* Set the last vector in the PSC. */
1220 vstream
->vap_prog_stream_cntl
[i
>> 1] |=
1221 (R300_LAST_VEC
<< (i
& 1 ? 16 : 0));
1223 vstream
->count
= (i
>> 1) + 1;
1226 /* Update the PSC tables for SW TCL, using Draw. */
1227 static void r300_swtcl_vertex_psc(struct r300_context
*r300
,
1228 struct r300_vertex_element_state
*velems
)
1230 struct r300_vertex_stream_state
*vstream
= &velems
->vertex_stream
;
1231 struct r300_vertex_shader
* vs
= r300
->vs_state
.state
;
1232 struct vertex_info
* vinfo
= &r300
->vertex_info
;
1233 uint16_t type
, swizzle
;
1234 enum pipe_format format
;
1235 unsigned i
, attrib_count
;
1236 int* vs_output_tab
= vs
->stream_loc_notcl
;
1238 /* For each Draw attribute, route it to the fragment shader according
1239 * to the vs_output_tab. */
1240 attrib_count
= vinfo
->num_attribs
;
1241 DBG(r300
, DBG_DRAW
, "r300: attrib count: %d\n", attrib_count
);
1242 for (i
= 0; i
< attrib_count
; i
++) {
1243 DBG(r300
, DBG_DRAW
, "r300: attrib: offset %d, interp %d, size %d,"
1244 " vs_output_tab %d\n", vinfo
->attrib
[i
].src_index
,
1245 vinfo
->attrib
[i
].interp_mode
, vinfo
->attrib
[i
].emit
,
1249 for (i
= 0; i
< attrib_count
; i
++) {
1250 /* Make sure we have a proper destination for our attribute. */
1251 assert(vs_output_tab
[i
] != -1);
1253 format
= draw_translate_vinfo_format(vinfo
->attrib
[i
].emit
);
1255 /* Obtain the type of data in this attribute. */
1256 type
= r300_translate_vertex_data_type(format
) |
1257 vs_output_tab
[i
] << R300_DST_VEC_LOC_SHIFT
;
1259 /* Obtain the swizzle for this attribute. Note that the default
1260 * swizzle in the hardware is not XYZW! */
1261 swizzle
= r300_translate_vertex_data_swizzle(format
);
1263 /* Add the attribute to the PSC table. */
1265 vstream
->vap_prog_stream_cntl
[i
>> 1] |= type
<< 16;
1266 vstream
->vap_prog_stream_cntl_ext
[i
>> 1] |= swizzle
<< 16;
1268 vstream
->vap_prog_stream_cntl
[i
>> 1] |= type
;
1269 vstream
->vap_prog_stream_cntl_ext
[i
>> 1] |= swizzle
;
1273 /* Set the last vector in the PSC. */
1277 vstream
->vap_prog_stream_cntl
[i
>> 1] |=
1278 (R300_LAST_VEC
<< (i
& 1 ? 16 : 0));
1280 vstream
->count
= (i
>> 1) + 1;
1283 static void* r300_create_vertex_elements_state(struct pipe_context
* pipe
,
1285 const struct pipe_vertex_element
* attribs
)
1287 struct r300_context
*r300
= r300_context(pipe
);
1288 struct r300_screen
* r300screen
= r300_screen(pipe
->screen
);
1289 struct r300_vertex_element_state
*velems
;
1291 assert(count
<= PIPE_MAX_ATTRIBS
);
1292 velems
= CALLOC_STRUCT(r300_vertex_element_state
);
1293 if (velems
!= NULL
) {
1294 velems
->count
= count
;
1295 memcpy(velems
->velem
, attribs
, sizeof(struct pipe_vertex_element
) * count
);
1297 if (r300screen
->caps
->has_tcl
) {
1298 r300_vertex_psc(velems
);
1300 memset(&r300
->vertex_info
, 0, sizeof(struct vertex_info
));
1301 r300_draw_emit_all_attribs(r300
);
1302 draw_compute_vertex_size(&r300
->vertex_info
);
1303 r300_swtcl_vertex_psc(r300
, velems
);
1309 static void r300_bind_vertex_elements_state(struct pipe_context
*pipe
,
1312 struct r300_context
*r300
= r300_context(pipe
);
1313 struct r300_vertex_element_state
*velems
= state
;
1315 if (velems
== NULL
) {
1319 r300
->velems
= velems
;
1322 draw_flush(r300
->draw
);
1323 draw_set_vertex_elements(r300
->draw
, velems
->count
, velems
->velem
);
1326 if (!r300_validate_aos(r300
)) {
1327 /* XXX We should fallback using draw. */
1332 UPDATE_STATE(&velems
->vertex_stream
, r300
->vertex_stream_state
);
1333 r300
->vertex_stream_state
.size
= (1 + velems
->vertex_stream
.count
) * 2;
1336 static void r300_delete_vertex_elements_state(struct pipe_context
*pipe
, void *state
)
1341 static void* r300_create_vs_state(struct pipe_context
* pipe
,
1342 const struct pipe_shader_state
* shader
)
1344 struct r300_context
* r300
= r300_context(pipe
);
1346 struct r300_vertex_shader
* vs
= CALLOC_STRUCT(r300_vertex_shader
);
1347 r300_vertex_shader_common_init(vs
, shader
);
1349 if (r300_screen(pipe
->screen
)->caps
->has_tcl
) {
1350 r300_translate_vertex_shader(r300
, vs
);
1352 vs
->draw_vs
= draw_create_vertex_shader(r300
->draw
, shader
);
1358 static void r300_bind_vs_state(struct pipe_context
* pipe
, void* shader
)
1360 struct r300_context
* r300
= r300_context(pipe
);
1361 struct r300_vertex_shader
* vs
= (struct r300_vertex_shader
*)shader
;
1364 r300
->vs_state
.state
= NULL
;
1367 if (vs
== r300
->vs_state
.state
) {
1370 r300
->vs_state
.state
= vs
;
1372 // VS output mapping for HWTCL or stream mapping for SWTCL to the RS block
1374 r300_vertex_shader_setup_wpos(r300
);
1376 memcpy(r300
->vap_output_state
.state
, &vs
->vap_out
,
1377 sizeof(struct r300_vap_output_state
));
1378 r300
->vap_output_state
.dirty
= TRUE
;
1380 /* The majority of the RS block bits is dependent on the vertex shader. */
1381 r300
->rs_block_state
.dirty
= TRUE
; /* Will be updated before the emission. */
1383 if (r300_screen(pipe
->screen
)->caps
->has_tcl
) {
1384 r300
->vs_state
.dirty
= TRUE
;
1385 r300
->vs_state
.size
= vs
->code
.length
+ 9;
1387 r300
->pvs_flush
.dirty
= TRUE
;
1389 r300
->dirty_state
|= R300_NEW_VERTEX_SHADER_CONSTANTS
;
1391 draw_flush(r300
->draw
);
1392 draw_bind_vertex_shader(r300
->draw
,
1393 (struct draw_vertex_shader
*)vs
->draw_vs
);
1397 static void r300_delete_vs_state(struct pipe_context
* pipe
, void* shader
)
1399 struct r300_context
* r300
= r300_context(pipe
);
1400 struct r300_vertex_shader
* vs
= (struct r300_vertex_shader
*)shader
;
1402 if (r300_screen(pipe
->screen
)->caps
->has_tcl
) {
1403 rc_constants_destroy(&vs
->code
.constants
);
1405 draw_delete_vertex_shader(r300
->draw
,
1406 (struct draw_vertex_shader
*)vs
->draw_vs
);
1409 FREE((void*)vs
->state
.tokens
);
1413 static void r300_set_constant_buffer(struct pipe_context
*pipe
,
1414 uint shader
, uint index
,
1415 struct pipe_buffer
*buf
)
1417 struct r300_context
* r300
= r300_context(pipe
);
1418 struct r300_screen
*r300screen
= r300_screen(pipe
->screen
);
1422 if (buf
== NULL
|| buf
->size
== 0 ||
1423 (mapped
= pipe_buffer_map(pipe
->screen
, buf
, PIPE_BUFFER_USAGE_CPU_READ
)) == NULL
)
1425 r300
->shader_constants
[shader
].count
= 0;
1429 assert((buf
->size
% 4 * sizeof(float)) == 0);
1431 /* Check the size of the constant buffer. */
1433 case PIPE_SHADER_VERTEX
:
1436 case PIPE_SHADER_FRAGMENT
:
1437 if (r300screen
->caps
->is_r500
) {
1439 /* XXX Implement emission of r400's extended constant buffer. */
1440 /*} else if (r300screen->caps->is_r400) {
1450 /* XXX Subtract immediates and RC_STATE_* variables. */
1451 if (buf
->size
> (sizeof(float) * 4 * max_size
)) {
1452 debug_printf("r300: Max size of the constant buffer is "
1453 "%i*4 floats.\n", max_size
);
1457 memcpy(r300
->shader_constants
[shader
].constants
, mapped
, buf
->size
);
1458 r300
->shader_constants
[shader
].count
= buf
->size
/ (4 * sizeof(float));
1459 pipe_buffer_unmap(pipe
->screen
, buf
);
1461 if (shader
== PIPE_SHADER_VERTEX
) {
1462 if (r300screen
->caps
->has_tcl
) {
1463 r300
->dirty_state
|= R300_NEW_VERTEX_SHADER_CONSTANTS
;
1464 r300
->pvs_flush
.dirty
= TRUE
;
1467 else if (shader
== PIPE_SHADER_FRAGMENT
)
1468 r300
->dirty_state
|= R300_NEW_FRAGMENT_SHADER_CONSTANTS
;
1471 void r300_init_state_functions(struct r300_context
* r300
)
1473 r300
->context
.create_blend_state
= r300_create_blend_state
;
1474 r300
->context
.bind_blend_state
= r300_bind_blend_state
;
1475 r300
->context
.delete_blend_state
= r300_delete_blend_state
;
1477 r300
->context
.set_blend_color
= r300_set_blend_color
;
1479 r300
->context
.set_clip_state
= r300_set_clip_state
;
1481 r300
->context
.set_constant_buffer
= r300_set_constant_buffer
;
1483 r300
->context
.create_depth_stencil_alpha_state
= r300_create_dsa_state
;
1484 r300
->context
.bind_depth_stencil_alpha_state
= r300_bind_dsa_state
;
1485 r300
->context
.delete_depth_stencil_alpha_state
= r300_delete_dsa_state
;
1487 r300
->context
.set_stencil_ref
= r300_set_stencil_ref
;
1489 r300
->context
.set_framebuffer_state
= r300_set_framebuffer_state
;
1491 r300
->context
.create_fs_state
= r300_create_fs_state
;
1492 r300
->context
.bind_fs_state
= r300_bind_fs_state
;
1493 r300
->context
.delete_fs_state
= r300_delete_fs_state
;
1495 r300
->context
.set_polygon_stipple
= r300_set_polygon_stipple
;
1497 r300
->context
.create_rasterizer_state
= r300_create_rs_state
;
1498 r300
->context
.bind_rasterizer_state
= r300_bind_rs_state
;
1499 r300
->context
.delete_rasterizer_state
= r300_delete_rs_state
;
1501 r300
->context
.create_sampler_state
= r300_create_sampler_state
;
1502 r300
->context
.bind_fragment_sampler_states
= r300_bind_sampler_states
;
1503 r300
->context
.bind_vertex_sampler_states
= r300_lacks_vertex_textures
;
1504 r300
->context
.delete_sampler_state
= r300_delete_sampler_state
;
1506 r300
->context
.set_fragment_sampler_views
= r300_set_fragment_sampler_views
;
1507 r300
->context
.create_sampler_view
= r300_create_sampler_view
;
1508 r300
->context
.sampler_view_destroy
= r300_sampler_view_destroy
;
1510 r300
->context
.set_scissor_state
= r300_set_scissor_state
;
1512 r300
->context
.set_viewport_state
= r300_set_viewport_state
;
1514 r300
->context
.set_vertex_buffers
= r300_set_vertex_buffers
;
1516 r300
->context
.create_vertex_elements_state
= r300_create_vertex_elements_state
;
1517 r300
->context
.bind_vertex_elements_state
= r300_bind_vertex_elements_state
;
1518 r300
->context
.delete_vertex_elements_state
= r300_delete_vertex_elements_state
;
1520 r300
->context
.create_vs_state
= r300_create_vs_state
;
1521 r300
->context
.bind_vs_state
= r300_bind_vs_state
;
1522 r300
->context
.delete_vs_state
= r300_delete_vs_state
;