1 /**********************************************************
2 * Copyright 2008-2009 VMware, Inc. All rights reserved.
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:
12 * The above copyright notice and this permission notice shall be
13 * included in all copies or substantial portions of the Software.
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
24 **********************************************************/
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"
33 #include "svga_context.h"
34 #include "svga_state.h"
36 #include "svga_shader.h"
37 #include "svga_resource_texture.h"
38 #include "svga_tgsi.h"
39 #include "svga_format.h"
41 #include "svga_hw_reg.h"
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.
52 static const struct tgsi_token
*
53 get_dummy_fragment_shader(void)
56 static const float color
[4] = { 1.0, 0.0, 0.0, 0.0 }; /* red */
58 static const float color
[4] = { 0.0, 0.0, 0.0, 0.0 }; /* black */
60 struct ureg_program
*ureg
;
61 const struct tgsi_token
*tokens
;
66 ureg
= ureg_create(TGSI_PROCESSOR_FRAGMENT
);
70 dst
= ureg_DECL_output(ureg
, TGSI_SEMANTIC_COLOR
, 0);
71 src
= ureg_DECL_immediate(ureg
, color
, 4);
72 ureg_MOV(ureg
, dst
, src
);
75 tokens
= ureg_get_tokens(ureg
, &num_tokens
);
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
)
88 if (svga_have_vgpu10(svga
)) {
89 return svga_tgsi_vgpu10_translate(svga
, &fs
->base
, key
,
90 PIPE_SHADER_FRAGMENT
);
93 return svga_tgsi_vgpu9_translate(&fs
->base
, key
, PIPE_SHADER_FRAGMENT
);
99 * Replace the given shader's instruction with a simple constant-color
100 * shader. We use this when normal shader translation fails.
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
)
107 const struct tgsi_token
*dummy
= get_dummy_fragment_shader();
108 struct svga_shader_variant
*variant
;
114 FREE((void *) fs
->base
.tokens
);
115 fs
->base
.tokens
= dummy
;
117 variant
= translate_fragment_program(svga
, fs
, key
);
123 * Translate TGSI shader into an svga shader variant.
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
)
131 struct svga_shader_variant
*variant
;
132 enum pipe_error ret
= PIPE_ERROR
;
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
);
140 else if (svga_shader_too_large(svga
, variant
)) {
141 /* too big, use dummy shader */
142 debug_printf("Shader too large (%u bytes),"
143 " using dummy shader instead.\n",
144 (unsigned) (variant
->nr_tokens
145 * sizeof(variant
->tokens
[0])));
146 /* Free the too-large variant */
147 svga_destroy_shader_variant(svga
, SVGA3D_SHADERTYPE_PS
, variant
);
148 /* Use simple pass-through shader instead */
149 variant
= get_compiled_dummy_shader(svga
, fs
, key
);
156 ret
= svga_define_shader(svga
, SVGA3D_SHADERTYPE_PS
, variant
);
157 if (ret
!= PIPE_OK
) {
158 svga_destroy_shader_variant(svga
, SVGA3D_SHADERTYPE_PS
, variant
);
162 *out_variant
= variant
;
164 /* insert variant at head of linked list */
165 variant
->next
= fs
->base
.variants
;
166 fs
->base
.variants
= variant
;
172 /* SVGA_NEW_TEXTURE_BINDING
174 * SVGA_NEW_NEED_SWTNL
177 static enum pipe_error
178 make_fs_key(const struct svga_context
*svga
,
179 struct svga_fragment_shader
*fs
,
180 struct svga_compile_key
*key
)
182 const unsigned shader
= PIPE_SHADER_FRAGMENT
;
185 memset(key
, 0, sizeof *key
);
187 memcpy(key
->generic_remap_table
, fs
->generic_remap_table
,
188 sizeof(fs
->generic_remap_table
));
190 /* SVGA_NEW_GS, SVGA_NEW_VS
193 key
->fs
.gs_generic_outputs
= svga
->curr
.gs
->generic_outputs
;
195 key
->fs
.vs_generic_outputs
= svga
->curr
.vs
->generic_outputs
;
198 /* Only need fragment shader fixup for twoside lighting if doing
199 * hwtnl. Otherwise the draw module does the whole job for us.
203 if (!svga
->state
.sw
.need_swtnl
) {
204 /* SVGA_NEW_RAST, SVGA_NEW_REDUCED_PRIMITIVE
206 key
->fs
.light_twoside
= svga
->curr
.rast
->templ
.light_twoside
;
207 key
->fs
.front_ccw
= svga
->curr
.rast
->templ
.front_ccw
;
208 key
->fs
.pstipple
= (svga
->curr
.rast
->templ
.poly_stipple_enable
&&
209 svga
->curr
.reduced_prim
== PIPE_PRIM_TRIANGLES
);
210 key
->fs
.aa_point
= (svga
->curr
.rast
->templ
.point_smooth
&&
211 svga
->curr
.reduced_prim
== PIPE_PRIM_POINTS
&&
212 (svga
->curr
.rast
->pointsize
> 1.0 ||
213 svga
->curr
.vs
->base
.info
.writes_psize
));
214 if (key
->fs
.aa_point
) {
215 assert(svga
->curr
.gs
!= NULL
);
216 assert(svga
->curr
.gs
->aa_point_coord_index
!= -1);
217 key
->fs
.aa_point_coord_index
= svga
->curr
.gs
->aa_point_coord_index
;
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
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.
230 * Ultimately, we could implement alphatest in the shader using
231 * texkil prior to overriding the outgoing fragment color.
235 if (svga
->curr
.blend
->need_white_fragments
) {
236 key
->fs
.white_fragments
= 1;
241 * We expect a consistent set of samplers and sampler views.
242 * Do some debug checks/warnings here.
245 static boolean warned
= FALSE
;
246 unsigned i
, n
= MAX2(svga
->curr
.num_sampler_views
[shader
],
247 svga
->curr
.num_samplers
[shader
]);
248 /* Only warn once to prevent too much debug output */
250 if (svga
->curr
.num_sampler_views
[shader
] !=
251 svga
->curr
.num_samplers
[shader
]) {
252 debug_printf("svga: mismatched number of sampler views (%u) "
253 "vs. samplers (%u)\n",
254 svga
->curr
.num_sampler_views
[shader
],
255 svga
->curr
.num_samplers
[shader
]);
257 for (i
= 0; i
< n
; i
++) {
258 if ((svga
->curr
.sampler_views
[shader
][i
] == NULL
) !=
259 (svga
->curr
.sampler
[shader
][i
] == NULL
))
260 debug_printf("sampler_view[%u] = %p but sampler[%u] = %p\n",
261 i
, svga
->curr
.sampler_views
[shader
][i
],
262 i
, svga
->curr
.sampler
[shader
][i
]);
269 /* XXX: want to limit this to the textures that the shader actually
272 * SVGA_NEW_TEXTURE_BINDING | SVGA_NEW_SAMPLER
274 svga_init_shader_key_common(svga
, shader
, key
);
276 for (i
= 0; i
< svga
->curr
.num_samplers
[shader
]; ++i
) {
277 struct pipe_sampler_view
*view
= svga
->curr
.sampler_views
[shader
][i
];
278 const struct svga_sampler_state
*sampler
= svga
->curr
.sampler
[shader
][i
];
280 struct pipe_resource
*tex
= view
->texture
;
281 if (tex
->target
!= PIPE_BUFFER
) {
282 struct svga_texture
*stex
= svga_texture(tex
);
283 SVGA3dSurfaceFormat format
= stex
->key
.format
;
285 if (!svga_have_vgpu10(svga
) &&
286 (format
== SVGA3D_Z_D16
||
287 format
== SVGA3D_Z_D24X8
||
288 format
== SVGA3D_Z_D24S8
)) {
289 /* If we're sampling from a SVGA3D_Z_D16, SVGA3D_Z_D24X8,
290 * or SVGA3D_Z_D24S8 surface, we'll automatically get
291 * shadow comparison. But we only get LEQUAL mode.
292 * Set TEX_COMPARE_NONE here so we don't emit the extra FS
293 * code for shadow comparison.
295 key
->tex
[i
].compare_mode
= PIPE_TEX_COMPARE_NONE
;
296 key
->tex
[i
].compare_func
= PIPE_FUNC_NEVER
;
297 /* These depth formats _only_ support comparison mode and
298 * not ordinary sampling so warn if the later is expected.
300 if (sampler
->compare_mode
!= PIPE_TEX_COMPARE_R_TO_TEXTURE
) {
301 debug_warn_once("Unsupported shadow compare mode");
303 /* The shader translation code can emit code to
304 * handle ALWAYS and NEVER compare functions
306 else if (sampler
->compare_func
== PIPE_FUNC_ALWAYS
||
307 sampler
->compare_func
== PIPE_FUNC_NEVER
) {
308 key
->tex
[i
].compare_mode
= sampler
->compare_mode
;
309 key
->tex
[i
].compare_func
= sampler
->compare_func
;
311 else if (sampler
->compare_func
!= PIPE_FUNC_LEQUAL
) {
312 debug_warn_once("Unsupported shadow compare function");
316 /* For other texture formats, just use the compare func/mode
317 * as-is. Should be no-ops for color textures. For depth
318 * textures, we do not get automatic depth compare. We have
319 * to do it ourselves in the shader. And we don't get PCF.
321 key
->tex
[i
].compare_mode
= sampler
->compare_mode
;
322 key
->tex
[i
].compare_func
= sampler
->compare_func
;
328 /* sprite coord gen state */
329 for (i
= 0; i
< svga
->curr
.num_samplers
[shader
]; ++i
) {
330 key
->tex
[i
].sprite_texgen
=
331 svga
->curr
.rast
->templ
.sprite_coord_enable
& (1 << i
);
334 key
->sprite_origin_lower_left
= (svga
->curr
.rast
->templ
.sprite_coord_mode
335 == PIPE_SPRITE_COORD_LOWER_LEFT
);
337 key
->fs
.flatshade
= svga
->curr
.rast
->templ
.flatshade
;
339 /* SVGA_NEW_DEPTH_STENCIL_ALPHA */
340 if (svga_have_vgpu10(svga
)) {
341 /* Alpha testing is not supported in integer-valued render targets. */
342 if (svga_has_any_integer_cbufs(svga
)) {
343 key
->fs
.alpha_func
= SVGA3D_CMP_ALWAYS
;
344 key
->fs
.alpha_ref
= 0;
347 key
->fs
.alpha_func
= svga
->curr
.depth
->alphafunc
;
348 key
->fs
.alpha_ref
= svga
->curr
.depth
->alpharef
;
352 /* SVGA_NEW_FRAME_BUFFER */
353 if (fs
->base
.info
.properties
[TGSI_PROPERTY_FS_COLOR0_WRITES_ALL_CBUFS
]) {
354 /* Replicate color0 output to N colorbuffers */
355 key
->fs
.write_color0_to_n_cbufs
= svga
->curr
.framebuffer
.nr_cbufs
;
363 * svga_reemit_fs_bindings - Reemit the fragment shader bindings
366 svga_reemit_fs_bindings(struct svga_context
*svga
)
370 assert(svga
->rebind
.flags
.fs
);
371 assert(svga_have_gb_objects(svga
));
373 if (!svga
->state
.hw_draw
.fs
)
376 if (!svga_need_to_rebind_resources(svga
)) {
377 ret
= svga
->swc
->resource_rebind(svga
->swc
, NULL
,
378 svga
->state
.hw_draw
.fs
->gb_shader
,
383 if (svga_have_vgpu10(svga
))
384 ret
= SVGA3D_vgpu10_SetShader(svga
->swc
, SVGA3D_SHADERTYPE_PS
,
385 svga
->state
.hw_draw
.fs
->gb_shader
,
386 svga
->state
.hw_draw
.fs
->id
);
388 ret
= SVGA3D_SetGBShader(svga
->swc
, SVGA3D_SHADERTYPE_PS
,
389 svga
->state
.hw_draw
.fs
->gb_shader
);
395 svga
->rebind
.flags
.fs
= FALSE
;
401 static enum pipe_error
402 emit_hw_fs(struct svga_context
*svga
, unsigned dirty
)
404 struct svga_shader_variant
*variant
= NULL
;
405 enum pipe_error ret
= PIPE_OK
;
406 struct svga_fragment_shader
*fs
= svga
->curr
.fs
;
407 struct svga_compile_key key
;
410 * SVGA_NEW_TEXTURE_BINDING
412 * SVGA_NEW_NEED_SWTNL
414 * SVGA_NEW_FRAME_BUFFER
415 * SVGA_NEW_DEPTH_STENCIL_ALPHA
418 ret
= make_fs_key(svga
, fs
, &key
);
422 variant
= svga_search_shader_key(&fs
->base
, &key
);
424 ret
= compile_fs(svga
, fs
, &key
, &variant
);
431 if (variant
!= svga
->state
.hw_draw
.fs
) {
432 ret
= svga_set_shader(svga
, SVGA3D_SHADERTYPE_PS
, variant
);
436 svga
->rebind
.flags
.fs
= FALSE
;
438 svga
->dirty
|= SVGA_NEW_FS_VARIANT
;
439 svga
->state
.hw_draw
.fs
= variant
;
445 struct svga_tracked_state svga_hw_fs
=
447 "fragment shader (hwtnl)",
451 SVGA_NEW_TEXTURE_BINDING
|
452 SVGA_NEW_NEED_SWTNL
|
454 SVGA_NEW_REDUCED_PRIMITIVE
|
456 SVGA_NEW_FRAME_BUFFER
|
457 SVGA_NEW_DEPTH_STENCIL_ALPHA
|