yay, UBOs work 100% now.

This commit is contained in:
Ryan Houdek 2011-12-10 08:07:13 -06:00
parent 7ab38cff68
commit 5925feb6e0
6 changed files with 66 additions and 99 deletions

View File

@ -518,7 +518,7 @@ const char* WriteBinding(API_TYPE ApiType, const u32 num)
} }
const char *WriteLocation(API_TYPE ApiType) const char *WriteLocation(API_TYPE ApiType)
{ {
if(ApiType == API_GLSL && g_ActiveConfig.backend_info.bSupportsGLSLUBO) if(ApiType == API_GLSL && !g_ActiveConfig.backend_info.bSupportsGLSLUBO)
return ""; return "";
static char result[64]; static char result[64];
sprintf(result, "uniform "); sprintf(result, "uniform ");
@ -611,7 +611,7 @@ const char *GeneratePixelShaderCode(DSTALPHA_MODE dstAlphaMode, API_TYPE ApiType
WRITE(p, "\n"); WRITE(p, "\n");
if(ApiType == API_GLSL && g_ActiveConfig.backend_info.bSupportsGLSLUBO) if(ApiType == API_GLSL && g_ActiveConfig.backend_info.bSupportsGLSLUBO)
WRITE(p, "layout(std140, binding = 0) uniform PSBlock {\n"); WRITE(p, "layout(std140, binding = 1) uniform PSBlock {\n");
WRITE(p, "%sfloat4 "I_COLORS"[4] %s;\n", WriteLocation(ApiType), WriteRegister(ApiType, "c", C_COLORS)); WRITE(p, "%sfloat4 "I_COLORS"[4] %s;\n", WriteLocation(ApiType), WriteRegister(ApiType, "c", C_COLORS));
WRITE(p, "%sfloat4 "I_KCOLORS"[4] %s;\n", WriteLocation(ApiType), WriteRegister(ApiType, "c", C_KCOLORS)); WRITE(p, "%sfloat4 "I_KCOLORS"[4] %s;\n", WriteLocation(ApiType), WriteRegister(ApiType, "c", C_KCOLORS));

View File

@ -166,12 +166,6 @@ char* GenerateVSOutputStruct(char* p, u32 components, API_TYPE ApiType)
extern const char* WriteRegister(API_TYPE ApiType, const char *prefix, const u32 num); extern const char* WriteRegister(API_TYPE ApiType, const char *prefix, const u32 num);
extern const char* WriteBinding(API_TYPE ApiType, const u32 num); extern const char* WriteBinding(API_TYPE ApiType, const u32 num);
const char *WriteLocation(API_TYPE ApiType); const char *WriteLocation(API_TYPE ApiType);
const char *WriteLocation2(API_TYPE ApiType)
{
static char result[64];
sprintf(result, "uniform ");
return result;
}
const char *GenerateVertexShaderCode(u32 components, API_TYPE ApiType) const char *GenerateVertexShaderCode(u32 components, API_TYPE ApiType)
{ {
@ -222,20 +216,21 @@ const char *GenerateVertexShaderCode(u32 components, API_TYPE ApiType)
} }
// uniforms // uniforms
//if(ApiType == API_GLSL && g_ActiveConfig.backend_info.bSupportsGLSLUBO) if(ApiType == API_GLSL && g_ActiveConfig.backend_info.bSupportsGLSLUBO)
//WRITE(p, "layout(std140, binding = 2) uniform VSBlock {\n"); WRITE(p, "layout(std140, binding = 2) uniform VSBlock {\n");
WRITE(p, "%sfloat4 "I_POSNORMALMATRIX"[6] %s;\n", WriteLocation2(ApiType), WriteRegister(ApiType, "c", C_POSNORMALMATRIX)); WRITE(p, "%sfloat4 "I_POSNORMALMATRIX"[6] %s;\n", WriteLocation(ApiType), WriteRegister(ApiType, "c", C_POSNORMALMATRIX));
//if(ApiType == API_GLSL && g_ActiveConfig.backend_info.bSupportsGLSLUBO) WRITE(p, "%sfloat4 "I_PROJECTION"[4] %s;\n", WriteLocation(ApiType), WriteRegister(ApiType, "c", C_PROJECTION));
//WRITE(p, "};\n"); WRITE(p, "%sfloat4 "I_MATERIALS"[4] %s;\n", WriteLocation(ApiType), WriteRegister(ApiType, "c", C_MATERIALS));
WRITE(p, "%sfloat4 "I_PROJECTION"[4] %s;\n", WriteLocation2(ApiType), WriteRegister(ApiType, "c", C_PROJECTION)); WRITE(p, "%sfloat4 "I_LIGHTS"[40] %s;\n", WriteLocation(ApiType), WriteRegister(ApiType, "c", C_LIGHTS));
WRITE(p, "%sfloat4 "I_MATERIALS"[4] %s;\n", WriteLocation2(ApiType), WriteRegister(ApiType, "c", C_MATERIALS)); WRITE(p, "%sfloat4 "I_TEXMATRICES"[24] %s;\n", WriteLocation(ApiType), WriteRegister(ApiType, "c", C_TEXMATRICES)); // also using tex matrices
WRITE(p, "%sfloat4 "I_LIGHTS"[40] %s;\n", WriteLocation2(ApiType), WriteRegister(ApiType, "c", C_LIGHTS)); WRITE(p, "%sfloat4 "I_TRANSFORMMATRICES"[64] %s;\n", WriteLocation(ApiType),WriteRegister(ApiType, "c", C_TRANSFORMMATRICES));
WRITE(p, "%sfloat4 "I_TEXMATRICES"[24] %s;\n", WriteLocation2(ApiType), WriteRegister(ApiType, "c", C_TEXMATRICES)); // also using tex matrices WRITE(p, "%sfloat4 "I_NORMALMATRICES"[32] %s;\n", WriteLocation(ApiType), WriteRegister(ApiType, "c", C_NORMALMATRICES));
WRITE(p, "%sfloat4 "I_TRANSFORMMATRICES"[64] %s;\n", WriteLocation2(ApiType), WriteRegister(ApiType, "c", C_TRANSFORMMATRICES)); WRITE(p, "%sfloat4 "I_POSTTRANSFORMMATRICES"[64] %s;\n", WriteLocation(ApiType), WriteRegister(ApiType, "c", C_POSTTRANSFORMMATRICES));
WRITE(p, "%sfloat4 "I_NORMALMATRICES"[32] %s;\n", WriteLocation2(ApiType), WriteRegister(ApiType, "c", C_NORMALMATRICES)); WRITE(p, "%sfloat4 "I_DEPTHPARAMS" %s;\n", WriteLocation(ApiType), WriteRegister(ApiType, "c", C_DEPTHPARAMS));
WRITE(p, "%sfloat4 "I_POSTTRANSFORMMATRICES"[64] %s;\n", WriteLocation2(ApiType), WriteRegister(ApiType, "c", C_POSTTRANSFORMMATRICES));
WRITE(p, "%sfloat4 "I_DEPTHPARAMS" %s;\n", WriteLocation2(ApiType), WriteRegister(ApiType, "c", C_DEPTHPARAMS)); if(ApiType == API_GLSL && g_ActiveConfig.backend_info.bSupportsGLSLUBO)
WRITE(p, "};\n");

View File

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

View File

@ -429,24 +429,6 @@ bool CompileGLSLPixelShader(FRAGMENTSHADER& ps, const char* pstrprogram)
glShaderSource(result, 1, &pstrprogram, NULL); glShaderSource(result, 1, &pstrprogram, NULL);
glCompileShader(result); glCompileShader(result);
GLsizei length = 0;
glGetShaderiv(result, GL_INFO_LOG_LENGTH, &length);
if (length > 0)
{
GLsizei charsWritten;
GLchar* infoLog = new GLchar[length];
glGetShaderInfoLog(result, length, &charsWritten, infoLog);
WARN_LOG(VIDEO, "PS Shader info log:\n%s", infoLog);
char szTemp[MAX_PATH];
sprintf(szTemp, "ps_%d.txt", result);
FILE *fp = fopen(szTemp, "wb");
fwrite(pstrprogram, strlen(pstrprogram), 1, fp);
fclose(fp);
if(strstr(infoLog, "warning") != NULL || strstr(infoLog, "error") != NULL)
exit(0);
delete[] infoLog;
}
GLint compileStatus; GLint compileStatus;
glGetShaderiv(result, GL_COMPILE_STATUS, &compileStatus); glGetShaderiv(result, GL_COMPILE_STATUS, &compileStatus);
@ -454,6 +436,23 @@ bool CompileGLSLPixelShader(FRAGMENTSHADER& ps, const char* pstrprogram)
{ {
// Compile failed // Compile failed
ERROR_LOG(VIDEO, "Shader compilation failed; see info log"); ERROR_LOG(VIDEO, "Shader compilation failed; see info log");
GLsizei length = 0;
glGetShaderiv(result, GL_INFO_LOG_LENGTH, &length);
if (length > 0)
{
GLsizei charsWritten;
GLchar* infoLog = new GLchar[length];
glGetShaderInfoLog(result, length, &charsWritten, infoLog);
WARN_LOG(VIDEO, "VS Shader info log:\n%s", infoLog);
char szTemp[MAX_PATH];
sprintf(szTemp, "vs_%d.txt", result);
FILE *fp = fopen(szTemp, "wb");
fwrite(pstrprogram, strlen(pstrprogram), 1, fp);
fclose(fp);
delete[] infoLog;
}
// Don't try to use this shader // Don't try to use this shader
glDeleteShader(result); glDeleteShader(result);
return false; return false;

View File

@ -17,16 +17,6 @@
#include "ProgramShaderCache.h" #include "ProgramShaderCache.h"
#include <assert.h> #include <assert.h>
GLuint GLERR(const char *function)
{
GLint err = glGetError();
if (err != GL_NO_ERROR)
{
printf( "(%s) OpenGL error 0x%x - %s\n",
function, err, gluErrorString(err));
}
return err;
}
namespace OGL namespace OGL
{ {
GLuint ProgramShaderCache::CurrentFShader = 0, ProgramShaderCache::CurrentVShader = 0, ProgramShaderCache::CurrentProgram = 0; GLuint ProgramShaderCache::CurrentFShader = 0, ProgramShaderCache::CurrentVShader = 0, ProgramShaderCache::CurrentProgram = 0;
@ -101,19 +91,14 @@ namespace OGL
glAttachShader(entry.program.glprogid, entry.program.psid); glAttachShader(entry.program.glprogid, entry.program.psid);
glLinkProgram(entry.program.glprogid); glLinkProgram(entry.program.glprogid);
GLsizei length = 0;
glGetProgramiv(entry.program.glprogid, GL_INFO_LOG_LENGTH, &length);
if (length > 0)
{
GLsizei charsWritten;
GLchar* infoLog = new GLchar[length];
glGetProgramInfoLog(entry.program.glprogid, length, &charsWritten, infoLog);
printf("Program info log:\n%s", infoLog);
delete[] infoLog;
}
glUseProgram(entry.program.glprogid); glUseProgram(entry.program.glprogid);
// Dunno why this is needed when I have the binding
// points statically set in the shader source
// Driver Bug? Nvidia GTX 570, 290.xx Driver, Linux x64
glUniformBlockBinding( entry.program.glprogid, 0, 1 );
glUniformBlockBinding( entry.program.glprogid, 1, 2 );
// We cache our uniform locations for now // We cache our uniform locations for now
// Once we move up to a newer version of GLSL, ~1.30 // Once we move up to a newer version of GLSL, ~1.30
@ -148,13 +133,11 @@ namespace OGL
} }
void ProgramShaderCache::SetUniformObjects(int Buffer, unsigned int offset, const float *f, unsigned int count) void ProgramShaderCache::SetUniformObjects(int Buffer, unsigned int offset, const float *f, unsigned int count)
{ {
GLERR("");
assert(Buffer > 1); assert(Buffer > 1);
static int _Buffer = -1; static int _Buffer = -1;
if(_Buffer != Buffer) if(_Buffer != Buffer)
{ {
_Buffer = Buffer; _Buffer = Buffer;
GLERR("bind");
glBindBuffer(GL_UNIFORM_BUFFER, UBOBuffers[_Buffer]); glBindBuffer(GL_UNIFORM_BUFFER, UBOBuffers[_Buffer]);
} }
// Query for the offsets of each block variable // Query for the offsets of each block variable
@ -162,7 +145,6 @@ namespace OGL
// glBufferSubData expects data in bytes, so multiply count by four // 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 // 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 * 4, f); glBufferSubData(GL_UNIFORM_BUFFER, offset * 4 * 4, count * 4 * 4, f);
GLERR("sub");
} }
GLuint ProgramShaderCache::GetCurrentProgram(void) { return CurrentProgram; } GLuint ProgramShaderCache::GetCurrentProgram(void) { return CurrentProgram; }
@ -179,7 +161,6 @@ namespace OGL
{ {
glGenBuffers(2, UBOBuffers); glGenBuffers(2, UBOBuffers);
glBindBuffer(GL_UNIFORM_BUFFER, UBOBuffers[0]); glBindBuffer(GL_UNIFORM_BUFFER, UBOBuffers[0]);
// We multiply by *4*4 because we need to get down to basic machine units. // 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 // So multiply by four to get how many floats we have from vec4s
@ -187,13 +168,12 @@ namespace OGL
glBufferData(GL_UNIFORM_BUFFER, C_PENVCONST_END * 4 * 4, NULL, GL_DYNAMIC_DRAW); glBufferData(GL_UNIFORM_BUFFER, C_PENVCONST_END * 4 * 4, NULL, GL_DYNAMIC_DRAW);
// Now bind the buffer to the index point // Now bind the buffer to the index point
// We know PS is 0 since we have it statically set in the shader // We know PS is 0 since we have it statically set in the shader
glBindBufferBase(GL_UNIFORM_BUFFER, 0, UBOBuffers[0]); glBindBufferBase(GL_UNIFORM_BUFFER, 1, UBOBuffers[0]);
// Repeat for VS shader // Repeat for VS shader
//glBindBuffer(GL_UNIFORM_BUFFER, UBOBuffers[1]); glBindBuffer(GL_UNIFORM_BUFFER, UBOBuffers[1]);
//glBufferData(GL_UNIFORM_BUFFER, C_PENVCONST_END * 4 * 4, NULL, GL_DYNAMIC_DRAW); glBufferData(GL_UNIFORM_BUFFER, 1024*1024, NULL, GL_DYNAMIC_DRAW);
//glBindBufferBase(GL_UNIFORM_BUFFER, 2, UBOBuffers[1]); glBindBufferBase(GL_UNIFORM_BUFFER, 2, UBOBuffers[1]);
GLERR("init");
} }
void ProgramShaderCache::Shutdown(void) void ProgramShaderCache::Shutdown(void)
{ {

View File

@ -189,25 +189,6 @@ bool CompileGLSLVertexShader(VERTEXSHADER& vs, const char* pstrprogram)
glShaderSource(result, 1, &pstrprogram, NULL); glShaderSource(result, 1, &pstrprogram, NULL);
glCompileShader(result); glCompileShader(result);
GLsizei length = 0;
glGetShaderiv(result, GL_INFO_LOG_LENGTH, &length);
if (length > 0)
{
GLsizei charsWritten;
GLchar* infoLog = new GLchar[length];
glGetShaderInfoLog(result, length, &charsWritten, infoLog);
WARN_LOG(VIDEO, "VS Shader info log:\n%s", infoLog);
char szTemp[MAX_PATH];
sprintf(szTemp, "vs_%d.txt", result);
FILE *fp = fopen(szTemp, "wb");
fwrite(pstrprogram, strlen(pstrprogram), 1, fp);
fclose(fp);
if(strstr(infoLog, "warning") != NULL || strstr(infoLog, "error") != NULL)
exit(0);
delete[] infoLog;
}
GLint compileStatus; GLint compileStatus;
glGetShaderiv(result, GL_COMPILE_STATUS, &compileStatus); glGetShaderiv(result, GL_COMPILE_STATUS, &compileStatus);
@ -215,6 +196,23 @@ bool CompileGLSLVertexShader(VERTEXSHADER& vs, const char* pstrprogram)
{ {
// Compile failed // Compile failed
ERROR_LOG(VIDEO, "Shader compilation failed; see info log"); ERROR_LOG(VIDEO, "Shader compilation failed; see info log");
GLsizei length = 0;
glGetShaderiv(result, GL_INFO_LOG_LENGTH, &length);
if (length > 0)
{
GLsizei charsWritten;
GLchar* infoLog = new GLchar[length];
glGetShaderInfoLog(result, length, &charsWritten, infoLog);
WARN_LOG(VIDEO, "VS Shader info log:\n%s", infoLog);
char szTemp[MAX_PATH];
sprintf(szTemp, "vs_%d.txt", result);
FILE *fp = fopen(szTemp, "wb");
fwrite(pstrprogram, strlen(pstrprogram), 1, fp);
fclose(fp);
delete[] infoLog;
}
// Don't try to use this shader // Don't try to use this shader
glDeleteShader(result); glDeleteShader(result);
return false; return false;
@ -240,7 +238,6 @@ void SetVSConstant4fvByName(const char * name, unsigned int offset, const float
} }
} }
} }
#define MAX_UNIFORM 0
void SetGLSLVSConstant4f(unsigned int const_number, float f1, float f2, float f3, float f4) void SetGLSLVSConstant4f(unsigned int const_number, float f1, float f2, float f3, float f4)
{ {
float buf[4]; float buf[4];
@ -250,9 +247,8 @@ void SetGLSLVSConstant4f(unsigned int const_number, float f1, float f2, float f3
buf[3] = f4; buf[3] = f4;
if (g_ActiveConfig.backend_info.bSupportsGLSLUBO) if (g_ActiveConfig.backend_info.bSupportsGLSLUBO)
{ {
if(const_number < MAX_UNIFORM) ProgramShaderCache::SetUniformObjects(1, const_number, buf);
ProgramShaderCache::SetUniformObjects(1, const_number, buf); return;
//return;
} }
for( unsigned int a = 0; a < 9; ++a) for( unsigned int a = 0; a < 9; ++a)
{ {
@ -269,9 +265,8 @@ void SetGLSLVSConstant4fv(unsigned int const_number, const float *f)
{ {
if (g_ActiveConfig.backend_info.bSupportsGLSLUBO) if (g_ActiveConfig.backend_info.bSupportsGLSLUBO)
{ {
if(const_number < MAX_UNIFORM)
ProgramShaderCache::SetUniformObjects(1, const_number, f); ProgramShaderCache::SetUniformObjects(1, const_number, f);
//return; return;
} }
for( unsigned int a = 0; a < 9; ++a) for( unsigned int a = 0; a < 9; ++a)
{ {
@ -288,9 +283,8 @@ void SetMultiGLSLVSConstant4fv(unsigned int const_number, unsigned int count, co
{ {
if (g_ActiveConfig.backend_info.bSupportsGLSLUBO) if (g_ActiveConfig.backend_info.bSupportsGLSLUBO)
{ {
if(const_number < MAX_UNIFORM)
ProgramShaderCache::SetUniformObjects(1, const_number, f, count); ProgramShaderCache::SetUniformObjects(1, const_number, f, count);
//return; return;
} }
for( unsigned int a = 0; a < 9; ++a) for( unsigned int a = 0; a < 9; ++a)
{ {
@ -315,9 +309,8 @@ void SetMultiGLSLVSConstant3fv(unsigned int const_number, unsigned int count, co
} }
if (g_ActiveConfig.backend_info.bSupportsGLSLUBO) if (g_ActiveConfig.backend_info.bSupportsGLSLUBO)
{ {
if(const_number < MAX_UNIFORM)
ProgramShaderCache::SetUniformObjects(1, const_number, buf, count); ProgramShaderCache::SetUniformObjects(1, const_number, buf, count);
//return; return;
} }
for( unsigned int a = 0; a < 9; ++a) for( unsigned int a = 0; a < 9; ++a)
{ {