// Copyright (C) 2020 Jérôme Leclercq // This file is part of the "Nazara Engine - Shader generator" // For conditions of distribution and use, see copyright notice in Config.hpp #include #include #include namespace Nz { template void GlslWriter::Append(const T& param) { NazaraAssert(m_currentState, "This function should only be called while processing an AST"); m_currentState->stream << param; } template void GlslWriter::DeclareVariables(const ShaderAst& shader, const std::vector& variables, const std::string& keyword, const std::string& section) { if (!variables.empty()) { if (!section.empty()) AppendCommentSection(section); for (const auto& var : variables) { if constexpr (std::is_same_v) { if (var.locationIndex) { Append("layout(location = "); Append(*var.locationIndex); Append(") "); } if (!keyword.empty()) { Append(keyword); Append(" "); } Append(var.type); Append(" "); Append(var.name); AppendLine(";"); } else if constexpr (std::is_same_v) { if (var.bindingIndex || var.memoryLayout) { Append("layout("); bool first = true; if (var.bindingIndex) { if (!first) Append(", "); Append("binding = "); Append(*var.bindingIndex); first = false; } if (var.memoryLayout) { if (!first) Append(", "); Append(*var.memoryLayout); first = false; } Append(") "); } if (!keyword.empty()) { Append(keyword); Append(" "); } std::visit([&](auto&& arg) { using U = std::decay_t; if constexpr (std::is_same_v) { Append(arg); Append(" "); Append(var.name); } else if constexpr (std::is_same_v) { const auto& structs = shader.GetStructs(); auto it = std::find_if(structs.begin(), structs.end(), [&](const auto& s) { return s.name == arg; }); if (it == structs.end()) throw std::runtime_error("struct " + arg + " has not been defined"); const auto& s = *it; AppendLine(var.name + "_interface"); AppendLine("{"); for (const auto& m : s.members) { Append("\t"); Append(m.type); Append(" "); Append(m.name); AppendLine(";"); } Append("} "); Append(var.name); } else static_assert(AlwaysFalse::value, "non-exhaustive visitor"); }, var.type); AppendLine(";"); AppendLine(); } } AppendLine(); } } } #include