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 struct r300_shader_key
{
41 struct r300_vertex_shader
* vs
;
42 struct r300_fragment_shader
* fs
;
45 struct r300_shader_derived_value
{
46 struct r300_vertex_format
* vformat
;
47 struct r300_rs_block
* rs_block
;
50 unsigned r300_shader_key_hash(void* key
) {
51 struct r300_shader_key
* shader_key
= (struct r300_shader_key
*)key
;
52 unsigned vs
= (intptr_t)shader_key
->vs
;
53 unsigned fs
= (intptr_t)shader_key
->fs
;
55 return (vs
<< 16) | (fs
& 0xffff);
58 int r300_shader_key_compare(void* key1
, void* key2
) {
59 struct r300_shader_key
* shader_key1
= (struct r300_shader_key
*)key1
;
60 struct r300_shader_key
* shader_key2
= (struct r300_shader_key
*)key2
;
62 return (shader_key1
->vs
== shader_key2
->vs
) &&
63 (shader_key1
->fs
== shader_key2
->fs
);
66 static void r300_draw_emit_attrib(struct r300_context
* r300
,
67 enum attrib_emit emit
,
68 enum interp_mode interp
,
71 struct tgsi_shader_info
* info
= &r300
->vs
->info
;
74 output
= draw_find_vs_output(r300
->draw
,
75 info
->output_semantic_name
[index
],
76 info
->output_semantic_index
[index
]);
77 draw_emit_vertex_attr(&r300
->vertex_info
->vinfo
, emit
, interp
, output
);
80 static void r300_draw_emit_all_attribs(struct r300_context
* r300
)
82 struct r300_shader_semantics
* vs_outputs
= &r300
->vs
->outputs
;
86 if (vs_outputs
->pos
!= ATTR_UNUSED
) {
87 r300_draw_emit_attrib(r300
, EMIT_4F
, INTERP_PERSPECTIVE
,
94 if (vs_outputs
->psize
!= ATTR_UNUSED
) {
95 r300_draw_emit_attrib(r300
, EMIT_1F_PSIZE
, INTERP_POS
,
100 for (i
= 0; i
< ATTR_COLOR_COUNT
; i
++) {
101 if (vs_outputs
->color
[i
] != ATTR_UNUSED
) {
102 r300_draw_emit_attrib(r300
, EMIT_4F
, INTERP_LINEAR
,
103 vs_outputs
->color
[i
]);
107 /* XXX Back-face colors. */
109 /* Texture coordinates. */
111 for (i
= 0; i
< ATTR_GENERIC_COUNT
; i
++) {
112 if (vs_outputs
->generic
[i
] != ATTR_UNUSED
) {
113 r300_draw_emit_attrib(r300
, EMIT_4F
, INTERP_PERSPECTIVE
,
114 vs_outputs
->generic
[i
]);
119 /* Fog coordinates. */
120 if (vs_outputs
->fog
!= ATTR_UNUSED
) {
121 r300_draw_emit_attrib(r300
, EMIT_4F
, INTERP_PERSPECTIVE
,
127 assert(gen_count
<= 8);
130 /* Update the PSC tables. */
131 static void r300_vertex_psc(struct r300_context
* r300
)
133 struct r300_vertex_info
*vformat
= r300
->vertex_info
;
134 uint16_t type
, swizzle
;
135 enum pipe_format format
;
137 int identity
[16] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15};
140 /* If TCL is bypassed, map vertex streams to equivalent VS output
142 if (r300
->rs_state
->enable_vte
) {
143 stream_tab
= identity
;
145 stream_tab
= r300
->vs
->stream_loc_notcl
;
148 /* Vertex shaders have no semantics on their inputs,
149 * so PSC should just route stuff based on the vertex elements,
150 * and not on attrib information. */
151 DBG(r300
, DBG_DRAW
, "r300: vs expects %d attribs, routing %d elements"
153 r300
->vs
->info
.num_inputs
,
154 r300
->vertex_element_count
);
156 for (i
= 0; i
< r300
->vertex_element_count
; i
++) {
157 format
= r300
->vertex_element
[i
].src_format
;
159 type
= r300_translate_vertex_data_type(format
) |
160 (stream_tab
[i
] << R300_DST_VEC_LOC_SHIFT
);
161 swizzle
= r300_translate_vertex_data_swizzle(format
);
164 vformat
->vap_prog_stream_cntl
[i
>> 1] |= type
<< 16;
165 vformat
->vap_prog_stream_cntl_ext
[i
>> 1] |= swizzle
<< 16;
167 vformat
->vap_prog_stream_cntl
[i
>> 1] |= type
;
168 vformat
->vap_prog_stream_cntl_ext
[i
>> 1] |= swizzle
;
174 /* Set the last vector in the PSC. */
178 vformat
->vap_prog_stream_cntl
[i
>> 1] |=
179 (R300_LAST_VEC
<< (i
& 1 ? 16 : 0));
182 /* Update the PSC tables for SW TCL, using Draw. */
183 static void r300_swtcl_vertex_psc(struct r300_context
* r300
)
185 struct r300_vertex_info
*vformat
= r300
->vertex_info
;
186 struct vertex_info
* vinfo
= &vformat
->vinfo
;
187 uint16_t type
, swizzle
;
188 enum pipe_format format
;
189 unsigned i
, attrib_count
;
190 int* vs_output_tab
= r300
->vs
->stream_loc_notcl
;
192 /* For each Draw attribute, route it to the fragment shader according
193 * to the vs_output_tab. */
194 attrib_count
= vinfo
->num_attribs
;
195 DBG(r300
, DBG_DRAW
, "r300: attrib count: %d\n", attrib_count
);
196 for (i
= 0; i
< attrib_count
; i
++) {
197 DBG(r300
, DBG_DRAW
, "r300: attrib: offset %d, interp %d, size %d,"
198 " vs_output_tab %d\n", vinfo
->attrib
[i
].src_index
,
199 vinfo
->attrib
[i
].interp_mode
, vinfo
->attrib
[i
].emit
,
203 for (i
= 0; i
< attrib_count
; i
++) {
204 /* Make sure we have a proper destination for our attribute. */
205 assert(vs_output_tab
[i
] != -1);
207 format
= draw_translate_vinfo_format(vinfo
->attrib
[i
].emit
);
209 /* Obtain the type of data in this attribute. */
210 type
= r300_translate_vertex_data_type(format
) |
211 vs_output_tab
[i
] << R300_DST_VEC_LOC_SHIFT
;
213 /* Obtain the swizzle for this attribute. Note that the default
214 * swizzle in the hardware is not XYZW! */
215 swizzle
= r300_translate_vertex_data_swizzle(format
);
217 /* Add the attribute to the PSC table. */
219 vformat
->vap_prog_stream_cntl
[i
>> 1] |= type
<< 16;
220 vformat
->vap_prog_stream_cntl_ext
[i
>> 1] |= swizzle
<< 16;
222 vformat
->vap_prog_stream_cntl
[i
>> 1] |= type
;
223 vformat
->vap_prog_stream_cntl_ext
[i
>> 1] |= swizzle
;
227 /* Set the last vector in the PSC. */
231 vformat
->vap_prog_stream_cntl
[i
>> 1] |=
232 (R300_LAST_VEC
<< (i
& 1 ? 16 : 0));
235 static void r300_rs_col(struct r300_rs_block
* rs
, int id
, int ptr
,
236 boolean swizzle_0001
)
238 rs
->ip
[id
] |= R300_RS_COL_PTR(ptr
);
240 rs
->ip
[id
] |= R300_RS_COL_FMT(R300_RS_COL_FMT_0001
);
242 rs
->ip
[id
] |= R300_RS_COL_FMT(R300_RS_COL_FMT_RGBA
);
244 rs
->inst
[id
] |= R300_RS_INST_COL_ID(id
);
247 static void r300_rs_col_write(struct r300_rs_block
* rs
, int id
, int fp_offset
)
249 rs
->inst
[id
] |= R300_RS_INST_COL_CN_WRITE
|
250 R300_RS_INST_COL_ADDR(fp_offset
);
253 static void r300_rs_tex(struct r300_rs_block
* rs
, int id
, int ptr
,
254 boolean swizzle_X001
)
257 rs
->ip
[id
] |= R300_RS_TEX_PTR(ptr
*4) |
258 R300_RS_SEL_S(R300_RS_SEL_C0
) |
259 R300_RS_SEL_T(R300_RS_SEL_K0
) |
260 R300_RS_SEL_R(R300_RS_SEL_K0
) |
261 R300_RS_SEL_Q(R300_RS_SEL_K1
);
263 rs
->ip
[id
] |= R300_RS_TEX_PTR(ptr
*4) |
264 R300_RS_SEL_S(R300_RS_SEL_C0
) |
265 R300_RS_SEL_T(R300_RS_SEL_C1
) |
266 R300_RS_SEL_R(R300_RS_SEL_C2
) |
267 R300_RS_SEL_Q(R300_RS_SEL_C3
);
269 rs
->inst
[id
] |= R300_RS_INST_TEX_ID(id
);
272 static void r300_rs_tex_write(struct r300_rs_block
* rs
, int id
, int fp_offset
)
274 rs
->inst
[id
] |= R300_RS_INST_TEX_CN_WRITE
|
275 R300_RS_INST_TEX_ADDR(fp_offset
);
278 static void r500_rs_col(struct r300_rs_block
* rs
, int id
, int ptr
,
279 boolean swizzle_0001
)
281 rs
->ip
[id
] |= R500_RS_COL_PTR(ptr
);
283 rs
->ip
[id
] |= R500_RS_COL_FMT(R300_RS_COL_FMT_0001
);
285 rs
->ip
[id
] |= R500_RS_COL_FMT(R300_RS_COL_FMT_RGBA
);
287 rs
->inst
[id
] |= R500_RS_INST_COL_ID(id
);
290 static void r500_rs_col_write(struct r300_rs_block
* rs
, int id
, int fp_offset
)
292 rs
->inst
[id
] |= R500_RS_INST_COL_CN_WRITE
|
293 R500_RS_INST_COL_ADDR(fp_offset
);
296 static void r500_rs_tex(struct r300_rs_block
* rs
, int id
, int ptr
,
297 boolean swizzle_X001
)
299 int rs_tex_comp
= ptr
*4;
302 rs
->ip
[id
] |= R500_RS_SEL_S(rs_tex_comp
) |
303 R500_RS_SEL_T(R500_RS_IP_PTR_K0
) |
304 R500_RS_SEL_R(R500_RS_IP_PTR_K0
) |
305 R500_RS_SEL_Q(R500_RS_IP_PTR_K1
);
307 rs
->ip
[id
] |= R500_RS_SEL_S(rs_tex_comp
) |
308 R500_RS_SEL_T(rs_tex_comp
+ 1) |
309 R500_RS_SEL_R(rs_tex_comp
+ 2) |
310 R500_RS_SEL_Q(rs_tex_comp
+ 3);
312 rs
->inst
[id
] |= R500_RS_INST_TEX_ID(id
);
315 static void r500_rs_tex_write(struct r300_rs_block
* rs
, int id
, int fp_offset
)
317 rs
->inst
[id
] |= R500_RS_INST_TEX_CN_WRITE
|
318 R500_RS_INST_TEX_ADDR(fp_offset
);
321 /* Set up the RS block.
323 * This is the part of the chipset that actually does the rasterization
324 * of vertices into fragments. This is also the part of the chipset that
325 * locks up if any part of it is even slightly wrong. */
326 static void r300_update_rs_block(struct r300_context
* r300
,
327 struct r300_shader_semantics
* vs_outputs
,
328 struct r300_shader_semantics
* fs_inputs
)
330 struct r300_rs_block
* rs
= r300
->rs_block
;
331 int i
, col_count
= 0, tex_count
= 0, fp_offset
= 0;
332 void (*rX00_rs_col
)(struct r300_rs_block
*, int, int, boolean
);
333 void (*rX00_rs_col_write
)(struct r300_rs_block
*, int, int);
334 void (*rX00_rs_tex
)(struct r300_rs_block
*, int, int, boolean
);
335 void (*rX00_rs_tex_write
)(struct r300_rs_block
*, int, int);
337 if (r300_screen(r300
->context
.screen
)->caps
->is_r500
) {
338 rX00_rs_col
= r500_rs_col
;
339 rX00_rs_col_write
= r500_rs_col_write
;
340 rX00_rs_tex
= r500_rs_tex
;
341 rX00_rs_tex_write
= r500_rs_tex_write
;
343 rX00_rs_col
= r300_rs_col
;
344 rX00_rs_col_write
= r300_rs_col_write
;
345 rX00_rs_tex
= r300_rs_tex
;
346 rX00_rs_tex_write
= r300_rs_tex_write
;
349 /* Rasterize colors. */
350 for (i
= 0; i
< ATTR_COLOR_COUNT
; i
++) {
351 if (vs_outputs
->color
[i
] != ATTR_UNUSED
) {
352 /* Always rasterize if it's written by the VS,
353 * otherwise it locks up. */
354 rX00_rs_col(rs
, col_count
, i
, FALSE
);
356 /* Write it to the FS input register if it's used by the FS. */
357 if (fs_inputs
->color
[i
] != ATTR_UNUSED
) {
358 rX00_rs_col_write(rs
, col_count
, fp_offset
);
363 /* Skip the FS input register, leave it uninitialized. */
364 /* If we try to set it to (0,0,0,1), it will lock up. */
365 if (fs_inputs
->color
[i
] != ATTR_UNUSED
) {
371 /* Rasterize texture coordinates. */
372 for (i
= 0; i
< ATTR_GENERIC_COUNT
; i
++) {
373 if (vs_outputs
->generic
[i
] != ATTR_UNUSED
) {
374 /* Always rasterize if it's written by the VS,
375 * otherwise it locks up. */
376 rX00_rs_tex(rs
, tex_count
, tex_count
, FALSE
);
378 /* Write it to the FS input register if it's used by the FS. */
379 if (fs_inputs
->generic
[i
] != ATTR_UNUSED
) {
380 rX00_rs_tex_write(rs
, tex_count
, fp_offset
);
385 /* Skip the FS input register, leave it uninitialized. */
386 /* If we try to set it to (0,0,0,1), it will lock up. */
387 if (fs_inputs
->generic
[i
] != ATTR_UNUSED
) {
393 /* Rasterize fog coordinates. */
394 if (vs_outputs
->fog
!= ATTR_UNUSED
) {
395 /* Always rasterize if it's written by the VS,
396 * otherwise it locks up. */
397 rX00_rs_tex(rs
, tex_count
, tex_count
, TRUE
);
399 /* Write it to the FS input register if it's used by the FS. */
400 if (fs_inputs
->fog
!= ATTR_UNUSED
) {
401 rX00_rs_tex_write(rs
, tex_count
, fp_offset
);
406 /* Skip the FS input register, leave it uninitialized. */
407 /* If we try to set it to (0,0,0,1), it will lock up. */
408 if (fs_inputs
->fog
!= ATTR_UNUSED
) {
413 /* Rasterize at least one color, or bad things happen. */
414 if (col_count
== 0 && tex_count
== 0) {
415 rX00_rs_col(rs
, 0, 0, TRUE
);
419 rs
->count
= (tex_count
*4) | (col_count
<< R300_IC_COUNT_SHIFT
) |
422 rs
->inst_count
= MAX3(col_count
- 1, tex_count
- 1, 0);
425 /* Update the vertex format. */
426 static void r300_update_derived_shader_state(struct r300_context
* r300
)
428 struct r300_screen
* r300screen
= r300_screen(r300
->context
.screen
);
431 struct r300_shader_key* key;
432 struct r300_shader_derived_value* value;
433 key = CALLOC_STRUCT(r300_shader_key);
437 value = (struct r300_shader_derived_value*)
438 util_hash_table_get(r300->shader_hash_table, (void*)key);
440 //vformat = value->vformat;
441 rs_block = value->rs_block;
445 rs_block = CALLOC_STRUCT(r300_rs_block);
446 value = CALLOC_STRUCT(r300_shader_derived_value);
448 r300_update_rs_block(r300, rs_block);
450 //value->vformat = vformat;
451 value->rs_block = rs_block;
452 util_hash_table_set(r300->shader_hash_table,
453 (void*)key, (void*)value);
456 /* Reset structures */
457 memset(r300
->rs_block
, 0, sizeof(struct r300_rs_block
));
458 memset(r300
->vertex_info
, 0, sizeof(struct r300_vertex_info
));
459 memcpy(r300
->vertex_info
->vinfo
.hwfmt
, r300
->vs
->hwfmt
, sizeof(uint
)*4);
461 r300_update_rs_block(r300
, &r300
->vs
->outputs
, &r300
->fs
->inputs
);
463 if (r300screen
->caps
->has_tcl
) {
464 r300_vertex_psc(r300
);
466 r300_draw_emit_all_attribs(r300
);
467 draw_compute_vertex_size(&r300
->vertex_info
->vinfo
);
468 r300_swtcl_vertex_psc(r300
);
471 r300
->dirty_state
|= R300_NEW_RS_BLOCK
;
474 static boolean
r300_dsa_writes_depth_stencil(struct r300_dsa_state
* dsa
)
476 /* We are interested only in the cases when a new depth or stencil value
477 * can be written and changed. */
479 /* We might optionally check for [Z func: never] and inspect the stencil
480 * state in a similar fashion, but it's not terribly important. */
481 return (dsa
->z_buffer_control
& R300_Z_WRITE_ENABLE
) ||
482 (dsa
->stencil_ref_mask
& R300_STENCILWRITEMASK_MASK
) ||
483 ((dsa
->z_buffer_control
& R500_STENCIL_REFMASK_FRONT_BACK
) &&
484 (dsa
->stencil_ref_bf
& R300_STENCILWRITEMASK_MASK
));
487 static boolean
r300_dsa_alpha_test_enabled(struct r300_dsa_state
* dsa
)
489 /* We are interested only in the cases when alpha testing can kill
491 uint32_t af
= dsa
->alpha_function
;
493 return (af
& R300_FG_ALPHA_FUNC_ENABLE
) &&
494 (af
& R300_FG_ALPHA_FUNC_ALWAYS
) != R300_FG_ALPHA_FUNC_ALWAYS
;
497 static void r300_update_ztop(struct r300_context
* r300
)
499 r300
->ztop_state
.z_buffer_top
= R300_ZTOP_ENABLE
;
501 /* This is important enough that I felt it warranted a comment.
503 * According to the docs, these are the conditions where ZTOP must be
505 * 1) Alpha testing enabled
506 * 2) Texture kill instructions in fragment shader
507 * 3) Chroma key culling enabled
508 * 4) W-buffering enabled
510 * The docs claim that for the first three cases, if no ZS writes happen,
511 * then ZTOP can be used.
513 * (3) will never apply since we do not support chroma-keyed operations.
514 * (4) will need to be re-examined (and this comment updated) if/when
515 * Hyper-Z becomes supported.
517 * Additionally, the following conditions require disabled ZTOP:
518 * 5) Depth writes in fragment shader
519 * 6) Outstanding occlusion queries
525 if (r300_dsa_writes_depth_stencil(r300
->dsa_state
) &&
526 (r300_dsa_alpha_test_enabled(r300
->dsa_state
) || /* (1) */
527 r300
->fs
->info
.uses_kill
)) { /* (2) */
528 r300
->ztop_state
.z_buffer_top
= R300_ZTOP_DISABLE
;
529 } else if (r300_fragment_shader_writes_depth(r300
->fs
)) { /* (5) */
530 r300
->ztop_state
.z_buffer_top
= R300_ZTOP_DISABLE
;
531 } else if (r300
->query_current
) { /* (6) */
532 r300
->ztop_state
.z_buffer_top
= R300_ZTOP_DISABLE
;
536 void r300_update_derived_state(struct r300_context
* r300
)
538 if (r300
->dirty_state
&
539 (R300_NEW_FRAGMENT_SHADER
| R300_NEW_VERTEX_SHADER
|
540 R300_NEW_VERTEX_FORMAT
)) {
541 r300_update_derived_shader_state(r300
);
544 if (r300
->dirty_state
&
545 (R300_NEW_DSA
| R300_NEW_FRAGMENT_SHADER
| R300_NEW_QUERY
)) {
546 r300_update_ztop(r300
);