// 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 #pragma once #ifndef NAZARA_SPIRVCONSTANTCACHE_HPP #define NAZARA_SPIRVCONSTANTCACHE_HPP #include #include #include #include #include #include #include #include #include #include namespace Nz { class ShaderAst; class SpirvSection; class NAZARA_SHADER_API SpirvConstantCache { public: SpirvConstantCache(UInt32& resultId); SpirvConstantCache(const SpirvConstantCache& cache) = delete; SpirvConstantCache(SpirvConstantCache&& cache) noexcept; ~SpirvConstantCache(); struct Constant; struct Type; using ConstantPtr = std::shared_ptr; using TypePtr = std::shared_ptr; struct Bool {}; struct Float { UInt32 width; }; struct Integer { UInt32 width; bool signedness; }; struct Void {}; struct Vector { TypePtr componentType; UInt32 componentCount; }; struct Matrix { TypePtr columnType; UInt32 columnCount; }; struct Image { std::optional qualifier; std::optional depth; std::optional sampled; SpirvDim dim; SpirvImageFormat format; TypePtr sampledType; bool arrayed; bool multisampled; }; struct Pointer { TypePtr type; SpirvStorageClass storageClass; }; struct Function { TypePtr returnType; std::vector parameters; }; struct SampledImage { TypePtr image; }; struct Structure { struct Member { std::string name; TypePtr type; }; std::string name; std::vector members; }; using AnyType = std::variant; struct ConstantBool { bool value; }; struct ConstantComposite { TypePtr type; std::vector values; }; struct ConstantScalar { std::variant value; }; using AnyConstant = std::variant; struct Variable { std::string debugName; TypePtr type; SpirvStorageClass storageClass; std::optional initializer; }; using BaseType = std::variant; using CompositeValue = std::variant; using PointerOrBaseType = std::variant; using PrimitiveType = std::variant; using ScalarType = std::variant; struct Constant { Constant(AnyConstant c) : constant(std::move(c)) { } AnyConstant constant; }; struct Type { Type(AnyType c) : type(std::move(c)) { } AnyType type; }; UInt32 GetId(const Constant& c); UInt32 GetId(const Type& t); UInt32 GetId(const Variable& v); UInt32 Register(Constant c); UInt32 Register(Type t); UInt32 Register(Variable v); void Write(SpirvSection& annotations, SpirvSection& constants, SpirvSection& debugInfos); SpirvConstantCache& operator=(const SpirvConstantCache& cache) = delete; SpirvConstantCache& operator=(SpirvConstantCache&& cache) noexcept; static ConstantPtr BuildConstant(const ShaderConstantValue& value); static TypePtr BuildPointerType(const ShaderNodes::BasicType& type, SpirvStorageClass storageClass); static TypePtr BuildPointerType(const ShaderAst& shader, const ShaderExpressionType& type, SpirvStorageClass storageClass); static TypePtr BuildType(const ShaderNodes::BasicType& type); static TypePtr BuildType(const ShaderAst& shader, const ShaderExpressionType& type); private: struct DepRegisterer; struct Eq; struct Internal; void Write(const AnyConstant& constant, UInt32 resultId, SpirvSection& constants); void Write(const AnyType& type, UInt32 resultId, SpirvSection& annotations, SpirvSection& constants, SpirvSection& debugInfos); void WriteStruct(const Structure& structData, UInt32 resultId, SpirvSection& annotations, SpirvSection& constants, SpirvSection& debugInfos); std::unique_ptr m_internal; }; } #include #endif