daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1 | // |
Geoff Lang | cec3590 | 2014-04-16 10:52:36 -0400 | [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 | // Shader.cpp: Implements the gl::Shader class and its derived classes |
| 8 | // VertexShader and FragmentShader. Implements GL shader objects and related |
| 9 | // functionality. [OpenGL ES 2.0.24] section 2.10 page 24 and section 3.8 page 84. |
| 10 | |
Geoff Lang | 2b5420c | 2014-11-19 14:20:15 -0500 | [diff] [blame] | 11 | #include "libANGLE/Shader.h" |
Geoff Lang | 0b7eef7 | 2014-06-12 14:10:47 -0400 | [diff] [blame] | 12 | |
| 13 | #include <sstream> |
| 14 | |
Jamie Madill | 91445bc | 2015-09-23 16:47:53 -0400 | [diff] [blame] | 15 | #include "common/utilities.h" |
| 16 | #include "GLSLANG/ShaderLang.h" |
Jamie Madill | 53ea9cc | 2016-05-17 10:12:52 -0400 | [diff] [blame] | 17 | #include "libANGLE/Caps.h" |
Jamie Madill | 006cbc5 | 2015-09-23 16:47:54 -0400 | [diff] [blame] | 18 | #include "libANGLE/Compiler.h" |
Jamie Madill | 91445bc | 2015-09-23 16:47:53 -0400 | [diff] [blame] | 19 | #include "libANGLE/Constants.h" |
Jamie Madill | 53ea9cc | 2016-05-17 10:12:52 -0400 | [diff] [blame] | 20 | #include "libANGLE/renderer/GLImplFactory.h" |
Jamie Madill | 91445bc | 2015-09-23 16:47:53 -0400 | [diff] [blame] | 21 | #include "libANGLE/renderer/ShaderImpl.h" |
| 22 | #include "libANGLE/ResourceManager.h" |
Bryan Bernhart | 619c833 | 2016-11-09 11:11:41 -0800 | [diff] [blame] | 23 | #include "libANGLE/Context.h" |
Jamie Madill | 91445bc | 2015-09-23 16:47:53 -0400 | [diff] [blame] | 24 | |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 25 | namespace gl |
| 26 | { |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 27 | |
Jamie Madill | 006cbc5 | 2015-09-23 16:47:54 -0400 | [diff] [blame] | 28 | namespace |
| 29 | { |
| 30 | template <typename VarT> |
| 31 | std::vector<VarT> GetActiveShaderVariables(const std::vector<VarT> *variableList) |
| 32 | { |
| 33 | ASSERT(variableList); |
| 34 | std::vector<VarT> result; |
| 35 | for (size_t varIndex = 0; varIndex < variableList->size(); varIndex++) |
| 36 | { |
| 37 | const VarT &var = variableList->at(varIndex); |
| 38 | if (var.staticUse) |
| 39 | { |
| 40 | result.push_back(var); |
| 41 | } |
| 42 | } |
| 43 | return result; |
| 44 | } |
| 45 | |
| 46 | template <typename VarT> |
| 47 | const std::vector<VarT> &GetShaderVariables(const std::vector<VarT> *variableList) |
| 48 | { |
| 49 | ASSERT(variableList); |
| 50 | return *variableList; |
| 51 | } |
| 52 | |
Jamie Madill | 9fc3682 | 2015-11-18 13:08:07 -0500 | [diff] [blame] | 53 | } // anonymous namespace |
| 54 | |
Jamie Madill | 006cbc5 | 2015-09-23 16:47:54 -0400 | [diff] [blame] | 55 | // true if varying x has a higher priority in packing than y |
Jamie Madill | 55c25d0 | 2015-11-18 13:08:08 -0500 | [diff] [blame] | 56 | bool CompareShaderVar(const sh::ShaderVariable &x, const sh::ShaderVariable &y) |
Jamie Madill | 006cbc5 | 2015-09-23 16:47:54 -0400 | [diff] [blame] | 57 | { |
| 58 | if (x.type == y.type) |
| 59 | { |
| 60 | return x.arraySize > y.arraySize; |
| 61 | } |
| 62 | |
| 63 | // Special case for handling structs: we sort these to the end of the list |
| 64 | if (x.type == GL_STRUCT_ANGLEX) |
| 65 | { |
| 66 | return false; |
| 67 | } |
| 68 | |
| 69 | if (y.type == GL_STRUCT_ANGLEX) |
| 70 | { |
| 71 | return true; |
| 72 | } |
| 73 | |
| 74 | return gl::VariableSortOrder(x.type) < gl::VariableSortOrder(y.type); |
| 75 | } |
| 76 | |
Jamie Madill | 34ca4f5 | 2017-06-13 11:49:39 -0400 | [diff] [blame] | 77 | ShaderState::ShaderState(GLenum shaderType) |
| 78 | : mLabel(), |
| 79 | mShaderType(shaderType), |
| 80 | mShaderVersion(100), |
| 81 | mCompileStatus(CompileStatus::NOT_COMPILED) |
Jamie Madill | 91445bc | 2015-09-23 16:47:53 -0400 | [diff] [blame] | 82 | { |
Martin Radev | 4c4c8e7 | 2016-08-04 12:25:34 +0300 | [diff] [blame] | 83 | mLocalSize.fill(-1); |
Jamie Madill | 91445bc | 2015-09-23 16:47:53 -0400 | [diff] [blame] | 84 | } |
| 85 | |
Jamie Madill | 15243d9 | 2016-04-26 13:41:35 -0400 | [diff] [blame] | 86 | ShaderState::~ShaderState() |
Jamie Madill | 91445bc | 2015-09-23 16:47:53 -0400 | [diff] [blame] | 87 | { |
| 88 | } |
| 89 | |
Geoff Lang | 4ddf5af | 2016-12-01 14:30:44 -0500 | [diff] [blame] | 90 | Shader::Shader(ShaderProgramManager *manager, |
Jamie Madill | 7aea7e0 | 2016-05-10 10:39:45 -0400 | [diff] [blame] | 91 | rx::GLImplFactory *implFactory, |
Jamie Madill | 006cbc5 | 2015-09-23 16:47:54 -0400 | [diff] [blame] | 92 | const gl::Limitations &rendererLimitations, |
| 93 | GLenum type, |
| 94 | GLuint handle) |
Jamie Madill | 15243d9 | 2016-04-26 13:41:35 -0400 | [diff] [blame] | 95 | : mState(type), |
| 96 | mImplementation(implFactory->createShader(mState)), |
Jamie Madill | 006cbc5 | 2015-09-23 16:47:54 -0400 | [diff] [blame] | 97 | mRendererLimitations(rendererLimitations), |
Brandon Jones | f05cdee | 2014-08-27 15:24:07 -0700 | [diff] [blame] | 98 | mHandle(handle), |
Corentin Wallez | bc99bb6 | 2015-05-14 17:42:20 -0400 | [diff] [blame] | 99 | mType(type), |
Brandon Jones | f05cdee | 2014-08-27 15:24:07 -0700 | [diff] [blame] | 100 | mRefCount(0), |
| 101 | mDeleteStatus(false), |
Corentin Wallez | bc99bb6 | 2015-05-14 17:42:20 -0400 | [diff] [blame] | 102 | mResourceManager(manager) |
Jamie Madill | e294bb8 | 2014-07-17 14:16:26 -0400 | [diff] [blame] | 103 | { |
Jamie Madill | 91445bc | 2015-09-23 16:47:53 -0400 | [diff] [blame] | 104 | ASSERT(mImplementation); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 105 | } |
| 106 | |
Jamie Madill | 4928b7c | 2017-06-20 12:57:39 -0400 | [diff] [blame] | 107 | void Shader::onDestroy(const gl::Context *context) |
| 108 | { |
| 109 | mBoundCompiler.set(context, nullptr); |
| 110 | mImplementation.reset(nullptr); |
| 111 | delete this; |
| 112 | } |
| 113 | |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 114 | Shader::~Shader() |
| 115 | { |
Jamie Madill | 4928b7c | 2017-06-20 12:57:39 -0400 | [diff] [blame] | 116 | ASSERT(!mImplementation); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 117 | } |
| 118 | |
Geoff Lang | 70d0f49 | 2015-12-10 17:45:46 -0500 | [diff] [blame] | 119 | void Shader::setLabel(const std::string &label) |
| 120 | { |
Jamie Madill | 15243d9 | 2016-04-26 13:41:35 -0400 | [diff] [blame] | 121 | mState.mLabel = label; |
Geoff Lang | 70d0f49 | 2015-12-10 17:45:46 -0500 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | const std::string &Shader::getLabel() const |
| 125 | { |
Jamie Madill | 15243d9 | 2016-04-26 13:41:35 -0400 | [diff] [blame] | 126 | return mState.mLabel; |
Geoff Lang | 70d0f49 | 2015-12-10 17:45:46 -0500 | [diff] [blame] | 127 | } |
| 128 | |
daniel@transgaming.com | 6c78521 | 2010-03-30 03:36:17 +0000 | [diff] [blame] | 129 | GLuint Shader::getHandle() const |
| 130 | { |
| 131 | return mHandle; |
| 132 | } |
| 133 | |
shannon.woods%transgaming.com@gtempaccount.com | 5f33933 | 2013-04-13 03:29:02 +0000 | [diff] [blame] | 134 | void Shader::setSource(GLsizei count, const char *const *string, const GLint *length) |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 135 | { |
Geoff Lang | 536d726 | 2013-08-26 17:04:20 -0400 | [diff] [blame] | 136 | std::ostringstream stream; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 137 | |
| 138 | for (int i = 0; i < count; i++) |
| 139 | { |
Geoff Lang | f60fab6 | 2014-11-24 11:21:20 -0500 | [diff] [blame] | 140 | if (length == nullptr || length[i] < 0) |
| 141 | { |
Jamie Madill | e7cfb3d | 2014-12-03 10:58:56 -0500 | [diff] [blame] | 142 | stream.write(string[i], strlen(string[i])); |
Geoff Lang | f60fab6 | 2014-11-24 11:21:20 -0500 | [diff] [blame] | 143 | } |
| 144 | else |
| 145 | { |
| 146 | stream.write(string[i], length[i]); |
| 147 | } |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 148 | } |
| 149 | |
Jamie Madill | 15243d9 | 2016-04-26 13:41:35 -0400 | [diff] [blame] | 150 | mState.mSource = stream.str(); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 151 | } |
| 152 | |
Jamie Madill | bd044ed | 2017-06-05 12:59:21 -0400 | [diff] [blame] | 153 | int Shader::getInfoLogLength(const Context *context) |
daniel@transgaming.com | cba5057 | 2010-03-28 19:36:09 +0000 | [diff] [blame] | 154 | { |
Jamie Madill | bd044ed | 2017-06-05 12:59:21 -0400 | [diff] [blame] | 155 | resolveCompile(context); |
Jamie Madill | 006cbc5 | 2015-09-23 16:47:54 -0400 | [diff] [blame] | 156 | if (mInfoLog.empty()) |
Jamie Madill | 91445bc | 2015-09-23 16:47:53 -0400 | [diff] [blame] | 157 | { |
| 158 | return 0; |
| 159 | } |
| 160 | |
Jamie Madill | 006cbc5 | 2015-09-23 16:47:54 -0400 | [diff] [blame] | 161 | return (static_cast<int>(mInfoLog.length()) + 1); |
daniel@transgaming.com | cba5057 | 2010-03-28 19:36:09 +0000 | [diff] [blame] | 162 | } |
| 163 | |
Jamie Madill | bd044ed | 2017-06-05 12:59:21 -0400 | [diff] [blame] | 164 | void Shader::getInfoLog(const Context *context, GLsizei bufSize, GLsizei *length, char *infoLog) |
daniel@transgaming.com | cba5057 | 2010-03-28 19:36:09 +0000 | [diff] [blame] | 165 | { |
Jamie Madill | bd044ed | 2017-06-05 12:59:21 -0400 | [diff] [blame] | 166 | resolveCompile(context); |
| 167 | |
daniel@transgaming.com | cba5057 | 2010-03-28 19:36:09 +0000 | [diff] [blame] | 168 | int index = 0; |
| 169 | |
daniel@transgaming.com | 807d8c3 | 2012-04-04 15:06:04 +0000 | [diff] [blame] | 170 | if (bufSize > 0) |
daniel@transgaming.com | cba5057 | 2010-03-28 19:36:09 +0000 | [diff] [blame] | 171 | { |
Jamie Madill | 006cbc5 | 2015-09-23 16:47:54 -0400 | [diff] [blame] | 172 | index = std::min(bufSize - 1, static_cast<GLsizei>(mInfoLog.length())); |
| 173 | memcpy(infoLog, mInfoLog.c_str(), index); |
daniel@transgaming.com | cba5057 | 2010-03-28 19:36:09 +0000 | [diff] [blame] | 174 | |
daniel@transgaming.com | cba5057 | 2010-03-28 19:36:09 +0000 | [diff] [blame] | 175 | infoLog[index] = '\0'; |
| 176 | } |
| 177 | |
| 178 | if (length) |
| 179 | { |
| 180 | *length = index; |
| 181 | } |
| 182 | } |
| 183 | |
| 184 | int Shader::getSourceLength() const |
| 185 | { |
Jamie Madill | 15243d9 | 2016-04-26 13:41:35 -0400 | [diff] [blame] | 186 | return mState.mSource.empty() ? 0 : (static_cast<int>(mState.mSource.length()) + 1); |
daniel@transgaming.com | cba5057 | 2010-03-28 19:36:09 +0000 | [diff] [blame] | 187 | } |
| 188 | |
Jamie Madill | bd044ed | 2017-06-05 12:59:21 -0400 | [diff] [blame] | 189 | int Shader::getTranslatedSourceLength(const Context *context) |
zmo@google.com | a574f78 | 2011-10-03 21:45:23 +0000 | [diff] [blame] | 190 | { |
Jamie Madill | bd044ed | 2017-06-05 12:59:21 -0400 | [diff] [blame] | 191 | resolveCompile(context); |
| 192 | |
Jamie Madill | 15243d9 | 2016-04-26 13:41:35 -0400 | [diff] [blame] | 193 | if (mState.mTranslatedSource.empty()) |
Jamie Madill | 91445bc | 2015-09-23 16:47:53 -0400 | [diff] [blame] | 194 | { |
| 195 | return 0; |
| 196 | } |
| 197 | |
Jamie Madill | 15243d9 | 2016-04-26 13:41:35 -0400 | [diff] [blame] | 198 | return (static_cast<int>(mState.mTranslatedSource.length()) + 1); |
zmo@google.com | a574f78 | 2011-10-03 21:45:23 +0000 | [diff] [blame] | 199 | } |
| 200 | |
Jamie Madill | bd044ed | 2017-06-05 12:59:21 -0400 | [diff] [blame] | 201 | int Shader::getTranslatedSourceWithDebugInfoLength(const Context *context) |
Jamie Madill | 847638a | 2015-11-20 13:01:41 -0500 | [diff] [blame] | 202 | { |
Jamie Madill | bd044ed | 2017-06-05 12:59:21 -0400 | [diff] [blame] | 203 | resolveCompile(context); |
| 204 | |
Jamie Madill | 847638a | 2015-11-20 13:01:41 -0500 | [diff] [blame] | 205 | const std::string &debugInfo = mImplementation->getDebugInfo(); |
| 206 | if (debugInfo.empty()) |
| 207 | { |
| 208 | return 0; |
| 209 | } |
| 210 | |
| 211 | return (static_cast<int>(debugInfo.length()) + 1); |
| 212 | } |
| 213 | |
Jamie Madill | bd044ed | 2017-06-05 12:59:21 -0400 | [diff] [blame] | 214 | // static |
| 215 | void Shader::GetSourceImpl(const std::string &source, |
| 216 | GLsizei bufSize, |
| 217 | GLsizei *length, |
| 218 | char *buffer) |
daniel@transgaming.com | cba5057 | 2010-03-28 19:36:09 +0000 | [diff] [blame] | 219 | { |
| 220 | int index = 0; |
| 221 | |
daniel@transgaming.com | 807d8c3 | 2012-04-04 15:06:04 +0000 | [diff] [blame] | 222 | if (bufSize > 0) |
daniel@transgaming.com | cba5057 | 2010-03-28 19:36:09 +0000 | [diff] [blame] | 223 | { |
Geoff Lang | 536d726 | 2013-08-26 17:04:20 -0400 | [diff] [blame] | 224 | index = std::min(bufSize - 1, static_cast<GLsizei>(source.length())); |
| 225 | memcpy(buffer, source.c_str(), index); |
daniel@transgaming.com | cba5057 | 2010-03-28 19:36:09 +0000 | [diff] [blame] | 226 | |
zmo@google.com | a574f78 | 2011-10-03 21:45:23 +0000 | [diff] [blame] | 227 | buffer[index] = '\0'; |
daniel@transgaming.com | cba5057 | 2010-03-28 19:36:09 +0000 | [diff] [blame] | 228 | } |
| 229 | |
| 230 | if (length) |
| 231 | { |
| 232 | *length = index; |
| 233 | } |
| 234 | } |
| 235 | |
Geoff Lang | 536d726 | 2013-08-26 17:04:20 -0400 | [diff] [blame] | 236 | void Shader::getSource(GLsizei bufSize, GLsizei *length, char *buffer) const |
zmo@google.com | a574f78 | 2011-10-03 21:45:23 +0000 | [diff] [blame] | 237 | { |
Jamie Madill | bd044ed | 2017-06-05 12:59:21 -0400 | [diff] [blame] | 238 | GetSourceImpl(mState.mSource, bufSize, length, buffer); |
zmo@google.com | a574f78 | 2011-10-03 21:45:23 +0000 | [diff] [blame] | 239 | } |
| 240 | |
Jamie Madill | bd044ed | 2017-06-05 12:59:21 -0400 | [diff] [blame] | 241 | void Shader::getTranslatedSource(const Context *context, |
| 242 | GLsizei bufSize, |
| 243 | GLsizei *length, |
| 244 | char *buffer) |
zmo@google.com | a574f78 | 2011-10-03 21:45:23 +0000 | [diff] [blame] | 245 | { |
Jamie Madill | bd044ed | 2017-06-05 12:59:21 -0400 | [diff] [blame] | 246 | GetSourceImpl(getTranslatedSource(context), bufSize, length, buffer); |
zmo@google.com | a574f78 | 2011-10-03 21:45:23 +0000 | [diff] [blame] | 247 | } |
| 248 | |
Jamie Madill | bd044ed | 2017-06-05 12:59:21 -0400 | [diff] [blame] | 249 | const std::string &Shader::getTranslatedSource(const Context *context) |
Tibor den Ouden | 97049c6 | 2014-10-06 21:39:16 +0200 | [diff] [blame] | 250 | { |
Jamie Madill | bd044ed | 2017-06-05 12:59:21 -0400 | [diff] [blame] | 251 | resolveCompile(context); |
| 252 | return mState.mTranslatedSource; |
| 253 | } |
| 254 | |
| 255 | void Shader::getTranslatedSourceWithDebugInfo(const Context *context, |
| 256 | GLsizei bufSize, |
| 257 | GLsizei *length, |
| 258 | char *buffer) |
| 259 | { |
| 260 | resolveCompile(context); |
Jamie Madill | 847638a | 2015-11-20 13:01:41 -0500 | [diff] [blame] | 261 | const std::string &debugInfo = mImplementation->getDebugInfo(); |
Jamie Madill | bd044ed | 2017-06-05 12:59:21 -0400 | [diff] [blame] | 262 | GetSourceImpl(debugInfo, bufSize, length, buffer); |
Tibor den Ouden | 97049c6 | 2014-10-06 21:39:16 +0200 | [diff] [blame] | 263 | } |
| 264 | |
Bryan Bernhart | 619c833 | 2016-11-09 11:11:41 -0800 | [diff] [blame] | 265 | void Shader::compile(const Context *context) |
Jamie Madill | bf9cce2 | 2014-07-18 10:33:09 -0400 | [diff] [blame] | 266 | { |
Jamie Madill | 15243d9 | 2016-04-26 13:41:35 -0400 | [diff] [blame] | 267 | mState.mTranslatedSource.clear(); |
Jamie Madill | 006cbc5 | 2015-09-23 16:47:54 -0400 | [diff] [blame] | 268 | mInfoLog.clear(); |
Jamie Madill | 15243d9 | 2016-04-26 13:41:35 -0400 | [diff] [blame] | 269 | mState.mShaderVersion = 100; |
| 270 | mState.mVaryings.clear(); |
| 271 | mState.mUniforms.clear(); |
| 272 | mState.mInterfaceBlocks.clear(); |
| 273 | mState.mActiveAttributes.clear(); |
| 274 | mState.mActiveOutputVariables.clear(); |
Jamie Madill | 91445bc | 2015-09-23 16:47:53 -0400 | [diff] [blame] | 275 | |
Jamie Madill | 34ca4f5 | 2017-06-13 11:49:39 -0400 | [diff] [blame] | 276 | mState.mCompileStatus = CompileStatus::COMPILE_REQUESTED; |
Jamie Madill | 4928b7c | 2017-06-20 12:57:39 -0400 | [diff] [blame] | 277 | mBoundCompiler.set(context, context->getCompiler()); |
Jamie Madill | bd044ed | 2017-06-05 12:59:21 -0400 | [diff] [blame] | 278 | |
| 279 | // Cache the compile source and options for compilation. Must be done now, since the source |
| 280 | // can change before the link call or another call that resolves the compile. |
Jamie Madill | 006cbc5 | 2015-09-23 16:47:54 -0400 | [diff] [blame] | 281 | |
| 282 | std::stringstream sourceStream; |
| 283 | |
Jamie Madill | bd044ed | 2017-06-05 12:59:21 -0400 | [diff] [blame] | 284 | mLastCompileOptions = |
| 285 | mImplementation->prepareSourceAndReturnOptions(&sourceStream, &mLastCompiledSourcePath); |
| 286 | mLastCompileOptions |= (SH_OBJECT_CODE | SH_VARIABLES); |
| 287 | mLastCompiledSource = sourceStream.str(); |
Jamie Madill | 006cbc5 | 2015-09-23 16:47:54 -0400 | [diff] [blame] | 288 | |
Bryan Bernhart | 619c833 | 2016-11-09 11:11:41 -0800 | [diff] [blame] | 289 | // Add default options to WebGL shaders to prevent unexpected behavior during compilation. |
| 290 | if (context->getExtensions().webglCompatibility) |
| 291 | { |
Jamie Madill | bd044ed | 2017-06-05 12:59:21 -0400 | [diff] [blame] | 292 | mLastCompileOptions |= SH_INIT_GL_POSITION; |
| 293 | mLastCompileOptions |= SH_LIMIT_CALL_STACK_DEPTH; |
| 294 | mLastCompileOptions |= SH_LIMIT_EXPRESSION_COMPLEXITY; |
| 295 | mLastCompileOptions |= SH_ENFORCE_PACKING_RESTRICTIONS; |
Bryan Bernhart | 619c833 | 2016-11-09 11:11:41 -0800 | [diff] [blame] | 296 | } |
| 297 | |
Jamie Madill | 006cbc5 | 2015-09-23 16:47:54 -0400 | [diff] [blame] | 298 | // Some targets (eg D3D11 Feature Level 9_3 and below) do not support non-constant loop indexes |
| 299 | // in fragment shaders. Shader compilation will fail. To provide a better error message we can |
| 300 | // instruct the compiler to pre-validate. |
| 301 | if (mRendererLimitations.shadersRequireIndexedLoopValidation) |
| 302 | { |
Jamie Madill | bd044ed | 2017-06-05 12:59:21 -0400 | [diff] [blame] | 303 | mLastCompileOptions |= SH_VALIDATE_LOOP_INDEXING; |
Jamie Madill | 006cbc5 | 2015-09-23 16:47:54 -0400 | [diff] [blame] | 304 | } |
Jamie Madill | bd044ed | 2017-06-05 12:59:21 -0400 | [diff] [blame] | 305 | } |
Jamie Madill | 006cbc5 | 2015-09-23 16:47:54 -0400 | [diff] [blame] | 306 | |
Jamie Madill | bd044ed | 2017-06-05 12:59:21 -0400 | [diff] [blame] | 307 | void Shader::resolveCompile(const Context *context) |
| 308 | { |
Jamie Madill | 34ca4f5 | 2017-06-13 11:49:39 -0400 | [diff] [blame] | 309 | if (!mState.compilePending()) |
Jamie Madill | d2c52e3 | 2015-10-14 17:07:05 -0400 | [diff] [blame] | 310 | { |
Jamie Madill | bd044ed | 2017-06-05 12:59:21 -0400 | [diff] [blame] | 311 | return; |
Jamie Madill | d2c52e3 | 2015-10-14 17:07:05 -0400 | [diff] [blame] | 312 | } |
| 313 | |
Jamie Madill | bd044ed | 2017-06-05 12:59:21 -0400 | [diff] [blame] | 314 | ASSERT(mBoundCompiler.get()); |
| 315 | ShHandle compilerHandle = mBoundCompiler->getCompilerHandle(mState.mShaderType); |
Jamie Madill | d2c52e3 | 2015-10-14 17:07:05 -0400 | [diff] [blame] | 316 | |
Jamie Madill | bd044ed | 2017-06-05 12:59:21 -0400 | [diff] [blame] | 317 | std::vector<const char *> srcStrings; |
Jamie Madill | 006cbc5 | 2015-09-23 16:47:54 -0400 | [diff] [blame] | 318 | |
Jamie Madill | bd044ed | 2017-06-05 12:59:21 -0400 | [diff] [blame] | 319 | if (!mLastCompiledSourcePath.empty()) |
| 320 | { |
| 321 | srcStrings.push_back(mLastCompiledSourcePath.c_str()); |
| 322 | } |
| 323 | |
| 324 | srcStrings.push_back(mLastCompiledSource.c_str()); |
| 325 | |
| 326 | if (!sh::Compile(compilerHandle, &srcStrings[0], srcStrings.size(), mLastCompileOptions)) |
Jamie Madill | 006cbc5 | 2015-09-23 16:47:54 -0400 | [diff] [blame] | 327 | { |
Jamie Madill | acb4b81 | 2016-11-07 13:50:29 -0500 | [diff] [blame] | 328 | mInfoLog = sh::GetInfoLog(compilerHandle); |
Yuly Novikov | d73f852 | 2017-01-13 17:48:57 -0500 | [diff] [blame] | 329 | WARN() << std::endl << mInfoLog; |
Jamie Madill | 34ca4f5 | 2017-06-13 11:49:39 -0400 | [diff] [blame] | 330 | mState.mCompileStatus = CompileStatus::NOT_COMPILED; |
Jamie Madill | 006cbc5 | 2015-09-23 16:47:54 -0400 | [diff] [blame] | 331 | return; |
| 332 | } |
| 333 | |
Jamie Madill | acb4b81 | 2016-11-07 13:50:29 -0500 | [diff] [blame] | 334 | mState.mTranslatedSource = sh::GetObjectCode(compilerHandle); |
Jamie Madill | 006cbc5 | 2015-09-23 16:47:54 -0400 | [diff] [blame] | 335 | |
Jamie Madill | bd044ed | 2017-06-05 12:59:21 -0400 | [diff] [blame] | 336 | #if !defined(NDEBUG) |
Jamie Madill | 006cbc5 | 2015-09-23 16:47:54 -0400 | [diff] [blame] | 337 | // Prefix translated shader with commented out un-translated shader. |
| 338 | // Useful in diagnostics tools which capture the shader source. |
| 339 | std::ostringstream shaderStream; |
| 340 | shaderStream << "// GLSL\n"; |
| 341 | shaderStream << "//\n"; |
| 342 | |
Geoff Lang | 9e1bf10 | 2017-03-28 15:10:48 -0400 | [diff] [blame] | 343 | std::istringstream inputSourceStream(mState.mSource); |
| 344 | std::string line; |
| 345 | while (std::getline(inputSourceStream, line)) |
Jamie Madill | 006cbc5 | 2015-09-23 16:47:54 -0400 | [diff] [blame] | 346 | { |
Geoff Lang | 9e1bf10 | 2017-03-28 15:10:48 -0400 | [diff] [blame] | 347 | // Remove null characters from the source line |
| 348 | line.erase(std::remove(line.begin(), line.end(), '\0'), line.end()); |
Jamie Madill | 006cbc5 | 2015-09-23 16:47:54 -0400 | [diff] [blame] | 349 | |
Geoff Lang | 9e1bf10 | 2017-03-28 15:10:48 -0400 | [diff] [blame] | 350 | shaderStream << "// " << line; |
Jamie Madill | 006cbc5 | 2015-09-23 16:47:54 -0400 | [diff] [blame] | 351 | } |
| 352 | shaderStream << "\n\n"; |
Jamie Madill | 15243d9 | 2016-04-26 13:41:35 -0400 | [diff] [blame] | 353 | shaderStream << mState.mTranslatedSource; |
| 354 | mState.mTranslatedSource = shaderStream.str(); |
Jamie Madill | bd044ed | 2017-06-05 12:59:21 -0400 | [diff] [blame] | 355 | #endif // !defined(NDEBUG) |
Jamie Madill | 006cbc5 | 2015-09-23 16:47:54 -0400 | [diff] [blame] | 356 | |
| 357 | // Gather the shader information |
Jamie Madill | acb4b81 | 2016-11-07 13:50:29 -0500 | [diff] [blame] | 358 | mState.mShaderVersion = sh::GetShaderVersion(compilerHandle); |
Jamie Madill | 006cbc5 | 2015-09-23 16:47:54 -0400 | [diff] [blame] | 359 | |
Jamie Madill | acb4b81 | 2016-11-07 13:50:29 -0500 | [diff] [blame] | 360 | mState.mVaryings = GetShaderVariables(sh::GetVaryings(compilerHandle)); |
| 361 | mState.mUniforms = GetShaderVariables(sh::GetUniforms(compilerHandle)); |
| 362 | mState.mInterfaceBlocks = GetShaderVariables(sh::GetInterfaceBlocks(compilerHandle)); |
Jamie Madill | 006cbc5 | 2015-09-23 16:47:54 -0400 | [diff] [blame] | 363 | |
Martin Radev | 4c4c8e7 | 2016-08-04 12:25:34 +0300 | [diff] [blame] | 364 | switch (mState.mShaderType) |
Jamie Madill | 006cbc5 | 2015-09-23 16:47:54 -0400 | [diff] [blame] | 365 | { |
Martin Radev | 4c4c8e7 | 2016-08-04 12:25:34 +0300 | [diff] [blame] | 366 | case GL_COMPUTE_SHADER: |
| 367 | { |
Jamie Madill | acb4b81 | 2016-11-07 13:50:29 -0500 | [diff] [blame] | 368 | mState.mLocalSize = sh::GetComputeShaderLocalGroupSize(compilerHandle); |
Martin Radev | 4c4c8e7 | 2016-08-04 12:25:34 +0300 | [diff] [blame] | 369 | break; |
| 370 | } |
| 371 | case GL_VERTEX_SHADER: |
| 372 | { |
Jamie Madill | acb4b81 | 2016-11-07 13:50:29 -0500 | [diff] [blame] | 373 | mState.mActiveAttributes = GetActiveShaderVariables(sh::GetAttributes(compilerHandle)); |
Martin Radev | 4c4c8e7 | 2016-08-04 12:25:34 +0300 | [diff] [blame] | 374 | break; |
| 375 | } |
| 376 | case GL_FRAGMENT_SHADER: |
| 377 | { |
| 378 | // TODO(jmadill): Figure out why we only sort in the FS, and if we need to. |
| 379 | std::sort(mState.mVaryings.begin(), mState.mVaryings.end(), CompareShaderVar); |
| 380 | mState.mActiveOutputVariables = |
Jamie Madill | acb4b81 | 2016-11-07 13:50:29 -0500 | [diff] [blame] | 381 | GetActiveShaderVariables(sh::GetOutputVariables(compilerHandle)); |
Martin Radev | 4c4c8e7 | 2016-08-04 12:25:34 +0300 | [diff] [blame] | 382 | break; |
| 383 | } |
| 384 | default: |
| 385 | UNREACHABLE(); |
Jamie Madill | 006cbc5 | 2015-09-23 16:47:54 -0400 | [diff] [blame] | 386 | } |
| 387 | |
Jamie Madill | 15243d9 | 2016-04-26 13:41:35 -0400 | [diff] [blame] | 388 | ASSERT(!mState.mTranslatedSource.empty()); |
Jamie Madill | 006cbc5 | 2015-09-23 16:47:54 -0400 | [diff] [blame] | 389 | |
Jamie Madill | bd044ed | 2017-06-05 12:59:21 -0400 | [diff] [blame] | 390 | bool success = mImplementation->postTranslateCompile(mBoundCompiler.get(), &mInfoLog); |
Jamie Madill | 34ca4f5 | 2017-06-13 11:49:39 -0400 | [diff] [blame] | 391 | mState.mCompileStatus = success ? CompileStatus::COMPILED : CompileStatus::NOT_COMPILED; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 392 | } |
| 393 | |
daniel@transgaming.com | da13f3e | 2010-07-28 19:20:56 +0000 | [diff] [blame] | 394 | void Shader::addRef() |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 395 | { |
daniel@transgaming.com | da13f3e | 2010-07-28 19:20:56 +0000 | [diff] [blame] | 396 | mRefCount++; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 397 | } |
| 398 | |
Jamie Madill | 6c1f671 | 2017-02-14 19:08:04 -0500 | [diff] [blame] | 399 | void Shader::release(const Context *context) |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 400 | { |
daniel@transgaming.com | da13f3e | 2010-07-28 19:20:56 +0000 | [diff] [blame] | 401 | mRefCount--; |
daniel@transgaming.com | 71cd868 | 2010-04-29 03:35:25 +0000 | [diff] [blame] | 402 | |
daniel@transgaming.com | da13f3e | 2010-07-28 19:20:56 +0000 | [diff] [blame] | 403 | if (mRefCount == 0 && mDeleteStatus) |
daniel@transgaming.com | 71cd868 | 2010-04-29 03:35:25 +0000 | [diff] [blame] | 404 | { |
Jamie Madill | 6c1f671 | 2017-02-14 19:08:04 -0500 | [diff] [blame] | 405 | mResourceManager->deleteShader(context, mHandle); |
daniel@transgaming.com | 71cd868 | 2010-04-29 03:35:25 +0000 | [diff] [blame] | 406 | } |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 407 | } |
| 408 | |
daniel@transgaming.com | da13f3e | 2010-07-28 19:20:56 +0000 | [diff] [blame] | 409 | unsigned int Shader::getRefCount() const |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 410 | { |
daniel@transgaming.com | da13f3e | 2010-07-28 19:20:56 +0000 | [diff] [blame] | 411 | return mRefCount; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 412 | } |
| 413 | |
daniel@transgaming.com | cba5057 | 2010-03-28 19:36:09 +0000 | [diff] [blame] | 414 | bool Shader::isFlaggedForDeletion() const |
| 415 | { |
| 416 | return mDeleteStatus; |
| 417 | } |
| 418 | |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 419 | void Shader::flagForDeletion() |
| 420 | { |
| 421 | mDeleteStatus = true; |
| 422 | } |
| 423 | |
Jamie Madill | bd044ed | 2017-06-05 12:59:21 -0400 | [diff] [blame] | 424 | bool Shader::isCompiled(const Context *context) |
Jamie Madill | 80a6fc0 | 2015-08-21 16:53:16 -0400 | [diff] [blame] | 425 | { |
Jamie Madill | bd044ed | 2017-06-05 12:59:21 -0400 | [diff] [blame] | 426 | resolveCompile(context); |
Jamie Madill | 34ca4f5 | 2017-06-13 11:49:39 -0400 | [diff] [blame] | 427 | return mState.mCompileStatus == CompileStatus::COMPILED; |
Jamie Madill | bd044ed | 2017-06-05 12:59:21 -0400 | [diff] [blame] | 428 | } |
| 429 | |
| 430 | int Shader::getShaderVersion(const Context *context) |
| 431 | { |
| 432 | resolveCompile(context); |
Jamie Madill | 15243d9 | 2016-04-26 13:41:35 -0400 | [diff] [blame] | 433 | return mState.mShaderVersion; |
Jamie Madill | 80a6fc0 | 2015-08-21 16:53:16 -0400 | [diff] [blame] | 434 | } |
| 435 | |
Jamie Madill | bd044ed | 2017-06-05 12:59:21 -0400 | [diff] [blame] | 436 | const std::vector<sh::Varying> &Shader::getVaryings(const Context *context) |
Jamie Madill | d15250e | 2014-09-03 09:40:44 -0400 | [diff] [blame] | 437 | { |
Jamie Madill | bd044ed | 2017-06-05 12:59:21 -0400 | [diff] [blame] | 438 | resolveCompile(context); |
Jamie Madill | 15243d9 | 2016-04-26 13:41:35 -0400 | [diff] [blame] | 439 | return mState.getVaryings(); |
Jamie Madill | d15250e | 2014-09-03 09:40:44 -0400 | [diff] [blame] | 440 | } |
| 441 | |
Jamie Madill | bd044ed | 2017-06-05 12:59:21 -0400 | [diff] [blame] | 442 | const std::vector<sh::Uniform> &Shader::getUniforms(const Context *context) |
Jamie Madill | d15250e | 2014-09-03 09:40:44 -0400 | [diff] [blame] | 443 | { |
Jamie Madill | bd044ed | 2017-06-05 12:59:21 -0400 | [diff] [blame] | 444 | resolveCompile(context); |
Jamie Madill | 15243d9 | 2016-04-26 13:41:35 -0400 | [diff] [blame] | 445 | return mState.getUniforms(); |
Jamie Madill | d15250e | 2014-09-03 09:40:44 -0400 | [diff] [blame] | 446 | } |
| 447 | |
Jamie Madill | bd044ed | 2017-06-05 12:59:21 -0400 | [diff] [blame] | 448 | const std::vector<sh::InterfaceBlock> &Shader::getInterfaceBlocks(const Context *context) |
Jamie Madill | d15250e | 2014-09-03 09:40:44 -0400 | [diff] [blame] | 449 | { |
Jamie Madill | bd044ed | 2017-06-05 12:59:21 -0400 | [diff] [blame] | 450 | resolveCompile(context); |
Jamie Madill | 15243d9 | 2016-04-26 13:41:35 -0400 | [diff] [blame] | 451 | return mState.getInterfaceBlocks(); |
Jamie Madill | d15250e | 2014-09-03 09:40:44 -0400 | [diff] [blame] | 452 | } |
| 453 | |
Jamie Madill | bd044ed | 2017-06-05 12:59:21 -0400 | [diff] [blame] | 454 | const std::vector<sh::Attribute> &Shader::getActiveAttributes(const Context *context) |
Jamie Madill | d15250e | 2014-09-03 09:40:44 -0400 | [diff] [blame] | 455 | { |
Jamie Madill | bd044ed | 2017-06-05 12:59:21 -0400 | [diff] [blame] | 456 | resolveCompile(context); |
Jamie Madill | 15243d9 | 2016-04-26 13:41:35 -0400 | [diff] [blame] | 457 | return mState.getActiveAttributes(); |
Jamie Madill | d15250e | 2014-09-03 09:40:44 -0400 | [diff] [blame] | 458 | } |
| 459 | |
Jamie Madill | bd044ed | 2017-06-05 12:59:21 -0400 | [diff] [blame] | 460 | const std::vector<sh::OutputVariable> &Shader::getActiveOutputVariables(const Context *context) |
Jamie Madill | d15250e | 2014-09-03 09:40:44 -0400 | [diff] [blame] | 461 | { |
Jamie Madill | bd044ed | 2017-06-05 12:59:21 -0400 | [diff] [blame] | 462 | resolveCompile(context); |
Jamie Madill | 15243d9 | 2016-04-26 13:41:35 -0400 | [diff] [blame] | 463 | return mState.getActiveOutputVariables(); |
Jamie Madill | d15250e | 2014-09-03 09:40:44 -0400 | [diff] [blame] | 464 | } |
| 465 | |
Jamie Madill | bd044ed | 2017-06-05 12:59:21 -0400 | [diff] [blame] | 466 | const sh::WorkGroupSize &Shader::getWorkGroupSize(const Context *context) |
| 467 | { |
| 468 | resolveCompile(context); |
| 469 | return mState.mLocalSize; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 470 | } |
Jamie Madill | bd044ed | 2017-06-05 12:59:21 -0400 | [diff] [blame] | 471 | |
| 472 | } // namespace gl |