blob: 7d2ea90d827c23a67d920e40c82d6776c2564659 [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");
39 glLinkProgram(programId);
40
41 GLint status;
42 glGetProgramiv(programId, GL_LINK_STATUS, &status);
43 if (status != GL_TRUE) {
44 ALOGE("Error while linking shaders:");
45 GLint infoLen = 0;
46 glGetProgramiv(programId, GL_INFO_LOG_LENGTH, &infoLen);
47 if (infoLen > 1) {
48 GLchar log[infoLen];
49 glGetProgramInfoLog(programId, infoLen, 0, &log[0]);
50 ALOGE("%s", log);
51 }
52 glDetachShader(programId, vertexId);
53 glDetachShader(programId, fragmentId);
54 glDeleteShader(vertexId);
55 glDeleteShader(fragmentId);
56 glDeleteProgram(programId);
57 } else {
58 mProgram = programId;
59 mVertexShader = vertexId;
60 mFragmentShader = fragmentId;
61 mInitialized = true;
Mathias Agopian3f844832013-08-07 21:24:32 -070062 mProjectionMatrixLoc = glGetUniformLocation(programId, "projection");
63 mTextureMatrixLoc = glGetUniformLocation(programId, "texture");
64 mSamplerLoc = glGetUniformLocation(programId, "sampler");
65 mColorLoc = glGetUniformLocation(programId, "color");
Peiyong Linfb069302018-04-25 14:34:31 -070066 mDisplayMaxLuminanceLoc = glGetUniformLocation(programId, "displayMaxLuminance");
Peiyong Lina296b0c2018-04-30 16:55:29 -070067 mInputTransformMatrixLoc = glGetUniformLocation(programId, "inputTransformMatrix");
68 mOutputTransformMatrixLoc = glGetUniformLocation(programId, "outputTransformMatrix");
Mathias Agopian3f844832013-08-07 21:24:32 -070069
70 // set-up the default values for our uniforms
71 glUseProgram(programId);
Chia-I Wub027f802017-11-29 14:00:52 -080072 glUniformMatrix4fv(mProjectionMatrixLoc, 1, GL_FALSE, mat4().asArray());
Mathias Agopian3f844832013-08-07 21:24:32 -070073 glEnableVertexAttribArray(0);
74 }
75}
76
Mathias Agopian3f844832013-08-07 21:24:32 -070077bool Program::isValid() const {
78 return mInitialized;
79}
80
81void Program::use() {
82 glUseProgram(mProgram);
83}
84
85GLuint Program::getAttrib(const char* name) const {
86 // TODO: maybe use a local cache
87 return glGetAttribLocation(mProgram, name);
88}
89
90GLint Program::getUniform(const char* name) const {
91 // TODO: maybe use a local cache
92 return glGetUniformLocation(mProgram, name);
93}
94
95GLuint Program::buildShader(const char* source, GLenum type) {
96 GLuint shader = glCreateShader(type);
97 glShaderSource(shader, 1, &source, 0);
98 glCompileShader(shader);
99 GLint status;
100 glGetShaderiv(shader, GL_COMPILE_STATUS, &status);
101 if (status != GL_TRUE) {
102 // Some drivers return wrong values for GL_INFO_LOG_LENGTH
103 // use a fixed size instead
104 GLchar log[512];
105 glGetShaderInfoLog(shader, sizeof(log), 0, log);
106 ALOGE("Error while compiling shader: \n%s\n%s", source, log);
107 glDeleteShader(shader);
108 return 0;
109 }
110 return shader;
111}
112
Mathias Agopian3f844832013-08-07 21:24:32 -0700113void Program::setUniforms(const Description& desc) {
Mathias Agopian3f844832013-08-07 21:24:32 -0700114 // TODO: we should have a mechanism here to not always reset uniforms that
115 // didn't change for this program.
116
117 if (mSamplerLoc >= 0) {
118 glUniform1i(mSamplerLoc, 0);
Peiyong Lin70b26ce2018-09-18 19:02:39 -0700119 glUniformMatrix4fv(mTextureMatrixLoc, 1, GL_FALSE, desc.texture.getMatrix().asArray());
Mathias Agopian3f844832013-08-07 21:24:32 -0700120 }
Mathias Agopian3f844832013-08-07 21:24:32 -0700121 if (mColorLoc >= 0) {
Peiyong Lin70b26ce2018-09-18 19:02:39 -0700122 const float color[4] = {desc.color.r, desc.color.g, desc.color.b, desc.color.a};
chaviw13fdc492017-06-27 12:40:18 -0700123 glUniform4fv(mColorLoc, 1, color);
Mathias Agopian3f844832013-08-07 21:24:32 -0700124 }
Peiyong Lina296b0c2018-04-30 16:55:29 -0700125 if (mInputTransformMatrixLoc >= 0) {
Peiyong Lin70b26ce2018-09-18 19:02:39 -0700126 mat4 inputTransformMatrix = desc.inputTransformMatrix;
Peiyong Lin00f9c022018-05-09 15:35:33 -0700127 glUniformMatrix4fv(mInputTransformMatrixLoc, 1, GL_FALSE, inputTransformMatrix.asArray());
Peiyong Lina296b0c2018-04-30 16:55:29 -0700128 }
129 if (mOutputTransformMatrixLoc >= 0) {
130 // The output transform matrix and color matrix can be combined as one matrix
131 // that is applied right before applying OETF.
Peiyong Lin70b26ce2018-09-18 19:02:39 -0700132 mat4 outputTransformMatrix = desc.colorMatrix * desc.outputTransformMatrix;
Peiyong Lin46080ef2018-10-26 18:43:14 -0700133 glUniformMatrix4fv(mOutputTransformMatrixLoc, 1, GL_FALSE, outputTransformMatrix.asArray());
Mathias Agopianff2ed702013-09-01 21:36:12 -0700134 }
Peiyong Linfb069302018-04-25 14:34:31 -0700135 if (mDisplayMaxLuminanceLoc >= 0) {
Peiyong Lin46080ef2018-10-26 18:43:14 -0700136 glUniform1f(mDisplayMaxLuminanceLoc, desc.displayMaxLuminance);
Peiyong Linfb069302018-04-25 14:34:31 -0700137 }
Mathias Agopian3f844832013-08-07 21:24:32 -0700138 // these uniforms are always present
Peiyong Lin70b26ce2018-09-18 19:02:39 -0700139 glUniformMatrix4fv(mProjectionMatrixLoc, 1, GL_FALSE, desc.projectionMatrix.asArray());
Mathias Agopian3f844832013-08-07 21:24:32 -0700140}
141
Peiyong Lin46080ef2018-10-26 18:43:14 -0700142} // namespace gl
143} // namespace renderengine
144} // namespace android