Add SpirvConstantCache

And unsigned int types for shaders
This commit is contained in:
Jérôme Leclercq
2020-08-20 01:05:16 +02:00
parent 0b507708f4
commit 9df219e402
14 changed files with 1341 additions and 421 deletions

View File

@@ -0,0 +1,32 @@
// 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_SHADER_CONSTANTVALUE_HPP
#define NAZARA_SHADER_CONSTANTVALUE_HPP
#include <Nazara/Prerequisites.hpp>
#include <Nazara/Math/Vector2.hpp>
#include <Nazara/Math/Vector3.hpp>
#include <Nazara/Math/Vector4.hpp>
#include <variant>
namespace Nz
{
using ShaderConstantValue = std::variant<
bool,
float,
Int32,
UInt32,
Vector2f,
Vector3f,
Vector4f,
Vector2i32,
Vector3i32,
Vector4i32
>;
}
#endif

View File

@@ -29,8 +29,11 @@ namespace Nz::ShaderNodes
Int4, //< ivec4
Mat4x4, //< mat4
Sampler2D, //< sampler2D
Void //< void
Void, //< void
UInt1, //< uint
UInt2, //< uvec2
UInt3, //< uvec3
UInt4 //< uvec4
};
enum class BinaryType

View File

@@ -12,6 +12,7 @@
#include <Nazara/Math/Vector3.hpp>
#include <Nazara/Math/Vector4.hpp>
#include <Nazara/Shader/Config.hpp>
#include <Nazara/Shader/ShaderConstantValue.hpp>
#include <Nazara/Shader/ShaderEnums.hpp>
#include <Nazara/Shader/ShaderExpressionType.hpp>
#include <Nazara/Shader/ShaderVariables.hpp>
@@ -222,19 +223,7 @@ namespace Nz
ShaderExpressionType GetExpressionType() const override;
void Visit(ShaderAstVisitor& visitor) override;
using Variant = std::variant<
bool,
float,
Int32,
Vector2f,
Vector3f,
Vector4f,
Vector2i32,
Vector3i32,
Vector4i32
>;
Variant value;
ShaderConstantValue value;
template<typename T> static std::shared_ptr<Constant> Build(const T& value);
};

View File

@@ -0,0 +1,194 @@
// 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 <Nazara/Prerequisites.hpp>
#include <Nazara/Shader/ShaderConstantValue.hpp>
#include <Nazara/Shader/ShaderEnums.hpp>
#include <Nazara/Shader/ShaderExpressionType.hpp>
#include <Nazara/Shader/SpirvData.hpp>
#include <memory>
#include <optional>
#include <string>
#include <variant>
#include <vector>
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<Constant>;
using TypePtr = std::shared_ptr<Type>;
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<SpirvAccessQualifier> qualifier;
std::optional<bool> depth;
std::optional<bool> sampled;
SpirvDim dim;
SpirvImageFormat format;
TypePtr sampledType;
bool arrayed;
bool multisampled;
};
struct Pointer
{
TypePtr type;
SpirvStorageClass storageClass;
};
struct Function
{
TypePtr returnType;
std::vector<TypePtr> parameters;
};
struct SampledImage
{
TypePtr image;
};
struct Structure
{
struct Member
{
std::string name;
TypePtr type;
};
std::string name;
std::vector<Member> members;
};
using AnyType = std::variant<Bool, Float, Function, Image, Integer, Matrix, Pointer, SampledImage, Structure, Vector, Void>;
struct ConstantBool
{
bool value;
};
struct ConstantComposite
{
TypePtr type;
std::vector<ConstantPtr> values;
};
struct ConstantScalar
{
std::variant<float, double, Nz::Int32, Nz::Int64, Nz::UInt32, Nz::UInt64> value;
};
using AnyConstant = std::variant<ConstantBool, ConstantComposite, ConstantScalar>;
struct Variable
{
std::string debugName;
TypePtr type;
SpirvStorageClass storageClass;
std::optional<ConstantPtr> initializer;
};
using BaseType = std::variant<Bool, Float, Integer, Vector, Matrix, Image>;
using CompositeValue = std::variant<ConstantBool, ConstantScalar, ConstantComposite>;
using PointerOrBaseType = std::variant<BaseType, Pointer>;
using PrimitiveType = std::variant<Bool, Float, Integer>;
using ScalarType = std::variant<Float, Integer>;
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, SpirvSection& types);
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 WriteStruct(const Structure& structData, UInt32 resultId, SpirvSection& annotations, SpirvSection& debugInfos, SpirvSection& types);
std::unique_ptr<Internal> m_internal;
};
}
#include <Nazara/Shader/SpirvConstantCache.inl>
#endif

View File

@@ -0,0 +1,12 @@
// 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 <Nazara/Shader/SpirvConstantCache.hpp>
#include <Nazara/Shader/Debug.hpp>
namespace Nz
{
}
#include <Nazara/Shader/DebugOff.hpp>

View File

@@ -96,7 +96,7 @@ namespace Nz
}
template<typename T>
unsigned int SpirvSection::CountWord(const T& value)
unsigned int SpirvSection::CountWord(const T& /*value*/)
{
return 1;
}

View File

@@ -11,9 +11,10 @@
#include <Nazara/Shader/Config.hpp>
#include <Nazara/Shader/ShaderAst.hpp>
#include <Nazara/Shader/ShaderAstVisitor.hpp>
#include <Nazara/Shader/ShaderConstantValue.hpp>
#include <Nazara/Shader/ShaderVarVisitor.hpp>
#include <Nazara/Shader/ShaderWriter.hpp>
#include <Nazara/Utility/FieldOffsets.hpp>
#include <Nazara/Shader/SpirvConstantCache.hpp>
#include <string>
#include <string_view>
#include <unordered_map>
@@ -47,20 +48,22 @@ namespace Nz
UInt32 AllocateResultId();
void AppendConstants();
void AppendHeader();
void AppendStructType(std::size_t structIndex, UInt32 resultId);
void AppendTypes();
UInt32 EvaluateExpression(const ShaderNodes::ExpressionPtr& expr);
UInt32 GetConstantId(const ShaderNodes::Constant::Variant& value) const;
UInt32 GetConstantId(const ShaderConstantValue& value) const;
UInt32 GetFunctionTypeId(ShaderExpressionType retType, const std::vector<ShaderAst::FunctionParameter>& parameters);
UInt32 GetPointerTypeId(const ShaderExpressionType& type, SpirvStorageClass storageClass) const;
UInt32 GetTypeId(const ShaderExpressionType& type) const;
void PushResultId(UInt32 value);
UInt32 PopResultId();
UInt32 ReadVariable(ExtVar& var);
UInt32 RegisterConstant(const ShaderConstantValue& value);
UInt32 RegisterFunctionType(ShaderExpressionType retType, const std::vector<ShaderAst::FunctionParameter>& parameters);
UInt32 RegisterPointerType(ShaderExpressionType type, SpirvStorageClass storageClass);
UInt32 RegisterType(ShaderExpressionType type);
using ShaderAstVisitor::Visit;