From a957b0561593df6d4bd9eb5a7179c2b466552b0d Mon Sep 17 00:00:00 2001 From: Dynamitos Date: Mon, 10 Mar 2025 18:35:35 +0100 Subject: [PATCH] adding basic tone mapping --- res/shaders/DepthCullingTask.slang | 4 +- res/shaders/FullscreenQuad.slang | 15 +++ res/shaders/LegacyPass.slang | 2 +- res/shaders/ToneMapping.slang | 117 ++++++++++++++++++ src/Benchmark/main.cpp | 1 - src/Editor/Asset/TextureLoader.cpp | 2 +- src/Editor/main.cpp | 9 +- src/Engine/Graphics/Initializer.h | 1 - src/Engine/Graphics/RenderPass/BasePass.cpp | 24 ++-- src/Engine/Graphics/RenderPass/CMakeLists.txt | 3 + .../Graphics/RenderPass/TerrainRenderer.cpp | 2 +- .../Graphics/RenderPass/ToneMappingPass.cpp | 112 +++++++++++++++++ .../Graphics/RenderPass/ToneMappingPass.h | 33 +++++ .../Graphics/RenderPass/WaterRenderer.cpp | 2 +- src/Engine/Graphics/RenderTarget.h | 3 +- src/Engine/Graphics/VertexData.cpp | 13 +- src/Engine/Graphics/Vulkan/RenderPass.cpp | 2 +- src/Engine/Graphics/Window.cpp | 2 +- src/Engine/Graphics/Window.h | 2 - src/Engine/Window/GameView.cpp | 2 + 20 files changed, 314 insertions(+), 37 deletions(-) create mode 100644 res/shaders/FullscreenQuad.slang create mode 100644 res/shaders/ToneMapping.slang create mode 100644 src/Engine/Graphics/RenderPass/ToneMappingPass.cpp create mode 100644 src/Engine/Graphics/RenderPass/ToneMappingPass.h diff --git a/res/shaders/DepthCullingTask.slang b/res/shaders/DepthCullingTask.slang index e98a2ac..1269b05 100644 --- a/res/shaders/DepthCullingTask.slang +++ b/res/shaders/DepthCullingTask.slang @@ -123,11 +123,11 @@ void taskMain( if(!culling.wasVisible()) { // if the meshlet is outside of the frustum, we skip it since we cant do depth culling anyways - if(meshlet.bounding.insideFrustum(viewFrustum)) + //if(meshlet.bounding.insideFrustum(viewFrustum)) { #ifdef DEPTH_CULLING // if the meshlet bounding box is behind the cached depth buffer, we skip - if(isBoxVisible(meshlet.bounding)) + //if(isBoxVisible(meshlet.bounding)) #endif { uint index; diff --git a/res/shaders/FullscreenQuad.slang b/res/shaders/FullscreenQuad.slang new file mode 100644 index 0000000..ada8674 --- /dev/null +++ b/res/shaders/FullscreenQuad.slang @@ -0,0 +1,15 @@ +struct QuadOutput +{ + float2 uv : UV; + float4 position : SV_Position; +} + +[shader("vertex")] +QuadOutput quadMain(uint vertexIndex : SV_VertexID) +{ + QuadOutput result; + result.uv = float2((vertexIndex << 1) & 2, vertexIndex & 2); + result.position = float4(result.uv * 2.0f + -1.0f, 0.0f, 1.0f); + result.uv.y = 1 - result.uv.y; + return result; +} \ No newline at end of file diff --git a/res/shaders/LegacyPass.slang b/res/shaders/LegacyPass.slang index 5038305..7bbe099 100644 --- a/res/shaders/LegacyPass.slang +++ b/res/shaders/LegacyPass.slang @@ -10,5 +10,5 @@ FragmentParameter vertexMain( ){ InstanceData inst = pScene.instances[input.instanceId]; VertexAttributes attr = pVertexData.getAttributes(input.vertexId); - return attr.getParameter(inst.transformMatrix); + return attr.getParameter(inst.transformMatrix, inst.inverseTransformMatrix); } \ No newline at end of file diff --git a/res/shaders/ToneMapping.slang b/res/shaders/ToneMapping.slang new file mode 100644 index 0000000..242be48 --- /dev/null +++ b/res/shaders/ToneMapping.slang @@ -0,0 +1,117 @@ +// MIT License +// +// Copyright (c) 2024 Missing Deadlines (Benjamin Wrensch) +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +// SOFTWARE. + +// All values used to derive this implementation are sourced from Troy�s initial AgX implementation/OCIO config file available here: +// https://github.com/sobotka/AgX + +struct Parameters +{ + float4 offset = float4(0.0); + float4 slope = float4(1.0); + float4 power = float4(1.0); + float sat = 1.0; + Texture2D hdrInputTexture; + SamplerState hdrSampler; +}; +ParameterBlock pParams; + +// AgX +// -> + +// Mean error^2: 3.6705141e-06 +float3 agxDefaultContrastApprox(float3 x) { + float3 x2 = x * x; + float3 x4 = x2 * x2; + + return + 15.5 * x4 * x2 + - 40.14 * x4 * x + + 31.96 * x4 + - 6.868 * x2 * x + + 0.4298 * x2 + + 0.1191 * x + - 0.00232; +} + +float3 agx(float3 val) { + const float3x3 agx_mat = transpose(float3x3( + 0.842479062253094, 0.0423282422610123, 0.0423756549057051, + 0.0784335999999992, 0.878468636469772, 0.0784336, + 0.0792237451477643, 0.0791661274605434, 0.879142973793104)); + + const float min_ev = -12.47393f; + const float max_ev = 4.026069f; + + // Input transform + val = mul(agx_mat, val); + + // Log2 space encoding + val = clamp(log2(val), min_ev, max_ev); + val = (val - min_ev) / (max_ev - min_ev); + + // Apply sigmoid function approximation + val = agxDefaultContrastApprox(val); + + return val; +} + +float3 agxEotf(float3 val) { + const float3x3 agx_mat_inv = transpose(float3x3( + 1.19687900512017, -0.0528968517574562, -0.0529716355144438, + -0.0980208811401368, 1.15190312990417, -0.0980434501171241, + -0.0990297440797205, -0.0989611768448433, 1.15107367264116)); + + // Undo input transform + val = mul(agx_mat_inv, val); + + // sRGB IEC 61966-2-1 2.2 Exponent Reference EOTF Display + //val = pow(val, float3(2.2)); + + return val; +} + +float3 agxLook(float3 val) { + const float3 lw = float3(0.2126, 0.7152, 0.0722); + float luma = dot(val, lw); + + // Default + float3 offset = pParams.offset.xyz; + float3 slope = pParams.slope.xyz; + float3 power = pParams.power.xyz; + float sat = pParams.sat; + + // ASC CDL + val = pow(val * slope + offset, power); + return luma + sat * (val - luma); +} + +[shader("pixel")] +float4 toneMapping(float2 uv : UV) : SV_Target +{ + float3 hdrValue = pParams.hdrInputTexture.Sample(pParams.hdrSampler, uv).rgb; + //float3 value = agx(hdrValue); + //value = agxLook(value); + //value = agxEotf(value); + + float3 value = hdrValue / (hdrValue + float3(10)); + + return float4(value, 1); +} \ No newline at end of file diff --git a/src/Benchmark/main.cpp b/src/Benchmark/main.cpp index 172af20..695e3d8 100644 --- a/src/Benchmark/main.cpp +++ b/src/Benchmark/main.cpp @@ -56,7 +56,6 @@ int main(int argc, char** argv) { .size = {1920, 1080}, .offset = {0, 0}, }, - .numSamples = Gfx::SE_SAMPLE_COUNT_4_BIT, }; OGameView sceneView = new PlayView(graphics, window, sceneViewInfo, binaryPath.generic_string(), useDepthCulling); sceneView->setFocused(); diff --git a/src/Editor/Asset/TextureLoader.cpp b/src/Editor/Asset/TextureLoader.cpp index 8330c32..bf24c20 100644 --- a/src/Editor/Asset/TextureLoader.cpp +++ b/src/Editor/Asset/TextureLoader.cpp @@ -60,7 +60,7 @@ void TextureLoader::import(TextureImportArgs args, PTextureAsset textureAsset) { int totalWidth = 0, totalHeight = 0, n = 0; unsigned char* data = stbi_load(args.filePath.string().c_str(), &totalWidth, &totalHeight, &n, 4); ktxTexture2* kTexture = nullptr; - VkFormat format = VK_FORMAT_R8G8B8A8_UNORM; + VkFormat format = VK_FORMAT_R8G8B8A8_SRGB; ktxTextureCreateInfo createInfo = { .vkFormat = (uint32)format, .baseDepth = 1, diff --git a/src/Editor/main.cpp b/src/Editor/main.cpp index 036d38f..5f0053f 100644 --- a/src/Editor/main.cpp +++ b/src/Editor/main.cpp @@ -112,10 +112,10 @@ int main() { // AssetImporter::importTexture(TextureImportArgs{ // .filePath = sourcePath / "import/textures/wgen.png", //}); - // AssetImporter::importMesh(MeshImportArgs{ - // .filePath = sourcePath / "import/models/after-the-rain-vr-sound/source/Whitechapel.glb", - // .importPath = "Whitechapel", - //}); + AssetImporter::importMesh(MeshImportArgs{ + .filePath = sourcePath / "import/models/after-the-rain-vr-sound/source/Whitechapel.glb", + .importPath = "Whitechapel", + }); //AssetImporter::importMesh(MeshImportArgs{ // .filePath = sourcePath / "import/models/box.glb", // .importPath = "", @@ -143,7 +143,6 @@ int main() { .size = {1920, 1080}, .offset = {0, 0}, }, - .numSamples = Gfx::SE_SAMPLE_COUNT_4_BIT, }; OGameView sceneView = new Editor::PlayView(graphics, window, sceneViewInfo, binaryPath.generic_string()); sceneView->setFocused(); diff --git a/src/Engine/Graphics/Initializer.h b/src/Engine/Graphics/Initializer.h index 7ab9916..4889cb8 100644 --- a/src/Engine/Graphics/Initializer.h +++ b/src/Engine/Graphics/Initializer.h @@ -35,7 +35,6 @@ struct WindowCreateInfo { struct ViewportCreateInfo { URect dimensions; float fieldOfView = glm::radians(70.0f); - Gfx::SeSampleCountFlags numSamples; }; // doesnt own the data, only proxy it struct DataSource { diff --git a/src/Engine/Graphics/RenderPass/BasePass.cpp b/src/Engine/Graphics/RenderPass/BasePass.cpp index 20c9905..bf215e2 100644 --- a/src/Engine/Graphics/RenderPass/BasePass.cpp +++ b/src/Engine/Graphics/RenderPass/BasePass.cpp @@ -189,7 +189,7 @@ void BasePass::render() { .pipelineLayout = collection->pipelineLayout, .multisampleState = { - .samples = viewport->getSamples(), + .samples = msColorAttachment.getNumSamples(), }, .rasterizationState = { @@ -210,7 +210,7 @@ void BasePass::render() { .pipelineLayout = collection->pipelineLayout, .multisampleState = { - .samples = viewport->getSamples(), + .samples = msColorAttachment.getNumSamples(), }, .rasterizationState = { @@ -231,7 +231,7 @@ void BasePass::render() { Gfx::SE_SHADER_STAGE_FRAGMENT_BIT, 0, sizeof(VertexData::DrawCallOffsets), &drawCall.offsets); if (graphics->supportMeshShading()) { - //command->drawMesh(drawCall.instanceMeshData.size(), 1, 1); + command->drawMesh(drawCall.instanceMeshData.size(), 1, 1); } else { command->bindIndexBuffer(vertexData->getIndexBuffer()); for (const auto& meshData : drawCall.instanceMeshData) { @@ -287,7 +287,7 @@ void BasePass::render() { .pipelineLayout = collection->pipelineLayout, .multisampleState = { - .samples = viewport->getSamples(), + .samples = msColorAttachment.getNumSamples(), }, .rasterizationState = { @@ -314,7 +314,7 @@ void BasePass::render() { .pipelineLayout = collection->pipelineLayout, .multisampleState = { - .samples = viewport->getSamples(), + .samples = msColorAttachment.getNumSamples(), }, .rasterizationState = { @@ -345,7 +345,7 @@ void BasePass::render() { Gfx::SE_SHADER_STAGE_FRAGMENT_BIT, 0, sizeof(VertexData::DrawCallOffsets), &t.offsets); if (graphics->supportMeshShading()) { - //transparentCommand->drawMesh(1, 1, 1); + transparentCommand->drawMesh(1, 1, 1); } else { // command->bindIndexBuffer(t.vertexData->getIndexBuffer()); // for (const auto& meshData : drawCall.instanceMeshData) { @@ -396,7 +396,7 @@ void BasePass::publishOutputs() { .format = Gfx::SE_FORMAT_D32_SFLOAT, .width = viewport->getOwner()->getFramebufferWidth(), .height = viewport->getOwner()->getFramebufferHeight(), - .samples = viewport->getSamples(), + .samples = Gfx::SE_SAMPLE_COUNT_4_BIT, .usage = Gfx::SE_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT | Gfx::SE_IMAGE_USAGE_TRANSIENT_ATTACHMENT_BIT, }); @@ -404,7 +404,7 @@ void BasePass::publishOutputs() { .format = Gfx::SE_FORMAT_R32G32B32A32_SFLOAT, .width = viewport->getOwner()->getFramebufferWidth(), .height = viewport->getOwner()->getFramebufferHeight(), - .samples = viewport->getSamples(), + .samples = Gfx::SE_SAMPLE_COUNT_4_BIT, // todo: configure .usage = Gfx::SE_IMAGE_USAGE_COLOR_ATTACHMENT_BIT | Gfx::SE_IMAGE_USAGE_TRANSIENT_ATTACHMENT_BIT, }); @@ -417,7 +417,7 @@ void BasePass::publishOutputs() { Gfx::SE_ATTACHMENT_LOAD_OP_CLEAR, Gfx::SE_ATTACHMENT_STORE_OP_DONT_CARE); msDepthAttachment.clear.depthStencil.depth = 0.0f; - colorAttachment = Gfx::RenderTargetAttachment(viewport, Gfx::SE_IMAGE_LAYOUT_UNDEFINED, Gfx::SE_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL, + colorAttachment = Gfx::RenderTargetAttachment(basePassColor, Gfx::SE_IMAGE_LAYOUT_UNDEFINED, Gfx::SE_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL, Gfx::SE_ATTACHMENT_LOAD_OP_DONT_CARE, Gfx::SE_ATTACHMENT_STORE_OP_STORE); msColorAttachment = @@ -437,7 +437,7 @@ void BasePass::publishOutputs() { } void BasePass::createRenderPass() { - RenderPass::beginFrame(Component::Camera()); + //RenderPass::beginFrame(Component::Camera()); //terrainRenderer = new TerrainRenderer(graphics, scene, viewParamsLayout, viewParamsSet); cullingBuffer = resources->requestBuffer("CULLINGBUFFER"); timestamps = resources->requestTimestampQuery("TIMESTAMPS"); @@ -538,7 +538,7 @@ void BasePass::createRenderPass() { .pipelineLayout = debugPipelineLayout, .multisampleState = { - .samples = viewport->getSamples(), + .samples = msColorAttachment.getNumSamples(), }, .rasterizationState = { @@ -612,7 +612,7 @@ void BasePass::createRenderPass() { .pipelineLayout = pipelineLayout, .multisampleState = { - .samples = viewport->getSamples(), + .samples = msColorAttachment.getNumSamples(), }, .rasterizationState = { diff --git a/src/Engine/Graphics/RenderPass/CMakeLists.txt b/src/Engine/Graphics/RenderPass/CMakeLists.txt index 06165ce..7102259 100644 --- a/src/Engine/Graphics/RenderPass/CMakeLists.txt +++ b/src/Engine/Graphics/RenderPass/CMakeLists.txt @@ -17,6 +17,8 @@ target_sources(Engine RenderPass.cpp TerrainRenderer.h TerrainRenderer.cpp + ToneMappingPass.h + ToneMappingPass.cpp UIPass.h UIPass.cpp VisibilityPass.h @@ -31,6 +33,7 @@ target_sources(Engine CachedDepthPass.h DepthCullingPass.h LightCullingPass.h + ToneMappingPass.h RayTracingPass.h RenderGraph.h RenderGraphResources.h diff --git a/src/Engine/Graphics/RenderPass/TerrainRenderer.cpp b/src/Engine/Graphics/RenderPass/TerrainRenderer.cpp index d5cc386..045a2b4 100644 --- a/src/Engine/Graphics/RenderPass/TerrainRenderer.cpp +++ b/src/Engine/Graphics/RenderPass/TerrainRenderer.cpp @@ -297,7 +297,7 @@ void TerrainRenderer::setViewport(Gfx::PViewport _viewport, Gfx::PRenderPass ren .pipelineLayout = meshUpdater.pipelineLayout, .multisampleState = { - .samples = viewport->getSamples(), + .samples = Gfx::SE_SAMPLE_COUNT_1_BIT, }, .rasterizationState = { diff --git a/src/Engine/Graphics/RenderPass/ToneMappingPass.cpp b/src/Engine/Graphics/RenderPass/ToneMappingPass.cpp new file mode 100644 index 0000000..6ba084b --- /dev/null +++ b/src/Engine/Graphics/RenderPass/ToneMappingPass.cpp @@ -0,0 +1,112 @@ +#include "ToneMappingPass.h" + +using namespace Seele; + +ToneMappingPass::ToneMappingPass(Gfx::PGraphics graphics) : RenderPass(graphics) { + layout = graphics->createDescriptorLayout("ToneMappingDescriptor"); + layout->addDescriptorBinding(Gfx::DescriptorBinding{ + .name = "offset", + .uniformLength = sizeof(Vector4), + }); + layout->addDescriptorBinding(Gfx::DescriptorBinding{ + .name = "slope", + .uniformLength = sizeof(Vector4), + }); + layout->addDescriptorBinding(Gfx::DescriptorBinding{ + .name = "power", + .uniformLength = sizeof(Vector4), + }); + layout->addDescriptorBinding(Gfx::DescriptorBinding{ + .name = "sat", + .uniformLength = sizeof(float), + }); + layout->addDescriptorBinding(Gfx::DescriptorBinding{ + .name = "hdrInputTexture", + .descriptorType = Gfx::SE_DESCRIPTOR_TYPE_SAMPLED_IMAGE, + }); + layout->addDescriptorBinding(Gfx::DescriptorBinding{ + .name = "hdrSampler", + .descriptorType = Gfx::SE_DESCRIPTOR_TYPE_SAMPLER, + }); + layout->create(); + pipelineLayout = graphics->createPipelineLayout("ToneMappingLayout"); + pipelineLayout->addDescriptorLayout(layout); + graphics->beginShaderCompilation(ShaderCompilationInfo{ + .name = "ToneMapping", + .modules = {"FullScreenQuad", "ToneMapping"}, + .entryPoints = {{"quadMain", "FullScreenQuad"}, {"toneMapping", "ToneMapping"}}, + .rootSignature = pipelineLayout, + }); + pipelineLayout->create(); + vert = graphics->createVertexShader({0}); + frag = graphics->createFragmentShader({1}); + sampler = graphics->createSampler({}); +} + +ToneMappingPass::~ToneMappingPass() {} + +void ToneMappingPass::beginFrame(const Component::Camera& cam) { RenderPass::beginFrame(cam); } + +void ToneMappingPass::render() { + hdrInputTexture.getTexture()->changeLayout(Gfx::SE_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL, Gfx::SE_ACCESS_COLOR_ATTACHMENT_WRITE_BIT, + Gfx::SE_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT, Gfx::SE_ACCESS_SHADER_READ_BIT, + Gfx::SE_PIPELINE_STAGE_FRAGMENT_SHADER_BIT); + + layout->reset(); + set = layout->allocateDescriptorSet(); + set->updateTexture("hdrInputTexture", 0, hdrInputTexture.getTexture()); + set->updateSampler("hdrSampler", 0, sampler); + set->writeChanges(); + graphics->beginRenderPass(renderPass); + Gfx::ORenderCommand command = graphics->createRenderCommand("ToneMapping"); + command->setViewport(viewport); + command->bindPipeline(pipeline); + command->bindDescriptor({set}); + command->draw(3, 1, 0, 0); + graphics->executeCommands(std::move(command)); + graphics->endRenderPass(); +} + +void ToneMappingPass::endFrame() {} + +void ToneMappingPass::publishOutputs() { + colorAttachment = Gfx::RenderTargetAttachment(viewport, Gfx::SE_IMAGE_LAYOUT_UNDEFINED, Gfx::SE_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL, + Gfx::SE_ATTACHMENT_LOAD_OP_DONT_CARE, Gfx::SE_ATTACHMENT_STORE_OP_STORE); + resources->registerRenderPassOutput("TONEMAPPING_COLOR", colorAttachment); +} + +void ToneMappingPass::createRenderPass() { + hdrInputTexture = resources->requestRenderTarget("BASEPASS_COLOR"); + Gfx::RenderTargetLayout targetLayout = Gfx::RenderTargetLayout{ + .colorAttachments = {colorAttachment}, + }; + Array dependency = { + { + .srcSubpass = ~0U, + .dstSubpass = 0, + .srcStage = Gfx::SE_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT, + .dstStage = Gfx::SE_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT, + .srcAccess = Gfx::SE_ACCESS_COLOR_ATTACHMENT_WRITE_BIT, + .dstAccess = Gfx::SE_ACCESS_COLOR_ATTACHMENT_WRITE_BIT, + }, + { + .srcSubpass = 0, + .dstSubpass = ~0U, + .srcStage = Gfx::SE_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT, + .dstStage = Gfx::SE_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT, + .srcAccess = Gfx::SE_ACCESS_COLOR_ATTACHMENT_WRITE_BIT, + .dstAccess = Gfx::SE_ACCESS_COLOR_ATTACHMENT_WRITE_BIT, + }, + }; + renderPass = graphics->createRenderPass(targetLayout, dependency, viewport, "ToneMappingPass"); + pipeline = graphics->createGraphicsPipeline(Gfx::LegacyPipelineCreateInfo{ + .vertexShader = vert, + .fragmentShader = frag, + .renderPass = renderPass, + .pipelineLayout = pipelineLayout, + .colorBlend = + { + .attachmentCount = 1, + }, + }); +} diff --git a/src/Engine/Graphics/RenderPass/ToneMappingPass.h b/src/Engine/Graphics/RenderPass/ToneMappingPass.h new file mode 100644 index 0000000..6a41dd6 --- /dev/null +++ b/src/Engine/Graphics/RenderPass/ToneMappingPass.h @@ -0,0 +1,33 @@ +#pragma once +#include "RenderPass.h" +#include "Graphics/Shader.h" + +namespace Seele +{ +class ToneMappingPass : public RenderPass { + public: + ToneMappingPass(Gfx::PGraphics graphics); + ToneMappingPass(ToneMappingPass&&) = default; + ToneMappingPass& operator=(ToneMappingPass&&) = default; + virtual ~ToneMappingPass(); + virtual void beginFrame(const Component::Camera& cam) override; + virtual void render() override; + virtual void endFrame() override; + virtual void publishOutputs() override; + virtual void createRenderPass() override; + + private: + // non-hdr swapchain output + Gfx::RenderTargetAttachment colorAttachment; + + Gfx::RenderTargetAttachment hdrInputTexture; + Gfx::OSampler sampler; + + Gfx::ODescriptorLayout layout; + Gfx::PDescriptorSet set; + Gfx::OPipelineLayout pipelineLayout; + Gfx::OVertexShader vert; + Gfx::OFragmentShader frag; + Gfx::PGraphicsPipeline pipeline; +}; +} \ No newline at end of file diff --git a/src/Engine/Graphics/RenderPass/WaterRenderer.cpp b/src/Engine/Graphics/RenderPass/WaterRenderer.cpp index 58c4c5a..785b0a2 100644 --- a/src/Engine/Graphics/RenderPass/WaterRenderer.cpp +++ b/src/Engine/Graphics/RenderPass/WaterRenderer.cpp @@ -458,7 +458,7 @@ void WaterRenderer::setViewport(Gfx::PViewport _viewport, Gfx::PRenderPass rende .pipelineLayout = waterLayout, .multisampleState = { - .samples = viewport->getSamples(), + .samples = Gfx::SE_SAMPLE_COUNT_4_BIT, }, .rasterizationState = { diff --git a/src/Engine/Graphics/RenderTarget.h b/src/Engine/Graphics/RenderTarget.h index 6468c1e..6aed4b4 100644 --- a/src/Engine/Graphics/RenderTarget.h +++ b/src/Engine/Graphics/RenderTarget.h @@ -35,7 +35,8 @@ class RenderTargetAttachment { } SeSampleCountFlags getNumSamples() const { if (viewport != nullptr) { - return viewport->getSamples(); + // viewport backbuffers are not multisampled themselves + return Gfx::SE_SAMPLE_COUNT_1_BIT; } return texture->getNumSamples(); } diff --git a/src/Engine/Graphics/VertexData.cpp b/src/Engine/Graphics/VertexData.cpp index fda6d04..fa4e401 100644 --- a/src/Engine/Graphics/VertexData.cpp +++ b/src/Engine/Graphics/VertexData.cpp @@ -55,6 +55,7 @@ void VertexData::updateMesh(uint32 meshletOffset, PMesh mesh, Component::Transfo matData.instances.resize(referencedInstance->getId() + 1); } BatchedDrawCall& matInstanceData = matData.instances[referencedInstance->getId()]; + matInstanceData.materialInstance = referencedInstance; for (const auto& data : meshData[mesh->id]) { if (mat->hasTransparency()) { auto params = referencedInstance->getMaterialOffsets(); @@ -75,7 +76,6 @@ void VertexData::updateMesh(uint32 meshletOffset, PMesh mesh, Component::Transfo .rayTracingScene = mesh->blas, }); } else { // opaque - matInstanceData.materialInstance = referencedInstance; matInstanceData.rayTracingData.add(mesh->blas); matInstanceData.instanceData.add(inst); matInstanceData.instanceMeshData.add(data); @@ -138,13 +138,12 @@ void VertexData::createDescriptors() { void VertexData::loadMesh(MeshId id, Array loadedIndices, Array loadedMeshlets) { std::unique_lock l(vertexDataLock); - uint32 numMeshData = (loadedMeshlets.size() + 2047) / 2048; // todo: magic number - for (uint32 n = 0; n < numMeshData; ++n) { - uint32 meshletsToProcess = std::min(loadedMeshlets.size() - n * 2048, 2048); + for (auto&& chunk : loadedMeshlets | std::views::chunk(2048)) { uint32 meshletOffset = meshlets.size(); AABB meshAABB; - for (uint32 i = 0; i < meshletsToProcess; ++i) { - Meshlet& m = loadedMeshlets[n * 2048 + i]; + uint32 numMeshlets = 0; + for (auto&& m : chunk) { + numMeshlets++; //... meshAABB = meshAABB.combine(m.boundingBox); uint32 vertexOffset = vertexIndices.size(); @@ -165,7 +164,7 @@ void VertexData::loadMesh(MeshId id, Array loadedIndices, Array } meshData[id].add(MeshData{ .bounding = meshAABB, //.toSphere(), - .numMeshlets = (uint32)meshletsToProcess, + .numMeshlets = numMeshlets, .meshletOffset = meshletOffset, }); } diff --git a/src/Engine/Graphics/Vulkan/RenderPass.cpp b/src/Engine/Graphics/Vulkan/RenderPass.cpp index f135df6..613ddcf 100644 --- a/src/Engine/Graphics/Vulkan/RenderPass.cpp +++ b/src/Engine/Graphics/Vulkan/RenderPass.cpp @@ -172,7 +172,7 @@ RenderPass::RenderPass(PGraphics graphics, Gfx::RenderTargetLayout _layout, Arra .colorAttachmentCount = (uint32)colorRefs.size(), .pColorAttachments = colorRefs.data(), .pResolveAttachments = resolveRefs.size() > 0 ? resolveRefs.data() : nullptr, - .pDepthStencilAttachment = &depthRef, + .pDepthStencilAttachment = layout.depthAttachment.getTexture() != nullptr ? &depthRef : nullptr, }; Array dep; for (const auto& d : dependencies) { diff --git a/src/Engine/Graphics/Window.cpp b/src/Engine/Graphics/Window.cpp index 960e4db..70bd098 100644 --- a/src/Engine/Graphics/Window.cpp +++ b/src/Engine/Graphics/Window.cpp @@ -10,7 +10,7 @@ Window::~Window() {} Viewport::Viewport(PWindow owner, const ViewportCreateInfo& viewportInfo) : sizeX(std::min(owner->getFramebufferWidth(), viewportInfo.dimensions.size.x)), sizeY(std::min(owner->getFramebufferHeight(), viewportInfo.dimensions.size.y)), offsetX(viewportInfo.dimensions.offset.x), - offsetY(viewportInfo.dimensions.offset.y), fieldOfView(viewportInfo.fieldOfView), samples(viewportInfo.numSamples), owner(owner) {} + offsetY(viewportInfo.dimensions.offset.y), fieldOfView(viewportInfo.fieldOfView), owner(owner) {} Viewport::~Viewport() {} diff --git a/src/Engine/Graphics/Window.h b/src/Engine/Graphics/Window.h index 87eeecb..11dfded 100644 --- a/src/Engine/Graphics/Window.h +++ b/src/Engine/Graphics/Window.h @@ -52,7 +52,6 @@ class Viewport { constexpr uint32 getOffsetY() const { return offsetY; } constexpr float getContentScaleX() const { return owner->getContentScaleX(); } constexpr float getContentScaleY() const { return owner->getContentScaleY(); } - constexpr Gfx::SeSampleCountFlags getSamples() const { return samples; } Matrix4 getProjectionMatrix() const; protected: @@ -61,7 +60,6 @@ class Viewport { uint32 offsetX; uint32 offsetY; float fieldOfView; - Gfx::SeSampleCountFlags samples; PWindow owner; }; DEFINE_REF(Viewport) diff --git a/src/Engine/Window/GameView.cpp b/src/Engine/Window/GameView.cpp index 8fae9fb..2b6582b 100644 --- a/src/Engine/Window/GameView.cpp +++ b/src/Engine/Window/GameView.cpp @@ -11,6 +11,7 @@ #include "Graphics/RenderPass/RayTracingPass.h" #include "Graphics/RenderPass/RenderGraphResources.h" #include "Graphics/RenderPass/VisibilityPass.h" +#include "Graphics/RenderPass/ToneMappingPass.h" #include "System/CameraUpdater.h" #include "System/LightGather.h" #include "System/MeshUpdater.h" @@ -26,6 +27,7 @@ GameView::GameView(Gfx::PGraphics graphics, PWindow window, const ViewportCreate renderGraph.addPass(new VisibilityPass(graphics)); renderGraph.addPass(new LightCullingPass(graphics, scene)); renderGraph.addPass(new BasePass(graphics, scene)); + renderGraph.addPass(new ToneMappingPass(graphics)); renderGraph.setViewport(viewport); renderGraph.createRenderPass(); if (graphics->supportRayTracing()) {