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