1 /**********************************************************
2 * Copyright 2008-2022 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 /**
84 * Replace the given shader's instruction with a simple constant-color
85 * shader. We use this when normal shader translation fails.
86 */
87 struct svga_shader_variant *
svga_get_compiled_dummy_fragment_shader(struct svga_context * svga,struct svga_shader * shader,const struct svga_compile_key * key)88 svga_get_compiled_dummy_fragment_shader(struct svga_context *svga,
89 struct svga_shader *shader,
90 const struct svga_compile_key *key)
91 {
92 struct svga_fragment_shader *fs = (struct svga_fragment_shader *)shader;
93 const struct tgsi_token *dummy = get_dummy_fragment_shader();
94 struct svga_shader_variant *variant;
95
96 if (!dummy) {
97 return NULL;
98 }
99
100 FREE((void *) fs->base.tokens);
101 fs->base.tokens = dummy;
102
103 svga_tgsi_scan_shader(&fs->base);
104 svga_remap_generics(fs->base.info.generic_inputs_mask,
105 fs->generic_remap_table);
106
107 variant = svga_tgsi_compile_shader(svga, shader, key);
108 return variant;
109 }
110
111
112 /* SVGA_NEW_TEXTURE_BINDING
113 * SVGA_NEW_RAST
114 * SVGA_NEW_NEED_SWTNL
115 * SVGA_NEW_SAMPLER
116 */
117 static enum pipe_error
make_fs_key(const struct svga_context * svga,struct svga_fragment_shader * fs,struct svga_compile_key * key)118 make_fs_key(const struct svga_context *svga,
119 struct svga_fragment_shader *fs,
120 struct svga_compile_key *key)
121 {
122 const enum pipe_shader_type shader = PIPE_SHADER_FRAGMENT;
123 unsigned i;
124
125 memset(key, 0, sizeof *key);
126
127 memcpy(key->generic_remap_table, fs->generic_remap_table,
128 sizeof(fs->generic_remap_table));
129
130 /* SVGA_NEW_GS, SVGA_NEW_VS
131 */
132 struct svga_geometry_shader *gs = svga->curr.gs;
133 struct svga_vertex_shader *vs = svga->curr.vs;
134 if (gs) {
135 key->fs.gs_generic_outputs = gs->base.info.generic_outputs_mask;
136 key->fs.layer_to_zero = !gs->base.info.writes_layer;
137 } else {
138 key->fs.vs_generic_outputs = vs->base.info.generic_outputs_mask;
139 key->fs.layer_to_zero = 1;
140 }
141
142 /* Only need fragment shader fixup for twoside lighting if doing
143 * hwtnl. Otherwise the draw module does the whole job for us.
144 *
145 * SVGA_NEW_SWTNL
146 */
147 if (!svga->state.sw.need_swtnl) {
148 /* SVGA_NEW_RAST, SVGA_NEW_REDUCED_PRIMITIVE
149 */
150 enum pipe_prim_type prim_mode;
151 struct svga_shader *shader;
152
153 /* Find the last shader in the vertex pipeline and the output primitive mode
154 * from that shader.
155 */
156 if (svga->curr.tes) {
157 shader = &svga->curr.tes->base;
158 prim_mode = shader->info.tes.prim_mode;
159 } else if (svga->curr.gs) {
160 shader = &svga->curr.gs->base;
161 prim_mode = shader->info.gs.out_prim;
162 } else {
163 shader = &svga->curr.vs->base;
164 prim_mode = svga->curr.reduced_prim;
165 }
166
167 key->fs.light_twoside = svga->curr.rast->templ.light_twoside;
168 key->fs.front_ccw = svga->curr.rast->templ.front_ccw;
169 key->fs.pstipple = (svga->curr.rast->templ.poly_stipple_enable &&
170 prim_mode == PIPE_PRIM_TRIANGLES);
171
172 if (svga->curr.gs) {
173 key->fs.aa_point = (svga->curr.rast->templ.point_smooth &&
174 shader->info.gs.in_prim == PIPE_PRIM_POINTS &&
175 (svga->curr.rast->pointsize > 1.0 ||
176 shader->info.writes_psize));
177
178 if (key->fs.aa_point) {
179 assert(svga->curr.gs->aa_point_coord_index != -1);
180 key->fs.aa_point_coord_index = svga->curr.gs->aa_point_coord_index;
181 }
182 }
183 }
184
185 /* The blend workaround for simulating logicop xor behaviour
186 * requires that the incoming fragment color be white. This change
187 * achieves that by creating a variant of the current fragment
188 * shader that overrides all output colors with 1,1,1,1
189 *
190 * This will work for most shaders, including those containing
191 * TEXKIL and/or depth-write. However, it will break on the
192 * combination of xor-logicop plus alphatest.
193 *
194 * Ultimately, we could implement alphatest in the shader using
195 * texkil prior to overriding the outgoing fragment color.
196 *
197 * SVGA_NEW_BLEND
198 */
199 key->fs.white_fragments = svga->curr.blend->need_white_fragments;
200
201 key->fs.alpha_to_one = svga->curr.blend->alpha_to_one;
202
203 #ifdef DEBUG
204 /*
205 * We expect a consistent set of samplers and sampler views.
206 * Do some debug checks/warnings here.
207 */
208 {
209 static boolean warned = FALSE;
210 unsigned i, n = MAX2(svga->curr.num_sampler_views[shader],
211 svga->curr.num_samplers[shader]);
212 /* Only warn once to prevent too much debug output */
213 if (!warned) {
214 if (svga->curr.num_sampler_views[shader] !=
215 svga->curr.num_samplers[shader]) {
216 debug_printf("svga: mismatched number of sampler views (%u) "
217 "vs. samplers (%u)\n",
218 svga->curr.num_sampler_views[shader],
219 svga->curr.num_samplers[shader]);
220 }
221 for (i = 0; i < n; i++) {
222 if ((svga->curr.sampler_views[shader][i] == NULL) !=
223 (svga->curr.sampler[shader][i] == NULL))
224 debug_printf("sampler_view[%u] = %p but sampler[%u] = %p\n",
225 i, svga->curr.sampler_views[shader][i],
226 i, svga->curr.sampler[shader][i]);
227 }
228 warned = TRUE;
229 }
230 }
231 #endif
232
233 /* XXX: want to limit this to the textures that the shader actually
234 * refers to.
235 *
236 * SVGA_NEW_TEXTURE_BINDING | SVGA_NEW_SAMPLER
237 */
238 svga_init_shader_key_common(svga, shader, &fs->base, key);
239
240 for (i = 0; i < svga->curr.num_samplers[shader]; ++i) {
241 struct pipe_sampler_view *view = svga->curr.sampler_views[shader][i];
242 const struct svga_sampler_state *sampler = svga->curr.sampler[shader][i];
243 if (view) {
244 struct pipe_resource *tex = view->texture;
245 if (tex->target != PIPE_BUFFER) {
246 struct svga_texture *stex = svga_texture(tex);
247 SVGA3dSurfaceFormat format = stex->key.format;
248
249 if (!svga_have_vgpu10(svga) &&
250 (format == SVGA3D_Z_D16 ||
251 format == SVGA3D_Z_D24X8 ||
252 format == SVGA3D_Z_D24S8)) {
253 /* If we're sampling from a SVGA3D_Z_D16, SVGA3D_Z_D24X8,
254 * or SVGA3D_Z_D24S8 surface, we'll automatically get
255 * shadow comparison. But we only get LEQUAL mode.
256 * Set TEX_COMPARE_NONE here so we don't emit the extra FS
257 * code for shadow comparison.
258 */
259 key->tex[i].compare_mode = PIPE_TEX_COMPARE_NONE;
260 key->tex[i].compare_func = PIPE_FUNC_NEVER;
261 /* These depth formats _only_ support comparison mode and
262 * not ordinary sampling so warn if the later is expected.
263 */
264 if (sampler->compare_mode != PIPE_TEX_COMPARE_R_TO_TEXTURE) {
265 debug_warn_once("Unsupported shadow compare mode");
266 }
267 /* The shader translation code can emit code to
268 * handle ALWAYS and NEVER compare functions
269 */
270 else if (sampler->compare_func == PIPE_FUNC_ALWAYS ||
271 sampler->compare_func == PIPE_FUNC_NEVER) {
272 key->tex[i].compare_mode = sampler->compare_mode;
273 key->tex[i].compare_func = sampler->compare_func;
274 }
275 else if (sampler->compare_func != PIPE_FUNC_LEQUAL) {
276 debug_warn_once("Unsupported shadow compare function");
277 }
278 }
279 }
280 }
281 }
282
283 /* sprite coord gen state */
284 key->sprite_coord_enable = svga->curr.rast->templ.sprite_coord_enable;
285
286 key->sprite_origin_lower_left = (svga->curr.rast->templ.sprite_coord_mode
287 == PIPE_SPRITE_COORD_LOWER_LEFT);
288
289 key->fs.flatshade = svga->curr.rast->templ.flatshade;
290
291 /* SVGA_NEW_DEPTH_STENCIL_ALPHA */
292 if (svga_have_vgpu10(svga)) {
293 /* Alpha testing is not supported in integer-valued render targets. */
294 if (svga_has_any_integer_cbufs(svga)) {
295 key->fs.alpha_func = SVGA3D_CMP_ALWAYS;
296 key->fs.alpha_ref = 0;
297 }
298 else {
299 key->fs.alpha_func = svga->curr.depth->alphafunc;
300 key->fs.alpha_ref = svga->curr.depth->alpharef;
301 }
302 }
303
304 /* SVGA_NEW_FRAME_BUFFER | SVGA_NEW_BLEND */
305 if (fs->base.info.fs.color0_writes_all_cbufs ||
306 svga->curr.blend->need_white_fragments) {
307 /* Replicate color0 output (or white) to N colorbuffers */
308 key->fs.write_color0_to_n_cbufs = svga->curr.framebuffer.nr_cbufs;
309 }
310
311 return PIPE_OK;
312 }
313
314
315 /**
316 * svga_reemit_fs_bindings - Reemit the fragment shader bindings
317 */
318 enum pipe_error
svga_reemit_fs_bindings(struct svga_context * svga)319 svga_reemit_fs_bindings(struct svga_context *svga)
320 {
321 enum pipe_error ret;
322
323 assert(svga->rebind.flags.fs);
324 assert(svga_have_gb_objects(svga));
325
326 if (!svga->state.hw_draw.fs)
327 return PIPE_OK;
328
329 if (!svga_need_to_rebind_resources(svga)) {
330 ret = svga->swc->resource_rebind(svga->swc, NULL,
331 svga->state.hw_draw.fs->gb_shader,
332 SVGA_RELOC_READ);
333 }
334 else {
335 if (svga_have_vgpu10(svga))
336 ret = SVGA3D_vgpu10_SetShader(svga->swc, SVGA3D_SHADERTYPE_PS,
337 svga->state.hw_draw.fs->gb_shader,
338 svga->state.hw_draw.fs->id);
339 else
340 ret = SVGA3D_SetGBShader(svga->swc, SVGA3D_SHADERTYPE_PS,
341 svga->state.hw_draw.fs->gb_shader);
342 }
343
344 if (ret != PIPE_OK)
345 return ret;
346
347 svga->rebind.flags.fs = FALSE;
348 return PIPE_OK;
349 }
350
351
352
353 static enum pipe_error
emit_hw_fs(struct svga_context * svga,uint64_t dirty)354 emit_hw_fs(struct svga_context *svga, uint64_t dirty)
355 {
356 struct svga_shader_variant *variant = NULL;
357 enum pipe_error ret = PIPE_OK;
358 struct svga_fragment_shader *fs = svga->curr.fs;
359 struct svga_compile_key key;
360 struct svga_shader *prevShader = NULL; /* shader in the previous stage */
361
362 SVGA_STATS_TIME_PUSH(svga_sws(svga), SVGA_STATS_TIME_EMITFS);
363
364 prevShader = svga->curr.gs ?
365 &svga->curr.gs->base : (svga->curr.tes ?
366 &svga->curr.tes->base : &svga->curr.vs->base);
367
368 /* Disable rasterization if rasterizer_discard flag is set or
369 * vs/gs does not output position.
370 */
371 svga->disable_rasterizer =
372 svga->curr.rast->templ.rasterizer_discard ||
373 !prevShader->info.writes_position;
374
375 /* Set FS to NULL when rasterization is to be disabled */
376 if (svga->disable_rasterizer) {
377 /* Set FS to NULL if it has not been done */
378 if (svga->state.hw_draw.fs) {
379 ret = svga_set_shader(svga, SVGA3D_SHADERTYPE_PS, NULL);
380 if (ret != PIPE_OK)
381 goto done;
382 }
383 svga->rebind.flags.fs = FALSE;
384 svga->state.hw_draw.fs = NULL;
385 goto done;
386 }
387
388 /* SVGA_NEW_BLEND
389 * SVGA_NEW_TEXTURE_BINDING
390 * SVGA_NEW_RAST
391 * SVGA_NEW_NEED_SWTNL
392 * SVGA_NEW_SAMPLER
393 * SVGA_NEW_FRAME_BUFFER
394 * SVGA_NEW_DEPTH_STENCIL_ALPHA
395 * SVGA_NEW_VS
396 */
397 ret = make_fs_key(svga, fs, &key);
398 if (ret != PIPE_OK)
399 goto done;
400
401 variant = svga_search_shader_key(&fs->base, &key);
402 if (!variant) {
403 ret = svga_compile_shader(svga, &fs->base, &key, &variant);
404 if (ret != PIPE_OK)
405 goto done;
406 }
407
408 assert(variant);
409
410 if (variant != svga->state.hw_draw.fs) {
411 ret = svga_set_shader(svga, SVGA3D_SHADERTYPE_PS, variant);
412 if (ret != PIPE_OK)
413 goto done;
414
415 svga->rebind.flags.fs = FALSE;
416
417 svga->dirty |= SVGA_NEW_FS_VARIANT;
418 svga->state.hw_draw.fs = variant;
419 }
420
421 done:
422 SVGA_STATS_TIME_POP(svga_sws(svga));
423 return ret;
424 }
425
426 struct svga_tracked_state svga_hw_fs =
427 {
428 "fragment shader (hwtnl)",
429 (SVGA_NEW_FS |
430 SVGA_NEW_GS |
431 SVGA_NEW_VS |
432 SVGA_NEW_TEXTURE_BINDING |
433 SVGA_NEW_NEED_SWTNL |
434 SVGA_NEW_RAST |
435 SVGA_NEW_STIPPLE |
436 SVGA_NEW_REDUCED_PRIMITIVE |
437 SVGA_NEW_SAMPLER |
438 SVGA_NEW_FRAME_BUFFER |
439 SVGA_NEW_DEPTH_STENCIL_ALPHA |
440 SVGA_NEW_BLEND |
441 SVGA_NEW_FS_RAW_BUFFER),
442 emit_hw_fs
443 };
444
445
446
447