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