blob: 95adaca73b9cc7263bda413e1443fbb8d83ed1b4 [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
17#include <stdint.h>
18
Mark Salyzyn7823e122016-09-29 08:08:05 -070019#include <log/log.h>
Mark Salyzyna5e161b2016-09-29 08:08:05 -070020#include <utils/String8.h>
Mathias Agopian3f844832013-08-07 21:24:32 -070021
Chia-I Wub027f802017-11-29 14:00:52 -080022#include <math/mat4.h>
23#include "Description.h"
Mathias Agopian3f844832013-08-07 21:24:32 -070024#include "Program.h"
25#include "ProgramCache.h"
Mathias Agopian3f844832013-08-07 21:24:32 -070026
27namespace android {
28
Mark Salyzyn92dc3fc2014-03-12 13:12:44 -070029Program::Program(const ProgramCache::Key& /*needs*/, const char* vertex, const char* fragment)
Chia-I Wub027f802017-11-29 14:00:52 -080030 : mInitialized(false) {
Mathias Agopian3f844832013-08-07 21:24:32 -070031 GLuint vertexId = buildShader(vertex, GL_VERTEX_SHADER);
32 GLuint fragmentId = buildShader(fragment, GL_FRAGMENT_SHADER);
33 GLuint programId = glCreateProgram();
34 glAttachShader(programId, vertexId);
35 glAttachShader(programId, fragmentId);
36 glBindAttribLocation(programId, position, "position");
37 glBindAttribLocation(programId, texCoords, "texCoords");
38 glLinkProgram(programId);
39
40 GLint status;
41 glGetProgramiv(programId, GL_LINK_STATUS, &status);
42 if (status != GL_TRUE) {
43 ALOGE("Error while linking shaders:");
44 GLint infoLen = 0;
45 glGetProgramiv(programId, GL_INFO_LOG_LENGTH, &infoLen);
46 if (infoLen > 1) {
47 GLchar log[infoLen];
48 glGetProgramInfoLog(programId, infoLen, 0, &log[0]);
49 ALOGE("%s", log);
50 }
51 glDetachShader(programId, vertexId);
52 glDetachShader(programId, fragmentId);
53 glDeleteShader(vertexId);
54 glDeleteShader(fragmentId);
55 glDeleteProgram(programId);
56 } else {
57 mProgram = programId;
58 mVertexShader = vertexId;
59 mFragmentShader = fragmentId;
60 mInitialized = true;
Mathias Agopian3f844832013-08-07 21:24:32 -070061 mProjectionMatrixLoc = glGetUniformLocation(programId, "projection");
62 mTextureMatrixLoc = glGetUniformLocation(programId, "texture");
63 mSamplerLoc = glGetUniformLocation(programId, "sampler");
64 mColorLoc = glGetUniformLocation(programId, "color");
Peiyong Linfb069302018-04-25 14:34:31 -070065 mDisplayMaxLuminanceLoc = glGetUniformLocation(programId, "displayMaxLuminance");
Peiyong Lina296b0c2018-04-30 16:55:29 -070066 mInputTransformMatrixLoc = glGetUniformLocation(programId, "inputTransformMatrix");
67 mOutputTransformMatrixLoc = glGetUniformLocation(programId, "outputTransformMatrix");
Mathias Agopian3f844832013-08-07 21:24:32 -070068
69 // set-up the default values for our uniforms
70 glUseProgram(programId);
Chia-I Wub027f802017-11-29 14:00:52 -080071 glUniformMatrix4fv(mProjectionMatrixLoc, 1, GL_FALSE, mat4().asArray());
Mathias Agopian3f844832013-08-07 21:24:32 -070072 glEnableVertexAttribArray(0);
73 }
74}
75
Chia-I Wub027f802017-11-29 14:00:52 -080076Program::~Program() {}
Mathias Agopian3f844832013-08-07 21:24:32 -070077
78bool Program::isValid() const {
79 return mInitialized;
80}
81
82void Program::use() {
83 glUseProgram(mProgram);
84}
85
86GLuint Program::getAttrib(const char* name) const {
87 // TODO: maybe use a local cache
88 return glGetAttribLocation(mProgram, name);
89}
90
91GLint Program::getUniform(const char* name) const {
92 // TODO: maybe use a local cache
93 return glGetUniformLocation(mProgram, name);
94}
95
96GLuint Program::buildShader(const char* source, GLenum type) {
97 GLuint shader = glCreateShader(type);
98 glShaderSource(shader, 1, &source, 0);
99 glCompileShader(shader);
100 GLint status;
101 glGetShaderiv(shader, GL_COMPILE_STATUS, &status);
102 if (status != GL_TRUE) {
103 // Some drivers return wrong values for GL_INFO_LOG_LENGTH
104 // use a fixed size instead
105 GLchar log[512];
106 glGetShaderInfoLog(shader, sizeof(log), 0, log);
107 ALOGE("Error while compiling shader: \n%s\n%s", source, log);
108 glDeleteShader(shader);
109 return 0;
110 }
111 return shader;
112}
113
Mark Salyzyn92dc3fc2014-03-12 13:12:44 -0700114String8& Program::dumpShader(String8& result, GLenum /*type*/) {
Mathias Agopian3f844832013-08-07 21:24:32 -0700115 GLuint shader = GL_FRAGMENT_SHADER ? mFragmentShader : mVertexShader;
116 GLint l;
117 glGetShaderiv(shader, GL_SHADER_SOURCE_LENGTH, &l);
118 char* src = new char[l];
Peiyong Lin566a3b42018-01-09 18:22:43 -0800119 glGetShaderSource(shader, l, nullptr, src);
Mathias Agopian3f844832013-08-07 21:24:32 -0700120 result.append(src);
Chia-I Wub027f802017-11-29 14:00:52 -0800121 delete[] src;
Mathias Agopian3f844832013-08-07 21:24:32 -0700122 return result;
123}
124
125void Program::setUniforms(const Description& desc) {
Mathias Agopian3f844832013-08-07 21:24:32 -0700126 // TODO: we should have a mechanism here to not always reset uniforms that
127 // didn't change for this program.
128
129 if (mSamplerLoc >= 0) {
130 glUniform1i(mSamplerLoc, 0);
Mathias Agopiana8c386f2013-08-26 20:42:07 -0700131 glUniformMatrix4fv(mTextureMatrixLoc, 1, GL_FALSE, desc.mTexture.getMatrix().asArray());
Mathias Agopian3f844832013-08-07 21:24:32 -0700132 }
Mathias Agopian3f844832013-08-07 21:24:32 -0700133 if (mColorLoc >= 0) {
chaviw89e197a2017-10-05 15:38:42 -0700134 const float color[4] = {desc.mColor.r, desc.mColor.g, desc.mColor.b, desc.mColor.a};
chaviw13fdc492017-06-27 12:40:18 -0700135 glUniform4fv(mColorLoc, 1, color);
Mathias Agopian3f844832013-08-07 21:24:32 -0700136 }
Peiyong Lina296b0c2018-04-30 16:55:29 -0700137 if (mInputTransformMatrixLoc >= 0) {
Peiyong Lin76dd77a2018-05-09 15:35:33 -0700138 // If the input transform matrix is not identity matrix, we want to merge
139 // the saturation matrix with input transform matrix so that the saturation
140 // matrix is applied at the correct stage.
141 mat4 inputTransformMatrix = mat4(desc.mInputTransformMatrix) * desc.mSaturationMatrix;
142 glUniformMatrix4fv(mInputTransformMatrixLoc, 1, GL_FALSE, inputTransformMatrix.asArray());
Peiyong Lina296b0c2018-04-30 16:55:29 -0700143 }
144 if (mOutputTransformMatrixLoc >= 0) {
145 // The output transform matrix and color matrix can be combined as one matrix
146 // that is applied right before applying OETF.
147 mat4 outputTransformMatrix = desc.mColorMatrix * desc.mOutputTransformMatrix;
Peiyong Lin76dd77a2018-05-09 15:35:33 -0700148 // If there is no input transform matrix, we want to merge the saturation
149 // matrix with output transform matrix to avoid extra matrix multiplication
150 // in shader.
151 if (mInputTransformMatrixLoc < 0) {
152 outputTransformMatrix *= desc.mSaturationMatrix;
153 }
Peiyong Lina296b0c2018-04-30 16:55:29 -0700154 glUniformMatrix4fv(mOutputTransformMatrixLoc, 1, GL_FALSE,
155 outputTransformMatrix.asArray());
Mathias Agopianff2ed702013-09-01 21:36:12 -0700156 }
Peiyong Linfb069302018-04-25 14:34:31 -0700157 if (mDisplayMaxLuminanceLoc >= 0) {
158 glUniform1f(mDisplayMaxLuminanceLoc, desc.mDisplayMaxLuminance);
159 }
Mathias Agopian3f844832013-08-07 21:24:32 -0700160 // these uniforms are always present
Mathias Agopiana8c386f2013-08-26 20:42:07 -0700161 glUniformMatrix4fv(mProjectionMatrixLoc, 1, GL_FALSE, desc.mProjectionMatrix.asArray());
Mathias Agopian3f844832013-08-07 21:24:32 -0700162}
163
164} /* namespace android */