首页 > 解决方案 > OpenGL 批处理渲染器中的纹理出血/损坏

问题描述

使用 C++ 开发引擎,并基于Cherno 的视频系列实现了批处理渲染器。这似乎工作了一段时间。最近注意到,在从事该项目的 2 台计算机中的 1 台上,精灵的一种纹理奇怪地渗入了另一种纹理。

精灵纹理相互渗透

Sprite 1 渗入 Sprite 2]([https://imgur.com/pSolKRb

经过一些研究,我们注意到具体而言,出血是根据它们的渲染顺序发生的。第一个渲染的精灵没有问题,随后的精灵大多是正确的,一小部分像素来自最后绘制的精灵的纹理。

我们认为问题与着色器有关,但批处理渲染器也可能存在问题。

顶点着色器

#version 330 core
layout (location = 0) in vec3 aPos;
layout (location = 1) in vec4 aColor;
layout (location = 2) in vec2 aTexCoord;
layout (location = 3) in int aTexIndex;

out vec4 ourColor;
out vec2 TexCoord;
flat out int TexIndex;

uniform mat4 uViewProjection;

void main()
{
    gl_Position = uViewProjection * vec4(aPos, 1.0);

    ourColor = aColor;
    TexCoord = aTexCoord;
    TexIndex = aTexIndex;
}

片段着色器

#version 330 core
out vec4 FragColor;

in vec4 ourColor;
in vec2 TexCoord;
flat in int TexIndex;

uniform sampler2D ourTextures[32];

void main()
{
    FragColor = texture(ourTextures[TexIndex], TexCoord) * ourColor;
}

批处理渲染器.h

#pragma once
#include "glm/glm.hpp"
#include "Shader.h"
#include "Memory/SmartPointers.h"
namespace Engine::Graphics
{
    // TODO: move this struct to a different header.
    struct Vertex
    {
        glm::vec3 pos;
        glm::vec4 col;
        glm::vec2 texCoords;
        i32 texIndex;
    };

    struct BatchData
    {
        GLuint VAO = 0;
        GLuint VB = 0;
        GLuint IB = 0;

        uint32_t indexCount = 0;

        Vertex* vertexBuffer = nullptr;
        Vertex* vertexBufferCurrentOffset = nullptr;

        GLint* textureSlotsArray = nullptr;
        i32 textureSlotIndex = 0;

        glm::mat4x4 currentViewMatrix = glm::mat4x4();
    };

    class BatchRenderer
    {
    public:
        void Init(SharedRef<Shader>& startShader);
        void Destroy();
        GLuint GetVAOID();

        static uint32_t GetMaxBatchSize();
        static uint32_t GetNoTexID();
        static uint32_t GetQuadsDrawnThisFrame();
        static uint32_t GetDrawCallsThisFrame();
        static void ResetQuadCounter();
        static void ResetDrawCallCounter();
        void BeginBatch();
        void EndBatch();
        void Flush();
        void SetShader(SharedRef<Shader>& shaderPassed);

        void DrawQuad(const glm::vec2& position1, const glm::vec2& position2, const glm::vec2& position3, const glm::vec2& position4, const glm::vec4& colour);
        void DrawQuad(const glm::vec2& position1, const glm::vec2& position2, const glm::vec2& position3, const glm::vec2& position4, uint32_t textureID);
        void DrawQuad(const glm::vec2& position1, const glm::vec2& position2, const glm::vec2& position3, const glm::vec2& position4, uint32_t textureID, const glm::vec4& colour);

        void UpdateViewMatrix(const glm::mat4x4& newMatrix);

        SharedRef<Shader> shaderRef;
        BatchData m_batchData;
        friend class BatchManager;

    };
}

Batch Renderer.cpp - 重要功能,请随意询问您认为我错过的任何内容。

void BatchRenderer::Init(SharedRef<Shader>& startShader)
    {
#ifdef _DEBUG
        assert(m_batchData.vertexBuffer == nullptr && "Batch Renderer already initialized.");
#endif
        shaderRef = startShader;
        shaderRef->Bind();
        GLint maxTextures;
        glGetIntegerv(GL_MAX_TEXTURE_IMAGE_UNITS, &maxTextures);

        MAX_TEXTURES_PER_BATCH = maxTextures;
        if (MAX_TEXTURES_PER_BATCH > 32)
        {
            MAX_TEXTURES_PER_BATCH = 32;
        }

        m_batchData.vertexBuffer = new Vertex[MAX_VERT_COUNT_PER_BATCH];

        glGenVertexArrays(1, &m_batchData.VAO);
        glBindVertexArray(m_batchData.VAO);

        shaderRef->Bind();

        glGenBuffers(1, &m_batchData.VB);
        glBindBuffer(GL_ARRAY_BUFFER, m_batchData.VB);
        glBufferData(GL_ARRAY_BUFFER, MAX_VERT_COUNT_PER_BATCH * sizeof(Vertex), nullptr, GL_DYNAMIC_DRAW);

        glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*)(0 * sizeof(float)));
        glEnableVertexAttribArray(0);

        glVertexAttribPointer(1, 4, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*)(3 * sizeof(float)));
        glEnableVertexAttribArray(1);

        glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*)(7 * sizeof(float)));
        glEnableVertexAttribArray(2);

        //glVertexAttribPointer(3, 1, GL_UNSIGNED, GL_FALSE, sizeof(Vertex), (void*)(9 * sizeof(float))); 
        glVertexAttribIPointer(3, 1, GL_INT, sizeof(Vertex), (void*)(9 * sizeof(float))); 
        glEnableVertexAttribArray(3);

        uint32_t* indices = new uint32_t[MAX_INDEX_COUNT_PER_BATCH];
        uint32_t offset = 0;
        for (size_t i = 0; i < MAX_INDEX_COUNT_PER_BATCH; i += 6)   
        {
            indices[i] = 0 + offset;
            indices[i + 1] = 1 + offset; 
            indices[i + 2] = 2 + offset;

            indices[i + 3] = 2 + offset;
            indices[i + 4] = 3 + offset;
            indices[i + 5] = 0 + offset;

            offset += 4;
        }

        glGenBuffers(1, &m_batchData.IB);
        glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_batchData.IB);
        glBufferData(GL_ELEMENT_ARRAY_BUFFER, MAX_INDEX_COUNT_PER_BATCH * sizeof(uint32_t), indices, GL_STATIC_DRAW); 
        delete[] indices;
        m_batchData.textureSlotsArray = new GLsizei[MAX_TEXTURES_PER_BATCH];
        m_batchData.textureSlotsArray[0] = noTexID; 
        GLint* samplers = new int32_t[MAX_TEXTURES_PER_BATCH];
        samplers[0] = 0;
        for (GLint i = 1; i < MAX_TEXTURES_PER_BATCH; i++)
        {
            m_batchData.textureSlotsArray[i] = 0; //zero out data
            samplers[i] = i;
        }
        glUniform1iv(shaderRef->GetUniformIndex("ourTextures"), static_cast<int>(MAX_TEXTURES_PER_BATCH), samplers);

    }

