From 21eb9bbf529b085317094295b9e3af5d06b38e83 Mon Sep 17 00:00:00 2001 From: BoyBaykiller Date: Tue, 19 Mar 2024 21:37:11 +0100 Subject: [PATCH] update to .net8, allow for more than 67mil particles (use native-int) --- .../Newtonian-Particle-Simulator.csproj | 2 +- .../res/shaders/particles/vertex.glsl | 2 + .../src/MainWindow.cs | 2 +- Newtonian-Particle-Simulator/src/Particle.cs | 4 +- .../src/Render/Objects/BufferObject.cs | 48 ++++++++----------- .../src/Render/ParticleSimulator.cs | 26 +++++----- 6 files changed, 38 insertions(+), 46 deletions(-) diff --git a/Newtonian-Particle-Simulator/Newtonian-Particle-Simulator.csproj b/Newtonian-Particle-Simulator/Newtonian-Particle-Simulator.csproj index c6d0bd2..e858937 100644 --- a/Newtonian-Particle-Simulator/Newtonian-Particle-Simulator.csproj +++ b/Newtonian-Particle-Simulator/Newtonian-Particle-Simulator.csproj @@ -2,7 +2,7 @@ Exe - net7.0 + net8.0 Newtonian_Particle_Simulator AnyCPU;x64 diff --git a/Newtonian-Particle-Simulator/res/shaders/particles/vertex.glsl b/Newtonian-Particle-Simulator/res/shaders/particles/vertex.glsl index 31fe48a..49837c4 100644 --- a/Newtonian-Particle-Simulator/res/shaders/particles/vertex.glsl +++ b/Newtonian-Particle-Simulator/res/shaders/particles/vertex.glsl @@ -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 diff --git a/Newtonian-Particle-Simulator/src/MainWindow.cs b/Newtonian-Particle-Simulator/src/MainWindow.cs index e3b3b4c..0117cef 100644 --- a/Newtonian-Particle-Simulator/src/MainWindow.cs +++ b/Newtonian-Particle-Simulator/src/MainWindow.cs @@ -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); diff --git a/Newtonian-Particle-Simulator/src/Particle.cs b/Newtonian-Particle-Simulator/src/Particle.cs index 6842469..c9693fd 100644 --- a/Newtonian-Particle-Simulator/src/Particle.cs +++ b/Newtonian-Particle-Simulator/src/Particle.cs @@ -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; } } diff --git a/Newtonian-Particle-Simulator/src/Render/Objects/BufferObject.cs b/Newtonian-Particle-Simulator/src/Render/Objects/BufferObject.cs index 65e324e..a0feffa 100644 --- a/Newtonian-Particle-Simulator/src/Render/Objects/BufferObject.cs +++ b/Newtonian-Particle-Simulator/src/Render/Objects/BufferObject.cs @@ -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) { @@ -30,74 +30,64 @@ public void Bind(BufferTarget bufferTarget) GL.BindBuffer(bufferTarget, ID); } - /// - /// Overrides all of this buffers memory with 0 - /// - public void Reset() + public void SubData(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(int offset, int size, T data) where T : struct - { - GL.NamedBufferSubData(ID, (IntPtr)offset, size, ref data); - } - public void SubData(int offset, int size, T[] data) where T : struct + public void SubData(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(int size, T data, BufferUsageHint bufferUsageHint) where T : struct + public void MutableAllocate(nint size, T data, BufferUsageHint bufferUsageHint) where T : struct { GL.NamedBufferData(ID, size, ref data, bufferUsageHint); Size = size; } - public void MutableAllocate(int size, T[] data, BufferUsageHint bufferUsageHint) where T : struct + public void MutableAllocate(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(int size, T data, BufferStorageFlags bufferStorageFlags) where T : struct + public void ImmutableAllocate(nint size, T data, BufferStorageFlags bufferStorageFlags) where T : struct { GL.NamedBufferStorage(ID, size, ref data, bufferStorageFlags); Size = size; } - public void ImmutableAllocate(int size, T[] data, BufferStorageFlags bufferStorageFlags) where T : struct + public void ImmutableAllocate(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(int offset, int size, out T data) where T : struct + public void GetSubData(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(int offset, int size, T[] data) where T : struct + public void GetSubData(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() diff --git a/Newtonian-Particle-Simulator/src/Render/ParticleSimulator.cs b/Newtonian-Particle-Simulator/src/Render/ParticleSimulator.cs index 3ad1003..82f4349 100644 --- a/Newtonian-Particle-Simulator/src/Render/ParticleSimulator.cs +++ b/Newtonian-Particle-Simulator/src/Render/ParticleSimulator.cs @@ -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() * NumParticles, particles, BufferStorageFlags.DynamicStorageBit); + particleBuffer = new BufferObject(BufferRangeTarget.ShaderStorageBuffer, 0); + particleBuffer.ImmutableAllocate(sizeof(Particle) * (nint)NumParticles, particles, BufferStorageFlags.DynamicStorageBit); IsRunning = true; } @@ -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); @@ -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)