Skip to content

Commit b54defc

Browse files
committed
moved shaders out of Cpp
1 parent 7dd0fdb commit b54defc

File tree

6 files changed

+189
-140
lines changed

6 files changed

+189
-140
lines changed

examples_tests/49.ComputeFFT/main.cpp

Lines changed: 18 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -98,13 +98,13 @@ static inline core::smart_refctd_ptr<video::IGPUPipelineLayout> getPipelineLayou
9898
}
9999
static inline core::smart_refctd_ptr<video::IGPUSpecializedShader> createShader_Convolution(
100100
video::IVideoDriver* driver,
101-
nbl::asset::IGLSLCompiler * glslc,
102-
nbl::io::IFileSystem * fs) {
103-
io::IReadFile* file = fs->createAndOpenFile("../convolve.comp");
104-
auto cpucs = glslc->resolveIncludeDirectives(file, asset::ISpecializedShader::ESS_COMPUTE, "../convolve.comp");
105-
auto cs = driver->createGPUShader(std::move(cpucs));
106-
file->drop();
107-
asset::ISpecializedShader::SInfo csinfo(nullptr, nullptr, "main", asset::ISpecializedShader::ESS_COMPUTE, "../convolve.comp");
101+
IAssetManager* am) {
102+
IAssetLoader::SAssetLoadParams lp;
103+
auto file_path = "../convolve.comp";
104+
auto shaderAsset = am->getAsset(file_path, lp);
105+
auto cpucs = IAsset::castDown<ICPUSpecializedShader>(shaderAsset.getContents().begin()[0]);
106+
auto cs = driver->createGPUShader(nbl::core::smart_refctd_ptr<const ICPUShader>((cpucs->getUnspecialized())));
107+
asset::ISpecializedShader::SInfo csinfo(nullptr, nullptr, "main", asset::ISpecializedShader::ESS_COMPUTE, file_path);
108108
auto cs_spec = driver->createGPUSpecializedShader(cs.get(), csinfo);
109109
return cs_spec;
110110
}
@@ -225,13 +225,14 @@ static inline core::smart_refctd_ptr<video::IGPUPipelineLayout> getPipelineLayou
225225
}
226226
static inline core::smart_refctd_ptr<video::IGPUSpecializedShader> createShader_RemovePadding(
227227
video::IVideoDriver* driver,
228-
nbl::asset::IGLSLCompiler * glslc,
229-
nbl::io::IFileSystem * fs) {
230-
io::IReadFile* file = fs->createAndOpenFile("../remove_padding.comp");
231-
auto cpucs = glslc->resolveIncludeDirectives(file, asset::ISpecializedShader::ESS_COMPUTE, "../remove_padding.comp");
232-
auto cs = driver->createGPUShader(std::move(cpucs));
233-
file->drop();
234-
asset::ISpecializedShader::SInfo csinfo(nullptr, nullptr, "main", asset::ISpecializedShader::ESS_COMPUTE, "../remove_padding.comp");
228+
IAssetManager* am) {
229+
230+
IAssetLoader::SAssetLoadParams lp;
231+
auto file_path = "../remove_padding.comp";
232+
auto shaderAsset = am->getAsset(file_path, lp);
233+
auto cpucs = IAsset::castDown<ICPUSpecializedShader>(shaderAsset.getContents().begin()[0]);
234+
auto cs = driver->createGPUShader(nbl::core::smart_refctd_ptr<const ICPUShader>((cpucs->getUnspecialized())));
235+
asset::ISpecializedShader::SInfo csinfo(nullptr, nullptr, "main", asset::ISpecializedShader::ESS_COMPUTE, file_path);
235236
auto cs_spec = driver->createGPUSpecializedShader(cs.get(), csinfo);
236237
return cs_spec;
237238
}
@@ -316,9 +317,6 @@ int main()
316317

317318
nbl::io::IFileSystem* filesystem = device->getFileSystem();
318319
IAssetManager* am = device->getAssetManager();
319-
auto* fs = am->getFileSystem();
320-
auto* glslc = am->getGLSLCompiler();
321-
322320
// Loading SrcImage and Kernel Image from File
323321

324322
IAssetLoader::SAssetLoadParams lp;
@@ -399,7 +397,7 @@ int main()
399397

