2009-07-28 21:32:10 +00:00
|
|
|
// Copyright (C) 2003 Dolphin Project.
|
2009-02-23 06:15:48 +00:00
|
|
|
|
|
|
|
// 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/
|
|
|
|
|
|
|
|
#include "Globals.h"
|
|
|
|
|
|
|
|
#include "GLUtil.h"
|
|
|
|
|
|
|
|
#include <cmath>
|
|
|
|
|
|
|
|
#include "Statistics.h"
|
2009-09-13 09:23:30 +00:00
|
|
|
#include "VideoConfig.h"
|
2009-02-23 06:15:48 +00:00
|
|
|
#include "ImageWrite.h"
|
|
|
|
#include "Common.h"
|
|
|
|
#include "Render.h"
|
|
|
|
#include "VertexShaderGen.h"
|
2011-12-01 03:00:21 +00:00
|
|
|
#include "ProgramShaderCache.h"
|
2009-02-23 06:15:48 +00:00
|
|
|
#include "PixelShaderCache.h"
|
|
|
|
#include "PixelShaderManager.h"
|
2011-12-11 11:15:08 +00:00
|
|
|
#include "OnScreenDisplay.h"
|
|
|
|
#include "StringUtil.h"
|
2010-02-02 21:56:29 +00:00
|
|
|
#include "FileUtil.h"
|
2010-12-05 14:15:36 +00:00
|
|
|
#include "Debugger.h"
|
2009-02-23 06:15:48 +00:00
|
|
|
|
2011-01-29 20:16:51 +00:00
|
|
|
namespace OGL
|
|
|
|
{
|
|
|
|
|
2009-02-23 06:15:48 +00:00
|
|
|
static int s_nMaxPixelInstructions;
|
2010-09-30 15:24:34 +00:00
|
|
|
PixelShaderCache::PSCache PixelShaderCache::PixelShaders;
|
2009-02-23 06:15:48 +00:00
|
|
|
PIXELSHADERUID PixelShaderCache::s_curuid;
|
2009-03-01 00:57:39 +00:00
|
|
|
bool PixelShaderCache::s_displayCompileAlert;
|
2009-09-26 12:39:12 +00:00
|
|
|
GLuint PixelShaderCache::CurrentShader;
|
|
|
|
bool PixelShaderCache::ShaderEnabled;
|
2009-02-23 06:15:48 +00:00
|
|
|
|
2011-09-29 21:32:05 +00:00
|
|
|
PixelShaderCache::PSCacheEntry* PixelShaderCache::last_entry = NULL;
|
|
|
|
PIXELSHADERUID PixelShaderCache::last_uid;
|
2009-09-10 05:02:31 +00:00
|
|
|
|
2009-02-23 06:15:48 +00:00
|
|
|
void PixelShaderCache::Init()
|
|
|
|
{
|
2011-12-26 05:15:54 +00:00
|
|
|
ShaderEnabled = true;
|
|
|
|
CurrentShader = 0;
|
|
|
|
last_entry = NULL;
|
|
|
|
GL_REPORT_ERRORD();
|
2011-12-01 03:00:21 +00:00
|
|
|
|
2011-12-26 05:15:54 +00:00
|
|
|
s_displayCompileAlert = true;
|
2009-02-23 06:15:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void PixelShaderCache::Shutdown()
|
|
|
|
{
|
2011-12-26 05:15:54 +00:00
|
|
|
PSCache::iterator iter = PixelShaders.begin();
|
|
|
|
for (; iter != PixelShaders.end(); iter++)
|
|
|
|
iter->second.Destroy();
|
|
|
|
PixelShaders.clear();
|
2009-02-23 06:15:48 +00:00
|
|
|
}
|
|
|
|
|
2010-10-21 05:22:18 +00:00
|
|
|
FRAGMENTSHADER* PixelShaderCache::SetShader(DSTALPHA_MODE dstAlphaMode, u32 components)
|
2009-02-23 06:15:48 +00:00
|
|
|
{
|
2011-12-26 05:15:54 +00:00
|
|
|
PIXELSHADERUID uid;
|
|
|
|
GetPixelShaderId(&uid, dstAlphaMode, components);
|
2011-12-01 03:00:21 +00:00
|
|
|
|
2011-12-26 05:15:54 +00:00
|
|
|
// Check if the shader is already set
|
|
|
|
if (last_entry)
|
2011-12-11 10:08:18 +00:00
|
|
|
{
|
2011-12-26 05:15:54 +00:00
|
|
|
if (uid == last_uid)
|
|
|
|
{
|
|
|
|
GFX_DEBUGGER_PAUSE_AT(NEXT_PIXEL_SHADER_CHANGE, true);
|
|
|
|
ValidatePixelShaderIDs(API_OPENGL, last_entry->safe_uid, last_entry->shader.strprog, dstAlphaMode, components);
|
|
|
|
return &last_entry->shader;
|
|
|
|
}
|
2011-12-11 10:08:18 +00:00
|
|
|
}
|
2011-12-01 03:00:21 +00:00
|
|
|
|
2011-12-26 05:15:54 +00:00
|
|
|
last_uid = uid;
|
2011-12-01 03:00:21 +00:00
|
|
|
|
2011-12-26 05:15:54 +00:00
|
|
|
PSCache::iterator iter = PixelShaders.find(uid);
|
|
|
|
if (iter != PixelShaders.end())
|
|
|
|
{
|
|
|
|
PSCacheEntry &entry = iter->second;
|
|
|
|
last_entry = &entry;
|
2011-12-01 03:00:21 +00:00
|
|
|
|
2011-12-26 05:15:54 +00:00
|
|
|
GFX_DEBUGGER_PAUSE_AT(NEXT_PIXEL_SHADER_CHANGE, true);
|
|
|
|
ValidatePixelShaderIDs(API_OPENGL, entry.safe_uid, entry.shader.strprog, dstAlphaMode, components);
|
|
|
|
return &last_entry->shader;
|
|
|
|
}
|
2011-12-01 03:00:21 +00:00
|
|
|
|
2011-12-26 05:15:54 +00:00
|
|
|
// Make an entry in the table
|
|
|
|
PSCacheEntry& newentry = PixelShaders[uid];
|
|
|
|
last_entry = &newentry;
|
2012-12-28 04:46:29 +00:00
|
|
|
const char *code = GeneratePixelShaderCode(dstAlphaMode, API_OPENGL, components);
|
2011-12-01 03:00:21 +00:00
|
|
|
|
2011-12-26 05:15:54 +00:00
|
|
|
if (g_ActiveConfig.bEnableShaderDebugging && code)
|
|
|
|
{
|
|
|
|
GetSafePixelShaderId(&newentry.safe_uid, dstAlphaMode, components);
|
|
|
|
newentry.shader.strprog = code;
|
|
|
|
}
|
2009-02-23 06:15:48 +00:00
|
|
|
|
|
|
|
#if defined(_DEBUG) || defined(DEBUGFAST)
|
2011-12-26 05:15:54 +00:00
|
|
|
if (g_ActiveConfig.iLog & CONF_SAVESHADERS && code) {
|
|
|
|
static int counter = 0;
|
|
|
|
char szTemp[MAX_PATH];
|
|
|
|
sprintf(szTemp, "%sps_%04i.txt", File::GetUserPath(D_DUMP_IDX).c_str(), counter++);
|
2011-12-01 03:00:21 +00:00
|
|
|
|
2011-12-26 05:15:54 +00:00
|
|
|
SaveData(szTemp, code);
|
|
|
|
}
|
2009-02-23 06:15:48 +00:00
|
|
|
#endif
|
|
|
|
|
2011-12-26 05:15:54 +00:00
|
|
|
if (!code || !CompilePixelShader(newentry.shader, code)) {
|
|
|
|
GFX_DEBUGGER_PAUSE_AT(NEXT_ERROR, true);
|
|
|
|
return NULL;
|
|
|
|
}
|
2011-12-01 03:00:21 +00:00
|
|
|
|
2011-12-26 05:15:54 +00:00
|
|
|
INCSTAT(stats.numPixelShadersCreated);
|
|
|
|
SETSTAT(stats.numPixelShadersAlive, PixelShaders.size());
|
|
|
|
GFX_DEBUGGER_PAUSE_AT(NEXT_PIXEL_SHADER_CHANGE, true);
|
|
|
|
return &last_entry->shader;
|
2009-02-23 06:15:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool PixelShaderCache::CompilePixelShader(FRAGMENTSHADER& ps, const char* pstrprogram)
|
2011-12-03 00:26:15 +00:00
|
|
|
{
|
2011-12-03 00:31:06 +00:00
|
|
|
GLuint result = glCreateShader(GL_FRAGMENT_SHADER);
|
|
|
|
|
|
|
|
glShaderSource(result, 1, &pstrprogram, NULL);
|
|
|
|
glCompileShader(result);
|
2013-01-26 17:24:24 +00:00
|
|
|
#if defined(_DEBUG) || defined(DEBUGFAST) || defined(DEBUG_GLSL)
|
2013-01-18 11:49:48 +00:00
|
|
|
GLsizei length = 0;
|
|
|
|
glGetShaderiv(result, GL_INFO_LOG_LENGTH, &length);
|
|
|
|
if (length > 1)
|
|
|
|
{
|
|
|
|
GLsizei charsWritten;
|
|
|
|
GLchar* infoLog = new GLchar[length];
|
|
|
|
glGetShaderInfoLog(result, length, &charsWritten, infoLog);
|
|
|
|
ERROR_LOG(VIDEO, "PS Shader info log:\n%s", infoLog);
|
|
|
|
char szTemp[MAX_PATH];
|
|
|
|
sprintf(szTemp, "ps_%d.txt", result);
|
|
|
|
FILE *fp = fopen(szTemp, "wb");
|
2013-02-07 11:47:41 +00:00
|
|
|
fwrite(infoLog, strlen(infoLog), 1, fp);
|
2013-01-18 11:49:48 +00:00
|
|
|
fwrite(pstrprogram, strlen(pstrprogram), 1, fp);
|
|
|
|
fclose(fp);
|
|
|
|
delete[] infoLog;
|
|
|
|
}
|
2011-12-03 00:31:06 +00:00
|
|
|
|
|
|
|
GLint compileStatus;
|
|
|
|
glGetShaderiv(result, GL_COMPILE_STATUS, &compileStatus);
|
|
|
|
if (compileStatus != GL_TRUE)
|
|
|
|
{
|
|
|
|
// Compile failed
|
|
|
|
ERROR_LOG(VIDEO, "Shader compilation failed; see info log");
|
2011-12-10 14:07:13 +00:00
|
|
|
|
2011-12-03 00:31:06 +00:00
|
|
|
// Don't try to use this shader
|
|
|
|
glDeleteShader(result);
|
|
|
|
return false;
|
|
|
|
}
|
2013-01-26 17:24:24 +00:00
|
|
|
#endif
|
2011-12-03 00:31:06 +00:00
|
|
|
(void)GL_REPORT_ERROR();
|
|
|
|
ps.glprogid = result;
|
|
|
|
return true;
|
2011-12-03 00:26:15 +00:00
|
|
|
}
|
2011-12-26 05:15:54 +00:00
|
|
|
|
2011-12-01 03:00:21 +00:00
|
|
|
void SetPSConstant4fvByName(const char * name, unsigned int offset, const float *f, const unsigned int count = 1)
|
|
|
|
{
|
2011-12-26 07:58:52 +00:00
|
|
|
ProgramShaderCache::PCacheEntry tmp = ProgramShaderCache::GetShaderProgram();
|
2011-12-26 05:15:54 +00:00
|
|
|
for (int a = 0; a < NUM_UNIFORMS; ++a)
|
2011-12-11 10:08:18 +00:00
|
|
|
{
|
2011-12-26 05:15:54 +00:00
|
|
|
if (!strcmp(name, UniformNames[a]))
|
|
|
|
{
|
|
|
|
if (tmp.UniformLocations[a] == -1)
|
|
|
|
return;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
glUniform4fv(tmp.UniformLocations[a] + offset, count, f);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2011-12-11 10:08:18 +00:00
|
|
|
}
|
2011-12-01 03:00:21 +00:00
|
|
|
}
|
2011-12-10 13:38:30 +00:00
|
|
|
|
2012-12-24 17:09:52 +00:00
|
|
|
// Renderer functions
|
|
|
|
void Renderer::SetPSConstant4f(unsigned int const_number, float f1, float f2, float f3, float f4)
|
2011-12-01 03:00:21 +00:00
|
|
|
{
|
2011-12-26 05:15:54 +00:00
|
|
|
float const f[4] = {f1, f2, f3, f4};
|
|
|
|
|
2011-12-09 23:30:05 +00:00
|
|
|
if (g_ActiveConfig.backend_info.bSupportsGLSLUBO)
|
|
|
|
{
|
2011-12-11 10:11:57 +00:00
|
|
|
ProgramShaderCache::SetMultiPSConstant4fv(const_number, f, 1);
|
|
|
|
return;
|
2011-12-09 23:30:05 +00:00
|
|
|
}
|
2011-12-26 05:15:54 +00:00
|
|
|
for (unsigned int a = 0; a < 10; ++a)
|
2011-12-11 10:08:18 +00:00
|
|
|
{
|
2011-12-26 05:15:54 +00:00
|
|
|
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);
|
|
|
|
return;
|
|
|
|
}
|
2011-12-11 10:08:18 +00:00
|
|
|
}
|
2011-12-01 03:00:21 +00:00
|
|
|
}
|
|
|
|
|
2012-12-24 17:09:52 +00:00
|
|
|
void Renderer::SetPSConstant4fv(unsigned int const_number, const float *f)
|
2011-12-01 03:00:21 +00:00
|
|
|
{
|
2011-12-09 23:30:05 +00:00
|
|
|
if (g_ActiveConfig.backend_info.bSupportsGLSLUBO)
|
|
|
|
{
|
2011-12-11 10:11:57 +00:00
|
|
|
ProgramShaderCache::SetMultiPSConstant4fv(const_number, f, 1);
|
|
|
|
return;
|
2011-12-09 23:30:05 +00:00
|
|
|
}
|
2011-12-26 05:15:54 +00:00
|
|
|
for (unsigned int a = 0; a < 10; ++a)
|
2011-12-11 10:08:18 +00:00
|
|
|
{
|
2011-12-26 05:15:54 +00:00
|
|
|
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);
|
|
|
|
return;
|
|
|
|
}
|
2011-12-11 10:08:18 +00:00
|
|
|
}
|
2011-12-01 03:00:21 +00:00
|
|
|
}
|
|
|
|
|
2012-12-24 17:09:52 +00:00
|
|
|
void Renderer::SetMultiPSConstant4fv(unsigned int const_number, unsigned int count, const float *f)
|
2011-12-01 03:00:21 +00:00
|
|
|
{
|
2011-12-09 23:30:05 +00:00
|
|
|
if (g_ActiveConfig.backend_info.bSupportsGLSLUBO)
|
|
|
|
{
|
2011-12-11 10:11:57 +00:00
|
|
|
ProgramShaderCache::SetMultiPSConstant4fv(const_number, f, count);
|
|
|
|
return;
|
2011-12-09 23:30:05 +00:00
|
|
|
}
|
2011-12-26 05:15:54 +00:00
|
|
|
for (unsigned int a = 0; a < 10; ++a)
|
2011-12-11 10:08:18 +00:00
|
|
|
{
|
2011-12-26 05:15:54 +00:00
|
|
|
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);
|
|
|
|
return;
|
|
|
|
}
|
2011-12-11 10:08:18 +00:00
|
|
|
}
|
2011-12-01 03:00:21 +00:00
|
|
|
}
|
2011-01-31 01:28:32 +00:00
|
|
|
} // namespace OGL
|