Add support for main view information in Surface materials during subpass rendering (eg. shadow depth)

This commit is contained in:
Wojtek Figat
2022-07-29 16:57:57 +02:00
parent 22209108c9
commit 6cf91b1da4
9 changed files with 72 additions and 15 deletions

View File

@@ -14,6 +14,8 @@ float4x4 WorldMatrix;
float4x4 ViewMatrix;
float4x4 PrevViewProjectionMatrix;
float4x4 PrevWorldMatrix;
float4x4 MainViewProjectionMatrix;
float4 MainScreenSize;
float3 ViewPos;
float ViewFar;
float3 ViewDir;

View File

@@ -357,11 +357,14 @@ namespace FlaxEditor.Surface.Archetypes
Title = "Screen Position",
Description = "Gathers screen position or texcoord",
Flags = NodeFlags.MaterialGraph,
Size = new Float2(150, 40),
Size = new Float2(160, 40),
DefaultValues = new object[] { false },
Elements = new[]
{
NodeElementArchetype.Factory.Output(0, "Position", typeof(Float2), 0),
NodeElementArchetype.Factory.Output(1, "Texcoord", typeof(Float2), 1),
NodeElementArchetype.Factory.Bool(0, 0, 0),
NodeElementArchetype.Factory.Text(20, 0, "Main View"),
}
},
new NodeArchetype
@@ -370,11 +373,14 @@ namespace FlaxEditor.Surface.Archetypes
Title = "Screen Size",
Description = "Gathers screen size",
Flags = NodeFlags.MaterialGraph,
Size = new Float2(150, 40),
Size = new Float2(160, 40),
DefaultValues = new object[] { false },
Elements = new[]
{
NodeElementArchetype.Factory.Output(0, "Size", typeof(Float2), 0),
NodeElementArchetype.Factory.Output(1, "Inv Size", typeof(Float2), 1),
NodeElementArchetype.Factory.Bool(0, 0, 0),
NodeElementArchetype.Factory.Text(20, 0, "Main View"),
}
},
new NodeArchetype

View File

