Skip to content

Commit

Permalink
gaussian_blur_5, gaussian_blur_13 を追加 (Siv3D#1148)
Browse files Browse the repository at this point in the history
  • Loading branch information
Reputeless authored Nov 19, 2023
1 parent 11d4d8f commit beb0444
Show file tree
Hide file tree
Showing 35 changed files with 816 additions and 136 deletions.
56 changes: 56 additions & 0 deletions Linux/App/resources/engine/shader/essl/gaussian_blur_13.frag
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
# version 300 es

// Copyright (c) 2008-2023 Ryo Suzuki.
// Copyright (c) 2016-2023 OpenSiv3D Project.
// Licensed under the MIT License.

precision mediump float;

//
// Textures
//
uniform sampler2D Texture0;

//
// PSInput
//
in vec4 Color;
in vec2 UV;

//
// PSOutput
//
layout(location = 0) out vec4 FragColor;

//
// Constant Buffer
//
layout(std140) uniform PSConstants2D
{
vec4 g_colorAdd;
vec4 g_sdfParam;
vec4 g_sdfOutlineColor;
vec4 g_sdfShadowColor;
vec2 g_pixelSize;
vec2 g_direction;
};

//
// Functions
//
void main()
{
vec2 offset1 = (1.41176470588235 * g_direction);
vec2 offset2 = (3.29411764705882 * g_direction);
vec2 offset3 = (5.17647058823529 * g_direction);

vec4 color = (texture(Texture0, UV) * 0.196482550151140);
color += (texture(Texture0, UV + (offset1 * g_pixelSize)) * 0.296906964672834);
color += (texture(Texture0, UV - (offset1 * g_pixelSize)) * 0.296906964672834);
color += (texture(Texture0, UV + (offset2 * g_pixelSize)) * 0.0944703978504473);
color += (texture(Texture0, UV - (offset2 * g_pixelSize)) * 0.0944703978504473);
color += (texture(Texture0, UV + (offset3 * g_pixelSize)) * 0.0103813624011481);
color += (texture(Texture0, UV - (offset3 * g_pixelSize)) * 0.0103813624011481);

FragColor = color;
}
50 changes: 50 additions & 0 deletions Linux/App/resources/engine/shader/essl/gaussian_blur_5.frag
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
# version 300 es

// Copyright (c) 2008-2023 Ryo Suzuki.
// Copyright (c) 2016-2023 OpenSiv3D Project.
// Licensed under the MIT License.

precision mediump float;

//
// Textures
//
uniform sampler2D Texture0;

//
// PSInput
//
in vec4 Color;
in vec2 UV;

//
// PSOutput
//
layout(location = 0) out vec4 FragColor;

//
// Constant Buffer
//
layout(std140) uniform PSConstants2D
{
vec4 g_colorAdd;
vec4 g_sdfParam;
vec4 g_sdfOutlineColor;
vec4 g_sdfShadowColor;
vec2 g_pixelSize;
vec2 g_direction;
};

//
// Functions
//
void main()
{
vec2 offset1 = (1.33333333333333 * g_direction);

vec4 color = (texture(Texture0, UV) * 0.294117647058824);
color += (texture(Texture0, UV + (offset1 * g_pixelSize)) * 0.352941176470588);
color += (texture(Texture0, UV - (offset1 * g_pixelSize)) * 0.352941176470588);

FragColor = color;
}
19 changes: 8 additions & 11 deletions Linux/App/resources/engine/shader/essl/gaussian_blur_9.frag
Original file line number Diff line number Diff line change
Expand Up @@ -40,17 +40,14 @@ layout(std140) uniform PSConstants2D
//
void main()
{
//
// http://rastergrid.com/blog/2010/09/efficient-gaussian-blur-with-linear-sampling/
//
vec2 offset1 = 1.38461538461538 * g_direction;
vec2 offset2 = 3.23076923076923 * g_direction;

vec4 color = texture(Texture0, UV) * 0.227027027027027;
color += texture(Texture0, UV + (offset1 * g_pixelSize)) * 0.316216216216216;
color += texture(Texture0, UV - (offset1 * g_pixelSize)) * 0.316216216216216;
color += texture(Texture0, UV + (offset2 * g_pixelSize)) * 0.070270270270270;
color += texture(Texture0, UV - (offset2 * g_pixelSize)) * 0.070270270270270;
vec2 offset1 = (1.38461538461538 * g_direction);
vec2 offset2 = (3.23076923076923 * g_direction);

vec4 color = (texture(Texture0, UV) * 0.227027027027027);
color += (texture(Texture0, UV + (offset1 * g_pixelSize)) * 0.316216216216216);
color += (texture(Texture0, UV - (offset1 * g_pixelSize)) * 0.316216216216216);
color += (texture(Texture0, UV + (offset2 * g_pixelSize)) * 0.070270270270270);
color += (texture(Texture0, UV - (offset2 * g_pixelSize)) * 0.070270270270270);

FragColor = color;
}
54 changes: 54 additions & 0 deletions Linux/App/resources/engine/shader/glsl/gaussian_blur_13.frag
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
// Copyright (c) 2008-2023 Ryo Suzuki.
// Copyright (c) 2016-2023 OpenSiv3D Project.
// Licensed under the MIT License.

# version 410

//
// Textures
//
uniform sampler2D Texture0;

//
// PSInput
//
layout(location = 0) in vec4 Color;
layout(location = 1) in vec2 UV;

//
// PSOutput
//
layout(location = 0) out vec4 FragColor;

//
// Constant Buffer
//
layout(std140) uniform PSConstants2D
{
vec4 g_colorAdd;
vec4 g_sdfParam;
vec4 g_sdfOutlineColor;
vec4 g_sdfShadowColor;
vec2 g_pixelSize;
vec2 g_direction;
};

//
// Functions
//
void main()
{
vec2 offset1 = (1.41176470588235 * g_direction);
vec2 offset2 = (3.29411764705882 * g_direction);
vec2 offset3 = (5.17647058823529 * g_direction);

vec4 color = (texture(Texture0, UV) * 0.196482550151140);
color += (texture(Texture0, UV + (offset1 * g_pixelSize)) * 0.296906964672834);
color += (texture(Texture0, UV - (offset1 * g_pixelSize)) * 0.296906964672834);
color += (texture(Texture0, UV + (offset2 * g_pixelSize)) * 0.0944703978504473);
color += (texture(Texture0, UV - (offset2 * g_pixelSize)) * 0.0944703978504473);
color += (texture(Texture0, UV + (offset3 * g_pixelSize)) * 0.0103813624011481);
color += (texture(Texture0, UV - (offset3 * g_pixelSize)) * 0.0103813624011481);

FragColor = color;
}
48 changes: 48 additions & 0 deletions Linux/App/resources/engine/shader/glsl/gaussian_blur_5.frag
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// Copyright (c) 2008-2023 Ryo Suzuki.
// Copyright (c) 2016-2023 OpenSiv3D Project.
// Licensed under the MIT License.

# version 410

//
// Textures
//
uniform sampler2D Texture0;

//
// PSInput
//
layout(location = 0) in vec4 Color;
layout(location = 1) in vec2 UV;

//
// PSOutput
//
layout(location = 0) out vec4 FragColor;

//
// Constant Buffer
//
layout(std140) uniform PSConstants2D
{
vec4 g_colorAdd;
vec4 g_sdfParam;
vec4 g_sdfOutlineColor;
vec4 g_sdfShadowColor;
vec2 g_pixelSize;
vec2 g_direction;
};

//
// Functions
//
void main()
{
vec2 offset1 = (1.33333333333333 * g_direction);

vec4 color = (texture(Texture0, UV) * 0.294117647058824);
color += (texture(Texture0, UV + (offset1 * g_pixelSize)) * 0.352941176470588);
color += (texture(Texture0, UV - (offset1 * g_pixelSize)) * 0.352941176470588);

FragColor = color;
}
19 changes: 8 additions & 11 deletions Linux/App/resources/engine/shader/glsl/gaussian_blur_9.frag
Original file line number Diff line number Diff line change
Expand Up @@ -38,17 +38,14 @@ layout(std140) uniform PSConstants2D
//
void main()
{
//
// http://rastergrid.com/blog/2010/09/efficient-gaussian-blur-with-linear-sampling/
//
vec2 offset1 = 1.38461538461538 * g_direction;
vec2 offset2 = 3.23076923076923 * g_direction;

vec4 color = texture(Texture0, UV) * 0.227027027027027;
color += texture(Texture0, UV + (offset1 * g_pixelSize)) * 0.316216216216216;
color += texture(Texture0, UV - (offset1 * g_pixelSize)) * 0.316216216216216;
color += texture(Texture0, UV + (offset2 * g_pixelSize)) * 0.070270270270270;
color += texture(Texture0, UV - (offset2 * g_pixelSize)) * 0.070270270270270;
vec2 offset1 = (1.38461538461538 * g_direction);
vec2 offset2 = (3.23076923076923 * g_direction);

vec4 color = (texture(Texture0, UV) * 0.227027027027027);
color += (texture(Texture0, UV + (offset1 * g_pixelSize)) * 0.316216216216216);
color += (texture(Texture0, UV - (offset1 * g_pixelSize)) * 0.316216216216216);
color += (texture(Texture0, UV + (offset2 * g_pixelSize)) * 0.070270270270270);
color += (texture(Texture0, UV - (offset2 * g_pixelSize)) * 0.070270270270270);

FragColor = color;
}
3 changes: 3 additions & 0 deletions Siv3D/include/Siv3D.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -1125,6 +1125,9 @@
// エッジ保持フィルタ | Edge preserving filter
# include <Siv3D/EdgePreservingFilterType.hpp>

// ボックスフィルタ | Box filter
# include <Siv3D/BoxFilterSize.hpp>

// 画像 | Image
# include <Siv3D/Image.hpp>

Expand Down
24 changes: 24 additions & 0 deletions Siv3D/include/Siv3D/BoxFilterSize.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
//-----------------------------------------------
//
// This file is part of the Siv3D Engine.
//
// Copyright (c) 2008-2023 Ryo Suzuki
// Copyright (c) 2016-2023 OpenSiv3D Project
//
// Licensed under the MIT License.
//
//-----------------------------------------------

# pragma once

namespace s3d
{
enum class BoxFilterSize
{
BoxFilter5x5 = 5,

BoxFilter9x9 = 9,

BoxFilter13x13 = 13,
};
}
10 changes: 6 additions & 4 deletions Siv3D/include/Siv3D/Shader.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
# include "Graphics2D.hpp"
# include "2DShapes.hpp"
# include "TextureFilter.hpp"
# include "BoxFilterSize.hpp"

namespace s3d
{
Expand All @@ -35,18 +36,19 @@ namespace s3d
/// @remark from と to は異なるテクスチャである必要があります。
void Downsample(const TextureRegion& from, const RenderTexture& to);

void GaussianBlurH(const TextureRegion& from, const RenderTexture& to);
void GaussianBlurH(const TextureRegion& from, const RenderTexture& to, BoxFilterSize boxFilterSize = BoxFilterSize::BoxFilter9x9);

void GaussianBlurV(const TextureRegion& from, const RenderTexture& to);
void GaussianBlurV(const TextureRegion& from, const RenderTexture& to, BoxFilterSize boxFilterSize = BoxFilterSize::BoxFilter9x9);

void GaussianBlur(const TextureRegion& from, const RenderTexture& to, const Vec2& direction);
void GaussianBlur(const TextureRegion& from, const RenderTexture& to, const Vec2& direction, BoxFilterSize boxFilterSize = BoxFilterSize::BoxFilter9x9);

/// @brief テクスチャの内容をガウスぼかしして別のレンダーテクスチャに描画します。
/// @param from 元のテクスチャ
/// @param internalBuffer 中間状態を格納するレンダーテクスチャ
/// @param to 描画先のレンダーテクスチャ
/// @param boxFilterSize ボックスフィルタのサイズ
/// @remark internalBuffer は from や to と同サイズで異なるテクスチャである必要があります。
void GaussianBlur(const TextureRegion& from, const RenderTexture& internalBuffer, const RenderTexture& to);
void GaussianBlur(const TextureRegion& from, const RenderTexture& internalBuffer, const RenderTexture& to, BoxFilterSize boxFilterSize = BoxFilterSize::BoxFilter9x9);

/// @brief 3D シーンを描画したリニア色空間のレンダーテクスチャを、メインのシーンに転送します。
/// @param src 転送するテクスチャ
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,9 @@ namespace s3d
// エンジン PS をロード
{
m_enginePSs << GLSL{ Resource(U"engine/shader/glsl/copy.frag"), {} };
m_enginePSs << GLSL{ Resource(U"engine/shader/glsl/gaussian_blur_5.frag"), {{ U"PSConstants2D", 0 }} };
m_enginePSs << GLSL{ Resource(U"engine/shader/glsl/gaussian_blur_9.frag"), {{ U"PSConstants2D", 0 }} };
m_enginePSs << GLSL{ Resource(U"engine/shader/glsl/gaussian_blur_13.frag"), {{ U"PSConstants2D", 0 }} };
m_enginePSs << GLSL{ Resource(U"engine/shader/glsl/apply_srgb_curve.frag"), {} };

if (not m_enginePSs.all([](const auto& ps) { return !!ps; })) // もしロードに失敗したシェーダがあれば
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,9 @@ namespace s3d
// エンジン PS をロード
{
m_enginePSs << ESSL{ Resource(U"engine/shader/essl/copy.frag"), {{ U"PSConstants2D", 0 }} };
m_enginePSs << ESSL{ Resource(U"engine/shader/essl/gaussian_blur_5.frag"), {{ U"PSConstants2D", 0 }} };
m_enginePSs << ESSL{ Resource(U"engine/shader/essl/gaussian_blur_9.frag"), {{ U"PSConstants2D", 0 }} };
m_enginePSs << ESSL{ Resource(U"engine/shader/essl/gaussian_blur_13.frag"), {{ U"PSConstants2D", 0 }} };
m_enginePSs << ESSL{ Resource(U"engine/shader/essl/apply_srgb_curve.frag"), {} };

if (not m_enginePSs.all([](const auto& ps) { return !!ps; })) // もしロードに失敗したシェーダがあれば
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,9 @@ namespace s3d
// エンジン PS をロード
{
m_enginePSs << WGSL{ Resource(U"engine/shader/wgsl/copy.frag.wgsl"), {{ U"PSConstants2D", 0 }} };
m_enginePSs << WGSL{ Resource(U"engine/shader/wgsl/gaussian_blur_5.frag.wgsl"), {{ U"PSConstants2D", 0 }} };
m_enginePSs << WGSL{ Resource(U"engine/shader/wgsl/gaussian_blur_9.frag.wgsl"), {{ U"PSConstants2D", 0 }} };
m_enginePSs << WGSL{ Resource(U"engine/shader/wgsl/gaussian_blur_13.frag.wgsl"), {{ U"PSConstants2D", 0 }} };
m_enginePSs << WGSL{ Resource(U"engine/shader/wgsl/apply_srgb_curve.frag.wgsl"), {{ U"PSConstants2D", 0 }} };

if (not m_enginePSs.all([](const auto& ps) { return !!ps; })) // もしロードに失敗したシェーダがあれば
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -150,9 +150,15 @@ namespace s3d
//compileHLSLFromFile(U"engine/shader/d3d11/copy.hlsl", ShaderStage::Pixel, U"PS")
// .save(U"engine/shader/d3d11/copy.ps");

//compileHLSLFromFile(U"engine/shader/d3d11/gaussian_blur_9.hlsl", ShaderStage::Pixel, U"PS")
//compileHLSLFromFile(U"engine/shader/d3d11/gaussian_blur.hlsl", ShaderStage::Pixel, U"Blur5_PS")
// .save(U"engine/shader/d3d11/gaussian_blur_5.ps");

//compileHLSLFromFile(U"engine/shader/d3d11/gaussian_blur.hlsl", ShaderStage::Pixel, U"Blur9_PS")
// .save(U"engine/shader/d3d11/gaussian_blur_9.ps");

//compileHLSLFromFile(U"engine/shader/d3d11/gaussian_blur.hlsl", ShaderStage::Pixel, U"Blur13_PS")
// .save(U"engine/shader/d3d11/gaussian_blur_13.ps");

//compileHLSLFromFile(U"engine/shader/d3d11/apply_srgb_curve.hlsl", ShaderStage::Pixel, U"PS")
// .save(U"engine/shader/d3d11/apply_srgb_curve.ps");

Expand Down Expand Up @@ -195,7 +201,9 @@ namespace s3d
// エンジン PS をロード
{
m_enginePSs << HLSL{ Resource(U"engine/shader/d3d11/copy.ps") };
m_enginePSs << HLSL{ Resource(U"engine/shader/d3d11/gaussian_blur_5.ps") };
m_enginePSs << HLSL{ Resource(U"engine/shader/d3d11/gaussian_blur_9.ps") };
m_enginePSs << HLSL{ Resource(U"engine/shader/d3d11/gaussian_blur_13.ps") };
m_enginePSs << HLSL{ Resource(U"engine/shader/d3d11/apply_srgb_curve.ps") };

if (not m_enginePSs.all([](const auto& ps) { return !!ps; })) // もしロードに失敗したシェーダがあれば
Expand Down
Loading

0 comments on commit beb0444

Please sign in to comment.