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
) {
53 /* Just copy vert attribs over as-is. */
54 for (i
= 0; i
< info
->num_inputs
; i
++) {
57 for (i
= 0; i
< info
->num_outputs
; i
++) {
58 switch (info
->output_semantic_name
[i
]) {
59 case TGSI_SEMANTIC_POSITION
:
62 case TGSI_SEMANTIC_COLOR
:
65 case TGSI_SEMANTIC_PSIZE
:
68 case TGSI_SEMANTIC_FOG
:
71 case TGSI_SEMANTIC_GENERIC
:
75 debug_printf("r300: Unknown vertex output %d\n",
76 info
->output_semantic_name
[i
]);
81 for (i
= 0; i
< info
->num_inputs
; i
++) {
82 switch (info
->input_semantic_name
[i
]) {
83 case TGSI_SEMANTIC_POSITION
:
87 case TGSI_SEMANTIC_COLOR
:
91 case TGSI_SEMANTIC_PSIZE
:
95 case TGSI_SEMANTIC_FOG
:
98 case TGSI_SEMANTIC_GENERIC
:
103 debug_printf("r300: Unknown vertex input %d\n",
104 info
->input_semantic_name
[i
]);
113 /* Do the actual vertex_info setup.
115 * vertex_info has four uints of hardware-specific data in it.
116 * vinfo.hwfmt[0] is R300_VAP_VTX_STATE_CNTL
117 * vinfo.hwfmt[1] is R300_VAP_VSM_VTX_ASSM
118 * vinfo.hwfmt[2] is R300_VAP_OUTPUT_VTX_FMT_0
119 * vinfo.hwfmt[3] is R300_VAP_OUTPUT_VTX_FMT_1 */
121 vinfo
->hwfmt
[0] = 0x5555; /* XXX this is classic Mesa bonghits */
124 debug_printf("r300: Forcing vertex position attribute emit...\n");
125 /* Make room for the position attribute
126 * at the beginning of the tab. */
127 for (i
= 15; i
> 0; i
--) {
132 draw_emit_vertex_attr(vinfo
, EMIT_4F
, INTERP_PERSPECTIVE
,
133 draw_find_vs_output(r300
->draw
, TGSI_SEMANTIC_POSITION
, 0));
134 vinfo
->hwfmt
[1] |= R300_INPUT_CNTL_POS
;
135 vinfo
->hwfmt
[2] |= R300_VAP_OUTPUT_VTX_FMT_0__POS_PRESENT
;
138 draw_emit_vertex_attr(vinfo
, EMIT_1F_PSIZE
, INTERP_POS
,
139 draw_find_vs_output(r300
->draw
, TGSI_SEMANTIC_PSIZE
, 0));
140 vinfo
->hwfmt
[2] |= R300_VAP_OUTPUT_VTX_FMT_0__PT_SIZE_PRESENT
;
143 for (i
= 0; i
< cols
; i
++) {
144 draw_emit_vertex_attr(vinfo
, EMIT_4F
, INTERP_LINEAR
,
145 draw_find_vs_output(r300
->draw
, TGSI_SEMANTIC_COLOR
, i
));
146 vinfo
->hwfmt
[1] |= R300_INPUT_CNTL_COLOR
;
147 vinfo
->hwfmt
[2] |= (R300_VAP_OUTPUT_VTX_FMT_0__COLOR_0_PRESENT
<< i
);
150 /* Init i right here, increment it if fog is enabled.
151 * This gets around a double-increment problem. */
156 draw_emit_vertex_attr(vinfo
, EMIT_4F
, INTERP_PERSPECTIVE
,
157 draw_find_vs_output(r300
->draw
, TGSI_SEMANTIC_FOG
, 0));
158 vinfo
->hwfmt
[1] |= (R300_INPUT_CNTL_TC0
<< i
);
159 vinfo
->hwfmt
[3] |= (4 << (3 * i
));
162 for (i
; i
< texs
; i
++) {
163 draw_emit_vertex_attr(vinfo
, EMIT_4F
, INTERP_PERSPECTIVE
,
164 draw_find_vs_output(r300
->draw
, TGSI_SEMANTIC_GENERIC
, i
));
165 vinfo
->hwfmt
[1] |= (R300_INPUT_CNTL_TC0
<< i
);
166 vinfo
->hwfmt
[3] |= (4 << (3 * i
));
169 /* Handle the case where the vertex shader will be generating some of
170 * the attribs based on its inputs. */
171 if (r300screen
->caps
->has_tcl
&&
172 info
->num_inputs
< info
->num_outputs
) {
173 vinfo
->num_attribs
= info
->num_inputs
;
176 draw_compute_vertex_size(vinfo
);
179 /* Update the PSC tables. */
180 static void r300_vertex_psc(struct r300_context
* r300
,
181 struct r300_vertex_format
* vformat
)
183 struct r300_screen
* r300screen
= r300_screen(r300
->context
.screen
);
184 struct vertex_info
* vinfo
= &vformat
->vinfo
;
185 int* tab
= vformat
->vs_tab
;
189 /* Vertex shaders have no semantics on their inputs,
190 * so PSC should just route stuff based on their info,
191 * and not on attrib information. */
192 if (r300screen
->caps
->has_tcl
) {
193 attrib_count
= r300
->vs
->info
.num_inputs
;
194 debug_printf("r300: routing %d attribs in psc for vs\n",
197 attrib_count
= vinfo
->num_attribs
;
198 debug_printf("r300: attrib count: %d\n", attrib_count
);
199 for (i
= 0; i
< attrib_count
; i
++) {
200 debug_printf("r300: attrib: offset %d, interp %d, size %d,"
201 " tab %d\n", vinfo
->attrib
[i
].src_index
,
202 vinfo
->attrib
[i
].interp_mode
, vinfo
->attrib
[i
].emit
,
207 for (i
= 0; i
< attrib_count
; i
++) {
208 /* Make sure we have a proper destination for our attribute */
209 assert(tab
[i
] != -1);
211 /* Add the attribute to the PSC table. */
212 temp
= r300screen
->caps
->has_tcl
?
213 R300_DATA_TYPE_FLOAT_4
:
214 translate_vertex_data_type(vinfo
->attrib
[i
].emit
);
215 temp
|= tab
[i
] << R300_DST_VEC_LOC_SHIFT
;
218 vformat
->vap_prog_stream_cntl
[i
>> 1] &= 0x0000ffff;
219 vformat
->vap_prog_stream_cntl
[i
>> 1] |= temp
<< 16;
221 vformat
->vap_prog_stream_cntl_ext
[i
>> 1] |=
222 (R300_VAP_SWIZZLE_XYZW
<< 16);
224 vformat
->vap_prog_stream_cntl
[i
>> 1] &= 0xffff0000;
225 vformat
->vap_prog_stream_cntl
[i
>> 1] |= temp
<< 0;
227 vformat
->vap_prog_stream_cntl_ext
[i
>> 1] |=
228 (R300_VAP_SWIZZLE_XYZW
<< 0);
232 /* Set the last vector in the PSC. */
234 vformat
->vap_prog_stream_cntl
[i
>> 1] |=
235 (R300_LAST_VEC
<< (i
& 1 ? 16 : 0));
238 /* Update the vertex format. */
239 static void r300_update_vertex_format(struct r300_context
* r300
)
241 struct r300_vertex_format vformat
;
244 memset(&vformat
, 0, sizeof(struct r300_vertex_format
));
245 for (i
= 0; i
< 16; i
++) {
246 vformat
.vs_tab
[i
] = -1;
247 vformat
.fs_tab
[i
] = -1;
250 r300_vs_tab_routes(r300
, &vformat
);
252 r300_vertex_psc(r300
, &vformat
);
254 if (memcmp(&r300
->vertex_info
, &vformat
,
255 sizeof(struct r300_vertex_format
))) {
256 memcpy(&r300
->vertex_info
, &vformat
,
257 sizeof(struct r300_vertex_format
));
258 r300
->dirty_state
|= R300_NEW_VERTEX_FORMAT
;
262 /* Set up the mappings from GB to US, for RS block. */
263 static void r300_update_fs_tab(struct r300_context
* r300
)
265 struct r300_vertex_format
* vformat
= &r300
->vertex_info
;
266 struct tgsi_shader_info
* info
= &r300
->fs
->info
;
267 int i
, cols
= 0, texs
= 0, cols_emitted
= 0;
268 int* tab
= vformat
->fs_tab
;
270 for (i
= 0; i
< 16; i
++) {
274 assert(info
->num_inputs
<= 16);
275 for (i
= 0; i
< info
->num_inputs
; i
++) {
276 switch (info
->input_semantic_name
[i
]) {
277 case TGSI_SEMANTIC_COLOR
:
278 tab
[i
] = INTERP_LINEAR
;
281 case TGSI_SEMANTIC_POSITION
:
282 case TGSI_SEMANTIC_PSIZE
:
283 debug_printf("r300: Implementation error: Can't use "
284 "pos attribs in fragshader yet!\n");
285 /* Pass through for now */
286 case TGSI_SEMANTIC_FOG
:
287 case TGSI_SEMANTIC_GENERIC
:
288 tab
[i
] = INTERP_PERSPECTIVE
;
291 debug_printf("r300: Unknown vertex input %d\n",
292 info
->input_semantic_name
[i
]);
297 /* Now that we know where everything is... */
298 debug_printf("r300: fp input count: %d\n", info
->num_inputs
);
299 for (i
= 0; i
< info
->num_inputs
; i
++) {
302 debug_printf("r300: attrib: "
303 "stack offset %d, color, tab %d\n",
305 tab
[i
] = cols_emitted
;
308 case INTERP_PERSPECTIVE
:
309 debug_printf("r300: attrib: "
310 "stack offset %d, texcoord, tab %d\n",
312 tab
[i
] = cols
+ texs
;
316 debug_printf("r300: Implementation error: Bad fp interp!\n");
324 /* Set up the RS block. This is the part of the chipset that actually does
325 * the rasterization of vertices into fragments. This is also the part of the
326 * chipset that locks up if any part of it is even slightly wrong. */
327 static void r300_update_rs_block(struct r300_context
* r300
)
329 struct r300_rs_block
* rs
= r300
->rs_block
;
330 struct tgsi_shader_info
* info
= &r300
->fs
->info
;
331 int* tab
= r300
->vertex_info
.fs_tab
;
332 int col_count
= 0, fp_offset
= 0, i
, memory_pos
, tex_count
= 0;
334 memset(rs
, 0, sizeof(struct r300_rs_block
));
336 if (r300_screen(r300
->context
.screen
)->caps
->is_r500
) {
337 for (i
= 0; i
< info
->num_inputs
; i
++) {
338 assert(tab
[i
] != -1);
339 memory_pos
= tab
[i
] * 4;
340 switch (info
->input_semantic_name
[i
]) {
341 case TGSI_SEMANTIC_COLOR
:
343 R500_RS_COL_PTR(memory_pos
) |
344 R500_RS_COL_FMT(R300_RS_COL_FMT_RGBA
);
347 case TGSI_SEMANTIC_GENERIC
:
349 R500_RS_SEL_S(memory_pos
) |
350 R500_RS_SEL_T(memory_pos
+ 1) |
351 R500_RS_SEL_R(memory_pos
+ 2) |
352 R500_RS_SEL_Q(memory_pos
+ 3);
360 if (col_count
== 0) {
361 rs
->ip
[0] |= R500_RS_COL_FMT(R300_RS_COL_FMT_0001
);
364 if (tex_count
== 0) {
366 R500_RS_SEL_S(R500_RS_IP_PTR_K0
) |
367 R500_RS_SEL_T(R500_RS_IP_PTR_K0
) |
368 R500_RS_SEL_R(R500_RS_IP_PTR_K0
) |
369 R500_RS_SEL_Q(R500_RS_IP_PTR_K1
);
372 /* Rasterize at least one color, or bad things happen. */
373 if ((col_count
== 0) && (tex_count
== 0)) {
377 for (i
= 0; i
< tex_count
; i
++) {
378 rs
->inst
[i
] |= R500_RS_INST_TEX_ID(i
) |
379 R500_RS_INST_TEX_CN_WRITE
| R500_RS_INST_TEX_ADDR(fp_offset
);
383 for (i
= 0; i
< col_count
; i
++) {
384 rs
->inst
[i
] |= R500_RS_INST_COL_ID(i
) |
385 R500_RS_INST_COL_CN_WRITE
| R500_RS_INST_COL_ADDR(fp_offset
);
389 for (i
= 0; i
< info
->num_inputs
; i
++) {
390 assert(tab
[i
] != -1);
391 memory_pos
= tab
[i
] * 4;
392 switch (info
->input_semantic_name
[i
]) {
393 case TGSI_SEMANTIC_COLOR
:
395 R300_RS_COL_PTR(memory_pos
) |
396 R300_RS_COL_FMT(R300_RS_COL_FMT_RGBA
);
399 case TGSI_SEMANTIC_GENERIC
:
401 R300_RS_TEX_PTR(memory_pos
) |
402 R300_RS_SEL_S(R300_RS_SEL_C0
) |
403 R300_RS_SEL_T(R300_RS_SEL_C1
) |
404 R300_RS_SEL_R(R300_RS_SEL_C2
) |
405 R300_RS_SEL_Q(R300_RS_SEL_C3
);
413 if (col_count
== 0) {
414 rs
->ip
[0] |= R300_RS_COL_FMT(R300_RS_COL_FMT_0001
);
417 if (tex_count
== 0) {
419 R300_RS_SEL_S(R300_RS_SEL_K0
) |
420 R300_RS_SEL_T(R300_RS_SEL_K0
) |
421 R300_RS_SEL_R(R300_RS_SEL_K0
) |
422 R300_RS_SEL_Q(R300_RS_SEL_K1
);
425 /* Rasterize at least one color, or bad things happen. */
426 if ((col_count
== 0) && (tex_count
== 0)) {
430 for (i
= 0; i
< tex_count
; i
++) {
431 rs
->inst
[i
] |= R300_RS_INST_TEX_ID(i
) |
432 R300_RS_INST_TEX_CN_WRITE
| R300_RS_INST_TEX_ADDR(fp_offset
);
436 for (i
= 0; i
< col_count
; i
++) {
437 rs
->inst
[i
] |= R300_RS_INST_COL_ID(i
) |
438 R300_RS_INST_COL_CN_WRITE
| R300_RS_INST_COL_ADDR(fp_offset
);
443 rs
->count
= (tex_count
* 4) | (col_count
<< R300_IC_COUNT_SHIFT
) |
446 rs
->inst_count
= MAX2(MAX2(col_count
- 1, tex_count
- 1), 0);
449 void r300_update_derived_state(struct r300_context
* r300
)
451 if (r300
->dirty_state
&
452 (R300_NEW_FRAGMENT_SHADER
| R300_NEW_VERTEX_SHADER
)) {
453 r300_update_vertex_format(r300
);
456 if (r300
->dirty_state
& R300_NEW_VERTEX_FORMAT
) {
457 r300_update_fs_tab(r300
);
458 r300_update_rs_block(r300
);