From a11a95e4300b33b6d383d34a97a696ecff165ab8 Mon Sep 17 00:00:00 2001 From: Menotdan <32620310+Menotdan@users.noreply.github.com> Date: Fri, 29 Mar 2024 00:02:11 -0400 Subject: [PATCH 01/12] Start of custom depth pass. Weird syncing issues between render scene and real scene. --- Source/Engine/Graphics/Enums.h | 14 ++++++++--- .../Materials/DeferredMaterialShader.cpp | 3 ++- .../Materials/DeferredMaterialShader.h | 4 ++++ Source/Engine/Graphics/RenderBuffers.cpp | 2 ++ Source/Engine/Graphics/RenderBuffers.h | 5 ++++ .../Level/Actors/ModelInstanceActor.cpp | 24 +++++++++++++++++++ .../Engine/Level/Actors/ModelInstanceActor.h | 21 ++++++++++++++++ Source/Engine/Level/Actors/StaticModel.cpp | 14 +++++++++-- Source/Engine/Renderer/CustomDepthPass.cpp | 0 Source/Engine/Renderer/CustomDepthPass.h | 13 ++++++++++ Source/Engine/Renderer/RenderList.cpp | 4 ++++ Source/Engine/Renderer/RenderList.h | 5 ++++ Source/Engine/Renderer/Renderer.cpp | 12 +++++++++- 13 files changed, 114 insertions(+), 7 deletions(-) create mode 100644 Source/Engine/Renderer/CustomDepthPass.cpp create mode 100644 Source/Engine/Renderer/CustomDepthPass.h diff --git a/Source/Engine/Graphics/Enums.h b/Source/Engine/Graphics/Enums.h index 3cbb4a1e14..ca5789c2a3 100644 --- a/Source/Engine/Graphics/Enums.h +++ b/Source/Engine/Graphics/Enums.h @@ -702,7 +702,7 @@ API_ENUM() enum class PostProcessEffectLocation /// /// The objects drawing pass types. Used as a flags for objects drawing masking. /// -API_ENUM(Attributes="Flags") enum class DrawPass : int32 +API_ENUM(Attributes = "Flags") enum class DrawPass : int32 { /// /// The none. @@ -717,7 +717,7 @@ API_ENUM(Attributes="Flags") enum class DrawPass : int32 /// /// The base pass rendering to the GBuffer (for opaque materials). /// - API_ENUM(Attributes="EditorDisplay(name: \"GBuffer\")") + API_ENUM(Attributes = "EditorDisplay(name: \"GBuffer\")") GBuffer = 1 << 1, /// @@ -745,6 +745,12 @@ API_ENUM(Attributes="Flags") enum class DrawPass : int32 /// GlobalSurfaceAtlas = 1 << 6, + /// + /// The custom depth rendering pass. Only certain models will be rendered to this depth. + /// + API_ENUM(Attributes="HideInEditor") + CustomDepth = 1 << 7, + /// /// The debug quad overdraw rendering (editor-only). /// @@ -761,11 +767,13 @@ API_ENUM(Attributes="Flags") enum class DrawPass : int32 /// The all draw passes combined into a single mask. /// API_ENUM(Attributes="HideInEditor") - All = Depth | GBuffer | Forward | Distortion | MotionVectors | GlobalSDF | GlobalSurfaceAtlas, + All = Depth | GBuffer | Forward | Distortion | MotionVectors | GlobalSDF | GlobalSurfaceAtlas | CustomDepth, }; DECLARE_ENUM_OPERATORS(DrawPass); +// TODO(Menotdan): Investigate these enums to add custom depth. + /// /// Describes frame rendering modes. /// diff --git a/Source/Engine/Graphics/Materials/DeferredMaterialShader.cpp b/Source/Engine/Graphics/Materials/DeferredMaterialShader.cpp index eeffb67d32..517f5a58e5 100644 --- a/Source/Engine/Graphics/Materials/DeferredMaterialShader.cpp +++ b/Source/Engine/Graphics/Materials/DeferredMaterialShader.cpp @@ -28,7 +28,7 @@ PACK_STRUCT(struct DeferredMaterialShaderData { DrawPass DeferredMaterialShader::GetDrawModes() const { - return DrawPass::Depth | DrawPass::GBuffer | DrawPass::GlobalSurfaceAtlas | DrawPass::MotionVectors | DrawPass::QuadOverdraw; + return DrawPass::Depth | DrawPass::CustomDepth | DrawPass::GBuffer | DrawPass::GlobalSurfaceAtlas | DrawPass::MotionVectors | DrawPass::QuadOverdraw; } bool DeferredMaterialShader::CanUseLightmap() const @@ -231,6 +231,7 @@ bool DeferredMaterialShader::Load() psDesc.PS = nullptr; } _cache.Depth.Init(psDesc); + _cache.CustomDepth.Init(psDesc); psDesc.VS = instancedDepthPassVS; _cacheInstanced.Depth.Init(psDesc); diff --git a/Source/Engine/Graphics/Materials/DeferredMaterialShader.h b/Source/Engine/Graphics/Materials/DeferredMaterialShader.h index 94bcef2417..1ae517c753 100644 --- a/Source/Engine/Graphics/Materials/DeferredMaterialShader.h +++ b/Source/Engine/Graphics/Materials/DeferredMaterialShader.h @@ -17,6 +17,7 @@ class DeferredMaterialShader : public MaterialShader PipelineStateCache DefaultLightmap; PipelineStateCache Depth; PipelineStateCache DepthSkinned; + PipelineStateCache CustomDepth; PipelineStateCache MotionVectors; PipelineStateCache MotionVectorsSkinned; PipelineStateCache MotionVectorsSkinnedPerBone; @@ -37,6 +38,8 @@ class DeferredMaterialShader : public MaterialShader return useLightmap ? &DefaultLightmap : (useSkinning ? &DefaultSkinned : &Default); case DrawPass::MotionVectors: return useSkinning ? (perBoneMotionBlur ? &MotionVectorsSkinnedPerBone : &MotionVectorsSkinned) : &MotionVectors; + case DrawPass::CustomDepth: + return &CustomDepth; #if USE_EDITOR case DrawPass::QuadOverdraw: return useSkinning ? &QuadOverdrawSkinned : &QuadOverdraw; @@ -53,6 +56,7 @@ class DeferredMaterialShader : public MaterialShader DefaultLightmap.Release(); Depth.Release(); DepthSkinned.Release(); + CustomDepth.Release(); MotionVectors.Release(); MotionVectorsSkinned.Release(); #if USE_EDITOR diff --git a/Source/Engine/Graphics/RenderBuffers.cpp b/Source/Engine/Graphics/RenderBuffers.cpp index 16a7af7a9f..2227dc95bd 100644 --- a/Source/Engine/Graphics/RenderBuffers.cpp +++ b/Source/Engine/Graphics/RenderBuffers.cpp @@ -15,6 +15,7 @@ RenderBuffers::RenderBuffers(const SpawnParams& params) { #define CREATE_TEXTURE(name) name = GPUDevice::Instance->CreateTexture(TEXT(#name)); _resources.Add(name) CREATE_TEXTURE(DepthBuffer); + CREATE_TEXTURE(CustomDepthBuffer); // TODO(Menotdan): Lazy allocate this buffer. CREATE_TEXTURE(MotionVectors); CREATE_TEXTURE(GBuffer0); CREATE_TEXTURE(GBuffer1); @@ -160,6 +161,7 @@ bool RenderBuffers::Init(int32 width, int32 height) if (GPUDevice::Instance->Limits.HasReadOnlyDepth) desc.Flags |= GPUTextureFlags::ReadOnlyDepthView; result |= DepthBuffer->Init(desc); + result |= CustomDepthBuffer->Init(desc); // MotionBlurPass initializes MotionVectors texture if needed (lazy init - not every game needs it) MotionVectors->ReleaseGPU(); diff --git a/Source/Engine/Graphics/RenderBuffers.h b/Source/Engine/Graphics/RenderBuffers.h index 617388d675..567388f78f 100644 --- a/Source/Engine/Graphics/RenderBuffers.h +++ b/Source/Engine/Graphics/RenderBuffers.h @@ -200,6 +200,11 @@ API_CLASS() class FLAXENGINE_API RenderBuffers : public ScriptingObject /// API_FIELD(ReadOnly) GPUTexture* DepthBuffer; + /// + /// Gets the custom depth buffer render target allocated within this render buffers collection (read only). + /// + API_FIELD(ReadOnly) GPUTexture* CustomDepthBuffer; + /// /// Gets the motion vectors render target allocated within this render buffers collection (read only). /// diff --git a/Source/Engine/Level/Actors/ModelInstanceActor.cpp b/Source/Engine/Level/Actors/ModelInstanceActor.cpp index 054e07b156..03e289ed30 100644 --- a/Source/Engine/Level/Actors/ModelInstanceActor.cpp +++ b/Source/Engine/Level/Actors/ModelInstanceActor.cpp @@ -3,6 +3,7 @@ #include "ModelInstanceActor.h" #include "Engine/Content/Assets/MaterialInstance.h" #include "Engine/Level/Scene/SceneRendering.h" +#include "Engine/Serialization/Serialization.h" ModelInstanceActor::ModelInstanceActor(const SpawnParams& params) : Actor(params) @@ -48,6 +49,29 @@ MaterialInstance* ModelInstanceActor::CreateAndSetVirtualMaterialInstance(int32 return result; } +void ModelInstanceActor::SetDrawCustomDepth(bool value) +{ + _drawCustomDepth = value; +} + +void ModelInstanceActor::Serialize(SerializeStream& stream, const void* otherObj) +{ + Actor::Serialize(stream, otherObj); + SERIALIZE_GET_OTHER_OBJ(ModelInstanceActor); + + SERIALIZE_MEMBER(DrawCustomDepth, _drawCustomDepth); +} + +void ModelInstanceActor::Deserialize(DeserializeStream& stream, ISerializeModifier* modifier) +{ + Actor::Deserialize(stream, modifier); + + DESERIALIZE_MEMBER(DrawCustomDepth, _drawCustomDepth); + + // Update state + SetDrawCustomDepth(_drawCustomDepth); +} + void ModelInstanceActor::WaitForModelLoad() { } diff --git a/Source/Engine/Level/Actors/ModelInstanceActor.h b/Source/Engine/Level/Actors/ModelInstanceActor.h index 793176ae08..5ce4abdc53 100644 --- a/Source/Engine/Level/Actors/ModelInstanceActor.h +++ b/Source/Engine/Level/Actors/ModelInstanceActor.h @@ -136,6 +136,25 @@ API_CLASS(Abstract) class FLAXENGINE_API ModelInstanceActor : public Actor /// virtual void UpdateBounds() = 0; + /// + /// Gets if this model should draw to the custom depth buffer. + /// + /// If this model should draw to the custom depth buffer. + API_PROPERTY(Attributes = "Serialize") + FORCE_INLINE const bool GetDrawCustomDepth() const + { + return _drawCustomDepth; + } + + /// + /// Sets if this model should draw to the custom depth buffer. + /// + /// If this model should draw to the custom depth buffer. + API_PROPERTY() void SetDrawCustomDepth(bool value); + +private: + bool _drawCustomDepth = false; + protected: virtual void WaitForModelLoad(); @@ -143,6 +162,8 @@ API_CLASS(Abstract) class FLAXENGINE_API ModelInstanceActor : public Actor // [Actor] void OnLayerChanged() override; void OnTransformChanged() override; + void Serialize(SerializeStream& stream, const void* otherObj) override; + void Deserialize(DeserializeStream& stream, ISerializeModifier* modifier) override; protected: // [Actor] diff --git a/Source/Engine/Level/Actors/StaticModel.cpp b/Source/Engine/Level/Actors/StaticModel.cpp index 07ff609bda..b98135bd69 100644 --- a/Source/Engine/Level/Actors/StaticModel.cpp +++ b/Source/Engine/Level/Actors/StaticModel.cpp @@ -286,6 +286,16 @@ void StaticModel::UpdateBounds() GetSceneRendering()->UpdateActor(this, _sceneRenderingKey); } +void StaticModel::SetDrawCustomDepth(bool value) +{ + ModelInstanceActor::SetDrawCustomDepth(value); + + if (!value) + DrawModes &= ~(DrawPass::CustomDepth); + else + DrawModes |= DrawPass::CustomDepth; +} + void StaticModel::FlushVertexColors() { RenderContext::GPULocker.Lock(); @@ -349,7 +359,7 @@ void StaticModel::Draw(RenderContext& renderContext) draw.Lightmap = _scene->LightmapsData.GetReadyLightmap(Lightmap.TextureIndex); draw.LightmapUVs = &Lightmap.UVsArea; draw.Flags = _staticFlags; - draw.DrawModes = DrawModes; + draw.DrawModes = DrawModes | (GetDrawCustomDepth() ? DrawPass::CustomDepth : DrawPass::None); draw.Bounds = _sphere; draw.Bounds.Center -= renderContext.View.Origin; draw.PerInstanceRandom = GetPerInstanceRandom(); @@ -383,7 +393,7 @@ void StaticModel::Draw(RenderContextBatch& renderContextBatch) draw.Lightmap = _scene->LightmapsData.GetReadyLightmap(Lightmap.TextureIndex); draw.LightmapUVs = &Lightmap.UVsArea; draw.Flags = _staticFlags; - draw.DrawModes = DrawModes; + draw.DrawModes = DrawModes | (GetDrawCustomDepth() ? DrawPass::CustomDepth : DrawPass::None); draw.Bounds = _sphere; draw.Bounds.Center -= renderContext.View.Origin; draw.PerInstanceRandom = GetPerInstanceRandom(); diff --git a/Source/Engine/Renderer/CustomDepthPass.cpp b/Source/Engine/Renderer/CustomDepthPass.cpp new file mode 100644 index 0000000000..e69de29bb2 diff --git a/Source/Engine/Renderer/CustomDepthPass.h b/Source/Engine/Renderer/CustomDepthPass.h new file mode 100644 index 0000000000..43d1e44218 --- /dev/null +++ b/Source/Engine/Renderer/CustomDepthPass.h @@ -0,0 +1,13 @@ +#pragma once + +#include "Engine/Graphics/RenderView.h" +#include "RendererPass.h" +#include "Engine/Content/Assets/Shader.h" + +/// +/// Custom Depth Pass. +/// +class CustomDepthPass : public RendererPass +{ + +}; diff --git a/Source/Engine/Renderer/RenderList.cpp b/Source/Engine/Renderer/RenderList.cpp index 577d582557..401e3886f3 100644 --- a/Source/Engine/Renderer/RenderList.cpp +++ b/Source/Engine/Renderer/RenderList.cpp @@ -487,6 +487,10 @@ void RenderList::AddDrawCall(const RenderContext& renderContext, DrawPass drawMo { DrawCallsLists[(int32)DrawCallsListType::MotionVectors].Indices.Add(index); } + if ((drawModes & DrawPass::CustomDepth) != DrawPass::None) + { + DrawCallsLists[(int32)DrawCallsListType::CustomDepth].Indices.Add(index); + } } void RenderList::AddDrawCall(const RenderContextBatch& renderContextBatch, DrawPass drawModes, StaticFlags staticFlags, ShadowsCastingMode shadowsMode, const BoundingSphere& bounds, DrawCall& drawCall, bool receivesDecals, int16 sortOrder) diff --git a/Source/Engine/Renderer/RenderList.h b/Source/Engine/Renderer/RenderList.h index 8d90be2e47..3841e7b8ff 100644 --- a/Source/Engine/Renderer/RenderList.h +++ b/Source/Engine/Renderer/RenderList.h @@ -196,6 +196,11 @@ API_ENUM() enum class DrawCallsListType /// MotionVectors, + /// + /// Custom depth buffer rendering. + /// + CustomDepth, + MAX, }; diff --git a/Source/Engine/Renderer/Renderer.cpp b/Source/Engine/Renderer/Renderer.cpp index f3c4ac6d21..df452091bb 100644 --- a/Source/Engine/Renderer/Renderer.cpp +++ b/Source/Engine/Renderer/Renderer.cpp @@ -18,6 +18,7 @@ #include "EyeAdaptationPass.h" #include "PostProcessingPass.h" #include "ColorGradingPass.h" +#include "CustomDepthPass.h" #include "MotionBlurPass.h" #include "VolumetricFogPass.h" #include "HistogramPass.h" @@ -349,7 +350,7 @@ void RenderInner(SceneRenderTask* task, RenderContext& renderContext, RenderCont { PROFILE_CPU_NAMED("Collect Draw Calls"); - view.Pass = DrawPass::GBuffer | DrawPass::Forward | DrawPass::Distortion; + view.Pass = DrawPass::GBuffer | DrawPass::Forward | DrawPass::Distortion | DrawPass::CustomDepth; if (setup.UseMotionVectors) view.Pass |= DrawPass::MotionVectors; renderContextBatch.GetMainContext() = renderContext; // Sync render context in batch with the current value @@ -395,6 +396,9 @@ void RenderInner(SceneRenderTask* task, RenderContext& renderContext, RenderCont renderContext.List->SortDrawCalls(renderContext, false, DrawCallsListType::GBufferNoDecals); renderContext.List->SortDrawCalls(renderContext, true, DrawCallsListType::Forward); renderContext.List->SortDrawCalls(renderContext, false, DrawCallsListType::Distortion); + renderContext.List->SortDrawCalls(renderContext, false, DrawCallsListType::CustomDepth); + if (!renderContext.List->DrawCallsLists[6].IsEmpty()) + int test = 1; if (setup.UseMotionVectors) renderContext.List->SortDrawCalls(renderContext, false, DrawCallsListType::MotionVectors); for (int32 i = 1; i < renderContextBatch.Contexts.Count(); i++) @@ -434,6 +438,12 @@ void RenderInner(SceneRenderTask* task, RenderContext& renderContext, RenderCont GlobalSignDistanceFieldPass::Instance()->Render(renderContext, context, bindingData); } + // Custom depth pass (can be used by materials later on) + if (true) + { + //CustomDepthPass::Instance() + } + // Fill GBuffer GBufferPass::Instance()->Fill(renderContext, lightBuffer); From 681456cf29686c8f54bb020babe68784ad5317e1 Mon Sep 17 00:00:00 2001 From: Menotdan <32620310+Menotdan@users.noreply.github.com> Date: Fri, 29 Mar 2024 01:15:05 -0400 Subject: [PATCH 02/12] It renders.. something? --- Source/Engine/Level/Actors/StaticModel.cpp | 10 -------- Source/Engine/Renderer/CustomDepthPass.cpp | 29 ++++++++++++++++++++++ Source/Engine/Renderer/CustomDepthPass.h | 5 ++++ Source/Engine/Renderer/RenderList.cpp | 4 +++ Source/Engine/Renderer/Renderer.cpp | 2 +- 5 files changed, 39 insertions(+), 11 deletions(-) diff --git a/Source/Engine/Level/Actors/StaticModel.cpp b/Source/Engine/Level/Actors/StaticModel.cpp index b98135bd69..918c3ac6f9 100644 --- a/Source/Engine/Level/Actors/StaticModel.cpp +++ b/Source/Engine/Level/Actors/StaticModel.cpp @@ -286,16 +286,6 @@ void StaticModel::UpdateBounds() GetSceneRendering()->UpdateActor(this, _sceneRenderingKey); } -void StaticModel::SetDrawCustomDepth(bool value) -{ - ModelInstanceActor::SetDrawCustomDepth(value); - - if (!value) - DrawModes &= ~(DrawPass::CustomDepth); - else - DrawModes |= DrawPass::CustomDepth; -} - void StaticModel::FlushVertexColors() { RenderContext::GPULocker.Lock(); diff --git a/Source/Engine/Renderer/CustomDepthPass.cpp b/Source/Engine/Renderer/CustomDepthPass.cpp index e69de29bb2..4b7a70f3a5 100644 --- a/Source/Engine/Renderer/CustomDepthPass.cpp +++ b/Source/Engine/Renderer/CustomDepthPass.cpp @@ -0,0 +1,29 @@ +#include "CustomDepthPass.h" +#include "RenderList.h" + +#include "Engine/Graphics/GPUDevice.h" +#include "Engine/Graphics/GPUContext.h" +#include "Engine/Graphics/RenderTask.h" +#include "Engine/Graphics/RenderBuffers.h" + +String CustomDepthPass::ToString() const +{ + return TEXT("CustomDepthPass"); +} + +void CustomDepthPass::Render(RenderContext& renderContext) +{ + PROFILE_GPU_CPU_NAMED("CustomDepth"); + + auto device = GPUDevice::Instance; + auto context = device->GetMainContext(); + + renderContext.View.Pass = DrawPass::CustomDepth; + + context->ClearDepth(*renderContext.Buffers->CustomDepthBuffer); + + context->SetRenderTarget(nullptr, *renderContext.Buffers->CustomDepthBuffer); + renderContext.List->ExecuteDrawCalls(renderContext, DrawCallsListType::CustomDepth); + + context->ResetRenderTarget(); +} diff --git a/Source/Engine/Renderer/CustomDepthPass.h b/Source/Engine/Renderer/CustomDepthPass.h index 43d1e44218..dc794a33ce 100644 --- a/Source/Engine/Renderer/CustomDepthPass.h +++ b/Source/Engine/Renderer/CustomDepthPass.h @@ -9,5 +9,10 @@ /// class CustomDepthPass : public RendererPass { +public: + void Render(RenderContext& renderContext); +public: + // [RendererPass] + String ToString() const override; }; diff --git a/Source/Engine/Renderer/RenderList.cpp b/Source/Engine/Renderer/RenderList.cpp index 401e3886f3..81b2e49ca7 100644 --- a/Source/Engine/Renderer/RenderList.cpp +++ b/Source/Engine/Renderer/RenderList.cpp @@ -534,6 +534,10 @@ void RenderList::AddDrawCall(const RenderContextBatch& renderContextBatch, DrawP { DrawCallsLists[(int32)DrawCallsListType::MotionVectors].Indices.Add(index); } + if ((drawModes & DrawPass::CustomDepth) != DrawPass::None) + { + DrawCallsLists[(int32)DrawCallsListType::CustomDepth].Indices.Add(index); + } } for (int32 i = 1; i < renderContextBatch.Contexts.Count(); i++) { diff --git a/Source/Engine/Renderer/Renderer.cpp b/Source/Engine/Renderer/Renderer.cpp index df452091bb..da4c83cd92 100644 --- a/Source/Engine/Renderer/Renderer.cpp +++ b/Source/Engine/Renderer/Renderer.cpp @@ -441,7 +441,7 @@ void RenderInner(SceneRenderTask* task, RenderContext& renderContext, RenderCont // Custom depth pass (can be used by materials later on) if (true) { - //CustomDepthPass::Instance() + CustomDepthPass::Instance()->Render(renderContext); } // Fill GBuffer From f356970e1ea6150885f8e12b67288f457510bda4 Mon Sep 17 00:00:00 2001 From: Menotdan <32620310+Menotdan@users.noreply.github.com> Date: Fri, 29 Mar 2024 03:10:57 -0400 Subject: [PATCH 03/12] Added debug view for the custom depth texture. Start of adding custom depth to the scene texture node. --- Content/Shaders/GBuffer.flax | 4 ++-- Source/Editor/Viewport/EditorViewport.cs | 1 + Source/Engine/Graphics/Enums.h | 5 +++++ Source/Engine/Graphics/Materials/MaterialInfo.h | 5 +++++ Source/Engine/Graphics/Materials/MaterialParams.cpp | 7 +++++++ Source/Engine/Renderer/CustomDepthPass.cpp | 3 ++- Source/Engine/Renderer/GBufferPass.cpp | 2 ++ Source/Shaders/GBuffer.hlsl | 1 + Source/Shaders/GBuffer.shader | 2 ++ 9 files changed, 27 insertions(+), 3 deletions(-) diff --git a/Content/Shaders/GBuffer.flax b/Content/Shaders/GBuffer.flax index 2261578be9..4365c21448 100644 --- a/Content/Shaders/GBuffer.flax +++ b/Content/Shaders/GBuffer.flax @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:993c151d661da0810cffd1b748268decd043551055099959edf79b107a902004 -size 2774 +oid sha256:97a08979e74275bb2c344b71bf49210543d0c5055232f9cb4b4134ec15c7832d +size 2921 diff --git a/Source/Editor/Viewport/EditorViewport.cs b/Source/Editor/Viewport/EditorViewport.cs index 2eeb1e8c01..a4b71b1cc4 100644 --- a/Source/Editor/Viewport/EditorViewport.cs +++ b/Source/Editor/Viewport/EditorViewport.cs @@ -1910,6 +1910,7 @@ public ViewModeOptions(string name, ViewModeOptions[] options) new ViewModeOptions(ViewMode.LightBuffer, "Light Buffer"), new ViewModeOptions(ViewMode.Reflections, "Reflections Buffer"), new ViewModeOptions(ViewMode.Depth, "Depth Buffer"), + new ViewModeOptions(ViewMode.CustomDepth, "Custom Depth Buffer"), new ViewModeOptions("GBuffer", new[] { new ViewModeOptions(ViewMode.Diffuse, "Diffuse"), diff --git a/Source/Engine/Graphics/Enums.h b/Source/Engine/Graphics/Enums.h index ca5789c2a3..207c61e8c4 100644 --- a/Source/Engine/Graphics/Enums.h +++ b/Source/Engine/Graphics/Enums.h @@ -913,6 +913,11 @@ API_ENUM() enum class ViewMode /// Draw Global Illumination debug preview (eg. irradiance probes). /// GlobalIllumination = 26, + + /// + /// Draw Custom Depth + /// + CustomDepth = 27, }; /// diff --git a/Source/Engine/Graphics/Materials/MaterialInfo.h b/Source/Engine/Graphics/Materials/MaterialInfo.h index 6fca1d189e..64b1e40c02 100644 --- a/Source/Engine/Graphics/Materials/MaterialInfo.h +++ b/Source/Engine/Graphics/Materials/MaterialInfo.h @@ -439,6 +439,11 @@ API_ENUM() enum class MaterialSceneTextures /// The scene world-space position (relative to the render view origin). /// WorldPosition = 11, + + /// + /// The custom depth. + /// + CustomDepth = 12, }; /// diff --git a/Source/Engine/Graphics/Materials/MaterialParams.cpp b/Source/Engine/Graphics/Materials/MaterialParams.cpp index 1904f9565e..36b8143e56 100644 --- a/Source/Engine/Graphics/Materials/MaterialParams.cpp +++ b/Source/Engine/Graphics/Materials/MaterialParams.cpp @@ -386,6 +386,13 @@ void MaterialParameter::Bind(BindMeta& meta) const case MaterialSceneTextures::Specular: view = meta.CanSampleGBuffer ? meta.Buffers->GBuffer2->View() : nullptr; break; + case MaterialSceneTextures::CustomDepth: + view = meta.CanSampleDepth + ? EnumHasAnyFlags(meta.Buffers->CustomDepthBuffer->Flags(), GPUTextureFlags::ReadOnlyDepthView) + ? meta.Buffers->CustomDepthBuffer->ViewReadOnlyDepth() + : meta.Buffers->CustomDepthBuffer->View() + : GPUDevice::Instance->GetDefaultWhiteTexture()->View(); + break; default: ; } } diff --git a/Source/Engine/Renderer/CustomDepthPass.cpp b/Source/Engine/Renderer/CustomDepthPass.cpp index 4b7a70f3a5..3d8415f84a 100644 --- a/Source/Engine/Renderer/CustomDepthPass.cpp +++ b/Source/Engine/Renderer/CustomDepthPass.cpp @@ -22,7 +22,8 @@ void CustomDepthPass::Render(RenderContext& renderContext) context->ClearDepth(*renderContext.Buffers->CustomDepthBuffer); - context->SetRenderTarget(nullptr, *renderContext.Buffers->CustomDepthBuffer); + // ? + context->SetRenderTarget(*renderContext.Buffers->CustomDepthBuffer, nullptr); renderContext.List->ExecuteDrawCalls(renderContext, DrawCallsListType::CustomDepth); context->ResetRenderTarget(); diff --git a/Source/Engine/Renderer/GBufferPass.cpp b/Source/Engine/Renderer/GBufferPass.cpp index c962160dbd..f59fa9074e 100644 --- a/Source/Engine/Renderer/GBufferPass.cpp +++ b/Source/Engine/Renderer/GBufferPass.cpp @@ -258,6 +258,7 @@ void GBufferPass::RenderDebug(RenderContext& renderContext) context->BindSR(2, renderContext.Buffers->GBuffer2); context->BindSR(3, renderContext.Buffers->DepthBuffer); context->BindSR(4, renderContext.Buffers->GBuffer3); + context->BindSR(5, renderContext.Buffers->CustomDepthBuffer); // Combine frame context->SetState(_psDebug); @@ -387,6 +388,7 @@ bool GBufferPass::IsDebugView(ViewMode mode) case ViewMode::SpecularColor: case ViewMode::SubsurfaceColor: case ViewMode::ShadingModel: + case ViewMode::CustomDepth: return true; default: return false; diff --git a/Source/Shaders/GBuffer.hlsl b/Source/Shaders/GBuffer.hlsl index 9378b3be50..fd8da1dd36 100644 --- a/Source/Shaders/GBuffer.hlsl +++ b/Source/Shaders/GBuffer.hlsl @@ -15,6 +15,7 @@ Texture2D Depth : register(t3); #if defined(USE_GBUFFER_CUSTOM_DATA) Texture2D GBuffer3 : register(t4); #endif +Texture2D CustomDepth : register(t5); // GBuffer Layout: // GBuffer0 = [RGB] Color, [A] AO diff --git a/Source/Shaders/GBuffer.shader b/Source/Shaders/GBuffer.shader index 32a02889bf..2c9a3ef3f5 100644 --- a/Source/Shaders/GBuffer.shader +++ b/Source/Shaders/GBuffer.shader @@ -33,6 +33,7 @@ DECLARE_GBUFFERDATA_ACCESS(GBuffer) #define View_Mode_MotionVectors 15 #define View_Mode_SubsurfaceColor 16 #define View_Mode_Unlit 17 +#define View_Mode_Custom_Depth 27 float3 GetShadingModelColor(GBufferSample gBuffer) { @@ -58,6 +59,7 @@ float4 PS_DebugView(Quad_VS2PS input) : SV_Target case View_Mode_Diffuse: result = gBuffer.Color; break; case View_Mode_Normals: result = gBuffer.Normal * 0.5 + 0.5; break; case View_Mode_Depth: result = gBuffer.ViewPos.z / GBuffer.ViewFar; break; + case View_Mode_Custom_Depth: result = CustomDepth.Sample(SamplerLinearClamp, input.TexCoord).r; break; case View_Mode_AmbientOcclusion: result = gBuffer.AO; break; case View_Mode_Metalness: result = gBuffer.Metalness; break; case View_Mode_Rougness: result = gBuffer.Roughness; break; From efd452414ecbe41b67fc887fed0f92b925257e59 Mon Sep 17 00:00:00 2001 From: Menotdan <32620310+Menotdan@users.noreply.github.com> Date: Fri, 29 Mar 2024 03:24:27 -0400 Subject: [PATCH 04/12] Add instanced custom depth (whatever that means?), and reversed the texture inputs again to match what should be correct but isn't rendering anything to the output. --- Source/Engine/Graphics/Materials/DeferredMaterialShader.cpp | 1 + Source/Engine/Renderer/CustomDepthPass.cpp | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/Source/Engine/Graphics/Materials/DeferredMaterialShader.cpp b/Source/Engine/Graphics/Materials/DeferredMaterialShader.cpp index 517f5a58e5..094bf249c4 100644 --- a/Source/Engine/Graphics/Materials/DeferredMaterialShader.cpp +++ b/Source/Engine/Graphics/Materials/DeferredMaterialShader.cpp @@ -234,6 +234,7 @@ bool DeferredMaterialShader::Load() _cache.CustomDepth.Init(psDesc); psDesc.VS = instancedDepthPassVS; _cacheInstanced.Depth.Init(psDesc); + _cacheInstanced.CustomDepth.Init(psDesc); // Depth Pass with skinning psDesc.VS = _shader->GetVS("VS_Skinned"); diff --git a/Source/Engine/Renderer/CustomDepthPass.cpp b/Source/Engine/Renderer/CustomDepthPass.cpp index 3d8415f84a..6cebb7fe02 100644 --- a/Source/Engine/Renderer/CustomDepthPass.cpp +++ b/Source/Engine/Renderer/CustomDepthPass.cpp @@ -23,7 +23,7 @@ void CustomDepthPass::Render(RenderContext& renderContext) context->ClearDepth(*renderContext.Buffers->CustomDepthBuffer); // ? - context->SetRenderTarget(*renderContext.Buffers->CustomDepthBuffer, nullptr); + context->SetRenderTarget(nullptr, *renderContext.Buffers->CustomDepthBuffer); renderContext.List->ExecuteDrawCalls(renderContext, DrawCallsListType::CustomDepth); context->ResetRenderTarget(); From 9b6eb49c318ad3ef5bea4099581bb4e5251bb923 Mon Sep 17 00:00:00 2001 From: Menotdan <32620310+Menotdan@users.noreply.github.com> Date: Fri, 29 Mar 2024 03:58:43 -0400 Subject: [PATCH 05/12] Flip parameters back for the 1000th time. --- Source/Engine/Renderer/CustomDepthPass.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Source/Engine/Renderer/CustomDepthPass.cpp b/Source/Engine/Renderer/CustomDepthPass.cpp index 6cebb7fe02..1e437c7b00 100644 --- a/Source/Engine/Renderer/CustomDepthPass.cpp +++ b/Source/Engine/Renderer/CustomDepthPass.cpp @@ -23,7 +23,7 @@ void CustomDepthPass::Render(RenderContext& renderContext) context->ClearDepth(*renderContext.Buffers->CustomDepthBuffer); // ? - context->SetRenderTarget(nullptr, *renderContext.Buffers->CustomDepthBuffer); + context->SetRenderTarget(*renderContext.Buffers->CustomDepthBuffer, static_cast(nullptr)); renderContext.List->ExecuteDrawCalls(renderContext, DrawCallsListType::CustomDepth); context->ResetRenderTarget(); From f14f78af893f1f35c1b3c38506774673a0b4c8c5 Mon Sep 17 00:00:00 2001 From: Menotdan <32620310+Menotdan@users.noreply.github.com> Date: Fri, 29 Mar 2024 04:16:35 -0400 Subject: [PATCH 06/12] Add graphics settings for custom depth. Remove old TODO. --- Source/Engine/Core/Config/GraphicsSettings.h | 6 ++++++ Source/Engine/Graphics/Enums.h | 2 -- Source/Engine/Renderer/Renderer.cpp | 2 +- 3 files changed, 7 insertions(+), 3 deletions(-) diff --git a/Source/Engine/Core/Config/GraphicsSettings.h b/Source/Engine/Core/Config/GraphicsSettings.h index 97c5f81cc0..9e5f22d464 100644 --- a/Source/Engine/Core/Config/GraphicsSettings.h +++ b/Source/Engine/Core/Config/GraphicsSettings.h @@ -77,6 +77,12 @@ API_CLASS(sealed, Namespace="FlaxEditor.Content.Settings", NoConstructor) class API_FIELD(Attributes = "EditorOrder(1502), EditorDisplay(\"Quality\")") bool UseHDRProbes = false; + /// + /// If checked, enables custom depth rendering. This can be used in materials. + /// + API_FIELD(Attributes = "EditorOrder(1600), EditorDisplay(\"Custom Depth\")") + bool EnableCustomDepth = false; + /// /// If checked, enables Global SDF rendering. This can be used in materials, shaders, and particles. /// diff --git a/Source/Engine/Graphics/Enums.h b/Source/Engine/Graphics/Enums.h index 207c61e8c4..3f40f3a4c8 100644 --- a/Source/Engine/Graphics/Enums.h +++ b/Source/Engine/Graphics/Enums.h @@ -772,8 +772,6 @@ API_ENUM(Attributes = "Flags") enum class DrawPass : int32 DECLARE_ENUM_OPERATORS(DrawPass); -// TODO(Menotdan): Investigate these enums to add custom depth. - /// /// Describes frame rendering modes. /// diff --git a/Source/Engine/Renderer/Renderer.cpp b/Source/Engine/Renderer/Renderer.cpp index da4c83cd92..7c36c16ac8 100644 --- a/Source/Engine/Renderer/Renderer.cpp +++ b/Source/Engine/Renderer/Renderer.cpp @@ -439,7 +439,7 @@ void RenderInner(SceneRenderTask* task, RenderContext& renderContext, RenderCont } // Custom depth pass (can be used by materials later on) - if (true) + if (graphicsSettings->EnableCustomDepth) { CustomDepthPass::Instance()->Render(renderContext); } From faf43b0fb5565b3eae037a49d2b01d056ac04be7 Mon Sep 17 00:00:00 2001 From: Menotdan <32620310+Menotdan@users.noreply.github.com> Date: Fri, 29 Mar 2024 13:13:35 -0400 Subject: [PATCH 07/12] Hide "Custom Depth Pass" debug view if it's disabled in settings. --- Source/Editor/Viewport/EditorViewport.cs | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/Source/Editor/Viewport/EditorViewport.cs b/Source/Editor/Viewport/EditorViewport.cs index a4b71b1cc4..3c567c765f 100644 --- a/Source/Editor/Viewport/EditorViewport.cs +++ b/Source/Editor/Viewport/EditorViewport.cs @@ -1957,8 +1957,17 @@ private void WidgetViewModeShowHide(Control cm) var ccm = (ContextMenu)cm; foreach (var e in ccm.Items) { - if (e is ContextMenuButton b && b.Tag is ViewMode v) - b.Icon = Task.ViewMode == v ? Style.Current.CheckBoxTick : SpriteHandle.Invalid; + if (e is not ContextMenuButton b) + continue; + + if (b.Tag is not ViewMode v) + continue; + + b.Icon = Task.ViewMode == v ? Style.Current.CheckBoxTick : SpriteHandle.Invalid; + + // Special cases for conditional debug modes + if (v == ViewMode.CustomDepth) + b.Visible = GameSettings.Load().EnableCustomDepth; } } From 3c7a46588d6c5296fd89d0d8c26752f27abcb8d2 Mon Sep 17 00:00:00 2001 From: Menotdan <32620310+Menotdan@users.noreply.github.com> Date: Fri, 29 Mar 2024 14:30:31 -0400 Subject: [PATCH 08/12] Lazy allocate custom depth buffer. --- .../Graphics/Materials/MaterialParams.cpp | 13 ++++--- Source/Engine/Graphics/RenderBuffers.cpp | 10 ++++-- Source/Engine/Graphics/RenderBuffers.h | 4 +++ Source/Engine/Renderer/CustomDepthPass.cpp | 34 ++++++++++++++++++- Source/Engine/Renderer/CustomDepthPass.h | 2 +- Source/Engine/Renderer/Renderer.cpp | 4 +-- 6 files changed, 56 insertions(+), 11 deletions(-) diff --git a/Source/Engine/Graphics/Materials/MaterialParams.cpp b/Source/Engine/Graphics/Materials/MaterialParams.cpp index 36b8143e56..7d174ac2df 100644 --- a/Source/Engine/Graphics/Materials/MaterialParams.cpp +++ b/Source/Engine/Graphics/Materials/MaterialParams.cpp @@ -387,11 +387,14 @@ void MaterialParameter::Bind(BindMeta& meta) const view = meta.CanSampleGBuffer ? meta.Buffers->GBuffer2->View() : nullptr; break; case MaterialSceneTextures::CustomDepth: - view = meta.CanSampleDepth - ? EnumHasAnyFlags(meta.Buffers->CustomDepthBuffer->Flags(), GPUTextureFlags::ReadOnlyDepthView) - ? meta.Buffers->CustomDepthBuffer->ViewReadOnlyDepth() - : meta.Buffers->CustomDepthBuffer->View() - : GPUDevice::Instance->GetDefaultWhiteTexture()->View(); + if (meta.Buffers->CustomDepthBuffer->IsAllocated()) + view = meta.CanSampleDepth + ? EnumHasAnyFlags(meta.Buffers->CustomDepthBuffer->Flags(), GPUTextureFlags::ReadOnlyDepthView) + ? meta.Buffers->CustomDepthBuffer->ViewReadOnlyDepth() + : meta.Buffers->CustomDepthBuffer->View() + : GPUDevice::Instance->GetDefaultWhiteTexture()->View(); + else + view = GPUDevice::Instance->GetDefaultWhiteTexture()->View(); break; default: ; } diff --git a/Source/Engine/Graphics/RenderBuffers.cpp b/Source/Engine/Graphics/RenderBuffers.cpp index 2227dc95bd..68141af64e 100644 --- a/Source/Engine/Graphics/RenderBuffers.cpp +++ b/Source/Engine/Graphics/RenderBuffers.cpp @@ -15,7 +15,7 @@ RenderBuffers::RenderBuffers(const SpawnParams& params) { #define CREATE_TEXTURE(name) name = GPUDevice::Instance->CreateTexture(TEXT(#name)); _resources.Add(name) CREATE_TEXTURE(DepthBuffer); - CREATE_TEXTURE(CustomDepthBuffer); // TODO(Menotdan): Lazy allocate this buffer. + CREATE_TEXTURE(CustomDepthBuffer); CREATE_TEXTURE(MotionVectors); CREATE_TEXTURE(GBuffer0); CREATE_TEXTURE(GBuffer1); @@ -161,7 +161,9 @@ bool RenderBuffers::Init(int32 width, int32 height) if (GPUDevice::Instance->Limits.HasReadOnlyDepth) desc.Flags |= GPUTextureFlags::ReadOnlyDepthView; result |= DepthBuffer->Init(desc); - result |= CustomDepthBuffer->Init(desc); + + // CustomDepthPass initializes the custom depth buffer as required. + CustomDepthBuffer->ReleaseGPU(); // MotionBlurPass initializes MotionVectors texture if needed (lazy init - not every game needs it) MotionVectors->ReleaseGPU(); @@ -216,6 +218,10 @@ void RenderBuffers::Release() LocalShadowedLightScattering = nullptr; LastFrameVolumetricFog = 0; + CustomDepthBuffer->ReleaseGPU(); + CustomDepthClear = false; + LastFrameCustomDepth = 0; + #define UPDATE_LAZY_KEEP_RT(name) \ RenderTargetPool::Release(name); \ name = nullptr; \ diff --git a/Source/Engine/Graphics/RenderBuffers.h b/Source/Engine/Graphics/RenderBuffers.h index 567388f78f..1b1d3b0379 100644 --- a/Source/Engine/Graphics/RenderBuffers.h +++ b/Source/Engine/Graphics/RenderBuffers.h @@ -89,6 +89,10 @@ API_CLASS() class FLAXENGINE_API RenderBuffers : public ScriptingObject GPUTexture* TemporalAA = nullptr; uint64 LastFrameTemporalAA = 0; + // Custom Depth State + bool CustomDepthClear = false; + uint64 LastFrameCustomDepth = 0; + // Maps the custom buffer type into the object that holds the state. Array CustomBuffers; diff --git a/Source/Engine/Renderer/CustomDepthPass.cpp b/Source/Engine/Renderer/CustomDepthPass.cpp index 1e437c7b00..a7b64c5a6c 100644 --- a/Source/Engine/Renderer/CustomDepthPass.cpp +++ b/Source/Engine/Renderer/CustomDepthPass.cpp @@ -5,6 +5,8 @@ #include "Engine/Graphics/GPUContext.h" #include "Engine/Graphics/RenderTask.h" #include "Engine/Graphics/RenderBuffers.h" +#include "Engine/Graphics/RenderTargetPool.h" +#include "Engine/Engine/Engine.h" String CustomDepthPass::ToString() const { @@ -18,13 +20,43 @@ void CustomDepthPass::Render(RenderContext& renderContext) auto device = GPUDevice::Instance; auto context = device->GetMainContext(); + if (!renderContext.Buffers->CustomDepthBuffer->IsAllocated()) + { + int width = renderContext.Buffers->GetWidth(); + int height = renderContext.Buffers->GetHeight(); + GPUTextureDescription desc = GPUTextureDescription::New2D(width, height, GPU_DEPTH_BUFFER_PIXEL_FORMAT, GPUTextureFlags::ShaderResource | GPUTextureFlags::DepthStencil); + if (GPUDevice::Instance->Limits.HasReadOnlyDepth) + desc.Flags |= GPUTextureFlags::ReadOnlyDepthView; + + renderContext.Buffers->CustomDepthBuffer->Init(desc); + } + renderContext.View.Pass = DrawPass::CustomDepth; context->ClearDepth(*renderContext.Buffers->CustomDepthBuffer); - // ? context->SetRenderTarget(*renderContext.Buffers->CustomDepthBuffer, static_cast(nullptr)); renderContext.List->ExecuteDrawCalls(renderContext, DrawCallsListType::CustomDepth); context->ResetRenderTarget(); + renderContext.Buffers->CustomDepthClear = false; + renderContext.Buffers->LastFrameCustomDepth = Engine::FrameCount; +} + +void CustomDepthPass::Clear(RenderContext& renderContext) +{ + // Release the buffer after a while + if (Engine::FrameCount - renderContext.Buffers->LastFrameCustomDepth > 240 && renderContext.Buffers->CustomDepthBuffer->IsAllocated()) + renderContext.Buffers->CustomDepthBuffer->ReleaseGPU(); + + // Only clear depth once and only if it's allocated + if (renderContext.Buffers->CustomDepthClear || !renderContext.Buffers->CustomDepthBuffer->IsAllocated()) + return; + + PROFILE_GPU_CPU_NAMED("CustomDepthClear"); + + auto device = GPUDevice::Instance; + auto context = device->GetMainContext(); + context->ClearDepth(*renderContext.Buffers->CustomDepthBuffer); + renderContext.Buffers->CustomDepthClear = true; } diff --git a/Source/Engine/Renderer/CustomDepthPass.h b/Source/Engine/Renderer/CustomDepthPass.h index dc794a33ce..76938dac02 100644 --- a/Source/Engine/Renderer/CustomDepthPass.h +++ b/Source/Engine/Renderer/CustomDepthPass.h @@ -11,7 +11,7 @@ class CustomDepthPass : public RendererPass { public: void Render(RenderContext& renderContext); - + void Clear(RenderContext& renderContext); public: // [RendererPass] String ToString() const override; diff --git a/Source/Engine/Renderer/Renderer.cpp b/Source/Engine/Renderer/Renderer.cpp index 7c36c16ac8..b48e628655 100644 --- a/Source/Engine/Renderer/Renderer.cpp +++ b/Source/Engine/Renderer/Renderer.cpp @@ -440,9 +440,9 @@ void RenderInner(SceneRenderTask* task, RenderContext& renderContext, RenderCont // Custom depth pass (can be used by materials later on) if (graphicsSettings->EnableCustomDepth) - { CustomDepthPass::Instance()->Render(renderContext); - } + else + CustomDepthPass::Instance()->Clear(renderContext); // This will only clear once // Fill GBuffer GBufferPass::Instance()->Fill(renderContext, lightBuffer); From 336e5fe9ecb8f82750d650455b3f07ff3ae6f067 Mon Sep 17 00:00:00 2001 From: Menotdan <32620310+Menotdan@users.noreply.github.com> Date: Fri, 29 Mar 2024 14:34:49 -0400 Subject: [PATCH 09/12] Reset more things after CustomDepthPass. --- Source/Engine/Renderer/CustomDepthPass.cpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Source/Engine/Renderer/CustomDepthPass.cpp b/Source/Engine/Renderer/CustomDepthPass.cpp index a7b64c5a6c..0ae61a89e6 100644 --- a/Source/Engine/Renderer/CustomDepthPass.cpp +++ b/Source/Engine/Renderer/CustomDepthPass.cpp @@ -39,6 +39,8 @@ void CustomDepthPass::Render(RenderContext& renderContext) renderContext.List->ExecuteDrawCalls(renderContext, DrawCallsListType::CustomDepth); context->ResetRenderTarget(); + context->ResetSR(); + context->FlushState(); renderContext.Buffers->CustomDepthClear = false; renderContext.Buffers->LastFrameCustomDepth = Engine::FrameCount; } From 0ca51206e22a5a66ceae3a979f2374807cc0be7d Mon Sep 17 00:00:00 2001 From: Menotdan <32620310+Menotdan@users.noreply.github.com> Date: Fri, 29 Mar 2024 14:36:15 -0400 Subject: [PATCH 10/12] Fix spacing change in Graphics/Enums.h --- Source/Engine/Graphics/Enums.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Source/Engine/Graphics/Enums.h b/Source/Engine/Graphics/Enums.h index 3f40f3a4c8..58901020f7 100644 --- a/Source/Engine/Graphics/Enums.h +++ b/Source/Engine/Graphics/Enums.h @@ -702,7 +702,7 @@ API_ENUM() enum class PostProcessEffectLocation /// /// The objects drawing pass types. Used as a flags for objects drawing masking. /// -API_ENUM(Attributes = "Flags") enum class DrawPass : int32 +API_ENUM(Attributes="Flags") enum class DrawPass : int32 { /// /// The none. @@ -717,7 +717,7 @@ API_ENUM(Attributes = "Flags") enum class DrawPass : int32 /// /// The base pass rendering to the GBuffer (for opaque materials). /// - API_ENUM(Attributes = "EditorDisplay(name: \"GBuffer\")") + API_ENUM(Attributes="EditorDisplay(name: \"GBuffer\")") GBuffer = 1 << 1, /// From 3f23a1adee63a5da8b9870abf9280adef5ab2017 Mon Sep 17 00:00:00 2001 From: Menotdan <32620310+Menotdan@users.noreply.github.com> Date: Fri, 29 Mar 2024 14:41:23 -0400 Subject: [PATCH 11/12] Fix spacing discrepancy in GBuffer.shader. --- Content/Shaders/GBuffer.flax | 4 ++-- Source/Shaders/GBuffer.shader | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/Content/Shaders/GBuffer.flax b/Content/Shaders/GBuffer.flax index 4365c21448..f50e36933a 100644 --- a/Content/Shaders/GBuffer.flax +++ b/Content/Shaders/GBuffer.flax @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:97a08979e74275bb2c344b71bf49210543d0c5055232f9cb4b4134ec15c7832d -size 2921 +oid sha256:4222371af9619e87c05cd38ad0785217391ac43a8b7a24e16b8e563ef5bc0058 +size 2915 diff --git a/Source/Shaders/GBuffer.shader b/Source/Shaders/GBuffer.shader index 2c9a3ef3f5..2fdd50403b 100644 --- a/Source/Shaders/GBuffer.shader +++ b/Source/Shaders/GBuffer.shader @@ -59,7 +59,7 @@ float4 PS_DebugView(Quad_VS2PS input) : SV_Target case View_Mode_Diffuse: result = gBuffer.Color; break; case View_Mode_Normals: result = gBuffer.Normal * 0.5 + 0.5; break; case View_Mode_Depth: result = gBuffer.ViewPos.z / GBuffer.ViewFar; break; - case View_Mode_Custom_Depth: result = CustomDepth.Sample(SamplerLinearClamp, input.TexCoord).r; break; + case View_Mode_Custom_Depth: result = CustomDepth.Sample(SamplerLinearClamp, input.TexCoord).r; break; case View_Mode_AmbientOcclusion: result = gBuffer.AO; break; case View_Mode_Metalness: result = gBuffer.Metalness; break; case View_Mode_Rougness: result = gBuffer.Roughness; break; From 43943e59ea253ee9dcdb35d1c291b9df66ab7b69 Mon Sep 17 00:00:00 2001 From: Menotdan <32620310+Menotdan@users.noreply.github.com> Date: Sat, 30 Mar 2024 03:46:25 -0400 Subject: [PATCH 12/12] Add skinned custom depth options. Added CustomDepth to the list of instance-support passes. --- Source/Engine/Graphics/Materials/DeferredMaterialShader.cpp | 1 + Source/Engine/Graphics/Materials/DeferredMaterialShader.h | 3 ++- Source/Engine/Renderer/CustomDepthPass.cpp | 2 -- Source/Engine/Renderer/RenderList.cpp | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/Source/Engine/Graphics/Materials/DeferredMaterialShader.cpp b/Source/Engine/Graphics/Materials/DeferredMaterialShader.cpp index 094bf249c4..29a7489ec7 100644 --- a/Source/Engine/Graphics/Materials/DeferredMaterialShader.cpp +++ b/Source/Engine/Graphics/Materials/DeferredMaterialShader.cpp @@ -239,6 +239,7 @@ bool DeferredMaterialShader::Load() // Depth Pass with skinning psDesc.VS = _shader->GetVS("VS_Skinned"); _cache.DepthSkinned.Init(psDesc); + _cache.CustomDepthSkinned.Init(psDesc); return failed; } diff --git a/Source/Engine/Graphics/Materials/DeferredMaterialShader.h b/Source/Engine/Graphics/Materials/DeferredMaterialShader.h index 1ae517c753..1b35581923 100644 --- a/Source/Engine/Graphics/Materials/DeferredMaterialShader.h +++ b/Source/Engine/Graphics/Materials/DeferredMaterialShader.h @@ -18,6 +18,7 @@ class DeferredMaterialShader : public MaterialShader PipelineStateCache Depth; PipelineStateCache DepthSkinned; PipelineStateCache CustomDepth; + PipelineStateCache CustomDepthSkinned; PipelineStateCache MotionVectors; PipelineStateCache MotionVectorsSkinned; PipelineStateCache MotionVectorsSkinnedPerBone; @@ -39,7 +40,7 @@ class DeferredMaterialShader : public MaterialShader case DrawPass::MotionVectors: return useSkinning ? (perBoneMotionBlur ? &MotionVectorsSkinnedPerBone : &MotionVectorsSkinned) : &MotionVectors; case DrawPass::CustomDepth: - return &CustomDepth; + return useSkinning ? &CustomDepthSkinned : &CustomDepth; #if USE_EDITOR case DrawPass::QuadOverdraw: return useSkinning ? &QuadOverdrawSkinned : &QuadOverdraw; diff --git a/Source/Engine/Renderer/CustomDepthPass.cpp b/Source/Engine/Renderer/CustomDepthPass.cpp index 0ae61a89e6..a7b64c5a6c 100644 --- a/Source/Engine/Renderer/CustomDepthPass.cpp +++ b/Source/Engine/Renderer/CustomDepthPass.cpp @@ -39,8 +39,6 @@ void CustomDepthPass::Render(RenderContext& renderContext) renderContext.List->ExecuteDrawCalls(renderContext, DrawCallsListType::CustomDepth); context->ResetRenderTarget(); - context->ResetSR(); - context->FlushState(); renderContext.Buffers->CustomDepthClear = false; renderContext.Buffers->LastFrameCustomDepth = Engine::FrameCount; } diff --git a/Source/Engine/Renderer/RenderList.cpp b/Source/Engine/Renderer/RenderList.cpp index 81b2e49ca7..a2558bff96 100644 --- a/Source/Engine/Renderer/RenderList.cpp +++ b/Source/Engine/Renderer/RenderList.cpp @@ -649,7 +649,7 @@ void RenderList::SortDrawCalls(const RenderContext& renderContext, bool reverseD FORCE_INLINE bool CanUseInstancing(DrawPass pass) { - return pass == DrawPass::GBuffer || pass == DrawPass::Depth; + return pass == DrawPass::GBuffer || pass == DrawPass::Depth || pass == DrawPass::CustomDepth; } void RenderList::ExecuteDrawCalls(const RenderContext& renderContext, DrawCallsList& list, const RenderListBuffer& drawCalls, GPUTextureView* input)