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
2
Question by raja-bala · Sep 11, 2014 at 12:51 AM · camerarendertextureshaderlab

Does filling a RenderTexture using a pixel shader require a camera?

I'd like to fill a 3D RenderTexture using a pixel shader (i can do this via a compute shader currently) to represents voxel data. These 3D RenderTexture's will subsequently be sampled to draw stuff on the screen.

I know that Texture3D (note: Not RenderTexture) can be written to on the CPU via the SetPixels API. I'm not interested in that.

TL;DR: I don't know the right way to go about this in Unity. I just need the pixel shader to be invoked WxH times. Each of those invocations will write to all the depth slices (essentially filling a voxel column). Nothing will be drawn to the screen.

Extra info:

The RenderTexture uses enableRandomWrite and the pixel shader references it via RWTexture3D I'm currently creating a quad mesh and using Graphics and GL classes to try to accomplish this:

My FillMetavoxel function is as follows:

 void FillMetavoxel(int xx, int yy, int zz)
     {
         mvFillTextures[zz, yy, xx].Create();
 
         Graphics.SetRandomWriteTarget(0, mvFillTextures[zz, yy, xx]);
         //Graphics.SetRenderTarget(mvFillTextures[zz, yy, xx]); 
 
         matFillVolume.SetPass(0); // activate first pass in shader associated with this material
   
         Vector3[] vertices = new Vector3[]{new Vector3(0, 0, 0),
                                            new Vector3(1, 0, 0),
                                            new Vector3(1, 1, 0),
                                            new Vector3(0, 1, 0)};
 
         int[] triangleIndices = new int[] {0, 1, 2, 0, 2, 3};
 
         Mesh quad = new Mesh();
         quad.vertices = vertices;
         quad.triangles = triangleIndices;
 
         GL.PushMatrix();
         // I'm doing something stupid here. I know it.
         GL.LoadPixelMatrix(0, 32, 0, 32);
         Graphics.DrawMeshNow(quad, Vector3.zero, Quaternion.identity);
         
         Debug.Log("Done filling " + xx + ", " + yy + ", " + zz);
 
         testCube.renderer.material.SetTexture("_Volume", mvFillTextures[zz, yy, xx]);
 
         GL.PopMatrix();
     }


The shader attached to testCube's material is:

     Shader "Custom/Fill Volume" {
     Properties {
     }
     SubShader {
         Pass{
         Cull Off ZWrite Off ZTest Always
         CGPROGRAM
         #pragma target 5.0
         #pragma vertex vert
         #pragma fragment frag
         #pragma exclude_renderers flash gles opengl
         #pragma enable_d3d11_debug_symbols
 
         #include "UnityCG.cginc"
            
         RWTexture3D<float> volumeTex;             
 
         struct v2f {
             float4 pos : SV_POSITION;
         };
 
         // We want the quad to be rasterized s.t each fragment is a voxel column
         v2f vert(appdata_base v)
         {
             v2f o;
             o.pos = float4(v.vertex.xy, 0.0f, 1.0f); // already in clip space
             return o;
         }


             // fill a depth column of the 3D RT
         half4 frag(v2f i) : COLOR
         {
             uint2 index = i.pos.xy * 32;
             int slice;
                     
             for(slice = 0; slice < 32; slice++) {
                 volumeTex[uint3(index, slice)] = half4(1.0f, 0.0f, 0.0f, 1.0f);
             }
 
             
             discard; // this shader draws nothing to the screen
             return half4(0.1f, 0.3f, 0.7f, 0.8f);
         }
         ENDCG
         }
     }FallBack Off
 }


What's the best way to ensure that a 32x32 pixel square is rasterized completely and the pixel shader is invoked 1k times? I'm guessing it has something to do with using an additional camera, but my pixel shader doesn't output a color (it just does UAV writes).

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
2
Best Answer

Answer by raja-bala · Sep 12, 2014 at 06:42 PM

I got the 2D and 3D UAV writes to work with a secondary camera in the scene. I attached a render target whose size depicted the 'compute work' i wanted to do, and used Graphics.Blit with a material that filled the UAVs.

I plan to use this setup for filling voxels in my scene, so this had to happen several (100?) times every frame. To ensure that no time was wasted on the render target attached to the secondary camera, I set its clear flags to none and cullmask to 0 (guessing this means 'Nothing').

I verified using Intel GPA that the process is optimal - no wasted effort on the secondary camera. alt text

Those interested can find the 2D & 3D UAV writes using PS & CS scene here.

I'd still like to know if there're better ways to do this (w/o having a secondary camera and RT attached to it). I couldn't get it to work using Graphics.DrawMesh(Now)


uav_scene_gpa_analysis.png (18.3 kB)
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 probesys · Oct 25, 2017 at 07:19 PM 0
Share

Hi! The link is broken, do you still have that demo somewhere? It would be very useful for me! Trying to write to a 3D texture from a fragment shader whithout any success...

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

24 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

Related Questions

Camera.RenderWithShader not working as of Unity 5.3? 0 Answers

Check if mouse is over GUI element using Rendertexture? 1 Answer

First Person Camera Pixelation 1 Answer

RenderTexture.active for a non-VR camera in a VR project 0 Answers

Steam VR Render object to only one eye 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