400398
auto fftGPUSpecializedShader_SSBOInput = FFTClass::createShader(driver, FFTClass::DataType::SSBO, maxPaddedDimensionSize);
401399
auto fftGPUSpecializedShader_ImageInput = FFTClass::createShader(driver, FFTClass::DataType::TEXTURE2D, maxPaddedDimensionSize);
402-
auto fftGPUSpecializedShader_KernelNormalization = FFTClass::createKernelNormalizationShader(driver);
400+
auto fftGPUSpecializedShader_KernelNormalization = FFTClass::createKernelNormalizationShader(driver, am);
403401

404402
auto fftPipelineLayout_SSBOInput = FFTClass::getDefaultPipelineLayout(driver, FFTClass::DataType::SSBO);
405403
auto fftPipelineLayout_ImageInput = FFTClass::getDefaultPipelineLayout(driver, FFTClass::DataType::TEXTURE2D);
@@ -412,7 +410,7 @@ int main()
412410
auto fftDispatchInfo_Horizontal = FFTClass::buildParameters(paddedDim, FFTClass::Direction::X, srcNumChannels);
413411
auto fftDispatchInfo_Vertical = FFTClass::buildParameters(paddedDim, FFTClass::Direction::Y, srcNumChannels);
414412

415-
auto convolveShader = createShader_Convolution(driver, glslc, fs);
413+
auto convolveShader = createShader_Convolution(driver, am);
416414
auto convolvePipelineLayout = getPipelineLayout_Convolution(driver);
417415
auto convolvePipeline = driver->createGPUComputePipeline(nullptr, core::smart_refctd_ptr(convolvePipelineLayout), std::move(convolveShader));
418416
auto convolveDispatchInfo = getDispatchInfo_Convolution(paddedDim, srcNumChannels);
@@ -482,7 +480,7 @@ int main()
482480
auto fftDescriptorSet_IFFT_Y = driver->createGPUDescriptorSet(core::smart_refctd_ptr<const IGPUDescriptorSetLayout>(fftPipelineLayout_SSBOInput->getDescriptorSetLayout(0u)));
483481
FFTClass::updateDescriptorSet(driver, fftDescriptorSet_IFFT_Y.get(), fftOutputBuffer_1, fftOutputBuffer_0);
484482

