Skip to content

Instantly share code, notes, and snippets.

@Stektpotet
Last active January 2, 2020 14:33
Show Gist options
  • Save Stektpotet/7b39fbf666837497c99db1099d885015 to your computer and use it in GitHub Desktop.
Save Stektpotet/7b39fbf666837497c99db1099d885015 to your computer and use it in GitHub Desktop.
Compute Shader variations
//named with '.hlsl' extension to get highlighting, it's really a '.compute'-file
#pragma kernel CSMain
RWTexture3D<float4> Result;
// if Shader Model doesn't support kernel threadgroups in the Z axis
// organize the threads differently
#if SHADER_TARGET < 40
[numthreads(16, 16, 1)]
#else
[numthreads(8, 8, 8)]
#endif
void CSMain(uint3 id : SV_DispatchThreadID)
{
Result[id] = Compute();
}
using UnityEngine;
using System;
class SomethingCompute : MonoBehaviour
{
[SerializeField] private Vector3Int resolution;
[SerializeField] private ComputeShader shader;
private uint
numThreadsX,
numThreadsY,
numThreadsZ;
private int kernel;
void Awake()
{
kernel = shader.FindKernel("CSMain");
shader.GetKernelThreadGroupSizes(kernel, out numThreadsX, out numThreadsY, out numThreadsZ);
}
void Dispatch()
{
Debug.LogFormat("Dispatching kernel with groups of size ({0}, {1}, {2})", numThreadsX, numThreadsY, numThreadsZ);
shader.Dispatch(kernel, resolution.x / (int)numThreadsX, resolution.y / (int)numThreadsY, resolution.z / (int)numThreadsZ);
}
}
@Stektpotet
Copy link
Author

NOTE - resolution (xyz) must be a multiple of numThreads (xyz)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment