From c30619d07de3384c8879d49c6fb9a330acfc087d Mon Sep 17 00:00:00 2001 From: Dynamitos Date: Fri, 10 Nov 2023 19:18:09 +0100 Subject: [PATCH] Depth rendering of terrain works --- CMakeLists.txt | 7 +- res/shaders/LegacyBasePass.slang | 3 +- res/shaders/lib/Common.slang | 1 + res/shaders/lib/StaticMeshVertexData.slang | 20 +- res/shaders/lib/VertexData.slang | 1 + res/shaders/test.glsl | 440 +++++------------- src/Editor/Asset/MeshLoader.cpp | 4 + src/Engine/Component/Component.h | 12 +- src/Engine/Component/Transform.cpp | 10 - src/Engine/Component/Transform.h | 4 - src/Engine/Graphics/Initializer.h | 2 +- src/Engine/Graphics/RenderPass/BasePass.h | 1 + .../Graphics/RenderPass/DepthPrepass.cpp | 7 +- src/Engine/Graphics/RenderPass/DepthPrepass.h | 3 +- src/Engine/Graphics/RenderPass/RenderPass.h | 1 - src/Engine/Graphics/Shader.cpp | 4 +- src/Engine/Graphics/VertexData.cpp | 108 ++--- src/Engine/Graphics/VertexData.h | 3 +- src/Engine/Graphics/Vulkan/Allocator.cpp | 6 +- src/Engine/Graphics/Vulkan/Allocator.h | 3 +- src/Engine/Graphics/Vulkan/Shader.cpp | 46 +- src/Engine/Math/Transform.cpp | 55 +-- src/Engine/Serialization/Serialization.h | 14 +- src/Engine/System/CMakeLists.txt | 3 + src/Engine/System/CameraUpdater.cpp | 18 + src/Engine/System/CameraUpdater.h | 19 + src/Engine/System/ComponentSystem.h | 3 +- src/Engine/Window/GameView.cpp | 2 + 28 files changed, 298 insertions(+), 502 deletions(-) create mode 100644 src/Engine/System/CameraUpdater.cpp create mode 100644 src/Engine/System/CameraUpdater.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 5a48877..62cd737 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -4,7 +4,6 @@ if(${CMAKE_VERSION} VERSION_LESS 3.12) cmake_policy(VERSION ${CMAKE_MAJOR_VERSION}.${CMAKE_MINOR_VERSION}) endif() set(CMAKE_CXX_STANDARD 20) - # Handle superbuild first option (USE_SUPERBUILD "Whether or not a superbuild should be invoked" ON) @@ -59,10 +58,6 @@ endif() if(WIN32) add_compile_definitions(USE_EXTENSIONS) endif() -if(CMAKE_DEBUG_POSTFIX) - add_compile_definitions(ENABLE_VALIDATION) - add_compile_definitions(SEELE_DEBUG) -endif() add_library(Engine SHARED "") @@ -127,6 +122,8 @@ else() target_compile_options(Engine PUBLIC -g -Wall -Wextra -Wno-error -pedantic -std=c++20 -Wno-missing-field-initializers -Wno-unused-function) target_compile_options(Editor PUBLIC -g -Wall -Wextra -Wno-error -pedantic -std=c++20) endif() +target_compile_options(Engine PUBLIC "$<$:-DENABLE_VALIDATION>") +target_compile_options(Engine PUBLIC "$<$:-DSEELE_DEBUG>") add_subdirectory(src/) diff --git a/res/shaders/LegacyBasePass.slang b/res/shaders/LegacyBasePass.slang index 77820df..1d19c35 100644 --- a/res/shaders/LegacyBasePass.slang +++ b/res/shaders/LegacyBasePass.slang @@ -1,3 +1,4 @@ +import Common; import VertexData; import MaterialParameter; @@ -10,7 +11,7 @@ struct Scene { StructuredBuffer instances; } - +layout(set=2) ParameterBlock pScene; [shader("vertex")] diff --git a/res/shaders/lib/Common.slang b/res/shaders/lib/Common.slang index d709156..a856073 100644 --- a/res/shaders/lib/Common.slang +++ b/res/shaders/lib/Common.slang @@ -9,6 +9,7 @@ struct ViewParameter float4 cameraPos_WS; float2 screenDimensions; } +layout(set = 0) ParameterBlock pViewParams; diff --git a/res/shaders/lib/StaticMeshVertexData.slang b/res/shaders/lib/StaticMeshVertexData.slang index 73142cf..c20d491 100644 --- a/res/shaders/lib/StaticMeshVertexData.slang +++ b/res/shaders/lib/StaticMeshVertexData.slang @@ -8,22 +8,22 @@ struct StaticMeshVertexData : IVertexData { VertexAttributes attributes; MaterialParameter params; - float4 localPos = float4(positions[index], 1); + float4 localPos = float4(positions[3 * index + 0], positions[3 * index + 1], positions[3 * index + 2], 1); float4 worldPos = mul(transform, localPos); float4 viewPos = mul(pViewParams.viewMatrix, worldPos); float4 clipPos = mul(pViewParams.projectionMatrix, viewPos); params.worldPosition = worldPos.xyz; - params.texCoords = texCoords[index]; - params.normal = normals[index]; - params.tangent = tangents[index]; - params.biTangent = biTangents[index]; + params.texCoords = float2(texCoords[2 * index + 0], texCoords[2 * index + 1]); + params.normal = float3(normals[3 * index + 0], normals[3 * index + 1], normals[3 * index + 2]); + params.tangent = float3(tangents[3 * index + 0], tangents[3 * index + 1], tangents[3 * index + 2]); + params.biTangent = float3(biTangents[3 * index + 0], biTangents[3 * index + 1], biTangents[3 * index + 2]); attributes.parameter = params; attributes.clipPosition = clipPos; return attributes; } - StructuredBuffer positions; - StructuredBuffer texCoords; - StructuredBuffer normals; - StructuredBuffer tangents; - StructuredBuffer biTangents; + StructuredBuffer positions; + StructuredBuffer texCoords; + StructuredBuffer normals; + StructuredBuffer tangents; + StructuredBuffer biTangents; }; diff --git a/res/shaders/lib/VertexData.slang b/res/shaders/lib/VertexData.slang index 25338f4..a6ac73a 100644 --- a/res/shaders/lib/VertexData.slang +++ b/res/shaders/lib/VertexData.slang @@ -4,4 +4,5 @@ interface IVertexData { VertexAttributes getAttributes(uint index, float4x4 transform); }; +layout(set = 1) ParameterBlock pVertexData; \ No newline at end of file diff --git a/res/shaders/test.glsl b/res/shaders/test.glsl index 731997e..c4c67dd 100755 --- a/res/shaders/test.glsl +++ b/res/shaders/test.glsl @@ -1,114 +1,45 @@ #version 450 -#extension GL_EXT_samplerless_texture_functions : require layout(row_major) uniform; layout(row_major) buffer; -#line 11 0 -struct CullingParams_0 +#line 5 0 +struct InstanceData_0 { - uvec3 numThreadGroups_0; - uint pad0_0; - uvec3 numThreads_0; - uint pad1_0; + mat4x4 transformMatrix_0; }; -#line 35 1 -layout(binding = 0, set = 1) -layout(std140) uniform _S1 -{ - uvec3 numThreadGroups_0; - uint pad0_0; - uvec3 numThreads_0; - uint pad1_0; -}gCullingParams_0; - -#line 2113 2 -layout(binding = 1, set = 1) -uniform texture2D gCullingParams_depthTextureVS_0; - - -#line 11 0 -layout(std430, binding = 2, set = 1) buffer StructuredBuffer_uint_t_0 { - uint _data[]; -} gCullingParams_oLightIndexCounter_0; - -#line 11 -layout(std430, binding = 3, set = 1) buffer StructuredBuffer_uint_t_1 { - uint _data[]; -} gCullingParams_tLightIndexCounter_0; - -#line 11 -layout(std430, binding = 4, set = 1) buffer StructuredBuffer_uint_t_2 { - uint _data[]; -} gCullingParams_oLightIndexList_0; - -#line 11 -layout(std430, binding = 5, set = 1) buffer StructuredBuffer_uint_t_3 { - uint _data[]; -} gCullingParams_tLightIndexList_0; - -#line 1572 2 -layout(rg32ui) -layout(binding = 6, set = 1) -uniform uimage2D gCullingParams_oLightGrid_0; - - -#line 1572 -layout(rg32ui) -layout(binding = 7, set = 1) -uniform uimage2D gCullingParams_tLightGrid_0; - - -#line 26 1 -struct Plane_0 -{ - vec3 n_0; - float d_0; -}; - -struct Frustum_0 -{ - Plane_0 sides_0[4]; - Plane_0 basePlane_0; -}; - - -#line 11 0 -layout(std430, binding = 8, set = 1) readonly buffer StructuredBuffer_Frustum_t_0 { - Frustum_0 _data[]; -} gCullingParams_frustums_0; - -#line 64 3 -struct LightEnv_0 -{ - uint numDirectionalLights_0; - uint numPointLights_0; -}; - - -#line 25 -layout(binding = 0, set = 2) -layout(std140) uniform _S2 -{ - uint numDirectionalLights_0; - uint numPointLights_0; -}gLightEnv_0; - #line 22 -struct PointLight_0 -{ - vec4 position_WS_0; - vec4 colorRange_0; -}; - - -#line 64 -layout(std430, binding = 2, set = 2) readonly buffer StructuredBuffer_PointLight_t_0 { - PointLight_0 _data[]; -} gLightEnv_pointLights_0; +layout(std430, binding = 0) readonly buffer StructuredBuffer_InstanceData_t_0 { + InstanceData_0 _data[]; +} pScene_instances_0; #line 5 1 +layout(std430, binding = 0, set = 2) readonly buffer StructuredBuffer_float3_t_0 { + vec3 _data[]; +} pVertexData_positions_0; + +#line 5 +layout(std430, binding = 1, set = 2) readonly buffer StructuredBuffer_float2_t_0 { + vec2 _data[]; +} pVertexData_texCoords_0; + +#line 5 +layout(std430, binding = 2, set = 2) readonly buffer StructuredBuffer_float3_t_1 { + vec3 _data[]; +} pVertexData_normals_0; + +#line 5 +layout(std430, binding = 3, set = 2) readonly buffer StructuredBuffer_float3_t_2 { + vec3 _data[]; +} pVertexData_tangents_0; + +#line 5 +layout(std430, binding = 4, set = 2) readonly buffer StructuredBuffer_float3_t_3 { + vec3 _data[]; +} pVertexData_biTangents_0; + +#line 5 2 struct ViewParameter_0 { mat4x4 viewMatrix_0; @@ -117,266 +48,131 @@ struct ViewParameter_0 vec2 screenDimensions_0; }; -layout(binding = 0) -layout(std140) uniform _S3 + +#line 12 +layout(binding = 0, set = 1) +layout(std140) uniform _S1 { mat4x4 viewMatrix_0; mat4x4 projectionMatrix_0; vec4 cameraPos_WS_0; vec2 screenDimensions_0; -}viewParams_0; +}pViewParams_0; -#line 39 0 -shared uint uMinDepth_0; +#line 4206 3 +layout(location = 0) +out vec3 _S2; -#line 40 -shared uint uMaxDepth_0; +#line 4206 +layout(location = 1) +out vec3 _S3; - -shared uint oLightCount_0; +#line 4206 +layout(location = 2) +out vec2 _S4; - -shared uint tLightCount_0; +#line 4206 +layout(location = 3) +out vec3 _S5; -#line 42 -shared Frustum_0 groupFrustum_0; +#line 4206 +layout(location = 4) +out vec3 _S6; -#line 50 -shared uint tLightList_0[1024]; +#line 4206 +layout(location = 5) +out vec3 _S7; -#line 63 -void tAppendLight_0(uint lightIndex_0) +#line 4206 +layout(location = 6) +out vec3 _S8; + + +#line 1 4 +struct MaterialParameter_0 { - uint index_0; - ((index_0) = atomicAdd((tLightCount_0), (1U))); - if(index_0 < 1024U) - { - tLightList_0[index_0] = lightIndex_0; + vec3 position_TS_0; + vec3 worldPosition_0; + vec2 texCoords_0; + vec3 normal_0; + vec3 tangent_0; + vec3 biTangent_0; + vec3 viewDir_TS_0; +}; -#line 67 - } +struct VertexAttributes_0 +{ + MaterialParameter_0 parameter_0; + vec4 clipPosition_0; +}; +#line 9 1 +VertexAttributes_0 StaticMeshVertexData_getAttributes_0(uint _S9, mat4x4 _S10) +{ - return; + vec4 worldPos_0 = (((vec4(pVertexData_positions_0._data[_S9], 1.0)) * (_S10))); + + vec4 clipPos_0 = ((((((worldPos_0) * (pViewParams_0.viewMatrix_0)))) * (pViewParams_0.projectionMatrix_0))); + +#line 10 + MaterialParameter_0 params_0; + +#line 15 + params_0.worldPosition_0 = worldPos_0.xyz; + params_0.texCoords_0 = pVertexData_texCoords_0._data[_S9]; + params_0.normal_0 = pVertexData_normals_0._data[_S9]; + params_0.tangent_0 = pVertexData_tangents_0._data[_S9]; + params_0.biTangent_0 = pVertexData_biTangents_0._data[_S9]; + +#line 9 + VertexAttributes_0 attributes_0; + +#line 20 + attributes_0.parameter_0 = params_0; + attributes_0.clipPosition_0 = clipPos_0; + return attributes_0; } -#line 58 3 -vec3 PointLight_getViewPos_0(PointLight_0 this_0) -{ - return (((this_0.position_WS_0) * (viewParams_0.viewMatrix_0))).xyz; -} - - -#line 36 -bool PointLight_insidePlane_0(PointLight_0 this_1, Plane_0 plane_0) -{ - return dot(plane_0.n_0, PointLight_getViewPos_0(this_1).xyz) - plane_0.d_0 < - this_1.colorRange_0.w; -} - - -#line 46 0 -shared uint oLightList_0[1024]; - - -#line 53 -void oAppendLight_0(uint lightIndex_1) -{ - uint index_1; - ((index_1) = atomicAdd((oLightCount_0), (1U))); - if(index_1 < 1024U) - { - oLightList_0[index_1] = lightIndex_1; - -#line 57 - } - - - - return; -} - - -#line 45 -shared uint oLightIndexStartOffset_0; - - - -shared uint tLightIndexStartOffset_0; - - -#line 75 -layout(local_size_x = 32, local_size_y = 32, local_size_z = 1) in; +#line 18 0 void main() { - ivec3 _S4 = ivec3(ivec2(gl_GlobalInvocationID.xy), 0); - uint uDepth_0 = floatBitsToUint((texelFetch((gCullingParams_depthTextureVS_0), ((_S4)).xy, ((_S4)).z)).x); - bool _S5 = gl_LocalInvocationIndex == 0U; +#line 18 + VertexAttributes_0 _S11 = StaticMeshVertexData_getAttributes_0(uint(gl_VertexIndex), pScene_instances_0._data[uint(gl_InstanceIndex)].transformMatrix_0); -#line 81 - if(_S5) - { - uMinDepth_0 = 4294967295U; - uMaxDepth_0 = 0U; - oLightCount_0 = 0U; - tLightCount_0 = 0U; - groupFrustum_0 = gCullingParams_frustums_0._data[gl_WorkGroupID.x + gl_WorkGroupID.y * gCullingParams_0.numThreadGroups_0.x]; +#line 18 + _S2 = _S11.parameter_0.position_TS_0; -#line 81 - } +#line 18 + _S3 = _S11.parameter_0.worldPosition_0; -#line 90 - barrier(); +#line 18 + _S4 = _S11.parameter_0.texCoords_0; - atomicMin((uMinDepth_0), (uDepth_0)); - atomicMax((uMaxDepth_0), (uDepth_0)); +#line 18 + _S5 = _S11.parameter_0.normal_0; - barrier(); +#line 18 + _S6 = _S11.parameter_0.tangent_0; -#line 104 - Plane_0 _S6 = { vec3(0.0, 0.0, -1.0), - uintBitsToFloat(uMinDepth_0) }; +#line 18 + _S7 = _S11.parameter_0.biTangent_0; -#line 125 - uvec2 _S7 = gl_WorkGroupID.xy; +#line 18 + _S8 = _S11.parameter_0.viewDir_TS_0; -#line 125 - uint i_0 = gl_LocalInvocationIndex; +#line 18 + gl_Position = _S11.clipPosition_0; -#line 125 - for(;;) - { - -#line 106 - if(i_0 < gLightEnv_0.numPointLights_0) - { - } - else - { - -#line 106 - break; - } - PointLight_0 light_0 = gLightEnv_pointLights_0._data[i_0]; - - - - tAppendLight_0(i_0); - if(!PointLight_insidePlane_0(light_0, _S6)) - { - oAppendLight_0(i_0); - -#line 113 - } - -#line 106 - i_0 = i_0 + 1024U; - -#line 106 - } - -#line 120 - barrier(); - - if(_S5) - { - ((oLightIndexStartOffset_0) = atomicAdd((gCullingParams_oLightIndexCounter_0._data[0U]), (oLightCount_0))); - imageStore((gCullingParams_oLightGrid_0), ivec2((_S7)), uvec4(uvec2(oLightIndexStartOffset_0, oLightCount_0), uint(0), uint(0))); - - ((tLightIndexStartOffset_0) = atomicAdd((gCullingParams_tLightIndexCounter_0._data[0U]), (tLightCount_0))); - imageStore((gCullingParams_tLightGrid_0), ivec2((_S7)), uvec4(uvec2(tLightIndexStartOffset_0, tLightCount_0), uint(0), uint(0))); - -#line 122 - } - -#line 130 - barrier(); - -#line 130 - uint k_0; - -#line 130 - if(gl_LocalInvocationIndex < oLightCount_0) - { - -#line 130 - k_0 = gl_LocalInvocationIndex; - -#line 130 - for(;;) - { - - - gCullingParams_oLightIndexList_0._data[oLightIndexStartOffset_0 + k_0] = oLightList_0[k_0]; - -#line 132 - uint j_0 = k_0 + 1024U; - -#line 132 - if(j_0 < oLightCount_0) - { - } - else - { - -#line 132 - break; - } - -#line 132 - k_0 = j_0; - -#line 132 - } - -#line 132 - } - -#line 132 - if(gl_LocalInvocationIndex < tLightCount_0) - { - -#line 132 - k_0 = gl_LocalInvocationIndex; - -#line 132 - for(;;) - { - -#line 140 - gCullingParams_tLightIndexList_0._data[tLightIndexStartOffset_0 + k_0] = tLightList_0[k_0]; - -#line 138 - uint k_1 = k_0 + 1024U; - -#line 138 - if(k_1 < tLightCount_0) - { - } - else - { - -#line 138 - break; - } - -#line 138 - k_0 = k_1; - -#line 138 - } - -#line 138 - } - -#line 144 +#line 18 return; } diff --git a/src/Editor/Asset/MeshLoader.cpp b/src/Editor/Asset/MeshLoader.cpp index 4e4f69c..f748e4f 100644 --- a/src/Editor/Asset/MeshLoader.cpp +++ b/src/Editor/Asset/MeshLoader.cpp @@ -239,6 +239,10 @@ void MeshLoader::loadGlobalMeshes(const aiScene* scene, const ArrayloadMesh(id, meshlets); diff --git a/src/Engine/Component/Component.h b/src/Engine/Component/Component.h index 1e593d3..bcc0c90 100644 --- a/src/Engine/Component/Component.h +++ b/src/Engine/Component/Component.h @@ -8,7 +8,9 @@ template struct Dependencies; template<> struct Dependencies<> -{}; +{ + int x; +}; template struct Dependencies : public Dependencies { @@ -17,6 +19,7 @@ struct Dependencies : public Dependencies { return Dependencies(); } + int x; }; namespace Component @@ -24,16 +27,15 @@ namespace Component template static int accessComponent(Comp& comp); } - -template -concept is_component = std::same_as>; +template +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 dependencies; + constexpr static Dependencies dependencies; #define DECLARE_COMPONENT(x) \ thread_local extern x* tl_##x; \ diff --git a/src/Engine/Component/Transform.cpp b/src/Engine/Component/Transform.cpp index 82173f3..a62c547 100644 --- a/src/Engine/Component/Transform.cpp +++ b/src/Engine/Component/Transform.cpp @@ -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; } \ No newline at end of file diff --git a/src/Engine/Component/Transform.h b/src/Engine/Component/Transform.h index 6809a34..dd1187c 100644 --- a/src/Engine/Component/Transform.h +++ b/src/Engine/Component/Transform.h @@ -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) diff --git a/src/Engine/Graphics/Initializer.h b/src/Engine/Graphics/Initializer.h index 6061895..1efe867 100644 --- a/src/Engine/Graphics/Initializer.h +++ b/src/Engine/Graphics/Initializer.h @@ -115,7 +115,7 @@ struct ShaderCreateInfo Array additionalModules; std::string name; // Debug info std::string entryPoint; - Array typeParameter; + Array> typeParameter; Map defines; }; diff --git a/src/Engine/Graphics/RenderPass/BasePass.h b/src/Engine/Graphics/RenderPass/BasePass.h index 07bd30e..34affe5 100644 --- a/src/Engine/Graphics/RenderPass/BasePass.h +++ b/src/Engine/Graphics/RenderPass/BasePass.h @@ -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 diff --git a/src/Engine/Graphics/RenderPass/DepthPrepass.cpp b/src/Engine/Graphics/RenderPass/DepthPrepass.cpp index 376fcd1..9a7fa57 100644 --- a/src/Engine/Graphics/RenderPass/DepthPrepass.cpp +++ b/src/Engine/Graphics/RenderPass/DepthPrepass.cpp @@ -49,9 +49,10 @@ void DepthPrepass::render() permutation.setVertexFile("LegacyBasePass"); } graphics->beginRenderPass(renderPass); + Array 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(); } diff --git a/src/Engine/Graphics/RenderPass/DepthPrepass.h b/src/Engine/Graphics/RenderPass/DepthPrepass.h index 54c0f52..4e556cf 100644 --- a/src/Engine/Graphics/RenderPass/DepthPrepass.h +++ b/src/Engine/Graphics/RenderPass/DepthPrepass.h @@ -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; diff --git a/src/Engine/Graphics/RenderPass/RenderPass.h b/src/Engine/Graphics/RenderPass/RenderPass.h index b921d28..3a88682 100644 --- a/src/Engine/Graphics/RenderPass/RenderPass.h +++ b/src/Engine/Graphics/RenderPass/RenderPass.h @@ -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; diff --git a/src/Engine/Graphics/Shader.cpp b/src/Engine/Graphics/Shader.cpp index 663cbef..c5e4d0a 100644 --- a/src/Engine/Graphics/Shader.cpp +++ b/src/Engine/Graphics/Shader.cpp @@ -87,12 +87,12 @@ ShaderCollection& ShaderCompiler::createShaders(ShaderPermutation permutation) ShaderCollection collection; ShaderCreateInfo createInfo; - createInfo.typeParameter = { permutation.vertexDataName }; + createInfo.typeParameter = { Pair("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("IMaterial", permutation.materialName)); } createInfo.additionalModules.add(permutation.vertexDataName); createInfo.additionalModules.add(permutation.vertexMeshFile); diff --git a/src/Engine/Graphics/VertexData.cpp b/src/Engine/Graphics/VertexData.cpp index 91950bd..26f7e23 100644 --- a/src/Engine/Graphics/VertexData.cpp +++ b/src/Engine/Graphics/VertexData.cpp @@ -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; + Array 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 loadedMeshlets) { meshData[id].clear(); @@ -65,79 +113,33 @@ void VertexData::loadMesh(MeshId id, Array loadedMeshlets) .numMeshlets = numMeshlets, .meshletOffset = meshletOffset, .indicesOffset = static_cast(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; - Array 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) diff --git a/src/Engine/Graphics/VertexData.h b/src/Engine/Graphics/VertexData.h index f5c81c0..e8e884e 100644 --- a/src/Engine/Graphics/VertexData.h +++ b/src/Engine/Graphics/VertexData.h @@ -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 meshlets); void createDescriptors(); + void loadMesh(MeshId id, Array meshlets); MeshId allocateVertexData(uint64 numVertices); uint64 getMeshOffset(MeshId id); uint64 getMeshVertexCount(MeshId id); diff --git a/src/Engine/Graphics/Vulkan/Allocator.cpp b/src/Engine/Graphics/Vulkan/Allocator.cpp index c6c60c1..92e72fe 100644 --- a/src/Engine/Graphics/Vulkan/Allocator.cpp +++ b/src/Engine/Graphics/Vulkan/Allocator.cpp @@ -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 ); diff --git a/src/Engine/Graphics/Vulkan/Allocator.h b/src/Engine/Graphics/Vulkan/Allocator.h index 4fce498..dc24d1e 100644 --- a/src/Engine/Graphics/Vulkan/Allocator.h +++ b/src/Engine/Graphics/Vulkan/Allocator.h @@ -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; }; diff --git a/src/Engine/Graphics/Vulkan/Shader.cpp b/src/Engine/Graphics/Vulkan/Shader.cpp index 2862595..fe6f143 100644 --- a/src/Engine/Graphics/Vulkan/Shader.cpp +++ b/src/Engine/Graphics/Vulkan/Shader.cpp @@ -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 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 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 conformingModule; - session->createCompositeComponentType(modules.data(), modules.size(), conformingModule.writeRef(), diagnostics.writeRef()); - */ Slang::ComPtr linkedProgram; moduleComposition->link(linkedProgram.writeRef(), diagnostics.writeRef()); if(diagnostics) @@ -122,9 +107,9 @@ void Shader::create(const ShaderCreateInfo& createInfo) } Array 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 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()); diff --git a/src/Engine/Math/Transform.cpp b/src/Engine/Math/Transform.cpp index 6c11c56..3ef757d 100644 --- a/src/Engine/Math/Transform.cpp +++ b/src/Engine/Math/Transform.cpp @@ -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 diff --git a/src/Engine/Serialization/Serialization.h b/src/Engine/Serialization/Serialization.h index af802ef..442d4c9 100644 --- a/src/Engine/Serialization/Serialization.h +++ b/src/Engine/Serialization/Serialization.h @@ -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 static void save(ArchiveBuffer& buffer, const T& type) diff --git a/src/Engine/System/CMakeLists.txt b/src/Engine/System/CMakeLists.txt index 1a6ed51..56fbb1b 100644 --- a/src/Engine/System/CMakeLists.txt +++ b/src/Engine/System/CMakeLists.txt @@ -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 diff --git a/src/Engine/System/CameraUpdater.cpp b/src/Engine/System/CameraUpdater.cpp new file mode 100644 index 0000000..9bbbf6d --- /dev/null +++ b/src/Engine/System/CameraUpdater.cpp @@ -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(); +} diff --git a/src/Engine/System/CameraUpdater.h b/src/Engine/System/CameraUpdater.h new file mode 100644 index 0000000..98ad461 --- /dev/null +++ b/src/Engine/System/CameraUpdater.h @@ -0,0 +1,19 @@ +#pragma once +#include "ComponentSystem.h" +#include "Component/Camera.h" + +namespace Seele +{ +namespace System +{ +class CameraUpdater : public ComponentSystem +{ +public: + CameraUpdater(PScene scene); + virtual ~CameraUpdater(); + + virtual void update(Component::Camera& camera); +private: +}; +} +} \ No newline at end of file diff --git a/src/Engine/System/ComponentSystem.h b/src/Engine/System/ComponentSystem.h index 90678dc..7f922a7 100644 --- a/src/Engine/System/ComponentSystem.h +++ b/src/Engine/System/ComponentSystem.h @@ -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 + template auto getDependencies() { return Comp::dependencies; diff --git a/src/Engine/Window/GameView.cpp b/src/Engine/Window/GameView.cpp index db10ac6..55c98c6 100644 --- a/src/Engine/Window/GameView.cpp +++ b/src/Engine/Window/GameView.cpp @@ -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)