首页 > 解决方案 > LWJGL Vulkan:添加第二个顶点缓冲区时未绘制任何内容

问题描述

我用 lwjgl 制作了一个小程序,它使用 Vulkan 显示一个彩色三角形,起初我通过单个顶点缓冲区(颜色 + 位置)将所有需要的数据发送到我的着色器。虽然知道仅使用一个顶点缓冲区是最佳实践,但我仍然想尝试通过 2 个不同的顶点缓冲区发送位置和颜色

为了添加我的第二个顶点缓冲区,我创建它的方式与创建第一个顶点缓冲区的方式相同,然后我添加了一个绑定并将第二个属性归因于它,如下所示:

VkVertexInputBindingDescription.Buffer bindingDescriptor = VkVertexInputAttributeDescription.calloc(2)
    .binding(0)
    .stride(2 * 4)
    .inputRate(VK_VERTEX_INPUT_RATE_VERTEX);
//I added this
bindingDescriptor.binding(1)
    .stride(3 * 4)
    .inputRate(VK_VERTEX_INPUT_RATE_VERTEX);

VkVertexInputAttributeDescription.Buffer attributeDescription = VkVertexInputAttributeDescription.calloc(2)
    .get(0)
    .binding(0)
    .location(0)
    .format(VK_FORMAT_R32G32_SFLOAT)
    .offset(0);
attributeDescription.get(1)
    //changed the binding from 0 to 1
    .binding(1)
    .location(1)
    .format(VK_FORMAT_R32G32B32_SFLOAT)
    .offset(0);

然后在渲染通道的定义中,我将第二个顶点缓冲区添加到提交的缓冲区中,vkCmdBindVertexBuffers如下所示:

LongBuffer offsets = memAllocLong(2);
offsets.put(0, 0L);
//added this
offsets.put(1, 0L);
LongBuffer pBuffers = memAllocLong(2);
pBuffers.put(0, verticesBuf);
//added this
pBuffers.put(1, colorBuf);
vkCmdBindVertexBuffers(renderCommandBuffers[i], 0, pBuffers, offsets);

问题是,当我添加第二个顶点缓冲区时,不再显示任何内容,唯一剩下的就是我的背景颜色我错过了什么吗?使用多个顶点缓冲区有什么特别的事情要做吗?

标签: javalwjglvulkan

解决方案


推荐阅读