blob: c468d394a200fa07b5f43ba212830f57a8b3d165 [file] [log] [blame]
Jan Sebechlebsky5cb39962023-11-22 17:33:07 +01001/*
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
30namespace android {
31namespace companion {
32namespace virtualcamera {
33
34namespace {
35
36constexpr char kGlExtYuvTarget[] = "GL_EXT_YUV_target";
37
38constexpr char kIdentityVertexShader[] = R"(
39 attribute vec4 vPosition;
40 void main() {
41 gl_Position = vPosition;
42 })";
43
44constexpr 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
70constexpr char kExternalTextureVertexShader[] = R"(#version 300 es
71 in vec4 aPosition;
72 in vec2 aTextureCoord;
73 out vec2 vTextureCoord;
74 void main() {
75 gl_Position = aPosition;
76 vTextureCoord = aTextureCoord;
77 })";
78
79constexpr char kExternalTextureFragmentShader[] = R"(#version 300 es
80 #extension GL_OES_EGL_image_external_essl3 : require
81 #extension GL_EXT_YUV_target : require
82 precision mediump float;
83 in vec2 vTextureCoord;
84 out vec4 fragColor;
85 uniform __samplerExternal2DY2YEXT uTexture;
86 void main() {
87 fragColor = texture(uTexture, vTextureCoord);
88 })";
89
90constexpr int kCoordsPerVertex = 3;
91constexpr std::array<float, 12> kSquareCoords{-1.f, 1.0f, 0.0f, // top left
92 -1.f, -1.f, 0.0f, // bottom left
93 1.0f, -1.f, 0.0f, // bottom right
94 1.0f, 1.0f, 0.0f}; // top right
95
96constexpr std::array<float, 8> kTextureCoords{0.0f, 1.0f, // top left
97 0.0f, 0.0f, // bottom left
98 1.0f, 0.0f, // bottom right
99 1.0f, 1.0f}; // top right
100
101constexpr std::array<uint8_t, 6> kDrawOrder{0, 1, 2, 0, 2, 3};
102
103GLuint compileShader(GLenum shaderType, const char* src) {
104 GLuint shader = glCreateShader(shaderType);
105 if (shader == 0) {
106 ALOGE("glCreateShader(shaderType=%x) error: %#x",
107 static_cast<unsigned int>(shaderType), glGetError());
108 return 0;
109 }
110
111 glShaderSource(shader, 1, &src, NULL);
112 glCompileShader(shader);
113
114 GLint compiled = 0;
115 glGetShaderiv(shader, GL_COMPILE_STATUS, &compiled);
116 if (!compiled) {
117 ALOGE("Compile of shader type %d failed", shaderType);
118 GLint infoLen = 0;
119 glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &infoLen);
120 if (infoLen) {
121 char* buf = new char[infoLen];
122 if (buf) {
123 glGetShaderInfoLog(shader, infoLen, NULL, buf);
124 ALOGE("Compile log: %s", buf);
125 delete[] buf;
126 }
127 }
128 glDeleteShader(shader);
129 return 0;
130 }
131 return shader;
132}
133
134} // namespace
135
136EglProgram::~EglProgram() {
137 if (mProgram) {
138 glDeleteProgram(mProgram);
139 }
140}
141
142bool EglProgram::initialize(const char* vertexShaderSrc,
143 const char* fragmentShaderSrc) {
144 GLuint vertexShaderId = compileShader(GL_VERTEX_SHADER, vertexShaderSrc);
145 if (checkEglError("compileShader(vertex)")) {
146 return false;
147 }
148 GLuint fragmentShaderId = compileShader(GL_FRAGMENT_SHADER, fragmentShaderSrc);
149 if (checkEglError("compileShader(fragment)")) {
150 return false;
151 }
152
153 GLuint programId = glCreateProgram();
154
155 glAttachShader(programId, vertexShaderId);
156 glAttachShader(programId, fragmentShaderId);
157 glLinkProgram(programId);
158
159 GLint linkStatus = GL_FALSE;
160 glGetProgramiv(programId, GL_LINK_STATUS, &linkStatus);
161 if (linkStatus != GL_TRUE) {
162 ALOGE("glLinkProgram failed");
163 GLint bufLength = 0;
164 glGetProgramiv(programId, GL_INFO_LOG_LENGTH, &bufLength);
165 if (bufLength) {
166 char* buf = new char[bufLength];
167 if (buf) {
168 glGetProgramInfoLog(programId, bufLength, NULL, buf);
169 ALOGE("Link log: %s", buf);
170 delete[] buf;
171 }
172 }
173 glDeleteProgram(programId);
174 return false;
175 }
176
177 mProgram = programId;
178
179 mIsInitialized = true;
180 return mIsInitialized;
181}
182
183bool EglProgram::isInitialized() const {
184 return mIsInitialized;
185}
186
187EglTestPatternProgram::EglTestPatternProgram() {
188 if (initialize(kIdentityVertexShader, kJuliaFractalFragmentShader)) {
189 ALOGV("Successfully initialized EGL shaders for test pattern program.");
190 } else {
191 ALOGE("Test pattern EGL shader program initialization failed.");
192 }
193}
194
195bool EglTestPatternProgram::draw(int width, int height, int frameNumber) {
196 glViewport(0, 0, static_cast<GLsizei>(width), static_cast<GLsizei>(height));
197 checkEglError("glViewport");
198
199 // Load compiled shader.
200 glUseProgram(mProgram);
201 checkEglError("glUseProgram");
202
203 // Compute point in complex plane corresponding to fractal for this frame number.
204 float time = float(frameNumber) / 120.0f;
205 const std::complex<float> c(std::sin(time) * 0.78f, std::cos(time) * 0.78f);
206
207 // Pass uniform values to the shader.
208 int resolutionHandle = glGetUniformLocation(mProgram, "uResolution");
209 checkEglError("glGetUniformLocation -> uResolution");
210 glUniform2f(resolutionHandle, static_cast<float>(width),
211 static_cast<float>(height));
212 checkEglError("glUniform2f -> uResolution");
213
214 // Pass "C" constant value determining the Julia set to the shader.
215 int cHandle = glGetUniformLocation(mProgram, "uC");
216 glUniform2f(cHandle, c.imag(), c.real());
217
218 // Pass chroma value to the shader.
219 int uvHandle = glGetUniformLocation(mProgram, "uUV");
220 glUniform2f(uvHandle, (c.imag() + 1.f) / 2.f, (c.real() + 1.f) / 2.f);
221
222 // Pass vertex array to draw.
223 int positionHandle = glGetAttribLocation(mProgram, "vPosition");
224 glEnableVertexAttribArray(positionHandle);
225
226 // Prepare the triangle coordinate data.
227 glVertexAttribPointer(positionHandle, kCoordsPerVertex, GL_FLOAT, false,
228 kSquareCoords.size(), kSquareCoords.data());
229
230 // Draw triangle strip forming a square filling the viewport.
231 glDrawElements(GL_TRIANGLES, kDrawOrder.size(), GL_UNSIGNED_BYTE,
232 kDrawOrder.data());
233 if (checkEglError("glDrawElements")) {
234 return false;
235 }
236
237 return true;
238}
239
240EglTextureProgram::EglTextureProgram() {
241 if (!isGlExtensionSupported(kGlExtYuvTarget)) {
242 ALOGE(
243 "Cannot initialize external texture program due to missing "
244 "GL_EXT_YUV_target extension");
245 return;
246 }
247
248 if (initialize(kExternalTextureVertexShader, kExternalTextureFragmentShader)) {
249 ALOGV("Successfully initialized EGL shaders for external texture program.");
250 } else {
251 ALOGE("External texture EGL shader program initialization failed.");
252 }
253}
254
255bool EglTextureProgram::draw(GLuint textureId) {
256 // Load compiled shader.
257 glUseProgram(mProgram);
258 if (checkEglError("glUseProgram")) {
259 return false;
260 }
261
262 // Pass vertex array to the shader.
263 int positionHandle = glGetAttribLocation(mProgram, "aPosition");
264 glEnableVertexAttribArray(positionHandle);
265 glVertexAttribPointer(positionHandle, kCoordsPerVertex, GL_FLOAT, false,
266 kSquareCoords.size(), kSquareCoords.data());
267
268 // Pass texture coordinates corresponding to vertex array to the shader.
269 int textureCoordHandle = glGetAttribLocation(mProgram, "aTextureCoord");
270 glEnableVertexAttribArray(textureCoordHandle);
271 glVertexAttribPointer(textureCoordHandle, 2, GL_FLOAT, false,
272 kTextureCoords.size(), kTextureCoords.data());
273
274 // Configure texture for the shader.
275 int textureHandle = glGetUniformLocation(mProgram, "uTexture");
276 glActiveTexture(GL_TEXTURE0);
277 glBindTexture(GL_TEXTURE_EXTERNAL_OES, textureId);
278 glUniform1i(textureHandle, 0);
279
280 // Draw triangle strip forming a square filling the viewport.
281 glDrawElements(GL_TRIANGLES, kDrawOrder.size(), GL_UNSIGNED_BYTE,
282 kDrawOrder.data());
283 if (checkEglError("glDrawElements")) {
284 return false;
285 }
286
287 return true;
288}
289
290} // namespace virtualcamera
291} // namespace companion
292} // namespace android