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

Add 'scale' setting that allows user to render shader at a fraction of the window resolution #128

Open
wants to merge 1 commit 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
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ The file can have the following contents: (all fields are optional)
"window":{ // default window size / state; if there's a setup dialog, it will override it
"width":1920,
"height":1080,
"scale":0.5, //render the shader at 1/2 the size of the window and scale up
"linearFilter":true, //use linear filtering when scaling up
"fullscreen":true,
},
"audio":{ // default audio device settings; if there's a setup dialog, it will override it
Expand Down
2 changes: 2 additions & 0 deletions src/Renderer.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ typedef struct
{
int nWidth;
int nHeight;
float fScale;
bool bLinearFilter;
RENDERER_WINDOWMODE windowMode;
bool bVsync;
} RENDERER_SETTINGS;
Expand Down
10 changes: 9 additions & 1 deletion src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,8 @@ int main(int argc, const char *argv[])
settings.sFFT.bUseRecordingDevice = options.get<jsonxx::Object>("audio").get<jsonxx::Boolean>("useInput");
}

settings.sRenderer.fScale = 1.0f;
settings.sRenderer.bLinearFilter = false;
settings.sRenderer.bVsync = false;
#ifdef _DEBUG
settings.sRenderer.nWidth = 1280;
Expand All @@ -124,9 +126,15 @@ int main(int argc, const char *argv[])
settings.sRenderer.nWidth = options.get<jsonxx::Object>("window").get<jsonxx::Number>("width");
if (options.get<jsonxx::Object>("window").has<jsonxx::Number>("height"))
settings.sRenderer.nHeight = options.get<jsonxx::Object>("window").get<jsonxx::Number>("height");
if (options.get<jsonxx::Object>("window").has<jsonxx::Number>("scale"))
settings.sRenderer.fScale = options.get<jsonxx::Object>("window").get<jsonxx::Number>("scale");
if (options.get<jsonxx::Object>("window").has<jsonxx::Boolean>("linearFilter"))
settings.sRenderer.bLinearFilter = options.get<jsonxx::Object>("window").get<jsonxx::Boolean>("linearFilter");
if (options.get<jsonxx::Object>("window").has<jsonxx::Boolean>("fullscreen"))
settings.sRenderer.windowMode = options.get<jsonxx::Object>("window").get<jsonxx::Boolean>("fullscreen") ? RENDERER_WINDOWMODE_FULLSCREEN : RENDERER_WINDOWMODE_WINDOWED;
}
if (settings.sRenderer.fScale < 0.0f) settings.sRenderer.fScale = 0.0f;
if (settings.sRenderer.fScale > 1.0f) settings.sRenderer.fScale = 1.0f;
if (!SetupDialog::Open( &settings ))
{
return -1;
Expand Down Expand Up @@ -479,7 +487,7 @@ int main(int argc, const char *argv[])
Renderer::keyEventBufferCount = 0;

Renderer::SetShaderConstant( "fGlobalTime", time );
Renderer::SetShaderConstant( "v2Resolution", settings.sRenderer.nWidth, settings.sRenderer.nHeight );
Renderer::SetShaderConstant( "v2Resolution", settings.sRenderer.nWidth*settings.sRenderer.fScale, settings.sRenderer.nHeight*settings.sRenderer.fScale );

for (std::map<int,std::string>::iterator it = midiRoutes.begin(); it != midiRoutes.end(); it++)
{
Expand Down
31 changes: 31 additions & 0 deletions src/platform_glfw/Renderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,8 @@ namespace Renderer
bool run = true;

GLuint theShader = 0;
GLuint glhShaderFB = 0;
GLuint glhShaderTex = 0;
GLuint glhVertexShader = 0;
GLuint glhFullscreenQuadVB = 0;
GLuint glhFullscreenQuadVA = 0;
Expand All @@ -175,6 +177,8 @@ namespace Renderer

int nWidth = 0;
int nHeight = 0;
float fScale = 1.0f;
bool bLinearFilter = false;

void MatrixOrthoOffCenterLH(float * pout, float l, float r, float b, float t, float zn, float zf)
{
Expand Down Expand Up @@ -227,6 +231,8 @@ namespace Renderer

nWidth = settings->nWidth;
nHeight = settings->nHeight;
fScale = settings->fScale;
bLinearFilter = settings->bLinearFilter;

glfwWindowHint(GLFW_RED_BITS, 8);
glfwWindowHint(GLFW_GREEN_BITS, 8);
Expand Down Expand Up @@ -306,6 +312,20 @@ namespace Renderer
nWidth = settings->nWidth = fbWidth;
nHeight = settings->nHeight = fbHeight;
printf("[GLFW] Obtained framebuffer size: %d x %d\n", fbWidth, fbHeight);

if (fScale != 1.0f) {
glGenTextures( 1, &glhShaderTex );
glBindTexture( GL_TEXTURE_2D, glhShaderTex );
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, GL_RGBA32F, nWidth*fScale, nHeight*fScale, 0, GL_RGBA, GL_FLOAT, NULL );
glBindTexture( GL_TEXTURE_2D, 0 );

glGenFramebuffers( 1, &glhShaderFB );
glBindFramebuffer( GL_FRAMEBUFFER, glhShaderFB );
glFramebufferTexture( GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, glhShaderTex, 0 );
glBindFramebuffer( GL_FRAMEBUFFER, 0 );
}

static float pFullscreenQuadVertices[] =
{
Expand Down Expand Up @@ -597,6 +617,10 @@ namespace Renderer
{
glBindVertexArray(glhFullscreenQuadVA);

if (fScale != 1.0f) {
glBindFramebuffer(GL_FRAMEBUFFER, glhShaderFB);
}

glUseProgram(theShader);

glBindBuffer( GL_ARRAY_BUFFER, glhFullscreenQuadVB );
Expand Down Expand Up @@ -625,6 +649,13 @@ namespace Renderer
glDisableVertexAttribArray( position );

glUseProgram(0);

if (fScale != 1.0f) {
glBindFramebuffer(GL_FRAMEBUFFER, 0);
glBindFramebuffer(GL_READ_FRAMEBUFFER, glhShaderFB);
glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0);
glBlitFramebuffer(0, 0, nWidth*fScale, nHeight*fScale, 0, 0, nWidth, nHeight, GL_COLOR_BUFFER_BIT, bLinearFilter ? GL_LINEAR : GL_NEAREST);
}
}

bool ReloadShader( const char * szShaderCode, int nShaderCodeSize, char * szErrorBuffer, int nErrorBufferSize )
Expand Down