sync with tizen_2.2
[sdk/emulator/qemu.git] / gl / mesa / 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 svga_destroy_shader_emitter( struct svga_shader_emitter *emit )
54 {
55    if (emit->buf != err_buf)
56       FREE(emit->buf);
57 }
58 #endif
59
60
61 static boolean svga_shader_expand( struct svga_shader_emitter *emit )
62 {
63    char *new_buf;
64    unsigned newsize = emit->size * 2;
65
66    if(emit->buf != err_buf)
67       new_buf = REALLOC(emit->buf, emit->size, newsize);
68    else
69       new_buf = NULL;
70
71    if (new_buf == NULL) {
72       emit->ptr = err_buf;
73       emit->buf = err_buf;
74       emit->size = sizeof(err_buf);
75       return FALSE;
76    }
77
78    emit->size = newsize;
79    emit->ptr = new_buf + (emit->ptr - emit->buf);
80    emit->buf = new_buf;
81    return TRUE;
82 }   
83
84 static INLINE boolean reserve(  struct svga_shader_emitter *emit,
85                                 unsigned nr_dwords )
86 {
87    if (emit->ptr - emit->buf + nr_dwords * sizeof(unsigned) >= emit->size) {
88       if (!svga_shader_expand( emit ))
89          return FALSE;
90    }
91
92    return TRUE;
93 }
94
95 boolean svga_shader_emit_dword( struct svga_shader_emitter *emit,
96                                 unsigned dword )
97 {
98    if (!reserve(emit, 1))
99       return FALSE;
100
101    *(unsigned *)emit->ptr = dword;
102    emit->ptr += sizeof dword;
103    return TRUE;
104 }
105
106 boolean svga_shader_emit_dwords( struct svga_shader_emitter *emit,
107                                  const unsigned *dwords,
108                                  unsigned nr )
109 {
110    if (!reserve(emit, nr))
111       return FALSE;
112
113    memcpy( emit->ptr, dwords, nr * sizeof *dwords );
114    emit->ptr += nr * sizeof *dwords;
115    return TRUE;
116 }
117
118 boolean svga_shader_emit_opcode( struct svga_shader_emitter *emit,
119                                  unsigned opcode )
120 {
121    SVGA3dShaderInstToken *here;
122
123    if (!reserve(emit, 1))
124       return FALSE;
125
126    here = (SVGA3dShaderInstToken *)emit->ptr;
127    here->value = opcode;
128
129    if (emit->insn_offset) {
130       SVGA3dShaderInstToken *prev = (SVGA3dShaderInstToken *)(emit->buf + 
131                                                               emit->insn_offset);
132       prev->size = (here - prev) - 1;
133    }
134    
135    emit->insn_offset = emit->ptr - emit->buf;
136    emit->ptr += sizeof(unsigned);
137    return TRUE;
138 }
139
140
141 static boolean svga_shader_emit_header( struct svga_shader_emitter *emit )
142 {
143    SVGA3dShaderVersion header;
144
145    memset( &header, 0, sizeof header );
146
147    switch (emit->unit) {
148    case PIPE_SHADER_FRAGMENT:
149       header.value = SVGA3D_PS_30;
150       break;
151    case PIPE_SHADER_VERTEX:
152       header.value = SVGA3D_VS_30;
153       break;
154    }
155  
156    return svga_shader_emit_dword( emit, header.value );
157 }
158
159
160 /**
161  * Use the shader info to generate a bitmask indicating which generic
162  * inputs are used by the shader.  A set bit indicates that GENERIC[i]
163  * is used.
164  */
165 unsigned
166 svga_get_generic_inputs_mask(const struct tgsi_shader_info *info)
167 {
168    unsigned i, mask = 0x0;
169
170    for (i = 0; i < info->num_inputs; i++) {
171       if (info->input_semantic_name[i] == TGSI_SEMANTIC_GENERIC) {
172          unsigned j = info->input_semantic_index[i];
173          assert(j < sizeof(mask) * 8);
174          mask |= 1 << j;
175       }
176    }
177
178    return mask;
179 }
180
181
182 /**
183  * Given a mask of used generic variables (as returned by the above functions)
184  * fill in a table which maps those indexes to small integers.
185  * This table is used by the remap_generic_index() function in
186  * svga_tgsi_decl_sm30.c
187  * Example: if generics_mask = binary(1010) it means that GENERIC[1] and
188  * GENERIC[3] are used.  The remap_table will contain:
189  *   table[1] = 0;
190  *   table[3] = 1;
191  * The remaining table entries will be filled in with the next unused
192  * generic index (in this example, 2).
193  */
194 void
195 svga_remap_generics(unsigned generics_mask,
196                     int8_t remap_table[MAX_GENERIC_VARYING])
197 {
198    /* Note texcoord[0] is reserved so start at 1 */
199    unsigned count = 1, i;
200
201    for (i = 0; i < MAX_GENERIC_VARYING; i++) {
202       remap_table[i] = -1;
203    }
204
205    /* for each bit set in generic_mask */
206    while (generics_mask) {
207       unsigned index = ffs(generics_mask) - 1;
208       remap_table[index] = count++;
209       generics_mask &= ~(1 << index);
210    }
211 }
212
213
214 /**
215  * Use the generic remap table to map a TGSI generic varying variable
216  * index to a small integer.  If the remapping table doesn't have a
217  * valid value for the given index (the table entry is -1) it means
218  * the fragment shader doesn't use that VS output.  Just allocate
219  * the next free value in that case.  Alternately, we could cull
220  * VS instructions that write to register, or replace the register
221  * with a dummy temp register.
222  * XXX TODO: we should do one of the later as it would save precious
223  * texcoord registers.
224  */
225 int
226 svga_remap_generic_index(int8_t remap_table[MAX_GENERIC_VARYING],
227                          int generic_index)
228 {
229    assert(generic_index < MAX_GENERIC_VARYING);
230
231    if (generic_index >= MAX_GENERIC_VARYING) {
232       /* just don't return a random/garbage value */
233       generic_index = MAX_GENERIC_VARYING - 1;
234    }
235
236    if (remap_table[generic_index] == -1) {
237       /* This is a VS output that has no matching PS input.  Find a
238        * free index.
239        */
240       int i, max = 0;
241       for (i = 0; i < MAX_GENERIC_VARYING; i++) {
242          max = MAX2(max, remap_table[i]);
243       }
244       remap_table[generic_index] = max + 1;
245    }
246
247    return remap_table[generic_index];
248 }
249
250
251 /* Parse TGSI shader and translate to SVGA/DX9 serialized
252  * representation.  
253  *
254  * In this function SVGA shader is emitted to an in-memory buffer that
255  * can be dynamically grown.  Once we've finished and know how large
256  * it is, it will be copied to a hardware buffer for upload.
257  */
258 static struct svga_shader_result *
259 svga_tgsi_translate( const struct svga_shader *shader,
260                      struct svga_compile_key key,
261                      unsigned unit )
262 {
263    struct svga_shader_result *result = NULL;
264    struct svga_shader_emitter emit;
265
266    memset(&emit, 0, sizeof(emit));
267
268    emit.size = 1024;
269    emit.buf = MALLOC(emit.size);
270    if (emit.buf == NULL) {
271       goto fail;
272    }
273
274    emit.ptr = emit.buf;
275    emit.unit = unit;
276    emit.key = key;
277
278    tgsi_scan_shader( shader->tokens, &emit.info);
279
280    emit.imm_start = emit.info.file_max[TGSI_FILE_CONSTANT] + 1;
281    
282    if (unit == PIPE_SHADER_FRAGMENT)
283       emit.imm_start += key.fkey.num_unnormalized_coords;
284
285    if (unit == PIPE_SHADER_VERTEX) {
286       emit.imm_start += key.vkey.need_prescale ? 2 : 0;
287       emit.imm_start += key.vkey.num_zero_stride_vertex_elements;
288    }
289
290    emit.nr_hw_float_const = (emit.imm_start + emit.info.file_max[TGSI_FILE_IMMEDIATE] + 1);
291
292    emit.nr_hw_temp = emit.info.file_max[TGSI_FILE_TEMPORARY] + 1;
293    emit.in_main_func = TRUE;
294
295    if (!svga_shader_emit_header( &emit ))
296       goto fail;
297
298    if (!svga_shader_emit_instructions( &emit, shader->tokens ))
299       goto fail;
300    
301    result = CALLOC_STRUCT(svga_shader_result);
302    if (result == NULL)
303       goto fail;
304
305    result->shader = shader;
306    result->tokens = (const unsigned *)emit.buf;
307    result->nr_tokens = (emit.ptr - emit.buf) / sizeof(unsigned);
308    memcpy(&result->key, &key, sizeof key);
309    result->id = UTIL_BITMASK_INVALID_INDEX;
310
311    if (SVGA_DEBUG & DEBUG_TGSI) 
312    {
313       debug_printf( "#####################################\n" );
314       debug_printf( "Shader %u below\n", shader->id );
315       tgsi_dump( shader->tokens, 0 );
316       if (SVGA_DEBUG & DEBUG_TGSI) {
317          debug_printf( "Shader %u compiled below\n", shader->id );
318          svga_shader_dump( result->tokens,
319                            result->nr_tokens ,
320                            FALSE );
321       }
322       debug_printf( "#####################################\n" );
323    }
324
325    return result;
326
327 fail:
328    FREE(result);
329    FREE(emit.buf);
330    return NULL;
331 }
332
333
334
335
336 struct svga_shader_result *
337 svga_translate_fragment_program( const struct svga_fragment_shader *fs,
338                                  const struct svga_fs_compile_key *fkey )
339 {
340    struct svga_compile_key key;
341
342    memcpy(&key.fkey, fkey, sizeof *fkey);
343
344    memcpy(key.generic_remap_table, fs->generic_remap_table,
345           sizeof(fs->generic_remap_table));
346
347    return svga_tgsi_translate( &fs->base, 
348                                key,
349                                PIPE_SHADER_FRAGMENT );
350 }
351
352 struct svga_shader_result *
353 svga_translate_vertex_program( const struct svga_vertex_shader *vs,
354                                const struct svga_vs_compile_key *vkey )
355 {
356    struct svga_compile_key key;
357
358    memcpy(&key.vkey, vkey, sizeof *vkey);
359
360    /* Note: we could alternately store the remap table in the vkey but
361     * that would make it larger.  We just regenerate it here instead.
362     */
363    svga_remap_generics(vkey->fs_generic_inputs, key.generic_remap_table);
364
365    return svga_tgsi_translate( &vs->base, 
366                                key,
367                                PIPE_SHADER_VERTEX );
368 }
369
370
371 void svga_destroy_shader_result( struct svga_shader_result *result )
372 {
373    FREE((unsigned *)result->tokens);
374    FREE(result);
375 }
376