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

Use render-to-texture for texPreviousFrame (makes it 32bit instead of 8bit colors) #173

Open
wants to merge 5 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
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 );
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(oops, this function was added for a different change. will come with another PR)


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
21 changes: 17 additions & 4 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,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 @@ -549,16 +550,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 +645,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
77 changes: 77 additions & 0 deletions src/platform_glfw/Renderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -176,10 +176,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 +338,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 +655,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 +719,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 +761,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 @@ -957,6 +1014,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