Skip to content

Commit

Permalink
Albedo use 16X anistropic filtering
Browse files Browse the repository at this point in the history
  • Loading branch information
Benualdo committed Nov 17, 2024
1 parent 62f05bd commit 70d7720
Show file tree
Hide file tree
Showing 10 changed files with 127 additions and 28 deletions.
24 changes: 21 additions & 3 deletions data/Shaders/system/materialdata.hlsli
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,24 @@
#include "system/table.hlsli"
#include "system/material_consts.hlsli"

#if 1
#define albedoSampler anisotropicRepeat
#else
#define albedoSampler linearRepeat
#endif

#if 0
#define normalSampler anisotropicRepeat
#else
#define normalSampler linearRepeat
#endif

#if 0
#define pbrSampler anisotropicRepeat
#else
#define pbrSampler linearRepeat
#endif

struct GPUMaterialData
{
#ifdef __cplusplus
Expand Down Expand Up @@ -140,7 +158,7 @@ struct GPUMaterialData
//--------------------------------------------------------------------------------------
float4 getAlbedo(float2 _uv, float4 _vertexColor, DisplayFlags _flags, bool _nonUniform = false)
{
float4 albedo = sampleTexture2D(getAlbedoTextureHandle(), linearRepeat, _uv, _nonUniform);
float4 albedo = sampleTexture2D(getAlbedoTextureHandle(), albedoSampler, _uv, _nonUniform);

#if _TOOLMODE
if (0 == (DisplayFlags::AlbedoMap & _flags))
Expand All @@ -155,7 +173,7 @@ struct GPUMaterialData
//--------------------------------------------------------------------------------------
float4 getNormal(float2 _uv, DisplayFlags _flags, bool _nonUniform = false)
{
float4 normal = sampleTexture2D(getNormalTextureHandle(), linearRepeat, _uv, _nonUniform);
float4 normal = sampleTexture2D(getNormalTextureHandle(), normalSampler, _uv, _nonUniform);

#if _TOOLMODE
if (0 == (DisplayFlags::NormalMap & _flags))
Expand All @@ -168,7 +186,7 @@ struct GPUMaterialData
//--------------------------------------------------------------------------------------
float4 getPBR(float2 _uv, bool _nonUniform = false)
{
float4 pbr = sampleTexture2D(getPBRTextureHandle(), linearRepeat, _uv, _nonUniform);
float4 pbr = sampleTexture2D(getPBRTextureHandle(), pbrSampler, _uv, _nonUniform);

pbr.r = lerp(1.0f, pbr.r, getOcclusion());
pbr.g *= getRoughness();
Expand Down
28 changes: 16 additions & 12 deletions data/Shaders/system/samplers.hlsli
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,27 @@

#ifdef _DX12

sampler nearestClamp : register(s0, space0);
sampler nearestRepeat : register(s1, space0);
sampler linearClamp : register(s2, space0);
sampler linearRepeat : register(s3, space0);
sampler nearestClamp : register(s0, space0);
sampler nearestRepeat : register(s1, space0);
sampler linearClamp : register(s2, space0);
sampler linearRepeat : register(s3, space0);
sampler anisotropicClamp : register(s4, space0);
sampler anisotropicRepeat : register(s5, space0);

SamplerComparisonState shadowcmp : register(s4, space0);
SamplerComparisonState shadowcmp : register(s6, space0);

#elif defined(_VULKAN)

[[vk::binding(0, 1)]] sampler vkSamplerArray[5] : register(s0, space1);
#define nearestClamp vkSamplerArray[0]
#define nearestRepeat vkSamplerArray[1]
#define linearClamp vkSamplerArray[2]
#define linearRepeat vkSamplerArray[3]
[[vk::binding(0, 1)]] sampler vkSamplerArray[7] : register(s0, space1);
#define nearestClamp vkSamplerArray[0]
#define nearestRepeat vkSamplerArray[1]
#define linearClamp vkSamplerArray[2]
#define linearRepeat vkSamplerArray[3]
#define anisotropicClamp vkSamplerArray[4]
#define anisotropicRepeat vkSamplerArray[5]

[[vk::binding(0, 1)]] SamplerComparisonState vkSamplerComparisonArray[5] : register(s0, space1);
#define shadowcmp vkSamplerComparisonArray[4]
[[vk::binding(0, 1)]] SamplerComparisonState vkSamplerComparisonArray[7] : register(s0, space1);
#define shadowcmp vkSamplerComparisonArray[6]

#endif

Expand Down
2 changes: 1 addition & 1 deletion src/application/WinMain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine
#endif

#ifdef VG_DEBUG
engineParams.renderer.device.debugDevice = false;
engineParams.renderer.device.debugDevice = true;
engineParams.renderer.device.breakOnWarnings = true;
engineParams.renderer.device.breakOnErrors = true;
#endif
Expand Down
4 changes: 2 additions & 2 deletions src/gfx/Device/vulkan/Device_vulkan.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -1184,7 +1184,7 @@ namespace vg::gfx::vulkan

VkPhysicalDeviceFeatures2 supportedFeatures = {};
supportedFeatures.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_FEATURES_2;
supportedFeatures.pNext = &vulkan12SupportedFeatures;
supportedFeatures.pNext = &vulkan12SupportedFeatures;

vkGetPhysicalDeviceFeatures2(m_vkPhysicalDevice, &supportedFeatures);

Expand Down Expand Up @@ -1257,8 +1257,8 @@ namespace vg::gfx::vulkan
VkPhysicalDeviceFeatures enabledFeatures = {};
CheckVulkanFeature(supportedFeatures.features, enabledFeatures, fillModeNonSolid, true);
CheckVulkanFeature(supportedFeatures.features, enabledFeatures, fragmentStoresAndAtomics, true);
//CheckVulkanFeature(supportedFeatures.features, enabledFeatures, textureCompressionETC2, true);
CheckVulkanFeature(supportedFeatures.features, enabledFeatures, sampleRateShading, true);
CheckVulkanFeature(supportedFeatures.features, enabledFeatures, samplerAnisotropy, true);

VkDeviceCreateInfo deviceCreateInfo;
deviceCreateInfo.sType = VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO;
Expand Down
16 changes: 12 additions & 4 deletions src/gfx/PipelineState/Graphic/SamplerState.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,14 @@ namespace vg::gfx
Mirror
);

vg_enum_class(Anisotropy, core::u8,
Anisotropy_None,
Anisotropy_2X,
Anisotropy_4X,
Anisotropy_8X,
Anisotropy_16X
);

namespace base
{
class SamplerState
Expand All @@ -31,9 +39,9 @@ namespace vg::gfx
{
struct
{
Filter filter : 4;
Address address : 4;

Filter filter : 2;
Address address : 3;
Anisotropy anisotropy : 3;
};
core::u8 bits;
};
Expand All @@ -52,7 +60,7 @@ namespace vg::gfx
public:

SamplerState(Sampler _sampler);
SamplerState(Filter _filter = Filter::Nearest, Address _address = Address::Repeat);
SamplerState(Filter _filter = Filter::Nearest, Address _address = Address::Repeat, Anisotropy _anisotropy = Anisotropy::Anisotropy_None);

inline bool operator == (const SamplerState & _other) const
{
Expand Down
11 changes: 10 additions & 1 deletion src/gfx/PipelineState/Graphic/SamplerState.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,25 @@ namespace vg::gfx
*this = SamplerState(Filter::Linear, Address::Repeat);
break;

case Sampler::AnisotropicClamp:
*this = SamplerState(Filter::Linear, Address::Clamp, Anisotropy::Anisotropy_16X);
break;

case Sampler::AnisotropicRepeat:
*this = SamplerState(Filter::Linear, Address::Repeat, Anisotropy::Anisotropy_16X);
break;

case Sampler::ShadowCmp:
*this = SamplerState(Filter::DepthCmp, Address::Clamp);
break;
}
}

//--------------------------------------------------------------------------------------
SamplerState::SamplerState(Filter _filter, Address _address)
SamplerState::SamplerState(Filter _filter, Address _address, Anisotropy _anisotropy)
{
filter = _filter;
address = _address;
anisotropy = _anisotropy;
}
}
2 changes: 2 additions & 0 deletions src/gfx/PipelineState/Graphic/SamplerState_consts.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ namespace vg::gfx
NearestRepeat,
LinearClamp,
LinearRepeat,
AnisotropicClamp,
AnisotropicRepeat,
ShadowCmp
);
}
33 changes: 31 additions & 2 deletions src/gfx/PipelineState/Graphic/dx12/SamplerState_dx12.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,37 @@ namespace vg::gfx::dx12
desc.AddressV = getd3d12Adress(samplerState.address);
desc.AddressW = getd3d12Adress(samplerState.address);

