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 "draw/draw_context.h"
25 #include "util/u_math.h"
26 #include "util/u_memory.h"
28 #include "r300_context.h"
30 #include "r300_screen.h"
31 #include "r300_state_derived.h"
32 #include "r300_state_inlines.h"
35 /* r300_state_derived: Various bits of state which are dependent upon
36 * currently bound CSO data. */
38 struct r300_shader_key
{
39 struct r300_vertex_shader
* vs
;
40 struct r300_fragment_shader
* fs
;
43 struct r300_shader_derived_value
{
44 struct r300_vertex_format
* vformat
;
45 struct r300_rs_block
* rs_block
;
48 unsigned r300_shader_key_hash(void* key
) {
49 struct r300_shader_key
* shader_key
= (struct r300_shader_key
*)key
;
50 unsigned vs
= (unsigned)shader_key
->vs
;
51 unsigned fs
= (unsigned)shader_key
->fs
;
53 return (vs
<< 16) | (fs
& 0xffff);
56 int r300_shader_key_compare(void* key1
, void* key2
) {
57 struct r300_shader_key
* shader_key1
= (struct r300_shader_key
*)key1
;
58 struct r300_shader_key
* shader_key2
= (struct r300_shader_key
*)key2
;
60 return (shader_key1
->vs
== shader_key2
->vs
) &&
61 (shader_key1
->fs
== shader_key2
->fs
);
64 /* Set up the vs_tab and routes. */
65 static void r300_vs_tab_routes(struct r300_context
* r300
,
66 struct r300_vertex_info
* vformat
)
68 struct vertex_info
* vinfo
= &vformat
->vinfo
;
69 int* tab
= vformat
->vs_tab
;
70 boolean pos
= FALSE
, psize
= FALSE
, fog
= FALSE
;
71 int i
, texs
= 0, cols
= 0;
72 struct tgsi_shader_info
* info
= &r300
->fs
->info
;
74 /* XXX One day we should figure out how to handle a different number of
75 * VS outputs and FS inputs, as well as a different number of vertex streams
76 * and VS inputs. It's definitely one of the sources of hardlocks. */
78 for (i
= 0; i
< info
->num_inputs
; i
++) {
79 switch (info
->input_semantic_name
[i
]) {
80 case TGSI_SEMANTIC_POSITION
:
84 case TGSI_SEMANTIC_COLOR
:
88 case TGSI_SEMANTIC_PSIZE
:
89 assert(psize
== FALSE
);
93 case TGSI_SEMANTIC_FOG
:
97 case TGSI_SEMANTIC_GENERIC
:
102 debug_printf("r300: Unknown vertex input %d\n",
103 info
->input_semantic_name
[i
]);
111 /* Do the actual vertex_info setup.
113 * vertex_info has four uints of hardware-specific data in it.
114 * vinfo.hwfmt[0] is R300_VAP_VTX_STATE_CNTL
115 * vinfo.hwfmt[1] is R300_VAP_VSM_VTX_ASSM
116 * vinfo.hwfmt[2] is R300_VAP_OUTPUT_VTX_FMT_0
117 * vinfo.hwfmt[3] is R300_VAP_OUTPUT_VTX_FMT_1 */
119 vinfo
->hwfmt
[0] = 0x5555; /* XXX this is classic Mesa bonghits */
121 /* We need to add vertex position attribute only for SW TCL case,
122 * for HW TCL case it could be generated by vertex shader */
124 /* Make room for the position attribute
125 * at the beginning of the tab. */
126 for (i
= 15; i
> 0; i
--) {
134 draw_emit_vertex_attr(vinfo
, EMIT_4F
, INTERP_PERSPECTIVE
,
135 draw_find_vs_output(r300
->draw
, TGSI_SEMANTIC_POSITION
, 0));
137 vinfo
->hwfmt
[1] |= R300_INPUT_CNTL_POS
;
138 vinfo
->hwfmt
[2] |= R300_VAP_OUTPUT_VTX_FMT_0__POS_PRESENT
;
143 draw_emit_vertex_attr(vinfo
, EMIT_1F_PSIZE
, INTERP_POS
,
144 draw_find_vs_output(r300
->draw
, TGSI_SEMANTIC_PSIZE
, 0));
146 vinfo
->hwfmt
[2] |= R300_VAP_OUTPUT_VTX_FMT_0__PT_SIZE_PRESENT
;
150 for (i
= 0; i
< cols
; i
++) {
152 draw_emit_vertex_attr(vinfo
, EMIT_4F
, INTERP_LINEAR
,
153 draw_find_vs_output(r300
->draw
, TGSI_SEMANTIC_COLOR
, i
));
155 vinfo
->hwfmt
[1] |= R300_INPUT_CNTL_COLOR
;
156 vinfo
->hwfmt
[2] |= (R300_VAP_OUTPUT_VTX_FMT_0__COLOR_0_PRESENT
<< i
);
159 /* Init i right here, increment it if fog is enabled.
160 * This gets around a double-increment problem. */
163 /* Fog. This is a special-cased texcoord. */
167 draw_emit_vertex_attr(vinfo
, EMIT_4F
, INTERP_PERSPECTIVE
,
168 draw_find_vs_output(r300
->draw
, TGSI_SEMANTIC_FOG
, 0));
170 vinfo
->hwfmt
[1] |= (R300_INPUT_CNTL_TC0
<< i
);
171 vinfo
->hwfmt
[3] |= (4 << (3 * i
));
175 for (; i
< texs
; i
++) {
177 draw_emit_vertex_attr(vinfo
, EMIT_4F
, INTERP_PERSPECTIVE
,
178 draw_find_vs_output(r300
->draw
, TGSI_SEMANTIC_GENERIC
, i
));
180 vinfo
->hwfmt
[1] |= (R300_INPUT_CNTL_TC0
<< i
);
181 vinfo
->hwfmt
[3] |= (4 << (3 * i
));
184 draw_compute_vertex_size(vinfo
);
187 /* Update the PSC tables. */
188 static void r300_vertex_psc(struct r300_context
* r300
,
189 struct r300_vertex_info
* vformat
)
191 uint16_t type
, swizzle
;
192 enum pipe_format format
;
195 /* Vertex shaders have no semantics on their inputs,
196 * so PSC should just route stuff based on the vertex elements,
197 * and not on attrib information. */
198 DBG(r300
, DBG_DRAW
, "r300: vs expects %d attribs, routing %d elements"
200 r300
->vs
->info
.num_inputs
,
201 r300
->vertex_element_count
);
203 for (i
= 0; i
< r300
->vertex_element_count
; i
++) {
204 format
= r300
->vertex_element
[i
].src_format
;
206 type
= r300_translate_vertex_data_type(format
) |
207 (i
<< R300_DST_VEC_LOC_SHIFT
);
208 swizzle
= r300_translate_vertex_data_swizzle(format
);
211 vformat
->vap_prog_stream_cntl
[i
>> 1] |= type
<< 16;
212 vformat
->vap_prog_stream_cntl_ext
[i
>> 1] |= swizzle
<< 16;
214 vformat
->vap_prog_stream_cntl
[i
>> 1] |= type
;
215 vformat
->vap_prog_stream_cntl_ext
[i
>> 1] |= swizzle
;
222 /* Set the last vector in the PSC. */
226 vformat
->vap_prog_stream_cntl
[i
>> 1] |=
227 (R300_LAST_VEC
<< (i
& 1 ? 16 : 0));
230 /* Update the PSC tables for SW TCL, using Draw. */
231 static void r300_swtcl_vertex_psc(struct r300_context
* r300
,
232 struct r300_vertex_info
* vformat
)
234 struct vertex_info
* vinfo
= &vformat
->vinfo
;
235 int* tab
= vformat
->vs_tab
;
236 uint16_t type
, swizzle
;
237 enum pipe_format format
;
238 unsigned i
, attrib_count
;
240 /* For each Draw attribute, route it to the fragment shader according
242 attrib_count
= vinfo
->num_attribs
;
243 DBG(r300
, DBG_DRAW
, "r300: attrib count: %d\n", attrib_count
);
244 for (i
= 0; i
< attrib_count
; i
++) {
245 DBG(r300
, DBG_DRAW
, "r300: attrib: offset %d, interp %d, size %d,"
246 " tab %d\n", vinfo
->attrib
[i
].src_index
,
247 vinfo
->attrib
[i
].interp_mode
, vinfo
->attrib
[i
].emit
,
251 for (i
= 0; i
< attrib_count
; i
++) {
252 /* Make sure we have a proper destination for our attribute. */
253 assert(tab
[i
] != -1);
255 format
= draw_translate_vinfo_format(vinfo
->attrib
[i
].emit
);
257 /* Obtain the type of data in this attribute. */
258 type
= r300_translate_vertex_data_type(format
) |
259 tab
[i
] << R300_DST_VEC_LOC_SHIFT
;
261 /* Obtain the swizzle for this attribute. Note that the default
262 * swizzle in the hardware is not XYZW! */
263 swizzle
= r300_translate_vertex_data_swizzle(format
);
265 /* Add the attribute to the PSC table. */
267 vformat
->vap_prog_stream_cntl
[i
>> 1] |= type
<< 16;
268 vformat
->vap_prog_stream_cntl_ext
[i
>> 1] |= swizzle
<< 16;
270 vformat
->vap_prog_stream_cntl
[i
>> 1] |= type
;
271 vformat
->vap_prog_stream_cntl_ext
[i
>> 1] |= swizzle
;
275 /* Set the last vector in the PSC. */
279 vformat
->vap_prog_stream_cntl
[i
>> 1] |=
280 (R300_LAST_VEC
<< (i
& 1 ? 16 : 0));
283 /* Set up the mappings from GB to US, for RS block. */
284 static void r300_update_fs_tab(struct r300_context
* r300
,
285 struct r300_vertex_info
* vformat
)
287 struct tgsi_shader_info
* info
= &r300
->fs
->info
;
288 int i
, cols
= 0, texs
= 0, cols_emitted
= 0;
289 int* tab
= vformat
->fs_tab
;
291 for (i
= 0; i
< 16; i
++) {
295 assert(info
->num_inputs
<= 16);
296 for (i
= 0; i
< info
->num_inputs
; i
++) {
297 switch (info
->input_semantic_name
[i
]) {
298 case TGSI_SEMANTIC_COLOR
:
299 tab
[i
] = INTERP_LINEAR
;
302 case TGSI_SEMANTIC_POSITION
:
303 case TGSI_SEMANTIC_PSIZE
:
304 debug_printf("r300: Implementation error: Can't use "
305 "pos attribs in fragshader yet!\n");
306 /* Pass through for now */
307 case TGSI_SEMANTIC_FOG
:
308 case TGSI_SEMANTIC_GENERIC
:
309 tab
[i
] = INTERP_PERSPECTIVE
;
312 debug_printf("r300: Unknown vertex input %d\n",
313 info
->input_semantic_name
[i
]);
318 /* Now that we know where everything is... */
319 DBG(r300
, DBG_DRAW
, "r300: fp input count: %d\n", info
->num_inputs
);
320 for (i
= 0; i
< info
->num_inputs
; i
++) {
323 DBG(r300
, DBG_DRAW
, "r300: attrib: "
324 "stack offset %d, color, tab %d\n",
326 tab
[i
] = cols_emitted
;
329 case INTERP_PERSPECTIVE
:
330 DBG(r300
, DBG_DRAW
, "r300: attrib: "
331 "stack offset %d, texcoord, tab %d\n",
333 tab
[i
] = cols
+ texs
;
337 debug_printf("r300: Implementation error: Bad fp interp!\n");
345 /* Set up the RS block. This is the part of the chipset that actually does
346 * the rasterization of vertices into fragments. This is also the part of the
347 * chipset that locks up if any part of it is even slightly wrong. */
348 static void r300_update_rs_block(struct r300_context
* r300
,
349 struct r300_rs_block
* rs
)
351 struct tgsi_shader_info
* info
= &r300
->fs
->info
;
352 int col_count
= 0, fp_offset
= 0, i
, tex_count
= 0;
355 if (r300_screen(r300
->context
.screen
)->caps
->is_r500
) {
356 for (i
= 0; i
< info
->num_inputs
; i
++) {
357 switch (info
->input_semantic_name
[i
]) {
358 case TGSI_SEMANTIC_COLOR
:
360 R500_RS_COL_PTR(col_count
) |
361 R500_RS_COL_FMT(R300_RS_COL_FMT_RGBA
);
364 case TGSI_SEMANTIC_GENERIC
:
366 R500_RS_SEL_S(rs_tex_comp
) |
367 R500_RS_SEL_T(rs_tex_comp
+ 1) |
368 R500_RS_SEL_R(rs_tex_comp
+ 2) |
369 R500_RS_SEL_Q(rs_tex_comp
+ 3);
378 /* Rasterize at least one color, or bad things happen. */
379 if ((col_count
== 0) && (tex_count
== 0)) {
380 rs
->ip
[0] |= R500_RS_COL_FMT(R300_RS_COL_FMT_0001
);
384 for (i
= 0; i
< tex_count
; i
++) {
385 rs
->inst
[i
] |= R500_RS_INST_TEX_ID(i
) |
386 R500_RS_INST_TEX_CN_WRITE
| R500_RS_INST_TEX_ADDR(fp_offset
);
390 for (i
= 0; i
< col_count
; i
++) {
391 rs
->inst
[i
] |= R500_RS_INST_COL_ID(i
) |
392 R500_RS_INST_COL_CN_WRITE
| R500_RS_INST_COL_ADDR(fp_offset
);
396 for (i
= 0; i
< info
->num_inputs
; i
++) {
397 switch (info
->input_semantic_name
[i
]) {
398 case TGSI_SEMANTIC_COLOR
:
400 R300_RS_COL_PTR(col_count
) |
401 R300_RS_COL_FMT(R300_RS_COL_FMT_RGBA
);
404 case TGSI_SEMANTIC_GENERIC
:
406 R300_RS_TEX_PTR(rs_tex_comp
) |
407 R300_RS_SEL_S(R300_RS_SEL_C0
) |
408 R300_RS_SEL_T(R300_RS_SEL_C1
) |
409 R300_RS_SEL_R(R300_RS_SEL_C2
) |
410 R300_RS_SEL_Q(R300_RS_SEL_C3
);
419 if (col_count
== 0) {
420 rs
->ip
[0] |= R300_RS_COL_FMT(R300_RS_COL_FMT_0001
);
423 if (tex_count
== 0) {
425 R300_RS_SEL_S(R300_RS_SEL_K0
) |
426 R300_RS_SEL_T(R300_RS_SEL_K0
) |
427 R300_RS_SEL_R(R300_RS_SEL_K0
) |
428 R300_RS_SEL_Q(R300_RS_SEL_K1
);
431 /* Rasterize at least one color, or bad things happen. */
432 if ((col_count
== 0) && (tex_count
== 0)) {
436 for (i
= 0; i
< tex_count
; i
++) {
437 rs
->inst
[i
] |= R300_RS_INST_TEX_ID(i
) |
438 R300_RS_INST_TEX_CN_WRITE
| R300_RS_INST_TEX_ADDR(fp_offset
);
442 for (i
= 0; i
< col_count
; i
++) {
443 rs
->inst
[i
] |= R300_RS_INST_COL_ID(i
) |
444 R300_RS_INST_COL_CN_WRITE
| R300_RS_INST_COL_ADDR(fp_offset
);
449 rs
->count
= (rs_tex_comp
) | (col_count
<< R300_IC_COUNT_SHIFT
) |
452 rs
->inst_count
= MAX3(col_count
- 1, tex_count
- 1, 0);
455 /* Update the vertex format. */
456 static void r300_update_derived_shader_state(struct r300_context
* r300
)
458 struct r300_screen
* r300screen
= r300_screen(r300
->context
.screen
);
459 struct r300_vertex_info
* vformat
;
460 struct r300_rs_block
* rs_block
;
464 struct r300_shader_key* key;
465 struct r300_shader_derived_value* value;
466 key = CALLOC_STRUCT(r300_shader_key);
470 value = (struct r300_shader_derived_value*)
471 util_hash_table_get(r300->shader_hash_table, (void*)key);
473 //vformat = value->vformat;
474 rs_block = value->rs_block;
478 rs_block = CALLOC_STRUCT(r300_rs_block);
479 value = CALLOC_STRUCT(r300_shader_derived_value);
481 r300_update_rs_block(r300, rs_block);
483 //value->vformat = vformat;
484 value->rs_block = rs_block;
485 util_hash_table_set(r300->shader_hash_table,
486 (void*)key, (void*)value);
489 /* XXX This will be refactored ASAP. */
490 vformat
= CALLOC_STRUCT(r300_vertex_info
);
491 rs_block
= CALLOC_STRUCT(r300_rs_block
);
493 for (i
= 0; i
< 16; i
++) {
494 vformat
->vs_tab
[i
] = -1;
495 vformat
->fs_tab
[i
] = -1;
498 r300_vs_tab_routes(r300
, vformat
);
500 if (r300screen
->caps
->has_tcl
) {
501 r300_vertex_psc(r300
, vformat
);
503 r300_swtcl_vertex_psc(r300
, vformat
);
506 r300_update_fs_tab(r300
, vformat
);
508 r300_update_rs_block(r300
, rs_block
);
510 FREE(r300
->vertex_info
);
511 FREE(r300
->rs_block
);
513 r300
->vertex_info
= vformat
;
514 r300
->rs_block
= rs_block
;
515 r300
->dirty_state
|= (R300_NEW_VERTEX_FORMAT
| R300_NEW_RS_BLOCK
);
518 static void r300_update_ztop(struct r300_context
* r300
)
520 r300
->ztop_state
.z_buffer_top
= R300_ZTOP_ENABLE
;
522 /* This is important enough that I felt it warranted a comment.
524 * According to the docs, these are the conditions where ZTOP must be
526 * 1) Alpha testing enabled
527 * 2) Texture kill instructions in fragment shader
528 * 3) Chroma key culling enabled
529 * 4) W-buffering enabled
531 * The docs claim that for the first three cases, if no ZS writes happen,
532 * then ZTOP can be used.
534 * Additionally, the following conditions require disabled ZTOP:
535 * ~) Depth writes in fragment shader
536 * ~) Outstanding occlusion queries
540 if (r300
->dsa_state
->alpha_function
) {
541 r300
->ztop_state
.z_buffer_top
= R300_ZTOP_DISABLE
;
542 } else if (r300
->fs
->info
.uses_kill
) {
543 r300
->ztop_state
.z_buffer_top
= R300_ZTOP_DISABLE
;
544 } else if (r300_fragment_shader_writes_depth(r300
->fs
)) {
545 r300
->ztop_state
.z_buffer_top
= R300_ZTOP_DISABLE
;
546 } else if (r300
->query_current
) {
547 r300
->ztop_state
.z_buffer_top
= R300_ZTOP_DISABLE
;
551 void r300_update_derived_state(struct r300_context
* r300
)
553 if (r300
->dirty_state
&
554 (R300_NEW_FRAGMENT_SHADER
| R300_NEW_VERTEX_SHADER
)) {
555 r300_update_derived_shader_state(r300
);
558 if (r300
->dirty_state
&
559 (R300_NEW_DSA
| R300_NEW_FRAGMENT_SHADER
| R300_NEW_QUERY
)) {
560 r300_update_ztop(r300
);