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 | |
Geoff Lang | 416a23e | 2017-11-20 12:34:20 -0500 | [diff] [blame^] | 86 | std::vector<uint8_t> binary(std::max(binaryLength, 1)); |
Geoff Lang | 65a0be9 | 2015-10-02 09:57:30 -0400 | [diff] [blame] | 87 | GLenum binaryFormat = GL_NONE; |
| 88 | mFunctions->getProgramBinary(mProgramID, binaryLength, &binaryLength, &binaryFormat, |
Geoff Lang | 416a23e | 2017-11-20 12:34:20 -0500 | [diff] [blame^] | 89 | binary.data()); |
Geoff Lang | 65a0be9 | 2015-10-02 09:57:30 -0400 | [diff] [blame] | 90 | |
| 91 | stream->writeInt(binaryFormat); |
| 92 | stream->writeInt(binaryLength); |
Geoff Lang | 416a23e | 2017-11-20 12:34:20 -0500 | [diff] [blame^] | 93 | stream->writeBytes(binary.data(), 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 | |
Jiajia Qin | 3a9090f | 2017-09-27 14:37:04 +0800 | [diff] [blame] | 583 | bool ProgramGL::getShaderStorageBlockMemberInfo(const std::string & /* memberName */, |
| 584 | const std::string &memberUniformMappedName, |
| 585 | sh::BlockMemberInfo *memberInfoOut) const |
| 586 | { |
| 587 | const GLchar *memberNameGLStr = memberUniformMappedName.c_str(); |
| 588 | GLuint index = |
| 589 | mFunctions->getProgramResourceIndex(mProgramID, GL_BUFFER_VARIABLE, memberNameGLStr); |
| 590 | |
| 591 | if (index == GL_INVALID_INDEX) |
| 592 | { |
| 593 | *memberInfoOut = sh::BlockMemberInfo::getDefaultBlockInfo(); |
| 594 | return false; |
| 595 | } |
| 596 | |
| 597 | constexpr int kPropCount = 5; |
| 598 | std::array<GLenum, kPropCount> props = { |
| 599 | {GL_ARRAY_STRIDE, GL_IS_ROW_MAJOR, GL_MATRIX_STRIDE, GL_OFFSET, GL_TOP_LEVEL_ARRAY_STRIDE}}; |
| 600 | std::array<GLint, kPropCount> params; |
| 601 | GLsizei length; |
| 602 | mFunctions->getProgramResourceiv(mProgramID, GL_BUFFER_VARIABLE, index, kPropCount, |
| 603 | props.data(), kPropCount, &length, params.data()); |
| 604 | ASSERT(kPropCount == length); |
| 605 | memberInfoOut->arrayStride = params[0]; |
Olli Etuaho | c4eca92 | 2017-11-13 12:27:23 +0200 | [diff] [blame] | 606 | memberInfoOut->isRowMajorMatrix = params[1] != 0; |
Jiajia Qin | 3a9090f | 2017-09-27 14:37:04 +0800 | [diff] [blame] | 607 | memberInfoOut->matrixStride = params[2]; |
| 608 | memberInfoOut->offset = params[3]; |
| 609 | memberInfoOut->topLevelArrayStride = params[4]; |
| 610 | |
| 611 | return true; |
| 612 | } |
| 613 | |
| 614 | bool ProgramGL::getShaderStorageBlockSize(const std::string &name, |
| 615 | const std::string &mappedName, |
| 616 | size_t *sizeOut) const |
| 617 | { |
| 618 | const GLchar *nameGLStr = mappedName.c_str(); |
| 619 | GLuint index = |
| 620 | mFunctions->getProgramResourceIndex(mProgramID, GL_SHADER_STORAGE_BLOCK, nameGLStr); |
| 621 | |
| 622 | if (index == GL_INVALID_INDEX) |
| 623 | { |
| 624 | *sizeOut = 0; |
| 625 | return false; |
| 626 | } |
| 627 | |
| 628 | GLenum prop = GL_BUFFER_DATA_SIZE; |
| 629 | GLsizei length = 0; |
| 630 | GLint dataSize = 0; |
| 631 | mFunctions->getProgramResourceiv(mProgramID, GL_SHADER_STORAGE_BLOCK, index, 1, &prop, 1, |
| 632 | &length, &dataSize); |
| 633 | *sizeOut = static_cast<size_t>(dataSize); |
| 634 | return true; |
| 635 | } |
| 636 | |
Sami Väisänen | 46eaa94 | 2016-06-29 10:26:37 +0300 | [diff] [blame] | 637 | void ProgramGL::setPathFragmentInputGen(const std::string &inputName, |
| 638 | GLenum genMode, |
| 639 | GLint components, |
| 640 | const GLfloat *coeffs) |
| 641 | { |
| 642 | ASSERT(mEnablePathRendering); |
| 643 | |
| 644 | for (const auto &input : mPathRenderingFragmentInputs) |
| 645 | { |
Olli Etuaho | 855d964 | 2017-05-17 14:05:06 +0300 | [diff] [blame] | 646 | if (input.mappedName == inputName) |
Sami Väisänen | 46eaa94 | 2016-06-29 10:26:37 +0300 | [diff] [blame] | 647 | { |
| 648 | mFunctions->programPathFragmentInputGenNV(mProgramID, input.location, genMode, |
| 649 | components, coeffs); |
| 650 | ASSERT(mFunctions->getError() == GL_NO_ERROR); |
| 651 | return; |
| 652 | } |
| 653 | } |
| 654 | |
| 655 | } |
| 656 | |
Geoff Lang | 65a0be9 | 2015-10-02 09:57:30 -0400 | [diff] [blame] | 657 | void ProgramGL::preLink() |
| 658 | { |
| 659 | // Reset the program state |
| 660 | mUniformRealLocationMap.clear(); |
| 661 | mUniformBlockRealLocationMap.clear(); |
Sami Väisänen | 46eaa94 | 2016-06-29 10:26:37 +0300 | [diff] [blame] | 662 | mPathRenderingFragmentInputs.clear(); |
Martin Radev | 4e619f5 | 2017-08-09 11:50:06 +0300 | [diff] [blame] | 663 | |
| 664 | mMultiviewBaseViewLayerIndexUniformLocation = -1; |
Geoff Lang | 65a0be9 | 2015-10-02 09:57:30 -0400 | [diff] [blame] | 665 | } |
| 666 | |
| 667 | bool ProgramGL::checkLinkStatus(gl::InfoLog &infoLog) |
| 668 | { |
| 669 | GLint linkStatus = GL_FALSE; |
| 670 | mFunctions->getProgramiv(mProgramID, GL_LINK_STATUS, &linkStatus); |
| 671 | if (linkStatus == GL_FALSE) |
| 672 | { |
| 673 | // Linking failed, put the error into the info log |
| 674 | GLint infoLogLength = 0; |
| 675 | mFunctions->getProgramiv(mProgramID, GL_INFO_LOG_LENGTH, &infoLogLength); |
| 676 | |
Geoff Lang | 65a0be9 | 2015-10-02 09:57:30 -0400 | [diff] [blame] | 677 | // Info log length includes the null terminator, so 1 means that the info log is an empty |
| 678 | // string. |
| 679 | if (infoLogLength > 1) |
| 680 | { |
| 681 | std::vector<char> buf(infoLogLength); |
| 682 | mFunctions->getProgramInfoLog(mProgramID, infoLogLength, nullptr, &buf[0]); |
| 683 | |
| 684 | mFunctions->deleteProgram(mProgramID); |
| 685 | mProgramID = 0; |
| 686 | |
| 687 | infoLog << buf.data(); |
| 688 | |
Yuly Novikov | bcb3f9b | 2017-01-27 22:45:18 -0500 | [diff] [blame] | 689 | WARN() << "Program link failed unexpectedly: " << buf.data(); |
Geoff Lang | 65a0be9 | 2015-10-02 09:57:30 -0400 | [diff] [blame] | 690 | } |
| 691 | else |
| 692 | { |
Yuly Novikov | bcb3f9b | 2017-01-27 22:45:18 -0500 | [diff] [blame] | 693 | WARN() << "Program link failed unexpectedly with no info log."; |
Geoff Lang | 65a0be9 | 2015-10-02 09:57:30 -0400 | [diff] [blame] | 694 | } |
Geoff Lang | 65a0be9 | 2015-10-02 09:57:30 -0400 | [diff] [blame] | 695 | |
| 696 | // TODO, return GL_OUT_OF_MEMORY or just fail the link? This is an unexpected case |
| 697 | return false; |
| 698 | } |
| 699 | |
| 700 | return true; |
| 701 | } |
| 702 | |
| 703 | void ProgramGL::postLink() |
| 704 | { |
| 705 | // Query the uniform information |
| 706 | ASSERT(mUniformRealLocationMap.empty()); |
Jamie Madill | 48ef11b | 2016-04-27 15:21:52 -0400 | [diff] [blame] | 707 | const auto &uniformLocations = mState.getUniformLocations(); |
| 708 | const auto &uniforms = mState.getUniforms(); |
Geoff Lang | 65a0be9 | 2015-10-02 09:57:30 -0400 | [diff] [blame] | 709 | mUniformRealLocationMap.resize(uniformLocations.size(), GL_INVALID_INDEX); |
| 710 | for (size_t uniformLocation = 0; uniformLocation < uniformLocations.size(); uniformLocation++) |
| 711 | { |
| 712 | const auto &entry = uniformLocations[uniformLocation]; |
Jamie Madill | fb997ec | 2017-09-20 15:44:27 -0400 | [diff] [blame] | 713 | if (!entry.used()) |
Geoff Lang | 65a0be9 | 2015-10-02 09:57:30 -0400 | [diff] [blame] | 714 | { |
| 715 | continue; |
| 716 | } |
| 717 | |
Olli Etuaho | c853804 | 2017-09-27 11:20:15 +0300 | [diff] [blame] | 718 | // From the GLES 3.0.5 spec: |
Geoff Lang | 65a0be9 | 2015-10-02 09:57:30 -0400 | [diff] [blame] | 719 | // "Locations for sequential array indices are not required to be sequential." |
| 720 | const gl::LinkedUniform &uniform = uniforms[entry.index]; |
| 721 | std::stringstream fullNameStr; |
Geoff Lang | 65a0be9 | 2015-10-02 09:57:30 -0400 | [diff] [blame] | 722 | if (uniform.isArray()) |
| 723 | { |
Olli Etuaho | d255123 | 2017-10-26 20:03:33 +0300 | [diff] [blame] | 724 | ASSERT(angle::EndsWith(uniform.mappedName, "[0]")); |
| 725 | fullNameStr << uniform.mappedName.substr(0, uniform.mappedName.length() - 3); |
Olli Etuaho | 1734e17 | 2017-10-27 15:30:27 +0300 | [diff] [blame] | 726 | fullNameStr << "[" << entry.arrayIndex << "]"; |
Olli Etuaho | d255123 | 2017-10-26 20:03:33 +0300 | [diff] [blame] | 727 | } |
| 728 | else |
| 729 | { |
| 730 | fullNameStr << uniform.mappedName; |
Geoff Lang | 65a0be9 | 2015-10-02 09:57:30 -0400 | [diff] [blame] | 731 | } |
| 732 | const std::string &fullName = fullNameStr.str(); |
| 733 | |
| 734 | GLint realLocation = mFunctions->getUniformLocation(mProgramID, fullName.c_str()); |
| 735 | mUniformRealLocationMap[uniformLocation] = realLocation; |
| 736 | } |
| 737 | |
Martin Radev | 4e619f5 | 2017-08-09 11:50:06 +0300 | [diff] [blame] | 738 | if (mState.usesMultiview()) |
| 739 | { |
| 740 | mMultiviewBaseViewLayerIndexUniformLocation = |
Olli Etuaho | 855d964 | 2017-05-17 14:05:06 +0300 | [diff] [blame] | 741 | mFunctions->getUniformLocation(mProgramID, "multiviewBaseViewLayerIndex"); |
Martin Radev | 4e619f5 | 2017-08-09 11:50:06 +0300 | [diff] [blame] | 742 | ASSERT(mMultiviewBaseViewLayerIndexUniformLocation != -1); |
| 743 | } |
| 744 | |
Sami Väisänen | 46eaa94 | 2016-06-29 10:26:37 +0300 | [diff] [blame] | 745 | // Discover CHROMIUM_path_rendering fragment inputs if enabled. |
| 746 | if (!mEnablePathRendering) |
| 747 | return; |
| 748 | |
| 749 | GLint numFragmentInputs = 0; |
| 750 | mFunctions->getProgramInterfaceiv(mProgramID, GL_FRAGMENT_INPUT_NV, GL_ACTIVE_RESOURCES, |
| 751 | &numFragmentInputs); |
| 752 | if (numFragmentInputs <= 0) |
| 753 | return; |
| 754 | |
| 755 | GLint maxNameLength = 0; |
| 756 | mFunctions->getProgramInterfaceiv(mProgramID, GL_FRAGMENT_INPUT_NV, GL_MAX_NAME_LENGTH, |
| 757 | &maxNameLength); |
| 758 | ASSERT(maxNameLength); |
| 759 | |
| 760 | for (GLint i = 0; i < numFragmentInputs; ++i) |
| 761 | { |
Olli Etuaho | 855d964 | 2017-05-17 14:05:06 +0300 | [diff] [blame] | 762 | std::string mappedName; |
| 763 | mappedName.resize(maxNameLength); |
Sami Väisänen | 46eaa94 | 2016-06-29 10:26:37 +0300 | [diff] [blame] | 764 | |
| 765 | GLsizei nameLen = 0; |
| 766 | mFunctions->getProgramResourceName(mProgramID, GL_FRAGMENT_INPUT_NV, i, maxNameLength, |
Olli Etuaho | 855d964 | 2017-05-17 14:05:06 +0300 | [diff] [blame] | 767 | &nameLen, &mappedName[0]); |
| 768 | mappedName.resize(nameLen); |
Sami Väisänen | 46eaa94 | 2016-06-29 10:26:37 +0300 | [diff] [blame] | 769 | |
| 770 | // Ignore built-ins |
Olli Etuaho | 855d964 | 2017-05-17 14:05:06 +0300 | [diff] [blame] | 771 | if (angle::BeginsWith(mappedName, "gl_")) |
Sami Väisänen | 46eaa94 | 2016-06-29 10:26:37 +0300 | [diff] [blame] | 772 | continue; |
| 773 | |
| 774 | const GLenum kQueryProperties[] = {GL_LOCATION, GL_ARRAY_SIZE}; |
| 775 | GLint queryResults[ArraySize(kQueryProperties)]; |
| 776 | GLsizei queryLength = 0; |
| 777 | |
Geoff Lang | 3f6a398 | 2016-07-15 15:20:45 -0400 | [diff] [blame] | 778 | mFunctions->getProgramResourceiv( |
| 779 | mProgramID, GL_FRAGMENT_INPUT_NV, i, static_cast<GLsizei>(ArraySize(kQueryProperties)), |
| 780 | kQueryProperties, static_cast<GLsizei>(ArraySize(queryResults)), &queryLength, |
| 781 | queryResults); |
Sami Väisänen | 46eaa94 | 2016-06-29 10:26:37 +0300 | [diff] [blame] | 782 | |
Olli Etuaho | e319171 | 2016-07-18 16:01:10 +0300 | [diff] [blame] | 783 | ASSERT(queryLength == static_cast<GLsizei>(ArraySize(kQueryProperties))); |
Sami Väisänen | 46eaa94 | 2016-06-29 10:26:37 +0300 | [diff] [blame] | 784 | |
Geoff Lang | 3f6a398 | 2016-07-15 15:20:45 -0400 | [diff] [blame] | 785 | PathRenderingFragmentInput baseElementInput; |
Olli Etuaho | 855d964 | 2017-05-17 14:05:06 +0300 | [diff] [blame] | 786 | baseElementInput.mappedName = mappedName; |
Geoff Lang | 3f6a398 | 2016-07-15 15:20:45 -0400 | [diff] [blame] | 787 | baseElementInput.location = queryResults[0]; |
| 788 | mPathRenderingFragmentInputs.push_back(std::move(baseElementInput)); |
Sami Väisänen | 46eaa94 | 2016-06-29 10:26:37 +0300 | [diff] [blame] | 789 | |
| 790 | // If the input is an array it's denoted by [0] suffix on the variable |
| 791 | // 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] | 792 | if (angle::EndsWith(mappedName, "[0]")) |
Sami Väisänen | 46eaa94 | 2016-06-29 10:26:37 +0300 | [diff] [blame] | 793 | { |
| 794 | // drop the suffix |
Olli Etuaho | 855d964 | 2017-05-17 14:05:06 +0300 | [diff] [blame] | 795 | mappedName.resize(mappedName.size() - 3); |
Sami Väisänen | 46eaa94 | 2016-06-29 10:26:37 +0300 | [diff] [blame] | 796 | |
| 797 | const auto arraySize = queryResults[1]; |
| 798 | const auto baseLocation = queryResults[0]; |
| 799 | |
Geoff Lang | 3f6a398 | 2016-07-15 15:20:45 -0400 | [diff] [blame] | 800 | for (GLint arrayIndex = 1; arrayIndex < arraySize; ++arrayIndex) |
Sami Väisänen | 46eaa94 | 2016-06-29 10:26:37 +0300 | [diff] [blame] | 801 | { |
Geoff Lang | 3f6a398 | 2016-07-15 15:20:45 -0400 | [diff] [blame] | 802 | PathRenderingFragmentInput arrayElementInput; |
Olli Etuaho | 855d964 | 2017-05-17 14:05:06 +0300 | [diff] [blame] | 803 | arrayElementInput.mappedName = mappedName + "[" + ToString(arrayIndex) + "]"; |
Geoff Lang | 3f6a398 | 2016-07-15 15:20:45 -0400 | [diff] [blame] | 804 | arrayElementInput.location = baseLocation + arrayIndex; |
| 805 | mPathRenderingFragmentInputs.push_back(std::move(arrayElementInput)); |
Sami Väisänen | 46eaa94 | 2016-06-29 10:26:37 +0300 | [diff] [blame] | 806 | } |
| 807 | } |
| 808 | } |
Geoff Lang | 65a0be9 | 2015-10-02 09:57:30 -0400 | [diff] [blame] | 809 | } |
| 810 | |
Martin Radev | 4e619f5 | 2017-08-09 11:50:06 +0300 | [diff] [blame] | 811 | void ProgramGL::enableSideBySideRenderingPath() const |
| 812 | { |
| 813 | ASSERT(mState.usesMultiview()); |
| 814 | ASSERT(mMultiviewBaseViewLayerIndexUniformLocation != -1); |
| 815 | |
| 816 | ASSERT(mFunctions->programUniform1i != nullptr); |
| 817 | mFunctions->programUniform1i(mProgramID, mMultiviewBaseViewLayerIndexUniformLocation, -1); |
| 818 | } |
| 819 | |
| 820 | void ProgramGL::enableLayeredRenderingPath(int baseViewIndex) const |
| 821 | { |
| 822 | ASSERT(mState.usesMultiview()); |
| 823 | ASSERT(mMultiviewBaseViewLayerIndexUniformLocation != -1); |
| 824 | |
| 825 | ASSERT(mFunctions->programUniform1i != nullptr); |
| 826 | mFunctions->programUniform1i(mProgramID, mMultiviewBaseViewLayerIndexUniformLocation, |
| 827 | baseViewIndex); |
| 828 | } |
| 829 | |
Jamie Madill | 54164b0 | 2017-08-28 15:17:37 -0400 | [diff] [blame] | 830 | void ProgramGL::getUniformfv(const gl::Context *context, GLint location, GLfloat *params) const |
| 831 | { |
| 832 | mFunctions->getUniformfv(mProgramID, uniLoc(location), params); |
| 833 | } |
| 834 | |
| 835 | void ProgramGL::getUniformiv(const gl::Context *context, GLint location, GLint *params) const |
| 836 | { |
| 837 | mFunctions->getUniformiv(mProgramID, uniLoc(location), params); |
| 838 | } |
| 839 | |
| 840 | void ProgramGL::getUniformuiv(const gl::Context *context, GLint location, GLuint *params) const |
| 841 | { |
| 842 | mFunctions->getUniformuiv(mProgramID, uniLoc(location), params); |
| 843 | } |
| 844 | |
Jamie Madill | fb997ec | 2017-09-20 15:44:27 -0400 | [diff] [blame] | 845 | void ProgramGL::markUnusedUniformLocations(std::vector<gl::VariableLocation> *uniformLocations, |
| 846 | std::vector<gl::SamplerBinding> *samplerBindings) |
Jamie Madill | 54164b0 | 2017-08-28 15:17:37 -0400 | [diff] [blame] | 847 | { |
| 848 | GLint maxLocation = static_cast<GLint>(uniformLocations->size()); |
| 849 | for (GLint location = 0; location < maxLocation; ++location) |
| 850 | { |
| 851 | if (uniLoc(location) == -1) |
| 852 | { |
Jamie Madill | fb997ec | 2017-09-20 15:44:27 -0400 | [diff] [blame] | 853 | auto &locationRef = (*uniformLocations)[location]; |
| 854 | if (mState.isSamplerUniformIndex(locationRef.index)) |
| 855 | { |
| 856 | GLuint samplerIndex = mState.getSamplerIndexFromUniformIndex(locationRef.index); |
| 857 | (*samplerBindings)[samplerIndex].unreferenced = true; |
| 858 | } |
| 859 | locationRef.markUnused(); |
Jamie Madill | 54164b0 | 2017-08-28 15:17:37 -0400 | [diff] [blame] | 860 | } |
| 861 | } |
| 862 | } |
| 863 | |
Jamie Madill | 6db1c2e | 2017-11-08 09:17:40 -0500 | [diff] [blame] | 864 | void ProgramGL::linkResources(const gl::ProgramLinkedResources &resources) |
| 865 | { |
| 866 | // Gather interface block info. |
| 867 | auto getUniformBlockSize = [this](const std::string &name, const std::string &mappedName, |
| 868 | size_t *sizeOut) { |
| 869 | return this->getUniformBlockSize(name, mappedName, sizeOut); |
| 870 | }; |
| 871 | |
| 872 | auto getUniformBlockMemberInfo = [this](const std::string &name, const std::string &mappedName, |
| 873 | sh::BlockMemberInfo *infoOut) { |
| 874 | return this->getUniformBlockMemberInfo(name, mappedName, infoOut); |
| 875 | }; |
| 876 | |
| 877 | resources.uniformBlockLinker.linkBlocks(getUniformBlockSize, getUniformBlockMemberInfo); |
| 878 | |
Jiajia Qin | 3a9090f | 2017-09-27 14:37:04 +0800 | [diff] [blame] | 879 | auto getShaderStorageBlockSize = [this](const std::string &name, const std::string &mappedName, |
| 880 | size_t *sizeOut) { |
| 881 | return this->getShaderStorageBlockSize(name, mappedName, sizeOut); |
Jamie Madill | 6db1c2e | 2017-11-08 09:17:40 -0500 | [diff] [blame] | 882 | }; |
| 883 | |
Jiajia Qin | 3a9090f | 2017-09-27 14:37:04 +0800 | [diff] [blame] | 884 | auto getShaderStorageBlockMemberInfo = [this](const std::string &name, |
| 885 | const std::string &mappedName, |
| 886 | sh::BlockMemberInfo *infoOut) { |
| 887 | return this->getShaderStorageBlockMemberInfo(name, mappedName, infoOut); |
| 888 | }; |
Jamie Madill | 6db1c2e | 2017-11-08 09:17:40 -0500 | [diff] [blame] | 889 | resources.shaderStorageBlockLinker.linkBlocks(getShaderStorageBlockSize, |
| 890 | getShaderStorageBlockMemberInfo); |
| 891 | } |
| 892 | |
Jamie Madill | 4a3c234 | 2015-10-08 12:58:45 -0400 | [diff] [blame] | 893 | } // namespace rx |