From ea673fb3b732fee5ff7f73d8673ecaefb04d545b Mon Sep 17 00:00:00 2001 From: Turanszki Janos Date: Sun, 4 Apr 2021 17:07:27 +0200 Subject: [PATCH] depth of field params moved to CameraComponent --- .../ScriptingAPI-Documentation.md | 30 +++------ Editor/CameraWindow.cpp | 60 ++++++++++++++++- Editor/CameraWindow.h | 4 ++ Editor/PostprocessWindow.cpp | 30 ++------- Editor/PostprocessWindow.h | 2 - WickedEngine/ArchiveVersionHistory.txt | 1 + WickedEngine/RenderPath3D.cpp | 8 +-- WickedEngine/RenderPath3D.h | 8 +-- WickedEngine/RenderPath3D_BindLua.cpp | 32 ---------- WickedEngine/RenderPath3D_BindLua.h | 2 - .../shaders/ShaderInterop_Postprocess.h | 6 +- WickedEngine/shaders/ShaderInterop_Renderer.h | 4 ++ WickedEngine/shaders/depthOfFieldHF.hlsli | 2 +- WickedEngine/shaders/depthoffield_mainCS.hlsl | 4 +- .../shaders/depthoffield_prepassCS.hlsl | 4 +- WickedEngine/shaders/raytraceCS.hlsl | 18 ++++-- WickedEngine/wiArchive.cpp | 2 +- WickedEngine/wiRenderer.cpp | 16 ++--- WickedEngine/wiRenderer.h | 6 +- WickedEngine/wiScene.h | 3 + WickedEngine/wiScene_BindLua.cpp | 64 +++++++++++++++++++ WickedEngine/wiScene_BindLua.h | 6 ++ WickedEngine/wiScene_Serializers.cpp | 14 ++++ WickedEngine/wiVersion.cpp | 2 +- 24 files changed, 202 insertions(+), 126 deletions(-) diff --git a/Content/Documentation/ScriptingAPI-Documentation.md b/Content/Documentation/ScriptingAPI-Documentation.md index bcf82b33c..920ee4cfd 100644 --- a/Content/Documentation/ScriptingAPI-Documentation.md +++ b/Content/Documentation/ScriptingAPI-Documentation.md @@ -512,6 +512,12 @@ Describes an orientation in 3D space. - SetNearPlane(float value) - GetFarPlane() : float result - SetFarPlane(float value) +- GetFocalDistance() : float result +- SetFocalDistance(float value) +- GetApertureSize() : float result +- SetApertureSize(float value) +- GetApertureShape() : float result +- SetApertureShape(Vector value) - GetView() : Matrix result - GetProjection() : Matrix result - GetViewProjection() : Matrix result @@ -629,9 +635,9 @@ It can hold Sprites and SpriteFonts and can sort them by layers, update and rend - SetFontOrder(SpriteFont font, int order) #### RenderPath3D -A 3D scene can either be rendered by a Forward or Deferred render path, or path tracing. +This is the default scene render path. It inherits functions from RenderPath2D, so it can render a 2D overlay. -- [void-constructor]RenderPath3D() +- [constructor]RenderPath3D() - SetAO(int value) -- Sets up the ambient occlusion effect (possible values below) - AO_DISABLED : int -- turn off AO computation (use in SetAO() function) - AO_SSAO : int -- enable simple brute force screen space ambient occlusion (use in SetAO() function) @@ -655,32 +661,12 @@ It inherits functions from RenderPath2D, so it can render a 2D overlay. - SetSSSEnabled(bool value) - SetDitherEnabled(bool value) - SetDepthOfFieldEnabled(bool value) -- SetDepthOfFieldFocus(float value) - SetDepthOfFieldStrength(float value) -- SetDepthOfFieldAspect(float value) - SetMSAASampleCount(int count) - SetSharpenFilterEnabled(bool value) - SetSharpenFilterAmount(float value) - SetExposure(float value) -##### RenderPath3D_Forward -It renders the scene contained by the Renderer in a forward render path. The component does not hold the scene information, -only the effects to render the scene. The scene is managed and ultimately rendered by the Renderer. -It inherits functions from RenderPath3D. -- [constructor]RenderPath3D_Forward() - -##### RenderPath3D_TiledForward -It renders the scene contained by the Renderer in a tiled forward render path. The component does not hold the scene information, -only the effects to render the scene. The scene is managed and ultimately rendered by the Renderer. -It inherits functions from RenderPath3D_Forward3D. -- [constructor]RenderPath3D_TiledForward() - -##### RenderPath3D_Deferred -It renders the scene contained by the Renderer in a deferred render path. The component does not hold the scene information, -only the effects to render the scene. The scene is managed and ultimately rendered by the Renderer. -It inherits functions from RenderPath3D. -- [constructor]RenderPath3D_Deferred() - #### LoadingScreen It is a RenderPath2D but one that internally manages resource loading and can display information about the process. It inherits functions from RenderPath2D. diff --git a/Editor/CameraWindow.cpp b/Editor/CameraWindow.cpp index f4d4fc345..69cf15ece 100644 --- a/Editor/CameraWindow.cpp +++ b/Editor/CameraWindow.cpp @@ -24,7 +24,7 @@ void CameraWindow::Create(EditorComponent* editor) camera_transform.MatrixTransform(wiScene::GetCamera().GetInvView()); camera_transform.UpdateTransform(); - SetSize(XMFLOAT2(380, 260)); + SetSize(XMFLOAT2(380, 340)); float x = 200; float y = 10; @@ -32,6 +32,7 @@ void CameraWindow::Create(EditorComponent* editor) float step = hei + 2; farPlaneSlider.Create(1, 5000, 1000, 100000, "Far Plane: "); + farPlaneSlider.SetTooltip("Controls the camera's far clip plane, geometry farther than this will be clipped."); farPlaneSlider.SetSize(XMFLOAT2(100, hei)); farPlaneSlider.SetPos(XMFLOAT2(x, y += step)); farPlaneSlider.SetValue(wiScene::GetCamera().zFarP); @@ -40,10 +41,12 @@ void CameraWindow::Create(EditorComponent* editor) CameraComponent& camera = wiScene::GetCamera(); camera.zFarP = args.fValue; camera.UpdateCamera(); + camera.SetDirty(); }); AddWidget(&farPlaneSlider); nearPlaneSlider.Create(0.01f, 10, 0.1f, 10000, "Near Plane: "); + nearPlaneSlider.SetTooltip("Controls the camera's near clip plane, geometry closer than this will be clipped."); nearPlaneSlider.SetSize(XMFLOAT2(100, hei)); nearPlaneSlider.SetPos(XMFLOAT2(x, y += step)); nearPlaneSlider.SetValue(wiScene::GetCamera().zNearP); @@ -52,10 +55,12 @@ void CameraWindow::Create(EditorComponent* editor) CameraComponent& camera = wiScene::GetCamera(); camera.zNearP = args.fValue; camera.UpdateCamera(); + camera.SetDirty(); }); AddWidget(&nearPlaneSlider); fovSlider.Create(1, 179, 60, 10000, "FOV: "); + fovSlider.SetTooltip("Controls the camera's top-down field of view (in degrees)"); fovSlider.SetSize(XMFLOAT2(100, hei)); fovSlider.SetPos(XMFLOAT2(x, y += step)); fovSlider.OnSlide([&](wiEventArgs args) { @@ -63,9 +68,62 @@ void CameraWindow::Create(EditorComponent* editor) CameraComponent& camera = wiScene::GetCamera(); camera.fov = args.fValue / 180.f * XM_PI; camera.UpdateCamera(); + camera.SetDirty(); }); AddWidget(&fovSlider); + focalLengthSlider.Create(0.001f, 100, 1, 10000, "Focal Length: "); + focalLengthSlider.SetTooltip("Controls the depth of field effect's focus distance"); + focalLengthSlider.SetSize(XMFLOAT2(100, hei)); + focalLengthSlider.SetPos(XMFLOAT2(x, y += step)); + focalLengthSlider.OnSlide([&](wiEventArgs args) { + Scene& scene = wiScene::GetScene(); + CameraComponent& camera = wiScene::GetCamera(); + camera.focal_length = args.fValue; + camera.UpdateCamera(); + camera.SetDirty(); + }); + AddWidget(&focalLengthSlider); + + apertureSizeSlider.Create(0, 1, 0, 10000, "Aperture Size: "); + apertureSizeSlider.SetTooltip("Controls the depth of field effect's strength"); + apertureSizeSlider.SetSize(XMFLOAT2(100, hei)); + apertureSizeSlider.SetPos(XMFLOAT2(x, y += step)); + apertureSizeSlider.OnSlide([&](wiEventArgs args) { + Scene& scene = wiScene::GetScene(); + CameraComponent& camera = wiScene::GetCamera(); + camera.aperture_size = args.fValue; + camera.UpdateCamera(); + camera.SetDirty(); + }); + AddWidget(&apertureSizeSlider); + + apertureShapeXSlider.Create(0, 2, 1, 10000, "Aperture Shape X: "); + apertureShapeXSlider.SetTooltip("Controls the depth of field effect's bokeh shape"); + apertureShapeXSlider.SetSize(XMFLOAT2(100, hei)); + apertureShapeXSlider.SetPos(XMFLOAT2(x, y += step)); + apertureShapeXSlider.OnSlide([&](wiEventArgs args) { + Scene& scene = wiScene::GetScene(); + CameraComponent& camera = wiScene::GetCamera(); + camera.aperture_shape.x = args.fValue; + camera.UpdateCamera(); + camera.SetDirty(); + }); + AddWidget(&apertureShapeXSlider); + + apertureShapeYSlider.Create(0, 2, 1, 10000, "Aperture Shape Y: "); + apertureShapeYSlider.SetTooltip("Controls the depth of field effect's bokeh shape"); + apertureShapeYSlider.SetSize(XMFLOAT2(100, hei)); + apertureShapeYSlider.SetPos(XMFLOAT2(x, y += step)); + apertureShapeYSlider.OnSlide([&](wiEventArgs args) { + Scene& scene = wiScene::GetScene(); + CameraComponent& camera = wiScene::GetCamera(); + camera.aperture_shape.y = args.fValue; + camera.UpdateCamera(); + camera.SetDirty(); + }); + AddWidget(&apertureShapeYSlider); + movespeedSlider.Create(1, 100, 10, 10000, "Movement Speed: "); movespeedSlider.SetSize(XMFLOAT2(100, hei)); movespeedSlider.SetPos(XMFLOAT2(x, y += step)); diff --git a/Editor/CameraWindow.h b/Editor/CameraWindow.h index 30f58c643..137641db1 100644 --- a/Editor/CameraWindow.h +++ b/Editor/CameraWindow.h @@ -20,6 +20,10 @@ public: wiSlider farPlaneSlider; wiSlider nearPlaneSlider; wiSlider fovSlider; + wiSlider focalLengthSlider; + wiSlider apertureSizeSlider; + wiSlider apertureShapeXSlider; + wiSlider apertureShapeYSlider; wiSlider movespeedSlider; wiSlider rotationspeedSlider; wiButton resetButton; diff --git a/Editor/PostprocessWindow.cpp b/Editor/PostprocessWindow.cpp index 847160a3b..3552fdb38 100644 --- a/Editor/PostprocessWindow.cpp +++ b/Editor/PostprocessWindow.cpp @@ -11,7 +11,7 @@ using namespace wiGraphics; void PostprocessWindow::Create(EditorComponent* editor) { wiWindow::Create("PostProcess Window"); - SetSize(XMFLOAT2(420, 540)); + SetSize(XMFLOAT2(420, 500)); float x = 150; float y = 10; @@ -249,39 +249,17 @@ void PostprocessWindow::Create(EditorComponent* editor) }); AddWidget(&depthOfFieldCheckBox); - depthOfFieldFocusSlider.Create(1.0f, 100, 10, 10000, "Focus: "); - depthOfFieldFocusSlider.SetTooltip("Set the focus distance from the camera. The picture will be sharper near the focus, and blurrier further from it."); - depthOfFieldFocusSlider.SetScriptTip("RenderPath3D::SetDepthOfFieldFocus(float value)"); - depthOfFieldFocusSlider.SetSize(XMFLOAT2(100, hei)); - depthOfFieldFocusSlider.SetPos(XMFLOAT2(x + 100, y)); - depthOfFieldFocusSlider.SetValue(editor->renderPath->getDepthOfFieldFocus()); - depthOfFieldFocusSlider.OnSlide([=](wiEventArgs args) { - editor->renderPath->setDepthOfFieldFocus(args.fValue); - }); - AddWidget(&depthOfFieldFocusSlider); - - depthOfFieldScaleSlider.Create(1.0f, 20, 100, 1000, "Scale: "); - depthOfFieldScaleSlider.SetTooltip("Set depth of field scale/falloff."); + depthOfFieldScaleSlider.Create(1.0f, 20, 100, 1000, "Strength: "); + depthOfFieldScaleSlider.SetTooltip("Set depth of field strength. This is used to scale the Camera's ApertureSize setting"); depthOfFieldScaleSlider.SetScriptTip("RenderPath3D::SetDepthOfFieldStrength(float value)"); depthOfFieldScaleSlider.SetSize(XMFLOAT2(100, hei)); - depthOfFieldScaleSlider.SetPos(XMFLOAT2(x + 100, y += step)); + depthOfFieldScaleSlider.SetPos(XMFLOAT2(x + 100, y)); depthOfFieldScaleSlider.SetValue(editor->renderPath->getDepthOfFieldStrength()); depthOfFieldScaleSlider.OnSlide([=](wiEventArgs args) { editor->renderPath->setDepthOfFieldStrength(args.fValue); }); AddWidget(&depthOfFieldScaleSlider); - depthOfFieldAspectSlider.Create(0.01f, 2, 1, 1000, "Aspect: "); - depthOfFieldAspectSlider.SetTooltip("Set depth of field bokeh aspect ratio (width/height)."); - depthOfFieldAspectSlider.SetScriptTip("RenderPath3D::SetDepthOfFieldAspect(float value)"); - depthOfFieldAspectSlider.SetSize(XMFLOAT2(100, hei)); - depthOfFieldAspectSlider.SetPos(XMFLOAT2(x + 100, y += step)); - depthOfFieldAspectSlider.SetValue(editor->renderPath->getDepthOfFieldAspect()); - depthOfFieldAspectSlider.OnSlide([=](wiEventArgs args) { - editor->renderPath->setDepthOfFieldAspect(args.fValue); - }); - AddWidget(&depthOfFieldAspectSlider); - bloomCheckBox.Create("Bloom: "); bloomCheckBox.SetTooltip("Enable bloom. The effect adds color bleeding to the brightest parts of the scene."); bloomCheckBox.SetScriptTip("RenderPath3D::SetBloomEnabled(bool value)"); diff --git a/Editor/PostprocessWindow.h b/Editor/PostprocessWindow.h index b27e0aca4..4ade3fa8e 100644 --- a/Editor/PostprocessWindow.h +++ b/Editor/PostprocessWindow.h @@ -27,9 +27,7 @@ public: wiCheckBox motionBlurCheckBox; wiSlider motionBlurStrengthSlider; wiCheckBox depthOfFieldCheckBox; - wiSlider depthOfFieldFocusSlider; wiSlider depthOfFieldScaleSlider; - wiSlider depthOfFieldAspectSlider; wiCheckBox bloomCheckBox; wiSlider bloomStrengthSlider; wiCheckBox fxaaCheckBox; diff --git a/WickedEngine/ArchiveVersionHistory.txt b/WickedEngine/ArchiveVersionHistory.txt index d8251e01b..bca137318 100644 --- a/WickedEngine/ArchiveVersionHistory.txt +++ b/WickedEngine/ArchiveVersionHistory.txt @@ -1,5 +1,6 @@ This file contains changelog of wiArchive versions +65: serialized CameraComponent focal_length, aperture_size and aperture_shape 64: serialized per-emitter gravity, velocity, drag and random_color 63: serialized wiResourceManager embedded resources 62: serialized WeatherComponent::colorGradingMapName property diff --git a/WickedEngine/RenderPath3D.cpp b/WickedEngine/RenderPath3D.cpp index e3cc87568..e78b740b8 100644 --- a/WickedEngine/RenderPath3D.cpp +++ b/WickedEngine/RenderPath3D.cpp @@ -1274,7 +1274,7 @@ void RenderPath3D::RenderPostprocessChain(CommandList cmd) const rt_first = &rtTemporalAA[output]; } - if (getDepthOfFieldEnabled()) + if (getDepthOfFieldEnabled() && camera->aperture_size > 0 && getDepthOfFieldStrength() > 0) { wiRenderer::Postprocess_DepthOfField( depthoffieldResources, @@ -1282,9 +1282,7 @@ void RenderPath3D::RenderPostprocessChain(CommandList cmd) const *rt_write, rtLinearDepth, cmd, - getDepthOfFieldFocus(), - getDepthOfFieldStrength(), - getDepthOfFieldAspect() + getDepthOfFieldStrength() ); rt_first = nullptr; @@ -1292,7 +1290,7 @@ void RenderPath3D::RenderPostprocessChain(CommandList cmd) const device->UnbindResources(TEXSLOT_ONDEMAND0, 1, cmd); } - if (getMotionBlurEnabled()) + if (getMotionBlurEnabled() && getMotionBlurStrength() > 0) { wiRenderer::Postprocess_MotionBlur( motionblurResources, diff --git a/WickedEngine/RenderPath3D.h b/WickedEngine/RenderPath3D.h index afa9e91dd..9295ed30e 100644 --- a/WickedEngine/RenderPath3D.h +++ b/WickedEngine/RenderPath3D.h @@ -24,9 +24,7 @@ private: float exposure = 1.0f; float bloomThreshold = 1.0f; float motionBlurStrength = 100.0f; - float dofFocus = 2.0f; float dofStrength = 10.0f; - float dofAspect = 1.0f; float sharpenFilterAmount = 0.28f; float outlineThreshold = 0.2f; float outlineThickness = 1.0f; @@ -53,7 +51,7 @@ private: bool lightShaftsEnabled = false; bool lensFlareEnabled = true; bool motionBlurEnabled = false; - bool depthOfFieldEnabled = false; + bool depthOfFieldEnabled = true; bool eyeAdaptionEnabled = false; bool sharpenFilterEnabled = false; bool outlineEnabled = false; @@ -182,9 +180,7 @@ public: constexpr float getExposure() const { return exposure; } constexpr float getBloomThreshold() const { return bloomThreshold; } constexpr float getMotionBlurStrength() const { return motionBlurStrength; } - constexpr float getDepthOfFieldFocus() const { return dofFocus; } constexpr float getDepthOfFieldStrength() const { return dofStrength; } - constexpr float getDepthOfFieldAspect() const { return dofAspect; } constexpr float getSharpenFilterAmount() const { return sharpenFilterAmount; } constexpr float getOutlineThreshold() const { return outlineThreshold; } constexpr float getOutlineThickness() const { return outlineThickness; } @@ -226,9 +222,7 @@ public: constexpr void setExposure(float value) { exposure = value; } constexpr void setBloomThreshold(float value){ bloomThreshold = value; } constexpr void setMotionBlurStrength(float value) { motionBlurStrength = value; } - constexpr void setDepthOfFieldFocus(float value){ dofFocus = value; } constexpr void setDepthOfFieldStrength(float value) { dofStrength = value; } - constexpr void setDepthOfFieldAspect(float value){ dofAspect = value; } constexpr void setSharpenFilterAmount(float value) { sharpenFilterAmount = value; } constexpr void setOutlineThreshold(float value) { outlineThreshold = value; } constexpr void setOutlineThickness(float value) { outlineThickness = value; } diff --git a/WickedEngine/RenderPath3D_BindLua.cpp b/WickedEngine/RenderPath3D_BindLua.cpp index 54b7bd466..74d4c5b87 100644 --- a/WickedEngine/RenderPath3D_BindLua.cpp +++ b/WickedEngine/RenderPath3D_BindLua.cpp @@ -43,9 +43,7 @@ Luna::FunctionType RenderPath3D_BindLua::methods[] = { lunamethod(RenderPath3D_BindLua, SetSharpenFilterAmount), lunamethod(RenderPath3D_BindLua, SetExposure), lunamethod(RenderPath3D_BindLua, SetMotionBlurStrength), - lunamethod(RenderPath3D_BindLua, SetDepthOfFieldFocus), lunamethod(RenderPath3D_BindLua, SetDepthOfFieldStrength), - lunamethod(RenderPath3D_BindLua, SetDepthOfFieldAspect), { NULL, NULL } }; Luna::PropertyType RenderPath3D_BindLua::properties[] = { @@ -348,21 +346,6 @@ int RenderPath3D_BindLua::SetMotionBlurStrength(lua_State* L) wiLua::SError(L, "SetMotionBlurStrength(float value) not enough arguments!"); return 0; } -int RenderPath3D_BindLua::SetDepthOfFieldFocus(lua_State* L) -{ - if (component == nullptr) - { - wiLua::SError(L, "SetDepthOfFieldFocus(float value) component is null!"); - return 0; - } - if (wiLua::SGetArgCount(L) > 0) - { - ((RenderPath3D*)component)->setDepthOfFieldFocus(wiLua::SGetFloat(L, 1)); - } - else - wiLua::SError(L, "SetDepthOfFieldFocus(float value) not enough arguments!"); - return 0; -} int RenderPath3D_BindLua::SetDepthOfFieldStrength(lua_State* L) { if (component == nullptr) @@ -378,21 +361,6 @@ int RenderPath3D_BindLua::SetDepthOfFieldStrength(lua_State* L) wiLua::SError(L, "SetDepthOfFieldStrength(float value) not enough arguments!"); return 0; } -int RenderPath3D_BindLua::SetDepthOfFieldAspect(lua_State* L) -{ - if (component == nullptr) - { - wiLua::SError(L, "SetDepthOfFieldAspect(float value) component is null!"); - return 0; - } - if (wiLua::SGetArgCount(L) > 0) - { - ((RenderPath3D*)component)->setDepthOfFieldAspect(wiLua::SGetFloat(L, 1)); - } - else - wiLua::SError(L, "SetDepthOfFieldAspect(float value) not enough arguments!"); - return 0; -} void RenderPath3D_BindLua::Bind() { diff --git a/WickedEngine/RenderPath3D_BindLua.h b/WickedEngine/RenderPath3D_BindLua.h index df018a43e..f1a4e97ba 100644 --- a/WickedEngine/RenderPath3D_BindLua.h +++ b/WickedEngine/RenderPath3D_BindLua.h @@ -44,9 +44,7 @@ public: int SetSharpenFilterAmount(lua_State* L); int SetExposure(lua_State* L); int SetMotionBlurStrength(lua_State* L); - int SetDepthOfFieldFocus(lua_State* L); int SetDepthOfFieldStrength(lua_State* L); - int SetDepthOfFieldAspect(lua_State* L); static void Bind(); }; diff --git a/WickedEngine/shaders/ShaderInterop_Postprocess.h b/WickedEngine/shaders/ShaderInterop_Postprocess.h index 4ddd37d5f..52faf1a4f 100644 --- a/WickedEngine/shaders/ShaderInterop_Postprocess.h +++ b/WickedEngine/shaders/ShaderInterop_Postprocess.h @@ -75,10 +75,8 @@ static const uint MOTIONBLUR_TILESIZE = 32; #define motionblur_strength xPPParams0.x static const uint DEPTHOFFIELD_TILESIZE = 32; -#define dof_focus xPPParams0.x -#define dof_scale xPPParams0.y -#define dof_aspect xPPParams0.z -#define dof_maxcoc xPPParams0.w +#define dof_cocscale xPPParams0.x +#define dof_maxcoc xPPParams0.y struct PushConstantsTonemap { diff --git a/WickedEngine/shaders/ShaderInterop_Renderer.h b/WickedEngine/shaders/ShaderInterop_Renderer.h index f0e9a37ab..2b95db494 100644 --- a/WickedEngine/shaders/ShaderInterop_Renderer.h +++ b/WickedEngine/shaders/ShaderInterop_Renderer.h @@ -434,6 +434,10 @@ CBUFFER(CameraCB, CBSLOT_RENDERER_CAMERA) float4x4 g_xCamera_PrevVP; // PrevView*PrevProjection float4x4 g_xCamera_PrevInvVP; // Inverse(PrevView*PrevProjection) float4x4 g_xCamera_ReflVP; // ReflectionView*ReflectionProjection + + float2 g_xCamera_ApertureShape; + float g_xCamera_ApertureSize; + float g_xCamera_FocalLength; }; diff --git a/WickedEngine/shaders/depthOfFieldHF.hlsli b/WickedEngine/shaders/depthOfFieldHF.hlsli index 54afe2b0d..24fd6bb56 100644 --- a/WickedEngine/shaders/depthOfFieldHF.hlsli +++ b/WickedEngine/shaders/depthOfFieldHF.hlsli @@ -3,7 +3,7 @@ inline float get_coc(in float linear_depth) { - return min(dof_maxcoc, dof_scale * pow(abs(1 - dof_focus / (linear_depth * g_xCamera_ZFarP)), 2.0f)); + return min(dof_maxcoc, dof_cocscale * g_xCamera_ApertureSize * pow(abs(1 - g_xCamera_FocalLength / (linear_depth * g_xCamera_ZFarP)), 2.0f)); } #define DOF_DEPTH_SCALE_FOREGROUND (g_xCamera_ZFarP * 1.5) diff --git a/WickedEngine/shaders/depthoffield_mainCS.hlsl b/WickedEngine/shaders/depthoffield_mainCS.hlsl index 3cb5b0fb1..611f01aec 100644 --- a/WickedEngine/shaders/depthoffield_mainCS.hlsl +++ b/WickedEngine/shaders/depthoffield_mainCS.hlsl @@ -46,7 +46,7 @@ void main(uint3 Gid : SV_GroupID, uint3 GTid : SV_GroupThreadID) const uint ringCount = clamp(ceil(pow(maxcoc, 0.5f) * DOF_RING_COUNT), 1, DOF_RING_COUNT); const float spreadScale = ringCount / maxcoc; - const float2 ringScale = float(ringCount) / float(DOF_RING_COUNT) * maxcoc * float2(max(1, dof_aspect), max(1, 2 - dof_aspect)) * xPPResolution_rcp; + const float2 ringScale = float(ringCount) / float(DOF_RING_COUNT) * maxcoc * g_xCamera_ApertureShape * xPPResolution_rcp; const float3 center_color = texture_prefilter[pixel]; const float3 center_presort = texture_presort[pixel]; @@ -56,8 +56,6 @@ void main(uint3 Gid : SV_GroupID, uint3 GTid : SV_GroupThreadID) const float2 uv = (pixel + 0.5f) * xPPResolution_rcp; - float seed = 12345; - #ifdef DEPTHOFFIELD_CHEAP color = center_color; [unroll(DOF_RING_COUNT)] diff --git a/WickedEngine/shaders/depthoffield_prepassCS.hlsl b/WickedEngine/shaders/depthoffield_prepassCS.hlsl index 26c6b9ccb..eb4ce921a 100644 --- a/WickedEngine/shaders/depthoffield_prepassCS.hlsl +++ b/WickedEngine/shaders/depthoffield_prepassCS.hlsl @@ -47,9 +47,7 @@ void main(uint3 Gid : SV_GroupID, uint3 GTid : SV_GroupThreadID) [branch] if (backgroundFactor < 1.0f) { - float seed = 54321; - - const float2 ringScale = 0.5f * coc * float2(max(1, dof_aspect), max(1, 2 - dof_aspect)) * xPPResolution_rcp; + const float2 ringScale = 0.5f * coc * g_xCamera_ApertureShape * xPPResolution_rcp; [unroll] for (uint i = ringSampleCount[0]; i < ringSampleCount[1]; ++i) { diff --git a/WickedEngine/shaders/raytraceCS.hlsl b/WickedEngine/shaders/raytraceCS.hlsl index 8f771d385..01bbbcc88 100644 --- a/WickedEngine/shaders/raytraceCS.hlsl +++ b/WickedEngine/shaders/raytraceCS.hlsl @@ -24,6 +24,17 @@ void main(uint3 DTid : SV_DispatchThreadID, uint groupIndex : SV_GroupIndex) // Create starting ray: RayDesc ray = CreateCameraRay(uv); + // Depth of field setup: + float3 focal_point = ray.Origin + ray.Direction * g_xCamera_FocalLength; + float3 coc = float3(hemispherepoint_cos(rand(seed, pixel), rand(seed, pixel)).xy, 0); + coc.xy *= g_xCamera_ApertureShape.xy; + coc = mul(coc, float3x3(cross(g_xCamera_Up, g_xCamera_At), g_xCamera_Up, g_xCamera_At)); + coc *= g_xCamera_FocalLength; + coc *= g_xCamera_ApertureSize; + coc *= 0.1f; + ray.Origin = ray.Origin + coc; + ray.Direction = focal_point - ray.Origin; // will be normalized before tracing! + uint bounces = xTraceUserData.x; const uint bouncelimit = 16; for (uint bounce = 0; ((bounce < min(bounces, bouncelimit)) && any(energy)); ++bounce) @@ -169,7 +180,7 @@ void main(uint3 DTid : SV_DispatchThreadID, uint groupIndex : SV_GroupIndex) } } - float3 lightColor = 0; + float3 lightColor = 1; SurfaceToLight surfaceToLight = (SurfaceToLight)0; // Light sampling: @@ -204,7 +215,7 @@ void main(uint3 DTid : SV_DispatchThreadID, uint groupIndex : SV_GroupIndex) atmosphereTransmittance = GetAtmosphericLightTransmittance(Atmosphere, surface.P, L, texture_transmittancelut); } - lightColor = light.GetColor().rgb * light.GetEnergy() * atmosphereTransmittance; + lightColor *= atmosphereTransmittance; } } break; @@ -229,7 +240,6 @@ void main(uint3 DTid : SV_DispatchThreadID, uint groupIndex : SV_GroupIndex) const float att = saturate(1 - (dist2 / range2)); const float attenuation = att * att; - lightColor = light.GetColor().rgb * light.GetEnergy(); lightColor *= attenuation; } } @@ -263,7 +273,6 @@ void main(uint3 DTid : SV_DispatchThreadID, uint groupIndex : SV_GroupIndex) float attenuation = att * att; attenuation *= saturate((1 - (1 - SpotFactor) * 1 / (1 - spotCutOff))); - lightColor = light.GetColor().rgb * light.GetEnergy(); lightColor *= attenuation; } } @@ -326,6 +335,7 @@ void main(uint3 DTid : SV_DispatchThreadID, uint groupIndex : SV_GroupIndex) #endif // RTAPI if (any(shadow)) { + lightColor *= light.GetColor().rgb * light.GetEnergy(); lighting.direct.specular = lightColor * BRDF_GetSpecular(surface, surfaceToLight); lighting.direct.diffuse = lightColor * BRDF_GetDiffuse(surface, surfaceToLight); result += max(0, shadow * (surface.albedo * lighting.direct.diffuse + lighting.direct.specular)); diff --git a/WickedEngine/wiArchive.cpp b/WickedEngine/wiArchive.cpp index 9190067ef..53aa9808e 100644 --- a/WickedEngine/wiArchive.cpp +++ b/WickedEngine/wiArchive.cpp @@ -6,7 +6,7 @@ using namespace std; // this should always be only INCREMENTED and only if a new serialization is implemeted somewhere! -uint64_t __archiveVersion = 64; +uint64_t __archiveVersion = 65; // this is the version number of which below the archive is not compatible with the current version uint64_t __archiveVersionBarrier = 22; diff --git a/WickedEngine/wiRenderer.cpp b/WickedEngine/wiRenderer.cpp index 469af1549..64ba8ad5b 100644 --- a/WickedEngine/wiRenderer.cpp +++ b/WickedEngine/wiRenderer.cpp @@ -7901,7 +7901,7 @@ void RayTraceScene( cb.xTraceResolution_rcp.y = 1.0f / cb.xTraceResolution.y; cb.xTraceUserData.x = raytraceBounceCount; cb.xTraceUserData.y = accumulation_sample; - cb.xTraceRandomSeed = (accumulation_sample + 1) * 0.0001f; + cb.xTraceRandomSeed = wiRandom::getRandom(1000000) / 1000000.f; device->UpdateBuffer(&constantBuffers[CBTYPE_RAYTRACE], &cb, cmd); device->BindConstantBuffer(CS, &constantBuffers[CBTYPE_RAYTRACE], CB_GETBINDSLOT(RaytracingCB), cmd); @@ -8277,7 +8277,7 @@ void RenderObjectLightMap(const Scene& scene, const ObjectComponent& object, Com cb.xTracePixelOffset.y = (halton.y * 2 - 1) * cb.xTraceResolution_rcp.y; cb.xTracePixelOffset.x *= 1.4f; // boost the jitter by a bit cb.xTracePixelOffset.y *= 1.4f; // boost the jitter by a bit - cb.xTraceRandomSeed = (lightmapIterationCount + 1) * 0.0001f; // random seed + cb.xTraceRandomSeed = wiRandom::getRandom(1000000) / 1000000.f; cb.xTraceAccumulationFactor = 1.0f / (lightmapIterationCount + 1.0f); // accumulation factor (alpha) cb.xTraceUserData.x = raytraceBounceCount; device->UpdateBuffer(&constantBuffers[CBTYPE_RAYTRACE], &cb, cmd); @@ -8435,6 +8435,10 @@ void UpdateCameraCB( XMStoreFloat4x4(&cb.g_xCamera_PrevInvVP, camera_previous.GetInvViewProjection()); XMStoreFloat4x4(&cb.g_xCamera_ReflVP, camera_reflection.GetViewProjection()); + cb.g_xCamera_FocalLength = camera.focal_length; + cb.g_xCamera_ApertureSize = camera.aperture_size; + cb.g_xCamera_ApertureShape = camera.aperture_shape; + device->UpdateBuffer(&constantBuffers[CBTYPE_CAMERA], &cb, cmd); } @@ -10651,9 +10655,7 @@ void Postprocess_DepthOfField( const Texture& output, const Texture& lineardepth, CommandList cmd, - float focus, - float scale, - float aspect, + float coc_scale, float max_coc ) { @@ -10669,9 +10671,7 @@ void Postprocess_DepthOfField( cb.xPPResolution.y = desc.Height; cb.xPPResolution_rcp.x = 1.0f / cb.xPPResolution.x; cb.xPPResolution_rcp.y = 1.0f / cb.xPPResolution.y; - cb.dof_focus = focus; - cb.dof_scale = scale; - cb.dof_aspect = aspect; + cb.dof_cocscale = coc_scale; cb.dof_maxcoc = max_coc; device->UpdateBuffer(&constantBuffers[CBTYPE_POSTPROCESS], &cb, cmd); device->BindConstantBuffer(CS, &constantBuffers[CBTYPE_POSTPROCESS], CB_GETBINDSLOT(PostProcessCB), cmd); diff --git a/WickedEngine/wiRenderer.h b/WickedEngine/wiRenderer.h index ad8bafb14..cc1aac9af 100644 --- a/WickedEngine/wiRenderer.h +++ b/WickedEngine/wiRenderer.h @@ -490,10 +490,8 @@ namespace wiRenderer const wiGraphics::Texture& output, const wiGraphics::Texture& lineardepth, wiGraphics::CommandList cmd, - float focus = 10.0f, - float scale = 1.0f, - float aspect = 1.0f, - float max_coc = 18.0f + float coc_scale = 10, + float max_coc = 18 ); void Postprocess_Outline( const wiGraphics::Texture& input, diff --git a/WickedEngine/wiScene.h b/WickedEngine/wiScene.h index 7252a4380..422818649 100644 --- a/WickedEngine/wiScene.h +++ b/WickedEngine/wiScene.h @@ -892,6 +892,9 @@ namespace wiScene float zNearP = 0.1f; float zFarP = 800.0f; float fov = XM_PI / 3.0f; + float focal_length = 1; + float aperture_size = 0; + XMFLOAT2 aperture_shape = XMFLOAT2(1, 1); // Non-serialized attributes: XMFLOAT3 Eye = XMFLOAT3(0, 0, 0); diff --git a/WickedEngine/wiScene_BindLua.cpp b/WickedEngine/wiScene_BindLua.cpp index 987b94bdf..9106a6da3 100644 --- a/WickedEngine/wiScene_BindLua.cpp +++ b/WickedEngine/wiScene_BindLua.cpp @@ -1477,6 +1477,12 @@ Luna::FunctionType CameraComponent_BindLua::methods[] = lunamethod(CameraComponent_BindLua, SetNearPlane), lunamethod(CameraComponent_BindLua, GetFarPlane), lunamethod(CameraComponent_BindLua, SetFarPlane), + lunamethod(CameraComponent_BindLua, GetFocalLength), + lunamethod(CameraComponent_BindLua, SetFocalLength), + lunamethod(CameraComponent_BindLua, GetApertureSize), + lunamethod(CameraComponent_BindLua, SetApertureSize), + lunamethod(CameraComponent_BindLua, GetApertureShape), + lunamethod(CameraComponent_BindLua, SetApertureShape), lunamethod(CameraComponent_BindLua, GetView), lunamethod(CameraComponent_BindLua, GetProjection), lunamethod(CameraComponent_BindLua, GetViewProjection), @@ -1583,6 +1589,64 @@ int CameraComponent_BindLua::SetFarPlane(lua_State* L) } return 0; } +int CameraComponent_BindLua::GetFocalLength(lua_State* L) +{ + wiLua::SSetFloat(L, component->focal_length); + return 1; +} +int CameraComponent_BindLua::SetFocalLength(lua_State* L) +{ + int argc = wiLua::SGetArgCount(L); + if (argc > 0) + { + component->focal_length = wiLua::SGetFloat(L, 1); + } + else + { + wiLua::SError(L, "SetFocalLength(float value) not enough arguments!"); + } + return 0; +} +int CameraComponent_BindLua::GetApertureSize(lua_State* L) +{ + wiLua::SSetFloat(L, component->aperture_size); + return 1; +} +int CameraComponent_BindLua::SetApertureSize(lua_State* L) +{ + int argc = wiLua::SGetArgCount(L); + if (argc > 0) + { + component->aperture_size = wiLua::SGetFloat(L, 1); + } + else + { + wiLua::SError(L, "SetApertureSize(float value) not enough arguments!"); + } + return 0; +} +int CameraComponent_BindLua::GetApertureShape(lua_State* L) +{ + Luna::push(L, new Vector_BindLua(XMLoadFloat2(&component->aperture_shape))); + return 1; +} +int CameraComponent_BindLua::SetApertureShape(lua_State* L) +{ + int argc = wiLua::SGetArgCount(L); + if (argc > 0) + { + Vector_BindLua* param = Luna::lightcheck(L, 1); + if (param != nullptr) + { + XMStoreFloat2(&component->aperture_shape, param->vector); + } + } + else + { + wiLua::SError(L, "SetApertureShape(Vector value) not enough arguments!"); + } + return 0; +} int CameraComponent_BindLua::GetView(lua_State* L) { Luna::push(L, new Matrix_BindLua(component->GetView())); diff --git a/WickedEngine/wiScene_BindLua.h b/WickedEngine/wiScene_BindLua.h index 9f7f4cef4..40d0ef898 100644 --- a/WickedEngine/wiScene_BindLua.h +++ b/WickedEngine/wiScene_BindLua.h @@ -167,6 +167,12 @@ namespace wiScene_BindLua int SetNearPlane(lua_State* L); int GetFarPlane(lua_State* L); int SetFarPlane(lua_State* L); + int GetFocalLength(lua_State* L); + int SetFocalLength(lua_State* L); + int GetApertureSize(lua_State* L); + int SetApertureSize(lua_State* L); + int GetApertureShape(lua_State* L); + int SetApertureShape(lua_State* L); int GetView(lua_State* L); int GetProjection(lua_State* L); int GetViewProjection(lua_State* L); diff --git a/WickedEngine/wiScene_Serializers.cpp b/WickedEngine/wiScene_Serializers.cpp index 15efc75a5..14fcc76e0 100644 --- a/WickedEngine/wiScene_Serializers.cpp +++ b/WickedEngine/wiScene_Serializers.cpp @@ -749,6 +749,13 @@ namespace wiScene archive >> zFarP; archive >> fov; + if (archive.GetVersion() >= 65) + { + archive >> focal_length; + archive >> aperture_size; + archive >> aperture_shape; + } + SetDirty(); } else @@ -759,6 +766,13 @@ namespace wiScene archive << zNearP; archive << zFarP; archive << fov; + + if (archive.GetVersion() >= 65) + { + archive << focal_length; + archive << aperture_size; + archive << aperture_shape; + } } } void EnvironmentProbeComponent::Serialize(wiArchive& archive, EntitySerializer& seri) diff --git a/WickedEngine/wiVersion.cpp b/WickedEngine/wiVersion.cpp index d840b34de..f2ba28351 100644 --- a/WickedEngine/wiVersion.cpp +++ b/WickedEngine/wiVersion.cpp @@ -9,7 +9,7 @@ namespace wiVersion // minor features, major updates, breaking compatibility changes const int minor = 55; // minor bug fixes, alterations, refactors, updates - const int revision = 12; + const int revision = 13; const std::string version_string = std::to_string(major) + "." + std::to_string(minor) + "." + std::to_string(revision);