// Copyright (c) 2012-2023 Wojciech Figat. All rights reserved.
#pragma once
#if COMPILE_WITH_SHADER_COMPILER
#include "ShaderCompilationContext.h"
#include "Parser/ShaderMeta.h"
#include "Engine/Graphics/Shaders/GPUShaderProgram.h"
///
/// Base class for the objects that can compile shaders source code.
///
class ShaderCompiler
{
public:
struct ShaderResourceBuffer
{
byte Slot;
bool IsUsed;
uint32 Size;
};
private:
Array _funcNameDefineBuffer;
protected:
ShaderProfile _profile;
ShaderCompilationContext* _context = nullptr;
Array _globalMacros;
Array _macros;
Array _constantBuffers;
public:
///
/// Initializes a new instance of the class.
///
/// The profile.
ShaderCompiler(ShaderProfile profile)
: _profile(profile)
{
}
///
/// Finalizes an instance of the class.
///
virtual ~ShaderCompiler() = default;
public:
///
/// Gets shader profile supported by this compiler.
///
/// The shader profile.
FORCE_INLINE ShaderProfile GetProfile() const
{
return _profile;
}
///
/// Performs the shader compilation.
///
/// The compilation context.
/// True if failed, otherwise false.
bool Compile(ShaderCompilationContext* context);
///
/// Gets the included file source code. Handles system includes and absolute includes. Method is thread-safe.
///
/// The compilation context.
/// The source file that is being compiled.
/// The included file name (absolute or relative).
/// The output source code of the file (null-terminated), null if failed to load.
/// The output source code length of the file (characters count), 0 if failed to load.
/// True if failed, otherwise false.
static bool GetIncludedFileSource(ShaderCompilationContext* context, const char* sourceFile, const char* includedFile, const char*& source, int32& sourceLength);
///
/// Clears the cache used by the shader includes.
///
static void DisposeIncludedFilesCache();
protected:
typedef bool (*WritePermutationData)(ShaderCompilationContext*, ShaderFunctionMeta&, int32, const Array&);
virtual bool CompileShader(ShaderFunctionMeta& meta, WritePermutationData customDataWrite = nullptr) = 0;
bool CompileShaders();
virtual bool OnCompileBegin();
virtual bool OnCompileEnd();
static bool WriteShaderFunctionBegin(ShaderCompilationContext* context, ShaderFunctionMeta& meta);
static bool WriteShaderFunctionPermutation(ShaderCompilationContext* context, ShaderFunctionMeta& meta, int32 permutationIndex, const ShaderBindings& bindings, const void* header, int32 headerSize, const void* cache, int32 cacheSize);
static bool WriteShaderFunctionPermutation(ShaderCompilationContext* context, ShaderFunctionMeta& meta, int32 permutationIndex, const ShaderBindings& bindings, const void* cache, int32 cacheSize);
static bool WriteShaderFunctionEnd(ShaderCompilationContext* context, ShaderFunctionMeta& meta);
static bool WriteCustomDataVS(ShaderCompilationContext* context, ShaderFunctionMeta& meta, int32 permutationIndex, const Array& macros);
static bool WriteCustomDataHS(ShaderCompilationContext* context, ShaderFunctionMeta& meta, int32 permutationIndex, const Array& macros);
void GetDefineForFunction(ShaderFunctionMeta& meta, Array& macros);
};
#endif