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