void BatchRenderer::Destroy()
    {
#ifdef _DEBUG
        assert(m_batchData.vertexBuffer != nullptr && "Batch Renderer not initialized, cannot destroy.");
#endif
        glDeleteVertexArrays(1, &m_batchData.VAO);
        glDeleteBuffers(1, &m_batchData.VB);
        glDeleteBuffers(1, &m_batchData.IB);

        delete[] m_batchData.vertexBuffer;
        delete[] m_batchData.textureSlotsArray;
    }

void BatchRenderer::BeginBatch()
    {
        m_batchData.vertexBufferCurrentOffset = m_batchData.vertexBuffer;
    }

    void BatchRenderer::EndBatch()
    {
        glBindVertexArray(m_batchData.VAO);
        shaderRef->Bind();
        GLsizeiptr totalSize = (uint8_t*)m_batchData.vertexBufferCurrentOffset - (uint8_t*)m_batchData.vertexBuffer;
        glBindBuffer(GL_ARRAY_BUFFER, m_batchData.VB);
        glBufferSubData(GL_ARRAY_BUFFER, 0, totalSize, m_batchData.vertexBuffer);
    }

    void BatchRenderer::Flush() 
    {
        glBindVertexArray(m_batchData.VAO);
        shaderRef->Bind();
        shaderRef->SetMat4x4("uViewProjection", m_batchData.currentViewMatrix);
        for (i32 i = 0; i < m_batchData.textureSlotIndex; i++)
        {
            glBindTextureUnit(i, m_batchData.textureSlotsArray[i]);
        }
        glDrawElements(GL_TRIANGLES, m_batchData.indexCount, GL_UNSIGNED_INT, nullptr);

        m_batchData.indexCount = 0;
        m_batchData.textureSlotIndex = 1;

#ifdef EDITOR
        drawCallsThisFrame++;
#endif
    }

