diff --git a/Content/Editor/Camera/M_Camera.flax b/Content/Editor/Camera/M_Camera.flax index 16bf873eb..3e93768ea 100644 --- a/Content/Editor/Camera/M_Camera.flax +++ b/Content/Editor/Camera/M_Camera.flax @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:c7a4c3bb2a339de16d9738b35538d33670a224d69f8eba00e5977d03cfa85f83 +oid sha256:91bd11d6a7304b2fbf90a20e187e0361ad8197232f71e16bc7ce3a7a74a44899 size 30456 diff --git a/Content/Editor/CubeTexturePreviewMaterial.flax b/Content/Editor/CubeTexturePreviewMaterial.flax index d1e545233..20f23a065 100644 --- a/Content/Editor/CubeTexturePreviewMaterial.flax +++ b/Content/Editor/CubeTexturePreviewMaterial.flax @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:89a9ab2a0168f0f51fd34ee064a896a65d1a3f85214cf04e370b82d41329cb09 +oid sha256:088ee6360b5dfaa084b70345219e986d2d98460d85660720e79d14cf3be89c43 size 32171 diff --git a/Content/Editor/DebugMaterials/DDGIDebugProbes.flax b/Content/Editor/DebugMaterials/DDGIDebugProbes.flax index 6a70e5490..967ab6221 100644 --- a/Content/Editor/DebugMaterials/DDGIDebugProbes.flax +++ b/Content/Editor/DebugMaterials/DDGIDebugProbes.flax @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:90cfc9c29e29f8b25ff3f68394cb5133a95a936e7e6c0d0fe7cc4890a2a1ed61 +oid sha256:9b2c042f719386568cb4585f07bad544ced23bffe6094c3f0c482fa4edb13053 size 41404 diff --git a/Content/Editor/DebugMaterials/SingleColor/Decal.flax b/Content/Editor/DebugMaterials/SingleColor/Decal.flax index e1b60860c..83fae0f76 100644 --- a/Content/Editor/DebugMaterials/SingleColor/Decal.flax +++ b/Content/Editor/DebugMaterials/SingleColor/Decal.flax @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:188d0f0574a2b12e96945fdde66082d0a63bf33965260969d50decbbbbd32590 +oid sha256:728b60a67786f8c50c02e3f496c76ad029943f4bc74e5769c5ac21faeef06d6e size 7489 diff --git a/Content/Editor/DebugMaterials/SingleColor/Particle.flax b/Content/Editor/DebugMaterials/SingleColor/Particle.flax index 1a84ad997..c53067528 100644 --- a/Content/Editor/DebugMaterials/SingleColor/Particle.flax +++ b/Content/Editor/DebugMaterials/SingleColor/Particle.flax @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:a6bfa4696c210cfdc8a3f3b6c1c28217e717bf8abe2dfacb793b36410681b388 -size 31721 +oid sha256:153b557292cab18c67490852e9ee80fd0dbdbf7e9fde559669e2440a9ce538f5 +size 32225 diff --git a/Content/Editor/DebugMaterials/SingleColor/Surface.flax b/Content/Editor/DebugMaterials/SingleColor/Surface.flax index e824434ac..09b96e399 100644 --- a/Content/Editor/DebugMaterials/SingleColor/Surface.flax +++ b/Content/Editor/DebugMaterials/SingleColor/Surface.flax @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:b5428f5f963d38ba8de16a932fba4f722e9b6bb03d874020122df3396841ea0e +oid sha256:da13bad48f77ba01bef9207fe5fb90b6a53dcf2ad48d6527598795dd1820bf14 size 30352 diff --git a/Content/Editor/DebugMaterials/SingleColor/SurfaceAdditive.flax b/Content/Editor/DebugMaterials/SingleColor/SurfaceAdditive.flax index 602434c46..a5b2ad177 100644 --- a/Content/Editor/DebugMaterials/SingleColor/SurfaceAdditive.flax +++ b/Content/Editor/DebugMaterials/SingleColor/SurfaceAdditive.flax @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:c4aa5cf0289c1489829e400d8c244712df6214520d1bc67feabcfe7e71c3b0f9 -size 32243 +oid sha256:89af473537cc03e7f0d07b3dfb0c515c608b1c38d572fcae01456b73bfc805b2 +size 32747 diff --git a/Content/Editor/DebugMaterials/SingleColor/Terrain.flax b/Content/Editor/DebugMaterials/SingleColor/Terrain.flax index 55029e5fb..4e385147c 100644 --- a/Content/Editor/DebugMaterials/SingleColor/Terrain.flax +++ b/Content/Editor/DebugMaterials/SingleColor/Terrain.flax @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:07066043d6c634162ade92ca33119be5798fd9c8183712b6569e027c41cc1bff +oid sha256:a2430a9200896c29f5cefca67099895f453c91adce655cbe19711cb66f0f0f68 size 20737 diff --git a/Content/Editor/DefaultFontMaterial.flax b/Content/Editor/DefaultFontMaterial.flax index fd8e96f88..5f62ee8ef 100644 --- a/Content/Editor/DefaultFontMaterial.flax +++ b/Content/Editor/DefaultFontMaterial.flax @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:33bd3e45519a05d48a7035f0740b3f879d962d80f7c4bc546505583585545341 +oid sha256:97f09eb4517c0e837033174115998aa673a196232f1cbc95ca50a6387613234c size 30531 diff --git a/Content/Editor/Gizmo/FoliageBrushMaterial.flax b/Content/Editor/Gizmo/FoliageBrushMaterial.flax index 81da98526..cf102fbd7 100644 --- a/Content/Editor/Gizmo/FoliageBrushMaterial.flax +++ b/Content/Editor/Gizmo/FoliageBrushMaterial.flax @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:eafdd82ba4c82f82e70525b4530f7f332646ce1f7d19c4c435fae84a25c502bf -size 38270 +oid sha256:c6f86f2215694fb0dccd685b9301c50c2c0f58a2fc669256632f03ca5dc57a8e +size 38774 diff --git a/Content/Editor/Gizmo/Material.flax b/Content/Editor/Gizmo/Material.flax index e6dab36f5..3dd1cd529 100644 --- a/Content/Editor/Gizmo/Material.flax +++ b/Content/Editor/Gizmo/Material.flax @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:ae83a3e9e14ebdd5bde7e6b40f8bd2de8a530a4471b111470d14359536f22a00 -size 32847 +oid sha256:a9b8f22650bac56484258539bee1a15f70ec6cb732c39e530494f128b3e523bd +size 33373 diff --git a/Content/Editor/Gizmo/MaterialWire.flax b/Content/Editor/Gizmo/MaterialWire.flax index b185becd2..1d6a332d6 100644 --- a/Content/Editor/Gizmo/MaterialWire.flax +++ b/Content/Editor/Gizmo/MaterialWire.flax @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:cc57eb71f0cde517be6082957b85439e6e464795bca9ef1c23fb00c679ac183a -size 31985 +oid sha256:21a6b790ea940e1b906306edd902f8796d02911126bc630d7afb31a2d5346dbe +size 32489 diff --git a/Content/Editor/Gizmo/SelectionOutlineMaterial.flax b/Content/Editor/Gizmo/SelectionOutlineMaterial.flax index f2877f0ec..be629fad7 100644 --- a/Content/Editor/Gizmo/SelectionOutlineMaterial.flax +++ b/Content/Editor/Gizmo/SelectionOutlineMaterial.flax @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:7fbde1a8dd1bdc621f562f18b7056c50de9df801719989c304a5a3aecbb31a4e +oid sha256:a8248cb3493de63e033edd1c5c784c57478eac0dd299505e43b57a0e7803f485 size 16166 diff --git a/Content/Editor/Gizmo/VertexColorsPreviewMaterial.flax b/Content/Editor/Gizmo/VertexColorsPreviewMaterial.flax index d92847c20..b42c9b927 100644 --- a/Content/Editor/Gizmo/VertexColorsPreviewMaterial.flax +++ b/Content/Editor/Gizmo/VertexColorsPreviewMaterial.flax @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:5835c9950bf5948a2a1bff56ed731a7e1788e0a03fea10e11762f40143057ae9 +oid sha256:6228ed09a33574dd15b169489c14268a873932334d0612d220da45cc973232ba size 31465 diff --git a/Content/Editor/Highlight Material.flax b/Content/Editor/Highlight Material.flax index 87488d15b..5dfd93651 100644 --- a/Content/Editor/Highlight Material.flax +++ b/Content/Editor/Highlight Material.flax @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:ecf23fb09c1d049f3c42668c2dabc6ce53f168af1b7b4d3ead13309799011e17 -size 30632 +oid sha256:2b8c75334a5b1966742706bb7ae1a6ed74cf58a32e47814cc33084ed90cb93f1 +size 31158 diff --git a/Content/Editor/Icons/IconsMaterial.flax b/Content/Editor/Icons/IconsMaterial.flax index 241c15820..296782a7b 100644 --- a/Content/Editor/Icons/IconsMaterial.flax +++ b/Content/Editor/Icons/IconsMaterial.flax @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:97b6c13093d5ca38b2d70da09fb7c7e9a289dd6a7812d8333c67a5558e410b8e -size 30560 +oid sha256:0b98bdd3156ebe53935a51da9fa5b04216e4f49f88b5f5304749c67afb4062a2 +size 31086 diff --git a/Content/Editor/IesProfilePreviewMaterial.flax b/Content/Editor/IesProfilePreviewMaterial.flax index 818bec914..9ed068e8f 100644 --- a/Content/Editor/IesProfilePreviewMaterial.flax +++ b/Content/Editor/IesProfilePreviewMaterial.flax @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:933cf9e187f8a475ae571560b4fe20edddc5d3a869e4696b81021031d8f4981c +oid sha256:39bf0f9e7d03c3356149fb768ff0b8b081e325bdbf4f12bd5ee8b5249aa49a0a size 18205 diff --git a/Content/Editor/MaterialTemplates/Features/ForwardShading.hlsl b/Content/Editor/MaterialTemplates/Features/ForwardShading.hlsl index 9dbc19369..bbc06f15d 100644 --- a/Content/Editor/MaterialTemplates/Features/ForwardShading.hlsl +++ b/Content/Editor/MaterialTemplates/Features/ForwardShading.hlsl @@ -125,6 +125,20 @@ void PS_Forward( float3 screenColor = sceneColorTexture.SampleLevel(SamplerPointClamp, hit.xy, 0).rgb; reflections = lerp(reflections, screenColor, hit.z); } + + // Fallback to software tracing if possible +#if USE_GLOBAL_SURFACE_ATLAS && CAN_USE_GLOBAL_SURFACE_ATLAS + if (hit.z < REFLECTIONS_HIT_THRESHOLD) + { + float3 reflectWS = ScreenSpaceReflectionDirection(screenUV, gBuffer, ViewPos); + float4 surfaceAtlas; + if (TraceSDFSoftwareReflections(gBuffer, reflectWS, surfaceAtlas)) + { + float3 screenColor = sceneColorTexture.SampleLevel(SamplerPointClamp, hit.xy, 0).rgb; + reflections = lerp(surfaceAtlas, float4(screenColor, 1), hit.z); + } + } +#endif #endif light.rgb += reflections * GetReflectionSpecularLighting(ViewPos, gBuffer) * light.a; diff --git a/Content/Editor/MaterialTemplates/Features/SDFReflections.hlsl b/Content/Editor/MaterialTemplates/Features/SDFReflections.hlsl new file mode 100644 index 000000000..34201f546 --- /dev/null +++ b/Content/Editor/MaterialTemplates/Features/SDFReflections.hlsl @@ -0,0 +1,37 @@ +// Copyright (c) 2012-2024 Wojciech Figat. All rights reserved. + +@0// SDF Reflections: Defines +#define USE_GLOBAL_SURFACE_ATLAS 1 +@1// SDF Reflections: Includes +#include "./Flax/GlobalSignDistanceField.hlsl" +#include "./Flax/GI/GlobalSurfaceAtlas.hlsl" +@2// SDF Reflections: Constants +GlobalSDFData GlobalSDF; +GlobalSurfaceAtlasData GlobalSurfaceAtlas; +@3// SDF Reflections: Resources +Texture3D GlobalSDFTex : register(t__SRV__); +Texture3D GlobalSDFMip : register(t__SRV__); +ByteAddressBuffer GlobalSurfaceAtlasChunks : register(t__SRV__); +ByteAddressBuffer RWGlobalSurfaceAtlasCulledObjects : register(t__SRV__); +Buffer GlobalSurfaceAtlasObjects : register(t__SRV__); +Texture2D GlobalSurfaceAtlasDepth : register(t__SRV__); +Texture2D GlobalSurfaceAtlasTex : register(t__SRV__); +@4// SDF Reflections: Utilities +bool TraceSDFSoftwareReflections(GBufferSample gBuffer, float3 reflectWS, out float4 surfaceAtlas) +{ + GlobalSDFTrace sdfTrace; + float maxDistance = GLOBAL_SDF_WORLD_SIZE; + float selfOcclusionBias = GlobalSDF.CascadeVoxelSize[0]; + sdfTrace.Init(gBuffer.WorldPos + gBuffer.Normal * selfOcclusionBias, reflectWS, 0.0f, maxDistance); + GlobalSDFHit sdfHit = RayTraceGlobalSDF(GlobalSDF, GlobalSDFTex, GlobalSDFMip, sdfTrace); + if (sdfHit.IsHit()) + { + float3 hitPosition = sdfHit.GetHitPosition(sdfTrace); + float surfaceThreshold = GetGlobalSurfaceAtlasThreshold(GlobalSDF, sdfHit); + surfaceAtlas = SampleGlobalSurfaceAtlas(GlobalSurfaceAtlas, GlobalSurfaceAtlasChunks, RWGlobalSurfaceAtlasCulledObjects, GlobalSurfaceAtlasObjects, GlobalSurfaceAtlasDepth, GlobalSurfaceAtlasTex, hitPosition, -reflectWS, surfaceThreshold); + return true; + } + return false; +} + +@5// SDF Reflections: Shaders diff --git a/Content/Editor/Particles/Particle Material Color.flax b/Content/Editor/Particles/Particle Material Color.flax index c7f46c810..f51633f2d 100644 --- a/Content/Editor/Particles/Particle Material Color.flax +++ b/Content/Editor/Particles/Particle Material Color.flax @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:0156c820df3b48f412b66c0bfdbc462896b36b30f59cab250c5a06e274bc193f -size 29952 +oid sha256:6fd8f139ce4d7eb4e59c32c050416c267b6cf1749e32d43b127c7dea81457e0c +size 30456 diff --git a/Content/Editor/Particles/Smoke Material.flax b/Content/Editor/Particles/Smoke Material.flax index dae8d0226..6d2ade5f5 100644 --- a/Content/Editor/Particles/Smoke Material.flax +++ b/Content/Editor/Particles/Smoke Material.flax @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:20635d62896ef520330d4a63b357a2e2f991b0fa7cdcd8bb586bfe5e7669717a -size 37132 +oid sha256:f307b7ccfff2c95c0a2642fbb43135961c0928d65c08c4a8bf5fbadd46534555 +size 37636 diff --git a/Content/Editor/SpriteMaterial.flax b/Content/Editor/SpriteMaterial.flax index 562280feb..2748f3b8d 100644 --- a/Content/Editor/SpriteMaterial.flax +++ b/Content/Editor/SpriteMaterial.flax @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:9c2385c51030b3224bce8896d4c692b41316373c76341c51f15bbf0c4c3fc240 +oid sha256:4d9860694b46d00e299ac8c883069d6df834e1070075ca5ff47eb854683d7c26 size 31544 diff --git a/Content/Editor/Terrain/Circle Brush Material.flax b/Content/Editor/Terrain/Circle Brush Material.flax index 38e8ac906..77bccb411 100644 --- a/Content/Editor/Terrain/Circle Brush Material.flax +++ b/Content/Editor/Terrain/Circle Brush Material.flax @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:f9c9455b6f4935d35b0a63cd9aea417f010576a630d3be9e1a124ca1d531ae8a +oid sha256:783c85f8938703a0ee1539de21a21843ae0cf6ca0d26df67c2debfac9398c3a4 size 27409 diff --git a/Content/Editor/Terrain/Highlight Terrain Material.flax b/Content/Editor/Terrain/Highlight Terrain Material.flax index f1e03310f..04a5fb7e0 100644 --- a/Content/Editor/Terrain/Highlight Terrain Material.flax +++ b/Content/Editor/Terrain/Highlight Terrain Material.flax @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:7436293db3692279754eaf29aed820a86ff214efc05a336a266582e8e0d3393c +oid sha256:098a1002fb6c50a2823792e22334e6d13d82b12227d4815c8066dd2ad8b3484d size 20790 diff --git a/Content/Editor/TexturePreviewMaterial.flax b/Content/Editor/TexturePreviewMaterial.flax index 80be4d726..85bf101ac 100644 --- a/Content/Editor/TexturePreviewMaterial.flax +++ b/Content/Editor/TexturePreviewMaterial.flax @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:3c8380071cf96428989cd6fd92d70c7b7ea694fe8a939608cd4367f957e75491 +oid sha256:15f87cf87b590acbf664847067557859809279b39de2a5dfeb5b7dd71ecb9013 size 10570 diff --git a/Content/Editor/Wires Debug Material.flax b/Content/Editor/Wires Debug Material.flax index 0834d47a6..e382c02f6 100644 --- a/Content/Editor/Wires Debug Material.flax +++ b/Content/Editor/Wires Debug Material.flax @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:a9dffca5051434a4731c16db377598d424dbeab57211331a8e7d16c05e18a7d9 -size 30632 +oid sha256:a575ee5265009b22fd675bd19284fe71a69410a5968c03c6e9e33bb2928665f3 +size 31136 diff --git a/Content/Engine/DefaultDeformableMaterial.flax b/Content/Engine/DefaultDeformableMaterial.flax index 097e5353c..0150b3279 100644 --- a/Content/Engine/DefaultDeformableMaterial.flax +++ b/Content/Engine/DefaultDeformableMaterial.flax @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:0fe507d8719930b86b9acfe2a6984f0d6e13523ba32c4c5df2c823bcb42281d9 +oid sha256:32200dbf1c0c5fb5f3f999b0f8b74c852a05bf7138d34ff9618cdd34e2831e7f size 18524 diff --git a/Content/Engine/DefaultMaterial.flax b/Content/Engine/DefaultMaterial.flax index 590833abc..3f31b7610 100644 --- a/Content/Engine/DefaultMaterial.flax +++ b/Content/Engine/DefaultMaterial.flax @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:404951f3ab7a8f05b7bec519f441e27225411c327508f795ac007a401d8d1453 +oid sha256:1f8469e5fc068a62ee72b55553e0aef41251665a97c1e3db977c27ce8731fa3f size 32377 diff --git a/Content/Engine/DefaultRadialMenu.flax b/Content/Engine/DefaultRadialMenu.flax index 1159e4719..41af69613 100644 --- a/Content/Engine/DefaultRadialMenu.flax +++ b/Content/Engine/DefaultRadialMenu.flax @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:d7c517175501d1b41143a9d2d81950bf0d91d0556cfebca5784e7ef2c4ba94cf +oid sha256:4fe1e884c6ca9d3b0a9c6d0ebac3cad58216f539245cd642e74e7bb217a948f2 size 20340 diff --git a/Content/Engine/DefaultTerrainMaterial.flax b/Content/Engine/DefaultTerrainMaterial.flax index f21cdc1df..35bb0833e 100644 --- a/Content/Engine/DefaultTerrainMaterial.flax +++ b/Content/Engine/DefaultTerrainMaterial.flax @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:210ca70aeac7856c59bfcfb0c3370bc60a6724a4f2346cf4fbee1715878ca672 +oid sha256:8659571dd94657ebfb6b13c8129e61e8d7aa5e32473f772c99d5b998b5f5ab6d size 22874 diff --git a/Content/Engine/SingleColorMaterial.flax b/Content/Engine/SingleColorMaterial.flax index 7edfefde9..4fbf7717d 100644 --- a/Content/Engine/SingleColorMaterial.flax +++ b/Content/Engine/SingleColorMaterial.flax @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:6c03577e1dda81ef42be30e3f7fb1dd92821baf52a30cec05aae6fa687ede451 +oid sha256:d4f1ac78ac1943cd276fdfebd265f1451ed37fc1d2a7aa144ed5164a2b2b6802 size 30553 diff --git a/Content/Engine/SkyboxMaterial.flax b/Content/Engine/SkyboxMaterial.flax index ac9e5fa57..6a26aa180 100644 --- a/Content/Engine/SkyboxMaterial.flax +++ b/Content/Engine/SkyboxMaterial.flax @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:4ddc74bc1aa47fe9b63e0a053b9dc56751ba2a9bcfa64987eddb230e382ef10a +oid sha256:83b4f1d136aaedd99d3d443d2867fbc0c22914c6a612e2cf34d4dda41a504011 size 31751 diff --git a/Content/Shaders/SSR.flax b/Content/Shaders/SSR.flax index b3c34e95e..b3fad3d88 100644 --- a/Content/Shaders/SSR.flax +++ b/Content/Shaders/SSR.flax @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:1c42203227cf1070143a4cd1a414cc3ae3625577c7d0dfd7d204f99bf8bc4542 -size 11142 +oid sha256:9f331e8d5300e0241520b063a6ceb9a5c7bdf42da78274ff9331788e7e239126 +size 11175 diff --git a/Source/Engine/Graphics/Materials/ForwardMaterialShader.cpp b/Source/Engine/Graphics/Materials/ForwardMaterialShader.cpp index af2b30348..7aa662d43 100644 --- a/Source/Engine/Graphics/Materials/ForwardMaterialShader.cpp +++ b/Source/Engine/Graphics/Materials/ForwardMaterialShader.cpp @@ -52,7 +52,11 @@ void ForwardMaterialShader::Bind(BindParameters& params) // Setup features if ((_info.FeaturesFlags & MaterialFeaturesFlags::GlobalIllumination) != MaterialFeaturesFlags::None) + { GlobalIlluminationFeature::Bind(params, cb, srv); + if ((_info.FeaturesFlags & MaterialFeaturesFlags::ScreenSpaceReflections) != MaterialFeaturesFlags::None) + SDFReflectionsFeature::Bind(params, cb, srv); + } ForwardShadingFeature::Bind(params, cb, srv); // Setup parameters diff --git a/Source/Engine/Graphics/Materials/MaterialShader.h b/Source/Engine/Graphics/Materials/MaterialShader.h index 7a5b842e2..5a4cec20c 100644 --- a/Source/Engine/Graphics/Materials/MaterialShader.h +++ b/Source/Engine/Graphics/Materials/MaterialShader.h @@ -10,7 +10,7 @@ /// /// Current materials shader version. /// -#define MATERIAL_GRAPH_VERSION 162 +#define MATERIAL_GRAPH_VERSION 163 class Material; class GPUShader; diff --git a/Source/Engine/Graphics/Materials/MaterialShaderFeatures.cpp b/Source/Engine/Graphics/Materials/MaterialShaderFeatures.cpp index 43646cc0e..697176763 100644 --- a/Source/Engine/Graphics/Materials/MaterialShaderFeatures.cpp +++ b/Source/Engine/Graphics/Materials/MaterialShaderFeatures.cpp @@ -5,6 +5,7 @@ #include "Engine/Graphics/Textures/GPUTexture.h" #include "Engine/Renderer/RenderList.h" #include "Engine/Renderer/ShadowsPass.h" +#include "Engine/Renderer/GlobalSignDistanceFieldPass.h" #if USE_EDITOR #include "Engine/Renderer/Lightmaps.h" #endif @@ -195,6 +196,58 @@ bool GlobalIlluminationFeature::Bind(MaterialShader::BindParameters& params, Spa return useGI; } +bool SDFReflectionsFeature::Bind(MaterialShader::BindParameters& params, Span& cb, int32& srv) +{ + auto& data = *(Data*)cb.Get(); + ASSERT_LOW_LAYER(cb.Length() >= sizeof(Data)); + + bool useSDFReflections = false; + if (EnumHasAnyFlags(params.RenderContext.View.Flags, ViewFlags::Reflections)) + { + switch (params.RenderContext.List->Settings.ScreenSpaceReflections.TraceMode) + { + case ReflectionsTraceMode::SoftwareTracing: + { + GlobalSignDistanceFieldPass::BindingData bindingDataSDF; + GlobalSurfaceAtlasPass::BindingData bindingDataSurfaceAtlas; + if (!GlobalSignDistanceFieldPass::Instance()->Get(params.RenderContext.Buffers, bindingDataSDF) && + !GlobalSurfaceAtlasPass::Instance()->Get(params.RenderContext.Buffers, bindingDataSurfaceAtlas)) + { + useSDFReflections = true; + + // Bind SDF and Surface Atlas data + data.GlobalSDF = bindingDataSDF.Constants; + data.GlobalSurfaceAtlas = bindingDataSurfaceAtlas.Constants; + params.GPUContext->BindSR(srv + 0, bindingDataSDF.Texture ? bindingDataSDF.Texture->ViewVolume() : nullptr); + params.GPUContext->BindSR(srv + 1, bindingDataSDF.TextureMip ? bindingDataSDF.TextureMip->ViewVolume() : nullptr); + params.GPUContext->BindSR(srv + 2, bindingDataSurfaceAtlas.Chunks ? bindingDataSurfaceAtlas.Chunks->View() : nullptr); + params.GPUContext->BindSR(srv + 3, bindingDataSurfaceAtlas.CulledObjects ? bindingDataSurfaceAtlas.CulledObjects->View() : nullptr); + params.GPUContext->BindSR(srv + 4, bindingDataSurfaceAtlas.Objects ? bindingDataSurfaceAtlas.Objects->View() : nullptr); + params.GPUContext->BindSR(srv + 5, bindingDataSurfaceAtlas.AtlasDepth->View()); + params.GPUContext->BindSR(srv + 6, bindingDataSurfaceAtlas.AtlasLighting->View()); + } + break; + } + } + } + if (!useSDFReflections) + { + // Unbind SRVs to prevent issues + data.GlobalSDF.CascadesCount = 0; + params.GPUContext->UnBindSR(srv + 0); + params.GPUContext->UnBindSR(srv + 1); + params.GPUContext->UnBindSR(srv + 2); + params.GPUContext->UnBindSR(srv + 3); + params.GPUContext->UnBindSR(srv + 4); + params.GPUContext->UnBindSR(srv + 5); + params.GPUContext->UnBindSR(srv + 6); + } + + cb = Span(cb.Get() + sizeof(Data), cb.Length() - sizeof(Data)); + srv += SRVs; + return useSDFReflections; +} + #if USE_EDITOR void ForwardShadingFeature::Generate(GeneratorData& data) @@ -222,6 +275,11 @@ void GlobalIlluminationFeature::Generate(GeneratorData& data) data.Template = TEXT("Features/GlobalIllumination.hlsl"); } +void SDFReflectionsFeature::Generate(GeneratorData& data) +{ + data.Template = TEXT("Features/SDFReflections.hlsl"); +} + void DistortionFeature::Generate(GeneratorData& data) { data.Template = TEXT("Features/Distortion.hlsl"); diff --git a/Source/Engine/Graphics/Materials/MaterialShaderFeatures.h b/Source/Engine/Graphics/Materials/MaterialShaderFeatures.h index 0df2e9a2e..5b85353a0 100644 --- a/Source/Engine/Graphics/Materials/MaterialShaderFeatures.h +++ b/Source/Engine/Graphics/Materials/MaterialShaderFeatures.h @@ -6,6 +6,8 @@ #include "Engine/Core/Math/Rectangle.h" #include "Engine/Core/Types/Span.h" #include "Engine/Renderer/GI/DynamicDiffuseGlobalIllumination.h" +#include "Engine/Renderer/GlobalSignDistanceFieldPass.h" +#include "Engine/Renderer/GI/GlobalSurfaceAtlasPass.h" // Material shader features are plugin-based functionalities that are reusable between different material domains. struct MaterialShaderFeature @@ -91,6 +93,25 @@ struct GlobalIlluminationFeature : MaterialShaderFeature #endif }; +// Material shader feature that adds SDF Reflections feature (software reflections). +struct SDFReflectionsFeature : MaterialShaderFeature +{ + enum { SRVs = 7 }; + + PACK_STRUCT(struct Data + { + GlobalSignDistanceFieldPass::ConstantsData GlobalSDF; + GlobalSurfaceAtlasPass::ConstantsData GlobalSurfaceAtlas; + }); + + + + static bool Bind(MaterialShader::BindParameters& params, Span& cb, int32& srv); +#if USE_EDITOR + static void Generate(GeneratorData& data); +#endif +}; + // Material shader feature that adds distortion vectors rendering pass. struct DistortionFeature : MaterialShaderFeature { diff --git a/Source/Engine/Renderer/GI/GlobalSurfaceAtlasPass.cpp b/Source/Engine/Renderer/GI/GlobalSurfaceAtlasPass.cpp index fb0faa27a..f2bc3169d 100644 --- a/Source/Engine/Renderer/GI/GlobalSurfaceAtlasPass.cpp +++ b/Source/Engine/Renderer/GI/GlobalSurfaceAtlasPass.cpp @@ -360,6 +360,17 @@ void GlobalSurfaceAtlasPass::Dispose() _shader = nullptr; } +bool GlobalSurfaceAtlasPass::Get(const RenderBuffers* buffers, BindingData& result) +{ + auto* surfaceAtlasData = buffers ? buffers->FindCustomBuffer(TEXT("GlobalSurfaceAtlas")) : nullptr; + if (surfaceAtlasData && surfaceAtlasData->LastFrameUsed + 1 >= Engine::FrameCount) // Allow to use Surface Atlas from the previous frame (not used currently) + { + result = surfaceAtlasData->Result; + return false; + } + return true; +} + bool GlobalSurfaceAtlasPass::Render(RenderContext& renderContext, GPUContext* context, BindingData& result) { // Skip if not supported diff --git a/Source/Engine/Renderer/GI/GlobalSurfaceAtlasPass.h b/Source/Engine/Renderer/GI/GlobalSurfaceAtlasPass.h index 443b3a401..45d144767 100644 --- a/Source/Engine/Renderer/GI/GlobalSurfaceAtlasPass.h +++ b/Source/Engine/Renderer/GI/GlobalSurfaceAtlasPass.h @@ -65,6 +65,14 @@ private: void* _currentActorObject; public: + /// + /// Gets the Global Surface Atlas (only if enabled in Graphics Settings). + /// + /// The rendering context buffers. + /// The result Global Surface Atlas data for binding to the shaders. + /// True if there is no valid Global Surface Atlas rendered during this frame, otherwise false. + bool Get(const RenderBuffers* buffers, BindingData& result); + /// /// Renders the Global Surface Atlas. /// diff --git a/Source/Engine/Tools/MaterialGenerator/MaterialGenerator.cpp b/Source/Engine/Tools/MaterialGenerator/MaterialGenerator.cpp index 8cacddf51..f9ca26705 100644 --- a/Source/Engine/Tools/MaterialGenerator/MaterialGenerator.cpp +++ b/Source/Engine/Tools/MaterialGenerator/MaterialGenerator.cpp @@ -188,40 +188,46 @@ bool MaterialGenerator::Generate(WriteStream& source, MaterialInfo& materialInfo { case MaterialDomain::Surface: if (materialInfo.TessellationMode != TessellationMethod::None) - ADD_FEATURE(TessellationFeature); + ADD_FEATURE(TessellationFeature); if (materialInfo.BlendMode == MaterialBlendMode::Opaque) - ADD_FEATURE(MotionVectorsFeature); + ADD_FEATURE(MotionVectorsFeature); if (materialInfo.BlendMode == MaterialBlendMode::Opaque) - ADD_FEATURE(LightmapFeature); + ADD_FEATURE(LightmapFeature); if (materialInfo.BlendMode == MaterialBlendMode::Opaque) - ADD_FEATURE(DeferredShadingFeature); + ADD_FEATURE(DeferredShadingFeature); if (materialInfo.BlendMode != MaterialBlendMode::Opaque && (materialInfo.FeaturesFlags & MaterialFeaturesFlags::DisableDistortion) == MaterialFeaturesFlags::None) - ADD_FEATURE(DistortionFeature); + ADD_FEATURE(DistortionFeature); if (materialInfo.BlendMode != MaterialBlendMode::Opaque && EnumHasAnyFlags(materialInfo.FeaturesFlags, MaterialFeaturesFlags::GlobalIllumination)) - ADD_FEATURE(GlobalIlluminationFeature); + { + ADD_FEATURE(GlobalIlluminationFeature); + + // SDF Reflections is only valid when both GI and SSR is enabled + if (materialInfo.BlendMode != MaterialBlendMode::Opaque && EnumHasAnyFlags(materialInfo.FeaturesFlags, MaterialFeaturesFlags::ScreenSpaceReflections)) + ADD_FEATURE(SDFReflectionsFeature); + } if (materialInfo.BlendMode != MaterialBlendMode::Opaque) - ADD_FEATURE(ForwardShadingFeature); + ADD_FEATURE(ForwardShadingFeature); break; case MaterialDomain::Terrain: if (materialInfo.TessellationMode != TessellationMethod::None) - ADD_FEATURE(TessellationFeature); + ADD_FEATURE(TessellationFeature); ADD_FEATURE(LightmapFeature); ADD_FEATURE(DeferredShadingFeature); break; case MaterialDomain::Particle: if (materialInfo.BlendMode != MaterialBlendMode::Opaque && (materialInfo.FeaturesFlags & MaterialFeaturesFlags::DisableDistortion) == MaterialFeaturesFlags::None) - ADD_FEATURE(DistortionFeature); + ADD_FEATURE(DistortionFeature); if (materialInfo.BlendMode != MaterialBlendMode::Opaque && EnumHasAnyFlags(materialInfo.FeaturesFlags, MaterialFeaturesFlags::GlobalIllumination)) - ADD_FEATURE(GlobalIlluminationFeature); + ADD_FEATURE(GlobalIlluminationFeature); ADD_FEATURE(ForwardShadingFeature); break; case MaterialDomain::Deformable: if (materialInfo.TessellationMode != TessellationMethod::None) - ADD_FEATURE(TessellationFeature); + ADD_FEATURE(TessellationFeature); if (materialInfo.BlendMode == MaterialBlendMode::Opaque) - ADD_FEATURE(DeferredShadingFeature); + ADD_FEATURE(DeferredShadingFeature); if (materialInfo.BlendMode != MaterialBlendMode::Opaque) - ADD_FEATURE(ForwardShadingFeature); + ADD_FEATURE(ForwardShadingFeature); break; default: break; @@ -708,7 +714,7 @@ void MaterialGenerator::ProcessGroupMath(Box* box, Node* node, Value& value) { switch (node->TypeID) { - // Vector Transform + // Vector Transform case 30: { // Get input vector diff --git a/Source/Shaders/ReflectionsCommon.hlsl b/Source/Shaders/ReflectionsCommon.hlsl index f571abc47..0af6e39c1 100644 --- a/Source/Shaders/ReflectionsCommon.hlsl +++ b/Source/Shaders/ReflectionsCommon.hlsl @@ -5,6 +5,9 @@ #include "./Flax/GBufferCommon.hlsl" +// Hit depth (view space) threshold to detect if sky was hit (value above it where 1.0f is default) +#define REFLECTIONS_HIT_THRESHOLD 0.9f + float GetSpecularOcclusion(float NoV, float roughnessSq, float ao) { return saturate(pow(NoV + ao, roughnessSq) - 1 + ao); diff --git a/Source/Shaders/SSR.shader b/Source/Shaders/SSR.shader index a813bdf36..a9cdbe57d 100644 --- a/Source/Shaders/SSR.shader +++ b/Source/Shaders/SSR.shader @@ -129,17 +129,17 @@ float4 PS_RayTracePass(Quad_VS2PS input) : SV_Target0 float mip = clamp(log2(intersectionCircleRadius * TraceSizeMax), 0.0, MaxColorMiplevel); float3 sampleColor = Texture0.SampleLevel(SamplerLinearClamp, screenHit.xy, mip).rgb; result = float4(sampleColor, screenHit.z); - if (screenHit.z >= 0.9f) + if (screenHit.z >= REFLECTIONS_HIT_THRESHOLD) return result; } - // Calculate reflection direction (the same TraceScreenSpaceReflection) - float3 reflectWS = ScreenSpaceReflectionDirection(input.TexCoord, gBuffer, gBufferData.ViewPos, TemporalEffect, TemporalTime, BRDFBias); - // Fallback to Global SDF and Global Surface Atlas tracing #if USE_GLOBAL_SURFACE_ATLAS && CAN_USE_GLOBAL_SURFACE_ATLAS + // Calculate reflection direction (the same TraceScreenSpaceReflection) + float3 reflectWS = ScreenSpaceReflectionDirection(input.TexCoord, gBuffer, gBufferData.ViewPos, TemporalEffect, TemporalTime, BRDFBias); + GlobalSDFTrace sdfTrace; - float maxDistance = 100000; + float maxDistance = GLOBAL_SDF_WORLD_SIZE; float selfOcclusionBias = GlobalSDF.CascadeVoxelSize[0]; sdfTrace.Init(gBuffer.WorldPos + gBuffer.Normal * selfOcclusionBias, reflectWS, 0.0f, maxDistance); GlobalSDFHit sdfHit = RayTraceGlobalSDF(GlobalSDF, GlobalSDFTex, GlobalSDFMip, sdfTrace);