-
-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Render/SobelFilterRenderProcess] Added a Sobel filter/operator proc.
- This computes the gradient & gradient direction of an image's pixels - Added a dedicated unit test & render result images
- Loading branch information
Showing
9 changed files
with
194 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
#pragma once | ||
|
||
#ifndef RAZ_SOBELFILTERRENDERPROCESS_HPP | ||
#define RAZ_SOBELFILTERRENDERPROCESS_HPP | ||
|
||
#include "RaZ/Render/MonoPassRenderProcess.hpp" | ||
|
||
namespace Raz { | ||
|
||
/// [Sobel filter](https://en.wikipedia.org/wiki/Sobel_operator) render process. | ||
class SobelFilterRenderProcess final : public MonoPassRenderProcess { | ||
public: | ||
explicit SobelFilterRenderProcess(RenderGraph& renderGraph); | ||
|
||
void resizeBuffers(unsigned int width, unsigned int height) override; | ||
void setInputBuffer(Texture2DPtr colorBuffer); | ||
/// Sets the output buffer which will contain the gradient's value. | ||
/// \param gradientBuffer Gradient's values buffer. | ||
void setOutputGradientBuffer(Texture2DPtr gradientBuffer); | ||
/// Sets the output buffer which will contain the gradient's direction. | ||
/// | ||
/// /--0.75--\ | ||
/// / \ | ||
/// / \ | ||
/// 0.5 0/1 | ||
/// \ / | ||
/// \ / | ||
/// \--0.25--/ | ||
/// | ||
/// \note The direction's values are just like those of [atan2](https://en.wikipedia.org/wiki/Atan2) (see image below), but remapped between [0; 1]. | ||
/// \imageSize{https://upload.cppreference.com/mwiki/images/9/91/math-atan2.png, height: 20%; width: 20%;} | ||
/// \image html https://upload.cppreference.com/mwiki/images/9/91/math-atan2.png | ||
/// \param gradDirBuffer Gradient's direction buffer. | ||
void setOutputGradientDirectionBuffer(Texture2DPtr gradDirBuffer); | ||
}; | ||
|
||
} // namespace Raz | ||
|
||
#endif // RAZ_SOBELFILTERRENDERPROCESS_HPP |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
#define PI 3.1415926535897932384626433832795 | ||
|
||
in vec2 fragTexcoords; | ||
|
||
uniform sampler2D uniBuffer; | ||
uniform vec2 uniInvBufferSize; | ||
|
||
layout(location = 0) out vec4 fragGradient; | ||
layout(location = 1) out vec4 fragGradDir; | ||
|
||
const float[9] horizKernel = float[]( | ||
1.0, 0.0, -1.0, | ||
2.0, 0.0, -2.0, | ||
1.0, 0.0, -1.0 | ||
); | ||
|
||
const float[9] vertKernel = float[]( | ||
1.0, 2.0, 1.0, | ||
0.0, 0.0, 0.0, | ||
-1.0, -2.0, -1.0 | ||
); | ||
|
||
const vec2[9] offsets = vec2[]( | ||
vec2(-1.0, 1.0), vec2(0.0, 1.0), vec2(1.0, 1.0), | ||
vec2(-1.0, 0.0), vec2(0.0, 0.0), vec2(1.0, 0.0), | ||
vec2(-1.0, -1.0), vec2(0.0, -1.0), vec2(1.0, -1.0) | ||
); | ||
|
||
const float invTau = 1.0 / (2.0 * PI); | ||
|
||
void main() { | ||
vec3 horizVal = vec3(0.0); | ||
vec3 vertVal = vec3(0.0); | ||
|
||
for (int i = 0; i < 9; ++i) { | ||
vec2 offset = offsets[i] * uniInvBufferSize; | ||
vec3 pixel = texture(uniBuffer, fragTexcoords + offset).rgb; | ||
|
||
horizVal += pixel * horizKernel[i]; | ||
vertVal += pixel * vertKernel[i]; | ||
} | ||
|
||
vec3 gradient = sqrt(horizVal * horizVal + vertVal * vertVal); | ||
vec3 gradDir = atan(vertVal, horizVal); | ||
gradDir = (gradDir + PI) * invTau; // Remapping from [-Pi; Pi] to [0; 1] | ||
|
||
fragGradient = vec4(gradient, 1.0); | ||
fragGradDir = vec4(gradDir, 1.0); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
#include "RaZ/Render/SobelFilterRenderProcess.hpp" | ||
#include "RaZ/Render/RenderPass.hpp" | ||
|
||
#include <string_view> | ||
|
||
namespace Raz { | ||
|
||
namespace { | ||
|
||
constexpr std::string_view sobelSource = { | ||
#include "sobel_filter.frag.embed" | ||
}; | ||
|
||
} // namespace | ||
|
||
SobelFilterRenderProcess::SobelFilterRenderProcess(RenderGraph& renderGraph) | ||
: MonoPassRenderProcess(renderGraph, FragmentShader::loadFromSource(sobelSource), "Sobel filter") {} | ||
|
||
void SobelFilterRenderProcess::resizeBuffers(unsigned int width, unsigned int height) { | ||
const Vec2f invBufferSize(1.f / static_cast<float>(width), 1.f / static_cast<float>(height)); | ||
m_pass.getProgram().setAttribute(invBufferSize, "uniInvBufferSize"); | ||
m_pass.getProgram().sendAttributes(); | ||
} | ||
|
||
void SobelFilterRenderProcess::setInputBuffer(Texture2DPtr colorBuffer) { | ||
resizeBuffers(colorBuffer->getWidth(), colorBuffer->getHeight()); | ||
MonoPassRenderProcess::setInputBuffer(std::move(colorBuffer), "uniBuffer"); | ||
} | ||
|
||
void SobelFilterRenderProcess::setOutputGradientBuffer(Texture2DPtr colorBuffer) { | ||
MonoPassRenderProcess::setOutputBuffer(std::move(colorBuffer), 0); | ||
} | ||
|
||
void SobelFilterRenderProcess::setOutputGradientDirectionBuffer(Texture2DPtr colorBuffer) { | ||
MonoPassRenderProcess::setOutputBuffer(std::move(colorBuffer), 1); | ||
} | ||
|
||
} // namespace Raz |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters