Depth rendering of terrain works

This commit is contained in:
Dynamitos
2023-11-10 19:18:09 +01:00
parent effb0c6214
commit c30619d07d
28 changed files with 298 additions and 502 deletions
+7 -5
View File
@@ -8,7 +8,9 @@ template<typename... Types>
struct Dependencies;
template<>
struct Dependencies<>
{};
{
int x;
};
template<typename This, typename... Rest>
struct Dependencies<This, Rest...> : public Dependencies<Rest...>
{
@@ -17,6 +19,7 @@ struct Dependencies<This, Rest...> : public Dependencies<Rest...>
{
return Dependencies<This, Rest..., Right...>();
}
int x;
};
namespace Component
@@ -24,16 +27,15 @@ namespace Component
template<typename Comp>
static int accessComponent(Comp& comp);
}
template<typename T, typename... Deps>
concept is_component = std::same_as<decltype(T::dependencies), Dependencies<Deps...>>;
template<typename Comp>
concept has_dependencies = requires(Comp) { Comp::dependencies; };
#define REQUIRE_COMPONENT(x) \
private: \
x& get##x() { return *tl_##x; } \
const x& get##x() const { return *tl_##x; }; \
public: \
static Dependencies<x> dependencies;
constexpr static Dependencies<x> dependencies;
#define DECLARE_COMPONENT(x) \
thread_local extern x* tl_##x; \
-10
View File
@@ -8,53 +8,43 @@ DEFINE_COMPONENT(Transform)
void Transform::setPosition(Vector pos)
{
transform.setPosition(pos);
dirty = true;
}
void Transform::setRotation(Quaternion quat)
{
transform.setRotation(quat);
dirty = true;
}
void Transform::setScale(Vector scale)
{
transform.setScale(scale);
dirty = true;
}
void Transform::setRelativeLocation(Vector location)
{
transform = Math::Transform(location, transform.getRotation(), transform.getScale());
dirty = true;
}
void Transform::setRelativeRotation(Vector rotation)
{
transform = Math::Transform(transform.getPosition(), Quaternion(rotation), transform.getScale());
dirty = true;
}
void Transform::setRelativeRotation(Quaternion rotation)
{
transform = Math::Transform(transform.getPosition(), rotation, transform.getScale());
dirty = true;
}
void Transform::setRelativeScale(Vector scale)
{
transform = Math::Transform(transform.getPosition(), transform.getRotation(), scale);
dirty = true;
}
void Transform::addRelativeLocation(Vector translation)
{
transform = Math::Transform(transform.getPosition() + translation, transform.getRotation(), transform.getScale());
dirty = true;
}
void Transform::addRelativeRotation(Vector rotation)
{
transform = Math::Transform(transform.getPosition(), transform.getRotation() * Quaternion(rotation), transform.getScale());
dirty = true;
}
void Transform::addRelativeRotation(Quaternion rotation)
{
transform = Math::Transform(transform.getPosition(), transform.getRotation() * rotation, transform.getScale());
dirty = true;
}
-4
View File
@@ -18,9 +18,6 @@ struct Transform
Matrix4 toMatrix() const { return transform.toMatrix(); }
bool isDirty() const { return dirty; }
void clean() { dirty = false; }
void setPosition(Vector pos);
void setRotation(Quaternion quat);
void setScale(Vector scale);
@@ -34,7 +31,6 @@ struct Transform
void addRelativeRotation(Quaternion rotation);
void addRelativeRotation(Vector rotation);
private:
bool dirty = true;
Math::Transform transform;
};
DECLARE_COMPONENT(Transform)
+1 -1
View File
@@ -115,7 +115,7 @@ struct ShaderCreateInfo
Array<std::string> additionalModules;
std::string name; // Debug info
std::string entryPoint;
Array<const char*> typeParameter;
Array<Pair<const char*, const char*>> typeParameter;
Map<const char*, const char*> defines;
};
@@ -30,6 +30,7 @@ private:
PCameraActor source;
Gfx::OPipelineLayout basePassLayout;
// Set 0: viewParameter, provided by renderpass
static constexpr uint32 INDEX_VIEW_PARAMS = 0;
// Set 1: light environment, provided by lightenv
static constexpr uint32 INDEX_LIGHT_ENV = 1;
// Set 2: light culling data
@@ -49,9 +49,10 @@ void DepthPrepass::render()
permutation.setVertexFile("LegacyBasePass");
}
graphics->beginRenderPass(renderPass);
Array<Gfx::PRenderCommand> commands;
for (VertexData* vertexData : VertexData::getList())
{
std::strncpy(permutation.vertexDataName, vertexData->getTypeName().c_str(), sizeof(permutation.vertexDataName));
permutation.setVertexData(vertexData->getTypeName());
const auto& materials = vertexData->getMaterialData();
for (const auto& [_, materialData] : materials)
{
@@ -64,6 +65,7 @@ void DepthPrepass::render()
Gfx::PermutationId id(permutation);
Gfx::PRenderCommand command = graphics->createRenderCommand("DepthRender");
command->setViewport(viewport);
Gfx::OPipelineLayout layout = graphics->createPipelineLayout(depthPrepassLayout);
//layout->addDescriptorLayout(INDEX_MATERIAL, materialData.material->getDescriptorLayout());
layout->addDescriptorLayout(INDEX_VERTEX_DATA, vertexData->getVertexDataLayout());
@@ -122,11 +124,14 @@ void DepthPrepass::render()
{
command->draw(vertexData->getMeshVertexCount(mesh.id), 1, vertexOffset, instanceOffset);
}
instanceOffset+=mesh.meshes;
}
}
}
commands.add(command);
}
}
graphics->executeCommands(commands);
graphics->endRenderPass();
}
@@ -25,7 +25,8 @@ private:
Gfx::OPipelineLayout depthPrepassLayout;
// Set 0: viewParameter
// Set 1: vertices, from VertexData
static constexpr uint32 INDEX_VIEW_PARAMS = 0;
// Set 0: vertices, from VertexData
constexpr static uint32 INDEX_VERTEX_DATA = 1;
// Set 2: mesh data, either index buffer or meshlet data
constexpr static uint32 INDEX_SCENE_DATA = 2;
@@ -36,7 +36,6 @@ protected:
Vector2 pad0;
} viewParams;
PRenderGraphResources resources;
static constexpr uint32 INDEX_VIEW_PARAMS = 0;
Gfx::ODescriptorLayout viewParamsLayout;
Gfx::OUniformBuffer viewParamsBuffer;
Gfx::PDescriptorSet viewParamsSet;
+2 -2
View File
@@ -87,12 +87,12 @@ ShaderCollection& ShaderCompiler::createShaders(ShaderPermutation permutation)
ShaderCollection collection;
ShaderCreateInfo createInfo;
createInfo.typeParameter = { permutation.vertexDataName };
createInfo.typeParameter = { Pair<const char*, const char*>("IVertexData", permutation.vertexDataName) };
createInfo.name = std::format("Material {0}", permutation.materialName);
if (std::strlen(permutation.materialName) > 0)
{
createInfo.additionalModules.add(permutation.materialName);
createInfo.typeParameter.add(permutation.materialName);
createInfo.typeParameter.add(Pair<const char*, const char*>("IMaterial", permutation.materialName));
}
createInfo.additionalModules.add(permutation.vertexDataName);
createInfo.additionalModules.add(permutation.vertexMeshFile);
+55 -53
View File
@@ -36,6 +36,54 @@ void VertexData::updateMesh(const Component::Transform& transform, PMesh mesh)
matInstanceData.numMeshes += meshData[mesh->id].size();
}
void VertexData::createDescriptors()
{
instanceDataLayout->reset();
for (const auto& [_, mat] : materialData)
{
for (auto& [_, matInst] : mat.instances)
{
Array<InstanceData> instanceData;
Array<MeshData> meshes;
for (auto& inst : matInst.meshes)
{
inst.meshes = 0;
for (const auto& mesh : meshData[inst.id])
{
instanceData.add(inst.instance);
meshes.add(mesh);
inst.meshes++;
}
}
matInst.instanceBuffer = graphics->createShaderBuffer(ShaderBufferCreateInfo{
.sourceData = {
.size = sizeof(InstanceData) * instanceData.size(),
.data = (uint8*)instanceData.data(),
},
.stride = sizeof(InstanceData)
});
matInst.descriptorSet = instanceDataLayout->allocateDescriptorSet();
matInst.descriptorSet->updateBuffer(0, matInst.instanceBuffer);
if (graphics->supportMeshShading())
{
matInst.meshDataBuffer = graphics->createShaderBuffer(ShaderBufferCreateInfo{
.sourceData = {
.size = sizeof(MeshData) * meshes.size(),
.data = (uint8*)meshes.data(),
},
.stride = sizeof(MeshData)
});
matInst.descriptorSet->updateBuffer(1, matInst.meshDataBuffer);
matInst.descriptorSet->updateBuffer(2, meshletBuffer);
matInst.descriptorSet->updateBuffer(3, primitiveIndicesBuffer);
matInst.descriptorSet->updateBuffer(4, vertexIndicesBuffer);
}
matInst.descriptorSet->writeChanges();
matInst.numMeshes = meshes.size();
}
}
}
void VertexData::loadMesh(MeshId id, Array<Meshlet> loadedMeshlets)
{
meshData[id].clear();
@@ -65,79 +113,33 @@ void VertexData::loadMesh(MeshId id, Array<Meshlet> loadedMeshlets)
.numMeshlets = numMeshlets,
.meshletOffset = meshletOffset,
.indicesOffset = static_cast<uint32>(meshOffsets[id]),
});
});
currentMesh += numMeshlets;
}
meshletBuffer = graphics->createShaderBuffer(ShaderBufferCreateInfo {
meshletBuffer = graphics->createShaderBuffer(ShaderBufferCreateInfo{
.sourceData = {
.size = sizeof(MeshletDescription) * meshlets.size(),
.data = (uint8*)meshlets.data()
},
.stride = sizeof(MeshletDescription),
.dynamic = true,
});
vertexIndicesBuffer = graphics->createShaderBuffer(ShaderBufferCreateInfo {
});
vertexIndicesBuffer = graphics->createShaderBuffer(ShaderBufferCreateInfo{
.sourceData = {
.size = sizeof(uint32) * vertexIndices.size(),
.data = (uint8*)vertexIndices.data(),
},
.stride = sizeof(uint32),
.dynamic = true,
});
primitiveIndicesBuffer = graphics->createShaderBuffer(ShaderBufferCreateInfo {
});
primitiveIndicesBuffer = graphics->createShaderBuffer(ShaderBufferCreateInfo{
.sourceData = {
.size = sizeof(uint8) * primitiveIndices.size(),
.data = (uint8*)primitiveIndices.data(),
},
.stride = sizeof(uint8),
.dynamic = true,
});
}
void VertexData::createDescriptors()
{
for (const auto& [_, mat] : materialData)
{
for (auto& [_, matInst] : mat.instances)
{
Array<InstanceData> instanceData;
Array<MeshData> meshes;
for (const auto& inst : matInst.meshes)
{
for (const auto& mesh : meshData[inst.id])
{
instanceData.add(inst.instance);
meshes.add(mesh);
}
}
matInst.instanceBuffer = graphics->createShaderBuffer(ShaderBufferCreateInfo{
.sourceData = {
.size = sizeof(InstanceData) * instanceData.size(),
.data = (uint8*)instanceData.data(),
},
.stride = sizeof(InstanceData)
});
instanceDataLayout->reset();
matInst.descriptorSet = instanceDataLayout->allocateDescriptorSet();
matInst.descriptorSet->updateBuffer(0, matInst.instanceBuffer);
if (graphics->supportMeshShading())
{
matInst.meshDataBuffer = graphics->createShaderBuffer(ShaderBufferCreateInfo{
.sourceData = {
.size = sizeof(MeshData) * meshes.size(),
.data = (uint8*)meshes.data(),
},
.stride = sizeof(MeshData)
});
matInst.descriptorSet->updateBuffer(1, matInst.meshDataBuffer);
matInst.descriptorSet->updateBuffer(2, meshletBuffer);
matInst.descriptorSet->updateBuffer(3, primitiveIndicesBuffer);
matInst.descriptorSet->updateBuffer(4, vertexIndicesBuffer);
}
matInst.descriptorSet->writeChanges();
matInst.numMeshes = meshes.size();
}
}
});
}
MeshId VertexData::allocateVertexData(uint64 numVertices)
+2 -1
View File
@@ -39,6 +39,7 @@ public:
MeshId id;
InstanceData instance;
Gfx::PIndexBuffer indexBuffer;
uint32 meshes;
};
struct MaterialInstanceData
{
@@ -62,8 +63,8 @@ public:
};
void resetMeshData();
void updateMesh(const Component::Transform& transform, PMesh mesh);
void loadMesh(MeshId id, Array<Meshlet> meshlets);
void createDescriptors();
void loadMesh(MeshId id, Array<Meshlet> meshlets);
MeshId allocateVertexData(uint64 numVertices);
uint64 getMeshOffset(MeshId id);
uint64 getMeshVertexCount(MeshId id);
+4 -2
View File
@@ -265,9 +265,10 @@ uint32 Allocator::findMemoryType(uint32 typeFilter, VkMemoryPropertyFlags proper
throw std::runtime_error("error finding memory");
}
StagingBuffer::StagingBuffer(OSubAllocation allocation, VkBuffer buffer, VkBufferUsageFlags usage, uint8 readable)
StagingBuffer::StagingBuffer(OSubAllocation allocation, VkBuffer buffer, VkDeviceSize size, VkBufferUsageFlags usage, uint8 readable)
: allocation(std::move(allocation))
, buffer(buffer)
, size(size)
, usage(usage)
, readable(readable)
{
@@ -306,7 +307,7 @@ VkDeviceSize StagingBuffer::getOffset() const
uint64 StagingBuffer::getSize() const
{
return allocation->getSize();
return size;
}
StagingManager::StagingManager(PGraphics graphics, PAllocator allocator)
@@ -368,6 +369,7 @@ OStagingBuffer StagingManager::allocateStagingBuffer(uint64 size, VkBufferUsageF
OStagingBuffer stagingBuffer = new StagingBuffer(
allocator->allocate(memReqs, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | (readable ? VK_MEMORY_PROPERTY_HOST_COHERENT_BIT : VK_MEMORY_PROPERTY_HOST_CACHED_BIT), buffer),
buffer,
size,
usage,
readable
);
+2 -1
View File
@@ -154,7 +154,7 @@ DECLARE_REF(StagingManager)
class StagingBuffer
{
public:
StagingBuffer(OSubAllocation allocation, VkBuffer buffer, VkBufferUsageFlags usage, uint8 readable);
StagingBuffer(OSubAllocation allocation, VkBuffer buffer, VkDeviceSize size, VkBufferUsageFlags usage, uint8 readable);
~StagingBuffer();
void* getMappedPointer();
void flushMappedMemory();
@@ -179,6 +179,7 @@ public:
private:
OSubAllocation allocation;
VkBuffer buffer;
VkDeviceSize size;
VkBufferUsageFlags usage;
uint8 readable;
};
+19 -27
View File
@@ -49,7 +49,7 @@ void Shader::create(const ShaderCreateInfo& createInfo)
sessionDesc.preprocessorMacroCount = macros.size();
sessionDesc.preprocessorMacros = macros.data();
slang::TargetDesc vulkan;
vulkan.profile = globalSession->findProfile("glsl_vk");
vulkan.profile = globalSession->findProfile("sm_6_6");
vulkan.format = SLANG_SPIRV;
sessionDesc.targetCount = 1;
sessionDesc.targets = &vulkan;
@@ -85,29 +85,14 @@ void Shader::create(const ShaderCreateInfo& createInfo)
mainModule->findEntryPointByName(createInfo.entryPoint.c_str(), entrypoint.writeRef());
modules.add(entrypoint);
slang::IComponentType* moduleComposition;
session->createCompositeComponentType(modules.data(), modules.size(), &moduleComposition, diagnostics.writeRef());
Slang::ComPtr<slang::IComponentType> moduleComposition;
session->createCompositeComponentType(modules.data(), modules.size(), moduleComposition.writeRef(), diagnostics.writeRef());
if(diagnostics)
{
std::cout << (const char*)diagnostics->getBufferPointer() << std::endl;
}
/*slang::ProgramLayout* layout = moduleComposition->getLayout();
for(auto typeParam : createInfo.typeParameter)
{
Slang::ComPtr<slang::ITypeConformance> typeConformance;
session->createTypeConformanceComponentType(layout->findTypeByName(typeParam), layout->findTypeByName("IMaterial"), typeConformance.writeRef(), -1, diagnostics.writeRef());
modules.add(typeConformance);
if(diagnostics)
{
std::cout << (const char*)diagnostics->getBufferPointer() << std::endl;
}
}
Slang::ComPtr<slang::IComponentType> conformingModule;
session->createCompositeComponentType(modules.data(), modules.size(), conformingModule.writeRef(), diagnostics.writeRef());
*/
Slang::ComPtr<slang::IComponentType> linkedProgram;
moduleComposition->link(linkedProgram.writeRef(), diagnostics.writeRef());
if(diagnostics)
@@ -122,9 +107,9 @@ void Shader::create(const ShaderCreateInfo& createInfo)
}
Array<slang::SpecializationArg> specialization;
for(auto typeArg : createInfo.typeParameter)
for(const auto& [key, value] : createInfo.typeParameter)
{
specialization.add(slang::SpecializationArg::fromType(reflection->findTypeByName(typeArg)));
specialization.add(slang::SpecializationArg::fromType(reflection->findTypeByName(value)));
}
Slang::ComPtr<slang::IComponentType> specializedComponent;
linkedProgram->specialize(specialization.data(), specialization.size(), specializedComponent.writeRef(), diagnostics.writeRef());
@@ -143,13 +128,20 @@ void Shader::create(const ShaderCreateInfo& createInfo)
{
std::cout << (const char*)diagnostics->getBufferPointer() << std::endl;
}
VkShaderModuleCreateInfo moduleInfo;
moduleInfo.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO;
moduleInfo.pNext = nullptr;
moduleInfo.flags = 0;
moduleInfo.codeSize = kernelBlob->getBufferSize();
moduleInfo.pCode = (uint32_t*)kernelBlob->getBufferPointer();
for (uint32 i = 0; i < reflection->getParameterCount(); ++i)
{
slang::VariableLayoutReflection* varLayout = reflection->getParameterByIndex(i);
std::cout << varLayout->getName() << " in space " << varLayout->getBindingSpace() << " index " << varLayout->getBindingIndex() << std::endl;
}
std::cout << reflection->getGlobalConstantBufferSize() << std::endl;
VkShaderModuleCreateInfo moduleInfo =
{
.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO,
.pNext = nullptr,
.flags = 0,
.codeSize = kernelBlob->getBufferSize(),
.pCode = (uint32_t*)kernelBlob->getBufferPointer(),
};
VK_CHECK(vkCreateShaderModule(graphics->getDevice(), &moduleInfo, nullptr, &module));
hash = CRC::Calculate(entryPointName.data(), entryPointName.size(), CRC::CRC_32());
+8 -47
View File
@@ -43,53 +43,14 @@ Transform::~Transform()
}
Matrix4 Transform::toMatrix() const
{
Matrix4 mat;
mat[3][0] = position.x;
mat[3][1] = position.y;
mat[3][2] = position.z;
const float x2 = rotation.x + rotation.x;
const float y2 = rotation.y + rotation.y;
const float z2 = rotation.z + rotation.z;
{
const float xx2 = rotation.x * x2;
const float yy2 = rotation.y * y2;
const float zz2 = rotation.z * z2;
mat[0][0] = (1.0f - (yy2 + zz2)) * scale.x;
mat[1][1] = (1.0f - (xx2 + zz2)) * scale.y;
mat[2][2] = (1.0f - (xx2 + yy2)) * scale.z;
}
{
const float yz2 = rotation.y * z2;
const float wx2 = rotation.w * x2;
mat[2][1] = (yz2 - wx2) * scale.z;
mat[1][2] = (yz2 + wx2) * scale.y;
}
{
const float xy2 = rotation.x * y2;
const float wz2 = rotation.w * z2;
mat[1][0] = (xy2 - wz2) * scale.y;
mat[0][1] = (xy2 + wz2) * scale.x;
}
{
const float xz2 = rotation.x * z2;
const float wy2 = rotation.w * y2;
mat[2][0] = (xz2 + wy2) * scale.z;
mat[0][2] = (xz2 - wy2) * scale.x;
}
mat[0][3] = 0.0f;
mat[1][3] = 0.0f;
mat[2][3] = 0.0f;
mat[3][3] = 1.0f;
return mat;
// TODO: actual calculations, SIMD
Matrix4 result = Matrix4(1);
result = glm::scale(result, Vector(scale));
result = glm::toMat4(rotation) * result;
result[3][0] = position.x;
result[3][1] = position.y;
result[3][2] = position.z;
return result;
}
Vector Transform::transformPosition(const Vector &v) const
+7 -7
View File
@@ -33,8 +33,8 @@ namespace Serialization
}
static void load(ArchiveBuffer& buffer, IVector2& vec)
{
save(buffer, vec.x);
save(buffer, vec.y);
load(buffer, vec.x);
load(buffer, vec.y);
}
static void save(ArchiveBuffer& buffer, const Vector2& vec)
{
@@ -43,8 +43,8 @@ namespace Serialization
}
static void load(ArchiveBuffer& buffer, Vector2& vec)
{
save(buffer, vec.x);
save(buffer, vec.y);
load(buffer, vec.x);
load(buffer, vec.y);
}
static void save(ArchiveBuffer& buffer, const Vector& vec)
{
@@ -54,9 +54,9 @@ namespace Serialization
}
static void load(ArchiveBuffer& buffer, Vector& vec)
{
save(buffer, vec.x);
save(buffer, vec.y);
save(buffer, vec.z);
load(buffer, vec.x);
load(buffer, vec.y);
load(buffer, vec.z);
}
template<enumeration T>
static void save(ArchiveBuffer& buffer, const T& type)
+3
View File
@@ -1,5 +1,7 @@
target_sources(Engine
PRIVATE
CameraUpdater.h
CameraUpdater.cpp
ComponentSystem.h
Executor.h
Executor.cpp
@@ -14,6 +16,7 @@ target_sources(Engine
target_sources(Engine
PUBLIC FILE_SET HEADERS
FILES
CameraUpdater.h
ComponentSystem.h
Executor.h
LightGather.h
+18
View File
@@ -0,0 +1,18 @@
#include "CameraUpdater.h"
using namespace Seele;
using namespace Seele::System;
CameraUpdater::CameraUpdater(PScene scene)
: ComponentSystem(scene)
{
}
CameraUpdater::~CameraUpdater()
{
}
void CameraUpdater::update(Component::Camera& camera)
{
camera.buildViewMatrix();
}
+19
View File
@@ -0,0 +1,19 @@
#pragma once
#include "ComponentSystem.h"
#include "Component/Camera.h"
namespace Seele
{
namespace System
{
class CameraUpdater : public ComponentSystem<Component::Camera>
{
public:
CameraUpdater(PScene scene);
virtual ~CameraUpdater();
virtual void update(Component::Camera& camera);
private:
};
}
}
+2 -1
View File
@@ -1,6 +1,7 @@
#pragma once
#include "SystemBase.h"
#include "Component/Component.h"
#include "Component/Camera.h"
namespace Seele
{
@@ -12,7 +13,7 @@ class ComponentSystem : public SystemBase
public:
ComponentSystem(PScene scene) : SystemBase(scene) {}
virtual ~ComponentSystem() {}
template<is_component Comp>
template<has_dependencies Comp>
auto getDependencies()
{
return Comp::dependencies;
+2
View File
@@ -6,6 +6,7 @@
#include "Asset/AssetRegistry.h"
#include "System/LightGather.h"
#include "System/MeshUpdater.h"
#include "System/CameraUpdater.h"
using namespace Seele;
@@ -76,6 +77,7 @@ void GameView::reloadGame()
gameInterface.getGame()->setupScene(scene, systemGraph);
systemGraph->addSystem(new System::LightGather(scene));
systemGraph->addSystem(new System::MeshUpdater(scene));
systemGraph->addSystem(new System::CameraUpdater(scene));
}
void GameView::keyCallback(KeyCode code, InputAction action, KeyModifier)