Debian Codegraph @

tests/interactive/test-cogl-shader-arbfp.c

raw
1 #include <clutter/clutter.h>
2
3 #include <errno.h>
4 #include <stdlib.h>
5 #include <glib.h>
6 #include <gmodule.h>
7
8 typedef struct
9 {
10 char *name;
11 char *source;
12 } ShaderSource;
13
14 static ShaderSource shaders[]=
15 {
16 /*{"brightness-contrast",
17 FRAGMENT_SHADER_VARS
18 "uniform float brightness, contrast;"
19 FRAGMENT_SHADER_BEGIN
20 " color.rgb = (color.rgb - vec3(0.5, 0.5, 0.5)) * contrast + "
21 "vec3 (brightness + 0.5, brightness + 0.5, brightness + 0.5);"
22 FRAGMENT_SHADER_END
23 },*/
24 {"brightness-contrast",
25 "!!ARBfp1.0\n"
26 "PARAM bc = program.local[0];"
27 "TEMP color;"
28 "TEMP color2;"
29 "TEX color.rgba, fragment.texcoord[0], texture[0], 2D;"
30 "SUB color.rgb, color, 0.5;"
31 "MUL color2, color, bc.w;"
32 "ADD color.rgb, color2, bc.z;"
33 "MOV result.color, color;"
34 "END"
35 },
36
37 /*{"box-blur",
38 FRAGMENT_SHADER_VARS
39
40 "vec4 get_rgba_rel(sampler2D tex, float dx, float dy)"
41 "{"
42 " return texture2D (tex, " TEX_COORD ".st "
43 " + vec2(dx, dy) * 2.0);"
44 "}"
45
46 FRAGMENT_SHADER_BEGIN
47 " float count = 1.0;"
48 " color += get_rgba_rel (tex, -x_step, -y_step); count++;"
49 " color += get_rgba_rel (tex, -x_step, 0.0); count++;"
50 " color += get_rgba_rel (tex, -x_step, y_step); count++;"
51 " color += get_rgba_rel (tex, 0.0, -y_step); count++;"
52 " color += get_rgba_rel (tex, 0.0, 0.0); count++;"
53 " color += get_rgba_rel (tex, 0.0, y_step); count++;"
54 " color += get_rgba_rel (tex, x_step, -y_step); count++;"
55 " color += get_rgba_rel (tex, x_step, 0.0); count++;"
56 " color += get_rgba_rel (tex, x_step, y_step); count++;"
57 " color = color / count;"
58 FRAGMENT_SHADER_END
59 },*/
60 {"box-blur",
61 "!!ARBfp1.0\n"
62 "PARAM params = program.local[0];"
63 "TEMP accum;"
64 "TEMP color;"
65 "TEMP coord;"
66 "TEMP step;"
67
68 "MUL step, params, 2.0;"
69
70 "SUB coord, fragment.texcoord[0], step;"
71 "TEX color.rgba, coord, texture[0], 2D;"
72 "MOV accum, color;"
73
74 "MOV coord, fragment.texcoord[0];"
75 "SUB coord.x, coord.x, step.x;"
76 "TEX color.rgba, coord, texture[0], 2D;"
77 "ADD accum, accum, color;"
78
79 "MOV coord, fragment.texcoord[0];"
80 "SUB coord.x, coord.x, step.x;"
81 "ADD coord.y, coord.y, step.y;"
82 "TEX color.rgba, coord, texture[0], 2D;"
83 "ADD accum, accum, color;"
84
85 "MOV coord, fragment.texcoord[0];"
86 "SUB coord.y, coord.y, step.y;"
87 "TEX color.rgba, coord, texture[0], 2D;"
88 "ADD accum, accum, color;"
89
90 "MOV coord, fragment.texcoord[0];"
91 "TEX color.rgba, coord, texture[0], 2D;"
92 "ADD accum, accum, color;"
93
94 "MOV coord, fragment.texcoord[0];"
95 "ADD coord.y, coord.y, step.y;"
96 "TEX color.rgba, coord, texture[0], 2D;"
97 "ADD accum, accum, color;"
98
99 "MOV coord, fragment.texcoord[0];"
100 "ADD coord.x, coord.x, step.x;"
101 "SUB coord.y, coord.y, step.y;"
102 "TEX color.rgba, coord, texture[0], 2D;"
103 "ADD accum, accum, color;"
104
105 "MOV coord, fragment.texcoord[0];"
106 "ADD coord.x, coord.x, step.x;"
107 "TEX color.rgba, coord, texture[0], 2D;"
108 "ADD accum, accum, color;"
109
110 "MOV coord, fragment.texcoord[0];"
111 "ADD coord.x, coord.x, step.x;"
112 "ADD coord.y, coord.y, step.y;"
113 "TEX color.rgba, coord, texture[0], 2D;"
114 "ADD accum, accum, color;"
115
116 "MUL color, accum, 0.11111111;"
117 "MOV result.color, color;"
118 "END"
119 },
120
121 /*{"invert",
122 FRAGMENT_SHADER_VARS
123 FRAGMENT_SHADER_BEGIN
124 " color.rgb = vec3(1.0, 1.0, 1.0) - color.rgb;\n"
125 FRAGMENT_SHADER_END
126 },*/
127 {"invert",
128 "!!ARBfp1.0\n"
129 "TEMP color;"
130 "TEX color.rgba, fragment.texcoord[0], texture[0], 2D;"
131 "ADD color.rgb, 1.0, -color;"
132 "MOV result.color, color;"
133 "END"
134 },
135
136 /*{"gray",
137 FRAGMENT_SHADER_VARS
138 FRAGMENT_SHADER_BEGIN
139 " float avg = (color.r + color.g + color.b) / 3.0;"
140 " color.r = avg;"
141 " color.g = avg;"
142 " color.b = avg;"
143 FRAGMENT_SHADER_END
144 },*/
145 {"gray",
146 "!!ARBfp1.0\n"
147 "TEMP color;"
148 "TEMP grey;"
149 "TEX color.rgba, fragment.texcoord[0], texture[0], 2D;"
150 "ADD grey, color.r, color.g;"
151 "ADD grey, grey, color.b;"
152 "MUL grey, grey, 0.33333333;"
153 "MOV color.rgb, grey;"
154 "MOV result.color, color;"
155 "END"
156 },
157
158 /* {"combined-mirror",
159 FRAGMENT_SHADER_VARS
160 FRAGMENT_SHADER_BEGIN
161 " vec4 colorB = texture2D (tex, vec2(" TEX_COORD ".ts));"
162 " float avg = (color.r + color.g + color.b) / 3.0;"
163 " color.r = avg;"
164 " color.g = avg;"
165 " color.b = avg;"
166 " color = (color + colorB)/2.0;"
167 FRAGMENT_SHADER_END
168 },*/
169 {"combined-mirror",
170 "!!ARBfp1.0\n"
171 "TEMP color1;"
172 "TEMP color2;"
173 "TEMP coord;"
174 "MOV coord.x, fragment.texcoord[0].y;"
175 "MOV coord.y, fragment.texcoord[0].x;"
176 "TEX color1.rgba, fragment.texcoord[0], texture[0], 2D;"
177 "TEX color2.rgba, coord, texture[0], 2D;"
178 "MUL color1, color1, 0.5;"
179 "MUL color2, color2, 0.5;"
180 "ADD result.color, color1, color2;"
181 "END"
182 },
183
184 /* {"edge-detect",
185 FRAGMENT_SHADER_VARS
186 "float get_avg_rel(sampler2D texB, float dx, float dy)"
187 "{"
188 " vec4 colorB = texture2D (texB, " TEX_COORD ".st + vec2(dx, dy));"
189 " return (colorB.r + colorB.g + colorB.b) / 3.0;"
190 "}"
191 FRAGMENT_SHADER_BEGIN
192 " mat3 sobel_h = mat3( 1.0, 2.0, 1.0,"
193 " 0.0, 0.0, 0.0,"
194 " -1.0, -2.0, -1.0);"
195 " mat3 sobel_v = mat3( 1.0, 0.0, -1.0,"
196 " 2.0, 0.0, -2.0,"
197 " 1.0, 0.0, -1.0);"
198 " mat3 map = mat3( get_avg_rel(tex, -x_step, -y_step),"
199 " get_avg_rel(tex, -x_step, 0.0),"
200 " get_avg_rel(tex, -x_step, y_step),"
201 " get_avg_rel(tex, 0.0, -y_step),"
202 " get_avg_rel(tex, 0.0, 0.0),"
203 " get_avg_rel(tex, 0.0, y_step),"
204 " get_avg_rel(tex, x_step, -y_step),"
205 " get_avg_rel(tex, x_step, 0.0),"
206 " get_avg_rel(tex, x_step, y_step) );"
207 " mat3 gh = sobel_h * map;"
208 " mat3 gv = map * sobel_v;"
209 " float avgh = (gh[0][0] + gh[0][1] + gh[0][2] +"
210 " gh[1][0] + gh[1][1] + gh[1][2] +"
211 " gh[2][0] + gh[2][1] + gh[2][2]) / 18.0 + 0.5;"
212 " float avgv = (gv[0][0] + gv[0][1] + gv[0][2] +"
213 " gv[1][0] + gv[1][1] + gv[1][2] +"
214 " gv[2][0] + gv[2][1] + gv[2][2]) / 18.0 + 0.5;"
215 " float avg = (avgh + avgv) / 2.0;"
216 " color.r = avg * color.r;"
217 " color.g = avg * color.g;"
218 " color.b = avg * color.b;"
219 FRAGMENT_SHADER_END
220 },*/
221
222 /* Don't really fancy doing this one in assembly :) */
223 };
224
225 static CoglHandle redhand;
226 static CoglMaterial *material;
227 static unsigned int timeout_id = 0;
228 static int shader_no = 0;
229
230 static void
231 paint_cb (ClutterActor *actor)
232 {
233 int stage_width = clutter_actor_get_width (actor);
234 int stage_height = clutter_actor_get_height (actor);
235 int image_width = cogl_texture_get_width (redhand);
236 int image_height = cogl_texture_get_height (redhand);
237
238 cogl_set_source (material);
239 cogl_rectangle (stage_width/2.0f - image_width/2.0f,
240 stage_height/2.0f - image_height/2.0f,
241 stage_width/2.0f + image_width/2.0f,
242 stage_height/2.0f + image_height/2.0f);
243 }
244
245 static void
246 set_shader_num (int new_no)
247 {
248 CoglHandle shader;
249 CoglHandle program;
250 int image_width = cogl_texture_get_width (redhand);
251 int image_height = cogl_texture_get_height (redhand);
252 float param0[4];
253 int uniform_no;
254
255 g_print ("setting shaders[%i] named '%s'\n",
256 new_no,
257 shaders[new_no].name);
258
259 shader = cogl_create_shader (COGL_SHADER_TYPE_FRAGMENT);
260 cogl_shader_source (shader, shaders[new_no].source);
261 cogl_shader_compile (shader);
262
263 program = cogl_create_program ();
264 cogl_program_attach_shader (program, shader);
265 cogl_handle_unref (shader);
266 cogl_program_link (program);
267
268 param0[0] = 1.0f/image_width; /* texel x step delta */
269 param0[1] = 1.0f/image_height; /* texel y step delta */
270 param0[2] = 0.4; /* brightness */
271 param0[3] = -1.9; /* contrast */
272
273 uniform_no = cogl_program_get_uniform_location (program, "program.local[0]");
274 cogl_program_set_uniform_float (program, uniform_no, 4, 1, param0);
275
276 cogl_material_set_user_program (material, program);
277 cogl_handle_unref (program);
278
279 shader_no = new_no;
280 }
281
282 static gboolean
283 button_release_cb (ClutterActor *actor,
284 ClutterEvent *event,
285 void *data)
286 {
287 int new_no;
288
289 /* Stop the automatic cycling if the user want to manually control
290 * which shader to display */
291 if (timeout_id)
292 {
293 g_source_remove (timeout_id);
294 timeout_id = 0;
295 }
296
297 if (event->button.button == 1)
298 {
299 new_no = shader_no - 1;
300 if (new_no < 0)
301 new_no = G_N_ELEMENTS (shaders) - 1;
302 }
303 else
304 {
305 new_no = shader_no + 1;
306 if (new_no >= G_N_ELEMENTS (shaders))
307 new_no = 0;
308 }
309
310 set_shader_num (new_no);
311
312 return CLUTTER_EVENT_STOP;
313 }
314
315 static gboolean
316 key_release_cb (ClutterActor *actor,
317 ClutterEvent *event,
318 gpointer user_data)
319 {
320 guint keysym = clutter_event_get_key_symbol (event);
321 ClutterModifierType mods = clutter_event_get_state (event);
322
323 if (keysym == CLUTTER_KEY_q ||
324 ((mods & CLUTTER_SHIFT_MASK) && keysym == CLUTTER_KEY_q))
325 clutter_main_quit ();
326
327 return CLUTTER_EVENT_STOP;
328 }
329
330 static gboolean
331 timeout_cb (gpointer user_data)
332 {
333 shader_no++;
334 if (shader_no > (G_N_ELEMENTS (shaders) - 1))
335 shader_no = 0;
336
337 set_shader_num (shader_no);
338
339 return G_SOURCE_CONTINUE;
340 }
341
342 static gboolean
343 idle_cb (gpointer data)
344 {
345 clutter_actor_queue_redraw (data);
346
347 return G_SOURCE_CONTINUE;
348 }
349
350 static gboolean
351 destroy_window_cb (ClutterStage *stage,
352 ClutterEvent *event,
353 void *user_data)
354 {
355 clutter_main_quit ();
356 return TRUE;
357 }
358
359 G_MODULE_EXPORT int
360 test_cogl_shader_arbfp_main (int argc, char *argv[])
361 {
362 ClutterActor *stage;
363 char *file;
364 GError *error;
365 ClutterColor stage_color = { 0x61, 0x64, 0x8c, 0xff };
366
367 if (clutter_init (&argc, &argv) != CLUTTER_INIT_SUCCESS)
368 return 1;
369
370 stage = clutter_stage_new ();
371
372 clutter_stage_set_title (CLUTTER_STAGE (stage), "Assembly Shader Test");
373 clutter_stage_set_color (CLUTTER_STAGE (stage), &stage_color);
374
375 file = g_build_filename (TESTS_DATADIR, "redhand.png", NULL);
376 error = NULL;
377 redhand = cogl_texture_new_from_file (file, 0, COGL_PIXEL_FORMAT_ANY,
378 &error);
379 if (redhand == COGL_INVALID_HANDLE)
380 g_error ("image load failed: %s", error->message);
381
382 material = cogl_material_new ();
383 cogl_material_set_layer (material, 0, redhand);
384
385 set_shader_num (0);
386 g_signal_connect_after (stage, "paint", G_CALLBACK (paint_cb), NULL);
387
388 clutter_actor_set_reactive (stage, TRUE);
389 g_signal_connect (stage, "button-release-event",
390 G_CALLBACK (button_release_cb), NULL);
391 g_signal_connect (stage, "key-release-event",
392 G_CALLBACK (key_release_cb), NULL);
393
394 g_signal_connect (stage, "delete-event",
395 G_CALLBACK (destroy_window_cb), NULL);
396
397 timeout_id = clutter_threads_add_timeout (1000, timeout_cb, NULL);
398
399 clutter_threads_add_idle (idle_cb, stage);
400
401 clutter_actor_show (stage);
402
403 clutter_main ();
404
405 return EXIT_SUCCESS;
406 }
407