From 089cedb3d3328e1b8be334e4281eb30944dbcb66 Mon Sep 17 00:00:00 2001 From: turanszkij Date: Sun, 5 Mar 2017 16:23:58 +0100 Subject: [PATCH] updated voxel radiance --- .gitignore | 1 + WickedEngine/WickedEngine_SHADERS.vcxproj | 10 --- .../WickedEngine_SHADERS.vcxproj.filters | 3 - WickedEngine/lightingHF.hlsli | 4 +- WickedEngine/objectPS_voxelizer.hlsl | 5 +- WickedEngine/voxelRadianceCS.hlsl | 70 ------------------- WickedEngine/wiEnums.h | 1 - WickedEngine/wiRenderer.cpp | 48 +++++++------ 8 files changed, 33 insertions(+), 109 deletions(-) delete mode 100644 WickedEngine/voxelRadianceCS.hlsl diff --git a/.gitignore b/.gitignore index f4c47b7cc..4e08442ee 100644 --- a/.gitignore +++ b/.gitignore @@ -211,3 +211,4 @@ ModelManifest.xml /WickedEngine/models/Sponza/temp /WickedEngine/models/Sponza/nsight_profiler.cfg /WickedEngine/models/Sponza/dirlight.wimf +/WickedEngine/models/Sponza/radiance.wimf diff --git a/WickedEngine/WickedEngine_SHADERS.vcxproj b/WickedEngine/WickedEngine_SHADERS.vcxproj index 7ffb4e081..5f804986c 100644 --- a/WickedEngine/WickedEngine_SHADERS.vcxproj +++ b/WickedEngine/WickedEngine_SHADERS.vcxproj @@ -878,16 +878,6 @@ Pixel Pixel - - Compute - 5.0 - Compute - 4.0 - Compute - 5.0 - Compute - 4.0 - Compute 5.0 diff --git a/WickedEngine/WickedEngine_SHADERS.vcxproj.filters b/WickedEngine/WickedEngine_SHADERS.vcxproj.filters index b9098fc3b..690b93299 100644 --- a/WickedEngine/WickedEngine_SHADERS.vcxproj.filters +++ b/WickedEngine/WickedEngine_SHADERS.vcxproj.filters @@ -513,9 +513,6 @@ PS - - CS - VS diff --git a/WickedEngine/lightingHF.hlsli b/WickedEngine/lightingHF.hlsli index 2cceb776c..4a7b91eb3 100644 --- a/WickedEngine/lightingHF.hlsli +++ b/WickedEngine/lightingHF.hlsli @@ -731,7 +731,7 @@ inline void VoxelRadiance(in float3 N, in float3 V, in float3 P, in float3 f0, i float4 radiance = 0; for (uint cone = 0; cone < numCones; ++cone) { - float3 coneVec = normalize(N * 2 + CONES[cone]) / g_xWorld_VoxelRadianceDataRes * float3(1, -1, 1); + float3 coneVec = normalize(N + CONES[cone]) / g_xWorld_VoxelRadianceDataRes * float3(1, -1, 1); float4 _radiance = 0; float step = 0; @@ -739,7 +739,7 @@ inline void VoxelRadiance(in float3 N, in float3 V, in float3 P, in float3 f0, i for (uint i = 0; i < 8; ++i) { step++; - float mip = 0.8f * i; + float mip = 0.897f * i; tc += coneVec; diff --git a/WickedEngine/objectPS_voxelizer.hlsl b/WickedEngine/objectPS_voxelizer.hlsl index fa89bc083..fdcf9b526 100644 --- a/WickedEngine/objectPS_voxelizer.hlsl +++ b/WickedEngine/objectPS_voxelizer.hlsl @@ -20,8 +20,9 @@ void main(float4 pos : SV_POSITION, float3 N : NORMAL, float2 tex : TEXCOORD, fl float3 diffuse = 0; - uint lightCount = (uint)g_xColor.x; - for (uint i = 0; i < lightCount; ++i) + uint lightIndexStart = (uint)g_xColor.x; + uint lightCount = (uint)g_xColor.y; + for (uint i = lightIndexStart; i < lightCount; ++i) { LightArrayType light = LightArray[i]; diff --git a/WickedEngine/voxelRadianceCS.hlsl b/WickedEngine/voxelRadianceCS.hlsl deleted file mode 100644 index 1f7b8dfae..000000000 --- a/WickedEngine/voxelRadianceCS.hlsl +++ /dev/null @@ -1,70 +0,0 @@ -#include "globals.hlsli" -#include "voxelHF.hlsli" - -TEXTURE3D(input_emission, float4, 0); -//TEXTURE3D(input_normal, float4, 1); -RWTEXTURE3D(output, float4, 0); - -groupshared float4 accumulation[4 * 4 * 4]; - -static const float3 SAMPLES[16] = { - float3(0.355512, -0.709318, -0.102371), - float3(0.534186, 0.71511, -0.115167), - float3(-0.87866, 0.157139, -0.115167), - float3(0.140679, -0.475516, -0.0639818), - float3(-0.0796121, 0.158842, -0.677075), - float3(-0.0759516, -0.101676, -0.483625), - float3(0.12493, -0.0223423, -0.483625), - float3(-0.0720074, 0.243395, -0.967251), - float3(-0.207641, 0.414286, 0.187755), - float3(-0.277332, -0.371262, 0.187755), - float3(0.63864, -0.114214, 0.262857), - float3(-0.184051, 0.622119, 0.262857), - float3(0.110007, -0.219486, 0.435574), - float3(0.235085, 0.314707, 0.696918), - float3(-0.290012, 0.0518654, 0.522688), - float3(0.0975089, -0.329594, 0.609803) -}; - -[numthreads(4, 4, 4)] -void main( uint3 DTid : SV_DispatchThreadID, uint GroupIndex : SV_GroupIndex ) -{ - float4 color = input_emission[DTid]; - - if (color.a > 0) - { - //float3 normal = input_normal[DTid]; - - output[DTid] = float4(color.rgb, 1); - } - else - { - output[DTid] = 0; - } - - - //float4 emittance = input_emittance[DTid]; - //float3 normal = input_normal[DTid]; - - //output[DTid] = emittance; - ////output[DTid] = float4(normal.rgb * 0.5f + 0.5f, emittance.a); - - ////float3 dim; - ////input.GetDimensions(dim.x, dim.y, dim.z); - ////float3 uvw = DTid / dim; - - ////float occ = 0; - ////uint count = 0; - ////for (uint i = 0; i < 16; ++i) - ////{ - //// for (uint j = 1; j < 6; ++j) - //// { - //// float3 tex = uvw + j * SAMPLES[i] / dim; - //// occ += input.SampleLevel(sampler_linear_clamp, tex, 0).a; - - //// count++; - //// } - ////} - - ////output[DTid] = occ / count; -} \ No newline at end of file diff --git a/WickedEngine/wiEnums.h b/WickedEngine/wiEnums.h index 57b351dc7..f513ef460 100644 --- a/WickedEngine/wiEnums.h +++ b/WickedEngine/wiEnums.h @@ -226,7 +226,6 @@ enum CSTYPES CSTYPE_TILEDLIGHTCULLING_DEBUG, CSTYPE_RESOLVEMSAADEPTHSTENCIL, CSTYPE_VOXELSCENECOPYCLEAR, - CSTYPE_VOXELRADIANCE, CSTYPE_LAST }; diff --git a/WickedEngine/wiRenderer.cpp b/WickedEngine/wiRenderer.cpp index 5dcd2e28a..d80ccca53 100644 --- a/WickedEngine/wiRenderer.cpp +++ b/WickedEngine/wiRenderer.cpp @@ -781,7 +781,6 @@ void wiRenderer::LoadShaders() computeShaders[CSTYPE_TILEDLIGHTCULLING_DEBUG] = static_cast(wiResourceManager::GetShaderManager()->add(SHADERPATH + "lightCullingCS_DEBUG.cso", wiResourceManager::COMPUTESHADER)); computeShaders[CSTYPE_RESOLVEMSAADEPTHSTENCIL] = static_cast(wiResourceManager::GetShaderManager()->add(SHADERPATH + "resolveMSAADepthStencilCS.cso", wiResourceManager::COMPUTESHADER)); computeShaders[CSTYPE_VOXELSCENECOPYCLEAR] = static_cast(wiResourceManager::GetShaderManager()->add(SHADERPATH + "voxelSceneCopyClearCS.cso", wiResourceManager::COMPUTESHADER)); - computeShaders[CSTYPE_VOXELRADIANCE] = static_cast(wiResourceManager::GetShaderManager()->add(SHADERPATH + "voxelRadianceCS.cso", wiResourceManager::COMPUTESHADER)); hullShaders[HSTYPE_OBJECT] = static_cast(wiResourceManager::GetShaderManager()->add(SHADERPATH + "objectHS.cso", wiResourceManager::HULLSHADER)); @@ -1515,6 +1514,21 @@ void wiRenderer::UpdatePerFrameData() { Frustum frustum; frustum.ConstructFrustum(min(camera->zFarP, GetScene().worldInfo.fogSEH.y), camera->Projection, camera->View); + + for (Model* model : GetScene().models) + { + if (model->decals.empty()) + continue; + + for (Decal* decal : model->decals) + { + if ((decal->texture || decal->normal) && frustum.CheckBox(decal->bounds)) + { + x.second.culledDecals.push_back(decal); + } + } + } + spTree_lights->getVisible(frustum, culling.culledLights, wiSPTree::SortType::SP_TREE_SORT_NONE); if (GetVoxelRadianceEnabled()) @@ -1528,7 +1542,7 @@ void wiRenderer::UpdatePerFrameData() // We wouldn't have to sort, but we need unique lights and that only works with sorted forward_list! spTree_lights->Sort(camera->translation, culling.culledLights, wiSPTree::SortType::SP_TREE_SORT_BACK_TO_FRONT); - int i = 0; + int i = (int)x.second.culledDecals.size(); // Index the light array after the decals int shadowCounter_2D = 0; int shadowCounter_Cube = 0; for (auto& c : culling.culledLights) @@ -1588,20 +1602,6 @@ void wiRenderer::UpdatePerFrameData() std::sort(culling.culledEmittedParticleSystems.begin(), culling.culledEmittedParticleSystems.end(), [&](const wiEmittedParticle* a, const wiEmittedParticle* b) { return wiMath::DistanceSquared(camera->translation, a->bounding_box->getCenter()) > wiMath::DistanceSquared(camera->translation,b->bounding_box->getCenter()); }); - - for (Model* model : GetScene().models) - { - if (model->decals.empty()) - continue; - - for (Decal* decal : model->decals) - { - if ((decal->texture || decal->normal) && getCamera()->frustum.CheckBox(decal->bounds)) - { - x.second.culledDecals.push_back(decal); - } - } - } } } wiProfiler::GetInstance().EndRange(); // SPTree Culling @@ -4012,8 +4012,12 @@ void wiRenderer::VoxelRadiance(GRAPHICSTHREAD threadID) culledRenderer[((Object*)object)->mesh].push_front((Object*)object); } + const FrameCulling& culling = frameCullings[getCamera()]; + + // Tell the voxelizer about the lights in the light array (exclude decals) MiscCB cb; - cb.mColor.x = (float)frameCullings[getCamera()].culledLight_count; + cb.mColor.x = (float)culling.culledDecals.size(); + cb.mColor.y = cb.mColor.x + (float)culling.culledLight_count; GetDevice()->UpdateBuffer(constantBuffers[CBTYPE_MISC], &cb, threadID); ViewPort VP; @@ -4033,21 +4037,23 @@ void wiRenderer::VoxelRadiance(GRAPHICSTHREAD threadID) RenderMeshes(center, culledRenderer, SHADERTYPE_VOXELIZE, RENDERTYPE_OPAQUE, threadID); - + // Copy the packed voxel scene data to a 3D texture, then delete the voxel scene data. The cone tracing will operate on the 3D texture GetDevice()->BindRenderTargets(0, nullptr, nullptr, threadID); GetDevice()->BindUnorderedAccessResourceCS(resourceBuffers[RBTYPE_VOXELSCENE], 0, threadID); GetDevice()->BindUnorderedAccessResourceCS(textures[TEXTYPE_3D_VOXELRADIANCE], 1, threadID); GetDevice()->BindCS(computeShaders[CSTYPE_VOXELSCENECOPYCLEAR], threadID); GetDevice()->Dispatch((UINT)(voxelSceneData.res * voxelSceneData.res * voxelSceneData.res / 1024), 1, 1, threadID); - //GetDevice()->BindCS(computeShaders[CSTYPE_VOXELRADIANCE], threadID); - //GetDevice()->Dispatch((UINT)(voxelSceneData.res / 4), (UINT)(voxelSceneData.res / 4), (UINT)(voxelSceneData.res / 4), threadID); GetDevice()->BindCS(nullptr, threadID); GetDevice()->UnBindUnorderedAccessResources(0, 2, threadID); + // Pre-integrate the voxel texture by creating blurred mip levels: GetDevice()->GenerateMips(textures[TEXTYPE_3D_VOXELRADIANCE], threadID); } - GetDevice()->BindResourceVS(textures[TEXTYPE_3D_VOXELRADIANCE], TEXSLOT_VOXELRADIANCE, threadID); + if (voxelHelper) + { + GetDevice()->BindResourceVS(textures[TEXTYPE_3D_VOXELRADIANCE], TEXSLOT_VOXELRADIANCE, threadID); + } GetDevice()->BindResourcePS(textures[TEXTYPE_3D_VOXELRADIANCE], TEXSLOT_VOXELRADIANCE, threadID); wiProfiler::GetInstance().EndRange(threadID);