Skip to content

Commit 9e059c7

Browse files
fix nbl_glsl_expImaginary, use shift theorem to get rid of coordiante shift when storing to image
1 parent d406e4d commit 9e059c7

File tree

4 files changed

+14
-32
lines changed

4 files changed

+14
-32
lines changed

examples_tests/49.ComputeFFT/fft_convolve_ifft.comp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,9 +76,10 @@ void convolve(in uint item_per_thread_count, in uint ch)
7676
uvec3 coords = nbl_glsl_ext_FFT_getCoordinates(tid);
7777
//coords &= uvec3(0xffeu);
7878
coords = bitfieldReverse(coords)>>uvec3(21u,22u,0u);
79-
//nbl_glsl_complex shift = nbl_glsl_expImaginary(-nbl_glsl_PI*float(k+l));
79+
const nbl_glsl_complex shift = nbl_glsl_expImaginary(-dot(vec2(coords.xy),vec2(512.f)/vec2(2048.f,1024.f))*nbl_glsl_PI); // TODO: does this shift go away later?
8080
//
81-
nbl_glsl_ext_FFT_impl_values[t] = nbl_glsl_complex_mul(nbl_glsl_ext_FFT_impl_values[t],texelFetch(NormalizedKernel[ch],ivec2(coords.xy),0).xy);
81+
nbl_glsl_complex value = nbl_glsl_complex_mul(nbl_glsl_ext_FFT_impl_values[t],shift);
82+
nbl_glsl_ext_FFT_impl_values[t] = nbl_glsl_complex_mul(texelFetch(NormalizedKernel[ch],ivec2(coords.xy),0).xy,value);
8283
}
8384
}
8485

examples_tests/49.ComputeFFT/last_fft.comp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ layout(set=0, binding=1, rgba16f) uniform image2D outImage;
2525
layout(push_constant) uniform PushConstants
2626
{
2727
nbl_glsl_ext_FFT_Parameters_t params;
28-
layout (offset = 32) uvec3 kernel_dimension;
2928
} pc;
3029

