Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@
# include "Skylighting/Skylighting.hlsli"
#endif

#if defined(IBL)
# include "IBL/IBL.hlsli"
#endif

namespace DynamicCubemaps
{
TextureCube<float4> EnvReflectionsTexture : register(t30);
Expand Down Expand Up @@ -33,6 +37,16 @@ namespace DynamicCubemaps

float3 finalIrradiance = 0;

# if defined(IBL) && defined(LIGHTING)
const bool inWorld = (Permutation::ExtraShaderDescriptor & Permutation::ExtraFlags::InWorld);
const bool inReflection = (Permutation::ExtraShaderDescriptor & Permutation::ExtraFlags::InReflection);
if (SharedData::iblSettings.EnableDiffuseIBL && SharedData::iblSettings.UseStaticIBL && !inWorld && !inReflection) {
float3 specularIrradiance = ImageBasedLighting::StaticSpecularIBLTexture.SampleLevel(SampColorSampler, R.xzy, level).xyz;
finalIrradiance += specularIrradiance;
return finalIrradiance;
}
# endif

# if defined(SKYLIGHTING)
if (SharedData::InInterior) {
float3 specularIrradiance = Color::GammaToLinear(EnvTexture.SampleLevel(SampColorSampler, R, level).xyz);
Expand Down Expand Up @@ -90,6 +104,16 @@ namespace DynamicCubemaps

float3 finalIrradiance = 0;

# if defined(IBL) && defined(LIGHTING)
const bool inWorld = (Permutation::ExtraShaderDescriptor & Permutation::ExtraFlags::InWorld);
const bool inReflection = (Permutation::ExtraShaderDescriptor & Permutation::ExtraFlags::InReflection);
if (SharedData::iblSettings.EnableDiffuseIBL && SharedData::iblSettings.UseStaticIBL && !inWorld && !inReflection) {
float3 specularIrradiance = ImageBasedLighting::StaticSpecularIBLTexture.SampleLevel(SampColorSampler, R.xzy, level).xyz;
finalIrradiance += specularIrradiance;
return horizon * (F0 * specularBRDF.x + specularBRDF.y) * finalIrradiance;
}
# endif

# if defined(SKYLIGHTING)
if (SharedData::InInterior) {
float3 specularIrradiance = Color::GammaToLinear(EnvTexture.SampleLevel(SampColorSampler, R, level).xyz);
Expand Down
Binary file added features/IBL/Shaders/IBL/DiffuseIBL.dds
Binary file not shown.
16 changes: 15 additions & 1 deletion features/IBL/Shaders/IBL/IBL.hlsli
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
#ifndef __IBL_HLSLI__
#define __IBL_HLSLI__

#include "Common/Color.hlsli"
#include "Common/Math.hlsli"
#include "Common/Random.hlsli"
Expand All @@ -12,6 +15,8 @@ namespace ImageBasedLighting
Texture2D<sh2> DiffuseIBLTexture : register(t14);
#else
Texture2D<sh2> DiffuseIBLTexture : register(t76);
TextureCube<float4> StaticDiffuseIBLTexture : register(t77);
TextureCube<float4> StaticSpecularIBLTexture : register(t78);
#endif
float3 GetDiffuseIBL(float3 rayDir)
{
Expand All @@ -24,6 +29,13 @@ namespace ImageBasedLighting
return float3(colorR, colorG, colorB) / Math::PI;
}

#if defined(LIGHTING)
float3 GetStaticDiffuseIBL(float3 N, SamplerState samp)
{
return StaticDiffuseIBLTexture.SampleLevel(samp, N.xzy, 0).xyz / Math::PI;
}
#endif

float3 GetFogIBLColor(float3 fogColor)
{
float3 directionalAmbientColor = max(0, mul(SharedData::DirectionalAmbient, float4(float3(0, 0, 0), 1.0))).xyz;
Expand All @@ -40,4 +52,6 @@ namespace ImageBasedLighting
}
return lerp(fogColor, iblColor, SharedData::iblSettings.FogAmount);
}
}
}

