272d1dd14e3c3cf809b24c1329a3ffb7535c51d4
[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 "pipe/p_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 = svga_fs_key_size( a );
44 return memcmp( a, b, keysize );
45 }
46
47
48 static struct svga_shader_result *search_fs_key( struct svga_fragment_shader *fs,
49 const struct svga_fs_compile_key *key )
50 {
51 struct svga_shader_result *result = fs->base.results;
52
53 assert(key);
54
55 for ( ; result; result = result->next) {
56 if (compare_fs_keys( key, &result->key.fkey ) == 0)
57 return result;
58 }
59
60 return NULL;
61 }
62
63
64 static enum pipe_error compile_fs( struct svga_context *svga,
65 struct svga_fragment_shader *fs,
66 const struct svga_fs_compile_key *key,
67 struct svga_shader_result **out_result )
68 {
69 struct svga_shader_result *result;
70 enum pipe_error ret;
71
72 result = svga_translate_fragment_program( fs, key );
73 if (result == NULL) {
74 ret = PIPE_ERROR_OUT_OF_MEMORY;
75 goto fail;
76 }
77
78 result->id = util_bitmask_add(svga->fs_bm);
79 if(result->id == UTIL_BITMASK_INVALID_INDEX)
80 goto fail;
81
82 ret = SVGA3D_DefineShader(svga->swc,
83 result->id,
84 SVGA3D_SHADERTYPE_PS,
85 result->tokens,
86 result->nr_tokens * sizeof result->tokens[0]);
87 if (ret)
88 goto fail;
89
90 *out_result = result;
91 result->next = fs->base.results;
92 fs->base.results = result;
93 return PIPE_OK;
94
95 fail:
96 if (result) {
97 if (result->id != UTIL_BITMASK_INVALID_INDEX)
98 util_bitmask_clear( svga->fs_bm, result->id );
99 svga_destroy_shader_result( result );
100 }
101 return ret;
102 }
103
104 /* The blend workaround for simulating logicop xor behaviour requires
105 * that the incoming fragment color be white. This change achieves
106 * that by hooking up a hard-wired fragment shader that just emits
107 * color 1,1,1,1
108 *
109 * This is a slightly incomplete solution as it assumes that the
110 * actual bound shader has no other effects beyond generating a
111 * fragment color. In particular shaders containing TEXKIL and/or
112 * depth-write will not have the correct behaviour, nor will those
113 * expecting to use alphatest.
114 *
115 * These are avoidable issues, but they are not much worse than the
116 * unavoidable ones associated with this technique, so it's not clear
117 * how much effort should be expended trying to resolve them - the
118 * ultimate result will still not be correct in most cases.
119 *
120 * Shader below was generated with:
121 * SVGA_DEBUG=tgsi ./mesa/progs/fp/fp-tri white.txt
122 */
123 static int emit_white_fs( struct svga_context *svga )
124 {
125 int ret = PIPE_ERROR;
126
127 /* ps_3_0
128 * def c0, 1.000000, 0.000000, 0.000000, 1.000000
129 * mov oC0, c0.x
130 * end
131 */
132 static const unsigned white_tokens[] = {
133 0xffff0300,
134 0x05000051,
135 0xa00f0000,
136 0x3f800000,
137 0x00000000,
138 0x00000000,
139 0x3f800000,
140 0x02000001,
141 0x800f0800,
142 0xa0000000,
143 0x0000ffff,
144 };
145
146 assert(SVGA3D_INVALID_ID == UTIL_BITMASK_INVALID_INDEX);
147 svga->state.white_fs_id = util_bitmask_add(svga->fs_bm);
148 if(svga->state.white_fs_id == SVGA3D_INVALID_ID)
149 goto no_fs_id;
150
151 ret = SVGA3D_DefineShader(svga->swc,
152 svga->state.white_fs_id,
153 SVGA3D_SHADERTYPE_PS,
154 white_tokens,
155 sizeof(white_tokens));
156 if (ret)
157 goto no_definition;
158
159 return 0;
160
161 no_definition:
162 util_bitmask_clear(svga->fs_bm, svga->state.white_fs_id);
163 svga->state.white_fs_id = SVGA3D_INVALID_ID;
164 no_fs_id:
165 return ret;
166 }
167
168
169 /* SVGA_NEW_TEXTURE_BINDING
170 * SVGA_NEW_RAST
171 * SVGA_NEW_NEED_SWTNL
172 * SVGA_NEW_SAMPLER
173 */
174 static int make_fs_key( const struct svga_context *svga,
175 struct svga_fs_compile_key *key )
176 {
177 int i;
178 int idx = 0;
179
180 memset(key, 0, sizeof *key);
181
182 /* Only need fragment shader fixup for twoside lighting if doing
183 * hwtnl. Otherwise the draw module does the whole job for us.
184 *
185 * SVGA_NEW_SWTNL
186 */
187 if (!svga->state.sw.need_swtnl) {
188 /* SVGA_NEW_RAST
189 */
190 key->light_twoside = svga->curr.rast->templ.light_twoside;
191 key->front_cw = (svga->curr.rast->templ.front_winding ==
192 PIPE_WINDING_CW);
193 }
194
195
196 /* XXX: want to limit this to the textures that the shader actually
197 * refers to.
198 *
199 * SVGA_NEW_TEXTURE_BINDING | SVGA_NEW_SAMPLER
200 */
201 for (i = 0; i < svga->curr.num_textures; i++) {
202 if (svga->curr.texture[i]) {
203 assert(svga->curr.sampler[i]);
204 key->tex[i].texture_target = svga->curr.texture[i]->target;
205 if (!svga->curr.sampler[i]->normalized_coords) {
206 key->tex[i].width_height_idx = idx++;
207 key->tex[i].unnormalized = TRUE;
208 ++key->num_unnormalized_coords;
209 }
210 }
211 }
212 key->num_textures = svga->curr.num_textures;
213
214 idx = 0;
215 for (i = 0; i < svga->curr.num_samplers; ++i) {
216 if (svga->curr.sampler[i]) {
217 key->tex[i].compare_mode = svga->curr.sampler[i]->compare_mode;
218 key->tex[i].compare_func = svga->curr.sampler[i]->compare_func;
219 }
220 }
221
222 return 0;
223 }
224
225
226
227 static int emit_hw_fs( struct svga_context *svga,
228 unsigned dirty )
229 {
230 struct svga_shader_result *result = NULL;
231 unsigned id = SVGA3D_INVALID_ID;
232 int ret = 0;
233
234 /* SVGA_NEW_BLEND
235 */
236 if (svga->curr.blend->need_white_fragments) {
237 if (svga->state.white_fs_id == SVGA3D_INVALID_ID) {
238 ret = emit_white_fs( svga );
239 if (ret)
240 return ret;
241 }
242 id = svga->state.white_fs_id;
243 }
244 else {
245 struct svga_fragment_shader *fs = svga->curr.fs;
246 struct svga_fs_compile_key key;
247
248 /* SVGA_NEW_TEXTURE_BINDING
249 * SVGA_NEW_RAST
250 * SVGA_NEW_NEED_SWTNL
251 * SVGA_NEW_SAMPLER
252 */
253 ret = make_fs_key( svga, &key );
254 if (ret)
255 return ret;
256
257 result = search_fs_key( fs, &key );
258 if (!result) {
259 ret = compile_fs( svga, fs, &key, &result );
260 if (ret)
261 return ret;
262 }
263
264 assert (result);
265 id = result->id;
266 }
267
268 assert(id != SVGA3D_INVALID_ID);
269
270 if (result != svga->state.hw_draw.fs) {
271 ret = SVGA3D_SetShader(svga->swc,
272 SVGA3D_SHADERTYPE_PS,
273 id );
274 if (ret)
275 return ret;
276
277 svga->dirty |= SVGA_NEW_FS_RESULT;
278 svga->state.hw_draw.fs = result;
279 }
280
281 return 0;
282 }
283
284 struct svga_tracked_state svga_hw_fs =
285 {
286 "fragment shader (hwtnl)",
287 (SVGA_NEW_FS |
288 SVGA_NEW_TEXTURE_BINDING |
289 SVGA_NEW_NEED_SWTNL |
290 SVGA_NEW_RAST |
291 SVGA_NEW_SAMPLER |
292 SVGA_NEW_BLEND),
293 emit_hw_fs
294 };
295
296
297