change sRGB encoding to uniform and push constants, never rely on hardware sRGB decoding/encoding

This commit is contained in:
goeiecool9999 2025-09-02 20:43:18 +02:00
parent de4bf7c2c1
commit 548cf3e965
10 changed files with 75 additions and 33 deletions

View file

@ -205,7 +205,7 @@ ImTextureID OpenGLRenderer::GenerateTexture(const std::vector<uint8>& data, cons
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
glActiveTexture(GL_TEXTURE0); glActiveTexture(GL_TEXTURE0);
glTexImage2D(GL_TEXTURE_2D, 0, GL_SRGB, size.x, size.y, 0, GL_RGB, GL_UNSIGNED_BYTE, data.data()); glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB8, size.x, size.y, 0, GL_RGB, GL_UNSIGNED_BYTE, data.data());
return (ImTextureID)(uintptr_t)textureId; return (ImTextureID)(uintptr_t)textureId;
} }
@ -318,8 +318,8 @@ 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");
// enable framebuffer SRGB support // display raw fragment shader output, we handle gamma encoding manually.
glEnable(GL_FRAMEBUFFER_SRGB); glDisable(GL_FRAMEBUFFER_SRGB);
if (this->m_vendor != GfxVendor::AMD) if (this->m_vendor != GfxVendor::AMD)
{ {
@ -603,7 +603,7 @@ void OpenGLRenderer::DrawBackbufferQuad(LatteTextureView* texView, RendererOutpu
shader_unbind(RendererShader::ShaderType::kGeometry); shader_unbind(RendererShader::ShaderType::kGeometry);
shader_bind(shader->GetVertexShader()); shader_bind(shader->GetVertexShader());
shader_bind(shader->GetFragmentShader()); shader_bind(shader->GetFragmentShader());
shader->SetUniformParameters(*texView, {imageWidth, imageHeight}); shader->SetUniformParameters(*texView, {imageWidth, imageHeight}, padView);
// set viewport // set viewport
glViewportIndexedf(0, imageX, imageY, imageWidth, imageHeight); glViewportIndexedf(0, imageX, imageY, imageWidth, imageHeight);
@ -620,15 +620,9 @@ void OpenGLRenderer::DrawBackbufferQuad(LatteTextureView* texView, RendererOutpu
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, useLinearTexFilter ? GL_LINEAR : GL_NEAREST); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, useLinearTexFilter ? GL_LINEAR : GL_NEAREST);
texViewGL->samplerState.filterMag = 0xFFFFFFFF; texViewGL->samplerState.filterMag = 0xFFFFFFFF;
if ((!padView && !LatteGPUState.tvBufferUsesSRGB) || (padView && !LatteGPUState.drcBufferUsesSRGB))
glDisable(GL_FRAMEBUFFER_SRGB);
uint16 indexData[6] = { 0,1,2,3,4,5 }; uint16 indexData[6] = { 0,1,2,3,4,5 };
glDrawRangeElements(GL_TRIANGLES, 0, 5, 6, GL_UNSIGNED_SHORT, indexData); glDrawRangeElements(GL_TRIANGLES, 0, 5, 6, GL_UNSIGNED_SHORT, indexData);
if ((!padView && !LatteGPUState.tvBufferUsesSRGB) || (padView && !LatteGPUState.drcBufferUsesSRGB))
glEnable(GL_FRAMEBUFFER_SRGB);
// unbind texture // unbind texture
texture_bindAndActivate(nullptr, 0); texture_bindAndActivate(nullptr, 0);

View file

@ -227,6 +227,11 @@ sint32 RendererShaderGL::GetUniformLocation(const char* name)
return glGetUniformLocation(m_program, name); return glGetUniformLocation(m_program, name);
} }
void RendererShaderGL::SetUniform1i(sint32 location, sint32 value)
{
glProgramUniform1i(m_program, location, value);
}
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,6 +18,8 @@ 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 SetUniform1i(sint32 location, sint32 value) 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

