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 Dobbydoo · Jul 31, 2020 at 04:52 PM · renderingperformanceoptimizationbatchingcompute shader

,Drawing 100 000+ cubes

I've started a project to try to learn compute shaders. I decided to do a very basic test where I have a field of cubes moving in waves.

So I started with making this normally. However, I've found that my biggest performance hit is in the rendering, which makes it difficult to compare the speed of a compute shader.

I'm trying to draw 100 000 cubes (default Unity cube). I have enabled Gpu instancing on the material. I'm actually batching them manually to not clutter the scene with GameObjects. I'm only getting around 20FPS using this with only the draw logic (right now it's not even calculating matrices).

Does anyone know anything I can do to speed this up more? I'm going to simulate boids later, so constructing an optimized cube mesh won't really help me here.

Here is my code:

 public class OscillatorPool : MonoBehaviour {
     public float CycleTime = 1f;
     public float Strength = 1f;
 
     public GameObject SpawnObject;
     MeshFilter MeshFilter;
     Renderer Renderer;
 
     List<Vector3> CubePositions;
     List<Vector3> OriginalCubePositions;
     List<Matrix4x4> CubeMatrices;
 
     public int Amount = 100;
 
     void Start()
     {
         MeshFilter = SpawnObject.GetComponent<MeshFilter>();
         Renderer = SpawnObject.GetComponent<Renderer>();
 
         CubeMatrices = new List<Matrix4x4>();
         CubePositions = new List<Vector3>();
         OriginalCubePositions = new List<Vector3>();
 
         var srqtAmount = (int)Mathf.Sqrt(Amount);
 
         for (int x = 0; x < srqtAmount; x++)
         {
             for (int z = 0; z < srqtAmount; z++)
             {
                 var posX = x * SpawnObject.transform.localScale.x;
                 var posZ = z * SpawnObject.transform.localScale.z;
 
                 var cubePos = new Vector3(posX, transform.position.y, posZ);
                 CubePositions.Add(cubePos);
                 OriginalCubePositions.Add(cubePos);
                 CubeMatrices.Add(Matrix4x4.TRS(
                                 cubePos,
                                 Renderer.transform.rotation,
                                 SpawnObject.transform.localScale));
             }
         }
     }
 
     private void Update()
     {
         /*for (int i = 0; i < CubePositions.Count; i++)
         {
             var cubePos = CubePositions[i];
             var originalPos = OriginalCubePositions[i];
 
             cubePos.y = originalPos.y + Mathf.Sin((Time.time * Mathf.PI + cubePos.x) + Mathf.Cos(Time.time * Mathf.PI + cubePos.z) / CycleTime) * Strength;
 
             CubePositions[i] = cubePos;
         }*/
 
         Draw();
     }
 
     private void Draw()
     {
         int drawAmount = 1023;
 
         for (int i = 0; i < CubePositions.Count; i += drawAmount)
         {
             var batch = CubeMatrices
                             .Skip(i)
                             .Take(drawAmount)
                             .ToArray();
 
             Graphics.DrawMeshInstanced(MeshFilter.sharedMesh,
                 0,
                 Renderer.sharedMaterial,
                 batch,
                 drawAmount <= batch.Length ? drawAmount : batch.Length);
         }
     }
 }
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
Best Answer

Answer by Dobbydoo · Aug 02, 2020 at 04:10 PM

I solved this by using Unity ECS. It seems Unity with gameobjects is just too slow for this task, and according to the profiler a lot of the performance issues were on the cpu, so I tried converting everything to use the ECS system. This gave me an incredible performance boost, and I was able to draw around 1 million cubes at 60fps.

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 Captain_Pineapple · Aug 02, 2020 at 07:49 PM 0
Share

why use compute shaders to then take the result back on the cpu to give it back to the gpu? What stops you from just having a shader that combines the computation and the drawing part?

avatar image Dobbydoo · Aug 02, 2020 at 09:25 PM 0
Share

I could probably do that as well yes. I’m gonna try to make a shader implementation as well after I’m done with the ECS version of flocking boids, but I don’t have any shader experience, so I don’t really know. This was just meant as a performance comparison between doing the same operation on the cpu and gpu.

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

174 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

Related Questions

Drawcalls or Batched, which is more important to performace? 1 Answer

Is there any way I can statically batch procedurally generated objects? 0 Answers

Adding ONE shadow caster and ONE shadow receiver to my scene DOUBLES my draw calls. What? 0 Answers

Why can't I batch some draw calls? 0 Answers

Dynamic Batching and Instantiating 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