blob: 9f5f8f577d206b12371075788f518eda78e870aa [file] [log] [blame]
Jamie Madill05a80ce2013-06-20 11:55:49 -04001//
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//
6
Geoff Lang17732822013-08-29 13:46:49 -04007#include "compiler/translator/ValidateOutputs.h"
8#include "compiler/translator/InfoSink.h"
9#include "compiler/translator/InitializeParseContext.h"
Jamie Madill6b9cb252013-10-17 10:45:47 -040010#include "compiler/translator/ParseContext.h"
Jamie Madill05a80ce2013-06-20 11:55:49 -040011
Kimmo Kinnunenb18609b2015-07-16 14:13:11 +030012namespace
13{
14void error(int *errorCount, TInfoSinkBase &sink, const TIntermSymbol &symbol, const char *reason)
15{
16 sink.prefix(EPrefixError);
17 sink.location(symbol.getLine());
18 sink << "'" << symbol.getSymbol() << "' : " << reason << "\n";
19 (*errorCount)++;
20}
21
22} // namespace
23
24ValidateOutputs::ValidateOutputs(const TExtensionBehavior &extBehavior, int maxDrawBuffers)
Olli Etuaho3d0d9a42015-06-01 12:16:36 +030025 : TIntermTraverser(true, false, false),
Jamie Madill05a80ce2013-06-20 11:55:49 -040026 mMaxDrawBuffers(maxDrawBuffers),
Kimmo Kinnunenb18609b2015-07-16 14:13:11 +030027 mAllowUnspecifiedOutputLocationResolution(
28 IsExtensionEnabled(extBehavior, "GL_EXT_blend_func_extended"))
Jamie Madill05a80ce2013-06-20 11:55:49 -040029{
30}
31
32void ValidateOutputs::visitSymbol(TIntermSymbol *symbol)
33{
34 TString name = symbol->getSymbol();
35 TQualifier qualifier = symbol->getQualifier();
36
Jamie Madill5ed23982016-04-22 15:08:57 -040037 if (mVisitedSymbols.count(name.c_str()) == 1)
Jamie Madill05a80ce2013-06-20 11:55:49 -040038 return;
39
Jamie Madill5ed23982016-04-22 15:08:57 -040040 mVisitedSymbols.insert(name.c_str());
Jamie Madill05a80ce2013-06-20 11:55:49 -040041
Jamie Madill19571812013-08-12 15:26:34 -070042 if (qualifier == EvqFragmentOut)
Jamie Madill05a80ce2013-06-20 11:55:49 -040043 {
Kimmo Kinnunenb18609b2015-07-16 14:13:11 +030044 if (symbol->getType().getLayoutQualifier().location == -1)
Jamie Madill05a80ce2013-06-20 11:55:49 -040045 {
Kimmo Kinnunenb18609b2015-07-16 14:13:11 +030046 mUnspecifiedLocationOutputs.push_back(symbol);
Jamie Madill05a80ce2013-06-20 11:55:49 -040047 }
48 else
49 {
Kimmo Kinnunenb18609b2015-07-16 14:13:11 +030050 mOutputs.push_back(symbol);
Jamie Madill05a80ce2013-06-20 11:55:49 -040051 }
52 }
53}
54
Kimmo Kinnunenb18609b2015-07-16 14:13:11 +030055int ValidateOutputs::validateAndCountErrors(TInfoSinkBase &sink) const
Jamie Madill05a80ce2013-06-20 11:55:49 -040056{
Kimmo Kinnunenb18609b2015-07-16 14:13:11 +030057 OutputVector validOutputs(mMaxDrawBuffers);
58 int errorCount = 0;
59
60 for (const auto &symbol : mOutputs)
61 {
62 const TType &type = symbol->getType();
Olli Etuaho856c4972016-08-08 11:38:39 +030063 const size_t elementCount = static_cast<size_t>(type.isArray() ? type.getArraySize() : 1u);
Kimmo Kinnunenb18609b2015-07-16 14:13:11 +030064 const size_t location = static_cast<size_t>(type.getLayoutQualifier().location);
65
66 ASSERT(type.getLayoutQualifier().location != -1);
67
68 if (location + elementCount <= validOutputs.size())
69 {
70 for (size_t elementIndex = 0; elementIndex < elementCount; elementIndex++)
71 {
72 const size_t offsetLocation = location + elementIndex;
73 if (validOutputs[offsetLocation])
74 {
75 std::stringstream strstr;
76 strstr << "conflicting output locations with previously defined output '"
77 << validOutputs[offsetLocation]->getSymbol() << "'";
78 error(&errorCount, sink, *symbol, strstr.str().c_str());
79 }
80 else
81 {
82 validOutputs[offsetLocation] = symbol;
83 }
84 }
85 }
86 else
87 {
88 if (elementCount > 0)
89 {
90 error(&errorCount, sink, *symbol,
91 elementCount > 1 ? "output array locations would exceed MAX_DRAW_BUFFERS"
92 : "output location must be < MAX_DRAW_BUFFERS");
93 }
94 }
95 }
96
97 if (!mAllowUnspecifiedOutputLocationResolution &&
98 ((!mOutputs.empty() && !mUnspecifiedLocationOutputs.empty()) ||
99 mUnspecifiedLocationOutputs.size() > 1))
100 {
101 for (const auto &symbol : mUnspecifiedLocationOutputs)
102 {
103 error(&errorCount, sink, *symbol,
104 "must explicitly specify all locations when using multiple fragment outputs");
105 }
106 }
107 return errorCount;
Jamie Madill05a80ce2013-06-20 11:55:49 -0400108}