Skip to content

Commit

Permalink
GLTF Viewer: render motion vectors for environment map
Browse files Browse the repository at this point in the history
  • Loading branch information
TheMostDiligent committed Feb 5, 2024
1 parent f456bdb commit 92105f7
Showing 1 changed file with 24 additions and 7 deletions.
31 changes: 24 additions & 7 deletions Samples/GLTFViewer/src/GLTFViewer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -427,6 +427,18 @@ struct PSOutput
}

static constexpr char EnvMapPSMain[] = R"(
void main(in float4 Pos : SV_Position,
in float4 ClipPos : CLIP_POS,
out float4 Color : SV_Target0,
out float4 MotionVec : SV_Target4)
{
SampleEnvMapOutput EnvMap = SampleEnvMap(ClipPos);
Color = EnvMap.Color;
MotionVec = float4(EnvMap.MotionVector, 0.0, 1.0);
}
)";

static constexpr char EnvMapPSMainGL[] = R"(
void main(in float4 Pos : SV_Position,
in float4 ClipPos : CLIP_POS,
out float4 Color : SV_Target0,
Expand All @@ -436,12 +448,12 @@ void main(in float4 Pos : SV_Position,
out float4 MotionVec : SV_Target4,
out float4 SpecularIBL : SV_Target5)
{
Color = SampleEnvMap(ClipPos);
SampleEnvMapOutput EnvMap = SampleEnvMap(ClipPos);
Color = EnvMap.Color;
Normal = float4(0.0, 0.0, 0.0, 0.0);
BaseColor = float4(0.0, 0.0, 0.0, 0.0);
MaterialData = float4(0.0, 0.0, 0.0, 0.0);
MotionVec = float4(0.0, 0.0, 0.0, 0.0);
MotionVec = float4(EnvMap.MotionVector, 0.0, 1.0);
SpecularIBL = float4(0.0, 0.0, 0.0, 0.0);
}
)";
Expand Down Expand Up @@ -539,8 +551,12 @@ void GLTFViewer::CrateEnvMapRenderer()

if (m_pDevice->GetDeviceInfo().IsGLDevice())
{
// Normally, environment map shader does not need to write to other targets.
// Normally, environment map shader only needs to write color and motion vector.
// However, on WebGL this results in errors.
EnvMapRendererCI.PSMainSource = EnvMapPSMainGL;
}
else
{
EnvMapRendererCI.PSMainSource = EnvMapPSMain;
}
}
Expand Down Expand Up @@ -1197,8 +1213,9 @@ void GLTFViewer::Render()
EnvMapAttribs.MipLevel = m_EnvMapMipLevel;
// It is essential to write zero alpha because we use alpha channel
// to attenuate SSR for transparent surfaces.
EnvMapAttribs.Alpha = 0.0;
EnvMapAttribs.ConvertOutputToSRGB = (m_RenderParams.Flags & GLTF_PBR_Renderer::PSO_FLAG_CONVERT_OUTPUT_TO_SRGB) != 0;
EnvMapAttribs.Alpha = 0.0;
EnvMapAttribs.ConvertOutputToSRGB = (m_RenderParams.Flags & GLTF_PBR_Renderer::PSO_FLAG_CONVERT_OUTPUT_TO_SRGB) != 0;
EnvMapAttribs.ComputeMotionVectors = m_bEnablePostProcessing;

m_EnvMapRenderer->Render(EnvMapAttribs, TMAttribs);
}
Expand Down Expand Up @@ -1274,7 +1291,7 @@ void GLTFViewer::Render()
Attribs.pContext = m_pImmediateContext;
Attribs.GridSize = {SCDesc.Width / 20, SCDesc.Height / 20};
// Render motion vectors in the opposite direction
Attribs.Scale = float2{-0.05f} / std::max(m_ElapsedTime, 0.001f);
Attribs.Scale = float2{-0.01f} / std::max(m_ElapsedTime, 0.001f);
Attribs.StartColor = float4{1};
Attribs.EndColor = float4{0.5, 0.5, 0.5, 1.0};
Attribs.ConvertOutputToSRGB = (SCDesc.ColorBufferFormat == TEX_FORMAT_RGBA8_UNORM || SCDesc.ColorBufferFormat == TEX_FORMAT_BGRA8_UNORM);
Expand Down

0 comments on commit 92105f7

Please sign in to comment.