Quantcast
Channel: Questions in topic: "hlsl"
Viewing all articles
Browse latest Browse all 206

More than one Kernel in Compute Shader

$
0
0
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 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?.

Viewing all articles
Browse latest Browse all 206

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>