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_bitmask.h"
31 #include "svga_context.h"
32 #include "svga_state.h"
34 #include "svga_tgsi.h"
36 #include "svga_hw_reg.h"
40 static INLINE
int compare_fs_keys( const struct svga_fs_compile_key
*a
,
41 const struct svga_fs_compile_key
*b
)
43 unsigned keysize_a
= svga_fs_key_size( a
);
44 unsigned keysize_b
= svga_fs_key_size( b
);
46 if (keysize_a
!= keysize_b
) {
47 return (int)(keysize_a
- keysize_b
);
49 return memcmp( a
, b
, keysize_a
);
53 static struct svga_shader_result
*search_fs_key( struct svga_fragment_shader
*fs
,
54 const struct svga_fs_compile_key
*key
)
56 struct svga_shader_result
*result
= fs
->base
.results
;
60 for ( ; result
; result
= result
->next
) {
61 if (compare_fs_keys( key
, &result
->key
.fkey
) == 0)
69 static enum pipe_error
compile_fs( struct svga_context
*svga
,
70 struct svga_fragment_shader
*fs
,
71 const struct svga_fs_compile_key
*key
,
72 struct svga_shader_result
**out_result
)
74 struct svga_shader_result
*result
;
75 enum pipe_error ret
= PIPE_ERROR
;
77 result
= svga_translate_fragment_program( fs
, key
);
79 ret
= PIPE_ERROR_OUT_OF_MEMORY
;
83 result
->id
= util_bitmask_add(svga
->fs_bm
);
84 if(result
->id
== UTIL_BITMASK_INVALID_INDEX
) {
85 ret
= PIPE_ERROR_OUT_OF_MEMORY
;
89 ret
= SVGA3D_DefineShader(svga
->swc
,
93 result
->nr_tokens
* sizeof result
->tokens
[0]);
98 result
->next
= fs
->base
.results
;
99 fs
->base
.results
= result
;
104 if (result
->id
!= UTIL_BITMASK_INVALID_INDEX
)
105 util_bitmask_clear( svga
->fs_bm
, result
->id
);
106 svga_destroy_shader_result( result
);
112 /* SVGA_NEW_TEXTURE_BINDING
114 * SVGA_NEW_NEED_SWTNL
117 static int make_fs_key( const struct svga_context
*svga
,
118 struct svga_fs_compile_key
*key
)
123 memset(key
, 0, sizeof *key
);
125 /* Only need fragment shader fixup for twoside lighting if doing
126 * hwtnl. Otherwise the draw module does the whole job for us.
130 if (!svga
->state
.sw
.need_swtnl
) {
133 key
->light_twoside
= svga
->curr
.rast
->templ
.light_twoside
;
134 key
->front_cw
= (svga
->curr
.rast
->templ
.front_winding
==
138 /* The blend workaround for simulating logicop xor behaviour
139 * requires that the incoming fragment color be white. This change
140 * achieves that by creating a varient of the current fragment
141 * shader that overrides all output colors with 1,1,1,1
143 * This will work for most shaders, including those containing
144 * TEXKIL and/or depth-write. However, it will break on the
145 * combination of xor-logicop plus alphatest.
147 * Ultimately, we could implement alphatest in the shader using
148 * texkil prior to overriding the outgoing fragment color.
152 if (svga
->curr
.blend
->need_white_fragments
) {
153 key
->white_fragments
= 1;
156 /* XXX: want to limit this to the textures that the shader actually
159 * SVGA_NEW_TEXTURE_BINDING | SVGA_NEW_SAMPLER
161 for (i
= 0; i
< svga
->curr
.num_sampler_views
; i
++) {
162 if (svga
->curr
.sampler_views
[i
]) {
163 assert(svga
->curr
.sampler
[i
]);
164 assert(svga
->curr
.sampler_views
[i
]->texture
);
165 key
->tex
[i
].texture_target
= svga
->curr
.sampler_views
[i
]->texture
->target
;
166 if (!svga
->curr
.sampler
[i
]->normalized_coords
) {
167 key
->tex
[i
].width_height_idx
= idx
++;
168 key
->tex
[i
].unnormalized
= TRUE
;
169 ++key
->num_unnormalized_coords
;
173 key
->num_textures
= svga
->curr
.num_sampler_views
;
176 for (i
= 0; i
< svga
->curr
.num_samplers
; ++i
) {
177 if (svga
->curr
.sampler
[i
]) {
178 key
->tex
[i
].compare_mode
= svga
->curr
.sampler
[i
]->compare_mode
;
179 key
->tex
[i
].compare_func
= svga
->curr
.sampler
[i
]->compare_func
;
188 static int emit_hw_fs( struct svga_context
*svga
,
191 struct svga_shader_result
*result
= NULL
;
192 unsigned id
= SVGA3D_INVALID_ID
;
195 struct svga_fragment_shader
*fs
= svga
->curr
.fs
;
196 struct svga_fs_compile_key key
;
199 * SVGA_NEW_TEXTURE_BINDING
201 * SVGA_NEW_NEED_SWTNL
204 ret
= make_fs_key( svga
, &key
);
208 result
= search_fs_key( fs
, &key
);
210 ret
= compile_fs( svga
, fs
, &key
, &result
);
218 assert(id
!= SVGA3D_INVALID_ID
);
220 if (result
!= svga
->state
.hw_draw
.fs
) {
221 ret
= SVGA3D_SetShader(svga
->swc
,
222 SVGA3D_SHADERTYPE_PS
,
227 svga
->dirty
|= SVGA_NEW_FS_RESULT
;
228 svga
->state
.hw_draw
.fs
= result
;
234 struct svga_tracked_state svga_hw_fs
=
236 "fragment shader (hwtnl)",
238 SVGA_NEW_TEXTURE_BINDING
|
239 SVGA_NEW_NEED_SWTNL
|