Skip to content

Commit

Permalink
programmatically prepend shader preamble
Browse files Browse the repository at this point in the history
  • Loading branch information
goeiecool9999 committed Oct 28, 2024
1 parent 21899d1 commit c86e164
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 39 deletions.
65 changes: 26 additions & 39 deletions src/Cafe/HW/Latte/Renderer/RendererOuputShader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,7 @@
#include "Cafe/HW/Latte/Renderer/OpenGL/OpenGLRenderer.h"

const std::string RendererOutputShader::s_copy_shader_source =
R"(#version 420
layout(location = 0) in vec2 passUV;
layout(binding = 0) uniform sampler2D textureSrc;
layout(location = 0) out vec4 colorOut0;
R"(
void main()
{
colorOut0 = vec4(texture(textureSrc, passUV).rgb,1.0);
Expand All @@ -16,22 +11,6 @@ void main()

const std::string RendererOutputShader::s_bicubic_shader_source =
R"(
#version 420
#ifdef VULKAN
layout(push_constant) uniform pc {
vec2 textureSrcResolution;
vec2 inputResolution;
vec2 outputResolution;
};
#else
uniform vec2 inputResolution;
#endif
layout(location = 0) in vec2 passUV;
layout(binding = 0) uniform sampler2D textureSrc;
layout(location = 0) out vec4 colorOut0;
vec4 cubic(float x)
{
float x2 = x * x;
Expand Down Expand Up @@ -77,22 +56,7 @@ void main(){
)";

const std::string RendererOutputShader::s_hermite_shader_source =
R"(#version 420
#ifdef VULKAN
layout(push_constant) uniform pc {
vec2 textureSrcResolution;
vec2 inputResolution;
vec2 outputResolution;
};
#else
uniform vec2 inputResolution;
#endif
layout(location = 0) in vec2 passUV;
layout(binding=0) uniform sampler2D textureSrc;
layout(location = 0) out vec4 colorOut0;
R"(
// https://www.shadertoy.com/view/MllSzX
vec3 CubicHermite (vec3 A, vec3 B, vec3 C, vec3 D, float t)
Expand Down Expand Up @@ -152,8 +116,10 @@ void main(){

RendererOutputShader::RendererOutputShader(const std::string& vertex_source, const std::string& fragment_source)
{
auto finalFragmentSrc = PrependFragmentPreamble(fragment_source);

m_vertex_shader = g_renderer->shader_create(RendererShader::ShaderType::kVertex, 0, 0, vertex_source, false, false);
m_fragment_shader = g_renderer->shader_create(RendererShader::ShaderType::kFragment, 0, 0, fragment_source, false, false);
m_fragment_shader = g_renderer->shader_create(RendererShader::ShaderType::kFragment, 0, 0, finalFragmentSrc, false, false);

m_vertex_shader->PreponeCompilation(true);
m_fragment_shader->PreponeCompilation(true);
Expand Down Expand Up @@ -332,6 +298,27 @@ void main(){
)";
return vertex_source.str();
}

std::string RendererOutputShader::PrependFragmentPreamble(const std::string& shaderSrc)
{
return R"(#version 420
#ifdef VULKAN
layout(push_constant) uniform pc {
vec2 textureSrcResolution;
vec2 inputResolution;
vec2 outputResolution;
};
#else
uniform vec2 textureSrcResolution;
uniform vec2 inputResolution;
uniform vec2 outputResolution;
#endif
layout(location = 0) in vec2 passUV;
layout(binding=0) uniform sampler2D textureSrc;
layout(location = 0) out vec4 colorOut0;
)" + shaderSrc;
}
void RendererOutputShader::InitializeStatic()
{
std::string vertex_source, vertex_source_ud;
Expand Down
2 changes: 2 additions & 0 deletions src/Cafe/HW/Latte/Renderer/RendererOuputShader.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ class RendererOutputShader
static std::string GetVulkanVertexSource(bool render_upside_down);
static std::string GetOpenGlVertexSource(bool render_upside_down);

static std::string PrependFragmentPreamble(const std::string& shaderSrc);

protected:
RendererShader* m_vertex_shader;
RendererShader* m_fragment_shader;
Expand Down

0 comments on commit c86e164

Please sign in to comment.