svga: Add GL4.1(compatibility profile) support in svga driver
[mesa.git] / src / gallium / drivers / svga / svga_state_vs.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 #include "util/u_inlines.h"
27 #include "pipe/p_defines.h"
28 #include "util/u_math.h"
29 #include "util/u_memory.h"
30 #include "util/u_bitmask.h"
31 #include "translate/translate.h"
32 #include "tgsi/tgsi_ureg.h"
33
34 #include "svga_context.h"
35 #include "svga_state.h"
36 #include "svga_cmd.h"
37 #include "svga_shader.h"
38 #include "svga_tgsi.h"
39
40 #include "svga_hw_reg.h"
41
42
43 /**
44 * If we fail to compile a vertex shader we'll use a dummy/fallback shader
45 * that simply emits a (0,0,0,1) vertex position.
46 */
47 static const struct tgsi_token *
48 get_dummy_vertex_shader(void)
49 {
50 static const float zero[4] = { 0.0, 0.0, 0.0, 1.0 };
51 struct ureg_program *ureg;
52 const struct tgsi_token *tokens;
53 struct ureg_src src;
54 struct ureg_dst dst;
55
56 ureg = ureg_create(PIPE_SHADER_VERTEX);
57 if (!ureg)
58 return NULL;
59
60 dst = ureg_DECL_output(ureg, TGSI_SEMANTIC_POSITION, 0);
61 src = ureg_DECL_immediate(ureg, zero, 4);
62 ureg_MOV(ureg, dst, src);
63 ureg_END(ureg);
64
65 tokens = ureg_get_tokens(ureg, NULL);
66
67 ureg_destroy(ureg);
68
69 return tokens;
70 }
71
72
73 static struct svga_shader_variant *
74 translate_vertex_program(struct svga_context *svga,
75 const struct svga_vertex_shader *vs,
76 const struct svga_compile_key *key)
77 {
78 if (svga_have_vgpu10(svga)) {
79 return svga_tgsi_vgpu10_translate(svga, &vs->base, key,
80 PIPE_SHADER_VERTEX);
81 }
82 else {
83 return svga_tgsi_vgpu9_translate(svga, &vs->base, key,
84 PIPE_SHADER_VERTEX);
85 }
86 }
87
88
89 /**
90 * Replace the given shader's instruction with a simple / dummy shader.
91 * We use this when normal shader translation fails.
92 */
93 static struct svga_shader_variant *
94 get_compiled_dummy_vertex_shader(struct svga_context *svga,
95 struct svga_vertex_shader *vs,
96 const struct svga_compile_key *key)
97 {
98 const struct tgsi_token *dummy = get_dummy_vertex_shader();
99 struct svga_shader_variant *variant;
100
101 if (!dummy) {
102 return NULL;
103 }
104
105 FREE((void *) vs->base.tokens);
106 vs->base.tokens = dummy;
107
108 tgsi_scan_shader(vs->base.tokens, &vs->base.info);
109 vs->generic_outputs = svga_get_generic_outputs_mask(&vs->base.info);
110
111 variant = translate_vertex_program(svga, vs, key);
112 return variant;
113 }
114
115
116 /**
117 * Translate TGSI shader into an svga shader variant.
118 */
119 static enum pipe_error
120 compile_vs(struct svga_context *svga,
121 struct svga_vertex_shader *vs,
122 const struct svga_compile_key *key,
123 struct svga_shader_variant **out_variant)
124 {
125 struct svga_shader_variant *variant;
126 enum pipe_error ret = PIPE_ERROR;
127
128 variant = translate_vertex_program(svga, vs, key);
129 if (variant == NULL) {
130 debug_printf("Failed to compile vertex shader,"
131 " using dummy shader instead.\n");
132 variant = get_compiled_dummy_vertex_shader(svga, vs, key);
133 }
134 else if (svga_shader_too_large(svga, variant)) {
135 /* too big, use dummy shader */
136 debug_printf("Shader too large (%u bytes),"
137 " using dummy shader instead.\n",
138 (unsigned) (variant->nr_tokens
139 * sizeof(variant->tokens[0])));
140 /* Free the too-large variant */
141 svga_destroy_shader_variant(svga, variant);
142 /* Use simple pass-through shader instead */
143 variant = get_compiled_dummy_vertex_shader(svga, vs, key);
144 }
145
146 if (!variant) {
147 return PIPE_ERROR;
148 }
149
150 ret = svga_define_shader(svga, variant);
151 if (ret != PIPE_OK) {
152 svga_destroy_shader_variant(svga, variant);
153 return ret;
154 }
155
156 *out_variant = variant;
157
158 return PIPE_OK;
159 }
160
161
162 /* SVGA_NEW_PRESCALE, SVGA_NEW_RAST, SVGA_NEW_FS
163 */
164 static void
165 make_vs_key(struct svga_context *svga, struct svga_compile_key *key)
166 {
167 struct svga_vertex_shader *vs = svga->curr.vs;
168
169 memset(key, 0, sizeof *key);
170
171 if (svga->state.sw.need_swtnl && svga_have_vgpu10(svga)) {
172 /* Set both of these flags, to match compile_passthrough_vs() */
173 key->vs.passthrough = 1;
174 key->vs.undo_viewport = 1;
175 return;
176 }
177
178 /* SVGA_NEW_PRESCALE */
179 key->vs.need_prescale = svga->state.hw_clear.prescale[0].enabled &&
180 (svga->curr.tes == NULL) &&
181 (svga->curr.gs == NULL);
182
183 /* SVGA_NEW_RAST */
184 key->vs.allow_psiz = svga->curr.rast->templ.point_size_per_vertex;
185
186 /* SVGA_NEW_FS */
187 key->vs.fs_generic_inputs = svga->curr.fs->generic_inputs;
188
189 svga_remap_generics(key->vs.fs_generic_inputs, key->generic_remap_table);
190
191 /* SVGA_NEW_VELEMENT */
192 key->vs.adjust_attrib_range = svga->curr.velems->adjust_attrib_range;
193 key->vs.adjust_attrib_w_1 = svga->curr.velems->adjust_attrib_w_1;
194 key->vs.attrib_is_pure_int = svga->curr.velems->attrib_is_pure_int;
195 key->vs.adjust_attrib_itof = svga->curr.velems->adjust_attrib_itof;
196 key->vs.adjust_attrib_utof = svga->curr.velems->adjust_attrib_utof;
197 key->vs.attrib_is_bgra = svga->curr.velems->attrib_is_bgra;
198 key->vs.attrib_puint_to_snorm = svga->curr.velems->attrib_puint_to_snorm;
199 key->vs.attrib_puint_to_uscaled = svga->curr.velems->attrib_puint_to_uscaled;
200 key->vs.attrib_puint_to_sscaled = svga->curr.velems->attrib_puint_to_sscaled;
201
202 /* SVGA_NEW_TEXTURE_BINDING | SVGA_NEW_SAMPLER */
203 svga_init_shader_key_common(svga, PIPE_SHADER_VERTEX, &vs->base, key);
204
205 /* SVGA_NEW_RAST */
206 key->clip_plane_enable = svga->curr.rast->templ.clip_plane_enable;
207
208 /* Determine if this shader is the last shader in the vertex
209 * processing stage.
210 */
211 key->last_vertex_stage = !(svga->curr.gs ||
212 svga->curr.tcs || svga->curr.tes);
213 }
214
215
216 /**
217 * svga_reemit_vs_bindings - Reemit the vertex shader bindings
218 */
219 enum pipe_error
220 svga_reemit_vs_bindings(struct svga_context *svga)
221 {
222 enum pipe_error ret;
223 struct svga_winsys_gb_shader *gbshader = NULL;
224 SVGA3dShaderId shaderId = SVGA3D_INVALID_ID;
225
226 assert(svga->rebind.flags.vs);
227 assert(svga_have_gb_objects(svga));
228
229 if (svga->state.hw_draw.vs) {
230 gbshader = svga->state.hw_draw.vs->gb_shader;
231 shaderId = svga->state.hw_draw.vs->id;
232 }
233
234 if (!svga_need_to_rebind_resources(svga)) {
235 ret = svga->swc->resource_rebind(svga->swc, NULL, gbshader,
236 SVGA_RELOC_READ);
237 }
238 else {
239 if (svga_have_vgpu10(svga))
240 ret = SVGA3D_vgpu10_SetShader(svga->swc, SVGA3D_SHADERTYPE_VS,
241 gbshader, shaderId);
242 else
243 ret = SVGA3D_SetGBShader(svga->swc, SVGA3D_SHADERTYPE_VS, gbshader);
244 }
245
246 if (ret != PIPE_OK)
247 return ret;
248
249 svga->rebind.flags.vs = FALSE;
250 return PIPE_OK;
251 }
252
253
254 /**
255 * The current vertex shader is already executed by the 'draw'
256 * module, so we just need to generate a simple vertex shader
257 * to pass through all those VS outputs that will
258 * be consumed by the fragment shader.
259 * Used when we employ the 'draw' module.
260 */
261 static enum pipe_error
262 compile_passthrough_vs(struct svga_context *svga,
263 struct svga_vertex_shader *vs,
264 struct svga_fragment_shader *fs,
265 struct svga_shader_variant **out_variant)
266 {
267 struct svga_shader_variant *variant = NULL;
268 unsigned num_inputs;
269 unsigned i;
270 unsigned num_elements;
271 struct svga_vertex_shader new_vs;
272 struct ureg_src src[PIPE_MAX_SHADER_INPUTS];
273 struct ureg_dst dst[PIPE_MAX_SHADER_OUTPUTS];
274 struct ureg_program *ureg;
275 struct svga_compile_key key;
276 enum pipe_error ret;
277
278 assert(svga_have_vgpu10(svga));
279 assert(fs);
280
281 num_inputs = fs->base.info.num_inputs;
282
283 ureg = ureg_create(PIPE_SHADER_VERTEX);
284 if (!ureg)
285 return PIPE_ERROR_OUT_OF_MEMORY;
286
287 /* draw will always add position */
288 dst[0] = ureg_DECL_output(ureg, TGSI_SEMANTIC_POSITION, 0);
289 src[0] = ureg_DECL_vs_input(ureg, 0);
290 num_elements = 1;
291
292 /**
293 * swtnl backend redefines the input layout based on the
294 * fragment shader's inputs. So we only need to passthrough
295 * those inputs that will be consumed by the fragment shader.
296 * Note: DX10 requires the number of vertex elements
297 * specified in the input layout to be no less than the
298 * number of inputs to the vertex shader.
299 */
300 for (i = 0; i < num_inputs; i++) {
301 switch (fs->base.info.input_semantic_name[i]) {
302 case TGSI_SEMANTIC_COLOR:
303 case TGSI_SEMANTIC_GENERIC:
304 case TGSI_SEMANTIC_FOG:
305 dst[num_elements] = ureg_DECL_output(ureg,
306 fs->base.info.input_semantic_name[i],
307 fs->base.info.input_semantic_index[i]);
308 src[num_elements] = ureg_DECL_vs_input(ureg, num_elements);
309 num_elements++;
310 break;
311 default:
312 break;
313 }
314 }
315
316 for (i = 0; i < num_elements; i++) {
317 ureg_MOV(ureg, dst[i], src[i]);
318 }
319
320 ureg_END(ureg);
321
322 memset(&new_vs, 0, sizeof(new_vs));
323 new_vs.base.tokens = ureg_get_tokens(ureg, NULL);
324 tgsi_scan_shader(new_vs.base.tokens, &new_vs.base.info);
325
326 memset(&key, 0, sizeof(key));
327 key.vs.undo_viewport = 1;
328
329 ret = compile_vs(svga, &new_vs, &key, &variant);
330 if (ret != PIPE_OK)
331 return ret;
332
333 ureg_free_tokens(new_vs.base.tokens);
334 ureg_destroy(ureg);
335
336 /* Overwrite the variant key to indicate it's a pass-through VS */
337 memset(&variant->key, 0, sizeof(variant->key));
338 variant->key.vs.passthrough = 1;
339 variant->key.vs.undo_viewport = 1;
340
341 *out_variant = variant;
342
343 return PIPE_OK;
344 }
345
346
347 static enum pipe_error
348 emit_hw_vs(struct svga_context *svga, uint64_t dirty)
349 {
350 struct svga_shader_variant *variant;
351 struct svga_vertex_shader *vs = svga->curr.vs;
352 struct svga_fragment_shader *fs = svga->curr.fs;
353 enum pipe_error ret = PIPE_OK;
354 struct svga_compile_key key;
355
356 SVGA_STATS_TIME_PUSH(svga_sws(svga), SVGA_STATS_TIME_EMITVS);
357
358 /* If there is an active geometry shader, and it has stream output
359 * defined, then we will skip the stream output from the vertex shader
360 */
361 if (!svga_have_gs_streamout(svga)) {
362 /* No GS stream out */
363 if (svga_have_vs_streamout(svga)) {
364 /* Set VS stream out */
365 ret = svga_set_stream_output(svga, vs->base.stream_output);
366 }
367 else {
368 /* turn off stream out */
369 ret = svga_set_stream_output(svga, NULL);
370 }
371 if (ret != PIPE_OK) {
372 goto done;
373 }
374 }
375
376 /* SVGA_NEW_NEED_SWTNL */
377 if (svga->state.sw.need_swtnl && !svga_have_vgpu10(svga)) {
378 /* No vertex shader is needed */
379 variant = NULL;
380 }
381 else {
382 make_vs_key(svga, &key);
383
384 /* See if we already have a VS variant that matches the key */
385 variant = svga_search_shader_key(&vs->base, &key);
386
387 if (!variant) {
388 /* Create VS variant now */
389 if (key.vs.passthrough) {
390 ret = compile_passthrough_vs(svga, vs, fs, &variant);
391 }
392 else {
393 ret = compile_vs(svga, vs, &key, &variant);
394 }
395 if (ret != PIPE_OK)
396 goto done;
397
398 /* insert the new variant at head of linked list */
399 assert(variant);
400 variant->next = vs->base.variants;
401 vs->base.variants = variant;
402 }
403 }
404
405 if (variant != svga->state.hw_draw.vs) {
406 /* Bind the new variant */
407 if (variant) {
408 ret = svga_set_shader(svga, SVGA3D_SHADERTYPE_VS, variant);
409 if (ret != PIPE_OK)
410 goto done;
411 svga->rebind.flags.vs = FALSE;
412 }
413
414 svga->dirty |= SVGA_NEW_VS_VARIANT;
415 svga->state.hw_draw.vs = variant;
416 }
417
418 done:
419 SVGA_STATS_TIME_POP(svga_sws(svga));
420 return ret;
421 }
422
423 struct svga_tracked_state svga_hw_vs =
424 {
425 "vertex shader (hwtnl)",
426 (SVGA_NEW_VS |
427 SVGA_NEW_FS |
428 SVGA_NEW_TEXTURE_BINDING |
429 SVGA_NEW_SAMPLER |
430 SVGA_NEW_RAST |
431 SVGA_NEW_PRESCALE |
432 SVGA_NEW_VELEMENT |
433 SVGA_NEW_NEED_SWTNL),
434 emit_hw_vs
435 };