每个顶点 ID 而不是顶点索引? [英] Per vertex ID rather than vertex index?

查看:48
本文介绍了每个顶点 ID 而不是顶点索引?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在 Vulkan 中渲染一堆小轴对齐 (2d) 四边形,而不是使用非索引绘制调用,我想尝试最小化传输开销并使用以下方案的索引绘制:

I'm trying to render a bunch of small axis-aligned (2d) quads in Vulkan, and rather than using a non-indexed draw call, I thought to try and minimize transfer overhead and use indexed draw with the following scheme:

#version 450
layout(location = 0) in vec2 inTopleft;
layout(location = 1) in vec2 inExtent;

vec2 positions[6] = vec2[](
    vec2(0, 0),
    vec2(0, 1),
    vec2(1, 0),
    vec2(0, 1),
    vec2(1, 1),
    vec2(1, 0)
);

void main() {
  vec2 position = positions[gl_VertexIndex % 6];

  gl_Position = vec4(inTopleft + position * inExtent, 0, 1);
}

这样我只需要为每个四边形发送一个顶点,然后我只需在索引缓冲区中放置六次相同的顶点,例如:

That way I only need to send one vertex per quad, and then I just put the same vertex six times in the index buffer like:

index_buffer = [0,0,0,0,0,0, 1,1,1,1,1,1, 2,2,2,2,2,2, ... n,n,n,n,n,n]

但是这个方案似乎不起作用,因为我怀疑 gl_VertexIndex 给出了 index_buffer 中元素的值,对吧?我的意思是第一个四边形 gl_VertexIndex 对于所有六个顶点都是 0,然后第二个对于所有六个顶点都是 1,依此类推.它实际上并没有为第一个四边形给出 0、1、2、3、4、5,为第二个四边形给出 6、7、8、9、10、11,依此类推.

but this scheme doesn't seem to work because gl_VertexIndex I suspect is giving the value of the element in the index_buffer, right? I mean for the first quad gl_VertexIndex is 0 for all six verticies, and then the second is 1 for all six verticies, and so on. It's not actually giving 0,1,2,3,4,5 for the first quad, and 6,7,8,9,10,11 for the second quad, and so on.

是吗?如果是这样,有什么办法可以做我想做的事情吗?

Is that right? And if so, is there any way to do what I'm trying to do?

推荐答案

所以我最终使用了一个实例绘制调用(非索引),每个四边形一个实例,这似乎使性能翻倍(200 fps ->500fps,渲染大约 10k 四边形),至少在我的显卡上(NVIDIA Corporation GP106 [GeForce GTX 1060 6GB] (rev a1)).

So I ended up using an instance draw call (non-indexed) with one instance per quad, and that seems to about double performance (200 fps -> 500fps, rendering about 10k quads), at least on my graphics card (NVIDIA Corporation GP106 [GeForce GTX 1060 6GB] (rev a1)).

我的意思是每个四边形都有自己的实例,因此绘制调用的调用方式类似于draw(nvertexes=6, ninstances=nquads),因此着色器可以从:

What I mean is each quad has its own instance and so the draw call is called like draw(nvertexes=6, ninstances=nquads), and so the shader can change from:

- vec2 position = positions[gl_VertexIndex % 6];
+ vec2 position = positions[gl_VertexIndex];

当然,顶点缓冲区现在是每个实例而不是每个顶点.

and of course the vertex buffer is now per instance instead of per vertex.

这篇关于每个顶点 ID 而不是顶点索引?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