svga: update driver for version 10 GPU interface
[mesa.git] / src / gallium / drivers / svga / svga_state_fs.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 "tgsi/tgsi_ureg.h"
32
33 #include "svga_context.h"
34 #include "svga_state.h"
35 #include "svga_cmd.h"
36 #include "svga_shader.h"
37 #include "svga_resource_texture.h"
38 #include "svga_tgsi.h"
39 #include "svga_format.h"
40
41 #include "svga_hw_reg.h"
42
43
44
45 /**
46 * If we fail to compile a fragment shader (because it uses too many
47 * registers, for example) we'll use a dummy/fallback shader that
48 * simply emits a constant color (red for debug, black for release).
49 * We hit this with the Unigine/Heaven demo when Shaders = High.
50 * With black, the demo still looks good.
51 */
52 static const struct tgsi_token *
53 get_dummy_fragment_shader(void)
54 {
55 #ifdef DEBUG
56 static const float color[4] = { 1.0, 0.0, 0.0, 0.0 }; /* red */
57 #else
58 static const float color[4] = { 0.0, 0.0, 0.0, 0.0 }; /* black */
59 #endif
60 struct ureg_program *ureg;
61 const struct tgsi_token *tokens;
62 struct ureg_src src;
63 struct ureg_dst dst;
64 unsigned num_tokens;
65
66 ureg = ureg_create(TGSI_PROCESSOR_FRAGMENT);
67 if (!ureg)
68 return NULL;
69
70 dst = ureg_DECL_output(ureg, TGSI_SEMANTIC_COLOR, 0);
71 src = ureg_DECL_immediate(ureg, color, 4);
72 ureg_MOV(ureg, dst, src);
73 ureg_END(ureg);
74
75 tokens = ureg_get_tokens(ureg, &num_tokens);
76
77 ureg_destroy(ureg);
78
79 return tokens;
80 }
81
82
83 static struct svga_shader_variant *
84 translate_fragment_program(struct svga_context *svga,
85 const struct svga_fragment_shader *fs,
86 const struct svga_compile_key *key)
87 {
88 if (svga_have_vgpu10(svga)) {
89 return svga_tgsi_vgpu10_translate(svga, &fs->base, key,
90 PIPE_SHADER_FRAGMENT);
91 }
92 else {
93 return svga_tgsi_vgpu9_translate(&fs->base, key, PIPE_SHADER_FRAGMENT);
94 }
95 }
96
97
98 /**
99 * Replace the given shader's instruction with a simple constant-color
100 * shader. We use this when normal shader translation fails.
101 */
102 static struct svga_shader_variant *
103 get_compiled_dummy_shader(struct svga_context *svga,
104 struct svga_fragment_shader *fs,
105 const struct svga_compile_key *key)
106 {
107 const struct tgsi_token *dummy = get_dummy_fragment_shader();
108 struct svga_shader_variant *variant;
109
110 if (!dummy) {
111 return NULL;
112 }
113
114 FREE((void *) fs->base.tokens);
115 fs->base.tokens = dummy;
116
117 variant = translate_fragment_program(svga, fs, key);
118 return variant;
119 }
120
121
122 /**
123 * Translate TGSI shader into an svga shader variant.
124 */
125 static enum pipe_error
126 compile_fs(struct svga_context *svga,
127 struct svga_fragment_shader *fs,
128 const struct svga_compile_key *key,
129 struct svga_shader_variant **out_variant)
130 {
131 struct svga_shader_variant *variant;
132 enum pipe_error ret = PIPE_ERROR;
133
134 variant = translate_fragment_program(svga, fs, key);
135 if (variant == NULL) {
136 debug_printf("Failed to compile fragment shader,"
137 " using dummy shader instead.\n");
138 variant = get_compiled_dummy_shader(svga, fs, key);
139 if (!variant) {
140 ret = PIPE_ERROR;
141 goto fail;
142 }
143 }
144
145 if (svga_shader_too_large(svga, variant)) {
146 /* too big, use dummy shader */
147 debug_printf("Shader too large (%u bytes),"
148 " using dummy shader instead.\n",
149 (unsigned) (variant->nr_tokens
150 * sizeof(variant->tokens[0])));
151 variant = get_compiled_dummy_shader(svga, fs, key);
152 if (!variant) {
153 ret = PIPE_ERROR;
154 goto fail;
155 }
156 }
157
158 ret = svga_define_shader(svga, SVGA3D_SHADERTYPE_PS, variant);
159 if (ret != PIPE_OK)
160 goto fail;
161
162 *out_variant = variant;
163
164 /* insert variants at head of linked list */
165 variant->next = fs->base.variants;
166 fs->base.variants = variant;
167
168 return PIPE_OK;
169
170 fail:
171 if (variant) {
172 svga_destroy_shader_variant(svga, SVGA3D_SHADERTYPE_PS, variant);
173 }
174 return ret;
175 }
176
177
178 /* SVGA_NEW_TEXTURE_BINDING
179 * SVGA_NEW_RAST
180 * SVGA_NEW_NEED_SWTNL
181 * SVGA_NEW_SAMPLER
182 */
183 static enum pipe_error
184 make_fs_key(const struct svga_context *svga,
185 struct svga_fragment_shader *fs,
186 struct svga_compile_key *key)
187 {
188 const unsigned shader = PIPE_SHADER_FRAGMENT;
189 unsigned i;
190
191 memset(key, 0, sizeof *key);
192
193 memcpy(key->generic_remap_table, fs->generic_remap_table,
194 sizeof(fs->generic_remap_table));
195
196 /* SVGA_NEW_GS, SVGA_NEW_VS
197 */
198 if (svga->curr.gs) {
199 key->fs.gs_generic_outputs = svga->curr.gs->generic_outputs;
200 } else {
201 key->fs.vs_generic_outputs = svga->curr.vs->generic_outputs;
202 }
203
204 /* Only need fragment shader fixup for twoside lighting if doing
205 * hwtnl. Otherwise the draw module does the whole job for us.
206 *
207 * SVGA_NEW_SWTNL
208 */
209 if (!svga->state.sw.need_swtnl) {
210 /* SVGA_NEW_RAST, SVGA_NEW_REDUCED_PRIMITIVE
211 */
212 key->fs.light_twoside = svga->curr.rast->templ.light_twoside;
213 key->fs.front_ccw = svga->curr.rast->templ.front_ccw;
214 key->fs.pstipple = (svga->curr.rast->templ.poly_stipple_enable &&
215 svga->curr.reduced_prim == PIPE_PRIM_TRIANGLES);
216 key->fs.aa_point = (svga->curr.rast->templ.point_smooth &&
217 svga->curr.reduced_prim == PIPE_PRIM_POINTS &&
218 (svga->curr.rast->pointsize > 1.0 ||
219 svga->curr.vs->base.info.writes_psize));
220 if (key->fs.aa_point) {
221 assert(svga->curr.gs != NULL);
222 assert(svga->curr.gs->aa_point_coord_index != -1);
223 key->fs.aa_point_coord_index = svga->curr.gs->aa_point_coord_index;
224 }
225 }
226
227 /* The blend workaround for simulating logicop xor behaviour
228 * requires that the incoming fragment color be white. This change
229 * achieves that by creating a variant of the current fragment
230 * shader that overrides all output colors with 1,1,1,1
231 *
232 * This will work for most shaders, including those containing
233 * TEXKIL and/or depth-write. However, it will break on the
234 * combination of xor-logicop plus alphatest.
235 *
236 * Ultimately, we could implement alphatest in the shader using
237 * texkil prior to overriding the outgoing fragment color.
238 *
239 * SVGA_NEW_BLEND
240 */
241 if (svga->curr.blend->need_white_fragments) {
242 key->fs.white_fragments = 1;
243 }
244
245 #ifdef DEBUG
246 /*
247 * We expect a consistent set of samplers and sampler views.
248 * Do some debug checks/warnings here.
249 */
250 {
251 static boolean warned = FALSE;
252 unsigned i, n = MAX2(svga->curr.num_sampler_views[shader],
253 svga->curr.num_samplers[shader]);
254 /* Only warn once to prevent too much debug output */
255 if (!warned) {
256 if (svga->curr.num_sampler_views[shader] !=
257 svga->curr.num_samplers[shader]) {
258 debug_printf("svga: mismatched number of sampler views (%u) "
259 "vs. samplers (%u)\n",
260 svga->curr.num_sampler_views[shader],
261 svga->curr.num_samplers[shader]);
262 }
263 for (i = 0; i < n; i++) {
264 if ((svga->curr.sampler_views[shader][i] == NULL) !=
265 (svga->curr.sampler[shader][i] == NULL))
266 debug_printf("sampler_view[%u] = %p but sampler[%u] = %p\n",
267 i, svga->curr.sampler_views[shader][i],
268 i, svga->curr.sampler[shader][i]);
269 }
270 warned = TRUE;
271 }
272 }
273 #endif
274
275 /* XXX: want to limit this to the textures that the shader actually
276 * refers to.
277 *
278 * SVGA_NEW_TEXTURE_BINDING | SVGA_NEW_SAMPLER
279 */
280 svga_init_shader_key_common(svga, shader, key);
281
282 for (i = 0; i < svga->curr.num_samplers[shader]; ++i) {
283 struct pipe_sampler_view *view = svga->curr.sampler_views[shader][i];
284 const struct svga_sampler_state *sampler = svga->curr.sampler[shader][i];
285 if (view) {
286 struct pipe_resource *tex = view->texture;
287 if (tex->target != PIPE_BUFFER) {
288 struct svga_texture *stex = svga_texture(tex);
289 SVGA3dSurfaceFormat format = stex->key.format;
290
291 if (!svga_have_vgpu10(svga) &&
292 (format == SVGA3D_Z_D16 ||
293 format == SVGA3D_Z_D24X8 ||
294 format == SVGA3D_Z_D24S8)) {
295 /* If we're sampling from a SVGA3D_Z_D16, SVGA3D_Z_D24X8,
296 * or SVGA3D_Z_D24S8 surface, we'll automatically get
297 * shadow comparison. But we only get LEQUAL mode.
298 * Set TEX_COMPARE_NONE here so we don't emit the extra FS
299 * code for shadow comparison.
300 */
301 key->tex[i].compare_mode = PIPE_TEX_COMPARE_NONE;
302 key->tex[i].compare_func = PIPE_FUNC_NEVER;
303 /* These depth formats _only_ support comparison mode and
304 * not ordinary sampling so warn if the later is expected.
305 */
306 if (sampler->compare_mode != PIPE_TEX_COMPARE_R_TO_TEXTURE) {
307 debug_warn_once("Unsupported shadow compare mode");
308 }
309 /* The shader translation code can emit code to
310 * handle ALWAYS and NEVER compare functions
311 */
312 else if (sampler->compare_func == PIPE_FUNC_ALWAYS ||
313 sampler->compare_func == PIPE_FUNC_NEVER) {
314 key->tex[i].compare_mode = sampler->compare_mode;
315 key->tex[i].compare_func = sampler->compare_func;
316 }
317 else if (sampler->compare_func != PIPE_FUNC_LEQUAL) {
318 debug_warn_once("Unsupported shadow compare function");
319 }
320 }
321 else {
322 /* For other texture formats, just use the compare func/mode
323 * as-is. Should be no-ops for color textures. For depth
324 * textures, we do not get automatic depth compare. We have
325 * to do it ourselves in the shader. And we don't get PCF.
326 */
327 key->tex[i].compare_mode = sampler->compare_mode;
328 key->tex[i].compare_func = sampler->compare_func;
329 }
330 }
331 }
332 }
333
334 /* sprite coord gen state */
335 for (i = 0; i < svga->curr.num_samplers[shader]; ++i) {
336 key->tex[i].sprite_texgen =
337 svga->curr.rast->templ.sprite_coord_enable & (1 << i);
338 }
339
340 key->sprite_origin_lower_left = (svga->curr.rast->templ.sprite_coord_mode
341 == PIPE_SPRITE_COORD_LOWER_LEFT);
342
343 key->fs.flatshade = svga->curr.rast->templ.flatshade;
344
345 /* SVGA_NEW_DEPTH_STENCIL_ALPHA */
346 if (svga_have_vgpu10(svga)) {
347 /* Alpha testing is not supported in integer-valued render targets. */
348 if (svga_has_any_integer_cbufs(svga)) {
349 key->fs.alpha_func = SVGA3D_CMP_ALWAYS;
350 key->fs.alpha_ref = 0;
351 }
352 else {
353 key->fs.alpha_func = svga->curr.depth->alphafunc;
354 key->fs.alpha_ref = svga->curr.depth->alpharef;
355 }
356 }
357
358 /* SVGA_NEW_FRAME_BUFFER */
359 if (fs->base.info.properties[TGSI_PROPERTY_FS_COLOR0_WRITES_ALL_CBUFS]) {
360 /* Replicate color0 output to N colorbuffers */
361 key->fs.write_color0_to_n_cbufs = svga->curr.framebuffer.nr_cbufs;
362 }
363
364 return PIPE_OK;
365 }
366
367
368 /**
369 * svga_reemit_fs_bindings - Reemit the fragment shader bindings
370 */
371 enum pipe_error
372 svga_reemit_fs_bindings(struct svga_context *svga)
373 {
374 enum pipe_error ret;
375
376 assert(svga->rebind.flags.fs);
377 assert(svga_have_gb_objects(svga));
378
379 if (!svga->state.hw_draw.fs)
380 return PIPE_OK;
381
382 if (!svga_need_to_rebind_resources(svga)) {
383 ret = svga->swc->resource_rebind(svga->swc, NULL,
384 svga->state.hw_draw.fs->gb_shader,
385 SVGA_RELOC_READ);
386 goto out;
387 }
388
389 if (svga_have_vgpu10(svga))
390 ret = SVGA3D_vgpu10_SetShader(svga->swc, SVGA3D_SHADERTYPE_PS,
391 svga->state.hw_draw.fs->gb_shader,
392 svga->state.hw_draw.fs->id);
393 else
394 ret = SVGA3D_SetGBShader(svga->swc, SVGA3D_SHADERTYPE_PS,
395 svga->state.hw_draw.fs->gb_shader);
396
397 out:
398 if (ret != PIPE_OK)
399 return ret;
400
401 svga->rebind.flags.fs = FALSE;
402 return PIPE_OK;
403 }
404
405
406
407 static enum pipe_error
408 emit_hw_fs(struct svga_context *svga, unsigned dirty)
409 {
410 struct svga_shader_variant *variant = NULL;
411 enum pipe_error ret = PIPE_OK;
412 struct svga_fragment_shader *fs = svga->curr.fs;
413 struct svga_compile_key key;
414
415 /* SVGA_NEW_BLEND
416 * SVGA_NEW_TEXTURE_BINDING
417 * SVGA_NEW_RAST
418 * SVGA_NEW_NEED_SWTNL
419 * SVGA_NEW_SAMPLER
420 * SVGA_NEW_FRAME_BUFFER
421 * SVGA_NEW_DEPTH_STENCIL_ALPHA
422 * SVGA_NEW_VS
423 */
424 ret = make_fs_key(svga, fs, &key);
425 if (ret != PIPE_OK)
426 return ret;
427
428 variant = svga_search_shader_key(&fs->base, &key);
429 if (!variant) {
430 ret = compile_fs(svga, fs, &key, &variant);
431 if (ret != PIPE_OK)
432 return ret;
433 }
434
435 assert(variant);
436
437 if (variant != svga->state.hw_draw.fs) {
438 ret = svga_set_shader(svga, SVGA3D_SHADERTYPE_PS, variant);
439 if (ret != PIPE_OK)
440 return ret;
441
442 svga->rebind.flags.fs = FALSE;
443
444 svga->dirty |= SVGA_NEW_FS_VARIANT;
445 svga->state.hw_draw.fs = variant;
446 }
447
448 return PIPE_OK;
449 }
450
451 struct svga_tracked_state svga_hw_fs =
452 {
453 "fragment shader (hwtnl)",
454 (SVGA_NEW_FS |
455 SVGA_NEW_GS |
456 SVGA_NEW_VS |
457 SVGA_NEW_TEXTURE_BINDING |
458 SVGA_NEW_NEED_SWTNL |
459 SVGA_NEW_RAST |
460 SVGA_NEW_REDUCED_PRIMITIVE |
461 SVGA_NEW_SAMPLER |
462 SVGA_NEW_FRAME_BUFFER |
463 SVGA_NEW_DEPTH_STENCIL_ALPHA |
464 SVGA_NEW_BLEND),
465 emit_hw_fs
466 };
467
468
469