Hi,
I’ve been trying to assign an float[ ] array to a StructuredBuffer in a compute shader and fill a RenderTexture with the values but no success. If i hardcode a value, let’s say red in the float4 in the cs kernel, i can see the render target red. However when i use the StructuredBuffer i get the default gray. Any brighter mind can bring some light? Thanks a lot in advance!
float[] positionsX;
float[] positionsY;
float[] positionsZ;
ComputeBuffer csPositionsX;
ComputeBuffer csPositionsY;
ComputeBuffer csPositionsZ;
Texture2D displacementTexture;
RenderTexture csDispOut;
// ---------- Initialize
// Here this.DataLength = textureSize.x * textureSize.y;
this.PositionsX = new float[this.DataLength];
this.PositionsY = new float[this.DataLength];
this.PositionsZ = new float[this.DataLength];
this.displacementTexture = new Texture2D((int)this.TextureSize.x, (int)this.TextureSize.y, TextureFormat.ARGB32, true, true);
this.displacementTexture.filterMode = FilterMode.Point;
this.displacementTexture.wrapMode = TextureWrapMode.Clamp;
this.csDispOut = new RenderTexture((int)this.TextureSize.x, (int)this.TextureSize.y, 8, RenderTextureFormat.ARGB32, RenderTextureReadWrite.Linear);
this.csDispOut.enableRandomWrite = true;
this.csDispOut.Create();
this.csPositionsX = new ComputeBuffer(this.DataLength, 4, ComputeBufferType.Default);
this.csPositionsY = new ComputeBuffer(this.DataLength, 4, ComputeBufferType.Default);
this.csPositionsZ = new ComputeBuffer(this.DataLength, 4, ComputeBufferType.Default);
// ------------ Update
// Compute Shader
this.csPositionsX.SetData(this.PositionsX);
this.csPositionsY.SetData(this.PositionsY);
this.csPositionsZ.SetData(this.PositionsZ);
this.csFiltering.SetTexture(0, "DispOut", this.csDispOut);
this.csFiltering.SetTexture(0, "NormOut", this.csNormOut);
this.csFiltering.Dispatch(0, ((int)(this.TextureSize.x)) / 8, ((int)(this.TextureSize.y)) / 8, 1);
RenderTexture.active = this.csDispOut;
this.displacementTexture.ReadPixels(new Rect(0, 0, (int)this.TextureSize.x, (int)this.TextureSize.y), 0, 0);
this.displacementTexture.Apply();
// Compute Shader
#pragma kernel CSBakeToTexture
StructuredBuffer<float> PositionsX;
StructuredBuffer<float> PositionsY;
StructuredBuffer<float> PositionsZ;
RWTexture2D<float4> DispOut;
#define BLOCKSIZE 8
// Floats to Texture
[numthreads(BLOCKSIZE, BLOCKSIZE, 1)]
void CSBakeToTexture (uint3 Gid : SV_GroupID, uint3 DTid : SV_DispatchThreadID, uint3 GTid : SV_GroupThreadID, uint GI : SV_GroupIndex)
{
// uint i = (DTid.x * DTid.y) + DTid.x;
uint i = (640 * DTid.y) + DTid.x;
DispOut[uint2(DTid.x, DTid.y)] = float4(PositionsX[i], PositionsY[i], PositionsZ[i], 1);
}