| Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 1 | /*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 Lin | 833074a | 2018-08-28 11:53:54 -0700 | [diff] [blame] | 17 | #include "Program.h" | 
| Peiyong Lin | cbc184f | 2018-08-22 13:24:10 -0700 | [diff] [blame] | 18 |  | 
| Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 19 | #include <stdint.h> | 
|  | 20 |  | 
| Mark Salyzyn | 7823e12 | 2016-09-29 08:08:05 -0700 | [diff] [blame] | 21 | #include <log/log.h> | 
| Chia-I Wu | b027f80 | 2017-11-29 14:00:52 -0800 | [diff] [blame] | 22 | #include <math/mat4.h> | 
| Peiyong Lin | cbc184f | 2018-08-22 13:24:10 -0700 | [diff] [blame] | 23 | #include <utils/String8.h> | 
| Peiyong Lin | 833074a | 2018-08-28 11:53:54 -0700 | [diff] [blame] | 24 | #include "ProgramCache.h" | 
| Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 25 |  | 
|  | 26 | namespace android { | 
| Peiyong Lin | 833074a | 2018-08-28 11:53:54 -0700 | [diff] [blame] | 27 | namespace renderengine { | 
|  | 28 | namespace gl { | 
| Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 29 |  | 
| Mark Salyzyn | 92dc3fc | 2014-03-12 13:12:44 -0700 | [diff] [blame] | 30 | Program::Program(const ProgramCache::Key& /*needs*/, const char* vertex, const char* fragment) | 
| Chia-I Wu | b027f80 | 2017-11-29 14:00:52 -0800 | [diff] [blame] | 31 | : mInitialized(false) { | 
| Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 32 | 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 Dupin | 1b6531c | 2018-07-05 17:18:21 -0700 | [diff] [blame] | 39 | glBindAttribLocation(programId, cropCoords, "cropCoords"); | 
| Vishnu Nair | 440992f | 2019-12-09 19:53:19 -0800 | [diff] [blame] | 40 | glBindAttribLocation(programId, shadowColor, "shadowColor"); | 
|  | 41 | glBindAttribLocation(programId, shadowParams, "shadowParams"); | 
| Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 42 | 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 Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 65 | mProjectionMatrixLoc = glGetUniformLocation(programId, "projection"); | 
|  | 66 | mTextureMatrixLoc = glGetUniformLocation(programId, "texture"); | 
|  | 67 | mSamplerLoc = glGetUniformLocation(programId, "sampler"); | 
|  | 68 | mColorLoc = glGetUniformLocation(programId, "color"); | 
| KaiChieh Chuang | ba2f0bc | 2020-09-07 13:48:42 +0800 | [diff] [blame] | 69 | mDisplayColorMatrixLoc = glGetUniformLocation(programId, "displayColorMatrix"); | 
| Peiyong Lin | fb06930 | 2018-04-25 14:34:31 -0700 | [diff] [blame] | 70 | mDisplayMaxLuminanceLoc = glGetUniformLocation(programId, "displayMaxLuminance"); | 
| Peiyong Lin | 1a70eca | 2019-11-15 09:33:33 -0800 | [diff] [blame] | 71 | mMaxMasteringLuminanceLoc = glGetUniformLocation(programId, "maxMasteringLuminance"); | 
|  | 72 | mMaxContentLuminanceLoc = glGetUniformLocation(programId, "maxContentLuminance"); | 
| Peiyong Lin | a296b0c | 2018-04-30 16:55:29 -0700 | [diff] [blame] | 73 | mInputTransformMatrixLoc = glGetUniformLocation(programId, "inputTransformMatrix"); | 
|  | 74 | mOutputTransformMatrixLoc = glGetUniformLocation(programId, "outputTransformMatrix"); | 
| Lucas Dupin | 1b6531c | 2018-07-05 17:18:21 -0700 | [diff] [blame] | 75 | mCornerRadiusLoc = glGetUniformLocation(programId, "cornerRadius"); | 
|  | 76 | mCropCenterLoc = glGetUniformLocation(programId, "cropCenter"); | 
| Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 77 |  | 
|  | 78 | // set-up the default values for our uniforms | 
|  | 79 | glUseProgram(programId); | 
| Chia-I Wu | b027f80 | 2017-11-29 14:00:52 -0800 | [diff] [blame] | 80 | glUniformMatrix4fv(mProjectionMatrixLoc, 1, GL_FALSE, mat4().asArray()); | 
| Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 81 | glEnableVertexAttribArray(0); | 
|  | 82 | } | 
|  | 83 | } | 
|  | 84 |  | 
| Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 85 | bool Program::isValid() const { | 
|  | 86 | return mInitialized; | 
|  | 87 | } | 
|  | 88 |  | 
|  | 89 | void Program::use() { | 
|  | 90 | glUseProgram(mProgram); | 
|  | 91 | } | 
|  | 92 |  | 
|  | 93 | GLuint Program::getAttrib(const char* name) const { | 
|  | 94 | // TODO: maybe use a local cache | 
|  | 95 | return glGetAttribLocation(mProgram, name); | 
|  | 96 | } | 
|  | 97 |  | 
|  | 98 | GLint Program::getUniform(const char* name) const { | 
|  | 99 | // TODO: maybe use a local cache | 
|  | 100 | return glGetUniformLocation(mProgram, name); | 
|  | 101 | } | 
|  | 102 |  | 
|  | 103 | GLuint Program::buildShader(const char* source, GLenum type) { | 
|  | 104 | GLuint shader = glCreateShader(type); | 
|  | 105 | glShaderSource(shader, 1, &source, 0); | 
|  | 106 | glCompileShader(shader); | 
|  | 107 | GLint status; | 
|  | 108 | glGetShaderiv(shader, GL_COMPILE_STATUS, &status); | 
|  | 109 | if (status != GL_TRUE) { | 
|  | 110 | // Some drivers return wrong values for GL_INFO_LOG_LENGTH | 
|  | 111 | // use a fixed size instead | 
|  | 112 | GLchar log[512]; | 
|  | 113 | glGetShaderInfoLog(shader, sizeof(log), 0, log); | 
|  | 114 | ALOGE("Error while compiling shader: \n%s\n%s", source, log); | 
|  | 115 | glDeleteShader(shader); | 
|  | 116 | return 0; | 
|  | 117 | } | 
|  | 118 | return shader; | 
|  | 119 | } | 
|  | 120 |  | 
| Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 121 | void Program::setUniforms(const Description& desc) { | 
| Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 122 | // TODO: we should have a mechanism here to not always reset uniforms that | 
|  | 123 | // didn't change for this program. | 
|  | 124 |  | 
|  | 125 | if (mSamplerLoc >= 0) { | 
|  | 126 | glUniform1i(mSamplerLoc, 0); | 
| Peiyong Lin | 70b26ce | 2018-09-18 19:02:39 -0700 | [diff] [blame] | 127 | glUniformMatrix4fv(mTextureMatrixLoc, 1, GL_FALSE, desc.texture.getMatrix().asArray()); | 
| Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 128 | } | 
| Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 129 | if (mColorLoc >= 0) { | 
| Peiyong Lin | 70b26ce | 2018-09-18 19:02:39 -0700 | [diff] [blame] | 130 | const float color[4] = {desc.color.r, desc.color.g, desc.color.b, desc.color.a}; | 
| chaviw | 13fdc49 | 2017-06-27 12:40:18 -0700 | [diff] [blame] | 131 | glUniform4fv(mColorLoc, 1, color); | 
| Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 132 | } | 
| KaiChieh Chuang | ba2f0bc | 2020-09-07 13:48:42 +0800 | [diff] [blame] | 133 | if (mDisplayColorMatrixLoc >= 0) { | 
|  | 134 | glUniformMatrix4fv(mDisplayColorMatrixLoc, 1, GL_FALSE, desc.displayColorMatrix.asArray()); | 
|  | 135 | } | 
| Peiyong Lin | a296b0c | 2018-04-30 16:55:29 -0700 | [diff] [blame] | 136 | if (mInputTransformMatrixLoc >= 0) { | 
| Peiyong Lin | 70b26ce | 2018-09-18 19:02:39 -0700 | [diff] [blame] | 137 | mat4 inputTransformMatrix = desc.inputTransformMatrix; | 
| Peiyong Lin | 00f9c02 | 2018-05-09 15:35:33 -0700 | [diff] [blame] | 138 | glUniformMatrix4fv(mInputTransformMatrixLoc, 1, GL_FALSE, inputTransformMatrix.asArray()); | 
| Peiyong Lin | a296b0c | 2018-04-30 16:55:29 -0700 | [diff] [blame] | 139 | } | 
|  | 140 | if (mOutputTransformMatrixLoc >= 0) { | 
|  | 141 | // The output transform matrix and color matrix can be combined as one matrix | 
|  | 142 | // that is applied right before applying OETF. | 
| Peiyong Lin | 70b26ce | 2018-09-18 19:02:39 -0700 | [diff] [blame] | 143 | mat4 outputTransformMatrix = desc.colorMatrix * desc.outputTransformMatrix; | 
| Peiyong Lin | 46080ef | 2018-10-26 18:43:14 -0700 | [diff] [blame] | 144 | glUniformMatrix4fv(mOutputTransformMatrixLoc, 1, GL_FALSE, outputTransformMatrix.asArray()); | 
| Mathias Agopian | ff2ed70 | 2013-09-01 21:36:12 -0700 | [diff] [blame] | 145 | } | 
| Peiyong Lin | fb06930 | 2018-04-25 14:34:31 -0700 | [diff] [blame] | 146 | if (mDisplayMaxLuminanceLoc >= 0) { | 
| Peiyong Lin | 46080ef | 2018-10-26 18:43:14 -0700 | [diff] [blame] | 147 | glUniform1f(mDisplayMaxLuminanceLoc, desc.displayMaxLuminance); | 
| Peiyong Lin | fb06930 | 2018-04-25 14:34:31 -0700 | [diff] [blame] | 148 | } | 
| Peiyong Lin | 1a70eca | 2019-11-15 09:33:33 -0800 | [diff] [blame] | 149 | if (mMaxMasteringLuminanceLoc >= 0) { | 
|  | 150 | glUniform1f(mMaxMasteringLuminanceLoc, desc.maxMasteringLuminance); | 
|  | 151 | } | 
|  | 152 | if (mMaxContentLuminanceLoc >= 0) { | 
|  | 153 | glUniform1f(mMaxContentLuminanceLoc, desc.maxContentLuminance); | 
|  | 154 | } | 
| Lucas Dupin | 1b6531c | 2018-07-05 17:18:21 -0700 | [diff] [blame] | 155 | if (mCornerRadiusLoc >= 0) { | 
|  | 156 | glUniform1f(mCornerRadiusLoc, desc.cornerRadius); | 
|  | 157 | } | 
|  | 158 | if (mCropCenterLoc >= 0) { | 
|  | 159 | glUniform2f(mCropCenterLoc, desc.cropSize.x / 2.0f, desc.cropSize.y / 2.0f); | 
|  | 160 | } | 
| Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 161 | // these uniforms are always present | 
| Peiyong Lin | 70b26ce | 2018-09-18 19:02:39 -0700 | [diff] [blame] | 162 | glUniformMatrix4fv(mProjectionMatrixLoc, 1, GL_FALSE, desc.projectionMatrix.asArray()); | 
| Mathias Agopian | 3f84483 | 2013-08-07 21:24:32 -0700 | [diff] [blame] | 163 | } | 
|  | 164 |  | 
| Peiyong Lin | 46080ef | 2018-10-26 18:43:14 -0700 | [diff] [blame] | 165 | } // namespace gl | 
|  | 166 | } // namespace renderengine | 
|  | 167 | } // namespace android |