2 * Copyright 2009 Corbin Simpson <MostAwesomeDude@gmail.com>
3 * Copyright 2009 Marek Olšák <maraeo@gmail.com>
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * on the rights to use, copy, modify, merge, publish, distribute, sub
9 * license, and/or sell copies of the Software, and to permit persons to whom
10 * the Software is furnished to do so, subject to the following conditions:
12 * The above copyright notice and this permission notice (including the next
13 * paragraph) shall be included in all copies or substantial portions of the
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHOR(S) AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
20 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
21 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
22 * USE OR OTHER DEALINGS IN THE SOFTWARE. */
27 #include "r300_context.h"
28 #include "r300_screen.h"
29 #include "r300_tgsi_to_rc.h"
32 #include "tgsi/tgsi_dump.h"
33 #include "tgsi/tgsi_parse.h"
35 #include "radeon_compiler.h"
37 #include "util/u_math.h"
39 /* Convert info about VS output semantics into r300_shader_semantics. */
40 static void r300_shader_read_vs_outputs(
41 struct tgsi_shader_info
* info
,
42 struct r300_shader_semantics
* vs_outputs
)
47 r300_shader_semantics_reset(vs_outputs
);
49 for (i
= 0; i
< info
->num_outputs
; i
++) {
50 index
= info
->output_semantic_index
[i
];
52 switch (info
->output_semantic_name
[i
]) {
53 case TGSI_SEMANTIC_POSITION
:
58 case TGSI_SEMANTIC_PSIZE
:
60 vs_outputs
->psize
= i
;
63 case TGSI_SEMANTIC_COLOR
:
64 assert(index
<= ATTR_COLOR_COUNT
);
65 vs_outputs
->color
[index
] = i
;
68 case TGSI_SEMANTIC_BCOLOR
:
69 assert(index
<= ATTR_COLOR_COUNT
);
70 vs_outputs
->bcolor
[index
] = i
;
73 case TGSI_SEMANTIC_GENERIC
:
74 assert(index
<= ATTR_GENERIC_COUNT
);
75 vs_outputs
->generic
[index
] = i
;
78 case TGSI_SEMANTIC_FOG
:
83 case TGSI_SEMANTIC_EDGEFLAG
:
85 fprintf(stderr
, "r300 VP: cannot handle edgeflag output\n");
94 static void r300_shader_vap_output_fmt(struct r300_vertex_shader
* vs
)
96 struct r300_shader_semantics
* vs_outputs
= &vs
->outputs
;
97 uint32_t* hwfmt
= vs
->hwfmt
;
99 boolean any_bcolor_used
= vs_outputs
->bcolor
[0] != ATTR_UNUSED
||
100 vs_outputs
->bcolor
[1] != ATTR_UNUSED
;
102 /* Do the actual vertex_info setup.
104 * vertex_info has four uints of hardware-specific data in it.
105 * vinfo.hwfmt[0] is R300_VAP_VTX_STATE_CNTL
106 * vinfo.hwfmt[1] is R300_VAP_VSM_VTX_ASSM
107 * vinfo.hwfmt[2] is R300_VAP_OUTPUT_VTX_FMT_0
108 * vinfo.hwfmt[3] is R300_VAP_OUTPUT_VTX_FMT_1 */
110 hwfmt
[0] = 0x5555; /* XXX this is classic Mesa bonghits */
113 if (vs_outputs
->pos
!= ATTR_UNUSED
) {
114 hwfmt
[1] |= R300_INPUT_CNTL_POS
;
115 hwfmt
[2] |= R300_VAP_OUTPUT_VTX_FMT_0__POS_PRESENT
;
121 if (vs_outputs
->psize
!= ATTR_UNUSED
) {
122 hwfmt
[2] |= R300_VAP_OUTPUT_VTX_FMT_0__PT_SIZE_PRESENT
;
126 for (i
= 0; i
< ATTR_COLOR_COUNT
; i
++) {
127 if (vs_outputs
->color
[i
] != ATTR_UNUSED
|| any_bcolor_used
) {
128 hwfmt
[1] |= R300_INPUT_CNTL_COLOR
;
129 hwfmt
[2] |= R300_VAP_OUTPUT_VTX_FMT_0__COLOR_0_PRESENT
<< i
;
133 /* Back-face colors. */
134 if (any_bcolor_used
) {
135 for (i
= 0; i
< ATTR_COLOR_COUNT
; i
++) {
136 hwfmt
[1] |= R300_INPUT_CNTL_COLOR
;
137 hwfmt
[2] |= R300_VAP_OUTPUT_VTX_FMT_0__COLOR_0_PRESENT
<< (2+i
);
141 /* Texture coordinates. */
143 for (i
= 0; i
< ATTR_GENERIC_COUNT
; i
++) {
144 if (vs_outputs
->generic
[i
] != ATTR_UNUSED
) {
145 hwfmt
[1] |= (R300_INPUT_CNTL_TC0
<< gen_count
);
146 hwfmt
[3] |= (4 << (3 * gen_count
));
151 /* Fog coordinates. */
152 if (vs_outputs
->fog
!= ATTR_UNUSED
) {
153 hwfmt
[1] |= (R300_INPUT_CNTL_TC0
<< gen_count
);
154 hwfmt
[3] |= (4 << (3 * gen_count
));
159 assert(gen_count
<= 8);
162 vs
->wpos_tex_output
= gen_count
;
165 /* Sets up stream mapping to equivalent VS outputs if TCL is bypassed
166 * or isn't present. */
167 static void r300_stream_locations_notcl(
168 struct r300_shader_semantics
* vs_outputs
,
171 int i
, tabi
= 0, gen_count
;
172 boolean any_bcolor_used
= vs_outputs
->bcolor
[0] != ATTR_UNUSED
||
173 vs_outputs
->bcolor
[1] != ATTR_UNUSED
;
176 stream_loc
[tabi
++] = 0;
179 if (vs_outputs
->psize
!= ATTR_UNUSED
) {
180 stream_loc
[tabi
++] = 1;
184 for (i
= 0; i
< ATTR_COLOR_COUNT
; i
++) {
185 if (vs_outputs
->color
[i
] != ATTR_UNUSED
|| any_bcolor_used
) {
186 stream_loc
[tabi
++] = 2 + i
;
190 /* Back-face colors. */
191 if (any_bcolor_used
) {
192 for (i
= 0; i
< ATTR_COLOR_COUNT
; i
++) {
193 stream_loc
[tabi
++] = 4 + i
;
197 /* Texture coordinates. */
199 for (i
= 0; i
< ATTR_GENERIC_COUNT
; i
++) {
200 if (vs_outputs
->generic
[i
] != ATTR_UNUSED
) {
202 stream_loc
[tabi
++] = 6 + gen_count
;
207 /* Fog coordinates. */
208 if (vs_outputs
->fog
!= ATTR_UNUSED
) {
210 stream_loc
[tabi
++] = 6 + gen_count
;
215 if (vs_outputs
->wpos
!= ATTR_UNUSED
) {
217 stream_loc
[tabi
++] = 6 + gen_count
;
222 stream_loc
[tabi
++] = -1;
226 static void set_vertex_inputs_outputs(struct r300_vertex_program_compiler
* c
)
228 struct r300_vertex_shader
* vs
= c
->UserData
;
229 struct r300_shader_semantics
* outputs
= &vs
->outputs
;
230 struct tgsi_shader_info
* info
= &vs
->info
;
232 boolean any_bcolor_used
= outputs
->bcolor
[0] != ATTR_UNUSED
||
233 outputs
->bcolor
[1] != ATTR_UNUSED
;
235 /* Fill in the input mapping */
236 for (i
= 0; i
< info
->num_inputs
; i
++)
237 c
->code
->inputs
[i
] = i
;
240 if (outputs
->pos
!= ATTR_UNUSED
) {
241 c
->code
->outputs
[outputs
->pos
] = reg
++;
247 if (outputs
->psize
!= ATTR_UNUSED
) {
248 c
->code
->outputs
[outputs
->psize
] = reg
++;
251 /* If we're writing back facing colors we need to send
252 * four colors to make front/back face colors selection work.
253 * If the vertex program doesn't write all 4 colors, lets
254 * pretend it does by skipping output index reg so the colors
255 * get written into appropriate output vectors.
259 for (i
= 0; i
< ATTR_COLOR_COUNT
; i
++) {
260 if (outputs
->color
[i
] != ATTR_UNUSED
) {
261 c
->code
->outputs
[outputs
->color
[i
]] = reg
++;
262 } else if (any_bcolor_used
) {
267 /* Back-face colors. */
268 for (i
= 0; i
< ATTR_COLOR_COUNT
; i
++) {
269 if (outputs
->bcolor
[i
] != ATTR_UNUSED
) {
270 c
->code
->outputs
[outputs
->bcolor
[i
]] = reg
++;
271 } else if (any_bcolor_used
) {
276 /* Texture coordinates. */
277 for (i
= 0; i
< ATTR_GENERIC_COUNT
; i
++) {
278 if (outputs
->generic
[i
] != ATTR_UNUSED
) {
279 c
->code
->outputs
[outputs
->generic
[i
]] = reg
++;
283 /* Fog coordinates. */
284 if (outputs
->fog
!= ATTR_UNUSED
) {
285 c
->code
->outputs
[outputs
->fog
] = reg
++;
289 if (outputs
->wpos
!= ATTR_UNUSED
) {
290 c
->code
->outputs
[outputs
->wpos
] = reg
++;
294 static void r300_insert_wpos(struct r300_vertex_program_compiler
* c
,
295 struct r300_shader_semantics
* outputs
)
297 int i
, lastOutput
= 0;
299 /* Find the max output index. */
300 lastOutput
= MAX2(lastOutput
, outputs
->psize
);
301 for (i
= 0; i
< ATTR_COLOR_COUNT
; i
++) {
302 lastOutput
= MAX2(lastOutput
, outputs
->color
[i
]);
303 lastOutput
= MAX2(lastOutput
, outputs
->bcolor
[i
]);
305 for (i
= 0; i
< ATTR_GENERIC_COUNT
; i
++) {
306 lastOutput
= MAX2(lastOutput
, outputs
->generic
[i
]);
308 lastOutput
= MAX2(lastOutput
, outputs
->fog
);
310 /* Set WPOS after the last output. */
312 rc_copy_output(&c
->Base
, 0, lastOutput
); /* out[lastOutput] = out[0]; */
313 outputs
->wpos
= lastOutput
;
316 void r300_translate_vertex_shader(struct r300_context
* r300
,
317 struct r300_vertex_shader
* vs
)
319 struct r300_vertex_program_compiler compiler
;
320 struct tgsi_to_rc ttr
;
323 r300_shader_read_vs_outputs(&vs
->info
, &vs
->outputs
);
325 /* Setup the compiler */
326 rc_init(&compiler
.Base
);
328 compiler
.Base
.Debug
= DBG_ON(r300
, DBG_VP
);
329 compiler
.code
= &vs
->code
;
330 compiler
.UserData
= vs
;
332 if (compiler
.Base
.Debug
) {
333 debug_printf("r300: Initial vertex program\n");
334 tgsi_dump(vs
->state
.tokens
, 0);
337 /* Translate TGSI to our internal representation */
338 ttr
.compiler
= &compiler
.Base
;
339 ttr
.info
= &vs
->info
;
341 r300_tgsi_to_rc(&ttr
, vs
->state
.tokens
);
343 compiler
.RequiredOutputs
= ~(~0 << (vs
->info
.num_outputs
+1));
344 compiler
.SetHwInputOutput
= &set_vertex_inputs_outputs
;
346 /* Insert the WPOS output. */
347 r300_insert_wpos(&compiler
, &vs
->outputs
);
349 r300_shader_vap_output_fmt(vs
);
350 r300_stream_locations_notcl(&vs
->outputs
, vs
->stream_loc_notcl
);
352 /* Invoke the compiler */
353 r3xx_compile_vertex_program(&compiler
);
354 if (compiler
.Base
.Error
) {
355 /* XXX We should fallback using Draw. */
356 fprintf(stderr
, "r300 VP: Compiler error\n");
360 /* And, finally... */
361 rc_destroy(&compiler
.Base
);
362 vs
->translated
= TRUE
;
365 boolean
r300_vertex_shader_setup_wpos(struct r300_context
* r300
)
367 struct r300_vertex_shader
* vs
= r300
->vs
;
368 int tex_output
= r300
->vs
->wpos_tex_output
;
369 uint32_t tex_fmt
= R300_INPUT_CNTL_TC0
<< tex_output
;
370 uint32_t* hwfmt
= vs
->hwfmt
;
372 if (r300
->fs
->inputs
.wpos
!= ATTR_UNUSED
) {
373 /* Enable WPOS in VAP. */
374 if (!(hwfmt
[1] & tex_fmt
)) {
376 hwfmt
[3] |= (4 << (3 * tex_output
));
378 assert(tex_output
< 8);
382 /* Disable WPOS in VAP. */
383 if (hwfmt
[1] & tex_fmt
) {
384 hwfmt
[1] &= ~tex_fmt
;
385 hwfmt
[3] &= ~(4 << (3 * tex_output
));