485-
auto removePaddingShader = createShader_RemovePadding(driver, glslc, fs);
483+
auto removePaddingShader = createShader_RemovePadding(driver, am);
486484
auto removePaddingPipelineLayout = getPipelineLayout_RemovePadding(driver);
487485
auto removePaddingPipeline = driver->createGPUComputePipeline(nullptr, core::smart_refctd_ptr(removePaddingPipelineLayout), std::move(removePaddingShader));
488486
auto removePaddingDescriptorSet = driver->createGPUDescriptorSet(core::smart_refctd_ptr<const IGPUDescriptorSetLayout>(removePaddingPipelineLayout->getDescriptorSetLayout(0u)));
Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
#ifndef _NBL_GLSL_EXT_DEFAULT_COMPUTE_FFT_INCLUDED_
2+
#define _NBL_GLSL_EXT_DEFAULT_COMPUTE_FFT_INCLUDED_
3+
4+
// WorkGroup Size
5+
6+
#ifndef USE_SSBO_FOR_INPUT
7+
#error "USE_SSBO_FOR_INPUT should be defined."
8+
#endif
9+
10+
#ifndef _NBL_GLSL_EXT_FFT_BLOCK_SIZE_X_DEFINED_
11+
#define _NBL_GLSL_EXT_FFT_BLOCK_SIZE_X_DEFINED_ 256
12+
#endif
13+
14+
#ifndef _NBL_GLSL_EXT_FFT_BLOCK_SIZE_Y_DEFINED_
15+
#define _NBL_GLSL_EXT_FFT_BLOCK_SIZE_Y_DEFINED_ 1
16+
#endif
17+
18+
#ifndef _NBL_GLSL_EXT_FFT_BLOCK_SIZE_Z_DEFINED_
19+
#define _NBL_GLSL_EXT_FFT_BLOCK_SIZE_Z_DEFINED_ 1
20+
#endif
21+
22+
#define _NBL_GLSL_WORKGROUP_SIZE_ (_NBL_GLSL_EXT_FFT_BLOCK_SIZE_X_DEFINED_*_NBL_GLSL_EXT_FFT_BLOCK_SIZE_Y_DEFINED_*_NBL_GLSL_EXT_FFT_BLOCK_SIZE_Z_DEFINED_)
23+
24+
layout(local_size_x=_NBL_GLSL_EXT_FFT_BLOCK_SIZE_X_DEFINED_, local_size_y=_NBL_GLSL_EXT_FFT_BLOCK_SIZE_Y_DEFINED_, local_size_z=_NBL_GLSL_EXT_FFT_BLOCK_SIZE_Z_DEFINED_) in;
25+
26+
27+
#define _NBL_GLSL_EXT_FFT_GET_DATA_DEFINED_
28+
#define _NBL_GLSL_EXT_FFT_SET_DATA_DEFINED_
29+
#define _NBL_GLSL_EXT_FFT_GET_PADDED_DATA_DEFINED_
30+
#include "nbl/builtin/glsl/ext/FFT/fft.glsl"
31+
32+
// Input Descriptor
33+
34+
struct nbl_glsl_ext_FFT_input_t
35+
{
36+
vec2 complex_value;
37+
};
38+
39+
#ifndef _NBL_GLSL_EXT_FFT_INPUT_SET_DEFINED_
40+
#define _NBL_GLSL_EXT_FFT_INPUT_SET_DEFINED_ 0
41+
#endif
42+
43+
#ifndef _NBL_GLSL_EXT_FFT_INPUT_BINDING_DEFINED_
44+
#define _NBL_GLSL_EXT_FFT_INPUT_BINDING_DEFINED_ 0
45+
#endif
46+
47+
#ifndef _NBL_GLSL_EXT_FFT_INPUT_DESCRIPTOR_DEFINED_
48+
49+
#if USE_SSBO_FOR_INPUT > 0
50+
#define _NBL_GLSL_EXT_FFT_INPUT_DESCRIPTOR_DEFINED_
51+
layout(set=_NBL_GLSL_EXT_FFT_INPUT_SET_DEFINED_, binding=_NBL_GLSL_EXT_FFT_INPUT_BINDING_DEFINED_) readonly restrict buffer InputBuffer
52+
{
53+
nbl_glsl_ext_FFT_input_t inData[];
54+
};
55+
#else
56+
layout(set=_NBL_GLSL_EXT_FFT_INPUT_SET_DEFINED_, binding=_NBL_GLSL_EXT_FFT_INPUT_BINDING_DEFINED_) uniform sampler2D inputImage;
57+
#endif
58+
59+
#endif
60+
61+
// Output Descriptor
62+
63+
struct nbl_glsl_ext_FFT_output_t
64+
{
65+
vec2 complex_value;
66+
};
67+
68+
69+
#ifndef _NBL_GLSL_EXT_FFT_OUTPUT_SET_DEFINED_
70+
#define _NBL_GLSL_EXT_FFT_OUTPUT_SET_DEFINED_ 0
71+
#endif
72+
73+
#ifndef _NBL_GLSL_EXT_FFT_OUTPUT_BINDING_DEFINED_
74+
#define _NBL_GLSL_EXT_FFT_OUTPUT_BINDING_DEFINED_ 1
75+
#endif
76+
77+
#ifndef _NBL_GLSL_EXT_FFT_OUTPUT_DESCRIPTOR_DEFINED_
78+
#define _NBL_GLSL_EXT_FFT_OUTPUT_DESCRIPTOR_DEFINED_
79+
layout(set=_NBL_GLSL_EXT_FFT_OUTPUT_SET_DEFINED_, binding=_NBL_GLSL_EXT_FFT_OUTPUT_BINDING_DEFINED_) restrict buffer OutputBuffer
80+
{
81+
nbl_glsl_ext_FFT_output_t outData[];
82+
};
83+
#endif
84+
85+
// Get/Set Data Function
86+
87+
nbl_glsl_complex nbl_glsl_ext_FFT_getData(in uvec3 coordinate, in uint channel)
88+
{
89+
vec2 retValue = vec2(0, 0);
90+
#if USE_SSBO_FOR_INPUT > 0
91+
uvec3 dimension = pc.dimension;
92+
uint index = channel * (dimension.x * dimension.y * dimension.z) + coordinate.z * (dimension.x * dimension.y) + coordinate.y * (dimension.x) + coordinate.x;
93+
retValue = inData[index].complex_value;
94+
#else
95+
vec4 texelValue= texelFetch(inputImage, ivec2(coordinate.xy), 0);
96+
retValue = vec2(texelValue[channel], 0.0f);
97+
#endif
98+
return retValue;
99+
}
100+
101+
void nbl_glsl_ext_FFT_setData(in uvec3 coordinate, in uint channel, in nbl_glsl_complex complex_value)
102+
{
103+
uvec3 dimension = pc.padded_dimension;
104+
uint index = channel * (dimension.x * dimension.y * dimension.z) + coordinate.z * (dimension.x * dimension.y) + coordinate.y * (dimension.x) + coordinate.x;
105+
outData[index].complex_value = complex_value;
106+
}
107+
108+
nbl_glsl_complex nbl_glsl_ext_FFT_getPaddedData(in uvec3 coordinate, in uint channel) {
109+
110+
uvec3 max_coord = pc.dimension - uvec3(1u);
111+
uvec3 clamped_coord = min(coordinate, max_coord);
112+
113+
bool is_out_of_range = any(bvec3(coordinate!=clamped_coord));
114+
115+
if (_NBL_GLSL_EXT_FFT_FILL_WITH_ZERO_ == pc.padding_type && is_out_of_range) {
116+
return nbl_glsl_complex(0, 0);
117+
}
118+
119+
return nbl_glsl_ext_FFT_getData(clamped_coord, channel);
120+
}
121+
122+
void main()
123+
{
124+
nbl_glsl_ext_FFT();
125+
}
126+
127+
#endif
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#version 430 core
2+
3+
#ifndef _NBL_GLSL_EXT_FFT_NORMALIZATION_INCLUDED_
4+
#define _NBL_GLSL_EXT_FFT_NORMALIZATION_INCLUDED_
5+
6+
layout(local_size_x=256, local_size_y=1, local_size_z=1) in;
7+
8+
#define complex_value vec2
9+
10+
layout(set=0, binding=0) restrict readonly buffer InBuffer
11+
{
12+
complex_value in_data[];
13+
};
14+
15+
layout(set=0, binding=1) restrict buffer OutBuffer
16+
{
17+
complex_value out_data[];
18+
};
19+
20+
void main()
21+
{
22+
float power = length(in_data[0]);
23+
vec2 normalized_data = in_data[gl_GlobalInvocationID.x] / power;
24+
out_data[gl_GlobalInvocationID.x] = normalized_data;
25+
}
26+
27+
#endif

include/nbl/ext/FFT/FFT.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,7 @@ class FFT : public core::TotalInterface
211211

212212
// Kernel Normalization
213213

214-
static core::smart_refctd_ptr<video::IGPUSpecializedShader> createKernelNormalizationShader(video::IVideoDriver* driver);
214+
static core::smart_refctd_ptr<video::IGPUSpecializedShader> createKernelNormalizationShader(video::IVideoDriver* driver, asset::IAssetManager* am);
215215

216216
static core::smart_refctd_ptr<video::IGPUPipelineLayout> getPipelineLayout_KernelNormalization(video::IVideoDriver* driver);
217217

src/nbl/builtin/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,9 @@ set(nbl_resources_to_embed
128128
"nbl/builtin/glsl/workgroup/vote.glsl"
129129
# ext shouldn't be built into the engine, but there's no harm including some non-dynamic GLSL source to make life easier
130130
#"nbl/builtin/glsl/ext/.glsl"
131+
"nbl/builtin/glsl/ext/FFT/default_compute_fft.comp"
131132
"nbl/builtin/glsl/ext/FFT/fft.glsl"
133+
"nbl/builtin/glsl/ext/FFT/normalization.comp"
132134
"nbl/builtin/glsl/ext/LumaMeter/common.glsl"
133135
"nbl/builtin/glsl/ext/LumaMeter/impl.glsl"
134136
"nbl/builtin/glsl/ext/ToneMapper/operators.glsl"

0 commit comments

Comments
 (0)