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
0
Question by Andrii · Jul 09, 2020 at 12:09 PM · compute shader

Two dimensional array in compute shader

I want to use a compute shader to calculate movements of cloth particles.

Cloth particle is a struct that contains three Vector3s its current position, previous position and acceleration to add. On CPU side I keep those particles in a 2D array, this way I know the place of each particle in the structure. Each frame I calculate new position for each particle in the structure. I want to do the same with a compute shader.

The question is, can I pass the 2D array of structures to compute shader via the ComputeBuffer.SetData() and access each particle by its position in the structure? Or do I have to pass it as a 1D array and restructure my code to process data this way?

I'm now to compute shaders and I need to understand some fundamental things to get everything right.

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

Answer by _dns_ · Jul 09, 2020 at 03:58 PM

Hi, I'm doing something like this with independents particles (not in a 2D organization) so the high level concept you describe works :-) For 2D arrays, I don't think it's possible to declare such thing for Structured Buffers but you can emulate a 2D array with a 1D array anyway (table[ x + y * size_x], It also saves a bounds check in C# so it's always good to do that when CPU is important). There are also some variant of SetData() and ComputeBuffers related API that works with NativeArrays, I would also recommend to use that to reduce Garbage creation and maybe some memcopy :-)

I think you could also work with 1 (or more) Texture that you can read and write to in a Compute Shader like a 2D array. I'm not sure of the performance though, and it could be difficult to organize/store all Vectors in different textures RGBA floats. I guess Structured Buffers were designed to not have to deal with that :-)

Comment
Add comment · Show 3 · 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 Andrii · Jul 09, 2020 at 08:57 PM 0
Share

Thanks for your answer. I just wanted to clarify for myself if 2D arrays in compute buffer are possible at all. I want to have two copies of my code: GPU and CPU for falling back if compute shaders are not supported. And I want to know how to organize things on the CPU side to copy the array to and from GPU more easily, without converting it. It looks like I'll need to update my current code to work with 1D array.

avatar image _dns_ Andrii · Jul 09, 2020 at 09:20 PM 0
Share

Well, even if it's better, having the (nearly) exact same code for CPU and GPU is not always possible: customization may be better for some optimizations. I would also recommend to try the job system from the new DOTS framework for the CPU fallback code. It's quite easy to use and works with 1D arrays too (and it's easy to switch between single/multi threaded for debugging purpose, and the code should be closer to the compute shader one, and this does not have to involve the Entity system: just the convenient Unity C# Jobs system :-)

avatar image Andrii _dns_ · Jul 09, 2020 at 10:35 PM 0
Share

Thanks a lot! This makes a lot of sense. I'll try it.

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

131 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

Related Questions

Lerp() and compute shader 0 Answers

How (Or Why) to use Consume Buffers safely on arbitrary data lengths 0 Answers

Callback when (compute) shader is reloaded 0 Answers

,Drawing 100 000+ cubes 1 Answer

How do you use Mathf.PerlinNoise in a compute shader? 2 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