📻 Addendum: This would be the equivalent branchless shader: // Current pixel colour in texture vec4 pixelColour = texture(TEXTURE, UV); // Colour of neighbouring pixel in texture vec4 nextColour = texture(TEXTURE, UV + TEXTURE_PIXEL_SIZE * reflectionOffset); // Colour of screen-space refraction effect vec4 refractionColour = texture(SCREEN_TEXTURE, SCREEN_UV + SCREEN_PIXEL_SIZE * refractionOffset); // If the adjacent colour is transparent, use reflection, else use refraction vec4 colour = mix(reflectionColour, refractionColour, nextColour.a); // Now only display this shader if the current texture pixel is not transparent COLOR = colour * pixelColour.a; As discussed in the video, I'm not sure how much of a speed benefit this will provide, if any. GPU branching is a more complex topic than CPU branching. In addition, the number of instructions within this shader is low enough that it may make no difference between the number of clock cycles for a branched vs branchless case.
thanks, this was super useful. I couldn't find any other tutorial explaining this
This is really awesome stuff, thanks for making the video!
Glad you liked it!
📻 Addendum:
This would be the equivalent branchless shader:
// Current pixel colour in texture
vec4 pixelColour = texture(TEXTURE, UV);
// Colour of neighbouring pixel in texture
vec4 nextColour = texture(TEXTURE, UV + TEXTURE_PIXEL_SIZE * reflectionOffset);
// Colour of screen-space refraction effect
vec4 refractionColour = texture(SCREEN_TEXTURE, SCREEN_UV + SCREEN_PIXEL_SIZE * refractionOffset);
// If the adjacent colour is transparent, use reflection, else use refraction
vec4 colour = mix(reflectionColour, refractionColour, nextColour.a);
// Now only display this shader if the current texture pixel is not transparent
COLOR = colour * pixelColour.a;
As discussed in the video, I'm not sure how much of a speed benefit this will provide, if any. GPU branching is a more complex topic than CPU branching. In addition, the number of instructions within this shader is low enough that it may make no difference between the number of clock cycles for a branched vs branchless case.