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-26 05:15:54 +00:00
|
|
|
#pragma once
|
2011-12-01 03:00:21 +00:00
|
|
|
|
|
|
|
#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-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-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];
|
|
|
|
|
|
|
|
class ProgramShaderCache
|
2011-12-01 03:00:21 +00:00
|
|
|
{
|
|
|
|
public:
|
2011-12-26 07:58:52 +00:00
|
|
|
typedef std::pair<u32, u32> ShaderUID;
|
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
|
|
|
{
|
2011-12-26 07:58:52 +00:00
|
|
|
GLuint prog_id;
|
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;
|
|
|
|
GLuint vsid, psid;
|
|
|
|
ShaderUID uid;
|
|
|
|
GLint UniformLocations[NUM_UNIFORMS];
|
2011-12-26 05:15:54 +00:00
|
|
|
|
2011-12-26 09:54:44 +00:00
|
|
|
PCacheEntry() : prog_id(0), binary(NULL), binary_size(0), vsid(0), psid(0) { }
|
2011-12-01 03:00:21 +00:00
|
|
|
|
2011-12-26 07:58:52 +00:00
|
|
|
~PCacheEntry()
|
|
|
|
{
|
|
|
|
FreeProgram();
|
|
|
|
}
|
2011-12-26 05:15:54 +00:00
|
|
|
|
2011-12-26 07:58:52 +00:00
|
|
|
void Create(const GLuint pix_id, const GLuint vert_id)
|
|
|
|
{
|
|
|
|
psid = pix_id;
|
|
|
|
vsid = vert_id;
|
|
|
|
uid = std::make_pair(psid, vsid);
|
|
|
|
prog_id = glCreateProgram();
|
|
|
|
}
|
2011-12-01 03:00:21 +00:00
|
|
|
|
2011-12-26 07:58:52 +00:00
|
|
|
void Destroy()
|
|
|
|
{
|
|
|
|
glDeleteProgram(prog_id);
|
|
|
|
prog_id = 0;
|
|
|
|
}
|
2011-12-26 05:15:54 +00:00
|
|
|
|
2011-12-26 07:58:52 +00:00
|
|
|
void UpdateSize()
|
|
|
|
{
|
|
|
|
if (binary_size == 0)
|
|
|
|
glGetProgramiv(prog_id, GL_PROGRAM_BINARY_LENGTH, &binary_size);
|
|
|
|
}
|
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];
|
2011-12-29 15:25:03 +00:00
|
|
|
glGetProgramBinary(prog_id, 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 void SetBothShaders(GLuint PS, GLuint VS);
|
|
|
|
static GLuint GetCurrentProgram(void);
|
|
|
|
|
|
|
|
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:
|
|
|
|
class ProgramShaderCacheInserter : public LinearDiskCacheReader<ShaderUID, u8>
|
2011-12-21 06:15:48 +00:00
|
|
|
{
|
2011-12-26 05:15:54 +00:00
|
|
|
public:
|
2011-12-26 07:58:52 +00:00
|
|
|
void Read(const ShaderUID &key, const u8 *value, u32 value_size)
|
2011-12-26 05:15:54 +00:00
|
|
|
{
|
|
|
|
// The two shaders might not even exist anymore
|
|
|
|
// But it is fine, no need to worry about that
|
2011-12-26 07:58:52 +00:00
|
|
|
PCacheEntry entry;
|
|
|
|
entry.Create(key.first, key.second);
|
2011-12-29 15:25:03 +00:00
|
|
|
|
|
|
|
glProgramBinary(entry.prog_id, entry.prog_format, value, value_size);
|
2011-12-26 05:15:54 +00:00
|
|
|
|
|
|
|
GLint success;
|
2011-12-26 07:58:52 +00:00
|
|
|
glGetProgramiv(entry.prog_id, GL_LINK_STATUS, &success);
|
2011-12-26 05:15:54 +00:00
|
|
|
|
|
|
|
if (success)
|
2011-12-21 06:15:48 +00:00
|
|
|
{
|
2011-12-26 07:58:52 +00:00
|
|
|
pshaders[key] = entry;
|
|
|
|
glUseProgram(entry.prog_id);
|
|
|
|
SetProgramVariables(entry);
|
2011-12-21 06:15:48 +00:00
|
|
|
}
|
2011-12-26 05:15:54 +00:00
|
|
|
}
|
2011-12-11 10:08:18 +00:00
|
|
|
};
|
2011-12-26 05:15:54 +00:00
|
|
|
|
2011-12-26 07:58:52 +00:00
|
|
|
typedef std::map<ShaderUID, PCacheEntry> PCache;
|
2011-12-11 10:08:18 +00:00
|
|
|
|
|
|
|
static PCache pshaders;
|
2013-01-15 16:10:43 +00:00
|
|
|
static GLuint CurrentProgram;
|
2011-12-26 07:58:52 +00:00
|
|
|
static ShaderUID 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;
|
2013-01-14 12:58:11 +00:00
|
|
|
static float *s_ubo_buffer;
|
|
|
|
static u32 s_ubo_buffer_size;
|
|
|
|
static bool s_ubo_dirty;
|
2011-12-26 07:58:52 +00:00
|
|
|
static void SetProgramVariables(PCacheEntry &entry);
|
2012-12-31 01:34:27 +00:00
|
|
|
static void SetProgramBindings(PCacheEntry &entry);
|
2011-12-01 03:00:21 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace OGL
|