Skip to content

Commit

Permalink
PBR_Render: Improved computing of IBL textures
Browse files Browse the repository at this point in the history
  • Loading branch information
MikhailGorobets committed May 7, 2024
1 parent 525ee5e commit f0a1094
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 5 deletions.
4 changes: 2 additions & 2 deletions PBR/interface/PBR_Renderer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -680,9 +680,9 @@ class PBR_Renderer
RefCntAutoPtr<ITextureView> m_pDefaultNormalMapSRV;
RefCntAutoPtr<ITextureView> m_pDefaultPhysDescSRV;

static constexpr TEXTURE_FORMAT IrradianceCubeFmt = TEX_FORMAT_RGBA32_FLOAT;
static constexpr TEXTURE_FORMAT IrradianceCubeFmt = TEX_FORMAT_RGBA16_FLOAT;
static constexpr TEXTURE_FORMAT PrefilteredEnvMapFmt = TEX_FORMAT_RGBA16_FLOAT;
static constexpr Uint32 IrradianceCubeDim = 64;
static constexpr Uint32 IrradianceCubeDim = 16;
static constexpr Uint32 PrefilteredEnvMapDim = 256;

RefCntAutoPtr<ITextureView> m_pIrradianceCubeSRV;
Expand Down
6 changes: 5 additions & 1 deletion PBR/src/PBR_Renderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -387,14 +387,17 @@ PBR_Renderer::~PBR_Renderer()
void PBR_Renderer::PrecomputeBRDF(IDeviceContext* pCtx,
Uint32 NumBRDFSamples)
{

bool IsSupportedUnormFormat = m_Device.GetTextureFormatInfo(TEX_FORMAT_RG16_UNORM).Supported;

TextureDesc TexDesc;
TexDesc.Name = "Preintegrated GGX";
TexDesc.Type = RESOURCE_DIM_TEX_2D;
TexDesc.Usage = USAGE_DEFAULT;
TexDesc.BindFlags = BIND_SHADER_RESOURCE | BIND_RENDER_TARGET;
TexDesc.Width = BRDF_LUT_Dim;
TexDesc.Height = BRDF_LUT_Dim;
TexDesc.Format = TEX_FORMAT_RG16_FLOAT;
TexDesc.Format = IsSupportedUnormFormat ? TEX_FORMAT_RG16_UNORM : TEX_FORMAT_RG16_FLOAT;
TexDesc.MipLevels = 1;
auto pPreintegratedGGX = m_Device.CreateTexture(TexDesc);
m_pPreintegratedGGX_SRV = pPreintegratedGGX->GetDefaultView(TEXTURE_VIEW_SHADER_RESOURCE);
Expand Down Expand Up @@ -498,6 +501,7 @@ void PBR_Renderer::PrecomputeCubemaps(IDeviceContext* pCtx,
ShaderMacroHelper Macros;
Macros.AddShaderMacro("NUM_PHI_SAMPLES", static_cast<int>(NumPhiSamples));
Macros.AddShaderMacro("NUM_THETA_SAMPLES", static_cast<int>(NumThetaSamples));
Macros.AddShaderMacro("IRRADIANCE_MAP_DIMENSION", static_cast<int>(IrradianceCubeDim));
ShaderCI.Macros = Macros;
RefCntAutoPtr<IShader> pVS;
{
Expand Down
13 changes: 11 additions & 2 deletions Shaders/PBR/private/ComputeIrradianceMap.psh
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,23 @@
# define NUM_THETA_SAMPLES 32
#endif

#ifndef IRRADIANCE_MAP_DIMENSION
# define IRRADIANCE_MAP_DIMENSION 16
#endif

TextureCube g_EnvironmentMap;
SamplerState g_EnvironmentMap_sampler;

void main(in float4 Pos : SV_Position,
in float3 WorldPos : WORLD_POS,
out float4 Color : SV_Target)
{
float3 N = normalize(WorldPos);

uint3 TextureDimension;
g_EnvironmentMap.GetDimensions(0, TextureDimension.x, TextureDimension.y, TextureDimension.z);
float EnvLod = max(0.0, log2(float(max(TextureDimension.x, TextureDimension.y) / IRRADIANCE_MAP_DIMENSION)) - 1.0);

float3 N = normalize(WorldPos);
float3 up = float3(0.0, 1.0, 0.0);
float3 right = normalize(cross(up, N));
up = cross(N, right);
Expand All @@ -34,7 +43,7 @@ void main(in float4 Pos : SV_Position,
float theta = float(t) * deltaTheta;
float3 tempVec = cos(phi) * right + sin(phi) * up;
float3 sampleDir = cos(theta) * N + sin(theta) * tempVec;
color += g_EnvironmentMap.Sample(g_EnvironmentMap_sampler, sampleDir).rgb * cos(theta) * sin(theta);
color += g_EnvironmentMap.SampleLevel(g_EnvironmentMap_sampler, sampleDir, EnvLod).rgb * cos(theta) * sin(theta);
sampleCount += 1.0;
}
}
Expand Down

0 comments on commit f0a1094

Please sign in to comment.