svga: Re-add shader dumping.
[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_memory.h"
34
35 #include "svgadump/svga_shader_dump.h"
36
37 #include "svga_context.h"
38 #include "svga_tgsi.h"
39 #include "svga_tgsi_emit.h"
40 #include "svga_debug.h"
41
42 #include "svga_hw_reg.h"
43 #include "svga3d_shaderdefs.h"
44
45
46 /* Sinkhole used only in error conditions.
47 */
48 static char err_buf[128];
49
50 #if 0
51 static void svga_destroy_shader_emitter( struct svga_shader_emitter *emit )
52 {
53 if (emit->buf != err_buf)
54 FREE(emit->buf);
55 }
56 #endif
57
58
59 static boolean svga_shader_expand( struct svga_shader_emitter *emit )
60 {
61 char *new_buf;
62 unsigned newsize = emit->size * 2;
63
64 if(emit->buf != err_buf)
65 new_buf = REALLOC(emit->buf, emit->size, newsize);
66 else
67 new_buf = NULL;
68
69 if (new_buf == NULL) {
70 emit->ptr = err_buf;
71 emit->buf = err_buf;
72 emit->size = sizeof(err_buf);
73 return FALSE;
74 }
75
76 emit->size = newsize;
77 emit->ptr = new_buf + (emit->ptr - emit->buf);
78 emit->buf = new_buf;
79 return TRUE;
80 }
81
82 static INLINE boolean reserve( struct svga_shader_emitter *emit,
83 unsigned nr_dwords )
84 {
85 if (emit->ptr - emit->buf + nr_dwords * sizeof(unsigned) >= emit->size) {
86 if (!svga_shader_expand( emit ))
87 return FALSE;
88 }
89
90 return TRUE;
91 }
92
93 boolean svga_shader_emit_dword( struct svga_shader_emitter *emit,
94 unsigned dword )
95 {
96 if (!reserve(emit, 1))
97 return FALSE;
98
99 *(unsigned *)emit->ptr = dword;
100 emit->ptr += sizeof dword;
101 return TRUE;
102 }
103
104 boolean svga_shader_emit_dwords( struct svga_shader_emitter *emit,
105 const unsigned *dwords,
106 unsigned nr )
107 {
108 if (!reserve(emit, nr))
109 return FALSE;
110
111 memcpy( emit->ptr, dwords, nr * sizeof *dwords );
112 emit->ptr += nr * sizeof *dwords;
113 return TRUE;
114 }
115
116 boolean svga_shader_emit_opcode( struct svga_shader_emitter *emit,
117 unsigned opcode )
118 {
119 SVGA3dShaderInstToken *here;
120
121 if (!reserve(emit, 1))
122 return FALSE;
123
124 here = (SVGA3dShaderInstToken *)emit->ptr;
125 here->value = opcode;
126
127 if (emit->insn_offset) {
128 SVGA3dShaderInstToken *prev = (SVGA3dShaderInstToken *)(emit->buf +
129 emit->insn_offset);
130 prev->size = (here - prev) - 1;
131 }
132
133 emit->insn_offset = emit->ptr - emit->buf;
134 emit->ptr += sizeof(unsigned);
135 return TRUE;
136 }
137
138 #define SVGA3D_PS_2X (SVGA3D_PS_20 | 1)
139 #define SVGA3D_VS_2X (SVGA3D_VS_20 | 1)
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 = emit->use_sm30 ? SVGA3D_PS_30 : SVGA3D_PS_2X;
150 break;
151 case PIPE_SHADER_VERTEX:
152 header.value = emit->use_sm30 ? SVGA3D_VS_30 : SVGA3D_VS_2X;
153 break;
154 }
155
156 return svga_shader_emit_dword( emit, header.value );
157 }
158
159
160
161
162
163 /* Parse TGSI shader and translate to SVGA/DX9 serialized
164 * representation.
165 *
166 * In this function SVGA shader is emitted to an in-memory buffer that
167 * can be dynamically grown. Once we've finished and know how large
168 * it is, it will be copied to a hardware buffer for upload.
169 */
170 static struct svga_shader_result *
171 svga_tgsi_translate( const struct svga_shader *shader,
172 union svga_compile_key key,
173 unsigned unit )
174 {
175 struct svga_shader_result *result = NULL;
176 struct svga_shader_emitter emit;
177 int ret = 0;
178
179 memset(&emit, 0, sizeof(emit));
180
181 emit.use_sm30 = shader->use_sm30;
182 emit.size = 1024;
183 emit.buf = MALLOC(emit.size);
184 if (emit.buf == NULL) {
185 ret = PIPE_ERROR_OUT_OF_MEMORY;
186 goto fail;
187 }
188
189 emit.ptr = emit.buf;
190 emit.unit = unit;
191 emit.key = key;
192
193 tgsi_scan_shader( shader->tokens, &emit.info);
194
195 emit.imm_start = emit.info.file_max[TGSI_FILE_CONSTANT] + 1;
196
197 if (unit == PIPE_SHADER_FRAGMENT)
198 emit.imm_start += key.fkey.num_unnormalized_coords;
199
200 if (unit == PIPE_SHADER_VERTEX) {
201 emit.imm_start += key.vkey.need_prescale ? 2 : 0;
202 emit.imm_start += key.vkey.num_zero_stride_vertex_elements;
203 }
204
205 emit.nr_hw_const = (emit.imm_start + emit.info.file_max[TGSI_FILE_IMMEDIATE] + 1);
206
207 emit.nr_hw_temp = emit.info.file_max[TGSI_FILE_TEMPORARY] + 1;
208 emit.in_main_func = TRUE;
209
210 if (!svga_shader_emit_header( &emit ))
211 goto fail;
212
213 if (!svga_shader_emit_instructions( &emit, shader->tokens ))
214 goto fail;
215
216 result = CALLOC_STRUCT(svga_shader_result);
217 if (result == NULL)
218 goto fail;
219
220 result->shader = shader;
221 result->tokens = (const unsigned *)emit.buf;
222 result->nr_tokens = (emit.ptr - emit.buf) / sizeof(unsigned);
223 memcpy(&result->key, &key, sizeof key);
224
225 if (SVGA_DEBUG & DEBUG_TGSI)
226 {
227 debug_printf( "#####################################\n" );
228 debug_printf( "Shader %u below\n", shader->id );
229 tgsi_dump( shader->tokens, 0 );
230 if (SVGA_DEBUG & DEBUG_TGSI) {
231 debug_printf( "Shader %u compiled below\n", shader->id );
232 svga_shader_dump( result->tokens,
233 result->nr_tokens ,
234 FALSE );
235 }
236 debug_printf( "#####################################\n" );
237 }
238
239 return result;
240
241 fail:
242 FREE(result);
243 FREE(emit.buf);
244 return NULL;
245 }
246
247
248
249
250 struct svga_shader_result *
251 svga_translate_fragment_program( const struct svga_fragment_shader *fs,
252 const struct svga_fs_compile_key *fkey )
253 {
254 union svga_compile_key key;
255 memcpy(&key.fkey, fkey, sizeof *fkey);
256
257 return svga_tgsi_translate( &fs->base,
258 key,
259 PIPE_SHADER_FRAGMENT );
260 }
261
262 struct svga_shader_result *
263 svga_translate_vertex_program( const struct svga_vertex_shader *vs,
264 const struct svga_vs_compile_key *vkey )
265 {
266 union svga_compile_key key;
267 memcpy(&key.vkey, vkey, sizeof *vkey);
268
269 return svga_tgsi_translate( &vs->base,
270 key,
271 PIPE_SHADER_VERTEX );
272 }
273
274
275 void svga_destroy_shader_result( struct svga_shader_result *result )
276 {
277 FREE((unsigned *)result->tokens);
278 FREE(result);
279 }
280