From 3c9465de4931c228283b8924651018d74ae7cbd8 Mon Sep 17 00:00:00 2001 From: Dynamitos Date: Sat, 2 Nov 2024 22:19:08 +0100 Subject: [PATCH] More metal changes --- res/shaders/lib/DispatchParams.slang | 4 +-- res/shaders/lib/MaterialParameter.slang | 25 +++++++------------ res/shaders/lib/Scene.slang | 4 +-- src/Engine/Graphics/Enums.h | 4 +-- src/Engine/Graphics/Metal/PipelineCache.mm | 25 +++++++++++++++++-- src/Engine/Graphics/Metal/Window.mm | 2 +- .../Graphics/RenderPass/DepthCullingPass.cpp | 9 +++++-- .../Graphics/RenderPass/LightCullingPass.cpp | 3 ++- src/Engine/Graphics/RenderPass/RenderPass.cpp | 2 +- src/Engine/Scene/LightEnvironment.cpp | 2 +- 10 files changed, 50 insertions(+), 30 deletions(-) diff --git a/res/shaders/lib/DispatchParams.slang b/res/shaders/lib/DispatchParams.slang index a9798b5..8b3707b 100644 --- a/res/shaders/lib/DispatchParams.slang +++ b/res/shaders/lib/DispatchParams.slang @@ -4,8 +4,8 @@ struct DispatchParams { uint3 numThreadGroups; uint pad0; - uint3 numThreads; - uint pad1; + uint3 numThreads; + uint pad1; RWStructuredBuffer frustums; }; diff --git a/res/shaders/lib/MaterialParameter.slang b/res/shaders/lib/MaterialParameter.slang index 1bd86f5..1783769 100644 --- a/res/shaders/lib/MaterialParameter.slang +++ b/res/shaders/lib/MaterialParameter.slang @@ -21,15 +21,15 @@ struct FragmentParameter { float4 position_CS : SV_Position; #ifndef POS_ONLY - float3 normal_WS : NORMAL0; - float3 tangent_WS : TANGENT0; - float3 biTangent_WS : TANGENT1; - float3 position_WS : POSITION2; - float3 vertexColor : COLOR0; - float4 texCoords0 : TEXCOORD0; - float4 texCoords1 : TEXCOORD1; - float4 texCoords2 : TEXCOORD2; - float4 texCoords3 : TEXCOORD3; + float3 normal_WS : NORMALWS; + float3 tangent_WS : TANGENTWS; + float3 biTangent_WS : BITANGENTWS; + float3 position_WS : POSITIONWS; + float3 vertexColor : COLOR; + float4 texCoords0 : TEXCOORDS0; + float4 texCoords1 : TEXCOORDS1; + float4 texCoords2 : TEXCOORDS2; + float4 texCoords3 : TEXCOORDS3; MaterialParameter getMaterialParameter() { MaterialParameter result; @@ -80,13 +80,6 @@ struct FragmentParameter } }; -// data passed to visibility render -struct VisibilityParameter -{ - uint32_t triangleIndex : POSITION5; - uint32_t meshletId : POSITION6; -}; - // data retrieved from VertexData struct VertexAttributes { diff --git a/res/shaders/lib/Scene.slang b/res/shaders/lib/Scene.slang index b5d6ab8..b18676d 100644 --- a/res/shaders/lib/Scene.slang +++ b/res/shaders/lib/Scene.slang @@ -20,8 +20,8 @@ struct MeshData uint32_t numIndices; }; -static const uint32_t MAX_VERTICES = 256; -static const uint32_t MAX_PRIMITIVES = 256; +static const uint32_t MAX_VERTICES = 64; +static const uint32_t MAX_PRIMITIVES = 128; static const uint32_t MAX_MESHLETS_PER_INSTANCE = 2048; struct InstanceData diff --git a/src/Engine/Graphics/Enums.h b/src/Engine/Graphics/Enums.h index b900614..7b3e5ad 100644 --- a/src/Engine/Graphics/Enums.h +++ b/src/Engine/Graphics/Enums.h @@ -159,8 +159,8 @@ static constexpr bool useAsyncCompute = false; static constexpr bool useMeshShading = true; static constexpr uint32 numFramesBuffered = 3; -static constexpr uint32 numVerticesPerMeshlet = 256; -static constexpr uint32 numPrimitivesPerMeshlet = 256; +static constexpr uint32 numVerticesPerMeshlet = 64; +static constexpr uint32 numPrimitivesPerMeshlet = 126; double getCurrentFrameDelta(); double getCurrentFrameTime(); uint32 getCurrentFrameIndex(); diff --git a/src/Engine/Graphics/Metal/PipelineCache.mm b/src/Engine/Graphics/Metal/PipelineCache.mm index 7bdc3d2..be074de 100644 --- a/src/Engine/Graphics/Metal/PipelineCache.mm +++ b/src/Engine/Graphics/Metal/PipelineCache.mm @@ -1,6 +1,7 @@ #include "PipelineCache.h" #include "Descriptor.h" #include "Enums.h" +#include "RenderPass.h" #include "Foundation/NSError.hpp" #include "Foundation/NSString.hpp" #include "Graphics.h" @@ -25,8 +26,7 @@ PipelineCache::PipelineCache(PGraphics graphics, const std::string& name) : grap PipelineCache::~PipelineCache() {} PGraphicsPipeline PipelineCache::createPipeline(Gfx::LegacyPipelineCreateInfo createInfo) { - PPipelineLayout layout = Gfx::PPipelineLayout(createInfo.pipelineLayout).cast(); - + PRenderPass renderPass = createInfo.renderPass.cast(); MTL::RenderPipelineDescriptor* pipelineDescriptor = MTL::RenderPipelineDescriptor::alloc()->init(); MTL::VertexDescriptor* vertexDescriptor = pipelineDescriptor->vertexDescriptor()->init(); @@ -68,6 +68,16 @@ PGraphicsPipeline PipelineCache::createPipeline(Gfx::LegacyPipelineCreateInfo cr pipelineDescriptor->setFragmentFunction(createInfo.fragmentShader.cast()->getFunction()); } pipelineDescriptor->setInputPrimitiveTopology(cast(createInfo.topology)); + for(uint c = 0; c < renderPass->getLayout().colorAttachments.size(); ++c) { + const auto& color = renderPass->getLayout().colorAttachments[c]; + MTL::RenderPipelineColorAttachmentDescriptor* desc = MTL::RenderPipelineColorAttachmentDescriptor::alloc()->init(); + desc->setWriteMask(MTL::ColorWriteMaskAll); + desc->setPixelFormat(cast(color.getFormat())); + desc->setAlphaBlendOperation(MTL::BlendOperationAdd); + desc->setDestinationAlphaBlendFactor(MTL::BlendFactorDestinationAlpha); + desc->setBlendingEnabled(false); + pipelineDescriptor->colorAttachments()->setObject(desc, c); + } if (createInfo.renderPass->getLayout().depthAttachment.getTexture() != nullptr) { pipelineDescriptor->setDepthAttachmentPixelFormat( cast(createInfo.renderPass->getLayout().depthAttachment.getTexture().cast()->getFormat())); @@ -131,6 +141,7 @@ PGraphicsPipeline PipelineCache::createPipeline(Gfx::LegacyPipelineCreateInfo cr } PGraphicsPipeline PipelineCache::createPipeline(Gfx::MeshPipelineCreateInfo createInfo) { + PRenderPass renderPass = createInfo.renderPass.cast(); MTL::MeshRenderPipelineDescriptor* pipelineDescriptor = MTL::MeshRenderPipelineDescriptor::alloc()->init(); pipelineDescriptor->setMeshFunction(createInfo.meshShader.cast()->getFunction()); @@ -140,6 +151,16 @@ PGraphicsPipeline PipelineCache::createPipeline(Gfx::MeshPipelineCreateInfo crea if (createInfo.fragmentShader != nullptr) { pipelineDescriptor->setFragmentFunction(createInfo.fragmentShader.cast()->getFunction()); } + for(uint c = 0; c < renderPass->getLayout().colorAttachments.size(); ++c) { + const auto& color = renderPass->getLayout().colorAttachments[c]; + MTL::RenderPipelineColorAttachmentDescriptor* desc = MTL::RenderPipelineColorAttachmentDescriptor::alloc()->init(); + desc->setWriteMask(MTL::ColorWriteMaskAll); + desc->setPixelFormat(cast(color.getFormat())); + desc->setAlphaBlendOperation(MTL::BlendOperationAdd); + desc->setDestinationAlphaBlendFactor(MTL::BlendFactorDestinationAlpha); + desc->setBlendingEnabled(false); + pipelineDescriptor->colorAttachments()->setObject(desc, c); + } if (createInfo.renderPass->getLayout().depthAttachment.getTexture() != nullptr) { pipelineDescriptor->setDepthAttachmentPixelFormat( cast(createInfo.renderPass->getLayout().depthAttachment.getTexture().cast()->getFormat())); diff --git a/src/Engine/Graphics/Metal/Window.mm b/src/Engine/Graphics/Metal/Window.mm index 269f72f..ed4600b 100644 --- a/src/Engine/Graphics/Metal/Window.mm +++ b/src/Engine/Graphics/Metal/Window.mm @@ -90,7 +90,7 @@ Window::Window(PGraphics graphics, const WindowCreateInfo& createInfo) : graphic metalLayer.drawableSize = CGSizeMake(createInfo.width, createInfo.height); metalWindow.contentView.layer = metalLayer; metalWindow.contentView.wantsLayer = YES; - framebufferFormat = Gfx::SE_FORMAT_R8G8B8A8_UNORM; + framebufferFormat = Gfx::SE_FORMAT_B8G8R8A8_UNORM; drawable = (__bridge CA::MetalDrawable*)[metalLayer nextDrawable]; createBackBuffer(); diff --git a/src/Engine/Graphics/RenderPass/DepthCullingPass.cpp b/src/Engine/Graphics/RenderPass/DepthCullingPass.cpp index 8d589df..199b769 100644 --- a/src/Engine/Graphics/RenderPass/DepthCullingPass.cpp +++ b/src/Engine/Graphics/RenderPass/DepthCullingPass.cpp @@ -176,9 +176,14 @@ void DepthCullingPass::render() { command->bindPipeline(pipeline); } command->bindDescriptor({viewParamsSet, vertexData->getVertexDataSet(), vertexData->getInstanceDataSet(), set}); - uint32 offset = 0; + VertexData::DrawCallOffsets offsets = { + .instanceOffset = 0, + .floatOffset = 0, + .samplerOffset = 0, + .textureOffset = 0, + }; command->pushConstants(Gfx::SE_SHADER_STAGE_TASK_BIT_EXT | Gfx::SE_SHADER_STAGE_VERTEX_BIT, 0, sizeof(VertexData::DrawCallOffsets), - &offset); + &offsets); if (graphics->supportMeshShading()) { command->drawMesh(vertexData->getNumInstances(), 1, 1); } else { diff --git a/src/Engine/Graphics/RenderPass/LightCullingPass.cpp b/src/Engine/Graphics/RenderPass/LightCullingPass.cpp index c359987..84b9a34 100644 --- a/src/Engine/Graphics/RenderPass/LightCullingPass.cpp +++ b/src/Engine/Graphics/RenderPass/LightCullingPass.cpp @@ -248,7 +248,7 @@ void LightCullingPass::setupFrustums() { dispatchParamsLayout->addDescriptorBinding(Gfx::DescriptorBinding{ .binding = 0, .descriptorType = Gfx::SE_DESCRIPTOR_TYPE_UNIFORM_BUFFER, - .uniformLength = sizeof(DispatchParams), + .uniformLength = 56,//sizeof(DispatchParams), }); dispatchParamsLayout->addDescriptorBinding(Gfx::DescriptorBinding{ .binding = 1, @@ -264,6 +264,7 @@ void LightCullingPass::setupFrustums() { .modules = {"ComputeFrustums"}, .entryPoints = {{"computeFrustums", "ComputeFrustums"}}, .rootSignature = frustumLayout, + .dumpIntermediate = true, }; graphics->beginShaderCompilation(createInfo); frustumShader = graphics->createComputeShader({0}); diff --git a/src/Engine/Graphics/RenderPass/RenderPass.cpp b/src/Engine/Graphics/RenderPass/RenderPass.cpp index bab5c46..ae67932 100644 --- a/src/Engine/Graphics/RenderPass/RenderPass.cpp +++ b/src/Engine/Graphics/RenderPass/RenderPass.cpp @@ -7,7 +7,7 @@ RenderPass::RenderPass(Gfx::PGraphics graphics, PScene scene) : graphics(graphic viewParamsLayout->addDescriptorBinding(Gfx::DescriptorBinding{ .binding = 0, .descriptorType = Gfx::SE_DESCRIPTOR_TYPE_UNIFORM_BUFFER, - .uniformLength = sizeof(ViewParameter), + .uniformLength = 576,//sizeof(ViewParameter), }); UniformBufferCreateInfo uniformInitializer = { .sourceData = diff --git a/src/Engine/Scene/LightEnvironment.cpp b/src/Engine/Scene/LightEnvironment.cpp index c18936f..fde7a6a 100644 --- a/src/Engine/Scene/LightEnvironment.cpp +++ b/src/Engine/Scene/LightEnvironment.cpp @@ -8,7 +8,7 @@ LightEnvironment::LightEnvironment(Gfx::PGraphics graphics) : graphics(graphics) layout->addDescriptorBinding(Gfx::DescriptorBinding{ .binding = 0, .descriptorType = Gfx::SE_DESCRIPTOR_TYPE_UNIFORM_BUFFER, - .uniformLength = sizeof(LightEnv) + .uniformLength = 24,//sizeof(LightEnv) }); layout->addDescriptorBinding(Gfx::DescriptorBinding{ .binding = 1,