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 /
  • Help Room /
avatar image
0
Question by Nightbringr · Aug 25, 2020 at 11:43 PM · bugmacshader programmingcompute shaderbuffer

ComputeShader: StructuredBuffer sometimes empty in Metal

Hi all,

I'm trying to access a StructuredBuffer from a Compute Shader targeting Metal on a MacBook Pro, and finding that it is sometimes full of zeros the first time the shader runs...

My shader is really simple and just returns green if the first item in the StructuredBuffer has a non-zero value and red otherwise...

I only ever set the value for an item to 1 so I know it should always be non-zero (green) however it seems to sometimes be zero (red).

Is this a bug?

Unity Version: 2020.1.3f1

Shader Code:

 #pragma kernel CSMain
 
 RWTexture2D<float4> Result;
 
 struct SomeObject
 {
     float value;
 };
 
 StructuredBuffer<SomeObject> _Objects;
 uint _ObjectsCount;
 
 [numthreads(8,8,1)]
 void CSMain (uint3 id : SV_DispatchThreadID)
 {
     if (_ObjectsCount > 0 && !any(_Objects[0].value)) {
         SomeObject s = _Objects[0];
         // Red
         Result[id.xy] = float4(1, 0, 0, 1);
     } else {
         // Green
         Result[id.xy] = float4(0, 1, 0, 1);
     }
 }

Script Code (Attached to the Camera):

 using System.Collections.Generic;
 using UnityEngine;
 
 struct SomeObject
 {
     public float value;
 }
 
 public class MyComputeShaderTest : MonoBehaviour
 {
     public ComputeShader RayTracingShader;
 
     private RenderTexture _target;
     private ComputeBuffer _objectBuffer;
 
     private int _renderCount = 0;
 
     private void OnRenderImage(RenderTexture source, RenderTexture destination)
     {
         if (_renderCount > 0)
         {
             return;
         }
 
         RayTracingShader.SetBuffer(0, "_Objects", _objectBuffer);
         RayTracingShader.SetInt("_ObjectsCount", _objectBuffer.count);
 
         // Make sure we have a current render target
         InitRenderTexture();
 
         // Set the target and dispatch the compute shader
         RayTracingShader.SetTexture(0, "Result", _target);
         int threadGroupsX = Mathf.CeilToInt(Screen.width / 8.0f);
         int threadGroupsY = Mathf.CeilToInt(Screen.height / 8.0f);
         RayTracingShader.Dispatch(0, threadGroupsX, threadGroupsY, 1);
         // Blit the result texture to the screen
         Graphics.Blit(_target, destination);
 
         _renderCount++;
     }
 
     private void InitRenderTexture()
     {
         if (_target == null || _target.width != Screen.width || _target.height != Screen.height)
         {
             // Release render texture if we already have one
             if (_target != null)
                 _target.Release();
             // Get a render target for Ray Tracing
             _target = new RenderTexture(Screen.width, Screen.height, 0,
                 RenderTextureFormat.ARGBFloat, RenderTextureReadWrite.Linear);
             _target.enableRandomWrite = true;
             _target.Create();
         }
     }
 
     private void OnEnable()
     {
         List<SomeObject> objects = new List<SomeObject>();
         // Add a number of random objects
         for (int i = 0; i < 100; i++)
         {
             SomeObject o = new SomeObject();
             o.value = 1;
             objects.Add(o);
         }
         // Assign to compute buffer
         _objectBuffer = new ComputeBuffer(objects.Count, 4);
         _objectBuffer.SetData(objects);
     }
 
     private void OnDisable()
     {
         if (_objectBuffer != null)
             _objectBuffer.Release();
     }
 }
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
0

Answer by Nightbringr · Aug 26, 2020 at 09:09 AM

Seems possible to work around this by "warming up" the shader

 private void OnEnable()
     {
         ...
 
         RayTracingShader.Dispatch(0, 1, 1, 1);
     }

This is probably not a fix and just masking an underlying timing issue

Comment
Add comment · 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

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

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

Related Questions

AsyncGPUReadback.GetData with computeBufferType.Append 0 Answers

Still Unity3D Editor 2017.2 , Freezing in Mac OS High Sierra 3 Answers

_WorldSpacePosLight0 suddenly changes when change camera position or direction 0 Answers

How can I read in the actual elements from an Append Compute Buffer? 2 Answers

How to use the compute shader to sample the depth map under URP and calculate the corresponding world coordinates? 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