Merge commit 'origin/7.8'
[mesa.git] / src / gallium / drivers / svga / svga_state_fs.c
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/u_math.h"
29 #include "util/u_bitmask.h"
30
31 #include "svga_context.h"
32 #include "svga_state.h"
33 #include "svga_cmd.h"
34 #include "svga_tgsi.h"
35
36 #include "svga_hw_reg.h"
37
38
39
40 static INLINE int compare_fs_keys( const struct svga_fs_compile_key *a,
41 const struct svga_fs_compile_key *b )
42 {
43 unsigned keysize_a = svga_fs_key_size( a );
44 unsigned keysize_b = svga_fs_key_size( b );
45
46 if (keysize_a != keysize_b) {
47 return (int)(keysize_a - keysize_b);
48 }
49 return memcmp( a, b, keysize_a );
50 }
51
52
53 static struct svga_shader_result *search_fs_key( struct svga_fragment_shader *fs,
54 const struct svga_fs_compile_key *key )
55 {
56 struct svga_shader_result *result = fs->base.results;
57
58 assert(key);
59
60 for ( ; result; result = result->next) {
61 if (compare_fs_keys( key, &result->key.fkey ) == 0)
62 return result;
63 }
64
65 return NULL;
66 }
67
68
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 )
73 {
74 struct svga_shader_result *result;
75 enum pipe_error ret = PIPE_ERROR;
76
77 result = svga_translate_fragment_program( fs, key );
78 if (result == NULL) {
79 ret = PIPE_ERROR_OUT_OF_MEMORY;
80 goto fail;
81 }
82
83 result->id = util_bitmask_add(svga->fs_bm);
84 if(result->id == UTIL_BITMASK_INVALID_INDEX) {
85 ret = PIPE_ERROR_OUT_OF_MEMORY;
86 goto fail;
87 }
88
89 ret = SVGA3D_DefineShader(svga->swc,
90 result->id,
91 SVGA3D_SHADERTYPE_PS,
92 result->tokens,
93 result->nr_tokens * sizeof result->tokens[0]);
94 if (ret)
95 goto fail;
96
97 *out_result = result;
98 result->next = fs->base.results;
99 fs->base.results = result;
100 return PIPE_OK;
101
102 fail:
103 if (result) {
104 if (result->id != UTIL_BITMASK_INVALID_INDEX)
105 util_bitmask_clear( svga->fs_bm, result->id );
106 svga_destroy_shader_result( result );
107 }
108 return ret;
109 }
110
111
112 /* SVGA_NEW_TEXTURE_BINDING
113 * SVGA_NEW_RAST
114 * SVGA_NEW_NEED_SWTNL
115 * SVGA_NEW_SAMPLER
116 */
117 static int make_fs_key( const struct svga_context *svga,
118 struct svga_fs_compile_key *key )
119 {
120 int i;
121 int idx = 0;
122
123 memset(key, 0, sizeof *key);
124
125 /* Only need fragment shader fixup for twoside lighting if doing
126 * hwtnl. Otherwise the draw module does the whole job for us.
127 *
128 * SVGA_NEW_SWTNL
129 */
130 if (!svga->state.sw.need_swtnl) {
131 /* SVGA_NEW_RAST
132 */
133 key->light_twoside = svga->curr.rast->templ.light_twoside;
134 key->front_cw = (svga->curr.rast->templ.front_winding ==
135 PIPE_WINDING_CW);
136 }
137
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
142 *
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.
146 *
147 * Ultimately, we could implement alphatest in the shader using
148 * texkil prior to overriding the outgoing fragment color.
149 *
150 * SVGA_NEW_BLEND
151 */
152 if (svga->curr.blend->need_white_fragments) {
153 key->white_fragments = 1;
154 }
155
156 /* XXX: want to limit this to the textures that the shader actually
157 * refers to.
158 *
159 * SVGA_NEW_TEXTURE_BINDING | SVGA_NEW_SAMPLER
160 */
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;
170 }
171 }
172 }
173 key->num_textures = svga->curr.num_sampler_views;
174
175 idx = 0;
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;
180 }
181 }
182
183 return 0;
184 }
185
186
187
188 static int emit_hw_fs( struct svga_context *svga,
189 unsigned dirty )
190 {
191 struct svga_shader_result *result = NULL;
192 unsigned id = SVGA3D_INVALID_ID;
193 int ret = 0;
194
195 struct svga_fragment_shader *fs = svga->curr.fs;
196 struct svga_fs_compile_key key;
197
198 /* SVGA_NEW_BLEND
199 * SVGA_NEW_TEXTURE_BINDING
200 * SVGA_NEW_RAST
201 * SVGA_NEW_NEED_SWTNL
202 * SVGA_NEW_SAMPLER
203 */
204 ret = make_fs_key( svga, &key );
205 if (ret)
206 return ret;
207
208 result = search_fs_key( fs, &key );
209 if (!result) {
210 ret = compile_fs( svga, fs, &key, &result );
211 if (ret)
212 return ret;
213 }
214
215 assert (result);
216 id = result->id;
217
218 assert(id != SVGA3D_INVALID_ID);
219
220 if (result != svga->state.hw_draw.fs) {
221 ret = SVGA3D_SetShader(svga->swc,
222 SVGA3D_SHADERTYPE_PS,
223 id );
224 if (ret)
225 return ret;
226
227 svga->dirty |= SVGA_NEW_FS_RESULT;
228 svga->state.hw_draw.fs = result;
229 }
230
231 return 0;
232 }
233
234 struct svga_tracked_state svga_hw_fs =
235 {
236 "fragment shader (hwtnl)",
237 (SVGA_NEW_FS |
238 SVGA_NEW_TEXTURE_BINDING |
239 SVGA_NEW_NEED_SWTNL |
240 SVGA_NEW_RAST |
241 SVGA_NEW_SAMPLER |
242 SVGA_NEW_BLEND),
243 emit_hw_fs
244 };
245
246
247