Change uniforms and cleanup code

- Make textureSrcResolution the actual resolution of the textureSrc sampler image
  This reflects the expectations of graphic pack creators
- Rename inputResolution to nativeResolution which is the non-overriden size
  Not sure if this is useful, but it's there already so let's not remove it.
- Change SetUniformParameters to use lambda expression
This commit is contained in:
goeiecool9999 2024-10-29 20:58:28 +01:00
parent cd4284ab63
commit c98a0c3ae5
3 changed files with 44 additions and 63 deletions

View File

@ -50,7 +50,7 @@ vec4 bcFilter(vec2 uv, vec4 texelSize)
} }
void main(){ void main(){
vec4 texelSize = vec4( 1.0 / inputResolution.xy, inputResolution.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);
} }
)"; )";
@ -109,7 +109,7 @@ vec3 BicubicHermiteTexture(vec2 uv, vec4 texelSize)
} }
void main(){ void main(){
vec4 texelSize = vec4( 1.0 / inputResolution.xy, inputResolution.xy); vec4 texelSize = vec4( 1.0 / textureSrcResolution.xy, textureSrcResolution.xy);
colorOut0 = vec4(BicubicHermiteTexture(passUV, texelSize), 1.0); colorOut0 = vec4(BicubicHermiteTexture(passUV, texelSize), 1.0);
} }
)"; )";
@ -132,62 +132,43 @@ RendererOutputShader::RendererOutputShader(const std::string& vertex_source, con
if (g_renderer->GetType() == RendererAPI::OpenGL) if (g_renderer->GetType() == RendererAPI::OpenGL)
{ {
m_attributes[0].m_loc_texture_src_resolution = m_vertex_shader->GetUniformLocation("textureSrcResolution"); m_uniformLocations[0].m_loc_textureSrcResolution = m_vertex_shader->GetUniformLocation("textureSrcResolution");
m_attributes[0].m_loc_input_resolution = m_vertex_shader->GetUniformLocation("inputResolution"); m_uniformLocations[0].m_loc_nativeResolution = m_vertex_shader->GetUniformLocation("nativeResolution");
m_attributes[0].m_loc_output_resolution = m_vertex_shader->GetUniformLocation("outputResolution"); m_uniformLocations[0].m_loc_outputResolution = m_vertex_shader->GetUniformLocation("outputResolution");
m_attributes[1].m_loc_texture_src_resolution = m_fragment_shader->GetUniformLocation("textureSrcResolution"); m_uniformLocations[1].m_loc_textureSrcResolution = m_fragment_shader->GetUniformLocation("textureSrcResolution");
m_attributes[1].m_loc_input_resolution = m_fragment_shader->GetUniformLocation("inputResolution"); m_uniformLocations[1].m_loc_nativeResolution = m_fragment_shader->GetUniformLocation("nativeResolution");
m_attributes[1].m_loc_output_resolution = m_fragment_shader->GetUniformLocation("outputResolution"); m_uniformLocations[1].m_loc_outputResolution = m_fragment_shader->GetUniformLocation("outputResolution");
} }
} }
void RendererOutputShader::SetUniformParameters(const LatteTextureView& texture_view, const Vector2i& input_res, const Vector2i& output_res) const void RendererOutputShader::SetUniformParameters(const LatteTextureView& texture_view, const Vector2i& input_res, const Vector2i& output_res) const
{ {
float res[2]; auto setUniforms = [&](RendererShader* shader, const UniformLocations& attributes){
// vertex shader float res[2];
if (m_attributes[0].m_loc_texture_src_resolution != -1) if (attributes.m_loc_textureSrcResolution != -1)
{ {
res[0] = (float)texture_view.baseTexture->width; res[0] = (float)input_res.x;
res[1] = (float)texture_view.baseTexture->height; res[1] = (float)input_res.y;
m_vertex_shader->SetUniform2fv(m_attributes[0].m_loc_texture_src_resolution, res, 1); shader->SetUniform2fv(attributes.m_loc_textureSrcResolution, res, 1);
} }
if (m_attributes[0].m_loc_input_resolution != -1) if (attributes.m_loc_nativeResolution != -1)
{ {
res[0] = (float)input_res.x; res[0] = (float)texture_view.baseTexture->width;
res[1] = (float)input_res.y; res[1] = (float)texture_view.baseTexture->height;
m_vertex_shader->SetUniform2fv(m_attributes[0].m_loc_input_resolution, res, 1); shader->SetUniform2fv(attributes.m_loc_nativeResolution, res, 1);
} }
if (m_attributes[0].m_loc_output_resolution != -1) if (attributes.m_loc_outputResolution != -1)
{ {
res[0] = (float)output_res.x; res[0] = (float)output_res.x;
res[1] = (float)output_res.y; res[1] = (float)output_res.y;
m_vertex_shader->SetUniform2fv(m_attributes[0].m_loc_output_resolution, res, 1); shader->SetUniform2fv(attributes.m_loc_outputResolution, res, 1);
} }
};
// fragment shader setUniforms(m_vertex_shader, m_uniformLocations[0]);
if (m_attributes[1].m_loc_texture_src_resolution != -1) setUniforms(m_fragment_shader, m_uniformLocations[1]);
{
res[0] = (float)texture_view.baseTexture->width;
res[1] = (float)texture_view.baseTexture->height;
m_fragment_shader->SetUniform2fv(m_attributes[1].m_loc_texture_src_resolution, res, 1);
}
if (m_attributes[1].m_loc_input_resolution != -1)
{
res[0] = (float)input_res.x;
res[1] = (float)input_res.y;
m_fragment_shader->SetUniform2fv(m_attributes[1].m_loc_input_resolution, res, 1);
}
if (m_attributes[1].m_loc_output_resolution != -1)
{
res[0] = (float)output_res.x;
res[1] = (float)output_res.y;
m_fragment_shader->SetUniform2fv(m_attributes[1].m_loc_output_resolution, res, 1);
}
} }
RendererOutputShader* RendererOutputShader::s_copy_shader; RendererOutputShader* RendererOutputShader::s_copy_shader;
@ -305,12 +286,12 @@ std::string RendererOutputShader::PrependFragmentPreamble(const std::string& sha
#ifdef VULKAN #ifdef VULKAN
layout(push_constant) uniform pc { layout(push_constant) uniform pc {
vec2 textureSrcResolution; vec2 textureSrcResolution;
vec2 inputResolution; vec2 nativeResolution;
vec2 outputResolution; vec2 outputResolution;
}; };
#else #else
uniform vec2 textureSrcResolution; uniform vec2 textureSrcResolution;
uniform vec2 inputResolution; uniform vec2 nativeResolution;
uniform vec2 outputResolution; uniform vec2 outputResolution;
#endif #endif

View File

@ -49,12 +49,12 @@ protected:
RendererShader* m_vertex_shader; RendererShader* m_vertex_shader;
RendererShader* m_fragment_shader; RendererShader* m_fragment_shader;
struct struct UniformLocations
{ {
sint32 m_loc_texture_src_resolution = -1; sint32 m_loc_textureSrcResolution = -1;
sint32 m_loc_input_resolution = -1; sint32 m_loc_nativeResolution = -1;
sint32 m_loc_output_resolution = -1; sint32 m_loc_outputResolution = -1;
} m_attributes[2]{}; } m_uniformLocations[2]{};
private: private:
static const std::string s_copy_shader_source; static const std::string s_copy_shader_source;

View File

@ -2968,16 +2968,16 @@ void VulkanRenderer::DrawBackbufferQuad(LatteTextureView* texView, RendererOutpu
Vector2f pushData; Vector2f pushData;
// textureSrcResolution // textureSrcResolution
sint32 effectiveWidth, effectiveHeight;
texView->baseTexture->GetEffectiveSize(effectiveWidth, effectiveHeight, 0);
pushData = {(float)effectiveWidth, (float)effectiveHeight};
vkCmdPushConstants(m_state.currentCommandBuffer, m_pipelineLayout, VK_SHADER_STAGE_FRAGMENT_BIT, 0*sizeof(float)*2, sizeof(float)*2, &pushData);
// nativeResolution
pushData = { pushData = {
(float)texViewVk->baseTexture->width, (float)texViewVk->baseTexture->width,
(float)texViewVk->baseTexture->height, (float)texViewVk->baseTexture->height,
}; };
vkCmdPushConstants(m_state.currentCommandBuffer, m_pipelineLayout, VK_SHADER_STAGE_FRAGMENT_BIT, 0*sizeof(float)*2, sizeof(float)*2, &pushData);
// inputResolution
sint32 effectiveWidth, effectiveHeight;
texView->baseTexture->GetEffectiveSize(effectiveWidth, effectiveHeight, 0);
pushData = {(float)effectiveWidth, (float)effectiveHeight};
vkCmdPushConstants(m_state.currentCommandBuffer, m_pipelineLayout, VK_SHADER_STAGE_FRAGMENT_BIT, 1*sizeof(float)*2, sizeof(float)*2, &pushData); vkCmdPushConstants(m_state.currentCommandBuffer, m_pipelineLayout, VK_SHADER_STAGE_FRAGMENT_BIT, 1*sizeof(float)*2, sizeof(float)*2, &pushData);
// outputResolution // outputResolution