@ -3,7 +3,7 @@
const std::string RendererOutputShader::s_copy_shader_source = const std::string RendererOutputShader::s_copy_shader_source =
R"( R"(
void main() void outputShader()
{ {
colorOut0 = vec4(texture(textureSrc, passUV).rgb,1.0); colorOut0 = vec4(texture(textureSrc, passUV).rgb,1.0);
} }
@ -49,7 +49,7 @@ vec4 bcFilter(vec2 uv, vec4 texelSize)
mix(sample1, sample0, sx), sy); mix(sample1, sample0, sx), sy);
} }
void main(){ void outputShader(){
vec4 texelSize = vec4( 1.0 / textureSrcResolution.xy, textureSrcResolution.xy); vec4 texelSize = vec4( 1.0 / textureSrcResolution.xy, textureSrcResolution.xy);
colorOut0 = vec4(bcFilter(passUV, texelSize).rgb,1.0); colorOut0 = vec4(bcFilter(passUV, texelSize).rgb,1.0);
} }
@ -108,7 +108,7 @@ vec3 BicubicHermiteTexture(vec2 uv, vec4 texelSize)
return CubicHermite(CP0X, CP1X, CP2X, CP3X, frac.y); return CubicHermite(CP0X, CP1X, CP2X, CP3X, frac.y);
} }
void main(){ void outputShader(){
vec4 texelSize = vec4( 1.0 / textureSrcResolution.xy, textureSrcResolution.xy); vec4 texelSize = vec4( 1.0 / textureSrcResolution.xy, textureSrcResolution.xy);
colorOut0 = vec4(BicubicHermiteTexture(passUV, texelSize), 1.0); colorOut0 = vec4(BicubicHermiteTexture(passUV, texelSize), 1.0);
} }
@ -135,14 +135,16 @@ RendererOutputShader::RendererOutputShader(const std::string& vertex_source, con
m_uniformLocations[0].m_loc_textureSrcResolution = m_vertex_shader->GetUniformLocation("textureSrcResolution"); m_uniformLocations[0].m_loc_textureSrcResolution = m_vertex_shader->GetUniformLocation("textureSrcResolution");
m_uniformLocations[0].m_loc_nativeResolution = m_vertex_shader->GetUniformLocation("nativeResolution"); m_uniformLocations[0].m_loc_nativeResolution = m_vertex_shader->GetUniformLocation("nativeResolution");
m_uniformLocations[0].m_loc_outputResolution = m_vertex_shader->GetUniformLocation("outputResolution"); m_uniformLocations[0].m_loc_outputResolution = m_vertex_shader->GetUniformLocation("outputResolution");
m_uniformLocations[0].m_loc_applySRGBEncoding = m_vertex_shader->GetUniformLocation("applySRGBEncoding");
m_uniformLocations[1].m_loc_textureSrcResolution = m_fragment_shader->GetUniformLocation("textureSrcResolution"); m_uniformLocations[1].m_loc_textureSrcResolution = m_fragment_shader->GetUniformLocation("textureSrcResolution");
m_uniformLocations[1].m_loc_nativeResolution = m_fragment_shader->GetUniformLocation("nativeResolution"); m_uniformLocations[1].m_loc_nativeResolution = m_fragment_shader->GetUniformLocation("nativeResolution");
m_uniformLocations[1].m_loc_outputResolution = m_fragment_shader->GetUniformLocation("outputResolution"); m_uniformLocations[1].m_loc_outputResolution = m_fragment_shader->GetUniformLocation("outputResolution");
m_uniformLocations[1].m_loc_applySRGBEncoding = m_fragment_shader->GetUniformLocation("applySRGBEncoding");
} }
} }
void RendererOutputShader::SetUniformParameters(const LatteTextureView& texture_view, const Vector2i& output_res) const void RendererOutputShader::SetUniformParameters(const LatteTextureView& texture_view, const Vector2i& output_res, const bool padView) const
{ {
sint32 effectiveWidth, effectiveHeight; sint32 effectiveWidth, effectiveHeight;
texture_view.baseTexture->GetEffectiveSize(effectiveWidth, effectiveHeight, 0); texture_view.baseTexture->GetEffectiveSize(effectiveWidth, effectiveHeight, 0);
@ -168,6 +170,11 @@ void RendererOutputShader::SetUniformParameters(const LatteTextureView& texture_
res[1] = (float)output_res.y; res[1] = (float)output_res.y;
shader->SetUniform2fv(locations.m_loc_outputResolution, res, 1); shader->SetUniform2fv(locations.m_loc_outputResolution, res, 1);
} }
if (locations.m_loc_applySRGBEncoding != -1)
{
shader->SetUniform1i(locations.m_loc_applySRGBEncoding, padView ? LatteGPUState.drcBufferUsesSRGB : LatteGPUState.tvBufferUsesSRGB);
}
}; };
setUniforms(m_vertex_shader.get(), m_uniformLocations[0]); setUniforms(m_vertex_shader.get(), m_uniformLocations[0]);
setUniforms(m_fragment_shader.get(), m_uniformLocations[1]); setUniforms(m_fragment_shader.get(), m_uniformLocations[1]);
@ -290,16 +297,44 @@ layout(push_constant) uniform pc {
vec2 textureSrcResolution; vec2 textureSrcResolution;
vec2 nativeResolution; vec2 nativeResolution;
vec2 outputResolution; vec2 outputResolution;
bool applySRGBEncoding; // true = app requested sRGB encoding
}; };
#else #else
uniform vec2 textureSrcResolution; uniform vec2 textureSrcResolution;
uniform vec2 nativeResolution; uniform vec2 nativeResolution;
uniform vec2 outputResolution; uniform vec2 outputResolution;
uniform bool applySRGBEncoding;
#endif #endif
layout(location = 0) smooth in vec2 passUV; layout(location = 0) smooth in vec2 passUV;
layout(binding = 0) uniform sampler2D textureSrc; layout(binding = 0) uniform sampler2D textureSrc;
layout(location = 0) out vec4 colorOut0; layout(location = 0) out vec4 colorOut0;
float sRGBEncode(float linear)
{
if(linear <= 0.0031308)
return 12.92f * linear;
else
return 1.055f * pow(linear, 1.0f / 2.4f) - 0.055f;
}
vec3 sRGBEncode(vec3 linear)
{
return vec3(sRGBEncode(linear.r), sRGBEncode(linear.g), sRGBEncode(linear.b));
}
// fwd. declaration
void outputShader();
void main()
{
outputShader(); // sets colorOut0
if(applySRGBEncoding)
{
colorOut0 = vec4(sRGBEncode(colorOut0.xyz), 1.0f);
}
}
)" + shaderSrc; )" + shaderSrc;
} }
void RendererOutputShader::InitializeStatic() void RendererOutputShader::InitializeStatic()

