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" |
Jamie Madill | c564c07 | 2017-06-01 12:45:42 -0400 | [diff] [blame] | 12 | #include "common/bitset_utils.h" |
Geoff Lang | f9a6f08 | 2015-01-22 13:32:49 -0500 | [diff] [blame] | 13 | #include "common/debug.h" |
Sami Väisänen | 46eaa94 | 2016-06-29 10:26:37 +0300 | [diff] [blame] | 14 | #include "common/string_utils.h" |
Geoff Lang | 5ed74cf | 2015-04-14 13:57:07 -0400 | [diff] [blame] | 15 | #include "common/utilities.h" |
Jamie Madill | c564c07 | 2017-06-01 12:45:42 -0400 | [diff] [blame] | 16 | #include "libANGLE/Context.h" |
Jamie Madill | 6db1c2e | 2017-11-08 09:17:40 -0500 | [diff] [blame] | 17 | #include "libANGLE/ProgramLinkedResources.h" |
Jamie Madill | c564c07 | 2017-06-01 12:45:42 -0400 | [diff] [blame] | 18 | #include "libANGLE/Uniform.h" |
Jamie Madill | a7d12dc | 2016-12-13 15:08:19 -0500 | [diff] [blame] | 19 | #include "libANGLE/renderer/gl/ContextGL.h" |
Geoff Lang | b1f435e | 2015-02-20 10:01:01 -0500 | [diff] [blame] | 20 | #include "libANGLE/renderer/gl/FunctionsGL.h" |
| 21 | #include "libANGLE/renderer/gl/ShaderGL.h" |
| 22 | #include "libANGLE/renderer/gl/StateManagerGL.h" |
Philippe Hamel | 4091119 | 2016-04-07 16:45:50 -0400 | [diff] [blame] | 23 | #include "libANGLE/renderer/gl/WorkaroundsGL.h" |
unknown | b4a3af2 | 2015-11-25 15:02:51 -0500 | [diff] [blame] | 24 | #include "platform/Platform.h" |
Geoff Lang | f9a6f08 | 2015-01-22 13:32:49 -0500 | [diff] [blame] | 25 | |
| 26 | namespace rx |
| 27 | { |
| 28 | |
Jamie Madill | 48ef11b | 2016-04-27 15:21:52 -0400 | [diff] [blame] | 29 | ProgramGL::ProgramGL(const gl::ProgramState &data, |
Jamie Madill | 5c6b7bf | 2015-08-17 12:53:35 -0400 | [diff] [blame] | 30 | const FunctionsGL *functions, |
Philippe Hamel | 4091119 | 2016-04-07 16:45:50 -0400 | [diff] [blame] | 31 | const WorkaroundsGL &workarounds, |
Sami Väisänen | 46eaa94 | 2016-06-29 10:26:37 +0300 | [diff] [blame] | 32 | StateManagerGL *stateManager, |
| 33 | bool enablePathRendering) |
Philippe Hamel | 4091119 | 2016-04-07 16:45:50 -0400 | [diff] [blame] | 34 | : ProgramImpl(data), |
| 35 | mFunctions(functions), |
| 36 | mWorkarounds(workarounds), |
| 37 | mStateManager(stateManager), |
Sami Väisänen | 46eaa94 | 2016-06-29 10:26:37 +0300 | [diff] [blame] | 38 | mEnablePathRendering(enablePathRendering), |
Martin Radev | 4e619f5 | 2017-08-09 11:50:06 +0300 | [diff] [blame] | 39 | mMultiviewBaseViewLayerIndexUniformLocation(-1), |
Philippe Hamel | 4091119 | 2016-04-07 16:45:50 -0400 | [diff] [blame] | 40 | mProgramID(0) |
Geoff Lang | b1f435e | 2015-02-20 10:01:01 -0500 | [diff] [blame] | 41 | { |
| 42 | ASSERT(mFunctions); |
| 43 | ASSERT(mStateManager); |
Geoff Lang | 0ca5378 | 2015-05-07 13:49:39 -0400 | [diff] [blame] | 44 | |
| 45 | mProgramID = mFunctions->createProgram(); |
Geoff Lang | b1f435e | 2015-02-20 10:01:01 -0500 | [diff] [blame] | 46 | } |
Geoff Lang | f9a6f08 | 2015-01-22 13:32:49 -0500 | [diff] [blame] | 47 | |
| 48 | ProgramGL::~ProgramGL() |
Geoff Lang | b1f435e | 2015-02-20 10:01:01 -0500 | [diff] [blame] | 49 | { |
Geoff Lang | 0ca5378 | 2015-05-07 13:49:39 -0400 | [diff] [blame] | 50 | mFunctions->deleteProgram(mProgramID); |
| 51 | mProgramID = 0; |
Geoff Lang | b1f435e | 2015-02-20 10:01:01 -0500 | [diff] [blame] | 52 | } |
Geoff Lang | f9a6f08 | 2015-01-22 13:32:49 -0500 | [diff] [blame] | 53 | |
Jamie Madill | 9cf9e87 | 2017-06-05 12:59:25 -0400 | [diff] [blame] | 54 | gl::LinkResult ProgramGL::load(const gl::Context *context, |
| 55 | gl::InfoLog &infoLog, |
| 56 | gl::BinaryInputStream *stream) |
Geoff Lang | f9a6f08 | 2015-01-22 13:32:49 -0500 | [diff] [blame] | 57 | { |
Geoff Lang | 65a0be9 | 2015-10-02 09:57:30 -0400 | [diff] [blame] | 58 | preLink(); |
| 59 | |
| 60 | // Read the binary format, size and blob |
| 61 | GLenum binaryFormat = stream->readInt<GLenum>(); |
| 62 | GLint binaryLength = stream->readInt<GLint>(); |
| 63 | const uint8_t *binary = stream->data() + stream->offset(); |
| 64 | stream->skip(binaryLength); |
| 65 | |
| 66 | // Load the binary |
| 67 | mFunctions->programBinary(mProgramID, binaryFormat, binary, binaryLength); |
| 68 | |
| 69 | // Verify that the program linked |
| 70 | if (!checkLinkStatus(infoLog)) |
| 71 | { |
Jamie Madill | b0a838b | 2016-11-13 20:02:12 -0500 | [diff] [blame] | 72 | return false; |
Geoff Lang | 65a0be9 | 2015-10-02 09:57:30 -0400 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | postLink(); |
Jamie Madill | 27a6063 | 2017-06-30 15:12:01 -0400 | [diff] [blame] | 76 | reapplyUBOBindingsIfNeeded(context); |
Jamie Madill | a7d12dc | 2016-12-13 15:08:19 -0500 | [diff] [blame] | 77 | |
Jamie Madill | b0a838b | 2016-11-13 20:02:12 -0500 | [diff] [blame] | 78 | return true; |
Geoff Lang | f9a6f08 | 2015-01-22 13:32:49 -0500 | [diff] [blame] | 79 | } |
| 80 | |
Jamie Madill | 27a6063 | 2017-06-30 15:12:01 -0400 | [diff] [blame] | 81 | void ProgramGL::save(const gl::Context *context, gl::BinaryOutputStream *stream) |
Geoff Lang | f9a6f08 | 2015-01-22 13:32:49 -0500 | [diff] [blame] | 82 | { |
Geoff Lang | 65a0be9 | 2015-10-02 09:57:30 -0400 | [diff] [blame] | 83 | GLint binaryLength = 0; |
| 84 | mFunctions->getProgramiv(mProgramID, GL_PROGRAM_BINARY_LENGTH, &binaryLength); |
| 85 | |
| 86 | std::vector<uint8_t> binary(binaryLength); |
| 87 | GLenum binaryFormat = GL_NONE; |
| 88 | mFunctions->getProgramBinary(mProgramID, binaryLength, &binaryLength, &binaryFormat, |
| 89 | &binary[0]); |
| 90 | |
| 91 | stream->writeInt(binaryFormat); |
| 92 | stream->writeInt(binaryLength); |
| 93 | stream->writeBytes(&binary[0], binaryLength); |
Jamie Madill | 27a6063 | 2017-06-30 15:12:01 -0400 | [diff] [blame] | 94 | |
| 95 | reapplyUBOBindingsIfNeeded(context); |
| 96 | } |
| 97 | |
| 98 | void ProgramGL::reapplyUBOBindingsIfNeeded(const gl::Context *context) |
| 99 | { |
| 100 | // Re-apply UBO bindings to work around driver bugs. |
| 101 | const WorkaroundsGL &workaroundsGL = GetImplAs<ContextGL>(context)->getWorkaroundsGL(); |
| 102 | if (workaroundsGL.reapplyUBOBindingsAfterUsingBinaryProgram) |
| 103 | { |
| 104 | const auto &blocks = mState.getUniformBlocks(); |
| 105 | for (size_t blockIndex : mState.getActiveUniformBlockBindingsMask()) |
| 106 | { |
| 107 | setUniformBlockBinding(static_cast<GLuint>(blockIndex), blocks[blockIndex].binding); |
| 108 | } |
| 109 | } |
Geoff Lang | f9a6f08 | 2015-01-22 13:32:49 -0500 | [diff] [blame] | 110 | } |
| 111 | |
Geoff Lang | c562975 | 2015-12-07 16:29:04 -0500 | [diff] [blame] | 112 | void ProgramGL::setBinaryRetrievableHint(bool retrievable) |
| 113 | { |
Geoff Lang | 65a0be9 | 2015-10-02 09:57:30 -0400 | [diff] [blame] | 114 | // glProgramParameteri isn't always available on ES backends. |
| 115 | if (mFunctions->programParameteri) |
| 116 | { |
| 117 | mFunctions->programParameteri(mProgramID, GL_PROGRAM_BINARY_RETRIEVABLE_HINT, |
| 118 | retrievable ? GL_TRUE : GL_FALSE); |
| 119 | } |
Geoff Lang | c562975 | 2015-12-07 16:29:04 -0500 | [diff] [blame] | 120 | } |
| 121 | |
Yunchao He | 61afff1 | 2017-03-14 15:34:03 +0800 | [diff] [blame] | 122 | void ProgramGL::setSeparable(bool separable) |
| 123 | { |
| 124 | mFunctions->programParameteri(mProgramID, GL_PROGRAM_SEPARABLE, separable ? GL_TRUE : GL_FALSE); |
| 125 | } |
| 126 | |
Jamie Madill | 9cf9e87 | 2017-06-05 12:59:25 -0400 | [diff] [blame] | 127 | gl::LinkResult ProgramGL::link(const gl::Context *context, |
Jamie Madill | c9727f3 | 2017-11-07 12:37:07 -0500 | [diff] [blame] | 128 | const gl::ProgramLinkedResources &resources, |
Jamie Madill | 9cf9e87 | 2017-06-05 12:59:25 -0400 | [diff] [blame] | 129 | gl::InfoLog &infoLog) |
Geoff Lang | f9a6f08 | 2015-01-22 13:32:49 -0500 | [diff] [blame] | 130 | { |
Geoff Lang | 65a0be9 | 2015-10-02 09:57:30 -0400 | [diff] [blame] | 131 | preLink(); |
Geoff Lang | b1f435e | 2015-02-20 10:01:01 -0500 | [diff] [blame] | 132 | |
Martin Radev | 4c4c8e7 | 2016-08-04 12:25:34 +0300 | [diff] [blame] | 133 | if (mState.getAttachedComputeShader()) |
Geoff Lang | 1a68346 | 2015-09-29 15:09:59 -0400 | [diff] [blame] | 134 | { |
Martin Radev | 4c4c8e7 | 2016-08-04 12:25:34 +0300 | [diff] [blame] | 135 | const ShaderGL *computeShaderGL = GetImplAs<ShaderGL>(mState.getAttachedComputeShader()); |
Geoff Lang | 1a68346 | 2015-09-29 15:09:59 -0400 | [diff] [blame] | 136 | |
Martin Radev | 4c4c8e7 | 2016-08-04 12:25:34 +0300 | [diff] [blame] | 137 | mFunctions->attachShader(mProgramID, computeShaderGL->getShaderID()); |
| 138 | |
| 139 | // Link and verify |
| 140 | mFunctions->linkProgram(mProgramID); |
| 141 | |
| 142 | // Detach the shaders |
| 143 | mFunctions->detachShader(mProgramID, computeShaderGL->getShaderID()); |
Geoff Lang | 1a68346 | 2015-09-29 15:09:59 -0400 | [diff] [blame] | 144 | } |
| 145 | else |
| 146 | { |
Martin Radev | 4c4c8e7 | 2016-08-04 12:25:34 +0300 | [diff] [blame] | 147 | // Set the transform feedback state |
Olli Etuaho | 855d964 | 2017-05-17 14:05:06 +0300 | [diff] [blame] | 148 | std::vector<std::string> transformFeedbackVaryingMappedNames; |
Martin Radev | 4c4c8e7 | 2016-08-04 12:25:34 +0300 | [diff] [blame] | 149 | for (const auto &tfVarying : mState.getTransformFeedbackVaryingNames()) |
Geoff Lang | 1528e56 | 2015-08-24 15:10:58 -0400 | [diff] [blame] | 150 | { |
Olli Etuaho | 855d964 | 2017-05-17 14:05:06 +0300 | [diff] [blame] | 151 | std::string tfVaryingMappedName = |
| 152 | mState.getAttachedVertexShader()->getTransformFeedbackVaryingMappedName(tfVarying, |
| 153 | context); |
| 154 | transformFeedbackVaryingMappedNames.push_back(tfVaryingMappedName); |
Geoff Lang | 1528e56 | 2015-08-24 15:10:58 -0400 | [diff] [blame] | 155 | } |
| 156 | |
Olli Etuaho | 855d964 | 2017-05-17 14:05:06 +0300 | [diff] [blame] | 157 | if (transformFeedbackVaryingMappedNames.empty()) |
Martin Radev | 4c4c8e7 | 2016-08-04 12:25:34 +0300 | [diff] [blame] | 158 | { |
| 159 | if (mFunctions->transformFeedbackVaryings) |
| 160 | { |
| 161 | mFunctions->transformFeedbackVaryings(mProgramID, 0, nullptr, |
| 162 | mState.getTransformFeedbackBufferMode()); |
| 163 | } |
| 164 | } |
| 165 | else |
| 166 | { |
| 167 | ASSERT(mFunctions->transformFeedbackVaryings); |
Olli Etuaho | 855d964 | 2017-05-17 14:05:06 +0300 | [diff] [blame] | 168 | std::vector<const GLchar *> transformFeedbackVaryings; |
| 169 | for (const auto &varying : transformFeedbackVaryingMappedNames) |
| 170 | { |
| 171 | transformFeedbackVaryings.push_back(varying.c_str()); |
| 172 | } |
Martin Radev | 4c4c8e7 | 2016-08-04 12:25:34 +0300 | [diff] [blame] | 173 | mFunctions->transformFeedbackVaryings( |
Olli Etuaho | 855d964 | 2017-05-17 14:05:06 +0300 | [diff] [blame] | 174 | mProgramID, static_cast<GLsizei>(transformFeedbackVaryingMappedNames.size()), |
Martin Radev | 4c4c8e7 | 2016-08-04 12:25:34 +0300 | [diff] [blame] | 175 | &transformFeedbackVaryings[0], mState.getTransformFeedbackBufferMode()); |
| 176 | } |
| 177 | |
| 178 | const ShaderGL *vertexShaderGL = GetImplAs<ShaderGL>(mState.getAttachedVertexShader()); |
| 179 | const ShaderGL *fragmentShaderGL = GetImplAs<ShaderGL>(mState.getAttachedFragmentShader()); |
| 180 | |
| 181 | // Attach the shaders |
| 182 | mFunctions->attachShader(mProgramID, vertexShaderGL->getShaderID()); |
| 183 | mFunctions->attachShader(mProgramID, fragmentShaderGL->getShaderID()); |
| 184 | |
| 185 | // Bind attribute locations to match the GL layer. |
| 186 | for (const sh::Attribute &attribute : mState.getAttributes()) |
| 187 | { |
Corentin Wallez | 6c1cbf5 | 2016-11-23 12:44:35 -0500 | [diff] [blame] | 188 | if (!attribute.staticUse || attribute.isBuiltIn()) |
Martin Radev | 4c4c8e7 | 2016-08-04 12:25:34 +0300 | [diff] [blame] | 189 | { |
| 190 | continue; |
| 191 | } |
| 192 | |
Olli Etuaho | 855d964 | 2017-05-17 14:05:06 +0300 | [diff] [blame] | 193 | mFunctions->bindAttribLocation(mProgramID, attribute.location, |
| 194 | attribute.mappedName.c_str()); |
Martin Radev | 4c4c8e7 | 2016-08-04 12:25:34 +0300 | [diff] [blame] | 195 | } |
| 196 | |
| 197 | // Link and verify |
| 198 | mFunctions->linkProgram(mProgramID); |
| 199 | |
| 200 | // Detach the shaders |
| 201 | mFunctions->detachShader(mProgramID, vertexShaderGL->getShaderID()); |
| 202 | mFunctions->detachShader(mProgramID, fragmentShaderGL->getShaderID()); |
Geoff Lang | 1528e56 | 2015-08-24 15:10:58 -0400 | [diff] [blame] | 203 | } |
| 204 | |
Geoff Lang | 0ca5378 | 2015-05-07 13:49:39 -0400 | [diff] [blame] | 205 | // Verify the link |
Geoff Lang | 65a0be9 | 2015-10-02 09:57:30 -0400 | [diff] [blame] | 206 | if (!checkLinkStatus(infoLog)) |
Geoff Lang | b1f435e | 2015-02-20 10:01:01 -0500 | [diff] [blame] | 207 | { |
Jamie Madill | b0a838b | 2016-11-13 20:02:12 -0500 | [diff] [blame] | 208 | return false; |
Geoff Lang | b1f435e | 2015-02-20 10:01:01 -0500 | [diff] [blame] | 209 | } |
| 210 | |
Philippe Hamel | 4091119 | 2016-04-07 16:45:50 -0400 | [diff] [blame] | 211 | if (mWorkarounds.alwaysCallUseProgramAfterLink) |
| 212 | { |
| 213 | mStateManager->forceUseProgram(mProgramID); |
| 214 | } |
| 215 | |
Jamie Madill | 6db1c2e | 2017-11-08 09:17:40 -0500 | [diff] [blame] | 216 | linkResources(resources); |
Geoff Lang | 65a0be9 | 2015-10-02 09:57:30 -0400 | [diff] [blame] | 217 | postLink(); |
Geoff Lang | b1f435e | 2015-02-20 10:01:01 -0500 | [diff] [blame] | 218 | |
Jamie Madill | b0a838b | 2016-11-13 20:02:12 -0500 | [diff] [blame] | 219 | return true; |
Geoff Lang | f9a6f08 | 2015-01-22 13:32:49 -0500 | [diff] [blame] | 220 | } |
| 221 | |
Jamie Madill | 36cfd6a | 2015-08-18 10:46:20 -0400 | [diff] [blame] | 222 | GLboolean ProgramGL::validate(const gl::Caps & /*caps*/, gl::InfoLog * /*infoLog*/) |
| 223 | { |
| 224 | // TODO(jmadill): implement validate |
| 225 | return true; |
| 226 | } |
| 227 | |
Geoff Lang | f9a6f08 | 2015-01-22 13:32:49 -0500 | [diff] [blame] | 228 | void ProgramGL::setUniform1fv(GLint location, GLsizei count, const GLfloat *v) |
| 229 | { |
Jiajia Qin | ee9f08c | 2016-11-16 10:06:10 +0800 | [diff] [blame] | 230 | if (mFunctions->programUniform1fv != nullptr) |
| 231 | { |
| 232 | mFunctions->programUniform1fv(mProgramID, uniLoc(location), count, v); |
| 233 | } |
| 234 | else |
| 235 | { |
| 236 | mStateManager->useProgram(mProgramID); |
| 237 | mFunctions->uniform1fv(uniLoc(location), count, v); |
| 238 | } |
Geoff Lang | f9a6f08 | 2015-01-22 13:32:49 -0500 | [diff] [blame] | 239 | } |
| 240 | |
| 241 | void ProgramGL::setUniform2fv(GLint location, GLsizei count, const GLfloat *v) |
| 242 | { |
Jiajia Qin | ee9f08c | 2016-11-16 10:06:10 +0800 | [diff] [blame] | 243 | if (mFunctions->programUniform2fv != nullptr) |
| 244 | { |
| 245 | mFunctions->programUniform2fv(mProgramID, uniLoc(location), count, v); |
| 246 | } |
| 247 | else |
| 248 | { |
| 249 | mStateManager->useProgram(mProgramID); |
| 250 | mFunctions->uniform2fv(uniLoc(location), count, v); |
| 251 | } |
Geoff Lang | f9a6f08 | 2015-01-22 13:32:49 -0500 | [diff] [blame] | 252 | } |
| 253 | |
| 254 | void ProgramGL::setUniform3fv(GLint location, GLsizei count, const GLfloat *v) |
| 255 | { |
Jiajia Qin | ee9f08c | 2016-11-16 10:06:10 +0800 | [diff] [blame] | 256 | if (mFunctions->programUniform3fv != nullptr) |
| 257 | { |
| 258 | mFunctions->programUniform3fv(mProgramID, uniLoc(location), count, v); |
| 259 | } |
| 260 | else |
| 261 | { |
| 262 | mStateManager->useProgram(mProgramID); |
| 263 | mFunctions->uniform3fv(uniLoc(location), count, v); |
| 264 | } |
Geoff Lang | f9a6f08 | 2015-01-22 13:32:49 -0500 | [diff] [blame] | 265 | } |
| 266 | |
| 267 | void ProgramGL::setUniform4fv(GLint location, GLsizei count, const GLfloat *v) |
| 268 | { |
Jiajia Qin | ee9f08c | 2016-11-16 10:06:10 +0800 | [diff] [blame] | 269 | if (mFunctions->programUniform4fv != nullptr) |
| 270 | { |
| 271 | mFunctions->programUniform4fv(mProgramID, uniLoc(location), count, v); |
| 272 | } |
| 273 | else |
| 274 | { |
| 275 | mStateManager->useProgram(mProgramID); |
| 276 | mFunctions->uniform4fv(uniLoc(location), count, v); |
| 277 | } |
Geoff Lang | f9a6f08 | 2015-01-22 13:32:49 -0500 | [diff] [blame] | 278 | } |
| 279 | |
| 280 | void ProgramGL::setUniform1iv(GLint location, GLsizei count, const GLint *v) |
| 281 | { |
Jiajia Qin | ee9f08c | 2016-11-16 10:06:10 +0800 | [diff] [blame] | 282 | if (mFunctions->programUniform1iv != nullptr) |
| 283 | { |
| 284 | mFunctions->programUniform1iv(mProgramID, uniLoc(location), count, v); |
| 285 | } |
| 286 | else |
| 287 | { |
| 288 | mStateManager->useProgram(mProgramID); |
| 289 | mFunctions->uniform1iv(uniLoc(location), count, v); |
| 290 | } |
Geoff Lang | f9a6f08 | 2015-01-22 13:32:49 -0500 | [diff] [blame] | 291 | } |
| 292 | |
| 293 | void ProgramGL::setUniform2iv(GLint location, GLsizei count, const GLint *v) |
| 294 | { |
Jiajia Qin | ee9f08c | 2016-11-16 10:06:10 +0800 | [diff] [blame] | 295 | if (mFunctions->programUniform2iv != nullptr) |
| 296 | { |
| 297 | mFunctions->programUniform2iv(mProgramID, uniLoc(location), count, v); |
| 298 | } |
| 299 | else |
| 300 | { |
| 301 | mStateManager->useProgram(mProgramID); |
| 302 | mFunctions->uniform2iv(uniLoc(location), count, v); |
| 303 | } |
Geoff Lang | f9a6f08 | 2015-01-22 13:32:49 -0500 | [diff] [blame] | 304 | } |
| 305 | |
| 306 | void ProgramGL::setUniform3iv(GLint location, GLsizei count, const GLint *v) |
| 307 | { |
Jiajia Qin | ee9f08c | 2016-11-16 10:06:10 +0800 | [diff] [blame] | 308 | if (mFunctions->programUniform3iv != nullptr) |
| 309 | { |
| 310 | mFunctions->programUniform3iv(mProgramID, uniLoc(location), count, v); |
| 311 | } |
| 312 | else |
| 313 | { |
| 314 | mStateManager->useProgram(mProgramID); |
| 315 | mFunctions->uniform3iv(uniLoc(location), count, v); |
| 316 | } |
Geoff Lang | f9a6f08 | 2015-01-22 13:32:49 -0500 | [diff] [blame] | 317 | } |
| 318 | |
| 319 | void ProgramGL::setUniform4iv(GLint location, GLsizei count, const GLint *v) |
| 320 | { |
Jiajia Qin | ee9f08c | 2016-11-16 10:06:10 +0800 | [diff] [blame] | 321 | if (mFunctions->programUniform4iv != nullptr) |
| 322 | { |
| 323 | mFunctions->programUniform4iv(mProgramID, uniLoc(location), count, v); |
| 324 | } |
| 325 | else |
| 326 | { |
| 327 | mStateManager->useProgram(mProgramID); |
| 328 | mFunctions->uniform4iv(uniLoc(location), count, v); |
| 329 | } |
Geoff Lang | f9a6f08 | 2015-01-22 13:32:49 -0500 | [diff] [blame] | 330 | } |
| 331 | |
| 332 | void ProgramGL::setUniform1uiv(GLint location, GLsizei count, const GLuint *v) |
| 333 | { |
Jiajia Qin | ee9f08c | 2016-11-16 10:06:10 +0800 | [diff] [blame] | 334 | if (mFunctions->programUniform1uiv != nullptr) |
| 335 | { |
| 336 | mFunctions->programUniform1uiv(mProgramID, uniLoc(location), count, v); |
| 337 | } |
| 338 | else |
| 339 | { |
| 340 | mStateManager->useProgram(mProgramID); |
| 341 | mFunctions->uniform1uiv(uniLoc(location), count, v); |
| 342 | } |
Geoff Lang | f9a6f08 | 2015-01-22 13:32:49 -0500 | [diff] [blame] | 343 | } |
| 344 | |
| 345 | void ProgramGL::setUniform2uiv(GLint location, GLsizei count, const GLuint *v) |
| 346 | { |
Jiajia Qin | ee9f08c | 2016-11-16 10:06:10 +0800 | [diff] [blame] | 347 | if (mFunctions->programUniform2uiv != nullptr) |
| 348 | { |
| 349 | mFunctions->programUniform2uiv(mProgramID, uniLoc(location), count, v); |
| 350 | } |
| 351 | else |
| 352 | { |
| 353 | mStateManager->useProgram(mProgramID); |
| 354 | mFunctions->uniform2uiv(uniLoc(location), count, v); |
| 355 | } |
Geoff Lang | f9a6f08 | 2015-01-22 13:32:49 -0500 | [diff] [blame] | 356 | } |
| 357 | |
| 358 | void ProgramGL::setUniform3uiv(GLint location, GLsizei count, const GLuint *v) |
| 359 | { |
Jiajia Qin | ee9f08c | 2016-11-16 10:06:10 +0800 | [diff] [blame] | 360 | if (mFunctions->programUniform3uiv != nullptr) |
| 361 | { |
| 362 | mFunctions->programUniform3uiv(mProgramID, uniLoc(location), count, v); |
| 363 | } |
| 364 | else |
| 365 | { |
| 366 | mStateManager->useProgram(mProgramID); |
| 367 | mFunctions->uniform3uiv(uniLoc(location), count, v); |
| 368 | } |
Geoff Lang | f9a6f08 | 2015-01-22 13:32:49 -0500 | [diff] [blame] | 369 | } |
| 370 | |
| 371 | void ProgramGL::setUniform4uiv(GLint location, GLsizei count, const GLuint *v) |
| 372 | { |
Jiajia Qin | ee9f08c | 2016-11-16 10:06:10 +0800 | [diff] [blame] | 373 | if (mFunctions->programUniform4uiv != nullptr) |
| 374 | { |
| 375 | mFunctions->programUniform4uiv(mProgramID, uniLoc(location), count, v); |
| 376 | } |
| 377 | else |
| 378 | { |
| 379 | mStateManager->useProgram(mProgramID); |
| 380 | mFunctions->uniform4uiv(uniLoc(location), count, v); |
| 381 | } |
Geoff Lang | f9a6f08 | 2015-01-22 13:32:49 -0500 | [diff] [blame] | 382 | } |
| 383 | |
| 384 | void ProgramGL::setUniformMatrix2fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value) |
| 385 | { |
Jiajia Qin | ee9f08c | 2016-11-16 10:06:10 +0800 | [diff] [blame] | 386 | if (mFunctions->programUniformMatrix2fv != nullptr) |
| 387 | { |
| 388 | mFunctions->programUniformMatrix2fv(mProgramID, uniLoc(location), count, transpose, value); |
| 389 | } |
| 390 | else |
| 391 | { |
| 392 | mStateManager->useProgram(mProgramID); |
| 393 | mFunctions->uniformMatrix2fv(uniLoc(location), count, transpose, value); |
| 394 | } |
Geoff Lang | f9a6f08 | 2015-01-22 13:32:49 -0500 | [diff] [blame] | 395 | } |
| 396 | |
| 397 | void ProgramGL::setUniformMatrix3fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value) |
| 398 | { |
Jiajia Qin | ee9f08c | 2016-11-16 10:06:10 +0800 | [diff] [blame] | 399 | if (mFunctions->programUniformMatrix3fv != nullptr) |
| 400 | { |
| 401 | mFunctions->programUniformMatrix3fv(mProgramID, uniLoc(location), count, transpose, value); |
| 402 | } |
| 403 | else |
| 404 | { |
| 405 | mStateManager->useProgram(mProgramID); |
| 406 | mFunctions->uniformMatrix3fv(uniLoc(location), count, transpose, value); |
| 407 | } |
Geoff Lang | f9a6f08 | 2015-01-22 13:32:49 -0500 | [diff] [blame] | 408 | } |
| 409 | |
| 410 | void ProgramGL::setUniformMatrix4fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value) |
| 411 | { |
Jiajia Qin | ee9f08c | 2016-11-16 10:06:10 +0800 | [diff] [blame] | 412 | if (mFunctions->programUniformMatrix4fv != nullptr) |
| 413 | { |
| 414 | mFunctions->programUniformMatrix4fv(mProgramID, uniLoc(location), count, transpose, value); |
| 415 | } |
| 416 | else |
| 417 | { |
| 418 | mStateManager->useProgram(mProgramID); |
| 419 | mFunctions->uniformMatrix4fv(uniLoc(location), count, transpose, value); |
| 420 | } |
Geoff Lang | f9a6f08 | 2015-01-22 13:32:49 -0500 | [diff] [blame] | 421 | } |
| 422 | |
| 423 | void ProgramGL::setUniformMatrix2x3fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value) |
| 424 | { |
Jiajia Qin | ee9f08c | 2016-11-16 10:06:10 +0800 | [diff] [blame] | 425 | if (mFunctions->programUniformMatrix2x3fv != nullptr) |
| 426 | { |
| 427 | mFunctions->programUniformMatrix2x3fv(mProgramID, uniLoc(location), count, transpose, |
| 428 | value); |
| 429 | } |
| 430 | else |
| 431 | { |
| 432 | mStateManager->useProgram(mProgramID); |
| 433 | mFunctions->uniformMatrix2x3fv(uniLoc(location), count, transpose, value); |
| 434 | } |
Geoff Lang | f9a6f08 | 2015-01-22 13:32:49 -0500 | [diff] [blame] | 435 | } |
| 436 | |
| 437 | void ProgramGL::setUniformMatrix3x2fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value) |
| 438 | { |
Jiajia Qin | ee9f08c | 2016-11-16 10:06:10 +0800 | [diff] [blame] | 439 | if (mFunctions->programUniformMatrix3x2fv != nullptr) |
| 440 | { |
| 441 | mFunctions->programUniformMatrix3x2fv(mProgramID, uniLoc(location), count, transpose, |
| 442 | value); |
| 443 | } |
| 444 | else |
| 445 | { |
| 446 | mStateManager->useProgram(mProgramID); |
| 447 | mFunctions->uniformMatrix3x2fv(uniLoc(location), count, transpose, value); |
| 448 | } |
Geoff Lang | f9a6f08 | 2015-01-22 13:32:49 -0500 | [diff] [blame] | 449 | } |
| 450 | |
| 451 | void ProgramGL::setUniformMatrix2x4fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value) |
| 452 | { |
Jiajia Qin | ee9f08c | 2016-11-16 10:06:10 +0800 | [diff] [blame] | 453 | if (mFunctions->programUniformMatrix2x4fv != nullptr) |
| 454 | { |
| 455 | mFunctions->programUniformMatrix2x4fv(mProgramID, uniLoc(location), count, transpose, |
| 456 | value); |
| 457 | } |
| 458 | else |
| 459 | { |
| 460 | mStateManager->useProgram(mProgramID); |
| 461 | mFunctions->uniformMatrix2x4fv(uniLoc(location), count, transpose, value); |
| 462 | } |
Geoff Lang | f9a6f08 | 2015-01-22 13:32:49 -0500 | [diff] [blame] | 463 | } |
| 464 | |
| 465 | void ProgramGL::setUniformMatrix4x2fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value) |
| 466 | { |
Jiajia Qin | ee9f08c | 2016-11-16 10:06:10 +0800 | [diff] [blame] | 467 | if (mFunctions->programUniformMatrix4x2fv != nullptr) |
| 468 | { |
| 469 | mFunctions->programUniformMatrix4x2fv(mProgramID, uniLoc(location), count, transpose, |
| 470 | value); |
| 471 | } |
| 472 | else |
| 473 | { |
| 474 | mStateManager->useProgram(mProgramID); |
| 475 | mFunctions->uniformMatrix4x2fv(uniLoc(location), count, transpose, value); |
| 476 | } |
Geoff Lang | f9a6f08 | 2015-01-22 13:32:49 -0500 | [diff] [blame] | 477 | } |
| 478 | |
| 479 | void ProgramGL::setUniformMatrix3x4fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value) |
| 480 | { |
Jiajia Qin | ee9f08c | 2016-11-16 10:06:10 +0800 | [diff] [blame] | 481 | if (mFunctions->programUniformMatrix3x4fv != nullptr) |
| 482 | { |
| 483 | mFunctions->programUniformMatrix3x4fv(mProgramID, uniLoc(location), count, transpose, |
| 484 | value); |
| 485 | } |
| 486 | else |
| 487 | { |
| 488 | mStateManager->useProgram(mProgramID); |
| 489 | mFunctions->uniformMatrix3x4fv(uniLoc(location), count, transpose, value); |
| 490 | } |
Geoff Lang | f9a6f08 | 2015-01-22 13:32:49 -0500 | [diff] [blame] | 491 | } |
| 492 | |
| 493 | void ProgramGL::setUniformMatrix4x3fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *value) |
| 494 | { |
Jiajia Qin | ee9f08c | 2016-11-16 10:06:10 +0800 | [diff] [blame] | 495 | if (mFunctions->programUniformMatrix4x3fv != nullptr) |
| 496 | { |
| 497 | mFunctions->programUniformMatrix4x3fv(mProgramID, uniLoc(location), count, transpose, |
| 498 | value); |
| 499 | } |
| 500 | else |
| 501 | { |
| 502 | mStateManager->useProgram(mProgramID); |
| 503 | mFunctions->uniformMatrix4x3fv(uniLoc(location), count, transpose, value); |
| 504 | } |
Geoff Lang | f9a6f08 | 2015-01-22 13:32:49 -0500 | [diff] [blame] | 505 | } |
| 506 | |
Geoff Lang | 5d124a6 | 2015-09-15 13:03:27 -0400 | [diff] [blame] | 507 | void ProgramGL::setUniformBlockBinding(GLuint uniformBlockIndex, GLuint uniformBlockBinding) |
| 508 | { |
Jamie Madill | 4a3c234 | 2015-10-08 12:58:45 -0400 | [diff] [blame] | 509 | // Lazy init |
| 510 | if (mUniformBlockRealLocationMap.empty()) |
| 511 | { |
Jamie Madill | 48ef11b | 2016-04-27 15:21:52 -0400 | [diff] [blame] | 512 | mUniformBlockRealLocationMap.reserve(mState.getUniformBlocks().size()); |
Jiajia Qin | 729b2c6 | 2017-08-14 09:36:11 +0800 | [diff] [blame] | 513 | for (const gl::InterfaceBlock &uniformBlock : mState.getUniformBlocks()) |
Jamie Madill | 4a3c234 | 2015-10-08 12:58:45 -0400 | [diff] [blame] | 514 | { |
Olli Etuaho | 855d964 | 2017-05-17 14:05:06 +0300 | [diff] [blame] | 515 | const std::string &mappedNameWithIndex = uniformBlock.mappedNameWithArrayIndex(); |
| 516 | GLuint blockIndex = |
| 517 | mFunctions->getUniformBlockIndex(mProgramID, mappedNameWithIndex.c_str()); |
Jamie Madill | 4a3c234 | 2015-10-08 12:58:45 -0400 | [diff] [blame] | 518 | mUniformBlockRealLocationMap.push_back(blockIndex); |
| 519 | } |
| 520 | } |
| 521 | |
| 522 | GLuint realBlockIndex = mUniformBlockRealLocationMap[uniformBlockIndex]; |
| 523 | if (realBlockIndex != GL_INVALID_INDEX) |
| 524 | { |
| 525 | mFunctions->uniformBlockBinding(mProgramID, realBlockIndex, uniformBlockBinding); |
| 526 | } |
Geoff Lang | 5d124a6 | 2015-09-15 13:03:27 -0400 | [diff] [blame] | 527 | } |
| 528 | |
Geoff Lang | b1f435e | 2015-02-20 10:01:01 -0500 | [diff] [blame] | 529 | GLuint ProgramGL::getProgramID() const |
| 530 | { |
| 531 | return mProgramID; |
| 532 | } |
| 533 | |
Olli Etuaho | 855d964 | 2017-05-17 14:05:06 +0300 | [diff] [blame] | 534 | bool ProgramGL::getUniformBlockSize(const std::string & /* blockName */, |
| 535 | const std::string &blockMappedName, |
| 536 | size_t *sizeOut) const |
Jamie Madill | 62d31cb | 2015-09-11 13:25:51 -0400 | [diff] [blame] | 537 | { |
Jamie Madill | 4a3c234 | 2015-10-08 12:58:45 -0400 | [diff] [blame] | 538 | ASSERT(mProgramID != 0u); |
Geoff Lang | 5d124a6 | 2015-09-15 13:03:27 -0400 | [diff] [blame] | 539 | |
Olli Etuaho | 855d964 | 2017-05-17 14:05:06 +0300 | [diff] [blame] | 540 | GLuint blockIndex = mFunctions->getUniformBlockIndex(mProgramID, blockMappedName.c_str()); |
Jamie Madill | 4a3c234 | 2015-10-08 12:58:45 -0400 | [diff] [blame] | 541 | if (blockIndex == GL_INVALID_INDEX) |
Geoff Lang | 5d124a6 | 2015-09-15 13:03:27 -0400 | [diff] [blame] | 542 | { |
Jamie Madill | 4a3c234 | 2015-10-08 12:58:45 -0400 | [diff] [blame] | 543 | *sizeOut = 0; |
| 544 | return false; |
Geoff Lang | 5d124a6 | 2015-09-15 13:03:27 -0400 | [diff] [blame] | 545 | } |
| 546 | |
Jamie Madill | 4a3c234 | 2015-10-08 12:58:45 -0400 | [diff] [blame] | 547 | GLint dataSize = 0; |
| 548 | mFunctions->getActiveUniformBlockiv(mProgramID, blockIndex, GL_UNIFORM_BLOCK_DATA_SIZE, |
| 549 | &dataSize); |
| 550 | *sizeOut = static_cast<size_t>(dataSize); |
| 551 | return true; |
| 552 | } |
| 553 | |
Olli Etuaho | 855d964 | 2017-05-17 14:05:06 +0300 | [diff] [blame] | 554 | bool ProgramGL::getUniformBlockMemberInfo(const std::string & /* memberUniformName */, |
| 555 | const std::string &memberUniformMappedName, |
Jamie Madill | 4a3c234 | 2015-10-08 12:58:45 -0400 | [diff] [blame] | 556 | sh::BlockMemberInfo *memberInfoOut) const |
| 557 | { |
| 558 | GLuint uniformIndex; |
Olli Etuaho | 855d964 | 2017-05-17 14:05:06 +0300 | [diff] [blame] | 559 | const GLchar *memberNameGLStr = memberUniformMappedName.c_str(); |
Jamie Madill | 4a3c234 | 2015-10-08 12:58:45 -0400 | [diff] [blame] | 560 | mFunctions->getUniformIndices(mProgramID, 1, &memberNameGLStr, &uniformIndex); |
| 561 | |
| 562 | if (uniformIndex == GL_INVALID_INDEX) |
Geoff Lang | 5d124a6 | 2015-09-15 13:03:27 -0400 | [diff] [blame] | 563 | { |
Jamie Madill | 4a3c234 | 2015-10-08 12:58:45 -0400 | [diff] [blame] | 564 | *memberInfoOut = sh::BlockMemberInfo::getDefaultBlockInfo(); |
| 565 | return false; |
Geoff Lang | 5d124a6 | 2015-09-15 13:03:27 -0400 | [diff] [blame] | 566 | } |
Jamie Madill | 4a3c234 | 2015-10-08 12:58:45 -0400 | [diff] [blame] | 567 | |
| 568 | mFunctions->getActiveUniformsiv(mProgramID, 1, &uniformIndex, GL_UNIFORM_OFFSET, |
| 569 | &memberInfoOut->offset); |
| 570 | mFunctions->getActiveUniformsiv(mProgramID, 1, &uniformIndex, GL_UNIFORM_ARRAY_STRIDE, |
| 571 | &memberInfoOut->arrayStride); |
| 572 | mFunctions->getActiveUniformsiv(mProgramID, 1, &uniformIndex, GL_UNIFORM_MATRIX_STRIDE, |
| 573 | &memberInfoOut->matrixStride); |
| 574 | |
| 575 | // TODO(jmadill): possibly determine this at the gl::Program level. |
| 576 | GLint isRowMajorMatrix = 0; |
| 577 | mFunctions->getActiveUniformsiv(mProgramID, 1, &uniformIndex, GL_UNIFORM_IS_ROW_MAJOR, |
| 578 | &isRowMajorMatrix); |
| 579 | memberInfoOut->isRowMajorMatrix = isRowMajorMatrix != GL_FALSE; |
| 580 | return true; |
Jamie Madill | 62d31cb | 2015-09-11 13:25:51 -0400 | [diff] [blame] | 581 | } |
Jamie Madill | 4a3c234 | 2015-10-08 12:58:45 -0400 | [diff] [blame] | 582 | |
Sami Väisänen | 46eaa94 | 2016-06-29 10:26:37 +0300 | [diff] [blame] | 583 | void ProgramGL::setPathFragmentInputGen(const std::string &inputName, |
| 584 | GLenum genMode, |
| 585 | GLint components, |
| 586 | const GLfloat *coeffs) |
| 587 | { |
| 588 | ASSERT(mEnablePathRendering); |
| 589 | |
| 590 | for (const auto &input : mPathRenderingFragmentInputs) |
| 591 | { |
Olli Etuaho | 855d964 | 2017-05-17 14:05:06 +0300 | [diff] [blame] | 592 | if (input.mappedName == inputName) |
Sami Väisänen | 46eaa94 | 2016-06-29 10:26:37 +0300 | [diff] [blame] | 593 | { |
| 594 | mFunctions->programPathFragmentInputGenNV(mProgramID, input.location, genMode, |
| 595 | components, coeffs); |
| 596 | ASSERT(mFunctions->getError() == GL_NO_ERROR); |
| 597 | return; |
| 598 | } |
| 599 | } |
| 600 | |
| 601 | } |
| 602 | |
Geoff Lang | 65a0be9 | 2015-10-02 09:57:30 -0400 | [diff] [blame] | 603 | void ProgramGL::preLink() |
| 604 | { |
| 605 | // Reset the program state |
| 606 | mUniformRealLocationMap.clear(); |
| 607 | mUniformBlockRealLocationMap.clear(); |
Sami Väisänen | 46eaa94 | 2016-06-29 10:26:37 +0300 | [diff] [blame] | 608 | mPathRenderingFragmentInputs.clear(); |
Martin Radev | 4e619f5 | 2017-08-09 11:50:06 +0300 | [diff] [blame] | 609 | |
| 610 | mMultiviewBaseViewLayerIndexUniformLocation = -1; |
Geoff Lang | 65a0be9 | 2015-10-02 09:57:30 -0400 | [diff] [blame] | 611 | } |
| 612 | |
| 613 | bool ProgramGL::checkLinkStatus(gl::InfoLog &infoLog) |
| 614 | { |
| 615 | GLint linkStatus = GL_FALSE; |
| 616 | mFunctions->getProgramiv(mProgramID, GL_LINK_STATUS, &linkStatus); |
| 617 | if (linkStatus == GL_FALSE) |
| 618 | { |
| 619 | // Linking failed, put the error into the info log |
| 620 | GLint infoLogLength = 0; |
| 621 | mFunctions->getProgramiv(mProgramID, GL_INFO_LOG_LENGTH, &infoLogLength); |
| 622 | |
Geoff Lang | 65a0be9 | 2015-10-02 09:57:30 -0400 | [diff] [blame] | 623 | // Info log length includes the null terminator, so 1 means that the info log is an empty |
| 624 | // string. |
| 625 | if (infoLogLength > 1) |
| 626 | { |
| 627 | std::vector<char> buf(infoLogLength); |
| 628 | mFunctions->getProgramInfoLog(mProgramID, infoLogLength, nullptr, &buf[0]); |
| 629 | |
| 630 | mFunctions->deleteProgram(mProgramID); |
| 631 | mProgramID = 0; |
| 632 | |
| 633 | infoLog << buf.data(); |
| 634 | |
Yuly Novikov | bcb3f9b | 2017-01-27 22:45:18 -0500 | [diff] [blame] | 635 | WARN() << "Program link failed unexpectedly: " << buf.data(); |
Geoff Lang | 65a0be9 | 2015-10-02 09:57:30 -0400 | [diff] [blame] | 636 | } |
| 637 | else |
| 638 | { |
Yuly Novikov | bcb3f9b | 2017-01-27 22:45:18 -0500 | [diff] [blame] | 639 | WARN() << "Program link failed unexpectedly with no info log."; |
Geoff Lang | 65a0be9 | 2015-10-02 09:57:30 -0400 | [diff] [blame] | 640 | } |
Geoff Lang | 65a0be9 | 2015-10-02 09:57:30 -0400 | [diff] [blame] | 641 | |
| 642 | // TODO, return GL_OUT_OF_MEMORY or just fail the link? This is an unexpected case |
| 643 | return false; |
| 644 | } |
| 645 | |
| 646 | return true; |
| 647 | } |
| 648 | |
| 649 | void ProgramGL::postLink() |
| 650 | { |
| 651 | // Query the uniform information |
| 652 | ASSERT(mUniformRealLocationMap.empty()); |
Jamie Madill | 48ef11b | 2016-04-27 15:21:52 -0400 | [diff] [blame] | 653 | const auto &uniformLocations = mState.getUniformLocations(); |
| 654 | const auto &uniforms = mState.getUniforms(); |
Geoff Lang | 65a0be9 | 2015-10-02 09:57:30 -0400 | [diff] [blame] | 655 | mUniformRealLocationMap.resize(uniformLocations.size(), GL_INVALID_INDEX); |
| 656 | for (size_t uniformLocation = 0; uniformLocation < uniformLocations.size(); uniformLocation++) |
| 657 | { |
| 658 | const auto &entry = uniformLocations[uniformLocation]; |
Jamie Madill | fb997ec | 2017-09-20 15:44:27 -0400 | [diff] [blame] | 659 | if (!entry.used()) |
Geoff Lang | 65a0be9 | 2015-10-02 09:57:30 -0400 | [diff] [blame] | 660 | { |
| 661 | continue; |
| 662 | } |
| 663 | |
Olli Etuaho | c853804 | 2017-09-27 11:20:15 +0300 | [diff] [blame] | 664 | // From the GLES 3.0.5 spec: |
Geoff Lang | 65a0be9 | 2015-10-02 09:57:30 -0400 | [diff] [blame] | 665 | // "Locations for sequential array indices are not required to be sequential." |
| 666 | const gl::LinkedUniform &uniform = uniforms[entry.index]; |
| 667 | std::stringstream fullNameStr; |
Geoff Lang | 65a0be9 | 2015-10-02 09:57:30 -0400 | [diff] [blame] | 668 | if (uniform.isArray()) |
| 669 | { |
Olli Etuaho | d255123 | 2017-10-26 20:03:33 +0300 | [diff] [blame] | 670 | ASSERT(angle::EndsWith(uniform.mappedName, "[0]")); |
| 671 | fullNameStr << uniform.mappedName.substr(0, uniform.mappedName.length() - 3); |
Olli Etuaho | 1734e17 | 2017-10-27 15:30:27 +0300 | [diff] [blame] | 672 | fullNameStr << "[" << entry.arrayIndex << "]"; |
Olli Etuaho | d255123 | 2017-10-26 20:03:33 +0300 | [diff] [blame] | 673 | } |
| 674 | else |
| 675 | { |
| 676 | fullNameStr << uniform.mappedName; |
Geoff Lang | 65a0be9 | 2015-10-02 09:57:30 -0400 | [diff] [blame] | 677 | } |
| 678 | const std::string &fullName = fullNameStr.str(); |
| 679 | |
| 680 | GLint realLocation = mFunctions->getUniformLocation(mProgramID, fullName.c_str()); |
| 681 | mUniformRealLocationMap[uniformLocation] = realLocation; |
| 682 | } |
| 683 | |
Martin Radev | 4e619f5 | 2017-08-09 11:50:06 +0300 | [diff] [blame] | 684 | if (mState.usesMultiview()) |
| 685 | { |
| 686 | mMultiviewBaseViewLayerIndexUniformLocation = |
Olli Etuaho | 855d964 | 2017-05-17 14:05:06 +0300 | [diff] [blame] | 687 | mFunctions->getUniformLocation(mProgramID, "multiviewBaseViewLayerIndex"); |
Martin Radev | 4e619f5 | 2017-08-09 11:50:06 +0300 | [diff] [blame] | 688 | ASSERT(mMultiviewBaseViewLayerIndexUniformLocation != -1); |
| 689 | } |
| 690 | |
Sami Väisänen | 46eaa94 | 2016-06-29 10:26:37 +0300 | [diff] [blame] | 691 | // Discover CHROMIUM_path_rendering fragment inputs if enabled. |
| 692 | if (!mEnablePathRendering) |
| 693 | return; |
| 694 | |
| 695 | GLint numFragmentInputs = 0; |
| 696 | mFunctions->getProgramInterfaceiv(mProgramID, GL_FRAGMENT_INPUT_NV, GL_ACTIVE_RESOURCES, |
| 697 | &numFragmentInputs); |
| 698 | if (numFragmentInputs <= 0) |
| 699 | return; |
| 700 | |
| 701 | GLint maxNameLength = 0; |
| 702 | mFunctions->getProgramInterfaceiv(mProgramID, GL_FRAGMENT_INPUT_NV, GL_MAX_NAME_LENGTH, |
| 703 | &maxNameLength); |
| 704 | ASSERT(maxNameLength); |
| 705 | |
| 706 | for (GLint i = 0; i < numFragmentInputs; ++i) |
| 707 | { |
Olli Etuaho | 855d964 | 2017-05-17 14:05:06 +0300 | [diff] [blame] | 708 | std::string mappedName; |
| 709 | mappedName.resize(maxNameLength); |
Sami Väisänen | 46eaa94 | 2016-06-29 10:26:37 +0300 | [diff] [blame] | 710 | |
| 711 | GLsizei nameLen = 0; |
| 712 | mFunctions->getProgramResourceName(mProgramID, GL_FRAGMENT_INPUT_NV, i, maxNameLength, |
Olli Etuaho | 855d964 | 2017-05-17 14:05:06 +0300 | [diff] [blame] | 713 | &nameLen, &mappedName[0]); |
| 714 | mappedName.resize(nameLen); |
Sami Väisänen | 46eaa94 | 2016-06-29 10:26:37 +0300 | [diff] [blame] | 715 | |
| 716 | // Ignore built-ins |
Olli Etuaho | 855d964 | 2017-05-17 14:05:06 +0300 | [diff] [blame] | 717 | if (angle::BeginsWith(mappedName, "gl_")) |
Sami Väisänen | 46eaa94 | 2016-06-29 10:26:37 +0300 | [diff] [blame] | 718 | continue; |
| 719 | |
| 720 | const GLenum kQueryProperties[] = {GL_LOCATION, GL_ARRAY_SIZE}; |
| 721 | GLint queryResults[ArraySize(kQueryProperties)]; |
| 722 | GLsizei queryLength = 0; |
| 723 | |
Geoff Lang | 3f6a398 | 2016-07-15 15:20:45 -0400 | [diff] [blame] | 724 | mFunctions->getProgramResourceiv( |
| 725 | mProgramID, GL_FRAGMENT_INPUT_NV, i, static_cast<GLsizei>(ArraySize(kQueryProperties)), |
| 726 | kQueryProperties, static_cast<GLsizei>(ArraySize(queryResults)), &queryLength, |
| 727 | queryResults); |
Sami Väisänen | 46eaa94 | 2016-06-29 10:26:37 +0300 | [diff] [blame] | 728 | |
Olli Etuaho | e319171 | 2016-07-18 16:01:10 +0300 | [diff] [blame] | 729 | ASSERT(queryLength == static_cast<GLsizei>(ArraySize(kQueryProperties))); |
Sami Väisänen | 46eaa94 | 2016-06-29 10:26:37 +0300 | [diff] [blame] | 730 | |
Geoff Lang | 3f6a398 | 2016-07-15 15:20:45 -0400 | [diff] [blame] | 731 | PathRenderingFragmentInput baseElementInput; |
Olli Etuaho | 855d964 | 2017-05-17 14:05:06 +0300 | [diff] [blame] | 732 | baseElementInput.mappedName = mappedName; |
Geoff Lang | 3f6a398 | 2016-07-15 15:20:45 -0400 | [diff] [blame] | 733 | baseElementInput.location = queryResults[0]; |
| 734 | mPathRenderingFragmentInputs.push_back(std::move(baseElementInput)); |
Sami Väisänen | 46eaa94 | 2016-06-29 10:26:37 +0300 | [diff] [blame] | 735 | |
| 736 | // If the input is an array it's denoted by [0] suffix on the variable |
| 737 | // name. We'll then create an entry per each array index where index > 0 |
Olli Etuaho | 855d964 | 2017-05-17 14:05:06 +0300 | [diff] [blame] | 738 | if (angle::EndsWith(mappedName, "[0]")) |
Sami Väisänen | 46eaa94 | 2016-06-29 10:26:37 +0300 | [diff] [blame] | 739 | { |
| 740 | // drop the suffix |
Olli Etuaho | 855d964 | 2017-05-17 14:05:06 +0300 | [diff] [blame] | 741 | mappedName.resize(mappedName.size() - 3); |
Sami Väisänen | 46eaa94 | 2016-06-29 10:26:37 +0300 | [diff] [blame] | 742 | |
| 743 | const auto arraySize = queryResults[1]; |
| 744 | const auto baseLocation = queryResults[0]; |
| 745 | |
Geoff Lang | 3f6a398 | 2016-07-15 15:20:45 -0400 | [diff] [blame] | 746 | for (GLint arrayIndex = 1; arrayIndex < arraySize; ++arrayIndex) |
Sami Väisänen | 46eaa94 | 2016-06-29 10:26:37 +0300 | [diff] [blame] | 747 | { |
Geoff Lang | 3f6a398 | 2016-07-15 15:20:45 -0400 | [diff] [blame] | 748 | PathRenderingFragmentInput arrayElementInput; |
Olli Etuaho | 855d964 | 2017-05-17 14:05:06 +0300 | [diff] [blame] | 749 | arrayElementInput.mappedName = mappedName + "[" + ToString(arrayIndex) + "]"; |
Geoff Lang | 3f6a398 | 2016-07-15 15:20:45 -0400 | [diff] [blame] | 750 | arrayElementInput.location = baseLocation + arrayIndex; |
| 751 | mPathRenderingFragmentInputs.push_back(std::move(arrayElementInput)); |
Sami Väisänen | 46eaa94 | 2016-06-29 10:26:37 +0300 | [diff] [blame] | 752 | } |
| 753 | } |
| 754 | } |
Geoff Lang | 65a0be9 | 2015-10-02 09:57:30 -0400 | [diff] [blame] | 755 | } |
| 756 | |
Martin Radev | 4e619f5 | 2017-08-09 11:50:06 +0300 | [diff] [blame] | 757 | void ProgramGL::enableSideBySideRenderingPath() const |
| 758 | { |
| 759 | ASSERT(mState.usesMultiview()); |
| 760 | ASSERT(mMultiviewBaseViewLayerIndexUniformLocation != -1); |
| 761 | |
| 762 | ASSERT(mFunctions->programUniform1i != nullptr); |
| 763 | mFunctions->programUniform1i(mProgramID, mMultiviewBaseViewLayerIndexUniformLocation, -1); |
| 764 | } |
| 765 | |
| 766 | void ProgramGL::enableLayeredRenderingPath(int baseViewIndex) const |
| 767 | { |
| 768 | ASSERT(mState.usesMultiview()); |
| 769 | ASSERT(mMultiviewBaseViewLayerIndexUniformLocation != -1); |
| 770 | |
| 771 | ASSERT(mFunctions->programUniform1i != nullptr); |
| 772 | mFunctions->programUniform1i(mProgramID, mMultiviewBaseViewLayerIndexUniformLocation, |
| 773 | baseViewIndex); |
| 774 | } |
| 775 | |
Jamie Madill | 54164b0 | 2017-08-28 15:17:37 -0400 | [diff] [blame] | 776 | void ProgramGL::getUniformfv(const gl::Context *context, GLint location, GLfloat *params) const |
| 777 | { |
| 778 | mFunctions->getUniformfv(mProgramID, uniLoc(location), params); |
| 779 | } |
| 780 | |
| 781 | void ProgramGL::getUniformiv(const gl::Context *context, GLint location, GLint *params) const |
| 782 | { |
| 783 | mFunctions->getUniformiv(mProgramID, uniLoc(location), params); |
| 784 | } |
| 785 | |
| 786 | void ProgramGL::getUniformuiv(const gl::Context *context, GLint location, GLuint *params) const |
| 787 | { |
| 788 | mFunctions->getUniformuiv(mProgramID, uniLoc(location), params); |
| 789 | } |
| 790 | |
Jamie Madill | fb997ec | 2017-09-20 15:44:27 -0400 | [diff] [blame] | 791 | void ProgramGL::markUnusedUniformLocations(std::vector<gl::VariableLocation> *uniformLocations, |
| 792 | std::vector<gl::SamplerBinding> *samplerBindings) |
Jamie Madill | 54164b0 | 2017-08-28 15:17:37 -0400 | [diff] [blame] | 793 | { |
| 794 | GLint maxLocation = static_cast<GLint>(uniformLocations->size()); |
| 795 | for (GLint location = 0; location < maxLocation; ++location) |
| 796 | { |
| 797 | if (uniLoc(location) == -1) |
| 798 | { |
Jamie Madill | fb997ec | 2017-09-20 15:44:27 -0400 | [diff] [blame] | 799 | auto &locationRef = (*uniformLocations)[location]; |
| 800 | if (mState.isSamplerUniformIndex(locationRef.index)) |
| 801 | { |
| 802 | GLuint samplerIndex = mState.getSamplerIndexFromUniformIndex(locationRef.index); |
| 803 | (*samplerBindings)[samplerIndex].unreferenced = true; |
| 804 | } |
| 805 | locationRef.markUnused(); |
Jamie Madill | 54164b0 | 2017-08-28 15:17:37 -0400 | [diff] [blame] | 806 | } |
| 807 | } |
| 808 | } |
| 809 | |
Jamie Madill | 6db1c2e | 2017-11-08 09:17:40 -0500 | [diff] [blame] | 810 | void ProgramGL::linkResources(const gl::ProgramLinkedResources &resources) |
| 811 | { |
| 812 | // Gather interface block info. |
| 813 | auto getUniformBlockSize = [this](const std::string &name, const std::string &mappedName, |
| 814 | size_t *sizeOut) { |
| 815 | return this->getUniformBlockSize(name, mappedName, sizeOut); |
| 816 | }; |
| 817 | |
| 818 | auto getUniformBlockMemberInfo = [this](const std::string &name, const std::string &mappedName, |
| 819 | sh::BlockMemberInfo *infoOut) { |
| 820 | return this->getUniformBlockMemberInfo(name, mappedName, infoOut); |
| 821 | }; |
| 822 | |
| 823 | resources.uniformBlockLinker.linkBlocks(getUniformBlockSize, getUniformBlockMemberInfo); |
| 824 | |
| 825 | // TODO(jiajia.qin@intel.com): Determine correct shader storage block info. |
| 826 | auto getShaderStorageBlockSize = [](const std::string &name, const std::string &mappedName, |
| 827 | size_t *sizeOut) { |
| 828 | *sizeOut = 0; |
| 829 | return true; |
| 830 | }; |
| 831 | |
| 832 | auto getShaderStorageBlockMemberInfo = |
| 833 | [](const std::string &name, const std::string &mappedName, sh::BlockMemberInfo *infoOut) { |
| 834 | *infoOut = sh::BlockMemberInfo::getDefaultBlockInfo(); |
| 835 | return true; |
| 836 | }; |
| 837 | resources.shaderStorageBlockLinker.linkBlocks(getShaderStorageBlockSize, |
| 838 | getShaderStorageBlockMemberInfo); |
| 839 | } |
| 840 | |
Jamie Madill | 4a3c234 | 2015-10-08 12:58:45 -0400 | [diff] [blame] | 841 | } // namespace rx |