#if defined(XCENGINE_SUPPORT_VULKAN) #include "fixtures/VulkanTestFixture.h" #include "XCEngine/RHI/RHIDescriptorPool.h" #include "XCEngine/RHI/RHIDescriptorSet.h" #include "XCEngine/RHI/RHIPipelineLayout.h" #include "XCEngine/RHI/RHIPipelineState.h" #include "XCEngine/RHI/RHIResourceView.h" #include "XCEngine/RHI/Vulkan/VulkanTexture.h" #include using namespace XCEngine::RHI; namespace { TEST_F(VulkanGraphicsFixture, CreateUnorderedAccessViewProducesValidView) { RHITexture* texture = m_device->CreateTexture(CreateColorTextureDesc(4, 4)); ASSERT_NE(texture, nullptr); ResourceViewDesc viewDesc = {}; viewDesc.format = static_cast(Format::R8G8B8A8_UNorm); viewDesc.dimension = ResourceViewDimension::Texture2D; RHIResourceView* uav = m_device->CreateUnorderedAccessView(texture, viewDesc); ASSERT_NE(uav, nullptr); EXPECT_TRUE(uav->IsValid()); EXPECT_EQ(uav->GetViewType(), ResourceViewType::UnorderedAccess); uav->Shutdown(); delete uav; texture->Shutdown(); delete texture; } TEST_F(VulkanGraphicsFixture, DispatchWritesUavTexture) { RHITexture* texture = m_device->CreateTexture(CreateColorTextureDesc(4, 4)); ASSERT_NE(texture, nullptr); ResourceViewDesc uavDesc = {}; uavDesc.format = static_cast(Format::R8G8B8A8_UNorm); uavDesc.dimension = ResourceViewDimension::Texture2D; RHIResourceView* uav = m_device->CreateUnorderedAccessView(texture, uavDesc); ASSERT_NE(uav, nullptr); DescriptorPoolDesc poolDesc = {}; poolDesc.type = DescriptorHeapType::CBV_SRV_UAV; poolDesc.descriptorCount = 1; poolDesc.shaderVisible = true; RHIDescriptorPool* pool = m_device->CreateDescriptorPool(poolDesc); ASSERT_NE(pool, nullptr); DescriptorSetLayoutBinding uavBinding = {}; uavBinding.binding = 0; uavBinding.type = static_cast(DescriptorType::UAV); uavBinding.count = 1; uavBinding.visibility = static_cast(ShaderVisibility::All); DescriptorSetLayoutDesc setLayout = {}; setLayout.bindings = &uavBinding; setLayout.bindingCount = 1; RHIPipelineLayoutDesc pipelineLayoutDesc = {}; pipelineLayoutDesc.setLayouts = &setLayout; pipelineLayoutDesc.setLayoutCount = 1; RHIPipelineLayout* pipelineLayout = m_device->CreatePipelineLayout(pipelineLayoutDesc); ASSERT_NE(pipelineLayout, nullptr); RHIDescriptorSet* descriptorSet = pool->AllocateSet(setLayout); ASSERT_NE(descriptorSet, nullptr); descriptorSet->Update(0, uav); GraphicsPipelineDesc pipelineDesc = {}; pipelineDesc.pipelineLayout = pipelineLayout; RHIPipelineState* pipelineState = m_device->CreatePipelineState(pipelineDesc); ASSERT_NE(pipelineState, nullptr); RHIShader* shader = CreateWriteRedComputeShader(); ASSERT_NE(shader, nullptr); pipelineState->SetComputeShader(shader); EXPECT_TRUE(pipelineState->HasComputeShader()); EXPECT_EQ(pipelineState->GetType(), PipelineType::Compute); RHICommandList* commandList = CreateCommandList(); ASSERT_NE(commandList, nullptr); commandList->Reset(); commandList->TransitionBarrier(uav, ResourceStates::Common, ResourceStates::UnorderedAccess); commandList->SetPipelineState(pipelineState); RHIDescriptorSet* descriptorSets[] = { descriptorSet }; commandList->SetComputeDescriptorSets(0, 1, descriptorSets, pipelineLayout); commandList->Dispatch(1, 1, 1); SubmitAndWait(commandList); const std::vector pixels = ReadTextureRgba8(static_cast(texture)); ASSERT_GE(pixels.size(), 4u); EXPECT_EQ(pixels[0], 255u); EXPECT_EQ(pixels[1], 0u); EXPECT_EQ(pixels[2], 0u); EXPECT_EQ(pixels[3], 255u); commandList->Shutdown(); delete commandList; shader->Shutdown(); delete shader; pipelineState->Shutdown(); delete pipelineState; descriptorSet->Shutdown(); delete descriptorSet; pipelineLayout->Shutdown(); delete pipelineLayout; pool->Shutdown(); delete pool; uav->Shutdown(); delete uav; texture->Shutdown(); delete texture; } TEST_F(VulkanGraphicsFixture, DispatchWritesUavTextureWithGlslComputeShader) { RHITexture* texture = m_device->CreateTexture(CreateColorTextureDesc(4, 4)); ASSERT_NE(texture, nullptr); ResourceViewDesc uavDesc = {}; uavDesc.format = static_cast(Format::R8G8B8A8_UNorm); uavDesc.dimension = ResourceViewDimension::Texture2D; RHIResourceView* uav = m_device->CreateUnorderedAccessView(texture, uavDesc); ASSERT_NE(uav, nullptr); DescriptorPoolDesc poolDesc = {}; poolDesc.type = DescriptorHeapType::CBV_SRV_UAV; poolDesc.descriptorCount = 1; poolDesc.shaderVisible = true; RHIDescriptorPool* pool = m_device->CreateDescriptorPool(poolDesc); ASSERT_NE(pool, nullptr); DescriptorSetLayoutBinding uavBinding = {}; uavBinding.binding = 0; uavBinding.type = static_cast(DescriptorType::UAV); uavBinding.count = 1; uavBinding.visibility = static_cast(ShaderVisibility::All); DescriptorSetLayoutDesc setLayout = {}; setLayout.bindings = &uavBinding; setLayout.bindingCount = 1; RHIPipelineLayoutDesc pipelineLayoutDesc = {}; pipelineLayoutDesc.setLayouts = &setLayout; pipelineLayoutDesc.setLayoutCount = 1; RHIPipelineLayout* pipelineLayout = m_device->CreatePipelineLayout(pipelineLayoutDesc); ASSERT_NE(pipelineLayout, nullptr); RHIDescriptorSet* descriptorSet = pool->AllocateSet(setLayout); ASSERT_NE(descriptorSet, nullptr); descriptorSet->Update(0, uav); GraphicsPipelineDesc pipelineDesc = {}; pipelineDesc.pipelineLayout = pipelineLayout; RHIPipelineState* pipelineState = m_device->CreatePipelineState(pipelineDesc); ASSERT_NE(pipelineState, nullptr); RHIShader* shader = CreateWriteRedComputeShaderFromGlsl(); ASSERT_NE(shader, nullptr); pipelineState->SetComputeShader(shader); EXPECT_TRUE(pipelineState->HasComputeShader()); EXPECT_EQ(pipelineState->GetType(), PipelineType::Compute); RHICommandList* commandList = CreateCommandList(); ASSERT_NE(commandList, nullptr); commandList->Reset(); commandList->TransitionBarrier(uav, ResourceStates::Common, ResourceStates::UnorderedAccess); commandList->SetPipelineState(pipelineState); RHIDescriptorSet* descriptorSets[] = { descriptorSet }; commandList->SetComputeDescriptorSets(0, 1, descriptorSets, pipelineLayout); commandList->Dispatch(1, 1, 1); SubmitAndWait(commandList); const std::vector pixels = ReadTextureRgba8(static_cast(texture)); ASSERT_GE(pixels.size(), 4u); EXPECT_EQ(pixels[0], 255u); EXPECT_EQ(pixels[1], 0u); EXPECT_EQ(pixels[2], 0u); EXPECT_EQ(pixels[3], 255u); commandList->Shutdown(); delete commandList; shader->Shutdown(); delete shader; pipelineState->Shutdown(); delete pipelineState; descriptorSet->Shutdown(); delete descriptorSet; pipelineLayout->Shutdown(); delete pipelineLayout; pool->Shutdown(); delete pool; uav->Shutdown(); delete uav; texture->Shutdown(); delete texture; } } // namespace #endif