Search Unity

Shader Complexity

Discussion in 'General Graphics' started by FuzzyShan, Jun 25, 2018.

  1. FuzzyShan

    FuzzyShan

    Joined:
    Jul 30, 2012
    Posts:
    182
    https://docs.unrealengine.com/en-us/Engine/UI/LevelEditor/Viewports/ViewModes#shadercomplexity

    Hi, I want to create something like this, my knowledge with creating something like this is limited. So I was hoping some experts in Unity would able to answer my questions about this.

    So my idea creating this for unity would be
    1. getting all the materials and its shader currently been rendered on the screen. So I am thinking about getting all the mesh renderer that's visible then iterate through all the material to get all the shaders.
    2. after getting all the shaders... is there a way to determine the instructions count for each shader during runtime? if not I guess I would have to create an asset that's keep track all the shader's instruction count for different platforms...
    3. after getting all the instructions I will then need to write to different material using a special shader which final fragment color is returned with the number of instructions count that I assume should be normal(is there a way to calculate on current platform what is the normal instruction count?).
    4. lastly, I will then render the final texture to camera.
     
  2. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,342
    This is the tripping point you'll have. Unity does not provide a way to do this. You would have to select each shader by hand and click on the "Compiled Shader" button, then find the instruction count in the txt file that generates and copy that into another file by hand. There is no way to do this from code!

    It should also be noted that Unreal's implementation is also very approximate, and really only good for getting a rough idea of where you have overdraw problems. It's totally wrong for opaque objects!
     
  3. FuzzyShan

    FuzzyShan

    Joined:
    Jul 30, 2012
    Posts:
    182
    Hi, I totally understand that, having 2 different functions would mean totally different processing speed, however it is still good to have an estimate, maybe once the system is there, I could get more information and go from there, I just think this is very good for visually trying to findout what's going on, maybe if framedebugger exposed runtime API, i would able to do more about the informations. I think one key is even if I manually compiled those, how do i get the instruction count?
     
  4. abegue

    abegue

    Joined:
    Jan 28, 2019
    Posts:
    24
    Making some post necromancy here, since I would like to make the same thing. Perhaps, some stuff has been changed (with evolutions of URP/HDRP) around shader compiler API.

    From what I found, the only thing accessible is the function to compile and open in VS (what the "Compile and show" button does). However, being able to open the file in VS is not really useful to make some treatment on the shader.
    Or maybe there is a way to communicate with VS to get the opened file after the API call?

    Then, the solution should be to use compilers according to your platform. On UE4, they use the offline shader compiler fxc (available from
    Program Files/Windows Kits/10/Bin/10.0.17763.0[or whatever version]/x64/fxc.exe
    ).

    Following usage example from here, we should be able to compile and get the cycle count for vertex shader and pixel shader. That's great!

    However, we have to feed this executable correctly. For that, we must have the complete shader and names of entry functions (the #pragma vertex and #pragma frag).

    And here comes the issue. How can I get the shader *after* Unity made all its custom processing (removing meta data, adding the "#include", adding the "#defines" for specific variant) and generate some raw HLSL?
    Can I get it from the material since there are kind of responsible of the shader variant used?

    Well, does someone have any idea or trick to be able to get cycle count?
     
  5. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,342
    Not really, no. These systems are basically unchanged.
     
  6. LUCKY_DUCK

    LUCKY_DUCK

    Joined:
    Feb 2, 2023
    Posts:
    1
    Maybe there is a way to accomplish alike function in Unity Source code instead of calling Unity Api.