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
1
Question by thebmxeur · Aug 13, 2014 at 09:28 PM · rendertotexture

How to clear multiple render buffers (MRT)

Hi, I'm trying to use multiple render targets for a game I'm working on. Since I haven't done that before I created a small project to try it. I have several questions :

  • what are the differences between :

    void Camera.SetTargetBuffers(RenderBuffer[] colorBuffer, RenderBuffer depthBuffer);

and

 static void Graphics.SetRenderTarget(RenderBuffer[] colorBuffers, RenderBuffer depthBuffer);

  • Camera.SetTargetBuffers doesn't seem to work well : if I call it the buffers are not filled (they are black), they don't cover the "render quad" and are placed on the bottom left of the screen. Am I missing something ?

  • where/when should I call those methods : OnPreRender (it seems to work) ? OnRenderImage ? ...

  • The second buffer is not cleared each frames. Is there a way to clear a buffer manually (ask for it to be cleared) ?

Here is the small example project so you can see what I did. The project only render a sphere, the red channel of the sphere is rendered in the first target, the green channel of the sphere is rendered in the second target. They are then combine with the first target being on the left side of the screen and the second target being on the right side (they are squeezed and thats intended). MRT example

And screenshots of what I get : alt text

As you can see the right side (second target) the buffer is not cleared. alt text

Thanks !

mrt.zip (81.0 kB)
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
3
Best Answer

Answer by kebrus · Aug 14, 2014 at 01:56 PM

Dude i got tell you thx for your example project, i was hammering my head trying to put MRT working and your example seems to work for the most part, i still get some weird results sometimes because the render texture leaks when developing and you can only get it to work again by closing and reopening unity.

Anyway, regarding your question it seems to work differently with dx11 and without it but one thing that worked with me was to use the GL command "Clear", the way you do it is by switching to the second target and clear it and then return to the first.

here's a simple code example, probably not really elegant but it works, put it in the beginning of PreRender

 RenderTexture lastActive = RenderTexture.active;
 RenderTexture.active = m_target2;
 GL.Clear(false, true, Color.clear);
 RenderTexture.active = lastActive;

You could probably don't use the lastActive RT and simply point to the first target, it's up to you

Comment
Add comment · Show 3 · 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 thebmxeur · Aug 14, 2014 at 05:55 PM 0
Share

Thanks a lot. I forgot about the GL class. Still some questions about how the $$anonymous$$RT functions work in Unity but that will do for now.

For the sake of being complete here is my OnPreRender function :

 void OnPreRender( ) {
 
     if ( SystemInfo.supportedRenderTargetCount >= 2 ) {
 
         //camera.SetTargetBuffers( m_buffers, m_target1.depthBuffer );
         RenderTexture.active = m_target2;
         GL.Clear( false, true, camera.backgroundColor );
         RenderTexture.active = m_target1;
 
         Graphics.SetRenderTarget( m_buffers, m_target1.depthBuffer );
     }
 }

A few notes :

  • the profiler tells me that there are 3 RenderTexture, and 2 used. So I think we could spare creating the first RenderTexture by getting the one that the camera use ( RenderTexture.active ).

  • the profiler shows me spikes of about 8ms every few frames with 96% of the frame time spent on RenderTexture.SetActive.

avatar image kebrus · Aug 14, 2014 at 06:29 PM 0
Share

To be honest, there's still stuff i don't get it with $$anonymous$$RT, the documentation and the lack of example don't give any ideas why some things don't work or work poorly

In a very similar example as yours going on and i'm getting 4 render textures being used. From my previous experience the numbers are not to be trusted because unity reserves and uses render textures as it needs them so sometimes is hard to tell if we are doing anything wrong or not, most of the times i just close unity and open in again, this usually clears everything

HDR, forward/deferred, depth buffer, gamma/linear space, dx11 are all things that can influence the outcome of a particular render texture and it's performance

i'm not getting any spikes on that call, in fact i'm not getting any spikes at all so i'm not really sure what that could be, could it be some setting in the quality settings?

for some reason my own example now doesn't let me paint the background of the second target it always uses the same color as the first target, along with that, my draw calls are most of the times increased by one with the script running (ex: from 10 to 11), which is okay but i don't really know why the extra draw call, so i'm still trying to figure out most of these stuff with $$anonymous$$RT, i'll definitely post something on the forums as soon as i get a good grasp of how things work, i even made a post yesterday on shaderlab about $$anonymous$$RT since i'm currently trying to improve my own glow shader effect into using $$anonymous$$RT to save draw calls

avatar image thebmxeur · Aug 16, 2014 at 04:12 PM 0
Share

New problem : it works in the editor but doesn't work in builds. In builds unity crash after 1 or 2 seconds (both debug and release). Plus in debug the display isn't good : what should be on the right is on the left and what should be on the left is on the right; and the second buffer isn't cleared. I think I'll report a bug.

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

RenderToCubemap with Image effect? 0 Answers

Render UI to RenderToTexture with alpha 0 Answers

Skinned Meshes cutted out on Render Texture 1 Answer

Drawing to an in-game screen 1 Answer

Is it possible to render to a texture via RWTexture2D without using Graphics.blit 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