gpt4 book ai didi

c++ - OpenGL 批处理渲染器中的纹理出血/损坏

转载 作者:行者123 更新时间:2023-12-02 10:02:17 34 4
gpt4 key购买 nike

这个问题在这里已经有了答案:





In a fragment shader, why can't I use a flat input integer to index a uniform array of sampler2D?

(1 个回答)


1年前关闭。




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

Sprite textures bleeding into one another

Sprite 1 Bleeds into Sprite 2]([https://imgur.com/pSolKRb

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

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

顶点着色器

#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;

};
}

批处理渲染器.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 有错误)。

最佳答案

ourTextures[TexIndex]是未定义的行为,因为 ourTextures是一组采样器和 TexIndex是片段着色器输入。碎片着色器输入不是 Dynamically uniform expression .

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

Samplers aggregated into arrays within a shader (using square brackets [ ]) can only be indexed with integral constant expressions



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

When aggregated into arrays within a shader, these types can only be indexed with a dynamically uniform expression, or texture lookup will result in undefined values.



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

从 GLSL 4.00 开始,可以通过统一索引采样器数组,因为统一变量的索引是 dynamically uniform expression .

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

关于c++ - OpenGL 批处理渲染器中的纹理出血/损坏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62025157/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com