View file

@ -17,7 +17,7 @@ public:
RendererOutputShader(const std::string& vertex_source, const std::string& fragment_source); RendererOutputShader(const std::string& vertex_source, const std::string& fragment_source);
virtual ~RendererOutputShader() = default; virtual ~RendererOutputShader() = default;
void SetUniformParameters(const LatteTextureView& texture_view, const Vector2i& output_res) const; void SetUniformParameters(const LatteTextureView& texture_view, const Vector2i& output_res, const bool padView) const;
RendererShader* GetVertexShader() const RendererShader* GetVertexShader() const
{ {
@ -55,6 +55,7 @@ protected:
sint32 m_loc_textureSrcResolution = -1; sint32 m_loc_textureSrcResolution = -1;
sint32 m_loc_nativeResolution = -1; sint32 m_loc_nativeResolution = -1;
sint32 m_loc_outputResolution = -1; sint32 m_loc_outputResolution = -1;
sint32 m_loc_applySRGBEncoding = -1;
} m_uniformLocations[2]{}; } m_uniformLocations[2]{};
private: private:

View file

@ -20,6 +20,7 @@ public:
virtual sint32 GetUniformLocation(const char* name) = 0; virtual sint32 GetUniformLocation(const char* name) = 0;
virtual void SetUniform1i(sint32 location, sint32 value) = 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

