在 OpenGL 调用后改变参考数据

Mutating data under reference after OpenGL call

考虑以下案例

glDeleteBuffers(1, &buffer);
// buffer = 0;
...
if(buffer == 0) {
    // Should not pass but does.
}

当然,当执行 glDeleteBuffers 时,buffer 设置为 0,但考虑到发出删除和实际执行之间的延迟,我可能会遇到非零值。

上述条件一直随机通过,这是不希望的。

我的问题是,我可以在不影响排队命令的情况下将零分配给缓冲区吗?我知道在传递价值时,不会发生任何不好的事情,但我发现文档中没有提及如何处理引用。 OpenGL是否在"consumed"之前将数据从引用复制到某个不可变的临时缓冲区?

尝试分配 -1 希望它会产生错误,但由于问题的随机行为,我真的不想依赖 "did not happen, will not happen"。

of course when glDeleteBuffers is executed, buffer is set to 0

不,不是。 None 个 glDelete* 函数将修改您提供的缓冲区名称。

but given the delay between issuing the delete and it actually being executed, I may encounter non-zero value.

即使 glDelete* 函数确实覆盖了您传递的数组,它们也会 立即 覆盖,就像 almost every other OpenGL function that writes to memory you provide.

My question is, can I assign zero to buffer without influencing queued command?

buffer只是一个整数;你可以把它设置成任何你喜欢的。 OpenGL 将继续做它正在做的事情。

当您删除一个 OpenGL 对象时,它只会被实际删除 after the object stops being attached to other objects and after the GPU gets done with whatever processes acted on it。但是是否丢弃对该对象的引用与此无关。

glDeleteBuffers 永远不会改变传递变量的值。实际上,第二个参数是一个const GLuint*,这意味着它是常量,不能修改。

关于传递 -1:除了缓冲区引用是 无符号 整数这一事实外,reference 声明:

glDeleteBuffers silently ignores 0's and names that do not correspond to existing buffer objects

通常和(几乎)所有 OpenGL 调用:您可以将任何您想要的分配给您传递给 OpenGL 的任何变量。当 OpenGL 调用 returns 时,可以保证它已完成对客户端内存的处理(除了少数例外,请阅读 this 了解更多详细信息)。