Latte: Refactor legacy OpenGL code for shader binding

This commit is contained in:
Exzap 2023-12-07 13:50:16 +01:00
parent dee764473d
commit 646835346c
14 changed files with 25 additions and 81 deletions

View File

@ -838,7 +838,6 @@ LatteDecompilerShader* LatteShader_CompileSeparablePixelShader(uint64 baseHash,
void LatteSHRC_UpdateVertexShader(uint8* vertexShaderPtr, uint32 vertexShaderSize, bool usesGeometryShader) void LatteSHRC_UpdateVertexShader(uint8* vertexShaderPtr, uint32 vertexShaderSize, bool usesGeometryShader)
{ {
// todo - should include VTX_SEMANTIC table in state // todo - should include VTX_SEMANTIC table in state
LatteSHRC_UpdateVSBaseHash(vertexShaderPtr, vertexShaderSize, usesGeometryShader); LatteSHRC_UpdateVSBaseHash(vertexShaderPtr, vertexShaderSize, usesGeometryShader);
uint64 vsAuxHash = 0; uint64 vsAuxHash = 0;
auto itBaseShader = sVertexShaders.find(_shaderBaseHash_vs); auto itBaseShader = sVertexShaders.find(_shaderBaseHash_vs);
@ -855,15 +854,13 @@ void LatteSHRC_UpdateVertexShader(uint8* vertexShaderPtr, uint32 vertexShaderSiz
LatteGPUState.activeShaderHasError = true; LatteGPUState.activeShaderHasError = true;
return; return;
} }
g_renderer->shader_bind(vertexShader->shader);
_activeVertexShader = vertexShader; _activeVertexShader = vertexShader;
} }
void LatteSHRC_UpdateGeometryShader(bool usesGeometryShader, uint8* geometryShaderPtr, uint32 geometryShaderSize, uint8* geometryCopyShader, uint32 geometryCopyShaderSize) void LatteSHRC_UpdateGeometryShader(bool usesGeometryShader, uint8* geometryShaderPtr, uint32 geometryShaderSize, uint8* geometryCopyShader, uint32 geometryCopyShaderSize)
{ {
if (usesGeometryShader == false || _activeVertexShader == nullptr) if (!usesGeometryShader || !_activeVertexShader)
{ {
g_renderer->shader_unbind(RendererShader::ShaderType::kGeometry);
_shaderBaseHash_gs = 0; _shaderBaseHash_gs = 0;
_activeGeometryShader = nullptr; _activeGeometryShader = nullptr;
return; return;
@ -887,21 +884,11 @@ void LatteSHRC_UpdateGeometryShader(bool usesGeometryShader, uint8* geometryShad
LatteGPUState.activeShaderHasError = true; LatteGPUState.activeShaderHasError = true;
return; return;
} }
g_renderer->shader_bind(geometryShader->shader);
_activeGeometryShader = geometryShader; _activeGeometryShader = geometryShader;
} }
void LatteSHRC_UpdatePixelShader(uint8* pixelShaderPtr, uint32 pixelShaderSize, bool usesGeometryShader) void LatteSHRC_UpdatePixelShader(uint8* pixelShaderPtr, uint32 pixelShaderSize, bool usesGeometryShader)
{ {
if (LatteGPUState.contextRegister[mmVGT_STRMOUT_EN] != 0 && g_renderer->GetType() == RendererAPI::OpenGL)
{
if (_activePixelShader)
{
g_renderer->shader_unbind(RendererShader::ShaderType::kFragment);
_activePixelShader = nullptr;
}
return;
}
LatteSHRC_UpdatePSBaseHash(pixelShaderPtr, pixelShaderSize, usesGeometryShader); LatteSHRC_UpdatePSBaseHash(pixelShaderPtr, pixelShaderSize, usesGeometryShader);
uint64 psAuxHash = 0; uint64 psAuxHash = 0;
auto itBaseShader = sPixelShaders.find(_shaderBaseHash_ps); auto itBaseShader = sPixelShaders.find(_shaderBaseHash_ps);
@ -918,7 +905,6 @@ void LatteSHRC_UpdatePixelShader(uint8* pixelShaderPtr, uint32 pixelShaderSize,
LatteGPUState.activeShaderHasError = true; LatteGPUState.activeShaderHasError = true;
return; return;
} }
g_renderer->shader_bind(pixelShader->shader);
_activePixelShader = pixelShader; _activePixelShader = pixelShader;
} }

View File