#endif // __IBL_HLSLI__
Binary file added features/IBL/Shaders/IBL/SpecIBL.dds
Binary file not shown.
2 changes: 1 addition & 1 deletion package/Shaders/Common/SharedData.hlsli
Original file line number Diff line number Diff line change
Expand Up @@ -179,12 +179,12 @@ namespace SharedData
{
uint EnableDiffuseIBL;
uint PreserveFogLuminance;
uint UseStaticIBL;
float DiffuseIBLScale;
float DALCAmount;
float IBLSaturation;
float FogAmount;
float DynamicCubemapsAmount;
float pad;
};

struct ExtendedTranslucencySettings
Expand Down
10 changes: 7 additions & 3 deletions package/Shaders/Lighting.hlsl
Original file line number Diff line number Diff line change
Expand Up @@ -2737,9 +2737,13 @@ PS_OUTPUT main(PS_INPUT input, bool frontFace : SV_IsFrontFace)
float3 directionalAmbientColor = max(0, mul(DirectionalAmbient, float4(worldNormal, 1.0)));

# if defined(IBL)
if (SharedData::iblSettings.EnableDiffuseIBL && !SharedData::InInterior) {
directionalAmbientColor *= SharedData::iblSettings.DALCAmount;
directionalAmbientColor += Color::Saturation(ImageBasedLighting::GetDiffuseIBL(-worldNormal), SharedData::iblSettings.IBLSaturation) * SharedData::iblSettings.DiffuseIBLScale;
if (SharedData::iblSettings.EnableDiffuseIBL) {
if (SharedData::iblSettings.UseStaticIBL && !inWorld && !inReflection) {
directionalAmbientColor = ImageBasedLighting::GetStaticDiffuseIBL(worldNormal, SampColorSampler);
} else if (!SharedData::InInterior) {
directionalAmbientColor *= SharedData::iblSettings.DALCAmount;
directionalAmbientColor += Color::Saturation(ImageBasedLighting::GetDiffuseIBL(-worldNormal), SharedData::iblSettings.IBLSaturation) * SharedData::iblSettings.DiffuseIBLScale;
}
}
Comment thread
jiayev marked this conversation as resolved.
# endif

Expand Down
90 changes: 88 additions & 2 deletions src/Features/IBL.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,14 @@
#include "Shadercache.h"
#include "State.h"

#include <DDSTextureLoader.h>
#include <DirectXTex.h>

NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE_WITH_DEFAULT(
IBL::Settings,
EnableDiffuseIBL,
PreserveFogLuminance,
UseStaticIBL,
DiffuseIBLScale,
DALCAmount,
IBLSaturation,
Expand All @@ -21,6 +25,10 @@ void IBL::DrawSettings()
ImGui::SliderFloat("Diffuse IBL Scale", &settings.DiffuseIBLScale, 0.0f, 10.0f, "%.2f");
ImGui::SliderFloat("Diffuse IBL Saturation", &settings.IBLSaturation, 0.0f, 2.0f, "%.2f");
ImGui::SliderFloat("DALC Amount", &settings.DALCAmount, 0.0f, 1.0f, "%.2f");
ImGui::Checkbox("Use Static IBL For Out-of-World Objects", (bool*)&settings.UseStaticIBL);
if (auto _tt = Util::HoverTooltipWrapper()) {
ImGui::Text("Enables the use of static IBL textures for objects that are not in the world (e.g. inventory items).");
}
ImGui::SliderFloat("Fog Mix", &settings.FogAmount, 0.0f, 1.0f, "%.2f");
ImGui::Checkbox("Preserve Fog Luminance", (bool*)&settings.PreserveFogLuminance);
ImGui::SliderFloat("Dynamic Cubemaps Amount", &settings.DynamicCubemapsAmount, 0.0f, 1.0f, "%.2f");
Expand Down Expand Up @@ -54,8 +62,12 @@ void IBL::EarlyPrepass()

// Set PS shader resource
{
ID3D11ShaderResourceView* srv = diffuseIBLTexture->srv.get();
context->PSSetShaderResources(76, 1, &srv);
std::array<ID3D11ShaderResourceView*, 3> srvs = {
diffuseIBLTexture->srv.get(),
staticDiffuseIBLTexture->srv.get(),
staticSpecularIBLTexture->srv.get()
};
context->PSSetShaderResources(76, 3, srvs.data());
Comment thread
doodlum marked this conversation as resolved.
}
}
}
Expand Down Expand Up @@ -146,6 +158,80 @@ void IBL::SetupResources()
diffuseIBLTexture->CreateSRV(srvDesc);
diffuseIBLTexture->CreateUAV(uavDesc);
}

