Dear Unity community,
When trying to use CommandBuffer.Blit in Single Pass Instanced VR mode I get this incorrect result
(Code at bottom of post)
I have tried all the suggestions in the doc about adding support to the shader for GPU instancing and add the defs to support texture arrays which did not help. I found that calling ScriptableRenderPass.Blit or CommandBuffer.Blit in any way causes the issue, even if it doesn’t blit into the CameraTargetColorTarget (back buffer?), for example,
this.Blit(cmd, m_textureResult.Identifier(), m_texture2Result.Identifier())
I have since move on to try this tutorial
This leads to more success, however, then doesn’t render any objects with Transparent materials.
Without feature:
Using SRP is a new area for me so I apologies for my lack of in-depth knowledge/explanations. Please help me with a solution for simple full screen SRP blitting that works for both VR and NoneVR Single Instanced (Multi View works with first solution already)?
Thanks!
Code for first attempt:
public class FadeSRP : ScriptableRendererFeature
{
public bool Enabled = false;
[Serializable]
public class FadeSettings
{
public RenderPassEvent renderPassEvent = RenderPassEvent.AfterRenderingTransparents;
public Material EffectMaterial;
}
private FadePass m_fadePass;
public FadeSettings Settings;
public override void Create()
{
if(Settings.EffectMaterial == null)
{
Debug.LogWarning("Fade Shader null");
return;
}
m_fadePass = new FadePass(Settings.EffectMaterial, Settings.renderPassEvent);
}
public override void AddRenderPasses(ScriptableRenderer renderer, ref RenderingData renderingData)
{
if (renderingData.cameraData.cameraType == CameraType.Game)
{
if (Settings.EffectMaterial != null && Enabled)
{
// Calling ConfigureInput with the ScriptableRenderPassInput.Color argument ensures that the opaque texture is available to the Render Pass
m_fadePass.ConfigureInput(ScriptableRenderPassInput.Color);
m_fadePass.Setup(renderer.cameraColorTarget);
renderer.EnqueuePass(m_fadePass);
}
}
}
public void SetFadeDelta(float delta)
{
m_fadePass.SetDelta(delta);
}
}
public class FadePass : ScriptableRenderPass
{
private readonly Material m_effectMaterial;
private RenderTargetIdentifier m_cameraTarget;
private const string TagName = "FadePass";
public FadePass(Material effectMaterial, RenderPassEvent renderPassEvent)
{
this.renderPassEvent = renderPassEvent;
m_effectMaterial = effectMaterial;
}
public void Setup(RenderTargetIdentifier cameraTarget)
{
m_cameraTarget = cameraTarget;
}
public override void Execute(ScriptableRenderContext context, ref RenderingData renderingData)
{
CommandBuffer cmd = CommandBufferPool.Get(TagName);
cmd.Clear();
this.Blit(cmd, ref renderingData, m_effectMaterial, 0);
context.ExecuteCommandBuffer(cmd);
CommandBufferPool.Release(cmd);
}
public void SetDelta(float delta)
{
m_effectMaterial.SetFloat("_Delta", delta);
}
}
Shader "SRP/SRPFade"
{
Properties
{
_MainTex("Texture", any) = "" {}
_Color("Multiplicative color", Color) = (1.0, 1.0, 1.0, 1.0)
_Delta("Delta", Float) = 0
}
SubShader{
Pass {
ZTest Always Cull Off ZWrite Off
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#pragma multi_compile_instancing
#include "UnityCG.cginc"
UNITY_DECLARE_SCREENSPACE_TEXTURE(_MainTex);
uniform float4 _MainTex_ST;
uniform float _Delta;
struct appdata_t {
float4 vertex : POSITION;
float2 texcoord : TEXCOORD0;
UNITY_VERTEX_INPUT_INSTANCE_ID
};
struct v2f {
float4 vertex : SV_POSITION;
float2 texcoord : TEXCOORD0;
UNITY_VERTEX_INPUT_INSTANCE_ID
UNITY_VERTEX_OUTPUT_STEREO
};
UNITY_INSTANCING_BUFFER_START(Props)
UNITY_DEFINE_INSTANCED_PROP(float4, _Color)
UNITY_INSTANCING_BUFFER_END(Props)
v2f vert(appdata_t v)
{
v2f o;
UNITY_SETUP_INSTANCE_ID(v);
UNITY_TRANSFER_INSTANCE_ID(v, o);
UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(o);
o.vertex = UnityObjectToClipPos(v.vertex);
o.texcoord = TRANSFORM_TEX(v.texcoord.xy, _MainTex);
return o;
}
fixed4 frag(v2f i) : SV_Target
{
UNITY_SETUP_STEREO_EYE_INDEX_POST_VERTEX(i);
UNITY_SETUP_INSTANCE_ID(i);
float4 rawColor = UNITY_SAMPLE_SCREENSPACE_TEXTURE(_MainTex, i.texcoord);
return lerp(rawColor, _Color, _Delta);
}
ENDCG
}
}
Fallback Off
}