Jan Sebechlebsky | 5cb3996 | 2023-11-22 17:33:07 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2023 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | // #define LOG_NDEBUG 0 |
| 18 | #define LOG_TAG "EglProgram" |
| 19 | #include "EglProgram.h" |
| 20 | |
| 21 | #include <array> |
| 22 | #include <complex> |
| 23 | |
| 24 | #include "EglUtil.h" |
| 25 | #include "GLES/gl.h" |
| 26 | #include "GLES2/gl2.h" |
| 27 | #include "GLES2/gl2ext.h" |
| 28 | #include "log/log.h" |
| 29 | |
| 30 | namespace android { |
| 31 | namespace companion { |
| 32 | namespace virtualcamera { |
| 33 | |
| 34 | namespace { |
| 35 | |
| 36 | constexpr char kGlExtYuvTarget[] = "GL_EXT_YUV_target"; |
| 37 | |
| 38 | constexpr char kIdentityVertexShader[] = R"( |
| 39 | attribute vec4 vPosition; |
| 40 | void main() { |
| 41 | gl_Position = vPosition; |
| 42 | })"; |
| 43 | |
| 44 | constexpr char kJuliaFractalFragmentShader[] = R"( |
| 45 | precision mediump float; |
| 46 | uniform vec2 uResolution; |
| 47 | uniform vec2 uC; |
| 48 | uniform vec2 uUV; |
| 49 | const float kIter = 64.0; |
| 50 | |
| 51 | vec2 imSq(vec2 n){ |
| 52 | return vec2(pow(n.x,2.0)-pow(n.y,2.0), 2.0*n.x*n.y); |
| 53 | } |
| 54 | |
| 55 | float julia(vec2 n, vec2 c) { |
| 56 | vec2 z = n; |
| 57 | for (float i=0.0;i<kIter; i+=1.0) { |
| 58 | z = imSq(z) + c; |
| 59 | if (length(z) > 2.0) return i/kIter; |
| 60 | } |
| 61 | return kIter; |
| 62 | } |
| 63 | |
| 64 | void main() { |
| 65 | vec2 uv = vec2(gl_FragCoord.x / uResolution.x - 0.5, gl_FragCoord.y / uResolution.y - 0.5); |
| 66 | float juliaVal = julia(uv * 4.0, uC); |
| 67 | gl_FragColor = vec4( juliaVal,uUV.x,uUV.y,0.0); |
| 68 | })"; |
| 69 | |
| 70 | constexpr char kExternalTextureVertexShader[] = R"(#version 300 es |
Jan Sebechlebsky | 99492e3 | 2023-12-20 09:49:45 +0100 | [diff] [blame^] | 71 | uniform mat4 aTextureTransformMatrix; // Transform matrix given by surface texture. |
Jan Sebechlebsky | 5cb3996 | 2023-11-22 17:33:07 +0100 | [diff] [blame] | 72 | in vec4 aPosition; |
| 73 | in vec2 aTextureCoord; |
| 74 | out vec2 vTextureCoord; |
| 75 | void main() { |
| 76 | gl_Position = aPosition; |
Jan Sebechlebsky | 99492e3 | 2023-12-20 09:49:45 +0100 | [diff] [blame^] | 77 | vTextureCoord = (aTextureTransformMatrix * vec4(aTextureCoord, 0.0, 1.0)).xy; |
Jan Sebechlebsky | 5cb3996 | 2023-11-22 17:33:07 +0100 | [diff] [blame] | 78 | })"; |
| 79 | |
Jan Sebechlebsky | 042d1fb | 2023-12-12 16:37:00 +0100 | [diff] [blame] | 80 | constexpr char kExternalYuvTextureFragmentShader[] = R"(#version 300 es |
Jan Sebechlebsky | 5cb3996 | 2023-11-22 17:33:07 +0100 | [diff] [blame] | 81 | #extension GL_OES_EGL_image_external_essl3 : require |
| 82 | #extension GL_EXT_YUV_target : require |
| 83 | precision mediump float; |
| 84 | in vec2 vTextureCoord; |
Jan Sebechlebsky | 042d1fb | 2023-12-12 16:37:00 +0100 | [diff] [blame] | 85 | layout (yuv) out vec4 fragColor; |
Jan Sebechlebsky | 5cb3996 | 2023-11-22 17:33:07 +0100 | [diff] [blame] | 86 | uniform __samplerExternal2DY2YEXT uTexture; |
| 87 | void main() { |
| 88 | fragColor = texture(uTexture, vTextureCoord); |
| 89 | })"; |
| 90 | |
Jan Sebechlebsky | 042d1fb | 2023-12-12 16:37:00 +0100 | [diff] [blame] | 91 | constexpr char kExternalRgbaTextureFragmentShader[] = R"(#version 300 es |
| 92 | #extension GL_OES_EGL_image_external : require |
| 93 | #extension GL_EXT_YUV_target : require |
| 94 | precision mediump float; |
| 95 | in vec2 vTextureCoord; |
| 96 | layout (yuv) out vec4 fragColor; |
| 97 | uniform samplerExternalOES uTexture; |
| 98 | void main() { |
| 99 | vec4 rgbaColor = texture(uTexture, vTextureCoord); |
| 100 | fragColor = vec4(rgb_2_yuv(rgbaColor.xyz, itu_601_full_range), 0.0); |
| 101 | })"; |
| 102 | |
Jan Sebechlebsky | 5cb3996 | 2023-11-22 17:33:07 +0100 | [diff] [blame] | 103 | constexpr int kCoordsPerVertex = 3; |
Jan Sebechlebsky | 99492e3 | 2023-12-20 09:49:45 +0100 | [diff] [blame^] | 104 | |
| 105 | constexpr std::array<float, 12> kSquareCoords{ |
| 106 | -1.f, -1.0f, 0.0f, // top left |
| 107 | -1.f, 1.f, 0.0f, // bottom left |
| 108 | 1.0f, 1.f, 0.0f, // bottom right |
| 109 | 1.0f, -1.0f, 0.0f}; // top right |
Jan Sebechlebsky | 5cb3996 | 2023-11-22 17:33:07 +0100 | [diff] [blame] | 110 | |
| 111 | constexpr std::array<float, 8> kTextureCoords{0.0f, 1.0f, // top left |
| 112 | 0.0f, 0.0f, // bottom left |
| 113 | 1.0f, 0.0f, // bottom right |
| 114 | 1.0f, 1.0f}; // top right |
| 115 | |
| 116 | constexpr std::array<uint8_t, 6> kDrawOrder{0, 1, 2, 0, 2, 3}; |
| 117 | |
| 118 | GLuint compileShader(GLenum shaderType, const char* src) { |
| 119 | GLuint shader = glCreateShader(shaderType); |
| 120 | if (shader == 0) { |
| 121 | ALOGE("glCreateShader(shaderType=%x) error: %#x", |
| 122 | static_cast<unsigned int>(shaderType), glGetError()); |
| 123 | return 0; |
| 124 | } |
| 125 | |
| 126 | glShaderSource(shader, 1, &src, NULL); |
| 127 | glCompileShader(shader); |
| 128 | |
| 129 | GLint compiled = 0; |
| 130 | glGetShaderiv(shader, GL_COMPILE_STATUS, &compiled); |
| 131 | if (!compiled) { |
| 132 | ALOGE("Compile of shader type %d failed", shaderType); |
| 133 | GLint infoLen = 0; |
| 134 | glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &infoLen); |
| 135 | if (infoLen) { |
| 136 | char* buf = new char[infoLen]; |
| 137 | if (buf) { |
| 138 | glGetShaderInfoLog(shader, infoLen, NULL, buf); |
| 139 | ALOGE("Compile log: %s", buf); |
| 140 | delete[] buf; |
| 141 | } |
| 142 | } |
| 143 | glDeleteShader(shader); |
| 144 | return 0; |
| 145 | } |
| 146 | return shader; |
| 147 | } |
| 148 | |
| 149 | } // namespace |
| 150 | |
| 151 | EglProgram::~EglProgram() { |
| 152 | if (mProgram) { |
| 153 | glDeleteProgram(mProgram); |
| 154 | } |
| 155 | } |
| 156 | |
| 157 | bool EglProgram::initialize(const char* vertexShaderSrc, |
| 158 | const char* fragmentShaderSrc) { |
| 159 | GLuint vertexShaderId = compileShader(GL_VERTEX_SHADER, vertexShaderSrc); |
| 160 | if (checkEglError("compileShader(vertex)")) { |
| 161 | return false; |
| 162 | } |
| 163 | GLuint fragmentShaderId = compileShader(GL_FRAGMENT_SHADER, fragmentShaderSrc); |
| 164 | if (checkEglError("compileShader(fragment)")) { |
| 165 | return false; |
| 166 | } |
| 167 | |
| 168 | GLuint programId = glCreateProgram(); |
| 169 | |
| 170 | glAttachShader(programId, vertexShaderId); |
| 171 | glAttachShader(programId, fragmentShaderId); |
| 172 | glLinkProgram(programId); |
| 173 | |
| 174 | GLint linkStatus = GL_FALSE; |
| 175 | glGetProgramiv(programId, GL_LINK_STATUS, &linkStatus); |
| 176 | if (linkStatus != GL_TRUE) { |
| 177 | ALOGE("glLinkProgram failed"); |
| 178 | GLint bufLength = 0; |
| 179 | glGetProgramiv(programId, GL_INFO_LOG_LENGTH, &bufLength); |
| 180 | if (bufLength) { |
| 181 | char* buf = new char[bufLength]; |
| 182 | if (buf) { |
| 183 | glGetProgramInfoLog(programId, bufLength, NULL, buf); |
| 184 | ALOGE("Link log: %s", buf); |
| 185 | delete[] buf; |
| 186 | } |
| 187 | } |
| 188 | glDeleteProgram(programId); |
| 189 | return false; |
| 190 | } |
| 191 | |
| 192 | mProgram = programId; |
| 193 | |
| 194 | mIsInitialized = true; |
| 195 | return mIsInitialized; |
| 196 | } |
| 197 | |
| 198 | bool EglProgram::isInitialized() const { |
| 199 | return mIsInitialized; |
| 200 | } |
| 201 | |
| 202 | EglTestPatternProgram::EglTestPatternProgram() { |
| 203 | if (initialize(kIdentityVertexShader, kJuliaFractalFragmentShader)) { |
| 204 | ALOGV("Successfully initialized EGL shaders for test pattern program."); |
| 205 | } else { |
| 206 | ALOGE("Test pattern EGL shader program initialization failed."); |
| 207 | } |
| 208 | } |
| 209 | |
| 210 | bool EglTestPatternProgram::draw(int width, int height, int frameNumber) { |
| 211 | glViewport(0, 0, static_cast<GLsizei>(width), static_cast<GLsizei>(height)); |
| 212 | checkEglError("glViewport"); |
| 213 | |
| 214 | // Load compiled shader. |
| 215 | glUseProgram(mProgram); |
| 216 | checkEglError("glUseProgram"); |
| 217 | |
| 218 | // Compute point in complex plane corresponding to fractal for this frame number. |
| 219 | float time = float(frameNumber) / 120.0f; |
| 220 | const std::complex<float> c(std::sin(time) * 0.78f, std::cos(time) * 0.78f); |
| 221 | |
| 222 | // Pass uniform values to the shader. |
| 223 | int resolutionHandle = glGetUniformLocation(mProgram, "uResolution"); |
| 224 | checkEglError("glGetUniformLocation -> uResolution"); |
| 225 | glUniform2f(resolutionHandle, static_cast<float>(width), |
| 226 | static_cast<float>(height)); |
| 227 | checkEglError("glUniform2f -> uResolution"); |
| 228 | |
| 229 | // Pass "C" constant value determining the Julia set to the shader. |
| 230 | int cHandle = glGetUniformLocation(mProgram, "uC"); |
| 231 | glUniform2f(cHandle, c.imag(), c.real()); |
| 232 | |
| 233 | // Pass chroma value to the shader. |
| 234 | int uvHandle = glGetUniformLocation(mProgram, "uUV"); |
| 235 | glUniform2f(uvHandle, (c.imag() + 1.f) / 2.f, (c.real() + 1.f) / 2.f); |
| 236 | |
| 237 | // Pass vertex array to draw. |
| 238 | int positionHandle = glGetAttribLocation(mProgram, "vPosition"); |
| 239 | glEnableVertexAttribArray(positionHandle); |
| 240 | |
| 241 | // Prepare the triangle coordinate data. |
| 242 | glVertexAttribPointer(positionHandle, kCoordsPerVertex, GL_FLOAT, false, |
| 243 | kSquareCoords.size(), kSquareCoords.data()); |
| 244 | |
| 245 | // Draw triangle strip forming a square filling the viewport. |
| 246 | glDrawElements(GL_TRIANGLES, kDrawOrder.size(), GL_UNSIGNED_BYTE, |
| 247 | kDrawOrder.data()); |
| 248 | if (checkEglError("glDrawElements")) { |
| 249 | return false; |
| 250 | } |
| 251 | |
| 252 | return true; |
| 253 | } |
| 254 | |
Jan Sebechlebsky | 042d1fb | 2023-12-12 16:37:00 +0100 | [diff] [blame] | 255 | EglTextureProgram::EglTextureProgram(const TextureFormat textureFormat) { |
Jan Sebechlebsky | 5cb3996 | 2023-11-22 17:33:07 +0100 | [diff] [blame] | 256 | if (!isGlExtensionSupported(kGlExtYuvTarget)) { |
| 257 | ALOGE( |
| 258 | "Cannot initialize external texture program due to missing " |
| 259 | "GL_EXT_YUV_target extension"); |
| 260 | return; |
| 261 | } |
| 262 | |
Jan Sebechlebsky | 042d1fb | 2023-12-12 16:37:00 +0100 | [diff] [blame] | 263 | const char* fragmentShaderSrc = textureFormat == TextureFormat::YUV |
| 264 | ? kExternalYuvTextureFragmentShader |
| 265 | : kExternalRgbaTextureFragmentShader; |
| 266 | if (initialize(kExternalTextureVertexShader, fragmentShaderSrc)) { |
Jan Sebechlebsky | 5cb3996 | 2023-11-22 17:33:07 +0100 | [diff] [blame] | 267 | ALOGV("Successfully initialized EGL shaders for external texture program."); |
| 268 | } else { |
| 269 | ALOGE("External texture EGL shader program initialization failed."); |
| 270 | } |
| 271 | } |
| 272 | |
Jan Sebechlebsky | 99492e3 | 2023-12-20 09:49:45 +0100 | [diff] [blame^] | 273 | bool EglTextureProgram::draw(GLuint textureId, |
| 274 | const std::array<float, 16>& transformMatrix) { |
Jan Sebechlebsky | 5cb3996 | 2023-11-22 17:33:07 +0100 | [diff] [blame] | 275 | // Load compiled shader. |
| 276 | glUseProgram(mProgram); |
| 277 | if (checkEglError("glUseProgram")) { |
| 278 | return false; |
| 279 | } |
| 280 | |
| 281 | // Pass vertex array to the shader. |
| 282 | int positionHandle = glGetAttribLocation(mProgram, "aPosition"); |
| 283 | glEnableVertexAttribArray(positionHandle); |
| 284 | glVertexAttribPointer(positionHandle, kCoordsPerVertex, GL_FLOAT, false, |
| 285 | kSquareCoords.size(), kSquareCoords.data()); |
| 286 | |
| 287 | // Pass texture coordinates corresponding to vertex array to the shader. |
| 288 | int textureCoordHandle = glGetAttribLocation(mProgram, "aTextureCoord"); |
| 289 | glEnableVertexAttribArray(textureCoordHandle); |
| 290 | glVertexAttribPointer(textureCoordHandle, 2, GL_FLOAT, false, |
| 291 | kTextureCoords.size(), kTextureCoords.data()); |
| 292 | |
Jan Sebechlebsky | 99492e3 | 2023-12-20 09:49:45 +0100 | [diff] [blame^] | 293 | // Pass transformation matrix for the texture coordinates. |
| 294 | int transformMatrixHandle = |
| 295 | glGetUniformLocation(mProgram, "aTextureTransformMatrix"); |
| 296 | glUniformMatrix4fv(transformMatrixHandle, 1, /*transpose=*/GL_FALSE, |
| 297 | transformMatrix.data()); |
| 298 | |
Jan Sebechlebsky | 5cb3996 | 2023-11-22 17:33:07 +0100 | [diff] [blame] | 299 | // Configure texture for the shader. |
| 300 | int textureHandle = glGetUniformLocation(mProgram, "uTexture"); |
| 301 | glActiveTexture(GL_TEXTURE0); |
| 302 | glBindTexture(GL_TEXTURE_EXTERNAL_OES, textureId); |
| 303 | glUniform1i(textureHandle, 0); |
| 304 | |
| 305 | // Draw triangle strip forming a square filling the viewport. |
| 306 | glDrawElements(GL_TRIANGLES, kDrawOrder.size(), GL_UNSIGNED_BYTE, |
| 307 | kDrawOrder.data()); |
| 308 | if (checkEglError("glDrawElements")) { |
| 309 | return false; |
| 310 | } |
| 311 | |
| 312 | return true; |
| 313 | } |
| 314 | |
| 315 | } // namespace virtualcamera |
| 316 | } // namespace companion |
| 317 | } // namespace android |