auto device = globals::d3d::device;

logger::debug("Loading static Diffuse IBL textures...");
{
DirectX::ScratchImage image;
try {
std::filesystem::path path = "Data\\Shaders\\IBL\\DiffuseIBL.dds";

DX::ThrowIfFailed(LoadFromDDSFile(path.c_str(), DirectX::DDS_FLAGS_NONE, nullptr, image));
} catch (const DX::com_exception& e) {
logger::error("{}", e.what());
return;
}

ID3D11Resource* pResource = nullptr;
try {
DX::ThrowIfFailed(CreateTexture(device,
image.GetImages(), image.GetImageCount(),
image.GetMetadata(), &pResource));
} catch (const DX::com_exception& e) {
logger::error("{}", e.what());
return;
}

staticDiffuseIBLTexture = eastl::make_unique<Texture2D>(reinterpret_cast<ID3D11Texture2D*>(pResource));

staticDiffuseIBLTexture->desc.MiscFlags |= D3D11_RESOURCE_MISC_TEXTURECUBE;

D3D11_SHADER_RESOURCE_VIEW_DESC srvDesc = {
.Format = staticDiffuseIBLTexture->desc.Format,
.ViewDimension = D3D11_SRV_DIMENSION_TEXTURECUBE,
.TextureCube = {
.MostDetailedMip = 0,
.MipLevels = 1 }
};
staticDiffuseIBLTexture->CreateSRV(srvDesc);
}

logger::debug("Loading static Specular IBL textures...");
{
DirectX::ScratchImage image;
try {
std::filesystem::path path = "Data\\Shaders\\IBL\\SpecIBL.dds";

DX::ThrowIfFailed(LoadFromDDSFile(path.c_str(), DirectX::DDS_FLAGS_NONE, nullptr, image));
} catch (const DX::com_exception& e) {
logger::error("{}", e.what());
return;
}

ID3D11Resource* pResource = nullptr;
try {
DX::ThrowIfFailed(CreateTexture(device,
image.GetImages(), image.GetImageCount(),
image.GetMetadata(), &pResource));
} catch (const DX::com_exception& e) {
logger::error("{}", e.what());
return;
}

staticSpecularIBLTexture = eastl::make_unique<Texture2D>(reinterpret_cast<ID3D11Texture2D*>(pResource));

staticSpecularIBLTexture->desc.MiscFlags |= D3D11_RESOURCE_MISC_TEXTURECUBE;

D3D11_SHADER_RESOURCE_VIEW_DESC srvDesc = {
.Format = staticSpecularIBLTexture->desc.Format,
.ViewDimension = D3D11_SRV_DIMENSION_TEXTURECUBE,
.TextureCube = {
.MostDetailedMip = 0,
.MipLevels = 8 }
};
staticSpecularIBLTexture->CreateSRV(srvDesc);
}
}

void IBL::ClearShaderCache()
Expand Down
5 changes: 4 additions & 1 deletion src/Features/IBL.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,16 @@ struct IBL : Feature
{
uint EnableDiffuseIBL = 1;
uint PreserveFogLuminance = 0;
uint UseStaticIBL = 1;
float DiffuseIBLScale = 1.0f;
float DALCAmount = 0.33f;
float IBLSaturation = 1.0f;
float FogAmount = 0.0f;
float DynamicCubemapsAmount = 0.0f;
float pad = 0.0f;
} settings;

eastl::unique_ptr<Texture2D> staticDiffuseIBLTexture = nullptr;
eastl::unique_ptr<Texture2D> staticSpecularIBLTexture = nullptr;

ID3D11ComputeShader* GetDiffuseIBLCS();
};