如何指定UBO绑定为无绑定



我用vec4 colorA和colorB创建了2+个简单的UBO。使用std140的不必要的复杂UBO创建/绑定过程,我只得到一个黑屏。我如何指定哪个索引,而使用glCreate和得到这个混乱的工作,所以我可以选择colorA或colorB?

//APP
glCreateBuffers(1, &testUBO);
glNamedBufferData(testUBO, sizeof(glm::vec4), 0, GL_DYNAMIC_DRAW);
glGetNamedBufferParameterui64vNV(testUBO, GL_BUFFER_GPU_ADDRESS_NV, &uboScene_64);
glMakeNamedBufferResidentNV(testUBO, GL_READ_ONLY);

glm::vec4 myVec4 = glm::vec4(0.f, 1.f, 0.f, 1.f); //add to structs.h
glNamedBufferSubData(testUBO, 0, sizeof(glm::vec4), &myVec4
//SHARED HEADER
typedef glm::vec4 vec4;
layout(std140, binding = 0) uniform sceneBuffer
{
    vec4 colorA;
};
layout(std140, binding = 1) uniform objectBuffer
{
    vec4 colorB;
};
//SHADER PROGRAM
void main()
{
    Ci = colorA;
    Ci = colorB;
}

给定这个着色器:

//GLSL
layout(std140, binding = 0) uniform sceneBuffer
{
    vec4 colorA;
};
layout(std140, binding = 1) uniform objectBuffer
{
    vec4 colorB;
};

和这个c++缓冲区初始化代码:

//Create scene buffer.
glCreateBuffers(1, &sceneUbo);
glNamedBufferStorage(sceneUbo, sizeof(glm::vec4), 0, GL_DYNAMIC_STORAGE_BIT);
glm::vec4 myVec4 = glm::vec4(0.f, 1.f, 0.f, 1.f);
glNamedBufferSubData(sceneUbo, 0, sizeof(glm::vec4), &myVec4);
//Create object buffer
glCreateBuffers(1, &objectUbo);
glNamedBufferStorage(objectUbo, sizeof(glm::vec4), 0, GL_DYNAMIC_STORAGE_BIT);
glm::vec4 myVec4 = glm::vec4(0.f, 1.f, 0.f, 1.f);
glNamedBufferSubData(objectUbo, 0, sizeof(glm::vec4), &myVec4);

NV_uniform_buffer_unified_memory的"无绑定"代码是这样的:

//Get addresses
GLuint64 sceneUboAddr;
glGetNamedBufferParameterui64vNV(sceneUbo, GL_BUFFER_GPU_ADDRESS_NV, &sceneUboAddr);
glMakeNamedBufferResidentNV(sceneUbo, GL_READ_ONLY);
GLuint64 objectUboAddr;
glGetNamedBufferParameterui64vNV(objectUbo, GL_BUFFER_GPU_ADDRESS_NV, &objectUboAddr);
glMakeNamedBufferResidentNV(objectUbo, GL_READ_ONLY);
//You have to call this to turn on bindless buffers.
glEnableClientState(UNIFORM_BUFFER_UNIFIED_NV);
//0 represents the scene UBO's `binding` from GLSL:
glBufferAddressRangeNV(UNIFORM_BUFFER_ADDRESS_NV, 0, sceneUboAddr, sizeof(glm::vec4));
//1 represents the object UBO's `binding` from GLSL:
glBufferAddressRangeNV(UNIFORM_BUFFER_ADDRESS_NV, 1, objectUboAddr, sizeof(glm::vec4));

注意,这个扩展有效地需要glEnable/DisableClientState,这是一个从核心配置文件中删除的函数。所以你需要使用一个兼容性配置文件来使用它

为了证明非盲代码几乎没有"不必要的复杂",这里是:

//The first 0 represents the scene UBO's `binding` from GLSL:
glBindBufferRange(GL_UNIFORM_BUFFER, 0, sceneUbo, 0, sizeof(glm::vec4));
//1 represents the object UBO's `binding` from GLSL:
glBindBufferRange(GL_UNIFORM_BUFFER, 1, objectUbo, 0, sizeof(glm::vec4));

相关内容

  • 没有找到相关文章