Fix OpenGL sampler and copy semantics

This commit is contained in:
2026-03-26 02:14:21 +08:00
parent c47e871c5a
commit 1ef3048da1
6 changed files with 215 additions and 6 deletions

View File

@@ -9,6 +9,7 @@
#include "XCEngine/RHI/OpenGL/OpenGLDevice.h"
#include "XCEngine/RHI/OpenGL/OpenGLResourceView.h"
#include <glad/glad.h>
#include <vector>
using namespace XCEngine::RHI;
@@ -538,6 +539,74 @@ TEST_P(RHITestFixture, CommandList_CopyResource_WithRealResources) {
delete dstTexture;
}
TEST_P(RHITestFixture, CommandList_CopyResource_UsesActualTextureDimensionsOnOpenGL) {
if (GetBackendType() != RHIType::OpenGL) {
GTEST_SKIP() << "OpenGL-specific copy verification";
}
constexpr uint32_t kWidth = 64;
constexpr uint32_t kHeight = 32;
std::vector<uint8_t> srcPixels(kWidth * kHeight * 4);
for (uint32_t y = 0; y < kHeight; ++y) {
for (uint32_t x = 0; x < kWidth; ++x) {
const size_t index = static_cast<size_t>((y * kWidth + x) * 4);
srcPixels[index + 0] = static_cast<uint8_t>(x * 3);
srcPixels[index + 1] = static_cast<uint8_t>(y * 5);
srcPixels[index + 2] = static_cast<uint8_t>((x + y) * 7);
srcPixels[index + 3] = 255;
}
}
TextureDesc texDesc = {};
texDesc.width = kWidth;
texDesc.height = kHeight;
texDesc.depth = 1;
texDesc.mipLevels = 1;
texDesc.arraySize = 1;
texDesc.format = static_cast<uint32_t>(Format::R8G8B8A8_UNorm);
texDesc.textureType = static_cast<uint32_t>(TextureType::Texture2D);
texDesc.sampleCount = 1;
texDesc.sampleQuality = 0;
RHITexture* srcTexture = GetDevice()->CreateTexture(texDesc, srcPixels.data(), srcPixels.size(), kWidth * 4);
ASSERT_NE(srcTexture, nullptr);
RHITexture* dstTexture = GetDevice()->CreateTexture(texDesc);
ASSERT_NE(dstTexture, nullptr);
RHIResourceView* srcView = GetDevice()->CreateShaderResourceView(srcTexture, {});
RHIResourceView* dstView = GetDevice()->CreateShaderResourceView(dstTexture, {});
ASSERT_NE(srcView, nullptr);
ASSERT_NE(dstView, nullptr);
CommandListDesc cmdDesc = {};
cmdDesc.commandListType = static_cast<uint32_t>(CommandQueueType::Direct);
RHICommandList* cmdList = GetDevice()->CreateCommandList(cmdDesc);
ASSERT_NE(cmdList, nullptr);
cmdList->Reset();
cmdList->CopyResource(dstView, srcView);
glFinish();
cmdList->Close();
std::vector<uint8_t> dstPixels(kWidth * kHeight * 4, 0);
const GLuint dstTextureId = static_cast<OpenGLResourceView*>(dstView)->GetTexture();
glBindTexture(GL_TEXTURE_2D, dstTextureId);
glGetTexImage(GL_TEXTURE_2D, 0, GL_RGBA, GL_UNSIGNED_BYTE, dstPixels.data());
glBindTexture(GL_TEXTURE_2D, 0);
EXPECT_EQ(dstPixels, srcPixels);
cmdList->Shutdown();
delete cmdList;
delete srcView;
delete dstView;
srcTexture->Shutdown();
delete srcTexture;
dstTexture->Shutdown();
delete dstTexture;
}
TEST_P(RHITestFixture, CommandList_BeginEndRenderPass_Basic) {
AttachmentDesc colorDesc = {};
colorDesc.format = Format::R8G8B8A8_UNorm;

View File

@@ -1,5 +1,7 @@
#include "fixtures/RHITestFixture.h"
#include "XCEngine/RHI/RHISampler.h"
#include "XCEngine/RHI/OpenGL/OpenGLSampler.h"
#include <glad/glad.h>
using namespace XCEngine::RHI;
@@ -75,3 +77,51 @@ TEST_P(RHITestFixture, Sampler_GetNativeHandle) {
sampler->Shutdown();
delete sampler;
}
TEST_P(RHITestFixture, Sampler_OpenGLHonorsSamplerDesc) {
if (GetBackendType() != RHIType::OpenGL) {
GTEST_SKIP() << "OpenGL-specific sampler parameter verification";
}
SamplerDesc desc = {};
desc.filter = static_cast<uint32_t>(FilterMode::ComparisonAnisotropic);
desc.addressU = static_cast<uint32_t>(TextureAddressMode::Clamp);
desc.addressV = static_cast<uint32_t>(TextureAddressMode::Mirror);
desc.addressW = static_cast<uint32_t>(TextureAddressMode::Border);
desc.maxAnisotropy = 8;
desc.comparisonFunc = static_cast<uint32_t>(ComparisonFunc::GreaterEqual);
desc.minLod = 1.0f;
desc.maxLod = 4.0f;
RHISampler* sampler = GetDevice()->CreateSampler(desc);
ASSERT_NE(sampler, nullptr);
const GLuint samplerId = static_cast<OpenGLSampler*>(sampler)->GetID();
GLint value = 0;
glGetSamplerParameteriv(samplerId, GL_TEXTURE_WRAP_S, &value);
EXPECT_EQ(value, GL_CLAMP_TO_EDGE);
glGetSamplerParameteriv(samplerId, GL_TEXTURE_WRAP_T, &value);
EXPECT_EQ(value, GL_MIRRORED_REPEAT);
glGetSamplerParameteriv(samplerId, GL_TEXTURE_WRAP_R, &value);
EXPECT_EQ(value, GL_CLAMP_TO_BORDER);
glGetSamplerParameteriv(samplerId, GL_TEXTURE_MIN_FILTER, &value);
EXPECT_EQ(value, GL_LINEAR_MIPMAP_LINEAR);
glGetSamplerParameteriv(samplerId, GL_TEXTURE_MAG_FILTER, &value);
EXPECT_EQ(value, GL_LINEAR);
glGetSamplerParameteriv(samplerId, GL_TEXTURE_COMPARE_MODE, &value);
EXPECT_EQ(value, GL_COMPARE_REF_TO_TEXTURE);
glGetSamplerParameteriv(samplerId, GL_TEXTURE_COMPARE_FUNC, &value);
EXPECT_EQ(value, GL_GEQUAL);
GLfloat floatValue = 0.0f;
glGetSamplerParameterfv(samplerId, GL_TEXTURE_MAX_ANISOTROPY, &floatValue);
EXPECT_GE(floatValue, 8.0f);
glGetSamplerParameterfv(samplerId, GL_TEXTURE_MIN_LOD, &floatValue);
EXPECT_FLOAT_EQ(floatValue, 1.0f);
glGetSamplerParameterfv(samplerId, GL_TEXTURE_MAX_LOD, &floatValue);
EXPECT_FLOAT_EQ(floatValue, 4.0f);
sampler->Shutdown();
delete sampler;
}