svga: update shader code for GBS
[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 /* XXX: want to limit this to the textures that the shader actually
240 * refers to.
241 *
242 * SVGA_NEW_TEXTURE_BINDING | SVGA_NEW_SAMPLER
243 */
244 for (i = 0; i < svga->curr.num_sampler_views; i++) {
245 if (svga->curr.sampler_views[i]) {
246 assert(svga->curr.sampler[i]);
247 assert(svga->curr.sampler_views[i]->texture);
248 key->tex[i].texture_target = svga->curr.sampler_views[i]->texture->target;
249 if (!svga->curr.sampler[i]->normalized_coords) {
250 key->tex[i].width_height_idx = idx++;
251 key->tex[i].unnormalized = TRUE;
252 ++key->num_unnormalized_coords;
253 }
254
255 key->tex[i].swizzle_r = svga->curr.sampler_views[i]->swizzle_r;
256 key->tex[i].swizzle_g = svga->curr.sampler_views[i]->swizzle_g;
257 key->tex[i].swizzle_b = svga->curr.sampler_views[i]->swizzle_b;
258 key->tex[i].swizzle_a = svga->curr.sampler_views[i]->swizzle_a;
259 }
260 }
261 key->num_textures = svga->curr.num_sampler_views;
262
263 idx = 0;
264 for (i = 0; i < svga->curr.num_samplers; ++i) {
265 if (svga->curr.sampler_views[i]) {
266 struct pipe_resource *tex = svga->curr.sampler_views[i]->texture;
267 struct svga_texture *stex = svga_texture(tex);
268 SVGA3dSurfaceFormat format = stex->key.format;
269
270 if (format == SVGA3D_Z_D16 ||
271 format == SVGA3D_Z_D24X8 ||
272 format == SVGA3D_Z_D24S8) {
273 /* If we're sampling from a SVGA3D_Z_D16, SVGA3D_Z_D24X8,
274 * or SVGA3D_Z_D24S8 surface, we'll automatically get
275 * shadow comparison. But we only get LEQUAL mode.
276 * Set TEX_COMPARE_NONE here so we don't emit the extra FS
277 * code for shadow comparison.
278 */
279 key->tex[i].compare_mode = PIPE_TEX_COMPARE_NONE;
280 key->tex[i].compare_func = PIPE_FUNC_NEVER;
281 /* These depth formats _only_ support comparison mode and
282 * not ordinary sampling so warn if the later is expected.
283 */
284 if (svga->curr.sampler[i]->compare_mode !=
285 PIPE_TEX_COMPARE_R_TO_TEXTURE) {
286 debug_warn_once("Unsupported shadow compare mode");
287 }
288 /* The only supported comparison mode is LEQUAL */
289 if (svga->curr.sampler[i]->compare_func != PIPE_FUNC_LEQUAL) {
290 debug_warn_once("Unsupported shadow compare function");
291 }
292 }
293 else {
294 /* For other texture formats, just use the compare func/mode
295 * as-is. Should be no-ops for color textures. For depth
296 * textures, we do not get automatic depth compare. We have
297 * to do it ourselves in the shader. And we don't get PCF.
298 */
299 key->tex[i].compare_mode = svga->curr.sampler[i]->compare_mode;
300 key->tex[i].compare_func = svga->curr.sampler[i]->compare_func;
301 }
302 }
303 }
304
305 /* sprite coord gen state */
306 for (i = 0; i < svga->curr.num_samplers; ++i) {
307 key->tex[i].sprite_texgen =
308 svga->curr.rast->templ.sprite_coord_enable & (1 << i);
309 }
310
311 key->sprite_origin_lower_left = (svga->curr.rast->templ.sprite_coord_mode
312 == PIPE_SPRITE_COORD_LOWER_LEFT);
313
314 /* SVGA_NEW_FRAME_BUFFER */
315 if (fs->base.info.color0_writes_all_cbufs) {
316 /* Replicate color0 output to N colorbuffers */
317 key->write_color0_to_n_cbufs = svga->curr.framebuffer.nr_cbufs;
318 }
319
320 return PIPE_OK;
321 }
322
323
324 /**
325 * svga_reemit_fs_bindings - Reemit the fragment shader bindings
326 */
327 enum pipe_error
328 svga_reemit_fs_bindings(struct svga_context *svga)
329 {
330 enum pipe_error ret;
331
332 assert(svga->rebind.fs);
333 assert(svga_have_gb_objects(svga));
334
335 if (!svga->state.hw_draw.fs)
336 return PIPE_OK;
337
338 ret = SVGA3D_SetGBShader(svga->swc, SVGA3D_SHADERTYPE_PS,
339 svga->state.hw_draw.fs->gb_shader);
340 if (ret != PIPE_OK)
341 return ret;
342
343 svga->rebind.fs = FALSE;
344 return PIPE_OK;
345 }
346
347
348
349 static enum pipe_error
350 emit_hw_fs(struct svga_context *svga, unsigned dirty)
351 {
352 struct svga_shader_variant *variant = NULL;
353 enum pipe_error ret = PIPE_OK;
354 struct svga_fragment_shader *fs = svga->curr.fs;
355 struct svga_fs_compile_key key;
356
357 /* SVGA_NEW_BLEND
358 * SVGA_NEW_TEXTURE_BINDING
359 * SVGA_NEW_RAST
360 * SVGA_NEW_NEED_SWTNL
361 * SVGA_NEW_SAMPLER
362 * SVGA_NEW_FRAME_BUFFER
363 */
364 ret = make_fs_key( svga, fs, &key );
365 if (ret != PIPE_OK)
366 return ret;
367
368 variant = search_fs_key( fs, &key );
369 if (!variant) {
370 ret = compile_fs( svga, fs, &key, &variant );
371 if (ret != PIPE_OK)
372 return ret;
373 }
374
375 assert(variant);
376
377 if (variant != svga->state.hw_draw.fs) {
378 if (svga_have_gb_objects(svga)) {
379 /*
380 * Bind is necessary here only because pipebuffer_fenced may move
381 * the shader contents around....
382 */
383 ret = SVGA3D_BindGBShader(svga->swc, variant->gb_shader);
384 if (ret != PIPE_OK)
385 return ret;
386
387 ret = SVGA3D_SetGBShader(svga->swc, SVGA3D_SHADERTYPE_PS,
388 variant->gb_shader);
389 if (ret != PIPE_OK)
390 return ret;
391
392 svga->rebind.fs = FALSE;
393 }
394 else {
395 ret = SVGA3D_SetShader(svga->swc, SVGA3D_SHADERTYPE_PS, variant->id);
396 if (ret != PIPE_OK)
397 return ret;
398 }
399
400 svga->dirty |= SVGA_NEW_FS_VARIANT;
401 svga->state.hw_draw.fs = variant;
402 }
403
404 return PIPE_OK;
405 }
406
407 struct svga_tracked_state svga_hw_fs =
408 {
409 "fragment shader (hwtnl)",
410 (SVGA_NEW_FS |
411 SVGA_NEW_TEXTURE_BINDING |
412 SVGA_NEW_NEED_SWTNL |
413 SVGA_NEW_RAST |
414 SVGA_NEW_SAMPLER |
415 SVGA_NEW_FRAME_BUFFER |
416 SVGA_NEW_BLEND),
417 emit_hw_fs
418 };
419
420
421