Honor input layouts in OpenGL vertex bindings

This commit is contained in:
2026-03-25 23:24:06 +08:00
parent 1597181458
commit 2470451d96
3 changed files with 264 additions and 1 deletions

View File

@@ -6,6 +6,8 @@
#include "XCEngine/RHI/RHIPipelineState.h"
#include "XCEngine/RHI/RHIRenderPass.h"
#include "XCEngine/RHI/RHIFramebuffer.h"
#include "XCEngine/RHI/OpenGL/OpenGLDevice.h"
#include <glad/glad.h>
using namespace XCEngine::RHI;
@@ -521,3 +523,125 @@ TEST_P(RHITestFixture, CommandList_SetShader) {
shader->Shutdown();
delete shader;
}
TEST_P(RHITestFixture, CommandList_SetVertexBuffers_InterleavedInputLayoutUsesPipelineLayout) {
if (GetBackendType() != RHIType::OpenGL) {
GTEST_SKIP() << "OpenGL-specific vertex layout binding";
}
auto* openGLDevice = static_cast<OpenGLDevice*>(GetDevice());
ASSERT_NE(openGLDevice, nullptr);
ASSERT_TRUE(openGLDevice->MakeContextCurrent());
GraphicsPipelineDesc pipelineDesc = {};
pipelineDesc.renderTargetFormats[0] = static_cast<uint32_t>(Format::R8G8B8A8_UNorm);
InputElementDesc position = {};
position.semanticName = "POSITION";
position.semanticIndex = 0;
position.format = static_cast<uint32_t>(Format::R32G32B32A32_Float);
position.inputSlot = 0;
position.alignedByteOffset = 0;
pipelineDesc.inputLayout.elements.push_back(position);
InputElementDesc color = {};
color.semanticName = "COLOR";
color.semanticIndex = 0;
color.format = static_cast<uint32_t>(Format::R32G32B32A32_Float);
color.inputSlot = 0;
color.alignedByteOffset = sizeof(float) * 4;
pipelineDesc.inputLayout.elements.push_back(color);
static const char* vertexSource = R"(#version 430
layout(location = 0) in vec4 aPosition;
layout(location = 1) in vec4 aColor;
out vec4 vColor;
void main() {
gl_Position = aPosition;
vColor = aColor;
}
)";
static const char* fragmentSource = R"(#version 430
in vec4 vColor;
layout(location = 0) out vec4 fragColor;
void main() {
fragColor = vColor;
}
)";
pipelineDesc.vertexShader.source.assign(vertexSource, vertexSource + strlen(vertexSource));
pipelineDesc.vertexShader.sourceLanguage = ShaderLanguage::GLSL;
pipelineDesc.vertexShader.profile = L"vs_4_30";
pipelineDesc.fragmentShader.source.assign(fragmentSource, fragmentSource + strlen(fragmentSource));
pipelineDesc.fragmentShader.sourceLanguage = ShaderLanguage::GLSL;
pipelineDesc.fragmentShader.profile = L"fs_4_30";
RHIPipelineState* pipelineState = GetDevice()->CreatePipelineState(pipelineDesc);
ASSERT_NE(pipelineState, nullptr);
BufferDesc bufferDesc = {};
bufferDesc.size = sizeof(float) * 8 * 3;
bufferDesc.stride = sizeof(float) * 8;
bufferDesc.bufferType = static_cast<uint32_t>(BufferType::Vertex);
RHIBuffer* buffer = GetDevice()->CreateBuffer(bufferDesc);
ASSERT_NE(buffer, nullptr);
ResourceViewDesc viewDesc = {};
viewDesc.dimension = ResourceViewDimension::Buffer;
viewDesc.structureByteStride = bufferDesc.stride;
RHIResourceView* vbv = GetDevice()->CreateVertexBufferView(buffer, viewDesc);
ASSERT_NE(vbv, nullptr);
CommandListDesc cmdDesc = {};
cmdDesc.commandListType = static_cast<uint32_t>(CommandQueueType::Direct);
RHICommandList* cmdList = GetDevice()->CreateCommandList(cmdDesc);
ASSERT_NE(cmdList, nullptr);
RHIResourceView* views[] = { vbv };
uint64_t offsets[] = { 0 };
uint32_t strides[] = { bufferDesc.stride };
cmdList->Reset();
cmdList->SetPipelineState(pipelineState);
cmdList->SetVertexBuffers(0, 1, views, offsets, strides);
GLint enabled0 = 0;
GLint enabled1 = 0;
GLint size0 = 0;
GLint size1 = 0;
GLint stride0 = 0;
GLint stride1 = 0;
void* pointer0 = nullptr;
void* pointer1 = nullptr;
glGetVertexAttribiv(0, GL_VERTEX_ATTRIB_ARRAY_ENABLED, &enabled0);
glGetVertexAttribiv(1, GL_VERTEX_ATTRIB_ARRAY_ENABLED, &enabled1);
glGetVertexAttribiv(0, GL_VERTEX_ATTRIB_ARRAY_SIZE, &size0);
glGetVertexAttribiv(1, GL_VERTEX_ATTRIB_ARRAY_SIZE, &size1);
glGetVertexAttribiv(0, GL_VERTEX_ATTRIB_ARRAY_STRIDE, &stride0);
glGetVertexAttribiv(1, GL_VERTEX_ATTRIB_ARRAY_STRIDE, &stride1);
glGetVertexAttribPointerv(0, GL_VERTEX_ATTRIB_ARRAY_POINTER, &pointer0);
glGetVertexAttribPointerv(1, GL_VERTEX_ATTRIB_ARRAY_POINTER, &pointer1);
EXPECT_EQ(enabled0, GL_TRUE);
EXPECT_EQ(enabled1, GL_TRUE);
EXPECT_EQ(size0, 4);
EXPECT_EQ(size1, 4);
EXPECT_EQ(stride0, static_cast<GLint>(bufferDesc.stride));
EXPECT_EQ(stride1, static_cast<GLint>(bufferDesc.stride));
EXPECT_EQ(reinterpret_cast<uintptr_t>(pointer0), 0u);
EXPECT_EQ(reinterpret_cast<uintptr_t>(pointer1), sizeof(float) * 4u);
cmdList->Close();
cmdList->Shutdown();
delete cmdList;
vbv->Shutdown();
delete vbv;
buffer->Shutdown();
delete buffer;
pipelineState->Shutdown();
delete pipelineState;
}