More than one Kernel in Compute Shader

Hi,

I’m experimenting with compute shaders and wanted to test having more than one kernel. I created a compute shader where one kernel fills a buffer with “red” the other with fills it with “green”. In the CPU I then grab the buffer data and copy it to a texture to visualise.

Compute Shader:

#pragma kernel FillWithRed
#pragma kernel FillWithGreen

RWStructuredBuffer<float3> outBuffer;

[numthreads(32, 32, 1)]
void FillWithRed(uint3 id : SV_GroupThreadID)
{
	int pos = id.y*32 + id.x;
	outBuffer[pos] = float3(1.0, 0.0, 0.0);
}

[numthreads(32, 32, 1)]
void FillWithGreen(uint3 id : SV_GroupThreadID)
{
	int pos = id.y*32 + id.x;
	outBuffer[pos] = float3(0.0, 1.0, 0.0);
}

CPU code:

public class Simulation : MonoBehaviour {

	Texture2D m_texture;
	int m_size = 32;
	public ComputeShader m_shaderSimulation;
	Vector3[] results;
	ComputeBuffer buffer;

	void Start()
    {
		m_texture = new Texture2D(m_size, m_size, TextureFormat.ARGB32, false, false);
		renderer.material.mainTexture = m_texture;
        renderer.material.mainTexture.wrapMode = TextureWrapMode.Clamp;
	}

    void CreateBuffersIfNeeded()
    {
        if (buffer == null)
        {
            buffer = new ComputeBuffer(m_size * m_size, 12, ComputeBufferType.Default);
            m_shaderSimulation.SetBuffer(0, "outBuffer", buffer);
            results = new Vector3[m_size * m_size];
        }
    }

	void Compute()
{
        CreateBuffersIfNeeded();
        m_shaderSimulation.Dispatch(0, 1, 1, 1);
        //m_shaderSimulation.Dispatch(1, 1, 1, 1);
}

    void CopyBufferToTexture()
    {
        // copy buffers to texture to test the result
        buffer.GetData(results);
        for (int i = 0; i < m_size; ++i)
        {
            for (int j = 0; j < m_size; ++j)
            {
                m_texture.SetPixel(j, i, new Color(results[i * m_size + j].x, results[i * m_size + j].y, results[i * m_size + j].z, 1f));
            }
        }
        m_texture.Apply();
    }
		
	void Update ()
	{
		Compute ();
        CopyBufferToTexture();
	}

	void OnDisable ()
	{
        buffer.Dispose();
	}
}

If I do m_shaderSimulation.Dispatch(0, 1, 1, 1) the texture shows up red as expected, but if I do m_shaderSimulation.Dispatch(1, 1, 1, 1) it just shows up black instead of green. Am I doing something wrong? What could be causing this?.

You texture is ARGB32.
So you’re setting Alpha = 1.0 in FillWithRed and Alpha = 0.0 in FillWithGreen, which is why it is showing up black.
Stick with float4 and include the alpha to avoid errors like this.

I believe that the stride on your buffer is incorrect. It should be in bytes, not bits (its silly that it doesn’t say that in the documentation). Use sizeof(Int32) to be certain of the stride. Should speed up GetData() significantly.

Last thing, it is good practice to use m_shaderSimulation.FindKernel(“FillWithRed”) instead of hardcoding the kernel ID.