From 4d018ade1283005940a69ff5f99bacf346b93857 Mon Sep 17 00:00:00 2001 From: Jack Andersen Date: Fri, 29 Jul 2016 12:22:17 -1000 Subject: [PATCH] Working CSpaceWarpFilter --- Editor/ViewManager.cpp | 3 +- Runtime/Graphics/CMakeLists.txt | 11 +++- Runtime/Graphics/Shaders/CSpaceWarpFilter.cpp | 66 +++++++++++-------- Runtime/Graphics/Shaders/CSpaceWarpFilter.hpp | 7 +- .../Graphics/Shaders/CSpaceWarpFilterGLSL.cpp | 17 +++-- .../Shaders/CSpaceWarpFilterMetal.cpp | 13 ++++ 6 files changed, 73 insertions(+), 44 deletions(-) diff --git a/Editor/ViewManager.cpp b/Editor/ViewManager.cpp index bafa36b8f..392386f27 100644 --- a/Editor/ViewManager.cpp +++ b/Editor/ViewManager.cpp @@ -92,6 +92,7 @@ void ViewManager::ParticleView::draw(boo::IGraphicsCommandQueue *gfxQ) m_vm.m_modelTest->GetInstance().ActivateLights(lights); m_vm.m_modelTest->Draw(flags); + m_spaceWarpFilter.setStrength(std::sin(m_theta * 5.f) * 0.5f + 0.5f); m_spaceWarpFilter.draw(zeus::CVector2f{0.f, 0.f}); } if (m_vm.m_partGen) @@ -361,7 +362,7 @@ void ViewManager::stop() m_videoVoice.reset(); m_projManager.shutdown(); TShader::Shutdown(); - TShader::Shutdown(); + TShader::Shutdown(); CElementGen::Shutdown(); CMoviePlayer::Shutdown(); CLineRenderer::Shutdown(); diff --git a/Runtime/Graphics/CMakeLists.txt b/Runtime/Graphics/CMakeLists.txt index ecf470598..9555a261d 100644 --- a/Runtime/Graphics/CMakeLists.txt +++ b/Runtime/Graphics/CMakeLists.txt @@ -1,7 +1,14 @@ if(WIN32) - set(PLAT_SRCS Shaders/CLineRendererShadersHLSL.cpp Shaders/CModelShadersHLSL.cpp Shaders/CThermalColdFilterHLSL.cpp) + set(PLAT_SRCS + Shaders/CLineRendererShadersHLSL.cpp + Shaders/CModelShadersHLSL.cpp + Shaders/CThermalColdFilterHLSL.cpp + Shaders/CSpaceWarpFilterHLSL.cpp) elseif(APPLE) - set(PLAT_SRCS Shaders/CLineRendererShadersMetal.cpp Shaders/CModelShadersMetal.cpp Shaders/CThermalColdFilterMetal.cpp) + set(PLAT_SRCS Shaders/CLineRendererShadersMetal.cpp + Shaders/CModelShadersMetal.cpp + Shaders/CThermalColdFilterMetal.cpp + Shaders/CSpaceWarpFilterMetal.cpp) endif() set(GRAPHICS_SOURCES diff --git a/Runtime/Graphics/Shaders/CSpaceWarpFilter.cpp b/Runtime/Graphics/Shaders/CSpaceWarpFilter.cpp index 7ee3e95f1..1157499e5 100644 --- a/Runtime/Graphics/Shaders/CSpaceWarpFilter.cpp +++ b/Runtime/Graphics/Shaders/CSpaceWarpFilter.cpp @@ -16,10 +16,14 @@ void CSpaceWarpFilter::GenerateWarpRampTex(boo::IGraphicsDataFactory::Context& c for (int x=0 ; x::BuildShaderDataBinding(ctx, *this); return true; }); - - setStrength(1.f); } void CSpaceWarpFilter::draw(const zeus::CVector2f& pt) @@ -57,71 +59,77 @@ void CSpaceWarpFilter::draw(const zeus::CVector2f& pt) /* Indirect coords are full-texture sampling when warp is completely in viewport */ m_uniform.m_indXf[1][1] = 1.f; m_uniform.m_indXf[0][0] = 1.f; - m_uniform.m_indXf[3][0] = 0.f; - m_uniform.m_indXf[3][1] = 0.f; + m_uniform.m_indXf[2][0] = 0.f; + m_uniform.m_indXf[2][1] = 0.f; /* Warp effect is fixed at 192x192 rectangle in original (1/2.5 viewport height) */ m_uniform.m_matrix[1][1] = 1.f / 2.5f; m_uniform.m_matrix[0][0] = m_uniform.m_matrix[1][1] / CGraphics::g_ProjAspect; SClipScreenRect clipRect = {}; - clipRect.x4_left = ((pt[0] - m_uniform.m_matrix[0][0] / 2.f) / 2.f + 0.5f) * CGraphics::g_ViewportResolution.x; + clipRect.x4_left = ((pt[0] - m_uniform.m_matrix[0][0]) / 2.f + 0.5f) * CGraphics::g_ViewportResolution.x; if (clipRect.x4_left >= CGraphics::g_ViewportResolution.x) return; - clipRect.x8_top = ((pt[1] - m_uniform.m_matrix[1][1] / 2.f) / 2.f + 0.5f) * CGraphics::g_ViewportResolution.y; + clipRect.x8_top = ((pt[1] - m_uniform.m_matrix[1][1]) / 2.f + 0.5f) * CGraphics::g_ViewportResolution.y; if (clipRect.x8_top >= CGraphics::g_ViewportResolution.y) return; - clipRect.xc_width = CGraphics::g_ViewportResolution.x * m_uniform.m_matrix[1][1]; + clipRect.xc_width = CGraphics::g_ViewportResolution.x * m_uniform.m_matrix[0][0]; if (clipRect.x4_left + clipRect.xc_width <= 0) return; - clipRect.x10_height = CGraphics::g_ViewportResolution.y * m_uniform.m_matrix[0][0]; + clipRect.x10_height = CGraphics::g_ViewportResolution.y * m_uniform.m_matrix[1][1]; if (clipRect.x8_top + clipRect.x10_height <= 0) return; + float oldW = clipRect.xc_width; if (clipRect.x4_left < 0) { - float old = clipRect.xc_width; clipRect.xc_width += clipRect.x4_left; - m_uniform.m_indXf[0][0] = clipRect.xc_width / old; - m_uniform.m_indXf[3][0] = -clipRect.x4_left * m_uniform.m_matrix[0][0]; + m_uniform.m_indXf[0][0] = clipRect.xc_width / oldW; + m_uniform.m_indXf[2][0] = -clipRect.x4_left / oldW; clipRect.x4_left = 0; } + + float oldH = clipRect.x10_height; if (clipRect.x8_top < 0) { - float old = clipRect.x10_height; clipRect.x10_height += clipRect.x8_top; - m_uniform.m_indXf[1][1] = clipRect.x10_height / old; - m_uniform.m_indXf[3][1] = -clipRect.x8_top * m_uniform.m_matrix[1][1]; + m_uniform.m_indXf[1][1] = clipRect.x10_height / oldH; + m_uniform.m_indXf[2][1] = -clipRect.x8_top / oldH; clipRect.x8_top = 0; } float tmp = clipRect.x4_left + clipRect.xc_width; if (tmp >= CGraphics::g_ViewportResolution.x) { - float old = clipRect.xc_width; clipRect.xc_width = CGraphics::g_ViewportResolution.x - clipRect.x4_left; - m_uniform.m_indXf[0][0] *= clipRect.xc_width / old; + m_uniform.m_indXf[0][0] = clipRect.xc_width / oldW; } + tmp = clipRect.x8_top + clipRect.x10_height; if (tmp >= CGraphics::g_ViewportResolution.y) { - float old = clipRect.x10_height; clipRect.x10_height = CGraphics::g_ViewportResolution.y - clipRect.x8_top; - m_uniform.m_indXf[1][1] *= clipRect.x10_height / old; + m_uniform.m_indXf[1][1] = clipRect.x10_height / oldH; } - CGraphics::ResolveSpareTexture(clipRect); + + SClipScreenRect clipRectDummy = {}; + clipRectDummy.xc_width = CGraphics::g_ViewportResolution.x; + clipRectDummy.x10_height = CGraphics::g_ViewportResolution.y; + CGraphics::ResolveSpareTexture(clipRectDummy); /* Transform UV coordinates of rectangle within viewport and sampled scene texels (clamped to viewport bounds) */ zeus::CVector2f vp{CGraphics::g_ViewportResolution.x, CGraphics::g_ViewportResolution.y}; - zeus::CVector2f center(clipRect.x4_left + clipRect.xc_width / 2.f, clipRect.x8_top + clipRect.x10_height / 2.f); - center /= vp; - center *= zeus::CVector2f{2.f, 2.f}; - center -= zeus::CVector2f{1.f, 1.f}; + //zeus::CVector2f center(clipRect.x4_left + clipRect.xc_width / 2.f, clipRect.x8_top + clipRect.x10_height / 2.f); + //center /= vp; + //center *= zeus::CVector2f{2.f, 2.f}; + //center -= zeus::CVector2f{1.f, 1.f}; m_uniform.m_matrix[0][0] = clipRect.xc_width / vp.x; m_uniform.m_matrix[1][1] = clipRect.x10_height / vp.y; - m_uniform.m_matrix[3][0] = center.x; - m_uniform.m_matrix[3][1] = center.y; - + m_uniform.m_matrix[2][0] = pt.x; + m_uniform.m_matrix[2][1] = pt.y; + + m_uniform.m_strength.x = m_uniform.m_matrix[0][0] * m_strength * 0.5f; + m_uniform.m_strength.y = m_uniform.m_matrix[1][1] * m_strength * 0.5f; m_uniBuf->load(&m_uniform, sizeof(m_uniform)); CGraphics::g_BooMainCommandQueue->setShaderDataBinding(m_dataBind); diff --git a/Runtime/Graphics/Shaders/CSpaceWarpFilter.hpp b/Runtime/Graphics/Shaders/CSpaceWarpFilter.hpp index ad912e13f..eaa0d7214 100644 --- a/Runtime/Graphics/Shaders/CSpaceWarpFilter.hpp +++ b/Runtime/Graphics/Shaders/CSpaceWarpFilter.hpp @@ -26,16 +26,13 @@ class CSpaceWarpFilter boo::IGraphicsBufferD* m_uniBuf; boo::IShaderDataBinding* m_dataBind = nullptr; Uniform m_uniform; + float m_strength = 1.f; void GenerateWarpRampTex(boo::IGraphicsDataFactory::Context& ctx); public: CSpaceWarpFilter(); - void setStrength(float scale) - { - m_uniform.m_strength[0] = scale; - m_uniform.m_strength[1] = scale; - } + void setStrength(float strength) { m_strength = strength; } void draw(const zeus::CVector2f& pt); using _CLS = CSpaceWarpFilter; diff --git a/Runtime/Graphics/Shaders/CSpaceWarpFilterGLSL.cpp b/Runtime/Graphics/Shaders/CSpaceWarpFilterGLSL.cpp index f95836d97..13677915d 100644 --- a/Runtime/Graphics/Shaders/CSpaceWarpFilterGLSL.cpp +++ b/Runtime/Graphics/Shaders/CSpaceWarpFilterGLSL.cpp @@ -18,18 +18,18 @@ BOO_GLSL_BINDING_HEAD "\n" "struct VertToFrag\n" "{\n" -" vec4 strength;\n" " vec2 sceneUv;\n" " vec2 indUv;\n" +" vec2 strength;\n" "};\n" "\n" "SBINDING(0) out VertToFrag vtf;\n" "void main()\n" "{\n" -" vtf.strength = strength;\n" -" vtf.sceneUv = (mainMtx * uvIn).xy;\n" -" vtf.indUv = (indMtx * uvIn).xy;\n" -" gl_Position = mainMtx * vec4(posIn.xyz, 1.0);\n" +" gl_Position = vec4(mat3(mainMtx) * vec3(posIn.xy, 1.0), 1.0);\n" +" vtf.sceneUv = gl_Position.xy * vec2(0.5) + vec2(0.5);\n" +" vtf.indUv = (mat3(indMtx) * vec3(uvIn.xy, 1.0)).xy;\n" +" vtf.strength = strength.xy;\n" "}\n"; static const char* FS = @@ -37,9 +37,9 @@ static const char* FS = BOO_GLSL_BINDING_HEAD "struct VertToFrag\n" "{\n" -" vec4 strength;\n" " vec2 sceneUv;\n" " vec2 indUv;\n" +" vec2 strength;\n" "};\n" "\n" "SBINDING(0) in VertToFrag vtf;\n" @@ -48,7 +48,10 @@ BOO_GLSL_BINDING_HEAD "TBINDING1 uniform sampler2D indTex;\n" "void main()\n" "{\n" -" colorOut = texture(sceneTex, vtf.sceneUv + mix(vtf.indUv, texture(indTex, vtf.indUv).xy, vtf.strength.xy) * vec2(2.0) - vec2(1.0));\n" +" colorOut = texture(sceneTex, vtf.sceneUv + (texture(indTex, vtf.indUv).xy * vec2(2.0) - vec2(1.0)) * vtf.strength.xy);\n" +" //colorOut *= vec4(0.00001);\n" +" //colorOut.rg = texture(indTex, vtf.indUv).xy;\n" +" //colorOut.a = 1.0;\n" "}\n"; URDE_DECL_SPECIALIZE_SHADER(CSpaceWarpFilter) diff --git a/Runtime/Graphics/Shaders/CSpaceWarpFilterMetal.cpp b/Runtime/Graphics/Shaders/CSpaceWarpFilterMetal.cpp index e69de29bb..9a4c3cdda 100644 --- a/Runtime/Graphics/Shaders/CSpaceWarpFilterMetal.cpp +++ b/Runtime/Graphics/Shaders/CSpaceWarpFilterMetal.cpp @@ -0,0 +1,13 @@ +#include "CSpaceWarpFilter.hpp" + +namespace urde +{ + +TShader::IDataBindingFactory* CSpaceWarpFilter::Initialize(boo::MetalDataFactory::Context& ctx, + boo::IShaderPipeline*& pipeOut, + boo::IVertexFormat*& vtxFmtOut) +{ + return nullptr; +} + +}