Skip to content

Commit

Permalink
Move texel shift logic from fragment shader to vertex shader.
Browse files Browse the repository at this point in the history
Separate checkerboard and blur quad shaders.
Add specialization to vertex shader.
  • Loading branch information
vcoda committed Oct 6, 2020
1 parent d2a7fe0 commit 1559930
Show file tree
Hide file tree
Showing 11 changed files with 111 additions and 36 deletions.
12 changes: 6 additions & 6 deletions blur-gaussian/blur-gaussian.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ class GaussianBlur : public GraphicsApp
// 1. Horizontal pass
horzDescriptor.layout = std::shared_ptr<magma::DescriptorSetLayout>(new magma::DescriptorSetLayout(device,
{
FragmentStageBinding(0, CombinedImageSampler(1)),
VertexFragmentStageBinding(0, CombinedImageSampler(1)),
FragmentStageBinding(1, StorageBuffer(1)),
}));
horzDescriptor.set = descriptorPool->allocateDescriptorSet(horzDescriptor.layout);
Expand All @@ -111,7 +111,7 @@ class GaussianBlur : public GraphicsApp
// 2. Vertical pass
vertDescriptor.layout = std::shared_ptr<magma::DescriptorSetLayout>(new magma::DescriptorSetLayout(device,
{
FragmentStageBinding(0, CombinedImageSampler(1)),
VertexFragmentStageBinding(0, CombinedImageSampler(1)),
FragmentStageBinding(1, StorageBuffer(1)),
}));
vertDescriptor.set = descriptorPool->allocateDescriptorSet(vertDescriptor.layout);
Expand All @@ -127,11 +127,11 @@ class GaussianBlur : public GraphicsApp
Constants constants;
constants.horzPass = VK_TRUE;
auto specialization = std::make_shared<magma::Specialization>(constants, entry);
horzPassPipeline = createFullscreenPipeline("quad.o", "blur.o", std::move(specialization), horzDescriptor.layout, tempFramebuffer);
horzPassPipeline = createFullscreenPipeline("quadoff.o", "blur.o", std::move(specialization), horzDescriptor.layout, tempFramebuffer);
// 2. Vertical pass
constants.horzPass = VK_FALSE;
specialization = std::make_shared<magma::Specialization>(constants, entry);
vertPassPipeline = createFullscreenPipeline("quad.o", "blur.o", std::move(specialization), vertDescriptor.layout, framebuffers[0]);
vertPassPipeline = createFullscreenPipeline("quadoff.o", "blur.o", std::move(specialization), vertDescriptor.layout, framebuffers[0]);
}

