[Unity] GPU Instancing Tutorial

  Рет қаралды 44,894

Kyle Andrews

Kyle Andrews

Күн бұрын

If you see anything wrong with this video please point it out with examples or references, I'm just starting to get into this section of the Unity documentation.
Timestamps:
0:00 - Intro
0:25 - GPU Comparison Chart
4:35 - Programming
Need critique, motivation, or help? Join our Discord server: / discord
=Credits=
Created by Kyle Andrews
• Website: gamesbykyle.com/
• Artwork: andrewk9.artstation.com/
• Follow me: / @gamesbykyle
=MUSIC=
Song: Culture Code feat. Karra - Make Me Move (James Roche Remix) [NCS Release]
Music provided by NoCopyrightSounds.
Video: • Culture Code feat. Kar...
Download: ncs.io/MakeMeMoveRemix

Пікірлер: 60
@purpl3grape
@purpl3grape 3 жыл бұрын
As much as the gpu instancing optimization is talked about in the community, it's surprising to find not that much coverage on it. This is great.
@underpowerjet
@underpowerjet 4 жыл бұрын
Thank for this tutorial!!! I am going to be trying this out to generate grass on dynamic LOD planet terrain. Recently, I decided to use Light Weight Render Pipeline for the Shager Graph. My custom Gerometry Shaders for generating grass are now completely borken due to LWRP. I am going to stick with LWRP because I hate writing shader code. This looks like it will work for the solution I'm looking for. This is awsome!
@guir0
@guir0 6 жыл бұрын
Nice job, to the point!! We need a follow up video ;) Suggestion/Challenge: NavMesh with boids on a plane to see how much it can handle! Cheers!!
@andreasnull
@andreasnull 4 жыл бұрын
Nice tutorial. Some comments regarding the performance: The main problem with your performance is that you are using Linq in the Update method. This is overkill. You actually invoke a Select over a collection which is bad and then ToList which again is not the best for performance. You probably need to create a solution with dictionaries. Also, the ObjData class can be a struct, which I don't think will matter for this example, but if you want in the future to create ObjData during gameplay this is the way to go.
@ThePsycho211
@ThePsycho211 3 жыл бұрын
Then throw Foreach out of the window and it will be even better.
@NumianVonKami
@NumianVonKami 4 жыл бұрын
thank you! what i did for my project is to initialize the batch.Select(...) part on the Start() method, as my objects wont change position.
@personmuc943
@personmuc943 5 жыл бұрын
What if i want to use multiple Meshes and Colors? Should i use multiple matrix4x4 lists?
@danieles6684
@danieles6684 11 күн бұрын
I know this is old as hell, but the built in GPU instancing wasn't working for me, so I wanted to try this. This ended up working a lot better. That being said, the selection via linq of the matrices generates a lot of garbage, so I found my FPS tanking every few frames from garbage collection. Just storing the matrices as the batches directly fix this so I wasn't generating new garbage every frame.
@YangSunWoo
@YangSunWoo 2 жыл бұрын
The liberal uses of 'fuck' really helped me keep my attention on the tutorial, thanks!
@Waffle4569
@Waffle4569 6 жыл бұрын
The GPU performance in editor might be explained by VRAM usage. Once your GPU runs out of VRAM, performance drastically drops.
@courageouscuber9278
@courageouscuber9278 Жыл бұрын
I did this to render grass in my game. I had to do a few changes to make the grass stick to the terrain, and I had to make the grass disappear when the player gets further away so that the tris count doesn't get too high, but it works and my game is running smoothly! Thank you so much!
@nukeythewise9424
@nukeythewise9424 7 ай бұрын
Could you please tell me what methods you used?
@varunkushwah451
@varunkushwah451 3 күн бұрын
Pls tell us
@ivanfilatov5482
@ivanfilatov5482 3 жыл бұрын
How to instance box with colliders? Trees in terrain support colliders, i dont think drawMesh can do the same
@jessicasan9643
@jessicasan9643 5 жыл бұрын
Can i use in Android device? The objects may have collider, rigidbody and can i use with Unet?
@BlendcraftCreations
@BlendcraftCreations 5 жыл бұрын
This should answer your questions: bfy.tw/JdFR
@Waffle4569
@Waffle4569 6 жыл бұрын
I've gotten much better performance with 100k cubes in editor (GTX 570) by combining the cubes into one mesh and using a vertex shader. If a combined mesh was then instanced, you'd probably be able to go even higher with little performance impact. Instancing the individual cubes adds up too much, when you could be doing a lot more in a draw call.
@tokyowarfare6729
@tokyowarfare6729 5 жыл бұрын
GPU instancing is not here to overcome issues bith static batches BUT with dynamic batching. You should keep doing static batching for your static stuff and consider using GPU agains Dynamic for your dynamic stuff
@kirienitram
@kirienitram 3 жыл бұрын
Hi Kyle. I was wondering why this is more efficiently than ticking of the draw instanced checkbox? I'm new to Unity, but I know a fair amount about graphics programming.
@BlendcraftCreations
@BlendcraftCreations 3 жыл бұрын
I haven't touched Unity since I made this video so I'm not sure what's the most efficient way to do anytime anymore. Sorry I couldn't be more helpful.
@kirienitram
@kirienitram 3 жыл бұрын
@@BlendcraftCreations No worries! I didn't look deeper into it since I asked, so I don't know anything more either. Thanks for the answering nonetheless!
@boulangermarguiertheo8937
@boulangermarguiertheo8937 4 жыл бұрын
Could we add physical interactions with those cubes ? Similar way to a rigidbody ? Gravity & collisions.
@comradekenobi8146
@comradekenobi8146 4 жыл бұрын
Boulanger Marguier Théo You'd have to write a custom physics system to do gravity -- Unity's in-built system will choke on that many objects.
@jesper9622
@jesper9622 3 жыл бұрын
@@comradekenobi8146 Thats unneccesary, just remove the collider component of the cubes if you just want to do gravity
@comradekenobi8146
@comradekenobi8146 3 жыл бұрын
@@jesper9622 The original comment also mentioned collisions. I meant to say "to do gravity and collisions."
@jesper9622
@jesper9622 3 жыл бұрын
@@comradekenobi8146 Ok hm, with collisions i think that will be hard to beat unitys physics system and make it support meshcolliders, friction, raycasts
@comradekenobi8146
@comradekenobi8146 3 жыл бұрын
Jesper Unity's physics system is more versatile, but there are simpler ways to handle basic collision detection that could run better with that many objects that wouldn't support mesh colliders, raycasts, or friction.
@caburnicus2285
@caburnicus2285 6 жыл бұрын
I haven't even finished watching the tutorial, but thumbs up already for convenient curse words...how the fuck do we run this thing? Lets find out!
@WreckedRevival
@WreckedRevival 6 жыл бұрын
I tested this with deferred vs forward rendering for 100k instances.. (cam set to perspective) Forward Render Batches | saved by batching | FPS | Render Thread | Tris | Verts | Set Pass 1202 | 598,800 | 34.5 - 38.9 | 25.9 - 26.8ms | 7.2m | 14.4m | 8 Deferred Batches | saved by batching | FPS | Render Thread | Tris | Verts | Set Pass 1007 | 498,000 | 41.7 - 46.8 | 20.9 - 21.8ms | 6m | 12m | 12
@BlendcraftCreations
@BlendcraftCreations 6 жыл бұрын
I've never heard of the deferred vs forward rendering, and after a few quick google searches it's something I think I'll have to experiment with. Thanks for sharing!
@salemazzam6502
@salemazzam6502 5 жыл бұрын
You should update your visual studio to 2017 and unity to 2018
@BlendcraftCreations
@BlendcraftCreations 5 жыл бұрын
I believe unity 2018 was released (just) after I made this. Also I use VS Code instead of studio now, it's much nicer.
@Kelewann
@Kelewann 6 жыл бұрын
I was following your tutorial to familiarise myself with GPU instancing. Linq has pretty poor performance and by directly storing the matrix4x4 in list for the batches, I got a solid 60fps with 100k cubes in editor (I have similar pc specs to yours). Also I do notice a weird thing and have no idea what's ups. If I manually put 10k cubes in the editor scene and then run it, I get 10k "saved by batching" as expected. But when doing it as you do through code I get 50k "saved by batching"... If you try this with linqs you will see it has pretty heavy performance impact, as if it was actually instantiating 50k.
@BlendcraftCreations
@BlendcraftCreations 6 жыл бұрын
I really couldn't find a ton of info on GPU Instancing when I was researching this during the week before filming so I made it work with what I knew/had available. If you have any references to how it could be done better I'd love to read/watch through them.
@Kelewann
@Kelewann 6 жыл бұрын
Sorry I don't have any other reference. Your video is actually the best material on the subject that I could find. I just wanted to give feedback on things I found while following your tutorial. I'll let you know if I find anything better.
@BlendcraftCreations
@BlendcraftCreations 6 жыл бұрын
I'm looking forward to it. I'm just glad I didn't miss anything major while researching this topic. After I hit the 6th page of Google I tend to lose hope in finding something useful.
@Gmania999
@Gmania999 6 жыл бұрын
Hi, very good result, but i dont understand how to write a script "the matrix4x4 in list for the batches", could you share the script pls)
@Kelewann
@Kelewann 6 жыл бұрын
I'll put up the full project on github soon. Not that it's much. If you take BlendCraft code in the video, I just added a list of Matrix4x4 and when creating the object I directly put the Matrix in this new list. Then instead of getting the matrix from the list of object with a Linq querry, I just give directly the list of matrix. Here is a pastebin copy-paste: pastebin.com/r63PiSa1
@simonfarre4907
@simonfarre4907 3 жыл бұрын
Also seems like a unity problem even in build. 10000 cubes should not be that taxing to draw instanced on the GPU if it was handcoded. Which is a strong argument for building your own engine.
@MattRix
@MattRix 3 жыл бұрын
It's not a Unity problem, he's just not using it correctly.
@RiversJ
@RiversJ 2 жыл бұрын
Old video, just about everything that could go wrong with the implementation went wrong. Other comments contain his reply and pointers by others how to do it right. Do it right with indirect version and you can pump out a million of those cubes even on the hardware of 3 years back.
@tokyowarfare6729
@tokyowarfare6729 5 жыл бұрын
having higher FPS in editor than build makes no sense. I've never seen this behaviour.
@BlendcraftCreations
@BlendcraftCreations 5 жыл бұрын
At what point was the FPS higher in the editor than the build?
@tokyowarfare6729
@tokyowarfare6729 5 жыл бұрын
@@BlendcraftCreations 2:55
@colonthree
@colonthree 4 жыл бұрын
4:35
@User33423
@User33423 2 жыл бұрын
Meshes that have a low number of vertices can’t be processed efficiently using GPU instancing because the GPU can’t distribute the work in a way that fully uses the GPU’s resources. This processing inefficiency can have a detrimental effect on performance. The threshold at which inefficiencies begin depends on the GPU, but as a general rule, don’t use GPU instancing for meshes that have fewer than 256 vertices. If you want to render a mesh with a low number of vertices many times, best practice is to create a single buffer that contains all the mesh information and use that to draw the meshes.
@YAS-vm8ko
@YAS-vm8ko 2 жыл бұрын
8:31
@chromosome24
@chromosome24 3 жыл бұрын
My dog food keeps producing dog shit.
@shloh532
@shloh532 6 жыл бұрын
Hi :D
@BlendcraftCreations
@BlendcraftCreations 6 жыл бұрын
Hello
@Gasimo
@Gasimo 5 жыл бұрын
I WANT 16BG RAM
@BlendcraftCreations
@BlendcraftCreations 5 жыл бұрын
downloadmoreram.com
@Hello-qg4yk
@Hello-qg4yk 3 жыл бұрын
@@BlendcraftCreations hey!
@scenes_dot
@scenes_dot 6 жыл бұрын
using System; using System.Collections; using System.Collections.Generic; using UnityEngine; using System.Linq; public class Spawner : MonoBehaviour { public class ObjData { public Vector3 pos; public Quaternion rot; public Vector3 scale; public Matrix4x4 matrix { get { return Matrix4x4.TRS(pos, rot, scale); } } public ObjData(Vector3 _pos, Vector3 _scale, Quaternion _rot) { pos = _pos; scale = _scale; rot = _rot; } } public int instanceNum; public Vector3 maxPos; public Mesh objMesh; public Material objMat; private List batches = new List(); void Start() { int batchIndexNum = 0; List currBatch = new List(); for (int i = 0; i < instanceNum; i++) { AddObj(currBatch, i); batchIndexNum++; if (batchIndexNum >= 1000) { batches.Add(currBatch); currBatch = BuildNewBatch(); batchIndexNum = 0; } } } void AddObj(List _currBatch, int _i) { Vector3 position = new Vector3(UnityEngine.Random.Range(-maxPos.x, maxPos.x), UnityEngine.Random.Range(-maxPos.y, maxPos.y), UnityEngine.Random.Range(-maxPos.z, maxPos.z) ); _currBatch.Add(new ObjData(position, new Vector3(2, 2, 2), Quaternion.identity)); } private List BuildNewBatch() { return new List(); } void Update() { RenderBatch(); } private void RenderBatch() { foreach (var batch in batches) { Graphics.DrawMeshInstanced(objMesh, 0, objMat, batch.Select((a) => a.matrix).ToList()); } } }
@BlendcraftCreations
@BlendcraftCreations 6 жыл бұрын
This is why god made pastebin.com/ use that and try again.
What I Did To Optimize My Game's Grass
8:13
Acerola
Рет қаралды 122 М.
Динамическая оптимизация в UNITY! GPU instancing
7:40
Яковлев Илья - IT и игры
Рет қаралды 19 М.
I Need Your Help..
00:33
Stokes Twins
Рет қаралды 164 МЛН
They RUINED Everything! 😢
00:31
Carter Sharer
Рет қаралды 18 МЛН
Как быстро замутить ЭлектроСамокат
00:59
ЖЕЛЕЗНЫЙ КОРОЛЬ
Рет қаралды 12 МЛН
Giving Personality to Procedural Animations using Math
15:30
t3ssel8r
Рет қаралды 2,4 МЛН
Improve Performance 10x Using GPU Instancing || Unity Tutorial
9:43
Freedom Coding
Рет қаралды 2,7 М.
How much MONEY my Game made (and how much it cost!)
14:00
Code Monkey
Рет қаралды 154 М.
Unity Performance Tips: Draw Calls
4:24
Lofi Dev
Рет қаралды 177 М.
20 Advanced Coding Tips For Big Unity Projects
22:23
Tesseract
Рет қаралды 155 М.
OpenGL - instancing
8:07
Brian Will
Рет қаралды 12 М.
Adding This One Thing Made my Game Look 327% Better
6:11
OverPhil Dev
Рет қаралды 82 М.
Procedural Grass in 'Ghost of Tsushima'
26:09
GDC
Рет қаралды 121 М.
Оптимизация игр на unity. Material instancing
8:19
Максим Крюков
Рет қаралды 17 М.
I Need Your Help..
00:33
Stokes Twins
Рет қаралды 164 МЛН