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