I had tested a simple case that ComputeShader + RenderTexture{dimension = TextureDimension.Tex2DArray} , In UnityEditor it works well, but it not working on device.
using UnityEngine;
using UnityEngine.Experimental.Rendering;
using UnityEngine.Rendering;
public class MyRenderTextureArrayCompute : MonoBehaviour
{
[SerializeField]
private ComputeShader _MyRenderTextureArrayComputeShader;
private CommandBuffer _cb;
private RenderTexture _renderTexture;
private readonly int Slices = 2;
private readonly int Resolution = 256;
// Start is called once before the first execution of Update after the MonoBehaviour is created
void Start()
{
_cb ??= new()
{
name = "MyRenderTextureArrayComputeCommandBuffer",
};
_renderTexture = _CreateRenderTextureArray();
_SetSampleTextureArray();
}
// Update is called once per frame
void LateUpdate()
{
_Cmd_Draw();
#if UNITY_VISIONOS
Unity.PolySpatial.PolySpatialObjectUtils.MarkDirty(_renderTexture);
#endif
}
void _Cmd_Draw()
{
_cb.Clear();
_cb.SetComputeTextureParam(_MyRenderTextureArrayComputeShader, 0, "_RW_Target", _renderTexture);
_cb.DispatchCompute
(
_MyRenderTextureArrayComputeShader,
0,
Resolution / 1,
Resolution / 1,
Slices
);
Graphics.ExecuteCommandBuffer(_cb);
}
private protected RenderTexture _CreateRenderTextureArray()
{
GraphicsFormat CompatibleTextureFormat = GraphicsFormat.R16G16B16A16_SFloat;
string _TextureName = "RenderTexture-MyRenderTextureArrayCompute";
bool NeedToReadWriteTextureData = true;
RenderTexture result = new(Resolution, Resolution, 0, CompatibleTextureFormat)
{
wrapMode = TextureWrapMode.Clamp,
antiAliasing = 1,
filterMode = FilterMode.Bilinear,
anisoLevel = 0,
useMipMap = false,
name = _TextureName,
dimension = TextureDimension.Tex2DArray,
volumeDepth = Slices,
enableRandomWrite = NeedToReadWriteTextureData,
};
result.Create();
return result;
}
void _SetSampleTextureArray()
{
#if UNITY_VISIONOS
Unity.PolySpatial.PolySpatialShaderGlobals.SetTexture("_MyTexture2DArray", _renderTexture);
#else
Shader.SetGlobalTexture("_MyTexture2DArray", _renderTexture);
#endif
}
}
May I ask where I am using it wrong? Or is this a bug?
For convenience, I share a simple project.
Unity: 6000.0.39f1
Polyspatial: 2.1.2
visionOS: 2.4
