blob: 2e0303cb3d44a4f4cfb2f3cf02c49286d65ee1d3 [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
Jamie Madill45bcc782016-11-07 13:58:48 -050012namespace sh
13{
14
Kimmo Kinnunenb18609b2015-07-16 14:13:11 +030015namespace
16{
Olli Etuaho77ba4082016-12-16 12:01:18 +000017void error(const TIntermSymbol &symbol, const char *reason, TDiagnostics *diagnostics)
Kimmo Kinnunenb18609b2015-07-16 14:13:11 +030018{
Olli Etuaho77ba4082016-12-16 12:01:18 +000019 diagnostics->error(symbol.getLine(), reason, symbol.getSymbol().c_str());
Kimmo Kinnunenb18609b2015-07-16 14:13:11 +030020}
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{
Jamie Madilld7b1ab52016-12-12 14:42:19 -050034 TString name = symbol->getSymbol();
Jamie Madill05a80ce2013-06-20 11:55:49 -040035 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
Olli Etuaho77ba4082016-12-16 12:01:18 +000055void ValidateOutputs::validate(TDiagnostics *diagnostics) const
Jamie Madill05a80ce2013-06-20 11:55:49 -040056{
Olli Etuaho77ba4082016-12-16 12:01:18 +000057 ASSERT(diagnostics);
Kimmo Kinnunenb18609b2015-07-16 14:13:11 +030058 OutputVector validOutputs(mMaxDrawBuffers);
Kimmo Kinnunenb18609b2015-07-16 14:13:11 +030059
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() << "'";
Olli Etuaho77ba4082016-12-16 12:01:18 +000078 error(*symbol, strstr.str().c_str(), diagnostics);
Kimmo Kinnunenb18609b2015-07-16 14:13:11 +030079 }
80 else
81 {
82 validOutputs[offsetLocation] = symbol;
83 }
84 }
85 }
86 else
87 {
88 if (elementCount > 0)
89 {
Olli Etuaho77ba4082016-12-16 12:01:18 +000090 error(*symbol,
Kimmo Kinnunenb18609b2015-07-16 14:13:11 +030091 elementCount > 1 ? "output array locations would exceed MAX_DRAW_BUFFERS"
Olli Etuaho77ba4082016-12-16 12:01:18 +000092 : "output location must be < MAX_DRAW_BUFFERS",
93 diagnostics);
Kimmo Kinnunenb18609b2015-07-16 14:13:11 +030094 }
95 }
96 }
97
98 if (!mAllowUnspecifiedOutputLocationResolution &&
99 ((!mOutputs.empty() && !mUnspecifiedLocationOutputs.empty()) ||
100 mUnspecifiedLocationOutputs.size() > 1))
101 {
102 for (const auto &symbol : mUnspecifiedLocationOutputs)
103 {
Olli Etuaho77ba4082016-12-16 12:01:18 +0000104 error(*symbol,
105 "must explicitly specify all locations when using multiple fragment outputs",
106 diagnostics);
Kimmo Kinnunenb18609b2015-07-16 14:13:11 +0300107 }
108 }
Jamie Madill05a80ce2013-06-20 11:55:49 -0400109}
Jamie Madill45bcc782016-11-07 13:58:48 -0500110
111} // namespace sh