Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 heyx3 · Aug 25, 2015 at 01:46 AM · memorycompute shaderhlslbuffer

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

I have a Compute Shader in my game that runs every FixedUpdate() call. It has two passes/kernels:

1) clears out an Append buffer by "consuming" every element

2) appends several new elements into said Append buffer.

After running it, I need to read the elements of that Append buffer onto the CPU. I know that the ComputeBuffer class has a GetData(Array data) method, but from what I understand it would grab the entire buffer, including data that isn't actually allocated yet. I'd like to only spend bandwidth on actual appended elements, instead of getting the full reserved buffer. Additionally, I don't even know how to find the number of existing elements in the buffer in the first place. So how can I get the number of elements currently in an Append buffer, and how can I read in just those values from the GPU?

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

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by SunnySunshine · Sep 24, 2017 at 05:38 AM

 // Reset append buffer count for this dispatch.
 appendBuffer.SetCounterValue(0);
 
 shader.SetBuffer(0, "appendBuffer", appendBuffer);
 shader.Dispatch(0, 1, 1, 1);
 
 // Buffer to store count in.
 // You shouldn't be creating this each frame, but rather create it once
 // and then reuse it. Here solely to show its creation.
 var countBuffer = new ComputeBuffer(1, sizeof(int), ComputeBufferType.IndirectArguments);
 
 // Copy the count.
 ComputeBuffer.CopyCount(appendBuffer, countBuffer, 0);
 
 // Retrieve it into array.
 int[] counter = new int[1] { 0 };
 countBuffer.GetData(counter);
 
 // Actual count in append buffer.
 int count = counter[0]; // <-- This is the answer
 
 // Get the append buffer data.
 var data = new WhateverData[count];
 appendBuffer.GetData(data);
 
 // Don't do this every frame.
 countBuffer.Dispose();
Comment
Add comment · Show 2 · 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 ModLunar · Jun 12, 2018 at 11:22 PM 1
Share

Thank you so much. This is really going to help me on my project where I need to use compute shaders for voxel stuff! And even better - your comments were actually useful! A++ :D

avatar image Shiv2k3 · Jan 04 at 08:39 PM 0
Share

I'm trying to do this but my count is always 0 even though in the compute shader I am appending 1 to the buffer on all threads if that makes sense.

avatar image
0

Answer by Summit_Peak · Nov 30, 2015 at 01:00 PM

To read in the actual elements from an Append Compute Buffer (pseudocode):

Assign the appendBuffer:

 appendBuffer = new AppendStructuredBuffer<int>(maxAppendBufferN);

Dispatch to append items:

 AppendBufferItems();

Next, declare a buffer to retrieve only the actual appended elements:

 buffer = new RWStructuredBuffer<int>(appendBuffer.InstanceN);

If you have difficulty retrieving the number of elements in the append buffer, you can use InterlockedAdd() to count the items.

Next, connect buffer to appendBuffer with a pointer:

 SetBuffer(kernel_gpuFunction, "buffer ", appendBuffer);

Now, all items in the appendBuffer can be accessed on the gpu using buffer:

 Dispatch(kernel_gpuFunction, gpuFunction, appendBuffer.InstanceN);

These appended items can be transfered to the cpu using:

 buffer.GetData();

Finally:

 ConsumeBufferItems();
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

29 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

Related Questions

Compute Shader Pass RWStructuredBuffer by Reference 1 Answer

How to work with multiple Kernels sequentially(!) in a ComputeShader on one ComputeBuffer (Kernel 2 requires results from Kernel 1) - and read the final result using AsyncGPUReadback.Request 1 Answer

ComputeShader: StructuredBuffer sometimes empty in Metal 1 Answer

How to use the compute shader to sample the depth map under URP and calculate the corresponding world coordinates? 0 Answers

AsyncGPUReadback.GetData with computeBufferType.Append 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