by my understanding, is that what the following basically does? (btw, this is an excerpt from scrawk’s excellent blog tutorial on compute shaders)
using UnityEngine;
using System.Collections;
public class BufferExample : MonoBehaviour
{
public Material material;
ComputeBuffer buffer;
const int count = 1024;
const float size = 5.0f;
void Start ()
{
buffer = new ComputeBuffer(count, sizeof(float)*3, ComputeBufferType.Default);
float[] points = new float[count*3];
Random.seed = 0;
for(int i = 0; i < count; i++)
{
points[i*3+0] = Random.Range(-size,size);
points[i*3+1] = Random.Range(-size,size);
points[i*3+2] = 0.0f;
}
buffer.SetData(points);
}
void OnPostRender()
{
material.SetPass(0);
material.SetBuffer("buffer", buffer);
Graphics.DrawProcedural(MeshTopology.Points, count, 1);
}
void OnDestroy()
{
buffer.Release();
}
}
and the shader:
Shader "Custom/BufferShader"
{
SubShader
{
Pass
{
ZTest Always Cull Off ZWrite Off
Fog { Mode off }
CGPROGRAM
#include "UnityCG.cginc"
#pragma target 5.0
#pragma vertex vert
#pragma fragment frag
uniform StructuredBuffer<float3> buffer;
struct v2f
{
float4 pos : SV_POSITION;
};
v2f vert(uint id : SV_VertexID)
{
float4 pos = float4(buffer[id], 1);
v2f OUT;
OUT.pos = mul(UNITY_MATRIX_MVP, pos);
return OUT;
}
float4 frag(v2f IN) : COLOR
{
return float4(1,0,0,1);
}
ENDCG
}
}
}
now if you’ll notice, there isn’t any GetData() call to the compute shader, however you’ll still need to pass in the buffer to the material for the shader, but since you aren’t actually modifying the buffer on the cpu side, i’m not sure if you’re just passing the pointer to the buffer to the shader ‘linking’ them…
please correct me if i’m wrong about my assumptions, because i’d like to know definitively as well…
[edit] actually, looking at this now it seems the compute shader feeds the vert shader, if the vert shader could act on the buffer such as a ‘RWStructuredBuffer buffer’ in the vert shader then the vert shader could act on the buffer, but i don’t think it can… mebbe with this? : uniform AppendStructuredBuffer appendBuffer