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