Developer knowledge network · moderated exchange

UnreliableCode қауымдастығы

Әзірлеушілерді зерттеу, кері инженерия және кодтау қауымдастығы

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 ай бұрын · 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 ай бұрын · 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 ай бұрын · Jul 11, 2026 4:35 PM
#3

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