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