1 | /* |
2 | Copyright (c) 2013, Broadcom Europe Ltd |
3 | Copyright (c) 2013, Tim Gover |
4 | All rights reserved. |
5 | |
6 | |
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. |
17 | |
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. |
28 | */ |
29 | |
30 | #include "sobel.h" |
31 | #include "RaspiTex.h" |
32 | #include "RaspiTexUtil.h" |
33 | #include <GLES2/gl2.h> |
34 | #include <EGL/egl.h> |
35 | #include <EGL/eglext.h> |
36 | |
37 | /* \file sobel.c |
38 | * Example code for implementing Sobel filter as GLSL shaders. |
39 | * The input image is a greyscale texture from the MMAL buffer Y plane. |
40 | */ |
41 | |
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" \ |
48 | "}\n" |
49 | |
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. |
55 | */ |
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" \ |
77 | "\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" \ |
82 | "}\n" |
83 | |
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, |
87 | }; |
88 | |
89 | static GLuint quad_vbo; |
90 | |
91 | static RASPITEXUTIL_SHADER_PROGRAM_T sobel_shader = |
92 | { |
93 | .vertex_source = SOBEL_VSHADER_SOURCE, |
94 | .fragment_source = SOBEL_FSHADER_SOURCE, |
95 | .uniform_names = {"tex" , "tex_unit" }, |
96 | .attribute_names = {"vertex" }, |
97 | }; |
98 | |
99 | static const EGLint sobel_egl_config_attribs[] = |
100 | { |
101 | EGL_RED_SIZE, 8, |
102 | EGL_GREEN_SIZE, 8, |
103 | EGL_BLUE_SIZE, 8, |
104 | EGL_ALPHA_SIZE, 8, |
105 | EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT, |
106 | EGL_NONE |
107 | }; |
108 | |
109 | |
110 | /** |
111 | * Initialisation of shader uniforms. |
112 | * |
113 | * @param width Width of the EGL image. |
114 | * @param width Height of the EGL image. |
115 | */ |
116 | static int shader_set_uniforms(RASPITEXUTIL_SHADER_PROGRAM_T *shader, |
117 | int width, int height) |
118 | { |
119 | GLCHK(glUseProgram(shader->program)); |
120 | GLCHK(glUniform1i(shader->uniform_locations[0], 0)); // Texture unit |
121 | |
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)); |
125 | |
126 | /* Enable attrib 0 as vertex array */ |
127 | GLCHK(glEnableVertexAttribArray(shader->attribute_locations[0])); |
128 | return 0; |
129 | } |
130 | |
131 | /** |
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. |
135 | */ |
136 | static int sobel_init(RASPITEX_STATE *raspitex_state) |
137 | { |
138 | int rc = 0; |
139 | int width = raspitex_state->width; |
140 | int height = raspitex_state->height; |
141 | |
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); |
145 | if (rc != 0) |
146 | goto end; |
147 | |
148 | rc = raspitexutil_build_shader_program(&sobel_shader); |
149 | if (rc != 0) |
150 | goto end; |
151 | |
152 | rc = shader_set_uniforms(&sobel_shader, width, height); |
153 | if (rc != 0) |
154 | goto end; |
155 | |
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)); |
160 | |
161 | end: |
162 | return rc; |
163 | } |
164 | |
165 | /* Redraws the scene with the latest luma buffer. |
166 | * |
167 | * @param raspitex_state A pointer to the GL preview state. |
168 | * @return Zero if successful. |
169 | */ |
170 | static int sobel_redraw(RASPITEX_STATE* state) |
171 | { |
172 | glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); |
173 | GLCHK(glUseProgram(sobel_shader.program)); |
174 | |
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)); |
182 | |
183 | return 0; |
184 | } |
185 | |
186 | int sobel_open(RASPITEX_STATE *state) |
187 | { |
188 | state->ops.gl_init = sobel_init; |
189 | state->ops.redraw = sobel_redraw; |
190 | state->ops.update_y_texture = raspitexutil_update_y_texture; |
191 | return 0; |
192 | } |
193 | |