From 193056493ac1e6b82899b73d1a564d5b3f9b03b6 Mon Sep 17 00:00:00 2001 From: degasus Date: Fri, 28 Dec 2012 00:52:44 +0100 Subject: [PATCH] also use shaderCaches in rasterFont --- .../Plugin_VideoOGL/Src/RasterFont.cpp | 28 +++++++++++++------ .../Plugins/Plugin_VideoOGL/Src/RasterFont.h | 5 +++- .../Plugin_VideoOGL/Src/VertexManager.cpp | 6 ++-- .../Plugin_VideoSoftware/Src/SWRenderer.cpp | 4 +-- 4 files changed, 29 insertions(+), 14 deletions(-) diff --git a/Source/Plugins/Plugin_VideoOGL/Src/RasterFont.cpp b/Source/Plugins/Plugin_VideoOGL/Src/RasterFont.cpp index f1cd9fd674..57ccdaad1b 100644 --- a/Source/Plugins/Plugin_VideoOGL/Src/RasterFont.cpp +++ b/Source/Plugins/Plugin_VideoOGL/Src/RasterFont.cpp @@ -18,8 +18,12 @@ #include "GLUtil.h" #include "RasterFont.h" +#include "PixelShaderCache.h" +#include "ProgramShaderCache.h" +#include "VertexShaderCache.h" // globals +namespace OGL { static const u32 char_width = 8; static const u32 char_height = 13; @@ -124,7 +128,7 @@ const u8 rasters[char_count][char_height] = { {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x06, 0x8f, 0xf1, 0x60, 0x00, 0x00, 0x00} }; -static const char *s_vertex_shader = +static const char *s_vertexShaderSrc = "attribute vec2 vertexPosition;\n" "attribute vec2 texturePosition;\n" "varying vec2 tpos;\n" @@ -133,7 +137,7 @@ static const char *s_vertex_shader = " tpos = texturePosition;\n" "}\n"; -static const char *s_fragment_shader = +static const char *s_fragmentShaderSrc = "#extension GL_ARB_texture_rectangle : enable\n" "uniform sampler2DRect textureSampler;\n" "uniform vec4 color;\n" @@ -142,6 +146,10 @@ static const char *s_fragment_shader = " gl_FragColor = texture2DRect(textureSampler,tpos) * color;\n" "}\n"; + +static FRAGMENTSHADER s_fragmentShader; +static VERTEXSHADER s_vertexShader; + RasterFont::RasterFont() { // generate the texture @@ -160,15 +168,16 @@ RasterFont::RasterFont() delete [] texture_data; // generate shader - shader_program = OpenGL_CompileProgram(s_vertex_shader, s_fragment_shader); + VertexShaderCache::CompileVertexShader(s_vertexShader, s_vertexShaderSrc); + PixelShaderCache::CompilePixelShader(s_fragmentShader, s_fragmentShaderSrc); + ProgramShaderCache::SetBothShaders(s_fragmentShader.glprogid, s_vertexShader.glprogid); + GLuint shader_program = ProgramShaderCache::GetCurrentProgram(); // bound uniforms - glUseProgram(shader_program); glUniform1i(glGetUniformLocation(shader_program,"textureSampler"), 0); // GL_TEXTURE0 uniform_color_id = glGetUniformLocation(shader_program,"color"); glUniform4f(uniform_color_id, 1, 1, 1, 1); cached_color = -1; - glUseProgram(0); // generate VBO & VAO glGenBuffers(1, &VBO); @@ -190,7 +199,8 @@ RasterFont::~RasterFont() glDeleteTextures(1, &texture); glDeleteBuffers(1, &VBO); glDeleteVertexArrays(1, &VAO); - glDeleteProgram(shader_program); + s_fragmentShader.Destroy(); + s_vertexShader.Destroy(); } void RasterFont::printMultilineText(const char *text, double start_x, double start_y, double z, int bbWidth, int bbHeight, u32 color) @@ -267,7 +277,7 @@ void RasterFont::printMultilineText(const char *text, double start_x, double sta // no printable char, so also nothing to do if(!usage) return; - glUseProgram(shader_program); + ProgramShaderCache::SetBothShaders(s_fragmentShader.glprogid, s_vertexShader.glprogid); if(color != cached_color) { glUniform4f(uniform_color_id, ((color>>16)&0xff)/255.f,((color>>8)&0xff)/255.f,((color>>0)&0xff)/255.f,((color>>24)&0xff)/255.f); @@ -281,6 +291,6 @@ void RasterFont::printMultilineText(const char *text, double start_x, double sta // TODO: this after merging with graphic_update glBindBuffer(GL_ARRAY_BUFFER, 0); glBindVertexArray(0); - - glUseProgram(0); } + +} \ No newline at end of file diff --git a/Source/Plugins/Plugin_VideoOGL/Src/RasterFont.h b/Source/Plugins/Plugin_VideoOGL/Src/RasterFont.h index fcbfdfff61..3715a91213 100644 --- a/Source/Plugins/Plugin_VideoOGL/Src/RasterFont.h +++ b/Source/Plugins/Plugin_VideoOGL/Src/RasterFont.h @@ -18,6 +18,8 @@ #ifndef _RASTERFONT_H_ #define _RASTERFONT_H_ +namespace OGL { + class RasterFont { public: RasterFont(); @@ -30,9 +32,10 @@ private: u32 VBO; u32 VAO; u32 texture; - u32 shader_program; u32 uniform_color_id; u32 cached_color; }; +} + #endif // _RASTERFONT_H_ diff --git a/Source/Plugins/Plugin_VideoOGL/Src/VertexManager.cpp b/Source/Plugins/Plugin_VideoOGL/Src/VertexManager.cpp index bbbae3388b..fe5f1d976a 100644 --- a/Source/Plugins/Plugin_VideoOGL/Src/VertexManager.cpp +++ b/Source/Plugins/Plugin_VideoOGL/Src/VertexManager.cpp @@ -202,7 +202,8 @@ void VertexManager::vFlush() } VERTEXSHADER* vs = VertexShaderCache::SetShader(g_nativeVertexFmt->m_components); - ProgramShaderCache::SetBothShaders(ps->glprogid, vs->glprogid); + if(ps && vs) + ProgramShaderCache::SetBothShaders(ps->glprogid, vs->glprogid); // set global constants VertexShaderManager::SetConstants(); @@ -219,7 +220,8 @@ void VertexManager::vFlush() if (useDstAlpha && !dualSourcePossible) { ps = PixelShaderCache::SetShader(DSTALPHA_ALPHA_PASS,g_nativeVertexFmt->m_components); - ProgramShaderCache::SetBothShaders(ps->glprogid, 0); + if(ps) + ProgramShaderCache::SetBothShaders(ps->glprogid, 0); if (!g_ActiveConfig.backend_info.bSupportsGLSLUBO) PixelShaderManager::SetConstants(); // Need to set these again, if we don't support UBO if (g_nativeVertexFmt) diff --git a/Source/Plugins/Plugin_VideoSoftware/Src/SWRenderer.cpp b/Source/Plugins/Plugin_VideoSoftware/Src/SWRenderer.cpp index e148906692..5a55516553 100644 --- a/Source/Plugins/Plugin_VideoSoftware/Src/SWRenderer.cpp +++ b/Source/Plugins/Plugin_VideoSoftware/Src/SWRenderer.cpp @@ -31,7 +31,7 @@ static GLuint program; // Rasterfont isn't compatible with GLES #ifndef USE_GLES -RasterFont* s_pfont = NULL; +OGL::RasterFont* s_pfont = NULL; #endif void SWRenderer::Init() @@ -85,7 +85,7 @@ void SWRenderer::Prepare() CreateShaders(); // TODO: Enable for GLES once RasterFont supports GLES #ifndef USE_GLES - s_pfont = new RasterFont(); + s_pfont = new OGL::RasterFont(); glEnable(GL_TEXTURE_RECTANGLE_ARB); #endif GL_REPORT_ERRORD();