Welcome Guest [Log In] [Register]
We hope you enjoy your visit.


You're currently viewing the Ultimate 3D Community as a guest. This means that you can only read posts, but can not create posts or topics by yourself. To be able to post you need to register. Then you can participate in the community active and use many member-only features such as customizing your profile, sending personal messages, and voting in polls. Registration is simple, fast, and completely free.

Join our community!

If you are already a member please log in to your account to access all of our features:

Username:   Password:
Add Reply
Oop - if-less programming; For games
Topic Started: May 19 2018, 01:13 PM (35 Views)
Despellanion
Member Avatar
Forum God
[ *  *  *  *  *  * ]
Lately I've become more and more interested in programming without if-conditionals, switch statements and ternary operators. I can grasp the basic principle of "code as data", but how worthwhile is it for game development and is it a common practice?
Offline Profile Quote Post Goto Top
 
Dr. Best
Member Avatar
Administrator
[ *  *  *  *  *  * ]
If you write data-parallel code for SIMD architectures, especially for GPUs, you want to avoid all sorts of branching (if, for, while, switch, etc.) that could be divergent across threads. Threads run in groups (on NVIDIA GPUs they are called warps and consist of 32 threads). If one of these threads executes an instruction, all threads in the warp pay the cost for this instruction, whether they execute it or not.

Thus, under these circumstances it is a common practice. However, if you do not have thousands of threads running in parallel to process similar data, the benefits should be much smaller. Conditionals do incur some overhead. If you can avoid them without doing much more work overall, that is often a good idea. But don't go out of your way to avoid conditionals in code that is not performance sensitive.

Also, avoiding conditionals is one of very many things that you should be doing if you write cryptographic code that must not be susceptible to timing attacks.
Offline Profile Quote Post Goto Top
 
Despellanion
Member Avatar
Forum God
[ *  *  *  *  *  * ]
Thanks, that puts it into better perspective.
Also, there are some instances in game development where it's more or less impossible to not use conditionals - at least to my knowledge.
Offline Profile Quote Post Goto Top
 
Dr. Best
Member Avatar
Administrator
[ *  *  *  *  *  * ]
Nah, it's totally possible, just look at branchless Doom.

Quote:
 
The mov-only DOOM renders approximately one frame every 7 hours, so playing this version requires somewhat increased patience.
Offline Profile Quote Post Goto Top
 
Despellanion
Member Avatar
Forum God
[ *  *  *  *  *  * ]
Dr. Best
May 24 2018, 11:08 AM
Nah, it's totally possible, just look at branchless Doom.

Quote:
 
The mov-only DOOM renders approximately one frame every 7 hours, so playing this version requires somewhat increased patience.
So there's basically no upsides to it except that it runs super-safe?
Offline Profile Quote Post Goto Top
 
Dr. Best
Member Avatar
Administrator
[ *  *  *  *  *  * ]
Despellanion
May 26 2018, 12:22 AM
Dr. Best
May 24 2018, 11:08 AM
Nah, it's totally possible, just look at branchless Doom.

Quote:
 
The mov-only DOOM renders approximately one frame every 7 hours, so playing this version requires somewhat increased patience.
So there's basically no upsides to it except that it runs super-safe?
And it's funny :) .
Offline Profile Quote Post Goto Top
 
« Previous Topic · Basics · Next Topic »
Add Reply