Skip to content

Commit

Permalink
update to .net8, allow for more than 67mil particles (use native-int)
Browse files Browse the repository at this point in the history
  • Loading branch information
BoyBaykiller committed Mar 19, 2024
1 parent f18fb0f commit 21eb9bb
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net7.0</TargetFramework>
<TargetFramework>net8.0</TargetFramework>
<RootNamespace>Newtonian_Particle_Simulator</RootNamespace>
<Platforms>AnyCPU;x64</Platforms>
<ApplicationIcon />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ const float DRAG_COEF = log(0.998) * 176.0; // log(0.70303228048)
struct Particle
{
vec3 Position;
float _pad0;
vec3 Velocity;
float _pad1;
};

layout(std430, binding = 0) restrict buffer ParticlesSSBO
Expand Down
2 changes: 1 addition & 1 deletion Newtonian-Particle-Simulator/src/MainWindow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ protected override void OnLoad(EventArgs e)
Particle[] particles = new Particle[numParticles];
for (int i = 0; i < particles.Length; i++)
{
particles[i].Position = new Vector3((float)rng.NextDouble() * 100 - 50, (float)rng.NextDouble() * 100 - 50, -(float)rng.NextDouble() * 100);
particles[i].Position = new Vector3(rng.NextSingle() * 100 - 50, rng.NextSingle() * 100 - 50, -rng.NextSingle() * 100);
//particles[i].Position = Helper.RandomUnitVector(rng) * 50.0f;
}
particleSimulator = new ParticleSimulator(particles);
Expand Down
4 changes: 2 additions & 2 deletions Newtonian-Particle-Simulator/src/Particle.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ namespace Newtonian_Particle_Simulator
struct Particle
{
public Vector3 Position;
int pad;
private readonly int _pad0;
public Vector3 Velocity;
int pad1;
private readonly int _pad1;
}
}
48 changes: 19 additions & 29 deletions Newtonian-Particle-Simulator/src/Render/Objects/BufferObject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace Newtonian_Particle_Simulator.Render.Objects
public class BufferObject : IDisposable
{
public readonly int ID;
public int Size { get; private set; }
public nint Size { get; private set; }

public BufferObject(BufferRangeTarget bufferRangeTarget, int bindingIndex)
{
Expand All @@ -30,74 +30,64 @@ public void Bind(BufferTarget bufferTarget)
GL.BindBuffer(bufferTarget, ID);
}

/// <summary>
/// Overrides all of this buffers memory with 0
/// </summary>
public void Reset()
public void SubData<T>(nint offset, nint size, T data) where T : struct
{
IntPtr intPtr = Marshal.AllocHGlobal(Size);
GL.NamedBufferSubData(ID, IntPtr.Zero, Size, intPtr);
Marshal.FreeHGlobal(intPtr);
GL.NamedBufferSubData(ID, offset, size, ref data);
}

public void SubData<T>(int offset, int size, T data) where T : struct
{
GL.NamedBufferSubData(ID, (IntPtr)offset, size, ref data);
}
public void SubData<T>(int offset, int size, T[] data) where T : struct
public void SubData<T>(nint offset, nint size, T[] data) where T : struct
{
GL.NamedBufferSubData(ID, (IntPtr)offset, size, data);
GL.NamedBufferSubData(ID, offset, size, data);
}
public void SubData(int offset, int size, IntPtr data)
public void SubData(nint offset, nint size, IntPtr data)
{
GL.NamedBufferSubData(ID, (IntPtr)offset, size, data);
GL.NamedBufferSubData(ID, offset, size, data);
}

public void MutableAllocate<T>(int size, T data, BufferUsageHint bufferUsageHint) where T : struct
public void MutableAllocate<T>(nint size, T data, BufferUsageHint bufferUsageHint) where T : struct
{
GL.NamedBufferData(ID, size, ref data, bufferUsageHint);
Size = size;
}
public void MutableAllocate<T>(int size, T[] data, BufferUsageHint bufferUsageHint) where T : struct
public void MutableAllocate<T>(nint size, T[] data, BufferUsageHint bufferUsageHint) where T : struct
{
GL.NamedBufferData(ID, size, data, bufferUsageHint);
Size = size;
}
public void MutableAllocate(int size, IntPtr data, BufferUsageHint bufferUsageHint)
public void MutableAllocate(nint size, IntPtr data, BufferUsageHint bufferUsageHint)
{
GL.NamedBufferData(ID, size, data, bufferUsageHint);
Size = size;
}

public void ImmutableAllocate<T>(int size, T data, BufferStorageFlags bufferStorageFlags) where T : struct
public void ImmutableAllocate<T>(nint size, T data, BufferStorageFlags bufferStorageFlags) where T : struct
{
GL.NamedBufferStorage(ID, size, ref data, bufferStorageFlags);
Size = size;
}
public void ImmutableAllocate<T>(int size, T[] data, BufferStorageFlags bufferStorageFlags) where T : struct
public void ImmutableAllocate<T>(nint size, T[] data, BufferStorageFlags bufferStorageFlags) where T : struct
{
GL.NamedBufferStorage(ID, size, data, bufferStorageFlags);
Size = size;
}
public void ImmutableAllocate(int size, IntPtr data, BufferStorageFlags bufferStorageFlags)
public void ImmutableAllocate(nint size, IntPtr data, BufferStorageFlags bufferStorageFlags)
{
GL.NamedBufferStorage(ID, size, data, bufferStorageFlags);
Size = size;
}

public void GetSubData<T>(int offset, int size, out T data) where T : struct
public void GetSubData<T>(nint offset, nint size, out T data) where T : struct
{
data = new T();
GL.GetNamedBufferSubData(ID, (IntPtr)offset, size, ref data);
GL.GetNamedBufferSubData(ID, offset, size, ref data);
}
public void GetSubData<T>(int offset, int size, T[] data) where T : struct
public void GetSubData<T>(nint offset, nint size, T[] data) where T : struct
{
GL.GetNamedBufferSubData(ID, (IntPtr)offset, size, data);
GL.GetNamedBufferSubData(ID, offset, size, data);
}
public void GetSubData(int offset, int size, out IntPtr data)
public void GetSubData(nint offset, nint size, out IntPtr data)
{
data = Marshal.AllocHGlobal(size);
GL.GetNamedBufferSubData(ID, (IntPtr)offset, size, data);
GL.GetNamedBufferSubData(ID, offset, size, data);
}

public void Dispose()
Expand Down
26 changes: 13 additions & 13 deletions Newtonian-Particle-Simulator/src/Render/ParticleSimulator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,16 @@ namespace Newtonian_Particle_Simulator.Render
class ParticleSimulator
{
public readonly int NumParticles;
public readonly BufferObject ParticleBuffer;
public readonly ShaderProgram ShaderProgram;
public ParticleSimulator(Particle[] particles)
private readonly BufferObject particleBuffer;
private readonly ShaderProgram shaderProgram;
public unsafe ParticleSimulator(Particle[] particles)
{
NumParticles = particles.Length;

ShaderProgram = new ShaderProgram(new Shader(ShaderType.VertexShader, "res/shaders/particles/vertex.glsl".GetPathContent()), new Shader(ShaderType.FragmentShader, "res/shaders/particles/fragment.glsl".GetPathContent()));
shaderProgram = new ShaderProgram(new Shader(ShaderType.VertexShader, "res/shaders/particles/vertex.glsl".GetPathContent()), new Shader(ShaderType.FragmentShader, "res/shaders/particles/fragment.glsl".GetPathContent()));

ParticleBuffer = new BufferObject(BufferRangeTarget.ShaderStorageBuffer, 0);
ParticleBuffer.ImmutableAllocate(System.Runtime.CompilerServices.Unsafe.SizeOf<Particle>() * NumParticles, particles, BufferStorageFlags.DynamicStorageBit);
particleBuffer = new BufferObject(BufferRangeTarget.ShaderStorageBuffer, 0);
particleBuffer.ImmutableAllocate(sizeof(Particle) * (nint)NumParticles, particles, BufferStorageFlags.DynamicStorageBit);

IsRunning = true;
}
Expand All @@ -34,14 +34,14 @@ public bool IsRunning
set
{
_isRunning = value;
ShaderProgram.Upload(3, _isRunning ? 1.0f : 0.0f);
shaderProgram.Upload(3, _isRunning ? 1.0f : 0.0f);
}
}
public void Run(float dT)
{
GL.Clear(ClearBufferMask.ColorBufferBit);
ShaderProgram.Use();
ShaderProgram.Upload(0, dT);
shaderProgram.Use();
shaderProgram.Upload(0, dT);

GL.DrawArrays(PrimitiveType.Points, 0, NumParticles);
GL.MemoryBarrier(MemoryBarrierFlags.ShaderStorageBarrierBit);
Expand All @@ -58,17 +58,17 @@ public void ProcessInputs(GameWindow gameWindow, in Vector3 camPos, in Matrix4 v
Vector3 dir = GetWorldSpaceRay(projection.Inverted(), view.Inverted(), normalizedDeviceCoords);

Vector3 pointOfMass = camPos + dir * 25.0f;
ShaderProgram.Upload(1, pointOfMass);
ShaderProgram.Upload(2, 1.0f);
shaderProgram.Upload(1, pointOfMass);
shaderProgram.Upload(2, 1.0f);
}
else
ShaderProgram.Upload(2, 0.0f);
shaderProgram.Upload(2, 0.0f);
}

if (KeyboardManager.IsKeyTouched(Key.T))
IsRunning = !IsRunning;

ShaderProgram.Upload(4, view * projection);
shaderProgram.Upload(4, view * projection);
}

public static Vector3 GetWorldSpaceRay(Matrix4 inverseProjection, Matrix4 inverseView, Vector2 normalizedDeviceCoords)
Expand Down

0 comments on commit 21eb9bb

Please sign in to comment.