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. The 2022.2 beta is now available for testing. To find out what's new, have a look at our 2022.2 feature highlights.
    Dismiss Notice
Dismiss Notice
Submit bug reports tagged with #Beta2022Win_Unity when you encounter unknown issues while testing the 2022.2 betas for a chance to win a year of Unity Pro. For more information, have a look at our Beta Sweepstakes Announcement.

CPU Performance Penalty for having separate objects in scene

Discussion in 'HDRP Ray Tracing' started by Camarent, Sep 13, 2021.

  1. Camarent

    Camarent

    Joined:
    Feb 19, 2014
    Posts:
    168
    Hi! I recently start to explore raytracing possibilities of HDRP and found one peculiarity that do not know how to handle.
    I enabled RTX and found out that Initilization step of HDRP become 3x bigger than before (from 2-2.5ms to 6.5-9.5ms). I expected that frame time increase but not in this place.
    I tested on the clean project and I manage to narrow what caused that. It was
    BuildRayTracingAccelerationStructure method. I checked the code and I found that it trace all scenes for all
    HDAdditionalLightData,
    HDAdditionalReflectionData,
    LODGroup,
    Renderer.

    upload_2021-9-13_10-54-28.png

    This method uses GetSharedMaterials, GetComponents, null checks for every Renderer, Mesh, Material and Shader in scenes and etc. Frame time always depends for how many objects do you have but Unity managed to cull it if it is not inside Frustrum and it did not trace the scene through C#. With this approach for building RT Acceleration Structure I would have to come back to combining mesh on build and creating atlases instead of rely on SRP Batcher

    I also want to mention that this code produce 16KB allocations every frame.

    Any thoughts about it? Does anyone know if Unity plan to change it?
     
    Last edited: Sep 13, 2021
  2. INedelcu

    INedelcu

    Unity Technologies

    Joined:
    Jul 14, 2015
    Posts:
    105
    Hi!

    There is a new engine side Scripting API function in development RayTracingAccelerationStructure.CullInstances which will replace the C# code. It will filter, cull Renderers and populate the acceleration structure, is multi-threaded and uses SIMD for culling (relative to user defined Sphere/Planes). Target version is 2022.1 - 2022.2.
     
    fuzzy3d likes this.
  3. m0nsky

    m0nsky

    Joined:
    Dec 9, 2015
    Posts:
    184
    This is interesting, will it replace the entire C# BuildRayTracingAccelerationStructure function, and will we still be able to use our own optimizations?

    I am currently running my own burst/jobs + cached version of the HDRaytracingManager. I calculate an "importance angle" (using the mesh diameter, distance to camera) and use this value to determine at which interval the transforms need to be updated in the acceleration structure. For example, objects close to the camera (important) could have their transforms rebuilt at 60hz, and smaller objects in the distance (unimportant) at 5hz, to avoid unnecessary CPU main thread & GPU/BVH workload. Renderers that have an "importance angle" below the specified cutoff threshold are culled on GPU hardware but kept in the HDRaytracingManager cache.

    Here is an example of the ray tracing stack running at 200-300 fps.



    HDRP ray traced reflections running at 400-500 fps.



    Also, do you happen to know if this potential API that Anis mentioned around february is still planned? (original post)



    Would be cool if this could work together with RayTracingAccelerationStructure.CullInstances somehow. I'm open to every solution, I'm mainly trying to eliminate all ray tracing CPU cost for VR.

    Edit
    Just saw this PR pop up, will be keeping an eye on this.
     
    Last edited: Oct 6, 2021
    rz_0lento likes this.
unityunity