blob: 26f61667616b45058450b993026c5cced8ae92b4 [file] [log] [blame]
Mathias Agopian3f844832013-08-07 21:24:32 -07001/*Gluint
2 * Copyright 2013 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
Peiyong Lin833074a2018-08-28 11:53:54 -070017#include "Program.h"
Peiyong Lincbc184f2018-08-22 13:24:10 -070018
Mathias Agopian3f844832013-08-07 21:24:32 -070019#include <stdint.h>
20
Mark Salyzyn7823e122016-09-29 08:08:05 -070021#include <log/log.h>
Chia-I Wub027f802017-11-29 14:00:52 -080022#include <math/mat4.h>
Peiyong Lincbc184f2018-08-22 13:24:10 -070023#include <utils/String8.h>
Peiyong Lin833074a2018-08-28 11:53:54 -070024#include "ProgramCache.h"
Mathias Agopian3f844832013-08-07 21:24:32 -070025
26namespace android {
Peiyong Lin833074a2018-08-28 11:53:54 -070027namespace renderengine {
28namespace gl {
Mathias Agopian3f844832013-08-07 21:24:32 -070029
Mark Salyzyn92dc3fc2014-03-12 13:12:44 -070030Program::Program(const ProgramCache::Key& /*needs*/, const char* vertex, const char* fragment)
Chia-I Wub027f802017-11-29 14:00:52 -080031 : mInitialized(false) {
Mathias Agopian3f844832013-08-07 21:24:32 -070032 GLuint vertexId = buildShader(vertex, GL_VERTEX_SHADER);
33 GLuint fragmentId = buildShader(fragment, GL_FRAGMENT_SHADER);
34 GLuint programId = glCreateProgram();
35 glAttachShader(programId, vertexId);
36 glAttachShader(programId, fragmentId);
37 glBindAttribLocation(programId, position, "position");
38 glBindAttribLocation(programId, texCoords, "texCoords");
Lucas Dupin1b6531c2018-07-05 17:18:21 -070039 glBindAttribLocation(programId, cropCoords, "cropCoords");
Vishnu Nair440992f2019-12-09 19:53:19 -080040 glBindAttribLocation(programId, shadowColor, "shadowColor");
41 glBindAttribLocation(programId, shadowParams, "shadowParams");
Mathias Agopian3f844832013-08-07 21:24:32 -070042 glLinkProgram(programId);
43
44 GLint status;
45 glGetProgramiv(programId, GL_LINK_STATUS, &status);
46 if (status != GL_TRUE) {
47 ALOGE("Error while linking shaders:");
48 GLint infoLen = 0;
49 glGetProgramiv(programId, GL_INFO_LOG_LENGTH, &infoLen);
50 if (infoLen > 1) {
51 GLchar log[infoLen];
52 glGetProgramInfoLog(programId, infoLen, 0, &log[0]);
53 ALOGE("%s", log);
54 }
55 glDetachShader(programId, vertexId);
56 glDetachShader(programId, fragmentId);
57 glDeleteShader(vertexId);
58 glDeleteShader(fragmentId);
59 glDeleteProgram(programId);
60 } else {
61 mProgram = programId;
62 mVertexShader = vertexId;
63 mFragmentShader = fragmentId;
64 mInitialized = true;
Mathias Agopian3f844832013-08-07 21:24:32 -070065 mProjectionMatrixLoc = glGetUniformLocation(programId, "projection");
66 mTextureMatrixLoc = glGetUniformLocation(programId, "texture");
67 mSamplerLoc = glGetUniformLocation(programId, "sampler");
68 mColorLoc = glGetUniformLocation(programId, "color");
KaiChieh Chuang436fc192020-09-07 13:48:42 +080069 mDisplayColorMatrixLoc = glGetUniformLocation(programId, "displayColorMatrix");
Peiyong Linfb069302018-04-25 14:34:31 -070070 mDisplayMaxLuminanceLoc = glGetUniformLocation(programId, "displayMaxLuminance");
Peiyong Lin1a70eca2019-11-15 09:33:33 -080071 mMaxMasteringLuminanceLoc = glGetUniformLocation(programId, "maxMasteringLuminance");
72 mMaxContentLuminanceLoc = glGetUniformLocation(programId, "maxContentLuminance");
Peiyong Lina296b0c2018-04-30 16:55:29 -070073 mInputTransformMatrixLoc = glGetUniformLocation(programId, "inputTransformMatrix");
74 mOutputTransformMatrixLoc = glGetUniformLocation(programId, "outputTransformMatrix");
Lucas Dupin1b6531c2018-07-05 17:18:21 -070075 mCornerRadiusLoc = glGetUniformLocation(programId, "cornerRadius");
76 mCropCenterLoc = glGetUniformLocation(programId, "cropCenter");
Mathias Agopian3f844832013-08-07 21:24:32 -070077
78 // set-up the default values for our uniforms
79 glUseProgram(programId);
Chia-I Wub027f802017-11-29 14:00:52 -080080 glUniformMatrix4fv(mProjectionMatrixLoc, 1, GL_FALSE, mat4().asArray());
Mathias Agopian3f844832013-08-07 21:24:32 -070081 glEnableVertexAttribArray(0);
82 }
83}
84
Alec Mouri2725c092020-11-25 09:53:56 -080085Program::~Program() {
86 glDetachShader(mProgram, mVertexShader);
87 glDetachShader(mProgram, mFragmentShader);
88 glDeleteShader(mVertexShader);
89 glDeleteShader(mFragmentShader);
90 glDeleteProgram(mProgram);
91}
92
Mathias Agopian3f844832013-08-07 21:24:32 -070093bool Program::isValid() const {
94 return mInitialized;
95}
96
97void Program::use() {
98 glUseProgram(mProgram);
99}
100
101GLuint Program::getAttrib(const char* name) const {
102 // TODO: maybe use a local cache
103 return glGetAttribLocation(mProgram, name);
104}
105
106GLint Program::getUniform(const char* name) const {
107 // TODO: maybe use a local cache
108 return glGetUniformLocation(mProgram, name);
109}
110
111GLuint Program::buildShader(const char* source, GLenum type) {
112 GLuint shader = glCreateShader(type);
113 glShaderSource(shader, 1, &source, 0);
114 glCompileShader(shader);
115 GLint status;
116 glGetShaderiv(shader, GL_COMPILE_STATUS, &status);
117 if (status != GL_TRUE) {
118 // Some drivers return wrong values for GL_INFO_LOG_LENGTH
119 // use a fixed size instead
120 GLchar log[512];
121 glGetShaderInfoLog(shader, sizeof(log), 0, log);
122 ALOGE("Error while compiling shader: \n%s\n%s", source, log);
123 glDeleteShader(shader);
124 return 0;
125 }
126 return shader;
127}
128
Mathias Agopian3f844832013-08-07 21:24:32 -0700129void Program::setUniforms(const Description& desc) {
Mathias Agopian3f844832013-08-07 21:24:32 -0700130 // TODO: we should have a mechanism here to not always reset uniforms that
131 // didn't change for this program.
132
133 if (mSamplerLoc >= 0) {
134 glUniform1i(mSamplerLoc, 0);
Peiyong Lin70b26ce2018-09-18 19:02:39 -0700135 glUniformMatrix4fv(mTextureMatrixLoc, 1, GL_FALSE, desc.texture.getMatrix().asArray());
Mathias Agopian3f844832013-08-07 21:24:32 -0700136 }
Mathias Agopian3f844832013-08-07 21:24:32 -0700137 if (mColorLoc >= 0) {
Peiyong Lin70b26ce2018-09-18 19:02:39 -0700138 const float color[4] = {desc.color.r, desc.color.g, desc.color.b, desc.color.a};
chaviw13fdc492017-06-27 12:40:18 -0700139 glUniform4fv(mColorLoc, 1, color);
Mathias Agopian3f844832013-08-07 21:24:32 -0700140 }
KaiChieh Chuang436fc192020-09-07 13:48:42 +0800141 if (mDisplayColorMatrixLoc >= 0) {
142 glUniformMatrix4fv(mDisplayColorMatrixLoc, 1, GL_FALSE, desc.displayColorMatrix.asArray());
143 }
Peiyong Lina296b0c2018-04-30 16:55:29 -0700144 if (mInputTransformMatrixLoc >= 0) {
Peiyong Lin70b26ce2018-09-18 19:02:39 -0700145 mat4 inputTransformMatrix = desc.inputTransformMatrix;
Peiyong Lin00f9c022018-05-09 15:35:33 -0700146 glUniformMatrix4fv(mInputTransformMatrixLoc, 1, GL_FALSE, inputTransformMatrix.asArray());
Peiyong Lina296b0c2018-04-30 16:55:29 -0700147 }
148 if (mOutputTransformMatrixLoc >= 0) {
149 // The output transform matrix and color matrix can be combined as one matrix
150 // that is applied right before applying OETF.
Peiyong Lin70b26ce2018-09-18 19:02:39 -0700151 mat4 outputTransformMatrix = desc.colorMatrix * desc.outputTransformMatrix;
Peiyong Lin46080ef2018-10-26 18:43:14 -0700152 glUniformMatrix4fv(mOutputTransformMatrixLoc, 1, GL_FALSE, outputTransformMatrix.asArray());
Mathias Agopianff2ed702013-09-01 21:36:12 -0700153 }
Peiyong Linfb069302018-04-25 14:34:31 -0700154 if (mDisplayMaxLuminanceLoc >= 0) {
Peiyong Lin46080ef2018-10-26 18:43:14 -0700155 glUniform1f(mDisplayMaxLuminanceLoc, desc.displayMaxLuminance);
Peiyong Linfb069302018-04-25 14:34:31 -0700156 }
Peiyong Lin1a70eca2019-11-15 09:33:33 -0800157 if (mMaxMasteringLuminanceLoc >= 0) {
158 glUniform1f(mMaxMasteringLuminanceLoc, desc.maxMasteringLuminance);
159 }
160 if (mMaxContentLuminanceLoc >= 0) {
161 glUniform1f(mMaxContentLuminanceLoc, desc.maxContentLuminance);
162 }
Lucas Dupin1b6531c2018-07-05 17:18:21 -0700163 if (mCornerRadiusLoc >= 0) {
164 glUniform1f(mCornerRadiusLoc, desc.cornerRadius);
165 }
166 if (mCropCenterLoc >= 0) {
167 glUniform2f(mCropCenterLoc, desc.cropSize.x / 2.0f, desc.cropSize.y / 2.0f);
168 }
Mathias Agopian3f844832013-08-07 21:24:32 -0700169 // these uniforms are always present
Peiyong Lin70b26ce2018-09-18 19:02:39 -0700170 glUniformMatrix4fv(mProjectionMatrixLoc, 1, GL_FALSE, desc.projectionMatrix.asArray());
Mathias Agopian3f844832013-08-07 21:24:32 -0700171}
172
Peiyong Lin46080ef2018-10-26 18:43:14 -0700173} // namespace gl
174} // namespace renderengine
175} // namespace android