From 1797e7fe1707cbb42f22f95b3a8569915674f7b8 Mon Sep 17 00:00:00 2001 From: ssdfasd <2156608475@qq.com> Date: Sun, 22 Mar 2026 14:33:57 +0800 Subject: [PATCH] fix: encapsulate OpenGL types in VertexAttribute to eliminate raw GL API usage in tests - Add VertexAttributeType and VertexAttributeNormalized enums in OpenGLVertexArray.h - Add ToGLAttributeType() converter in OpenGLVertexArray.cpp - Remove glActiveTexture() call from quad test (already handled by texture.Bind()) - Remove #include from triangle test - Update unit tests to use encapsulated enums All three OpenGL integration tests (minimal, triangle, quad) pass with 0% pixel difference. --- .../audio-loader/{canload.md => can-load.md} | 0 ...ultsettings.md => get-default-settings.md} | 0 ...ensions.md => get-supported-extensions.md} | 0 docs/api/resources/audioclip/audio-clip.md | 89 ------------------- .../XCEngine/RHI/OpenGL/OpenGLVertexArray.h | 25 +++++- engine/src/RHI/OpenGL/OpenGLVertexArray.cpp | 24 ++++- tests/RHI/OpenGL/integration/quad/main.cpp | 9 +- .../RHI/OpenGL/integration/triangle/main.cpp | 10 +-- tests/RHI/OpenGL/unit/test_command_list.cpp | 4 +- tests/RHI/OpenGL/unit/test_vertex_array.cpp | 12 +-- 10 files changed, 61 insertions(+), 112 deletions(-) rename docs/api/resources/audio-loader/{canload.md => can-load.md} (100%) rename docs/api/resources/audio-loader/{getdefaultsettings.md => get-default-settings.md} (100%) rename docs/api/resources/audio-loader/{getsupportedextensions.md => get-supported-extensions.md} (100%) delete mode 100644 docs/api/resources/audioclip/audio-clip.md diff --git a/docs/api/resources/audio-loader/canload.md b/docs/api/resources/audio-loader/can-load.md similarity index 100% rename from docs/api/resources/audio-loader/canload.md rename to docs/api/resources/audio-loader/can-load.md diff --git a/docs/api/resources/audio-loader/getdefaultsettings.md b/docs/api/resources/audio-loader/get-default-settings.md similarity index 100% rename from docs/api/resources/audio-loader/getdefaultsettings.md rename to docs/api/resources/audio-loader/get-default-settings.md diff --git a/docs/api/resources/audio-loader/getsupportedextensions.md b/docs/api/resources/audio-loader/get-supported-extensions.md similarity index 100% rename from docs/api/resources/audio-loader/getsupportedextensions.md rename to docs/api/resources/audio-loader/get-supported-extensions.md diff --git a/docs/api/resources/audioclip/audio-clip.md b/docs/api/resources/audioclip/audio-clip.md deleted file mode 100644 index e1de38c3..00000000 --- a/docs/api/resources/audioclip/audio-clip.md +++ /dev/null @@ -1,89 +0,0 @@ -# AudioClip - -**命名空间**: `XCEngine::Resources` - -**类型**: `class` - -**描述**: 音频片段资源类,管理音频样本数据、格式信息和播放参数。 - -## 概述 - -`AudioClip` 是 XCEngine 中的音频资源类,继承自 `IResource`。它管理音频的原始样本数据、采样率、通道数、位深度、时长、格式类型和播放参数。AudioClip 支持 WAV、OGG、MP3、FLAC 等多种音频格式,可用于音效、音乐、语音和环境音等不同用途。 - -## 公共方法 - -| 方法 | 描述 | -|------|------| -| [`AudioClip`](audio-clip.md) | 构造函数 | -| [`~AudioClip`](dtor.md) | 析构函数 | -| [`GetType`](get-type.md) | 获取资源类型 | -| [`GetName`](get-name.md) | 获取音频名称 | -| [`GetPath`](get-path.md) | 获取音频路径 | -| [`GetGUID`](get-guid.md) | 获取全局唯一标识符 | -| [`IsValid`](is-valid.md) | 检查音频是否有效 | -| [`GetMemorySize`](get-memory-size.md) | 获取内存大小 | -| [`Release`](release.md) | 释放音频资源 | -| [`SetAudioData`](setaudiodata.md) | 设置音频数据 | -| [`GetAudioData`](get-audio-data.md) | 获取音频数据 | -| [`SetSampleRate`](set-sample-rate.md) | 设置采样率 | -| [`GetSampleRate`](get-sample-rate.md) | 获取采样率 | -| [`SetChannels`](set-channels.md) | 设置通道数 | -| [`GetChannels`](get-channels.md) | 获取通道数 | -| [`SetBitsPerSample`](set-bits-per-sample.md) | 设置位深度 | -| [`GetBitsPerSample`](get-bits-per-sample.md) | 获取位深度 | -| [`SetDuration`](set-duration.md) | 设置时长 | -| [`GetDuration`](get-duration.md) | 获取时长 | -| [`SetAudioFormat`](set-audio-format.md) | 设置音频格式 | -| [`GetAudioFormat`](get-audio-format.md) | 获取音频格式 | -| [`SetAudioType`](set-audio-type.md) | 设置音频类型 | -| [`GetAudioType`](get-audio-type.md) | 获取音频类型 | -| [`SetIs3D`](set-is-3d.md) | 设置是否为 3D 音频 | -| [`Is3D`](is-3d.md) | 检查是否为 3D 音频 | -| [`SetLoop`](set-loop.md) | 设置是否循环播放 | -| [`IsLoop`](is-loop.md) | 检查是否循环播放 | -| [`GetRHIResource`](get-rhi-resource.md) | 获取 RHI 音频缓冲区 | -| [`SetRHIResource`](set-rhi-resource.md) | 设置 RHI 音频缓冲区 | - -## 使用示例 - -```cpp -#include "Resources/AudioClip.h" -#include "Resources/ResourceManager.h" -#include "Resources/ResourceFileSystem.h" - -using namespace XCEngine::Resources; - -AudioClip* CreateExplosionSound() { - AudioClip* clip = new AudioClip(); - - auto wavData = ResourceFileSystem::Get().ReadResource("sounds/explosion.wav"); - clip->SetAudioData(wavData); - clip->SetSampleRate(44100); - clip->SetChannels(2); - clip->SetBitsPerSample(16); - clip->SetDuration(1.5f); - clip->SetAudioFormat(AudioFormat::WAV); - clip->SetAudioType(AudioType::SoundEffect); - clip->SetLoop(false); - clip->SetIs3D(false); - - return clip; -} - -void PlaySoundEffect() { - ResourceHandle sfx = ResourceManager::Get().Load("sounds/explosion.wav"); - - if (sfx->IsValid()) { - uint32_t sampleRate = sfx->GetSampleRate(); - float duration = sfx->GetDuration(); - bool loop = sfx->IsLoop(); - AudioFormat format = sfx->GetAudioFormat(); - AudioType type = sfx->GetAudioType(); - } -} -``` - -## 相关文档 - -- [IResource](../iresource/iresource.md) - 资源基类 -- [Resources](../resources.md) - 资源模块总览 diff --git a/engine/include/XCEngine/RHI/OpenGL/OpenGLVertexArray.h b/engine/include/XCEngine/RHI/OpenGL/OpenGLVertexArray.h index af976f90..df0efc9b 100644 --- a/engine/include/XCEngine/RHI/OpenGL/OpenGLVertexArray.h +++ b/engine/include/XCEngine/RHI/OpenGL/OpenGLVertexArray.h @@ -6,11 +6,32 @@ namespace XCEngine { namespace RHI { +enum class VertexAttributeType { + Float = 0, + Int, + UnsignedInt, + Short, + UnsignedShort, + Byte, + UnsignedByte, + Double, + HalfFloat, + Fixed, + Int2101010Rev, + UnsignedInt2101010Rev, + UnsignedInt10F11F11FRev +}; + +enum class VertexAttributeNormalized { + False = 0, + True +}; + struct VertexAttribute { unsigned int index; int count; - unsigned int type; - bool normalized; + VertexAttributeType type; + VertexAttributeNormalized normalized; size_t stride; size_t offset; }; diff --git a/engine/src/RHI/OpenGL/OpenGLVertexArray.cpp b/engine/src/RHI/OpenGL/OpenGLVertexArray.cpp index a344bb7b..00bdeb2a 100644 --- a/engine/src/RHI/OpenGL/OpenGLVertexArray.cpp +++ b/engine/src/RHI/OpenGL/OpenGLVertexArray.cpp @@ -4,6 +4,25 @@ namespace XCEngine { namespace RHI { +static unsigned int ToGLAttributeType(VertexAttributeType type) { + switch (type) { + case VertexAttributeType::Float: return GL_FLOAT; + case VertexAttributeType::Int: return GL_INT; + case VertexAttributeType::UnsignedInt: return GL_UNSIGNED_INT; + case VertexAttributeType::Short: return GL_SHORT; + case VertexAttributeType::UnsignedShort: return GL_UNSIGNED_SHORT; + case VertexAttributeType::Byte: return GL_BYTE; + case VertexAttributeType::UnsignedByte: return GL_UNSIGNED_BYTE; + case VertexAttributeType::Double: return GL_DOUBLE; + case VertexAttributeType::HalfFloat: return GL_HALF_FLOAT; + case VertexAttributeType::Fixed: return GL_FIXED; + case VertexAttributeType::Int2101010Rev: return GL_INT_2_10_10_10_REV; + case VertexAttributeType::UnsignedInt2101010Rev: return GL_UNSIGNED_INT_2_10_10_10_REV; + case VertexAttributeType::UnsignedInt10F11F11FRev: return GL_UNSIGNED_INT_10F_11F_11F_REV; + default: return GL_FLOAT; + } +} + OpenGLVertexArray::OpenGLVertexArray() : m_vao(0) , m_indexBuffer(0) @@ -24,8 +43,9 @@ void OpenGLVertexArray::AddVertexBuffer(unsigned int buffer, const VertexAttribu glBindVertexArray(m_vao); glBindBuffer(GL_ARRAY_BUFFER, buffer); glEnableVertexAttribArray(attribute.index); - glVertexAttribPointer(attribute.index, attribute.count, attribute.type, - attribute.normalized ? GL_TRUE : GL_FALSE, + glVertexAttribPointer(attribute.index, attribute.count, + ToGLAttributeType(attribute.type), + attribute.normalized == VertexAttributeNormalized::True ? GL_TRUE : GL_FALSE, attribute.stride, (void*)attribute.offset); glBindBuffer(GL_ARRAY_BUFFER, 0); glBindVertexArray(0); diff --git a/tests/RHI/OpenGL/integration/quad/main.cpp b/tests/RHI/OpenGL/integration/quad/main.cpp index 958775fa..a4c2820a 100644 --- a/tests/RHI/OpenGL/integration/quad/main.cpp +++ b/tests/RHI/OpenGL/integration/quad/main.cpp @@ -127,8 +127,8 @@ int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine VertexAttribute posAttr = {}; posAttr.index = 0; posAttr.count = 4; - posAttr.type = GL_FLOAT; - posAttr.normalized = GL_FALSE; + posAttr.type = VertexAttributeType::Float; + posAttr.normalized = VertexAttributeNormalized::False; posAttr.stride = sizeof(Vertex); posAttr.offset = 0; vertexArray.AddVertexBuffer(vertexBuffer.GetID(), posAttr); @@ -136,8 +136,8 @@ int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine VertexAttribute texAttr = {}; texAttr.index = 1; texAttr.count = 2; - texAttr.type = GL_FLOAT; - texAttr.normalized = GL_FALSE; + texAttr.type = VertexAttributeType::Float; + texAttr.normalized = VertexAttributeNormalized::False; texAttr.stride = sizeof(Vertex); texAttr.offset = sizeof(float) * 4; vertexArray.AddVertexBuffer(vertexBuffer.GetID(), texAttr); @@ -200,7 +200,6 @@ int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine pipelineState.Bind(); vertexArray.Bind(); - glActiveTexture(GL_TEXTURE0); texture.Bind(0); sampler.Bind(0); diff --git a/tests/RHI/OpenGL/integration/triangle/main.cpp b/tests/RHI/OpenGL/integration/triangle/main.cpp index 04e6e554..cb892ee8 100644 --- a/tests/RHI/OpenGL/integration/triangle/main.cpp +++ b/tests/RHI/OpenGL/integration/triangle/main.cpp @@ -5,8 +5,6 @@ #include -#include - #include "XCEngine/RHI/OpenGL/OpenGLDevice.h" #include "XCEngine/RHI/OpenGL/OpenGLSwapChain.h" #include "XCEngine/RHI/OpenGL/OpenGLCommandList.h" @@ -124,8 +122,8 @@ int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine VertexAttribute posAttr = {}; posAttr.index = 0; posAttr.count = 4; - posAttr.type = GL_FLOAT; - posAttr.normalized = GL_FALSE; + posAttr.type = VertexAttributeType::Float; + posAttr.normalized = VertexAttributeNormalized::False; posAttr.stride = sizeof(Vertex); posAttr.offset = 0; vertexArray.AddVertexBuffer(vertexBuffer.GetID(), posAttr); @@ -133,8 +131,8 @@ int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine VertexAttribute colAttr = {}; colAttr.index = 1; colAttr.count = 4; - colAttr.type = GL_FLOAT; - colAttr.normalized = GL_FALSE; + colAttr.type = VertexAttributeType::Float; + colAttr.normalized = VertexAttributeNormalized::False; colAttr.stride = sizeof(Vertex); colAttr.offset = sizeof(float) * 4; vertexArray.AddVertexBuffer(vertexBuffer.GetID(), colAttr); diff --git a/tests/RHI/OpenGL/unit/test_command_list.cpp b/tests/RHI/OpenGL/unit/test_command_list.cpp index 97c99b14..ad847595 100644 --- a/tests/RHI/OpenGL/unit/test_command_list.cpp +++ b/tests/RHI/OpenGL/unit/test_command_list.cpp @@ -54,8 +54,8 @@ TEST_F(OpenGLTestFixture, CommandList_Draw_VAO) { VertexAttribute attr; attr.index = 0; attr.count = 3; - attr.type = GL_FLOAT; - attr.normalized = false; + attr.type = VertexAttributeType::Float; + attr.normalized = VertexAttributeNormalized::False; attr.stride = sizeof(float) * 3; attr.offset = 0; vao.AddVertexBuffer(vbo.GetID(), attr); diff --git a/tests/RHI/OpenGL/unit/test_vertex_array.cpp b/tests/RHI/OpenGL/unit/test_vertex_array.cpp index 9069a3f4..bff67c6f 100644 --- a/tests/RHI/OpenGL/unit/test_vertex_array.cpp +++ b/tests/RHI/OpenGL/unit/test_vertex_array.cpp @@ -25,8 +25,8 @@ TEST_F(OpenGLTestFixture, VertexArray_AddVertexBuffer) { VertexAttribute attr; attr.index = 0; attr.count = 3; - attr.type = GL_FLOAT; - attr.normalized = false; + attr.type = VertexAttributeType::Float; + attr.normalized = VertexAttributeNormalized::False; attr.stride = sizeof(float) * 3; attr.offset = 0; @@ -84,16 +84,16 @@ TEST_F(OpenGLTestFixture, VertexArray_Bind_MultipleAttributes) { VertexAttribute attrPos; attrPos.index = 0; attrPos.count = 2; - attrPos.type = GL_FLOAT; - attrPos.normalized = false; + attrPos.type = VertexAttributeType::Float; + attrPos.normalized = VertexAttributeNormalized::False; attrPos.stride = sizeof(float) * 2; attrPos.offset = 0; VertexAttribute attrColor; attrColor.index = 1; attrColor.count = 4; - attrColor.type = GL_FLOAT; - attrColor.normalized = false; + attrColor.type = VertexAttributeType::Float; + attrColor.normalized = VertexAttributeNormalized::False; attrColor.stride = sizeof(float) * 4; attrColor.offset = 0;