desc.MaxAnisotropy = 1;
desc.MipLODBias = 0;
switch (samplerState.anisotropy)
{
default:
VG_ASSERT_ENUM_NOT_IMPLEMENTED(samplerState.anisotropy);

case Anisotropy::Anisotropy_None:
desc.MaxAnisotropy = 1;
break;

case Anisotropy::Anisotropy_2X:
desc.MaxAnisotropy = 2;
desc.Filter = D3D12_FILTER_ANISOTROPIC;
break;

case Anisotropy::Anisotropy_4X:
desc.MaxAnisotropy = 4;
desc.Filter = D3D12_FILTER_ANISOTROPIC;
break;

case Anisotropy::Anisotropy_8X:
desc.MaxAnisotropy = 8;
desc.Filter = D3D12_FILTER_ANISOTROPIC;
break;

case Anisotropy::Anisotropy_16X:
desc.MaxAnisotropy = 16;
desc.Filter = D3D12_FILTER_ANISOTROPIC;
break;
}

desc.MipLODBias = 0;
desc.MinLOD = 0;
desc.MaxLOD = D3D12_FLOAT32_MAX;

Expand Down
33 changes: 31 additions & 2 deletions src/gfx/PipelineState/Graphic/vulkan/SamplerState_vulkan.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,37 @@ namespace vg::gfx::vulkan
samplerCreateInfo.addressModeV = getVulkanAddressMode(samplerState.address); // VK_SAMPLER_ADDRESS_MODE_REPEAT;
samplerCreateInfo.addressModeW = getVulkanAddressMode(samplerState.address); // VK_SAMPLER_ADDRESS_MODE_REPEAT;
samplerCreateInfo.mipLodBias = 0.0;
samplerCreateInfo.anisotropyEnable = VK_FALSE;
samplerCreateInfo.maxAnisotropy = 1;