void renderScene(uint32_t bufferIndex)
Expand All @@ -150,8 +150,8 @@ class GaussianBlur : public GraphicsApp
{
if (blurImage)
cmdBuffer->beginRenderPass(inputFramebuffer->getRenderPass(), inputFramebuffer->getFramebuffer());
else
cmdBuffer->beginRenderPass(renderPass, framebuffers[bufferIndex]); // Draw to swapchain
else // Draw to swapchain
cmdBuffer->beginRenderPass(renderPass, framebuffers[bufferIndex]);
{
cmdBuffer->bindPipeline(checkerboardPipeline);
cmdBuffer->draw(4, 0);
Expand Down
15 changes: 14 additions & 1 deletion blur-gaussian/blur-gaussian.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
<Command Condition="'$(Configuration)|$(Platform)'=='Release|x64'">$(VK_SDK_PATH)\Bin32\glslangValidator.exe -V %(FullPath) -I..\framework\shaders -o %(Filename).o</Command>
<Outputs Condition="'$(Configuration)|$(Platform)'=='Release|x64'">%(Filename).o</Outputs>
</CustomBuild>
<CustomBuild Include="shaders\quad.vert">
<CustomBuild Include="shaders\quadoff.vert">
<FileType>Document</FileType>
<Command Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">$(VK_SDK_PATH)\Bin32\glslangValidator.exe -V %(FullPath) -I..\framework\shaders -o %(Filename).o</Command>
<Outputs Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">%(Filename).o</Outputs>
Expand All @@ -58,6 +58,19 @@
<Outputs Condition="'$(Configuration)|$(Platform)'=='Release|x64'">%(Filename).o</Outputs>
</CustomBuild>
</ItemGroup>
<ItemGroup>
<CustomBuild Include="shaders\quad.vert">
<FileType>Document</FileType>
<Command Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">$(VK_SDK_PATH)\Bin32\glslangValidator.exe -V %(FullPath) -I..\framework\shaders -o %(Filename).o</Command>
<Command Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">$(VK_SDK_PATH)\Bin32\glslangValidator.exe -V %(FullPath) -I..\framework\shaders -o %(Filename).o</Command>
<Command Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">$(VK_SDK_PATH)\Bin32\glslangValidator.exe -V %(FullPath) -I..\framework\shaders -o %(Filename).o</Command>
<Command Condition="'$(Configuration)|$(Platform)'=='Release|x64'">$(VK_SDK_PATH)\Bin32\glslangValidator.exe -V %(FullPath) -I..\framework\shaders -o %(Filename).o</Command>
<Outputs Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">%(Filename).o</Outputs>
<Outputs Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">%(Filename).o</Outputs>
<Outputs Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">%(Filename).o</Outputs>
<Outputs Condition="'$(Configuration)|$(Platform)'=='Release|x64'">%(Filename).o</Outputs>
</CustomBuild>
</ItemGroup>
<PropertyGroup Label="Globals">
<VCProjectVersion>15.0</VCProjectVersion>
<ProjectGuid>{709912CF-51E5-4AD2-85AE-A7D70B4C6495}</ProjectGuid>
Expand Down
3 changes: 3 additions & 0 deletions blur-gaussian/blur-gaussian.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@
<CustomBuild Include="shaders\blur.frag">
<Filter>Resource Files</Filter>
</CustomBuild>
<CustomBuild Include="shaders\quadoff.vert">
<Filter>Resource Files</Filter>
</CustomBuild>
</ItemGroup>
<ItemGroup>
<ClCompile Include="blur-gaussian.cpp">
Expand Down
23 changes: 4 additions & 19 deletions blur-gaussian/shaders/blur.frag
Original file line number Diff line number Diff line change
Expand Up @@ -2,37 +2,22 @@

#define N 15 // odd

layout(constant_id = 0) const bool c_horzPass = true;

layout(binding = 0) uniform sampler2D img;
layout(binding = 1) buffer Weights {
float weights[N + 1]; // + norm factor
};

layout(location = 0) in vec2 texCoord;
layout(location = 0) in vec4 texCoord; // u, v, du, dv
layout(location = 0) out vec3 oColor;

void main()
{
const vec2 tx = 1./textureSize(img, 0);
const int n = N >> 1;
vec2 uv = texCoord;
vec2 duv;

if (c_horzPass)
{
uv.x -= n * tx.x;
duv = vec2(tx.x, 0);
}
else
{
uv.y -= n * tx.y;
duv = vec2(0, tx.y);
}
vec2 uv = texCoord.xy;
vec2 duv = texCoord.zw;

// Gaussian filter
oColor = vec3(0.);
for (int i = 0; i < N; ++i, uv += duv)
oColor += texture(img, uv).rgb * weights[i];
oColor += textureLod(img, uv, 0).rgb * weights[i];
oColor.rgb *= weights[N]; // normalize
}
38 changes: 38 additions & 0 deletions blur-gaussian/shaders/quadoff.vert
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#version 450

#define N 15 // odd

layout(constant_id = 0) const bool c_horzPass = true;

layout(binding = 0) uniform sampler2D img;

layout(location = 0) out vec4 oTexCoord; // u, v, du, dv
out gl_PerVertex {
vec4 gl_Position;
};

void main()
{
vec2 quad[4] = vec2[](
// top
vec2(-1.,-1.), // left
vec2( 1.,-1.), // right
// bottom
vec2(-1., 1.), // left
vec2( 1., 1.) // right
);
gl_Position = vec4(quad[gl_VertexIndex], 0., 1.);
oTexCoord.xy = gl_Position.xy * .5 + .5;
oTexCoord.zw = 1./textureSize(img, 0);
const int n = N >> 1;
if (c_horzPass)
{
oTexCoord.x -= n * oTexCoord.z;
oTexCoord.w = 0.; // dv
}
else
{
oTexCoord.y -= n * oTexCoord.w;
oTexCoord.z = 0.; // du
}
}
2 changes: 1 addition & 1 deletion blur-ping-pong/blur-ping-pong.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ class PingPongBlur : public GraphicsApp
const Constants constant = {MAGMA_BOOLEAN(ping)};
const magma::SpecializationEntry entry(0, &Constants::ping);
auto specialization = std::make_shared<magma::Specialization>(constant, entry);
pass.pipeline = createFullscreenPipeline("quad.o", "bilerp.o", std::move(specialization), std::move(layout), pass.framebuffer);
pass.pipeline = createFullscreenPipeline("quadoff.o", "bilerp.o", std::move(specialization), std::move(layout), pass.framebuffer);
return pass;
}

Expand Down
13 changes: 13 additions & 0 deletions blur-ping-pong/blur-ping-pong.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,19 @@
<Outputs Condition="'$(Configuration)|$(Platform)'=='Release|x64'">%(Filename).o</Outputs>
</CustomBuild>
</ItemGroup>
<ItemGroup>
<CustomBuild Include="shaders\quadoff.vert">
<FileType>Document</FileType>
<Command Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">$(VK_SDK_PATH)\Bin32\glslangValidator.exe -V %(FullPath) -I..\framework\shaders -o %(Filename).o</Command>
<Command Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">$(VK_SDK_PATH)\Bin32\glslangValidator.exe -V %(FullPath) -I..\framework\shaders -o %(Filename).o</Command>
<Command Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">$(VK_SDK_PATH)\Bin32\glslangValidator.exe -V %(FullPath) -I..\framework\shaders -o %(Filename).o</Command>
<Command Condition="'$(Configuration)|$(Platform)'=='Release|x64'">$(VK_SDK_PATH)\Bin32\glslangValidator.exe -V %(FullPath) -I..\framework\shaders -o %(Filename).o</Command>
<Outputs Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">%(Filename).o</Outputs>
<Outputs Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">%(Filename).o</Outputs>
<Outputs Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">%(Filename).o</Outputs>
<Outputs Condition="'$(Configuration)|$(Platform)'=='Release|x64'">%(Filename).o</Outputs>
</CustomBuild>
</ItemGroup>
<PropertyGroup Label="Globals">
<VCProjectVersion>15.0</VCProjectVersion>
<ProjectGuid>{1C51CC8A-6183-43C7-A231-9066FA95587A}</ProjectGuid>
Expand Down
3 changes: 3 additions & 0 deletions blur-ping-pong/blur-ping-pong.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@
</CustomBuild>
<CustomBuild Include="shaders\quad.vert">
<Filter>Resource Files</Filter>
</CustomBuild>
<CustomBuild Include="shaders\quadoff.vert">
<Filter>Resource Files</Filter>
</CustomBuild>
</ItemGroup>
</Project>
6 changes: 0 additions & 6 deletions blur-ping-pong/shaders/quad.vert
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
#version 450

layout(constant_id = 0) const bool c_ping = true;

layout(binding = 0) uniform sampler2D img;

layout(location = 0) out vec2 oTexCoord;
out gl_PerVertex {
vec4 gl_Position;
Expand All @@ -19,8 +15,6 @@ void main()
vec2(-1., 1.), // left
vec2( 1., 1.) // right
);
const vec2 off = 0.5/textureSize(img, 0); // half texel offset
gl_Position = vec4(quad[gl_VertexIndex], 0., 1.);
oTexCoord = gl_Position.xy * .5 + .5;
oTexCoord += c_ping ? off : -off;
}
26 changes: 26 additions & 0 deletions blur-ping-pong/shaders/quadoff.vert
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#version 450

