释放模式不允许大纹理

时间:2011-04-14 13:47:30

标签: visual-c++ opengl buffer textures

MS Visual Studio 2005中是否有任何设置会阻止OpenGL分配大纹理?我遇到以下行为:

情况:

我创建了两个大对象:一个纹理和一个缓冲区,大约260 mb

在调试模式下:

两个对象的分配和使用都可以正常工作。我甚至可以用不同但相当的尺寸多次破坏和重建物体。

在发布模式下:

分配缓冲区,但是当我尝试分配纹理时,OpenGL会抛出[1] OUT_OF_MEMORY错误。此时有大约1000mb的可用视频内存。 [2]

有没有人知道某些MSVC设置/参数/以这种方式影响OpenGL的东西? 混淆

修改

添加一些代码:

这是我创建缓冲区的地方(称为updateBuffer):

glm::u32 size = glm::compMul(size) * sizeof(glm::u16);

util::odprintf("[...] creating updatebuffer\n");
util::odprintf("[...] - for texture size: %s\n", glm::string(size).c_str());
util::odprintf("[...] - new size: %.2fkb\n", size / 1024.0f);

updateBuffer.create(size, GL_STREAM_DRAW);

util::odprintf("[...] - done\n");

此时映射缓冲区,写入数据,取消映射缓冲区并将数据流式传输到图形卡。在下一帧的开头,我创建了纹理:

util::odprintf("[...] initialize new data texture\n");
util::odprintf("[...] - available vram (before): %.2f\n", ogl::getCurrentAvailableVidMem() / 1024.0f);
util::odprintf("[...] - old texture: %d\n", newDataTexture());  // returns opengl handle
util::odprintf("[...] - new size: %s\n", glm::string(size).c_str());

newDataTexture.create(
    GL_R16F,        // internal format
    size,       // size
    0,      // border
    GL_RED,     // data format
    updateGLType,   // data type
    0);     // copy from buffer

util::odprintf("[...] - new texture %d\n", newDataTexture());
util::odprintf("[...] - available vram (after): %.2f\n", ogl::getCurrentAvailableVidMem() / 1024.0f);

(缓冲区当然在此之前绑定到GL_PIXEL_UNPACK_BUFFER)

发布和调试版本的输出相同......但是在发行版中,OpenGL无法创建纹理:

[...] processing scheduled update
[...] creating updatebuffer
[...] - for texture size: ivec3(1376, 1040, 93)
[...] - new size: 259935.00kb
[...] - done
[...] update available
[...] initialize new data texture
[...] - available vram (before): 1004.09
[...] - old texture: 1
[...] - new size: ivec3(1376, 1040, 93)
Eine Ausnahme (erste Chance) bei 0x7c812afb in ....exe: Microsoft C++-Ausnahme: ogl::debug_output an Speicherposition 0x0012dc68..

修改2

as by request:纹理类的代码

#pragma once

#include "GLTypes.h"

#include "ParameterTraits.h"
#include "Wrapper.h"
#include "TextureUnit.h"

namespace ogl {

namespace detail {


    template <>
    struct ReferenceHandler<gl_texture>
    {   
        static 
        void onDestroy(gl_texture & handle){
            TRACE("[Texture] destroying texture %d (%p)\n", handle, &handle);
            glDeleteTextures(1,&handle.t);
            handle = 0;
        };
    };
}


template <GLenum target>
struct texture_traits {};

template <>
struct texture_traits<GL_TEXTURE_1D> {
    typedef int size_t;
};

template <>
struct texture_traits<GL_TEXTURE_2D> {
    typedef glm::ivec2 size_t;
};

template <>
struct texture_traits<GL_TEXTURE_3D> {
    typedef glm::ivec3 size_t;
};



template <GLenum target>
class Texture : public detail::Wrapper<detail::gl_texture>, public detail::TextureUnit::Bindable
{
public:
    typedef typename texture_traits<target>::size_t size_t;

public:

    /**
     * \brief default constructor
     *
     * this actually doesn't do anything useful
     */
    Texture() : Wrapper<detail::gl_texture>() {
        glGenTextures(1,&object.t);
    };

    /**
     * \brief create new texture
     *
     * this constructs a new texture
     * the new texture will have a size and
     * will be empty
     *
     * \param internalFormat    texture format
     * \param size              texture size
     * \param border            texture border
     */
    void create(
        GLint internalFormat,
        size_t size,
        GLint border);

    /**
     * \brief create new texture
     *
     * this constructs a new texture
     * the new texture will have a size and
     * will be empty
     *
     * \param internalFormat    texture format
     * \param size              texture size
     * \param border            texture border
     * \param format            data format
     * \param type              data type
     */
    void create(
        GLint internalFormat,
        size_t size,
        GLint border,
        GLenum format,
        GLenum type,
        void * data);

    /** 
     * \brief virtual deconstructor
     */
    virtual ~Texture(void){};

    /**
     * \brief copy constructor
     */
    Texture & Texture::operator = (Texture const& rhs)
    {
        if (this != &rhs) {
            detail::Wrapper<detail::gl_texture>::operator=(rhs);
        }
        return *this;
    }

