Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Using a render texture with RenderTextureFormat.RInt and passing to compute shader

Discussion in 'Shaders' started by Unvestigate, Oct 12, 2014.

  1. Unvestigate

    Unvestigate

    Joined:
    Jul 9, 2013
    Posts:
    16
    Hi!

    I am just checking with you guys that I have understood this correctly, before beginning to implement this thing.

    I want to render a couple of meshes (with depth) but instead of rendering their color I want to render their object IDs (32-bit integers) into the texture. This texture will then be passed to a compute shader which will look up the IDs rendered.

    So first question: If I create a RT with format RenderTextureFormat.RInt, how do I write into that? Can I just create a fragment shader with return value int?

    Second question: Will the depth still work like it does when rendering the meshes "normally", provided I tell the system to use 24 bits for depth or something?

    Third question: As I said I will then pass the RT as an input to a compute shader which will analyze the contents and find distinct colors. How should I represent the RT in the compute shader? Is Texture2D<int> the right way to go and Can I just use the index operator to get the ints out of the texture or do I need to use something like the HLSL Load() function?

    Thanks!
     
  2. scrawk

    scrawk

    Joined:
    Nov 22, 2012
    Posts:
    804
    First off, I have never tried to render into a RInt format render texture so some of this is just a guess but I dont see why it wouldnt work.

    Just attach it to the camera's target from a script and render like normal. Should work the same as rendering into any other format.

    Just return a float4 like normal from the frag shader. The GPU should cast the red channel to a int. If that does not work try returning a int4 ??

    Yes. The depth buffer is separate from the color buffer. Depth testing will work as normal.

    Texture2D<int> is correct and yes you can just use the index operator or Load. Both will work.
     
  3. Sebastian Ahlman

    Sebastian Ahlman

    Joined:
    Apr 1, 2014
    Posts:
    11
    Thanks for the info, I actually managed to get it working. Seems that the "R" in "RInt" stands for red, so by returning an int4 from the fragment shader with the correct int value in the red channel seems to work.
     
    julienkay and Simon-O like this.
  4. Simon-O

    Simon-O

    Joined:
    Jan 22, 2014
    Posts:
    51
    Thank you for letting us know what worked... Saved me a lot of searching.