How to render large number of similar objects?

后端 未结 2 473
误落风尘
误落风尘 2021-01-15 21:04

I have large number of objects (at least 10 000 particles) like triangles, squares, circles or spheres. Actually now I have one object which I render many times. It\'s looks

相关标签:
2条回答
  • 2021-01-15 21:40
    1. Factor out the shader binding and unbinding. Bind the shader once, draw everything that uses that shader, and then unbind it. Group your objects by shader. If you have a more complicated material system, you'll want to draw everything with the same material properties at once (e.g. uniforms like texture and lighting components). While you're at it, leave the vertex attributes bound and only turn them off when you're done. All of this minimizes the number of OpenGL calls you do.

    2. If you can, pack your geometry into optimally-sized VBOs (which should vary by your GPU). I suspect you can pack all of your shapes sequentially into one buffer and then use an offset to pick which one to draw. This will save you a VBO switch.

    3. If you're going to be drawing the same thing lots of times, consider instanced rendering

    4. If you're going to be drawing a lot of things a lot of times, group them into batches of like type.

    5. If you're trying to make a particle system, look into making a GPU particle system. Particle attributes like position and velocity get stored in textures, and then fragment shaders update those attributes once per draw call. After they have been updated, a long list of indices ((u,v) coordinates) is passed in as vertex data, which is used to index into the particle textures to retrieve the data. Each particle is then turned into triangles or quads in the geometry shader, and finally rendered to the screen.

    0 讨论(0)
  • 2021-01-15 21:52

    Ordinarily with any sort of optimisation question, the first thing to establish is where the bottleneck is. Optimising on the CPU side is useless if the bottleneck is the shader complexity. However, as you mention 10000 draw calls which is rather excessive, I'm going to just assume that you are CPU bound.

    So top priority will be to reduce your number of draw calls. At the moment, you seem to be achieving 10000 particles with 10000 draw calls.

    Usually, when I do a particle system, all of my particles are rendered as screen facing quads (billboards) with a texture applied using a single texture atlas, and I would aim to render all 10000 particles in a single draw call.

    You describe a slightly more complicated scenario; triangles, squares and circles could all be batched together in a single indexed triangle list, although perhaps you should consider implementing your circles as a textured quad instead of as lots of triangles.

    The sphere is a bit different though because it is a relatively complicated high polygon object. If it really can't be simulated as a billboard, then perhaps you are better off looking into instancing techniques to draw all your spheres at once.

    0 讨论(0)
提交回复
热议问题