@ -232,6 +232,11 @@ sint32 RendererShaderVk::GetUniformLocation(const char* name)
return 0; return 0;
} }
void RendererShaderVk::SetUniform1i(sint32 location, sint32 value)
{
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,6 +32,7 @@ public:
static void Shutdown(); static void Shutdown();
sint32 GetUniformLocation(const char* name) override; sint32 GetUniformLocation(const char* name) override;
void SetUniform1i(sint32 location, sint32 value) 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

@ -318,20 +318,10 @@ VkSurfaceFormatKHR SwapchainInfoVk::ChooseSurfaceFormat(const std::vector<VkSurf
return{ VK_FORMAT_B8G8R8A8_UNORM, VK_COLOR_SPACE_SRGB_NONLINEAR_KHR }; return{ VK_FORMAT_B8G8R8A8_UNORM, VK_COLOR_SPACE_SRGB_NONLINEAR_KHR };
for (const auto& format : formats) for (const auto& format : formats)
{
bool useSRGB = mainWindow ? LatteGPUState.tvBufferUsesSRGB : LatteGPUState.drcBufferUsesSRGB;
if (useSRGB)
{
if (format.format == VK_FORMAT_B8G8R8A8_SRGB && format.colorSpace == VK_COLOR_SPACE_SRGB_NONLINEAR_KHR)
return format;
}
else
{ {
if (format.format == VK_FORMAT_B8G8R8A8_UNORM && format.colorSpace == VK_COLOR_SPACE_SRGB_NONLINEAR_KHR) if (format.format == VK_FORMAT_B8G8R8A8_UNORM && format.colorSpace == VK_COLOR_SPACE_SRGB_NONLINEAR_KHR)
return format; return format;
} }
}
return formats[0]; return formats[0];
} }

View file

@ -2659,7 +2659,7 @@ VkPipeline VulkanRenderer::backbufferBlit_createGraphicsPipeline(VkDescriptorSet
VkPushConstantRange pushConstantRange{ VkPushConstantRange pushConstantRange{
.stageFlags = VK_SHADER_STAGE_FRAGMENT_BIT, .stageFlags = VK_SHADER_STAGE_FRAGMENT_BIT,
.offset = 0, .offset = 0,
.size = 3 * sizeof(float) * 2 // 3 vec2's .size = 3 * sizeof(float) * 2 + 1 // 3 vec2's + 1 bool
}; };
VkPipelineLayoutCreateInfo pipelineLayoutInfo{}; VkPipelineLayoutCreateInfo pipelineLayoutInfo{};
@ -3046,24 +3046,32 @@ void VulkanRenderer::DrawBackbufferQuad(LatteTextureView* texView, RendererOutpu
vkCmdBindDescriptorSets(m_state.currentCommandBuffer, VK_PIPELINE_BIND_POINT_GRAPHICS, m_pipelineLayout, 0, 1, &descriptSet, 0, nullptr); vkCmdBindDescriptorSets(m_state.currentCommandBuffer, VK_PIPELINE_BIND_POINT_GRAPHICS, m_pipelineLayout, 0, 1, &descriptSet, 0, nullptr);
// update push constants // update push constants
Vector2f pushData[3]; struct
{
Vector2f vecs[3];
bool applySRGBEncoding;
} pushData;
// textureSrcResolution // textureSrcResolution
sint32 effectiveWidth, effectiveHeight; sint32 effectiveWidth, effectiveHeight;
texView->baseTexture->GetEffectiveSize(effectiveWidth, effectiveHeight, 0); texView->baseTexture->GetEffectiveSize(effectiveWidth, effectiveHeight, 0);
pushData[0] = {(float)effectiveWidth, (float)effectiveHeight}; pushData.vecs[0] = {(float)effectiveWidth, (float)effectiveHeight};
// nativeResolution // nativeResolution
pushData[1] = { pushData.vecs[1] = {
(float)texViewVk->baseTexture->width, (float)texViewVk->baseTexture->width,
(float)texViewVk->baseTexture->height, (float)texViewVk->baseTexture->height,
}; };
// outputResolution // outputResolution
pushData[2] = {(float)imageWidth,(float)imageHeight}; pushData.vecs[2] = {(float)imageWidth,(float)imageHeight};
vkCmdPushConstants(m_state.currentCommandBuffer, m_pipelineLayout, VK_SHADER_STAGE_FRAGMENT_BIT, 0, sizeof(float) * 2 * 3, &pushData); // applySRGBEncoding
pushData.applySRGBEncoding = padView ? LatteGPUState.drcBufferUsesSRGB : LatteGPUState.tvBufferUsesSRGB;
vkCmdPushConstants(m_state.currentCommandBuffer, m_pipelineLayout, VK_SHADER_STAGE_FRAGMENT_BIT, 0, sizeof(pushData), &pushData);
vkCmdDraw(m_state.currentCommandBuffer, 6, 1, 0, 0); vkCmdDraw(m_state.currentCommandBuffer, 6, 1, 0, 0);