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 | 01306fc | 2015-10-05 16:53:10 +0000 | [diff] [blame] | 46 | UNIMPLEMENTED(); |
| 47 | return LinkResult(false, gl::Error(GL_INVALID_OPERATION)); |
Geoff Lang | f9a6f08 | 2015-01-22 13:32:49 -0500 | [diff] [blame] | 48 | } |
| 49 | |
| 50 | gl::Error ProgramGL::save(gl::BinaryOutputStream *stream) |
| 51 | { |
Geoff Lang | 01306fc | 2015-10-05 16:53:10 +0000 | [diff] [blame] | 52 | UNIMPLEMENTED(); |
| 53 | return gl::Error(GL_INVALID_OPERATION); |
Geoff Lang | f9a6f08 | 2015-01-22 13:32:49 -0500 | [diff] [blame] | 54 | } |
| 55 | |
Geoff Lang | c562975 | 2015-12-07 16:29:04 -0500 | [diff] [blame] | 56 | void ProgramGL::setBinaryRetrievableHint(bool retrievable) |
| 57 | { |
| 58 | UNIMPLEMENTED(); |
| 59 | } |
| 60 | |
Jamie Madill | f5f4ad2 | 2015-09-02 18:32:38 +0000 | [diff] [blame] | 61 | LinkResult ProgramGL::link(const gl::Data &data, gl::InfoLog &infoLog) |
Geoff Lang | f9a6f08 | 2015-01-22 13:32:49 -0500 | [diff] [blame] | 62 | { |
Geoff Lang | 01306fc | 2015-10-05 16:53:10 +0000 | [diff] [blame] | 63 | // Reset the program state, delete the current program if one exists |
| 64 | reset(); |
Geoff Lang | b1f435e | 2015-02-20 10:01:01 -0500 | [diff] [blame] | 65 | |
Geoff Lang | 1a68346 | 2015-09-29 15:09:59 -0400 | [diff] [blame] | 66 | // Set the transform feedback state |
| 67 | std::vector<const GLchar *> transformFeedbackVaryings; |
| 68 | for (const auto &tfVarying : mData.getTransformFeedbackVaryingNames()) |
| 69 | { |
| 70 | transformFeedbackVaryings.push_back(tfVarying.c_str()); |
| 71 | } |
| 72 | |
| 73 | if (transformFeedbackVaryings.empty()) |
| 74 | { |
| 75 | if (mFunctions->transformFeedbackVaryings) |
| 76 | { |
| 77 | mFunctions->transformFeedbackVaryings(mProgramID, 0, nullptr, |
| 78 | mData.getTransformFeedbackBufferMode()); |
| 79 | } |
| 80 | } |
| 81 | else |
| 82 | { |
| 83 | ASSERT(mFunctions->transformFeedbackVaryings); |
| 84 | mFunctions->transformFeedbackVaryings( |
| 85 | mProgramID, static_cast<GLsizei>(transformFeedbackVaryings.size()), |
| 86 | &transformFeedbackVaryings[0], mData.getTransformFeedbackBufferMode()); |
| 87 | } |
| 88 | |
Geoff Lang | 01306fc | 2015-10-05 16:53:10 +0000 | [diff] [blame] | 89 | const gl::Shader *vertexShader = mData.getAttachedVertexShader(); |
| 90 | const gl::Shader *fragmentShader = mData.getAttachedFragmentShader(); |
| 91 | |
| 92 | const ShaderGL *vertexShaderGL = GetImplAs<ShaderGL>(vertexShader); |
| 93 | const ShaderGL *fragmentShaderGL = GetImplAs<ShaderGL>(fragmentShader); |
Geoff Lang | b1f435e | 2015-02-20 10:01:01 -0500 | [diff] [blame] | 94 | |
Geoff Lang | b1f435e | 2015-02-20 10:01:01 -0500 | [diff] [blame] | 95 | // Attach the shaders |
| 96 | mFunctions->attachShader(mProgramID, vertexShaderGL->getShaderID()); |
| 97 | mFunctions->attachShader(mProgramID, fragmentShaderGL->getShaderID()); |
| 98 | |
Geoff Lang | 1528e56 | 2015-08-24 15:10:58 -0400 | [diff] [blame] | 99 | // Bind attribute locations to match the GL layer. |
| 100 | for (const sh::Attribute &attribute : mData.getAttributes()) |
| 101 | { |
| 102 | if (!attribute.staticUse) |
| 103 | { |
| 104 | continue; |
| 105 | } |
| 106 | |
| 107 | mFunctions->bindAttribLocation(mProgramID, attribute.location, attribute.name.c_str()); |
Geoff Lang | 1528e56 | 2015-08-24 15:10:58 -0400 | [diff] [blame] | 108 | } |
| 109 | |
Geoff Lang | b1f435e | 2015-02-20 10:01:01 -0500 | [diff] [blame] | 110 | // Link and verify |
| 111 | mFunctions->linkProgram(mProgramID); |
| 112 | |
Geoff Lang | 0ca5378 | 2015-05-07 13:49:39 -0400 | [diff] [blame] | 113 | // Detach the shaders |
| 114 | mFunctions->detachShader(mProgramID, vertexShaderGL->getShaderID()); |
| 115 | mFunctions->detachShader(mProgramID, fragmentShaderGL->getShaderID()); |
| 116 | |
| 117 | // Verify the link |
Geoff Lang | 01306fc | 2015-10-05 16:53:10 +0000 | [diff] [blame] | 118 | GLint linkStatus = GL_FALSE; |
| 119 | mFunctions->getProgramiv(mProgramID, GL_LINK_STATUS, &linkStatus); |
Geoff Lang | 01306fc | 2015-10-05 16:53:10 +0000 | [diff] [blame] | 120 | if (linkStatus == GL_FALSE) |
Geoff Lang | b1f435e | 2015-02-20 10:01:01 -0500 | [diff] [blame] | 121 | { |
Geoff Lang | 01306fc | 2015-10-05 16:53:10 +0000 | [diff] [blame] | 122 | // Linking failed, put the error into the info log |
| 123 | GLint infoLogLength = 0; |
| 124 | mFunctions->getProgramiv(mProgramID, GL_INFO_LOG_LENGTH, &infoLogLength); |
| 125 | |
Corentin Wallez | 01ad644 | 2016-03-03 13:53:45 -0500 | [diff] [blame] | 126 | std::string warning; |
Olli Etuaho | 6794676 | 2016-03-08 15:43:55 +0200 | [diff] [blame] | 127 | |
| 128 | // Info log length includes the null terminator, so 1 means that the info log is an empty |
| 129 | // string. |
| 130 | if (infoLogLength > 1) |
Corentin Wallez | 01ad644 | 2016-03-03 13:53:45 -0500 | [diff] [blame] | 131 | { |
| 132 | std::vector<char> buf(infoLogLength); |
| 133 | mFunctions->getProgramInfoLog(mProgramID, infoLogLength, nullptr, &buf[0]); |
Geoff Lang | 01306fc | 2015-10-05 16:53:10 +0000 | [diff] [blame] | 134 | |
Corentin Wallez | 01ad644 | 2016-03-03 13:53:45 -0500 | [diff] [blame] | 135 | mFunctions->deleteProgram(mProgramID); |
| 136 | mProgramID = 0; |
Geoff Lang | 01306fc | 2015-10-05 16:53:10 +0000 | [diff] [blame] | 137 | |
Corentin Wallez | 01ad644 | 2016-03-03 13:53:45 -0500 | [diff] [blame] | 138 | infoLog << buf.data(); |
unknown | b4a3af2 | 2015-11-25 15:02:51 -0500 | [diff] [blame] | 139 | |
Corentin Wallez | 01ad644 | 2016-03-03 13:53:45 -0500 | [diff] [blame] | 140 | warning = FormatString("Program link failed unexpectedly: %s", buf.data()); |
| 141 | } |
| 142 | else |
| 143 | { |
| 144 | warning = "Program link failed unexpectedly with no info log."; |
| 145 | } |
unknown | b4a3af2 | 2015-11-25 15:02:51 -0500 | [diff] [blame] | 146 | ANGLEPlatformCurrent()->logWarning(warning.c_str()); |
| 147 | TRACE("\n%s", warning.c_str()); |
Geoff Lang | 01306fc | 2015-10-05 16:53:10 +0000 | [diff] [blame] | 148 | |
| 149 | // TODO, return GL_OUT_OF_MEMORY or just fail the link? This is an unexpected case |
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 | 01306fc | 2015-10-05 16:53:10 +0000 | [diff] [blame] | 158 | // Query the uniform information |
| 159 | ASSERT(mUniformRealLocationMap.empty()); |
| 160 | const auto &uniforms = mData.getUniforms(); |
| 161 | for (const gl::VariableLocation &entry : mData.getUniformLocations()) |
| 162 | { |
| 163 | // From the spec: |
| 164 | // "Locations for sequential array indices are not required to be sequential." |
| 165 | const gl::LinkedUniform &uniform = uniforms[entry.index]; |
| 166 | std::stringstream fullNameStr; |
| 167 | fullNameStr << uniform.name; |
| 168 | if (uniform.isArray()) |
| 169 | { |
| 170 | fullNameStr << "[" << entry.element << "]"; |
| 171 | } |
| 172 | const std::string &fullName = fullNameStr.str(); |
| 173 | |
| 174 | GLint realLocation = mFunctions->getUniformLocation(mProgramID, fullName.c_str()); |
| 175 | mUniformRealLocationMap.push_back(realLocation); |
| 176 | } |
| 177 | |
| 178 | mUniformIndexToSamplerIndex.resize(mData.getUniforms().size(), GL_INVALID_INDEX); |
| 179 | |
| 180 | for (size_t uniformId = 0; uniformId < uniforms.size(); ++uniformId) |
| 181 | { |
| 182 | const gl::LinkedUniform &linkedUniform = uniforms[uniformId]; |
| 183 | |
| 184 | if (!linkedUniform.isSampler() || !linkedUniform.staticUse) |
| 185 | continue; |
| 186 | |
| 187 | mUniformIndexToSamplerIndex[uniformId] = mSamplerBindings.size(); |
| 188 | |
| 189 | // If uniform is a sampler type, insert it into the mSamplerBindings array |
| 190 | SamplerBindingGL samplerBinding; |
| 191 | samplerBinding.textureType = gl::SamplerTypeToTextureType(linkedUniform.type); |
| 192 | samplerBinding.boundTextureUnits.resize(linkedUniform.elementCount(), 0); |
| 193 | mSamplerBindings.push_back(samplerBinding); |
| 194 | } |
Geoff Lang | b1f435e | 2015-02-20 10:01:01 -0500 | [diff] [blame] | 195 | |
Geoff Lang | b1f435e | 2015-02-20 10:01:01 -0500 | [diff] [blame] | 196 | return LinkResult(true, gl::Error(GL_NO_ERROR)); |
Geoff Lang | f9a6f08 | 2015-01-22 13:32:49 -0500 | [diff] [blame] | 197 | } |
| 198 | |
Jamie Madill | 36cfd6a | 2015-08-18 10:46:20 -0400 | [diff] [blame] | 199 | GLboolean ProgramGL::validate(const gl::Caps & /*caps*/, gl::InfoLog * /*infoLog*/) |
| 200 | { |
| 201 | // TODO(jmadill): implement validate |
| 202 | return true; |
| 203 | } |
| 204 | |
Geoff Lang | f9a6f08 | 2015-01-22 13:32:49 -0500 | [diff] [blame] | 205 | void ProgramGL::setUniform1fv(GLint location, GLsizei count, const GLfloat *v) |
| 206 | { |
Geoff Lang | 63cbace | 2015-02-26 10:03:12 -0500 | [diff] [blame] | 207 | mStateManager->useProgram(mProgramID); |
Jamie Madill | 62d31cb | 2015-09-11 13:25:51 -0400 | [diff] [blame] | 208 | mFunctions->uniform1fv(uniLoc(location), count, v); |
Geoff Lang | f9a6f08 | 2015-01-22 13:32:49 -0500 | [diff] [blame] | 209 | } |
| 210 | |
| 211 | void ProgramGL::setUniform2fv(GLint location, GLsizei count, const GLfloat *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->uniform2fv(uniLoc(location), count, v); |
Geoff Lang | f9a6f08 | 2015-01-22 13:32:49 -0500 | [diff] [blame] | 215 | } |
| 216 | |
| 217 | void ProgramGL::setUniform3fv(GLint location, GLsizei count, const GLfloat *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->uniform3fv(uniLoc(location), count, v); |
Geoff Lang | f9a6f08 | 2015-01-22 13:32:49 -0500 | [diff] [blame] | 221 | } |
| 222 | |
| 223 | void ProgramGL::setUniform4fv(GLint location, GLsizei count, const GLfloat *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->uniform4fv(uniLoc(location), count, v); |
Geoff Lang | f9a6f08 | 2015-01-22 13:32:49 -0500 | [diff] [blame] | 227 | } |
| 228 | |
| 229 | void ProgramGL::setUniform1iv(GLint location, GLsizei count, const GLint *v) |
| 230 | { |
Geoff Lang | 63cbace | 2015-02-26 10:03:12 -0500 | [diff] [blame] | 231 | mStateManager->useProgram(mProgramID); |
Jamie Madill | 62d31cb | 2015-09-11 13:25:51 -0400 | [diff] [blame] | 232 | mFunctions->uniform1iv(uniLoc(location), count, v); |
Geoff Lang | f51bc79 | 2015-05-04 14:57:03 -0400 | [diff] [blame] | 233 | |
Jamie Madill | 62d31cb | 2015-09-11 13:25:51 -0400 | [diff] [blame] | 234 | const gl::VariableLocation &locationEntry = mData.getUniformLocations()[location]; |
| 235 | |
| 236 | size_t samplerIndex = mUniformIndexToSamplerIndex[locationEntry.index]; |
| 237 | if (samplerIndex != GL_INVALID_INDEX) |
Geoff Lang | f51bc79 | 2015-05-04 14:57:03 -0400 | [diff] [blame] | 238 | { |
Jamie Madill | 62d31cb | 2015-09-11 13:25:51 -0400 | [diff] [blame] | 239 | std::vector<GLuint> &boundTextureUnits = mSamplerBindings[samplerIndex].boundTextureUnits; |
Geoff Lang | f51bc79 | 2015-05-04 14:57:03 -0400 | [diff] [blame] | 240 | |
Jamie Madill | 62d31cb | 2015-09-11 13:25:51 -0400 | [diff] [blame] | 241 | size_t copyCount = |
| 242 | std::max<size_t>(count, boundTextureUnits.size() - locationEntry.element); |
| 243 | std::copy(v, v + copyCount, boundTextureUnits.begin() + locationEntry.element); |
Geoff Lang | f51bc79 | 2015-05-04 14:57:03 -0400 | [diff] [blame] | 244 | } |
Geoff Lang | f9a6f08 | 2015-01-22 13:32:49 -0500 | [diff] [blame] | 245 | } |
| 246 | |
| 247 | void ProgramGL::setUniform2iv(GLint location, GLsizei count, const GLint *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->uniform2iv(uniLoc(location), count, v); |
Geoff Lang | f9a6f08 | 2015-01-22 13:32:49 -0500 | [diff] [blame] | 251 | } |
| 252 | |
| 253 | void ProgramGL::setUniform3iv(GLint location, GLsizei count, const GLint *v) |
| 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->uniform3iv(uniLoc(location), count, v); |
Geoff Lang | f9a6f08 | 2015-01-22 13:32:49 -0500 | [diff] [blame] | 257 | } |
| 258 | |
| 259 | void ProgramGL::setUniform4iv(GLint location, GLsizei count, const GLint *v) |
| 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->uniform4iv(uniLoc(location), count, v); |
Geoff Lang | f9a6f08 | 2015-01-22 13:32:49 -0500 | [diff] [blame] | 263 | } |
| 264 | |
| 265 | void ProgramGL::setUniform1uiv(GLint location, GLsizei count, const GLuint *v) |
| 266 | { |
Geoff Lang | 63cbace | 2015-02-26 10:03:12 -0500 | [diff] [blame] | 267 | mStateManager->useProgram(mProgramID); |
Geoff Lang | b1f435e | 2015-02-20 10:01:01 -0500 | [diff] [blame] | 268 | mFunctions->uniform1uiv(location, count, v); |
Geoff Lang | f9a6f08 | 2015-01-22 13:32:49 -0500 | [diff] [blame] | 269 | } |
| 270 | |
| 271 | void ProgramGL::setUniform2uiv(GLint location, GLsizei count, const GLuint *v) |
| 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->uniform2uiv(uniLoc(location), count, v); |
Geoff Lang | f9a6f08 | 2015-01-22 13:32:49 -0500 | [diff] [blame] | 275 | } |
| 276 | |
| 277 | void ProgramGL::setUniform3uiv(GLint location, GLsizei count, const GLuint *v) |
| 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->uniform3uiv(uniLoc(location), count, v); |
Geoff Lang | f9a6f08 | 2015-01-22 13:32:49 -0500 | [diff] [blame] | 281 | } |
| 282 | |
| 283 | void ProgramGL::setUniform4uiv(GLint location, GLsizei count, const GLuint *v) |
| 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->uniform4uiv(uniLoc(location), count, v); |
Geoff Lang | f9a6f08 | 2015-01-22 13:32:49 -0500 | [diff] [blame] | 287 | } |
| 288 | |
| 289 | void ProgramGL::setUniformMatrix2fv(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->uniformMatrix2fv(uniLoc(location), count, transpose, value); |
Geoff Lang | f9a6f08 | 2015-01-22 13:32:49 -0500 | [diff] [blame] | 293 | } |
| 294 | |
| 295 | void ProgramGL::setUniformMatrix3fv(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->uniformMatrix3fv(uniLoc(location), count, transpose, value); |
Geoff Lang | f9a6f08 | 2015-01-22 13:32:49 -0500 | [diff] [blame] | 299 | } |
| 300 | |
| 301 | void ProgramGL::setUniformMatrix4fv(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->uniformMatrix4fv(uniLoc(location), count, transpose, value); |
Geoff Lang | f9a6f08 | 2015-01-22 13:32:49 -0500 | [diff] [blame] | 305 | } |
| 306 | |
| 307 | void ProgramGL::setUniformMatrix2x3fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value) |
| 308 | { |
Geoff Lang | 63cbace | 2015-02-26 10:03:12 -0500 | [diff] [blame] | 309 | mStateManager->useProgram(mProgramID); |
Jamie Madill | 62d31cb | 2015-09-11 13:25:51 -0400 | [diff] [blame] | 310 | mFunctions->uniformMatrix2x3fv(uniLoc(location), count, transpose, value); |
Geoff Lang | f9a6f08 | 2015-01-22 13:32:49 -0500 | [diff] [blame] | 311 | } |
| 312 | |
| 313 | void ProgramGL::setUniformMatrix3x2fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value) |
| 314 | { |
Geoff Lang | 63cbace | 2015-02-26 10:03:12 -0500 | [diff] [blame] | 315 | mStateManager->useProgram(mProgramID); |
Jamie Madill | 62d31cb | 2015-09-11 13:25:51 -0400 | [diff] [blame] | 316 | mFunctions->uniformMatrix3x2fv(uniLoc(location), count, transpose, value); |
Geoff Lang | f9a6f08 | 2015-01-22 13:32:49 -0500 | [diff] [blame] | 317 | } |
| 318 | |
| 319 | void ProgramGL::setUniformMatrix2x4fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value) |
| 320 | { |
Geoff Lang | 63cbace | 2015-02-26 10:03:12 -0500 | [diff] [blame] | 321 | mStateManager->useProgram(mProgramID); |
Jamie Madill | 62d31cb | 2015-09-11 13:25:51 -0400 | [diff] [blame] | 322 | mFunctions->uniformMatrix2x4fv(uniLoc(location), count, transpose, value); |
Geoff Lang | f9a6f08 | 2015-01-22 13:32:49 -0500 | [diff] [blame] | 323 | } |
| 324 | |
| 325 | void ProgramGL::setUniformMatrix4x2fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value) |
| 326 | { |
Geoff Lang | 63cbace | 2015-02-26 10:03:12 -0500 | [diff] [blame] | 327 | mStateManager->useProgram(mProgramID); |
Jamie Madill | 62d31cb | 2015-09-11 13:25:51 -0400 | [diff] [blame] | 328 | mFunctions->uniformMatrix4x2fv(uniLoc(location), count, transpose, value); |
Geoff Lang | f9a6f08 | 2015-01-22 13:32:49 -0500 | [diff] [blame] | 329 | } |
| 330 | |
| 331 | void ProgramGL::setUniformMatrix3x4fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value) |
| 332 | { |
Geoff Lang | 63cbace | 2015-02-26 10:03:12 -0500 | [diff] [blame] | 333 | mStateManager->useProgram(mProgramID); |
Jamie Madill | 62d31cb | 2015-09-11 13:25:51 -0400 | [diff] [blame] | 334 | mFunctions->uniformMatrix3x4fv(uniLoc(location), count, transpose, value); |
Geoff Lang | f9a6f08 | 2015-01-22 13:32:49 -0500 | [diff] [blame] | 335 | } |
| 336 | |
| 337 | void ProgramGL::setUniformMatrix4x3fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value) |
| 338 | { |
Geoff Lang | 63cbace | 2015-02-26 10:03:12 -0500 | [diff] [blame] | 339 | mStateManager->useProgram(mProgramID); |
Jamie Madill | 62d31cb | 2015-09-11 13:25:51 -0400 | [diff] [blame] | 340 | mFunctions->uniformMatrix4x3fv(uniLoc(location), count, transpose, value); |
Geoff Lang | f9a6f08 | 2015-01-22 13:32:49 -0500 | [diff] [blame] | 341 | } |
| 342 | |
Geoff Lang | 5d124a6 | 2015-09-15 13:03:27 -0400 | [diff] [blame] | 343 | void ProgramGL::setUniformBlockBinding(GLuint uniformBlockIndex, GLuint uniformBlockBinding) |
| 344 | { |
Jamie Madill | 4a3c234 | 2015-10-08 12:58:45 -0400 | [diff] [blame] | 345 | // Lazy init |
| 346 | if (mUniformBlockRealLocationMap.empty()) |
| 347 | { |
| 348 | mUniformBlockRealLocationMap.reserve(mData.getUniformBlocks().size()); |
| 349 | for (const gl::UniformBlock &uniformBlock : mData.getUniformBlocks()) |
| 350 | { |
| 351 | const std::string &nameWithIndex = uniformBlock.nameWithArrayIndex(); |
| 352 | GLuint blockIndex = mFunctions->getUniformBlockIndex(mProgramID, nameWithIndex.c_str()); |
| 353 | mUniformBlockRealLocationMap.push_back(blockIndex); |
| 354 | } |
| 355 | } |
| 356 | |
| 357 | GLuint realBlockIndex = mUniformBlockRealLocationMap[uniformBlockIndex]; |
| 358 | if (realBlockIndex != GL_INVALID_INDEX) |
| 359 | { |
| 360 | mFunctions->uniformBlockBinding(mProgramID, realBlockIndex, uniformBlockBinding); |
| 361 | } |
Geoff Lang | 5d124a6 | 2015-09-15 13:03:27 -0400 | [diff] [blame] | 362 | } |
| 363 | |
Geoff Lang | 01306fc | 2015-10-05 16:53:10 +0000 | [diff] [blame] | 364 | void ProgramGL::reset() |
| 365 | { |
| 366 | mUniformRealLocationMap.clear(); |
Jamie Madill | 4a3c234 | 2015-10-08 12:58:45 -0400 | [diff] [blame] | 367 | mUniformBlockRealLocationMap.clear(); |
Geoff Lang | 01306fc | 2015-10-05 16:53:10 +0000 | [diff] [blame] | 368 | mSamplerBindings.clear(); |
| 369 | mUniformIndexToSamplerIndex.clear(); |
| 370 | } |
| 371 | |
Geoff Lang | b1f435e | 2015-02-20 10:01:01 -0500 | [diff] [blame] | 372 | GLuint ProgramGL::getProgramID() const |
| 373 | { |
| 374 | return mProgramID; |
| 375 | } |
| 376 | |
Geoff Lang | f51bc79 | 2015-05-04 14:57:03 -0400 | [diff] [blame] | 377 | const std::vector<SamplerBindingGL> &ProgramGL::getAppliedSamplerUniforms() const |
| 378 | { |
| 379 | return mSamplerBindings; |
| 380 | } |
| 381 | |
Jamie Madill | 4a3c234 | 2015-10-08 12:58:45 -0400 | [diff] [blame] | 382 | bool ProgramGL::getUniformBlockSize(const std::string &blockName, size_t *sizeOut) const |
Jamie Madill | 62d31cb | 2015-09-11 13:25:51 -0400 | [diff] [blame] | 383 | { |
Jamie Madill | 4a3c234 | 2015-10-08 12:58:45 -0400 | [diff] [blame] | 384 | ASSERT(mProgramID != 0u); |
Geoff Lang | 5d124a6 | 2015-09-15 13:03:27 -0400 | [diff] [blame] | 385 | |
Jamie Madill | 4a3c234 | 2015-10-08 12:58:45 -0400 | [diff] [blame] | 386 | GLuint blockIndex = mFunctions->getUniformBlockIndex(mProgramID, blockName.c_str()); |
| 387 | if (blockIndex == GL_INVALID_INDEX) |
Geoff Lang | 5d124a6 | 2015-09-15 13:03:27 -0400 | [diff] [blame] | 388 | { |
Jamie Madill | 4a3c234 | 2015-10-08 12:58:45 -0400 | [diff] [blame] | 389 | *sizeOut = 0; |
| 390 | return false; |
Geoff Lang | 5d124a6 | 2015-09-15 13:03:27 -0400 | [diff] [blame] | 391 | } |
| 392 | |
Jamie Madill | 4a3c234 | 2015-10-08 12:58:45 -0400 | [diff] [blame] | 393 | GLint dataSize = 0; |
| 394 | mFunctions->getActiveUniformBlockiv(mProgramID, blockIndex, GL_UNIFORM_BLOCK_DATA_SIZE, |
| 395 | &dataSize); |
| 396 | *sizeOut = static_cast<size_t>(dataSize); |
| 397 | return true; |
| 398 | } |
| 399 | |
| 400 | bool ProgramGL::getUniformBlockMemberInfo(const std::string &memberUniformName, |
| 401 | sh::BlockMemberInfo *memberInfoOut) const |
| 402 | { |
| 403 | GLuint uniformIndex; |
| 404 | const GLchar *memberNameGLStr = memberUniformName.c_str(); |
| 405 | mFunctions->getUniformIndices(mProgramID, 1, &memberNameGLStr, &uniformIndex); |
| 406 | |
| 407 | if (uniformIndex == GL_INVALID_INDEX) |
Geoff Lang | 5d124a6 | 2015-09-15 13:03:27 -0400 | [diff] [blame] | 408 | { |
Jamie Madill | 4a3c234 | 2015-10-08 12:58:45 -0400 | [diff] [blame] | 409 | *memberInfoOut = sh::BlockMemberInfo::getDefaultBlockInfo(); |
| 410 | return false; |
Geoff Lang | 5d124a6 | 2015-09-15 13:03:27 -0400 | [diff] [blame] | 411 | } |
Jamie Madill | 4a3c234 | 2015-10-08 12:58:45 -0400 | [diff] [blame] | 412 | |
| 413 | mFunctions->getActiveUniformsiv(mProgramID, 1, &uniformIndex, GL_UNIFORM_OFFSET, |
| 414 | &memberInfoOut->offset); |
| 415 | mFunctions->getActiveUniformsiv(mProgramID, 1, &uniformIndex, GL_UNIFORM_ARRAY_STRIDE, |
| 416 | &memberInfoOut->arrayStride); |
| 417 | mFunctions->getActiveUniformsiv(mProgramID, 1, &uniformIndex, GL_UNIFORM_MATRIX_STRIDE, |
| 418 | &memberInfoOut->matrixStride); |
| 419 | |
| 420 | // TODO(jmadill): possibly determine this at the gl::Program level. |
| 421 | GLint isRowMajorMatrix = 0; |
| 422 | mFunctions->getActiveUniformsiv(mProgramID, 1, &uniformIndex, GL_UNIFORM_IS_ROW_MAJOR, |
| 423 | &isRowMajorMatrix); |
| 424 | memberInfoOut->isRowMajorMatrix = isRowMajorMatrix != GL_FALSE; |
| 425 | return true; |
Jamie Madill | 62d31cb | 2015-09-11 13:25:51 -0400 | [diff] [blame] | 426 | } |
Jamie Madill | 4a3c234 | 2015-10-08 12:58:45 -0400 | [diff] [blame] | 427 | |
| 428 | } // namespace rx |