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"
40 #include "svga_hw_reg.h"
45 compare_fs_keys(const struct svga_fs_compile_key
*a
,
46 const struct svga_fs_compile_key
*b
)
48 unsigned keysize_a
= svga_fs_key_size( a
);
49 unsigned keysize_b
= svga_fs_key_size( b
);
51 if (keysize_a
!= keysize_b
) {
52 return (int)(keysize_a
- keysize_b
);
54 return memcmp( a
, b
, keysize_a
);
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
)
63 struct svga_shader_variant
*variant
= fs
->base
.variants
;
67 for ( ; variant
; variant
= variant
->next
) {
68 if (compare_fs_keys( key
, &variant
->key
.fkey
) == 0)
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.
83 static const struct tgsi_token
*
84 get_dummy_fragment_shader(void)
87 static const float color
[4] = { 1.0, 0.0, 0.0, 0.0 }; /* red */
89 static const float color
[4] = { 0.0, 0.0, 0.0, 0.0 }; /* black */
91 struct ureg_program
*ureg
;
92 const struct tgsi_token
*tokens
;
97 ureg
= ureg_create(TGSI_PROCESSOR_FRAGMENT
);
101 dst
= ureg_DECL_output(ureg
, TGSI_SEMANTIC_COLOR
, 0);
102 src
= ureg_DECL_immediate(ureg
, color
, 4);
103 ureg_MOV(ureg
, dst
, src
);
106 tokens
= ureg_get_tokens(ureg
, &num_tokens
);
115 * Replace the given shader's instruction with a simple constant-color
116 * shader. We use this when normal shader translation fails.
118 static struct svga_shader_variant
*
119 get_compiled_dummy_shader(struct svga_fragment_shader
*fs
,
120 const struct svga_fs_compile_key
*key
)
122 const struct tgsi_token
*dummy
= get_dummy_fragment_shader();
123 struct svga_shader_variant
*variant
;
129 FREE((void *) fs
->base
.tokens
);
130 fs
->base
.tokens
= dummy
;
132 variant
= svga_translate_fragment_program(fs
, key
);
138 * Translate TGSI shader into an svga shader variant.
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
)
146 struct svga_shader_variant
*variant
;
147 enum pipe_error ret
= PIPE_ERROR
;
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
);
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
);
174 ret
= svga_define_shader(svga
, SVGA3D_SHADERTYPE_PS
, variant
);
178 *out_variant
= variant
;
180 /* insert variants at head of linked list */
181 variant
->next
= fs
->base
.variants
;
182 fs
->base
.variants
= variant
;
188 svga_destroy_shader_variant(svga
, SVGA3D_SHADERTYPE_PS
, variant
);
194 /* SVGA_NEW_TEXTURE_BINDING
196 * SVGA_NEW_NEED_SWTNL
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
)
207 memset(key
, 0, sizeof *key
);
209 /* Only need fragment shader fixup for twoside lighting if doing
210 * hwtnl. Otherwise the draw module does the whole job for us.
214 if (!svga
->state
.sw
.need_swtnl
) {
217 key
->light_twoside
= svga
->curr
.rast
->templ
.light_twoside
;
218 key
->front_ccw
= svga
->curr
.rast
->templ
.front_ccw
;
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
->white_fragments
= 1;
239 /* XXX: want to limit this to the textures that the shader actually
242 * SVGA_NEW_TEXTURE_BINDING | SVGA_NEW_SAMPLER
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
;
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
;
261 key
->num_textures
= svga
->curr
.num_sampler_views
;
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
;
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.
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.
284 if (svga
->curr
.sampler
[i
]->compare_mode
!=
285 PIPE_TEX_COMPARE_R_TO_TEXTURE
) {
286 debug_warn_once("Unsupported shadow compare mode");
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");
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.
299 key
->tex
[i
].compare_mode
= svga
->curr
.sampler
[i
]->compare_mode
;
300 key
->tex
[i
].compare_func
= svga
->curr
.sampler
[i
]->compare_func
;
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
);
311 key
->sprite_origin_lower_left
= (svga
->curr
.rast
->templ
.sprite_coord_mode
312 == PIPE_SPRITE_COORD_LOWER_LEFT
);
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
;
325 * svga_reemit_fs_bindings - Reemit the fragment shader bindings
328 svga_reemit_fs_bindings(struct svga_context
*svga
)
332 assert(svga
->rebind
.fs
);
333 assert(svga_have_gb_objects(svga
));
335 if (!svga
->state
.hw_draw
.fs
)
338 ret
= SVGA3D_SetGBShader(svga
->swc
, SVGA3D_SHADERTYPE_PS
,
339 svga
->state
.hw_draw
.fs
->gb_shader
);
343 svga
->rebind
.fs
= FALSE
;
349 static enum pipe_error
350 emit_hw_fs(struct svga_context
*svga
, unsigned dirty
)
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
;
358 * SVGA_NEW_TEXTURE_BINDING
360 * SVGA_NEW_NEED_SWTNL
362 * SVGA_NEW_FRAME_BUFFER
364 ret
= make_fs_key( svga
, fs
, &key
);
368 variant
= search_fs_key( fs
, &key
);
370 ret
= compile_fs( svga
, fs
, &key
, &variant
);
377 if (variant
!= svga
->state
.hw_draw
.fs
) {
378 if (svga_have_gb_objects(svga
)) {
380 * Bind is necessary here only because pipebuffer_fenced may move
381 * the shader contents around....
383 ret
= SVGA3D_BindGBShader(svga
->swc
, variant
->gb_shader
);
387 ret
= SVGA3D_SetGBShader(svga
->swc
, SVGA3D_SHADERTYPE_PS
,
392 svga
->rebind
.fs
= FALSE
;
395 ret
= SVGA3D_SetShader(svga
->swc
, SVGA3D_SHADERTYPE_PS
, variant
->id
);
400 svga
->dirty
|= SVGA_NEW_FS_VARIANT
;
401 svga
->state
.hw_draw
.fs
= variant
;
407 struct svga_tracked_state svga_hw_fs
=
409 "fragment shader (hwtnl)",
411 SVGA_NEW_TEXTURE_BINDING
|
412 SVGA_NEW_NEED_SWTNL
|
415 SVGA_NEW_FRAME_BUFFER
|