mirror of
https://github.com/dolphin-emu/dolphin.git
synced 2025-04-10 03:44:26 +00:00
OGL: Uber shader support
This commit is contained in:
parent
901bf9c257
commit
cd502990fa
@ -57,6 +57,7 @@ GLVertexFormat::GLVertexFormat(const PortableVertexDeclaration& _vtx_decl)
|
|||||||
|
|
||||||
glGenVertexArrays(1, &VAO);
|
glGenVertexArrays(1, &VAO);
|
||||||
glBindVertexArray(VAO);
|
glBindVertexArray(VAO);
|
||||||
|
ProgramShaderCache::BindVertexFormat(this);
|
||||||
|
|
||||||
// the element buffer is bound directly to the vao, so we must it set for every vao
|
// the element buffer is bound directly to the vao, so we must it set for every vao
|
||||||
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, vm->m_index_buffers);
|
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, vm->m_index_buffers);
|
||||||
@ -74,8 +75,6 @@ GLVertexFormat::GLVertexFormat(const PortableVertexDeclaration& _vtx_decl)
|
|||||||
SetPointer(SHADER_TEXTURE0_ATTRIB + i, vertex_stride, _vtx_decl.texcoords[i]);
|
SetPointer(SHADER_TEXTURE0_ATTRIB + i, vertex_stride, _vtx_decl.texcoords[i]);
|
||||||
|
|
||||||
SetPointer(SHADER_POSMTX_ATTRIB, vertex_stride, _vtx_decl.posmtx);
|
SetPointer(SHADER_POSMTX_ATTRIB, vertex_stride, _vtx_decl.posmtx);
|
||||||
|
|
||||||
vm->m_last_vao = VAO;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
GLVertexFormat::~GLVertexFormat()
|
GLVertexFormat::~GLVertexFormat()
|
||||||
|
File diff suppressed because it is too large
Load Diff
@ -4,17 +4,25 @@
|
|||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
|
#include <memory>
|
||||||
#include <tuple>
|
#include <tuple>
|
||||||
|
|
||||||
#include "Common/GL/GLUtil.h"
|
#include "Common/GL/GLUtil.h"
|
||||||
#include "Common/LinearDiskCache.h"
|
#include "Common/LinearDiskCache.h"
|
||||||
|
|
||||||
|
#include "VideoCommon/AsyncShaderCompiler.h"
|
||||||
#include "VideoCommon/GeometryShaderGen.h"
|
#include "VideoCommon/GeometryShaderGen.h"
|
||||||
#include "VideoCommon/PixelShaderGen.h"
|
#include "VideoCommon/PixelShaderGen.h"
|
||||||
|
#include "VideoCommon/UberShaderPixel.h"
|
||||||
|
#include "VideoCommon/UberShaderVertex.h"
|
||||||
#include "VideoCommon/VertexShaderGen.h"
|
#include "VideoCommon/VertexShaderGen.h"
|
||||||
|
|
||||||
|
class cInterfaceBase;
|
||||||
|
|
||||||
namespace OGL
|
namespace OGL
|
||||||
{
|
{
|
||||||
|
class GLVertexFormat;
|
||||||
|
|
||||||
class SHADERUID
|
class SHADERUID
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
@ -24,30 +32,53 @@ public:
|
|||||||
|
|
||||||
bool operator<(const SHADERUID& r) const
|
bool operator<(const SHADERUID& r) const
|
||||||
{
|
{
|
||||||
return std::tie(puid, vuid, guid) < std::tie(r.puid, r.vuid, r.guid);
|
return std::tie(vuid, puid, guid) < std::tie(r.vuid, r.puid, r.guid);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool operator==(const SHADERUID& r) const
|
bool operator==(const SHADERUID& r) const
|
||||||
{
|
{
|
||||||
return std::tie(puid, vuid, guid) == std::tie(r.puid, r.vuid, r.guid);
|
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);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
struct SHADER
|
struct SHADER
|
||||||
{
|
{
|
||||||
SHADER() : glprogid(0) {}
|
|
||||||
void Destroy()
|
void Destroy()
|
||||||
{
|
{
|
||||||
glDeleteProgram(glprogid);
|
DestroyShaders();
|
||||||
glprogid = 0;
|
if (glprogid)
|
||||||
|
{
|
||||||
|
glDeleteProgram(glprogid);
|
||||||
|
glprogid = 0;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
GLuint glprogid; // OpenGL program id
|
|
||||||
|
|
||||||
std::string strvprog, strpprog, strgprog;
|
GLuint vsid = 0;
|
||||||
|
GLuint gsid = 0;
|
||||||
|
GLuint psid = 0;
|
||||||
|
GLuint glprogid = 0;
|
||||||
|
|
||||||
void SetProgramVariables();
|
void SetProgramVariables();
|
||||||
void SetProgramBindings(bool is_compute);
|
void SetProgramBindings(bool is_compute);
|
||||||
void Bind() const;
|
void Bind() const;
|
||||||
|
void DestroyShaders();
|
||||||
};
|
};
|
||||||
|
|
||||||
class ProgramShaderCache
|
class ProgramShaderCache
|
||||||
@ -57,43 +88,126 @@ public:
|
|||||||
{
|
{
|
||||||
SHADER shader;
|
SHADER shader;
|
||||||
bool in_cache;
|
bool in_cache;
|
||||||
|
bool pending;
|
||||||
|
|
||||||
void Destroy() { shader.Destroy(); }
|
void Destroy() { shader.Destroy(); }
|
||||||
};
|
};
|
||||||
|
|
||||||
static PCacheEntry GetShaderProgram();
|
static PCacheEntry GetShaderProgram();
|
||||||
static SHADER* SetShader(u32 primitive_type);
|
static SHADER* SetShader(u32 primitive_type, const GLVertexFormat* vertex_format);
|
||||||
static void GetShaderId(SHADERUID* uid, u32 primitive_type);
|
static SHADER* SetUberShader(u32 primitive_type, const GLVertexFormat* vertex_format);
|
||||||
|
static void BindVertexFormat(const GLVertexFormat* vertex_format);
|
||||||
|
static void InvalidateVertexFormat();
|
||||||
|
static void BindLastVertexFormat();
|
||||||
|
|
||||||
static bool CompileShader(SHADER& shader, const std::string& vcode, const std::string& pcode,
|
static bool CompileShader(SHADER& shader, const std::string& vcode, const std::string& pcode,
|
||||||
const std::string& gcode = "");
|
const std::string& gcode = "");
|
||||||
static bool CompileComputeShader(SHADER& shader, const std::string& code);
|
static bool CompileComputeShader(SHADER& shader, const std::string& code);
|
||||||
static GLuint CompileSingleShader(GLuint type, const std::string& code);
|
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);
|
||||||
static void UploadConstants();
|
static void UploadConstants();
|
||||||
|
|
||||||
static void Init();
|
static void Init();
|
||||||
static void Reload();
|
static void Reload();
|
||||||
static void Shutdown();
|
static void Shutdown();
|
||||||
static void CreateHeader();
|
static void CreateHeader();
|
||||||
|
static void RetrieveAsyncShaders();
|
||||||
|
static void PrecompileUberShaders();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
class ProgramShaderCacheInserter : public LinearDiskCacheReader<SHADERUID, u8>
|
template <typename UIDType>
|
||||||
|
class ProgramShaderCacheInserter : public LinearDiskCacheReader<UIDType, u8>
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
void Read(const SHADERUID& key, const u8* value, u32 value_size) override;
|
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;
|
||||||
|
};
|
||||||
|
|
||||||
|
struct SharedContextData
|
||||||
|
{
|
||||||
|
std::unique_ptr<cInterfaceBase> context;
|
||||||
|
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);
|
||||||
static void LoadProgramBinaries();
|
static void LoadProgramBinaries();
|
||||||
static void SaveProgramBinaries();
|
static void SaveProgramBinaries();
|
||||||
static void DestroyShaders();
|
static void DestroyShaders();
|
||||||
|
static void CreatePrerenderArrays(SharedContextData* data);
|
||||||
|
static void DestroyPrerenderArrays(SharedContextData* data);
|
||||||
|
static void DrawPrerenderArray(const SHADER& shader, u32 primitive_type);
|
||||||
|
|
||||||
typedef std::map<SHADERUID, PCacheEntry> PCache;
|
|
||||||
static PCache pshaders;
|
static PCache pshaders;
|
||||||
|
static UberPCache ubershaders;
|
||||||
static PCacheEntry* last_entry;
|
static PCacheEntry* last_entry;
|
||||||
|
static PCacheEntry* last_uber_entry;
|
||||||
static SHADERUID last_uid;
|
static SHADERUID last_uid;
|
||||||
|
static UBERSHADERUID last_uber_uid;
|
||||||
|
|
||||||
|
static std::unique_ptr<SharedContextAsyncShaderCompiler> s_async_compiler;
|
||||||
static u32 s_ubo_buffer_size;
|
static u32 s_ubo_buffer_size;
|
||||||
static s32 s_ubo_align;
|
static s32 s_ubo_align;
|
||||||
|
static u32 s_last_VAO;
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace OGL
|
} // namespace OGL
|
||||||
|
@ -1472,6 +1472,7 @@ void Renderer::SwapImpl(u32 xfbAddr, u32 fbWidth, u32 fbStride, u32 fbHeight,
|
|||||||
|
|
||||||
// Clean out old stuff from caches. It's not worth it to clean out the shader caches.
|
// Clean out old stuff from caches. It's not worth it to clean out the shader caches.
|
||||||
g_texture_cache->Cleanup(frameCount);
|
g_texture_cache->Cleanup(frameCount);
|
||||||
|
ProgramShaderCache::RetrieveAsyncShaders();
|
||||||
|
|
||||||
// Render to the framebuffer.
|
// Render to the framebuffer.
|
||||||
FramebufferManager::SetFramebuffer(0);
|
FramebufferManager::SetFramebuffer(0);
|
||||||
@ -1768,10 +1769,9 @@ void Renderer::RestoreAPIState()
|
|||||||
SetBlendMode(true);
|
SetBlendMode(true);
|
||||||
SetViewport();
|
SetViewport();
|
||||||
|
|
||||||
|
ProgramShaderCache::BindLastVertexFormat();
|
||||||
const VertexManager* const vm = static_cast<VertexManager*>(g_vertex_manager.get());
|
const VertexManager* const vm = static_cast<VertexManager*>(g_vertex_manager.get());
|
||||||
glBindBuffer(GL_ARRAY_BUFFER, vm->m_vertex_buffers);
|
glBindBuffer(GL_ARRAY_BUFFER, vm->m_vertex_buffers);
|
||||||
if (vm->m_last_vao)
|
|
||||||
glBindVertexArray(vm->m_last_vao);
|
|
||||||
|
|
||||||
OGLTexture::SetStage();
|
OGLTexture::SetStage();
|
||||||
}
|
}
|
||||||
|
@ -58,6 +58,7 @@ struct VideoConfig
|
|||||||
bool bSupportsConservativeDepth;
|
bool bSupportsConservativeDepth;
|
||||||
bool bSupportsImageLoadStore;
|
bool bSupportsImageLoadStore;
|
||||||
bool bSupportsAniso;
|
bool bSupportsAniso;
|
||||||
|
bool bSupportsBitfield;
|
||||||
|
|
||||||
const char* gl_vendor;
|
const char* gl_vendor;
|
||||||
const char* gl_renderer;
|
const char* gl_renderer;
|
||||||
|
@ -53,8 +53,6 @@ void VertexManager::CreateDeviceObjects()
|
|||||||
|
|
||||||
s_indexBuffer = StreamBuffer::Create(GL_ELEMENT_ARRAY_BUFFER, MAX_IBUFFER_SIZE);
|
s_indexBuffer = StreamBuffer::Create(GL_ELEMENT_ARRAY_BUFFER, MAX_IBUFFER_SIZE);
|
||||||
m_index_buffers = s_indexBuffer->m_buffer;
|
m_index_buffers = s_indexBuffer->m_buffer;
|
||||||
|
|
||||||
m_last_vao = 0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void VertexManager::DestroyDeviceObjects()
|
void VertexManager::DestroyDeviceObjects()
|
||||||
@ -142,22 +140,13 @@ void VertexManager::vFlush()
|
|||||||
GLVertexFormat* nativeVertexFmt = (GLVertexFormat*)VertexLoaderManager::GetCurrentVertexFormat();
|
GLVertexFormat* nativeVertexFmt = (GLVertexFormat*)VertexLoaderManager::GetCurrentVertexFormat();
|
||||||
u32 stride = nativeVertexFmt->GetVertexStride();
|
u32 stride = nativeVertexFmt->GetVertexStride();
|
||||||
|
|
||||||
if (m_last_vao != nativeVertexFmt->VAO)
|
ProgramShaderCache::SetShader(m_current_primitive_type, nativeVertexFmt);
|
||||||
{
|
|
||||||
glBindVertexArray(nativeVertexFmt->VAO);
|
|
||||||
m_last_vao = nativeVertexFmt->VAO;
|
|
||||||
}
|
|
||||||
|
|
||||||
PrepareDrawBuffers(stride);
|
PrepareDrawBuffers(stride);
|
||||||
|
|
||||||
ProgramShaderCache::SetShader(m_current_primitive_type);
|
|
||||||
|
|
||||||
// upload global constants
|
// upload global constants
|
||||||
ProgramShaderCache::UploadConstants();
|
ProgramShaderCache::UploadConstants();
|
||||||
|
|
||||||
// setup the pointers
|
|
||||||
nativeVertexFmt->SetupVertexPointers();
|
|
||||||
|
|
||||||
if (::BoundingBox::active && !g_Config.BBoxUseFragmentShaderImplementation())
|
if (::BoundingBox::active && !g_Config.BBoxUseFragmentShaderImplementation())
|
||||||
{
|
{
|
||||||
glEnable(GL_STENCIL_TEST);
|
glEnable(GL_STENCIL_TEST);
|
||||||
@ -171,24 +160,6 @@ void VertexManager::vFlush()
|
|||||||
glDisable(GL_STENCIL_TEST);
|
glDisable(GL_STENCIL_TEST);
|
||||||
}
|
}
|
||||||
|
|
||||||
#if defined(_DEBUG) || defined(DEBUGFAST)
|
|
||||||
if (g_ActiveConfig.iLog & CONF_SAVESHADERS)
|
|
||||||
{
|
|
||||||
// save the shaders
|
|
||||||
ProgramShaderCache::PCacheEntry prog = ProgramShaderCache::GetShaderProgram();
|
|
||||||
std::string filename = StringFromFormat(
|
|
||||||
"%sps%.3d.txt", File::GetUserPath(D_DUMPFRAMES_IDX).c_str(), g_ActiveConfig.iSaveTargetId);
|
|
||||||
std::ofstream fps;
|
|
||||||
File::OpenFStream(fps, filename, std::ios_base::out);
|
|
||||||
fps << prog.shader.strpprog;
|
|
||||||
|
|
||||||
filename = StringFromFormat("%svs%.3d.txt", File::GetUserPath(D_DUMPFRAMES_IDX).c_str(),
|
|
||||||
g_ActiveConfig.iSaveTargetId);
|
|
||||||
std::ofstream fvs;
|
|
||||||
File::OpenFStream(fvs, filename, std::ios_base::out);
|
|
||||||
fvs << prog.shader.strvprog;
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
g_Config.iSaveTargetId++;
|
g_Config.iSaveTargetId++;
|
||||||
ClearEFBCache();
|
ClearEFBCache();
|
||||||
}
|
}
|
||||||
|
@ -42,7 +42,6 @@ public:
|
|||||||
// NativeVertexFormat use this
|
// NativeVertexFormat use this
|
||||||
GLuint m_vertex_buffers;
|
GLuint m_vertex_buffers;
|
||||||
GLuint m_index_buffers;
|
GLuint m_index_buffers;
|
||||||
GLuint m_last_vao;
|
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
void ResetBuffer(u32 stride) override;
|
void ResetBuffer(u32 stride) override;
|
||||||
|
@ -98,7 +98,10 @@ static BugInfo m_known_bugs[] = {
|
|||||||
BUG_BROKEN_BITWISE_OP_NEGATION, -1.0, -1.0, true},
|
BUG_BROKEN_BITWISE_OP_NEGATION, -1.0, -1.0, true},
|
||||||
{API_VULKAN, OS_ALL, VENDOR_ATI, DRIVER_ATI, Family::UNKNOWN, BUG_PRIMITIVE_RESTART, -1.0, -1.0,
|
{API_VULKAN, OS_ALL, VENDOR_ATI, DRIVER_ATI, Family::UNKNOWN, BUG_PRIMITIVE_RESTART, -1.0, -1.0,
|
||||||
true},
|
true},
|
||||||
};
|
{API_OPENGL, OS_LINUX, VENDOR_MESA, DRIVER_I965, Family::UNKNOWN,
|
||||||
|
BUG_SHARED_CONTEXT_SHADER_COMPILATION, -1.0, -1.0, true},
|
||||||
|
{API_OPENGL, OS_LINUX, VENDOR_MESA, DRIVER_NOUVEAU, Family::UNKNOWN,
|
||||||
|
BUG_SHARED_CONTEXT_SHADER_COMPILATION, -1.0, -1.0, true}};
|
||||||
|
|
||||||
static std::map<Bug, BugInfo> m_bugs;
|
static std::map<Bug, BugInfo> m_bugs;
|
||||||
|
|
||||||
|
@ -247,6 +247,12 @@ enum Bug
|
|||||||
// fail compilation with no useful diagnostic log. This can be worked around by storing
|
// fail compilation with no useful diagnostic log. This can be worked around by storing
|
||||||
// the negated value to a temporary variable then using that in the bitwise op.
|
// the negated value to a temporary variable then using that in the bitwise op.
|
||||||
BUG_BROKEN_BITWISE_OP_NEGATION,
|
BUG_BROKEN_BITWISE_OP_NEGATION,
|
||||||
|
|
||||||
|
// Bug: Shaders are recompiled on the main thread after being previously compiled on
|
||||||
|
// a worker thread on Mesa i965.
|
||||||
|
// Started version: -1
|
||||||
|
// Ended Version: -1
|
||||||
|
BUG_SHARED_CONTEXT_SHADER_COMPILATION,
|
||||||
};
|
};
|
||||||
|
|
||||||
// Initializes our internal vendor, device family, and driver version
|
// Initializes our internal vendor, device family, and driver version
|
||||||
|
Loading…
x
Reference in New Issue
Block a user