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"
29 #include "r300_context.h"
31 #include "r300_screen.h"
32 #include "r300_shader_semantics.h"
33 #include "r300_state_derived.h"
34 #include "r300_state_inlines.h"
37 /* r300_state_derived: Various bits of state which are dependent upon
38 * currently bound CSO data. */
40 static void r300_rs_col(struct r300_rs_block
* rs
, int id
, int ptr
,
43 rs
->ip
[id
] |= R300_RS_COL_PTR(ptr
);
45 rs
->ip
[id
] |= R300_RS_COL_FMT(R300_RS_COL_FMT_0001
);
47 rs
->ip
[id
] |= R300_RS_COL_FMT(R300_RS_COL_FMT_RGBA
);
49 rs
->inst
[id
] |= R300_RS_INST_COL_ID(id
);
52 static void r300_rs_col_write(struct r300_rs_block
* rs
, int id
, int fp_offset
)
54 rs
->inst
[id
] |= R300_RS_INST_COL_CN_WRITE
|
55 R300_RS_INST_COL_ADDR(fp_offset
);
58 static void r300_rs_tex(struct r300_rs_block
* rs
, int id
, int ptr
,
62 rs
->ip
[id
] |= R300_RS_TEX_PTR(ptr
*4) |
63 R300_RS_SEL_S(R300_RS_SEL_C0
) |
64 R300_RS_SEL_T(R300_RS_SEL_K0
) |
65 R300_RS_SEL_R(R300_RS_SEL_K0
) |
66 R300_RS_SEL_Q(R300_RS_SEL_K1
);
68 rs
->ip
[id
] |= R300_RS_TEX_PTR(ptr
*4) |
69 R300_RS_SEL_S(R300_RS_SEL_C0
) |
70 R300_RS_SEL_T(R300_RS_SEL_C1
) |
71 R300_RS_SEL_R(R300_RS_SEL_C2
) |
72 R300_RS_SEL_Q(R300_RS_SEL_C3
);
74 rs
->inst
[id
] |= R300_RS_INST_TEX_ID(id
);
77 static void r300_rs_tex_write(struct r300_rs_block
* rs
, int id
, int fp_offset
)
79 rs
->inst
[id
] |= R300_RS_INST_TEX_CN_WRITE
|
80 R300_RS_INST_TEX_ADDR(fp_offset
);
83 static void r500_rs_col(struct r300_rs_block
* rs
, int id
, int ptr
,
86 rs
->ip
[id
] |= R500_RS_COL_PTR(ptr
);
88 rs
->ip
[id
] |= R500_RS_COL_FMT(R300_RS_COL_FMT_0001
);
90 rs
->ip
[id
] |= R500_RS_COL_FMT(R300_RS_COL_FMT_RGBA
);
92 rs
->inst
[id
] |= R500_RS_INST_COL_ID(id
);
95 static void r500_rs_col_write(struct r300_rs_block
* rs
, int id
, int fp_offset
)
97 rs
->inst
[id
] |= R500_RS_INST_COL_CN_WRITE
|
98 R500_RS_INST_COL_ADDR(fp_offset
);
101 static void r500_rs_tex(struct r300_rs_block
* rs
, int id
, int ptr
,
102 boolean swizzle_X001
)
104 int rs_tex_comp
= ptr
*4;
107 rs
->ip
[id
] |= R500_RS_SEL_S(rs_tex_comp
) |
108 R500_RS_SEL_T(R500_RS_IP_PTR_K0
) |
109 R500_RS_SEL_R(R500_RS_IP_PTR_K0
) |
110 R500_RS_SEL_Q(R500_RS_IP_PTR_K1
);
112 rs
->ip
[id
] |= R500_RS_SEL_S(rs_tex_comp
) |
113 R500_RS_SEL_T(rs_tex_comp
+ 1) |
114 R500_RS_SEL_R(rs_tex_comp
+ 2) |
115 R500_RS_SEL_Q(rs_tex_comp
+ 3);
117 rs
->inst
[id
] |= R500_RS_INST_TEX_ID(id
);
120 static void r500_rs_tex_write(struct r300_rs_block
* rs
, int id
, int fp_offset
)
122 rs
->inst
[id
] |= R500_RS_INST_TEX_CN_WRITE
|
123 R500_RS_INST_TEX_ADDR(fp_offset
);
126 /* Set up the RS block.
128 * This is the part of the chipset that actually does the rasterization
129 * of vertices into fragments. This is also the part of the chipset that
130 * locks up if any part of it is even slightly wrong. */
131 static void r300_update_rs_block(struct r300_context
* r300
,
132 struct r300_shader_semantics
* vs_outputs
,
133 struct r300_shader_semantics
* fs_inputs
)
135 struct r300_rs_block rs
= { { 0 } };
136 int i
, col_count
= 0, tex_count
= 0, fp_offset
= 0, count
;
137 void (*rX00_rs_col
)(struct r300_rs_block
*, int, int, boolean
);
138 void (*rX00_rs_col_write
)(struct r300_rs_block
*, int, int);
139 void (*rX00_rs_tex
)(struct r300_rs_block
*, int, int, boolean
);
140 void (*rX00_rs_tex_write
)(struct r300_rs_block
*, int, int);
141 boolean any_bcolor_used
= vs_outputs
->bcolor
[0] != ATTR_UNUSED
||
142 vs_outputs
->bcolor
[1] != ATTR_UNUSED
;
144 if (r300_screen(r300
->context
.screen
)->caps
->is_r500
) {
145 rX00_rs_col
= r500_rs_col
;
146 rX00_rs_col_write
= r500_rs_col_write
;
147 rX00_rs_tex
= r500_rs_tex
;
148 rX00_rs_tex_write
= r500_rs_tex_write
;
150 rX00_rs_col
= r300_rs_col
;
151 rX00_rs_col_write
= r300_rs_col_write
;
152 rX00_rs_tex
= r300_rs_tex
;
153 rX00_rs_tex_write
= r300_rs_tex_write
;
156 /* Rasterize colors. */
157 for (i
= 0; i
< ATTR_COLOR_COUNT
; i
++) {
158 if (vs_outputs
->color
[i
] != ATTR_UNUSED
|| any_bcolor_used
||
159 vs_outputs
->color
[1] != ATTR_UNUSED
) {
160 /* Always rasterize if it's written by the VS,
161 * otherwise it locks up. */
162 rX00_rs_col(&rs
, col_count
, i
, FALSE
);
164 /* Write it to the FS input register if it's used by the FS. */
165 if (fs_inputs
->color
[i
] != ATTR_UNUSED
) {
166 rX00_rs_col_write(&rs
, col_count
, fp_offset
);
171 /* Skip the FS input register, leave it uninitialized. */
172 /* If we try to set it to (0,0,0,1), it will lock up. */
173 if (fs_inputs
->color
[i
] != ATTR_UNUSED
) {
179 /* Rasterize texture coordinates. */
180 for (i
= 0; i
< ATTR_GENERIC_COUNT
; i
++) {
181 if (vs_outputs
->generic
[i
] != ATTR_UNUSED
) {
182 /* Always rasterize if it's written by the VS,
183 * otherwise it locks up. */
184 rX00_rs_tex(&rs
, tex_count
, tex_count
, FALSE
);
186 /* Write it to the FS input register if it's used by the FS. */
187 if (fs_inputs
->generic
[i
] != ATTR_UNUSED
) {
188 rX00_rs_tex_write(&rs
, tex_count
, fp_offset
);
193 /* Skip the FS input register, leave it uninitialized. */
194 /* If we try to set it to (0,0,0,1), it will lock up. */
195 if (fs_inputs
->generic
[i
] != ATTR_UNUSED
) {
201 /* Rasterize fog coordinates. */
202 if (vs_outputs
->fog
!= ATTR_UNUSED
) {
203 /* Always rasterize if it's written by the VS,
204 * otherwise it locks up. */
205 rX00_rs_tex(&rs
, tex_count
, tex_count
, TRUE
);
207 /* Write it to the FS input register if it's used by the FS. */
208 if (fs_inputs
->fog
!= ATTR_UNUSED
) {
209 rX00_rs_tex_write(&rs
, tex_count
, fp_offset
);
214 /* Skip the FS input register, leave it uninitialized. */
215 /* If we try to set it to (0,0,0,1), it will lock up. */
216 if (fs_inputs
->fog
!= ATTR_UNUSED
) {
221 /* Rasterize WPOS. */
222 /* If the FS doesn't need it, it's not written by the VS. */
223 if (fs_inputs
->wpos
!= ATTR_UNUSED
) {
224 rX00_rs_tex(&rs
, tex_count
, tex_count
, FALSE
);
225 rX00_rs_tex_write(&rs
, tex_count
, fp_offset
);
231 /* Rasterize at least one color, or bad things happen. */
232 if (col_count
== 0 && tex_count
== 0) {
233 rX00_rs_col(&rs
, 0, 0, TRUE
);
237 rs
.count
= (tex_count
*4) | (col_count
<< R300_IC_COUNT_SHIFT
) |
240 count
= MAX3(col_count
, tex_count
, 1);
241 rs
.inst_count
= count
- 1;
243 /* Now, after all that, see if we actually need to update the state. */
244 if (memcmp(r300
->rs_block_state
.state
, &rs
, sizeof(struct r300_rs_block
))) {
245 memcpy(r300
->rs_block_state
.state
, &rs
, sizeof(struct r300_rs_block
));
246 r300
->rs_block_state
.size
= 5 + count
*2;
250 /* Update the shader-dependant states. */
251 static void r300_update_derived_shader_state(struct r300_context
* r300
)
253 struct r300_vertex_shader
* vs
= r300
->vs_state
.state
;
255 r300_update_rs_block(r300
, &vs
->outputs
, &r300
->fs
->inputs
);
258 static boolean
r300_dsa_writes_depth_stencil(struct r300_dsa_state
* dsa
)
260 /* We are interested only in the cases when a new depth or stencil value
261 * can be written and changed. */
263 /* We might optionally check for [Z func: never] and inspect the stencil
264 * state in a similar fashion, but it's not terribly important. */
265 return (dsa
->z_buffer_control
& R300_Z_WRITE_ENABLE
) ||
266 (dsa
->stencil_ref_mask
& R300_STENCILWRITEMASK_MASK
) ||
267 ((dsa
->z_buffer_control
& R500_STENCIL_REFMASK_FRONT_BACK
) &&
268 (dsa
->stencil_ref_bf
& R300_STENCILWRITEMASK_MASK
));
271 static boolean
r300_dsa_alpha_test_enabled(struct r300_dsa_state
* dsa
)
273 /* We are interested only in the cases when alpha testing can kill
275 uint32_t af
= dsa
->alpha_function
;
277 return (af
& R300_FG_ALPHA_FUNC_ENABLE
) &&
278 (af
& R300_FG_ALPHA_FUNC_ALWAYS
) != R300_FG_ALPHA_FUNC_ALWAYS
;
281 static void r300_update_ztop(struct r300_context
* r300
)
283 struct r300_ztop_state
* ztop_state
=
284 (struct r300_ztop_state
*)r300
->ztop_state
.state
;
286 /* This is important enough that I felt it warranted a comment.
288 * According to the docs, these are the conditions where ZTOP must be
290 * 1) Alpha testing enabled
291 * 2) Texture kill instructions in fragment shader
292 * 3) Chroma key culling enabled
293 * 4) W-buffering enabled
295 * The docs claim that for the first three cases, if no ZS writes happen,
296 * then ZTOP can be used.
298 * (3) will never apply since we do not support chroma-keyed operations.
299 * (4) will need to be re-examined (and this comment updated) if/when
300 * Hyper-Z becomes supported.
302 * Additionally, the following conditions require disabled ZTOP:
303 * 5) Depth writes in fragment shader
304 * 6) Outstanding occlusion queries
306 * This register causes stalls all the way from SC to CB when changed,
307 * but it is buffered on-chip so it does not hurt to write it if it has
314 if (r300_dsa_writes_depth_stencil(r300
->dsa_state
.state
) &&
315 (r300_dsa_alpha_test_enabled(r300
->dsa_state
.state
) ||/* (1) */
316 r300
->fs
->info
.uses_kill
)) { /* (2) */
317 ztop_state
->z_buffer_top
= R300_ZTOP_DISABLE
;
318 } else if (r300_fragment_shader_writes_depth(r300
->fs
)) { /* (5) */
319 ztop_state
->z_buffer_top
= R300_ZTOP_DISABLE
;
320 } else if (r300
->query_current
) { /* (6) */
321 ztop_state
->z_buffer_top
= R300_ZTOP_DISABLE
;
323 ztop_state
->z_buffer_top
= R300_ZTOP_ENABLE
;
326 r300
->ztop_state
.dirty
= TRUE
;
329 static void r300_merge_textures_and_samplers(struct r300_context
* r300
)
331 struct r300_textures_state
*state
=
332 (struct r300_textures_state
*)r300
->textures_state
.state
;
333 struct r300_texture_sampler_state
*texstate
;
334 struct r300_sampler_state
*sampler
;
335 struct r300_texture
*tex
;
336 unsigned min_level
, max_level
, i
, size
;
337 unsigned count
= MIN2(state
->texture_count
, state
->sampler_count
);
339 state
->tx_enable
= 0;
342 for (i
= 0; i
< count
; i
++) {
343 if (state
->fragment_sampler_views
[i
] && state
->sampler_states
[i
]) {
344 state
->tx_enable
|= 1 << i
;
346 tex
= (struct r300_texture
*)state
->fragment_sampler_views
[i
]->texture
;
347 sampler
= state
->sampler_states
[i
];
349 texstate
= &state
->regs
[i
];
350 memcpy(texstate
->format
, &tex
->state
, sizeof(uint32_t)*3);
351 texstate
->filter
[0] = sampler
->filter0
;
352 texstate
->filter
[1] = sampler
->filter1
;
353 texstate
->border_color
= sampler
->border_color
;
354 texstate
->tile_config
= R300_TXO_MACRO_TILE(tex
->macrotile
) |
355 R300_TXO_MICRO_TILE(tex
->microtile
);
357 /* to emulate 1D textures through 2D ones correctly */
358 if (tex
->tex
.target
== PIPE_TEXTURE_1D
) {
359 texstate
->filter
[0] &= ~R300_TX_WRAP_T_MASK
;
360 texstate
->filter
[0] |= R300_TX_WRAP_T(R300_TX_CLAMP_TO_EDGE
);
364 /* NPOT textures don't support mip filter, unfortunately.
365 * This prevents incorrect rendering. */
366 texstate
->filter
[0] &= ~R300_TX_MIN_FILTER_MIP_MASK
;
368 /* determine min/max levels */
369 /* the MAX_MIP level is the largest (finest) one */
370 max_level
= MIN2(sampler
->max_lod
, tex
->tex
.last_level
);
371 min_level
= MIN2(sampler
->min_lod
, max_level
);
372 texstate
->format
[0] |= R300_TX_NUM_LEVELS(max_level
);
373 texstate
->filter
[0] |= R300_TX_MAX_MIP_LEVEL(min_level
);
376 texstate
->filter
[0] |= i
<< 28;
383 r300
->textures_state
.size
= size
;
386 void r300_update_derived_state(struct r300_context
* r300
)
388 if (r300
->rs_block_state
.dirty
) {
389 r300_update_derived_shader_state(r300
);
392 if (r300
->textures_state
.dirty
) {
393 r300_merge_textures_and_samplers(r300
);
396 r300_update_ztop(r300
);