Geoff Lang | f9a6f08 | 2015-01-22 13:32:49 -0500 | [diff] [blame] | 1 | // |
| 2 | // Copyright 2015 The ANGLE Project Authors. All rights reserved. |
| 3 | // Use of this source code is governed by a BSD-style license that can be |
| 4 | // found in the LICENSE file. |
| 5 | // |
| 6 | |
| 7 | // ProgramGL.cpp: Implements the class methods for ProgramGL. |
| 8 | |
| 9 | #include "libANGLE/renderer/gl/ProgramGL.h" |
| 10 | |
Sami Väisänen | 46eaa94 | 2016-06-29 10:26:37 +0300 | [diff] [blame^] | 11 | #include "common/angleutils.h" |
Geoff Lang | f9a6f08 | 2015-01-22 13:32:49 -0500 | [diff] [blame] | 12 | #include "common/debug.h" |
Sami Väisänen | 46eaa94 | 2016-06-29 10:26:37 +0300 | [diff] [blame^] | 13 | #include "common/string_utils.h" |
Geoff Lang | 5ed74cf | 2015-04-14 13:57:07 -0400 | [diff] [blame] | 14 | #include "common/utilities.h" |
Geoff Lang | b1f435e | 2015-02-20 10:01:01 -0500 | [diff] [blame] | 15 | #include "libANGLE/renderer/gl/FunctionsGL.h" |
| 16 | #include "libANGLE/renderer/gl/ShaderGL.h" |
| 17 | #include "libANGLE/renderer/gl/StateManagerGL.h" |
Philippe Hamel | 4091119 | 2016-04-07 16:45:50 -0400 | [diff] [blame] | 18 | #include "libANGLE/renderer/gl/WorkaroundsGL.h" |
Jamie Madill | 53ea9cc | 2016-05-17 10:12:52 -0400 | [diff] [blame] | 19 | #include "libANGLE/Uniform.h" |
unknown | b4a3af2 | 2015-11-25 15:02:51 -0500 | [diff] [blame] | 20 | #include "platform/Platform.h" |
Geoff Lang | f9a6f08 | 2015-01-22 13:32:49 -0500 | [diff] [blame] | 21 | |
| 22 | namespace rx |
| 23 | { |
| 24 | |
Jamie Madill | 48ef11b | 2016-04-27 15:21:52 -0400 | [diff] [blame] | 25 | ProgramGL::ProgramGL(const gl::ProgramState &data, |
Jamie Madill | 5c6b7bf | 2015-08-17 12:53:35 -0400 | [diff] [blame] | 26 | const FunctionsGL *functions, |
Philippe Hamel | 4091119 | 2016-04-07 16:45:50 -0400 | [diff] [blame] | 27 | const WorkaroundsGL &workarounds, |
Sami Väisänen | 46eaa94 | 2016-06-29 10:26:37 +0300 | [diff] [blame^] | 28 | StateManagerGL *stateManager, |
| 29 | bool enablePathRendering) |
Philippe Hamel | 4091119 | 2016-04-07 16:45:50 -0400 | [diff] [blame] | 30 | : ProgramImpl(data), |
| 31 | mFunctions(functions), |
| 32 | mWorkarounds(workarounds), |
| 33 | mStateManager(stateManager), |
Sami Väisänen | 46eaa94 | 2016-06-29 10:26:37 +0300 | [diff] [blame^] | 34 | mEnablePathRendering(enablePathRendering), |
Philippe Hamel | 4091119 | 2016-04-07 16:45:50 -0400 | [diff] [blame] | 35 | mProgramID(0) |
Geoff Lang | b1f435e | 2015-02-20 10:01:01 -0500 | [diff] [blame] | 36 | { |
| 37 | ASSERT(mFunctions); |
| 38 | ASSERT(mStateManager); |
Geoff Lang | 0ca5378 | 2015-05-07 13:49:39 -0400 | [diff] [blame] | 39 | |
| 40 | mProgramID = mFunctions->createProgram(); |
Geoff Lang | b1f435e | 2015-02-20 10:01:01 -0500 | [diff] [blame] | 41 | } |
Geoff Lang | f9a6f08 | 2015-01-22 13:32:49 -0500 | [diff] [blame] | 42 | |
| 43 | ProgramGL::~ProgramGL() |
Geoff Lang | b1f435e | 2015-02-20 10:01:01 -0500 | [diff] [blame] | 44 | { |
Geoff Lang | 0ca5378 | 2015-05-07 13:49:39 -0400 | [diff] [blame] | 45 | mFunctions->deleteProgram(mProgramID); |
| 46 | mProgramID = 0; |
Geoff Lang | b1f435e | 2015-02-20 10:01:01 -0500 | [diff] [blame] | 47 | } |
Geoff Lang | f9a6f08 | 2015-01-22 13:32:49 -0500 | [diff] [blame] | 48 | |
Geoff Lang | f9a6f08 | 2015-01-22 13:32:49 -0500 | [diff] [blame] | 49 | LinkResult ProgramGL::load(gl::InfoLog &infoLog, gl::BinaryInputStream *stream) |
| 50 | { |
Geoff Lang | 65a0be9 | 2015-10-02 09:57:30 -0400 | [diff] [blame] | 51 | preLink(); |
| 52 | |
| 53 | // Read the binary format, size and blob |
| 54 | GLenum binaryFormat = stream->readInt<GLenum>(); |
| 55 | GLint binaryLength = stream->readInt<GLint>(); |
| 56 | const uint8_t *binary = stream->data() + stream->offset(); |
| 57 | stream->skip(binaryLength); |
| 58 | |
| 59 | // Load the binary |
| 60 | mFunctions->programBinary(mProgramID, binaryFormat, binary, binaryLength); |
| 61 | |
| 62 | // Verify that the program linked |
| 63 | if (!checkLinkStatus(infoLog)) |
| 64 | { |
| 65 | return LinkResult(false, gl::Error(GL_NO_ERROR)); |
| 66 | } |
| 67 | |
| 68 | postLink(); |
| 69 | |
| 70 | return LinkResult(true, gl::Error(GL_NO_ERROR)); |
Geoff Lang | f9a6f08 | 2015-01-22 13:32:49 -0500 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | gl::Error ProgramGL::save(gl::BinaryOutputStream *stream) |
| 74 | { |
Geoff Lang | 65a0be9 | 2015-10-02 09:57:30 -0400 | [diff] [blame] | 75 | GLint binaryLength = 0; |
| 76 | mFunctions->getProgramiv(mProgramID, GL_PROGRAM_BINARY_LENGTH, &binaryLength); |
| 77 | |
| 78 | std::vector<uint8_t> binary(binaryLength); |
| 79 | GLenum binaryFormat = GL_NONE; |
| 80 | mFunctions->getProgramBinary(mProgramID, binaryLength, &binaryLength, &binaryFormat, |
| 81 | &binary[0]); |
| 82 | |
| 83 | stream->writeInt(binaryFormat); |
| 84 | stream->writeInt(binaryLength); |
| 85 | stream->writeBytes(&binary[0], binaryLength); |
| 86 | |
| 87 | return gl::Error(GL_NO_ERROR); |
Geoff Lang | f9a6f08 | 2015-01-22 13:32:49 -0500 | [diff] [blame] | 88 | } |
| 89 | |
Geoff Lang | c562975 | 2015-12-07 16:29:04 -0500 | [diff] [blame] | 90 | void ProgramGL::setBinaryRetrievableHint(bool retrievable) |
| 91 | { |
Geoff Lang | 65a0be9 | 2015-10-02 09:57:30 -0400 | [diff] [blame] | 92 | // glProgramParameteri isn't always available on ES backends. |
| 93 | if (mFunctions->programParameteri) |
| 94 | { |
| 95 | mFunctions->programParameteri(mProgramID, GL_PROGRAM_BINARY_RETRIEVABLE_HINT, |
| 96 | retrievable ? GL_TRUE : GL_FALSE); |
| 97 | } |
Geoff Lang | c562975 | 2015-12-07 16:29:04 -0500 | [diff] [blame] | 98 | } |
| 99 | |
Jamie Madill | 9082b98 | 2016-04-27 15:21:51 -0400 | [diff] [blame] | 100 | LinkResult ProgramGL::link(const gl::ContextState &data, gl::InfoLog &infoLog) |
Geoff Lang | f9a6f08 | 2015-01-22 13:32:49 -0500 | [diff] [blame] | 101 | { |
Geoff Lang | 65a0be9 | 2015-10-02 09:57:30 -0400 | [diff] [blame] | 102 | preLink(); |
Geoff Lang | b1f435e | 2015-02-20 10:01:01 -0500 | [diff] [blame] | 103 | |
Geoff Lang | 1a68346 | 2015-09-29 15:09:59 -0400 | [diff] [blame] | 104 | // Set the transform feedback state |
| 105 | std::vector<const GLchar *> transformFeedbackVaryings; |
Jamie Madill | 48ef11b | 2016-04-27 15:21:52 -0400 | [diff] [blame] | 106 | for (const auto &tfVarying : mState.getTransformFeedbackVaryingNames()) |
Geoff Lang | 1a68346 | 2015-09-29 15:09:59 -0400 | [diff] [blame] | 107 | { |
| 108 | transformFeedbackVaryings.push_back(tfVarying.c_str()); |
| 109 | } |
| 110 | |
| 111 | if (transformFeedbackVaryings.empty()) |
| 112 | { |
| 113 | if (mFunctions->transformFeedbackVaryings) |
| 114 | { |
| 115 | mFunctions->transformFeedbackVaryings(mProgramID, 0, nullptr, |
Jamie Madill | 48ef11b | 2016-04-27 15:21:52 -0400 | [diff] [blame] | 116 | mState.getTransformFeedbackBufferMode()); |
Geoff Lang | 1a68346 | 2015-09-29 15:09:59 -0400 | [diff] [blame] | 117 | } |
| 118 | } |
| 119 | else |
| 120 | { |
| 121 | ASSERT(mFunctions->transformFeedbackVaryings); |
| 122 | mFunctions->transformFeedbackVaryings( |
| 123 | mProgramID, static_cast<GLsizei>(transformFeedbackVaryings.size()), |
Jamie Madill | 48ef11b | 2016-04-27 15:21:52 -0400 | [diff] [blame] | 124 | &transformFeedbackVaryings[0], mState.getTransformFeedbackBufferMode()); |
Geoff Lang | 1a68346 | 2015-09-29 15:09:59 -0400 | [diff] [blame] | 125 | } |
| 126 | |
Jamie Madill | 48ef11b | 2016-04-27 15:21:52 -0400 | [diff] [blame] | 127 | const ShaderGL *vertexShaderGL = GetImplAs<ShaderGL>(mState.getAttachedVertexShader()); |
| 128 | const ShaderGL *fragmentShaderGL = GetImplAs<ShaderGL>(mState.getAttachedFragmentShader()); |
Geoff Lang | b1f435e | 2015-02-20 10:01:01 -0500 | [diff] [blame] | 129 | |
Geoff Lang | b1f435e | 2015-02-20 10:01:01 -0500 | [diff] [blame] | 130 | // Attach the shaders |
| 131 | mFunctions->attachShader(mProgramID, vertexShaderGL->getShaderID()); |
| 132 | mFunctions->attachShader(mProgramID, fragmentShaderGL->getShaderID()); |
| 133 | |
Geoff Lang | 1528e56 | 2015-08-24 15:10:58 -0400 | [diff] [blame] | 134 | // Bind attribute locations to match the GL layer. |
Jamie Madill | 48ef11b | 2016-04-27 15:21:52 -0400 | [diff] [blame] | 135 | for (const sh::Attribute &attribute : mState.getAttributes()) |
Geoff Lang | 1528e56 | 2015-08-24 15:10:58 -0400 | [diff] [blame] | 136 | { |
| 137 | if (!attribute.staticUse) |
| 138 | { |
| 139 | continue; |
| 140 | } |
| 141 | |
| 142 | mFunctions->bindAttribLocation(mProgramID, attribute.location, attribute.name.c_str()); |
Geoff Lang | 1528e56 | 2015-08-24 15:10:58 -0400 | [diff] [blame] | 143 | } |
| 144 | |
Geoff Lang | b1f435e | 2015-02-20 10:01:01 -0500 | [diff] [blame] | 145 | // Link and verify |
| 146 | mFunctions->linkProgram(mProgramID); |
| 147 | |
Geoff Lang | 0ca5378 | 2015-05-07 13:49:39 -0400 | [diff] [blame] | 148 | // Detach the shaders |
| 149 | mFunctions->detachShader(mProgramID, vertexShaderGL->getShaderID()); |
| 150 | mFunctions->detachShader(mProgramID, fragmentShaderGL->getShaderID()); |
| 151 | |
| 152 | // Verify the link |
Geoff Lang | 65a0be9 | 2015-10-02 09:57:30 -0400 | [diff] [blame] | 153 | if (!checkLinkStatus(infoLog)) |
Geoff Lang | b1f435e | 2015-02-20 10:01:01 -0500 | [diff] [blame] | 154 | { |
Geoff Lang | b1f435e | 2015-02-20 10:01:01 -0500 | [diff] [blame] | 155 | return LinkResult(false, gl::Error(GL_NO_ERROR)); |
| 156 | } |
| 157 | |
Philippe Hamel | 4091119 | 2016-04-07 16:45:50 -0400 | [diff] [blame] | 158 | if (mWorkarounds.alwaysCallUseProgramAfterLink) |
| 159 | { |
| 160 | mStateManager->forceUseProgram(mProgramID); |
| 161 | } |
| 162 | |
Geoff Lang | 65a0be9 | 2015-10-02 09:57:30 -0400 | [diff] [blame] | 163 | postLink(); |
Geoff Lang | b1f435e | 2015-02-20 10:01:01 -0500 | [diff] [blame] | 164 | |
Geoff Lang | b1f435e | 2015-02-20 10:01:01 -0500 | [diff] [blame] | 165 | return LinkResult(true, gl::Error(GL_NO_ERROR)); |
Geoff Lang | f9a6f08 | 2015-01-22 13:32:49 -0500 | [diff] [blame] | 166 | } |
| 167 | |
Jamie Madill | 36cfd6a | 2015-08-18 10:46:20 -0400 | [diff] [blame] | 168 | GLboolean ProgramGL::validate(const gl::Caps & /*caps*/, gl::InfoLog * /*infoLog*/) |
| 169 | { |
| 170 | // TODO(jmadill): implement validate |
| 171 | return true; |
| 172 | } |
| 173 | |
Geoff Lang | f9a6f08 | 2015-01-22 13:32:49 -0500 | [diff] [blame] | 174 | void ProgramGL::setUniform1fv(GLint location, GLsizei count, const GLfloat *v) |
| 175 | { |
Geoff Lang | 63cbace | 2015-02-26 10:03:12 -0500 | [diff] [blame] | 176 | mStateManager->useProgram(mProgramID); |
Jamie Madill | 62d31cb | 2015-09-11 13:25:51 -0400 | [diff] [blame] | 177 | mFunctions->uniform1fv(uniLoc(location), count, v); |
Geoff Lang | f9a6f08 | 2015-01-22 13:32:49 -0500 | [diff] [blame] | 178 | } |
| 179 | |
| 180 | void ProgramGL::setUniform2fv(GLint location, GLsizei count, const GLfloat *v) |
| 181 | { |
Geoff Lang | 63cbace | 2015-02-26 10:03:12 -0500 | [diff] [blame] | 182 | mStateManager->useProgram(mProgramID); |
Jamie Madill | 62d31cb | 2015-09-11 13:25:51 -0400 | [diff] [blame] | 183 | mFunctions->uniform2fv(uniLoc(location), count, v); |
Geoff Lang | f9a6f08 | 2015-01-22 13:32:49 -0500 | [diff] [blame] | 184 | } |
| 185 | |
| 186 | void ProgramGL::setUniform3fv(GLint location, GLsizei count, const GLfloat *v) |
| 187 | { |
Geoff Lang | 63cbace | 2015-02-26 10:03:12 -0500 | [diff] [blame] | 188 | mStateManager->useProgram(mProgramID); |
Jamie Madill | 62d31cb | 2015-09-11 13:25:51 -0400 | [diff] [blame] | 189 | mFunctions->uniform3fv(uniLoc(location), count, v); |
Geoff Lang | f9a6f08 | 2015-01-22 13:32:49 -0500 | [diff] [blame] | 190 | } |
| 191 | |
| 192 | void ProgramGL::setUniform4fv(GLint location, GLsizei count, const GLfloat *v) |
| 193 | { |
Geoff Lang | 63cbace | 2015-02-26 10:03:12 -0500 | [diff] [blame] | 194 | mStateManager->useProgram(mProgramID); |
Jamie Madill | 62d31cb | 2015-09-11 13:25:51 -0400 | [diff] [blame] | 195 | mFunctions->uniform4fv(uniLoc(location), count, v); |
Geoff Lang | f9a6f08 | 2015-01-22 13:32:49 -0500 | [diff] [blame] | 196 | } |
| 197 | |
| 198 | void ProgramGL::setUniform1iv(GLint location, GLsizei count, const GLint *v) |
| 199 | { |
Geoff Lang | 63cbace | 2015-02-26 10:03:12 -0500 | [diff] [blame] | 200 | mStateManager->useProgram(mProgramID); |
Jamie Madill | 62d31cb | 2015-09-11 13:25:51 -0400 | [diff] [blame] | 201 | mFunctions->uniform1iv(uniLoc(location), count, v); |
Geoff Lang | f51bc79 | 2015-05-04 14:57:03 -0400 | [diff] [blame] | 202 | |
Jamie Madill | 48ef11b | 2016-04-27 15:21:52 -0400 | [diff] [blame] | 203 | const gl::VariableLocation &locationEntry = mState.getUniformLocations()[location]; |
Jamie Madill | 62d31cb | 2015-09-11 13:25:51 -0400 | [diff] [blame] | 204 | |
| 205 | size_t samplerIndex = mUniformIndexToSamplerIndex[locationEntry.index]; |
| 206 | if (samplerIndex != GL_INVALID_INDEX) |
Geoff Lang | f51bc79 | 2015-05-04 14:57:03 -0400 | [diff] [blame] | 207 | { |
Jamie Madill | 62d31cb | 2015-09-11 13:25:51 -0400 | [diff] [blame] | 208 | std::vector<GLuint> &boundTextureUnits = mSamplerBindings[samplerIndex].boundTextureUnits; |
Geoff Lang | f51bc79 | 2015-05-04 14:57:03 -0400 | [diff] [blame] | 209 | |
Jamie Madill | 62d31cb | 2015-09-11 13:25:51 -0400 | [diff] [blame] | 210 | size_t copyCount = |
| 211 | std::max<size_t>(count, boundTextureUnits.size() - locationEntry.element); |
| 212 | std::copy(v, v + copyCount, boundTextureUnits.begin() + locationEntry.element); |
Geoff Lang | f51bc79 | 2015-05-04 14:57:03 -0400 | [diff] [blame] | 213 | } |
Geoff Lang | f9a6f08 | 2015-01-22 13:32:49 -0500 | [diff] [blame] | 214 | } |
| 215 | |
| 216 | void ProgramGL::setUniform2iv(GLint location, GLsizei count, const GLint *v) |
| 217 | { |
Geoff Lang | 63cbace | 2015-02-26 10:03:12 -0500 | [diff] [blame] | 218 | mStateManager->useProgram(mProgramID); |
Jamie Madill | 62d31cb | 2015-09-11 13:25:51 -0400 | [diff] [blame] | 219 | mFunctions->uniform2iv(uniLoc(location), count, v); |
Geoff Lang | f9a6f08 | 2015-01-22 13:32:49 -0500 | [diff] [blame] | 220 | } |
| 221 | |
| 222 | void ProgramGL::setUniform3iv(GLint location, GLsizei count, const GLint *v) |
| 223 | { |
Geoff Lang | 63cbace | 2015-02-26 10:03:12 -0500 | [diff] [blame] | 224 | mStateManager->useProgram(mProgramID); |
Jamie Madill | 62d31cb | 2015-09-11 13:25:51 -0400 | [diff] [blame] | 225 | mFunctions->uniform3iv(uniLoc(location), count, v); |
Geoff Lang | f9a6f08 | 2015-01-22 13:32:49 -0500 | [diff] [blame] | 226 | } |
| 227 | |
| 228 | void ProgramGL::setUniform4iv(GLint location, GLsizei count, const GLint *v) |
| 229 | { |
Geoff Lang | 63cbace | 2015-02-26 10:03:12 -0500 | [diff] [blame] | 230 | mStateManager->useProgram(mProgramID); |
Jamie Madill | 62d31cb | 2015-09-11 13:25:51 -0400 | [diff] [blame] | 231 | mFunctions->uniform4iv(uniLoc(location), count, v); |
Geoff Lang | f9a6f08 | 2015-01-22 13:32:49 -0500 | [diff] [blame] | 232 | } |
| 233 | |
| 234 | void ProgramGL::setUniform1uiv(GLint location, GLsizei count, const GLuint *v) |
| 235 | { |
Geoff Lang | 63cbace | 2015-02-26 10:03:12 -0500 | [diff] [blame] | 236 | mStateManager->useProgram(mProgramID); |
Corentin Wallez | 171e6a5 | 2016-06-02 09:57:13 -0400 | [diff] [blame] | 237 | mFunctions->uniform1uiv(uniLoc(location), count, v); |
Geoff Lang | f9a6f08 | 2015-01-22 13:32:49 -0500 | [diff] [blame] | 238 | } |
| 239 | |
| 240 | void ProgramGL::setUniform2uiv(GLint location, GLsizei count, const GLuint *v) |
| 241 | { |
Geoff Lang | 63cbace | 2015-02-26 10:03:12 -0500 | [diff] [blame] | 242 | mStateManager->useProgram(mProgramID); |
Jamie Madill | 62d31cb | 2015-09-11 13:25:51 -0400 | [diff] [blame] | 243 | mFunctions->uniform2uiv(uniLoc(location), count, v); |
Geoff Lang | f9a6f08 | 2015-01-22 13:32:49 -0500 | [diff] [blame] | 244 | } |
| 245 | |
| 246 | void ProgramGL::setUniform3uiv(GLint location, GLsizei count, const GLuint *v) |
| 247 | { |
Geoff Lang | 63cbace | 2015-02-26 10:03:12 -0500 | [diff] [blame] | 248 | mStateManager->useProgram(mProgramID); |
Jamie Madill | 62d31cb | 2015-09-11 13:25:51 -0400 | [diff] [blame] | 249 | mFunctions->uniform3uiv(uniLoc(location), count, v); |
Geoff Lang | f9a6f08 | 2015-01-22 13:32:49 -0500 | [diff] [blame] | 250 | } |
| 251 | |
| 252 | void ProgramGL::setUniform4uiv(GLint location, GLsizei count, const GLuint *v) |
| 253 | { |
Geoff Lang | 63cbace | 2015-02-26 10:03:12 -0500 | [diff] [blame] | 254 | mStateManager->useProgram(mProgramID); |
Jamie Madill | 62d31cb | 2015-09-11 13:25:51 -0400 | [diff] [blame] | 255 | mFunctions->uniform4uiv(uniLoc(location), count, v); |
Geoff Lang | f9a6f08 | 2015-01-22 13:32:49 -0500 | [diff] [blame] | 256 | } |
| 257 | |
| 258 | void ProgramGL::setUniformMatrix2fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value) |
| 259 | { |
Geoff Lang | 63cbace | 2015-02-26 10:03:12 -0500 | [diff] [blame] | 260 | mStateManager->useProgram(mProgramID); |
Jamie Madill | 62d31cb | 2015-09-11 13:25:51 -0400 | [diff] [blame] | 261 | mFunctions->uniformMatrix2fv(uniLoc(location), count, transpose, value); |
Geoff Lang | f9a6f08 | 2015-01-22 13:32:49 -0500 | [diff] [blame] | 262 | } |
| 263 | |
| 264 | void ProgramGL::setUniformMatrix3fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value) |
| 265 | { |
Geoff Lang | 63cbace | 2015-02-26 10:03:12 -0500 | [diff] [blame] | 266 | mStateManager->useProgram(mProgramID); |
Jamie Madill | 62d31cb | 2015-09-11 13:25:51 -0400 | [diff] [blame] | 267 | mFunctions->uniformMatrix3fv(uniLoc(location), count, transpose, value); |
Geoff Lang | f9a6f08 | 2015-01-22 13:32:49 -0500 | [diff] [blame] | 268 | } |
| 269 | |
| 270 | void ProgramGL::setUniformMatrix4fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value) |
| 271 | { |
Geoff Lang | 63cbace | 2015-02-26 10:03:12 -0500 | [diff] [blame] | 272 | mStateManager->useProgram(mProgramID); |
Jamie Madill | 62d31cb | 2015-09-11 13:25:51 -0400 | [diff] [blame] | 273 | mFunctions->uniformMatrix4fv(uniLoc(location), count, transpose, value); |
Geoff Lang | f9a6f08 | 2015-01-22 13:32:49 -0500 | [diff] [blame] | 274 | } |
| 275 | |
| 276 | void ProgramGL::setUniformMatrix2x3fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value) |
| 277 | { |
Geoff Lang | 63cbace | 2015-02-26 10:03:12 -0500 | [diff] [blame] | 278 | mStateManager->useProgram(mProgramID); |
Jamie Madill | 62d31cb | 2015-09-11 13:25:51 -0400 | [diff] [blame] | 279 | mFunctions->uniformMatrix2x3fv(uniLoc(location), count, transpose, value); |
Geoff Lang | f9a6f08 | 2015-01-22 13:32:49 -0500 | [diff] [blame] | 280 | } |
| 281 | |
| 282 | void ProgramGL::setUniformMatrix3x2fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value) |
| 283 | { |
Geoff Lang | 63cbace | 2015-02-26 10:03:12 -0500 | [diff] [blame] | 284 | mStateManager->useProgram(mProgramID); |
Jamie Madill | 62d31cb | 2015-09-11 13:25:51 -0400 | [diff] [blame] | 285 | mFunctions->uniformMatrix3x2fv(uniLoc(location), count, transpose, value); |
Geoff Lang | f9a6f08 | 2015-01-22 13:32:49 -0500 | [diff] [blame] | 286 | } |
| 287 | |
| 288 | void ProgramGL::setUniformMatrix2x4fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value) |
| 289 | { |
Geoff Lang | 63cbace | 2015-02-26 10:03:12 -0500 | [diff] [blame] | 290 | mStateManager->useProgram(mProgramID); |
Jamie Madill | 62d31cb | 2015-09-11 13:25:51 -0400 | [diff] [blame] | 291 | mFunctions->uniformMatrix2x4fv(uniLoc(location), count, transpose, value); |
Geoff Lang | f9a6f08 | 2015-01-22 13:32:49 -0500 | [diff] [blame] | 292 | } |
| 293 | |
| 294 | void ProgramGL::setUniformMatrix4x2fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value) |
| 295 | { |
Geoff Lang | 63cbace | 2015-02-26 10:03:12 -0500 | [diff] [blame] | 296 | mStateManager->useProgram(mProgramID); |
Jamie Madill | 62d31cb | 2015-09-11 13:25:51 -0400 | [diff] [blame] | 297 | mFunctions->uniformMatrix4x2fv(uniLoc(location), count, transpose, value); |
Geoff Lang | f9a6f08 | 2015-01-22 13:32:49 -0500 | [diff] [blame] | 298 | } |
| 299 | |
| 300 | void ProgramGL::setUniformMatrix3x4fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value) |
| 301 | { |
Geoff Lang | 63cbace | 2015-02-26 10:03:12 -0500 | [diff] [blame] | 302 | mStateManager->useProgram(mProgramID); |
Jamie Madill | 62d31cb | 2015-09-11 13:25:51 -0400 | [diff] [blame] | 303 | mFunctions->uniformMatrix3x4fv(uniLoc(location), count, transpose, value); |
Geoff Lang | f9a6f08 | 2015-01-22 13:32:49 -0500 | [diff] [blame] | 304 | } |
| 305 | |
| 306 | void ProgramGL::setUniformMatrix4x3fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value) |
| 307 | { |
Geoff Lang | 63cbace | 2015-02-26 10:03:12 -0500 | [diff] [blame] | 308 | mStateManager->useProgram(mProgramID); |
Jamie Madill | 62d31cb | 2015-09-11 13:25:51 -0400 | [diff] [blame] | 309 | mFunctions->uniformMatrix4x3fv(uniLoc(location), count, transpose, value); |
Geoff Lang | f9a6f08 | 2015-01-22 13:32:49 -0500 | [diff] [blame] | 310 | } |
| 311 | |
Geoff Lang | 5d124a6 | 2015-09-15 13:03:27 -0400 | [diff] [blame] | 312 | void ProgramGL::setUniformBlockBinding(GLuint uniformBlockIndex, GLuint uniformBlockBinding) |
| 313 | { |
Jamie Madill | 4a3c234 | 2015-10-08 12:58:45 -0400 | [diff] [blame] | 314 | // Lazy init |
| 315 | if (mUniformBlockRealLocationMap.empty()) |
| 316 | { |
Jamie Madill | 48ef11b | 2016-04-27 15:21:52 -0400 | [diff] [blame] | 317 | mUniformBlockRealLocationMap.reserve(mState.getUniformBlocks().size()); |
| 318 | for (const gl::UniformBlock &uniformBlock : mState.getUniformBlocks()) |
Jamie Madill | 4a3c234 | 2015-10-08 12:58:45 -0400 | [diff] [blame] | 319 | { |
| 320 | const std::string &nameWithIndex = uniformBlock.nameWithArrayIndex(); |
| 321 | GLuint blockIndex = mFunctions->getUniformBlockIndex(mProgramID, nameWithIndex.c_str()); |
| 322 | mUniformBlockRealLocationMap.push_back(blockIndex); |
| 323 | } |
| 324 | } |
| 325 | |
| 326 | GLuint realBlockIndex = mUniformBlockRealLocationMap[uniformBlockIndex]; |
| 327 | if (realBlockIndex != GL_INVALID_INDEX) |
| 328 | { |
| 329 | mFunctions->uniformBlockBinding(mProgramID, realBlockIndex, uniformBlockBinding); |
| 330 | } |
Geoff Lang | 5d124a6 | 2015-09-15 13:03:27 -0400 | [diff] [blame] | 331 | } |
| 332 | |
Geoff Lang | b1f435e | 2015-02-20 10:01:01 -0500 | [diff] [blame] | 333 | GLuint ProgramGL::getProgramID() const |
| 334 | { |
| 335 | return mProgramID; |
| 336 | } |
| 337 | |
Geoff Lang | f51bc79 | 2015-05-04 14:57:03 -0400 | [diff] [blame] | 338 | const std::vector<SamplerBindingGL> &ProgramGL::getAppliedSamplerUniforms() const |
| 339 | { |
| 340 | return mSamplerBindings; |
| 341 | } |
| 342 | |
Jamie Madill | 4a3c234 | 2015-10-08 12:58:45 -0400 | [diff] [blame] | 343 | bool ProgramGL::getUniformBlockSize(const std::string &blockName, size_t *sizeOut) const |
Jamie Madill | 62d31cb | 2015-09-11 13:25:51 -0400 | [diff] [blame] | 344 | { |
Jamie Madill | 4a3c234 | 2015-10-08 12:58:45 -0400 | [diff] [blame] | 345 | ASSERT(mProgramID != 0u); |
Geoff Lang | 5d124a6 | 2015-09-15 13:03:27 -0400 | [diff] [blame] | 346 | |
Jamie Madill | 4a3c234 | 2015-10-08 12:58:45 -0400 | [diff] [blame] | 347 | GLuint blockIndex = mFunctions->getUniformBlockIndex(mProgramID, blockName.c_str()); |
| 348 | if (blockIndex == GL_INVALID_INDEX) |
Geoff Lang | 5d124a6 | 2015-09-15 13:03:27 -0400 | [diff] [blame] | 349 | { |
Jamie Madill | 4a3c234 | 2015-10-08 12:58:45 -0400 | [diff] [blame] | 350 | *sizeOut = 0; |
| 351 | return false; |
Geoff Lang | 5d124a6 | 2015-09-15 13:03:27 -0400 | [diff] [blame] | 352 | } |
| 353 | |
Jamie Madill | 4a3c234 | 2015-10-08 12:58:45 -0400 | [diff] [blame] | 354 | GLint dataSize = 0; |
| 355 | mFunctions->getActiveUniformBlockiv(mProgramID, blockIndex, GL_UNIFORM_BLOCK_DATA_SIZE, |
| 356 | &dataSize); |
| 357 | *sizeOut = static_cast<size_t>(dataSize); |
| 358 | return true; |
| 359 | } |
| 360 | |
| 361 | bool ProgramGL::getUniformBlockMemberInfo(const std::string &memberUniformName, |
| 362 | sh::BlockMemberInfo *memberInfoOut) const |
| 363 | { |
| 364 | GLuint uniformIndex; |
| 365 | const GLchar *memberNameGLStr = memberUniformName.c_str(); |
| 366 | mFunctions->getUniformIndices(mProgramID, 1, &memberNameGLStr, &uniformIndex); |
| 367 | |
| 368 | if (uniformIndex == GL_INVALID_INDEX) |
Geoff Lang | 5d124a6 | 2015-09-15 13:03:27 -0400 | [diff] [blame] | 369 | { |
Jamie Madill | 4a3c234 | 2015-10-08 12:58:45 -0400 | [diff] [blame] | 370 | *memberInfoOut = sh::BlockMemberInfo::getDefaultBlockInfo(); |
| 371 | return false; |
Geoff Lang | 5d124a6 | 2015-09-15 13:03:27 -0400 | [diff] [blame] | 372 | } |
Jamie Madill | 4a3c234 | 2015-10-08 12:58:45 -0400 | [diff] [blame] | 373 | |
| 374 | mFunctions->getActiveUniformsiv(mProgramID, 1, &uniformIndex, GL_UNIFORM_OFFSET, |
| 375 | &memberInfoOut->offset); |
| 376 | mFunctions->getActiveUniformsiv(mProgramID, 1, &uniformIndex, GL_UNIFORM_ARRAY_STRIDE, |
| 377 | &memberInfoOut->arrayStride); |
| 378 | mFunctions->getActiveUniformsiv(mProgramID, 1, &uniformIndex, GL_UNIFORM_MATRIX_STRIDE, |
| 379 | &memberInfoOut->matrixStride); |
| 380 | |
| 381 | // TODO(jmadill): possibly determine this at the gl::Program level. |
| 382 | GLint isRowMajorMatrix = 0; |
| 383 | mFunctions->getActiveUniformsiv(mProgramID, 1, &uniformIndex, GL_UNIFORM_IS_ROW_MAJOR, |
| 384 | &isRowMajorMatrix); |
| 385 | memberInfoOut->isRowMajorMatrix = isRowMajorMatrix != GL_FALSE; |
| 386 | return true; |
Jamie Madill | 62d31cb | 2015-09-11 13:25:51 -0400 | [diff] [blame] | 387 | } |
Jamie Madill | 4a3c234 | 2015-10-08 12:58:45 -0400 | [diff] [blame] | 388 | |
Sami Väisänen | 46eaa94 | 2016-06-29 10:26:37 +0300 | [diff] [blame^] | 389 | void ProgramGL::setPathFragmentInputGen(const std::string &inputName, |
| 390 | GLenum genMode, |
| 391 | GLint components, |
| 392 | const GLfloat *coeffs) |
| 393 | { |
| 394 | ASSERT(mEnablePathRendering); |
| 395 | |
| 396 | for (const auto &input : mPathRenderingFragmentInputs) |
| 397 | { |
| 398 | if (input.name == inputName) |
| 399 | { |
| 400 | mFunctions->programPathFragmentInputGenNV(mProgramID, input.location, genMode, |
| 401 | components, coeffs); |
| 402 | ASSERT(mFunctions->getError() == GL_NO_ERROR); |
| 403 | return; |
| 404 | } |
| 405 | } |
| 406 | |
| 407 | } |
| 408 | |
Geoff Lang | 65a0be9 | 2015-10-02 09:57:30 -0400 | [diff] [blame] | 409 | void ProgramGL::preLink() |
| 410 | { |
| 411 | // Reset the program state |
| 412 | mUniformRealLocationMap.clear(); |
| 413 | mUniformBlockRealLocationMap.clear(); |
| 414 | mSamplerBindings.clear(); |
| 415 | mUniformIndexToSamplerIndex.clear(); |
Sami Väisänen | 46eaa94 | 2016-06-29 10:26:37 +0300 | [diff] [blame^] | 416 | mPathRenderingFragmentInputs.clear(); |
Geoff Lang | 65a0be9 | 2015-10-02 09:57:30 -0400 | [diff] [blame] | 417 | } |
| 418 | |
| 419 | bool ProgramGL::checkLinkStatus(gl::InfoLog &infoLog) |
| 420 | { |
| 421 | GLint linkStatus = GL_FALSE; |
| 422 | mFunctions->getProgramiv(mProgramID, GL_LINK_STATUS, &linkStatus); |
| 423 | if (linkStatus == GL_FALSE) |
| 424 | { |
| 425 | // Linking failed, put the error into the info log |
| 426 | GLint infoLogLength = 0; |
| 427 | mFunctions->getProgramiv(mProgramID, GL_INFO_LOG_LENGTH, &infoLogLength); |
| 428 | |
| 429 | std::string warning; |
| 430 | |
| 431 | // Info log length includes the null terminator, so 1 means that the info log is an empty |
| 432 | // string. |
| 433 | if (infoLogLength > 1) |
| 434 | { |
| 435 | std::vector<char> buf(infoLogLength); |
| 436 | mFunctions->getProgramInfoLog(mProgramID, infoLogLength, nullptr, &buf[0]); |
| 437 | |
| 438 | mFunctions->deleteProgram(mProgramID); |
| 439 | mProgramID = 0; |
| 440 | |
| 441 | infoLog << buf.data(); |
| 442 | |
| 443 | warning = FormatString("Program link failed unexpectedly: %s", buf.data()); |
| 444 | } |
| 445 | else |
| 446 | { |
| 447 | warning = "Program link failed unexpectedly with no info log."; |
| 448 | } |
| 449 | ANGLEPlatformCurrent()->logWarning(warning.c_str()); |
| 450 | TRACE("\n%s", warning.c_str()); |
| 451 | |
| 452 | // TODO, return GL_OUT_OF_MEMORY or just fail the link? This is an unexpected case |
| 453 | return false; |
| 454 | } |
| 455 | |
| 456 | return true; |
| 457 | } |
| 458 | |
| 459 | void ProgramGL::postLink() |
| 460 | { |
| 461 | // Query the uniform information |
| 462 | ASSERT(mUniformRealLocationMap.empty()); |
Jamie Madill | 48ef11b | 2016-04-27 15:21:52 -0400 | [diff] [blame] | 463 | const auto &uniformLocations = mState.getUniformLocations(); |
| 464 | const auto &uniforms = mState.getUniforms(); |
Geoff Lang | 65a0be9 | 2015-10-02 09:57:30 -0400 | [diff] [blame] | 465 | mUniformRealLocationMap.resize(uniformLocations.size(), GL_INVALID_INDEX); |
| 466 | for (size_t uniformLocation = 0; uniformLocation < uniformLocations.size(); uniformLocation++) |
| 467 | { |
| 468 | const auto &entry = uniformLocations[uniformLocation]; |
| 469 | if (!entry.used) |
| 470 | { |
| 471 | continue; |
| 472 | } |
| 473 | |
| 474 | // From the spec: |
| 475 | // "Locations for sequential array indices are not required to be sequential." |
| 476 | const gl::LinkedUniform &uniform = uniforms[entry.index]; |
| 477 | std::stringstream fullNameStr; |
| 478 | fullNameStr << uniform.name; |
| 479 | if (uniform.isArray()) |
| 480 | { |
| 481 | fullNameStr << "[" << entry.element << "]"; |
| 482 | } |
| 483 | const std::string &fullName = fullNameStr.str(); |
| 484 | |
| 485 | GLint realLocation = mFunctions->getUniformLocation(mProgramID, fullName.c_str()); |
| 486 | mUniformRealLocationMap[uniformLocation] = realLocation; |
| 487 | } |
| 488 | |
Jamie Madill | 48ef11b | 2016-04-27 15:21:52 -0400 | [diff] [blame] | 489 | mUniformIndexToSamplerIndex.resize(mState.getUniforms().size(), GL_INVALID_INDEX); |
Geoff Lang | 65a0be9 | 2015-10-02 09:57:30 -0400 | [diff] [blame] | 490 | |
| 491 | for (size_t uniformId = 0; uniformId < uniforms.size(); ++uniformId) |
| 492 | { |
| 493 | const gl::LinkedUniform &linkedUniform = uniforms[uniformId]; |
| 494 | |
| 495 | if (!linkedUniform.isSampler() || !linkedUniform.staticUse) |
| 496 | continue; |
| 497 | |
| 498 | mUniformIndexToSamplerIndex[uniformId] = mSamplerBindings.size(); |
| 499 | |
| 500 | // If uniform is a sampler type, insert it into the mSamplerBindings array |
| 501 | SamplerBindingGL samplerBinding; |
| 502 | samplerBinding.textureType = gl::SamplerTypeToTextureType(linkedUniform.type); |
| 503 | samplerBinding.boundTextureUnits.resize(linkedUniform.elementCount(), 0); |
| 504 | mSamplerBindings.push_back(samplerBinding); |
| 505 | } |
Sami Väisänen | 46eaa94 | 2016-06-29 10:26:37 +0300 | [diff] [blame^] | 506 | |
| 507 | // Discover CHROMIUM_path_rendering fragment inputs if enabled. |
| 508 | if (!mEnablePathRendering) |
| 509 | return; |
| 510 | |
| 511 | GLint numFragmentInputs = 0; |
| 512 | mFunctions->getProgramInterfaceiv(mProgramID, GL_FRAGMENT_INPUT_NV, GL_ACTIVE_RESOURCES, |
| 513 | &numFragmentInputs); |
| 514 | if (numFragmentInputs <= 0) |
| 515 | return; |
| 516 | |
| 517 | GLint maxNameLength = 0; |
| 518 | mFunctions->getProgramInterfaceiv(mProgramID, GL_FRAGMENT_INPUT_NV, GL_MAX_NAME_LENGTH, |
| 519 | &maxNameLength); |
| 520 | ASSERT(maxNameLength); |
| 521 | |
| 522 | for (GLint i = 0; i < numFragmentInputs; ++i) |
| 523 | { |
| 524 | std::string name; |
| 525 | name.resize(maxNameLength); |
| 526 | |
| 527 | GLsizei nameLen = 0; |
| 528 | mFunctions->getProgramResourceName(mProgramID, GL_FRAGMENT_INPUT_NV, i, maxNameLength, |
| 529 | &nameLen, &name[0]); |
| 530 | name.resize(nameLen); |
| 531 | |
| 532 | // Ignore built-ins |
| 533 | if (angle::BeginsWith(name, "gl_")) |
| 534 | continue; |
| 535 | |
| 536 | const GLenum kQueryProperties[] = {GL_LOCATION, GL_ARRAY_SIZE}; |
| 537 | GLint queryResults[ArraySize(kQueryProperties)]; |
| 538 | GLsizei queryLength = 0; |
| 539 | |
| 540 | mFunctions->getProgramResourceiv(mProgramID, GL_FRAGMENT_INPUT_NV, i, |
| 541 | ArraySize(kQueryProperties), kQueryProperties, |
| 542 | ArraySize(queryResults), &queryLength, queryResults); |
| 543 | |
| 544 | ASSERT(queryLength == ArraySize(kQueryProperties)); |
| 545 | |
| 546 | PathRenderingFragmentInput input; |
| 547 | input.name = name; |
| 548 | input.location = queryResults[0]; |
| 549 | mPathRenderingFragmentInputs.push_back(std::move(input)); |
| 550 | |
| 551 | // If the input is an array it's denoted by [0] suffix on the variable |
| 552 | // name. We'll then create an entry per each array index where index > 0 |
| 553 | if (angle::EndsWith(name, "[0]")) |
| 554 | { |
| 555 | // drop the suffix |
| 556 | name.resize(name.size() - 3); |
| 557 | |
| 558 | const auto arraySize = queryResults[1]; |
| 559 | const auto baseLocation = queryResults[0]; |
| 560 | |
| 561 | for (GLint i = 1; i < arraySize; ++i) |
| 562 | { |
| 563 | PathRenderingFragmentInput input; |
| 564 | input.name = name + "[" + std::to_string(i) + "]"; |
| 565 | input.location = baseLocation + i; |
| 566 | mPathRenderingFragmentInputs.push_back(std::move(input)); |
| 567 | } |
| 568 | } |
| 569 | } |
Geoff Lang | 65a0be9 | 2015-10-02 09:57:30 -0400 | [diff] [blame] | 570 | } |
| 571 | |
Jamie Madill | 4a3c234 | 2015-10-08 12:58:45 -0400 | [diff] [blame] | 572 | } // namespace rx |