layout(constant_id = 0) const bool c_ping = true;

layout(binding = 0) uniform sampler2D img;

layout(location = 0) out vec2 oTexCoord;
out gl_PerVertex {
vec4 gl_Position;
};

void main()
{
vec2 quad[4] = vec2[](
// top
vec2(-1.,-1.), // left
vec2( 1.,-1.), // right
// bottom
vec2(-1., 1.), // left
vec2( 1., 1.) // right
);
const vec2 off = 0.5/textureSize(img, 0); // half texel offset
gl_Position = vec4(quad[gl_VertexIndex], 0., 1.);
oTexCoord = gl_Position.xy * .5 + .5;
oTexCoord += c_ping ? off : -off;
}
6 changes: 3 additions & 3 deletions framework/graphicsApp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ std::shared_ptr<magma::GraphicsPipeline> GraphicsApp::createCommonSpecializedPip
std::move(setLayout));
return std::make_shared<magma::GraphicsPipeline>(device,
std::vector<magma::PipelineShaderStage>{
loadShaderStage(vertexShaderFile),
loadShaderStage(vertexShaderFile, specialization),
loadShaderStage(fragmentShaderFile, std::move(specialization))
}, vertexInputState,
magma::renderstates::triangleList,
Expand Down Expand Up @@ -279,7 +279,7 @@ std::shared_ptr<magma::GraphicsPipeline> GraphicsApp::createFullscreenPipeline(c
pipelineLayout = std::make_shared<magma::PipelineLayout>(std::move(setLayout));
return std::make_shared<magma::GraphicsPipeline>(device,
std::vector<magma::PipelineShaderStage>{
loadShaderStage(vertexShaderFile, std::move(specialization)),
loadShaderStage(vertexShaderFile, specialization),
loadShaderStage(fragmentShaderFile, std::move(specialization))
}, magma::renderstates::nullVertexInput,
magma::renderstates::triangleStrip,
Expand All @@ -305,7 +305,7 @@ std::shared_ptr<magma::GraphicsPipeline> GraphicsApp::createFullscreenPipeline(c
pipelineLayout = std::make_shared<magma::PipelineLayout>(std::move(setLayout));
return std::make_shared<magma::GraphicsPipeline>(device,
std::vector<magma::PipelineShaderStage>{
loadShaderStage(vertexShaderFile),
loadShaderStage(vertexShaderFile, specialization),
loadShaderStage(fragmentShaderFile, std::move(specialization))
}, magma::renderstates::nullVertexInput,
magma::renderstates::triangleStrip,
Expand Down

0 comments on commit 1559930

Please sign in to comment.