3130
nbl_glsl_ext_FFT_Parameters_t nbl_glsl_ext_FFT_getParameters() {
@@ -45,8 +44,8 @@ nbl_glsl_complex nbl_glsl_ext_FFT_getData(in uvec3 coordinate, in uint channel)
4544

4645
void nbl_glsl_ext_FFT_setData(in uvec3 coordinate, in uint channel, in nbl_glsl_complex complex_value)
4746
{
48-
ivec2 coords = ivec2(coordinate.xy) - ivec2(pc.kernel_dimension.xy / 2);
49-
//const ivec2 coords = ivec2(coordinate.xy);
47+
const ivec2 coords = ivec2(coordinate.xy);
48+
5049
vec4 color_value = imageLoad(outImage, coords);
5150
color_value[channel] = complex_value.x;
5251
imageStore(outImage, coords, color_value);

examples_tests/49.ComputeFFT/main.cpp

Lines changed: 8 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -160,20 +160,6 @@ static inline core::smart_refctd_ptr<video::IGPUPipelineLayout> getPipelineLayou
160160

161161
using FFTClass = ext::FFT::FFT;
162162

163-
static const asset::SPushConstantRange ranges[2] =
164-
{
165-
{
166-
ISpecializedShader::ESS_COMPUTE,
167-
0u,
168-
sizeof(FFTClass::Parameters_t)
169-
},
170-
{
171-
ISpecializedShader::ESS_COMPUTE,
172-
sizeof(FFTClass::Parameters_t),
173-
sizeof(uint32_t) * 3
174-
},
175-
};
176-
177163
static IGPUDescriptorSetLayout::SBinding bnd[] =
178164
{
179165
{
@@ -191,8 +177,9 @@ static inline core::smart_refctd_ptr<video::IGPUPipelineLayout> getPipelineLayou
191177
nullptr
192178
},
193179
};
194-
195-
core::SRange<const asset::SPushConstantRange> pcRange = {ranges, ranges+2};
180+
181+
using FFTClass = ext::FFT::FFT;
182+
core::SRange<const asset::SPushConstantRange> pcRange = FFTClass::getDefaultPushConstantRanges();
196183
core::SRange<const video::IGPUDescriptorSetLayout::SBinding> bindings = {bnd, bnd+sizeof(bnd)/sizeof(IGPUDescriptorSetLayout::SBinding)};;
197184

198185
return driver->createGPUPipelineLayout(
@@ -362,9 +349,7 @@ int main()
362349
}
363350
// TODO: re-examine
364351
const VkExtent3D paddedDim = FFTClass::padDimensionToNextPOT(srcDim);
365-
uint32_t maxPaddedDimensionSize = core::max(core::max(paddedDim.width, paddedDim.height), paddedDim.depth);
366-
auto fftGPUSpecializedShader_SSBOInput = FFTClass::createShader(driver, FFTClass::DataType::SSBO, maxPaddedDimensionSize);
367-
auto fftGPUSpecializedShader_ImageInput = FFTClass::createShader(driver, FFTClass::DataType::TEXTURE2D, maxPaddedDimensionSize);
352+
auto fftGPUSpecializedShader_ImageInput = FFTClass::createShader(driver, FFTClass::DataType::TEXTURE2D, paddedDim.width);
368353
auto fftGPUSpecializedShader_KernelNormalization = [&]() -> auto
369354
{
370355
IAssetLoader::SAssetLoadParams lp;
@@ -373,7 +358,6 @@ int main()
373358
return *stuff->begin();
374359
}();
375360

376-
auto fftPipelineLayout_SSBOInput = FFTClass::getDefaultPipelineLayout(driver, FFTClass::DataType::SSBO);
377361
auto fftPipelineLayout_ImageInput = FFTClass::getDefaultPipelineLayout(driver, FFTClass::DataType::TEXTURE2D);
378362
auto fftPipelineLayout_KernelNormalization = [&]() -> auto
379363
{
@@ -400,18 +384,17 @@ int main()
400384
);
401385
}();
402386

403-
auto fftPipeline_SSBOInput = driver->createGPUComputePipeline(nullptr, core::smart_refctd_ptr(fftPipelineLayout_SSBOInput), std::move(fftGPUSpecializedShader_SSBOInput));
404387
auto fftPipeline_ImageInput = driver->createGPUComputePipeline(nullptr, core::smart_refctd_ptr(fftPipelineLayout_ImageInput), std::move(fftGPUSpecializedShader_ImageInput));
405388
auto fftPipeline_KernelNormalization = driver->createGPUComputePipeline(nullptr, core::smart_refctd_ptr(fftPipelineLayout_KernelNormalization), std::move(fftGPUSpecializedShader_KernelNormalization));
406389

407390
auto fftDispatchInfo_Horizontal = FFTClass::buildParameters(paddedDim, FFTClass::Direction::X);
408391
auto fftDispatchInfo_Vertical = FFTClass::buildParameters(paddedDim, FFTClass::Direction::Y);
409392

410-
auto convolveShader = createShader_Convolution(driver, am, maxPaddedDimensionSize);
393+
auto convolveShader = createShader_Convolution(driver, am, paddedDim.height);
411394
auto convolvePipelineLayout = getPipelineLayout_Convolution(driver);
412395
auto convolvePipeline = driver->createGPUComputePipeline(nullptr, core::smart_refctd_ptr(convolvePipelineLayout), std::move(convolveShader));
413396

414-
auto lastFFTShader = createShader_LastFFT(driver, am, maxPaddedDimensionSize);
397+
auto lastFFTShader = createShader_LastFFT(driver, am, paddedDim.width);
415398
auto lastFFTPipelineLayout = getPipelineLayout_LastFFT(driver);
416399
auto lastFFTPipeline = driver->createGPUComputePipeline(nullptr, core::smart_refctd_ptr(lastFFTPipelineLayout), std::move(lastFFTShader));
417400

@@ -452,6 +435,7 @@ int main()
452435
FFTClass::updateDescriptorSet(driver, fftDescriptorSet_Ker_FFT_X.get(), kerImageView, fftOutputBuffer_0, ISampler::ETC_CLAMP_TO_BORDER);
453436

454437
// Ker FFT Y
438+
auto fftPipelineLayout_SSBOInput = FFTClass::getDefaultPipelineLayout(driver, FFTClass::DataType::SSBO);
455439
auto fftDescriptorSet_Ker_FFT_Y = driver->createGPUDescriptorSet(core::smart_refctd_ptr<const IGPUDescriptorSetLayout>(fftPipelineLayout_SSBOInput->getDescriptorSetLayout(0u)));
456440
FFTClass::updateDescriptorSet(driver, fftDescriptorSet_Ker_FFT_Y.get(), fftOutputBuffer_0, fftOutputBuffer_1);
457441

@@ -502,6 +486,7 @@ int main()
502486
FFTClass::dispatchHelper(driver, fftDispatchInfo_Horizontal);
503487

504488
// Ker Image FFT Y
489+
auto fftPipeline_SSBOInput = driver->createGPUComputePipeline(nullptr, core::smart_refctd_ptr(fftPipelineLayout_SSBOInput), FFTClass::createShader(driver,FFTClass::DataType::SSBO,paddedDim.height));
505490
driver->bindComputePipeline(fftPipeline_SSBOInput.get());
506491
driver->bindDescriptorSets(EPBP_COMPUTE, fftPipelineLayout_SSBOInput.get(), 0u, 1u, &fftDescriptorSet_Ker_FFT_Y.get(), nullptr);
507492
FFTClass::pushConstants(driver, fftPipelineLayout_SSBOInput.get(), paddedDim, paddedDim, FFTClass::Direction::Y, false, srcNumChannels);
@@ -559,7 +544,6 @@ int main()
559544
driver->bindComputePipeline(lastFFTPipeline.get());
560545
driver->bindDescriptorSets(EPBP_COMPUTE, lastFFTPipelineLayout.get(), 0u, 1u, &lastFFTDescriptorSet.get(), nullptr);
561546
FFTClass::pushConstants(driver, lastFFTPipelineLayout.get(), paddedDim, paddedDim, FFTClass::Direction::X, true, srcNumChannels);
562-
driver->pushConstants(lastFFTPipelineLayout.get(), nbl::video::IGPUSpecializedShader::ESS_COMPUTE, sizeof(FFTClass::Parameters_t), sizeof(uint32_t) * 3, &kerDim); // numSrcChannels
563547
FFTClass::dispatchHelper(driver, fftDispatchInfo_Horizontal);
564548

565549
if(false == savedToFile) {

include/nbl/builtin/glsl/math/complex.glsl

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,7 @@
1717

1818
nbl_glsl_complex nbl_glsl_expImaginary(in float _theta)
1919
{
20-
nbl_glsl_complex retval;
21-
nbl_glsl_sincos(_theta,retval.y,retval.x);
22-
return retval;
20+
return vec2(cos(_theta),sin(_theta));
2321
}
2422

2523
nbl_glsl_complex nbl_glsl_complex_mul(in nbl_glsl_complex rhs, in nbl_glsl_complex lhs)

0 commit comments

Comments
 (0)