| 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 |
/* a couple of boilerplate defines that are common amongst all the |
| 15 |
* sample shaders |
| 16 |
*/ |
| 17 |
|
| 18 |
/* FRAGMENT_SHADER_BEGIN: generate boilerplate with a local vec4 color already |
| 19 |
* initialized, from a sampler2D in a variable tex. |
| 20 |
*/ |
| 21 |
#define FRAGMENT_SHADER_VARS \ |
| 22 |
"uniform sampler2D tex;" \ |
| 23 |
"uniform float x_step, y_step;" |
| 24 |
|
| 25 |
#define FRAGMENT_SHADER_BEGIN \ |
| 26 |
"void main (){" \ |
| 27 |
" vec4 color = texture2D (tex, vec2(cogl_tex_coord_in[0]));" |
| 28 |
|
| 29 |
/* FRAGMENT_SHADER_END: apply the changed color to the output buffer correctly |
| 30 |
* blended with the gl specified color (makes the opacity of actors work |
| 31 |
* correctly). |
| 32 |
*/ |
| 33 |
#define FRAGMENT_SHADER_END \ |
| 34 |
" cogl_color_out = color;" \ |
| 35 |
" cogl_color_out = cogl_color_out * cogl_color_in;" \ |
| 36 |
"}" |
| 37 |
|
| 38 |
static ShaderSource shaders[]= |
| 39 |
{ |
| 40 |
{"brightness-contrast", |
| 41 |
FRAGMENT_SHADER_VARS |
| 42 |
"uniform float brightness, contrast;" |
| 43 |
FRAGMENT_SHADER_BEGIN |
| 44 |
" color.rgb /= color.a;" |
| 45 |
" color.rgb = (color.rgb - vec3(0.5, 0.5, 0.5)) * contrast + " |
| 46 |
"vec3 (brightness + 0.5, brightness + 0.5, brightness + 0.5);" |
| 47 |
" color.rgb *= color.a;" |
| 48 |
FRAGMENT_SHADER_END |
| 49 |
}, |
| 50 |
|
| 51 |
{"box-blur", |
| 52 |
FRAGMENT_SHADER_VARS |
| 53 |
|
| 54 |
#if GPU_SUPPORTS_DYNAMIC_BRANCHING |
| 55 |
"uniform float radius;" |
| 56 |
FRAGMENT_SHADER_BEGIN |
| 57 |
"float u, v;" |
| 58 |
"int count = 1;" |
| 59 |
"for (u=-radius;u<radius;u++)" |
| 60 |
" for (v=-radius;v<radius;v++)" |
| 61 |
" {" |
| 62 |
" color += texture2D(tex, " |
| 63 |
" vec2(cogl_tex_coord_in[0].s + u * 2.0 * x_step, " |
| 64 |
" cogl_tex_coord_in[0].t + v * 2.0 * y_step));" |
| 65 |
" count ++;" |
| 66 |
" }" |
| 67 |
"color = color / float(count);" |
| 68 |
FRAGMENT_SHADER_END |
| 69 |
#else |
| 70 |
"vec4 get_rgba_rel(sampler2D tex, float dx, float dy)" |
| 71 |
"{" |
| 72 |
" return texture2D (tex, cogl_tex_coord_in[0].st " |
| 73 |
" + vec2(dx, dy) * 2.0);" |
| 74 |
"}" |
| 75 |
|
| 76 |
FRAGMENT_SHADER_BEGIN |
| 77 |
" float count = 1.0;" |
| 78 |
" color += get_rgba_rel (tex, -x_step, -y_step); count++;" |
| 79 |
" color += get_rgba_rel (tex, -x_step, 0.0); count++;" |
| 80 |
" color += get_rgba_rel (tex, -x_step, y_step); count++;" |
| 81 |
" color += get_rgba_rel (tex, 0.0, -y_step); count++;" |
| 82 |
" color += get_rgba_rel (tex, 0.0, 0.0); count++;" |
| 83 |
" color += get_rgba_rel (tex, 0.0, y_step); count++;" |
| 84 |
" color += get_rgba_rel (tex, x_step, -y_step); count++;" |
| 85 |
" color += get_rgba_rel (tex, x_step, 0.0); count++;" |
| 86 |
" color += get_rgba_rel (tex, x_step, y_step); count++;" |
| 87 |
" color = color / count;" |
| 88 |
FRAGMENT_SHADER_END |
| 89 |
#endif |
| 90 |
}, |
| 91 |
|
| 92 |
{"invert", |
| 93 |
FRAGMENT_SHADER_VARS |
| 94 |
FRAGMENT_SHADER_BEGIN |
| 95 |
" color.rgb /= color.a;" |
| 96 |
" color.rgb = vec3(1.0, 1.0, 1.0) - color.rgb;\n" |
| 97 |
" color.rgb *= color.a;" |
| 98 |
FRAGMENT_SHADER_END |
| 99 |
}, |
| 100 |
|
| 101 |
{"brightness-contrast", |
| 102 |
FRAGMENT_SHADER_VARS |
| 103 |
"uniform float brightness;" |
| 104 |
"uniform float contrast;" |
| 105 |
FRAGMENT_SHADER_BEGIN |
| 106 |
" color.rgb /= color.a;" |
| 107 |
" color.r = (color.r - 0.5) * contrast + brightness + 0.5;" |
| 108 |
" color.g = (color.g - 0.5) * contrast + brightness + 0.5;" |
| 109 |
" color.b = (color.b - 0.5) * contrast + brightness + 0.5;" |
| 110 |
" color.rgb *= color.a;" |
| 111 |
FRAGMENT_SHADER_END |
| 112 |
}, |
| 113 |
|
| 114 |
{"gray", |
| 115 |
FRAGMENT_SHADER_VARS |
| 116 |
FRAGMENT_SHADER_BEGIN |
| 117 |
" float avg = (color.r + color.g + color.b) / 3.0;" |
| 118 |
" color.r = avg;" |
| 119 |
" color.g = avg;" |
| 120 |
" color.b = avg;" |
| 121 |
FRAGMENT_SHADER_END |
| 122 |
}, |
| 123 |
|
| 124 |
{"combined-mirror", |
| 125 |
FRAGMENT_SHADER_VARS |
| 126 |
FRAGMENT_SHADER_BEGIN |
| 127 |
" vec4 colorB = texture2D (tex, vec2(cogl_tex_coord_in[0].ts));" |
| 128 |
" float avg = (color.r + color.g + color.b) / 3.0;" |
| 129 |
" color.r = avg;" |
| 130 |
" color.g = avg;" |
| 131 |
" color.b = avg;" |
| 132 |
" color = (color + colorB)/2.0;" |
| 133 |
FRAGMENT_SHADER_END |
| 134 |
}, |
| 135 |
|
| 136 |
{"edge-detect", |
| 137 |
FRAGMENT_SHADER_VARS |
| 138 |
"float get_avg_rel(sampler2D texB, float dx, float dy)" |
| 139 |
"{" |
| 140 |
" vec4 colorB = texture2D (texB, cogl_tex_coord_in[0].st + vec2(dx, dy));" |
| 141 |
" return (colorB.r + colorB.g + colorB.b) / 3.0;" |
| 142 |
"}" |
| 143 |
FRAGMENT_SHADER_BEGIN |
| 144 |
" mat3 sobel_h = mat3( 1.0, 2.0, 1.0," |
| 145 |
" 0.0, 0.0, 0.0," |
| 146 |
" -1.0, -2.0, -1.0);" |
| 147 |
" mat3 sobel_v = mat3( 1.0, 0.0, -1.0," |
| 148 |
" 2.0, 0.0, -2.0," |
| 149 |
" 1.0, 0.0, -1.0);" |
| 150 |
" mat3 map = mat3( get_avg_rel(tex, -x_step, -y_step)," |
| 151 |
" get_avg_rel(tex, -x_step, 0.0)," |
| 152 |
" get_avg_rel(tex, -x_step, y_step)," |
| 153 |
" get_avg_rel(tex, 0.0, -y_step)," |
| 154 |
" get_avg_rel(tex, 0.0, 0.0)," |
| 155 |
" get_avg_rel(tex, 0.0, y_step)," |
| 156 |
" get_avg_rel(tex, x_step, -y_step)," |
| 157 |
" get_avg_rel(tex, x_step, 0.0)," |
| 158 |
" get_avg_rel(tex, x_step, y_step) );" |
| 159 |
" mat3 gh = sobel_h * map;" |
| 160 |
" mat3 gv = map * sobel_v;" |
| 161 |
" float avgh = (gh[0][0] + gh[0][1] + gh[0][2] +" |
| 162 |
" gh[1][0] + gh[1][1] + gh[1][2] +" |
| 163 |
" gh[2][0] + gh[2][1] + gh[2][2]) / 18.0 + 0.5;" |
| 164 |
" float avgv = (gv[0][0] + gv[0][1] + gv[0][2] +" |
| 165 |
" gv[1][0] + gv[1][1] + gv[1][2] +" |
| 166 |
" gv[2][0] + gv[2][1] + gv[2][2]) / 18.0 + 0.5;" |
| 167 |
" float avg = (avgh + avgv) / 2.0;" |
| 168 |
" color.r = avg * color.r;" |
| 169 |
" color.g = avg * color.g;" |
| 170 |
" color.b = avg * color.b;" |
| 171 |
FRAGMENT_SHADER_END |
| 172 |
} |
| 173 |
}; |
| 174 |
|
| 175 |
static CoglHandle redhand; |
| 176 |
static CoglMaterial *material; |
| 177 |
static unsigned int timeout_id = 0; |
| 178 |
static int shader_no = 0; |
| 179 |
|
| 180 |
static void |
| 181 |
paint_cb (ClutterActor *actor) |
| 182 |
{ |
| 183 |
float stage_width = clutter_actor_get_width (actor); |
| 184 |
float stage_height = clutter_actor_get_height (actor); |
| 185 |
float image_width = cogl_texture_get_width (redhand); |
| 186 |
float image_height = cogl_texture_get_height (redhand); |
| 187 |
|
| 188 |
cogl_set_source (material); |
| 189 |
cogl_rectangle (stage_width/2.0f - image_width/2.0f, |
| 190 |
stage_height/2.0f - image_height/2.0f, |
| 191 |
stage_width/2.0f + image_width/2.0f, |
| 192 |
stage_height/2.0f + image_height/2.0f); |
| 193 |
} |
| 194 |
|
| 195 |
static void |
| 196 |
set_shader_num (int new_no) |
| 197 |
{ |
| 198 |
CoglHandle shader; |
| 199 |
CoglHandle program; |
| 200 |
int image_width = cogl_texture_get_width (redhand); |
| 201 |
int image_height = cogl_texture_get_height (redhand); |
| 202 |
int uniform_no; |
| 203 |
|
| 204 |
g_print ("setting shaders[%i] named '%s'\n", |
| 205 |
new_no, |
| 206 |
shaders[new_no].name); |
| 207 |
|
| 208 |
shader = cogl_create_shader (COGL_SHADER_TYPE_FRAGMENT); |
| 209 |
cogl_shader_source (shader, shaders[new_no].source); |
| 210 |
cogl_shader_compile (shader); |
| 211 |
|
| 212 |
program = cogl_create_program (); |
| 213 |
cogl_program_attach_shader (program, shader); |
| 214 |
cogl_handle_unref (shader); |
| 215 |
cogl_program_link (program); |
| 216 |
|
| 217 |
uniform_no = cogl_program_get_uniform_location (program, "tex"); |
| 218 |
cogl_program_set_uniform_1i (program, uniform_no, 0); |
| 219 |
uniform_no = cogl_program_get_uniform_location (program, "radius"); |
| 220 |
cogl_program_set_uniform_1f (program, uniform_no, 3.0); |
| 221 |
uniform_no = cogl_program_get_uniform_location (program, "brightness"); |
| 222 |
cogl_program_set_uniform_1f (program, uniform_no, 0.4); |
| 223 |
uniform_no = cogl_program_get_uniform_location (program, "contrast"); |
| 224 |
cogl_program_set_uniform_1f (program, uniform_no, -1.9); |
| 225 |
|
| 226 |
uniform_no = cogl_program_get_uniform_location (program, "x_step"); |
| 227 |
cogl_program_set_uniform_1f (program, uniform_no, 1.0f / image_width); |
| 228 |
uniform_no = cogl_program_get_uniform_location (program, "y_step"); |
| 229 |
cogl_program_set_uniform_1f (program, uniform_no, 1.0f / image_height); |
| 230 |
|
| 231 |
cogl_material_set_user_program (material, program); |
| 232 |
cogl_handle_unref (program); |
| 233 |
|
| 234 |
shader_no = new_no; |
| 235 |
} |
| 236 |
|
| 237 |
static gboolean |
| 238 |
button_release_cb (ClutterActor *actor, |
| 239 |
ClutterEvent *event, |
| 240 |
gpointer data) |
| 241 |
{ |
| 242 |
int new_no; |
| 243 |
|
| 244 |
/* Stop the automatic cycling if the user want to manually control |
| 245 |
* which shader to display */ |
| 246 |
if (timeout_id) |
| 247 |
{ |
| 248 |
g_source_remove (timeout_id); |
| 249 |
timeout_id = 0; |
| 250 |
} |
| 251 |
|
| 252 |
if (event->button.button == 1) |
| 253 |
{ |
| 254 |
new_no = shader_no - 1; |
| 255 |
if (new_no < 0) |
| 256 |
new_no = G_N_ELEMENTS (shaders) - 1; |
| 257 |
} |
| 258 |
else |
| 259 |
{ |
| 260 |
new_no = shader_no + 1; |
| 261 |
if (new_no >= G_N_ELEMENTS (shaders)) |
| 262 |
new_no = 0; |
| 263 |
} |
| 264 |
|
| 265 |
set_shader_num (new_no); |
| 266 |
|
| 267 |
return CLUTTER_EVENT_STOP; |
| 268 |
} |
| 269 |
|
| 270 |
static gboolean |
| 271 |
key_release_cb (ClutterActor *actor, |
| 272 |
ClutterEvent *event, |
| 273 |
gpointer user_data) |
| 274 |
{ |
| 275 |
guint keysym = clutter_event_get_key_symbol (event); |
| 276 |
ClutterModifierType mods = clutter_event_get_state (event); |
| 277 |
|
| 278 |
if (keysym == CLUTTER_KEY_q || |
| 279 |
((mods & CLUTTER_SHIFT_MASK) && keysym == CLUTTER_KEY_q)) |
| 280 |
clutter_main_quit (); |
| 281 |
|
| 282 |
return CLUTTER_EVENT_STOP; |
| 283 |
} |
| 284 |
|
| 285 |
static gboolean |
| 286 |
timeout_cb (gpointer user_data) |
| 287 |
{ |
| 288 |
shader_no++; |
| 289 |
if (shader_no > (G_N_ELEMENTS (shaders) - 1)) |
| 290 |
shader_no = 0; |
| 291 |
|
| 292 |
set_shader_num (shader_no); |
| 293 |
|
| 294 |
return G_SOURCE_CONTINUE; |
| 295 |
} |
| 296 |
|
| 297 |
static gboolean |
| 298 |
idle_cb (gpointer data) |
| 299 |
{ |
| 300 |
clutter_actor_queue_redraw (data); |
| 301 |
|
| 302 |
return G_SOURCE_CONTINUE; |
| 303 |
} |
| 304 |
|
| 305 |
static gboolean |
| 306 |
destroy_window_cb (ClutterStage *stage, |
| 307 |
ClutterEvent *event, |
| 308 |
gpointer user_data) |
| 309 |
{ |
| 310 |
clutter_main_quit (); |
| 311 |
|
| 312 |
return CLUTTER_EVENT_STOP; |
| 313 |
} |
| 314 |
|
| 315 |
G_MODULE_EXPORT int |
| 316 |
test_cogl_shader_glsl_main (int argc, char *argv[]) |
| 317 |
{ |
| 318 |
ClutterActor *stage; |
| 319 |
char *file; |
| 320 |
GError *error; |
| 321 |
ClutterColor stage_color = { 0x61, 0x64, 0x8c, 0xff }; |
| 322 |
|
| 323 |
if (clutter_init (&argc, &argv) != CLUTTER_INIT_SUCCESS) |
| 324 |
return 1; |
| 325 |
|
| 326 |
stage = clutter_stage_new (); |
| 327 |
|
| 328 |
clutter_stage_set_title (CLUTTER_STAGE (stage), "Assembly Shader Test"); |
| 329 |
clutter_stage_set_color (CLUTTER_STAGE (stage), &stage_color); |
| 330 |
|
| 331 |
file = g_build_filename (TESTS_DATADIR, "redhand.png", NULL); |
| 332 |
error = NULL; |
| 333 |
redhand = cogl_texture_new_from_file (file, |
| 334 |
COGL_TEXTURE_NO_ATLAS, |
| 335 |
COGL_PIXEL_FORMAT_ANY, |
| 336 |
&error); |
| 337 |
if (redhand == COGL_INVALID_HANDLE) |
| 338 |
g_error ("image load failed: %s", error->message); |
| 339 |
|
| 340 |
material = cogl_material_new (); |
| 341 |
cogl_material_set_layer (material, 0, redhand); |
| 342 |
|
| 343 |
set_shader_num (0); |
| 344 |
g_signal_connect_after (stage, "paint", G_CALLBACK (paint_cb), NULL); |
| 345 |
|
| 346 |
clutter_actor_set_reactive (stage, TRUE); |
| 347 |
g_signal_connect (stage, "button-release-event", |
| 348 |
G_CALLBACK (button_release_cb), NULL); |
| 349 |
g_signal_connect (stage, "key-release-event", |
| 350 |
G_CALLBACK (key_release_cb), NULL); |
| 351 |
|
| 352 |
g_signal_connect (stage, "delete-event", |
| 353 |
G_CALLBACK (destroy_window_cb), NULL); |
| 354 |
|
| 355 |
timeout_id = clutter_threads_add_timeout (1000, timeout_cb, NULL); |
| 356 |
|
| 357 |
clutter_threads_add_idle (idle_cb, stage); |
| 358 |
|
| 359 |
clutter_actor_show (stage); |
| 360 |
|
| 361 |
clutter_main (); |
| 362 |
|
| 363 |
return EXIT_SUCCESS; |
| 364 |
} |
| 365 |
|