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