    /**
     * \brief update texture data
     *
     * update texture with new data
     *
     * \param level     texture level to update
     * \param offset    offset in texture
     * \param size      size of update data
     * \param format    data format
     * \param type      data type
     * \param data      data
     */
    void update(
        GLint level,
        size_t offset,
        size_t size,
        GLenum format,
        GLenum type,
        void * data);

    /**
     * \brief convenience method that updates the whole texture
     *
     * note: uses getters to determinate size of texture -> slow
     */
    void updateAll(
        GLint level,
        GLenum format,
        GLenum type,
        void * data);

    /**
     * \brief bind texture to texture unit
     *
     * this should not be included in texture. but texture unit is no real
     * object so this is defined here
     */

    void bind(GLenum texUnit) const
    {
        glBindMultiTextureEXT(texUnit, target, object);
    }

    // --- parameter access

    // - setters

    /**
     * \brief set texture parameters
     */
    template <typename T>
    void setParameter(GLenum name, T param);

    template <>
    void setParameter(GLenum name, GLint param)
    {
        glTextureParameteriEXT(object, target, name, param);
    }
    template <>
    void setParameter(GLenum name, GLfloat param)
    {
        glTextureParameterfEXT(object, target, name, param);
    }

    template <>
    void setParameter(GLenum name, GLint * param)
    {
        glTextureParameterivEXT(object, target, name, param);
    }
    template <>
    void setParameter(GLenum name, GLfloat * param)
    {
        glTextureParameterfvEXT(object, target, name, param);
    }

    // - getters

    /**
     * \brief get texture parameters
     */
    template <typename T>
    void getParameter(GLenum name, T & value);

    template <>
    void getParameter(GLenum name, GLfloat & value)
    {
        glGetTextureParameterfvEXT(
            object, target,
            name, &value);
    }
    template <>
    void getParameter(GLenum name, GLint & value)
    {
        glGetTextureParameterivEXT(
            object, target,
            name, &value);
    }

    // - level getters

    /**
     * \brief get texture level parameters
     */
    template <typename T>
    void getLevelParameter(GLint level, GLenum name, T & value);

    template <>
    void getLevelParameter(GLint level, GLenum name, GLint & value)
    {
        glGetTextureLevelParameterivEXT(
            object, target,
            level,
            name, &value);
    }

    template <>
    void getLevelParameter(GLint level, GLenum name, GLfloat & value)
    {
        glGetTextureLevelParameterfvEXT(
            object, target,
            level,
            name, &value);
    }



