简体   繁体   English

opengl 3+ 中许多 2d 纹理方块(四边形)的快速方法

[英]Fasted way to many 2d textured squares (quads) in opengl 3+

For a 2d game I need to draw many textured quads each frame.对于 2d 游戏,我需要每帧绘制许多纹理四边形。 They do not share any vertexes and should should be considered to have a random location that change every frame.它们不共享任何顶点,应被视为具有每帧都会改变的随机位置。 As I am using opengl 3+ I can no longer use quads (it has been removed).当我使用 opengl 3+ 时,我不能再使用四边形(它已被删除)。 I could instead use 2 triangles to make each quad but this would create a large amount of redundant reused data being passed to the GPU each frame.我可以改为使用 2 个三角形来制作每个四边形,但这会产生大量重复使用的冗余数据,每帧都传递给 GPU。 So I was wondering what would be the best way to achieve my goal?所以我想知道实现我的目标的最佳方法是什么?

I could create a geometry shader and simply pass it the quads top left point as well as it's width, height and texture position.我可以创建一个几何着色器,然后简单地将四边形的左上角以及它的宽度、高度和纹理位置传递给它。 The shader could then create the 2 triangles.然后着色器可以创建 2 个三角形。 However I think this would be quite slow?但是我认为这会很慢?

You have two potential methods:您有两种可能的方法:

  • Use a trilist (not strips).使用 trilist(而不是条带)。 Trying to use the transforms would be very slow, and unless you have a lot of squares, the data won't be that heavy.尝试使用变换会很慢,除非你有很多方块,否则数据不会那么重。 If you're worried, you can optimize the vertex format and get it down to 8 or 16 bytes per;如果您担心,您可以优化顶点格式并将其降低到每个 8 或 16 个字节; remember that PCIe 2.0 x16 is gigabytes per second of bandwidth, a few megs is reasonable for a particle system or the like.请记住,PCIe 2.0 x16 是每秒千兆字节的带宽,对于粒子系统或类似系统,几兆是合理的。 Indices may help depending on your workload.索引可能会有所帮助,具体取决于您的工作量。
  • Use one or a small batch of triangles, indexed and in a buffer, and use shaders to reposition them.使用一个或一小批三角形,索引并在缓冲区中,并使用着色器重新定位它们。 This takes advantage of batching (draw 100 squares with a set of coords, update the coords in the shader, draw again) and saves memory, but you have to worry about depth (handling it in the shader) and some other details.这利用了批处理(用一组坐标绘制 100 个方块,更新着色器中的坐标,再次绘制)并节省内存,但您必须担心深度(在着色器中处理它)和其他一些细节。 It's not the worst thing ever, and I can't definitely say it's better or not.这不是有史以来最糟糕的事情,我不能肯定地说它更好与否。

You probably should not use a geometry shader.您可能不应该使用几何着色器。 They typically aren't meant to be run every frame, although they certainly can be.它们通常并不意味着每帧都运行,尽管它们当然可以。 You get some benefits, particularly very little memory bandwidth being used, but the hardware requirements are significantly higher and the APIs are much stricter.您会获得一些好处,特别是使用的内存带宽非常少,但硬件要求明显更高,API 也更加严格。 If your data is coming from CPU calculation that can be optimized on there, you may also end up with better performance;如果您的数据来自可以在那里优化的 CPU 计算,那么您最终也可能获得更好的性能; at best you'll have to either translate the calculations or download the data to video memory.充其量您必须转换计算或将数据下载到视频内存。

You absolutely do not want to calculate two triangles from the geometry shader, unless you mean two per square.你绝对不想从几何着色器计算两个三角形,除非你的意思是每平方两个。 The main benefit of shaders is parallelism, so you'd want to run through as many squares as possible and draw those in large batches.着色器的主要优点是并行性,因此您需要遍历尽可能多的方块并大批量绘制它们。 Batching will almost always give you a performance boost, and there are a variety of ways to do it.批处理几乎总是会给您带来性能提升,并且有多种方法可以做到这一点。 Things like changing the transforms in the API are terribly slow and best avoided (probably not much of an issue if you're on 3.0+, but it used to be a huge one).像更改 API 中的转换这样的事情非常缓慢,最好避免(如果您使用的是 3.0+,可能不是什么大问题,但它曾经是一个很大的问题)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM