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 r300_screen
* r300screen
= r300_screen(r300
->context
.screen
);
69 struct vertex_info
* vinfo
= &vformat
->vinfo
;
70 int* tab
= vformat
->vs_tab
;
71 boolean pos
= FALSE
, psize
= FALSE
, fog
= FALSE
;
72 int i
, texs
= 0, cols
= 0;
73 struct tgsi_shader_info
* info
;
75 if (r300screen
->caps
->has_tcl
) {
76 /* Use vertex shader to determine required routes. */
77 info
= &r300
->vs
->info
;
79 /* Use fragment shader to determine required routes. */
80 info
= &r300
->fs
->info
;
83 assert(info
->num_inputs
<= 16);
85 if (!r300screen
->caps
->has_tcl
|| !r300
->rs_state
->enable_vte
)
87 for (i
= 0; i
< info
->num_inputs
; i
++) {
88 switch (r300
->vs
->code
.inputs
[i
]) {
89 case TGSI_SEMANTIC_POSITION
:
93 case TGSI_SEMANTIC_COLOR
:
97 case TGSI_SEMANTIC_PSIZE
:
98 assert(psize
== FALSE
);
102 case TGSI_SEMANTIC_FOG
:
103 assert(fog
== FALSE
);
106 case TGSI_SEMANTIC_GENERIC
:
111 debug_printf("r300: Unknown vertex input %d\n",
112 info
->input_semantic_name
[i
]);
119 /* Just copy vert attribs over as-is. */
120 for (i
= 0; i
< info
->num_inputs
; i
++) {
124 for (i
= 0; i
< info
->num_outputs
; i
++) {
125 switch (info
->output_semantic_name
[i
]) {
126 case TGSI_SEMANTIC_POSITION
:
129 case TGSI_SEMANTIC_COLOR
:
132 case TGSI_SEMANTIC_PSIZE
:
135 case TGSI_SEMANTIC_FOG
:
138 case TGSI_SEMANTIC_GENERIC
:
142 debug_printf("r300: Unknown vertex output %d\n",
143 info
->output_semantic_name
[i
]);
152 /* Do the actual vertex_info setup.
154 * vertex_info has four uints of hardware-specific data in it.
155 * vinfo.hwfmt[0] is R300_VAP_VTX_STATE_CNTL
156 * vinfo.hwfmt[1] is R300_VAP_VSM_VTX_ASSM
157 * vinfo.hwfmt[2] is R300_VAP_OUTPUT_VTX_FMT_0
158 * vinfo.hwfmt[3] is R300_VAP_OUTPUT_VTX_FMT_1 */
160 vinfo
->hwfmt
[0] = 0x5555; /* XXX this is classic Mesa bonghits */
162 /* We need to add vertex position attribute only for SW TCL case,
163 * for HW TCL case it could be generated by vertex shader */
164 if (!pos
&& !r300screen
->caps
->has_tcl
) {
165 debug_printf("r300: Forcing vertex position attribute emit...\n");
166 /* Make room for the position attribute
167 * at the beginning of the tab. */
168 for (i
= 15; i
> 0; i
--) {
176 draw_emit_vertex_attr(vinfo
, EMIT_4F
, INTERP_PERSPECTIVE
,
177 draw_find_vs_output(r300
->draw
, TGSI_SEMANTIC_POSITION
, 0));
179 vinfo
->hwfmt
[1] |= R300_INPUT_CNTL_POS
;
180 vinfo
->hwfmt
[2] |= R300_VAP_OUTPUT_VTX_FMT_0__POS_PRESENT
;
185 draw_emit_vertex_attr(vinfo
, EMIT_1F_PSIZE
, INTERP_POS
,
186 draw_find_vs_output(r300
->draw
, TGSI_SEMANTIC_PSIZE
, 0));
188 vinfo
->hwfmt
[2] |= R300_VAP_OUTPUT_VTX_FMT_0__PT_SIZE_PRESENT
;
192 for (i
= 0; i
< cols
; i
++) {
194 draw_emit_vertex_attr(vinfo
, EMIT_4F
, INTERP_LINEAR
,
195 draw_find_vs_output(r300
->draw
, TGSI_SEMANTIC_COLOR
, i
));
197 vinfo
->hwfmt
[1] |= R300_INPUT_CNTL_COLOR
;
198 vinfo
->hwfmt
[2] |= (R300_VAP_OUTPUT_VTX_FMT_0__COLOR_0_PRESENT
<< i
);
201 /* Init i right here, increment it if fog is enabled.
202 * This gets around a double-increment problem. */
205 /* Fog. This is a special-cased texcoord. */
209 draw_emit_vertex_attr(vinfo
, EMIT_4F
, INTERP_PERSPECTIVE
,
210 draw_find_vs_output(r300
->draw
, TGSI_SEMANTIC_FOG
, 0));
212 vinfo
->hwfmt
[1] |= (R300_INPUT_CNTL_TC0
<< i
);
213 vinfo
->hwfmt
[3] |= (4 << (3 * i
));
217 for (; i
< texs
; i
++) {
219 draw_emit_vertex_attr(vinfo
, EMIT_4F
, INTERP_PERSPECTIVE
,
220 draw_find_vs_output(r300
->draw
, TGSI_SEMANTIC_GENERIC
, i
));
222 vinfo
->hwfmt
[1] |= (R300_INPUT_CNTL_TC0
<< i
);
223 vinfo
->hwfmt
[3] |= (4 << (3 * i
));
226 draw_compute_vertex_size(vinfo
);
229 /* Update the PSC tables. */
230 static void r300_vertex_psc(struct r300_context
* r300
,
231 struct r300_vertex_info
* vformat
)
233 struct r300_screen
* r300screen
= r300_screen(r300
->context
.screen
);
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 /* Vertex shaders have no semantics on their inputs,
241 * so PSC should just route stuff based on their info,
242 * and not on attrib information. */
243 if (r300screen
->caps
->has_tcl
) {
244 attrib_count
= r300
->vs
->info
.num_inputs
;
245 DBG(r300
, DBG_DRAW
, "r300: routing %d attribs in psc for vs\n",
248 attrib_count
= vinfo
->num_attribs
;
249 DBG(r300
, DBG_DRAW
, "r300: attrib count: %d\n", attrib_count
);
250 for (i
= 0; i
< attrib_count
; i
++) {
251 DBG(r300
, DBG_DRAW
, "r300: attrib: offset %d, interp %d, size %d,"
252 " tab %d\n", vinfo
->attrib
[i
].src_index
,
253 vinfo
->attrib
[i
].interp_mode
, vinfo
->attrib
[i
].emit
,
258 for (i
= 0; i
< attrib_count
; i
++) {
259 /* Make sure we have a proper destination for our attribute. */
260 assert(tab
[i
] != -1);
262 format
= draw_translate_vinfo_format(vinfo
->attrib
[i
].emit
);
264 /* Obtain the type of data in this attribute. */
265 type
= r300_translate_vertex_data_type(format
) |
266 tab
[i
] << R300_DST_VEC_LOC_SHIFT
;
268 /* Obtain the swizzle for this attribute. Note that the default
269 * swizzle in the hardware is not XYZW! */
270 swizzle
= r300_translate_vertex_data_swizzle(format
);
272 /* Add the attribute to the PSC table. */
274 vformat
->vap_prog_stream_cntl
[i
>> 1] |= type
<< 16;
276 vformat
->vap_prog_stream_cntl_ext
[i
>> 1] |= swizzle
<< 16;
278 vformat
->vap_prog_stream_cntl
[i
>> 1] |= type
<< 0;
280 vformat
->vap_prog_stream_cntl_ext
[i
>> 1] |= swizzle
<< 0;
284 /* Set the last vector in the PSC. */
288 vformat
->vap_prog_stream_cntl
[i
>> 1] |=
289 (R300_LAST_VEC
<< (i
& 1 ? 16 : 0));
292 /* Set up the mappings from GB to US, for RS block. */
293 static void r300_update_fs_tab(struct r300_context
* r300
,
294 struct r300_vertex_info
* vformat
)
296 struct tgsi_shader_info
* info
= &r300
->fs
->info
;
297 int i
, cols
= 0, texs
= 0, cols_emitted
= 0;
298 int* tab
= vformat
->fs_tab
;
300 for (i
= 0; i
< 16; i
++) {
304 assert(info
->num_inputs
<= 16);
305 for (i
= 0; i
< info
->num_inputs
; i
++) {
306 switch (info
->input_semantic_name
[i
]) {
307 case TGSI_SEMANTIC_COLOR
:
308 tab
[i
] = INTERP_LINEAR
;
311 case TGSI_SEMANTIC_POSITION
:
312 case TGSI_SEMANTIC_PSIZE
:
313 debug_printf("r300: Implementation error: Can't use "
314 "pos attribs in fragshader yet!\n");
315 /* Pass through for now */
316 case TGSI_SEMANTIC_FOG
:
317 case TGSI_SEMANTIC_GENERIC
:
318 tab
[i
] = INTERP_PERSPECTIVE
;
321 debug_printf("r300: Unknown vertex input %d\n",
322 info
->input_semantic_name
[i
]);
327 /* Now that we know where everything is... */
328 DBG(r300
, DBG_DRAW
, "r300: fp input count: %d\n", info
->num_inputs
);
329 for (i
= 0; i
< info
->num_inputs
; i
++) {
332 DBG(r300
, DBG_DRAW
, "r300: attrib: "
333 "stack offset %d, color, tab %d\n",
335 tab
[i
] = cols_emitted
;
338 case INTERP_PERSPECTIVE
:
339 DBG(r300
, DBG_DRAW
, "r300: attrib: "
340 "stack offset %d, texcoord, tab %d\n",
342 tab
[i
] = cols
+ texs
;
346 debug_printf("r300: Implementation error: Bad fp interp!\n");
354 /* Set up the RS block. This is the part of the chipset that actually does
355 * the rasterization of vertices into fragments. This is also the part of the
356 * chipset that locks up if any part of it is even slightly wrong. */
357 static void r300_update_rs_block(struct r300_context
* r300
,
358 struct r300_rs_block
* rs
)
360 struct tgsi_shader_info
* info
= &r300
->fs
->info
;
361 int col_count
= 0, fp_offset
= 0, i
, tex_count
= 0;
364 if (r300_screen(r300
->context
.screen
)->caps
->is_r500
) {
365 for (i
= 0; i
< info
->num_inputs
; i
++) {
366 switch (info
->input_semantic_name
[i
]) {
367 case TGSI_SEMANTIC_COLOR
:
369 R500_RS_COL_PTR(col_count
) |
370 R500_RS_COL_FMT(R300_RS_COL_FMT_RGBA
);
373 case TGSI_SEMANTIC_GENERIC
:
375 R500_RS_SEL_S(rs_tex_comp
) |
376 R500_RS_SEL_T(rs_tex_comp
+ 1) |
377 R500_RS_SEL_R(rs_tex_comp
+ 2) |
378 R500_RS_SEL_Q(rs_tex_comp
+ 3);
387 /* Rasterize at least one color, or bad things happen. */
388 if ((col_count
== 0) && (tex_count
== 0)) {
389 rs
->ip
[0] |= R500_RS_COL_FMT(R300_RS_COL_FMT_0001
);
393 for (i
= 0; i
< tex_count
; i
++) {
394 rs
->inst
[i
] |= R500_RS_INST_TEX_ID(i
) |
395 R500_RS_INST_TEX_CN_WRITE
| R500_RS_INST_TEX_ADDR(fp_offset
);
399 for (i
= 0; i
< col_count
; i
++) {
400 rs
->inst
[i
] |= R500_RS_INST_COL_ID(i
) |
401 R500_RS_INST_COL_CN_WRITE
| R500_RS_INST_COL_ADDR(fp_offset
);
405 for (i
= 0; i
< info
->num_inputs
; i
++) {
406 switch (info
->input_semantic_name
[i
]) {
407 case TGSI_SEMANTIC_COLOR
:
409 R300_RS_COL_PTR(col_count
) |
410 R300_RS_COL_FMT(R300_RS_COL_FMT_RGBA
);
413 case TGSI_SEMANTIC_GENERIC
:
415 R300_RS_TEX_PTR(rs_tex_comp
) |
416 R300_RS_SEL_S(R300_RS_SEL_C0
) |
417 R300_RS_SEL_T(R300_RS_SEL_C1
) |
418 R300_RS_SEL_R(R300_RS_SEL_C2
) |
419 R300_RS_SEL_Q(R300_RS_SEL_C3
);
428 if (col_count
== 0) {
429 rs
->ip
[0] |= R300_RS_COL_FMT(R300_RS_COL_FMT_0001
);
432 if (tex_count
== 0) {
434 R300_RS_SEL_S(R300_RS_SEL_K0
) |
435 R300_RS_SEL_T(R300_RS_SEL_K0
) |
436 R300_RS_SEL_R(R300_RS_SEL_K0
) |
437 R300_RS_SEL_Q(R300_RS_SEL_K1
);
440 /* Rasterize at least one color, or bad things happen. */
441 if ((col_count
== 0) && (tex_count
== 0)) {
445 for (i
= 0; i
< tex_count
; i
++) {
446 rs
->inst
[i
] |= R300_RS_INST_TEX_ID(i
) |
447 R300_RS_INST_TEX_CN_WRITE
| R300_RS_INST_TEX_ADDR(fp_offset
);
451 for (i
= 0; i
< col_count
; i
++) {
452 rs
->inst
[i
] |= R300_RS_INST_COL_ID(i
) |
453 R300_RS_INST_COL_CN_WRITE
| R300_RS_INST_COL_ADDR(fp_offset
);
458 rs
->count
= (rs_tex_comp
) | (col_count
<< R300_IC_COUNT_SHIFT
) |
461 rs
->inst_count
= MAX2(MAX2(col_count
- 1, tex_count
- 1), 0);
464 /* Update the vertex format. */
465 static void r300_update_derived_shader_state(struct r300_context
* r300
)
467 struct r300_screen
* r300screen
= r300_screen(r300
->context
.screen
);
468 struct r300_vertex_info
* vformat
;
469 struct r300_rs_block
* rs_block
;
473 struct r300_shader_key* key;
474 struct r300_shader_derived_value* value;
475 key = CALLOC_STRUCT(r300_shader_key);
479 value = (struct r300_shader_derived_value*)
480 util_hash_table_get(r300->shader_hash_table, (void*)key);
482 //vformat = value->vformat;
483 rs_block = value->rs_block;
487 rs_block = CALLOC_STRUCT(r300_rs_block);
488 value = CALLOC_STRUCT(r300_shader_derived_value);
490 r300_update_rs_block(r300, rs_block);
492 //value->vformat = vformat;
493 value->rs_block = rs_block;
494 util_hash_table_set(r300->shader_hash_table,
495 (void*)key, (void*)value);
498 /* XXX This will be refactored ASAP. */
499 vformat
= CALLOC_STRUCT(r300_vertex_info
);
500 rs_block
= CALLOC_STRUCT(r300_rs_block
);
502 for (i
= 0; i
< 16; i
++) {
503 vformat
->vs_tab
[i
] = -1;
504 vformat
->fs_tab
[i
] = -1;
507 r300_vs_tab_routes(r300
, vformat
);
508 r300_vertex_psc(r300
, vformat
);
509 r300_update_fs_tab(r300
, vformat
);
511 r300_update_rs_block(r300
, rs_block
);
513 FREE(r300
->vertex_info
);
514 FREE(r300
->rs_block
);
516 r300
->vertex_info
= vformat
;
517 r300
->rs_block
= rs_block
;
518 r300
->dirty_state
|= (R300_NEW_VERTEX_FORMAT
| R300_NEW_RS_BLOCK
);
521 static void r300_update_ztop(struct r300_context
* r300
)
523 r300
->ztop_state
.z_buffer_top
= R300_ZTOP_ENABLE
;
525 /* This is important enough that I felt it warranted a comment.
527 * According to the docs, these are the conditions where ZTOP must be
529 * 1) Alpha testing enabled
530 * 2) Texture kill instructions in fragment shader
531 * 3) Chroma key culling enabled
532 * 4) W-buffering enabled
534 * The docs claim that for the first three cases, if no ZS writes happen,
535 * then ZTOP can be used.
537 * Additionally, the following conditions require disabled ZTOP:
538 * ~) Depth writes in fragment shader
539 * ~) Outstanding occlusion queries
543 if (r300
->dsa_state
->alpha_function
) {
544 r300
->ztop_state
.z_buffer_top
= R300_ZTOP_DISABLE
;
545 } else if (r300
->fs
->info
.uses_kill
) {
546 r300
->ztop_state
.z_buffer_top
= R300_ZTOP_DISABLE
;
547 } else if (r300_fragment_shader_writes_depth(r300
->fs
)) {
548 r300
->ztop_state
.z_buffer_top
= R300_ZTOP_DISABLE
;
549 } else if (r300
->query_current
) {
550 r300
->ztop_state
.z_buffer_top
= R300_ZTOP_DISABLE
;
554 void r300_update_derived_state(struct r300_context
* r300
)
557 if (TRUE
|| r300
->dirty_state
&
558 (R300_NEW_FRAGMENT_SHADER
| R300_NEW_VERTEX_SHADER
)) {
559 r300_update_derived_shader_state(r300
);
562 if (r300
->dirty_state
&
563 (R300_NEW_DSA
| R300_NEW_FRAGMENT_SHADER
| R300_NEW_QUERY
)) {
564 r300_update_ztop(r300
);