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 /* Convert info about VS output semantics into r300_shader_semantics. */
38 static void r300_shader_read_vs_outputs(
39 struct tgsi_shader_info
* info
,
40 struct r300_shader_semantics
* vs_outputs
)
45 r300_shader_semantics_reset(vs_outputs
);
47 for (i
= 0; i
< info
->num_outputs
; i
++) {
48 index
= info
->output_semantic_index
[i
];
50 switch (info
->output_semantic_name
[i
]) {
51 case TGSI_SEMANTIC_POSITION
:
56 case TGSI_SEMANTIC_PSIZE
:
58 vs_outputs
->psize
= i
;
61 case TGSI_SEMANTIC_COLOR
:
62 assert(index
< ATTR_COLOR_COUNT
);
63 vs_outputs
->color
[index
] = i
;
66 case TGSI_SEMANTIC_BCOLOR
:
67 assert(index
< ATTR_COLOR_COUNT
);
68 vs_outputs
->bcolor
[index
] = i
;
71 case TGSI_SEMANTIC_GENERIC
:
72 assert(index
< ATTR_GENERIC_COUNT
);
73 vs_outputs
->generic
[index
] = i
;
76 case TGSI_SEMANTIC_FOG
:
81 case TGSI_SEMANTIC_EDGEFLAG
:
83 fprintf(stderr
, "r300 VP: cannot handle edgeflag output\n");
91 /* WPOS is a straight copy of POSITION and it's always emitted. */
95 /* This function sets up:
96 * - VAP mapping, which maps VS registers to output semantics and
97 * at the same time it indicates which attributes are enabled and should
99 * - Stream mapping to VS outputs if TCL is not present. */
100 static void r300_init_vs_output_mapping(struct r300_vertex_shader
* vs
)
102 struct r300_shader_semantics
* vs_outputs
= &vs
->outputs
;
103 struct r300_vap_output_state
*vap_out
= &vs
->vap_out
;
104 int *stream_loc
= vs
->stream_loc_notcl
;
105 int i
, gen_count
, tabi
= 0;
106 boolean any_bcolor_used
= vs_outputs
->bcolor
[0] != ATTR_UNUSED
||
107 vs_outputs
->bcolor
[1] != ATTR_UNUSED
;
109 vap_out
->vap_vtx_state_cntl
= 0x5555; /* XXX this is classic Mesa bonghits */
112 if (vs_outputs
->pos
!= ATTR_UNUSED
) {
113 vap_out
->vap_vsm_vtx_assm
|= R300_INPUT_CNTL_POS
;
114 vap_out
->vap_out_vtx_fmt
[0] |= R300_VAP_OUTPUT_VTX_FMT_0__POS_PRESENT
;
116 stream_loc
[tabi
++] = 0;
122 if (vs_outputs
->psize
!= ATTR_UNUSED
) {
123 vap_out
->vap_out_vtx_fmt
[0] |= R300_VAP_OUTPUT_VTX_FMT_0__PT_SIZE_PRESENT
;
125 stream_loc
[tabi
++] = 1;
129 for (i
= 0; i
< ATTR_COLOR_COUNT
; i
++) {
130 if (vs_outputs
->color
[i
] != ATTR_UNUSED
|| any_bcolor_used
||
131 vs_outputs
->color
[1] != ATTR_UNUSED
) {
132 vap_out
->vap_vsm_vtx_assm
|= R300_INPUT_CNTL_COLOR
;
133 vap_out
->vap_out_vtx_fmt
[0] |= R300_VAP_OUTPUT_VTX_FMT_0__COLOR_0_PRESENT
<< i
;
135 stream_loc
[tabi
++] = 2 + i
;
139 /* Back-face colors. */
140 if (any_bcolor_used
) {
141 for (i
= 0; i
< ATTR_COLOR_COUNT
; i
++) {
142 vap_out
->vap_vsm_vtx_assm
|= R300_INPUT_CNTL_COLOR
;
143 vap_out
->vap_out_vtx_fmt
[0] |= R300_VAP_OUTPUT_VTX_FMT_0__COLOR_0_PRESENT
<< (2+i
);
145 stream_loc
[tabi
++] = 4 + i
;
149 /* Texture coordinates. */
151 for (i
= 0; i
< ATTR_GENERIC_COUNT
; i
++) {
152 if (vs_outputs
->generic
[i
] != ATTR_UNUSED
) {
153 vap_out
->vap_vsm_vtx_assm
|= (R300_INPUT_CNTL_TC0
<< gen_count
);
154 vap_out
->vap_out_vtx_fmt
[1] |= (4 << (3 * gen_count
));
157 stream_loc
[tabi
++] = 6 + gen_count
;
162 /* Fog coordinates. */
163 if (vs_outputs
->fog
!= ATTR_UNUSED
) {
164 vap_out
->vap_vsm_vtx_assm
|= (R300_INPUT_CNTL_TC0
<< gen_count
);
165 vap_out
->vap_out_vtx_fmt
[1] |= (4 << (3 * gen_count
));
168 stream_loc
[tabi
++] = 6 + gen_count
;
173 assert(gen_count
<= 8);
176 vs
->wpos_tex_output
= gen_count
;
179 stream_loc
[tabi
++] = 6 + gen_count
;
182 stream_loc
[tabi
++] = -1;
186 static void set_vertex_inputs_outputs(struct r300_vertex_program_compiler
* c
)
188 struct r300_vertex_shader
* vs
= c
->UserData
;
189 struct r300_shader_semantics
* outputs
= &vs
->outputs
;
190 struct tgsi_shader_info
* info
= &vs
->info
;
192 boolean any_bcolor_used
= outputs
->bcolor
[0] != ATTR_UNUSED
||
193 outputs
->bcolor
[1] != ATTR_UNUSED
;
195 /* Fill in the input mapping */
196 for (i
= 0; i
< info
->num_inputs
; i
++)
197 c
->code
->inputs
[i
] = i
;
200 if (outputs
->pos
!= ATTR_UNUSED
) {
201 c
->code
->outputs
[outputs
->pos
] = reg
++;
207 if (outputs
->psize
!= ATTR_UNUSED
) {
208 c
->code
->outputs
[outputs
->psize
] = reg
++;
211 /* If we're writing back facing colors we need to send
212 * four colors to make front/back face colors selection work.
213 * If the vertex program doesn't write all 4 colors, lets
214 * pretend it does by skipping output index reg so the colors
215 * get written into appropriate output vectors.
219 for (i
= 0; i
< ATTR_COLOR_COUNT
; i
++) {
220 if (outputs
->color
[i
] != ATTR_UNUSED
) {
221 c
->code
->outputs
[outputs
->color
[i
]] = reg
++;
222 } else if (any_bcolor_used
||
223 outputs
->color
[1] != ATTR_UNUSED
) {
228 /* Back-face colors. */
229 for (i
= 0; i
< ATTR_COLOR_COUNT
; i
++) {
230 if (outputs
->bcolor
[i
] != ATTR_UNUSED
) {
231 c
->code
->outputs
[outputs
->bcolor
[i
]] = reg
++;
232 } else if (any_bcolor_used
) {
237 /* Texture coordinates. */
238 for (i
= 0; i
< ATTR_GENERIC_COUNT
; i
++) {
239 if (outputs
->generic
[i
] != ATTR_UNUSED
) {
240 c
->code
->outputs
[outputs
->generic
[i
]] = reg
++;
244 /* Fog coordinates. */
245 if (outputs
->fog
!= ATTR_UNUSED
) {
246 c
->code
->outputs
[outputs
->fog
] = reg
++;
250 if (outputs
->wpos
!= ATTR_UNUSED
) {
251 c
->code
->outputs
[outputs
->wpos
] = reg
++;
255 void r300_vertex_shader_common_init(struct r300_vertex_shader
*vs
,
256 const struct pipe_shader_state
*shader
)
258 /* Copy state directly into shader. */
260 vs
->state
.tokens
= tgsi_dup_tokens(shader
->tokens
);
261 tgsi_scan_shader(shader
->tokens
, &vs
->info
);
263 r300_shader_read_vs_outputs(&vs
->info
, &vs
->outputs
);
264 r300_init_vs_output_mapping(vs
);
267 void r300_translate_vertex_shader(struct r300_context
* r300
,
268 struct r300_vertex_shader
* vs
)
270 struct r300_vertex_program_compiler compiler
;
271 struct tgsi_to_rc ttr
;
273 /* Setup the compiler */
274 rc_init(&compiler
.Base
);
276 compiler
.Base
.Debug
= DBG_ON(r300
, DBG_VP
);
277 compiler
.code
= &vs
->code
;
278 compiler
.UserData
= vs
;
280 if (compiler
.Base
.Debug
) {
281 debug_printf("r300: Initial vertex program\n");
282 tgsi_dump(vs
->state
.tokens
, 0);
285 /* Translate TGSI to our internal representation */
286 ttr
.compiler
= &compiler
.Base
;
287 ttr
.info
= &vs
->info
;
288 ttr
.use_half_swizzles
= FALSE
;
290 r300_tgsi_to_rc(&ttr
, vs
->state
.tokens
);
292 compiler
.RequiredOutputs
= ~(~0 << (vs
->info
.num_outputs
+1));
293 compiler
.SetHwInputOutput
= &set_vertex_inputs_outputs
;
295 /* Insert the WPOS output. */
296 rc_copy_output(&compiler
.Base
, 0, vs
->outputs
.wpos
);
298 /* Invoke the compiler */
299 r3xx_compile_vertex_program(&compiler
);
300 if (compiler
.Base
.Error
) {
301 /* XXX We should fallback using Draw. */
302 fprintf(stderr
, "r300 VP: Compiler error:\n%s", compiler
.Base
.ErrorMsg
);
306 /* And, finally... */
307 rc_destroy(&compiler
.Base
);
310 boolean
r300_vertex_shader_setup_wpos(struct r300_context
* r300
)
312 struct r300_vertex_shader
* vs
= r300
->vs_state
.state
;
313 struct r300_vap_output_state
*vap_out
= &vs
->vap_out
;
314 int tex_output
= vs
->wpos_tex_output
;
315 uint32_t tex_fmt
= R300_INPUT_CNTL_TC0
<< tex_output
;
317 if (r300
->fs
->inputs
.wpos
!= ATTR_UNUSED
) {
318 /* Enable WPOS in VAP. */
319 if (!(vap_out
->vap_vsm_vtx_assm
& tex_fmt
)) {
320 vap_out
->vap_vsm_vtx_assm
|= tex_fmt
;
321 vap_out
->vap_out_vtx_fmt
[1] |= (4 << (3 * tex_output
));
323 assert(tex_output
< 8);
327 /* Disable WPOS in VAP. */
328 if (vap_out
->vap_vsm_vtx_assm
& tex_fmt
) {
329 vap_out
->vap_vsm_vtx_assm
&= ~tex_fmt
;
330 vap_out
->vap_out_vtx_fmt
[1] &= ~(4 << (3 * tex_output
));