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/
|
|
|
|
|
2013-01-28 21:18:54 +00:00
|
|
|
#ifndef PROGRAM_SHADER_CACHE_H_
|
|
|
|
#define PROGRAM_SHADER_CACHE_H_
|
2011-12-01 03:00:21 +00:00
|
|
|
|
|
|
|
#include "GLUtil.h"
|
|
|
|
|
|
|
|
#include "PixelShaderGen.h"
|
|
|
|
#include "VertexShaderGen.h"
|
|
|
|
|
2011-12-21 06:15:48 +00:00
|
|
|
#include "LinearDiskCache.h"
|
|
|
|
#include "ConfigManager.h"
|
|
|
|
|
2011-12-26 07:58:52 +00:00
|
|
|
namespace OGL
|
2011-12-26 05:15:54 +00:00
|
|
|
{
|
2011-12-01 03:00:21 +00:00
|
|
|
|
2013-02-13 12:12:19 +00:00
|
|
|
template<bool safe>
|
|
|
|
class _SHADERUID
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
_VERTEXSHADERUID<safe> vuid;
|
|
|
|
_PIXELSHADERUID<safe> puid;
|
|
|
|
|
|
|
|
_SHADERUID() {}
|
|
|
|
|
|
|
|
_SHADERUID(const _SHADERUID& r) : vuid(r.vuid), puid(r.puid) {}
|
|
|
|
|
|
|
|
bool operator <(const _SHADERUID& r) const
|
|
|
|
{
|
|
|
|
if(puid < r.puid) return true;
|
|
|
|
if(r.puid < puid) return false;
|
|
|
|
if(vuid < r.vuid) return true;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool operator ==(const _SHADERUID& r) const
|
|
|
|
{
|
|
|
|
return puid == r.puid && vuid == r.vuid;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
typedef _SHADERUID<false> SHADERUID;
|
|
|
|
typedef _SHADERUID<true> SHADERUIDSAFE;
|
|
|
|
|
|
|
|
|
2013-01-18 23:12:02 +00:00
|
|
|
const int NUM_UNIFORMS = 19;
|
2011-12-26 07:58:52 +00:00
|
|
|
extern const char *UniformNames[NUM_UNIFORMS];
|
2013-02-13 12:12:19 +00:00
|
|
|
|
|
|
|
struct SHADER
|
|
|
|
{
|
|
|
|
SHADER() : glprogid(0) { }
|
|
|
|
void Destroy()
|
|
|
|
{
|
|
|
|
glDeleteProgram(glprogid);
|
|
|
|
glprogid = 0;
|
|
|
|
}
|
|
|
|
GLuint glprogid; // opengl program id
|
|
|
|
|
|
|
|
std::string strvprog, strpprog;
|
|
|
|
GLint UniformLocations[NUM_UNIFORMS];
|
|
|
|
|
|
|
|
void SetProgramVariables();
|
|
|
|
void SetProgramBindings();
|
|
|
|
void Bind();
|
|
|
|
};
|
2011-12-26 07:58:52 +00:00
|
|
|
|
|
|
|
class ProgramShaderCache
|
2011-12-01 03:00:21 +00:00
|
|
|
{
|
|
|
|
public:
|
2011-12-11 10:08:18 +00:00
|
|
|
|
2011-12-26 07:58:52 +00:00
|
|
|
struct PCacheEntry
|
2011-12-11 10:08:18 +00:00
|
|
|
{
|
2013-02-13 12:12:19 +00:00
|
|
|
SHADER shader;
|
|
|
|
PIXELSHADERUIDSAFE safe_uid;
|
|
|
|
|
2011-12-29 15:25:03 +00:00
|
|
|
static GLenum prog_format;
|
2011-12-26 07:58:52 +00:00
|
|
|
u8 *binary;
|
|
|
|
GLint binary_size;
|
2013-02-13 12:12:19 +00:00
|
|
|
|
|
|
|
PCacheEntry() :binary(NULL), binary_size(0) { }
|
2011-12-01 03:00:21 +00:00
|
|
|
|
2013-02-13 12:12:19 +00:00
|
|
|
~PCacheEntry() {}
|
2011-12-01 03:00:21 +00:00
|
|
|
|
2011-12-26 07:58:52 +00:00
|
|
|
void Destroy()
|
|
|
|
{
|
2013-02-13 12:12:19 +00:00
|
|
|
shader.Destroy();
|
|
|
|
FreeProgram();
|
2011-12-26 07:58:52 +00:00
|
|
|
}
|
2011-12-26 05:15:54 +00:00
|
|
|
|
2011-12-26 07:58:52 +00:00
|
|
|
void UpdateSize()
|
|
|
|
{
|
|
|
|
if (binary_size == 0)
|
2013-02-13 12:12:19 +00:00
|
|
|
glGetProgramiv(shader.glprogid, GL_PROGRAM_BINARY_LENGTH, &binary_size);
|
2011-12-26 07:58:52 +00:00
|
|
|
}
|
2011-12-01 03:00:21 +00:00
|
|
|
|
2011-12-29 15:25:03 +00:00
|
|
|
// No idea how necessary this is
|
|
|
|
static GLenum SetProgramFormat()
|
|
|
|
{
|
|
|
|
GLint Supported;
|
|
|
|
glGetIntegerv(GL_NUM_PROGRAM_BINARY_FORMATS, &Supported);
|
|
|
|
|
|
|
|
GLint *Formats = new GLint[Supported];
|
|
|
|
glGetIntegerv(GL_PROGRAM_BINARY_FORMATS, Formats);
|
|
|
|
// We don't really care about format
|
|
|
|
GLenum prog_format = (GLenum)Formats[0];
|
|
|
|
delete[] Formats;
|
|
|
|
return prog_format;
|
|
|
|
}
|
|
|
|
|
2011-12-26 07:58:52 +00:00
|
|
|
u8 *GetProgram()
|
2011-12-26 05:15:54 +00:00
|
|
|
{
|
2011-12-26 07:58:52 +00:00
|
|
|
UpdateSize();
|
|
|
|
FreeProgram();
|
|
|
|
binary = new u8[binary_size];
|
2013-02-13 12:12:19 +00:00
|
|
|
glGetProgramBinary(shader.glprogid, binary_size, NULL, &prog_format, binary);
|
2011-12-26 07:58:52 +00:00
|
|
|
return binary;
|
2011-12-11 10:14:02 +00:00
|
|
|
}
|
2011-12-26 05:15:54 +00:00
|
|
|
|
2011-12-26 07:58:52 +00:00
|
|
|
void FreeProgram()
|
2011-12-21 06:15:48 +00:00
|
|
|
{
|
2011-12-26 07:58:52 +00:00
|
|
|
delete [] binary;
|
2011-12-26 15:27:18 +00:00
|
|
|
binary = NULL;
|
2011-12-21 06:15:48 +00:00
|
|
|
}
|
2011-12-26 05:15:54 +00:00
|
|
|
|
2011-12-21 06:15:48 +00:00
|
|
|
GLint Size()
|
|
|
|
{
|
2011-12-26 07:58:52 +00:00
|
|
|
UpdateSize();
|
|
|
|
return binary_size;
|
2011-12-21 06:15:48 +00:00
|
|
|
}
|
|
|
|
};
|
2011-12-26 05:15:54 +00:00
|
|
|
|
2011-12-26 07:58:52 +00:00
|
|
|
static PCacheEntry GetShaderProgram(void);
|
|
|
|
static GLuint GetCurrentProgram(void);
|
2013-02-13 12:12:19 +00:00
|
|
|
static SHADER* SetShader(DSTALPHA_MODE dstAlphaMode, u32 components);
|
|
|
|
static void GetShaderId(SHADERUID *uid, DSTALPHA_MODE dstAlphaMode, u32 components);
|
|
|
|
static void GetSafeShaderId(SHADERUIDSAFE *uid, DSTALPHA_MODE dstAlphaMode, u32 components);
|
|
|
|
static void ValidateShaderIDs(PCacheEntry *entry, DSTALPHA_MODE dstAlphaMode, u32 components);
|
|
|
|
|
|
|
|
static bool CompileShader(SHADER &shader, const char* vcode, const char* pcode);
|
|
|
|
static GLuint CompileSingleShader(GLuint type, const char *code);
|
2011-12-26 07:58:52 +00:00
|
|
|
|
|
|
|
static void SetMultiPSConstant4fv(unsigned int offset, const float *f, unsigned int count);
|
|
|
|
static void SetMultiVSConstant4fv(unsigned int offset, const float *f, unsigned int count);
|
2013-01-14 12:58:11 +00:00
|
|
|
static void UploadConstants();
|
2011-12-26 07:58:52 +00:00
|
|
|
|
|
|
|
static void Init(void);
|
|
|
|
static void Shutdown(void);
|
|
|
|
|
|
|
|
private:
|
2013-02-13 12:12:19 +00:00
|
|
|
class ProgramShaderCacheInserter : public LinearDiskCacheReader<SHADERUID, u8>
|
2011-12-21 06:15:48 +00:00
|
|
|
{
|
2011-12-26 05:15:54 +00:00
|
|
|
public:
|
2013-02-13 12:12:19 +00:00
|
|
|
void Read(const SHADERUID &key, const u8 *value, u32 value_size);
|
2011-12-11 10:08:18 +00:00
|
|
|
};
|
2011-12-26 05:15:54 +00:00
|
|
|
|
2013-02-13 12:12:19 +00:00
|
|
|
typedef std::map<SHADERUID, PCacheEntry> PCache;
|
2011-12-11 10:08:18 +00:00
|
|
|
|
|
|
|
static PCache pshaders;
|
2013-02-13 12:12:19 +00:00
|
|
|
static PCacheEntry* last_entry;
|
|
|
|
static SHADERUID last_uid;
|
2011-12-11 10:08:18 +00:00
|
|
|
|
2011-12-11 12:02:04 +00:00
|
|
|
static GLintptr s_vs_data_offset;
|
2013-01-31 22:11:53 +00:00
|
|
|
static u8 *s_ubo_buffer;
|
2013-01-14 12:58:11 +00:00
|
|
|
static u32 s_ubo_buffer_size;
|
|
|
|
static bool s_ubo_dirty;
|
2011-12-01 03:00:21 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace OGL
|
2013-01-28 21:18:54 +00:00
|
|
|
#endif
|