Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

How to count/show shader cycles or operations for shader profileing?

Discussion in 'Shaders' started by marcSP, Jan 24, 2019.

  1. marcSP

    marcSP

    Joined:
    Jan 13, 2016
    Posts:
    7
    Well, I think the headline is self explanatory...

    Is there any way to check how many cycles or math operations a specific shader is using?
    If possible...
    can you show the counts used on the current scene with its current conditions (fog, number of lights, etc)?

    I've been struggling with this for 2 days without any success...

    In the previous unity version (I'm using 2018.3.0f1 now) I was able to check some "math operations" once I selected the shader and clicked "compile and show code" on the inspector window. But with the current version, not any more :/

    but, anyway, that was a mess...

    So, I surprised to see that this is not a common topic when people is profileing, for instance, for mobile devices.

    Any information will be more than welcome :)
    thanks in advance!
     
  2. jbooth

    jbooth

    Joined:
    Jan 6, 2014
    Posts:
    5,445
    Well, to be frank, it’s just not that useful for general profiling. Reducing ALU instructions won’t matter much if your memory bound, etc. It ends up being another counter, like “polygon count”, which is mostly meaningless in actual context. Also, all instructions are not created equal; some might take one cycle while another a dozen.

    Better are profilers like the one in instruments, which can show you precisely how much time your shader is spending in each section of code, if it’s memory or ALU bound, etc.
     
  3. marcSP

    marcSP

    Joined:
    Jan 13, 2016
    Posts:
    7
    Rely appreciate your quick answer jbooth but... I (kind of) disagree ;)

    Even if it's true that a simple "counter" per se is not that usefull, it gives an excellent insight (specially for low-end phones) of which are the performance limits (generally speaking) of each device under certain (full-screen-shader) conditions.

    Besides, my problem (more specifically) is that I have no way to measure the difference of "perfomance consumption" (empirically speaking) between two shaders.

    Specially if those are so "obscure" like the "standard shader" from unity with all their "variants" & branches
    :p
     
  4. jbooth

    jbooth

    Joined:
    Jan 6, 2014
    Posts:
    5,445
    I think your asking for the wrong tool:

    Lets say we have two shaders, one has an ALU instruction count of 54, another of 33. Which is faster?

    You can't tell. The 33 one might be significantly slower than the 54 one, even if both are ALU bound and do no texture lookups.

    So your basically asking for something to give you a bunch of information which won't tell you which is faster, and will most likely lead you to wrong conclusions.

    One argument might be "Well, I want to see if I made the shader faster with my changes". But here too it can't tell you that. Because your code change might be less instructions, but the instructions your using might take longer to execute, even if they are again strictly ALU instructions and not something like moving some math to a LUT instead of computing it. And, on top of it, it's entirely platform dependent. An instruction which is fast on GCN might be slow on nvidia cards.

    If your use case is to compare the performance of two shaders to see which one is faster, and for whatever reason you don't want to use a GPU profiler to do this (which will give you very detailed information), then write a test case that draws both shaders over the screen some crazy number fo times and run both and compare the speed.

    People often want optimization to be simple "Keep it under this poly count", "Under this number of shader instructions", but if anyone tells you something like this, they are over simplifying the issue to the point where I question if they understand what they are doing.
     
  5. marcSP

    marcSP

    Joined:
    Jan 13, 2016
    Posts:
    7
    hehehe... well, I think you overexagerated a bit with your last sentence ;) but I understand what you mean.

    No worries, GPU profilers are one of the tools of my arsenal too, but I was surprised that unity didin't include a tool that can give us (even if its only a brief and bit biased) overview of what's happening inside the shader.
     
  6. jbooth

    jbooth

    Joined:
    Jan 6, 2014
    Posts:
    5,445
    You'll note that UE3 had this in their material editor, but removed it for UE4.
     
  7. aleksandrk

    aleksandrk

    Unity Technologies

    Joined:
    Jul 3, 2017
    Posts:
    2,845
    Mali offline shader compiler can give you the cycles. It will even tell you if you're arithmetic bound or texture bound. Obviously, only about Mali GPUs :)
     
    OlegPavlov likes this.
  8. marcSP

    marcSP

    Joined:
    Jan 13, 2016
    Posts:
    7
    Do you think it will be vaild even for the "standard" with all their includes and pre-predefined variables?
     
  9. aleksandrk

    aleksandrk

    Unity Technologies

    Joined:
    Jul 3, 2017
    Posts:
    2,845
    You need to compile it first :)
    Select the shader you need, click on the down arrow on "Compile and show code" button, select the target you want (I suppose GLES3x in this case), then click "Compile and show code" itself. It will output all the used variants in a text file. Copy one of those (within #ifdef VERTEX/#endif or #ifdef FRAGMENT/endif) into a separate text file and feed it to the mali compiler.
     
  10. marcSP

    marcSP

    Joined:
    Jan 13, 2016
    Posts:
    7
    BTW, it looks like, doesn't matter if I disable some of the platforms, it will always compile and show the same amount of variants.
     
  11. aleksandrk

    aleksandrk

    Unity Technologies

    Joined:
    Jul 3, 2017
    Posts:
    2,845
    That's not what I'm seeing when using this.
    Are you sure?
     
  12. marcSP

    marcSP

    Joined:
    Jan 13, 2016
    Posts:
    7
    Well, according to this... yes

    upload_2019-1-25_11-56-18.png

    Same amount of variants even if I reduce massively the number of platforms.

    BTW, I'm using the Unity v 2018.3.0f1
     

    Attached Files:

  13. aleksandrk

    aleksandrk

    Unity Technologies

    Joined:
    Jul 3, 2017
    Posts:
    2,845
    OK, got it. This one shows the amount of variants of the shader itself, not what gets actually compiled.
     
  14. ZeBraNS

    ZeBraNS

    Joined:
    Feb 21, 2015
    Posts:
    40
    @aleksandrk is the Mali offline shader compiler still relevant to testing URP shaders?
    I get an error - 'binding' qualifier is not allowed in language version 300 es

    Which other "easy to use" profiler do you recommend for testing the impact of various shaders created in Unity Shader Graph?
     
  15. aleksandrk

    aleksandrk

    Unity Technologies

    Joined:
    Jul 3, 2017
    Posts:
    2,845
    @ZeBraNS you can set the the version to 320 es manually
     
    vuphuonghoang88 likes this.
  16. ZeBraNS

    ZeBraNS

    Joined:
    Feb 21, 2015
    Posts:
    40
    Thank you very much aleksandrk, it works now.
     
    aleksandrk likes this.