Developer knowledge network · moderated exchange

مجتمع الكود غير الموثوق به

أبحاث المطورين، مجتمع الهندسة العكسية والترميز

Knowledge indexيعيش
4Categories
919Threads
2.8Kدعامات
Tutorial

Writing High-Performance Compute Shaders in HLSL for 1,000,000 Particle Physics Simulation [StackOverflow Architecture Guide]

shader_magician
HLSL / GLSL Dev
MEMBER
مندوب: 176
تاريخ الانضمام: Jan 2021
دعامات: 8
شكرًا: 32
1 months ago · Jul 11, 2026 7:40 AM
#1

Simulating 1M particles on GPU compute shader threads in HLSL:

HLSL
struct Particle { float3 position; float3 velocity; float life; };
RWStructuredBuffer<Particle> Particles : register(u0);

[numthreads(256, 1, 1)]
void CSMain(uint3 id : SV_DispatchThreadID)
{
    if (id.x >= 1000000) return;
    Particle p = Particles[id.x];
    p.velocity += float3(0, -9.81, 0) * 0.016;
    p.position += p.velocity * 0.016;
    p.life -= 0.016;
    Particles[id.x] = p;
}

Dispatched via Dispatch(1000000 / 256 + 1, 1, 1). Simulates 1,000,000 particles in < 0.4 milliseconds on modern GPUs!

graphics_pipeline_pro
DirectX / Vulkan Engineer
MEMBER
مندوب: 172
تاريخ الانضمام: Sep 2018
دعامات: 10
شكرًا: 51
1 months ago · Jul 11, 2026 12:41 PM
#2

numthreads(256, 1, 1) provides optimal warp/wavefront occupancy across NVIDIA (32-thread warps) and AMD (64-thread wavefronts).

simd_vector_ace
SIMD & Intrinsics
MEMBER
مندوب: 151
تاريخ الانضمام: May 2020
دعامات: 8
شكرًا: 36
1 months ago · Jul 11, 2026 4:35 PM
#3

Direct compute simulation avoids reading particle buffers back to CPU RAM across PCIe.