Optimize ProbesFilter shader
This commit is contained in:
BIN
Content/Shaders/ProbesFilter.flax
(Stored with Git LFS)
BIN
Content/Shaders/ProbesFilter.flax
(Stored with Git LFS)
Binary file not shown.
@@ -74,12 +74,6 @@ PACK_STRUCT(struct Data
|
||||
Float2 Dummy0;
|
||||
int32 CubeFace;
|
||||
int32 SourceMipIndex;
|
||||
Float4 Sample01;
|
||||
Float4 Sample23;
|
||||
Float4 CoefficientMask0;
|
||||
Float4 CoefficientMask1;
|
||||
Float3 Dummy1;
|
||||
float CoefficientMask2;
|
||||
});
|
||||
|
||||
namespace ProbesRendererImpl
|
||||
@@ -92,11 +86,6 @@ namespace ProbesRendererImpl
|
||||
bool _isReady = false;
|
||||
AssetReference<Shader> _shader;
|
||||
GPUPipelineState* _psFilterFace = nullptr;
|
||||
GPUPipelineState* _psCopyFace = nullptr;
|
||||
GPUPipelineState* _psCalcDiffuseIrradiance = nullptr;
|
||||
GPUPipelineState* _psAccDiffuseIrradiance = nullptr;
|
||||
GPUPipelineState* _psAccumulateCubeFaces = nullptr;
|
||||
GPUPipelineState* _psCopyFrameLHB = nullptr;
|
||||
SceneRenderTask* _task = nullptr;
|
||||
GPUTexture* _output = nullptr;
|
||||
GPUTexture* _probe = nullptr;
|
||||
@@ -246,42 +235,12 @@ bool ProbesRenderer::Init()
|
||||
|
||||
// Create pipeline stages
|
||||
_psFilterFace = GPUDevice::Instance->CreatePipelineState();
|
||||
_psCopyFace = GPUDevice::Instance->CreatePipelineState();
|
||||
_psCalcDiffuseIrradiance = GPUDevice::Instance->CreatePipelineState();
|
||||
_psAccDiffuseIrradiance = GPUDevice::Instance->CreatePipelineState();
|
||||
_psAccumulateCubeFaces = GPUDevice::Instance->CreatePipelineState();
|
||||
_psCopyFrameLHB = GPUDevice::Instance->CreatePipelineState();
|
||||
GPUPipelineState::Description psDesc = GPUPipelineState::Description::DefaultFullscreenTriangle;
|
||||
{
|
||||
psDesc.PS = shader->GetPS("PS_FilterFace");
|
||||
if (_psFilterFace->Init(psDesc))
|
||||
return true;
|
||||
}
|
||||
{
|
||||
psDesc.PS = shader->GetPS("PS_CopyFace");
|
||||
if (_psCopyFace->Init(psDesc))
|
||||
return true;
|
||||
}
|
||||
{
|
||||
psDesc.PS = shader->GetPS("PS_CalcDiffuseIrradiance");
|
||||
if (_psCalcDiffuseIrradiance->Init(psDesc))
|
||||
return true;
|
||||
}
|
||||
{
|
||||
psDesc.PS = shader->GetPS("PS_AccDiffuseIrradiance");
|
||||
if (_psAccDiffuseIrradiance->Init(psDesc))
|
||||
return true;
|
||||
}
|
||||
{
|
||||
psDesc.PS = shader->GetPS("PS_AccumulateCubeFaces");
|
||||
if (_psAccumulateCubeFaces->Init(psDesc))
|
||||
return true;
|
||||
}
|
||||
{
|
||||
psDesc.PS = shader->GetPS("PS_CopyFrameLHB");
|
||||
if (_psCopyFrameLHB->Init(psDesc))
|
||||
return true;
|
||||
}
|
||||
|
||||
// Init rendering pipeline
|
||||
_output = GPUDevice::Instance->CreateTexture(TEXT("Output"));
|
||||
@@ -338,11 +297,6 @@ void ProbesRenderer::Release()
|
||||
|
||||
// Release data
|
||||
SAFE_DELETE_GPU_RESOURCE(_psFilterFace);
|
||||
SAFE_DELETE_GPU_RESOURCE(_psCopyFace);
|
||||
SAFE_DELETE_GPU_RESOURCE(_psCalcDiffuseIrradiance);
|
||||
SAFE_DELETE_GPU_RESOURCE(_psAccDiffuseIrradiance);
|
||||
SAFE_DELETE_GPU_RESOURCE(_psAccumulateCubeFaces);
|
||||
SAFE_DELETE_GPU_RESOURCE(_psCopyFrameLHB);
|
||||
_shader = nullptr;
|
||||
SAFE_DELETE_GPU_RESOURCE(_output);
|
||||
SAFE_DELETE(_task);
|
||||
@@ -573,9 +527,7 @@ void ProbesRenderer::onRender(RenderTask* task, GPUContext* context)
|
||||
|
||||
// Copy face back to the cubemap
|
||||
context->SetRenderTarget(_probe->View(faceIndex, mipIndex));
|
||||
context->BindSR(1, _tmpFace->View(0, mipIndex));
|
||||
context->SetState(_psCopyFace);
|
||||
context->DrawFullscreenTriangle();
|
||||
context->Draw(_tmpFace->View(0, mipIndex));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -9,21 +9,9 @@ META_CB_BEGIN(0, Data)
|
||||
float2 Dummy0;
|
||||
int CubeFace;
|
||||
int SourceMipIndex;
|
||||
float4 Sample01;
|
||||
float4 Sample23;
|
||||
float4 CoefficientMask0;
|
||||
float4 CoefficientMask1;
|
||||
float3 Dummy1;
|
||||
float CoefficientMask2;
|
||||
META_CB_END
|
||||
|
||||
TextureCube Cube : register(t0);
|
||||
Texture2D Image : register(t1);
|
||||
|
||||
float4 SampleCubemap(float3 uv)
|
||||
{
|
||||
return Cube.SampleLevel(SamplerLinearClamp, uv, SourceMipIndex);
|
||||
}
|
||||
|
||||
float3 UvToCubeMapUv(float2 uv)
|
||||
{
|
||||
@@ -81,82 +69,10 @@ float4 PS_FilterFace(Quad_VS2PS input) : SV_Target
|
||||
BRANCH
|
||||
if (NoL > 0)
|
||||
{
|
||||
filteredColor += SampleCubemap(L) * NoL;
|
||||
filteredColor += Cube.SampleLevel(SamplerLinearClamp, L, SourceMipIndex) * NoL;
|
||||
weight += NoL;
|
||||
}
|
||||
}
|
||||
|
||||
return filteredColor / max(weight, 0.001);
|
||||
}
|
||||
|
||||
// Pixel Shader for coping probe face
|
||||
META_PS(true, FEATURE_LEVEL_ES2)
|
||||
float4 PS_CopyFace(Quad_VS2PS input) : SV_Target
|
||||
{
|
||||
return Image.SampleLevel(SamplerLinearClamp, input.TexCoord, 0);
|
||||
}
|
||||
|
||||
// Pixel Shader for calculating Diffuse Irradiance
|
||||
META_PS(true, FEATURE_LEVEL_ES2)
|
||||
float4 PS_CalcDiffuseIrradiance(Quad_VS2PS input) : SV_Target
|
||||
{
|
||||
float2 uv = input.TexCoord * 2 - 1;
|
||||
float3 cubeCoordinates = normalize(UvToCubeMapUv(uv));
|
||||
float squaredUVs = 1 + dot(uv, uv);
|
||||
float weight = 4 / (sqrt(squaredUVs) * squaredUVs);
|
||||
|
||||
ThreeBandSHVector shCoefficients = SHBasisFunction3(cubeCoordinates);
|
||||
float currentSHCoefficient = dot(shCoefficients.V0, CoefficientMask0) + dot(shCoefficients.V1, CoefficientMask1) + shCoefficients.V2 * CoefficientMask2;
|
||||
|
||||
float3 radiance = SampleCubemap(cubeCoordinates).rgb;
|
||||
return float4(radiance * currentSHCoefficient * weight, weight);
|
||||
}
|
||||
|
||||
// Pixel Shader for accumulating Diffuse Irradiance
|
||||
META_PS(true, FEATURE_LEVEL_ES2)
|
||||
float4 PS_AccDiffuseIrradiance(Quad_VS2PS input) : SV_Target
|
||||
{
|
||||
float4 result = 0;
|
||||
{
|
||||
float2 uv = saturate(input.TexCoord + Sample01.xy) * 2 - 1;
|
||||
float3 cubeCoordinates = UvToCubeMapUv(uv);
|
||||
result += SampleCubemap(cubeCoordinates);
|
||||
}
|
||||
{
|
||||
float2 uv = saturate(input.TexCoord + Sample01.zw) * 2 - 1;
|
||||
float3 cubeCoordinates = UvToCubeMapUv(uv);
|
||||
result += SampleCubemap(cubeCoordinates);
|
||||
}
|
||||
{
|
||||
float2 uv = saturate(input.TexCoord + Sample23.xy) * 2 - 1;
|
||||
float3 cubeCoordinates = UvToCubeMapUv(uv);
|
||||
result += SampleCubemap(cubeCoordinates);
|
||||
}
|
||||
{
|
||||
float2 uv = saturate(input.TexCoord + Sample23.zw) * 2 - 1;
|
||||
float3 cubeCoordinates = UvToCubeMapUv(uv);
|
||||
result += SampleCubemap(cubeCoordinates);
|
||||
}
|
||||
return result / 4.0f;
|
||||
}
|
||||
|
||||
// Pixel Shader for accumulating cube faces into one pixel
|
||||
META_PS(true, FEATURE_LEVEL_ES2)
|
||||
float4 PS_AccumulateCubeFaces(Quad_VS2PS input) : SV_Target
|
||||
{
|
||||
float4 result = SampleCubemap(float3(1, 0, 0));
|
||||
result += SampleCubemap(float3(-1, 0, 0));
|
||||
result += SampleCubemap(float3(0, 1, 0));
|
||||
result += SampleCubemap(float3(0, -1, 0));
|
||||
result += SampleCubemap(float3(0, 0, 1));
|
||||
result += SampleCubemap(float3(0, 0, -1));
|
||||
return float4((4 * PI) * result.rgb / max(result.a, 0.00001f), 0);
|
||||
}
|
||||
|
||||
// Pixel Shader for copying frame to cube face with setting lower hemisphere to black
|
||||
META_PS(true, FEATURE_LEVEL_ES2)
|
||||
float4 PS_CopyFrameLHB(Quad_VS2PS input) : SV_Target
|
||||
{
|
||||
float mask = input.TexCoord.y < 0.5;// TODO: make is smooth (and branchless using function)
|
||||
return float4(Image.SampleLevel(SamplerLinearClamp, input.TexCoord, 0).xyz * mask * CoefficientMask2, 1);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user