Last active
January 2, 2020 14:33
-
-
Save Stektpotet/7b39fbf666837497c99db1099d885015 to your computer and use it in GitHub Desktop.
Compute Shader variations
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
NOTE - resolution (xyz) must be a multiple of numThreads (xyz)