Shader: Add support for depth_write and early_fragment_tests attributes (+ FragDepth builtin)

This commit is contained in:
Jérôme Leclercq
2021-06-01 12:32:24 +02:00
parent 465837ff12
commit 16e2f5f819
12 changed files with 456 additions and 136 deletions

View File

@@ -8,97 +8,131 @@
#define NAZARA_SHADER_AST_ENUMS_HPP
#include <Nazara/Prerequisites.hpp>
#include <Nazara/Core/Flags.hpp>
namespace Nz::ShaderAst
namespace Nz
{
enum class AssignType
namespace ShaderAst
{
Simple //< =
enum class AssignType
{
Simple //< =
};
enum class AttributeType
{
Binding, //< Binding (external var only) - has argument index
Builtin, //< Builtin (struct member only) - has argument type
DepthWrite, //< Depth write mode (function only) - has argument type
EarlyFragmentTests, //< Entry point (function only) - has argument on/off
Entry, //< Entry point (function only) - has argument type
Layout, //< Struct layout (struct only) - has argument style
Location, //< Location (struct member only) - has argument index
Option, //< Conditional compilation option - has argument expr
};
enum class BinaryType
{
Add, //< +
Subtract, //< -
Multiply, //< *
Divide, //< /
CompEq, //< ==
CompGe, //< >=
CompGt, //< >
CompLe, //< <=
CompLt, //< <
CompNe //< <=
};
enum class BuiltinEntry
{
FragCoord = 1, // gl_FragCoord
FragDepth = 2, // gl_FragDepth
VertexPosition = 0, // gl_Position
};
enum class DepthWriteMode
{
Greater,
Less,
Replace,
Unchanged,
};
enum class ExpressionCategory
{
LValue,
RValue
};
enum class FunctionFlag
{
DoesDiscard,
DoesWriteFragDepth,
Max = DoesWriteFragDepth
};
}
template<>
struct EnumAsFlags<ShaderAst::FunctionFlag>
{
static constexpr ShaderAst::FunctionFlag max = ShaderAst::FunctionFlag::Max;
};
enum class AttributeType
namespace ShaderAst
{
Binding, //< Binding (external var only) - has argument index
Builtin, //< Builtin (struct member only) - has argument type
Entry, //< Entry point (function only) - has argument type
Layout, //< Struct layout (struct only) - has argument style
Location, //< Location (struct member only) - has argument index
Option, //< Conditional compilation option - has argument expr
};
using FunctionFlags = Flags<FunctionFlag>;
enum class BinaryType
{
Add, //< +
Subtract, //< -
Multiply, //< *
Divide, //< /
enum class IntrinsicType
{
CrossProduct = 0,
DotProduct = 1,
Length = 3,
Max = 4,
Min = 5,
SampleTexture = 2,
};
CompEq, //< ==
CompGe, //< >=
CompGt, //< >
CompLe, //< <=
CompLt, //< <
CompNe //< <=
};
enum class MemoryLayout
{
Std140
};
enum class BuiltinEntry
{
FragCoord = 1, // gl_FragCoord
VertexPosition = 0, // gl_Position
};
enum class ExpressionCategory
{
LValue,
RValue
};
enum class IntrinsicType
{
CrossProduct = 0,
DotProduct = 1,
Length = 3,
Max = 4,
Min = 5,
SampleTexture = 2,
};
enum class MemoryLayout
{
Std140
};
enum class NodeType
{
None = -1,
enum class NodeType
{
None = -1,
#define NAZARA_SHADERAST_NODE(Node) Node,
#define NAZARA_SHADERAST_STATEMENT_LAST(Node) Node, Max = Node
#include <Nazara/Shader/Ast/AstNodeList.hpp>
};
};
enum class PrimitiveType
{
Boolean, //< bool
Float32, //< f32
Int32, //< i32
UInt32, //< ui32
};
enum class PrimitiveType
{
Boolean, //< bool
Float32, //< f32
Int32, //< i32
UInt32, //< ui32
};
enum class SwizzleComponent
{
First,
Second,
Third,
Fourth
};
enum class SwizzleComponent
{
First,
Second,
Third,
Fourth
};
enum class UnaryType
{
LogicalNot, //< !v
Minus, //< -v
Plus, //< +v
};
enum class UnaryType
{
LogicalNot, //< !v
Minus, //< -v
Plus, //< +v
};
}
}
#endif // NAZARA_SHADER_ENUMS_HPP

View File

@@ -272,6 +272,8 @@ namespace Nz::ShaderAst
ExpressionType type;
};
std::optional<DepthWriteMode> depthWrite;
std::optional<bool> earlyFragmentTests;
std::optional<ShaderStageType> entryStage;
std::optional<std::size_t> funcIndex;
std::optional<std::size_t> varIndex;

View File

@@ -8,6 +8,7 @@
#define NAZARA_SHADERAST_TRANSFORMVISITOR_HPP
#include <Nazara/Prerequisites.hpp>
#include <Nazara/Core/Bitset.hpp>
#include <Nazara/Shader/Config.hpp>
#include <Nazara/Shader/Ast/AstCloner.hpp>
#include <unordered_map>
@@ -39,6 +40,7 @@ namespace Nz::ShaderAst
};
private:
struct FunctionData;
struct Identifier;
const ExpressionType& CheckField(const ExpressionType& structType, const std::string* memberIdentifier, std::size_t remainingMembers, std::size_t* structIndices);
@@ -65,6 +67,7 @@ namespace Nz::ShaderAst
StatementPtr Clone(DeclareOptionStatement& node) override;
StatementPtr Clone(DeclareStructStatement& node) override;
StatementPtr Clone(DeclareVariableStatement& node) override;
StatementPtr Clone(DiscardStatement& node) override;
StatementPtr Clone(ExpressionStatement& node) override;
StatementPtr Clone(MultiStatement& node) override;
@@ -78,12 +81,18 @@ namespace Nz::ShaderAst
void PushScope();
void PopScope();
std::size_t RegisterFunction(DeclareFunctionStatement* funcDecl);
std::size_t DeclareFunction(DeclareFunctionStatement* funcDecl);
void PropagateFunctionFlags(std::size_t funcIndex, FunctionFlags flags, Bitset<>& seen);
FunctionData& RegisterFunction(std::size_t functionIndex);
std::size_t RegisterIntrinsic(std::string name, IntrinsicType type);
std::size_t RegisterOption(std::string name, ExpressionType type);
std::size_t RegisterStruct(std::string name, StructDescription description);
std::size_t RegisterVariable(std::string name, ExpressionType type);
void ResolveFunctions();
std::size_t ResolveStruct(const ExpressionType& exprType);
std::size_t ResolveStruct(const IdentifierType& identifierType);
std::size_t ResolveStruct(const StructType& structType);
@@ -95,6 +104,14 @@ namespace Nz::ShaderAst
void Validate(CallFunctionExpression& node, const DeclareFunctionStatement* referenceDeclaration);
void Validate(IntrinsicExpression& node);
struct FunctionData
{
Bitset<> calledByFunctions;
DeclareFunctionStatement* node;
FunctionFlags flags;
bool defined = false;
};
struct Identifier
{
enum class Type
@@ -112,9 +129,8 @@ namespace Nz::ShaderAst
Type type;
};
std::unordered_map<std::string /*functionName*/, std::pair<const DeclareFunctionStatement*, std::size_t>> m_functionDeclarations;
std::vector<Identifier> m_identifiersInScope;
std::vector<DeclareFunctionStatement*> m_functions;
std::vector<FunctionData> m_functions;
std::vector<IntrinsicType> m_intrinsics;
std::vector<ExpressionType> m_options;
std::vector<StructDescription> m_structs;

View File

@@ -39,6 +39,8 @@ namespace Nz
private:
struct BindingAttribute;
struct BuiltinAttribute;
struct DepthWriteAttribute;
struct EarlyFragmentTestsAttribute;
struct EntryAttribute;
struct LayoutAttribute;
struct LocationAttribute;
@@ -55,8 +57,10 @@ namespace Nz
template<typename T> void Append(const T& param);
template<typename T1, typename T2, typename... Args> void Append(const T1& firstParam, const T2& secondParam, Args&&... params);
template<typename... Args> void AppendAttributes(bool appendLine, Args&&... params);
void AppendAttribute(BindingAttribute builtin);
void AppendAttribute(BindingAttribute binding);
void AppendAttribute(BuiltinAttribute builtin);
void AppendAttribute(DepthWriteAttribute depthWrite);
void AppendAttribute(EarlyFragmentTestsAttribute earlyFragmentTests);
void AppendAttribute(EntryAttribute entry);
void AppendAttribute(LayoutAttribute layout);
void AppendAttribute(LocationAttribute location);

View File

@@ -95,6 +95,7 @@ namespace Nz
std::optional<UInt32> outputStructTypeId;
std::vector<Input> inputs;
std::vector<Output> outputs;
std::vector<SpirvExecutionMode> executionModes;
};
struct FuncData