Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

option to use a uniform float array for midi inputs #174

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ The file can have the following contents: (all fields are optional)
"midi":{ // the keys below will become the shader variable names, the values are the CC numbers
"fMidiKnob": 16, // e.g. this would be CC#16, i.e. by default the leftmost knob on a nanoKONTROL 2
},
"useMidiArray": false, // alterntaive to the above. if true, all 127 midi values will be provided in a single uniform float array (uniform float[127] fMidi;)
// this section is if you want to enable NDI streaming; otherwise just ignore it
"ndi":{
"enabled": true,
Expand Down
7 changes: 7 additions & 0 deletions src/Renderer.h
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ void RenderFullscreenQuad();
bool ReloadShader( const char * szShaderCode, int nShaderCodeSize, char * szErrorBuffer, int nErrorBufferSize );
void SetShaderConstant( const char * szConstName, float x );
void SetShaderConstant( const char * szConstName, float x, float y );
void SetShaderConstant( const char * szConstName, unsigned int num, float* data );

void StartTextRendering();
void SetTextRenderingViewport( Scintilla::PRectangle rect );
Expand All @@ -106,6 +107,7 @@ bool GrabFrame( void * pPixelBuffer ); // input buffer must be able to hold w *
void Close();

Texture * CreateRGBA8Texture();
Texture * CreateBackbufferTexture();
Texture * CreateRGBA8TextureFromFile( const char * szFilename );
Texture * CreateA8TextureFromData( int w, int h, const unsigned char * data );
Texture * Create1DR32Texture( int w );
Expand All @@ -114,6 +116,11 @@ void SetShaderTexture( const char * szTextureName, Texture * tex );
void BindTexture( Texture * tex ); // temporary function until all the quad rendering is moved to the renderer
void ReleaseTexture( Texture * tex );

void BindFramebuffer();
void UnbindFramebuffer();
void AttachBackbufferTexture( Texture * tex );
void BlitFramebufferToScreen();

void CopyBackbufferToTexture( Texture * tex );

void RenderQuad( const Vertex & a, const Vertex & b, const Vertex & c, const Vertex & d );
Expand Down
39 changes: 35 additions & 4 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@ int main( int argc, const char * argv[] )

std::map<std::string, Renderer::Texture *> textures;
std::map<int, std::string> midiRoutes;
bool useMidiArray;

const char * szDefaultFontPath = Misc::GetDefaultFontPath();

Expand Down Expand Up @@ -301,6 +302,10 @@ int main( int argc, const char * argv[] )
midiRoutes[ it->second->number_value_ ] = it->first;
}
}

if ( options.has<jsonxx::Boolean>( "useMidiArray" ) )
useMidiArray = options.get<jsonxx::Boolean>( "useMidiArray" );

if ( options.has<jsonxx::String>( "postExitCmd" ) )
{
sPostExitCmd = options.get<jsonxx::String>( "postExitCmd" );
Expand All @@ -318,7 +323,8 @@ int main( int argc, const char * argv[] )
return 0;
}

Renderer::Texture * texPreviousFrame = Renderer::CreateRGBA8Texture();
Renderer::Texture * texFrontBuffer = Renderer::CreateBackbufferTexture();
Renderer::Texture * texBackBuffer = Renderer::CreateBackbufferTexture();
Renderer::Texture * texFFT = Renderer::Create1DR32Texture( FFT_SIZE );
Renderer::Texture * texFFTSmoothed = Renderer::Create1DR32Texture( FFT_SIZE );
Renderer::Texture * texFFTIntegrated = Renderer::Create1DR32Texture( FFT_SIZE );
Expand Down Expand Up @@ -359,6 +365,12 @@ int main( int argc, const char * argv[] )
tokens.push_back( it->second );
ReplaceTokens( sDefShader, "{%midi:begin%}", "{%midi:name%}", "{%midi:end%}", tokens );

tokens.clear();
if (useMidiArray) {
tokens.push_back("fMidi");
}
ReplaceTokens( sDefShader, "{%midiArray:begin%}", "{%midiArray:name%}", "{%midiArray:end%}", tokens );

