Files
Seele/src/Engine/Graphics/Shader.h
T

171 lines
4.2 KiB
C++
Raw Normal View History

2023-10-26 18:37:29 +02:00
#pragma once
#include "Enums.h"
2023-11-01 13:38:49 +01:00
#include "CRC.h"
2023-11-01 23:12:30 +01:00
#include "Resources.h"
2023-11-05 10:36:01 +01:00
#include "VertexData.h"
2023-10-26 18:37:29 +02:00
namespace Seele
{
namespace Gfx
{
class Shader
{};
DEFINE_REF(Shader)
class TaskShader
{
public:
TaskShader() {}
virtual ~TaskShader() {}
};
DEFINE_REF(TaskShader)
class MeshShader
{
public:
MeshShader() {}
virtual ~MeshShader() {}
};
DEFINE_REF(MeshShader)
class VertexShader
{
public:
VertexShader() {}
virtual ~VertexShader() {}
};
DEFINE_REF(VertexShader)
class FragmentShader
{
public:
FragmentShader() {}
virtual ~FragmentShader() {}
};
DEFINE_REF(FragmentShader)
class ComputeShader
{
public:
ComputeShader() {}
virtual ~ComputeShader() {}
};
DEFINE_REF(ComputeShader)
2023-11-09 22:15:51 +01:00
//Uniquely identifies a permutation of shaders
//using the type parameters used to generate it
2023-10-26 18:37:29 +02:00
struct ShaderPermutation
{
2023-11-05 10:36:01 +01:00
char taskFile[32];
char vertexMeshFile[32];
char fragmentFile[32];
char vertexDataName[32];
char materialName[32];
uint8 hasFragment : 1;
uint8 useMeshShading : 1;
uint8 hasTaskShader : 1;
2023-11-09 22:15:51 +01:00
uint8 useMaterial : 1;
2023-10-26 18:37:29 +02:00
//TODO: lightmapping etc
2023-11-08 23:27:21 +01:00
ShaderPermutation()
{
std::memset(this, 0, sizeof(ShaderPermutation));
}
2023-11-09 22:15:51 +01:00
void setTaskFile(std::string_view name)
{
std::memset(taskFile, 0, sizeof(taskFile));
hasTaskShader = 1;
std::strncpy(taskFile, name.data(), 32);
}
void setVertexFile(std::string_view name)
{
std::memset(vertexMeshFile, 0, sizeof(vertexMeshFile));
useMeshShading = 0;
std::strncpy(vertexMeshFile, name.data(), 32);
}
void setMeshFile(std::string_view name)
{
std::memset(vertexMeshFile, 0, sizeof(vertexMeshFile));
useMeshShading = 1;
std::strncpy(vertexMeshFile, name.data(), 32);
}
void setFragmentFile(std::string_view name)
{
std::memset(fragmentFile, 0, sizeof(fragmentFile));
hasFragment = 1;
std::strncpy(fragmentFile, name.data(), 32);
}
void setVertexData(std::string_view name)
{
std::memset(vertexDataName, 0, sizeof(vertexDataName));
std::strncpy(vertexDataName, name.data(), 32);
}
void setMaterial(std::string_view name)
{
std::memset(materialName, 0, sizeof(materialName));
useMaterial = 1;
std::strncpy(materialName, name.data(), 32);
}
2023-10-26 18:37:29 +02:00
};
//Hashed ShaderPermutation for fast lookup
struct PermutationId
{
uint32 hash;
PermutationId()
: hash(0)
{}
PermutationId(ShaderPermutation permutation)
: hash(CRC::Calculate(&permutation, sizeof(ShaderPermutation), CRC::CRC_32()))
{}
2023-11-05 10:36:01 +01:00
friend constexpr bool operator==(const PermutationId& lhs, const PermutationId& rhs)
2023-10-26 18:37:29 +02:00
{
return lhs.hash == rhs.hash;
}
2023-11-05 10:36:01 +01:00
friend constexpr auto operator<=>(const PermutationId& lhs, const PermutationId& rhs)
2023-10-26 18:37:29 +02:00
{
2023-11-05 10:36:01 +01:00
return lhs.hash <=> rhs.hash;
2023-10-26 18:37:29 +02:00
}
};
struct ShaderCollection
{
2023-11-01 23:12:30 +01:00
OVertexDeclaration vertexDeclaration;
OVertexShader vertexShader;
OTaskShader taskShader;
OMeshShader meshShader;
OFragmentShader fragmentShader;
2023-10-26 18:37:29 +02:00
};
2023-11-05 10:36:01 +01:00
class ShaderCompiler
2023-10-26 18:37:29 +02:00
{
public:
2023-11-05 10:36:01 +01:00
ShaderCompiler(Gfx::PGraphics graphics);
~ShaderCompiler();
const ShaderCollection* findShaders(PermutationId id) const;
void registerMaterial(PMaterial material);
void registerVertexData(VertexData* vertexData);
void registerRenderPass(std::string name,
std::string mainFile,
bool useMaterials = false,
bool hasFragmentShader = false,
std::string fragmentFile = "",
bool useMeshShading = false,
bool hasTaskShader = false,
std::string taskFile = "");
2023-10-26 18:37:29 +02:00
private:
2023-11-05 10:36:01 +01:00
void compile();
ShaderCollection& createShaders(ShaderPermutation permutation);
2023-10-26 18:37:29 +02:00
std::mutex shadersLock;
2023-11-05 10:36:01 +01:00
Map<PermutationId, ShaderCollection> shaders;
Map<std::string, PMaterial> materials;
Map<std::string, VertexData*> vertexData;
struct PassConfig
{
std::string taskFile;
std::string mainFile;
std::string fragmentFile;
bool hasFragmentShader;
bool useMeshShading;
bool hasTaskShader;
bool useMaterial;
};
Map<std::string, PassConfig> passes;
Gfx::PGraphics graphics;
2023-10-26 18:37:29 +02:00
};
2023-11-05 10:36:01 +01:00
DEFINE_REF(ShaderCompiler)
2023-10-26 18:37:29 +02:00
}
} // namespace Seele