blob: 4eb5eb6a250853cc56fd9ecf4e62aa005a79600e [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");
Mathias Agopian3f844832013-08-07 21:24:32 -070040 glLinkProgram(programId);
41
42 GLint status;
43 glGetProgramiv(programId, GL_LINK_STATUS, &status);
44 if (status != GL_TRUE) {
45 ALOGE("Error while linking shaders:");
46 GLint infoLen = 0;
47 glGetProgramiv(programId, GL_INFO_LOG_LENGTH, &infoLen);
48 if (infoLen > 1) {
49 GLchar log[infoLen];
50 glGetProgramInfoLog(programId, infoLen, 0, &log[0]);
51 ALOGE("%s", log);
52 }
53 glDetachShader(programId, vertexId);
54 glDetachShader(programId, fragmentId);
55 glDeleteShader(vertexId);
56 glDeleteShader(fragmentId);
57 glDeleteProgram(programId);
58 } else {
59 mProgram = programId;
60 mVertexShader = vertexId;
61 mFragmentShader = fragmentId;
62 mInitialized = true;
Mathias Agopian3f844832013-08-07 21:24:32 -070063 mProjectionMatrixLoc = glGetUniformLocation(programId, "projection");
64 mTextureMatrixLoc = glGetUniformLocation(programId, "texture");
65 mSamplerLoc = glGetUniformLocation(programId, "sampler");
66 mColorLoc = glGetUniformLocation(programId, "color");
Peiyong Linfb069302018-04-25 14:34:31 -070067 mDisplayMaxLuminanceLoc = glGetUniformLocation(programId, "displayMaxLuminance");
Peiyong Lin1a70eca2019-11-15 09:33:33 -080068 mMaxMasteringLuminanceLoc = glGetUniformLocation(programId, "maxMasteringLuminance");
69 mMaxContentLuminanceLoc = glGetUniformLocation(programId, "maxContentLuminance");
Peiyong Lina296b0c2018-04-30 16:55:29 -070070 mInputTransformMatrixLoc = glGetUniformLocation(programId, "inputTransformMatrix");
71 mOutputTransformMatrixLoc = glGetUniformLocation(programId, "outputTransformMatrix");
Lucas Dupin1b6531c2018-07-05 17:18:21 -070072 mCornerRadiusLoc = glGetUniformLocation(programId, "cornerRadius");
73 mCropCenterLoc = glGetUniformLocation(programId, "cropCenter");
Mathias Agopian3f844832013-08-07 21:24:32 -070074
75 // set-up the default values for our uniforms
76 glUseProgram(programId);
Chia-I Wub027f802017-11-29 14:00:52 -080077 glUniformMatrix4fv(mProjectionMatrixLoc, 1, GL_FALSE, mat4().asArray());
Mathias Agopian3f844832013-08-07 21:24:32 -070078 glEnableVertexAttribArray(0);
79 }
80}
81
Mathias Agopian3f844832013-08-07 21:24:32 -070082bool Program::isValid() const {
83 return mInitialized;
84}
85
86void Program::use() {
87 glUseProgram(mProgram);
88}
89
90GLuint Program::getAttrib(const char* name) const {
91 // TODO: maybe use a local cache
92 return glGetAttribLocation(mProgram, name);
93}
94
95GLint Program::getUniform(const char* name) const {
96 // TODO: maybe use a local cache
97 return glGetUniformLocation(mProgram, name);
98}
99
100GLuint Program::buildShader(const char* source, GLenum type) {
101 GLuint shader = glCreateShader(type);
102 glShaderSource(shader, 1, &source, 0);
103 glCompileShader(shader);
104 GLint status;
105 glGetShaderiv(shader, GL_COMPILE_STATUS, &status);
106 if (status != GL_TRUE) {
107 // Some drivers return wrong values for GL_INFO_LOG_LENGTH
108 // use a fixed size instead
109 GLchar log[512];
110 glGetShaderInfoLog(shader, sizeof(log), 0, log);
111 ALOGE("Error while compiling shader: \n%s\n%s", source, log);
112 glDeleteShader(shader);
113 return 0;
114 }
115 return shader;
116}
117
Mathias Agopian3f844832013-08-07 21:24:32 -0700118void Program::setUniforms(const Description& desc) {
Mathias Agopian3f844832013-08-07 21:24:32 -0700119 // TODO: we should have a mechanism here to not always reset uniforms that
120 // didn't change for this program.
121
122 if (mSamplerLoc >= 0) {
123 glUniform1i(mSamplerLoc, 0);
Peiyong Lin70b26ce2018-09-18 19:02:39 -0700124 glUniformMatrix4fv(mTextureMatrixLoc, 1, GL_FALSE, desc.texture.getMatrix().asArray());
Mathias Agopian3f844832013-08-07 21:24:32 -0700125 }
Mathias Agopian3f844832013-08-07 21:24:32 -0700126 if (mColorLoc >= 0) {
Peiyong Lin70b26ce2018-09-18 19:02:39 -0700127 const float color[4] = {desc.color.r, desc.color.g, desc.color.b, desc.color.a};
chaviw13fdc492017-06-27 12:40:18 -0700128 glUniform4fv(mColorLoc, 1, color);
Mathias Agopian3f844832013-08-07 21:24:32 -0700129 }
Peiyong Lina296b0c2018-04-30 16:55:29 -0700130 if (mInputTransformMatrixLoc >= 0) {
Peiyong Lin70b26ce2018-09-18 19:02:39 -0700131 mat4 inputTransformMatrix = desc.inputTransformMatrix;
Peiyong Lin00f9c022018-05-09 15:35:33 -0700132 glUniformMatrix4fv(mInputTransformMatrixLoc, 1, GL_FALSE, inputTransformMatrix.asArray());
Peiyong Lina296b0c2018-04-30 16:55:29 -0700133 }
134 if (mOutputTransformMatrixLoc >= 0) {
135 // The output transform matrix and color matrix can be combined as one matrix
136 // that is applied right before applying OETF.
Peiyong Lin70b26ce2018-09-18 19:02:39 -0700137 mat4 outputTransformMatrix = desc.colorMatrix * desc.outputTransformMatrix;
Peiyong Lin46080ef2018-10-26 18:43:14 -0700138 glUniformMatrix4fv(mOutputTransformMatrixLoc, 1, GL_FALSE, outputTransformMatrix.asArray());
Mathias Agopianff2ed702013-09-01 21:36:12 -0700139 }
Peiyong Linfb069302018-04-25 14:34:31 -0700140 if (mDisplayMaxLuminanceLoc >= 0) {
Peiyong Lin46080ef2018-10-26 18:43:14 -0700141 glUniform1f(mDisplayMaxLuminanceLoc, desc.displayMaxLuminance);
Peiyong Linfb069302018-04-25 14:34:31 -0700142 }
Peiyong Lin1a70eca2019-11-15 09:33:33 -0800143 if (mMaxMasteringLuminanceLoc >= 0) {
144 glUniform1f(mMaxMasteringLuminanceLoc, desc.maxMasteringLuminance);
145 }
146 if (mMaxContentLuminanceLoc >= 0) {
147 glUniform1f(mMaxContentLuminanceLoc, desc.maxContentLuminance);
148 }
Lucas Dupin1b6531c2018-07-05 17:18:21 -0700149 if (mCornerRadiusLoc >= 0) {
150 glUniform1f(mCornerRadiusLoc, desc.cornerRadius);
151 }
152 if (mCropCenterLoc >= 0) {
153 glUniform2f(mCropCenterLoc, desc.cropSize.x / 2.0f, desc.cropSize.y / 2.0f);
154 }
Mathias Agopian3f844832013-08-07 21:24:32 -0700155 // these uniforms are always present
Peiyong Lin70b26ce2018-09-18 19:02:39 -0700156 glUniformMatrix4fv(mProjectionMatrixLoc, 1, GL_FALSE, desc.projectionMatrix.asArray());
Mathias Agopian3f844832013-08-07 21:24:32 -0700157}
158
Peiyong Lin46080ef2018-10-26 18:43:14 -0700159} // namespace gl
160} // namespace renderengine
161} // namespace android