#Help using compute shaders
1 messages · Page 1 of 1 (latest)
The code:
// Create rendering device
var rd = RenderingServer.CreateLocalRenderingDevice();
Debug.Print("Creating Rendering Device");
// Compile Shader
var shaderFile = GD.Load<RDShaderFile>("res://shaders/shots_compute.glsl");
var shaderSpirV = shaderFile.GetSpirV();
Debug.Print("Got Shader Byte Code");
Debug.Print(shaderSpirV.ToString());
var shader = rd.ShaderCreateFromSpirV(shaderSpirV); // <--- Line 18 ---<
Debug.Print("Compiled Shader");
// Prepare our data
int[] input = {0};
var inputBytes = new byte[sizeof(int)];
Buffer.BlockCopy(input, 0, inputBytes, 0, inputBytes.Length);
Debug.Print("Preped Data");
// Create storage buffer
var buffer = rd.StorageBufferCreate((uint) inputBytes.Length, inputBytes);
Debug.Print("Created Buffer");
// Create uniform
var uniform = new RDUniform
{
UniformType = RenderingDevice.UniformType.StorageBuffer,
Binding = 0
};
uniform.AddId(buffer);
var uniformSet = rd.UniformSetCreate([uniform], shader, 0);
Debug.Print("Created Storage Buffer");
// Create shader pipeline
var pipeline = rd.ComputePipelineCreate(shader);
var computeList = rd.ComputeListBegin();
rd.ComputeListBindComputePipeline(computeList, pipeline);
rd.ComputeListBindUniformSet(computeList, uniformSet, 0);
rd.ComputeListDispatch(computeList, xGroups: (uint) interval, yGroups: 1, zGroups: 1);
rd.ComputeListEnd();
// Submit to GPU
rd.Submit();
// Wait for Sync. Only for testing
rd.Sync();
var outputBytes = rd.BufferGetData(buffer);
int[] output = new int[input.Length];
Buffer.BlockCopy(outputBytes, 0, output, 0, outputBytes.Length);
Debug.Print(output[0].ToString());
return output[0];