Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
This post has been wikified, any user with enough reputation can edit it.
avatar image
0
Question by kpo · Jun 08, 2014 at 03:11 PM · shadertexturecompute shaderhlsldirectx 11

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

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
1

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image laurentlavigne · Jan 16, 2018 at 02:32 AM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

22 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges