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 /* Set up the vs_tab and routes. */
33 static void r300_vs_tab_routes(struct r300_context
* r300
,
34 struct r300_vertex_format
* vformat
)
36 struct r300_screen
* r300screen
= r300_screen(r300
->context
.screen
);
37 struct vertex_info
* vinfo
= &vformat
->vinfo
;
38 int* tab
= vformat
->vs_tab
;
39 boolean pos
= FALSE
, psize
= FALSE
, fog
= FALSE
;
40 int i
, texs
= 0, cols
= 0;
41 struct tgsi_shader_info
* info
;
43 if (r300screen
->caps
->has_tcl
) {
44 /* Use vertex shader to determine required routes. */
45 info
= &r300
->vs
->info
;
47 /* Use fragment shader to determine required routes. */
48 info
= &r300
->fs
->info
;
51 assert(info
->num_inputs
<= 16);
53 if (!r300screen
->caps
->has_tcl
|| !r300
->rs_state
->enable_vte
)
55 for (i
= 0; i
< info
->num_inputs
; i
++) {
56 switch (r300
->vs
->code
.inputs
[i
]) {
57 case TGSI_SEMANTIC_POSITION
:
61 case TGSI_SEMANTIC_COLOR
:
65 case TGSI_SEMANTIC_PSIZE
:
66 assert(psize
== FALSE
);
70 case TGSI_SEMANTIC_FOG
:
74 case TGSI_SEMANTIC_GENERIC
:
79 debug_printf("r300: Unknown vertex input %d\n",
80 info
->input_semantic_name
[i
]);
87 /* Just copy vert attribs over as-is. */
88 for (i
= 0; i
< info
->num_inputs
; i
++) {
92 for (i
= 0; i
< info
->num_outputs
; i
++) {
93 switch (info
->output_semantic_name
[i
]) {
94 case TGSI_SEMANTIC_POSITION
:
97 case TGSI_SEMANTIC_COLOR
:
100 case TGSI_SEMANTIC_PSIZE
:
103 case TGSI_SEMANTIC_FOG
:
106 case TGSI_SEMANTIC_GENERIC
:
110 debug_printf("r300: Unknown vertex output %d\n",
111 info
->output_semantic_name
[i
]);
120 /* Do the actual vertex_info setup.
122 * vertex_info has four uints of hardware-specific data in it.
123 * vinfo.hwfmt[0] is R300_VAP_VTX_STATE_CNTL
124 * vinfo.hwfmt[1] is R300_VAP_VSM_VTX_ASSM
125 * vinfo.hwfmt[2] is R300_VAP_OUTPUT_VTX_FMT_0
126 * vinfo.hwfmt[3] is R300_VAP_OUTPUT_VTX_FMT_1 */
128 vinfo
->hwfmt
[0] = 0x5555; /* XXX this is classic Mesa bonghits */
130 /* We need to add vertex position attribute only for SW TCL case,
131 * for HW TCL case it could be generated by vertex shader */
132 if (!pos
&& !r300screen
->caps
->has_tcl
) {
133 debug_printf("r300: Forcing vertex position attribute emit...\n");
134 /* Make room for the position attribute
135 * at the beginning of the tab. */
136 for (i
= 15; i
> 0; i
--) {
141 draw_emit_vertex_attr(vinfo
, EMIT_4F
, INTERP_PERSPECTIVE
,
142 draw_find_vs_output(r300
->draw
, TGSI_SEMANTIC_POSITION
, 0));
143 vinfo
->hwfmt
[1] |= R300_INPUT_CNTL_POS
;
144 vinfo
->hwfmt
[2] |= R300_VAP_OUTPUT_VTX_FMT_0__POS_PRESENT
;
147 draw_emit_vertex_attr(vinfo
, EMIT_1F_PSIZE
, INTERP_POS
,
148 draw_find_vs_output(r300
->draw
, TGSI_SEMANTIC_PSIZE
, 0));
149 vinfo
->hwfmt
[2] |= R300_VAP_OUTPUT_VTX_FMT_0__PT_SIZE_PRESENT
;
152 for (i
= 0; i
< cols
; i
++) {
153 draw_emit_vertex_attr(vinfo
, EMIT_4F
, INTERP_LINEAR
,
154 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. */
165 draw_emit_vertex_attr(vinfo
, EMIT_4F
, INTERP_PERSPECTIVE
,
166 draw_find_vs_output(r300
->draw
, TGSI_SEMANTIC_FOG
, 0));
167 vinfo
->hwfmt
[1] |= (R300_INPUT_CNTL_TC0
<< i
);
168 vinfo
->hwfmt
[3] |= (4 << (3 * i
));
171 for (; i
< texs
; i
++) {
172 draw_emit_vertex_attr(vinfo
, EMIT_4F
, INTERP_PERSPECTIVE
,
173 draw_find_vs_output(r300
->draw
, TGSI_SEMANTIC_GENERIC
, i
));
174 vinfo
->hwfmt
[1] |= (R300_INPUT_CNTL_TC0
<< i
);
175 vinfo
->hwfmt
[3] |= (4 << (3 * i
));
178 /* Handle the case where the vertex shader will be generating some of
179 * the attribs based on its inputs. */
180 if (r300screen
->caps
->has_tcl
&&
181 info
->num_inputs
< info
->num_outputs
) {
182 vinfo
->num_attribs
= info
->num_inputs
;
185 draw_compute_vertex_size(vinfo
);
188 /* Update the PSC tables. */
189 static void r300_vertex_psc(struct r300_context
* r300
,
190 struct r300_vertex_format
* vformat
)
192 struct r300_screen
* r300screen
= r300_screen(r300
->context
.screen
);
193 struct vertex_info
* vinfo
= &vformat
->vinfo
;
194 int* tab
= vformat
->vs_tab
;
198 /* Vertex shaders have no semantics on their inputs,
199 * so PSC should just route stuff based on their info,
200 * and not on attrib information. */
201 if (r300screen
->caps
->has_tcl
) {
202 attrib_count
= r300
->vs
->info
.num_inputs
;
203 DBG(r300
, DBG_DRAW
, "r300: routing %d attribs in psc for vs\n",
206 attrib_count
= vinfo
->num_attribs
;
207 DBG(r300
, DBG_DRAW
, "r300: attrib count: %d\n", attrib_count
);
208 for (i
= 0; i
< attrib_count
; i
++) {
209 DBG(r300
, DBG_DRAW
, "r300: attrib: offset %d, interp %d, size %d,"
210 " tab %d\n", vinfo
->attrib
[i
].src_index
,
211 vinfo
->attrib
[i
].interp_mode
, vinfo
->attrib
[i
].emit
,
216 for (i
= 0; i
< attrib_count
; i
++) {
217 /* Make sure we have a proper destination for our attribute */
218 assert(tab
[i
] != -1);
220 /* Add the attribute to the PSC table. */
221 temp
= r300screen
->caps
->has_tcl
?
222 R300_DATA_TYPE_FLOAT_4
:
223 translate_vertex_data_type(vinfo
->attrib
[i
].emit
);
224 temp
|= tab
[i
] << R300_DST_VEC_LOC_SHIFT
;
227 vformat
->vap_prog_stream_cntl
[i
>> 1] &= 0x0000ffff;
228 vformat
->vap_prog_stream_cntl
[i
>> 1] |= temp
<< 16;
230 vformat
->vap_prog_stream_cntl_ext
[i
>> 1] |=
231 (R300_VAP_SWIZZLE_XYZW
<< 16);
233 vformat
->vap_prog_stream_cntl
[i
>> 1] &= 0xffff0000;
234 vformat
->vap_prog_stream_cntl
[i
>> 1] |= temp
<< 0;
236 vformat
->vap_prog_stream_cntl_ext
[i
>> 1] |=
237 (R300_VAP_SWIZZLE_XYZW
<< 0);
241 /* Set the last vector in the PSC. */
243 vformat
->vap_prog_stream_cntl
[i
>> 1] |=
244 (R300_LAST_VEC
<< (i
& 1 ? 16 : 0));
247 /* Update the vertex format. */
248 static void r300_update_vertex_format(struct r300_context
* r300
)
250 struct r300_vertex_format vformat
;
253 memset(&vformat
, 0, sizeof(struct r300_vertex_format
));
254 for (i
= 0; i
< 16; i
++) {
255 vformat
.vs_tab
[i
] = -1;
256 vformat
.fs_tab
[i
] = -1;
259 r300_vs_tab_routes(r300
, &vformat
);
261 r300_vertex_psc(r300
, &vformat
);
263 if (memcmp(&r300
->vertex_info
, &vformat
,
264 sizeof(struct r300_vertex_format
))) {
265 memcpy(&r300
->vertex_info
, &vformat
,
266 sizeof(struct r300_vertex_format
));
267 r300
->dirty_state
|= R300_NEW_VERTEX_FORMAT
;
271 /* Set up the mappings from GB to US, for RS block. */
272 static void r300_update_fs_tab(struct r300_context
* r300
)
274 struct r300_vertex_format
* vformat
= &r300
->vertex_info
;
275 struct tgsi_shader_info
* info
= &r300
->fs
->info
;
276 int i
, cols
= 0, texs
= 0, cols_emitted
= 0;
277 int* tab
= vformat
->fs_tab
;
279 for (i
= 0; i
< 16; i
++) {
283 assert(info
->num_inputs
<= 16);
284 for (i
= 0; i
< info
->num_inputs
; i
++) {
285 switch (info
->input_semantic_name
[i
]) {
286 case TGSI_SEMANTIC_COLOR
:
287 tab
[i
] = INTERP_LINEAR
;
290 case TGSI_SEMANTIC_POSITION
:
291 case TGSI_SEMANTIC_PSIZE
:
292 debug_printf("r300: Implementation error: Can't use "
293 "pos attribs in fragshader yet!\n");
294 /* Pass through for now */
295 case TGSI_SEMANTIC_FOG
:
296 case TGSI_SEMANTIC_GENERIC
:
297 tab
[i
] = INTERP_PERSPECTIVE
;
300 debug_printf("r300: Unknown vertex input %d\n",
301 info
->input_semantic_name
[i
]);
306 /* Now that we know where everything is... */
307 DBG(r300
, DBG_DRAW
, "r300: fp input count: %d\n", info
->num_inputs
);
308 for (i
= 0; i
< info
->num_inputs
; i
++) {
311 DBG(r300
, DBG_DRAW
, "r300: attrib: "
312 "stack offset %d, color, tab %d\n",
314 tab
[i
] = cols_emitted
;
317 case INTERP_PERSPECTIVE
:
318 DBG(r300
, DBG_DRAW
, "r300: attrib: "
319 "stack offset %d, texcoord, tab %d\n",
321 tab
[i
] = cols
+ texs
;
325 debug_printf("r300: Implementation error: Bad fp interp!\n");
333 /* Set up the RS block. This is the part of the chipset that actually does
334 * the rasterization of vertices into fragments. This is also the part of the
335 * chipset that locks up if any part of it is even slightly wrong. */
336 static void r300_update_rs_block(struct r300_context
* r300
)
338 struct r300_rs_block
* rs
= r300
->rs_block
;
339 struct tgsi_shader_info
* info
= &r300
->fs
->info
;
340 int* tab
= r300
->vertex_info
.fs_tab
;
341 int col_count
= 0, fp_offset
= 0, i
, tex_count
= 0;
343 memset(rs
, 0, sizeof(struct r300_rs_block
));
345 if (r300_screen(r300
->context
.screen
)->caps
->is_r500
) {
346 for (i
= 0; i
< info
->num_inputs
; i
++) {
347 assert(tab
[i
] != -1);
348 switch (info
->input_semantic_name
[i
]) {
349 case TGSI_SEMANTIC_COLOR
:
351 R500_RS_COL_PTR(col_count
) |
352 R500_RS_COL_FMT(R300_RS_COL_FMT_RGBA
);
355 case TGSI_SEMANTIC_GENERIC
:
357 R500_RS_SEL_S(rs_tex_comp
) |
358 R500_RS_SEL_T(rs_tex_comp
+ 1) |
359 R500_RS_SEL_R(rs_tex_comp
+ 2) |
360 R500_RS_SEL_Q(rs_tex_comp
+ 3);
369 /* Rasterize at least one color, or bad things happen. */
370 if ((col_count
== 0) && (tex_count
== 0)) {
371 rs
->ip
[0] |= R500_RS_COL_FMT(R300_RS_COL_FMT_0001
);
375 for (i
= 0; i
< tex_count
; i
++) {
376 rs
->inst
[i
] |= R500_RS_INST_TEX_ID(i
) |
377 R500_RS_INST_TEX_CN_WRITE
| R500_RS_INST_TEX_ADDR(fp_offset
);
381 for (i
= 0; i
< col_count
; i
++) {
382 rs
->inst
[i
] |= R500_RS_INST_COL_ID(i
) |
383 R500_RS_INST_COL_CN_WRITE
| R500_RS_INST_COL_ADDR(fp_offset
);
387 for (i
= 0; i
< info
->num_inputs
; i
++) {
388 assert(tab
[i
] != -1);
389 switch (info
->input_semantic_name
[i
]) {
390 case TGSI_SEMANTIC_COLOR
:
392 R300_RS_COL_PTR(col_count
) |
393 R300_RS_COL_FMT(R300_RS_COL_FMT_RGBA
);
396 case TGSI_SEMANTIC_GENERIC
:
398 R300_RS_TEX_PTR(rs_tex_comp
) |
399 R300_RS_SEL_S(R300_RS_SEL_C0
) |
400 R300_RS_SEL_T(R300_RS_SEL_C1
) |
401 R300_RS_SEL_R(R300_RS_SEL_C2
) |
402 R300_RS_SEL_Q(R300_RS_SEL_C3
);
411 if (col_count
== 0) {
412 rs
->ip
[0] |= R300_RS_COL_FMT(R300_RS_COL_FMT_0001
);
415 if (tex_count
== 0) {
417 R300_RS_SEL_S(R300_RS_SEL_K0
) |
418 R300_RS_SEL_T(R300_RS_SEL_K0
) |
419 R300_RS_SEL_R(R300_RS_SEL_K0
) |
420 R300_RS_SEL_Q(R300_RS_SEL_K1
);
423 /* Rasterize at least one color, or bad things happen. */
424 if ((col_count
== 0) && (tex_count
== 0)) {
428 for (i
= 0; i
< tex_count
; i
++) {
429 rs
->inst
[i
] |= R300_RS_INST_TEX_ID(i
) |
430 R300_RS_INST_TEX_CN_WRITE
| R300_RS_INST_TEX_ADDR(fp_offset
);
434 for (i
= 0; i
< col_count
; i
++) {
435 rs
->inst
[i
] |= R300_RS_INST_COL_ID(i
) |
436 R300_RS_INST_COL_CN_WRITE
| R300_RS_INST_COL_ADDR(fp_offset
);
441 rs
->count
= (rs_tex_comp
) | (col_count
<< R300_IC_COUNT_SHIFT
) |
444 rs
->inst_count
= MAX2(MAX2(col_count
- 1, tex_count
- 1), 0);
447 void r300_update_derived_state(struct r300_context
* r300
)
449 if (r300
->dirty_state
&
450 (R300_NEW_FRAGMENT_SHADER
| R300_NEW_VERTEX_SHADER
)) {
451 r300_update_vertex_format(r300
);
454 if (r300
->dirty_state
& R300_NEW_VERTEX_FORMAT
) {
455 r300_update_fs_tab(r300
);
456 r300_update_rs_block(r300
);