Skip to content

Commit

Permalink
Kernel sampling bugfix
Browse files Browse the repository at this point in the history
  • Loading branch information
Fletterio committed Jan 25, 2025
1 parent 67473d7 commit 662d15d
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 17 deletions.
6 changes: 5 additions & 1 deletion 28_FFTBloom/app_resources/fft_convolve_ifft.hlsl
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,12 @@ struct PreloadedSecondAxisAccessor : PreloadedAccessorMirrorTradeBase
NBL_CONSTEXPR_STATIC_INLINE uint32_t NumWorkgroups = ShaderConstevalParameters::NumWorkgroups;
NBL_CONSTEXPR_STATIC_INLINE uint32_t PreviousWorkgroupSize = uint32_t(ShaderConstevalParameters::PreviousWorkgroupSize);
NBL_CONSTEXPR_STATIC_INLINE float32_t TotalSizeReciprocal = ShaderConstevalParameters::TotalSizeReciprocal;
NBL_CONSTEXPR_STATIC_INLINE uint16_t KernelSideLength = ShaderConstevalParameters::KernelSideLength;
NBL_CONSTEXPR_STATIC_INLINE float32_t2 KernelHalfPixelSize;

// When sampling u/v coordinates along the first axis we did an FFT on, workgroup `w` samples at normalized position `SampleSlope * w + KernelHalfPixelSize`
NBL_CONSTEXPR_STATIC_INLINE float32_t SampleSlope = float32_t(KernelSideLength) / float32_t(NumWorkgroups * uint32_t(KernelSideLength + 2));

NBL_CONSTEXPR_STATIC_INLINE vector<scalar_t, 2> One;

// ---------------------------------------------------- Utils ---------------------------------------------------------
Expand Down Expand Up @@ -134,7 +138,7 @@ struct PreloadedSecondAxisAccessor : PreloadedAccessorMirrorTradeBase
{
const uint32_t indexDFT = FFTIndexingUtils::getDFTIndex(globalElementIndex);
const uint32_t2 texCoords = uint32_t2(indexDFT, y);
const float32_t2 uv = texCoords * float32_t2(TotalSizeReciprocal, 1.f / NumWorkgroups) + KernelHalfPixelSize;
const float32_t2 uv = texCoords * float32_t2(TotalSizeReciprocal, SampleSlope) + KernelHalfPixelSize;
const vector<scalar_t, 2> sampledKernelVector = vector<scalar_t, 2>(kernelChannels.SampleLevel(samplerState, float32_t3(uv, float32_t(glsl::gl_WorkGroupID().y)), 0));
const vector<scalar_t, 2> sampledKernelInterpolatedVector = lerp(sampledKernelVector, One, promote<vector<scalar_t, 2>, float32_t>(pushConstants.interpolatingFactor));
const complex_t<scalar_t> sampledKernelInterpolated = { sampledKernelInterpolatedVector.x, sampledKernelInterpolatedVector.y };
Expand Down
37 changes: 21 additions & 16 deletions 28_FFTBloom/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -142,14 +142,15 @@ class FFTBloomApp final : public examples::SimpleWindowedApplication, public app
uint16_t numWorkgroupsLog2 = 0;
uint16_t previousElementsPerInvocationLog2 = 0;
uint16_t previousWorkgroupSizeLog2 = 0;
uint16_t kernelSideLength = 0;
float32_t2 kernelHalfPixelSize = { 0.5f, 0.5f };
};

SShaderConstevalParameters(SShaderConstevalParametersCreateInfo& info) :
scalar_t(info.useHalfFloats ? "float16_t" : "float32_t"), elementsPerInvocationLog2(info.elementsPerInvocationLog2),
workgroupSizeLog2(info.workgroupSizeLog2), numWorkgroupsLog2(info.numWorkgroupsLog2), numWorkgroups(uint32_t(1) << info.numWorkgroupsLog2),
previousElementsPerInvocationLog2(info.previousElementsPerInvocationLog2), previousWorkgroupSizeLog2(info.previousWorkgroupSizeLog2),
previousWorkgroupSize(uint16_t(1) << info.previousWorkgroupSizeLog2), kernelHalfPixelSize(info.kernelHalfPixelSize)
previousWorkgroupSize(uint16_t(1) << info.previousWorkgroupSizeLog2), kernelSideLength(info.kernelSideLength), kernelHalfPixelSize(info.kernelHalfPixelSize)
{
const uint32_t totalSize = uint32_t(1) << (elementsPerInvocationLog2 + workgroupSizeLog2);
totalSizeReciprocal = 1.f / float32_t(totalSize);
Expand All @@ -163,6 +164,7 @@ class FFTBloomApp final : public examples::SimpleWindowedApplication, public app
uint16_t previousElementsPerInvocationLog2;
uint16_t previousWorkgroupSizeLog2;
uint16_t previousWorkgroupSize;
uint16_t kernelSideLength;
float32_t2 kernelHalfPixelSize;
float32_t totalSizeReciprocal;
};
Expand Down Expand Up @@ -191,6 +193,7 @@ class FFTBloomApp final : public examples::SimpleWindowedApplication, public app
NBL_CONSTEXPR_STATIC_INLINE uint16_t PreviousElementsPerInvocationLog2 = )===" << shaderConstants.previousElementsPerInvocationLog2 << R"===(;
NBL_CONSTEXPR_STATIC_INLINE uint16_t PreviousWorkgroupSizeLog2 = )===" << shaderConstants.previousWorkgroupSizeLog2 << R"===(;
NBL_CONSTEXPR_STATIC_INLINE uint16_t PreviousWorkgroupSize = )===" << shaderConstants.previousWorkgroupSize << R"===(;
NBL_CONSTEXPR_STATIC_INLINE uint16_t KernelSideLength = )===" << shaderConstants.kernelSideLength << R"===(;
NBL_CONSTEXPR_STATIC_INLINE float32_t2 KernelHalfPixelSize;
NBL_CONSTEXPR_STATIC_INLINE float32_t TotalSizeReciprocal = )===" << shaderConstants.totalSizeReciprocal << R"===(;
};
Expand Down Expand Up @@ -367,9 +370,9 @@ class FFTBloomApp final : public examples::SimpleWindowedApplication, public app
// Create samplers
ICPUSampler::SParams samplerCreationParams =
{
ISampler::ETC_MIRROR,
ISampler::ETC_MIRROR,
ISampler::ETC_MIRROR,
ISampler::E_TEXTURE_CLAMP::ETC_REPEAT,
ISampler::E_TEXTURE_CLAMP::ETC_REPEAT,
ISampler::E_TEXTURE_CLAMP::ETC_REPEAT,
ISampler::ETBC_FLOAT_OPAQUE_BLACK,
ISampler::ETF_LINEAR,
ISampler::ETF_LINEAR,
Expand All @@ -379,21 +382,22 @@ class FFTBloomApp final : public examples::SimpleWindowedApplication, public app
ISampler::ECO_ALWAYS
};

