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