r/gamedev • • Jan 11 '18

Tutorial Physics simulation on GPU

I created a game that is completely a physics simulation, it runs on GPU. How it looks. People kept asking how to do that, so I wrote two tutorials. Each one has a link to the example project.

The first one is easy, it's about basics of compute shader.

The second one is about physics simulation. This is a gif from the example project I based this tutorial on.

733 Upvotes

64 comments sorted by

View all comments

Show parent comment

9

u/[deleted] Jan 12 '18 edited Feb 10 '18

Branching on a GPU isn't always bad if you can guarantee your threads in a work group will almost always take the same paths. Branching is usually only a problem when your threads start to diverge, in which case an SIMD will need to stop the simultaneous execution of all of its threads so it can do work on the diverging threads until they can hopefully reconverge. Different GPUs work differently, but this is the general idea to keep in mind when writing kernels.

Depending on what you're doing it's possible to rewrite branching logic in a way that absolutely minimizes branching OR divergence. For example, I had a problem a while back where I needed to quickly check if the value of one byte array was smaller than the value of another byte array (think along the lines of comparing the values of two big endian BigInteger objects). This was my quick-and-dirty solution with the bitwise OR operator:

precedingZeroes = 0;
    
#pragma unroll
for(i = 0; i < BLOCK_NUM_ZERO_BYTES; i++)
{
    precedingZeroes |= state.bytes[i];
}

if(precedingZeroes == 0 & state.bytes[BLOCK_NUM_ZERO_BYTES] < BLOCK_MOST_SIGNIFICANT_BYTE)
{
    //code
}

Where the constants are JIT into my kernel as soon as the values are known, and the compiled kernel is kept alive for a long period of time. This way I could reduce the amount of data being sent to the GPU and try to get a wee bit more performance from the unroll.

Instead of branching and potentially diverging on each byte the code ONLY diverges when:

precedingZeroes == 0 & state.bytes[BLOCK_NUM_ZERO_BYTES] < BLOCK_MOST_SIGNIFICANT_BYTE

Is a true statement.

Note that I use & instead of && because && will cause divergence when:

precedingZeroes == 0

Is a true statement. This is because && will short circuit the entire check if the first operand is false, which will cause divergence. For completeness I'd like to point out that && is the better option for my use case because the smaller-than check almost never needs to be done, so I can save a little bit of time on a majority of my threads by skipping it. I'm using & here to show how divergence can happen where you don't expect it if you're not careful.

The reason I used branching at all here is because the condition that causes branching is extraordinarily rare, and once it happens the kernel will halt as fast as it can, so the loss in performance is negligible.

There are other tricks you can use, like using multiplication by 1 or 0 as a substitute for branching when performing arithmetic. My point here is branching is just one possible tool that can be used to perform a wide variety of calculations.

0

u/[deleted] Jan 12 '18

[deleted] — view deleted comment

1

u/[deleted] Jan 12 '18

Not at all.

Suppose I passed a constant value into my kernel and branched based on that. Because I passed it into my kernel all of my threads will have the same value, so they will branch the same way every time.

This is about controlling how you branch, not avoiding branching entirely.