st/xorg: radial gradient shader
[mesa.git] / src / gallium / state_trackers / xorg / xorg_exa_tgsi.c
1 #include "xorg_exa_tgsi.h"
2
3 /*### stupidity defined in X11/extensions/XI.h */
4 #undef Absolute
5
6 #include "pipe/p_format.h"
7 #include "pipe/p_context.h"
8 #include "pipe/p_state.h"
9 #include "pipe/p_inlines.h"
10 #include "pipe/p_shader_tokens.h"
11
12 #include "util/u_memory.h"
13 #include "util/u_simple_shaders.h"
14
15 #include "tgsi/tgsi_ureg.h"
16
17 #include "cso_cache/cso_context.h"
18 #include "cso_cache/cso_hash.h"
19
20 /* Vertex shader:
21 * IN[0] = vertex pos
22 * IN[1] = src tex coord | solid fill color
23 * IN[2] = mask tex coord
24 * IN[3] = dst tex coord
25 * CONST[0] = (2/dst_width, 2/dst_height, 1, 1)
26 * CONST[1] = (-1, -1, 0, 0)
27 *
28 * OUT[0] = vertex pos
29 * OUT[1] = src tex coord | solid fill color
30 * OUT[2] = mask tex coord
31 * OUT[3] = dst tex coord
32 */
33
34 /* Fragment shader:
35 * SAMP[0] = src
36 * SAMP[1] = mask
37 * SAMP[2] = dst
38 * IN[0] = pos src | solid fill color
39 * IN[1] = pos mask
40 * IN[2] = pos dst
41 * CONST[0] = (0, 0, 0, 1)
42 *
43 * OUT[0] = color
44 */
45
46 struct xorg_shaders {
47 struct exa_context *exa;
48
49 struct cso_hash *vs_hash;
50 struct cso_hash *fs_hash;
51 };
52
53 static const char over_op[] =
54 "SUB TEMP[3], CONST[0].wwww, TEMP[1].wwww\n"
55 "MUL TEMP[3], TEMP[0], TEMP[3]\n"
56 "ADD TEMP[0], TEMP[3], TEMP[0]\n";
57
58
59 static INLINE void
60 create_preamble(struct ureg_program *ureg)
61 {
62 }
63
64
65 static INLINE void
66 src_in_mask(struct ureg_program *ureg,
67 struct ureg_dst dst,
68 struct ureg_src src,
69 struct ureg_src mask)
70 {
71 /* MUL dst, src, mask.wwww */
72 ureg_MUL(ureg, dst, src,
73 ureg_scalar(mask, TGSI_SWIZZLE_W));
74 }
75
76 static struct ureg_src
77 vs_normalize_coords(struct ureg_program *ureg, struct ureg_src coords,
78 struct ureg_src const0, struct ureg_src const1)
79 {
80 struct ureg_dst tmp = ureg_DECL_temporary(ureg);
81 struct ureg_src ret;
82 ureg_MUL(ureg, tmp, coords, const0);
83 ureg_ADD(ureg, tmp, ureg_src(tmp), const1);
84 ret = ureg_src(tmp);
85 ureg_release_temporary(ureg, tmp);
86 return ret;
87 }
88
89 static void
90 linear_gradient(struct ureg_program *ureg,
91 struct ureg_dst out,
92 struct ureg_src pos,
93 struct ureg_src sampler,
94 struct ureg_src coords,
95 struct ureg_src const0124,
96 struct ureg_src matrow0,
97 struct ureg_src matrow1,
98 struct ureg_src matrow2)
99 {
100 struct ureg_dst temp0 = ureg_DECL_temporary(ureg);
101 struct ureg_dst temp1 = ureg_DECL_temporary(ureg);
102 struct ureg_dst temp2 = ureg_DECL_temporary(ureg);
103 struct ureg_dst temp3 = ureg_DECL_temporary(ureg);
104 struct ureg_dst temp4 = ureg_DECL_temporary(ureg);
105 struct ureg_dst temp5 = ureg_DECL_temporary(ureg);
106
107 ureg_MOV(ureg,
108 ureg_writemask(temp0, TGSI_WRITEMASK_XY), pos);
109 ureg_MOV(ureg,
110 ureg_writemask(temp0, TGSI_WRITEMASK_Z),
111 ureg_scalar(const0124, TGSI_SWIZZLE_Y));
112
113 ureg_DP3(ureg, temp1, matrow0, ureg_src(temp0));
114 ureg_DP3(ureg, temp2, matrow1, ureg_src(temp0));
115 ureg_DP3(ureg, temp3, matrow2, ureg_src(temp0));
116 ureg_RCP(ureg, temp3, ureg_src(temp3));
117 ureg_MUL(ureg, temp1, ureg_src(temp1), ureg_src(temp3));
118 ureg_MUL(ureg, temp2, ureg_src(temp2), ureg_src(temp3));
119
120 ureg_MOV(ureg, ureg_writemask(temp4, TGSI_WRITEMASK_X),
121 ureg_src(temp1));
122 ureg_MOV(ureg, ureg_writemask(temp4, TGSI_WRITEMASK_Y),
123 ureg_src(temp2));
124
125 ureg_MUL(ureg, temp0,
126 ureg_scalar(coords, TGSI_SWIZZLE_Y),
127 ureg_scalar(ureg_src(temp4), TGSI_SWIZZLE_Y));
128 ureg_MAD(ureg, temp1,
129 ureg_scalar(coords, TGSI_SWIZZLE_X),
130 ureg_scalar(ureg_src(temp4), TGSI_SWIZZLE_X),
131 ureg_src(temp0));
132
133 ureg_MUL(ureg, temp2,
134 ureg_src(temp1),
135 ureg_scalar(coords, TGSI_SWIZZLE_Z));
136
137 ureg_TEX(ureg, out,
138 TGSI_TEXTURE_1D, ureg_src(temp2), sampler);
139
140 ureg_release_temporary(ureg, temp0);
141 ureg_release_temporary(ureg, temp1);
142 ureg_release_temporary(ureg, temp2);
143 ureg_release_temporary(ureg, temp3);
144 ureg_release_temporary(ureg, temp4);
145 ureg_release_temporary(ureg, temp5);
146 }
147
148
149 static void
150 radial_gradient(struct ureg_program *ureg,
151 struct ureg_dst out,
152 struct ureg_src pos,
153 struct ureg_src sampler,
154 struct ureg_src coords,
155 struct ureg_src const0124,
156 struct ureg_src matrow0,
157 struct ureg_src matrow1,
158 struct ureg_src matrow2)
159 {
160 struct ureg_dst temp0 = ureg_DECL_temporary(ureg);
161 struct ureg_dst temp1 = ureg_DECL_temporary(ureg);
162 struct ureg_dst temp2 = ureg_DECL_temporary(ureg);
163 struct ureg_dst temp3 = ureg_DECL_temporary(ureg);
164 struct ureg_dst temp4 = ureg_DECL_temporary(ureg);
165 struct ureg_dst temp5 = ureg_DECL_temporary(ureg);
166
167 ureg_MOV(ureg,
168 ureg_writemask(temp0, TGSI_WRITEMASK_XY),
169 pos);
170 ureg_MOV(ureg,
171 ureg_writemask(temp0, TGSI_WRITEMASK_Z),
172 ureg_scalar(const0124, TGSI_SWIZZLE_Y));
173
174 ureg_DP3(ureg, temp1, matrow0, ureg_src(temp0));
175 ureg_DP3(ureg, temp2, matrow1, ureg_src(temp0));
176 ureg_DP3(ureg, temp3, matrow2, ureg_src(temp0));
177 ureg_RCP(ureg, temp3, ureg_src(temp3));
178 ureg_MUL(ureg, temp1, ureg_src(temp1), ureg_src(temp3));
179 ureg_MUL(ureg, temp2, ureg_src(temp2), ureg_src(temp3));
180
181 ureg_MOV(ureg, ureg_writemask(temp5, TGSI_WRITEMASK_X),
182 ureg_src(temp1));
183 ureg_MOV(ureg, ureg_writemask(temp5, TGSI_WRITEMASK_Y),
184 ureg_src(temp2));
185
186 ureg_MUL(ureg, temp0, ureg_scalar(coords, TGSI_SWIZZLE_Y),
187 ureg_scalar(ureg_src(temp5), TGSI_SWIZZLE_Y));
188 ureg_MAD(ureg, temp1,
189 ureg_scalar(coords, TGSI_SWIZZLE_X),
190 ureg_scalar(ureg_src(temp5), TGSI_SWIZZLE_X),
191 ureg_src(temp0));
192 ureg_ADD(ureg, temp1,
193 ureg_src(temp1), ureg_src(temp1));
194 ureg_MUL(ureg, temp3,
195 ureg_scalar(ureg_src(temp5), TGSI_SWIZZLE_Y),
196 ureg_scalar(ureg_src(temp5), TGSI_SWIZZLE_Y));
197 ureg_MAD(ureg, temp4,
198 ureg_scalar(ureg_src(temp5), TGSI_SWIZZLE_X),
199 ureg_scalar(ureg_src(temp5), TGSI_SWIZZLE_X),
200 ureg_src(temp3));
201 ureg_MOV(ureg, temp4, ureg_negate(ureg_src(temp4)));
202 ureg_MUL(ureg, temp2,
203 ureg_scalar(coords, TGSI_SWIZZLE_Z),
204 ureg_src(temp4));
205 ureg_MUL(ureg, temp0,
206 ureg_scalar(const0124, TGSI_SWIZZLE_W),
207 ureg_src(temp2));
208 ureg_MUL(ureg, temp3,
209 ureg_src(temp1), ureg_src(temp1));
210 ureg_SUB(ureg, temp2,
211 ureg_src(temp3), ureg_src(temp0));
212 ureg_RSQ(ureg, temp2, ureg_abs(ureg_src(temp2)));
213 ureg_RCP(ureg, temp2, ureg_src(temp2));
214 ureg_SUB(ureg, temp1,
215 ureg_src(temp2), ureg_src(temp1));
216 ureg_ADD(ureg, temp0,
217 ureg_scalar(coords, TGSI_SWIZZLE_Z),
218 ureg_scalar(coords, TGSI_SWIZZLE_Z));
219 ureg_RCP(ureg, temp0, ureg_src(temp0));
220 ureg_MUL(ureg, temp2,
221 ureg_src(temp1), ureg_src(temp0));
222 ureg_TEX(ureg, out, TGSI_TEXTURE_1D,
223 ureg_src(temp2), sampler);
224
225 ureg_release_temporary(ureg, temp0);
226 ureg_release_temporary(ureg, temp1);
227 ureg_release_temporary(ureg, temp2);
228 ureg_release_temporary(ureg, temp3);
229 ureg_release_temporary(ureg, temp4);
230 ureg_release_temporary(ureg, temp5);
231 }
232
233 static void *
234 create_vs(struct pipe_context *pipe,
235 unsigned vs_traits)
236 {
237 struct ureg_program *ureg;
238 struct ureg_src src;
239 struct ureg_dst dst;
240 struct ureg_src const0, const1;
241 boolean is_fill = vs_traits & VS_FILL;
242 boolean is_composite = vs_traits & VS_COMPOSITE;
243 boolean has_mask = vs_traits & VS_MASK;
244
245 ureg = ureg_create(TGSI_PROCESSOR_VERTEX);
246 if (ureg == NULL)
247 return 0;
248
249 const0 = ureg_DECL_constant(ureg);
250 const1 = ureg_DECL_constant(ureg);
251
252 /* it has to be either a fill or a composite op */
253 debug_assert(is_fill ^ is_composite);
254
255 src = ureg_DECL_vs_input(ureg,
256 TGSI_SEMANTIC_POSITION, 0);
257 dst = ureg_DECL_output(ureg, TGSI_SEMANTIC_POSITION, 0);
258 src = vs_normalize_coords(ureg, src,
259 const0, const1);
260 ureg_MOV(ureg, dst, src);
261
262
263 if (is_composite) {
264 src = ureg_DECL_vs_input(ureg,
265 TGSI_SEMANTIC_GENERIC, 1);
266 dst = ureg_DECL_output(ureg, TGSI_SEMANTIC_GENERIC, 1);
267 ureg_MOV(ureg, dst, src);
268 }
269 if (is_fill) {
270 src = ureg_DECL_vs_input(ureg,
271 TGSI_SEMANTIC_COLOR, 1);
272 dst = ureg_DECL_output(ureg, TGSI_SEMANTIC_COLOR, 1);
273 ureg_MOV(ureg, dst, src);
274 }
275
276 if (has_mask) {
277 src = ureg_DECL_vs_input(ureg,
278 TGSI_SEMANTIC_GENERIC, 2);
279 dst = ureg_DECL_output(ureg, TGSI_SEMANTIC_POSITION, 2);
280 ureg_MOV(ureg, dst, src);
281 }
282
283 ureg_END(ureg);
284
285 return ureg_create_shader_and_destroy(ureg, pipe);
286 }
287
288 static void *
289 create_fs(struct pipe_context *pipe,
290 unsigned fs_traits)
291 {
292 struct ureg_program *ureg;
293 struct ureg_src /*dst_sampler,*/ src_sampler, mask_sampler;
294 struct ureg_src /*dst_pos,*/ src_pos, mask_pos;
295 struct ureg_src src, mask;
296 struct ureg_dst out;
297 boolean is_fill = fs_traits & VS_FILL;
298 boolean is_composite = fs_traits & VS_COMPOSITE;
299 /*boolean has_mask = fs_traits & VS_MASK;*/
300
301 ureg = ureg_create(TGSI_PROCESSOR_FRAGMENT);
302 if (ureg == NULL)
303 return 0;
304
305 /* it has to be either a fill or a composite op */
306 debug_assert(is_fill ^ is_composite);
307
308 out = ureg_DECL_output(ureg,
309 TGSI_SEMANTIC_COLOR,
310 0);
311
312 if (is_composite) {
313 src_sampler = ureg_DECL_sampler(ureg, 0);
314 src_pos = ureg_DECL_fs_input(ureg,
315 TGSI_SEMANTIC_POSITION,
316 0,
317 TGSI_INTERPOLATE_PERSPECTIVE);
318 }
319 if (is_fill) {
320 src_pos = ureg_DECL_fs_input(ureg,
321 TGSI_SEMANTIC_COLOR,
322 0,
323 TGSI_INTERPOLATE_PERSPECTIVE);
324 }
325
326 if ((fs_traits & FS_MASK)) {
327 mask_sampler = ureg_DECL_sampler(ureg, 1);
328 mask_pos = ureg_DECL_fs_input(ureg,
329 TGSI_SEMANTIC_POSITION,
330 1,
331 TGSI_INTERPOLATE_PERSPECTIVE);
332 }
333
334 #if 0 /* unused right now */
335 dst_sampler = ureg_DECL_sampler(ureg, 2);
336 dst_pos = ureg_DECL_fs_input(ureg,
337 TGSI_SEMANTIC_POSITION,
338 2,
339 TGSI_INTERPOLATE_PERSPECTIVE);
340 #endif
341
342 if ((fs_traits & FS_MASK)) {
343 ureg_TEX(ureg, ureg_dst(mask),
344 TGSI_TEXTURE_2D, mask_pos, mask_sampler);
345 /* src IN mask */
346 src_in_mask(ureg, out, src, mask);
347 } else {
348 ureg_TEX(ureg, out,
349 TGSI_TEXTURE_2D, src_pos, src_sampler);
350 }
351
352 ureg_END(ureg);
353
354 return ureg_create_shader_and_destroy(ureg, pipe);
355 }
356
357 struct xorg_shaders * xorg_shaders_create(struct exa_context *exa)
358 {
359 struct xorg_shaders *sc = CALLOC_STRUCT(xorg_shaders);
360
361 sc->exa = exa;
362 sc->vs_hash = cso_hash_create();
363 sc->fs_hash = cso_hash_create();
364
365 return sc;
366 }
367
368 static void
369 cache_destroy(struct cso_context *cso,
370 struct cso_hash *hash,
371 unsigned processor)
372 {
373 struct cso_hash_iter iter = cso_hash_first_node(hash);
374 while (!cso_hash_iter_is_null(iter)) {
375 void *shader = (void *)cso_hash_iter_data(iter);
376 if (processor == PIPE_SHADER_FRAGMENT) {
377 cso_delete_fragment_shader(cso, shader);
378 } else if (processor == PIPE_SHADER_VERTEX) {
379 cso_delete_vertex_shader(cso, shader);
380 }
381 iter = cso_hash_erase(hash, iter);
382 }
383 cso_hash_delete(hash);
384 }
385
386 void xorg_shaders_destroy(struct xorg_shaders *sc)
387 {
388 cache_destroy(sc->exa->cso, sc->vs_hash,
389 PIPE_SHADER_VERTEX);
390 cache_destroy(sc->exa->cso, sc->fs_hash,
391 PIPE_SHADER_FRAGMENT);
392
393 free(sc);
394 }
395
396 static INLINE void *
397 shader_from_cache(struct pipe_context *pipe,
398 unsigned type,
399 struct cso_hash *hash,
400 unsigned key)
401 {
402 void *shader = 0;
403
404 struct cso_hash_iter iter = cso_hash_find(hash, key);
405
406 if (cso_hash_iter_is_null(iter)) {
407 if (type == PIPE_SHADER_VERTEX)
408 shader = create_vs(pipe, key);
409 else
410 shader = create_fs(pipe, key);
411 cso_hash_insert(hash, key, shader);
412 } else
413 shader = (void *)cso_hash_iter_data(iter);
414
415 return shader;
416 }
417
418 struct xorg_shader xorg_shaders_get(struct xorg_shaders *sc,
419 unsigned vs_traits,
420 unsigned fs_traits)
421 {
422 struct xorg_shader shader = {0};
423 void *vs, *fs;
424
425 vs = shader_from_cache(sc->exa->ctx, PIPE_SHADER_VERTEX,
426 sc->vs_hash, vs_traits);
427 fs = shader_from_cache(sc->exa->ctx, PIPE_SHADER_FRAGMENT,
428 sc->fs_hash, fs_traits);
429
430 debug_assert(vs && fs);
431 if (!vs || !fs)
432 return shader;
433
434 shader.vs = vs;
435 shader.fs = fs;
436
437 return shader;
438 }