@ -275,10 +275,6 @@ void OpenGLRenderer::Initialize()
cemuLog_log(LogType::Force, "ARB_copy_image: {}", (glCopyImageSubData != NULL) ? "available" : "not supported"); cemuLog_log(LogType::Force, "ARB_copy_image: {}", (glCopyImageSubData != NULL) ? "available" : "not supported");
cemuLog_log(LogType::Force, "NV_depth_buffer_float: {}", (glDepthRangedNV != NULL) ? "available" : "not supported"); cemuLog_log(LogType::Force, "NV_depth_buffer_float: {}", (glDepthRangedNV != NULL) ? "available" : "not supported");
// generate default frame buffer
glGenFramebuffers(1, &m_defaultFramebufferId);
catchOpenGLError();
// enable framebuffer SRGB support // enable framebuffer SRGB support
glEnable(GL_FRAMEBUFFER_SRGB); glEnable(GL_FRAMEBUFFER_SRGB);
@ -566,10 +562,9 @@ void OpenGLRenderer::DrawBackbufferQuad(LatteTextureView* texView, RendererOutpu
sint32 effectiveHeight; sint32 effectiveHeight;
LatteTexture_getEffectiveSize(texView->baseTexture, &effectiveWidth, &effectiveHeight, nullptr, 0); LatteTexture_getEffectiveSize(texView->baseTexture, &effectiveWidth, &effectiveHeight, nullptr, 0);
g_renderer->shader_unbind(RendererShader::ShaderType::kVertex); shader_unbind(RendererShader::ShaderType::kGeometry);
g_renderer->shader_unbind(RendererShader::ShaderType::kGeometry); shader_bind(shader->GetVertexShader());
g_renderer->shader_unbind(RendererShader::ShaderType::kFragment); shader_bind(shader->GetFragmentShader());
shader->Bind();
shader->SetUniformParameters(*texView, { effectiveWidth, effectiveHeight }, { imageWidth, imageHeight }); shader->SetUniformParameters(*texView, { effectiveWidth, effectiveHeight }, { imageWidth, imageHeight });
// set viewport // set viewport
@ -1433,31 +1428,25 @@ RendererShader* OpenGLRenderer::shader_create(RendererShader::ShaderType type, u
void OpenGLRenderer::shader_bind(RendererShader* shader) void OpenGLRenderer::shader_bind(RendererShader* shader)
{ {
auto shaderGL = (RendererShaderGL*)shader; auto shaderGL = (RendererShaderGL*)shader;
GLbitfield shaderBit; GLbitfield shaderBit;
const auto program = shaderGL->GetProgram(); const auto program = shaderGL->GetProgram();
switch(shader->GetType()) switch(shader->GetType())
{ {
case RendererShader::ShaderType::kVertex: case RendererShader::ShaderType::kVertex:
if (program == prevVertexShaderProgram) if (program == prevVertexShaderProgram)
return; return;
shaderBit = GL_VERTEX_SHADER_BIT; shaderBit = GL_VERTEX_SHADER_BIT;
prevVertexShaderProgram = program; prevVertexShaderProgram = program;
break; break;
case RendererShader::ShaderType::kFragment: case RendererShader::ShaderType::kFragment:
if (program == prevPixelShaderProgram) if (program == prevPixelShaderProgram)
return; return;
shaderBit = GL_FRAGMENT_SHADER_BIT; shaderBit = GL_FRAGMENT_SHADER_BIT;
prevPixelShaderProgram = program; prevPixelShaderProgram = program;
break; break;
case RendererShader::ShaderType::kGeometry: case RendererShader::ShaderType::kGeometry:
if (program == prevGeometryShaderProgram) if (program == prevGeometryShaderProgram)
return; return;
shaderBit = GL_GEOMETRY_SHADER_BIT; shaderBit = GL_GEOMETRY_SHADER_BIT;
prevGeometryShaderProgram = program; prevGeometryShaderProgram = program;
break; break;
@ -1470,13 +1459,6 @@ void OpenGLRenderer::shader_bind(RendererShader* shader)
catchOpenGLError(); catchOpenGLError();
} }
void OpenGLRenderer::shader_bind(GLuint program, GLbitfield shaderType)
{
catchOpenGLError();
glUseProgramStages(m_pipeline, shaderType, program);
catchOpenGLError();
}
void OpenGLRenderer::shader_unbind(RendererShader::ShaderType shaderType) void OpenGLRenderer::shader_unbind(RendererShader::ShaderType shaderType)
{ {
switch (shaderType) { switch (shaderType) {

View File

@ -127,9 +127,8 @@ public:
// shader // shader
RendererShader* shader_create(RendererShader::ShaderType type, uint64 baseHash, uint64 auxHash, const std::string& source, bool isGameShader, bool isGfxPackShader) override; RendererShader* shader_create(RendererShader::ShaderType type, uint64 baseHash, uint64 auxHash, const std::string& source, bool isGameShader, bool isGfxPackShader) override;
void shader_bind(RendererShader* shader) override; void shader_bind(RendererShader* shader);
void shader_bind(GLuint program, GLbitfield shaderType); void shader_unbind(RendererShader::ShaderType shaderType);
void shader_unbind(RendererShader::ShaderType shaderType) override;
// streamout // streamout
void streamout_setupXfbBuffer(uint32 bufferIndex, sint32 ringBufferOffset, uint32 rangeAddr, uint32 rangeSize) override; void streamout_setupXfbBuffer(uint32 bufferIndex, sint32 ringBufferOffset, uint32 rangeAddr, uint32 rangeSize) override;
@ -165,7 +164,6 @@ private:
void texture_syncSliceSpecialBC4(LatteTexture* srcTexture, sint32 srcSliceIndex, sint32 srcMipIndex, LatteTexture* dstTexture, sint32 dstSliceIndex, sint32 dstMipIndex); void texture_syncSliceSpecialBC4(LatteTexture* srcTexture, sint32 srcSliceIndex, sint32 srcMipIndex, LatteTexture* dstTexture, sint32 dstSliceIndex, sint32 dstMipIndex);
void texture_syncSliceSpecialIntegerToBC3(LatteTexture* srcTexture, sint32 srcSliceIndex, sint32 srcMipIndex, LatteTexture* dstTexture, sint32 dstSliceIndex, sint32 dstMipIndex); void texture_syncSliceSpecialIntegerToBC3(LatteTexture* srcTexture, sint32 srcSliceIndex, sint32 srcMipIndex, LatteTexture* dstTexture, sint32 dstSliceIndex, sint32 dstMipIndex);
GLuint m_defaultFramebufferId;
GLuint m_pipeline = 0; GLuint m_pipeline = 0;
bool m_isPadViewContext{}; bool m_isPadViewContext{};
@ -216,8 +214,6 @@ private:
uint32 prevLogicOp = 0; uint32 prevLogicOp = 0;
uint32 prevBlendColorConstant[4] = { 0 }; uint32 prevBlendColorConstant[4] = { 0 };
uint8 prevAlphaTestEnable = 0; uint8 prevAlphaTestEnable = 0;
uint8 prevAlphaTestFunc = 0;
uint32 prevAlphaTestRefU32 = 0;
bool prevDepthEnable = 0; bool prevDepthEnable = 0;
bool prevDepthWriteEnable = 0; bool prevDepthWriteEnable = 0;
Latte::LATTE_DB_DEPTH_CONTROL::E_ZFUNC prevDepthFunc = (Latte::LATTE_DB_DEPTH_CONTROL::E_ZFUNC)-1; Latte::LATTE_DB_DEPTH_CONTROL::E_ZFUNC prevDepthFunc = (Latte::LATTE_DB_DEPTH_CONTROL::E_ZFUNC)-1;
@ -263,9 +259,9 @@ private:
std::vector<class LatteQueryObjectGL*> list_queryCacheOcclusion; // cache for unused queries std::vector<class LatteQueryObjectGL*> list_queryCacheOcclusion; // cache for unused queries
// resource garbage collection // resource garbage collection
struct bufferCacheReleaseQueueEntry_t struct BufferCacheReleaseQueueEntry
{ {
bufferCacheReleaseQueueEntry_t(VirtualBufferHeap_t* heap, VirtualBufferHeapEntry_t* entry) : m_heap(heap), m_entry(entry) {}; BufferCacheReleaseQueueEntry(VirtualBufferHeap_t* heap, VirtualBufferHeapEntry_t* entry) : m_heap(heap), m_entry(entry) {};
void free() void free()
{ {
@ -279,7 +275,7 @@ private:
struct struct
{ {
sint32 index; sint32 index;
std::vector<bufferCacheReleaseQueueEntry_t> bufferCacheEntries; std::vector<BufferCacheReleaseQueueEntry> bufferCacheEntries;
}m_destructionQueues; }m_destructionQueues;
}; };

View File

@ -912,6 +912,21 @@ void OpenGLRenderer::draw_genericDrawHandler(uint32 baseVertex, uint32 baseInsta
{ {
beginPerfMonProfiling(performanceMonitor.gpuTime_dcStageShaderAndUniformMgr); beginPerfMonProfiling(performanceMonitor.gpuTime_dcStageShaderAndUniformMgr);
LatteSHRC_UpdateActiveShaders(); LatteSHRC_UpdateActiveShaders();
LatteDecompilerShader* vs = (LatteDecompilerShader*)LatteSHRC_GetActiveVertexShader();
LatteDecompilerShader* gs = (LatteDecompilerShader*)LatteSHRC_GetActiveGeometryShader();
LatteDecompilerShader* ps = (LatteDecompilerShader*)LatteSHRC_GetActivePixelShader();
if (vs)
shader_bind(vs->shader);
else
shader_unbind(RendererShader::ShaderType::kVertex);
if (ps && LatteGPUState.contextRegister[mmVGT_STRMOUT_EN] == 0)
shader_bind(ps->shader);
else
shader_unbind(RendererShader::ShaderType::kFragment);
if (gs)
shader_bind(gs->shader);
else
shader_unbind(RendererShader::ShaderType::kGeometry);
endPerfMonProfiling(performanceMonitor.gpuTime_dcStageShaderAndUniformMgr); endPerfMonProfiling(performanceMonitor.gpuTime_dcStageShaderAndUniformMgr);
} }
if (LatteGPUState.activeShaderHasError) if (LatteGPUState.activeShaderHasError)

View File

@ -230,11 +230,6 @@ sint32 RendererShaderGL::GetUniformLocation(const char* name)
return glGetUniformLocation(m_program, name); return glGetUniformLocation(m_program, name);
} }
void RendererShaderGL::SetUniform1iv(sint32 location, void* data, sint32 count)
{
glProgramUniform1iv(m_program, location, count, (const GLint*)data);
}
void RendererShaderGL::SetUniform2fv(sint32 location, void* data, sint32 count) void RendererShaderGL::SetUniform2fv(sint32 location, void* data, sint32 count)
{ {
glProgramUniform2fv(m_program, location, count, (const GLfloat*)data); glProgramUniform2fv(m_program, location, count, (const GLfloat*)data);

View File

@ -18,7 +18,6 @@ public:
GLuint GetShaderObject() const { cemu_assert_debug(m_isCompiled); return m_shader_object; } GLuint GetShaderObject() const { cemu_assert_debug(m_isCompiled); return m_shader_object; }
sint32 GetUniformLocation(const char* name) override; sint32 GetUniformLocation(const char* name) override;
void SetUniform1iv(sint32 location, void* data, sint32 count) override;
void SetUniform2fv(sint32 location, void* data, sint32 count) override; void SetUniform2fv(sint32 location, void* data, sint32 count) override;
void SetUniform4iv(sint32 location, void* data, sint32 count) override; void SetUniform4iv(sint32 location, void* data, sint32 count) override;

View File

@ -135,8 +135,6 @@ public:
// shader // shader
virtual RendererShader* shader_create(RendererShader::ShaderType type, uint64 baseHash, uint64 auxHash, const std::string& source, bool compileAsync, bool isGfxPackSource) = 0; virtual RendererShader* shader_create(RendererShader::ShaderType type, uint64 baseHash, uint64 auxHash, const std::string& source, bool compileAsync, bool isGfxPackSource) = 0;
virtual void shader_bind(RendererShader* shader) = 0;
virtual void shader_unbind(RendererShader::ShaderType shaderType) = 0;
// streamout // streamout
virtual void streamout_setupXfbBuffer(uint32 bufferIndex, sint32 ringBufferOffset, uint32 rangeAddr, uint32 rangeSize) = 0; virtual void streamout_setupXfbBuffer(uint32 bufferIndex, sint32 ringBufferOffset, uint32 rangeAddr, uint32 rangeSize) = 0;

View File

@ -233,12 +233,6 @@ void RendererOutputShader::SetUniformParameters(const LatteTextureView& texture_
} }
} }
void RendererOutputShader::Bind() const
{
g_renderer->shader_bind(m_vertex_shader);
g_renderer->shader_bind(m_fragment_shader);
}
RendererOutputShader* RendererOutputShader::s_copy_shader; RendererOutputShader* RendererOutputShader::s_copy_shader;
RendererOutputShader* RendererOutputShader::s_copy_shader_ud; RendererOutputShader* RendererOutputShader::s_copy_shader_ud;

View File

@ -18,7 +18,6 @@ public:
virtual ~RendererOutputShader() = default; virtual ~RendererOutputShader() = default;
void SetUniformParameters(const LatteTextureView& texture_view, const Vector2i& input_res, const Vector2i& output_res) const; void SetUniformParameters(const LatteTextureView& texture_view, const Vector2i& input_res, const Vector2i& output_res) const;
void Bind() const;
RendererShader* GetVertexShader() const RendererShader* GetVertexShader() const
{ {

View File

@ -20,7 +20,6 @@ public:
virtual sint32 GetUniformLocation(const char* name) = 0; virtual sint32 GetUniformLocation(const char* name) = 0;
virtual void SetUniform1iv(sint32 location, void* data, sint32 count) = 0;
virtual void SetUniform2fv(sint32 location, void* data, sint32 count) = 0; virtual void SetUniform2fv(sint32 location, void* data, sint32 count) = 0;
virtual void SetUniform4iv(sint32 location, void* data, sint32 count) = 0; virtual void SetUniform4iv(sint32 location, void* data, sint32 count) = 0;

View File

@ -227,11 +227,6 @@ sint32 RendererShaderVk::GetUniformLocation(const char* name)
return 0; return 0;
} }
void RendererShaderVk::SetUniform1iv(sint32 location, void* data, sint32 count)
{
cemu_assert_suspicious();
}
void RendererShaderVk::SetUniform2fv(sint32 location, void* data, sint32 count) void RendererShaderVk::SetUniform2fv(sint32 location, void* data, sint32 count)
{ {
cemu_assert_suspicious(); cemu_assert_suspicious();

View File

@ -32,7 +32,6 @@ public:
static void Shutdown(); static void Shutdown();
sint32 GetUniformLocation(const char* name) override; sint32 GetUniformLocation(const char* name) override;
void SetUniform1iv(sint32 location, void* data, sint32 count) override;
void SetUniform2fv(sint32 location, void* data, sint32 count) override; void SetUniform2fv(sint32 location, void* data, sint32 count) override;
void SetUniform4iv(sint32 location, void* data, sint32 count) override; void SetUniform4iv(sint32 location, void* data, sint32 count) override;
VkShaderModule& GetShaderModule() { return m_shader_module; } VkShaderModule& GetShaderModule() { return m_shader_module; }

View File

@ -1090,17 +1090,6 @@ RendererShader* VulkanRenderer::shader_create(RendererShader::ShaderType type, u
return new RendererShaderVk(type, baseHash, auxHash, isGameShader, isGfxPackShader, source); return new RendererShaderVk(type, baseHash, auxHash, isGameShader, isGfxPackShader, source);
} }
void VulkanRenderer::shader_bind(RendererShader* shader)
{
// does nothing on Vulkan
// remove from main render backend and internalize into GL backend
}
void VulkanRenderer::shader_unbind(RendererShader::ShaderType shaderType)
{
// does nothing on Vulkan
}
bool VulkanRenderer::CheckDeviceExtensionSupport(const VkPhysicalDevice device, FeatureControl& info) bool VulkanRenderer::CheckDeviceExtensionSupport(const VkPhysicalDevice device, FeatureControl& info)
{ {
std::vector<VkExtensionProperties> availableDeviceExtensions; std::vector<VkExtensionProperties> availableDeviceExtensions;

View File

@ -350,8 +350,6 @@ public:
void buffer_bindUniformBuffer(LatteConst::ShaderType shaderType, uint32 bufferIndex, uint32 offset, uint32 size) override; void buffer_bindUniformBuffer(LatteConst::ShaderType shaderType, uint32 bufferIndex, uint32 offset, uint32 size) override;
RendererShader* shader_create(RendererShader::ShaderType type, uint64 baseHash, uint64 auxHash, const std::string& source, bool isGameShader, bool isGfxPackShader) override; RendererShader* shader_create(RendererShader::ShaderType type, uint64 baseHash, uint64 auxHash, const std::string& source, bool isGameShader, bool isGfxPackShader) override;
void shader_bind(RendererShader* shader) override;
void shader_unbind(RendererShader::ShaderType shaderType) override;
void* indexData_reserveIndexMemory(uint32 size, uint32& offset, uint32& bufferIndex) override; void* indexData_reserveIndexMemory(uint32 size, uint32& offset, uint32& bufferIndex) override;
void indexData_uploadIndexMemory(uint32 offset, uint32 size) override; void indexData_uploadIndexMemory(uint32 offset, uint32 size) override;