Files
XCEngine/tests/RHI/Vulkan/unit/test_render_pass.cpp

128 lines
4.6 KiB
C++

#if defined(XCENGINE_SUPPORT_VULKAN)
#include "fixtures/VulkanTestFixture.h"
#include "XCEngine/RHI/RHIFramebuffer.h"
#include "XCEngine/RHI/RHIRenderPass.h"
#include "XCEngine/RHI/RHIResourceView.h"
#include "XCEngine/RHI/Vulkan/VulkanTexture.h"
#include <cstdint>
#include <vector>
using namespace XCEngine::RHI;
namespace {
TEST_F(VulkanGraphicsFixture, RenderPassFramebufferBeginRenderPassClearWritesColor) {
AttachmentDesc colorAttachment = {};
colorAttachment.format = Format::R8G8B8A8_UNorm;
colorAttachment.loadOp = LoadAction::Clear;
colorAttachment.storeOp = StoreAction::Store;
AttachmentDesc depthAttachment = {};
depthAttachment.format = Format::D24_UNorm_S8_UInt;
depthAttachment.loadOp = LoadAction::Clear;
depthAttachment.storeOp = StoreAction::Store;
depthAttachment.stencilLoadOp = LoadAction::Clear;
depthAttachment.stencilStoreOp = StoreAction::Store;
RHIRenderPass* renderPass = m_device->CreateRenderPass(1, &colorAttachment, &depthAttachment);
ASSERT_NE(renderPass, nullptr);
RHITexture* colorTexture = m_device->CreateTexture(CreateColorTextureDesc(64, 64));
RHITexture* depthTexture = m_device->CreateTexture(CreateDepthTextureDesc(64, 64));
ASSERT_NE(colorTexture, nullptr);
ASSERT_NE(depthTexture, nullptr);
RHIResourceView* colorView = m_device->CreateRenderTargetView(colorTexture, {});
RHIResourceView* depthView = m_device->CreateDepthStencilView(depthTexture, {});
ASSERT_NE(colorView, nullptr);
ASSERT_NE(depthView, nullptr);
RHIFramebuffer* framebuffer = m_device->CreateFramebuffer(renderPass, 64, 64, 1, &colorView, depthView);
ASSERT_NE(framebuffer, nullptr);
RHICommandList* commandList = CreateCommandList();
ASSERT_NE(commandList, nullptr);
ClearValue clearValues[2] = {};
clearValues[0].color = { 0.25f, 0.5f, 0.75f, 1.0f };
clearValues[1].depth = 1.0f;
clearValues[1].stencil = 0;
commandList->Reset();
commandList->BeginRenderPass(renderPass, framebuffer, Rect{0, 0, 64, 64}, 2, clearValues);
commandList->EndRenderPass();
SubmitAndWait(commandList);
const std::vector<uint8_t> pixels = ReadTextureRgba8(static_cast<VulkanTexture*>(colorTexture));
ASSERT_GE(pixels.size(), 4u);
EXPECT_NEAR(static_cast<int>(pixels[0]), 64, 1);
EXPECT_NEAR(static_cast<int>(pixels[1]), 128, 1);
EXPECT_NEAR(static_cast<int>(pixels[2]), 191, 1);
EXPECT_EQ(pixels[3], 255u);
commandList->Shutdown();
delete commandList;
framebuffer->Shutdown();
delete framebuffer;
delete depthView;
delete colorView;
depthTexture->Shutdown();
delete depthTexture;
colorTexture->Shutdown();
delete colorTexture;
renderPass->Shutdown();
delete renderPass;
}
TEST_F(VulkanGraphicsFixture, CopyResourceCopiesTexturePixels) {
constexpr uint32_t kWidth = 32;
constexpr uint32_t kHeight = 16;
std::vector<uint8_t> sourcePixels(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);
sourcePixels[index + 0] = static_cast<uint8_t>((x * 13) & 0xFF);
sourcePixels[index + 1] = static_cast<uint8_t>((y * 29) & 0xFF);
sourcePixels[index + 2] = static_cast<uint8_t>(((x + y) * 17) & 0xFF);
sourcePixels[index + 3] = 255;
}
}
TextureDesc textureDesc = CreateColorTextureDesc(kWidth, kHeight);
RHITexture* sourceTexture = m_device->CreateTexture(textureDesc, sourcePixels.data(), sourcePixels.size(), kWidth * 4);
RHITexture* destinationTexture = m_device->CreateTexture(textureDesc);
ASSERT_NE(sourceTexture, nullptr);
ASSERT_NE(destinationTexture, nullptr);
RHIResourceView* sourceView = m_device->CreateShaderResourceView(sourceTexture, {});
RHIResourceView* destinationView = m_device->CreateShaderResourceView(destinationTexture, {});
ASSERT_NE(sourceView, nullptr);
ASSERT_NE(destinationView, nullptr);
RHICommandList* commandList = CreateCommandList();
ASSERT_NE(commandList, nullptr);
commandList->Reset();
commandList->CopyResource(destinationView, sourceView);
SubmitAndWait(commandList);
const std::vector<uint8_t> copiedPixels = ReadTextureRgba8(static_cast<VulkanTexture*>(destinationTexture));
EXPECT_EQ(copiedPixels, sourcePixels);
commandList->Shutdown();
delete commandList;
delete destinationView;
delete sourceView;
destinationTexture->Shutdown();
delete destinationTexture;
sourceTexture->Shutdown();
delete sourceTexture;
}
} // namespace
#endif