2 * Copyright 2008 Corbin Simpson <MostAwesomeDude@gmail.com>
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * on the rights to use, copy, modify, merge, publish, distribute, sub
8 * license, and/or sell copies of the Software, and to permit persons to whom
9 * the Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHOR(S) AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
19 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
20 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
21 * USE OR OTHER DEALINGS IN THE SOFTWARE. */
23 #include "r300_state_derived.h"
26 #include "r300_state_inlines.h"
29 /* r300_state_derived: Various bits of state which are dependent upon
30 * currently bound CSO data. */
32 struct r300_shader_key
{
33 struct r300_vertex_shader
* vs
;
34 struct r300_fragment_shader
* fs
;
37 struct r300_shader_derived_value
{
38 struct r300_vertex_format
* vformat
;
39 struct r300_rs_block
* rs_block
;
42 unsigned r300_shader_key_hash(void* key
) {
43 struct r300_shader_key
* shader_key
= (struct r300_shader_key
*)key
;
44 unsigned vs
= (unsigned)shader_key
->vs
;
45 unsigned fs
= (unsigned)shader_key
->fs
;
47 return (vs
<< 16) | (fs
& 0xffff);
50 int r300_shader_key_compare(void* key1
, void* key2
) {
51 struct r300_shader_key
* shader_key1
= (struct r300_shader_key
*)key1
;
52 struct r300_shader_key
* shader_key2
= (struct r300_shader_key
*)key2
;
54 return (shader_key1
->vs
== shader_key2
->vs
) &&
55 (shader_key1
->fs
== shader_key2
->fs
);
58 /* Set up the vs_tab and routes. */
59 static void r300_vs_tab_routes(struct r300_context
* r300
,
60 struct r300_vertex_format
* vformat
)
62 struct r300_screen
* r300screen
= r300_screen(r300
->context
.screen
);
63 struct vertex_info
* vinfo
= &vformat
->vinfo
;
64 int* tab
= vformat
->vs_tab
;
65 boolean pos
= FALSE
, psize
= FALSE
, fog
= FALSE
;
66 int i
, texs
= 0, cols
= 0;
67 struct tgsi_shader_info
* info
;
69 if (r300screen
->caps
->has_tcl
) {
70 /* Use vertex shader to determine required routes. */
71 info
= &r300
->vs
->info
;
73 /* Use fragment shader to determine required routes. */
74 info
= &r300
->fs
->info
;
77 assert(info
->num_inputs
<= 16);
79 if (!r300screen
->caps
->has_tcl
|| !r300
->rs_state
->enable_vte
)
81 for (i
= 0; i
< info
->num_inputs
; i
++) {
82 switch (r300
->vs
->code
.inputs
[i
]) {
83 case TGSI_SEMANTIC_POSITION
:
87 case TGSI_SEMANTIC_COLOR
:
91 case TGSI_SEMANTIC_PSIZE
:
92 assert(psize
== FALSE
);
96 case TGSI_SEMANTIC_FOG
:
100 case TGSI_SEMANTIC_GENERIC
:
105 debug_printf("r300: Unknown vertex input %d\n",
106 info
->input_semantic_name
[i
]);
113 /* Just copy vert attribs over as-is. */
114 for (i
= 0; i
< info
->num_inputs
; i
++) {
118 for (i
= 0; i
< info
->num_outputs
; i
++) {
119 switch (info
->output_semantic_name
[i
]) {
120 case TGSI_SEMANTIC_POSITION
:
123 case TGSI_SEMANTIC_COLOR
:
126 case TGSI_SEMANTIC_PSIZE
:
129 case TGSI_SEMANTIC_FOG
:
132 case TGSI_SEMANTIC_GENERIC
:
136 debug_printf("r300: Unknown vertex output %d\n",
137 info
->output_semantic_name
[i
]);
146 /* Do the actual vertex_info setup.
148 * vertex_info has four uints of hardware-specific data in it.
149 * vinfo.hwfmt[0] is R300_VAP_VTX_STATE_CNTL
150 * vinfo.hwfmt[1] is R300_VAP_VSM_VTX_ASSM
151 * vinfo.hwfmt[2] is R300_VAP_OUTPUT_VTX_FMT_0
152 * vinfo.hwfmt[3] is R300_VAP_OUTPUT_VTX_FMT_1 */
154 vinfo
->hwfmt
[0] = 0x5555; /* XXX this is classic Mesa bonghits */
156 /* We need to add vertex position attribute only for SW TCL case,
157 * for HW TCL case it could be generated by vertex shader */
158 if (!pos
&& !r300screen
->caps
->has_tcl
) {
159 debug_printf("r300: Forcing vertex position attribute emit...\n");
160 /* Make room for the position attribute
161 * at the beginning of the tab. */
162 for (i
= 15; i
> 0; i
--) {
167 draw_emit_vertex_attr(vinfo
, EMIT_4F
, INTERP_PERSPECTIVE
,
168 draw_find_vs_output(r300
->draw
, TGSI_SEMANTIC_POSITION
, 0));
169 vinfo
->hwfmt
[1] |= R300_INPUT_CNTL_POS
;
170 vinfo
->hwfmt
[2] |= R300_VAP_OUTPUT_VTX_FMT_0__POS_PRESENT
;
173 draw_emit_vertex_attr(vinfo
, EMIT_1F_PSIZE
, INTERP_POS
,
174 draw_find_vs_output(r300
->draw
, TGSI_SEMANTIC_PSIZE
, 0));
175 vinfo
->hwfmt
[2] |= R300_VAP_OUTPUT_VTX_FMT_0__PT_SIZE_PRESENT
;
178 for (i
= 0; i
< cols
; i
++) {
179 draw_emit_vertex_attr(vinfo
, EMIT_4F
, INTERP_LINEAR
,
180 draw_find_vs_output(r300
->draw
, TGSI_SEMANTIC_COLOR
, i
));
181 vinfo
->hwfmt
[1] |= R300_INPUT_CNTL_COLOR
;
182 vinfo
->hwfmt
[2] |= (R300_VAP_OUTPUT_VTX_FMT_0__COLOR_0_PRESENT
<< i
);
185 /* Init i right here, increment it if fog is enabled.
186 * This gets around a double-increment problem. */
191 draw_emit_vertex_attr(vinfo
, EMIT_4F
, INTERP_PERSPECTIVE
,
192 draw_find_vs_output(r300
->draw
, TGSI_SEMANTIC_FOG
, 0));
193 vinfo
->hwfmt
[1] |= (R300_INPUT_CNTL_TC0
<< i
);
194 vinfo
->hwfmt
[3] |= (4 << (3 * i
));
197 for (; i
< texs
; i
++) {
198 draw_emit_vertex_attr(vinfo
, EMIT_4F
, INTERP_PERSPECTIVE
,
199 draw_find_vs_output(r300
->draw
, TGSI_SEMANTIC_GENERIC
, i
));
200 vinfo
->hwfmt
[1] |= (R300_INPUT_CNTL_TC0
<< i
);
201 vinfo
->hwfmt
[3] |= (4 << (3 * i
));
204 /* Handle the case where the vertex shader will be generating some of
205 * the attribs based on its inputs. */
206 if (r300screen
->caps
->has_tcl
&&
207 info
->num_inputs
< info
->num_outputs
) {
208 vinfo
->num_attribs
= info
->num_inputs
;
211 draw_compute_vertex_size(vinfo
);
214 /* Update the PSC tables. */
215 static void r300_vertex_psc(struct r300_context
* r300
,
216 struct r300_vertex_format
* vformat
)
218 struct r300_screen
* r300screen
= r300_screen(r300
->context
.screen
);
219 struct vertex_info
* vinfo
= &vformat
->vinfo
;
220 int* tab
= vformat
->vs_tab
;
222 unsigned i
, attrib_count
;
224 /* Vertex shaders have no semantics on their inputs,
225 * so PSC should just route stuff based on their info,
226 * and not on attrib information. */
227 if (r300screen
->caps
->has_tcl
) {
228 attrib_count
= r300
->vs
->info
.num_inputs
;
229 DBG(r300
, DBG_DRAW
, "r300: routing %d attribs in psc for vs\n",
232 attrib_count
= vinfo
->num_attribs
;
233 DBG(r300
, DBG_DRAW
, "r300: attrib count: %d\n", attrib_count
);
234 for (i
= 0; i
< attrib_count
; i
++) {
235 DBG(r300
, DBG_DRAW
, "r300: attrib: offset %d, interp %d, size %d,"
236 " tab %d\n", vinfo
->attrib
[i
].src_index
,
237 vinfo
->attrib
[i
].interp_mode
, vinfo
->attrib
[i
].emit
,
242 for (i
= 0; i
< attrib_count
; i
++) {
243 /* Make sure we have a proper destination for our attribute */
244 assert(tab
[i
] != -1);
246 /* Add the attribute to the PSC table. */
247 temp
= translate_vertex_data_type(vinfo
->attrib
[i
].emit
) |
248 tab
[i
] << R300_DST_VEC_LOC_SHIFT
;
251 vformat
->vap_prog_stream_cntl
[i
>> 1] &= 0x0000ffff;
252 vformat
->vap_prog_stream_cntl
[i
>> 1] |= temp
<< 16;
254 vformat
->vap_prog_stream_cntl_ext
[i
>> 1] |=
255 (R300_VAP_SWIZZLE_XYZW
<< 16);
257 vformat
->vap_prog_stream_cntl
[i
>> 1] &= 0xffff0000;
258 vformat
->vap_prog_stream_cntl
[i
>> 1] |= temp
<< 0;
260 vformat
->vap_prog_stream_cntl_ext
[i
>> 1] |=
261 (R300_VAP_SWIZZLE_XYZW
<< 0);
265 /* Set the last vector in the PSC. */
269 vformat
->vap_prog_stream_cntl
[i
>> 1] |=
270 (R300_LAST_VEC
<< (i
& 1 ? 16 : 0));
273 /* Set up the mappings from GB to US, for RS block. */
274 static void r300_update_fs_tab(struct r300_context
* r300
,
275 struct r300_vertex_format
* vformat
)
277 struct tgsi_shader_info
* info
= &r300
->fs
->info
;
278 int i
, cols
= 0, texs
= 0, cols_emitted
= 0;
279 int* tab
= vformat
->fs_tab
;
281 for (i
= 0; i
< 16; i
++) {
285 assert(info
->num_inputs
<= 16);
286 for (i
= 0; i
< info
->num_inputs
; i
++) {
287 switch (info
->input_semantic_name
[i
]) {
288 case TGSI_SEMANTIC_COLOR
:
289 tab
[i
] = INTERP_LINEAR
;
292 case TGSI_SEMANTIC_POSITION
:
293 case TGSI_SEMANTIC_PSIZE
:
294 debug_printf("r300: Implementation error: Can't use "
295 "pos attribs in fragshader yet!\n");
296 /* Pass through for now */
297 case TGSI_SEMANTIC_FOG
:
298 case TGSI_SEMANTIC_GENERIC
:
299 tab
[i
] = INTERP_PERSPECTIVE
;
302 debug_printf("r300: Unknown vertex input %d\n",
303 info
->input_semantic_name
[i
]);
308 /* Now that we know where everything is... */
309 DBG(r300
, DBG_DRAW
, "r300: fp input count: %d\n", info
->num_inputs
);
310 for (i
= 0; i
< info
->num_inputs
; i
++) {
313 DBG(r300
, DBG_DRAW
, "r300: attrib: "
314 "stack offset %d, color, tab %d\n",
316 tab
[i
] = cols_emitted
;
319 case INTERP_PERSPECTIVE
:
320 DBG(r300
, DBG_DRAW
, "r300: attrib: "
321 "stack offset %d, texcoord, tab %d\n",
323 tab
[i
] = cols
+ texs
;
327 debug_printf("r300: Implementation error: Bad fp interp!\n");
335 /* Set up the RS block. This is the part of the chipset that actually does
336 * the rasterization of vertices into fragments. This is also the part of the
337 * chipset that locks up if any part of it is even slightly wrong. */
338 static void r300_update_rs_block(struct r300_context
* r300
,
339 struct r300_rs_block
* rs
)
341 struct tgsi_shader_info
* info
= &r300
->fs
->info
;
342 int col_count
= 0, fp_offset
= 0, i
, tex_count
= 0;
345 if (r300_screen(r300
->context
.screen
)->caps
->is_r500
) {
346 for (i
= 0; i
< info
->num_inputs
; i
++) {
347 switch (info
->input_semantic_name
[i
]) {
348 case TGSI_SEMANTIC_COLOR
:
350 R500_RS_COL_PTR(col_count
) |
351 R500_RS_COL_FMT(R300_RS_COL_FMT_RGBA
);
354 case TGSI_SEMANTIC_GENERIC
:
356 R500_RS_SEL_S(rs_tex_comp
) |
357 R500_RS_SEL_T(rs_tex_comp
+ 1) |
358 R500_RS_SEL_R(rs_tex_comp
+ 2) |
359 R500_RS_SEL_Q(rs_tex_comp
+ 3);
368 /* Rasterize at least one color, or bad things happen. */
369 if ((col_count
== 0) && (tex_count
== 0)) {
370 rs
->ip
[0] |= R500_RS_COL_FMT(R300_RS_COL_FMT_0001
);
374 for (i
= 0; i
< tex_count
; i
++) {
375 rs
->inst
[i
] |= R500_RS_INST_TEX_ID(i
) |
376 R500_RS_INST_TEX_CN_WRITE
| R500_RS_INST_TEX_ADDR(fp_offset
);
380 for (i
= 0; i
< col_count
; i
++) {
381 rs
->inst
[i
] |= R500_RS_INST_COL_ID(i
) |
382 R500_RS_INST_COL_CN_WRITE
| R500_RS_INST_COL_ADDR(fp_offset
);
386 for (i
= 0; i
< info
->num_inputs
; i
++) {
387 switch (info
->input_semantic_name
[i
]) {
388 case TGSI_SEMANTIC_COLOR
:
390 R300_RS_COL_PTR(col_count
) |
391 R300_RS_COL_FMT(R300_RS_COL_FMT_RGBA
);
394 case TGSI_SEMANTIC_GENERIC
:
396 R300_RS_TEX_PTR(rs_tex_comp
) |
397 R300_RS_SEL_S(R300_RS_SEL_C0
) |
398 R300_RS_SEL_T(R300_RS_SEL_C1
) |
399 R300_RS_SEL_R(R300_RS_SEL_C2
) |
400 R300_RS_SEL_Q(R300_RS_SEL_C3
);
409 if (col_count
== 0) {
410 rs
->ip
[0] |= R300_RS_COL_FMT(R300_RS_COL_FMT_0001
);
413 if (tex_count
== 0) {
415 R300_RS_SEL_S(R300_RS_SEL_K0
) |
416 R300_RS_SEL_T(R300_RS_SEL_K0
) |
417 R300_RS_SEL_R(R300_RS_SEL_K0
) |
418 R300_RS_SEL_Q(R300_RS_SEL_K1
);
421 /* Rasterize at least one color, or bad things happen. */
422 if ((col_count
== 0) && (tex_count
== 0)) {
426 for (i
= 0; i
< tex_count
; i
++) {
427 rs
->inst
[i
] |= R300_RS_INST_TEX_ID(i
) |
428 R300_RS_INST_TEX_CN_WRITE
| R300_RS_INST_TEX_ADDR(fp_offset
);
432 for (i
= 0; i
< col_count
; i
++) {
433 rs
->inst
[i
] |= R300_RS_INST_COL_ID(i
) |
434 R300_RS_INST_COL_CN_WRITE
| R300_RS_INST_COL_ADDR(fp_offset
);
439 rs
->count
= (rs_tex_comp
) | (col_count
<< R300_IC_COUNT_SHIFT
) |
442 rs
->inst_count
= MAX2(MAX2(col_count
- 1, tex_count
- 1), 0);
445 /* Update the vertex format. */
446 static void r300_update_derived_shader_state(struct r300_context
* r300
)
448 struct r300_shader_key
* key
;
449 struct r300_vertex_format
* vformat
;
450 struct r300_rs_block
* rs_block
;
451 struct r300_shader_derived_value
* value
;
454 key
= CALLOC_STRUCT(r300_shader_key
);
458 value
= (struct r300_shader_derived_value
*)
459 util_hash_table_get(r300
->shader_hash_table
, (void*)key
);
461 //vformat = value->vformat;
462 rs_block
= value
->rs_block
;
466 rs_block
= CALLOC_STRUCT(r300_rs_block
);
467 value
= CALLOC_STRUCT(r300_shader_derived_value
);
469 r300_update_rs_block(r300
, rs_block
);
471 //value->vformat = vformat;
472 value
->rs_block
= rs_block
;
473 util_hash_table_set(r300
->shader_hash_table
,
474 (void*)key
, (void*)value
);
477 /* XXX This will be refactored ASAP. */
478 vformat
= CALLOC_STRUCT(r300_vertex_format
);
480 for (i
= 0; i
< 16; i
++) {
481 vformat
->vs_tab
[i
] = -1;
482 vformat
->fs_tab
[i
] = -1;
485 r300_vs_tab_routes(r300
, vformat
);
486 r300_vertex_psc(r300
, vformat
);
487 r300_update_fs_tab(r300
, vformat
);
489 FREE(r300
->vertex_info
);
491 r300
->vertex_info
= vformat
;
492 r300
->rs_block
= rs_block
;
493 r300
->dirty_state
|= (R300_NEW_VERTEX_FORMAT
| R300_NEW_RS_BLOCK
);
496 static void r300_update_ztop(struct r300_context
* r300
)
498 r300
->ztop_state
.z_buffer_top
= R300_ZTOP_ENABLE
;
500 /* This is important enough that I felt it warranted a comment.
502 * According to the docs, these are the conditions where ZTOP must be
504 * 1) Alpha testing enabled
505 * 2) Texture kill instructions in fragment shader
506 * 3) Chroma key culling enabled
507 * 4) W-buffering enabled
509 * The docs claim that for the first three cases, if no ZS writes happen,
510 * then ZTOP can be used.
512 * Additionally, the following conditions require disabled ZTOP:
513 * ~) Depth writes in fragment shader
514 * ~) Outstanding occlusion queries
518 if (r300
->dsa_state
->alpha_function
) {
519 r300
->ztop_state
.z_buffer_top
= R300_ZTOP_DISABLE
;
520 } else if (r300
->fs
->info
.uses_kill
) {
521 r300
->ztop_state
.z_buffer_top
= R300_ZTOP_DISABLE
;
522 } else if (r300_fragment_shader_writes_depth(r300
->fs
)) {
523 r300
->ztop_state
.z_buffer_top
= R300_ZTOP_DISABLE
;
524 } else if (r300
->query_current
) {
525 r300
->ztop_state
.z_buffer_top
= R300_ZTOP_DISABLE
;
529 void r300_update_derived_state(struct r300_context
* r300
)
531 if (r300
->dirty_state
&
532 (R300_NEW_FRAGMENT_SHADER
| R300_NEW_VERTEX_SHADER
)) {
533 r300_update_derived_shader_state(r300
);
536 if (r300
->dirty_state
&
537 (R300_NEW_DSA
| R300_NEW_FRAGMENT_SHADER
| R300_NEW_QUERY
)) {
538 r300_update_ztop(r300
);