1 /*
2 * Copyright 2009 Corbin Simpson <MostAwesomeDude@gmail.com>
3 * Copyright 2009 Marek Olšák <maraeo@gmail.com>
4 * SPDX-License-Identifier: MIT
5 */
6
7 #include "r300_vs.h"
8
9 #include "r300_context.h"
10 #include "r300_screen.h"
11 #include "r300_tgsi_to_rc.h"
12 #include "r300_reg.h"
13
14 #include "tgsi/tgsi_dump.h"
15
16 #include "compiler/radeon_compiler.h"
17
18 /* Convert info about VS output semantics into r300_shader_semantics. */
r300_shader_read_vs_outputs(struct r300_context * r300,struct tgsi_shader_info * info,struct r300_shader_semantics * vs_outputs)19 static void r300_shader_read_vs_outputs(
20 struct r300_context *r300,
21 struct tgsi_shader_info* info,
22 struct r300_shader_semantics* vs_outputs)
23 {
24 int i;
25 unsigned index;
26
27 r300_shader_semantics_reset(vs_outputs);
28
29 for (i = 0; i < info->num_outputs; i++) {
30 index = info->output_semantic_index[i];
31
32 switch (info->output_semantic_name[i]) {
33 case TGSI_SEMANTIC_POSITION:
34 assert(index == 0);
35 vs_outputs->pos = i;
36 break;
37
38 case TGSI_SEMANTIC_PSIZE:
39 assert(index == 0);
40 vs_outputs->psize = i;
41 break;
42
43 case TGSI_SEMANTIC_COLOR:
44 assert(index < ATTR_COLOR_COUNT);
45 vs_outputs->color[index] = i;
46 break;
47
48 case TGSI_SEMANTIC_BCOLOR:
49 assert(index < ATTR_COLOR_COUNT);
50 vs_outputs->bcolor[index] = i;
51 break;
52
53 case TGSI_SEMANTIC_TEXCOORD:
54 assert(index < ATTR_TEXCOORD_COUNT);
55 vs_outputs->texcoord[index] = i;
56 vs_outputs->num_texcoord++;
57 break;
58
59 case TGSI_SEMANTIC_GENERIC:
60 assert(index < ATTR_GENERIC_COUNT);
61 vs_outputs->generic[index] = i;
62 vs_outputs->num_generic++;
63 break;
64
65 case TGSI_SEMANTIC_FOG:
66 assert(index == 0);
67 vs_outputs->fog = i;
68 break;
69
70 case TGSI_SEMANTIC_EDGEFLAG:
71 assert(index == 0);
72 fprintf(stderr, "r300 VP: cannot handle edgeflag output.\n");
73 break;
74
75 case TGSI_SEMANTIC_CLIPVERTEX:
76 assert(index == 0);
77 /* Draw does clip vertex for us. */
78 if (r300->screen->caps.has_tcl) {
79 unreachable();
80 }
81 break;
82
83 default:
84 fprintf(stderr, "r300 VP: unknown vertex output semantic: %i.\n",
85 info->output_semantic_name[i]);
86 }
87 }
88
89 /* WPOS is a straight copy of POSITION */
90 vs_outputs->wpos = i;
91 }
92
set_vertex_inputs_outputs(struct r300_vertex_program_compiler * c)93 static void set_vertex_inputs_outputs(struct r300_vertex_program_compiler * c)
94 {
95 struct r300_vertex_shader_code * vs = c->UserData;
96 struct r300_shader_semantics* outputs = &vs->outputs;
97 struct tgsi_shader_info* info = &vs->info;
98 int i, reg = 0;
99 bool any_bcolor_used = outputs->bcolor[0] != ATTR_UNUSED ||
100 outputs->bcolor[1] != ATTR_UNUSED;
101
102 /* Fill in the input mapping */
103 for (i = 0; i < info->num_inputs; i++)
104 c->code->inputs[i] = i;
105
106 /* Position. */
107 if (outputs->pos != ATTR_UNUSED) {
108 c->code->outputs[outputs->pos] = reg++;
109 } else {
110 assert(0);
111 }
112
113 /* Point size. */
114 if (outputs->psize != ATTR_UNUSED) {
115 c->code->outputs[outputs->psize] = reg++;
116 }
117
118 /* If we're writing back facing colors we need to send
119 * four colors to make front/back face colors selection work.
120 * If the vertex program doesn't write all 4 colors, lets
121 * pretend it does by skipping output index reg so the colors
122 * get written into appropriate output vectors.
123 */
124
125 /* Colors. */
126 for (i = 0; i < ATTR_COLOR_COUNT; i++) {
127 if (outputs->color[i] != ATTR_UNUSED) {
128 c->code->outputs[outputs->color[i]] = reg++;
129 } else if (any_bcolor_used ||
130 outputs->color[1] != ATTR_UNUSED) {
131 reg++;
132 }
133 }
134
135 /* Back-face colors. */
136 for (i = 0; i < ATTR_COLOR_COUNT; i++) {
137 if (outputs->bcolor[i] != ATTR_UNUSED) {
138 c->code->outputs[outputs->bcolor[i]] = reg++;
139 } else if (any_bcolor_used) {
140 reg++;
141 }
142 }
143
144 /* Generics. */
145 for (i = 0; i < ATTR_GENERIC_COUNT; i++) {
146 if (outputs->generic[i] != ATTR_UNUSED) {
147 c->code->outputs[outputs->generic[i]] = reg++;
148 }
149 }
150
151 /* Texture coordinates. */
152 for (i = 0; i < ATTR_TEXCOORD_COUNT; i++) {
153 if (outputs->texcoord[i] != ATTR_UNUSED) {
154 c->code->outputs[outputs->texcoord[i]] = reg++;
155 }
156 }
157
158 /* Fog coordinates. */
159 if (outputs->fog != ATTR_UNUSED) {
160 c->code->outputs[outputs->fog] = reg++;
161 }
162
163 /* WPOS. */
164 if (vs->wpos)
165 c->code->outputs[outputs->wpos] = reg++;
166 }
167
r300_init_vs_outputs(struct r300_context * r300,struct r300_vertex_shader * vs)168 void r300_init_vs_outputs(struct r300_context *r300,
169 struct r300_vertex_shader *vs)
170 {
171 tgsi_scan_shader(vs->state.tokens, &vs->shader->info);
172 r300_shader_read_vs_outputs(r300, &vs->shader->info, &vs->shader->outputs);
173 }
174
r300_translate_vertex_shader(struct r300_context * r300,struct r300_vertex_shader * shader)175 void r300_translate_vertex_shader(struct r300_context *r300,
176 struct r300_vertex_shader *shader)
177 {
178 struct r300_vertex_program_compiler compiler;
179 struct tgsi_to_rc ttr;
180 unsigned i;
181 struct r300_vertex_shader_code *vs = shader->shader;
182
183 r300_init_vs_outputs(r300, shader);
184
185 /* Nothing to do if the shader does not write gl_Position. */
186 if (vs->outputs.pos == ATTR_UNUSED) {
187 vs->dummy = true;
188 return;
189 }
190
191 /* Setup the compiler */
192 memset(&compiler, 0, sizeof(compiler));
193 rc_init(&compiler.Base, &r300->vs_regalloc_state);
194
195 DBG_ON(r300, DBG_VP) ? compiler.Base.Debug |= RC_DBG_LOG : 0;
196 compiler.code = &vs->code;
197 compiler.UserData = vs;
198 compiler.Base.debug = &r300->context.debug;
199 compiler.Base.is_r500 = r300->screen->caps.is_r500;
200 compiler.Base.disable_optimizations = DBG_ON(r300, DBG_NO_OPT);
201 /* Only R500 has few IEEE math opcodes. */
202 if (r300->screen->options.ieeemath && r300->screen->caps.is_r500) {
203 compiler.Base.math_rules = RC_MATH_IEEE;
204 } else if (r300->screen->options.ffmath) {
205 compiler.Base.math_rules = RC_MATH_FF;
206 }
207 compiler.Base.has_half_swizzles = false;
208 compiler.Base.has_presub = false;
209 compiler.Base.has_omod = false;
210 compiler.Base.max_temp_regs = 32;
211 compiler.Base.max_constants = 256;
212 compiler.Base.max_alu_insts = r300->screen->caps.is_r500 ? 1024 : 256;
213
214 if (compiler.Base.Debug & RC_DBG_LOG) {
215 DBG(r300, DBG_VP, "r300: Initial vertex program\n");
216 tgsi_dump(shader->state.tokens, 0);
217 }
218
219 /* Translate TGSI to our internal representation */
220 ttr.compiler = &compiler.Base;
221 ttr.info = &vs->info;
222
223 r300_tgsi_to_rc(&ttr, shader->state.tokens);
224
225 if (ttr.error) {
226 fprintf(stderr, "r300 VP: Cannot translate a shader. "
227 "Corresponding draws will be skipped.\n");
228 vs->dummy = true;
229 return;
230 }
231
232 if (compiler.Base.Program.Constants.Count > 200) {
233 compiler.Base.remove_unused_constants = true;
234 }
235
236 compiler.RequiredOutputs = ~(~0U << (vs->info.num_outputs + (vs->wpos ? 1 : 0)));
237 compiler.SetHwInputOutput = &set_vertex_inputs_outputs;
238
239 /* Insert the WPOS output. */
240 if (vs->wpos)
241 rc_copy_output(&compiler.Base, vs->outputs.pos, vs->outputs.wpos);
242
243 /* Invoke the compiler */
244 r3xx_compile_vertex_program(&compiler);
245 if (compiler.Base.Error) {
246 fprintf(stderr, "r300 VP: Compiler error:\n%sCorresponding draws will be"
247 " skipped.\n", compiler.Base.ErrorMsg);
248
249 rc_destroy(&compiler.Base);
250 vs->dummy = true;
251 return;
252 }
253
254 /* Initialize numbers of constants for each type. */
255 vs->externals_count = 0;
256 for (i = 0;
257 i < vs->code.constants.Count &&
258 vs->code.constants.Constants[i].Type == RC_CONSTANT_EXTERNAL; i++) {
259 vs->externals_count = i+1;
260 }
261 for (; i < vs->code.constants.Count; i++) {
262 assert(vs->code.constants.Constants[i].Type == RC_CONSTANT_IMMEDIATE);
263 }
264 vs->immediates_count = vs->code.constants.Count - vs->externals_count;
265
266 /* And, finally... */
267 rc_destroy(&compiler.Base);
268 }
269