# SetRenderTarget problem

**URL:** https://discussions.unity.com/t/setrendertarget-problem/657059
**Category:** Shaders & Materials
**Tags:** Shaders
**Created:** [March 2, 2017, 5:05pm UTC](https://discussions.unity.com/t/setrendertarget-problem/657059 "2017-03-02T17:05:37Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![SubPixelPerfect](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/subpixelperfect/32/230342_2.png) [@SubPixelPerfect](https://discussions.unity.com/u/SubPixelPerfect)
#### Post date: [March 2, 2017, 5:05pm UTC](https://discussions.unity.com/t/setrendertarget-problem/657059/1 "2017-03-02T17:05:37Z")

</div>

Hi, I’m working on a post effect

and for that i need to render set of meshes to texture with ZTest LEqual to create a mask  
 ![2979189--221505--mask.jpg](https://europe1.discourse-cdn.com/unity/original/4X/6/e/4/6e4e10ab4c2f0a36a37bfd932a9490a193b8cb19.jpeg)  
here is a sphere rendered to separate texture

so i’m creating new RenderTexture and setting it as active color buffer while leaving depth buffer unchanged  
rendering what i need and resetting render target

and everything works perfect

but when i’m enabling Direct3D in player settings and enabling anti-aliasing in quality settings  
i’m getting an error:

> [@](#):
>
> ArgumentException: Graphics.SetRenderTarget called with depth RenderBuffer from screen and color RenderBuffer from RenderTexture  
> UnityEngine.Graphics.SetRenderTargetImpl (RenderBuffer colorBuffer, RenderBuffer depthBuffer, Int32 mipLevel, CubemapFace face, Int32 depthSlice) (at C:/buildslave/unity/build/Runtime/Export/Graphics.cs:189)  
> UnityEngine.Graphics.SetRenderTarget (RenderBuffer colorBuffer, RenderBuffer depthBuffer) (at C:/buildslave/unity/build/Runtime/Export/Graphics.cs:225)

here is my code:

```csharp
void OnRenderImageTest0(RenderTexture source, RenderTexture destination){
        // create new render target
        RenderTexture rt = RenderTexture.GetTemporary(_camera.pixelWidth, _camera.pixelHeight);
        rt.Create();
        RenderBuffer tmp = Graphics.activeColorBuffer;

        // change render target
        Graphics.SetRenderTarget(rt.colorBuffer, Graphics.activeDepthBuffer);

        ...
        // render meshes
        ...

        // reset render target
        Graphics.SetRenderTarget(tmp, Graphics.activeDepthBuffer);
        rt.Release();
    }

```

is it a bug or i’m doing something wrong?  
is there any way to do that on Direct3D with enabled anti-aliasing?

---

<div class="post-metadata">

### Author: ![SubPixelPerfect](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/subpixelperfect/32/230342_2.png) [@SubPixelPerfect](https://discussions.unity.com/u/SubPixelPerfect)
#### Post date: [March 2, 2017, 9:31pm UTC](https://discussions.unity.com/t/setrendertarget-problem/657059/2 "2017-03-02T21:31:31Z")

</div>

[here](http://stackoverflow.com/questions/19023273/why-do-i-need-depthbuffer-to-use-rendertexture) i’ve found that

> [@](#):
>
> in D3D10 you cannot share a depth surface between render targets of different sizes.

But in my case size is equal.

unity documentation says:

> **[Unity - Manual: Cameras and depth textures](https://docs.unity3d.com/Manual/SL-CameraDepthTexture.html)**

> [@](#):
>
> When implementing complex Shaders or Image Effects, keep [Rendering Differences Between Platforms](https://docs.unity3d.com/Manual/SL-PlatformDifferences.html) in mind. In particular, using depth texture in an Image Effect often needs special handling on Direct3D + Anti-Aliasing.
> 
> In some cases, the depth texture might come directly from the native Z buffer. If you see artifacts in your depth texture, make sure that the shaders that use it do not write into the Z buffer (use [ZWrite Off](https://docs.unity3d.com/Manual/SL-CullAndDepth.html)).

ok, I have ZWrite Off

more from docs:

> **[Unity - Manual: Writing shaders for different graphics APIs](https://docs.unity3d.com/Manual/SL-PlatformDifferences.html)**

> [@](#):
>
> Vertical Texture coordinate conventions differ between two types of platforms: Direct3D-like and OpenGL-like.  
> …  
> When you use [Image Effects](https://docs.unity3d.com/Manual/WritingImageEffects.html) and anti-aliasing, the resulting source Texture for an Image Effect is not flipped to match the OpenGL-like platform convention. In this case, Unity renders to the screen to get anti-aliasing and then resolves rendering into a Render Texture for further processing with an Image Effect.  
> …  
> …  
> …  
> Depth (Z) direction differs on different Shader platforms.  
> …  
> If you are fetching the depth (Z) buffer value manually, you might want to check the buffer direction.

this is clear, but what i can do in my case?
