Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by misabiko · Feb 15, 2020 at 06:31 PM · renderinguniversalcompute shader

ComputeBuffer stays empty in compute shader in URP

I'm trying to pass a ComputeBuffer to read on my ComputeShader with the Universal Render Pipeline, and everything works, except that the buffer remains empty in the shader.

I'm relatively new to the ScriptableRenderPipeline and shaders, tried to piece things together from various forum threads and SRP tutorials, so there might be some major concept I'm missing to get this to work. But I couldn't find any code example with ComputeShader in SRP, let alone using a ComputeBuffer.

I've created a separate project to isolate the problem, with only one ScriptableRenderPass, that creates a RenderTexture, dispatches the ComputeShader on it and blit it to the camera's texture. I send a float4 _Color for sanity check and draw it on every pixels, that works. I then send a StructuredBuffer _Colors with the exact same content, and draw it on every pixels, and I get nothing.

Here's the renderpass:

 class CustomRenderPass : ScriptableRenderPass {
 public ComputeShader computeShader;
 public string commandBufferName;
 public RenderTargetIdentifier source;

 RenderTexture target;
 RenderTargetIdentifier targetIdentifier;
 
 public override void Configure(CommandBuffer cmd, RenderTextureDescriptor cameraTextureDescriptor) {
     if (target != null)
         target.Release();
     
     target = new RenderTexture(cameraTextureDescriptor) {
         enableRandomWrite = true
     };
     target.Create();
     
     targetIdentifier = new RenderTargetIdentifier(target);
 }
 
 public override void Execute(ScriptableRenderContext context, ref RenderingData renderingData) {
     CommandBuffer cmd = CommandBufferPool.Get(commandBufferName);
     
     //Setting the compute shader's texture to target
     cmd.SetComputeTextureParam(computeShader, 0, "Result", targetIdentifier);
     
     //Setting the StructuredBuffer<float4> _Colors
     ComputeBuffer buffer = new ComputeBuffer(1, 4 * sizeof(float));
     buffer.SetData(new Vector4[] {new Vector4(1, 0, 0, 1)});
     cmd.SetComputeBufferParam(computeShader, 0, "_Colors", buffer);
     
     //Setting an example float4 _Color
     cmd.SetComputeVectorParam(computeShader, "_Color", new Vector4(1, 0, 0, 1));
     
     //Dispatching compute shader
     cmd.DispatchCompute(
         computeShader,
         0,
         Mathf.CeilToInt(Screen.width / 8f),
         Mathf.CeilToInt(Screen.height / 8f),
         1
     );
     
     //Pasting texture on the camera's
     cmd.Blit(targetIdentifier, source);
     
     context.ExecuteCommandBuffer(cmd);
     buffer.Release();
     CommandBufferPool.Release(cmd);
 }
 }



And here's the compute shader:

 #pragma kernel CSMain
 
 RWTexture2D<float4> Result;
 StructuredBuffer<float4> _Colors;
 float4 _Color;
 
 [numthreads(8,8,1)]
 void CSMain (uint3 id : SV_DispatchThreadID) {
     //This doesn't work
     Result[id.xy] = _Colors[0];
     
     //But this works
     //Result[id.xy] = _Color;
 }
 

I've removed everything else so I don't see how this could not work. I don't get any errors, it just draws black.

For context, I'm trying to render spheres with Ray Marching on top of my scene, and I was following GPU Ray Tracing in Unity for instructions on the Compute Shader as well as a Ray Marching tutorial, I've got hard coded spheres to render no issues, but I'm stuck when trying to pass a ComputeBuffer of sphere structs.

Comment
Add comment · Show 2
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 Daniel-Gutierrez · Sep 25, 2020 at 02:49 AM 0
Share

Where is public RenderTargetIdentifier source set, and what is it set to ?

avatar image misabiko Daniel-Gutierrez · Sep 25, 2020 at 05:00 AM 0
Share

In the ScriptableRendererFeature's AddRenderPasses(), I've assigned it renderer.cameraColorTarget directly before enqueuing the pass.

It's been a while since I've touched it so I'm not sure if the last commit compiles, but if it can help here's the code I was working on: https://github.com/misabiko/JumpThing/blob/master/Assets/Rendering/Ray$$anonymous$$archingFeature.cs

1 Reply

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by misabiko · Feb 16, 2020 at 01:15 AM

The problem was that I released the buffer before it could be used by the shader. If I put the buffer as a member variable to the render pass, and only release it before I create it again, I can access the data on the shader. And putting the release in ScriptableRenderFeature's OnDisable() seems to have fixed the Garbage Collector warnings so far.

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 naelstrof · Jul 03, 2021 at 07:25 AM 0
Share

I had the exact same problem, thank you! I was releasing the buffer during the FrameCleanup() override. I would have never guessed that it was being released too early. Thank you!

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

220 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 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 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 avatar image avatar image avatar image avatar image

Related Questions

URP Don clear flag 0 Answers

How Do I Fix URP Rendering Mobile? 0 Answers

Send enums to compute shader? 0 Answers

Need help to convert custom shader to URP 0 Answers

How to fix Black Reflection in AR with URP on Android? 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