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