From d11db8ef2b9d4664f0e1821e5ab793d254eb203d Mon Sep 17 00:00:00 2001 From: Fabien Spindler Date: Fri, 27 Sep 2024 13:04:40 +0200 Subject: [PATCH] Refactor shaders Fix warning: missing terminating " character [enabled by default] --- .../include/visp3/ar/vpPanda3DCommonFilters.h | 6 +- .../vpPanda3DCommonFilters.cpp | 193 ++++++++---------- 2 files changed, 87 insertions(+), 112 deletions(-) diff --git a/modules/ar/include/visp3/ar/vpPanda3DCommonFilters.h b/modules/ar/include/visp3/ar/vpPanda3DCommonFilters.h index 9b2dafda5a..8bee8df17b 100644 --- a/modules/ar/include/visp3/ar/vpPanda3DCommonFilters.h +++ b/modules/ar/include/visp3/ar/vpPanda3DCommonFilters.h @@ -53,7 +53,7 @@ class VISP_EXPORT vpPanda3DLuminanceFilter : public vpPanda3DPostProcessFilter void getRender(vpImage &I) const; private: - static const char *FRAGMENT_SHADER; + static const std::string FRAGMENT_SHADER; }; /** @@ -71,7 +71,7 @@ class VISP_EXPORT vpPanda3DGaussianBlur : public vpPanda3DPostProcessFilter void getRender(vpImage &I) const; private: - static const char *FRAGMENT_SHADER; + static const std::string FRAGMENT_SHADER; }; /** @@ -96,7 +96,7 @@ class VISP_EXPORT vpPanda3DCanny : public vpPanda3DPostProcessFilter void setupScene() VP_OVERRIDE; private: - static const char *FRAGMENT_SHADER; + static const std::string FRAGMENT_SHADER; float m_edgeThreshold; }; diff --git a/modules/ar/src/panda3d-simulator/vpPanda3DCommonFilters.cpp b/modules/ar/src/panda3d-simulator/vpPanda3DCommonFilters.cpp index 232a2302eb..381ba22121 100644 --- a/modules/ar/src/panda3d-simulator/vpPanda3DCommonFilters.cpp +++ b/modules/ar/src/panda3d-simulator/vpPanda3DCommonFilters.cpp @@ -34,20 +34,15 @@ #if defined(VISP_HAVE_PANDA3D) BEGIN_VISP_NAMESPACE -const char *vpPanda3DLuminanceFilter::FRAGMENT_SHADER = R"shader( -#version 330 - -in vec2 texcoords; - -uniform sampler2D p3d_Texture0; - -out vec4 p3d_FragData; - -void main() { - vec4 v = texture(p3d_Texture0, texcoords); - p3d_FragData.b = 0.299 * v.r + 0.587 * v.g + 0.114 * v.b; -} -)shader"; +const std::string vpPanda3DLuminanceFilter::FRAGMENT_SHADER = +"#version 330\n" +"in vec2 texcoords;\n" +"uniform sampler2D p3d_Texture0;\n" +"out vec4 p3d_FragData;\n" +"void main() {\n" +" vec4 v = texture(p3d_Texture0, texcoords);\n" +" p3d_FragData.b = 0.299 * v.r + 0.587 * v.g + 0.114 * v.b;\n" +"}\n"; vpPanda3DLuminanceFilter::vpPanda3DLuminanceFilter(const std::string &name, std::shared_ptr inputRenderer, bool isOutput) : vpPanda3DPostProcessFilter(name, inputRenderer, isOutput, std::string(vpPanda3DLuminanceFilter::FRAGMENT_SHADER)) @@ -65,42 +60,34 @@ void vpPanda3DLuminanceFilter::getRender(vpImage &I) const vpPanda3DPostProcessFilter::getRenderBasic(I); } -const char *vpPanda3DGaussianBlur::FRAGMENT_SHADER = R"shader( -#version 330 - -in vec2 texcoords; - -uniform sampler2D p3d_Texture0; -uniform vec2 dp; // 1 divided by number of pixels - -const float kernel[25] = float[25]( - 2, 4, 5, 4, 2, - 4, 9, 12, 9, 4, - 5, 12, 15, 12, 5, - 4, 9, 12, 9, 4, - 2, 4, 5, 4, 2 -); -const float normalize = 1 / 159.0; - -vec2 offset[25] = vec2[25]( - vec2(-2*dp.x,-2*dp.y), vec2(-dp.x,-2*dp.y), vec2(0,-2*dp.y), vec2(dp.x,-2*dp.y), vec2(2*dp.x,-2*dp.y), - vec2(-2*dp.x,-dp.y), vec2(-dp.x, -dp.y), vec2(0.0, -dp.y), vec2(dp.x, -dp.y), vec2(2*dp.x,-dp.y), - vec2(-2*dp.x,0.0), vec2(-dp.x, 0.0), vec2(0.0, 0.0), vec2(dp.x, 0.0), vec2(2*dp.x,0.0), - vec2(-2*dp.x, dp.y), vec2(-dp.x, dp.y), vec2(0.0, dp.y), vec2(dp.x, dp.y), vec2(2*dp.x, dp.y), - vec2(-2*dp.x, 2*dp.y), vec2(-dp.x, 2*dp.y), vec2(0.0, 2*dp.y), vec2(dp.x, 2*dp.y), vec2(2*dp.x, 2*dp.y) -); - -out vec4 p3d_FragData; - -void main() { - float v = 0.f; - - for(int i = 0; i < 25; ++i) { - v += kernel[i] * texture(p3d_Texture0, texcoords + offset[i]).b ; - } - p3d_FragData.b = v * normalize; -} -)shader"; +const std::string vpPanda3DGaussianBlur::FRAGMENT_SHADER = +"#version 330\n" +"in vec2 texcoords;\n" +"uniform sampler2D p3d_Texture0;\n" +"uniform vec2 dp; // 1 divided by number of pixels\n" +"const float kernel[25] = float[25](\n" +" 2, 4, 5, 4, 2,\n" +" 4, 9, 12, 9, 4,\n" +" 5, 12, 15, 12, 5,\n" +" 4, 9, 12, 9, 4,\n" +" 2, 4, 5, 4, 2\n" +");\n" +"const float normalize = 1 / 159.0;\n" +"vec2 offset[25] = vec2[25](\n" +" vec2(-2*dp.x,-2*dp.y), vec2(-dp.x,-2*dp.y), vec2(0,-2*dp.y), vec2(dp.x,-2*dp.y), vec2(2*dp.x,-2*dp.y),\n" +" vec2(-2*dp.x,-dp.y), vec2(-dp.x, -dp.y), vec2(0.0, -dp.y), vec2(dp.x, -dp.y), vec2(2*dp.x,-dp.y),\n" +" vec2(-2*dp.x,0.0), vec2(-dp.x, 0.0), vec2(0.0, 0.0), vec2(dp.x, 0.0), vec2(2*dp.x,0.0),\n" +" vec2(-2*dp.x, dp.y), vec2(-dp.x, dp.y), vec2(0.0, dp.y), vec2(dp.x, dp.y), vec2(2*dp.x, dp.y),\n" +" vec2(-2*dp.x, 2*dp.y), vec2(-dp.x, 2*dp.y), vec2(0.0, 2*dp.y), vec2(dp.x, 2*dp.y), vec2(2*dp.x, 2*dp.y)\n" +");\n" +"out vec4 p3d_FragData;\n" +"void main() {\n" +" float v = 0.f;\n" +" for(int i = 0; i < 25; ++i) {\n" +" v += kernel[i] * texture(p3d_Texture0, texcoords + offset[i]).b;\n" +" }\n" +" p3d_FragData.b = v * normalize;\n" +"}\n"; vpPanda3DGaussianBlur::vpPanda3DGaussianBlur(const std::string &name, std::shared_ptr inputRenderer, bool isOutput) : vpPanda3DPostProcessFilter(name, inputRenderer, isOutput, vpPanda3DGaussianBlur::FRAGMENT_SHADER) @@ -120,64 +107,53 @@ void vpPanda3DGaussianBlur::getRender(vpImage &I) const vpPanda3DPostProcessFilter::getRenderBasic(I); } -const char *vpPanda3DCanny::FRAGMENT_SHADER = R"shader( -#version 330 - -in vec2 texcoords; - -uniform sampler2D p3d_Texture0; -uniform vec2 dp; // 1 divided by number of pixels -uniform float edgeThreshold; - -const float kernel[9] = float[9]( - 0.0, 1.0, 0.0, - 1.0,-4.0, 1.0, - 0.0, 1.0, 0.0 -); - -const float kernel_h[9] = float[9]( - -1.0, 0.0, 1.0, - -2.0, 0.0, 2.0, - -1.0, 0.0, 1.0 -); - -const float kernel_v[9] = float[9]( - -1.0, -2.0, -1.0, - 0.0, 0.0, 0.0, - 1.0, 2.0, 1.0 -); - -vec2 offset[9] = vec2[9]( - vec2(-dp.x, -dp.y), vec2(0.0, -dp.y), vec2(dp.x, -dp.y), - vec2(-dp.x, 0.0), vec2(0.0, 0.0), vec2(dp.x, 0.0), - vec2(-dp.x, dp.y), vec2(0.0, dp.y), vec2(dp.x, dp.y) -); - - -out vec4 p3d_FragData; - -void main() { - float sum = 0.f; - for(int i = 0; i < 9; ++i) { - float pix = texture(p3d_Texture0, texcoords + offset[i]).b; - sum += pix * kernel[i]; - } - if(abs(sum * 255.f) > edgeThreshold) { - float sum_h = 0.f; - float sum_v = 0.f; - for(int i = 0; i < 9; ++i) { - float pix = texture(p3d_Texture0, texcoords + offset[i]).b; - sum_h += pix * kernel_h[i]; - sum_v += pix * kernel_v[i]; - } - - vec2 orientationAndValid = sum_h * sum_h + sum_v * sum_v > 0 ? vec2(-atan(sum_v/sum_h), 1.f) : vec2(0.f, 0.f); - p3d_FragData = vec4(sum_h, sum_v, orientationAndValid.x, orientationAndValid.y); - } else { - p3d_FragData = vec4(0.f, 0.f, 0.f, 0.f); - } -} -)shader"; +const std::string vpPanda3DCanny::FRAGMENT_SHADER = +"#version 330\n" +"in vec2 texcoords;\n" +"uniform sampler2D p3d_Texture0;\n" +"uniform vec2 dp; // 1 divided by number of pixels\n" +"uniform float edgeThreshold;\n" +"const float kernel[9] = float[9](\n" +" 0.0, 1.0, 0.0,\n" +" 1.0,-4.0, 1.0,\n" +" 0.0, 1.0, 0.0\n" +");\n" +"const float kernel_h[9] = float[9](\n" +" -1.0, 0.0, 1.0,\n" +" -2.0, 0.0, 2.0,\n" +" -1.0, 0.0, 1.0\n" +");\n" +"const float kernel_v[9] = float[9](\n" +" -1.0, -2.0, -1.0,\n" +" 0.0, 0.0, 0.0,\n" +" 1.0, 2.0, 1.0\n" +");\n" +"vec2 offset[9] = vec2[9](\n" +" vec2(-dp.x, -dp.y), vec2(0.0, -dp.y), vec2(dp.x, -dp.y),\n" +" vec2(-dp.x, 0.0), vec2(0.0, 0.0), vec2(dp.x, 0.0),\n" +" vec2(-dp.x, dp.y), vec2(0.0, dp.y), vec2(dp.x, dp.y)\n" +");\n" +"out vec4 p3d_FragData;\n" +"void main() {\n" +" float sum = 0.f;\n" +" for(int i = 0; i < 9; ++i) {\n" +" float pix = texture(p3d_Texture0, texcoords + offset[i]).b;\n" +" sum += pix * kernel[i];\n" +" }\n" +" if(abs(sum * 255.f) > edgeThreshold) {\n" +" float sum_h = 0.f;\n" +" float sum_v = 0.f;\n" +" for(int i = 0; i < 9; ++i) {\n" +" float pix = texture(p3d_Texture0, texcoords + offset[i]).b;\n" +" sum_h += pix * kernel_h[i];\n" +" sum_v += pix * kernel_v[i];\n" +" }\n" +" vec2 orientationAndValid = sum_h * sum_h + sum_v * sum_v > 0 ? vec2(-atan(sum_v/sum_h), 1.f) : vec2(0.f, 0.f);\n" +" p3d_FragData = vec4(sum_h, sum_v, orientationAndValid.x, orientationAndValid.y);\n" +" } else {\n" +" p3d_FragData = vec4(0.f, 0.f, 0.f, 0.f);\n" +" }\n" +"}\n"; vpPanda3DCanny::vpPanda3DCanny(const std::string &name, std::shared_ptr inputRenderer, bool isOutput, float edgeThreshold) : vpPanda3DPostProcessFilter(name, inputRenderer, isOutput, vpPanda3DCanny::FRAGMENT_SHADER), m_edgeThreshold(edgeThreshold) @@ -195,7 +171,6 @@ void vpPanda3DCanny::setEdgeThreshold(float edgeThreshold) m_renderRoot.set_shader_input("edgeThreshold", LVector2f(m_edgeThreshold)); } - FrameBufferProperties vpPanda3DCanny::getBufferProperties() const { FrameBufferProperties fbp;