Search Unity

  1. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question FPS drop when transferring data to the shader

Discussion in 'Shaders' started by Zimaell, Dec 3, 2023.

  1. Zimaell

    Zimaell

    Joined:
    May 7, 2020
    Posts:
    399
    good afternoon everyone, tell me how to correctly transfer data to the shader. The situation is the following - I need to select part of the terrain, for this, when selecting with the mouse, I transfer the coordinates to the shader, 4 variables (2 edges of the frame)
    (from script)
    Code (CSharp):
    1. TerrainMaterial.SetVector("_SelArea", new Vector4(z0, x0, z1, x1));
    (in shader)
    Code (CSharp):
    1. Properties{
    2.    ...........
    3.    [HideInInspector] _SelArea("Selection Area", Float) = (1000, 1000, 1000, 1000)
    4.    }
    5. CBUFFER_START(UnityPerMaterial)
    6.    ...........
    7.    float4 _SelArea;
    8. CBUFFER_END
    9. .........
    10. if(pos.x + 0.5 >= _SelArea.y && pos.x - 0.5 <= _SelArea.w && pos.z + 0.5 >= _SelArea.x && pos.z - 0.5 <= _SelArea.z){
    11.    color = lerp(color, _ColorEdit, _ColorEdit.a);
    visually I get the desired result, the area is highlighted in the desired color, but when adjusting the frame, that is, changing the coordinates and transferring them to the shader, the FPS drops by 2-3 times, and if you also move the camera, everything happens in very large jerks.

    What's the best way to pass coordinates to the shader?
     
  2. aleksandrk

    aleksandrk

    Unity Technologies

    Joined:
    Jul 3, 2017
    Posts:
    2,956
    This should be a
    Vector
    , not a float.
     
    Zimaell likes this.
  3. Zimaell

    Zimaell

    Joined:
    May 7, 2020
    Posts:
    399
    Well, firstly, it already works (probably it gets better during compilation), and secondly, it still doesn’t change the situation, the performance drop is the same...
     
  4. aleksandrk

    aleksandrk

    Unity Technologies

    Joined:
    Jul 3, 2017
    Posts:
    2,956
    Are you sure it's this single change that introduced the performance drop? What's the HW you're running this on?
     
    Zimaell likes this.
  5. Zimaell

    Zimaell

    Joined:
    May 7, 2020
    Posts:
    399
    Change Float to Vector did not lead to any changes.

    I conducted a series of tests and settled on the fact that a sharp drop in FPS occurs precisely during the transfer of data to the shader. Initially, I transferred the value every frame since the adjustment came from mouse movement, then I set a limit on the transfer of data only when the coordinates change and at intervals of 0.2 seconds, but even with this, the FPS drops precisely during the data transfer, I note that I even disabled the use of these variables in the shader, all the same, but I don’t know how else to transfer coordinates to the shader...
     
  6. aleksandrk

    aleksandrk

    Unity Technologies

    Joined:
    Jul 3, 2017
    Posts:
    2,956
    You mean this call?
     
    Zimaell likes this.
  7. aleksandrk

    aleksandrk

    Unity Technologies

    Joined:
    Jul 3, 2017
    Posts:
    2,956
  8. Zimaell

    Zimaell

    Joined:
    May 7, 2020
    Posts:
    399
    yes
    It did not help

    I think I can guess what the problem might be, although I don’t know how to solve it, I’ll try to describe the situation: I have 144 small terrains (64x64 and resolution 512x512), this is done so that I can edit them during the game (large terrains are expensive), all terrains have the same material, it is to this material that I send this data so that a certain area is highlighted, I think this is the problem, but how to approach the solution from the other side....
     
  9. aleksandrk

    aleksandrk

    Unity Technologies

    Joined:
    Jul 3, 2017
    Posts:
    2,956
    Please use the profiler to understand what's the cause of the performance problem.
     
    Zimaell likes this.
  10. Zimaell

    Zimaell

    Joined:
    May 7, 2020
    Posts:
    399
    I tried to understand what was going on in the profiler, but I just can’t figure it out, there’s too many things...

    Here I show you in screenshots, maybe you can tell me something...
     

    Attached Files:

    • 2.jpg
      2.jpg
      File size:
      285.3 KB
      Views:
      14
    • 3.jpg
      3.jpg
      File size:
      199.2 KB
      Views:
      14
    • 4.jpg
      4.jpg
      File size:
      229.3 KB
      Views:
      14
    • 5.jpg
      5.jpg
      File size:
      401 KB
      Views:
      14
  11. aleksandrk

    aleksandrk

    Unity Technologies

    Joined:
    Jul 3, 2017
    Posts:
    2,956
    I don't see any perf problem on these screenshots.
    Also, Editor is usually not indicative of the built application performance. Please check the built player.
     
    Zimaell likes this.
  12. Zimaell

    Zimaell

    Joined:
    May 7, 2020
    Posts:
    399
    here is the profiler during transfer to the shader
     

    Attached Files:

    • 2.jpg
      2.jpg
      File size:
      368.8 KB
      Views:
      15
  13. aleksandrk

    aleksandrk

    Unity Technologies

    Joined:
    Jul 3, 2017
    Posts:
    2,956
    This is still play mode in Editor, not the player profile.
     
    Zimaell likes this.
  14. Zimaell

    Zimaell

    Joined:
    May 7, 2020
    Posts:
    399
    Please explain what exactly needs to be shown, where to click which menu?
    maybe I misunderstand what to watch...
     
  15. aleksandrk

    aleksandrk

    Unity Technologies

    Joined:
    Jul 3, 2017
    Posts:
    2,956
    You capture the profile from pressing the Play button in the Editor. I'm suggesting to do an actual build and profile that instead.
     
    Zimaell likes this.
  16. Zimaell

    Zimaell

    Joined:
    May 7, 2020
    Posts:
    399
    thanks, but I solved this problem in a different way, I created one texture for a 768x768 material (mask) and mark the selection in the pixels of this mask, dynamically taking into account the selected area, in general it works much better, there are no more jumps and drops in performance...
     
    aleksandrk likes this.