- Home /
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?.
Answer by Kibsgaard · Jul 15, 2014 at 10:33 AM
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.
In some cases Find$$anonymous$$ernel can't find the kernel For example: #pragma kernel CS$$anonymous$$ain #pragma kernel SwapBuffer
RWStructuredBuffer<float> output;
StructuredBuffer<float> buffer;
int width;
int height;
StructuredBuffer<float> emitters;
StructuredBuffer<float> obstacles;
[numthreads(8,8,1)]
void CS$$anonymous$$ain (uint2 id : SV_DispatchThreadID)
{
int em = emitters[id.x + width * id.y];
float p = em
+ buffer[id.x + 1 + width * id.y]
+ buffer[id.x - 1 + width * id.y]
+ buffer[id.x + width * (id.y + 1)]
+ buffer[id.x + width * (id.y - 1)]
;
output[id.x] = p * obstacles[id.x + width * id.y];
}
[numthreads(64,1,1)]
void SwapBuffer (uint2 id : SV_DispatchThreadID)
{
buffer[id.x] = output[id.x];
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Unity.Collections;
using UnityEngine.Experimental.Rendering;
public class SimulationOnComputeShader : Simulation {
public ComputeShader cs;
public int size = 100;
ComputeBuffer bufferEmitters;
ComputeBuffer bufferObstacles;
ComputeBuffer output;
ComputeBuffer buffer;
NativeArray<float> result = new NativeArray<float>();
int kernel$$anonymous$$ain, kernelSwapBuffer;
void OnEnable()
{
cs.SetInt("width", size);
cs.SetInt("height", size);
kernel$$anonymous$$ain = cs.Find$$anonymous$$ernel("CS$$anonymous$$ain");
kernelSwapBuffer = cs.Find$$anonymous$$ernel("SwapBuffer");
bufferEmitters = new ComputeBuffer(size * size, sizeof(float));
bufferObstacles = new ComputeBuffer(size * size, sizeof(float));
output = new ComputeBuffer(size * size, sizeof(float));
buffer = new ComputeBuffer(size * size, sizeof(float));
cs.SetBuffer(kernel$$anonymous$$ain, "emitters", bufferEmitters);
cs.SetBuffer(kernel$$anonymous$$ain, "obstacles", bufferObstacles);
cs.SetBuffer(kernel$$anonymous$$ain, "output", output);
cs.SetBuffer(kernel$$anonymous$$ain, "buffer", buffer);
}
}
Your answer
Follow this Question
Related Questions
Texture sampling will move when sampled same position. 1 Answer
Compute Shader not work on older GPU with DX11 0 Answers
Compute Shader crashes when buffer is too big 0 Answers
Calling Graphics.DrawProcedural multiple times for chunked procedural terrain 2 Answers
ComputeShader: Accessing array elements within a StructuredBuffer 1 Answer