void BatchRenderer::DrawQuad(const glm::vec2& position1, const glm::vec2& position2, const glm::vec2& position3, const glm::vec2& position4, uint32_t textureID, const glm::vec4& colour)
    {
        if (m_batchData.indexCount > QUICK_LOWER_INDEX_ACCESS || m_batchData.textureSlotIndex >= MAX_TEXTURES_PER_BATCH)
        {
            EndBatch();
            Flush();
            BeginBatch();
        }

        i32 textureIndex = -1;
        for (i32 i = 0; i < m_batchData.textureSlotIndex; i++)
        {
            if (m_batchData.textureSlotsArray[i] == textureID)
            {
                textureIndex = i;
                break;
            }
        }

        if (textureIndex == -1)
        {
            textureIndex = m_batchData.textureSlotIndex;
            m_batchData.textureSlotsArray[m_batchData.textureSlotIndex] = static_cast<i32>(textureID);
            m_batchData.textureSlotIndex++;
        }

        m_batchData.vertexBufferCurrentOffset->pos = { position1.x, position1.y, 0.0f };
        m_batchData.vertexBufferCurrentOffset->col = colour;
        m_batchData.vertexBufferCurrentOffset->texCoords = { 0.0f, 0.0f };
        m_batchData.vertexBufferCurrentOffset->texIndex = textureIndex;
        m_batchData.vertexBufferCurrentOffset++;

        m_batchData.vertexBufferCurrentOffset->pos = { position2.x, position2.y, 0.0f };
        m_batchData.vertexBufferCurrentOffset->col = colour;
        m_batchData.vertexBufferCurrentOffset->texCoords = { 1.0f, 0.0f };
        m_batchData.vertexBufferCurrentOffset->texIndex = textureIndex;
        m_batchData.vertexBufferCurrentOffset++;

        m_batchData.vertexBufferCurrentOffset->pos = { position3.x, position3.y, 0.0f };
        m_batchData.vertexBufferCurrentOffset->col = colour;
        m_batchData.vertexBufferCurrentOffset->texCoords = { 1.0f, 1.0f };
        m_batchData.vertexBufferCurrentOffset->texIndex = textureIndex;
        m_batchData.vertexBufferCurrentOffset++;

        m_batchData.vertexBufferCurrentOffset->pos = { position4.x, position4.y, 0.0f };
        m_batchData.vertexBufferCurrentOffset->col = colour;
        m_batchData.vertexBufferCurrentOffset->texCoords = { 0.0f, 1.0f };
        m_batchData.vertexBufferCurrentOffset->texIndex = textureIndex;
        m_batchData.vertexBufferCurrentOffset++;

        m_batchData.indexCount += 6;
#ifdef EDITOR
        quadCountThisFrame++;
#endif
    }

如上所述,如果该信息有用,则该错误仅出现在从事该项目的 2 台 PC 中的 1 台上( AMD RX 580有错误)。

标签: c++openglglslrenderingglfw

解决方案


ourTextures[TexIndex]是未定义的行为,因为ourTextures它是一个采样器数组并且TexIndex是一个片段着色器输入。碎片着色器输入不是动态统一的表达式

请参阅您使用的 GLSL 版本 3.30(来自OpenGL Shading Language 3.30 Specification - 4.1.7 Samplers):

在着色器中聚合成数组的采样器(使用方括号 [ ])只能使用整数常量表达式进行索引

请参阅 GLSL 版本 4.60(最新)(来自OpenGL Shading Language 4.60 Specification - 4.1.7. Opaque Types):(
此规则适用于自 GLSL 4.00 以来的所有版本)

当在着色器中聚合成数组时,这些类型只能使用动态统一表达式进行索引,否则纹理查找将导致未定义的值。

因此,无论是在您使用的 GLSL 版本中,还是在最新版本中,采样器数组都可以通过顶点着色器输入(属性)进行索引。

从 GLSL 4.00 开始,可以通过统一索引采样器数组,因为统一变量索引是动态统一表达式


我建议使用 s sampler2DArray(请参阅Sampler)而不是sampler2D.
当您使用 asampler2DArray时,您根本不需要任何索引,因为“索引”在纹理查找时被编码在纹理坐标的第三个分量中(请参阅 参考资料Texture)。


推荐阅读