glBufferData 崩溃 [英] Crash on glBufferData

查看:75
本文介绍了glBufferData 崩溃的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 glBufferData 有随机崩溃,它只发生在 Windows 上.Mac OS 和 Linux 没有问题.那是我的上传功能,它将我保存在内存中的所有内容上传到 GPU.vertex 是一个浮点向量数组,它是:

I have random crashes at glBufferData, and it only happens on Windows. Mac OS and Linux have no problems. That is my upload function which uploads everything I kept in memory to GPU. vertex is a float vector array which is:

std::vector<float> vertex[MAX_VBO];

代码在第一个 glBufferData 函数处随机崩溃,我调用它是随机的,因为它有时可以工作,但是当我的顶点数据很大时,它几乎肯定会崩溃.

and the code randomly crashes at the first glBufferData function, I am calling that random because it can work sometimes, but when my vertex data is huge it is almost guaranteed to crash.

void VertexBuffer::upload() {
    glGenBuffers(5, vbo);

    printf("VBO %d - %d - %d\n", vertex[vboPosition].size(), vertex[vboNormal].size(), indices.size());
    if (vertex[vboPosition].size() > 0) {
        glBindBuffer(GL_ARRAY_BUFFER, vbo[vboPosition]);
        glBufferData(GL_ARRAY_BUFFER, vertex[vboPosition].size() * sizeof(float)*4, &vertex[vboPosition][0], GL_STATIC_DRAW);
    }


    if (vertex[vboNormal].size() > 0) {
        glBindBuffer(GL_ARRAY_BUFFER, vbo[vboNormal]);
        glBufferData(GL_ARRAY_BUFFER, vertex[vboNormal].size() * sizeof(float)*3, &vertex[vboNormal][0], GL_STATIC_DRAW);
    }


    if (vertex[vboColor].size() > 0) {
        glBindBuffer(GL_ARRAY_BUFFER, vbo[vboColor]);
        glBufferData(GL_ARRAY_BUFFER, vertex[vboColor].size() * sizeof(float)*4, &vertex[vboColor][0], GL_STATIC_DRAW);
    }


    if (vertex[vboUV].size() > 0) {
        glBindBuffer(GL_ARRAY_BUFFER, vbo[vboUV]);
        glBufferData(GL_ARRAY_BUFFER, vertex[vboUV].size() * sizeof(float)*2, &vertex[vboUV][0], GL_STATIC_DRAW);
    }


    if (vertex[vboTangent].size() > 0) {
        glBindBuffer(GL_ARRAY_BUFFER, vbo[vboTangent]);
        glBufferData(GL_ARRAY_BUFFER, vertex[vboTangent].size() * sizeof(float)*3, &vertex[vboTangent][0], GL_STATIC_DRAW);
    }

    glBindBuffer(GL_ARRAY_BUFFER, 0);

    glGenBuffers(1, &vboind);
    if (indices.size() > 0) {
        glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, vboind);
        glBufferData(GL_ELEMENT_ARRAY_BUFFER, indices.size() * sizeof(unsigned int), &indices[0], GL_STATIC_DRAW);
        glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);

    }
    printf("Success \n");
}

更多信息:我已经尝试了发布和调试模式,以防微软编译器在向量之间放置奇怪的东西(不连续等),但它是一样的.我也在使用 OpenGL 3.3 核心配置文件.

More info: I have tried both release and debug mode in case microsoft compiler puts weird stuff between vectors (not continuous etc.) but it is the same. Also I am using OpenGL 3.3 core profile.

推荐答案

看起来您传递给 glBufferData() 的大小远大于数据的实际大小.例如在这个:

It looks like the size you're passing to glBufferData() is much bigger than the actual size of the data. For example in this one:

std::vector<float> vertex[MAX_VBO];
    ...
    glBufferData(GL_ARRAY_BUFFER, vertex[vboPosition].size() * sizeof(float)*4,
            &vertex[vboPosition][0], GL_STATIC_DRAW);

由于 vertex[vboPosition] 是一个浮点数向量,调用 .size() 返回向量中浮点数的数量.然后将该值乘以 sizeof(float) 4,最终乘以 16.但是一个 float 只有 4 个字节,所以这是 4 倍.

Since vertex[vboPosition] is an vector of floats, calling .size() on it returns the number of floats in the vector. You then multiply that value by sizeof(float) and by 4, which ends up multiplying it by 16. But one float is only 4 bytes, so this is 4 times too much.

第二个参数是要加载的数据量(以字节为单位).所以正确的计算是:

The 2nd argument is the amount of data to load in bytes. So the correct calculation is:

    glBufferData(GL_ARRAY_BUFFER, vertex[vboPosition].size() * sizeof(float),
            &vertex[vboPosition][0], GL_STATIC_DRAW);

同样的事情适用于所有其他 glBufferData() 调用.

The same thing applies to all the other glBufferData() calls.

这篇关于glBufferData 崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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