@@ -22,6 +22,8 @@ PACK_STRUCT(struct DeferredMaterialShaderData {
Matrix ViewMatrix;
Matrix PrevViewProjectionMatrix;
Matrix PrevWorldMatrix;
Matrix MainViewProjectionMatrix;
Float4 MainScreenSize;
Float3 ViewPos;
float ViewFar;
Float3 ViewDir;
@@ -86,6 +88,8 @@ void DeferredMaterialShader::Bind(BindParameters& params)
Matrix::Transpose(view.View, materialData->ViewMatrix);
Matrix::Transpose(drawCall.Surface.PrevWorld, materialData->PrevWorldMatrix);
Matrix::Transpose(view.PrevViewProjection, materialData->PrevViewProjectionMatrix);
Matrix::Transpose(view.MainViewProjection, materialData->MainViewProjectionMatrix);
materialData->MainScreenSize = view.MainScreenSize;
materialData->ViewPos = view.Position;
materialData->ViewFar = view.Far;
materialData->ViewDir = view.Direction;

View File

@@ -24,6 +24,8 @@ PACK_STRUCT(struct ForwardMaterialShaderData {
Matrix ViewMatrix;
Matrix PrevViewProjectionMatrix;
Matrix PrevWorldMatrix;
Matrix MainViewProjectionMatrix;
Float4 MainScreenSize;
Float3 ViewPos;
float ViewFar;
Float3 ViewDir;
@@ -94,6 +96,8 @@ void ForwardMaterialShader::Bind(BindParameters& params)
Matrix::Transpose(view.View, materialData->ViewMatrix);
Matrix::Transpose(drawCall.Surface.PrevWorld, materialData->PrevWorldMatrix);
Matrix::Transpose(view.PrevViewProjection, materialData->PrevViewProjectionMatrix);
Matrix::Transpose(view.MainViewProjection, materialData->MainViewProjectionMatrix);
materialData->MainScreenSize = view.MainScreenSize;
materialData->ViewPos = view.Position;
materialData->ViewFar = view.Far;
materialData->ViewDir = view.Direction;

View File

@@ -10,7 +10,7 @@
/// <summary>
/// Current materials shader version.
/// </summary>
#define MATERIAL_GRAPH_VERSION 154
#define MATERIAL_GRAPH_VERSION 155
class Material;
class GPUShader;

View File

@@ -60,7 +60,7 @@ void RenderView::Prepare(RenderContext& renderContext)
PrepareCache(renderContext, width, height, taaJitter);
}
void RenderView::PrepareCache(RenderContext& renderContext, float width, float height, const Float2& temporalAAJitter)
void RenderView::PrepareCache(RenderContext& renderContext, float width, float height, const Float2& temporalAAJitter, RenderView* mainView)
{
// The same format used by the Flax common shaders and postFx materials
ViewInfo = Float4(1.0f / Projection.M11, 1.0f / Projection.M22, Far / (Far - Near), (-Far * Near) / (Far - Near) / Far);
@@ -76,6 +76,12 @@ void RenderView::PrepareCache(RenderContext& renderContext, float width, float h
// Ortho views have issues with screen size LOD culling
const float modelLODDistanceFactor = (renderContext.LodProxyView ? renderContext.LodProxyView->IsOrthographicProjection() : IsOrthographicProjection()) ? 100.0f : ModelLODDistanceFactor;
ModelLODDistanceFactorSqrt = modelLODDistanceFactor * modelLODDistanceFactor;
// Setup main view render info
if (!mainView)
mainView = this;
MainViewProjection = mainView->ViewProjection();
MainScreenSize = mainView->ScreenSize;
}
void RenderView::SetUp(const Matrix& view, const Matrix& projection)

View File

@@ -202,6 +202,16 @@ public:
/// </summary>
API_FIELD() Matrix PrevViewProjection;
/// <summary>
/// The main viewport view * projection matrix.
/// </summary>
API_FIELD() Matrix MainViewProjection;
/// <summary>
/// The main viewport screen size packed (x - width, y - height, zw - inv width, w - inv height).
/// </summary>
API_FIELD() Float4 MainScreenSize;
/// <summary>
/// Square of <see cref="ModelLODDistanceFactor"/>. Cached by rendering backend.
/// </summary>
@@ -220,7 +230,8 @@ public:
/// <param name="width">The rendering width.</param>
/// <param name="height">The rendering height.</param>
/// <param name="temporalAAJitter">The temporal jitter for this frame.</param>
void PrepareCache(RenderContext& renderContext, float width, float height, const Float2& temporalAAJitter);
/// <param name="mainView">The main rendering viewport. Use null if it's top level view; pass pointer to main view for sub-passes like shadow depths.</param>
void PrepareCache(RenderContext& renderContext, float width, float height, const Float2& temporalAAJitter, RenderView* mainView = nullptr);
/// <summary>
/// Determines whether view is perspective projection or orthographic.

View File

@@ -289,7 +289,7 @@ void ShadowsPass::RenderShadow(RenderContext& renderContext, RendererPointLightD
const auto shadowMapsSizeCube = (float)_shadowMapsSizeCube;
context->SetViewportAndScissors(shadowMapsSizeCube, shadowMapsSizeCube);
_shadowContext.View.SetUpCube(PointLight_NearPlane, lightRadius, lightPosition);
_shadowContext.View.PrepareCache(_shadowContext, shadowMapsSizeCube, shadowMapsSizeCube, Float2::Zero);
_shadowContext.View.PrepareCache(_shadowContext, shadowMapsSizeCube, shadowMapsSizeCube, Float2::Zero, &view);
// Render depth to all 6 faces of the cube map
for (int32 faceIndex = 0; faceIndex < 6; faceIndex++)
@@ -392,7 +392,7 @@ void ShadowsPass::RenderShadow(RenderContext& renderContext, RendererSpotLightDa
const auto shadowMapsSizeCube = (float)_shadowMapsSizeCube;
context->SetViewportAndScissors(shadowMapsSizeCube, shadowMapsSizeCube);
_shadowContext.View.SetProjector(SpotLight_NearPlane, lightRadius, lightPosition, lightDirection, light.UpVector, light.OuterConeAngle * 2.0f);
_shadowContext.View.PrepareCache(_shadowContext, shadowMapsSizeCube, shadowMapsSizeCube, Float2::Zero);
_shadowContext.View.PrepareCache(_shadowContext, shadowMapsSizeCube, shadowMapsSizeCube, Float2::Zero, &view);
// Render depth to all 1 face of the cube map
const int32 cubeFaceIndex = 0;
@@ -588,7 +588,7 @@ void ShadowsPass::RenderShadow(RenderContext& renderContext, RendererDirectional
// Set up GPU context and render view
const auto shadowMapsSizeCSM = (float)_shadowMapsSizeCSM;
context->SetViewportAndScissors(shadowMapsSizeCSM, shadowMapsSizeCSM);
_shadowContext.View.PrepareCache(_shadowContext, shadowMapsSizeCSM, shadowMapsSizeCSM, Float2::Zero);
_shadowContext.View.PrepareCache(_shadowContext, shadowMapsSizeCSM, shadowMapsSizeCSM, Float2::Zero, &view);
// Create the different view and projection matrices for each split
float splitMinRatio = 0;

View File

@@ -45,19 +45,43 @@ void MaterialGenerator::ProcessGroupMaterial(Box* box, Node* node, Value& value)
// Screen Position
case 6:
{
// Position
if (box->ID == 0)
value = Value(VariantType::Float2, TEXT("input.SvPosition.xy"));
// Texcoord
else if (box->ID == 1)
value = writeLocal(VariantType::Float2, TEXT("input.SvPosition.xy * ScreenSize.zw"), node);
// Check if use main view position
const auto layer = GetRootLayer();
if (layer && layer->Domain == MaterialDomain::Surface && node->Values.Count() > 0 && node->Values[0].AsBool)
{
// Transform world position into main viewport texcoord space
Value clipPosition = writeLocal(VariantType::Float4, TEXT("mul(float4(input.WorldPosition.xyz, 1), MainViewProjectionMatrix)"), node);
Value uvPos = writeLocal(VariantType::Float2, String::Format(TEXT("(({0}.xy / {0}.w) * float2(0.5, -0.5) + float2(0.5, 0.5))"), clipPosition.Value), node);
// Position
if (box->ID == 0)
value = writeLocal(VariantType::Float2, String::Format(TEXT("{0} * MainScreenSize.xy"), uvPos.Value), node);
// Texcoord
else if (box->ID == 1)
value = uvPos;
}
else
{
// Position
if (box->ID == 0)
value = Value(VariantType::Float2, TEXT("input.SvPosition.xy"));
// Texcoord
else if (box->ID == 1)
value = writeLocal(VariantType::Float2, TEXT("input.SvPosition.xy * ScreenSize.zw"), node);
}
break;
}
// Screen Size
case 7:
value = Value(VariantType::Float2, box->ID == 0 ? TEXT("ScreenSize.xy") : TEXT("ScreenSize.zw"));
{
// Check if use main view position
const auto layer = GetRootLayer();
if (layer && layer->Domain == MaterialDomain::Surface && node->Values.Count() > 0 && node->Values[0].AsBool)
value = Value(VariantType::Float2, box->ID == 0 ? TEXT("MainScreenSize.xy") : TEXT("MainScreenSize.zw"));
else
value = Value(VariantType::Float2, box->ID == 0 ? TEXT("ScreenSize.xy") : TEXT("ScreenSize.zw"));
break;
}
// Custom code
case 8:
{