Skip to content

Simple class to speed up working with compute shaders in Unity

License

Notifications You must be signed in to change notification settings

cau777/Compute-Shader-Helper

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 

Repository files navigation

Compute-Shader-Helper

Simple class to speed up working with compute shaders in Unity

How to Use

  1. Create ComputeHelper object

ComputeHelper helper = new ComputeHelper(myComputeShader, kernelIndex);

  1. Set properties in the ComputeShader object

myComputeShader.SetInt("Count", 3);

  1. Create Buffers using the ComputeHelper object. There are 3 types of buffers:
  • Write buffer: writes the contents of the array to the Buffer in the shader

CreateWriteBuffer(string name, int size, int stride, Array reference)

helper.CreateWriteBuffer("Inputs", inputs.Length, sizeof(float), inputs);

  • Read buffer: reads the contents of the Buffer and stores them into the array after the shader is dispached

CreateReadWriteBuffer(string name, int size, int stride, Array reference)

helper.CreateReadBuffer("Outputs", outputs.Length, sizeof(float), outputs);

  • Read/Write buffer: writes the contents of the array to the Buffer in the shader and reads the contents of the Buffer and stores them into the array after the shader is dispached

CreateReadWriteBuffer(string name, int size, int stride, Array reference)

helper.CreateReadWriteBuffer("ThingToBeModified", thingsToBeModified.Length, sizeof(float), thingsToBeModified);

  1. Run the Shader, specifying the total thread count for x, y, z

RunShader(int x, int y, int z)

helper.RunShader(500, 1, 1); // The shader will run 500 times

After this command, the program retrieves data from the buffers, stores them into the arrays and releases all ComputeBuffers

About

Simple class to speed up working with compute shaders in Unity

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages