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 /* Set up the vs_tab and routes. */
29 static void r300_vs_tab_routes(struct r300_context
* r300
,
30 struct r300_vertex_format
* vformat
)
32 struct r300_screen
* r300screen
= r300_screen(r300
->context
.screen
);
33 struct vertex_info
* vinfo
= &vformat
->vinfo
;
34 int* tab
= vformat
->vs_tab
;
35 boolean pos
= FALSE
, psize
= FALSE
, fog
= FALSE
;
36 int i
, texs
= 0, cols
= 0;
37 struct tgsi_shader_info
* info
;
39 if (r300screen
->caps
->has_tcl
) {
40 /* Use vertex shader to determine required routes. */
41 info
= &r300
->vs
->info
;
43 /* Use fragment shader to determine required routes. */
44 info
= &r300
->fs
->info
;
47 assert(info
->num_inputs
<= 16);
49 if (r300screen
->caps
->has_tcl
) {
50 /* Just copy vert attribs over as-is. */
51 for (i
= 0; i
< info
->num_inputs
; i
++) {
54 for (i
= 0; i
< info
->num_outputs
; i
++) {
55 switch (info
->output_semantic_name
[i
]) {
56 case TGSI_SEMANTIC_POSITION
:
59 case TGSI_SEMANTIC_COLOR
:
62 case TGSI_SEMANTIC_PSIZE
:
65 case TGSI_SEMANTIC_FOG
:
68 case TGSI_SEMANTIC_GENERIC
:
72 debug_printf("r300: Unknown vertex output %d\n",
73 info
->output_semantic_name
[i
]);
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
:
92 case TGSI_SEMANTIC_FOG
:
95 case TGSI_SEMANTIC_GENERIC
:
100 debug_printf("r300: Unknown vertex input %d\n",
101 info
->input_semantic_name
[i
]);
110 /* Do the actual vertex_info setup.
112 * vertex_info has four uints of hardware-specific data in it.
113 * vinfo.hwfmt[0] is R300_VAP_VTX_STATE_CNTL
114 * vinfo.hwfmt[1] is R300_VAP_VSM_VTX_ASSM
115 * vinfo.hwfmt[2] is R300_VAP_OUTPUT_VTX_FMT_0
116 * vinfo.hwfmt[3] is R300_VAP_OUTPUT_VTX_FMT_1 */
118 vinfo
->hwfmt
[0] = 0x5555; /* XXX this is classic Mesa bonghits */
121 debug_printf("r300: Forcing vertex position attribute emit...\n");
122 /* Make room for the position attribute
123 * at the beginning of the tab. */
124 for (i
= 15; i
> 0; i
--) {
129 draw_emit_vertex_attr(vinfo
, EMIT_4F
, INTERP_PERSPECTIVE
,
130 draw_find_vs_output(r300
->draw
, TGSI_SEMANTIC_POSITION
, 0));
131 vinfo
->hwfmt
[1] |= R300_INPUT_CNTL_POS
;
132 vinfo
->hwfmt
[2] |= R300_VAP_OUTPUT_VTX_FMT_0__POS_PRESENT
;
135 draw_emit_vertex_attr(vinfo
, EMIT_1F_PSIZE
, INTERP_POS
,
136 draw_find_vs_output(r300
->draw
, TGSI_SEMANTIC_PSIZE
, 0));
137 vinfo
->hwfmt
[2] |= R300_VAP_OUTPUT_VTX_FMT_0__PT_SIZE_PRESENT
;
140 for (i
= 0; i
< cols
; i
++) {
141 draw_emit_vertex_attr(vinfo
, EMIT_4F
, INTERP_LINEAR
,
142 draw_find_vs_output(r300
->draw
, TGSI_SEMANTIC_COLOR
, i
));
143 vinfo
->hwfmt
[1] |= R300_INPUT_CNTL_COLOR
;
144 vinfo
->hwfmt
[2] |= (R300_VAP_OUTPUT_VTX_FMT_0__COLOR_0_PRESENT
<< i
);
147 /* Init i right here, increment it if fog is enabled.
148 * This gets around a double-increment problem. */
153 draw_emit_vertex_attr(vinfo
, EMIT_4F
, INTERP_PERSPECTIVE
,
154 draw_find_vs_output(r300
->draw
, TGSI_SEMANTIC_FOG
, 0));
155 vinfo
->hwfmt
[1] |= (R300_INPUT_CNTL_TC0
<< i
);
156 vinfo
->hwfmt
[3] |= (4 << (3 * i
));
159 for (i
; i
< texs
; i
++) {
160 draw_emit_vertex_attr(vinfo
, EMIT_4F
, INTERP_PERSPECTIVE
,
161 draw_find_vs_output(r300
->draw
, TGSI_SEMANTIC_GENERIC
, i
));
162 vinfo
->hwfmt
[1] |= (R300_INPUT_CNTL_TC0
<< i
);
163 vinfo
->hwfmt
[3] |= (4 << (3 * i
));
166 /* Handle the case where the vertex shader will be generating some of
167 * the attribs based on its inputs. */
168 if (r300screen
->caps
->has_tcl
&&
169 info
->num_inputs
< info
->num_outputs
) {
170 vinfo
->num_attribs
= info
->num_inputs
;
173 draw_compute_vertex_size(vinfo
);
176 /* Update the PSC tables. */
177 static void r300_vertex_psc(struct r300_context
* r300
,
178 struct r300_vertex_format
* vformat
)
180 struct r300_screen
* r300screen
= r300_screen(r300
->context
.screen
);
181 struct vertex_info
* vinfo
= &vformat
->vinfo
;
182 int* tab
= vformat
->vs_tab
;
186 /* Vertex shaders have no semantics on their inputs,
187 * so PSC should just route stuff based on their info,
188 * and not on attrib information. */
189 if (r300screen
->caps
->has_tcl
) {
190 attrib_count
= r300
->vs
->info
.num_inputs
;
191 debug_printf("r300: routing %d attribs in psc for vs\n",
194 attrib_count
= vinfo
->num_attribs
;
195 debug_printf("r300: attrib count: %d\n", attrib_count
);
196 for (i
= 0; i
< attrib_count
; i
++) {
197 debug_printf("r300: attrib: offset %d, interp %d, size %d,"
198 " tab %d\n", vinfo
->attrib
[i
].src_index
,
199 vinfo
->attrib
[i
].interp_mode
, vinfo
->attrib
[i
].emit
,
204 for (i
= 0; i
< attrib_count
; i
++) {
205 /* Make sure we have a proper destination for our attribute */
206 assert(tab
[i
] != -1);
208 /* Add the attribute to the PSC table. */
209 temp
= r300screen
->caps
->has_tcl
?
210 R300_DATA_TYPE_FLOAT_4
:
211 translate_vertex_data_type(vinfo
->attrib
[i
].emit
);
212 temp
|= tab
[i
] << R300_DST_VEC_LOC_SHIFT
;
215 vformat
->vap_prog_stream_cntl
[i
>> 1] &= 0x0000ffff;
216 vformat
->vap_prog_stream_cntl
[i
>> 1] |= temp
<< 16;
218 vformat
->vap_prog_stream_cntl_ext
[i
>> 1] |=
219 (R300_VAP_SWIZZLE_XYZW
<< 16);
221 vformat
->vap_prog_stream_cntl
[i
>> 1] &= 0xffff0000;
222 vformat
->vap_prog_stream_cntl
[i
>> 1] |= temp
<< 0;
224 vformat
->vap_prog_stream_cntl_ext
[i
>> 1] |=
225 (R300_VAP_SWIZZLE_XYZW
<< 0);
229 /* Set the last vector in the PSC. */
231 vformat
->vap_prog_stream_cntl
[i
>> 1] |=
232 (R300_LAST_VEC
<< (i
& 1 ? 16 : 0));
235 /* Update the vertex format. */
236 static void r300_update_vertex_format(struct r300_context
* r300
)
238 struct r300_vertex_format vformat
;
241 memset(&vformat
, 0, sizeof(struct r300_vertex_format
));
242 for (i
= 0; i
< 16; i
++) {
243 vformat
.vs_tab
[i
] = -1;
244 vformat
.fs_tab
[i
] = -1;
247 r300_vs_tab_routes(r300
, &vformat
);
249 r300_vertex_psc(r300
, &vformat
);
251 if (memcmp(&r300
->vertex_info
, &vformat
,
252 sizeof(struct r300_vertex_format
))) {
253 memcpy(&r300
->vertex_info
, &vformat
,
254 sizeof(struct r300_vertex_format
));
255 r300
->dirty_state
|= R300_NEW_VERTEX_FORMAT
;
259 /* Set up the mappings from GB to US, for RS block. */
260 static void r300_update_fs_tab(struct r300_context
* r300
)
262 struct r300_vertex_format
* vformat
= &r300
->vertex_info
;
263 struct tgsi_shader_info
* info
= &r300
->fs
->info
;
264 int i
, cols
= 0, texs
= 0, cols_emitted
= 0;
265 int* tab
= vformat
->fs_tab
;
267 for (i
= 0; i
< 16; i
++) {
271 assert(info
->num_inputs
<= 16);
272 for (i
= 0; i
< info
->num_inputs
; i
++) {
273 switch (info
->input_semantic_name
[i
]) {
274 case TGSI_SEMANTIC_COLOR
:
275 tab
[i
] = INTERP_LINEAR
;
278 case TGSI_SEMANTIC_POSITION
:
279 case TGSI_SEMANTIC_PSIZE
:
280 debug_printf("r300: Implementation error: Can't use "
281 "pos attribs in fragshader yet!\n");
282 /* Pass through for now */
283 case TGSI_SEMANTIC_FOG
:
284 case TGSI_SEMANTIC_GENERIC
:
285 tab
[i
] = INTERP_PERSPECTIVE
;
288 debug_printf("r300: Unknown vertex input %d\n",
289 info
->input_semantic_name
[i
]);
294 /* Now that we know where everything is... */
295 debug_printf("r300: fp input count: %d\n", info
->num_inputs
);
296 for (i
= 0; i
< info
->num_inputs
; i
++) {
299 debug_printf("r300: attrib: "
300 "stack offset %d, color, tab %d\n",
302 tab
[i
] = cols_emitted
;
305 case INTERP_PERSPECTIVE
:
306 debug_printf("r300: attrib: "
307 "stack offset %d, texcoord, tab %d\n",
309 tab
[i
] = cols
+ texs
;
313 debug_printf("r300: Implementation error: Bad fp interp!\n");
321 /* Set up the RS block. This is the part of the chipset that actually does
322 * the rasterization of vertices into fragments. This is also the part of the
323 * chipset that locks up if any part of it is even slightly wrong. */
324 static void r300_update_rs_block(struct r300_context
* r300
)
326 struct r300_rs_block
* rs
= r300
->rs_block
;
327 struct tgsi_shader_info
* info
= &r300
->fs
->info
;
328 int* tab
= r300
->vertex_info
.fs_tab
;
329 int col_count
= 0, fp_offset
= 0, i
, memory_pos
, tex_count
= 0;
331 memset(rs
, 0, sizeof(struct r300_rs_block
));
333 if (r300_screen(r300
->context
.screen
)->caps
->is_r500
) {
334 for (i
= 0; i
< info
->num_inputs
; i
++) {
335 assert(tab
[i
] != -1);
336 memory_pos
= tab
[i
] * 4;
337 switch (info
->input_semantic_name
[i
]) {
338 case TGSI_SEMANTIC_COLOR
:
340 R500_RS_COL_PTR(memory_pos
) |
341 R500_RS_COL_FMT(R300_RS_COL_FMT_RGBA
);
344 case TGSI_SEMANTIC_GENERIC
:
346 R500_RS_SEL_S(memory_pos
) |
347 R500_RS_SEL_T(memory_pos
+ 1) |
348 R500_RS_SEL_R(memory_pos
+ 2) |
349 R500_RS_SEL_Q(memory_pos
+ 3);
357 if (col_count
== 0) {
358 rs
->ip
[0] |= R500_RS_COL_FMT(R300_RS_COL_FMT_0001
);
361 if (tex_count
== 0) {
363 R500_RS_SEL_S(R500_RS_IP_PTR_K0
) |
364 R500_RS_SEL_T(R500_RS_IP_PTR_K0
) |
365 R500_RS_SEL_R(R500_RS_IP_PTR_K0
) |
366 R500_RS_SEL_Q(R500_RS_IP_PTR_K1
);
369 /* Rasterize at least one color, or bad things happen. */
370 if ((col_count
== 0) && (tex_count
== 0)) {
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 assert(tab
[i
] != -1);
388 memory_pos
= tab
[i
] * 4;
389 switch (info
->input_semantic_name
[i
]) {
390 case TGSI_SEMANTIC_COLOR
:
392 R300_RS_COL_PTR(memory_pos
) |
393 R300_RS_COL_FMT(R300_RS_COL_FMT_RGBA
);
396 case TGSI_SEMANTIC_GENERIC
:
398 R300_RS_TEX_PTR(memory_pos
) |
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
);
410 if (col_count
== 0) {
411 rs
->ip
[0] |= R300_RS_COL_FMT(R300_RS_COL_FMT_0001
);
414 if (tex_count
== 0) {
416 R300_RS_SEL_S(R300_RS_SEL_K0
) |
417 R300_RS_SEL_T(R300_RS_SEL_K0
) |
418 R300_RS_SEL_R(R300_RS_SEL_K0
) |
419 R300_RS_SEL_Q(R300_RS_SEL_K1
);
422 /* Rasterize at least one color, or bad things happen. */
423 if ((col_count
== 0) && (tex_count
== 0)) {
427 for (i
= 0; i
< tex_count
; i
++) {
428 rs
->inst
[i
] |= R300_RS_INST_TEX_ID(i
) |
429 R300_RS_INST_TEX_CN_WRITE
| R300_RS_INST_TEX_ADDR(fp_offset
);
433 for (i
= 0; i
< col_count
; i
++) {
434 rs
->inst
[i
] |= R300_RS_INST_COL_ID(i
) |
435 R300_RS_INST_COL_CN_WRITE
| R300_RS_INST_COL_ADDR(fp_offset
);
440 rs
->count
= (tex_count
* 4) | (col_count
<< R300_IC_COUNT_SHIFT
) |
443 rs
->inst_count
= MAX2(MAX2(col_count
- 1, tex_count
- 1), 0);
446 void r300_update_derived_state(struct r300_context
* r300
)
448 if (r300
->dirty_state
&
449 (R300_NEW_FRAGMENT_SHADER
| R300_NEW_VERTEX_SHADER
)) {
450 r300_update_vertex_format(r300
);
453 if (r300
->dirty_state
& R300_NEW_VERTEX_FORMAT
) {
454 r300_update_fs_tab(r300
);
455 r300_update_rs_block(r300
);