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. */
26 #include "r300_context.h"
27 #include "r300_screen.h"
28 #include "r300_tgsi_to_rc.h"
31 #include "tgsi/tgsi_dump.h"
32 #include "tgsi/tgsi_parse.h"
34 #include "radeon_compiler.h"
36 /* Convert info about VS output semantics into r300_shader_semantics. */
37 static void r300_shader_read_vs_outputs(
38 struct tgsi_shader_info
* info
,
39 struct r300_shader_semantics
* vs_outputs
)
44 r300_shader_semantics_reset(vs_outputs
);
46 for (i
= 0; i
< info
->num_outputs
; i
++) {
47 index
= info
->output_semantic_index
[i
];
49 switch (info
->output_semantic_name
[i
]) {
50 case TGSI_SEMANTIC_POSITION
:
55 case TGSI_SEMANTIC_PSIZE
:
57 vs_outputs
->psize
= i
;
60 case TGSI_SEMANTIC_COLOR
:
61 assert(index
<= ATTR_COLOR_COUNT
);
62 vs_outputs
->color
[index
] = i
;
65 case TGSI_SEMANTIC_BCOLOR
:
66 assert(index
<= ATTR_COLOR_COUNT
);
67 vs_outputs
->bcolor
[index
] = i
;
70 case TGSI_SEMANTIC_GENERIC
:
71 assert(index
<= ATTR_GENERIC_COUNT
);
72 vs_outputs
->generic
[index
] = i
;
75 case TGSI_SEMANTIC_FOG
:
86 static void r300_shader_vap_output_fmt(
87 struct r300_shader_semantics
* vs_outputs
,
92 /* Do the actual vertex_info setup.
94 * vertex_info has four uints of hardware-specific data in it.
95 * vinfo.hwfmt[0] is R300_VAP_VTX_STATE_CNTL
96 * vinfo.hwfmt[1] is R300_VAP_VSM_VTX_ASSM
97 * vinfo.hwfmt[2] is R300_VAP_OUTPUT_VTX_FMT_0
98 * vinfo.hwfmt[3] is R300_VAP_OUTPUT_VTX_FMT_1 */
100 hwfmt
[0] = 0x5555; /* XXX this is classic Mesa bonghits */
103 if (vs_outputs
->pos
!= ATTR_UNUSED
) {
104 hwfmt
[1] |= R300_INPUT_CNTL_POS
;
105 hwfmt
[2] |= R300_VAP_OUTPUT_VTX_FMT_0__POS_PRESENT
;
111 if (vs_outputs
->psize
!= ATTR_UNUSED
) {
112 hwfmt
[2] |= R300_VAP_OUTPUT_VTX_FMT_0__PT_SIZE_PRESENT
;
116 for (i
= 0; i
< ATTR_COLOR_COUNT
; i
++) {
117 if (vs_outputs
->color
[i
] != ATTR_UNUSED
) {
118 hwfmt
[1] |= R300_INPUT_CNTL_COLOR
;
119 hwfmt
[2] |= R300_VAP_OUTPUT_VTX_FMT_0__COLOR_0_PRESENT
<< i
;
123 /* XXX Back-face colors. */
125 /* Texture coordinates. */
127 for (i
= 0; i
< ATTR_GENERIC_COUNT
; i
++) {
128 if (vs_outputs
->generic
[i
] != ATTR_UNUSED
) {
129 hwfmt
[1] |= (R300_INPUT_CNTL_TC0
<< gen_count
);
130 hwfmt
[3] |= (4 << (3 * gen_count
));
135 /* Fog coordinates. */
136 if (vs_outputs
->fog
!= ATTR_UNUSED
) {
137 hwfmt
[1] |= (R300_INPUT_CNTL_TC0
<< gen_count
);
138 hwfmt
[3] |= (4 << (3 * gen_count
));
143 assert(gen_count
<= 8);
146 /* Set VS output stream locations for SWTCL. */
147 static void r300_stream_locations_swtcl(
148 struct r300_shader_semantics
* vs_outputs
,
149 int* output_stream_loc
)
151 int i
, tabi
= 0, gen_count
;
153 /* XXX Check whether the numbers (0, 1, 2+i, etc.) are correct.
154 * These should go to VAP_PROG_STREAM_CNTL/DST_VEC_LOC. */
157 output_stream_loc
[tabi
++] = 0;
160 if (vs_outputs
->psize
!= ATTR_UNUSED
) {
161 output_stream_loc
[tabi
++] = 1;
165 for (i
= 0; i
< ATTR_COLOR_COUNT
; i
++) {
166 if (vs_outputs
->color
[i
] != ATTR_UNUSED
) {
167 output_stream_loc
[tabi
++] = 2 + i
;
171 /* Back-face colors. */
172 for (i
= 0; i
< ATTR_COLOR_COUNT
; i
++) {
173 if (vs_outputs
->bcolor
[i
] != ATTR_UNUSED
) {
174 output_stream_loc
[tabi
++] = 4 + i
;
178 /* Texture coordinates. */
180 for (i
= 0; i
< ATTR_GENERIC_COUNT
; i
++) {
181 if (vs_outputs
->bcolor
[i
] != ATTR_UNUSED
) {
183 output_stream_loc
[tabi
++] = 6 + gen_count
;
188 /* Fog coordinates. */
189 if (vs_outputs
->fog
!= ATTR_UNUSED
) {
191 output_stream_loc
[tabi
++] = 6 + gen_count
;
196 assert(gen_count
<= 8);
199 output_stream_loc
[tabi
++] = -1;
203 static void set_vertex_inputs_outputs(struct r300_vertex_program_compiler
* c
)
205 struct r300_vertex_shader
* vs
= c
->UserData
;
206 struct r300_shader_semantics
* outputs
= &vs
->outputs
;
207 struct tgsi_shader_info
* info
= &vs
->info
;
210 /* Fill in the input mapping */
211 for (i
= 0; i
< info
->num_inputs
; i
++)
212 c
->code
->inputs
[i
] = i
;
215 if (outputs
->pos
!= ATTR_UNUSED
) {
216 c
->code
->outputs
[outputs
->pos
] = reg
++;
222 if (outputs
->psize
!= ATTR_UNUSED
) {
223 c
->code
->outputs
[outputs
->psize
] = reg
++;
227 for (i
= 0; i
< ATTR_COLOR_COUNT
; i
++) {
228 if (outputs
->color
[i
] != ATTR_UNUSED
) {
229 c
->code
->outputs
[outputs
->color
[i
]] = reg
++;
233 /* XXX Back-face colors. */
235 /* Texture coordinates. */
236 for (i
= 0; i
< ATTR_GENERIC_COUNT
; i
++) {
237 if (outputs
->generic
[i
] != ATTR_UNUSED
) {
238 c
->code
->outputs
[outputs
->generic
[i
]] = reg
++;
242 /* Fog coordinates. */
243 if (outputs
->fog
!= ATTR_UNUSED
) {
244 c
->code
->outputs
[outputs
->fog
] = reg
++;
248 void r300_translate_vertex_shader(struct r300_context
* r300
,
249 struct r300_vertex_shader
* vs
)
251 struct r300_vertex_program_compiler compiler
;
252 struct tgsi_to_rc ttr
;
255 r300_shader_read_vs_outputs(&vs
->info
, &vs
->outputs
);
256 r300_shader_vap_output_fmt(&vs
->outputs
, vs
->hwfmt
);
258 if (!r300_screen(r300
->context
.screen
)->caps
->has_tcl
) {
259 r300_stream_locations_swtcl(&vs
->outputs
, vs
->output_stream_loc_swtcl
);
262 /* Setup the compiler */
263 rc_init(&compiler
.Base
);
265 compiler
.Base
.Debug
= DBG_ON(r300
, DBG_VP
);
266 compiler
.code
= &vs
->code
;
267 compiler
.UserData
= vs
;
269 if (compiler
.Base
.Debug
) {
270 debug_printf("r300: Initial vertex program\n");
271 tgsi_dump(vs
->state
.tokens
, 0);
274 /* Translate TGSI to our internal representation */
275 ttr
.compiler
= &compiler
.Base
;
276 ttr
.info
= &vs
->info
;
278 r300_tgsi_to_rc(&ttr
, vs
->state
.tokens
);
280 compiler
.RequiredOutputs
= ~(~0 << vs
->info
.num_outputs
);
281 compiler
.SetHwInputOutput
= &set_vertex_inputs_outputs
;
283 /* Invoke the compiler */
284 r3xx_compile_vertex_program(&compiler
);
285 if (compiler
.Base
.Error
) {
286 /* XXX Fail gracefully */
287 fprintf(stderr
, "r300 VP: Compiler error\n");
291 /* And, finally... */
292 rc_destroy(&compiler
.Base
);
293 vs
->translated
= TRUE
;