by tlackemann · 4y ago · 0 comments
Shaders are one of those topics that you can easily get lost in. It was one of my more recent "you don't know what you don't know" topics. The idea that your code will run one time for each pixel on your screen, 60-144x a second (!), is mind-boggling. It's still hard for me to wrap my head around it sometimes and when I finally write something that compiles it feels like magic every time.
That means if you had a shader like:
if (pixelIndex % 2) {
longFunctionA();
} else {
longFunctionB();
}
It would actually take twice as long to run compared to every pixel calling the same function. Each core is executing a batch of pixels (a warp) that is evenly split between two completely different sections of code, so it has to alternate between each until they both finish.* Core might not be the exact right term, Nvidia calls them SMs and other GPU vendors have different names.