2011-12-01 03:00:21 +00:00
|
|
|
// Copyright (C) 2003 Dolphin Project.
|
|
|
|
|
|
|
|
// This program is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU General Public License as published by
|
|
|
|
// the Free Software Foundation, version 2.0.
|
|
|
|
|
|
|
|
// This program is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU General Public License 2.0 for more details.
|
|
|
|
|
|
|
|
// A copy of the GPL 2.0 should have been included with the program.
|
|
|
|
// If not, see http://www.gnu.org/licenses/
|
|
|
|
|
|
|
|
// Official SVN repository and contact information can be found at
|
|
|
|
// http://code.google.com/p/dolphin-emu/
|
2011-12-09 23:30:05 +00:00
|
|
|
|
2011-12-01 03:00:21 +00:00
|
|
|
#include "ProgramShaderCache.h"
|
2011-12-10 21:58:44 +00:00
|
|
|
#include "MathUtil.h"
|
|
|
|
|
2011-12-01 03:00:21 +00:00
|
|
|
namespace OGL
|
|
|
|
{
|
2011-12-21 06:15:48 +00:00
|
|
|
|
2011-12-26 05:15:54 +00:00
|
|
|
GLuint ProgramShaderCache::CurrentFShader = 0, ProgramShaderCache::CurrentVShader = 0, ProgramShaderCache::CurrentProgram = 0;
|
|
|
|
ProgramShaderCache::PCache ProgramShaderCache::pshaders;
|
|
|
|
GLuint ProgramShaderCache::s_ps_vs_ubo;
|
|
|
|
GLintptr ProgramShaderCache::s_vs_data_offset;
|
2013-01-14 12:58:11 +00:00
|
|
|
float *ProgramShaderCache::s_ubo_buffer;
|
|
|
|
u32 ProgramShaderCache::s_ubo_buffer_size;
|
|
|
|
bool ProgramShaderCache::s_ubo_dirty;
|
2011-12-26 05:15:54 +00:00
|
|
|
|
2011-12-26 07:58:52 +00:00
|
|
|
LinearDiskCache<ProgramShaderCache::ShaderUID, u8> g_program_disk_cache;
|
2011-12-26 05:15:54 +00:00
|
|
|
GLenum ProgramFormat;
|
|
|
|
|
2012-01-18 18:22:03 +00:00
|
|
|
GLuint ProgramShaderCache::PCacheEntry::prog_format = 0;
|
2011-12-29 15:25:03 +00:00
|
|
|
|
2011-12-26 05:15:54 +00:00
|
|
|
std::pair<u32, u32> ProgramShaderCache::CurrentShaderProgram;
|
|
|
|
const char *UniformNames[NUM_UNIFORMS] =
|
|
|
|
{
|
|
|
|
// SAMPLERS
|
|
|
|
"samp0","samp1","samp2","samp3","samp4","samp5","samp6","samp7",
|
|
|
|
// PIXEL SHADER UNIFORMS
|
|
|
|
I_COLORS,
|
|
|
|
I_KCOLORS,
|
|
|
|
I_ALPHA,
|
|
|
|
I_TEXDIMS,
|
|
|
|
I_ZBIAS ,
|
|
|
|
I_INDTEXSCALE ,
|
|
|
|
I_INDTEXMTX,
|
|
|
|
I_FOG,
|
|
|
|
I_PLIGHTS,
|
|
|
|
I_PMATERIALS,
|
|
|
|
// VERTEX SHADER UNIFORMS
|
|
|
|
I_POSNORMALMATRIX,
|
|
|
|
I_PROJECTION ,
|
|
|
|
I_MATERIALS,
|
|
|
|
I_LIGHTS,
|
|
|
|
I_TEXMATRICES,
|
|
|
|
I_TRANSFORMMATRICES ,
|
|
|
|
I_NORMALMATRICES ,
|
|
|
|
I_POSTTRANSFORMMATRICES,
|
|
|
|
I_DEPTHPARAMS,
|
|
|
|
};
|
|
|
|
|
2011-12-26 07:58:52 +00:00
|
|
|
void ProgramShaderCache::SetProgramVariables(PCacheEntry &entry)
|
2011-12-26 05:15:54 +00:00
|
|
|
{
|
2013-01-02 15:56:08 +00:00
|
|
|
// Bind UBO
|
2011-12-26 05:15:54 +00:00
|
|
|
if (g_ActiveConfig.backend_info.bSupportsGLSLUBO)
|
2011-12-11 10:08:18 +00:00
|
|
|
{
|
2013-01-02 15:56:08 +00:00
|
|
|
GLint PSBlock_id = glGetUniformBlockIndex(entry.prog_id, "PSBlock");
|
|
|
|
GLint VSBlock_id = glGetUniformBlockIndex(entry.prog_id, "VSBlock");
|
|
|
|
|
|
|
|
if(PSBlock_id != -1)
|
|
|
|
glUniformBlockBinding(entry.prog_id, PSBlock_id, 1);
|
|
|
|
if(VSBlock_id != -1)
|
|
|
|
glUniformBlockBinding(entry.prog_id, VSBlock_id, 2);
|
2011-12-26 05:15:54 +00:00
|
|
|
}
|
2013-01-02 15:56:08 +00:00
|
|
|
|
2011-12-26 05:15:54 +00:00
|
|
|
// We cache our uniform locations for now
|
|
|
|
// Once we move up to a newer version of GLSL, ~1.30
|
|
|
|
// We can remove this
|
2011-12-11 10:14:02 +00:00
|
|
|
|
2011-12-26 05:15:54 +00:00
|
|
|
// (Sonicadvance): For some reason this fails on my hardware
|
2011-12-26 07:58:52 +00:00
|
|
|
//glGetUniformIndices(entry.prog_id, NUM_UNIFORMS, UniformNames, entry.UniformLocations);
|
2011-12-26 05:15:54 +00:00
|
|
|
// Got to do it this crappy way.
|
|
|
|
if (!g_ActiveConfig.backend_info.bSupportsGLSLUBO)
|
|
|
|
for (int a = 8; a < NUM_UNIFORMS; ++a)
|
2011-12-26 07:58:52 +00:00
|
|
|
entry.UniformLocations[a] = glGetUniformLocation(entry.prog_id, UniformNames[a]);
|
2011-12-11 10:14:02 +00:00
|
|
|
|
2013-01-02 15:56:08 +00:00
|
|
|
// Bind Texture Sampler
|
|
|
|
for (int a = 0; a < 8; ++a)
|
2011-12-26 05:15:54 +00:00
|
|
|
{
|
2013-01-02 15:56:08 +00:00
|
|
|
// Still need to get sampler locations since we aren't binding them statically in the shaders
|
|
|
|
entry.UniformLocations[a] = glGetUniformLocation(entry.prog_id, UniformNames[a]);
|
|
|
|
if (entry.UniformLocations[a] != -1)
|
|
|
|
glUniform1i(entry.UniformLocations[a], a);
|
2012-12-31 01:34:27 +00:00
|
|
|
}
|
2013-01-02 15:56:08 +00:00
|
|
|
|
2012-12-31 01:34:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void ProgramShaderCache::SetProgramBindings ( ProgramShaderCache::PCacheEntry& entry )
|
|
|
|
{
|
2013-01-14 19:00:33 +00:00
|
|
|
if (g_ActiveConfig.backend_info.bSupportsDualSourceBlend)
|
2012-12-31 01:34:27 +00:00
|
|
|
{
|
2013-01-02 15:56:08 +00:00
|
|
|
// So we do support extended blending
|
|
|
|
// So we need to set a few more things here.
|
|
|
|
// Bind our out locations
|
|
|
|
glBindFragDataLocationIndexed(entry.prog_id, 0, 0, "ocol0");
|
|
|
|
glBindFragDataLocationIndexed(entry.prog_id, 0, 1, "ocol1");
|
2011-12-26 05:15:54 +00:00
|
|
|
}
|
2011-12-11 10:14:02 +00:00
|
|
|
|
2012-12-31 01:34:27 +00:00
|
|
|
// Need to set some attribute locations
|
2013-01-11 20:24:59 +00:00
|
|
|
glBindAttribLocation(entry.prog_id, SHADER_POSITION_ATTRIB, "rawpos");
|
2013-01-14 21:59:08 +00:00
|
|
|
|
2013-01-11 10:59:42 +00:00
|
|
|
glBindAttribLocation(entry.prog_id, SHADER_POSMTX_ATTRIB, "fposmtx");
|
2013-01-14 21:59:08 +00:00
|
|
|
|
2013-01-11 10:59:42 +00:00
|
|
|
glBindAttribLocation(entry.prog_id, SHADER_COLOR0_ATTRIB, "color0");
|
2013-01-14 21:59:08 +00:00
|
|
|
glBindAttribLocation(entry.prog_id, SHADER_COLOR1_ATTRIB, "color1");
|
|
|
|
|
|
|
|
glBindAttribLocation(entry.prog_id, SHADER_NORM0_ATTRIB, "rawnorm0");
|
2013-01-11 10:59:42 +00:00
|
|
|
glBindAttribLocation(entry.prog_id, SHADER_NORM1_ATTRIB, "rawnorm1");
|
|
|
|
glBindAttribLocation(entry.prog_id, SHADER_NORM2_ATTRIB, "rawnorm2");
|
2013-01-14 21:59:08 +00:00
|
|
|
|
|
|
|
for(int i=0; i<8; i++) {
|
|
|
|
char attrib_name[8];
|
|
|
|
snprintf(attrib_name, 8, "tex%d", i);
|
|
|
|
glBindAttribLocation(entry.prog_id, SHADER_TEXTURE0_ATTRIB+i, attrib_name);
|
|
|
|
}
|
2011-12-26 05:15:54 +00:00
|
|
|
}
|
2011-12-11 10:14:02 +00:00
|
|
|
|
2012-12-31 01:34:27 +00:00
|
|
|
|
2011-12-26 05:15:54 +00:00
|
|
|
void ProgramShaderCache::SetBothShaders(GLuint PS, GLuint VS)
|
|
|
|
{
|
|
|
|
CurrentFShader = PS;
|
|
|
|
CurrentVShader = VS;
|
2011-12-11 10:14:02 +00:00
|
|
|
|
2011-12-26 07:58:52 +00:00
|
|
|
if (CurrentFShader == 0 && CurrentVShader == 0)
|
2011-12-11 12:02:04 +00:00
|
|
|
{
|
2011-12-26 05:15:54 +00:00
|
|
|
CurrentProgram = 0;
|
|
|
|
glUseProgram(0);
|
|
|
|
return;
|
2011-12-11 10:32:57 +00:00
|
|
|
}
|
|
|
|
|
2011-12-26 05:15:54 +00:00
|
|
|
// Fragment shaders can survive without Vertex Shaders
|
|
|
|
// We have a valid fragment shader, let's create our program
|
2011-12-26 07:58:52 +00:00
|
|
|
std::pair<u32, u32> ShaderPair = std::make_pair(CurrentFShader, CurrentVShader);
|
2011-12-26 05:15:54 +00:00
|
|
|
PCache::iterator iter = pshaders.find(ShaderPair);
|
|
|
|
if (iter != pshaders.end())
|
2011-12-11 10:32:57 +00:00
|
|
|
{
|
2011-12-26 05:15:54 +00:00
|
|
|
PCacheEntry &entry = iter->second;
|
2011-12-26 07:58:52 +00:00
|
|
|
glUseProgram(entry.prog_id);
|
2011-12-26 05:15:54 +00:00
|
|
|
CurrentShaderProgram = ShaderPair;
|
2011-12-26 07:58:52 +00:00
|
|
|
CurrentProgram = entry.prog_id;
|
2011-12-26 05:15:54 +00:00
|
|
|
return;
|
2011-12-11 11:13:05 +00:00
|
|
|
}
|
2011-12-11 10:08:18 +00:00
|
|
|
|
2011-12-26 05:15:54 +00:00
|
|
|
PCacheEntry entry;
|
2011-12-26 07:58:52 +00:00
|
|
|
entry.Create(CurrentFShader, CurrentVShader);
|
2011-12-26 05:15:54 +00:00
|
|
|
|
|
|
|
// Right, the program is created now
|
|
|
|
// Let's attach everything
|
2011-12-26 07:58:52 +00:00
|
|
|
if (entry.vsid != 0) // attaching zero vertex shader makes it freak out
|
|
|
|
glAttachShader(entry.prog_id, entry.vsid);
|
2011-12-26 05:15:54 +00:00
|
|
|
|
2011-12-26 07:58:52 +00:00
|
|
|
glAttachShader(entry.prog_id, entry.psid);
|
2011-12-26 05:15:54 +00:00
|
|
|
|
|
|
|
if (g_ActiveConfig.backend_info.bSupportsGLSLCache)
|
2011-12-26 07:58:52 +00:00
|
|
|
glProgramParameteri(entry.prog_id, GL_PROGRAM_BINARY_RETRIEVABLE_HINT, GL_TRUE);
|
2011-12-26 05:15:54 +00:00
|
|
|
|
2012-12-31 01:34:27 +00:00
|
|
|
SetProgramBindings(entry);
|
|
|
|
|
2011-12-26 07:58:52 +00:00
|
|
|
glLinkProgram(entry.prog_id);
|
2011-12-26 05:15:54 +00:00
|
|
|
|
2011-12-26 07:58:52 +00:00
|
|
|
glUseProgram(entry.prog_id);
|
2011-12-26 05:15:54 +00:00
|
|
|
|
2011-12-26 07:58:52 +00:00
|
|
|
SetProgramVariables(entry);
|
2011-12-26 05:15:54 +00:00
|
|
|
|
|
|
|
pshaders[ShaderPair] = entry;
|
|
|
|
CurrentShaderProgram = ShaderPair;
|
2011-12-26 07:58:52 +00:00
|
|
|
CurrentProgram = entry.prog_id;
|
2011-12-26 05:15:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void ProgramShaderCache::SetMultiPSConstant4fv(unsigned int offset, const float *f, unsigned int count)
|
|
|
|
{
|
2013-01-14 12:58:11 +00:00
|
|
|
s_ubo_dirty = true;
|
|
|
|
memcpy(s_ubo_buffer+(offset*4), f, count*4*sizeof(float));
|
2011-12-26 05:15:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void ProgramShaderCache::SetMultiVSConstant4fv(unsigned int offset, const float *f, unsigned int count)
|
|
|
|
{
|
2013-01-14 12:58:11 +00:00
|
|
|
s_ubo_dirty = true;
|
|
|
|
memcpy(s_ubo_buffer+(offset*4)+s_vs_data_offset/sizeof(float), f, count*4*sizeof(float));
|
2011-12-26 05:15:54 +00:00
|
|
|
}
|
|
|
|
|
2013-01-14 12:58:11 +00:00
|
|
|
void ProgramShaderCache::UploadConstants()
|
|
|
|
{
|
|
|
|
if(s_ubo_dirty)
|
|
|
|
glBufferData(GL_UNIFORM_BUFFER, s_ubo_buffer_size, s_ubo_buffer, GL_STREAM_DRAW);
|
|
|
|
s_ubo_dirty = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-12-26 05:15:54 +00:00
|
|
|
GLuint ProgramShaderCache::GetCurrentProgram(void)
|
|
|
|
{
|
|
|
|
return CurrentProgram;
|
|
|
|
}
|
|
|
|
|
2011-12-26 07:58:52 +00:00
|
|
|
ProgramShaderCache::PCacheEntry ProgramShaderCache::GetShaderProgram(void)
|
2011-12-26 05:15:54 +00:00
|
|
|
{
|
2011-12-26 07:58:52 +00:00
|
|
|
return pshaders[CurrentShaderProgram];
|
2011-12-26 05:15:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void ProgramShaderCache::Init(void)
|
|
|
|
{
|
|
|
|
// We have to get the UBO alignment here because
|
|
|
|
// if we generate a buffer that isn't aligned
|
|
|
|
// then the UBO will fail.
|
|
|
|
if (g_ActiveConfig.backend_info.bSupportsGLSLUBO)
|
2011-12-11 10:08:18 +00:00
|
|
|
{
|
2011-12-26 05:15:54 +00:00
|
|
|
GLint Align;
|
|
|
|
glGetIntegerv(GL_UNIFORM_BUFFER_OFFSET_ALIGNMENT, &Align);
|
|
|
|
|
|
|
|
GLintptr const ps_data_size = ROUND_UP(C_PENVCONST_END * sizeof(float) * 4, Align);
|
|
|
|
GLintptr const vs_data_size = ROUND_UP(C_VENVCONST_END * sizeof(float) * 4, Align);
|
|
|
|
s_vs_data_offset = ps_data_size;
|
2013-01-14 12:58:11 +00:00
|
|
|
s_ubo_buffer_size = ps_data_size + vs_data_size;
|
2011-12-26 05:15:54 +00:00
|
|
|
|
|
|
|
// 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
|
|
|
|
glGenBuffers(1, &s_ps_vs_ubo);
|
|
|
|
glBindBuffer(GL_UNIFORM_BUFFER, s_ps_vs_ubo);
|
2013-01-14 12:58:11 +00:00
|
|
|
glBufferData(GL_UNIFORM_BUFFER, s_ubo_buffer_size, NULL, GL_STREAM_DRAW);
|
2011-12-26 05:15:54 +00:00
|
|
|
|
|
|
|
// Now bind the buffer to the index point
|
|
|
|
// We know PS is 0 since we have it statically set in the shader
|
|
|
|
// Repeat for VS shader
|
|
|
|
glBindBufferRange(GL_UNIFORM_BUFFER, 1, s_ps_vs_ubo, 0, ps_data_size);
|
|
|
|
glBindBufferRange(GL_UNIFORM_BUFFER, 2, s_ps_vs_ubo, s_vs_data_offset, vs_data_size);
|
2013-01-14 12:58:11 +00:00
|
|
|
|
|
|
|
s_ubo_buffer = new float[s_ubo_buffer_size/sizeof(float)];
|
|
|
|
s_ubo_dirty = true;
|
2011-12-11 10:08:18 +00:00
|
|
|
}
|
2011-12-26 05:15:54 +00:00
|
|
|
|
|
|
|
// Read our shader cache, only if supported
|
|
|
|
if (g_ActiveConfig.backend_info.bSupportsGLSLCache)
|
2011-12-11 10:19:11 +00:00
|
|
|
{
|
2012-01-18 18:22:03 +00:00
|
|
|
PCacheEntry::prog_format = PCacheEntry::SetProgramFormat();
|
|
|
|
|
2011-12-26 05:15:54 +00:00
|
|
|
char cache_filename[MAX_PATH];
|
|
|
|
sprintf(cache_filename, "%sogl-%s-shaders.cache", File::GetUserPath(D_SHADERCACHE_IDX).c_str(),
|
|
|
|
SConfig::GetInstance().m_LocalCoreStartupParameter.m_strUniqueID.c_str());
|
2012-01-18 18:22:03 +00:00
|
|
|
|
2011-12-26 05:15:54 +00:00
|
|
|
ProgramShaderCacheInserter inserter;
|
|
|
|
g_program_disk_cache.OpenAndRead(cache_filename, inserter);
|
2011-12-11 10:19:11 +00:00
|
|
|
}
|
2011-12-26 05:15:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void ProgramShaderCache::Shutdown(void)
|
|
|
|
{
|
|
|
|
if (g_ActiveConfig.backend_info.bSupportsGLSLCache)
|
2011-12-11 10:08:18 +00:00
|
|
|
{
|
2011-12-11 10:14:02 +00:00
|
|
|
PCache::iterator iter = pshaders.begin();
|
2011-12-11 10:11:57 +00:00
|
|
|
for (; iter != pshaders.end(); ++iter)
|
2011-12-26 15:27:18 +00:00
|
|
|
{
|
2011-12-26 07:58:52 +00:00
|
|
|
g_program_disk_cache.Append(iter->second.uid, iter->second.GetProgram(), iter->second.Size());
|
2011-12-26 15:27:18 +00:00
|
|
|
iter->second.FreeProgram();
|
|
|
|
}
|
2011-12-11 10:11:57 +00:00
|
|
|
|
2011-12-26 05:15:54 +00:00
|
|
|
g_program_disk_cache.Sync();
|
|
|
|
g_program_disk_cache.Close();
|
|
|
|
}
|
|
|
|
|
|
|
|
PCache::iterator iter = pshaders.begin();
|
|
|
|
for (; iter != pshaders.end(); ++iter)
|
|
|
|
iter->second.Destroy();
|
|
|
|
pshaders.clear();
|
|
|
|
|
|
|
|
if (g_ActiveConfig.backend_info.bSupportsGLSLUBO)
|
|
|
|
{
|
|
|
|
glBindBuffer(GL_UNIFORM_BUFFER, 0);
|
|
|
|
glDeleteBuffers(1, &s_ps_vs_ubo);
|
|
|
|
s_ps_vs_ubo = 0;
|
2013-01-14 12:58:11 +00:00
|
|
|
delete [] s_ubo_buffer;
|
|
|
|
s_ubo_buffer = 0;
|
2011-12-11 10:08:18 +00:00
|
|
|
}
|
2011-12-01 03:00:21 +00:00
|
|
|
}
|
|
|
|
|
2011-12-26 05:15:54 +00:00
|
|
|
} // namespace OGL
|