daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1 | // |
Geoff Lang | 48dcae7 | 2014-02-05 16:28:24 -0500 | [diff] [blame] | 2 | // Copyright (c) 2002-2014 The ANGLE Project Authors. All rights reserved. |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 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 | // Program.cpp: Implements the gl::Program class. Implements GL program objects |
| 8 | // and related functionality. [OpenGL ES 2.0.24] section 2.10.3 page 28. |
| 9 | |
Geoff Lang | 2b5420c | 2014-11-19 14:20:15 -0500 | [diff] [blame] | 10 | #include "libANGLE/Program.h" |
Jamie Madill | 437d266 | 2014-12-05 14:23:35 -0500 | [diff] [blame] | 11 | |
Jamie Madill | 9e0478f | 2015-01-13 11:13:54 -0500 | [diff] [blame] | 12 | #include <algorithm> |
| 13 | |
| 14 | #include "common/debug.h" |
| 15 | #include "common/platform.h" |
| 16 | #include "common/utilities.h" |
| 17 | #include "common/version.h" |
| 18 | #include "compiler/translator/blocklayout.h" |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 19 | #include "libANGLE/Data.h" |
Jamie Madill | 437d266 | 2014-12-05 14:23:35 -0500 | [diff] [blame] | 20 | #include "libANGLE/ResourceManager.h" |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 21 | #include "libANGLE/features.h" |
Geoff Lang | 2b5420c | 2014-11-19 14:20:15 -0500 | [diff] [blame] | 22 | #include "libANGLE/renderer/Renderer.h" |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 23 | #include "libANGLE/renderer/ProgramImpl.h" |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 24 | |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 25 | namespace gl |
| 26 | { |
apatrick@chromium.org | 253b8d2 | 2012-06-22 19:27:21 +0000 | [diff] [blame] | 27 | const char * const g_fakepath = "C:\\fakepath"; |
| 28 | |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 29 | namespace |
| 30 | { |
| 31 | |
| 32 | unsigned int ParseAndStripArrayIndex(std::string* name) |
| 33 | { |
| 34 | unsigned int subscript = GL_INVALID_INDEX; |
| 35 | |
| 36 | // Strip any trailing array operator and retrieve the subscript |
| 37 | size_t open = name->find_last_of('['); |
| 38 | size_t close = name->find_last_of(']'); |
| 39 | if (open != std::string::npos && close == name->length() - 1) |
| 40 | { |
| 41 | subscript = atoi(name->substr(open + 1).c_str()); |
| 42 | name->erase(open); |
| 43 | } |
| 44 | |
| 45 | return subscript; |
| 46 | } |
| 47 | |
| 48 | } |
| 49 | |
apatrick@chromium.org | e2a59bb | 2012-06-07 21:09:53 +0000 | [diff] [blame] | 50 | AttributeBindings::AttributeBindings() |
| 51 | { |
| 52 | } |
| 53 | |
| 54 | AttributeBindings::~AttributeBindings() |
| 55 | { |
apatrick@chromium.org | 9a30b09 | 2012-06-06 20:21:55 +0000 | [diff] [blame] | 56 | } |
| 57 | |
Jamie Madill | 71c3b2c | 2015-05-07 11:49:20 -0400 | [diff] [blame] | 58 | InfoLog::InfoLog() |
apatrick@chromium.org | 253b8d2 | 2012-06-22 19:27:21 +0000 | [diff] [blame] | 59 | { |
| 60 | } |
| 61 | |
| 62 | InfoLog::~InfoLog() |
| 63 | { |
apatrick@chromium.org | 253b8d2 | 2012-06-22 19:27:21 +0000 | [diff] [blame] | 64 | } |
| 65 | |
Jamie Madill | 71c3b2c | 2015-05-07 11:49:20 -0400 | [diff] [blame] | 66 | size_t InfoLog::getLength() const |
apatrick@chromium.org | 253b8d2 | 2012-06-22 19:27:21 +0000 | [diff] [blame] | 67 | { |
Jamie Madill | 71c3b2c | 2015-05-07 11:49:20 -0400 | [diff] [blame] | 68 | const std::string &logString = mStream.str(); |
| 69 | return logString.empty() ? 0 : logString.length() + 1; |
apatrick@chromium.org | 253b8d2 | 2012-06-22 19:27:21 +0000 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | void InfoLog::getLog(GLsizei bufSize, GLsizei *length, char *infoLog) |
| 73 | { |
Jamie Madill | 71c3b2c | 2015-05-07 11:49:20 -0400 | [diff] [blame] | 74 | size_t index = 0; |
apatrick@chromium.org | 253b8d2 | 2012-06-22 19:27:21 +0000 | [diff] [blame] | 75 | |
| 76 | if (bufSize > 0) |
| 77 | { |
Jamie Madill | 71c3b2c | 2015-05-07 11:49:20 -0400 | [diff] [blame] | 78 | const std::string str(mStream.str()); |
| 79 | |
| 80 | if (!str.empty()) |
apatrick@chromium.org | 253b8d2 | 2012-06-22 19:27:21 +0000 | [diff] [blame] | 81 | { |
Jamie Madill | 71c3b2c | 2015-05-07 11:49:20 -0400 | [diff] [blame] | 82 | index = std::min(static_cast<size_t>(bufSize) - 1, str.length()); |
| 83 | memcpy(infoLog, str.c_str(), index); |
apatrick@chromium.org | 253b8d2 | 2012-06-22 19:27:21 +0000 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | infoLog[index] = '\0'; |
| 87 | } |
| 88 | |
| 89 | if (length) |
| 90 | { |
Jamie Madill | 71c3b2c | 2015-05-07 11:49:20 -0400 | [diff] [blame] | 91 | *length = static_cast<GLsizei>(index); |
apatrick@chromium.org | 253b8d2 | 2012-06-22 19:27:21 +0000 | [diff] [blame] | 92 | } |
| 93 | } |
| 94 | |
| 95 | // append a santized message to the program info log. |
| 96 | // The D3D compiler includes a fake file path in some of the warning or error |
| 97 | // messages, so lets remove all occurrences of this fake file path from the log. |
| 98 | void InfoLog::appendSanitized(const char *message) |
| 99 | { |
| 100 | std::string msg(message); |
| 101 | |
| 102 | size_t found; |
| 103 | do |
| 104 | { |
| 105 | found = msg.find(g_fakepath); |
| 106 | if (found != std::string::npos) |
| 107 | { |
| 108 | msg.erase(found, strlen(g_fakepath)); |
| 109 | } |
| 110 | } |
| 111 | while (found != std::string::npos); |
| 112 | |
Jamie Madill | 71c3b2c | 2015-05-07 11:49:20 -0400 | [diff] [blame] | 113 | mStream << message << std::endl; |
apatrick@chromium.org | 253b8d2 | 2012-06-22 19:27:21 +0000 | [diff] [blame] | 114 | } |
| 115 | |
apatrick@chromium.org | 253b8d2 | 2012-06-22 19:27:21 +0000 | [diff] [blame] | 116 | void InfoLog::reset() |
| 117 | { |
apatrick@chromium.org | 253b8d2 | 2012-06-22 19:27:21 +0000 | [diff] [blame] | 118 | } |
| 119 | |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 120 | VariableLocation::VariableLocation() |
Jamie Madill | 71c3b2c | 2015-05-07 11:49:20 -0400 | [diff] [blame] | 121 | : name(), |
| 122 | element(0), |
| 123 | index(0) |
apatrick@chromium.org | e2a59bb | 2012-06-07 21:09:53 +0000 | [diff] [blame] | 124 | { |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 125 | } |
| 126 | |
| 127 | VariableLocation::VariableLocation(const std::string &name, unsigned int element, unsigned int index) |
Jamie Madill | 71c3b2c | 2015-05-07 11:49:20 -0400 | [diff] [blame] | 128 | : name(name), |
| 129 | element(element), |
| 130 | index(index) |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 131 | { |
| 132 | } |
| 133 | |
| 134 | LinkedVarying::LinkedVarying() |
| 135 | { |
| 136 | } |
| 137 | |
| 138 | LinkedVarying::LinkedVarying(const std::string &name, GLenum type, GLsizei size, const std::string &semanticName, |
| 139 | unsigned int semanticIndex, unsigned int semanticIndexCount) |
| 140 | : name(name), type(type), size(size), semanticName(semanticName), semanticIndex(semanticIndex), semanticIndexCount(semanticIndexCount) |
| 141 | { |
| 142 | } |
| 143 | |
Jamie Madill | 5c6b7bf | 2015-08-17 12:53:35 -0400 | [diff] [blame^] | 144 | Program::Data::Data() |
| 145 | : mAttachedFragmentShader(nullptr), |
| 146 | mAttachedVertexShader(nullptr), |
Corentin Wallez | bc99bb6 | 2015-05-14 17:42:20 -0400 | [diff] [blame] | 147 | mTransformFeedbackVaryings(), |
Jamie Madill | 5c6b7bf | 2015-08-17 12:53:35 -0400 | [diff] [blame^] | 148 | mTransformFeedbackBufferMode(GL_NONE) |
| 149 | { |
| 150 | } |
| 151 | |
| 152 | Program::Data::~Data() |
| 153 | { |
| 154 | if (mAttachedVertexShader != nullptr) |
| 155 | { |
| 156 | mAttachedVertexShader->release(); |
| 157 | } |
| 158 | |
| 159 | if (mAttachedFragmentShader != nullptr) |
| 160 | { |
| 161 | mAttachedFragmentShader->release(); |
| 162 | } |
| 163 | } |
| 164 | |
| 165 | Program::Program(rx::ImplFactory *factory, ResourceManager *manager, GLuint handle) |
| 166 | : mProgram(factory->createProgram(mData)), |
| 167 | mLinkedAttributes(gl::MAX_VERTEX_ATTRIBS), |
| 168 | mValidated(false), |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 169 | mLinked(false), |
| 170 | mDeleteStatus(false), |
| 171 | mRefCount(0), |
| 172 | mResourceManager(manager), |
| 173 | mHandle(handle) |
| 174 | { |
| 175 | ASSERT(mProgram); |
shannonwoods@chromium.org | 70eb1ea | 2013-05-30 00:07:20 +0000 | [diff] [blame] | 176 | |
| 177 | resetUniformBlockBindings(); |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 178 | unlink(); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 179 | } |
| 180 | |
| 181 | Program::~Program() |
| 182 | { |
| 183 | unlink(true); |
daniel@transgaming.com | 71cd868 | 2010-04-29 03:35:25 +0000 | [diff] [blame] | 184 | |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 185 | SafeDelete(mProgram); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 186 | } |
| 187 | |
| 188 | bool Program::attachShader(Shader *shader) |
| 189 | { |
| 190 | if (shader->getType() == GL_VERTEX_SHADER) |
| 191 | { |
Jamie Madill | 5c6b7bf | 2015-08-17 12:53:35 -0400 | [diff] [blame^] | 192 | if (mData.mAttachedVertexShader) |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 193 | { |
| 194 | return false; |
| 195 | } |
| 196 | |
Jamie Madill | 5c6b7bf | 2015-08-17 12:53:35 -0400 | [diff] [blame^] | 197 | mData.mAttachedVertexShader = shader; |
| 198 | mData.mAttachedVertexShader->addRef(); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 199 | } |
| 200 | else if (shader->getType() == GL_FRAGMENT_SHADER) |
| 201 | { |
Jamie Madill | 5c6b7bf | 2015-08-17 12:53:35 -0400 | [diff] [blame^] | 202 | if (mData.mAttachedFragmentShader) |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 203 | { |
| 204 | return false; |
| 205 | } |
| 206 | |
Jamie Madill | 5c6b7bf | 2015-08-17 12:53:35 -0400 | [diff] [blame^] | 207 | mData.mAttachedFragmentShader = shader; |
| 208 | mData.mAttachedFragmentShader->addRef(); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 209 | } |
| 210 | else UNREACHABLE(); |
| 211 | |
| 212 | return true; |
| 213 | } |
| 214 | |
| 215 | bool Program::detachShader(Shader *shader) |
| 216 | { |
| 217 | if (shader->getType() == GL_VERTEX_SHADER) |
| 218 | { |
Jamie Madill | 5c6b7bf | 2015-08-17 12:53:35 -0400 | [diff] [blame^] | 219 | if (mData.mAttachedVertexShader != shader) |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 220 | { |
| 221 | return false; |
| 222 | } |
| 223 | |
Jamie Madill | 5c6b7bf | 2015-08-17 12:53:35 -0400 | [diff] [blame^] | 224 | shader->release(); |
| 225 | mData.mAttachedVertexShader = nullptr; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 226 | } |
| 227 | else if (shader->getType() == GL_FRAGMENT_SHADER) |
| 228 | { |
Jamie Madill | 5c6b7bf | 2015-08-17 12:53:35 -0400 | [diff] [blame^] | 229 | if (mData.mAttachedFragmentShader != shader) |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 230 | { |
| 231 | return false; |
| 232 | } |
| 233 | |
Jamie Madill | 5c6b7bf | 2015-08-17 12:53:35 -0400 | [diff] [blame^] | 234 | shader->release(); |
| 235 | mData.mAttachedFragmentShader = nullptr; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 236 | } |
| 237 | else UNREACHABLE(); |
| 238 | |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 239 | return true; |
| 240 | } |
| 241 | |
daniel@transgaming.com | cba5057 | 2010-03-28 19:36:09 +0000 | [diff] [blame] | 242 | int Program::getAttachedShadersCount() const |
| 243 | { |
Jamie Madill | 5c6b7bf | 2015-08-17 12:53:35 -0400 | [diff] [blame^] | 244 | return (mData.mAttachedVertexShader ? 1 : 0) + (mData.mAttachedFragmentShader ? 1 : 0); |
daniel@transgaming.com | cba5057 | 2010-03-28 19:36:09 +0000 | [diff] [blame] | 245 | } |
| 246 | |
apatrick@chromium.org | e2a59bb | 2012-06-07 21:09:53 +0000 | [diff] [blame] | 247 | void AttributeBindings::bindAttributeLocation(GLuint index, const char *name) |
| 248 | { |
| 249 | if (index < MAX_VERTEX_ATTRIBS) |
| 250 | { |
| 251 | for (int i = 0; i < MAX_VERTEX_ATTRIBS; i++) |
| 252 | { |
| 253 | mAttributeBinding[i].erase(name); |
| 254 | } |
| 255 | |
| 256 | mAttributeBinding[index].insert(name); |
| 257 | } |
apatrick@chromium.org | 9a30b09 | 2012-06-06 20:21:55 +0000 | [diff] [blame] | 258 | } |
| 259 | |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 260 | void Program::bindAttributeLocation(GLuint index, const char *name) |
| 261 | { |
apatrick@chromium.org | 9a30b09 | 2012-06-06 20:21:55 +0000 | [diff] [blame] | 262 | mAttributeBindings.bindAttributeLocation(index, name); |
Geoff Lang | 0ca5378 | 2015-05-07 13:49:39 -0400 | [diff] [blame] | 263 | mProgram->bindAttributeLocation(index, name); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 264 | } |
| 265 | |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 266 | // Links the HLSL code of the vertex and pixel shader by matching up their varyings, |
| 267 | // compiling them into binaries, determining the attribute mappings, and collecting |
| 268 | // a list of uniforms |
Jamie Madill | 5c6b7bf | 2015-08-17 12:53:35 -0400 | [diff] [blame^] | 269 | Error Program::link(const gl::Data &data) |
apatrick@chromium.org | e2a59bb | 2012-06-07 21:09:53 +0000 | [diff] [blame] | 270 | { |
| 271 | unlink(false); |
| 272 | |
apatrick@chromium.org | 253b8d2 | 2012-06-22 19:27:21 +0000 | [diff] [blame] | 273 | mInfoLog.reset(); |
shannonwoods@chromium.org | 70eb1ea | 2013-05-30 00:07:20 +0000 | [diff] [blame] | 274 | resetUniformBlockBindings(); |
apatrick@chromium.org | 253b8d2 | 2012-06-22 19:27:21 +0000 | [diff] [blame] | 275 | |
Jamie Madill | 5c6b7bf | 2015-08-17 12:53:35 -0400 | [diff] [blame^] | 276 | if (!mData.mAttachedFragmentShader || !mData.mAttachedFragmentShader->isCompiled()) |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 277 | { |
| 278 | return Error(GL_NO_ERROR); |
| 279 | } |
Jamie Madill | 5c6b7bf | 2015-08-17 12:53:35 -0400 | [diff] [blame^] | 280 | ASSERT(mData.mAttachedFragmentShader->getType() == GL_FRAGMENT_SHADER); |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 281 | |
Jamie Madill | 5c6b7bf | 2015-08-17 12:53:35 -0400 | [diff] [blame^] | 282 | if (!mData.mAttachedVertexShader || !mData.mAttachedVertexShader->isCompiled()) |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 283 | { |
| 284 | return Error(GL_NO_ERROR); |
| 285 | } |
Jamie Madill | 5c6b7bf | 2015-08-17 12:53:35 -0400 | [diff] [blame^] | 286 | ASSERT(mData.mAttachedVertexShader->getType() == GL_VERTEX_SHADER); |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 287 | |
Jamie Madill | 5c6b7bf | 2015-08-17 12:53:35 -0400 | [diff] [blame^] | 288 | if (!linkAttributes(data, mInfoLog, mAttributeBindings, mData.mAttachedVertexShader)) |
Jamie Madill | 437d266 | 2014-12-05 14:23:35 -0500 | [diff] [blame] | 289 | { |
| 290 | return Error(GL_NO_ERROR); |
| 291 | } |
| 292 | |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 293 | int registers; |
| 294 | std::vector<LinkedVarying> linkedVaryings; |
Jamie Madill | 5c6b7bf | 2015-08-17 12:53:35 -0400 | [diff] [blame^] | 295 | rx::LinkResult result = |
| 296 | mProgram->link(data, mInfoLog, mData.mAttachedFragmentShader, mData.mAttachedVertexShader, |
| 297 | mData.mTransformFeedbackVaryings, mData.mTransformFeedbackBufferMode, |
| 298 | ®isters, &linkedVaryings, &mOutputVariables); |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 299 | if (result.error.isError() || !result.linkSuccess) |
Geoff Lang | b543aff | 2014-09-30 14:52:54 -0400 | [diff] [blame] | 300 | { |
| 301 | return result.error; |
| 302 | } |
daniel@transgaming.com | 4c962bf | 2012-07-24 18:37:02 +0000 | [diff] [blame] | 303 | |
Jamie Madill | 5c6b7bf | 2015-08-17 12:53:35 -0400 | [diff] [blame^] | 304 | if (!mProgram->linkUniforms(mInfoLog, *mData.mAttachedVertexShader, |
| 305 | *mData.mAttachedFragmentShader, *data.caps)) |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 306 | { |
| 307 | return Error(GL_NO_ERROR); |
| 308 | } |
| 309 | |
Jamie Madill | 5c6b7bf | 2015-08-17 12:53:35 -0400 | [diff] [blame^] | 310 | if (!linkUniformBlocks(mInfoLog, *mData.mAttachedVertexShader, *mData.mAttachedFragmentShader, |
| 311 | *data.caps)) |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 312 | { |
| 313 | return Error(GL_NO_ERROR); |
| 314 | } |
| 315 | |
Jamie Madill | 5c6b7bf | 2015-08-17 12:53:35 -0400 | [diff] [blame^] | 316 | if (!gatherTransformFeedbackLinkedVaryings( |
| 317 | mInfoLog, linkedVaryings, mData.mTransformFeedbackVaryings, |
| 318 | mData.mTransformFeedbackBufferMode, &mProgram->getTransformFeedbackLinkedVaryings(), |
| 319 | *data.caps)) |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 320 | { |
| 321 | return Error(GL_NO_ERROR); |
| 322 | } |
| 323 | |
| 324 | // TODO: The concept of "executables" is D3D only, and as such this belongs in ProgramD3D. It must be called, |
| 325 | // however, last in this function, so it can't simply be moved to ProgramD3D::link without further shuffling. |
Jamie Madill | 5c6b7bf | 2015-08-17 12:53:35 -0400 | [diff] [blame^] | 326 | result = mProgram->compileProgramExecutables(mInfoLog, registers); |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 327 | if (result.error.isError() || !result.linkSuccess) |
| 328 | { |
Jamie Madill | f611316 | 2015-05-07 11:49:21 -0400 | [diff] [blame] | 329 | mInfoLog << "Failed to create D3D shaders."; |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 330 | unlink(false); |
| 331 | return result.error; |
| 332 | } |
| 333 | |
| 334 | mLinked = true; |
Geoff Lang | b543aff | 2014-09-30 14:52:54 -0400 | [diff] [blame] | 335 | return gl::Error(GL_NO_ERROR); |
apatrick@chromium.org | 9a30b09 | 2012-06-06 20:21:55 +0000 | [diff] [blame] | 336 | } |
| 337 | |
apatrick@chromium.org | 9a30b09 | 2012-06-06 20:21:55 +0000 | [diff] [blame] | 338 | int AttributeBindings::getAttributeBinding(const std::string &name) const |
daniel@transgaming.com | b4ff1f8 | 2010-04-22 13:35:18 +0000 | [diff] [blame] | 339 | { |
| 340 | for (int location = 0; location < MAX_VERTEX_ATTRIBS; location++) |
| 341 | { |
| 342 | if (mAttributeBinding[location].find(name) != mAttributeBinding[location].end()) |
| 343 | { |
| 344 | return location; |
| 345 | } |
| 346 | } |
| 347 | |
| 348 | return -1; |
| 349 | } |
| 350 | |
daniel@transgaming.com | aa5e59b | 2011-10-04 18:43:12 +0000 | [diff] [blame] | 351 | // Returns the program object to an unlinked state, before re-linking, or at destruction |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 352 | void Program::unlink(bool destroy) |
| 353 | { |
| 354 | if (destroy) // Object being destructed |
| 355 | { |
Jamie Madill | 5c6b7bf | 2015-08-17 12:53:35 -0400 | [diff] [blame^] | 356 | if (mData.mAttachedFragmentShader) |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 357 | { |
Jamie Madill | 5c6b7bf | 2015-08-17 12:53:35 -0400 | [diff] [blame^] | 358 | mData.mAttachedFragmentShader->release(); |
| 359 | mData.mAttachedFragmentShader = nullptr; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 360 | } |
| 361 | |
Jamie Madill | 5c6b7bf | 2015-08-17 12:53:35 -0400 | [diff] [blame^] | 362 | if (mData.mAttachedVertexShader) |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 363 | { |
Jamie Madill | 5c6b7bf | 2015-08-17 12:53:35 -0400 | [diff] [blame^] | 364 | mData.mAttachedVertexShader->release(); |
| 365 | mData.mAttachedVertexShader = nullptr; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 366 | } |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 367 | } |
| 368 | |
Jamie Madill | 5c6b7bf | 2015-08-17 12:53:35 -0400 | [diff] [blame^] | 369 | mLinkedAttributes.assign(mLinkedAttributes.size(), sh::Attribute()); |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 370 | mOutputVariables.clear(); |
| 371 | |
| 372 | mProgram->reset(); |
| 373 | |
| 374 | mValidated = false; |
| 375 | |
daniel@transgaming.com | 716056c | 2012-07-24 18:38:59 +0000 | [diff] [blame] | 376 | mLinked = false; |
| 377 | } |
| 378 | |
| 379 | bool Program::isLinked() |
| 380 | { |
| 381 | return mLinked; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 382 | } |
| 383 | |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 384 | Error Program::loadBinary(GLenum binaryFormat, const void *binary, GLsizei length) |
apatrick@chromium.org | 3ce8dbc | 2012-06-08 17:52:30 +0000 | [diff] [blame] | 385 | { |
| 386 | unlink(false); |
apatrick@chromium.org | 90080e3 | 2012-07-09 22:15:33 +0000 | [diff] [blame] | 387 | |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 388 | #if ANGLE_PROGRAM_BINARY_LOAD != ANGLE_ENABLED |
| 389 | return Error(GL_NO_ERROR); |
| 390 | #else |
| 391 | ASSERT(binaryFormat == mProgram->getBinaryFormat()); |
apatrick@chromium.org | 90080e3 | 2012-07-09 22:15:33 +0000 | [diff] [blame] | 392 | |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 393 | BinaryInputStream stream(binary, length); |
| 394 | |
| 395 | GLenum format = stream.readInt<GLenum>(); |
| 396 | if (format != mProgram->getBinaryFormat()) |
apatrick@chromium.org | 90080e3 | 2012-07-09 22:15:33 +0000 | [diff] [blame] | 397 | { |
Jamie Madill | f611316 | 2015-05-07 11:49:21 -0400 | [diff] [blame] | 398 | mInfoLog << "Invalid program binary format."; |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 399 | return Error(GL_NO_ERROR); |
| 400 | } |
| 401 | |
| 402 | int majorVersion = stream.readInt<int>(); |
| 403 | int minorVersion = stream.readInt<int>(); |
| 404 | if (majorVersion != ANGLE_MAJOR_VERSION || minorVersion != ANGLE_MINOR_VERSION) |
| 405 | { |
Jamie Madill | f611316 | 2015-05-07 11:49:21 -0400 | [diff] [blame] | 406 | mInfoLog << "Invalid program binary version."; |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 407 | return Error(GL_NO_ERROR); |
| 408 | } |
| 409 | |
| 410 | unsigned char commitString[ANGLE_COMMIT_HASH_SIZE]; |
| 411 | stream.readBytes(commitString, ANGLE_COMMIT_HASH_SIZE); |
| 412 | if (memcmp(commitString, ANGLE_COMMIT_HASH, sizeof(unsigned char) * ANGLE_COMMIT_HASH_SIZE) != 0) |
| 413 | { |
Jamie Madill | f611316 | 2015-05-07 11:49:21 -0400 | [diff] [blame] | 414 | mInfoLog << "Invalid program binary version."; |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 415 | return Error(GL_NO_ERROR); |
| 416 | } |
| 417 | |
Jamie Madill | 3da79b7 | 2015-04-27 11:09:17 -0400 | [diff] [blame] | 418 | // TODO(jmadill): replace MAX_VERTEX_ATTRIBS |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 419 | for (int i = 0; i < MAX_VERTEX_ATTRIBS; ++i) |
| 420 | { |
Jamie Madill | 5c6b7bf | 2015-08-17 12:53:35 -0400 | [diff] [blame^] | 421 | stream.readInt(&mLinkedAttributes[i].type); |
| 422 | stream.readString(&mLinkedAttributes[i].name); |
Jamie Madill | 437d266 | 2014-12-05 14:23:35 -0500 | [diff] [blame] | 423 | stream.readInt(&mProgram->getSemanticIndexes()[i]); |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 424 | } |
| 425 | |
Jamie Madill | 3da79b7 | 2015-04-27 11:09:17 -0400 | [diff] [blame] | 426 | unsigned int attribCount = stream.readInt<unsigned int>(); |
| 427 | for (unsigned int attribIndex = 0; attribIndex < attribCount; ++attribIndex) |
| 428 | { |
| 429 | GLenum type = stream.readInt<GLenum>(); |
| 430 | GLenum precision = stream.readInt<GLenum>(); |
| 431 | std::string name = stream.readString(); |
| 432 | GLint arraySize = stream.readInt<GLint>(); |
| 433 | int location = stream.readInt<int>(); |
| 434 | mProgram->setShaderAttribute(attribIndex, type, precision, name, arraySize, location); |
| 435 | } |
| 436 | |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 437 | rx::LinkResult result = mProgram->load(mInfoLog, &stream); |
| 438 | if (result.error.isError() || !result.linkSuccess) |
| 439 | { |
Geoff Lang | b543aff | 2014-09-30 14:52:54 -0400 | [diff] [blame] | 440 | return result.error; |
apatrick@chromium.org | 90080e3 | 2012-07-09 22:15:33 +0000 | [diff] [blame] | 441 | } |
daniel@transgaming.com | 4c962bf | 2012-07-24 18:37:02 +0000 | [diff] [blame] | 442 | |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 443 | mLinked = true; |
Geoff Lang | b543aff | 2014-09-30 14:52:54 -0400 | [diff] [blame] | 444 | return Error(GL_NO_ERROR); |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 445 | #endif // #if ANGLE_PROGRAM_BINARY_LOAD == ANGLE_ENABLED |
| 446 | } |
| 447 | |
| 448 | Error Program::saveBinary(GLenum *binaryFormat, void *binary, GLsizei bufSize, GLsizei *length) const |
| 449 | { |
| 450 | if (binaryFormat) |
| 451 | { |
| 452 | *binaryFormat = mProgram->getBinaryFormat(); |
| 453 | } |
| 454 | |
| 455 | BinaryOutputStream stream; |
| 456 | |
| 457 | stream.writeInt(mProgram->getBinaryFormat()); |
| 458 | stream.writeInt(ANGLE_MAJOR_VERSION); |
| 459 | stream.writeInt(ANGLE_MINOR_VERSION); |
| 460 | stream.writeBytes(reinterpret_cast<const unsigned char*>(ANGLE_COMMIT_HASH), ANGLE_COMMIT_HASH_SIZE); |
| 461 | |
Jamie Madill | 3da79b7 | 2015-04-27 11:09:17 -0400 | [diff] [blame] | 462 | // TODO(jmadill): replace MAX_VERTEX_ATTRIBS |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 463 | for (unsigned int i = 0; i < MAX_VERTEX_ATTRIBS; ++i) |
| 464 | { |
Jamie Madill | 5c6b7bf | 2015-08-17 12:53:35 -0400 | [diff] [blame^] | 465 | stream.writeInt(mLinkedAttributes[i].type); |
| 466 | stream.writeString(mLinkedAttributes[i].name); |
Jamie Madill | 437d266 | 2014-12-05 14:23:35 -0500 | [diff] [blame] | 467 | stream.writeInt(mProgram->getSemanticIndexes()[i]); |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 468 | } |
| 469 | |
Jamie Madill | 3da79b7 | 2015-04-27 11:09:17 -0400 | [diff] [blame] | 470 | const auto &shaderAttributes = mProgram->getShaderAttributes(); |
| 471 | stream.writeInt(shaderAttributes.size()); |
| 472 | for (const auto &attrib : shaderAttributes) |
| 473 | { |
| 474 | stream.writeInt(attrib.type); |
| 475 | stream.writeInt(attrib.precision); |
| 476 | stream.writeString(attrib.name); |
| 477 | stream.writeInt(attrib.arraySize); |
| 478 | stream.writeInt(attrib.location); |
| 479 | } |
| 480 | |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 481 | gl::Error error = mProgram->save(&stream); |
| 482 | if (error.isError()) |
| 483 | { |
| 484 | return error; |
| 485 | } |
| 486 | |
Cooper Partin | 4d61f7e | 2015-08-12 10:56:50 -0700 | [diff] [blame] | 487 | GLsizei streamLength = static_cast<GLsizei>(stream.length()); |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 488 | const void *streamData = stream.data(); |
| 489 | |
| 490 | if (streamLength > bufSize) |
| 491 | { |
| 492 | if (length) |
| 493 | { |
| 494 | *length = 0; |
| 495 | } |
| 496 | |
| 497 | // TODO: This should be moved to the validation layer but computing the size of the binary before saving |
| 498 | // it causes the save to happen twice. It may be possible to write the binary to a separate buffer, validate |
| 499 | // sizes and then copy it. |
| 500 | return Error(GL_INVALID_OPERATION); |
| 501 | } |
| 502 | |
| 503 | if (binary) |
| 504 | { |
| 505 | char *ptr = reinterpret_cast<char*>(binary); |
| 506 | |
| 507 | memcpy(ptr, streamData, streamLength); |
| 508 | ptr += streamLength; |
| 509 | |
| 510 | ASSERT(ptr - streamLength == binary); |
| 511 | } |
| 512 | |
| 513 | if (length) |
| 514 | { |
| 515 | *length = streamLength; |
| 516 | } |
| 517 | |
| 518 | return Error(GL_NO_ERROR); |
| 519 | } |
| 520 | |
| 521 | GLint Program::getBinaryLength() const |
| 522 | { |
| 523 | GLint length; |
Jamie Madill | 71c3b2c | 2015-05-07 11:49:20 -0400 | [diff] [blame] | 524 | Error error = saveBinary(nullptr, nullptr, std::numeric_limits<GLint>::max(), &length); |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 525 | if (error.isError()) |
| 526 | { |
| 527 | return 0; |
| 528 | } |
| 529 | |
| 530 | return length; |
apatrick@chromium.org | 3ce8dbc | 2012-06-08 17:52:30 +0000 | [diff] [blame] | 531 | } |
| 532 | |
daniel@transgaming.com | da13f3e | 2010-07-28 19:20:56 +0000 | [diff] [blame] | 533 | void Program::release() |
| 534 | { |
| 535 | mRefCount--; |
| 536 | |
| 537 | if (mRefCount == 0 && mDeleteStatus) |
| 538 | { |
| 539 | mResourceManager->deleteProgram(mHandle); |
| 540 | } |
| 541 | } |
| 542 | |
| 543 | void Program::addRef() |
| 544 | { |
| 545 | mRefCount++; |
| 546 | } |
| 547 | |
| 548 | unsigned int Program::getRefCount() const |
| 549 | { |
| 550 | return mRefCount; |
| 551 | } |
| 552 | |
apatrick@chromium.org | e2a59bb | 2012-06-07 21:09:53 +0000 | [diff] [blame] | 553 | int Program::getInfoLogLength() const |
| 554 | { |
Jamie Madill | 71c3b2c | 2015-05-07 11:49:20 -0400 | [diff] [blame] | 555 | return static_cast<int>(mInfoLog.getLength()); |
apatrick@chromium.org | e2a59bb | 2012-06-07 21:09:53 +0000 | [diff] [blame] | 556 | } |
| 557 | |
apatrick@chromium.org | e2a59bb | 2012-06-07 21:09:53 +0000 | [diff] [blame] | 558 | void Program::getInfoLog(GLsizei bufSize, GLsizei *length, char *infoLog) |
| 559 | { |
apatrick@chromium.org | 253b8d2 | 2012-06-22 19:27:21 +0000 | [diff] [blame] | 560 | return mInfoLog.getLog(bufSize, length, infoLog); |
apatrick@chromium.org | e2a59bb | 2012-06-07 21:09:53 +0000 | [diff] [blame] | 561 | } |
| 562 | |
daniel@transgaming.com | 6c78521 | 2010-03-30 03:36:17 +0000 | [diff] [blame] | 563 | void Program::getAttachedShaders(GLsizei maxCount, GLsizei *count, GLuint *shaders) |
| 564 | { |
| 565 | int total = 0; |
| 566 | |
Jamie Madill | 5c6b7bf | 2015-08-17 12:53:35 -0400 | [diff] [blame^] | 567 | if (mData.mAttachedVertexShader) |
daniel@transgaming.com | 6c78521 | 2010-03-30 03:36:17 +0000 | [diff] [blame] | 568 | { |
| 569 | if (total < maxCount) |
| 570 | { |
Jamie Madill | 5c6b7bf | 2015-08-17 12:53:35 -0400 | [diff] [blame^] | 571 | shaders[total] = mData.mAttachedVertexShader->getHandle(); |
daniel@transgaming.com | 6c78521 | 2010-03-30 03:36:17 +0000 | [diff] [blame] | 572 | } |
| 573 | |
| 574 | total++; |
| 575 | } |
| 576 | |
Jamie Madill | 5c6b7bf | 2015-08-17 12:53:35 -0400 | [diff] [blame^] | 577 | if (mData.mAttachedFragmentShader) |
daniel@transgaming.com | 6c78521 | 2010-03-30 03:36:17 +0000 | [diff] [blame] | 578 | { |
| 579 | if (total < maxCount) |
| 580 | { |
Jamie Madill | 5c6b7bf | 2015-08-17 12:53:35 -0400 | [diff] [blame^] | 581 | shaders[total] = mData.mAttachedFragmentShader->getHandle(); |
daniel@transgaming.com | 6c78521 | 2010-03-30 03:36:17 +0000 | [diff] [blame] | 582 | } |
| 583 | |
| 584 | total++; |
| 585 | } |
| 586 | |
| 587 | if (count) |
| 588 | { |
| 589 | *count = total; |
| 590 | } |
| 591 | } |
| 592 | |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 593 | GLuint Program::getAttributeLocation(const std::string &name) |
| 594 | { |
Jamie Madill | 5c6b7bf | 2015-08-17 12:53:35 -0400 | [diff] [blame^] | 595 | for (size_t index = 0; index < mLinkedAttributes.size(); index++) |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 596 | { |
Jamie Madill | 5c6b7bf | 2015-08-17 12:53:35 -0400 | [diff] [blame^] | 597 | if (mLinkedAttributes[index].name == name) |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 598 | { |
Jamie Madill | 5c6b7bf | 2015-08-17 12:53:35 -0400 | [diff] [blame^] | 599 | return static_cast<GLuint>(index); |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 600 | } |
| 601 | } |
| 602 | |
Austin Kinross | b8af723 | 2015-03-16 22:33:25 -0700 | [diff] [blame] | 603 | return static_cast<GLuint>(-1); |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 604 | } |
| 605 | |
Jamie Madill | 56c6e3c | 2015-04-15 10:18:05 -0400 | [diff] [blame] | 606 | const int *Program::getSemanticIndexes() const |
| 607 | { |
| 608 | return mProgram->getSemanticIndexes(); |
| 609 | } |
| 610 | |
Jamie Madill | 46565a4 | 2015-06-22 13:57:21 -0400 | [diff] [blame] | 611 | int Program::getSemanticIndex(int attributeIndex) const |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 612 | { |
| 613 | ASSERT(attributeIndex >= 0 && attributeIndex < MAX_VERTEX_ATTRIBS); |
| 614 | |
Jamie Madill | 437d266 | 2014-12-05 14:23:35 -0500 | [diff] [blame] | 615 | return mProgram->getSemanticIndexes()[attributeIndex]; |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 616 | } |
| 617 | |
apatrick@chromium.org | e2a59bb | 2012-06-07 21:09:53 +0000 | [diff] [blame] | 618 | void Program::getActiveAttribute(GLuint index, GLsizei bufsize, GLsizei *length, GLint *size, GLenum *type, GLchar *name) |
| 619 | { |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 620 | if (mLinked) |
apatrick@chromium.org | e2a59bb | 2012-06-07 21:09:53 +0000 | [diff] [blame] | 621 | { |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 622 | // Skip over inactive attributes |
| 623 | unsigned int activeAttribute = 0; |
| 624 | unsigned int attribute; |
Jamie Madill | 5c6b7bf | 2015-08-17 12:53:35 -0400 | [diff] [blame^] | 625 | for (attribute = 0; attribute < static_cast<unsigned int>(mLinkedAttributes.size()); |
| 626 | attribute++) |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 627 | { |
Jamie Madill | 5c6b7bf | 2015-08-17 12:53:35 -0400 | [diff] [blame^] | 628 | if (mLinkedAttributes[attribute].name.empty()) |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 629 | { |
| 630 | continue; |
| 631 | } |
| 632 | |
| 633 | if (activeAttribute == index) |
| 634 | { |
| 635 | break; |
| 636 | } |
| 637 | |
| 638 | activeAttribute++; |
| 639 | } |
| 640 | |
| 641 | if (bufsize > 0) |
| 642 | { |
Jamie Madill | 5c6b7bf | 2015-08-17 12:53:35 -0400 | [diff] [blame^] | 643 | const char *string = mLinkedAttributes[attribute].name.c_str(); |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 644 | |
| 645 | strncpy(name, string, bufsize); |
| 646 | name[bufsize - 1] = '\0'; |
| 647 | |
| 648 | if (length) |
| 649 | { |
Cooper Partin | 4d61f7e | 2015-08-12 10:56:50 -0700 | [diff] [blame] | 650 | *length = static_cast<GLsizei>(strlen(name)); |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 651 | } |
| 652 | } |
| 653 | |
| 654 | *size = 1; // Always a single 'type' instance |
| 655 | |
Jamie Madill | 5c6b7bf | 2015-08-17 12:53:35 -0400 | [diff] [blame^] | 656 | *type = mLinkedAttributes[attribute].type; |
apatrick@chromium.org | e2a59bb | 2012-06-07 21:09:53 +0000 | [diff] [blame] | 657 | } |
| 658 | else |
| 659 | { |
| 660 | if (bufsize > 0) |
| 661 | { |
| 662 | name[0] = '\0'; |
| 663 | } |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 664 | |
apatrick@chromium.org | e2a59bb | 2012-06-07 21:09:53 +0000 | [diff] [blame] | 665 | if (length) |
| 666 | { |
| 667 | *length = 0; |
| 668 | } |
| 669 | |
| 670 | *type = GL_NONE; |
| 671 | *size = 1; |
| 672 | } |
| 673 | } |
| 674 | |
apatrick@chromium.org | e2a59bb | 2012-06-07 21:09:53 +0000 | [diff] [blame] | 675 | GLint Program::getActiveAttributeCount() |
| 676 | { |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 677 | int count = 0; |
| 678 | |
| 679 | if (mLinked) |
apatrick@chromium.org | e2a59bb | 2012-06-07 21:09:53 +0000 | [diff] [blame] | 680 | { |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 681 | for (int attributeIndex = 0; attributeIndex < MAX_VERTEX_ATTRIBS; attributeIndex++) |
| 682 | { |
Jamie Madill | 5c6b7bf | 2015-08-17 12:53:35 -0400 | [diff] [blame^] | 683 | if (!mLinkedAttributes[attributeIndex].name.empty()) |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 684 | { |
| 685 | count++; |
| 686 | } |
| 687 | } |
apatrick@chromium.org | e2a59bb | 2012-06-07 21:09:53 +0000 | [diff] [blame] | 688 | } |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 689 | |
| 690 | return count; |
apatrick@chromium.org | e2a59bb | 2012-06-07 21:09:53 +0000 | [diff] [blame] | 691 | } |
| 692 | |
apatrick@chromium.org | e2a59bb | 2012-06-07 21:09:53 +0000 | [diff] [blame] | 693 | GLint Program::getActiveAttributeMaxLength() |
| 694 | { |
Jamie Madill | 5c6b7bf | 2015-08-17 12:53:35 -0400 | [diff] [blame^] | 695 | GLint maxLength = 0; |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 696 | |
| 697 | if (mLinked) |
apatrick@chromium.org | e2a59bb | 2012-06-07 21:09:53 +0000 | [diff] [blame] | 698 | { |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 699 | for (int attributeIndex = 0; attributeIndex < MAX_VERTEX_ATTRIBS; attributeIndex++) |
| 700 | { |
Jamie Madill | 5c6b7bf | 2015-08-17 12:53:35 -0400 | [diff] [blame^] | 701 | if (!mLinkedAttributes[attributeIndex].name.empty()) |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 702 | { |
Jamie Madill | 5c6b7bf | 2015-08-17 12:53:35 -0400 | [diff] [blame^] | 703 | maxLength = std::max( |
| 704 | static_cast<GLint>(mLinkedAttributes[attributeIndex].name.length() + 1), |
| 705 | maxLength); |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 706 | } |
| 707 | } |
apatrick@chromium.org | e2a59bb | 2012-06-07 21:09:53 +0000 | [diff] [blame] | 708 | } |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 709 | |
| 710 | return maxLength; |
| 711 | } |
| 712 | |
| 713 | // Returns one more than the highest sampler index used. |
| 714 | GLint Program::getUsedSamplerRange(SamplerType type) |
| 715 | { |
| 716 | return mProgram->getUsedSamplerRange(type); |
| 717 | } |
| 718 | |
| 719 | bool Program::usesPointSize() const |
| 720 | { |
| 721 | return mProgram->usesPointSize(); |
| 722 | } |
| 723 | |
| 724 | GLint Program::getSamplerMapping(SamplerType type, unsigned int samplerIndex, const Caps &caps) |
| 725 | { |
| 726 | return mProgram->getSamplerMapping(type, samplerIndex, caps); |
| 727 | } |
| 728 | |
| 729 | GLenum Program::getSamplerTextureType(SamplerType type, unsigned int samplerIndex) |
| 730 | { |
| 731 | return mProgram->getSamplerTextureType(type, samplerIndex); |
| 732 | } |
| 733 | |
| 734 | GLint Program::getFragDataLocation(const std::string &name) const |
| 735 | { |
| 736 | std::string baseName(name); |
| 737 | unsigned int arrayIndex = ParseAndStripArrayIndex(&baseName); |
Jamie Madill | 5c6b7bf | 2015-08-17 12:53:35 -0400 | [diff] [blame^] | 738 | for (auto outputPair : mOutputVariables) |
apatrick@chromium.org | e2a59bb | 2012-06-07 21:09:53 +0000 | [diff] [blame] | 739 | { |
Jamie Madill | 5c6b7bf | 2015-08-17 12:53:35 -0400 | [diff] [blame^] | 740 | const VariableLocation &outputVariable = outputPair.second; |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 741 | if (outputVariable.name == baseName && (arrayIndex == GL_INVALID_INDEX || arrayIndex == outputVariable.element)) |
| 742 | { |
Jamie Madill | 5c6b7bf | 2015-08-17 12:53:35 -0400 | [diff] [blame^] | 743 | return static_cast<GLint>(outputPair.first); |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 744 | } |
apatrick@chromium.org | e2a59bb | 2012-06-07 21:09:53 +0000 | [diff] [blame] | 745 | } |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 746 | return -1; |
apatrick@chromium.org | e2a59bb | 2012-06-07 21:09:53 +0000 | [diff] [blame] | 747 | } |
| 748 | |
apatrick@chromium.org | e2a59bb | 2012-06-07 21:09:53 +0000 | [diff] [blame] | 749 | void Program::getActiveUniform(GLuint index, GLsizei bufsize, GLsizei *length, GLint *size, GLenum *type, GLchar *name) |
| 750 | { |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 751 | if (mLinked) |
apatrick@chromium.org | e2a59bb | 2012-06-07 21:09:53 +0000 | [diff] [blame] | 752 | { |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 753 | ASSERT(index < mProgram->getUniforms().size()); // index must be smaller than getActiveUniformCount() |
| 754 | LinkedUniform *uniform = mProgram->getUniforms()[index]; |
| 755 | |
| 756 | if (bufsize > 0) |
| 757 | { |
| 758 | std::string string = uniform->name; |
| 759 | if (uniform->isArray()) |
| 760 | { |
| 761 | string += "[0]"; |
| 762 | } |
| 763 | |
| 764 | strncpy(name, string.c_str(), bufsize); |
| 765 | name[bufsize - 1] = '\0'; |
| 766 | |
| 767 | if (length) |
| 768 | { |
Cooper Partin | 4d61f7e | 2015-08-12 10:56:50 -0700 | [diff] [blame] | 769 | *length = static_cast<GLsizei>(strlen(name)); |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 770 | } |
| 771 | } |
| 772 | |
| 773 | *size = uniform->elementCount(); |
| 774 | *type = uniform->type; |
apatrick@chromium.org | e2a59bb | 2012-06-07 21:09:53 +0000 | [diff] [blame] | 775 | } |
| 776 | else |
| 777 | { |
| 778 | if (bufsize > 0) |
| 779 | { |
| 780 | name[0] = '\0'; |
| 781 | } |
| 782 | |
| 783 | if (length) |
| 784 | { |
| 785 | *length = 0; |
| 786 | } |
| 787 | |
| 788 | *size = 0; |
| 789 | *type = GL_NONE; |
| 790 | } |
| 791 | } |
| 792 | |
apatrick@chromium.org | e2a59bb | 2012-06-07 21:09:53 +0000 | [diff] [blame] | 793 | GLint Program::getActiveUniformCount() |
| 794 | { |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 795 | if (mLinked) |
apatrick@chromium.org | e2a59bb | 2012-06-07 21:09:53 +0000 | [diff] [blame] | 796 | { |
Cooper Partin | 4d61f7e | 2015-08-12 10:56:50 -0700 | [diff] [blame] | 797 | return static_cast<GLint>(mProgram->getUniforms().size()); |
apatrick@chromium.org | e2a59bb | 2012-06-07 21:09:53 +0000 | [diff] [blame] | 798 | } |
| 799 | else |
| 800 | { |
| 801 | return 0; |
| 802 | } |
| 803 | } |
| 804 | |
apatrick@chromium.org | e2a59bb | 2012-06-07 21:09:53 +0000 | [diff] [blame] | 805 | GLint Program::getActiveUniformMaxLength() |
| 806 | { |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 807 | int maxLength = 0; |
| 808 | |
| 809 | if (mLinked) |
apatrick@chromium.org | e2a59bb | 2012-06-07 21:09:53 +0000 | [diff] [blame] | 810 | { |
Cooper Partin | 4d61f7e | 2015-08-12 10:56:50 -0700 | [diff] [blame] | 811 | unsigned int numUniforms = static_cast<unsigned int>(mProgram->getUniforms().size()); |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 812 | for (unsigned int uniformIndex = 0; uniformIndex < numUniforms; uniformIndex++) |
| 813 | { |
| 814 | if (!mProgram->getUniforms()[uniformIndex]->name.empty()) |
| 815 | { |
| 816 | int length = (int)(mProgram->getUniforms()[uniformIndex]->name.length() + 1); |
| 817 | if (mProgram->getUniforms()[uniformIndex]->isArray()) |
| 818 | { |
| 819 | length += 3; // Counting in "[0]". |
| 820 | } |
| 821 | maxLength = std::max(length, maxLength); |
| 822 | } |
| 823 | } |
apatrick@chromium.org | e2a59bb | 2012-06-07 21:09:53 +0000 | [diff] [blame] | 824 | } |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 825 | |
| 826 | return maxLength; |
| 827 | } |
| 828 | |
| 829 | GLint Program::getActiveUniformi(GLuint index, GLenum pname) const |
| 830 | { |
| 831 | const gl::LinkedUniform& uniform = *mProgram->getUniforms()[index]; |
| 832 | switch (pname) |
apatrick@chromium.org | e2a59bb | 2012-06-07 21:09:53 +0000 | [diff] [blame] | 833 | { |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 834 | case GL_UNIFORM_TYPE: return static_cast<GLint>(uniform.type); |
| 835 | case GL_UNIFORM_SIZE: return static_cast<GLint>(uniform.elementCount()); |
| 836 | case GL_UNIFORM_NAME_LENGTH: return static_cast<GLint>(uniform.name.size() + 1 + (uniform.isArray() ? 3 : 0)); |
| 837 | case GL_UNIFORM_BLOCK_INDEX: return uniform.blockIndex; |
| 838 | case GL_UNIFORM_OFFSET: return uniform.blockInfo.offset; |
| 839 | case GL_UNIFORM_ARRAY_STRIDE: return uniform.blockInfo.arrayStride; |
| 840 | case GL_UNIFORM_MATRIX_STRIDE: return uniform.blockInfo.matrixStride; |
| 841 | case GL_UNIFORM_IS_ROW_MAJOR: return static_cast<GLint>(uniform.blockInfo.isRowMajorMatrix); |
| 842 | default: |
| 843 | UNREACHABLE(); |
| 844 | break; |
apatrick@chromium.org | e2a59bb | 2012-06-07 21:09:53 +0000 | [diff] [blame] | 845 | } |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 846 | return 0; |
| 847 | } |
| 848 | |
| 849 | bool Program::isValidUniformLocation(GLint location) const |
| 850 | { |
Geoff Lang | 9513784 | 2015-06-02 15:38:43 -0400 | [diff] [blame] | 851 | const auto &uniformIndices = mProgram->getUniformIndices(); |
| 852 | ASSERT(rx::IsIntegerCastSafe<GLint>(uniformIndices.size())); |
| 853 | return (location >= 0 && uniformIndices.find(location) != uniformIndices.end()); |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 854 | } |
| 855 | |
| 856 | LinkedUniform *Program::getUniformByLocation(GLint location) const |
| 857 | { |
| 858 | return mProgram->getUniformByLocation(location); |
| 859 | } |
| 860 | |
| 861 | LinkedUniform *Program::getUniformByName(const std::string &name) const |
| 862 | { |
| 863 | return mProgram->getUniformByName(name); |
| 864 | } |
| 865 | |
| 866 | GLint Program::getUniformLocation(const std::string &name) |
| 867 | { |
| 868 | return mProgram->getUniformLocation(name); |
| 869 | } |
| 870 | |
| 871 | GLuint Program::getUniformIndex(const std::string &name) |
| 872 | { |
| 873 | return mProgram->getUniformIndex(name); |
| 874 | } |
| 875 | |
| 876 | void Program::setUniform1fv(GLint location, GLsizei count, const GLfloat *v) |
| 877 | { |
| 878 | mProgram->setUniform1fv(location, count, v); |
| 879 | } |
| 880 | |
| 881 | void Program::setUniform2fv(GLint location, GLsizei count, const GLfloat *v) |
| 882 | { |
| 883 | mProgram->setUniform2fv(location, count, v); |
| 884 | } |
| 885 | |
| 886 | void Program::setUniform3fv(GLint location, GLsizei count, const GLfloat *v) |
| 887 | { |
| 888 | mProgram->setUniform3fv(location, count, v); |
| 889 | } |
| 890 | |
| 891 | void Program::setUniform4fv(GLint location, GLsizei count, const GLfloat *v) |
| 892 | { |
| 893 | mProgram->setUniform4fv(location, count, v); |
| 894 | } |
| 895 | |
| 896 | void Program::setUniform1iv(GLint location, GLsizei count, const GLint *v) |
| 897 | { |
| 898 | mProgram->setUniform1iv(location, count, v); |
| 899 | } |
| 900 | |
| 901 | void Program::setUniform2iv(GLint location, GLsizei count, const GLint *v) |
| 902 | { |
| 903 | mProgram->setUniform2iv(location, count, v); |
| 904 | } |
| 905 | |
| 906 | void Program::setUniform3iv(GLint location, GLsizei count, const GLint *v) |
| 907 | { |
| 908 | mProgram->setUniform3iv(location, count, v); |
| 909 | } |
| 910 | |
| 911 | void Program::setUniform4iv(GLint location, GLsizei count, const GLint *v) |
| 912 | { |
| 913 | mProgram->setUniform4iv(location, count, v); |
| 914 | } |
| 915 | |
| 916 | void Program::setUniform1uiv(GLint location, GLsizei count, const GLuint *v) |
| 917 | { |
| 918 | mProgram->setUniform1uiv(location, count, v); |
| 919 | } |
| 920 | |
| 921 | void Program::setUniform2uiv(GLint location, GLsizei count, const GLuint *v) |
| 922 | { |
| 923 | mProgram->setUniform2uiv(location, count, v); |
| 924 | } |
| 925 | |
| 926 | void Program::setUniform3uiv(GLint location, GLsizei count, const GLuint *v) |
| 927 | { |
| 928 | mProgram->setUniform3uiv(location, count, v); |
| 929 | } |
| 930 | |
| 931 | void Program::setUniform4uiv(GLint location, GLsizei count, const GLuint *v) |
| 932 | { |
| 933 | mProgram->setUniform4uiv(location, count, v); |
| 934 | } |
| 935 | |
| 936 | void Program::setUniformMatrix2fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v) |
| 937 | { |
| 938 | mProgram->setUniformMatrix2fv(location, count, transpose, v); |
| 939 | } |
| 940 | |
| 941 | void Program::setUniformMatrix3fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v) |
| 942 | { |
| 943 | mProgram->setUniformMatrix3fv(location, count, transpose, v); |
| 944 | } |
| 945 | |
| 946 | void Program::setUniformMatrix4fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v) |
| 947 | { |
| 948 | mProgram->setUniformMatrix4fv(location, count, transpose, v); |
| 949 | } |
| 950 | |
| 951 | void Program::setUniformMatrix2x3fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v) |
| 952 | { |
| 953 | mProgram->setUniformMatrix2x3fv(location, count, transpose, v); |
| 954 | } |
| 955 | |
| 956 | void Program::setUniformMatrix2x4fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v) |
| 957 | { |
| 958 | mProgram->setUniformMatrix2x4fv(location, count, transpose, v); |
| 959 | } |
| 960 | |
| 961 | void Program::setUniformMatrix3x2fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v) |
| 962 | { |
| 963 | mProgram->setUniformMatrix3x2fv(location, count, transpose, v); |
| 964 | } |
| 965 | |
| 966 | void Program::setUniformMatrix3x4fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v) |
| 967 | { |
| 968 | mProgram->setUniformMatrix3x4fv(location, count, transpose, v); |
| 969 | } |
| 970 | |
| 971 | void Program::setUniformMatrix4x2fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v) |
| 972 | { |
| 973 | mProgram->setUniformMatrix4x2fv(location, count, transpose, v); |
| 974 | } |
| 975 | |
| 976 | void Program::setUniformMatrix4x3fv(GLint location, GLsizei count, GLboolean transpose, const GLfloat *v) |
| 977 | { |
| 978 | mProgram->setUniformMatrix4x3fv(location, count, transpose, v); |
| 979 | } |
| 980 | |
| 981 | void Program::getUniformfv(GLint location, GLfloat *v) |
| 982 | { |
| 983 | mProgram->getUniformfv(location, v); |
| 984 | } |
| 985 | |
| 986 | void Program::getUniformiv(GLint location, GLint *v) |
| 987 | { |
| 988 | mProgram->getUniformiv(location, v); |
| 989 | } |
| 990 | |
| 991 | void Program::getUniformuiv(GLint location, GLuint *v) |
| 992 | { |
| 993 | mProgram->getUniformuiv(location, v); |
| 994 | } |
| 995 | |
| 996 | // Applies all the uniforms set for this program object to the renderer |
| 997 | Error Program::applyUniforms() |
| 998 | { |
| 999 | return mProgram->applyUniforms(); |
| 1000 | } |
| 1001 | |
Gregoire Payen de La Garanderie | 68694e9 | 2015-03-24 14:03:37 +0000 | [diff] [blame] | 1002 | Error Program::applyUniformBuffers(const gl::Data &data) |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1003 | { |
Gregoire Payen de La Garanderie | 68694e9 | 2015-03-24 14:03:37 +0000 | [diff] [blame] | 1004 | return mProgram->applyUniformBuffers(data, mUniformBlockBindings); |
apatrick@chromium.org | e2a59bb | 2012-06-07 21:09:53 +0000 | [diff] [blame] | 1005 | } |
| 1006 | |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1007 | void Program::flagForDeletion() |
| 1008 | { |
| 1009 | mDeleteStatus = true; |
| 1010 | } |
| 1011 | |
| 1012 | bool Program::isFlaggedForDeletion() const |
| 1013 | { |
| 1014 | return mDeleteStatus; |
| 1015 | } |
daniel@transgaming.com | 86a7a13 | 2010-04-29 03:32:32 +0000 | [diff] [blame] | 1016 | |
Brandon Jones | 43a53e2 | 2014-08-28 16:23:22 -0700 | [diff] [blame] | 1017 | void Program::validate(const Caps &caps) |
apatrick@chromium.org | 253b8d2 | 2012-06-22 19:27:21 +0000 | [diff] [blame] | 1018 | { |
| 1019 | mInfoLog.reset(); |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1020 | mValidated = false; |
apatrick@chromium.org | 253b8d2 | 2012-06-22 19:27:21 +0000 | [diff] [blame] | 1021 | |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1022 | if (mLinked) |
apatrick@chromium.org | 253b8d2 | 2012-06-22 19:27:21 +0000 | [diff] [blame] | 1023 | { |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1024 | applyUniforms(); |
| 1025 | mValidated = mProgram->validateSamplers(&mInfoLog, caps); |
apatrick@chromium.org | 253b8d2 | 2012-06-22 19:27:21 +0000 | [diff] [blame] | 1026 | } |
| 1027 | else |
| 1028 | { |
Jamie Madill | f611316 | 2015-05-07 11:49:21 -0400 | [diff] [blame] | 1029 | mInfoLog << "Program has not been successfully linked."; |
apatrick@chromium.org | 253b8d2 | 2012-06-22 19:27:21 +0000 | [diff] [blame] | 1030 | } |
| 1031 | } |
| 1032 | |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1033 | bool Program::validateSamplers(InfoLog *infoLog, const Caps &caps) |
| 1034 | { |
| 1035 | return mProgram->validateSamplers(infoLog, caps); |
| 1036 | } |
| 1037 | |
apatrick@chromium.org | e2a59bb | 2012-06-07 21:09:53 +0000 | [diff] [blame] | 1038 | bool Program::isValidated() const |
| 1039 | { |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1040 | return mValidated; |
| 1041 | } |
| 1042 | |
| 1043 | void Program::updateSamplerMapping() |
| 1044 | { |
| 1045 | return mProgram->updateSamplerMapping(); |
| 1046 | } |
| 1047 | |
| 1048 | GLuint Program::getActiveUniformBlockCount() |
| 1049 | { |
Cooper Partin | 4d61f7e | 2015-08-12 10:56:50 -0700 | [diff] [blame] | 1050 | return static_cast<GLuint>(mProgram->getUniformBlocks().size()); |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1051 | } |
| 1052 | |
| 1053 | void Program::getActiveUniformBlockName(GLuint uniformBlockIndex, GLsizei bufSize, GLsizei *length, GLchar *uniformBlockName) const |
| 1054 | { |
| 1055 | ASSERT(uniformBlockIndex < mProgram->getUniformBlocks().size()); // index must be smaller than getActiveUniformBlockCount() |
| 1056 | |
| 1057 | const UniformBlock &uniformBlock = *mProgram->getUniformBlocks()[uniformBlockIndex]; |
| 1058 | |
| 1059 | if (bufSize > 0) |
apatrick@chromium.org | e2a59bb | 2012-06-07 21:09:53 +0000 | [diff] [blame] | 1060 | { |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1061 | std::string string = uniformBlock.name; |
| 1062 | |
| 1063 | if (uniformBlock.isArrayElement()) |
| 1064 | { |
| 1065 | string += ArrayString(uniformBlock.elementIndex); |
| 1066 | } |
| 1067 | |
| 1068 | strncpy(uniformBlockName, string.c_str(), bufSize); |
| 1069 | uniformBlockName[bufSize - 1] = '\0'; |
| 1070 | |
| 1071 | if (length) |
| 1072 | { |
Cooper Partin | 4d61f7e | 2015-08-12 10:56:50 -0700 | [diff] [blame] | 1073 | *length = static_cast<GLsizei>(strlen(uniformBlockName)); |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1074 | } |
apatrick@chromium.org | e2a59bb | 2012-06-07 21:09:53 +0000 | [diff] [blame] | 1075 | } |
| 1076 | } |
| 1077 | |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1078 | void Program::getActiveUniformBlockiv(GLuint uniformBlockIndex, GLenum pname, GLint *params) const |
shannonwoods@chromium.org | e684b58 | 2013-05-30 00:07:42 +0000 | [diff] [blame] | 1079 | { |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1080 | ASSERT(uniformBlockIndex < mProgram->getUniformBlocks().size()); // index must be smaller than getActiveUniformBlockCount() |
| 1081 | |
| 1082 | const UniformBlock &uniformBlock = *mProgram->getUniformBlocks()[uniformBlockIndex]; |
| 1083 | |
| 1084 | switch (pname) |
shannonwoods@chromium.org | e684b58 | 2013-05-30 00:07:42 +0000 | [diff] [blame] | 1085 | { |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1086 | case GL_UNIFORM_BLOCK_DATA_SIZE: |
| 1087 | *params = static_cast<GLint>(uniformBlock.dataSize); |
| 1088 | break; |
| 1089 | case GL_UNIFORM_BLOCK_NAME_LENGTH: |
| 1090 | *params = static_cast<GLint>(uniformBlock.name.size() + 1 + (uniformBlock.isArrayElement() ? 3 : 0)); |
| 1091 | break; |
| 1092 | case GL_UNIFORM_BLOCK_ACTIVE_UNIFORMS: |
| 1093 | *params = static_cast<GLint>(uniformBlock.memberUniformIndexes.size()); |
| 1094 | break; |
| 1095 | case GL_UNIFORM_BLOCK_ACTIVE_UNIFORM_INDICES: |
| 1096 | { |
| 1097 | for (unsigned int blockMemberIndex = 0; blockMemberIndex < uniformBlock.memberUniformIndexes.size(); blockMemberIndex++) |
| 1098 | { |
| 1099 | params[blockMemberIndex] = static_cast<GLint>(uniformBlock.memberUniformIndexes[blockMemberIndex]); |
| 1100 | } |
| 1101 | } |
| 1102 | break; |
| 1103 | case GL_UNIFORM_BLOCK_REFERENCED_BY_VERTEX_SHADER: |
| 1104 | *params = static_cast<GLint>(uniformBlock.isReferencedByVertexShader()); |
| 1105 | break; |
| 1106 | case GL_UNIFORM_BLOCK_REFERENCED_BY_FRAGMENT_SHADER: |
| 1107 | *params = static_cast<GLint>(uniformBlock.isReferencedByFragmentShader()); |
| 1108 | break; |
| 1109 | default: UNREACHABLE(); |
shannonwoods@chromium.org | e684b58 | 2013-05-30 00:07:42 +0000 | [diff] [blame] | 1110 | } |
| 1111 | } |
| 1112 | |
| 1113 | GLint Program::getActiveUniformBlockMaxLength() |
| 1114 | { |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1115 | int maxLength = 0; |
| 1116 | |
| 1117 | if (mLinked) |
shannonwoods@chromium.org | e684b58 | 2013-05-30 00:07:42 +0000 | [diff] [blame] | 1118 | { |
Cooper Partin | 4d61f7e | 2015-08-12 10:56:50 -0700 | [diff] [blame] | 1119 | unsigned int numUniformBlocks = |
| 1120 | static_cast<unsigned int>(mProgram->getUniformBlocks().size()); |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1121 | for (unsigned int uniformBlockIndex = 0; uniformBlockIndex < numUniformBlocks; uniformBlockIndex++) |
| 1122 | { |
| 1123 | const UniformBlock &uniformBlock = *mProgram->getUniformBlocks()[uniformBlockIndex]; |
| 1124 | if (!uniformBlock.name.empty()) |
| 1125 | { |
Cooper Partin | 4d61f7e | 2015-08-12 10:56:50 -0700 | [diff] [blame] | 1126 | const int length = static_cast<int>(uniformBlock.name.length()) + 1; |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1127 | |
| 1128 | // Counting in "[0]". |
| 1129 | const int arrayLength = (uniformBlock.isArrayElement() ? 3 : 0); |
| 1130 | |
| 1131 | maxLength = std::max(length + arrayLength, maxLength); |
| 1132 | } |
| 1133 | } |
shannonwoods@chromium.org | e684b58 | 2013-05-30 00:07:42 +0000 | [diff] [blame] | 1134 | } |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1135 | |
| 1136 | return maxLength; |
| 1137 | } |
| 1138 | |
| 1139 | GLuint Program::getUniformBlockIndex(const std::string &name) |
| 1140 | { |
| 1141 | return mProgram->getUniformBlockIndex(name); |
shannonwoods@chromium.org | e684b58 | 2013-05-30 00:07:42 +0000 | [diff] [blame] | 1142 | } |
| 1143 | |
Gregoire Payen de La Garanderie | 68694e9 | 2015-03-24 14:03:37 +0000 | [diff] [blame] | 1144 | const UniformBlock *Program::getUniformBlockByIndex(GLuint index) const |
| 1145 | { |
| 1146 | return mProgram->getUniformBlockByIndex(index); |
| 1147 | } |
| 1148 | |
shannonwoods@chromium.org | 70eb1ea | 2013-05-30 00:07:20 +0000 | [diff] [blame] | 1149 | void Program::bindUniformBlock(GLuint uniformBlockIndex, GLuint uniformBlockBinding) |
| 1150 | { |
| 1151 | mUniformBlockBindings[uniformBlockIndex] = uniformBlockBinding; |
| 1152 | } |
| 1153 | |
| 1154 | GLuint Program::getUniformBlockBinding(GLuint uniformBlockIndex) const |
| 1155 | { |
| 1156 | return mUniformBlockBindings[uniformBlockIndex]; |
| 1157 | } |
| 1158 | |
| 1159 | void Program::resetUniformBlockBindings() |
| 1160 | { |
| 1161 | for (unsigned int blockId = 0; blockId < IMPLEMENTATION_MAX_COMBINED_SHADER_UNIFORM_BUFFERS; blockId++) |
| 1162 | { |
| 1163 | mUniformBlockBindings[blockId] = 0; |
| 1164 | } |
| 1165 | } |
| 1166 | |
Geoff Lang | 48dcae7 | 2014-02-05 16:28:24 -0500 | [diff] [blame] | 1167 | void Program::setTransformFeedbackVaryings(GLsizei count, const GLchar *const *varyings, GLenum bufferMode) |
| 1168 | { |
Jamie Madill | 5c6b7bf | 2015-08-17 12:53:35 -0400 | [diff] [blame^] | 1169 | mData.mTransformFeedbackVaryings.resize(count); |
Geoff Lang | 48dcae7 | 2014-02-05 16:28:24 -0500 | [diff] [blame] | 1170 | for (GLsizei i = 0; i < count; i++) |
| 1171 | { |
Jamie Madill | 5c6b7bf | 2015-08-17 12:53:35 -0400 | [diff] [blame^] | 1172 | mData.mTransformFeedbackVaryings[i] = varyings[i]; |
Geoff Lang | 48dcae7 | 2014-02-05 16:28:24 -0500 | [diff] [blame] | 1173 | } |
| 1174 | |
Jamie Madill | 5c6b7bf | 2015-08-17 12:53:35 -0400 | [diff] [blame^] | 1175 | mData.mTransformFeedbackBufferMode = bufferMode; |
Geoff Lang | 48dcae7 | 2014-02-05 16:28:24 -0500 | [diff] [blame] | 1176 | } |
| 1177 | |
| 1178 | void Program::getTransformFeedbackVarying(GLuint index, GLsizei bufSize, GLsizei *length, GLsizei *size, GLenum *type, GLchar *name) const |
| 1179 | { |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1180 | if (mLinked) |
Geoff Lang | 48dcae7 | 2014-02-05 16:28:24 -0500 | [diff] [blame] | 1181 | { |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1182 | ASSERT(index < mProgram->getTransformFeedbackLinkedVaryings().size()); |
| 1183 | const LinkedVarying &varying = mProgram->getTransformFeedbackLinkedVaryings()[index]; |
Geoff Lang | 48dcae7 | 2014-02-05 16:28:24 -0500 | [diff] [blame] | 1184 | GLsizei lastNameIdx = std::min(bufSize - 1, static_cast<GLsizei>(varying.name.length())); |
| 1185 | if (length) |
| 1186 | { |
| 1187 | *length = lastNameIdx; |
| 1188 | } |
| 1189 | if (size) |
| 1190 | { |
| 1191 | *size = varying.size; |
| 1192 | } |
| 1193 | if (type) |
| 1194 | { |
| 1195 | *type = varying.type; |
| 1196 | } |
| 1197 | if (name) |
| 1198 | { |
| 1199 | memcpy(name, varying.name.c_str(), lastNameIdx); |
| 1200 | name[lastNameIdx] = '\0'; |
| 1201 | } |
| 1202 | } |
| 1203 | } |
| 1204 | |
Geoff Lang | 1b6edcb | 2014-02-03 14:27:56 -0500 | [diff] [blame] | 1205 | GLsizei Program::getTransformFeedbackVaryingCount() const |
| 1206 | { |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1207 | if (mLinked) |
Geoff Lang | 48dcae7 | 2014-02-05 16:28:24 -0500 | [diff] [blame] | 1208 | { |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1209 | return static_cast<GLsizei>(mProgram->getTransformFeedbackLinkedVaryings().size()); |
Geoff Lang | 48dcae7 | 2014-02-05 16:28:24 -0500 | [diff] [blame] | 1210 | } |
| 1211 | else |
| 1212 | { |
| 1213 | return 0; |
| 1214 | } |
Geoff Lang | 1b6edcb | 2014-02-03 14:27:56 -0500 | [diff] [blame] | 1215 | } |
| 1216 | |
| 1217 | GLsizei Program::getTransformFeedbackVaryingMaxLength() const |
| 1218 | { |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1219 | if (mLinked) |
Geoff Lang | 48dcae7 | 2014-02-05 16:28:24 -0500 | [diff] [blame] | 1220 | { |
| 1221 | GLsizei maxSize = 0; |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1222 | for (size_t i = 0; i < mProgram->getTransformFeedbackLinkedVaryings().size(); i++) |
Geoff Lang | 48dcae7 | 2014-02-05 16:28:24 -0500 | [diff] [blame] | 1223 | { |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1224 | const LinkedVarying &varying = mProgram->getTransformFeedbackLinkedVaryings()[i]; |
Geoff Lang | 48dcae7 | 2014-02-05 16:28:24 -0500 | [diff] [blame] | 1225 | maxSize = std::max(maxSize, static_cast<GLsizei>(varying.name.length() + 1)); |
| 1226 | } |
| 1227 | |
| 1228 | return maxSize; |
| 1229 | } |
| 1230 | else |
| 1231 | { |
| 1232 | return 0; |
| 1233 | } |
Geoff Lang | 1b6edcb | 2014-02-03 14:27:56 -0500 | [diff] [blame] | 1234 | } |
| 1235 | |
| 1236 | GLenum Program::getTransformFeedbackBufferMode() const |
| 1237 | { |
Jamie Madill | 5c6b7bf | 2015-08-17 12:53:35 -0400 | [diff] [blame^] | 1238 | return mData.mTransformFeedbackBufferMode; |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1239 | } |
| 1240 | |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1241 | bool Program::linkVaryings(InfoLog &infoLog, Shader *fragmentShader, Shader *vertexShader) |
| 1242 | { |
| 1243 | std::vector<PackedVarying> &fragmentVaryings = fragmentShader->getVaryings(); |
| 1244 | std::vector<PackedVarying> &vertexVaryings = vertexShader->getVaryings(); |
| 1245 | |
| 1246 | for (size_t fragVaryingIndex = 0; fragVaryingIndex < fragmentVaryings.size(); fragVaryingIndex++) |
| 1247 | { |
| 1248 | PackedVarying *input = &fragmentVaryings[fragVaryingIndex]; |
| 1249 | bool matched = false; |
| 1250 | |
| 1251 | // Built-in varyings obey special rules |
| 1252 | if (input->isBuiltIn()) |
| 1253 | { |
| 1254 | continue; |
| 1255 | } |
| 1256 | |
| 1257 | for (size_t vertVaryingIndex = 0; vertVaryingIndex < vertexVaryings.size(); vertVaryingIndex++) |
| 1258 | { |
| 1259 | PackedVarying *output = &vertexVaryings[vertVaryingIndex]; |
| 1260 | if (output->name == input->name) |
| 1261 | { |
| 1262 | if (!linkValidateVaryings(infoLog, output->name, *input, *output)) |
| 1263 | { |
| 1264 | return false; |
| 1265 | } |
| 1266 | |
| 1267 | output->registerIndex = input->registerIndex; |
| 1268 | output->columnIndex = input->columnIndex; |
| 1269 | |
| 1270 | matched = true; |
| 1271 | break; |
| 1272 | } |
| 1273 | } |
| 1274 | |
| 1275 | // We permit unmatched, unreferenced varyings |
| 1276 | if (!matched && input->staticUse) |
| 1277 | { |
Jamie Madill | f611316 | 2015-05-07 11:49:21 -0400 | [diff] [blame] | 1278 | infoLog << "Fragment varying " << input->name << " does not match any vertex varying"; |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1279 | return false; |
| 1280 | } |
| 1281 | } |
| 1282 | |
| 1283 | return true; |
| 1284 | } |
| 1285 | |
| 1286 | bool Program::linkValidateInterfaceBlockFields(InfoLog &infoLog, const std::string &uniformName, const sh::InterfaceBlockField &vertexUniform, const sh::InterfaceBlockField &fragmentUniform) |
| 1287 | { |
| 1288 | if (!linkValidateVariablesBase(infoLog, uniformName, vertexUniform, fragmentUniform, true)) |
| 1289 | { |
| 1290 | return false; |
| 1291 | } |
| 1292 | |
| 1293 | if (vertexUniform.isRowMajorLayout != fragmentUniform.isRowMajorLayout) |
| 1294 | { |
Jamie Madill | f611316 | 2015-05-07 11:49:21 -0400 | [diff] [blame] | 1295 | infoLog << "Matrix packings for " << uniformName << " differ between vertex and fragment shaders"; |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1296 | return false; |
| 1297 | } |
| 1298 | |
| 1299 | return true; |
| 1300 | } |
| 1301 | |
| 1302 | // Determines the mapping between GL attributes and Direct3D 9 vertex stream usage indices |
Jamie Madill | 5c6b7bf | 2015-08-17 12:53:35 -0400 | [diff] [blame^] | 1303 | bool Program::linkAttributes(const gl::Data &data, |
Jamie Madill | 3da79b7 | 2015-04-27 11:09:17 -0400 | [diff] [blame] | 1304 | InfoLog &infoLog, |
| 1305 | const AttributeBindings &attributeBindings, |
| 1306 | const Shader *vertexShader) |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1307 | { |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1308 | unsigned int usedLocations = 0; |
| 1309 | const std::vector<sh::Attribute> &shaderAttributes = vertexShader->getActiveAttributes(); |
Jamie Madill | 3da79b7 | 2015-04-27 11:09:17 -0400 | [diff] [blame] | 1310 | GLuint maxAttribs = data.caps->maxVertexAttributes; |
| 1311 | |
| 1312 | // TODO(jmadill): handle aliasing robustly |
Jamie Madill | 8e695ed | 2015-06-15 17:00:44 -0400 | [diff] [blame] | 1313 | if (shaderAttributes.size() > maxAttribs) |
Jamie Madill | 3da79b7 | 2015-04-27 11:09:17 -0400 | [diff] [blame] | 1314 | { |
Jamie Madill | f611316 | 2015-05-07 11:49:21 -0400 | [diff] [blame] | 1315 | infoLog << "Too many vertex attributes."; |
Jamie Madill | 3da79b7 | 2015-04-27 11:09:17 -0400 | [diff] [blame] | 1316 | return false; |
| 1317 | } |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1318 | |
| 1319 | // Link attributes that have a binding location |
| 1320 | for (unsigned int attributeIndex = 0; attributeIndex < shaderAttributes.size(); attributeIndex++) |
| 1321 | { |
| 1322 | const sh::Attribute &attribute = shaderAttributes[attributeIndex]; |
| 1323 | |
| 1324 | ASSERT(attribute.staticUse); |
| 1325 | |
| 1326 | const int location = attribute.location == -1 ? attributeBindings.getAttributeBinding(attribute.name) : attribute.location; |
| 1327 | |
Jamie Madill | 3da79b7 | 2015-04-27 11:09:17 -0400 | [diff] [blame] | 1328 | mProgram->setShaderAttribute(attributeIndex, attribute); |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1329 | |
| 1330 | if (location != -1) // Set by glBindAttribLocation or by location layout qualifier |
| 1331 | { |
| 1332 | const int rows = VariableRegisterCount(attribute.type); |
| 1333 | |
Jamie Madill | 3da79b7 | 2015-04-27 11:09:17 -0400 | [diff] [blame] | 1334 | if (static_cast<GLuint>(rows + location) > maxAttribs) |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1335 | { |
Jamie Madill | f611316 | 2015-05-07 11:49:21 -0400 | [diff] [blame] | 1336 | infoLog << "Active attribute (" << attribute.name << ") at location " |
| 1337 | << location << " is too big to fit"; |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1338 | |
| 1339 | return false; |
| 1340 | } |
| 1341 | |
| 1342 | for (int row = 0; row < rows; row++) |
| 1343 | { |
| 1344 | const int rowLocation = location + row; |
Jamie Madill | 5c6b7bf | 2015-08-17 12:53:35 -0400 | [diff] [blame^] | 1345 | sh::ShaderVariable *linkedAttribute = &mLinkedAttributes[rowLocation]; |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1346 | |
| 1347 | // In GLSL 3.00, attribute aliasing produces a link error |
Jamie Madill | 3da79b7 | 2015-04-27 11:09:17 -0400 | [diff] [blame] | 1348 | // In GLSL 1.00, attribute aliasing is allowed, but ANGLE currently has a bug |
| 1349 | // TODO(jmadill): fix aliasing on ES2 |
| 1350 | // if (mProgram->getShaderVersion() >= 300) |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1351 | { |
Jamie Madill | 5c6b7bf | 2015-08-17 12:53:35 -0400 | [diff] [blame^] | 1352 | if (!linkedAttribute->name.empty()) |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1353 | { |
Jamie Madill | 5c6b7bf | 2015-08-17 12:53:35 -0400 | [diff] [blame^] | 1354 | infoLog << "Attribute '" << attribute.name << "' aliases attribute '" |
| 1355 | << linkedAttribute->name << "' at location " << rowLocation; |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1356 | return false; |
| 1357 | } |
| 1358 | } |
| 1359 | |
Jamie Madill | 5c6b7bf | 2015-08-17 12:53:35 -0400 | [diff] [blame^] | 1360 | *linkedAttribute = attribute; |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1361 | usedLocations |= 1 << rowLocation; |
| 1362 | } |
| 1363 | } |
| 1364 | } |
| 1365 | |
| 1366 | // Link attributes that don't have a binding location |
| 1367 | for (unsigned int attributeIndex = 0; attributeIndex < shaderAttributes.size(); attributeIndex++) |
| 1368 | { |
| 1369 | const sh::Attribute &attribute = shaderAttributes[attributeIndex]; |
| 1370 | |
| 1371 | ASSERT(attribute.staticUse); |
| 1372 | |
| 1373 | const int location = attribute.location == -1 ? attributeBindings.getAttributeBinding(attribute.name) : attribute.location; |
| 1374 | |
| 1375 | if (location == -1) // Not set by glBindAttribLocation or by location layout qualifier |
| 1376 | { |
| 1377 | int rows = VariableRegisterCount(attribute.type); |
Jamie Madill | 3da79b7 | 2015-04-27 11:09:17 -0400 | [diff] [blame] | 1378 | int availableIndex = AllocateFirstFreeBits(&usedLocations, rows, maxAttribs); |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1379 | |
Jamie Madill | 3da79b7 | 2015-04-27 11:09:17 -0400 | [diff] [blame] | 1380 | if (availableIndex == -1 || static_cast<GLuint>(availableIndex + rows) > maxAttribs) |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1381 | { |
Jamie Madill | f611316 | 2015-05-07 11:49:21 -0400 | [diff] [blame] | 1382 | infoLog << "Too many active attributes (" << attribute.name << ")"; |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1383 | return false; // Fail to link |
| 1384 | } |
| 1385 | |
Jamie Madill | 5c6b7bf | 2015-08-17 12:53:35 -0400 | [diff] [blame^] | 1386 | mLinkedAttributes[availableIndex] = attribute; |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1387 | } |
| 1388 | } |
| 1389 | |
Jamie Madill | 3da79b7 | 2015-04-27 11:09:17 -0400 | [diff] [blame] | 1390 | for (GLuint attributeIndex = 0; attributeIndex < maxAttribs;) |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1391 | { |
Jamie Madill | 5c6b7bf | 2015-08-17 12:53:35 -0400 | [diff] [blame^] | 1392 | int index = vertexShader->getSemanticIndex(mLinkedAttributes[attributeIndex].name); |
| 1393 | int rows = VariableRegisterCount(mLinkedAttributes[attributeIndex].type); |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1394 | |
| 1395 | for (int r = 0; r < rows; r++) |
| 1396 | { |
Jamie Madill | 437d266 | 2014-12-05 14:23:35 -0500 | [diff] [blame] | 1397 | mProgram->getSemanticIndexes()[attributeIndex++] = index++; |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1398 | } |
| 1399 | } |
| 1400 | |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1401 | return true; |
| 1402 | } |
| 1403 | |
| 1404 | bool Program::linkUniformBlocks(InfoLog &infoLog, const Shader &vertexShader, const Shader &fragmentShader, const Caps &caps) |
| 1405 | { |
| 1406 | const std::vector<sh::InterfaceBlock> &vertexInterfaceBlocks = vertexShader.getInterfaceBlocks(); |
| 1407 | const std::vector<sh::InterfaceBlock> &fragmentInterfaceBlocks = fragmentShader.getInterfaceBlocks(); |
| 1408 | // Check that interface blocks defined in the vertex and fragment shaders are identical |
| 1409 | typedef std::map<std::string, const sh::InterfaceBlock*> UniformBlockMap; |
| 1410 | UniformBlockMap linkedUniformBlocks; |
| 1411 | for (unsigned int blockIndex = 0; blockIndex < vertexInterfaceBlocks.size(); blockIndex++) |
| 1412 | { |
| 1413 | const sh::InterfaceBlock &vertexInterfaceBlock = vertexInterfaceBlocks[blockIndex]; |
| 1414 | linkedUniformBlocks[vertexInterfaceBlock.name] = &vertexInterfaceBlock; |
| 1415 | } |
| 1416 | for (unsigned int blockIndex = 0; blockIndex < fragmentInterfaceBlocks.size(); blockIndex++) |
| 1417 | { |
| 1418 | const sh::InterfaceBlock &fragmentInterfaceBlock = fragmentInterfaceBlocks[blockIndex]; |
| 1419 | UniformBlockMap::const_iterator entry = linkedUniformBlocks.find(fragmentInterfaceBlock.name); |
| 1420 | if (entry != linkedUniformBlocks.end()) |
| 1421 | { |
| 1422 | const sh::InterfaceBlock &vertexInterfaceBlock = *entry->second; |
| 1423 | if (!areMatchingInterfaceBlocks(infoLog, vertexInterfaceBlock, fragmentInterfaceBlock)) |
| 1424 | { |
| 1425 | return false; |
| 1426 | } |
| 1427 | } |
| 1428 | } |
| 1429 | for (unsigned int blockIndex = 0; blockIndex < vertexInterfaceBlocks.size(); blockIndex++) |
| 1430 | { |
| 1431 | const sh::InterfaceBlock &interfaceBlock = vertexInterfaceBlocks[blockIndex]; |
| 1432 | // Note: shared and std140 layouts are always considered active |
| 1433 | if (interfaceBlock.staticUse || interfaceBlock.layout != sh::BLOCKLAYOUT_PACKED) |
| 1434 | { |
| 1435 | if (!mProgram->defineUniformBlock(infoLog, vertexShader, interfaceBlock, caps)) |
| 1436 | { |
| 1437 | return false; |
| 1438 | } |
| 1439 | } |
| 1440 | } |
| 1441 | for (unsigned int blockIndex = 0; blockIndex < fragmentInterfaceBlocks.size(); blockIndex++) |
| 1442 | { |
| 1443 | const sh::InterfaceBlock &interfaceBlock = fragmentInterfaceBlocks[blockIndex]; |
| 1444 | // Note: shared and std140 layouts are always considered active |
| 1445 | if (interfaceBlock.staticUse || interfaceBlock.layout != sh::BLOCKLAYOUT_PACKED) |
| 1446 | { |
| 1447 | if (!mProgram->defineUniformBlock(infoLog, fragmentShader, interfaceBlock, caps)) |
| 1448 | { |
| 1449 | return false; |
| 1450 | } |
| 1451 | } |
| 1452 | } |
| 1453 | return true; |
| 1454 | } |
| 1455 | |
| 1456 | bool Program::areMatchingInterfaceBlocks(gl::InfoLog &infoLog, const sh::InterfaceBlock &vertexInterfaceBlock, |
| 1457 | const sh::InterfaceBlock &fragmentInterfaceBlock) |
| 1458 | { |
| 1459 | const char* blockName = vertexInterfaceBlock.name.c_str(); |
| 1460 | // validate blocks for the same member types |
| 1461 | if (vertexInterfaceBlock.fields.size() != fragmentInterfaceBlock.fields.size()) |
| 1462 | { |
Jamie Madill | f611316 | 2015-05-07 11:49:21 -0400 | [diff] [blame] | 1463 | infoLog << "Types for interface block '" << blockName |
| 1464 | << "' differ between vertex and fragment shaders"; |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1465 | return false; |
| 1466 | } |
| 1467 | if (vertexInterfaceBlock.arraySize != fragmentInterfaceBlock.arraySize) |
| 1468 | { |
Jamie Madill | f611316 | 2015-05-07 11:49:21 -0400 | [diff] [blame] | 1469 | infoLog << "Array sizes differ for interface block '" << blockName |
| 1470 | << "' between vertex and fragment shaders"; |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1471 | return false; |
| 1472 | } |
| 1473 | if (vertexInterfaceBlock.layout != fragmentInterfaceBlock.layout || vertexInterfaceBlock.isRowMajorLayout != fragmentInterfaceBlock.isRowMajorLayout) |
| 1474 | { |
Jamie Madill | f611316 | 2015-05-07 11:49:21 -0400 | [diff] [blame] | 1475 | infoLog << "Layout qualifiers differ for interface block '" << blockName |
| 1476 | << "' between vertex and fragment shaders"; |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1477 | return false; |
| 1478 | } |
Cooper Partin | 4d61f7e | 2015-08-12 10:56:50 -0700 | [diff] [blame] | 1479 | const unsigned int numBlockMembers = |
| 1480 | static_cast<unsigned int>(vertexInterfaceBlock.fields.size()); |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1481 | for (unsigned int blockMemberIndex = 0; blockMemberIndex < numBlockMembers; blockMemberIndex++) |
| 1482 | { |
| 1483 | const sh::InterfaceBlockField &vertexMember = vertexInterfaceBlock.fields[blockMemberIndex]; |
| 1484 | const sh::InterfaceBlockField &fragmentMember = fragmentInterfaceBlock.fields[blockMemberIndex]; |
| 1485 | if (vertexMember.name != fragmentMember.name) |
| 1486 | { |
Jamie Madill | f611316 | 2015-05-07 11:49:21 -0400 | [diff] [blame] | 1487 | infoLog << "Name mismatch for field " << blockMemberIndex |
| 1488 | << " of interface block '" << blockName |
| 1489 | << "': (in vertex: '" << vertexMember.name |
| 1490 | << "', in fragment: '" << fragmentMember.name << "')"; |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1491 | return false; |
| 1492 | } |
| 1493 | std::string memberName = "interface block '" + vertexInterfaceBlock.name + "' member '" + vertexMember.name + "'"; |
| 1494 | if (!linkValidateInterfaceBlockFields(infoLog, memberName, vertexMember, fragmentMember)) |
| 1495 | { |
| 1496 | return false; |
| 1497 | } |
| 1498 | } |
| 1499 | return true; |
| 1500 | } |
| 1501 | |
| 1502 | bool Program::linkValidateVariablesBase(InfoLog &infoLog, const std::string &variableName, const sh::ShaderVariable &vertexVariable, |
| 1503 | const sh::ShaderVariable &fragmentVariable, bool validatePrecision) |
| 1504 | { |
| 1505 | if (vertexVariable.type != fragmentVariable.type) |
| 1506 | { |
Jamie Madill | f611316 | 2015-05-07 11:49:21 -0400 | [diff] [blame] | 1507 | infoLog << "Types for " << variableName << " differ between vertex and fragment shaders"; |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1508 | return false; |
| 1509 | } |
| 1510 | if (vertexVariable.arraySize != fragmentVariable.arraySize) |
| 1511 | { |
Jamie Madill | f611316 | 2015-05-07 11:49:21 -0400 | [diff] [blame] | 1512 | infoLog << "Array sizes for " << variableName << " differ between vertex and fragment shaders"; |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1513 | return false; |
| 1514 | } |
| 1515 | if (validatePrecision && vertexVariable.precision != fragmentVariable.precision) |
| 1516 | { |
Jamie Madill | f611316 | 2015-05-07 11:49:21 -0400 | [diff] [blame] | 1517 | infoLog << "Precisions for " << variableName << " differ between vertex and fragment shaders"; |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1518 | return false; |
| 1519 | } |
| 1520 | |
| 1521 | if (vertexVariable.fields.size() != fragmentVariable.fields.size()) |
| 1522 | { |
Jamie Madill | f611316 | 2015-05-07 11:49:21 -0400 | [diff] [blame] | 1523 | infoLog << "Structure lengths for " << variableName << " differ between vertex and fragment shaders"; |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1524 | return false; |
| 1525 | } |
Cooper Partin | 4d61f7e | 2015-08-12 10:56:50 -0700 | [diff] [blame] | 1526 | const unsigned int numMembers = static_cast<unsigned int>(vertexVariable.fields.size()); |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1527 | for (unsigned int memberIndex = 0; memberIndex < numMembers; memberIndex++) |
| 1528 | { |
| 1529 | const sh::ShaderVariable &vertexMember = vertexVariable.fields[memberIndex]; |
| 1530 | const sh::ShaderVariable &fragmentMember = fragmentVariable.fields[memberIndex]; |
| 1531 | |
| 1532 | if (vertexMember.name != fragmentMember.name) |
| 1533 | { |
Jamie Madill | f611316 | 2015-05-07 11:49:21 -0400 | [diff] [blame] | 1534 | infoLog << "Name mismatch for field '" << memberIndex |
| 1535 | << "' of " << variableName |
| 1536 | << ": (in vertex: '" << vertexMember.name |
| 1537 | << "', in fragment: '" << fragmentMember.name << "')"; |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1538 | return false; |
| 1539 | } |
| 1540 | |
| 1541 | const std::string memberName = variableName.substr(0, variableName.length() - 1) + "." + |
| 1542 | vertexMember.name + "'"; |
| 1543 | |
| 1544 | if (!linkValidateVariablesBase(infoLog, vertexMember.name, vertexMember, fragmentMember, validatePrecision)) |
| 1545 | { |
| 1546 | return false; |
| 1547 | } |
| 1548 | } |
| 1549 | |
| 1550 | return true; |
| 1551 | } |
| 1552 | |
| 1553 | bool Program::linkValidateUniforms(InfoLog &infoLog, const std::string &uniformName, const sh::Uniform &vertexUniform, const sh::Uniform &fragmentUniform) |
| 1554 | { |
Cooper Partin | 1acf438 | 2015-06-12 12:38:57 -0700 | [diff] [blame] | 1555 | #if ANGLE_PROGRAM_LINK_VALIDATE_UNIFORM_PRECISION == ANGLE_ENABLED |
| 1556 | const bool validatePrecision = true; |
| 1557 | #else |
| 1558 | const bool validatePrecision = false; |
| 1559 | #endif |
| 1560 | |
| 1561 | if (!linkValidateVariablesBase(infoLog, uniformName, vertexUniform, fragmentUniform, validatePrecision)) |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1562 | { |
| 1563 | return false; |
| 1564 | } |
| 1565 | |
| 1566 | return true; |
| 1567 | } |
| 1568 | |
| 1569 | bool Program::linkValidateVaryings(InfoLog &infoLog, const std::string &varyingName, const sh::Varying &vertexVarying, const sh::Varying &fragmentVarying) |
| 1570 | { |
| 1571 | if (!linkValidateVariablesBase(infoLog, varyingName, vertexVarying, fragmentVarying, false)) |
| 1572 | { |
| 1573 | return false; |
| 1574 | } |
| 1575 | |
Jamie Madill | e9cc469 | 2015-02-19 16:00:13 -0500 | [diff] [blame] | 1576 | if (!sh::InterpolationTypesMatch(vertexVarying.interpolation, fragmentVarying.interpolation)) |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1577 | { |
Jamie Madill | f611316 | 2015-05-07 11:49:21 -0400 | [diff] [blame] | 1578 | infoLog << "Interpolation types for " << varyingName << " differ between vertex and fragment shaders"; |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1579 | return false; |
| 1580 | } |
| 1581 | |
| 1582 | return true; |
| 1583 | } |
| 1584 | |
| 1585 | bool Program::gatherTransformFeedbackLinkedVaryings(InfoLog &infoLog, const std::vector<LinkedVarying> &linkedVaryings, |
| 1586 | const std::vector<std::string> &transformFeedbackVaryingNames, |
| 1587 | GLenum transformFeedbackBufferMode, |
| 1588 | std::vector<LinkedVarying> *outTransformFeedbackLinkedVaryings, |
| 1589 | const Caps &caps) const |
| 1590 | { |
| 1591 | size_t totalComponents = 0; |
| 1592 | |
| 1593 | // Gather the linked varyings that are used for transform feedback, they should all exist. |
| 1594 | outTransformFeedbackLinkedVaryings->clear(); |
| 1595 | for (size_t i = 0; i < transformFeedbackVaryingNames.size(); i++) |
| 1596 | { |
| 1597 | bool found = false; |
| 1598 | for (size_t j = 0; j < linkedVaryings.size(); j++) |
| 1599 | { |
| 1600 | if (transformFeedbackVaryingNames[i] == linkedVaryings[j].name) |
| 1601 | { |
| 1602 | for (size_t k = 0; k < outTransformFeedbackLinkedVaryings->size(); k++) |
| 1603 | { |
| 1604 | if (outTransformFeedbackLinkedVaryings->at(k).name == linkedVaryings[j].name) |
| 1605 | { |
Jamie Madill | f611316 | 2015-05-07 11:49:21 -0400 | [diff] [blame] | 1606 | infoLog << "Two transform feedback varyings specify the same output variable (" |
| 1607 | << linkedVaryings[j].name << ")."; |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1608 | return false; |
| 1609 | } |
| 1610 | } |
| 1611 | |
| 1612 | size_t componentCount = linkedVaryings[j].semanticIndexCount * 4; |
| 1613 | if (transformFeedbackBufferMode == GL_SEPARATE_ATTRIBS && |
| 1614 | componentCount > caps.maxTransformFeedbackSeparateComponents) |
| 1615 | { |
Jamie Madill | f611316 | 2015-05-07 11:49:21 -0400 | [diff] [blame] | 1616 | infoLog << "Transform feedback varying's " << linkedVaryings[j].name |
| 1617 | << " components (" << componentCount |
| 1618 | << ") exceed the maximum separate components (" |
| 1619 | << caps.maxTransformFeedbackSeparateComponents << ")."; |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1620 | return false; |
| 1621 | } |
| 1622 | |
| 1623 | totalComponents += componentCount; |
| 1624 | |
| 1625 | outTransformFeedbackLinkedVaryings->push_back(linkedVaryings[j]); |
| 1626 | found = true; |
| 1627 | break; |
| 1628 | } |
| 1629 | } |
| 1630 | |
| 1631 | // All transform feedback varyings are expected to exist since packVaryings checks for them. |
| 1632 | ASSERT(found); |
Corentin Wallez | 54c34e0 | 2015-07-02 15:06:55 -0400 | [diff] [blame] | 1633 | UNUSED_ASSERTION_VARIABLE(found); |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1634 | } |
| 1635 | |
Jamie Madill | f611316 | 2015-05-07 11:49:21 -0400 | [diff] [blame] | 1636 | if (transformFeedbackBufferMode == GL_INTERLEAVED_ATTRIBS && |
| 1637 | totalComponents > caps.maxTransformFeedbackInterleavedComponents) |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1638 | { |
Jamie Madill | f611316 | 2015-05-07 11:49:21 -0400 | [diff] [blame] | 1639 | infoLog << "Transform feedback varying total components (" << totalComponents |
| 1640 | << ") exceed the maximum interleaved components (" |
| 1641 | << caps.maxTransformFeedbackInterleavedComponents << ")."; |
Geoff Lang | 7dd2e10 | 2014-11-10 15:19:26 -0500 | [diff] [blame] | 1642 | return false; |
| 1643 | } |
| 1644 | |
| 1645 | return true; |
Geoff Lang | 1b6edcb | 2014-02-03 14:27:56 -0500 | [diff] [blame] | 1646 | } |
| 1647 | |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1648 | } |