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"
25 /* r300_state_derived: Various bits of state which are dependent upon
26 * currently bound CSO data. */
28 /* Update the vertex_info struct in our r300_context.
30 * The vertex_info struct describes the post-TCL format of vertices. It is
31 * required for Draw when doing SW TCL, and also for describing the
32 * dreaded RS block on R300 chipsets. */
33 /* XXX this function should be able to handle vert shaders as well as draw */
34 static void r300_update_vertex_layout(struct r300_context
* r300
)
36 struct r300_vertex_format vformat
;
37 struct vertex_info vinfo
;
38 boolean pos
= FALSE
, psize
= FALSE
, fog
= FALSE
;
39 int i
, texs
= 0, cols
= 0;
42 struct tgsi_shader_info
* info
= &r300
->fs
->info
;
44 memset(&vinfo
, 0, sizeof(vinfo
));
45 for (i
= 0; i
< 16; i
++) {
49 assert(info
->num_inputs
<= 16);
51 for (i
= 0; i
< info
->num_inputs
; i
++) {
52 switch (info
->input_semantic_name
[i
]) {
53 case TGSI_SEMANTIC_POSITION
:
57 case TGSI_SEMANTIC_COLOR
:
60 case TGSI_SEMANTIC_FOG
:
64 case TGSI_SEMANTIC_PSIZE
:
68 case TGSI_SEMANTIC_GENERIC
:
72 debug_printf("r300: Unknown vertex input %d\n",
73 info
->input_semantic_name
[i
]);
78 /* Do the actual vertex_info setup.
80 * vertex_info has four uints of hardware-specific data in it.
81 * vinfo.hwfmt[0] is R300_VAP_VTX_STATE_CNTL
82 * vinfo.hwfmt[1] is R300_VAP_VSM_VTX_ASSM
83 * vinfo.hwfmt[2] is R300_VAP_OUTPUT_VTX_FMT_0
84 * vinfo.hwfmt[3] is R300_VAP_OUTPUT_VTX_FMT_1 */
86 vinfo
.hwfmt
[0] = 0x5555; /* XXX this is classic Mesa bonghits */
89 debug_printf("r300: Forcing vertex position attribute emit...\n");
90 /* Make room for the position attribute
91 * at the beginning of the tab. */
92 for (i
= 1; i
< 16; i
++) {
97 draw_emit_vertex_attr(&vinfo
, EMIT_4F
, INTERP_POS
,
98 draw_find_vs_output(r300
->draw
, TGSI_SEMANTIC_POSITION
, 0));
100 draw_emit_vertex_attr(&vinfo
, EMIT_4F
, INTERP_PERSPECTIVE
,
101 draw_find_vs_output(r300
->draw
, TGSI_SEMANTIC_POSITION
, 0));
103 vinfo
.hwfmt
[1] |= R300_INPUT_CNTL_POS
;
104 vinfo
.hwfmt
[2] |= R300_VAP_OUTPUT_VTX_FMT_0__POS_PRESENT
;
107 draw_emit_vertex_attr(&vinfo
, EMIT_1F_PSIZE
, INTERP_POS
,
108 draw_find_vs_output(r300
->draw
, TGSI_SEMANTIC_PSIZE
, 0));
109 vinfo
.hwfmt
[2] |= R300_VAP_OUTPUT_VTX_FMT_0__PT_SIZE_PRESENT
;
112 for (i
= 0; i
< cols
; i
++) {
113 draw_emit_vertex_attr(&vinfo
, EMIT_4F
, INTERP_LINEAR
,
114 draw_find_vs_output(r300
->draw
, TGSI_SEMANTIC_COLOR
, i
));
115 vinfo
.hwfmt
[1] |= R300_INPUT_CNTL_COLOR
;
116 vinfo
.hwfmt
[2] |= (R300_VAP_OUTPUT_VTX_FMT_0__COLOR_0_PRESENT
<< i
);
119 for (i
= 0; i
< texs
; i
++) {
120 draw_emit_vertex_attr(&vinfo
, EMIT_4F
, INTERP_PERSPECTIVE
,
121 draw_find_vs_output(r300
->draw
, TGSI_SEMANTIC_GENERIC
, i
));
122 vinfo
.hwfmt
[1] |= (R300_INPUT_CNTL_TC0
<< i
);
123 vinfo
.hwfmt
[3] |= (4 << (3 * i
));
128 draw_emit_vertex_attr(&vinfo
, EMIT_4F
, INTERP_PERSPECTIVE
,
129 draw_find_vs_output(r300
->draw
, TGSI_SEMANTIC_FOG
, 0));
130 vinfo
.hwfmt
[1] |= (R300_INPUT_CNTL_TC0
<< i
);
131 vinfo
.hwfmt
[3] |= (4 << (3 * i
));
134 draw_compute_vertex_size(&vinfo
);
136 if (memcmp(&r300
->vertex_info
, &vinfo
, sizeof(struct vertex_info
))) {
138 debug_printf("attrib count: %d, fp input count: %d\n",
139 vinfo
.num_attribs
, info
->num_inputs
);
140 for (i
= 0; i
< vinfo
.num_attribs
; i
++) {
141 debug_printf("attrib: offset %d, interp %d, size %d,"
142 " tab %d\n", vinfo
.attrib
[i
].src_index
,
143 vinfo
.attrib
[i
].interp_mode
, vinfo
.attrib
[i
].emit
,
147 for (i
= 0; i
< vinfo
.num_attribs
; i
++) {
148 /* Make sure we have a proper destination for our attribute */
149 assert(tab
[i
] != -1);
151 temp
= translate_vertex_data_type(vinfo
.attrib
[i
].emit
) |
152 (tab
[i
] << R300_DST_VEC_LOC_SHIFT
);
154 r300
->vertex_info
.vap_prog_stream_cntl
[i
>> 1] &= 0x0000ffff;
155 r300
->vertex_info
.vap_prog_stream_cntl
[i
>> 1] |= temp
<< 16;
157 r300
->vertex_info
.vap_prog_stream_cntl
[i
>> 1] &= 0xffff0000;
158 r300
->vertex_info
.vap_prog_stream_cntl
[i
>> 1] |= temp
;
161 r300
->vertex_info
.vap_prog_stream_cntl_ext
[i
>> 1] |=
162 (R300_VAP_SWIZZLE_XYZW
<< (i
& 1 ? 16 : 0));
164 r300
->vertex_info
.vap_prog_stream_cntl
[i
>> 1] |= (R300_LAST_VEC
<<
167 memcpy(r300
->vertex_info
.tab
, tab
, sizeof(tab
));
168 memcpy(&r300
->vertex_info
, &vinfo
, sizeof(struct vertex_info
));
169 r300
->dirty_state
|= R300_NEW_VERTEX_FORMAT
;
173 /* Set up the RS block. This is the part of the chipset that actually does
174 * the rasterization of vertices into fragments. This is also the part of the
175 * chipset that locks up if any part of it is even slightly wrong. */
176 static void r300_update_rs_block(struct r300_context
* r300
)
178 struct r300_rs_block
* rs
= r300
->rs_block
;
179 struct vertex_info
* vinfo
= &r300
->vertex_info
.vinfo
;
180 int* tab
= r300
->vertex_info
.tab
;
181 int col_count
= 0, fp_offset
= 0, i
, memory_pos
, tex_count
= 0;
183 memset(rs
, 0, sizeof(struct r300_rs_block
));
185 if (r300_screen(r300
->context
.screen
)->caps
->is_r500
) {
186 for (i
= 0; i
< vinfo
->num_attribs
; i
++) {
187 assert(tab
[vinfo
->attrib
[i
].src_index
] != -1);
188 memory_pos
= tab
[vinfo
->attrib
[i
].src_index
] * 4;
189 switch (vinfo
->attrib
[i
].interp_mode
) {
192 R500_RS_COL_PTR(memory_pos
) |
193 R500_RS_COL_FMT(R300_RS_COL_FMT_RGBA
);
196 case INTERP_PERSPECTIVE
:
198 R500_RS_SEL_S(memory_pos
) |
199 R500_RS_SEL_T(memory_pos
+ 1) |
200 R500_RS_SEL_R(memory_pos
+ 2) |
201 R500_RS_SEL_Q(memory_pos
+ 3);
209 /* Set up at least one texture pointer or RS will not be happy. */
210 if (tex_count
== 0) {
212 R500_RS_SEL_S(R500_RS_IP_PTR_K0
) |
213 R500_RS_SEL_T(R500_RS_IP_PTR_K0
) |
214 R500_RS_SEL_R(R500_RS_IP_PTR_K0
) |
215 R500_RS_SEL_Q(R500_RS_IP_PTR_K1
);
218 for (i
= 0; i
< tex_count
; i
++) {
219 rs
->inst
[i
] |= R500_RS_INST_TEX_ID(i
) | R500_RS_INST_TEX_CN_WRITE
|
220 R500_RS_INST_TEX_ADDR(fp_offset
);
224 for (i
= 0; i
< col_count
; i
++) {
225 rs
->inst
[i
] |= R500_RS_INST_COL_ID(i
) | R500_RS_INST_COL_CN_WRITE
|
226 R500_RS_INST_COL_ADDR(fp_offset
);
230 rs
->inst_count
= MAX2(col_count
, tex_count
);
232 for (i
= 0; i
< vinfo
->num_attribs
; i
++) {
233 memory_pos
= tab
[vinfo
->attrib
[i
].src_index
] * 4;
234 assert(tab
[vinfo
->attrib
[i
].src_index
] != -1);
235 switch (vinfo
->attrib
[i
].interp_mode
) {
238 R300_RS_COL_PTR(memory_pos
) |
239 R300_RS_COL_FMT(R300_RS_COL_FMT_RGBA
);
242 case INTERP_PERSPECTIVE
:
244 R300_RS_TEX_PTR(memory_pos
) |
245 R300_RS_SEL_S(R300_RS_SEL_C0
) |
246 R300_RS_SEL_T(R300_RS_SEL_C1
) |
247 R300_RS_SEL_R(R300_RS_SEL_C2
) |
248 R300_RS_SEL_Q(R300_RS_SEL_C3
);
256 if (tex_count
== 0) {
258 R300_RS_SEL_S(R300_RS_SEL_K0
) |
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
);
264 for (i
= 0; i
< 8; i
++)
265 debug_printf("ip %d: 0x%x\n", i
, rs
->ip
[i
]);
267 for (i
= 0; i
< tex_count
; i
++) {
268 rs
->inst
[i
] |= R300_RS_INST_TEX_ID(i
) | R300_RS_INST_TEX_CN_WRITE
|
269 R300_RS_INST_TEX_ADDR(fp_offset
);
273 for (i
= 0; i
< col_count
; i
++) {
274 rs
->inst
[i
] |= R300_RS_INST_COL_ID(i
) | R300_RS_INST_COL_CN_WRITE
|
275 R300_RS_INST_COL_ADDR(fp_offset
);
279 for (i
= 0; i
< 8; i
++)
280 debug_printf("inst %d: 0x%x\n", i
, rs
->inst
[i
]);
283 rs
->count
= (tex_count
* 4) | (col_count
<< R300_IC_COUNT_SHIFT
) |
286 rs
->inst_count
= MAX2(MAX2(col_count
- 1, tex_count
- 1), 0);
288 debug_printf("count: 0x%x, inst_count: 0x%x\n", rs
->count
, rs
->inst_count
);
291 void r300_update_derived_state(struct r300_context
* r300
)
293 if (r300
->dirty_state
& R300_NEW_FRAGMENT_SHADER
) {
294 r300_update_vertex_layout(r300
);
297 if (r300
->dirty_state
& R300_NEW_VERTEX_FORMAT
) {
298 r300_update_rs_block(r300
);