auto mirrorSamplerCPU = make_smart_refctd_ptr<ICPUSampler>(samplerCreationParams);
auto repeatSamplerCPU = make_smart_refctd_ptr<ICPUSampler>(samplerCreationParams);

smart_refctd_ptr<ICPUSampler> imageSamplerCPU;
if (!m_useMirrorPadding_first)
{
samplerCreationParams.TextureWrapU = ISampler::ETC_CLAMP_TO_BORDER;
samplerCreationParams.TextureWrapV = ISampler::ETC_CLAMP_TO_BORDER;
samplerCreationParams.TextureWrapW = ISampler::ETC_CLAMP_TO_BORDER;
imageSamplerCPU = make_smart_refctd_ptr<ICPUSampler>(samplerCreationParams);
samplerCreationParams.TextureWrapU = ISampler::E_TEXTURE_CLAMP::ETC_CLAMP_TO_BORDER;
samplerCreationParams.TextureWrapV = ISampler::E_TEXTURE_CLAMP::ETC_CLAMP_TO_BORDER;
samplerCreationParams.TextureWrapW = ISampler::E_TEXTURE_CLAMP::ETC_CLAMP_TO_BORDER;
}
else
{
imageSamplerCPU = mirrorSamplerCPU;
samplerCreationParams.TextureWrapU = ISampler::E_TEXTURE_CLAMP::ETC_MIRROR;
samplerCreationParams.TextureWrapV = ISampler::E_TEXTURE_CLAMP::ETC_MIRROR;
samplerCreationParams.TextureWrapW = ISampler::E_TEXTURE_CLAMP::ETC_MIRROR;
}

imageSamplerCPU = make_smart_refctd_ptr<ICPUSampler>(samplerCreationParams);

// Set descriptor set values for automatic upload

Expand All @@ -407,7 +411,7 @@ class FFTBloomApp final : public examples::SimpleWindowedApplication, public app
secondSampledImageDescriptorInfo.info.image.imageLayout = IImage::LAYOUT::READ_ONLY_OPTIMAL;

auto& mirrorSamplerDescriptorInfo = descriptorSetCPU->getDescriptorInfos(ICPUDescriptorSetLayout::CBindingRedirect::binding_number_t(1u), IDescriptor::E_TYPE::ET_SAMPLER).front();
mirrorSamplerDescriptorInfo.desc = mirrorSamplerCPU;
mirrorSamplerDescriptorInfo.desc = repeatSamplerCPU;

auto& imageSamplerDescriptorInfo = descriptorSetCPU->getDescriptorInfos(ICPUDescriptorSetLayout::CBindingRedirect::binding_number_t(4u), IDescriptor::E_TYPE::ET_SAMPLER).front();
imageSamplerDescriptorInfo.desc = imageSamplerCPU;
Expand Down Expand Up @@ -906,11 +910,12 @@ class FFTBloomApp final : public examples::SimpleWindowedApplication, public app
SShaderConstevalParameters::SShaderConstevalParametersCreateInfo shaderConstevalInfo =
{
.useHalfFloats = m_useHalfFloats,
.elementsPerInvocationLog2 = elementsPerInvocationLog2,
.workgroupSizeLog2 = workgroupSizeLog2,
.numWorkgroupsLog2 = firstAxisFFTHalfLengthLog2,
.elementsPerInvocationLog2 = elementsPerInvocationLog2,
.workgroupSizeLog2 = workgroupSizeLog2,
.numWorkgroupsLog2 = firstAxisFFTHalfLengthLog2,
.previousElementsPerInvocationLog2 = firstAxisFFTElementsPerInvocationLog2,
.previousWorkgroupSizeLog2 = firstAxisFFTWorkgroupSizeLog2,
.previousWorkgroupSizeLog2 = firstAxisFFTWorkgroupSizeLog2,
.kernelSideLength = uint16_t(kerDim.height),
.kernelHalfPixelSize = kernelHalfPixelSize
};
SShaderConstevalParameters shaderConstevalParameters(shaderConstevalInfo);
Expand Down

0 comments on commit 662d15d

Please sign in to comment.