Why are if statements in shaders heavily discouraged?

Поділитися
Вставка
  • Опубліковано 22 лис 2023
  • This video was sponsored by Brilliant
    To try everything Brilliant has to offer-free-for a full 30 days, visit brilliant.org/VoxelRifts . The first 200 of you will get 20% off Brilliant’s annual premium subscription.
    If you've done shader programming before, you might've heard people say that you shouldn't use if statements or any other conditional branching statements in shaders. However, it might not be obvious why exactly this is the case, especially because people don't say the same for regular CPU side programming. In this video, we go through why this is the case, along with covering why GPUs are so much faster than CPUs at certain tasks.
    Discord: / discord
    #gpu #branching #lowlevel

КОМЕНТАРІ • 18

  • @voxelrifts
    @voxelrifts  6 місяців тому +4

    To try everything Brilliant has to offer-free-for a full 30 days, visit brilliant.org/VoxelRifts . The first 200 of you will get 20% off Brilliant’s annual premium subscription.

  • @DomskiPlays
    @DomskiPlays 6 місяців тому +7

    I didn't know anything about GPUs but this video has such depth yet still explains concepts so well I think I understood about all of it! Awesome man!

  • @JoseRomagueraM
    @JoseRomagueraM 6 місяців тому +5

    Thats a great introduction of how GPUs work

  • @pronaysarkar2356
    @pronaysarkar2356 6 місяців тому +2

    nicely explained!! And congratulations on your first sponsor! :)

  • @pitche
    @pitche 6 місяців тому +4

    Congrats on your first sponsor! :)

  • @oh-facts
    @oh-facts 6 місяців тому +3

    Very cool, thank you!

  • @truck.-kun.
    @truck.-kun. 4 місяці тому +1

    This is like intro to High Performance Computing! This video is so informative. loved it

  • @k_otey
    @k_otey 6 місяців тому +9

    sponsor 😎😎

  • @David_Box
    @David_Box 6 місяців тому +1

    Regarding treating booleans like integers and optimising conditional statements this way, is it not true that in a lot of cases, the compiler can figure out the optimisation by itself and change your code as if you were doing it that? I know it is true in C, if not in glsl.

    • @voxelrifts
      @voxelrifts  6 місяців тому +3

      In most, if not all cases, yes. But it's always good to know *how* it's done

    • @David_Box
      @David_Box 6 місяців тому +1

      @@voxelrifts The reason I'm saying it is because in some instances the resulting machine code with the manual optimisation might actually be slower than the one where you let the compiler do it for you, because in the first case the compiler doesn't really know what you're attempting to do and refuses to do some other kinds of optimization as a result.
      Either way if statements on GPUs only really become an issue if the work being done in each case is wildly different (rather than just picking out a different value depending on the case), so yeah it should probably only be done for demonstration purposes.

  • @julianheinken4870
    @julianheinken4870 6 місяців тому +3

    It still depends however. I've seen shader code that was measurably faster with if-statement + the condition was per-pixel and not per-wavefront. Always profile! :)

    • @voxelrifts
      @voxelrifts  6 місяців тому +3

      Absolutely, always profile!

  • @someone-mh1bo
    @someone-mh1bo 6 місяців тому +6

    I just realized you have a accent.

    • @k_otey
      @k_otey 6 місяців тому +2

      yeah i think it sounds stronger in this particular video. probably just imagining things though

  • @luigidabro
    @luigidabro 5 місяців тому +1

    Is a ternary operator that just sets a value as bad as the other if statements?

  • @wrog268
    @wrog268 6 місяців тому

    does cuda have that too?