调用 glGenBuffers() 时的访问冲突

Access Violation in calling glGenBuffers()

我正在尝试开发游戏引擎并添加了批处理渲染功能。我这里有 2 个问题,

  1. 有一些不寻常的访问冲突错误

    我把Bufferclasses初始化为Renderer的成员变量class

    Scope<VertexBuffer> vertex = CreateScope<OpenGLVertexBuffer>(ME_MAX_VERTEX_BUFFER_SIZE, GL_DYNAMIC_DRAW);
    Scope<IndexBuffer> index = CreateScope<OpenGLIndexBuffer>(ME_MAX_INDEX_BUFFER_SIZE, GL_DYNAMIC_DRAW);
    

    上面的Scope是指unique_ptrCreateScope是指make_unique
    代码文件:
    Header: https://github.com/VikramSGIT/MarsEngine/blob/master/MarsEngine/src/RenderAPI/OpenGL/OpenGLRenderer.cpp
    人民党:https://github.com/VikramSGIT/MarsEngine/blob/master/MarsEngine/src/RenderAPI/OpenGL/OpenGLRenderer.cpp
    Vertex Buffer的构造函数classes(Index Buffer Class有类似的构造函数)

    OpenGLVertexBuffer::OpenGLVertexBuffer(const unsigned int& size, const unsigned int& mode)
        :Emptybuffer(true)
    {
        ME_PROFILE_TRACE_CALL();
    
        GLLogCall(glGenBuffers(1, &m_RendererID));
        GLLogCall(glBindBuffer(GL_ARRAY_BUFFER, m_RendererID));
        GLLogCall(glBufferData(GL_ARRAY_BUFFER, size, nullptr, mode));
    }
    

    正好在 glGenBuffers(1 ,&m_RendererID) 发生了访问冲突。 甚至双重检查我在 glfwMakeCurrentContext(window).

    之后调用了 glewInit()
  2. 当我尝试在上面的 OpenGLVertexBuffer class 构造函数中添加 glewInit() 时。出现了另一个不寻常的问题,其中 glDeleteBuffers(1, &m_RendererID) 未终止!!

    OpenGLVertexBuffer::~OpenGLVertexBuffer()
    {
        ME_PROFILE_TRACE_CALL();
    
        GLLogCall(glDeleteBuffers(1, &m_RendererID));
    }
    

    然后我在VS Code里运行同款代码,运行流畅。所以这得出结论,我没有 driver 问题。

    And to be noted I didn't get an output (was not included as I'm not sure with logic) on my screen and ImGui ran smoothly. But I ran breakpoints over every part of the logic, buffers filled up as expected ba can fain waz goin wroonng :(

Link 到我的 Github 回购:https://github.com/VikramSGIT/MarsEngine

template<typename T>
using Ref = std::shared_ptr<T>;

template<typename T>
using Scope = std::shared_ptr<T>;

template<typename T, typename... Args>
Ref<T> CreateRef(Args&& ... args) { return std::make_shared<T>(std::forward<Args (args)...); }

template<typename T, typename ... Args>
Scope<T> CreateScope(Args&& ...args) { return std::make_unique<T>(std::forward<Args> (args)...); }

您的 CreateScope 函数创建一个 unique_ptr,但您的 Scope 对象存储一个 shared_ptr。我怀疑问题是这样的:

template<typename T>
using Scope = std::shared_ptr<T>;

应该是:

template<typename T>
using Scope = std::unique_ptr<T>;