Working CSpaceWarpFilter

This commit is contained in:
Jack Andersen 2016-07-29 12:22:17 -10:00
parent 67801e993a
commit 4d018ade12
6 changed files with 73 additions and 44 deletions

View File

@ -92,6 +92,7 @@ void ViewManager::ParticleView::draw(boo::IGraphicsCommandQueue *gfxQ)
m_vm.m_modelTest->GetInstance().ActivateLights(lights); m_vm.m_modelTest->GetInstance().ActivateLights(lights);
m_vm.m_modelTest->Draw(flags); 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}); m_spaceWarpFilter.draw(zeus::CVector2f{0.f, 0.f});
} }
if (m_vm.m_partGen) if (m_vm.m_partGen)
@ -361,7 +362,7 @@ void ViewManager::stop()
m_videoVoice.reset(); m_videoVoice.reset();
m_projManager.shutdown(); m_projManager.shutdown();
TShader<CThermalColdFilter>::Shutdown(); TShader<CThermalColdFilter>::Shutdown();
TShader<CThermalColdFilter>::Shutdown(); TShader<CSpaceWarpFilter>::Shutdown();
CElementGen::Shutdown(); CElementGen::Shutdown();
CMoviePlayer::Shutdown(); CMoviePlayer::Shutdown();
CLineRenderer::Shutdown(); CLineRenderer::Shutdown();

View File

