1030% faster comp shader
This commit is contained in:
parent
47ca1265e6
commit
9ab7191c95
2 changed files with 30 additions and 33 deletions
|
|
@ -1,7 +1,7 @@
|
||||||
#[compute]
|
#[compute]
|
||||||
#version 450
|
#version 450
|
||||||
// Invocations in the (x, y, z) dimension
|
// Invocations in the (x, y, z) dimension
|
||||||
layout(local_size_x = 1000, local_size_y = 1, local_size_z = 1) in;
|
layout(local_size_x = 25, local_size_y = 1, local_size_z = 1) in;
|
||||||
|
|
||||||
// A binding to the buffer we create in our script
|
// A binding to the buffer we create in our script
|
||||||
layout(set = 0, binding = 0, std430) restrict buffer PointBuffer {
|
layout(set = 0, binding = 0, std430) restrict buffer PointBuffer {
|
||||||
|
|
|
||||||
|
|
@ -101,6 +101,9 @@ public class PlanetHelper
|
||||||
public Oct Octree = new Oct();
|
public Oct Octree = new Oct();
|
||||||
|
|
||||||
private Curve _remapCurve;
|
private Curve _remapCurve;
|
||||||
|
private RenderingDevice _rd;
|
||||||
|
private Rid _shader;
|
||||||
|
|
||||||
public PlanetHelper(MeshInstance3D meshInstance, TextureRect textureRect, Curve remapCurve)
|
public PlanetHelper(MeshInstance3D meshInstance, TextureRect textureRect, Curve remapCurve)
|
||||||
{
|
{
|
||||||
_meshInstance = meshInstance;
|
_meshInstance = meshInstance;
|
||||||
|
|
@ -132,6 +135,11 @@ public class PlanetHelper
|
||||||
textureShaderMaterial.SetShaderParameter("mode", 1);
|
textureShaderMaterial.SetShaderParameter("mode", 1);
|
||||||
}
|
}
|
||||||
UpdateMesh();
|
UpdateMesh();
|
||||||
|
|
||||||
|
_rd = RenderingServer.CreateLocalRenderingDevice();
|
||||||
|
var shaderFile = GD.Load<RDShaderFile>("res://shaders/compute/PlateExpansion.glsl");
|
||||||
|
var shaderBytecode = shaderFile.GetSpirV();
|
||||||
|
_shader = _rd.ShaderCreateFromSpirV(shaderBytecode);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void InitializeGeneration()
|
public void InitializeGeneration()
|
||||||
|
|
@ -295,18 +303,6 @@ public class PlanetHelper
|
||||||
|
|
||||||
public void PlateGeneration()
|
public void PlateGeneration()
|
||||||
{
|
{
|
||||||
var rd = RenderingServer.CreateLocalRenderingDevice();
|
|
||||||
var shaderFile = GD.Load<RDShaderFile>("res://shaders/compute/PlateExpansion.glsl");
|
|
||||||
GD.Print(shaderFile.BaseError);
|
|
||||||
var shaderBytecode = shaderFile.GetSpirV();
|
|
||||||
GD.Print(shaderBytecode.CompileErrorCompute);
|
|
||||||
GD.Print(shaderBytecode.CompileErrorFragment);
|
|
||||||
GD.Print(shaderBytecode.CompileErrorVertex);
|
|
||||||
GD.Print(shaderBytecode.CompileErrorTesselationEvaluation);
|
|
||||||
GD.Print(shaderBytecode.CompileErrorTesselationControl);
|
|
||||||
var shader = rd.ShaderCreateFromSpirV(shaderBytecode);
|
|
||||||
GD.Print(shader.IsValid);
|
|
||||||
|
|
||||||
// Prepare our data. We use floats in the shader, so we need 32 bit.
|
// Prepare our data. We use floats in the shader, so we need 32 bit.
|
||||||
int[] points = Enumerable.Range(0, Mdt.GetVertexCount()).ToArray();
|
int[] points = Enumerable.Range(0, Mdt.GetVertexCount()).ToArray();
|
||||||
var sizePoints = points.Length * sizeof(int);
|
var sizePoints = points.Length * sizeof(int);
|
||||||
|
|
@ -330,7 +326,7 @@ public class PlanetHelper
|
||||||
UniformType = RenderingDevice.UniformType.StorageBuffer,
|
UniformType = RenderingDevice.UniformType.StorageBuffer,
|
||||||
Binding = 0
|
Binding = 0
|
||||||
};
|
};
|
||||||
var pointBuffer = rd.StorageBufferCreate((uint)pointBytes.Length, pointBytes);
|
var pointBuffer = _rd.StorageBufferCreate((uint)pointBytes.Length, pointBytes);
|
||||||
pointUniform.AddId(pointBuffer);
|
pointUniform.AddId(pointBuffer);
|
||||||
|
|
||||||
var neighborUniform = new RDUniform
|
var neighborUniform = new RDUniform
|
||||||
|
|
@ -338,7 +334,7 @@ public class PlanetHelper
|
||||||
UniformType = RenderingDevice.UniformType.StorageBuffer,
|
UniformType = RenderingDevice.UniformType.StorageBuffer,
|
||||||
Binding = 1
|
Binding = 1
|
||||||
};
|
};
|
||||||
var neighborBuffer = rd.StorageBufferCreate((uint)neighborBytes.Length, neighborBytes);
|
var neighborBuffer = _rd.StorageBufferCreate((uint)neighborBytes.Length, neighborBytes);
|
||||||
neighborUniform.AddId(neighborBuffer);
|
neighborUniform.AddId(neighborBuffer);
|
||||||
|
|
||||||
var plateUniform = new RDUniform
|
var plateUniform = new RDUniform
|
||||||
|
|
@ -346,23 +342,23 @@ public class PlanetHelper
|
||||||
UniformType = RenderingDevice.UniformType.StorageBuffer,
|
UniformType = RenderingDevice.UniformType.StorageBuffer,
|
||||||
Binding = 2
|
Binding = 2
|
||||||
};
|
};
|
||||||
var plateBuffer = rd.StorageBufferCreate((uint)plateBytes.Length, plateBytes);
|
var plateBuffer = _rd.StorageBufferCreate((uint)plateBytes.Length, plateBytes);
|
||||||
plateUniform.AddId(plateBuffer);
|
plateUniform.AddId(plateBuffer);
|
||||||
|
|
||||||
var uniformSet = rd.UniformSetCreate([pointUniform, neighborUniform, plateUniform], shader, 0);
|
var uniformSet = _rd.UniformSetCreate([pointUniform, neighborUniform, plateUniform], _shader, 0);
|
||||||
|
|
||||||
// Create a compute pipeline
|
// Create a compute pipeline
|
||||||
var pipeline = rd.ComputePipelineCreate(shader);
|
var pipeline = _rd.ComputePipelineCreate(_shader);
|
||||||
var computeList = rd.ComputeListBegin();
|
var computeList = _rd.ComputeListBegin();
|
||||||
rd.ComputeListBindComputePipeline(computeList, pipeline);
|
_rd.ComputeListBindComputePipeline(computeList, pipeline);
|
||||||
rd.ComputeListBindUniformSet(computeList, uniformSet, 0);
|
_rd.ComputeListBindUniformSet(computeList, uniformSet, 0);
|
||||||
uint xgroups = (uint)(Mathf.Ceil((double)points.Length / 1000.0));
|
uint xgroups = (uint)(Mathf.Ceil((double)points.Length / 25.0));
|
||||||
rd.ComputeListDispatch(computeList, xGroups: xgroups, yGroups: 50, zGroups: 1);
|
_rd.ComputeListDispatch(computeList, xGroups: xgroups, yGroups: 50, zGroups: 1);
|
||||||
rd.ComputeListEnd();
|
_rd.ComputeListEnd();
|
||||||
rd.Submit();
|
_rd.Submit();
|
||||||
rd.Sync();
|
_rd.Sync();
|
||||||
// Read back the data from the buffers
|
// Read back the data from the buffers
|
||||||
var outputBytes = rd.BufferGetData(plateBuffer);
|
var outputBytes = _rd.BufferGetData(plateBuffer);
|
||||||
var output = new int[plateids.Length];
|
var output = new int[plateids.Length];
|
||||||
Buffer.BlockCopy(outputBytes, 0, output, 0, plateBytes.Length);
|
Buffer.BlockCopy(outputBytes, 0, output, 0, plateBytes.Length);
|
||||||
|
|
||||||
|
|
@ -376,11 +372,12 @@ public class PlanetHelper
|
||||||
}
|
}
|
||||||
index++;
|
index++;
|
||||||
}
|
}
|
||||||
rd.FreeRid(pointBuffer);
|
|
||||||
rd.FreeRid(plateBuffer);
|
_rd.FreeRid(pipeline);
|
||||||
rd.FreeRid(neighborBuffer);
|
_rd.FreeRid(uniformSet);
|
||||||
rd.FreeRid(pipeline);
|
_rd.FreeRid(pointBuffer);
|
||||||
rd.FreeRid(uniformSet);
|
_rd.FreeRid(plateBuffer);
|
||||||
|
_rd.FreeRid(neighborBuffer);
|
||||||
|
|
||||||
AssignOceanPlates(Plates);
|
AssignOceanPlates(Plates);
|
||||||
CompleteStage();
|
CompleteStage();
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue