When I use StructuredBuffer in shaders for Android, they always draw objects purple, and their Shader.isSupported return false.
My Android device supports OpenGL ES 3.2, and SystemInfo.supportsComputeShaders returns true, though.
Of course it passes target 4.5. Drawing fails only if the shader contains StructuredBuffer.
I tried to turn on Requires ES 3.1 check at player settings, which had no effect.
What is the way to make them work?
These are the codes:
Shader "TestShader" {
Properties{
_MainTex("Texture", 2D) = "white" {}
}
Category{
Tags{ "Queue" = "Transparent" "IgnoreProjector" = "True" "RenderType" = "Transparent" }
Blend SrcAlpha OneMinusSrcAlpha
Cull Off
Lighting Off
ZWrite On
ZTest On
SubShader{
Pass{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#pragma target 4.5
#include "UnityCG.cginc"
sampler2D _MainTex;
StructuredBuffer<float> _Colors;
struct appdata_t {
float4 vertex : POSITION;
float2 texcoord : TEXCOORD0;
};
struct v2f {
float4 vertex : SV_POSITION;
float2 texcoord : TEXCOORD0;
float4 color : TEXCOORD1;
};
v2f vert(appdata_t v) {
v2f o;
o.vertex = UnityObjectToClipPos(v.vertex);
o.texcoord = v.texcoord;
o.color = float4(_Colors[0], _Colors[1], _Colors[2], 1);
return o;
}
fixed4 frag(v2f i) : SV_Target {
float4 color = tex2D(_MainTex, i.texcoord) * i.color;
clip(color.a - 0.001);
return color;
}
ENDCG
}
}
}
}
using UnityEngine;
public class TestComponent : MonoBehaviour {
ComputeBuffer buffer;
float[] array;
float time;
void Start() {
buffer = new ComputeBuffer(4, sizeof(float));
var mr = GetComponent<MeshRenderer>();
mr.material.SetBuffer("_Colors", buffer);
array = new float[3];
}
void Update() {
time += Time.deltaTime;
if (time > 0.1f) {
array[0] = Random.Range(0.5f, 1f);
array[1] = Random.Range(0.5f, 1f);
array[2] = Random.Range(0.5f, 1f);
buffer.SetData(array);
time -= 0.1f;
}
}
void OnDestroy() {
buffer.Release();
}
void OnGUI() {
var mr = GetComponent<MeshRenderer>();
GUILayout.Label("ComputeShader is supported: " + SystemInfo.supportsComputeShaders.ToString());
GUILayout.Label("This shader is supported: " + mr.sharedMaterial.shader.isSupported.ToString());
}
}