2015-05-24 04:55:12 +00:00
|
|
|
// Copyright 2011 Dolphin Emulator Project
|
2015-05-17 23:08:10 +00:00
|
|
|
// Licensed under GPLv2+
|
2013-04-18 03:29:41 +00:00
|
|
|
// Refer to the license.txt file included.
|
2011-12-01 03:00:21 +00:00
|
|
|
|
2014-02-10 18:54:46 +00:00
|
|
|
#pragma once
|
2011-12-01 03:00:21 +00:00
|
|
|
|
2017-07-20 05:25:29 +00:00
|
|
|
#include <memory>
|
2016-01-02 03:28:42 +00:00
|
|
|
#include <tuple>
|
|
|
|
|
2015-09-18 16:40:00 +00:00
|
|
|
#include "Common/GL/GLUtil.h"
|
2016-06-24 08:43:46 +00:00
|
|
|
#include "Common/LinearDiskCache.h"
|
2015-09-18 16:40:00 +00:00
|
|
|
|
2017-07-20 05:25:29 +00:00
|
|
|
#include "VideoCommon/AsyncShaderCompiler.h"
|
2014-10-30 22:40:03 +00:00
|
|
|
#include "VideoCommon/GeometryShaderGen.h"
|
2014-02-17 10:18:15 +00:00
|
|
|
#include "VideoCommon/PixelShaderGen.h"
|
2017-07-20 05:25:29 +00:00
|
|
|
#include "VideoCommon/UberShaderPixel.h"
|
|
|
|
#include "VideoCommon/UberShaderVertex.h"
|
2014-02-17 10:18:15 +00:00
|
|
|
#include "VideoCommon/VertexShaderGen.h"
|
2011-12-21 06:15:48 +00:00
|
|
|
|
2017-07-20 05:25:29 +00:00
|
|
|
class cInterfaceBase;
|
|
|
|
|
2011-12-26 07:58:52 +00:00
|
|
|
namespace OGL
|
2011-12-26 05:15:54 +00:00
|
|
|
{
|
2017-07-20 05:25:29 +00:00
|
|
|
class GLVertexFormat;
|
|
|
|
|
2013-03-26 21:16:29 +00:00
|
|
|
class SHADERUID
|
2013-02-13 12:12:19 +00:00
|
|
|
{
|
|
|
|
public:
|
2016-06-24 08:43:46 +00:00
|
|
|
VertexShaderUid vuid;
|
|
|
|
PixelShaderUid puid;
|
|
|
|
GeometryShaderUid guid;
|
|
|
|
|
|
|
|
bool operator<(const SHADERUID& r) const
|
|
|
|
{
|
2017-07-20 05:25:29 +00:00
|
|
|
return std::tie(vuid, puid, guid) < std::tie(r.vuid, r.puid, r.guid);
|
2016-06-24 08:43:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool operator==(const SHADERUID& r) const
|
|
|
|
{
|
2017-07-20 05:25:29 +00:00
|
|
|
return std::tie(vuid, puid, guid) == std::tie(r.vuid, r.puid, r.guid);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
class UBERSHADERUID
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
UberShader::VertexShaderUid vuid;
|
|
|
|
UberShader::PixelShaderUid puid;
|
|
|
|
GeometryShaderUid guid;
|
|
|
|
|
|
|
|
bool operator<(const UBERSHADERUID& r) const
|
|
|
|
{
|
|
|
|
return std::tie(vuid, puid, guid) < std::tie(r.vuid, r.puid, r.guid);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool operator==(const UBERSHADERUID& r) const
|
|
|
|
{
|
|
|
|
return std::tie(vuid, puid, guid) == std::tie(r.vuid, r.puid, r.guid);
|
2016-06-24 08:43:46 +00:00
|
|
|
}
|
2013-02-13 12:12:19 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
struct SHADER
|
|
|
|
{
|
2016-06-24 08:43:46 +00:00
|
|
|
void Destroy()
|
|
|
|
{
|
2017-07-20 05:25:29 +00:00
|
|
|
DestroyShaders();
|
|
|
|
if (glprogid)
|
|
|
|
{
|
|
|
|
glDeleteProgram(glprogid);
|
|
|
|
glprogid = 0;
|
|
|
|
}
|
2016-06-24 08:43:46 +00:00
|
|
|
}
|
|
|
|
|
2017-07-20 05:25:29 +00:00
|
|
|
GLuint vsid = 0;
|
|
|
|
GLuint gsid = 0;
|
|
|
|
GLuint psid = 0;
|
|
|
|
GLuint glprogid = 0;
|
2016-06-24 08:43:46 +00:00
|
|
|
|
|
|
|
void SetProgramVariables();
|
2016-11-27 08:14:56 +00:00
|
|
|
void SetProgramBindings(bool is_compute);
|
2017-04-23 04:44:34 +00:00
|
|
|
void Bind() const;
|
2017-07-20 05:25:29 +00:00
|
|
|
void DestroyShaders();
|
2013-02-13 12:12:19 +00:00
|
|
|
};
|
2011-12-26 07:58:52 +00:00
|
|
|
|
|
|
|
class ProgramShaderCache
|
2011-12-01 03:00:21 +00:00
|
|
|
{
|
|
|
|
public:
|
2016-06-24 08:43:46 +00:00
|
|
|
struct PCacheEntry
|
|
|
|
{
|
|
|
|
SHADER shader;
|
|
|
|
bool in_cache;
|
2017-07-20 05:25:29 +00:00
|
|
|
bool pending;
|
2011-12-11 10:08:18 +00:00
|
|
|
|
2016-06-24 08:43:46 +00:00
|
|
|
void Destroy() { shader.Destroy(); }
|
|
|
|
};
|
2013-03-29 14:08:00 +00:00
|
|
|
|
2016-06-24 08:43:46 +00:00
|
|
|
static PCacheEntry GetShaderProgram();
|
2017-07-20 05:25:29 +00:00
|
|
|
static SHADER* SetShader(u32 primitive_type, const GLVertexFormat* vertex_format);
|
|
|
|
static SHADER* SetUberShader(u32 primitive_type, const GLVertexFormat* vertex_format);
|
|
|
|
static void BindVertexFormat(const GLVertexFormat* vertex_format);
|
|
|
|
static void InvalidateVertexFormat();
|
|
|
|
static void BindLastVertexFormat();
|
2013-10-29 05:23:17 +00:00
|
|
|
|
2016-06-24 08:43:46 +00:00
|
|
|
static bool CompileShader(SHADER& shader, const std::string& vcode, const std::string& pcode,
|
|
|
|
const std::string& gcode = "");
|
2016-11-27 08:14:56 +00:00
|
|
|
static bool CompileComputeShader(SHADER& shader, const std::string& code);
|
2017-07-20 05:25:29 +00:00
|
|
|
static GLuint CompileSingleShader(GLenum type, const std::string& code);
|
|
|
|
static bool CheckShaderCompileResult(GLuint id, GLenum type, const std::string& code);
|
|
|
|
static bool CheckProgramLinkResult(GLuint id, const std::string& vcode, const std::string& pcode,
|
|
|
|
const std::string& gcode);
|
2016-06-24 08:43:46 +00:00
|
|
|
static void UploadConstants();
|
2011-12-26 07:58:52 +00:00
|
|
|
|
2016-06-24 08:43:46 +00:00
|
|
|
static void Init();
|
2017-06-24 09:20:34 +00:00
|
|
|
static void Reload();
|
2016-06-24 08:43:46 +00:00
|
|
|
static void Shutdown();
|
|
|
|
static void CreateHeader();
|
2017-07-20 05:25:29 +00:00
|
|
|
static void RetrieveAsyncShaders();
|
|
|
|
static void PrecompileUberShaders();
|
2011-12-26 07:58:52 +00:00
|
|
|
|
|
|
|
private:
|
2017-07-20 05:25:29 +00:00
|
|
|
template <typename UIDType>
|
|
|
|
class ProgramShaderCacheInserter : public LinearDiskCacheReader<UIDType, u8>
|
2016-06-24 08:43:46 +00:00
|
|
|
{
|
|
|
|
public:
|
2017-07-20 05:25:29 +00:00
|
|
|
ProgramShaderCacheInserter(std::map<UIDType, PCacheEntry>& shader_map)
|
|
|
|
: m_shader_map(shader_map)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void Read(const UIDType& key, const u8* value, u32 value_size) override
|
|
|
|
{
|
|
|
|
if (m_shader_map.find(key) != m_shader_map.end())
|
|
|
|
return;
|
|
|
|
|
|
|
|
PCacheEntry& entry = m_shader_map[key];
|
|
|
|
if (!CreateCacheEntryFromBinary(&entry, value, value_size))
|
|
|
|
{
|
|
|
|
m_shader_map.erase(key);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
std::map<UIDType, PCacheEntry>& m_shader_map;
|
|
|
|
};
|
|
|
|
|
|
|
|
class SharedContextAsyncShaderCompiler : public VideoCommon::AsyncShaderCompiler
|
|
|
|
{
|
|
|
|
protected:
|
|
|
|
virtual bool WorkerThreadInitMainThread(void** param) override;
|
|
|
|
virtual bool WorkerThreadInitWorkerThread(void* param) override;
|
|
|
|
virtual void WorkerThreadExit(void* param) override;
|
2016-06-24 08:43:46 +00:00
|
|
|
};
|
|
|
|
|
2017-07-20 05:25:29 +00:00
|
|
|
struct SharedContextData
|
|
|
|
{
|
|
|
|
std::unique_ptr<cInterfaceBase> context;
|
2017-07-31 12:39:52 +00:00
|
|
|
GLuint prerender_FBO;
|
|
|
|
GLuint prerender_FBO_tex;
|
|
|
|
GLuint prerender_FBO_depth;
|
2017-07-20 05:25:29 +00:00
|
|
|
GLuint prerender_VBO;
|
|
|
|
GLuint prerender_VAO;
|
|
|
|
GLuint prerender_IBO;
|
|
|
|
};
|
|
|
|
|
|
|
|
class ShaderCompileWorkItem : public VideoCommon::AsyncShaderCompiler::WorkItem
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
ShaderCompileWorkItem(const SHADERUID& uid);
|
|
|
|
|
|
|
|
bool Compile() override;
|
|
|
|
void Retrieve() override;
|
|
|
|
|
|
|
|
private:
|
|
|
|
SHADERUID m_uid;
|
|
|
|
SHADER m_program;
|
|
|
|
};
|
|
|
|
|
|
|
|
class UberShaderCompileWorkItem : public VideoCommon::AsyncShaderCompiler::WorkItem
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
UberShaderCompileWorkItem(const UBERSHADERUID& uid);
|
|
|
|
|
|
|
|
bool Compile() override;
|
|
|
|
void Retrieve() override;
|
|
|
|
|
|
|
|
private:
|
|
|
|
UBERSHADERUID m_uid;
|
|
|
|
SHADER m_program;
|
|
|
|
};
|
|
|
|
|
|
|
|
typedef std::map<SHADERUID, PCacheEntry> PCache;
|
|
|
|
typedef std::map<UBERSHADERUID, PCacheEntry> UberPCache;
|
|
|
|
|
|
|
|
static GLuint CreateProgramFromBinary(const u8* value, u32 value_size);
|
|
|
|
static bool CreateCacheEntryFromBinary(PCacheEntry* entry, const u8* value, u32 value_size);
|
2017-06-24 09:20:34 +00:00
|
|
|
static void LoadProgramBinaries();
|
|
|
|
static void SaveProgramBinaries();
|
|
|
|
static void DestroyShaders();
|
2017-07-20 05:25:29 +00:00
|
|
|
static void CreatePrerenderArrays(SharedContextData* data);
|
|
|
|
static void DestroyPrerenderArrays(SharedContextData* data);
|
|
|
|
static void DrawPrerenderArray(const SHADER& shader, u32 primitive_type);
|
2017-06-24 09:20:34 +00:00
|
|
|
|
2016-06-24 08:43:46 +00:00
|
|
|
static PCache pshaders;
|
2017-07-20 05:25:29 +00:00
|
|
|
static UberPCache ubershaders;
|
2016-06-24 08:43:46 +00:00
|
|
|
static PCacheEntry* last_entry;
|
2017-07-20 05:25:29 +00:00
|
|
|
static PCacheEntry* last_uber_entry;
|
2016-06-24 08:43:46 +00:00
|
|
|
static SHADERUID last_uid;
|
2017-07-20 05:25:29 +00:00
|
|
|
static UBERSHADERUID last_uber_uid;
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2017-07-20 05:25:29 +00:00
|
|
|
static std::unique_ptr<SharedContextAsyncShaderCompiler> s_async_compiler;
|
2016-06-24 08:43:46 +00:00
|
|
|
static u32 s_ubo_buffer_size;
|
|
|
|
static s32 s_ubo_align;
|
2017-07-20 05:25:29 +00:00
|
|
|
static u32 s_last_VAO;
|
2011-12-01 03:00:21 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace OGL
|