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/
|
|
|
|
|
|
|
|
#ifndef _ProgramShaderCache_H_
|
|
|
|
#define _ProgramShaderCache_H_
|
|
|
|
|
|
|
|
#include "GLUtil.h"
|
|
|
|
|
|
|
|
#include "VertexShaderCache.h"
|
|
|
|
#include "PixelShaderCache.h"
|
|
|
|
#include "PixelShaderGen.h"
|
|
|
|
#include "VertexShaderGen.h"
|
|
|
|
|
2011-12-21 06:15:48 +00:00
|
|
|
#include "LinearDiskCache.h"
|
|
|
|
#include "ConfigManager.h"
|
|
|
|
|
2011-12-11 10:08:18 +00:00
|
|
|
union PID
|
|
|
|
{
|
2011-12-11 10:14:02 +00:00
|
|
|
struct {
|
|
|
|
GLuint vsid, psid;
|
|
|
|
};
|
|
|
|
u64 id;
|
2011-12-11 10:08:18 +00:00
|
|
|
};
|
2011-12-01 03:00:21 +00:00
|
|
|
|
|
|
|
class PROGRAMUID
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
2011-12-11 10:08:18 +00:00
|
|
|
PID uid;
|
|
|
|
|
|
|
|
PROGRAMUID()
|
|
|
|
{
|
2011-12-11 10:14:02 +00:00
|
|
|
uid.id = 0;
|
2011-12-11 10:08:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
PROGRAMUID(const PROGRAMUID& r)
|
|
|
|
{
|
2011-12-11 10:14:02 +00:00
|
|
|
uid.id = r.uid.id;
|
2011-12-11 10:08:18 +00:00
|
|
|
}
|
|
|
|
PROGRAMUID(GLuint _v, GLuint _p)
|
|
|
|
{
|
2011-12-11 10:14:02 +00:00
|
|
|
uid.vsid = _v;
|
|
|
|
uid.psid = _p;
|
2011-12-11 10:08:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int GetNumValues() const
|
|
|
|
{
|
2011-12-11 10:14:02 +00:00
|
|
|
return uid.id;
|
2011-12-11 10:08:18 +00:00
|
|
|
}
|
2011-12-01 03:00:21 +00:00
|
|
|
};
|
|
|
|
void GetProgramShaderId(PROGRAMUID *uid, GLuint _v, GLuint _p);
|
|
|
|
|
|
|
|
namespace OGL
|
|
|
|
{
|
|
|
|
#define NUM_UNIFORMS 27
|
|
|
|
extern const char *UniformNames[NUM_UNIFORMS];
|
2011-12-21 06:15:48 +00:00
|
|
|
extern GLenum ProgramFormat;
|
2011-12-01 03:00:21 +00:00
|
|
|
|
|
|
|
struct PROGRAMSHADER
|
|
|
|
{
|
2011-12-21 06:15:48 +00:00
|
|
|
PROGRAMSHADER() : glprogid(0), vsid(0), psid(0), binaryLength(0){}
|
2011-12-11 10:08:18 +00:00
|
|
|
GLuint glprogid; // opengl program id
|
|
|
|
GLuint vsid, psid;
|
2011-12-24 08:19:30 +00:00
|
|
|
PROGRAMUID uid;
|
2011-12-11 10:08:18 +00:00
|
|
|
GLint UniformLocations[NUM_UNIFORMS];
|
2011-12-21 06:15:48 +00:00
|
|
|
GLint binaryLength;
|
|
|
|
u8 *Data()
|
|
|
|
{
|
2011-12-24 06:37:13 +00:00
|
|
|
#ifdef GLEW_VERSION_4_0
|
2011-12-21 06:15:48 +00:00
|
|
|
glGetProgramiv(glprogid, GL_PROGRAM_BINARY_LENGTH, &binaryLength);
|
|
|
|
u8* binary = (u8*)malloc(binaryLength);
|
|
|
|
glGetProgramBinary(glprogid, binaryLength, NULL, &ProgramFormat, binary);
|
|
|
|
return binary;
|
2011-12-24 06:37:13 +00:00
|
|
|
#else
|
|
|
|
return NULL;
|
|
|
|
#endif
|
2011-12-21 06:15:48 +00:00
|
|
|
}
|
|
|
|
GLint Size()
|
|
|
|
{
|
2011-12-24 06:37:13 +00:00
|
|
|
#ifdef GLEW_VERSION_4_0
|
2011-12-21 06:15:48 +00:00
|
|
|
if(!binaryLength)
|
|
|
|
glGetProgramiv(glprogid, GL_PROGRAM_BINARY_LENGTH, &binaryLength);
|
|
|
|
return binaryLength;
|
2011-12-24 06:37:13 +00:00
|
|
|
#else
|
|
|
|
return 0;
|
|
|
|
#endif
|
2011-12-21 06:15:48 +00:00
|
|
|
}
|
2011-12-01 03:00:21 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
class ProgramShaderCache
|
|
|
|
{
|
2011-12-11 10:08:18 +00:00
|
|
|
struct PCacheEntry
|
|
|
|
{
|
2011-12-11 10:14:02 +00:00
|
|
|
PROGRAMSHADER program;
|
|
|
|
int frameCount;
|
|
|
|
PCacheEntry() : frameCount(0) {}
|
|
|
|
void Destroy() {
|
|
|
|
glDeleteProgram(program.glprogid);
|
|
|
|
program.glprogid = 0;
|
|
|
|
}
|
2011-12-21 06:15:48 +00:00
|
|
|
u8* Data()
|
|
|
|
{
|
|
|
|
return program.Data();
|
|
|
|
}
|
|
|
|
GLint Size()
|
|
|
|
{
|
|
|
|
return program.Size();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
class ProgramShaderCacheInserter : public LinearDiskCacheReader<PROGRAMUID, u8>
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
void Read(const PROGRAMUID &key, const u8 *value, u32 value_size)
|
|
|
|
{
|
2011-12-24 06:37:13 +00:00
|
|
|
#ifdef GLEW_VERSION_4_0
|
2011-12-21 06:15:48 +00:00
|
|
|
PCacheEntry entry;
|
|
|
|
|
|
|
|
// The two shaders might not even exist anymore
|
|
|
|
// But it is fine, no need to worry about that
|
|
|
|
entry.program.vsid = key.uid.vsid;
|
|
|
|
entry.program.psid = key.uid.psid;
|
|
|
|
|
|
|
|
entry.program.glprogid = glCreateProgram();
|
|
|
|
|
|
|
|
glProgramBinary(entry.program.glprogid, ProgramFormat, value, value_size);
|
|
|
|
|
|
|
|
GLint success;
|
|
|
|
glGetProgramiv(entry.program.glprogid, GL_LINK_STATUS, &success);
|
|
|
|
|
|
|
|
if (success)
|
|
|
|
{
|
|
|
|
pshaders[std::make_pair(key.uid.psid, key.uid.vsid)] = entry;
|
|
|
|
glUseProgram(entry.program.glprogid);
|
|
|
|
SetProgramVariables(entry, key);
|
|
|
|
}
|
2011-12-24 06:37:13 +00:00
|
|
|
#endif
|
2011-12-21 06:15:48 +00:00
|
|
|
}
|
2011-12-11 10:08:18 +00:00
|
|
|
};
|
2011-12-21 06:15:48 +00:00
|
|
|
|
2011-12-24 08:19:30 +00:00
|
|
|
typedef std::map<std::pair<u32, u32>, PCacheEntry> PCache;
|
2011-12-11 10:08:18 +00:00
|
|
|
|
|
|
|
static PCache pshaders;
|
|
|
|
static GLuint CurrentFShader, CurrentVShader, CurrentProgram;
|
2011-12-24 08:19:30 +00:00
|
|
|
static std::pair<u32, u32> CurrentShaderProgram;
|
2011-12-11 10:08:18 +00:00
|
|
|
|
2011-12-11 12:02:04 +00:00
|
|
|
static GLuint s_ps_vs_ubo;
|
|
|
|
static GLintptr s_vs_data_offset;
|
2011-12-21 06:15:48 +00:00
|
|
|
static void SetProgramVariables(PCacheEntry &entry, const PROGRAMUID &uid);
|
2011-12-11 12:02:04 +00:00
|
|
|
|
2011-12-01 03:00:21 +00:00
|
|
|
public:
|
2011-12-11 10:08:18 +00:00
|
|
|
static PROGRAMSHADER GetShaderProgram(void);
|
|
|
|
static void SetBothShaders(GLuint PS, GLuint VS);
|
|
|
|
static GLuint GetCurrentProgram(void);
|
2011-12-11 10:11:57 +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);
|
2011-12-11 10:08:18 +00:00
|
|
|
|
|
|
|
static void Init(void);
|
|
|
|
static void Shutdown(void);
|
2011-12-01 03:00:21 +00:00
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace OGL
|
|
|
|
|
|
|
|
#endif
|