Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by sehgalvibhor15 · Aug 24, 2020 at 11:00 AM · unity 5vrfpsshadersdepth-buffer

Saving Depth buffer for VR Game on Unity - DROP IN FPS

I am building a VR game in Unity and I want to save the depth buffer on to the disk, ideally, I want every 10th frame and maintain the game FPS as 90FPS. So for every 90 frames, I want to get 10 frames of depth. I have tried everything like AsyncGPUReadbackRequest, writing raw Texture 2D files, write raw byte files but nothing seems to work. My game FPS drops to 30-40.

I have one main camera, and one which is a duplicate of the main camera, I don't want to render the depth buffer to the user's eye hence I create another camera. I am not sure if this is even required?

Right now, I am doing : camera.depthTextureMode = DepthTextureMode.Depth;

I have a shader and follow this tutorial: https://www.ronja-tutorials.com/2018/07/01/postprocessing-depth.html

Then: Graphics.Blit(source, destination, material);

Any suggestions on how can I remove the second camera and/or increase faster storing of the depth maps on to the disk?

Update: Removed second camera, still low FPS

Removed the second camera and removed the EncodeToPNG() but still, the FPS is dropping.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by Namey5 · Aug 24, 2020 at 11:59 AM

You don't need a second camera, you can just copy the depth texture of your current camera to a render texture and write that to a file. This example uses a coroutine to spread the operations out over multiple frames for performance;

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using System.IO;
 
 public class StoreDepthTexture : MonoBehaviour
 {
     private Camera m_Camera;
     private bool m_ShouldSaveDepth = false;
     private bool m_SavingDepth = false;
 
     private void OnEnable ()
     {
         //Make sure our camera writes to depth
         m_Camera = GetComponent<Camera> ();
         m_Camera.depthTextureMode |= DepthTextureMode.Depth;
     }
 
     private void Update ()
     {
         //If we aren't already saving a depth texture, save a new one when the space key is pressed
         if (!m_SavingDepth)
             if (Input.GetKeyDown (KeyCode.Space))
                 m_ShouldSaveDepth = true;
     }
 
     //This is called after the attached camera is finished rendering, meaning the depth texture should be available
     private void OnPostRender ()
     {
         if (m_ShouldSaveDepth)
         {
             //Start saving the depth texture
             StartCoroutine (CaptureDepth());
             m_ShouldSaveDepth = false;
         }
     }
 
     private IEnumerator CaptureDepth ()
     {
         m_SavingDepth = true;
 
         //The depth texture is set as a shader global under the variable name '_CameraDepthTexture' - we can just get a reference to that global
         RenderTexture depth = Shader.GetGlobalTexture ("_CameraDepthTexture") as RenderTexture;
         //The depth is technically stored in the depth buffer which we can't access directly, however if we blit that texture into another texture it will copy the depth into the red colour channel
         RenderTexture tmp = RenderTexture.GetTemporary (depth.width, depth.height, 16, RenderTextureFormat.ARGB32);
         Graphics.Blit (depth, tmp);
 
         //Wait until the next frame
         yield return null;
 
         //Store the last active render texture and set our depth copy as active
         RenderTexture lastActive = RenderTexture.active;
         RenderTexture.active = tmp;
 
         //Copy the active render texture into a normal Texture2D
         //Unfortunately readpixels doesn't work with single channel formats, so ARGB32 will have to do
         Texture2D tex = new Texture2D (depth.width, depth.height, TextureFormat.ARGB32, false);
         tex.ReadPixels (new Rect (0, 0, depth.width, depth.height), 0, 0);
         tex.Apply ();
 
         //Restore the active render texture and release our temporary tex
         RenderTexture.active = lastActive;
         RenderTexture.ReleaseTemporary (tmp);
 
         //Wait another frame
         yield return null;
 
         //Encode the texture data into .png formatted bytes
         byte[] data = tex.EncodeToPNG();
         
         //Wait another frame
         yield return null;
 
         //Write the texture to a file
         File.WriteAllBytes (Application.dataPath + "/depthTexture.png", data);
 
         m_SavingDepth = false;
     }
 }
Comment
Add comment · Show 34 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image sehgalvibhor15 · Aug 24, 2020 at 04:33 PM 0
Share

Thank you for such a great explanation. But this is dropping the FPS down to ~30, I got rid of the tex.EncodeToPNG() and now saving raw texture files (I'll convert them later using post-processing script) but still can't see any visible improvements. Since it is a Virtual reality game, 30 FPS is a problem. Any suggestions on how can I speed it up?

avatar image Namey5 sehgalvibhor15 · Aug 24, 2020 at 09:07 PM 0
Share

Reading data from the GPU back onto the CPU is not cheap, so I can only suggest potentially reading back chunks of the texture at a time rather than the whole thing.

avatar image sehgalvibhor15 Namey5 · Aug 24, 2020 at 09:44 PM 0
Share

Thank you, I tried downsampling the texture but did not really help. Appreciate your help though!

avatar image rh_galaxy sehgalvibhor15 · Aug 24, 2020 at 09:49 PM 0
Share

Can you identify which part that is the bottleneck? $$anonymous$$easure the time taken for each step.

 float t = Time.realtimeSinceStartup;
 //code to measure
 Debug.Log("Time: " + (Time.realtimeSinceStartup - t) * 1000.0f);

If it's tex.ReadPixels it should be easy to spread it out over more frames by doing it in chunks as Namey5 says.

Beyond that you can also do things like saving to disk in another Thread if the CPU is not at 100% load, but since most unity calls must be done from the main thread that is not always possible.

avatar image sehgalvibhor15 rh_galaxy · Aug 24, 2020 at 09:57 PM 0
Share

Sure, I can try and identify the bottleneck. For saving to disk, I created a RamDISK and was running the application from there but the FPS is still struggling.

Show more comments

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

319 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Unity 5: How to display bird eye view layout of a 3D scene with UI component overlay on screen? 1 Answer

Snapchat's lenses skin smoothing effects in unity3d? achievable by shaders? 1 Answer

Draw call difference in Unity 5.3.4 and Unity 5.0. 1 Answer

Trouble with culling mask in Google cardboard. 1 Answer

How to correct deprecated GvrReticle and GazeInputModule 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges