Java-Gaming.org Hi !
Featured games (91)
games approved by the League of Dukes
Games in Showcase (803)
Games in Android Showcase (237)
games submitted by our members
Games in WIP (867)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Issues with batching meshes  (Read 4742 times)
0 Members and 1 Guest are viewing this topic.
Offline ShadedVertex
« Posted 2016-10-14 09:11:57 »

I've been working on a new game, and I decided to batch the cubes that I'm rendering. I batched them, no problem, reduced the number of draw calls to 1, which quadrupled my FPS Tongue

Problem is, now I can't change the translation, rotation, scale and colour of the cubes without having to run the batch function all over again during the render loop, which causes lag. If I want to remove a cube from the batch, I can't do that either because it's all in one VAO and I have to run the batch function again to remove the cube.

If Minecraft batches the blocks to optimise the rendering process, how is it that the environment in Minecraft is destructible? And how is it that individual blocks can be outlined, even though all the blocks are part of a single mesh (the chunk)?

I'd love to know how ^this^ is done.
Offline VaTTeRGeR
« Reply #1 - Posted 2016-10-14 09:21:25 »

Have look at instancing: http://www.opengl-tutorial.org/intermediate-tutorials/billboards-particles/particles-instancing/
It's used for particles and trees and grass and stuff, ThinMatrix also has a nice tutorial for instancing on youtube.

You basically upload your mesh once and then only buffers with all the position/rotation/etc data.
Offline ShadedVertex
« Reply #2 - Posted 2016-10-14 09:25:41 »

Have look at instancing: http://www.opengl-tutorial.org/intermediate-tutorials/billboards-particles/particles-instancing/
It's used for particles and trees and grass and stuff, ThinMatrix also has a nice tutorial for instancing on youtube.

You basically upload your mesh once and then only buffers with all the position/rotation/etc data.

Yeah, I thought about instanced rendering. Is there another way, though? Instanced rendering can only be used if the meshes I'm rendering are all the same, which is inflexible, because I may want to render different types of meshes.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline KaiHH

JGO Kernel


Medals: 786



« Reply #3 - Posted 2016-10-14 11:58:57 »

No one said that you may only use one single instanced draw call for your entire game world any everything in it.
If you have some number of mesh types, such as block, grass and stone, you just create three batches for each of those respective types, which you can render using three instanced draw calls.
Offline ShadedVertex
« Reply #4 - Posted 2016-10-14 13:25:26 »

No one said that you may only use one single instanced draw call for your entire game world any everything in it.
If you have some number of mesh types, such as block, grass and stone, you just create three batches for each of those respective types, which you can render using three instanced draw calls.

Ahh, I don't really feel like doing instanced rendering because I'm so proud of the batchng, but I guess I've got no choice Tongue
Offline theagentd
« Reply #5 - Posted 2016-10-14 14:05:09 »

Batching has two purposes:

1. Improving performance.
Batching is faster than doing one drawcall per cube. Draw calls are expensive, so packing your vertex data together and drawing it all in one draw call will be faster.

2. As an investment.
In the case of cube worlds, batching also works as an investment. Looping through the volume of an entire cube world is slow as hell. A 1000x1000x1000 world is already 1 billion blocks to check. This is a huge waste to each frame, because in the end only a tiny mesh will be generated for this world, as 99.99% of the world will be either solid ground or air. Hence, we precompute the mesh once and store the mesh, giving us the flexibility of cube worlds with the performance of a mesh.

The problem occurs when you want to change a single cube in the world. It's too slow and expensive to try to modify the mesh based on the cube added/removed, so the only real choice is to regenerate the mesh from scratch. This will cause a massive spike even when just a single cube is changed. The solution is to split up the world into chunks, so that you only need to regenerate the chunks affected by the changed cube instead of the entire world. In theory, this introduces a trade-off, as we now have more than 1 draw call, but even 10 or 100 draw calls isn't really that significant. The main point is to avoid having to regenerate the mesh each frame, which is still the case.

You get further problems if you want to update the terrain every frame. The answer to this is: don't. If you want to animate the terrain textures, you can either update the texture to animate all cubes identically, or do the animation in the shader based on an ID (this way all cubes can be animated individually).

Instancing is NOT a good choice for a cube world. You want to only draw the faces that are between a solid block and an air/transparent block. In 99% of all cases, you won't even be drawing an entire block. Hence, only being able to control visibility with the granularity of an entire cube with all faces visible is not good enough. For a simple case of a flat floor, this will draw 6x as many triangles as a face-based mesh. In addition, instancing is not as effective for small meshes. A cube is only 24 vertices. You should try to have have batches of at least 100 vertices or you'll get reduced GPU performance drawing all the tiny meshes. Basically, on the CPU side instancing is much faster as it's just one draw call, but on the GPU side you're still drawing a crapload of small meshes, which GPUs aren't good at drawing.

