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"
28 /* r300_state_derived: Various bits of state which are dependent upon
29 * currently bound CSO data. */
31 /* Set up the vs_tab and routes. */
32 static void r300_vs_tab_routes(struct r300_context
* r300
,
33 struct r300_vertex_format
* vformat
)
35 struct r300_screen
* r300screen
= r300_screen(r300
->context
.screen
);
36 struct vertex_info
* vinfo
= &vformat
->vinfo
;
37 int* tab
= vformat
->vs_tab
;
38 boolean pos
= FALSE
, psize
= FALSE
, fog
= FALSE
;
39 int i
, texs
= 0, cols
= 0;
40 struct tgsi_shader_info
* info
;
42 if (r300screen
->caps
->has_tcl
) {
43 /* Use vertex shader to determine required routes. */
44 info
= &r300
->vs
->info
;
46 /* Use fragment shader to determine required routes. */
47 info
= &r300
->fs
->info
;
50 assert(info
->num_inputs
<= 16);
52 if (!r300screen
->caps
->has_tcl
|| !r300
->rs_state
->enable_vte
)
54 for (i
= 0; i
< info
->num_inputs
; i
++) {
55 switch (info
->input_semantic_name
[i
]) {
56 case TGSI_SEMANTIC_POSITION
:
60 case TGSI_SEMANTIC_COLOR
:
64 case TGSI_SEMANTIC_PSIZE
:
68 case TGSI_SEMANTIC_FOG
:
71 case TGSI_SEMANTIC_GENERIC
:
76 debug_printf("r300: Unknown vertex input %d\n",
77 info
->input_semantic_name
[i
]);
84 /* Just copy vert attribs over as-is. */
85 for (i
= 0; i
< info
->num_inputs
; i
++) {
89 for (i
= 0; i
< info
->num_outputs
; i
++) {
90 switch (info
->output_semantic_name
[i
]) {
91 case TGSI_SEMANTIC_POSITION
:
94 case TGSI_SEMANTIC_COLOR
:
97 case TGSI_SEMANTIC_PSIZE
:
100 case TGSI_SEMANTIC_FOG
:
103 case TGSI_SEMANTIC_GENERIC
:
107 debug_printf("r300: Unknown vertex output %d\n",
108 info
->output_semantic_name
[i
]);
117 /* Do the actual vertex_info setup.
119 * vertex_info has four uints of hardware-specific data in it.
120 * vinfo.hwfmt[0] is R300_VAP_VTX_STATE_CNTL
121 * vinfo.hwfmt[1] is R300_VAP_VSM_VTX_ASSM
122 * vinfo.hwfmt[2] is R300_VAP_OUTPUT_VTX_FMT_0
123 * vinfo.hwfmt[3] is R300_VAP_OUTPUT_VTX_FMT_1 */
125 vinfo
->hwfmt
[0] = 0x5555; /* XXX this is classic Mesa bonghits */
128 debug_printf("r300: Forcing vertex position attribute emit...\n");
129 /* Make room for the position attribute
130 * at the beginning of the tab. */
131 for (i
= 15; i
> 0; i
--) {
136 draw_emit_vertex_attr(vinfo
, EMIT_4F
, INTERP_PERSPECTIVE
,
137 draw_find_vs_output(r300
->draw
, TGSI_SEMANTIC_POSITION
, 0));
138 vinfo
->hwfmt
[1] |= R300_INPUT_CNTL_POS
;
139 vinfo
->hwfmt
[2] |= R300_VAP_OUTPUT_VTX_FMT_0__POS_PRESENT
;
142 draw_emit_vertex_attr(vinfo
, EMIT_1F_PSIZE
, INTERP_POS
,
143 draw_find_vs_output(r300
->draw
, TGSI_SEMANTIC_PSIZE
, 0));
144 vinfo
->hwfmt
[2] |= R300_VAP_OUTPUT_VTX_FMT_0__PT_SIZE_PRESENT
;
147 for (i
= 0; i
< cols
; i
++) {
148 draw_emit_vertex_attr(vinfo
, EMIT_4F
, INTERP_LINEAR
,
149 draw_find_vs_output(r300
->draw
, TGSI_SEMANTIC_COLOR
, i
));
150 vinfo
->hwfmt
[1] |= R300_INPUT_CNTL_COLOR
;
151 vinfo
->hwfmt
[2] |= (R300_VAP_OUTPUT_VTX_FMT_0__COLOR_0_PRESENT
<< i
);
154 /* Init i right here, increment it if fog is enabled.
155 * This gets around a double-increment problem. */
160 draw_emit_vertex_attr(vinfo
, EMIT_4F
, INTERP_PERSPECTIVE
,
161 draw_find_vs_output(r300
->draw
, TGSI_SEMANTIC_FOG
, 0));
162 vinfo
->hwfmt
[1] |= (R300_INPUT_CNTL_TC0
<< i
);
163 vinfo
->hwfmt
[3] |= (4 << (3 * i
));
166 for (i
; i
< texs
; i
++) {
167 draw_emit_vertex_attr(vinfo
, EMIT_4F
, INTERP_PERSPECTIVE
,
168 draw_find_vs_output(r300
->draw
, TGSI_SEMANTIC_GENERIC
, i
));
169 vinfo
->hwfmt
[1] |= (R300_INPUT_CNTL_TC0
<< i
);
170 vinfo
->hwfmt
[3] |= (4 << (3 * i
));
173 /* Handle the case where the vertex shader will be generating some of
174 * the attribs based on its inputs. */
175 if (r300screen
->caps
->has_tcl
&&
176 info
->num_inputs
< info
->num_outputs
) {
177 vinfo
->num_attribs
= info
->num_inputs
;
180 draw_compute_vertex_size(vinfo
);
183 /* Update the PSC tables. */
184 static void r300_vertex_psc(struct r300_context
* r300
,
185 struct r300_vertex_format
* vformat
)
187 struct r300_screen
* r300screen
= r300_screen(r300
->context
.screen
);
188 struct vertex_info
* vinfo
= &vformat
->vinfo
;
189 int* tab
= vformat
->vs_tab
;
193 /* Vertex shaders have no semantics on their inputs,
194 * so PSC should just route stuff based on their info,
195 * and not on attrib information. */
196 if (r300screen
->caps
->has_tcl
) {
197 attrib_count
= r300
->vs
->info
.num_inputs
;
198 debug_printf("r300: routing %d attribs in psc for vs\n",
201 attrib_count
= vinfo
->num_attribs
;
202 debug_printf("r300: attrib count: %d\n", attrib_count
);
203 for (i
= 0; i
< attrib_count
; i
++) {
204 debug_printf("r300: attrib: offset %d, interp %d, size %d,"
205 " tab %d\n", vinfo
->attrib
[i
].src_index
,
206 vinfo
->attrib
[i
].interp_mode
, vinfo
->attrib
[i
].emit
,
211 for (i
= 0; i
< attrib_count
; i
++) {
212 /* Make sure we have a proper destination for our attribute */
213 assert(tab
[i
] != -1);
215 /* Add the attribute to the PSC table. */
216 temp
= r300screen
->caps
->has_tcl
?
217 R300_DATA_TYPE_FLOAT_4
:
218 translate_vertex_data_type(vinfo
->attrib
[i
].emit
);
219 temp
|= tab
[i
] << R300_DST_VEC_LOC_SHIFT
;
222 vformat
->vap_prog_stream_cntl
[i
>> 1] &= 0x0000ffff;
223 vformat
->vap_prog_stream_cntl
[i
>> 1] |= temp
<< 16;
225 vformat
->vap_prog_stream_cntl_ext
[i
>> 1] |=
226 (R300_VAP_SWIZZLE_XYZW
<< 16);
228 vformat
->vap_prog_stream_cntl
[i
>> 1] &= 0xffff0000;
229 vformat
->vap_prog_stream_cntl
[i
>> 1] |= temp
<< 0;
231 vformat
->vap_prog_stream_cntl_ext
[i
>> 1] |=
232 (R300_VAP_SWIZZLE_XYZW
<< 0);
236 /* Set the last vector in the PSC. */
238 vformat
->vap_prog_stream_cntl
[i
>> 1] |=
239 (R300_LAST_VEC
<< (i
& 1 ? 16 : 0));
242 /* Update the vertex format. */
243 static void r300_update_vertex_format(struct r300_context
* r300
)
245 struct r300_vertex_format vformat
;
248 memset(&vformat
, 0, sizeof(struct r300_vertex_format
));
249 for (i
= 0; i
< 16; i
++) {
250 vformat
.vs_tab
[i
] = -1;
251 vformat
.fs_tab
[i
] = -1;
254 r300_vs_tab_routes(r300
, &vformat
);
256 r300_vertex_psc(r300
, &vformat
);
258 if (memcmp(&r300
->vertex_info
, &vformat
,
259 sizeof(struct r300_vertex_format
))) {
260 memcpy(&r300
->vertex_info
, &vformat
,
261 sizeof(struct r300_vertex_format
));
262 r300
->dirty_state
|= R300_NEW_VERTEX_FORMAT
;
266 /* Set up the mappings from GB to US, for RS block. */
267 static void r300_update_fs_tab(struct r300_context
* r300
)
269 struct r300_vertex_format
* vformat
= &r300
->vertex_info
;
270 struct tgsi_shader_info
* info
= &r300
->fs
->info
;
271 int i
, cols
= 0, texs
= 0, cols_emitted
= 0;
272 int* tab
= vformat
->fs_tab
;
274 for (i
= 0; i
< 16; i
++) {
278 assert(info
->num_inputs
<= 16);
279 for (i
= 0; i
< info
->num_inputs
; i
++) {
280 switch (info
->input_semantic_name
[i
]) {
281 case TGSI_SEMANTIC_COLOR
:
282 tab
[i
] = INTERP_LINEAR
;
285 case TGSI_SEMANTIC_POSITION
:
286 case TGSI_SEMANTIC_PSIZE
:
287 debug_printf("r300: Implementation error: Can't use "
288 "pos attribs in fragshader yet!\n");
289 /* Pass through for now */
290 case TGSI_SEMANTIC_FOG
:
291 case TGSI_SEMANTIC_GENERIC
:
292 tab
[i
] = INTERP_PERSPECTIVE
;
295 debug_printf("r300: Unknown vertex input %d\n",
296 info
->input_semantic_name
[i
]);
301 /* Now that we know where everything is... */
302 debug_printf("r300: fp input count: %d\n", info
->num_inputs
);
303 for (i
= 0; i
< info
->num_inputs
; i
++) {
306 debug_printf("r300: attrib: "
307 "stack offset %d, color, tab %d\n",
309 tab
[i
] = cols_emitted
;
312 case INTERP_PERSPECTIVE
:
313 debug_printf("r300: attrib: "
314 "stack offset %d, texcoord, tab %d\n",
316 tab
[i
] = cols
+ texs
;
320 debug_printf("r300: Implementation error: Bad fp interp!\n");
328 /* Set up the RS block. This is the part of the chipset that actually does
329 * the rasterization of vertices into fragments. This is also the part of the
330 * chipset that locks up if any part of it is even slightly wrong. */
331 static void r300_update_rs_block(struct r300_context
* r300
)
333 struct r300_rs_block
* rs
= r300
->rs_block
;
334 struct tgsi_shader_info
* info
= &r300
->fs
->info
;
335 int* tab
= r300
->vertex_info
.fs_tab
;
336 int col_count
= 0, fp_offset
= 0, i
, memory_pos
, tex_count
= 0;
338 memset(rs
, 0, sizeof(struct r300_rs_block
));
340 if (r300_screen(r300
->context
.screen
)->caps
->is_r500
) {
341 for (i
= 0; i
< info
->num_inputs
; i
++) {
342 assert(tab
[i
] != -1);
343 memory_pos
= tab
[i
] * 4;
344 switch (info
->input_semantic_name
[i
]) {
345 case TGSI_SEMANTIC_COLOR
:
347 R500_RS_COL_PTR(memory_pos
) |
348 R500_RS_COL_FMT(R300_RS_COL_FMT_RGBA
);
351 case TGSI_SEMANTIC_GENERIC
:
353 R500_RS_SEL_S(memory_pos
) |
354 R500_RS_SEL_T(memory_pos
+ 1) |
355 R500_RS_SEL_R(memory_pos
+ 2) |
356 R500_RS_SEL_Q(memory_pos
+ 3);
364 if (col_count
== 0) {
365 rs
->ip
[0] |= R500_RS_COL_FMT(R300_RS_COL_FMT_0001
);
368 if (tex_count
== 0) {
370 R500_RS_SEL_S(R500_RS_IP_PTR_K0
) |
371 R500_RS_SEL_T(R500_RS_IP_PTR_K0
) |
372 R500_RS_SEL_R(R500_RS_IP_PTR_K0
) |
373 R500_RS_SEL_Q(R500_RS_IP_PTR_K1
);
376 /* Rasterize at least one color, or bad things happen. */
377 if ((col_count
== 0) && (tex_count
== 0)) {
381 for (i
= 0; i
< tex_count
; i
++) {
382 rs
->inst
[i
] |= R500_RS_INST_TEX_ID(i
) |
383 R500_RS_INST_TEX_CN_WRITE
| R500_RS_INST_TEX_ADDR(fp_offset
);
387 for (i
= 0; i
< col_count
; i
++) {
388 rs
->inst
[i
] |= R500_RS_INST_COL_ID(i
) |
389 R500_RS_INST_COL_CN_WRITE
| R500_RS_INST_COL_ADDR(fp_offset
);
393 for (i
= 0; i
< info
->num_inputs
; i
++) {
394 assert(tab
[i
] != -1);
395 memory_pos
= tab
[i
] * 4;
396 switch (info
->input_semantic_name
[i
]) {
397 case TGSI_SEMANTIC_COLOR
:
399 R300_RS_COL_PTR(memory_pos
) |
400 R300_RS_COL_FMT(R300_RS_COL_FMT_RGBA
);
403 case TGSI_SEMANTIC_GENERIC
:
405 R300_RS_TEX_PTR(memory_pos
) |
406 R300_RS_SEL_S(R300_RS_SEL_C0
) |
407 R300_RS_SEL_T(R300_RS_SEL_C1
) |
408 R300_RS_SEL_R(R300_RS_SEL_C2
) |
409 R300_RS_SEL_Q(R300_RS_SEL_C3
);
417 if (col_count
== 0) {
418 rs
->ip
[0] |= R300_RS_COL_FMT(R300_RS_COL_FMT_0001
);
421 if (tex_count
== 0) {
423 R300_RS_SEL_S(R300_RS_SEL_K0
) |
424 R300_RS_SEL_T(R300_RS_SEL_K0
) |
425 R300_RS_SEL_R(R300_RS_SEL_K0
) |
426 R300_RS_SEL_Q(R300_RS_SEL_K1
);
429 /* Rasterize at least one color, or bad things happen. */
430 if ((col_count
== 0) && (tex_count
== 0)) {
434 for (i
= 0; i
< tex_count
; i
++) {
435 rs
->inst
[i
] |= R300_RS_INST_TEX_ID(i
) |
436 R300_RS_INST_TEX_CN_WRITE
| R300_RS_INST_TEX_ADDR(fp_offset
);
440 for (i
= 0; i
< col_count
; i
++) {
441 rs
->inst
[i
] |= R300_RS_INST_COL_ID(i
) |
442 R300_RS_INST_COL_CN_WRITE
| R300_RS_INST_COL_ADDR(fp_offset
);
447 rs
->count
= (tex_count
* 4) | (col_count
<< R300_IC_COUNT_SHIFT
) |
450 rs
->inst_count
= MAX2(MAX2(col_count
- 1, tex_count
- 1), 0);
453 void r300_update_derived_state(struct r300_context
* r300
)
455 if (r300
->dirty_state
&
456 (R300_NEW_FRAGMENT_SHADER
| R300_NEW_VERTEX_SHADER
)) {
457 r300_update_vertex_format(r300
);
460 if (r300
->dirty_state
& R300_NEW_VERTEX_FORMAT
) {
461 r300_update_fs_tab(r300
);
462 r300_update_rs_block(r300
);