Merge remote-tracking branch 'public/master' into vulkan
[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
54 static boolean
55 svga_shader_expand(struct svga_shader_emitter *emit)
56 {
57 char *new_buf;
58 unsigned newsize = emit->size * 2;
59
60 if (emit->buf != err_buf)
61 new_buf = REALLOC(emit->buf, emit->size, newsize);
62 else
63 new_buf = NULL;
64
65 if (!new_buf) {
66 emit->ptr = err_buf;
67 emit->buf = err_buf;
68 emit->size = sizeof(err_buf);
69 return FALSE;
70 }
71
72 emit->size = newsize;
73 emit->ptr = new_buf + (emit->ptr - emit->buf);
74 emit->buf = new_buf;
75 return TRUE;
76 }
77
78
79 static inline boolean
80 reserve(struct svga_shader_emitter *emit, unsigned nr_dwords)
81 {
82 if (emit->ptr - emit->buf + nr_dwords * sizeof(unsigned) >= emit->size) {
83 if (!svga_shader_expand(emit)) {
84 return FALSE;
85 }
86 }
87
88 return TRUE;
89 }
90
91
92 boolean
93 svga_shader_emit_dword(struct svga_shader_emitter * emit, unsigned dword)
94 {
95 if (!reserve(emit, 1))
96 return FALSE;
97
98 *(unsigned *) emit->ptr = dword;
99 emit->ptr += sizeof dword;
100 return TRUE;
101 }
102
103
104 boolean
105 svga_shader_emit_dwords(struct svga_shader_emitter * emit,
106 const unsigned *dwords, 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
117 boolean
118 svga_shader_emit_opcode(struct svga_shader_emitter * emit, unsigned opcode)
119 {
120 SVGA3dShaderInstToken *here;
121
122 if (!reserve(emit, 1))
123 return FALSE;
124
125 here = (SVGA3dShaderInstToken *) emit->ptr;
126 here->value = opcode;
127
128 if (emit->insn_offset) {
129 SVGA3dShaderInstToken *prev =
130 (SVGA3dShaderInstToken *) (emit->buf + emit->insn_offset);
131 prev->size = (here - prev) - 1;
132 }
133
134 emit->insn_offset = emit->ptr - emit->buf;
135 emit->ptr += sizeof(unsigned);
136 return TRUE;
137 }
138
139
140 static boolean
141 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 * Parse TGSI shader and translate to SVGA/DX9 serialized
162 * representation.
163 *
164 * In this function SVGA shader is emitted to an in-memory buffer that
165 * can be dynamically grown. Once we've finished and know how large
166 * it is, it will be copied to a hardware buffer for upload.
167 */
168 struct svga_shader_variant *
169 svga_tgsi_vgpu9_translate(struct svga_context *svga,
170 const struct svga_shader *shader,
171 const struct svga_compile_key *key, unsigned unit)
172 {
173 struct svga_shader_variant *variant = NULL;
174 struct svga_shader_emitter emit;
175
176 memset(&emit, 0, sizeof(emit));
177
178 emit.size = 1024;
179 emit.buf = MALLOC(emit.size);
180 if (emit.buf == NULL) {
181 goto fail;
182 }
183
184 emit.ptr = emit.buf;
185 emit.unit = unit;
186 emit.key = *key;
187
188 tgsi_scan_shader(shader->tokens, &emit.info);
189
190 emit.imm_start = emit.info.file_max[TGSI_FILE_CONSTANT] + 1;
191
192 if (unit == PIPE_SHADER_FRAGMENT)
193 emit.imm_start += key->num_unnormalized_coords;
194
195 if (unit == PIPE_SHADER_VERTEX) {
196 emit.imm_start += key->vs.need_prescale ? 2 : 0;
197 }
198
199 emit.nr_hw_float_const =
200 (emit.imm_start + emit.info.file_max[TGSI_FILE_IMMEDIATE] + 1);
201
202 emit.nr_hw_temp = emit.info.file_max[TGSI_FILE_TEMPORARY] + 1;
203
204 if (emit.nr_hw_temp >= SVGA3D_TEMPREG_MAX) {
205 debug_printf("svga: too many temporary registers (%u)\n",
206 emit.nr_hw_temp);
207 goto fail;
208 }
209
210 emit.in_main_func = TRUE;
211
212 if (!svga_shader_emit_header(&emit)) {
213 debug_printf("svga: emit header failed\n");
214 goto fail;
215 }
216
217 if (!svga_shader_emit_instructions(&emit, shader->tokens)) {
218 debug_printf("svga: emit instructions failed\n");
219 goto fail;
220 }
221
222 variant = svga_new_shader_variant(svga);
223 if (!variant)
224 goto fail;
225
226 variant->shader = shader;
227 variant->tokens = (const unsigned *) emit.buf;
228 variant->nr_tokens = (emit.ptr - emit.buf) / sizeof(unsigned);
229 memcpy(&variant->key, key, sizeof(*key));
230 variant->id = UTIL_BITMASK_INVALID_INDEX;
231
232 variant->pstipple_sampler_unit = emit.pstipple_sampler_unit;
233
234 /* If there was exactly one write to a fragment shader output register
235 * and it came from a constant buffer, we know all fragments will have
236 * the same color (except for blending).
237 */
238 variant->constant_color_output =
239 emit.constant_color_output && emit.num_output_writes == 1;
240
241 #if 0
242 if (!svga_shader_verify(variant->tokens, variant->nr_tokens) ||
243 SVGA_DEBUG & DEBUG_TGSI) {
244 debug_printf("#####################################\n");
245 debug_printf("Shader %u below\n", shader->id);
246 tgsi_dump(shader->tokens, 0);
247 if (SVGA_DEBUG & DEBUG_TGSI) {
248 debug_printf("Shader %u compiled below\n", shader->id);
249 svga_shader_dump(variant->tokens, variant->nr_tokens, FALSE);
250 }
251 debug_printf("#####################################\n");
252 }
253 #endif
254
255 return variant;
256
257 fail:
258 FREE(variant);
259 if (emit.buf != err_buf)
260 FREE(emit.buf);
261 return NULL;
262 }