TL;DR: Use chunking, and if you need to animate the texture of a cube either update the texture or use a shader to animate it.

Myomyomyo.
Offline ShadedVertex
« Reply #6 - Posted 2016-10-14 14:44:54 »

theagentd:

This was the answer that I wanted, thank you Cheesy

I was kinda thinking along the same lines, because whenever I see people using instanced rendering (on forums, tutorials, etc.) they always use it to render billboards (grass, particles, distant trees, etc.), but not actual cubes and big 3D meshes. That's why I was reluctant to use instanced rendering for the cubes.

Also, my world isn't the typical voxel game terrain. It's just a whole bunch of (randomly positioned) blocks bound within a 200x200x200 space, so there aren't that many cubes. I was just a little scared of regenerating the mesh whenever a cube is modified. Problem is, if one of the cubes is moving constantly and since I can't regenerate the mesh every frame, I'll have to exclude that block from the mesh Sad

Whatever, I can't be too greedy about performance. At the very most, I'll be sending in about 5-10 draw calls, which isn't too bad.
Offline ndnwarrior15
« Reply #7 - Posted 2016-10-14 14:53:23 »

Interesting stuff here, but I had a question I feel is relevant.

Do you know of a tutorial that shows how to batch draw with the programmable pipeline? Or can give an idea on how to pack the data and send it to the shader program? I've seen a few with the old fixed pipeline but am not sure how to accomplish batching with shaders.

I'm assuming, for instance that "attribute vec3 vertices" is only capable of accepting one buffer of vertices that is passed in via glVertexAttribPointer() but how can many different objects be passed in with only one draw call?
Offline theagentd
« Reply #8 - Posted 2016-10-14 16:28:44 »

I'm assuming, for instance that "attribute vec3 vertices" is only capable of accepting one buffer of vertices that is passed in via glVertexAttribPointer() but how can many different objects be passed in with only one draw call?
There are major advantages to placing all your geometry (at least the one that uses the same shader) into a single VBO, just packed sequentially into the buffer. This way, you can bind a single VAO, using index buffer offsets and BaseVertex-versions of the draw calls to read the right mesh inside the VBO. This still requires one draw call per mesh, but the cost of a draw call is proportional to how much state you change inbetween each call, as the OpenGL driver has to do extensive validation, state setup, look up premade resources for certain state combinations, etc.

Myomyomyo.
Offline ndnwarrior15
« Reply #9 - Posted 2016-10-14 17:09:37 »

So if I'm understanding this correctly it's like any type of data streaming where information is grouped together representing different objects.

EDIT: I think I get it. Going to code and check it out, thanks for responding
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline theagentd
« Reply #10 - Posted 2016-10-14 22:07:53 »

Literally just dump all your meshes sequentially in a single VBO. Keep track of the vertex and index offset of each mesh in the buffer, and use those to draw the right one with the *BaseVertex() draw calls. The base vertex allows you add an offset to every single vertex read. This is nice, because it avoids you having to use 32-bit indices if you have more than 65536 vertices. So you can pack 100 meshes with 60000 vertices each and you can still use 16-bit indices.

Myomyomyo.
Pages: [1]
  ignore  |  Print  
 
 

 
Riven (397 views)
2019-09-04 15:33:17

hadezbladez (5280 views)
2018-11-16 13:46:03

hadezbladez (2204 views)
2018-11-16 13:41:33

hadezbladez (5544 views)
2018-11-16 13:35:35

hadezbladez (1150 views)
2018-11-16 13:32:03

EgonOlsen (4584 views)
2018-06-10 19:43:48

EgonOlsen (5462 views)
2018-06-10 19:43:44

EgonOlsen (3119 views)
2018-06-10 19:43:20

DesertCoockie (4015 views)
2018-05-13 18:23:11

nelsongames (4708 views)
2018-04-24 18:15:36
A NON-ideal modular configuration for Eclipse with JavaFX
by philfrei
2019-12-19 19:35:12

Java Gaming Resources
by philfrei
2019-05-14 16:15:13

Deployment and Packaging
by philfrei
2019-05-08 15:15:36

Deployment and Packaging
by philfrei
2019-05-08 15:13:34

Deployment and Packaging
by philfrei
2019-02-17 20:25:53

Deployment and Packaging
by mudlee
2018-08-22 18:09:50

Java Gaming Resources
by gouessej
2018-08-22 08:19:41

Deployment and Packaging
by gouessej
2018-08-22 08:04:08
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!