svga: rename shader_result -> variant
[mesa.git] / src / gallium / drivers / svga / svga_tgsi.c
1 /**********************************************************
2 * Copyright 2008-2009 VMware, Inc. All rights reserved.
3 *
4 * Permission is hereby granted, free of charge, to any person
5 * obtaining a copy of this software and associated documentation
6 * files (the "Software"), to deal in the Software without
7 * restriction, including without limitation the rights to use, copy,
8 * modify, merge, publish, distribute, sublicense, and/or sell copies
9 * of the Software, and to permit persons to whom the Software is
10 * furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be
13 * included in all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
19 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
20 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 * SOFTWARE.
23 *
24 **********************************************************/
25
26
27 #include "pipe/p_compiler.h"
28 #include "pipe/p_shader_tokens.h"
29 #include "pipe/p_defines.h"
30 #include "tgsi/tgsi_parse.h"
31 #include "tgsi/tgsi_dump.h"
32 #include "tgsi/tgsi_scan.h"
33 #include "util/u_math.h"
34 #include "util/u_memory.h"
35 #include "util/u_bitmask.h"
36
37 #include "svgadump/svga_shader_dump.h"
38
39 #include "svga_context.h"
40 #include "svga_tgsi.h"
41 #include "svga_tgsi_emit.h"
42 #include "svga_debug.h"
43
44 #include "svga_hw_reg.h"
45 #include "svga3d_shaderdefs.h"
46
47
48 /* Sinkhole used only in error conditions.
49 */
50 static char err_buf[128];
51
52 #if 0
53 static void
54 svga_destroy_shader_emitter(struct svga_shader_emitter *emit)
55 {
56 if (emit->buf != err_buf)
57 FREE(emit->buf);
58 }
59 #endif
60
61
62 static boolean
63 svga_shader_expand(struct svga_shader_emitter *emit)
64 {
65 char *new_buf;
66 unsigned newsize = emit->size * 2;
67
68 if (emit->buf != err_buf)
69 new_buf = REALLOC(emit->buf, emit->size, newsize);
70 else
71 new_buf = NULL;
72
73 if (new_buf == NULL) {
74 emit->ptr = err_buf;
75 emit->buf = err_buf;
76 emit->size = sizeof(err_buf);
77 return FALSE;
78 }
79
80 emit->size = newsize;
81 emit->ptr = new_buf + (emit->ptr - emit->buf);
82 emit->buf = new_buf;
83 return TRUE;
84 }
85
86
87 static INLINE boolean
88 reserve(struct svga_shader_emitter *emit, unsigned nr_dwords)
89 {
90 if (emit->ptr - emit->buf + nr_dwords * sizeof(unsigned) >= emit->size) {
91 if (!svga_shader_expand(emit)) {
92 return FALSE;
93 }
94 }
95
96 return TRUE;
97 }
98
99
100 boolean
101 svga_shader_emit_dword(struct svga_shader_emitter * emit, unsigned dword)
102 {
103 if (!reserve(emit, 1))
104 return FALSE;
105
106 *(unsigned *) emit->ptr = dword;
107 emit->ptr += sizeof dword;
108 return TRUE;
109 }
110
111
112 boolean
113 svga_shader_emit_dwords(struct svga_shader_emitter * emit,
114 const unsigned *dwords, unsigned nr)
115 {
116 if (!reserve(emit, nr))
117 return FALSE;
118
119 memcpy(emit->ptr, dwords, nr * sizeof *dwords);
120 emit->ptr += nr * sizeof *dwords;
121 return TRUE;
122 }
123
124
125 boolean
126 svga_shader_emit_opcode(struct svga_shader_emitter * emit, unsigned opcode)
127 {
128 SVGA3dShaderInstToken *here;
129
130 if (!reserve(emit, 1))
131 return FALSE;
132
133 here = (SVGA3dShaderInstToken *) emit->ptr;
134 here->value = opcode;
135
136 if (emit->insn_offset) {
137 SVGA3dShaderInstToken *prev =
138 (SVGA3dShaderInstToken *) (emit->buf + emit->insn_offset);
139 prev->size = (here - prev) - 1;
140 }
141
142 emit->insn_offset = emit->ptr - emit->buf;
143 emit->ptr += sizeof(unsigned);
144 return TRUE;
145 }
146
147
148 static boolean
149 svga_shader_emit_header(struct svga_shader_emitter *emit)
150 {
151 SVGA3dShaderVersion header;
152
153 memset(&header, 0, sizeof header);
154
155 switch (emit->unit) {
156 case PIPE_SHADER_FRAGMENT:
157 header.value = SVGA3D_PS_30;
158 break;
159 case PIPE_SHADER_VERTEX:
160 header.value = SVGA3D_VS_30;
161 break;
162 }
163
164 return svga_shader_emit_dword(emit, header.value);
165 }
166
167
168 /**
169 * Use the shader info to generate a bitmask indicating which generic
170 * inputs are used by the shader. A set bit indicates that GENERIC[i]
171 * is used.
172 */
173 unsigned
174 svga_get_generic_inputs_mask(const struct tgsi_shader_info *info)
175 {
176 unsigned i, mask = 0x0;
177
178 for (i = 0; i < info->num_inputs; i++) {
179 if (info->input_semantic_name[i] == TGSI_SEMANTIC_GENERIC) {
180 unsigned j = info->input_semantic_index[i];
181 assert(j < sizeof(mask) * 8);
182 mask |= 1 << j;
183 }
184 }
185
186 return mask;
187 }
188
189
190 /**
191 * Given a mask of used generic variables (as returned by the above functions)
192 * fill in a table which maps those indexes to small integers.
193 * This table is used by the remap_generic_index() function in
194 * svga_tgsi_decl_sm30.c
195 * Example: if generics_mask = binary(1010) it means that GENERIC[1] and
196 * GENERIC[3] are used. The remap_table will contain:
197 * table[1] = 0;
198 * table[3] = 1;
199 * The remaining table entries will be filled in with the next unused
200 * generic index (in this example, 2).
201 */
202 void
203 svga_remap_generics(unsigned generics_mask,
204 int8_t remap_table[MAX_GENERIC_VARYING])
205 {
206 /* Note texcoord[0] is reserved so start at 1 */
207 unsigned count = 1, i;
208
209 for (i = 0; i < MAX_GENERIC_VARYING; i++) {
210 remap_table[i] = -1;
211 }
212
213 /* for each bit set in generic_mask */
214 while (generics_mask) {
215 unsigned index = ffs(generics_mask) - 1;
216 remap_table[index] = count++;
217 generics_mask &= ~(1 << index);
218 }
219 }
220
221
222 /**
223 * Use the generic remap table to map a TGSI generic varying variable
224 * index to a small integer. If the remapping table doesn't have a
225 * valid value for the given index (the table entry is -1) it means
226 * the fragment shader doesn't use that VS output. Just allocate
227 * the next free value in that case. Alternately, we could cull
228 * VS instructions that write to register, or replace the register
229 * with a dummy temp register.
230 * XXX TODO: we should do one of the later as it would save precious
231 * texcoord registers.
232 */
233 int
234 svga_remap_generic_index(int8_t remap_table[MAX_GENERIC_VARYING],
235 int generic_index)
236 {
237 assert(generic_index < MAX_GENERIC_VARYING);
238
239 if (generic_index >= MAX_GENERIC_VARYING) {
240 /* just don't return a random/garbage value */
241 generic_index = MAX_GENERIC_VARYING - 1;
242 }
243
244 if (remap_table[generic_index] == -1) {
245 /* This is a VS output that has no matching PS input. Find a
246 * free index.
247 */
248 int i, max = 0;
249 for (i = 0; i < MAX_GENERIC_VARYING; i++) {
250 max = MAX2(max, remap_table[i]);
251 }
252 remap_table[generic_index] = max + 1;
253 }
254
255 return remap_table[generic_index];
256 }
257
258
259 /**
260 * Parse TGSI shader and translate to SVGA/DX9 serialized
261 * representation.
262 *
263 * In this function SVGA shader is emitted to an in-memory buffer that
264 * can be dynamically grown. Once we've finished and know how large
265 * it is, it will be copied to a hardware buffer for upload.
266 */
267 static struct svga_shader_variant *
268 svga_tgsi_translate(const struct svga_shader *shader,
269 const struct svga_compile_key *key, unsigned unit)
270 {
271 struct svga_shader_variant *variant = NULL;
272 struct svga_shader_emitter emit;
273
274 memset(&emit, 0, sizeof(emit));
275
276 emit.size = 1024;
277 emit.buf = MALLOC(emit.size);
278 if (emit.buf == NULL) {
279 goto fail;
280 }
281
282 emit.ptr = emit.buf;
283 emit.unit = unit;
284 emit.key = *key;
285
286 tgsi_scan_shader(shader->tokens, &emit.info);
287
288 emit.imm_start = emit.info.file_max[TGSI_FILE_CONSTANT] + 1;
289
290 if (unit == PIPE_SHADER_FRAGMENT)
291 emit.imm_start += key->fkey.num_unnormalized_coords;
292
293 if (unit == PIPE_SHADER_VERTEX) {
294 emit.imm_start += key->vkey.need_prescale ? 2 : 0;
295 }
296
297 emit.nr_hw_float_const =
298 (emit.imm_start + emit.info.file_max[TGSI_FILE_IMMEDIATE] + 1);
299
300 emit.nr_hw_temp = emit.info.file_max[TGSI_FILE_TEMPORARY] + 1;
301
302 if (emit.nr_hw_temp >= SVGA3D_TEMPREG_MAX) {
303 debug_printf("svga: too many temporary registers (%u)\n",
304 emit.nr_hw_temp);
305 goto fail;
306 }
307
308 emit.in_main_func = TRUE;
309
310 if (!svga_shader_emit_header(&emit)) {
311 debug_printf("svga: emit header failed\n");
312 goto fail;
313 }
314
315 if (!svga_shader_emit_instructions(&emit, shader->tokens)) {
316 debug_printf("svga: emit instructions failed\n");
317 goto fail;
318 }
319
320 variant = CALLOC_STRUCT(svga_shader_variant);
321 if (variant == NULL)
322 goto fail;
323
324 variant->shader = shader;
325 variant->tokens = (const unsigned *) emit.buf;
326 variant->nr_tokens = (emit.ptr - emit.buf) / sizeof(unsigned);
327 memcpy(&variant->key, key, sizeof(*key));
328 variant->id = UTIL_BITMASK_INVALID_INDEX;
329
330 if (SVGA_DEBUG & DEBUG_TGSI) {
331 debug_printf("#####################################\n");
332 debug_printf("Shader %u below\n", shader->id);
333 tgsi_dump(shader->tokens, 0);
334 if (SVGA_DEBUG & DEBUG_TGSI) {
335 debug_printf("Shader %u compiled below\n", shader->id);
336 svga_shader_dump(variant->tokens, variant->nr_tokens, FALSE);
337 }
338 debug_printf("#####################################\n");
339 }
340
341 return variant;
342
343 fail:
344 FREE(variant);
345 FREE(emit.buf);
346 return NULL;
347 }
348
349
350 struct svga_shader_variant *
351 svga_translate_fragment_program(const struct svga_fragment_shader *fs,
352 const struct svga_fs_compile_key *fkey)
353 {
354 struct svga_compile_key key;
355
356 memset(&key, 0, sizeof(key));
357
358 memcpy(&key.fkey, fkey, sizeof *fkey);
359
360 memcpy(key.generic_remap_table, fs->generic_remap_table,
361 sizeof(fs->generic_remap_table));
362
363 return svga_tgsi_translate(&fs->base, &key, PIPE_SHADER_FRAGMENT);
364 }
365
366
367 struct svga_shader_variant *
368 svga_translate_vertex_program(const struct svga_vertex_shader *vs,
369 const struct svga_vs_compile_key *vkey)
370 {
371 struct svga_compile_key key;
372
373 memset(&key, 0, sizeof(key));
374
375 memcpy(&key.vkey, vkey, sizeof *vkey);
376
377 /* Note: we could alternately store the remap table in the vkey but
378 * that would make it larger. We just regenerate it here instead.
379 */
380 svga_remap_generics(vkey->fs_generic_inputs, key.generic_remap_table);
381
382 return svga_tgsi_translate(&vs->base, &key, PIPE_SHADER_VERTEX);
383 }
384
385
386 void
387 svga_destroy_shader_variant(struct svga_shader_variant *variant)
388 {
389 FREE((unsigned *) variant->tokens);
390 FREE(variant);
391 }