switch (samplerState.anisotropy)
{
default:
VG_ASSERT_ENUM_NOT_IMPLEMENTED(samplerState.anisotropy);

case Anisotropy::Anisotropy_None:
samplerCreateInfo.anisotropyEnable = VK_FALSE;
samplerCreateInfo.maxAnisotropy = 1;
break;

case Anisotropy::Anisotropy_2X:
samplerCreateInfo.anisotropyEnable = VK_TRUE;
samplerCreateInfo.maxAnisotropy = 2;
break;

case Anisotropy::Anisotropy_4X:
samplerCreateInfo.anisotropyEnable = VK_TRUE;
samplerCreateInfo.maxAnisotropy = 4;
break;

case Anisotropy::Anisotropy_8X:
samplerCreateInfo.anisotropyEnable = VK_TRUE;
samplerCreateInfo.maxAnisotropy = 8;
break;

case Anisotropy::Anisotropy_16X:
samplerCreateInfo.anisotropyEnable = VK_TRUE;
samplerCreateInfo.maxAnisotropy = 16;
break;
}

if (samplerState.filter == Filter::DepthCmp)
{
Expand Down
2 changes: 1 addition & 1 deletion src/version.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@

#define VG_FRAMEWORK_VERSION_MAJOR 0
#define VG_FRAMEWORK_VERSION_MINOR 42
#define VG_FRAMEWORK_VERSION_PATCH 5
#define VG_FRAMEWORK_VERSION_PATCH 6

0 comments on commit 70d7720

Please sign in to comment.