strncpy( szShader, sDefShader.c_str(), 65535 );
if ( !Renderer::ReloadShader( szShader, (int) strlen( szShader ), szError, 4096 ) )
{
Expand Down Expand Up @@ -524,6 +536,13 @@ int main( int argc, const char * argv[] )
Renderer::SetShaderConstant( it->second.c_str(), MIDI::GetCCValue( it->first ) );
}

if (useMidiArray) {
static std::vector<float> values(127);
for (unsigned char cc = 0; cc < 127; ++cc) {
values[cc] = MIDI::GetCCValue( cc );
}
Renderer::SetShaderConstant( "fMidi", 127, values.data());
}

if ( FFT::GetFFT( fftData ) )
{
Expand All @@ -549,16 +568,27 @@ int main( int argc, const char * argv[] )
Renderer::SetShaderTexture( "texFFT", texFFT );
Renderer::SetShaderTexture( "texFFTSmoothed", texFFTSmoothed );
Renderer::SetShaderTexture( "texFFTIntegrated", texFFTIntegrated );
Renderer::SetShaderTexture( "texPreviousFrame", texPreviousFrame );
Renderer::SetShaderTexture( "texPreviousFrame", texFrontBuffer );

for ( std::map<std::string, Renderer::Texture *>::iterator it = textures.begin(); it != textures.end(); it++ )
{
Renderer::SetShaderTexture( it->first.c_str(), it->second );
}


Renderer::AttachBackbufferTexture(texBackBuffer);
Renderer::BindFramebuffer();

Renderer::RenderFullscreenQuad();

Renderer::UnbindFramebuffer();

Renderer::BlitFramebufferToScreen();

Renderer::CopyBackbufferToTexture( texPreviousFrame );
// swap front and back buffer:
Renderer::Texture * temp = texBackBuffer;
texBackBuffer = texFrontBuffer;
texFrontBuffer = temp;

Renderer::StartTextRendering();

Expand Down Expand Up @@ -633,7 +663,8 @@ int main( int argc, const char * argv[] )
MIDI::Close();
FFT::Close();

Renderer::ReleaseTexture( texPreviousFrame );
Renderer::ReleaseTexture( texBackBuffer );
Renderer::ReleaseTexture( texFrontBuffer );
Renderer::ReleaseTexture( texFFT );
Renderer::ReleaseTexture( texFFTSmoothed );
for ( std::map<std::string, Renderer::Texture *>::iterator it = textures.begin(); it != textures.end(); it++ )
Expand Down
85 changes: 83 additions & 2 deletions src/platform_glfw/Renderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,9 @@ const char szDefaultShader[ 65536 ] =
"{%midi:begin%}" // leave off \n here
"uniform float {%midi:name%};\n"
"{%midi:end%}" // leave off \n here
"{%midiArray:begin%}" // leave off \n here
"uniform float[127] {%midiArray:name%};\n"
"{%midiArray:end%}" // leave off \n here
"\n"
"in vec2 out_texcoord;\n"
"layout(location = 0) out vec4 out_color; // out_color must be written in order to see anything\n"
Expand Down Expand Up @@ -176,10 +179,20 @@ GLuint glhFullscreenQuadVA = 0;
GLuint glhGUIVB = 0;
GLuint glhGUIVA = 0;
GLuint glhGUIProgram = 0;
GLuint glhFramebuffer = 0;

int nWidth = 0;
int nHeight = 0;

void checkError()
{
GLenum err;
while((err = glGetError()) != GL_NO_ERROR)
{
printf( "GL error %d\n", err );
}
}

void MatrixOrthoOffCenterLH( float * pout, float l, float r, float b, float t, float zn, float zf )
{
memset( pout, 0, sizeof( float ) * 4 * 4 );
Expand Down Expand Up @@ -328,6 +341,8 @@ bool Open( Renderer::Settings * settings )

glGenVertexArrays( 1, &glhFullscreenQuadVA );

glGenFramebuffers(1, &glhFramebuffer);

glhVertexShader = glCreateShader( GL_VERTEX_SHADER );

const char * szVertexShader =
Expand Down Expand Up @@ -643,6 +658,14 @@ void RenderFullscreenQuad()
glUseProgram( 0 );
}

void BlitFramebufferToScreen()
{
glBindFramebuffer(GL_READ_FRAMEBUFFER, glhFramebuffer);
glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0);
glBlitFramebuffer(0, 0, nWidth, nHeight, 0, 0, nWidth, nHeight, GL_COLOR_BUFFER_BIT, GL_NEAREST);
checkError();
}

