2 Copyright (c) 2013, Broadcom Europe Ltd
3 Copyright (c) 2013, Tim Gover
7 Redistribution and use in source and binary forms, with or without
8 modification, are permitted provided that the following conditions are met:
9 * Redistributions of source code must retain the above copyright
10 notice, this list of conditions and the following disclaimer.
11 * Redistributions in binary form must reproduce the above copyright
12 notice, this list of conditions and the following disclaimer in the
13 documentation and/or other materials provided with the distribution.
14 * Neither the name of the copyright holder nor the
15 names of its contributors may be used to endorse or promote products
16 derived from this software without specific prior written permission.
18 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
19 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY
22 DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
24 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
25 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 #include "RaspiTexUtil.h"
33 #include <GLES2/gl2.h>
35 #include <EGL/eglext.h>
38 * Example code for implementing Sobel filter as GLSL shaders.
39 * The input image is a greyscale texture from the MMAL buffer Y plane.
42 #define SOBEL_VSHADER_SOURCE \
43 "attribute vec2 vertex;\n" \
44 "varying vec2 texcoord;\n" \
45 "void main(void) {\n" \
46 " texcoord = 0.5 * (vertex + 1.0);\n" \
47 " gl_Position = vec4(vertex, 0.0, 1.0);\n" \
50 /* Example Sobel edge detct shader. The texture format for
51 * EGL_IMAGE_BRCM_MULTIMEDIA_Y is a one byte per pixel greyscale GL_LUMINANCE.
52 * If the output is to be fed into another image processing shader then it may
53 * be worth changing this code to take 4 input Y pixels and pack the result
54 * into a 32bpp RGBA pixel.
56 #define SOBEL_FSHADER_SOURCE \
57 "#extension GL_OES_EGL_image_external : require\n" \
58 "uniform samplerExternalOES tex;\n" \
59 "varying vec2 texcoord;\n" \
60 "uniform vec2 tex_unit;\n" \
61 "void main(void) {\n" \
62 " float x = texcoord.x;\n" \
63 " float y = texcoord.y;\n" \
64 " float x1 = x - tex_unit.x;\n" \
65 " float y1 = y - tex_unit.y;\n" \
66 " float x2 = x + tex_unit.x;\n" \
67 " float y2 = y + tex_unit.y;\n" \
68 " vec4 p0 = texture2D(tex, vec2(x1, y1));\n" \
69 " vec4 p1 = texture2D(tex, vec2(x, y1));\n" \
70 " vec4 p2 = texture2D(tex, vec2(x2, y1));\n" \
71 " vec4 p3 = texture2D(tex, vec2(x1, y));\n" \
72 " /* vec4 p4 = texture2D(tex, vec2(x, y)); */\n" \
73 " vec4 p5 = texture2D(tex, vec2(x2, y));\n" \
74 " vec4 p6 = texture2D(tex, vec2(x1, y2));\n" \
75 " vec4 p7 = texture2D(tex, vec2(x, y2));\n" \
76 " vec4 p8 = texture2D(tex, vec2(x2, y2));\n" \
78 " vec4 v = p0 + (2.0 * p1) + p3 -p6 + (-2.0 * p7) + -p8;\n" \
79 " vec4 h = p0 + (2.0 * p3) + p7 -p2 + (-2.0 * p5) + -p8;\n" \
80 " gl_FragColor = sqrt(h*h + v*v);\n" \
81 " gl_FragColor.a = 1.0;\n" \
84 static GLfloat quad_varray[] = {
85 -1.0f, -1.0f, 1.0f, 1.0f, 1.0f, -1.0f,
86 -1.0f, 1.0f, 1.0f, 1.0f, -1.0f, -1.0f,
89 static GLuint quad_vbo;
91 static RASPITEXUTIL_SHADER_PROGRAM_T sobel_shader =
93 .vertex_source = SOBEL_VSHADER_SOURCE,
94 .fragment_source = SOBEL_FSHADER_SOURCE,
95 .uniform_names = {"tex", "tex_unit"},
96 .attribute_names = {"vertex"},
99 static const EGLint sobel_egl_config_attribs[] =
105 EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
111 * Initialisation of shader uniforms.
113 * @param width Width of the EGL image.
114 * @param width Height of the EGL image.
116 static int shader_set_uniforms(RASPITEXUTIL_SHADER_PROGRAM_T *shader,
117 int width, int height)
119 GLCHK(glUseProgram(shader->program));
120 GLCHK(glUniform1i(shader->uniform_locations[0], 0)); // Texture unit
122 /* Dimensions of a single pixel in texture co-ordinates */
123 GLCHK(glUniform2f(shader->uniform_locations[1],
124 1.0 / (float) width, 1.0 / (float) height));
126 /* Enable attrib 0 as vertex array */
127 GLCHK(glEnableVertexAttribArray(shader->attribute_locations[0]));
132 * Creates the OpenGL ES 2.X context and builds the shaders.
133 * @param raspitex_state A pointer to the GL preview state.
134 * @return Zero if successful.
136 static int sobel_init(RASPITEX_STATE *raspitex_state)
139 int width = raspitex_state->width;
140 int height = raspitex_state->height;
142 vcos_log_trace("%s", VCOS_FUNCTION);
143 raspitex_state->egl_config_attribs = sobel_egl_config_attribs;
144 rc = raspitexutil_gl_init_2_0(raspitex_state);
148 rc = raspitexutil_build_shader_program(&sobel_shader);
152 rc = shader_set_uniforms(&sobel_shader, width, height);
156 GLCHK(glGenBuffers(1, &quad_vbo));
157 GLCHK(glBindBuffer(GL_ARRAY_BUFFER, quad_vbo));
158 GLCHK(glBufferData(GL_ARRAY_BUFFER, sizeof(quad_varray), quad_varray, GL_STATIC_DRAW));
159 GLCHK(glClearColor(0.0f, 0.0f, 0.0f, 1.0f));
165 /* Redraws the scene with the latest luma buffer.
167 * @param raspitex_state A pointer to the GL preview state.
168 * @return Zero if successful.
170 static int sobel_redraw(RASPITEX_STATE* state)
172 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
173 GLCHK(glUseProgram(sobel_shader.program));
175 /* Bind the Y plane texture */
176 GLCHK(glActiveTexture(GL_TEXTURE0));
177 GLCHK(glBindTexture(GL_TEXTURE_EXTERNAL_OES, state->y_texture));
178 GLCHK(glBindBuffer(GL_ARRAY_BUFFER, quad_vbo));
179 GLCHK(glEnableVertexAttribArray(sobel_shader.attribute_locations[0]));
180 GLCHK(glVertexAttribPointer(sobel_shader.attribute_locations[0], 2, GL_FLOAT, GL_FALSE, 0, 0));
181 GLCHK(glDrawArrays(GL_TRIANGLES, 0, 6));
186 int sobel_open(RASPITEX_STATE *state)
188 state->ops.gl_init = sobel_init;
189 state->ops.redraw = sobel_redraw;
190 state->ops.update_y_texture = raspitexutil_update_y_texture;