Add in UBOs, doesn't work yet. Still debugging here.

This commit is contained in:
Ryan Houdek 2011-12-09 17:30:05 -06:00 committed by Sonicadvance1
parent 5f0f26ef35
commit 4a84c6f742
12 changed files with 138 additions and 49 deletions

View File

@ -619,21 +619,24 @@ const char *GeneratePixelShaderCode(DSTALPHA_MODE dstAlphaMode, API_TYPE ApiType
}
WRITE(p, "\n");
WRITE(p, "%suniform float4 "I_COLORS"[4] %s;\n", WriteLocation(ApiType, C_COLORS), WriteRegister(ApiType, "c", C_COLORS));
WRITE(p, "%suniform float4 "I_KCOLORS"[4] %s;\n", WriteLocation(ApiType, C_KCOLORS), WriteRegister(ApiType, "c", C_KCOLORS));
WRITE(p, "%suniform float4 "I_ALPHA"[1] %s;\n", WriteLocation(ApiType, C_ALPHA), WriteRegister(ApiType, "c", C_ALPHA));
WRITE(p, "%suniform float4 "I_TEXDIMS"[8] %s;\n", WriteLocation(ApiType, C_TEXDIMS), WriteRegister(ApiType, "c", C_TEXDIMS));
WRITE(p, "%suniform float4 "I_ZBIAS"[2] %s;\n", WriteLocation(ApiType, C_ZBIAS), WriteRegister(ApiType, "c", C_ZBIAS));
WRITE(p, "%suniform float4 "I_INDTEXSCALE"[2] %s;\n", WriteLocation(ApiType, C_INDTEXSCALE), WriteRegister(ApiType, "c", C_INDTEXSCALE));
WRITE(p, "%suniform float4 "I_INDTEXMTX"[6] %s;\n", WriteLocation(ApiType, C_INDTEXMTX), WriteRegister(ApiType, "c", C_INDTEXMTX));
WRITE(p, "%suniform float4 "I_FOG"[3] %s;\n", WriteLocation(ApiType, C_FOG), WriteRegister(ApiType, "c", C_FOG));
if(g_ActiveConfig.bEnablePixelLighting && g_ActiveConfig.backend_info.bSupportsPixelLighting)
{
WRITE(p, "%suniform float4 "I_PLIGHTS"[40] %s;\n", WriteLocation(ApiType, C_PLIGHTS), WriteRegister(ApiType, "c", C_PLIGHTS));
WRITE(p, "%suniform float4 "I_PMATERIALS"[4] %s;\n", WriteLocation(ApiType, C_PMATERIALS), WriteRegister(ApiType, "c", C_PMATERIALS));
}
if(ApiType == API_GLSL && g_ActiveConfig.backend_info.bSupportsGLSLUBO)
WRITE(p, "layout(std140, binding = 0) uniform PSBlock {\n");
WRITE(p, "%suniform float4 "I_COLORS"[4] %s;\n", WriteLocation(ApiType, C_COLORS), WriteRegister(ApiType, "c", C_COLORS));
WRITE(p, "%suniform float4 "I_KCOLORS"[4] %s;\n", WriteLocation(ApiType, C_KCOLORS), WriteRegister(ApiType, "c", C_KCOLORS));
WRITE(p, "%suniform float4 "I_ALPHA"[1] %s;\n", WriteLocation(ApiType, C_ALPHA), WriteRegister(ApiType, "c", C_ALPHA));
WRITE(p, "%suniform float4 "I_TEXDIMS"[8] %s;\n", WriteLocation(ApiType, C_TEXDIMS), WriteRegister(ApiType, "c", C_TEXDIMS));
WRITE(p, "%suniform float4 "I_ZBIAS"[2] %s;\n", WriteLocation(ApiType, C_ZBIAS), WriteRegister(ApiType, "c", C_ZBIAS));
WRITE(p, "%suniform float4 "I_INDTEXSCALE"[2] %s;\n", WriteLocation(ApiType, C_INDTEXSCALE), WriteRegister(ApiType, "c", C_INDTEXSCALE));
WRITE(p, "%suniform float4 "I_INDTEXMTX"[6] %s;\n", WriteLocation(ApiType, C_INDTEXMTX), WriteRegister(ApiType, "c", C_INDTEXMTX));
WRITE(p, "%suniform float4 "I_FOG"[3] %s;\n", WriteLocation(ApiType, C_FOG), WriteRegister(ApiType, "c", C_FOG));
// Compiler will optimize these out by itself.
WRITE(p, "%suniform float4 "I_PLIGHTS"[40] %s;\n", WriteLocation(ApiType, C_PLIGHTS), WriteRegister(ApiType, "c", C_PLIGHTS));
WRITE(p, "%suniform float4 "I_PMATERIALS"[4] %s;\n", WriteLocation(ApiType, C_PMATERIALS), WriteRegister(ApiType, "c", C_PMATERIALS));
if(ApiType == API_GLSL && g_ActiveConfig.backend_info.bSupportsGLSLUBO)
WRITE(p, "};\n");
if(ApiType != API_GLSL)
{

View File

@ -85,7 +85,7 @@ void PixelShaderManager::Shutdown()
void PixelShaderManager::SetConstants()
{
if (g_ActiveConfig.bUseGLSL)
if (g_ActiveConfig.bUseGLSL && !g_ActiveConfig.backend_info.bSupportsGLSLUBO)
Dirty();
for (int i = 0; i < 2; ++i)
{

View File

@ -214,16 +214,23 @@ const char *GenerateVertexShaderCode(u32 components, API_TYPE ApiType)
WRITE(p, "#define saturate(x) clamp(x, 0.0f, 1.0f)\n");
WRITE(p, "#define lerp(x, y, z) mix(x, y, z)\n");
}
// uniforms
WRITE(p, "%suniform float4 "I_POSNORMALMATRIX"[6] %s;\n", WriteLocation(ApiType, C_POSNORMALMATRIX), WriteRegister(ApiType, "c", C_POSNORMALMATRIX));
WRITE(p, "%suniform float4 "I_PROJECTION"[4] %s;\n", WriteLocation(ApiType, C_PROJECTION), WriteRegister(ApiType, "c", C_PROJECTION));
WRITE(p, "%suniform float4 "I_MATERIALS"[4] %s;\n", WriteLocation(ApiType, C_MATERIALS), WriteRegister(ApiType, "c", C_MATERIALS));
WRITE(p, "%suniform float4 "I_LIGHTS"[40] %s;\n", WriteLocation(ApiType, C_LIGHTS), WriteRegister(ApiType, "c", C_LIGHTS));
WRITE(p, "%suniform float4 "I_TEXMATRICES"[24] %s;\n", WriteLocation(ApiType, C_TEXMATRICES), WriteRegister(ApiType, "c", C_TEXMATRICES)); // also using tex matrices
WRITE(p, "%suniform float4 "I_TRANSFORMMATRICES"[64] %s;\n", WriteLocation(ApiType, C_TRANSFORMMATRICES), WriteRegister(ApiType, "c", C_TRANSFORMMATRICES));
WRITE(p, "%suniform float4 "I_NORMALMATRICES"[32] %s;\n", WriteLocation(ApiType, C_NORMALMATRICES), WriteRegister(ApiType, "c", C_NORMALMATRICES));
WRITE(p, "%suniform float4 "I_POSTTRANSFORMMATRICES"[64] %s;\n", WriteLocation(ApiType, C_POSTTRANSFORMMATRICES), WriteRegister(ApiType, "c", C_POSTTRANSFORMMATRICES));
WRITE(p, "%suniform float4 "I_DEPTHPARAMS" %s;\n", WriteLocation(ApiType, C_DEPTHPARAMS), WriteRegister(ApiType, "c", C_DEPTHPARAMS));
if(ApiType == API_GLSL && g_ActiveConfig.backend_info.bSupportsGLSLUBO)
WRITE(p, "layout(std140, binding = 1) uniform VSBlock {\n");
WRITE(p, "%suniform float4 "I_POSNORMALMATRIX"[6] %s;\n", WriteLocation(ApiType, C_POSNORMALMATRIX), WriteRegister(ApiType, "c", C_POSNORMALMATRIX));
WRITE(p, "%suniform float4 "I_PROJECTION"[4] %s;\n", WriteLocation(ApiType, C_PROJECTION), WriteRegister(ApiType, "c", C_PROJECTION));
WRITE(p, "%suniform float4 "I_MATERIALS"[4] %s;\n", WriteLocation(ApiType, C_MATERIALS), WriteRegister(ApiType, "c", C_MATERIALS));
WRITE(p, "%suniform float4 "I_LIGHTS"[40] %s;\n", WriteLocation(ApiType, C_LIGHTS), WriteRegister(ApiType, "c", C_LIGHTS));
WRITE(p, "%suniform float4 "I_TEXMATRICES"[24] %s;\n", WriteLocation(ApiType, C_TEXMATRICES), WriteRegister(ApiType, "c", C_TEXMATRICES)); // also using tex matrices
WRITE(p, "%suniform float4 "I_TRANSFORMMATRICES"[64] %s;\n", WriteLocation(ApiType, C_TRANSFORMMATRICES), WriteRegister(ApiType, "c", C_TRANSFORMMATRICES));
WRITE(p, "%suniform float4 "I_NORMALMATRICES"[32] %s;\n", WriteLocation(ApiType, C_NORMALMATRICES), WriteRegister(ApiType, "c", C_NORMALMATRICES));
WRITE(p, "%suniform float4 "I_POSTTRANSFORMMATRICES"[64] %s;\n", WriteLocation(ApiType, C_POSTTRANSFORMMATRICES), WriteRegister(ApiType, "c", C_POSTTRANSFORMMATRICES));
WRITE(p, "%suniform float4 "I_DEPTHPARAMS" %s;\n", WriteLocation(ApiType, C_DEPTHPARAMS), WriteRegister(ApiType, "c", C_DEPTHPARAMS));
if(ApiType == API_GLSL && g_ActiveConfig.backend_info.bSupportsGLSLUBO)
WRITE(p, "};\n");
p = GenerateVSOutputStruct(p, components, ApiType);

View File

@ -46,7 +46,7 @@
#define C_NORMALMATRICES (C_TRANSFORMMATRICES + 64)
#define C_POSTTRANSFORMMATRICES (C_NORMALMATRICES + 32)
#define C_DEPTHPARAMS (C_POSTTRANSFORMMATRICES + 64)
#define C_VENVCONST_END (C_DEPTHPARAMS + 4)
#define C_VENVCONST_END (C_DEPTHPARAMS + 1)
const s_svar VSVar_Loc[] = { {I_POSNORMALMATRIX, C_POSNORMALMATRIX, 6 },
{I_PROJECTION , C_PROJECTION, 4 },
{I_MATERIALS, C_MATERIALS, 4 },

View File

@ -184,7 +184,7 @@ void VertexShaderManager::Dirty()
// TODO: A cleaner way to control the matricies without making a mess in the parameters field
void VertexShaderManager::SetConstants()
{
if (g_ActiveConfig.bUseGLSL)
if (g_ActiveConfig.bUseGLSL && !g_ActiveConfig.backend_info.bSupportsGLSLUBO)
Dirty();
if (nTransformMatricesChanged[0] >= 0)
{

View File

@ -163,9 +163,11 @@ struct VideoConfig
bool bSupportsDualSourceBlend; // only supported by D3D11 and OpenGL
bool bSupportsFormatReinterpretation;
bool bSupportsPixelLighting;
bool bSupportsGLSL;
bool bSupportsGLSLBinding;
bool bSupportsGLSLLocation;
bool bSupportsGLSLUBO;
} backend_info;
};

View File

@ -82,8 +82,13 @@ void PixelShaderCache::Init()
if (strstr((const char*)glGetString(GL_EXTENSIONS), "GL_ARB_shading_language_420pack") != NULL)
g_Config.backend_info.bSupportsGLSLBinding = true;
// This bit doesn't quite work yet, always set to false
// TODO: Probably just drop this entirely in favour of UBOS
//if (strstr((const char*)glGetString(GL_EXTENSIONS), "GL_ARB_separate_shader_objects") != NULL)
g_Config.backend_info.bSupportsGLSLLocation = false;
if (strstr((const char*)glGetString(GL_EXTENSIONS), "GL_ARB_uniform_buffer_object") != NULL)
g_Config.backend_info.bSupportsGLSLUBO = true;
UpdateActiveConfig();
}
else
@ -466,11 +471,11 @@ bool CompileGLSLPixelShader(FRAGMENTSHADER& ps, const char* pstrprogram)
}
void PixelShaderCache::SetPSSampler(const char * name, unsigned int Tex)
{
if(g_ActiveConfig.backend_info.bSupportsGLSLBinding)
if (g_ActiveConfig.backend_info.bSupportsGLSLBinding)
return;
PROGRAMSHADER tmp = ProgramShaderCache::GetShaderProgram();
for(int a = 0; a < NUM_UNIFORMS; ++a)
if(!strcmp(name, UniformNames[a]))
for (int a = 0; a < NUM_UNIFORMS; ++a)
if (!strcmp(name, UniformNames[a]))
{
if(tmp.UniformLocations[a] == -1)
return;
@ -484,8 +489,8 @@ void PixelShaderCache::SetPSSampler(const char * name, unsigned int Tex)
void SetPSConstant4fvByName(const char * name, unsigned int offset, const float *f, const unsigned int count = 1)
{
PROGRAMSHADER tmp = ProgramShaderCache::GetShaderProgram();
for(int a = 0; a < NUM_UNIFORMS; ++a)
if(!strcmp(name, UniformNames[a]))
for (int a = 0; a < NUM_UNIFORMS; ++a)
if (!strcmp(name, UniformNames[a]))
{
if(tmp.UniformLocations[a] == -1)
return;
@ -500,14 +505,19 @@ void SetGLSLPSConstant4f(unsigned int const_number, float f1, float f2, float f3
{
float f[4] = { f1, f2, f3, f4 };
if(g_ActiveConfig.backend_info.bSupportsGLSLLocation)
if (g_ActiveConfig.backend_info.bSupportsGLSLLocation)
{
glUniform4fv(const_number, 1, f);
return;
}
for( unsigned int a = 0; a < 10; ++a)
if (g_ActiveConfig.backend_info.bSupportsGLSLUBO)
{
ProgramShaderCache::SetUniformObjects(0, const_number, f);
return;
}
for (unsigned int a = 0; a < 10; ++a)
{
if( const_number >= PSVar_Loc[a].reg && const_number < (PSVar_Loc[a].reg + PSVar_Loc[a].size))
if ( const_number >= PSVar_Loc[a].reg && const_number < (PSVar_Loc[a].reg + PSVar_Loc[a].size))
{
unsigned int offset = const_number - PSVar_Loc[a].reg;
SetPSConstant4fvByName(PSVar_Loc[a].name, offset, f);
@ -518,14 +528,19 @@ void SetGLSLPSConstant4f(unsigned int const_number, float f1, float f2, float f3
void SetGLSLPSConstant4fv(unsigned int const_number, const float *f)
{
if(g_ActiveConfig.backend_info.bSupportsGLSLLocation)
if (g_ActiveConfig.backend_info.bSupportsGLSLLocation)
{
glUniform4fv(const_number, 1, f);
return;
}
for( unsigned int a = 0; a < 10; ++a)
if (g_ActiveConfig.backend_info.bSupportsGLSLUBO)
{
ProgramShaderCache::SetUniformObjects(0, const_number, f);
return;
}
for (unsigned int a = 0; a < 10; ++a)
{
if( const_number >= PSVar_Loc[a].reg && const_number < (PSVar_Loc[a].reg + PSVar_Loc[a].size))
if ( const_number >= PSVar_Loc[a].reg && const_number < (PSVar_Loc[a].reg + PSVar_Loc[a].size))
{
unsigned int offset = const_number - PSVar_Loc[a].reg;
SetPSConstant4fvByName(PSVar_Loc[a].name, offset, f);
@ -536,14 +551,19 @@ void SetGLSLPSConstant4fv(unsigned int const_number, const float *f)
void SetMultiGLSLPSConstant4fv(unsigned int const_number, unsigned int count, const float *f)
{
if(g_ActiveConfig.backend_info.bSupportsGLSLLocation)
if (g_ActiveConfig.backend_info.bSupportsGLSLLocation)
{
glUniform4fv(const_number, count, f);
return;
}
for( unsigned int a = 0; a < 10; ++a)
if (g_ActiveConfig.backend_info.bSupportsGLSLUBO)
{
ProgramShaderCache::SetUniformObjects(0, const_number, f, count);
return;
}
for (unsigned int a = 0; a < 10; ++a)
{
if( const_number >= PSVar_Loc[a].reg && const_number < (PSVar_Loc[a].reg + PSVar_Loc[a].size))
if (const_number >= PSVar_Loc[a].reg && const_number < (PSVar_Loc[a].reg + PSVar_Loc[a].size))
{
unsigned int offset = const_number - PSVar_Loc[a].reg;
SetPSConstant4fvByName(PSVar_Loc[a].name, offset, f, count);

View File

@ -14,12 +14,15 @@
// Official SVN repository and contact information can be found at
// http://code.google.com/p/dolphin-emu/
#include "ProgramShaderCache.h"
#include <assert.h>
namespace OGL
{
GLuint ProgramShaderCache::CurrentFShader = 0, ProgramShaderCache::CurrentVShader = 0, ProgramShaderCache::CurrentProgram = 0;
ProgramShaderCache::PCache ProgramShaderCache::pshaders;
GLuint ProgramShaderCache::UBOBuffers[2];
std::pair<u64, u64> ProgramShaderCache::CurrentShaderProgram;
const char *UniformNames[NUM_UNIFORMS] = {
@ -85,12 +88,14 @@ namespace OGL
// Let's attach everything
if(entry.program.vsid != 0) // attaching zero vertex shader makes it freak out
glAttachShader(entry.program.glprogid, entry.program.vsid);
glAttachShader(entry.program.glprogid, entry.program.psid);
glLinkProgram(entry.program.glprogid);
//checkForGLError("linking program");
glUseProgram(entry.program.glprogid);
//checkForGLError("using program");
glUseProgram(entry.program.glprogid);
// We cache our uniform locations for now
// Once we move up to a newer version of GLSL, ~1.30
// We can remove this
@ -122,18 +127,40 @@ namespace OGL
CurrentShaderProgram = ShaderPair;
CurrentProgram = entry.program.glprogid;
}
void ProgramShaderCache::SetUniformObjects(int Buffer, unsigned int offset, const float *f, unsigned int count)
{
assert(Buffer > 1);
glBindBuffer(GL_UNIFORM_BUFFER, UBOBuffers[Buffer]);
// glBufferSubData expects data in bytes, so multiply count by four
// Expects the offset in bytes as well, so multiply by *4 *4 since we are passing in a vec4 location
glBufferSubData(GL_UNIFORM_BUFFER, offset * 4 * 4, count * 4, (void*)&f[0]);
}
GLuint ProgramShaderCache::GetCurrentProgram(void) { return CurrentProgram; }
GLint ProgramShaderCache::GetAttr(int num)
{
return pshaders[CurrentShaderProgram].program.attrLoc[num];
}
PROGRAMSHADER ProgramShaderCache::GetShaderProgram()
PROGRAMSHADER ProgramShaderCache::GetShaderProgram(void)
{
return pshaders[CurrentShaderProgram].program;
}
void ProgramShaderCache::Init(void)
{
glGenBuffers(2, UBOBuffers);
glBindBuffer(GL_UNIFORM_BUFFER, UBOBuffers[0]);
// We multiply by *4*4 because we need to get down to basic machine units.
// So multiply by four to get how many floats we have from vec4s
// Then once more to get bytes
glBufferData(GL_UNIFORM_BUFFER, C_PENVCONST_END * 4 * 4, NULL, GL_DYNAMIC_DRAW);
// Now bind the buffer to the index point
// We know PS is 0 since we have it statically set in the shader
glBindBufferBase(GL_UNIFORM_BUFFER, 0, UBOBuffers[0]);
// Repeat for VS shader
glBindBuffer(GL_UNIFORM_BUFFER, UBOBuffers[1]);
glBufferData(GL_UNIFORM_BUFFER, C_VENVCONST_END * 4 * 4, NULL, GL_DYNAMIC_DRAW);
glBindBufferBase(GL_UNIFORM_BUFFER, 1, UBOBuffers[1]);
}
void ProgramShaderCache::Shutdown(void)
{
PCache::iterator iter = pshaders.begin();

View File

@ -94,11 +94,17 @@ class ProgramShaderCache
static PCache pshaders;
static GLuint CurrentFShader, CurrentVShader, CurrentProgram;
static std::pair<u64, u64> CurrentShaderProgram;
// For UBOS
static GLuint UBOBuffers[2]; // PS is 0, VS is 1
public:
static PROGRAMSHADER GetShaderProgram();
static PROGRAMSHADER GetShaderProgram(void);
static GLint GetAttr(int num);
static void SetBothShaders(GLuint PS, GLuint VS);
static GLuint GetCurrentProgram(void);
static void SetUniformObjects(int Buffer, unsigned int offset, const float *f, unsigned int count = 1);
static void Init(void);
static void Shutdown(void);
};

View File

@ -231,7 +231,8 @@ void VertexManager::vFlush()
if(g_ActiveConfig.bUseGLSL)
{
ProgramShaderCache::SetBothShaders(ps->glprogid, 0);
PixelShaderManager::SetConstants(); // Need to set these again
if(!g_ActiveConfig.backend_info.bSupportsGLSLUBO)
PixelShaderManager::SetConstants(); // Need to set these again, if we don't support UBO
if (g_nativeVertexFmt)
g_nativeVertexFmt->SetupVertexPointers();
for (int i = 0; i < 8; i++)

View File

@ -252,6 +252,11 @@ void SetGLSLVSConstant4f(unsigned int const_number, float f1, float f2, float f3
glUniform4fv(const_number, 1, buf);
return;
}
if (g_ActiveConfig.backend_info.bSupportsGLSLUBO)
{
ProgramShaderCache::SetUniformObjects(1, const_number, buf);
return;
}
for( unsigned int a = 0; a < 9; ++a)
{
if( const_number >= VSVar_Loc[a].reg && const_number < ( VSVar_Loc[a].reg + VSVar_Loc[a].size))
@ -270,6 +275,11 @@ void SetGLSLVSConstant4fv(unsigned int const_number, const float *f)
glUniform4fv(const_number, 1, f);
return;
}
if (g_ActiveConfig.backend_info.bSupportsGLSLUBO)
{
ProgramShaderCache::SetUniformObjects(1, const_number, f);
return;
}
for( unsigned int a = 0; a < 9; ++a)
{
if( const_number >= VSVar_Loc[a].reg && const_number < ( VSVar_Loc[a].reg + VSVar_Loc[a].size))
@ -288,6 +298,11 @@ void SetMultiGLSLVSConstant4fv(unsigned int const_number, unsigned int count, co
glUniform4fv(const_number, count, f);
return;
}
if (g_ActiveConfig.backend_info.bSupportsGLSLUBO)
{
ProgramShaderCache::SetUniformObjects(1, const_number, f, count);
return;
}
for( unsigned int a = 0; a < 9; ++a)
{
if( const_number >= VSVar_Loc[a].reg && const_number < ( VSVar_Loc[a].reg + VSVar_Loc[a].size))
@ -314,6 +329,11 @@ void SetMultiGLSLVSConstant3fv(unsigned int const_number, unsigned int count, co
glUniform4fv(const_number, count, buf);
return;
}
if (g_ActiveConfig.backend_info.bSupportsGLSLUBO)
{
ProgramShaderCache::SetUniformObjects(1, const_number, buf, count);
return;
}
for( unsigned int a = 0; a < 9; ++a)
{
if( const_number >= VSVar_Loc[a].reg && const_number < ( VSVar_Loc[a].reg + VSVar_Loc[a].size))

View File

@ -83,6 +83,7 @@ Make AA apply instantly during gameplay if possible
#include "PixelShaderManager.h"
#include "VertexShaderCache.h"
#include "VertexShaderManager.h"
#include "ProgramShaderCache.h"
#include "CommandProcessor.h"
#include "PixelEngine.h"
#include "TextureConverter.h"
@ -200,6 +201,7 @@ void VideoBackend::Video_Prepare()
VertexShaderManager::Init();
PixelShaderCache::Init();
PixelShaderManager::Init();
ProgramShaderCache::Init();
PostProcessing::Init();
GL_REPORT_ERRORD();
VertexLoaderManager::Init();
@ -227,6 +229,7 @@ void VideoBackend::Shutdown()
// And need to be called from the video thread
TextureConverter::Shutdown();
VertexLoaderManager::Shutdown();
ProgramShaderCache::Shutdown();
VertexShaderCache::Shutdown();
VertexShaderManager::Shutdown();
PixelShaderManager::Shutdown();