Jamie Madill | 05a80ce | 2013-06-20 11:55:49 -0400 | [diff] [blame] | 1 | // |
| 2 | // Copyright (c) 2013 The ANGLE Project Authors. All rights reserved. |
| 3 | // Use of this source code is governed by a BSD-style license that can be |
| 4 | // found in the LICENSE file. |
| 5 | // |
Olli Etuaho | 12b0b39 | 2017-05-30 13:22:31 +0300 | [diff] [blame] | 6 | // ValidateOutputs validates fragment shader outputs. It checks for conflicting locations, |
| 7 | // out-of-range locations, that locations are specified when using multiple outputs, and YUV output |
| 8 | // validity. |
Jamie Madill | 05a80ce | 2013-06-20 11:55:49 -0400 | [diff] [blame] | 9 | |
Geoff Lang | 1773282 | 2013-08-29 13:46:49 -0400 | [diff] [blame] | 10 | #include "compiler/translator/ValidateOutputs.h" |
Olli Etuaho | 12b0b39 | 2017-05-30 13:22:31 +0300 | [diff] [blame] | 11 | |
| 12 | #include <set> |
| 13 | |
Geoff Lang | 1773282 | 2013-08-29 13:46:49 -0400 | [diff] [blame] | 14 | #include "compiler/translator/InfoSink.h" |
Jamie Madill | 6b9cb25 | 2013-10-17 10:45:47 -0400 | [diff] [blame] | 15 | #include "compiler/translator/ParseContext.h" |
Olli Etuaho | c26214d | 2018-03-16 10:43:11 +0200 | [diff] [blame] | 16 | #include "compiler/translator/tree_util/IntermTraverse.h" |
Jamie Madill | 05a80ce | 2013-06-20 11:55:49 -0400 | [diff] [blame] | 17 | |
Jamie Madill | 45bcc78 | 2016-11-07 13:58:48 -0500 | [diff] [blame] | 18 | namespace sh |
| 19 | { |
| 20 | |
Kimmo Kinnunen | b18609b | 2015-07-16 14:13:11 +0300 | [diff] [blame] | 21 | namespace |
| 22 | { |
Olli Etuaho | 8b5e8fd | 2017-12-15 14:59:15 +0200 | [diff] [blame] | 23 | |
Olli Etuaho | 77ba408 | 2016-12-16 12:01:18 +0000 | [diff] [blame] | 24 | void error(const TIntermSymbol &symbol, const char *reason, TDiagnostics *diagnostics) |
Kimmo Kinnunen | b18609b | 2015-07-16 14:13:11 +0300 | [diff] [blame] | 25 | { |
Olli Etuaho | fbb1c79 | 2018-01-19 16:26:59 +0200 | [diff] [blame] | 26 | diagnostics->error(symbol.getLine(), reason, symbol.getName().data()); |
Kimmo Kinnunen | b18609b | 2015-07-16 14:13:11 +0300 | [diff] [blame] | 27 | } |
| 28 | |
Olli Etuaho | 12b0b39 | 2017-05-30 13:22:31 +0300 | [diff] [blame] | 29 | class ValidateOutputsTraverser : public TIntermTraverser |
| 30 | { |
| 31 | public: |
| 32 | ValidateOutputsTraverser(const TExtensionBehavior &extBehavior, int maxDrawBuffers); |
Kimmo Kinnunen | b18609b | 2015-07-16 14:13:11 +0300 | [diff] [blame] | 33 | |
Olli Etuaho | 12b0b39 | 2017-05-30 13:22:31 +0300 | [diff] [blame] | 34 | void validate(TDiagnostics *diagnostics) const; |
| 35 | |
| 36 | void visitSymbol(TIntermSymbol *) override; |
| 37 | |
| 38 | private: |
| 39 | int mMaxDrawBuffers; |
| 40 | bool mAllowUnspecifiedOutputLocationResolution; |
| 41 | bool mUsesFragDepth; |
| 42 | |
| 43 | typedef std::vector<TIntermSymbol *> OutputVector; |
| 44 | OutputVector mOutputs; |
| 45 | OutputVector mUnspecifiedLocationOutputs; |
| 46 | OutputVector mYuvOutputs; |
Olli Etuaho | aaa55bf | 2017-12-28 11:20:30 +0200 | [diff] [blame] | 47 | std::set<int> mVisitedSymbols; // Visited symbol ids. |
Olli Etuaho | 12b0b39 | 2017-05-30 13:22:31 +0300 | [diff] [blame] | 48 | }; |
| 49 | |
| 50 | ValidateOutputsTraverser::ValidateOutputsTraverser(const TExtensionBehavior &extBehavior, |
| 51 | int maxDrawBuffers) |
Olli Etuaho | 3d0d9a4 | 2015-06-01 12:16:36 +0300 | [diff] [blame] | 52 | : TIntermTraverser(true, false, false), |
Jamie Madill | 05a80ce | 2013-06-20 11:55:49 -0400 | [diff] [blame] | 53 | mMaxDrawBuffers(maxDrawBuffers), |
Kimmo Kinnunen | b18609b | 2015-07-16 14:13:11 +0300 | [diff] [blame] | 54 | mAllowUnspecifiedOutputLocationResolution( |
Olli Etuaho | 2a1e8f9 | 2017-07-14 11:49:36 +0300 | [diff] [blame] | 55 | IsExtensionEnabled(extBehavior, TExtension::EXT_blend_func_extended)), |
Andrei Volykhin | a552707 | 2017-03-22 16:46:30 +0300 | [diff] [blame] | 56 | mUsesFragDepth(false) |
Jamie Madill | b980c56 | 2018-11-27 11:34:27 -0500 | [diff] [blame] | 57 | {} |
Jamie Madill | 05a80ce | 2013-06-20 11:55:49 -0400 | [diff] [blame] | 58 | |
Olli Etuaho | 12b0b39 | 2017-05-30 13:22:31 +0300 | [diff] [blame] | 59 | void ValidateOutputsTraverser::visitSymbol(TIntermSymbol *symbol) |
Jamie Madill | 05a80ce | 2013-06-20 11:55:49 -0400 | [diff] [blame] | 60 | { |
Olli Etuaho | aaa55bf | 2017-12-28 11:20:30 +0200 | [diff] [blame] | 61 | if (symbol->variable().symbolType() == SymbolType::Empty) |
Jamie Madill | 05a80ce | 2013-06-20 11:55:49 -0400 | [diff] [blame] | 62 | return; |
| 63 | |
Olli Etuaho | aaa55bf | 2017-12-28 11:20:30 +0200 | [diff] [blame] | 64 | if (mVisitedSymbols.count(symbol->uniqueId().get()) == 1) |
| 65 | return; |
Jamie Madill | 05a80ce | 2013-06-20 11:55:49 -0400 | [diff] [blame] | 66 | |
Olli Etuaho | aaa55bf | 2017-12-28 11:20:30 +0200 | [diff] [blame] | 67 | mVisitedSymbols.insert(symbol->uniqueId().get()); |
| 68 | |
| 69 | TQualifier qualifier = symbol->getQualifier(); |
Jamie Madill | 1957181 | 2013-08-12 15:26:34 -0700 | [diff] [blame] | 70 | if (qualifier == EvqFragmentOut) |
Jamie Madill | 05a80ce | 2013-06-20 11:55:49 -0400 | [diff] [blame] | 71 | { |
Andrei Volykhin | a552707 | 2017-03-22 16:46:30 +0300 | [diff] [blame] | 72 | if (symbol->getType().getLayoutQualifier().location != -1) |
Jamie Madill | 05a80ce | 2013-06-20 11:55:49 -0400 | [diff] [blame] | 73 | { |
Kimmo Kinnunen | b18609b | 2015-07-16 14:13:11 +0300 | [diff] [blame] | 74 | mOutputs.push_back(symbol); |
Jamie Madill | 05a80ce | 2013-06-20 11:55:49 -0400 | [diff] [blame] | 75 | } |
Andrei Volykhin | a552707 | 2017-03-22 16:46:30 +0300 | [diff] [blame] | 76 | else if (symbol->getType().getLayoutQualifier().yuv == true) |
| 77 | { |
| 78 | mYuvOutputs.push_back(symbol); |
| 79 | } |
| 80 | else |
| 81 | { |
| 82 | mUnspecifiedLocationOutputs.push_back(symbol); |
| 83 | } |
| 84 | } |
| 85 | else if (qualifier == EvqFragDepth || qualifier == EvqFragDepthEXT) |
| 86 | { |
| 87 | mUsesFragDepth = true; |
Jamie Madill | 05a80ce | 2013-06-20 11:55:49 -0400 | [diff] [blame] | 88 | } |
| 89 | } |
| 90 | |
Olli Etuaho | 12b0b39 | 2017-05-30 13:22:31 +0300 | [diff] [blame] | 91 | void ValidateOutputsTraverser::validate(TDiagnostics *diagnostics) const |
Jamie Madill | 05a80ce | 2013-06-20 11:55:49 -0400 | [diff] [blame] | 92 | { |
Olli Etuaho | 77ba408 | 2016-12-16 12:01:18 +0000 | [diff] [blame] | 93 | ASSERT(diagnostics); |
Olli Etuaho | a78092c | 2018-09-26 14:16:13 +0300 | [diff] [blame] | 94 | OutputVector validOutputs(mMaxDrawBuffers, nullptr); |
| 95 | OutputVector validSecondaryOutputs(mMaxDrawBuffers, nullptr); |
Kimmo Kinnunen | b18609b | 2015-07-16 14:13:11 +0300 | [diff] [blame] | 96 | |
| 97 | for (const auto &symbol : mOutputs) |
| 98 | { |
Jamie Madill | b980c56 | 2018-11-27 11:34:27 -0500 | [diff] [blame] | 99 | const TType &type = symbol->getType(); |
Olli Etuaho | 96f6adf | 2017-08-16 11:18:54 +0300 | [diff] [blame] | 100 | ASSERT(!type.isArrayOfArrays()); // Disallowed in GLSL ES 3.10 section 4.3.6. |
| 101 | const size_t elementCount = |
| 102 | static_cast<size_t>(type.isArray() ? type.getOutermostArraySize() : 1u); |
Jamie Madill | b980c56 | 2018-11-27 11:34:27 -0500 | [diff] [blame] | 103 | const size_t location = static_cast<size_t>(type.getLayoutQualifier().location); |
Kimmo Kinnunen | b18609b | 2015-07-16 14:13:11 +0300 | [diff] [blame] | 104 | |
| 105 | ASSERT(type.getLayoutQualifier().location != -1); |
| 106 | |
Olli Etuaho | 0ca0975 | 2018-09-24 11:00:50 +0300 | [diff] [blame] | 107 | OutputVector *validOutputsToUse = &validOutputs; |
Olli Etuaho | a78092c | 2018-09-26 14:16:13 +0300 | [diff] [blame] | 108 | // The default index is 0, so we only assign the output to secondary outputs in case the |
| 109 | // index is explicitly set to 1. |
| 110 | if (type.getLayoutQualifier().index == 1) |
| 111 | { |
Olli Etuaho | 0ca0975 | 2018-09-24 11:00:50 +0300 | [diff] [blame] | 112 | validOutputsToUse = &validSecondaryOutputs; |
Olli Etuaho | a78092c | 2018-09-26 14:16:13 +0300 | [diff] [blame] | 113 | } |
| 114 | |
Olli Etuaho | 0ca0975 | 2018-09-24 11:00:50 +0300 | [diff] [blame] | 115 | if (location + elementCount <= validOutputsToUse->size()) |
Kimmo Kinnunen | b18609b | 2015-07-16 14:13:11 +0300 | [diff] [blame] | 116 | { |
| 117 | for (size_t elementIndex = 0; elementIndex < elementCount; elementIndex++) |
| 118 | { |
| 119 | const size_t offsetLocation = location + elementIndex; |
Olli Etuaho | 0ca0975 | 2018-09-24 11:00:50 +0300 | [diff] [blame] | 120 | if ((*validOutputsToUse)[offsetLocation]) |
Kimmo Kinnunen | b18609b | 2015-07-16 14:13:11 +0300 | [diff] [blame] | 121 | { |
Jonah Ryan-Davis | 5f662c0 | 2019-01-31 13:53:59 -0500 | [diff] [blame^] | 122 | std::stringstream strstr = sh::InitializeStream<std::stringstream>(); |
Kimmo Kinnunen | b18609b | 2015-07-16 14:13:11 +0300 | [diff] [blame] | 123 | strstr << "conflicting output locations with previously defined output '" |
Olli Etuaho | 0ca0975 | 2018-09-24 11:00:50 +0300 | [diff] [blame] | 124 | << (*validOutputsToUse)[offsetLocation]->getName() << "'"; |
Olli Etuaho | 77ba408 | 2016-12-16 12:01:18 +0000 | [diff] [blame] | 125 | error(*symbol, strstr.str().c_str(), diagnostics); |
Kimmo Kinnunen | b18609b | 2015-07-16 14:13:11 +0300 | [diff] [blame] | 126 | } |
| 127 | else |
| 128 | { |
Olli Etuaho | 0ca0975 | 2018-09-24 11:00:50 +0300 | [diff] [blame] | 129 | (*validOutputsToUse)[offsetLocation] = symbol; |
Kimmo Kinnunen | b18609b | 2015-07-16 14:13:11 +0300 | [diff] [blame] | 130 | } |
| 131 | } |
| 132 | } |
| 133 | else |
| 134 | { |
| 135 | if (elementCount > 0) |
| 136 | { |
Olli Etuaho | 77ba408 | 2016-12-16 12:01:18 +0000 | [diff] [blame] | 137 | error(*symbol, |
Kimmo Kinnunen | b18609b | 2015-07-16 14:13:11 +0300 | [diff] [blame] | 138 | elementCount > 1 ? "output array locations would exceed MAX_DRAW_BUFFERS" |
Olli Etuaho | 77ba408 | 2016-12-16 12:01:18 +0000 | [diff] [blame] | 139 | : "output location must be < MAX_DRAW_BUFFERS", |
| 140 | diagnostics); |
Kimmo Kinnunen | b18609b | 2015-07-16 14:13:11 +0300 | [diff] [blame] | 141 | } |
| 142 | } |
| 143 | } |
| 144 | |
| 145 | if (!mAllowUnspecifiedOutputLocationResolution && |
| 146 | ((!mOutputs.empty() && !mUnspecifiedLocationOutputs.empty()) || |
| 147 | mUnspecifiedLocationOutputs.size() > 1)) |
| 148 | { |
| 149 | for (const auto &symbol : mUnspecifiedLocationOutputs) |
| 150 | { |
Olli Etuaho | 77ba408 | 2016-12-16 12:01:18 +0000 | [diff] [blame] | 151 | error(*symbol, |
| 152 | "must explicitly specify all locations when using multiple fragment outputs", |
| 153 | diagnostics); |
Kimmo Kinnunen | b18609b | 2015-07-16 14:13:11 +0300 | [diff] [blame] | 154 | } |
| 155 | } |
Andrei Volykhin | a552707 | 2017-03-22 16:46:30 +0300 | [diff] [blame] | 156 | |
| 157 | if (!mYuvOutputs.empty() && (mYuvOutputs.size() > 1 || mUsesFragDepth || !mOutputs.empty() || |
| 158 | !mUnspecifiedLocationOutputs.empty())) |
| 159 | { |
| 160 | for (const auto &symbol : mYuvOutputs) |
| 161 | { |
| 162 | error(*symbol, |
| 163 | "not allowed to specify yuv qualifier when using depth or multiple color " |
| 164 | "fragment outputs", |
| 165 | diagnostics); |
| 166 | } |
| 167 | } |
Jamie Madill | 05a80ce | 2013-06-20 11:55:49 -0400 | [diff] [blame] | 168 | } |
Jamie Madill | 45bcc78 | 2016-11-07 13:58:48 -0500 | [diff] [blame] | 169 | |
Olli Etuaho | 12b0b39 | 2017-05-30 13:22:31 +0300 | [diff] [blame] | 170 | } // anonymous namespace |
| 171 | |
| 172 | bool ValidateOutputs(TIntermBlock *root, |
| 173 | const TExtensionBehavior &extBehavior, |
| 174 | int maxDrawBuffers, |
| 175 | TDiagnostics *diagnostics) |
| 176 | { |
| 177 | ValidateOutputsTraverser validateOutputs(extBehavior, maxDrawBuffers); |
| 178 | root->traverse(&validateOutputs); |
| 179 | int numErrorsBefore = diagnostics->numErrors(); |
| 180 | validateOutputs.validate(diagnostics); |
| 181 | return (diagnostics->numErrors() == numErrorsBefore); |
| 182 | } |
| 183 | |
Jamie Madill | 45bcc78 | 2016-11-07 13:58:48 -0500 | [diff] [blame] | 184 | } // namespace sh |