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