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