also use shaderCaches in rasterFont

This commit is contained in:
degasus
2012-12-28 00:52:44 +01:00
parent 316a33d1e6
commit 193056493a
4 changed files with 29 additions and 14 deletions

View File

@ -18,8 +18,12 @@
#include "GLUtil.h" #include "GLUtil.h"
#include "RasterFont.h" #include "RasterFont.h"
#include "PixelShaderCache.h"
#include "ProgramShaderCache.h"
#include "VertexShaderCache.h"
// globals // globals
namespace OGL {
static const u32 char_width = 8; static const u32 char_width = 8;
static const u32 char_height = 13; 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} {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 vertexPosition;\n"
"attribute vec2 texturePosition;\n" "attribute vec2 texturePosition;\n"
"varying vec2 tpos;\n" "varying vec2 tpos;\n"
@ -133,7 +137,7 @@ static const char *s_vertex_shader =
" tpos = texturePosition;\n" " tpos = texturePosition;\n"
"}\n"; "}\n";
static const char *s_fragment_shader = static const char *s_fragmentShaderSrc =
"#extension GL_ARB_texture_rectangle : enable\n" "#extension GL_ARB_texture_rectangle : enable\n"
"uniform sampler2DRect textureSampler;\n" "uniform sampler2DRect textureSampler;\n"
"uniform vec4 color;\n" "uniform vec4 color;\n"
@ -142,6 +146,10 @@ static const char *s_fragment_shader =
" gl_FragColor = texture2DRect(textureSampler,tpos) * color;\n" " gl_FragColor = texture2DRect(textureSampler,tpos) * color;\n"
"}\n"; "}\n";
static FRAGMENTSHADER s_fragmentShader;
static VERTEXSHADER s_vertexShader;
RasterFont::RasterFont() RasterFont::RasterFont()
{ {
// generate the texture // generate the texture
@ -160,15 +168,16 @@ RasterFont::RasterFont()
delete [] texture_data; delete [] texture_data;
// generate shader // 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 // bound uniforms
glUseProgram(shader_program);
glUniform1i(glGetUniformLocation(shader_program,"textureSampler"), 0); // GL_TEXTURE0 glUniform1i(glGetUniformLocation(shader_program,"textureSampler"), 0); // GL_TEXTURE0
uniform_color_id = glGetUniformLocation(shader_program,"color"); uniform_color_id = glGetUniformLocation(shader_program,"color");
glUniform4f(uniform_color_id, 1, 1, 1, 1); glUniform4f(uniform_color_id, 1, 1, 1, 1);
cached_color = -1; cached_color = -1;
glUseProgram(0);
// generate VBO & VAO // generate VBO & VAO
glGenBuffers(1, &VBO); glGenBuffers(1, &VBO);
@ -190,7 +199,8 @@ RasterFont::~RasterFont()
glDeleteTextures(1, &texture); glDeleteTextures(1, &texture);
glDeleteBuffers(1, &VBO); glDeleteBuffers(1, &VBO);
glDeleteVertexArrays(1, &VAO); 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) 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 // no printable char, so also nothing to do
if(!usage) return; if(!usage) return;
glUseProgram(shader_program); ProgramShaderCache::SetBothShaders(s_fragmentShader.glprogid, s_vertexShader.glprogid);
if(color != cached_color) { 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); 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 // TODO: this after merging with graphic_update
glBindBuffer(GL_ARRAY_BUFFER, 0); glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindVertexArray(0); glBindVertexArray(0);
glUseProgram(0);
} }
}

View File

@ -18,6 +18,8 @@
#ifndef _RASTERFONT_H_ #ifndef _RASTERFONT_H_
#define _RASTERFONT_H_ #define _RASTERFONT_H_
namespace OGL {
class RasterFont { class RasterFont {
public: public:
RasterFont(); RasterFont();
@ -30,9 +32,10 @@ private:
u32 VBO; u32 VBO;
u32 VAO; u32 VAO;
u32 texture; u32 texture;
u32 shader_program;
u32 uniform_color_id; u32 uniform_color_id;
u32 cached_color; u32 cached_color;
}; };
}
#endif // _RASTERFONT_H_ #endif // _RASTERFONT_H_

View File

@ -202,7 +202,8 @@ void VertexManager::vFlush()
} }
VERTEXSHADER* vs = VertexShaderCache::SetShader(g_nativeVertexFmt->m_components); 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 // set global constants
VertexShaderManager::SetConstants(); VertexShaderManager::SetConstants();
@ -219,7 +220,8 @@ void VertexManager::vFlush()
if (useDstAlpha && !dualSourcePossible) if (useDstAlpha && !dualSourcePossible)
{ {
ps = PixelShaderCache::SetShader(DSTALPHA_ALPHA_PASS,g_nativeVertexFmt->m_components); 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) if (!g_ActiveConfig.backend_info.bSupportsGLSLUBO)
PixelShaderManager::SetConstants(); // Need to set these again, if we don't support UBO PixelShaderManager::SetConstants(); // Need to set these again, if we don't support UBO
if (g_nativeVertexFmt) if (g_nativeVertexFmt)

View File

@ -31,7 +31,7 @@ static GLuint program;
// Rasterfont isn't compatible with GLES // Rasterfont isn't compatible with GLES
#ifndef USE_GLES #ifndef USE_GLES
RasterFont* s_pfont = NULL; OGL::RasterFont* s_pfont = NULL;
#endif #endif
void SWRenderer::Init() void SWRenderer::Init()
@ -85,7 +85,7 @@ void SWRenderer::Prepare()
CreateShaders(); CreateShaders();
// TODO: Enable for GLES once RasterFont supports GLES // TODO: Enable for GLES once RasterFont supports GLES
#ifndef USE_GLES #ifndef USE_GLES
s_pfont = new RasterFont(); s_pfont = new OGL::RasterFont();
glEnable(GL_TEXTURE_RECTANGLE_ARB); glEnable(GL_TEXTURE_RECTANGLE_ARB);
#endif #endif
GL_REPORT_ERRORD(); GL_REPORT_ERRORD();