@ -1,7 +1,14 @@
if(WIN32) 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) 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() endif()
set(GRAPHICS_SOURCES set(GRAPHICS_SOURCES

View File

@ -16,10 +16,14 @@ void CSpaceWarpFilter::GenerateWarpRampTex(boo::IGraphicsDataFactory::Context& c
for (int x=0 ; x<WARP_RAMP_RES ; ++x) for (int x=0 ; x<WARP_RAMP_RES ; ++x)
{ {
zeus::CVector2f vec((x - halfRes) / halfRes, (y - halfRes) / halfRes); zeus::CVector2f vec((x - halfRes) / halfRes, (y - halfRes) / halfRes);
if (vec.magnitude() <= halfRes && vec.canBeNormalized()) float mag = vec.magnitude();
if (mag <= 1.f && vec.canBeNormalized())
{
vec.normalize(); vec.normalize();
data[y][x][0] = zeus::clamp(0.f, vec.x / 2.f + 1.f, 1.f) * 255; vec *= std::sqrt(mag);
data[y][x][1] = zeus::clamp(0.f, vec.y / 2.f + 1.f, 1.f) * 255; }
data[y][x][0] = zeus::clamp(0.f, ((vec.x / 2.f + 0.5f) - x / float(WARP_RAMP_RES)) + 0.5f, 1.f) * 255;
data[y][x][1] = zeus::clamp(0.f, ((vec.y / 2.f + 0.5f) - y / float(WARP_RAMP_RES)) + 0.5f, 1.f) * 255;
} }
} }
m_warpTex = ctx.newStaticTexture(WARP_RAMP_RES, WARP_RAMP_RES, 1, m_warpTex = ctx.newStaticTexture(WARP_RAMP_RES, WARP_RAMP_RES, 1,
@ -48,8 +52,6 @@ CSpaceWarpFilter::CSpaceWarpFilter()
m_dataBind = TShader<CSpaceWarpFilter>::BuildShaderDataBinding(ctx, *this); m_dataBind = TShader<CSpaceWarpFilter>::BuildShaderDataBinding(ctx, *this);
return true; return true;
}); });
setStrength(1.f);
} }
void CSpaceWarpFilter::draw(const zeus::CVector2f& pt) 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 */ /* Indirect coords are full-texture sampling when warp is completely in viewport */
m_uniform.m_indXf[1][1] = 1.f; m_uniform.m_indXf[1][1] = 1.f;
m_uniform.m_indXf[0][0] = 1.f; m_uniform.m_indXf[0][0] = 1.f;
m_uniform.m_indXf[3][0] = 0.f; m_uniform.m_indXf[2][0] = 0.f;
m_uniform.m_indXf[3][1] = 0.f; m_uniform.m_indXf[2][1] = 0.f;
/* Warp effect is fixed at 192x192 rectangle in original (1/2.5 viewport height) */ /* 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[1][1] = 1.f / 2.5f;
m_uniform.m_matrix[0][0] = m_uniform.m_matrix[1][1] / CGraphics::g_ProjAspect; m_uniform.m_matrix[0][0] = m_uniform.m_matrix[1][1] / CGraphics::g_ProjAspect;
SClipScreenRect clipRect = {}; 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) if (clipRect.x4_left >= CGraphics::g_ViewportResolution.x)
return; 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) if (clipRect.x8_top >= CGraphics::g_ViewportResolution.y)
return; 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) if (clipRect.x4_left + clipRect.xc_width <= 0)
return; 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) if (clipRect.x8_top + clipRect.x10_height <= 0)
return; return;
float oldW = clipRect.xc_width;
if (clipRect.x4_left < 0) if (clipRect.x4_left < 0)
{ {
float old = clipRect.xc_width;
clipRect.xc_width += clipRect.x4_left; clipRect.xc_width += clipRect.x4_left;
m_uniform.m_indXf[0][0] = clipRect.xc_width / old; m_uniform.m_indXf[0][0] = clipRect.xc_width / oldW;
m_uniform.m_indXf[3][0] = -clipRect.x4_left * m_uniform.m_matrix[0][0]; m_uniform.m_indXf[2][0] = -clipRect.x4_left / oldW;
clipRect.x4_left = 0; clipRect.x4_left = 0;
} }
float oldH = clipRect.x10_height;
if (clipRect.x8_top < 0) if (clipRect.x8_top < 0)
{ {
float old = clipRect.x10_height;
clipRect.x10_height += clipRect.x8_top; clipRect.x10_height += clipRect.x8_top;
m_uniform.m_indXf[1][1] = clipRect.x10_height / old; m_uniform.m_indXf[1][1] = clipRect.x10_height / oldH;
m_uniform.m_indXf[3][1] = -clipRect.x8_top * m_uniform.m_matrix[1][1]; m_uniform.m_indXf[2][1] = -clipRect.x8_top / oldH;
clipRect.x8_top = 0; clipRect.x8_top = 0;
} }
float tmp = clipRect.x4_left + clipRect.xc_width; float tmp = clipRect.x4_left + clipRect.xc_width;
if (tmp >= CGraphics::g_ViewportResolution.x) if (tmp >= CGraphics::g_ViewportResolution.x)
{ {
float old = clipRect.xc_width;
clipRect.xc_width = CGraphics::g_ViewportResolution.x - clipRect.x4_left; 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; tmp = clipRect.x8_top + clipRect.x10_height;
if (tmp >= CGraphics::g_ViewportResolution.y) if (tmp >= CGraphics::g_ViewportResolution.y)
{ {
float old = clipRect.x10_height;
clipRect.x10_height = CGraphics::g_ViewportResolution.y - clipRect.x8_top; 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) */ /* 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 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); //zeus::CVector2f center(clipRect.x4_left + clipRect.xc_width / 2.f, clipRect.x8_top + clipRect.x10_height / 2.f);
center /= vp; //center /= vp;
center *= zeus::CVector2f{2.f, 2.f}; //center *= zeus::CVector2f{2.f, 2.f};
center -= zeus::CVector2f{1.f, 1.f}; //center -= zeus::CVector2f{1.f, 1.f};
m_uniform.m_matrix[0][0] = clipRect.xc_width / vp.x; 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[1][1] = clipRect.x10_height / vp.y;
m_uniform.m_matrix[3][0] = center.x; m_uniform.m_matrix[2][0] = pt.x;
m_uniform.m_matrix[3][1] = center.y; 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)); m_uniBuf->load(&m_uniform, sizeof(m_uniform));
CGraphics::g_BooMainCommandQueue->setShaderDataBinding(m_dataBind); CGraphics::g_BooMainCommandQueue->setShaderDataBinding(m_dataBind);

View File

@ -26,16 +26,13 @@ class CSpaceWarpFilter
boo::IGraphicsBufferD* m_uniBuf; boo::IGraphicsBufferD* m_uniBuf;
boo::IShaderDataBinding* m_dataBind = nullptr; boo::IShaderDataBinding* m_dataBind = nullptr;
Uniform m_uniform; Uniform m_uniform;
float m_strength = 1.f;
void GenerateWarpRampTex(boo::IGraphicsDataFactory::Context& ctx); void GenerateWarpRampTex(boo::IGraphicsDataFactory::Context& ctx);
public: public:
CSpaceWarpFilter(); CSpaceWarpFilter();
void setStrength(float scale) void setStrength(float strength) { m_strength = strength; }
{
m_uniform.m_strength[0] = scale;
m_uniform.m_strength[1] = scale;
}
void draw(const zeus::CVector2f& pt); void draw(const zeus::CVector2f& pt);
using _CLS = CSpaceWarpFilter; using _CLS = CSpaceWarpFilter;

View File

@ -18,18 +18,18 @@ BOO_GLSL_BINDING_HEAD
"\n" "\n"
"struct VertToFrag\n" "struct VertToFrag\n"
"{\n" "{\n"
" vec4 strength;\n"
" vec2 sceneUv;\n" " vec2 sceneUv;\n"
" vec2 indUv;\n" " vec2 indUv;\n"
" vec2 strength;\n"
"};\n" "};\n"
"\n" "\n"
"SBINDING(0) out VertToFrag vtf;\n" "SBINDING(0) out VertToFrag vtf;\n"
"void main()\n" "void main()\n"
"{\n" "{\n"
" vtf.strength = strength;\n" " gl_Position = vec4(mat3(mainMtx) * vec3(posIn.xy, 1.0), 1.0);\n"
" vtf.sceneUv = (mainMtx * uvIn).xy;\n" " vtf.sceneUv = gl_Position.xy * vec2(0.5) + vec2(0.5);\n"
" vtf.indUv = (indMtx * uvIn).xy;\n" " vtf.indUv = (mat3(indMtx) * vec3(uvIn.xy, 1.0)).xy;\n"
" gl_Position = mainMtx * vec4(posIn.xyz, 1.0);\n" " vtf.strength = strength.xy;\n"
"}\n"; "}\n";
static const char* FS = static const char* FS =
@ -37,9 +37,9 @@ static const char* FS =
BOO_GLSL_BINDING_HEAD BOO_GLSL_BINDING_HEAD
"struct VertToFrag\n" "struct VertToFrag\n"
"{\n" "{\n"
" vec4 strength;\n"
" vec2 sceneUv;\n" " vec2 sceneUv;\n"
" vec2 indUv;\n" " vec2 indUv;\n"
" vec2 strength;\n"
"};\n" "};\n"
"\n" "\n"
"SBINDING(0) in VertToFrag vtf;\n" "SBINDING(0) in VertToFrag vtf;\n"
@ -48,7 +48,10 @@ BOO_GLSL_BINDING_HEAD
"TBINDING1 uniform sampler2D indTex;\n" "TBINDING1 uniform sampler2D indTex;\n"
"void main()\n" "void main()\n"
"{\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"; "}\n";
URDE_DECL_SPECIALIZE_SHADER(CSpaceWarpFilter) URDE_DECL_SPECIALIZE_SHADER(CSpaceWarpFilter)

View File

@ -0,0 +1,13 @@
#include "CSpaceWarpFilter.hpp"
namespace urde
{
TShader<CSpaceWarpFilter>::IDataBindingFactory* CSpaceWarpFilter::Initialize(boo::MetalDataFactory::Context& ctx,
boo::IShaderPipeline*& pipeOut,
boo::IVertexFormat*& vtxFmtOut)
{
return nullptr;
}
}