    /**
     * \brief generate mipmap levels for this texture
     */
    void generateMipmap() {
        glGenerateTextureMipmapEXT(object, target);
    }
};


// --- specialization for 1d texture

template <> 
inline void Texture<GL_TEXTURE_1D>::create(
    GLint internalFormat,
    GLint size,
    GLint border )
{
    //glGenTextures(1,&object.t);

    //std::vector<GLbyte> tmp(size*4);
    glTextureImage1DEXT(
        object,     // texture
        GL_TEXTURE_1D,          // target
        0,              // level
        internalFormat, // internal format
        size,           // size
        border,         // border
        GL_RGBA,        // format
        GL_BYTE,        // type
        0);     // don't load anything
}

template <> 
inline void Texture<GL_TEXTURE_1D>::create(
        GLint internalFormat,
        size_t size,
        GLint border,
        GLenum format,
        GLenum type,
        void * data)
{
    //glGenTextures(1,&object.t);

    glTextureImage1DEXT(
        object,                 // texture
        GL_TEXTURE_1D,          // target
        0,                      // level
        internalFormat,         // internal format
        size,                   // size
        border,                 // border
        format,                 // data format
        type,                   // data type
        data);                  // data
}

template <> 
inline void Texture<GL_TEXTURE_1D>::update(
    GLint level,
    GLint offset,
    GLint size,
    GLenum format,
    GLenum type,
    void * data )
{
    glTextureSubImage1DEXT(
        object,
        GL_TEXTURE_1D,
        level,
        offset,
        size, 
        format,
        type,
        data
        );
}

template <> 
inline void Texture<GL_TEXTURE_1D>::updateAll(
    GLint level,
    GLenum format,
    GLenum type,
    void * data)
{
    GLint size;
    glGetTextureLevelParameterivEXT(
        object,
        GL_TEXTURE_1D,
        level,
        GL_TEXTURE_WIDTH,
        &size
        );

    update(level, 0, size, format, type, data);
}

// --- specialization for 2d texture

template <> 
inline void Texture<GL_TEXTURE_2D>::create(
    GLint internalFormat,
    glm::ivec2 size,
    GLint border )
{
    //glGenTextures(1,&object.t);

    //std::vector<GLbyte> tmp(glm::compMul(size)*4);
    glTextureImage2DEXT(
        object,     // texture
        GL_TEXTURE_2D,          // target
        0,              // level
        internalFormat, // internal format
        size.x, size.y, // size
        border,         // border
        GL_RGBA,        // format
        GL_BYTE,        // type
        0);     // don't load anything
}

template <> 
inline void Texture<GL_TEXTURE_2D>::create(
        GLint internalFormat,
        size_t size,
        GLint border,
        GLenum format,
        GLenum type,
        void * data)
{
    //glGenTextures(1,&object.t);

    glTextureImage2DEXT(
        object,                 // texture
        GL_TEXTURE_2D,          // target
        0,                      // level
        internalFormat,         // internal format
        size.x, size.y,         // size
        border,                 // border
        format,                 // data format
        type,                   // data type
        data);                  // data
}

template <> 
inline void Texture<GL_TEXTURE_2D>::update(
    GLint level,
    glm::ivec2 offset,
    glm::ivec2 size,
    GLenum format,
    GLenum type,
    void * data )
{
    glTextureSubImage2DEXT(
        object,
        GL_TEXTURE_2D,
        level,
        offset.x, offset.y,
        size.x, size.y,
        format,
        type,
        data
        );
}

template <> 
inline void Texture<GL_TEXTURE_2D>::updateAll(
    GLint level,
    GLenum format,
    GLenum type,
    void * data)
{
    glm::ivec2 size;
    glGetTextureLevelParameterivEXT(
        object,
        GL_TEXTURE_2D,
        level,
        GL_TEXTURE_WIDTH,
        &size.x
        );
    glGetTextureLevelParameterivEXT(
        object,
        GL_TEXTURE_2D,
        level,
        GL_TEXTURE_HEIGHT,
        &size.y
        );

    update(level, glm::ivec2(0,0), size, format, type, data);
}

// --- specialization for 3d texture

template <> 
inline void Texture<GL_TEXTURE_3D>::create(
    GLint internalFormat,
    glm::ivec3 size,
    GLint border )
{
    //glGenTextures(1,&object.t);

    //std::vector<GLbyte> tmp(glm::compMul(size)*4);
    glTextureImage3DEXT(
        object,                 // texture
        GL_TEXTURE_3D,          // target
        0,                      // level
        internalFormat,         // internal format
        size.x, size.y, size.z, // size
        border,                 // border
        GL_RGBA,                // format
        GL_BYTE,                // type
        0);             // don't load anything
}

template <> 
inline void Texture<GL_TEXTURE_3D>::create(
        GLint internalFormat,
        size_t size,
        GLint border,
        GLenum format,
        GLenum type,
        void * data)
{
    //glGenTextures(1,&object.t);

    glTextureImage3DEXT(
        object,                 // texture
        GL_TEXTURE_3D,          // target
        0,                      // level
        internalFormat,         // internal format
        size.x, size.y, size.z, // size
        border,                 // border
        format,                 // data format
        type,                   // data type
        data);                  // data
}

template <> 
inline void Texture<GL_TEXTURE_3D>::update(
    GLint level,
    glm::ivec3 offset,
    glm::ivec3 size,
    GLenum format,
    GLenum type,
    void * data ) 
{
    glTextureSubImage3DEXT(
        object,
        GL_TEXTURE_3D,
        level,
        offset.x, offset.y, offset.z,
        size.x, size.y, size.z,
        format,
        type,
        data
        );
}

template <> 
inline void Texture<GL_TEXTURE_3D>::updateAll(
    GLint level,
    GLenum format,
    GLenum type,
    void * data)
{
    glm::ivec3 size;
    glGetTextureLevelParameterivEXT(
        object,
        GL_TEXTURE_3D,
        level,
        GL_TEXTURE_WIDTH,
        &size.x
        );
    glGetTextureLevelParameterivEXT(
        object,
        GL_TEXTURE_3D,
        level,
        GL_TEXTURE_HEIGHT,
        &size.y
        );
    glGetTextureLevelParameterivEXT(
        object,
        GL_TEXTURE_3D,
        level,
        GL_TEXTURE_DEPTH,
        &size.z
        );

    update(level, glm::ivec3(0,0,0), size, format, type, data);
}

typedef Texture<GL_TEXTURE_1D> Texture1d;
typedef Texture<GL_TEXTURE_2D> Texture2d;
typedef Texture<GL_TEXTURE_3D> Texture3d;

};

如您所见,我正在使用EXT_direct_state_access。 “Wrapper”类是标准的refcounted设计。我不能透露那个...但我可以肯定地说它已被证明有效。

请继续询问您是否需要了解更多......我的想法已经不多了。 :(

**解决方案**

man ...有人在发布版本中禁用了/ LARGEMEMORYAWARE ...感谢所有试图围绕这个问题的人:)

[1]我正在使用ARB_debug_output [2]用GL_NVX_gpu_memory_info检查了

1 个答案:

答案 0 :(得分:1)

你没有使用OpenGL,你正在使用一些反过来使用OpenGL的C ++库。我会在那里找你的问题。

这个库是纯头文件(使用编译器设置从项目中的源代码重新编译),还是有二进制组件?二进制库通常不能很好地处理已更改的编译器设置,例如Debug和Release配置之间的区别。