bool ReloadShader( const char * szShaderCode, int nShaderCodeSize, char * szErrorBuffer, int nErrorBufferSize )
{
GLuint prg = glCreateProgram();
Expand Down Expand Up @@ -699,6 +722,15 @@ void SetShaderConstant( const char * szConstName, float x, float y )
}
}

void SetShaderConstant( const char * szConstName, unsigned int num, float* data )
{
GLint location = glGetUniformLocation( theShader, szConstName );
if ( location != -1 )
{
glProgramUniform1fv( theShader, location, num, data);
}
}

struct GLTexture : public Texture
{
GLuint ID;
Expand Down Expand Up @@ -732,6 +764,34 @@ Texture * CreateRGBA8Texture()
return tex;
}

Texture * CreateBackbufferTexture()
{
void * data = NULL;
GLenum internalFormat = GL_RGBA32F;
GLenum srcFormat = GL_RGBA;
GLenum format = GL_FLOAT;

GLuint glTexId = 0;
glGenTextures( 1, &glTexId );
glBindTexture( GL_TEXTURE_2D, glTexId );

glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT );
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT );
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );

glTexImage2D( GL_TEXTURE_2D, 0, internalFormat, nWidth, nHeight, 0, srcFormat, format, nullptr );

GLTexture * tex = new GLTexture();
tex->width = nWidth;
tex->height = nHeight;
tex->ID = glTexId;
tex->type = TEXTURETYPE_2D;
tex->unit = textureUnit++;
checkError();
return tex;
}

Texture * CreateRGBA8TextureFromFile( const char * szFilename )
{
int comp = 0;
Expand Down Expand Up @@ -782,14 +842,15 @@ Texture * Create1DR32Texture( int w )
glBindTexture( GL_TEXTURE_1D, glTexId );

glTexParameteri( GL_TEXTURE_1D, GL_TEXTURE_WRAP_S, GL_REPEAT );
glTexParameteri( GL_TEXTURE_1D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
glTexParameteri( GL_TEXTURE_1D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR );
glTexParameteri( GL_TEXTURE_1D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );

float * data = new float[ w ];
for ( int i = 0; i < w; ++i )
data[ i ] = 0.0f;

glTexImage1D( GL_TEXTURE_1D, 0, GL_R32F, w, 0, GL_RED, GL_FLOAT, data );
glGenerateMipmap(GL_TEXTURE_1D);

delete[] data;

Expand Down Expand Up @@ -827,7 +888,7 @@ bool UpdateR32Texture( Texture * tex, float * data )
glActiveTexture( GL_TEXTURE0 + ( (GLTexture *) tex )->unit );
glBindTexture( GL_TEXTURE_1D, ( (GLTexture *) tex )->ID );
glTexSubImage1D( GL_TEXTURE_1D, 0, 0, tex->width, GL_RED, GL_FLOAT, data );

glGenerateMipmap(GL_TEXTURE_1D);
return true;
}

Expand Down Expand Up @@ -957,6 +1018,26 @@ void BindTexture( Texture * tex )
}
}

void BindFramebuffer()
{
glBindFramebuffer(GL_FRAMEBUFFER, glhFramebuffer);
checkError();
}

void UnbindFramebuffer()
{
glBindFramebuffer(GL_FRAMEBUFFER, 0);
checkError();
}

void AttachBackbufferTexture( Texture * tex )
{
BindFramebuffer();
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, ( (GLTexture *) tex )->ID, 0);
checkError();
UnbindFramebuffer();
}

void RenderQuad( const Vertex & a, const Vertex & b, const Vertex & c, const Vertex & d )
{
if ( !lastModeIsQuad )
Expand Down