Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 dbsierra · Sep 06, 2017 at 05:22 PM · arraysrendertexturepostprocesssliceblitting

How would you efficiently access the last N rendered frames every frame?

Hello!

I'm trying to do some effects in Unity that require looking back in time to previously rendered frames, for example to superimpose delayed versions of the rendered view on top of each other for ghost like trails.

I implemented this in a very simple way first using OnRenderImage() to read in the incoming rendered image, storing it in a Texture2D, then stuffing that into an array of Texture2Ds in a circular buffer style implementation. This obviously eats up a lot of bandwidth by going unnecessarily into CPU memory every frame which I don't need, so it cuts me down from like 200fps to 60. This should stay purely on the GPU memory.

I looked into using Texture2DArray, which looks like what I want, but I'm having trouble getting it to write into a specific slice of the array. I found this line from here : "You can also use a geometry shader to render into individual elements." But it doesn't describe how. I also found this question was asked before as well by @gsourima here but the solution seems very hacky, albeit looks like it worked.

Anyone have any leads on how I can solve this problem? Is @gsourima's solution the only one atm?

Thanks a lot for any and all help!

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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by BastianUrbach · Sep 06, 2017 at 05:53 PM

Unless you already use it you should have a look at Graphics.CopyTexture which allows you to write from any texture to any texture, including the option to access specific Texture2DArray entries. To my knowledge Graphics.CopyTexture does not pull data from the graphics card which should give you a performance boost if you've been using Texture2D.ReadPixels so far.

Comment
Add comment · Show 2 · 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 dbsierra · Sep 07, 2017 at 02:03 AM 0
Share

That works!

One thing though, the call to CopyTexture that accepts the slice index as a parameter also takes in miplevel as a parameter, however my textures have no miplevels, so even if I put it to 0 I get an error every frame telling me my textures have no mips and that RenderTexture.Generate$$anonymous$$ips failed. Is there not a version of this method that takes in slice index without requiring miplevels?

Thank you!

avatar image dbsierra dbsierra · Sep 07, 2017 at 02:09 AM 0
Share

Just saw this: https://feedback.unity3d.com/suggestions/graphics-dot-copytexture-copy-all-mips-of-an-element

avatar image
0

Answer by gsourima · Sep 07, 2017 at 07:48 AM

Hi!

I can confirm that since I wrote the answer you refer to, I did not change my code and still use it (now in 2017.1).

In the OnRenderImage(RenderTexture source, RenderTexture destination) method I first store the current frame (_sliceNum being obviously the index of the slice I want to write into) :

 Graphics.SetRenderTarget(_texArray, 0, CubemapFace.Unknown, _sliceNum);

 GL.PushMatrix();
 GL.LoadOrtho();

 _matStore.SetTexture("_CamTex", source);
 _matStore.SetPass(0);

 GL.Begin(GL.QUADS);
 GL.TexCoord2(0, 0);
 GL.Vertex3(0, 0, 0);
 GL.TexCoord2(1, 0);
 GL.Vertex3(1, 0, 0);
 GL.TexCoord2(1, 1);
 GL.Vertex3(1, 1, 0);
 GL.TexCoord2(0, 1);
 GL.Vertex3(0, 1, 0);
 GL.End();

 GL.PopMatrix();

The _matStore member is the simplest Material ever. It's shader just passes the input texture to the current target (here the proper tex array slice, but the shader does not need to know anything about the destination buffer).

 Shader "Store Frame"
 {
     Properties
     {
         _CamTex ( "CamTex", 2D ) = "white" {}
     }
     SubShader
     {
         // No culling or depth
         Cull Off ZWrite Off ZTest Always
         Pass
         {
             CGPROGRAM
             #pragma vertex   vert
             #pragma fragment frag
             #include "UnityCG.cginc"

             struct vertInput
             {
                 float4 vertex : POSITION;
                 float2 uv     : TEXCOORD0;
             };

             struct vertOutput
             {
                 float4 screenPos : SV_POSITION;
                 float2 uvs       : TEXCOORD0;
             };

             sampler2D _CamTex;

             vertOutput vert ( vertInput v )
             {
                 vertOutput o;
                 o.screenPos = UnityObjectToClipPos( v.vertex );
                 o.uvs = v.uv;
                 return o;
             }

             fixed4 frag ( vertOutput i ) : SV_Target
             {
                 fixed4 col = tex2D( _CamTex, i.uvs );
                 return col;
             }

             ENDCG
         }
     }
 }

Coming back to the OnRenderImage() call, you can now use the whole _texArray in a new shader, as presented in my initial message on the page you referenced.

Hope this helps!

Comment
Add comment · Show 1 · 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 dbsierra · Sep 10, 2017 at 05:59 PM 0
Share

Thanks for posting! Sorry haven't had a chance to try this solution out yet but I definitely will

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

72 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

Related Questions

Weird problem with RenderTexture and HDRP Custom Pass 0 Answers

RenderTexture is black, nothing rendered 1 Answer

Blitting to RenderTexture does not seem to be accurate 0 Answers

Render to texture performance problems 0 Answers

Graphics.Blit scale 1 Answer


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