Skip to content

Instantly share code, notes, and snippets.

@gnail737
Created April 6, 2014 16:46
Show Gist options
  • Select an option

  • Save gnail737/10008526 to your computer and use it in GitHub Desktop.

Select an option

Save gnail737/10008526 to your computer and use it in GitHub Desktop.
FFT Implementation on GPU
/*******************************************************************************************
* My work in progress Code for FFT on GPU
*
*******************************************************************************************/
//every shader needs folllowing lines
#define GL3ES_VERSION_SEPC "#version 300 es \n"
#define FP_PREC_SPEC "precision mediump float; \n"
#define FS_LAYOUT_LINE "layout(location = 0) out vec4 fragColor; \n"
#define L_(STMT) " " #STMT " \n"
static const char VERTEX_SHADER[] =
GL3ES_VERSION_SEPC
L_(layout(location = 0) in vec2 vPosition; )
L_(void main() { )
L_( gl_Position = vec4(vPosition, 0.0, 1.0); )
L_(} );
static const char FRAGMENT_SHADER[] =
GL3ES_VERSION_SEPC
FP_PREC_SPEC
L_(layout(location = 0) out vec4 fragColor; )
L_(void main() { )
L_( fragColor = vec4(0.95, 0.95, 0, 0); )
L_(} );
static const char VERTEX_FILL_SHADER[] =
GL3ES_VERSION_SEPC
L_(layout(location = 0) in vec4 vPosition; )
L_(out vec2 texCoord; )
L_(void main() { )
L_( gl_Position = vec4(vPosition.xy, 0.0, 1.0); )
L_( texCoord = vPosition.zw; )
L_(} );
static const char FRAGMENT_FILL_SHADER[] =
GL3ES_VERSION_SEPC
FP_PREC_SPEC
FS_LAYOUT_LINE
L_(in vec2 texCoord; )
L_(uniform sampler2D floatArray; )
L_(void main() { )
L_( ivec2 lookupInd = ivec2(gl_FragCoord.xy); )
L_( fragColor = vec4(texelFetch(floatArray, lookupInd, 0).rg, 0.0, 1.0); )
L_(} );
/* for simplicity first assume we only use 1D Texture arrays, so GL_FragCoord only take care one dimensional data */
static const char DECIMATION_IN_TIME_SHADER[] =
GL3ES_VERSION_SEPC
FP_PREC_SPEC
FS_LAYOUT_LINE
L_(uniform sampler2D floatArray; )
L_(uniform int butterflyIndex; )
L_(const float PI = 3.141592654; )
L_(vec2 mult_twiddleFactor(vec2 in, float kn, float sum) { )
L_( vec2 out = vec(0); )
L_( out.x = in.x*cos(-2.0*PI*kn/sum) - in.y*sin(-2.0*PI*kn/sum); )
L_( out.y = in.y*cos(-2.0*PI*kn/sum) + in.x*sin(-2.0*PI*kn/sum); )
L_( return out; )
L_(} )
L_(void main() { )
L_( ivec2 lookupInd = ivec2(gl_FragCoord.xy); )
L_( ivec2 lookupInd_c = lookupInd ^ butterflyIndex; )
L_( int signess = -((lookupInd.x & butterflyIndex) * 2 - 1); )
L_( vec2 complex_pair_1 = float(signess) * texelFetch(floatArray, lookupInd, 0).rg; )
L_( vec2 complex_pair_2 = texelFetch(floatArray, lookupInd_c, 0).rg; )
L_( complex_pair_1 += complex_pair_2; )
L_( if (((butterflyIndex << 1) & lookupInd.x) != 0) { )
L_( complex_pair_1 = mult_twiddleFactor(complex_pair_1, float(lookupInd.x&((butterflyIndex<<1)-1)), butterflyIndex<<2); )
L_( })
L_( fragColor = vec4(complex_pair_1, 0.0, 1.0); )
L_(});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment