From 3f8805cde83f9d0b681debe0fbc7550c5b3cac16 Mon Sep 17 00:00:00 2001 From: ssdfasd <2156608475@qq.com> Date: Tue, 17 Mar 2026 04:02:35 +0800 Subject: [PATCH] Add Phase 3 tests for Buffer and Texture - Buffer: CreateBuffer (Default/Upload Heap), GPU Virtual Address, Map/Unmap, Alignment - Texture: 2D, 3D, MipLevels, TextureArray - All 38 tests pass --- tests/RHI/D3D12/test_buffer.cpp | 147 ++++++++++++++++++++++++++++++- tests/RHI/D3D12/test_texture.cpp | 141 ++++++++++++++++++++++++++++- 2 files changed, 286 insertions(+), 2 deletions(-) diff --git a/tests/RHI/D3D12/test_buffer.cpp b/tests/RHI/D3D12/test_buffer.cpp index 59bbfe2c..6f06a493 100644 --- a/tests/RHI/D3D12/test_buffer.cpp +++ b/tests/RHI/D3D12/test_buffer.cpp @@ -1,5 +1,150 @@ #include "fixtures/D3D12TestFixture.h" TEST_F(D3D12TestFixture, Buffer_Placeholder) { - ASSERT_TRUE(true); + ASSERT_NE(GetDevice(), nullptr); +} + +TEST_F(D3D12TestFixture, Buffer_CreateBuffer_DefaultHeap) { + const uint64_t bufferSize = 1024; + + D3D12_HEAP_PROPERTIES heapProps = {}; + heapProps.Type = D3D12_HEAP_TYPE_DEFAULT; + heapProps.CPUPageProperty = D3D12_CPU_PAGE_PROPERTY_UNKNOWN; + heapProps.MemoryPoolPreference = D3D12_MEMORY_POOL_UNKNOWN; + + D3D12_RESOURCE_DESC resourceDesc = {}; + resourceDesc.Dimension = D3D12_RESOURCE_DIMENSION_BUFFER; + resourceDesc.Width = bufferSize; + resourceDesc.Height = 1; + resourceDesc.DepthOrArraySize = 1; + resourceDesc.MipLevels = 1; + resourceDesc.Format = DXGI_FORMAT_UNKNOWN; + resourceDesc.SampleDesc.Count = 1; + resourceDesc.Layout = D3D12_TEXTURE_LAYOUT_ROW_MAJOR; + + D3D12_RESOURCE_STATES initialState = D3D12_RESOURCE_STATE_COMMON; + + ComPtr buffer; + HRESULT hr = GetDevice()->CreateCommittedResource( + &heapProps, + D3D12_HEAP_FLAG_NONE, + &resourceDesc, + initialState, + nullptr, + IID_PPV_ARGS(&buffer) + ); + ASSERT_HRESULT_SUCCEEDED(hr); + ASSERT_NE(buffer.Get(), nullptr); + + D3D12_RESOURCE_DESC desc = buffer->GetDesc(); + EXPECT_EQ(desc.Dimension, D3D12_RESOURCE_DIMENSION_BUFFER); + EXPECT_EQ(desc.Width, bufferSize); +} + +TEST_F(D3D12TestFixture, Buffer_CreateBuffer_UploadHeap) { + const uint64_t bufferSize = 2048; + + D3D12_HEAP_PROPERTIES heapProps = {}; + heapProps.Type = D3D12_HEAP_TYPE_UPLOAD; + heapProps.CPUPageProperty = D3D12_CPU_PAGE_PROPERTY_UNKNOWN; + heapProps.MemoryPoolPreference = D3D12_MEMORY_POOL_UNKNOWN; + + D3D12_RESOURCE_DESC resourceDesc = {}; + resourceDesc.Dimension = D3D12_RESOURCE_DIMENSION_BUFFER; + resourceDesc.Width = bufferSize; + resourceDesc.Height = 1; + resourceDesc.DepthOrArraySize = 1; + resourceDesc.MipLevels = 1; + resourceDesc.Format = DXGI_FORMAT_UNKNOWN; + resourceDesc.SampleDesc.Count = 1; + resourceDesc.Layout = D3D12_TEXTURE_LAYOUT_ROW_MAJOR; + + ComPtr buffer; + HRESULT hr = GetDevice()->CreateCommittedResource( + &heapProps, + D3D12_HEAP_FLAG_NONE, + &resourceDesc, + D3D12_RESOURCE_STATE_GENERIC_READ, + nullptr, + IID_PPV_ARGS(&buffer) + ); + ASSERT_HRESULT_SUCCEEDED(hr); + ASSERT_NE(buffer.Get(), nullptr); +} + +TEST_F(D3D12TestFixture, Buffer_GetGPUVirtualAddress) { + const uint64_t bufferSize = 512; + + D3D12_HEAP_PROPERTIES heapProps = {}; + heapProps.Type = D3D12_HEAP_TYPE_DEFAULT; + + D3D12_RESOURCE_DESC resourceDesc = {}; + resourceDesc.Dimension = D3D12_RESOURCE_DIMENSION_BUFFER; + resourceDesc.Width = bufferSize; + resourceDesc.Height = 1; + resourceDesc.DepthOrArraySize = 1; + resourceDesc.MipLevels = 1; + resourceDesc.Format = DXGI_FORMAT_UNKNOWN; + resourceDesc.SampleDesc.Count = 1; + resourceDesc.Layout = D3D12_TEXTURE_LAYOUT_ROW_MAJOR; + + ComPtr buffer; + HRESULT hr = GetDevice()->CreateCommittedResource( + &heapProps, + D3D12_HEAP_FLAG_NONE, + &resourceDesc, + D3D12_RESOURCE_STATE_COMMON, + nullptr, + IID_PPV_ARGS(&buffer) + ); + ASSERT_HRESULT_SUCCEEDED(hr); + + D3D12_GPU_VIRTUAL_ADDRESS gpuAddress = buffer->GetGPUVirtualAddress(); + EXPECT_NE(gpuAddress, 0); +} + +TEST_F(D3D12TestFixture, Buffer_MapUnmap) { + const uint64_t bufferSize = 256; + + D3D12_HEAP_PROPERTIES heapProps = {}; + heapProps.Type = D3D12_HEAP_TYPE_UPLOAD; + + D3D12_RESOURCE_DESC resourceDesc = {}; + resourceDesc.Dimension = D3D12_RESOURCE_DIMENSION_BUFFER; + resourceDesc.Width = bufferSize; + resourceDesc.Height = 1; + resourceDesc.DepthOrArraySize = 1; + resourceDesc.MipLevels = 1; + resourceDesc.Format = DXGI_FORMAT_UNKNOWN; + resourceDesc.SampleDesc.Count = 1; + resourceDesc.Layout = D3D12_TEXTURE_LAYOUT_ROW_MAJOR; + + ComPtr buffer; + HRESULT hr = GetDevice()->CreateCommittedResource( + &heapProps, + D3D12_HEAP_FLAG_NONE, + &resourceDesc, + D3D12_RESOURCE_STATE_GENERIC_READ, + nullptr, + IID_PPV_ARGS(&buffer) + ); + ASSERT_HRESULT_SUCCEEDED(hr); + + void* mappedData = nullptr; + hr = buffer->Map(0, nullptr, &mappedData); + ASSERT_HRESULT_SUCCEEDED(hr); + ASSERT_NE(mappedData, nullptr); + + memset(mappedData, 0xAB, static_cast(bufferSize)); + + buffer->Unmap(0, nullptr); +} + +TEST_F(D3D12TestFixture, Buffer_AlignmentRequirements) { + D3D12_CONSTANT_BUFFER_VIEW_DESC cbvDesc = {}; + cbvDesc.BufferLocation = 256; + cbvDesc.SizeInBytes = 256; + + EXPECT_GE(cbvDesc.SizeInBytes, 256); + EXPECT_EQ(cbvDesc.BufferLocation % 256, 0); } diff --git a/tests/RHI/D3D12/test_texture.cpp b/tests/RHI/D3D12/test_texture.cpp index 2fe3ec6c..a76dc350 100644 --- a/tests/RHI/D3D12/test_texture.cpp +++ b/tests/RHI/D3D12/test_texture.cpp @@ -1,5 +1,144 @@ #include "fixtures/D3D12TestFixture.h" TEST_F(D3D12TestFixture, Texture_Placeholder) { - ASSERT_TRUE(true); + ASSERT_NE(GetDevice(), nullptr); +} + +TEST_F(D3D12TestFixture, Texture_CreateTexture2D) { + const uint32_t width = 256; + const uint32_t height = 256; + const DXGI_FORMAT format = DXGI_FORMAT_R8G8B8A8_UNORM; + + D3D12_HEAP_PROPERTIES heapProps = {}; + heapProps.Type = D3D12_HEAP_TYPE_DEFAULT; + + D3D12_RESOURCE_DESC resourceDesc = {}; + resourceDesc.Dimension = D3D12_RESOURCE_DIMENSION_TEXTURE2D; + resourceDesc.Width = width; + resourceDesc.Height = height; + resourceDesc.DepthOrArraySize = 1; + resourceDesc.MipLevels = 1; + resourceDesc.Format = format; + resourceDesc.SampleDesc.Count = 1; + resourceDesc.SampleDesc.Quality = 0; + resourceDesc.Layout = D3D12_TEXTURE_LAYOUT_UNKNOWN; + + ComPtr texture; + HRESULT hr = GetDevice()->CreateCommittedResource( + &heapProps, + D3D12_HEAP_FLAG_NONE, + &resourceDesc, + D3D12_RESOURCE_STATE_COMMON, + nullptr, + IID_PPV_ARGS(&texture) + ); + ASSERT_HRESULT_SUCCEEDED(hr); + ASSERT_NE(texture.Get(), nullptr); + + D3D12_RESOURCE_DESC desc = texture->GetDesc(); + EXPECT_EQ(desc.Dimension, D3D12_RESOURCE_DIMENSION_TEXTURE2D); + EXPECT_EQ(desc.Width, width); + EXPECT_EQ(desc.Height, height); + EXPECT_EQ(desc.Format, format); +} + +TEST_F(D3D12TestFixture, Texture_CreateTexture3D) { + const uint32_t width = 64; + const uint32_t height = 64; + const uint32_t depth = 64; + + D3D12_HEAP_PROPERTIES heapProps = {}; + heapProps.Type = D3D12_HEAP_TYPE_DEFAULT; + + D3D12_RESOURCE_DESC resourceDesc = {}; + resourceDesc.Dimension = D3D12_RESOURCE_DIMENSION_TEXTURE3D; + resourceDesc.Width = width; + resourceDesc.Height = height; + resourceDesc.DepthOrArraySize = depth; + resourceDesc.MipLevels = 1; + resourceDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM; + resourceDesc.SampleDesc.Count = 1; + resourceDesc.Layout = D3D12_TEXTURE_LAYOUT_UNKNOWN; + + ComPtr texture; + HRESULT hr = GetDevice()->CreateCommittedResource( + &heapProps, + D3D12_HEAP_FLAG_NONE, + &resourceDesc, + D3D12_RESOURCE_STATE_COMMON, + nullptr, + IID_PPV_ARGS(&texture) + ); + ASSERT_HRESULT_SUCCEEDED(hr); + ASSERT_NE(texture.Get(), nullptr); + + D3D12_RESOURCE_DESC desc = texture->GetDesc(); + EXPECT_EQ(desc.Dimension, D3D12_RESOURCE_DIMENSION_TEXTURE3D); + EXPECT_EQ(desc.DepthOrArraySize, depth); +} + +TEST_F(D3D12TestFixture, Texture_CreateTextureWithMipLevels) { + const uint32_t width = 512; + const uint32_t height = 512; + const uint32_t mipLevels = 5; + + D3D12_HEAP_PROPERTIES heapProps = {}; + heapProps.Type = D3D12_HEAP_TYPE_DEFAULT; + + D3D12_RESOURCE_DESC resourceDesc = {}; + resourceDesc.Dimension = D3D12_RESOURCE_DIMENSION_TEXTURE2D; + resourceDesc.Width = width; + resourceDesc.Height = height; + resourceDesc.DepthOrArraySize = 1; + resourceDesc.MipLevels = mipLevels; + resourceDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM; + resourceDesc.SampleDesc.Count = 1; + resourceDesc.Layout = D3D12_TEXTURE_LAYOUT_UNKNOWN; + + ComPtr texture; + HRESULT hr = GetDevice()->CreateCommittedResource( + &heapProps, + D3D12_HEAP_FLAG_NONE, + &resourceDesc, + D3D12_RESOURCE_STATE_COMMON, + nullptr, + IID_PPV_ARGS(&texture) + ); + ASSERT_HRESULT_SUCCEEDED(hr); + + D3D12_RESOURCE_DESC desc = texture->GetDesc(); + EXPECT_EQ(desc.MipLevels, mipLevels); +} + +TEST_F(D3D12TestFixture, Texture_CreateTextureArray) { + const uint32_t width = 128; + const uint32_t height = 128; + const uint32_t arraySize = 4; + + D3D12_HEAP_PROPERTIES heapProps = {}; + heapProps.Type = D3D12_HEAP_TYPE_DEFAULT; + + D3D12_RESOURCE_DESC resourceDesc = {}; + resourceDesc.Dimension = D3D12_RESOURCE_DIMENSION_TEXTURE2D; + resourceDesc.Width = width; + resourceDesc.Height = height; + resourceDesc.DepthOrArraySize = arraySize; + resourceDesc.MipLevels = 1; + resourceDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM; + resourceDesc.SampleDesc.Count = 1; + resourceDesc.Layout = D3D12_TEXTURE_LAYOUT_UNKNOWN; + + ComPtr texture; + HRESULT hr = GetDevice()->CreateCommittedResource( + &heapProps, + D3D12_HEAP_FLAG_NONE, + &resourceDesc, + D3D12_RESOURCE_STATE_COMMON, + nullptr, + IID_PPV_ARGS(&texture) + ); + ASSERT_HRESULT_SUCCEEDED(hr); + + D3D12_RESOURCE_DESC desc = texture->GetDesc(); + EXPECT_EQ(desc.DepthOrArraySize, arraySize); }