Add formal compute pipeline creation API
This commit is contained in:
@@ -756,10 +756,6 @@ TEST_F(OpenGLTestFixture, CommandList_SetComputeDescriptorSets_UsesSetAwareImage
|
||||
ASSERT_NE(descriptorSet, nullptr);
|
||||
descriptorSet->Update(0, uav);
|
||||
|
||||
GraphicsPipelineDesc pipelineDesc = {};
|
||||
RHIPipelineState* pipelineState = GetDevice()->CreatePipelineState(pipelineDesc);
|
||||
ASSERT_NE(pipelineState, nullptr);
|
||||
|
||||
ShaderCompileDesc shaderDesc = {};
|
||||
shaderDesc.sourceLanguage = ShaderLanguage::GLSL;
|
||||
static const char* computeSource = R"(
|
||||
@@ -772,9 +768,11 @@ TEST_F(OpenGLTestFixture, CommandList_SetComputeDescriptorSets_UsesSetAwareImage
|
||||
)";
|
||||
shaderDesc.source.assign(computeSource, computeSource + std::strlen(computeSource));
|
||||
shaderDesc.profile = L"cs_4_30";
|
||||
RHIShader* computeShader = GetDevice()->CreateShader(shaderDesc);
|
||||
ASSERT_NE(computeShader, nullptr);
|
||||
pipelineState->SetComputeShader(computeShader);
|
||||
ComputePipelineDesc pipelineDesc = {};
|
||||
pipelineDesc.pipelineLayout = pipelineLayout;
|
||||
pipelineDesc.computeShader = shaderDesc;
|
||||
RHIPipelineState* pipelineState = GetDevice()->CreateComputePipelineState(pipelineDesc);
|
||||
ASSERT_NE(pipelineState, nullptr);
|
||||
|
||||
CommandListDesc cmdDesc = {};
|
||||
cmdDesc.commandListType = static_cast<uint32_t>(CommandQueueType::Direct);
|
||||
@@ -815,8 +813,6 @@ TEST_F(OpenGLTestFixture, CommandList_SetComputeDescriptorSets_UsesSetAwareImage
|
||||
|
||||
cmdList->Shutdown();
|
||||
delete cmdList;
|
||||
computeShader->Shutdown();
|
||||
delete computeShader;
|
||||
pipelineState->Shutdown();
|
||||
delete pipelineState;
|
||||
descriptorSet->Shutdown();
|
||||
|
||||
@@ -8,12 +8,59 @@
|
||||
#include "XCEngine/RHI/RHIResourceView.h"
|
||||
#include "XCEngine/RHI/Vulkan/VulkanTexture.h"
|
||||
|
||||
#include <cstring>
|
||||
#include <vector>
|
||||
|
||||
using namespace XCEngine::RHI;
|
||||
|
||||
namespace {
|
||||
|
||||
constexpr uint32_t kWriteRedComputeSpirv[] = {
|
||||
0x07230203, 0x00010000, 0x0008000B, 0x00000017, 0x00000000, 0x00020011, 0x00000001, 0x0006000B,
|
||||
0x00000001, 0x4C534C47, 0x6474732E, 0x3035342E, 0x00000000, 0x0003000E, 0x00000000, 0x00000001,
|
||||
0x0005000F, 0x00000005, 0x00000004, 0x6E69616D, 0x00000000, 0x00060010, 0x00000004, 0x00000011,
|
||||
0x00000001, 0x00000001, 0x00000001, 0x00030003, 0x00000002, 0x000001C2, 0x00040005, 0x00000004,
|
||||
0x6E69616D, 0x00000000, 0x00040005, 0x00000009, 0x616D4975, 0x00006567, 0x00030047, 0x00000009,
|
||||
0x00000019, 0x00040047, 0x00000009, 0x00000021, 0x00000000, 0x00040047, 0x00000009, 0x00000022,
|
||||
0x00000000, 0x00040047, 0x00000016, 0x0000000B, 0x00000019, 0x00020013, 0x00000002, 0x00030021,
|
||||
0x00000003, 0x00000002, 0x00030016, 0x00000006, 0x00000020, 0x00090019, 0x00000007, 0x00000006,
|
||||
0x00000001, 0x00000000, 0x00000000, 0x00000000, 0x00000002, 0x00000004, 0x00040020, 0x00000008,
|
||||
0x00000000, 0x00000007, 0x0004003B, 0x00000008, 0x00000009, 0x00000000, 0x00040015, 0x0000000B,
|
||||
0x00000020, 0x00000001, 0x00040017, 0x0000000C, 0x0000000B, 0x00000002, 0x0004002B, 0x0000000B,
|
||||
0x0000000D, 0x00000000, 0x0005002C, 0x0000000C, 0x0000000E, 0x0000000D, 0x0000000D, 0x00040017,
|
||||
0x0000000F, 0x00000006, 0x0000000004, 0x0004002B, 0x00000006, 0x00000010, 0x3F800000, 0x0004002B,
|
||||
0x00000006, 0x00000011, 0x00000000, 0x0007002C, 0x0000000F, 0x00000012, 0x00000010, 0x00000011,
|
||||
0x00000011, 0x00000010, 0x00040015, 0x00000013, 0x00000020, 0x00000000, 0x00040017, 0x00000014,
|
||||
0x00000013, 0x00000003, 0x0004002B, 0x00000013, 0x00000015, 0x00000001, 0x0006002C, 0x00000014,
|
||||
0x00000016, 0x00000015, 0x00000015, 0x00000015, 0x00050036, 0x00000002, 0x00000004, 0x00000000,
|
||||
0x00000003, 0x000200F8, 0x00000005, 0x0004003D, 0x00000007, 0x0000000A, 0x00000009, 0x00040063,
|
||||
0x0000000A, 0x0000000E, 0x00000012, 0x000100FD, 0x00010038
|
||||
};
|
||||
|
||||
ShaderCompileDesc MakeWriteRedComputeShaderDesc() {
|
||||
ShaderCompileDesc shaderDesc = {};
|
||||
shaderDesc.sourceLanguage = ShaderLanguage::SPIRV;
|
||||
shaderDesc.profile = L"cs_6_0";
|
||||
shaderDesc.source.resize(sizeof(kWriteRedComputeSpirv));
|
||||
std::memcpy(shaderDesc.source.data(), kWriteRedComputeSpirv, sizeof(kWriteRedComputeSpirv));
|
||||
return shaderDesc;
|
||||
}
|
||||
|
||||
ShaderCompileDesc MakeWriteRedComputeShaderFromGlslDesc() {
|
||||
static const char* computeSource = R"(#version 450
|
||||
layout(local_size_x = 1, local_size_y = 1, local_size_z = 1) in;
|
||||
layout(set = 0, binding = 0, rgba8) uniform writeonly image2D uImage;
|
||||
void main() {
|
||||
imageStore(uImage, ivec2(0, 0), vec4(1.0, 0.0, 0.0, 1.0));
|
||||
}
|
||||
)";
|
||||
|
||||
ShaderCompileDesc shaderDesc = {};
|
||||
shaderDesc.sourceLanguage = ShaderLanguage::GLSL;
|
||||
shaderDesc.source.assign(computeSource, computeSource + std::strlen(computeSource));
|
||||
return shaderDesc;
|
||||
}
|
||||
|
||||
TEST_F(VulkanGraphicsFixture, CreateUnorderedAccessViewProducesValidView) {
|
||||
RHITexture* texture = m_device->CreateTexture(CreateColorTextureDesc(4, 4));
|
||||
ASSERT_NE(texture, nullptr);
|
||||
@@ -70,14 +117,11 @@ TEST_F(VulkanGraphicsFixture, DispatchWritesUavTexture) {
|
||||
ASSERT_NE(descriptorSet, nullptr);
|
||||
descriptorSet->Update(0, uav);
|
||||
|
||||
GraphicsPipelineDesc pipelineDesc = {};
|
||||
ComputePipelineDesc pipelineDesc = {};
|
||||
pipelineDesc.pipelineLayout = pipelineLayout;
|
||||
RHIPipelineState* pipelineState = m_device->CreatePipelineState(pipelineDesc);
|
||||
pipelineDesc.computeShader = MakeWriteRedComputeShaderDesc();
|
||||
RHIPipelineState* pipelineState = m_device->CreateComputePipelineState(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);
|
||||
|
||||
@@ -101,8 +145,6 @@ TEST_F(VulkanGraphicsFixture, DispatchWritesUavTexture) {
|
||||
|
||||
commandList->Shutdown();
|
||||
delete commandList;
|
||||
shader->Shutdown();
|
||||
delete shader;
|
||||
pipelineState->Shutdown();
|
||||
delete pipelineState;
|
||||
descriptorSet->Shutdown();
|
||||
@@ -154,14 +196,11 @@ TEST_F(VulkanGraphicsFixture, DispatchWritesUavTextureWithGlslComputeShader) {
|
||||
ASSERT_NE(descriptorSet, nullptr);
|
||||
descriptorSet->Update(0, uav);
|
||||
|
||||
GraphicsPipelineDesc pipelineDesc = {};
|
||||
ComputePipelineDesc pipelineDesc = {};
|
||||
pipelineDesc.pipelineLayout = pipelineLayout;
|
||||
RHIPipelineState* pipelineState = m_device->CreatePipelineState(pipelineDesc);
|
||||
pipelineDesc.computeShader = MakeWriteRedComputeShaderFromGlslDesc();
|
||||
RHIPipelineState* pipelineState = m_device->CreateComputePipelineState(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);
|
||||
|
||||
@@ -185,8 +224,6 @@ TEST_F(VulkanGraphicsFixture, DispatchWritesUavTextureWithGlslComputeShader) {
|
||||
|
||||
commandList->Shutdown();
|
||||
delete commandList;
|
||||
shader->Shutdown();
|
||||
delete shader;
|
||||
pipelineState->Shutdown();
|
||||
delete pipelineState;
|
||||
descriptorSet->Shutdown();
|
||||
|
||||
@@ -165,28 +165,53 @@ TEST_P(RHITestFixture, PipelineState_EnsureValid_Compute) {
|
||||
delete pso;
|
||||
}
|
||||
|
||||
TEST_P(RHITestFixture, Device_CreateComputePipelineState_ReturnsValidComputePipeline) {
|
||||
ComputePipelineDesc desc = {};
|
||||
desc.computeShader = MakeComputeShaderDesc(GetBackendType());
|
||||
|
||||
RHIPipelineState* pso = GetDevice()->CreateComputePipelineState(desc);
|
||||
ASSERT_NE(pso, nullptr);
|
||||
EXPECT_TRUE(pso->IsValid());
|
||||
EXPECT_TRUE(pso->HasComputeShader());
|
||||
EXPECT_EQ(pso->GetType(), PipelineType::Compute);
|
||||
|
||||
pso->Shutdown();
|
||||
delete pso;
|
||||
}
|
||||
|
||||
TEST_P(RHITestFixture, Device_CreateComputePipelineState_UsesExternalPipelineLayout) {
|
||||
RHIPipelineLayoutDesc pipelineLayoutDesc = {};
|
||||
RHIPipelineLayout* pipelineLayout = GetDevice()->CreatePipelineLayout(pipelineLayoutDesc);
|
||||
ASSERT_NE(pipelineLayout, nullptr);
|
||||
|
||||
ComputePipelineDesc desc = {};
|
||||
desc.computeShader = MakeComputeShaderDesc(GetBackendType());
|
||||
desc.pipelineLayout = pipelineLayout;
|
||||
|
||||
RHIPipelineState* pso = GetDevice()->CreateComputePipelineState(desc);
|
||||
ASSERT_NE(pso, nullptr);
|
||||
EXPECT_TRUE(pso->IsValid());
|
||||
EXPECT_TRUE(pso->HasComputeShader());
|
||||
EXPECT_EQ(pso->GetType(), PipelineType::Compute);
|
||||
|
||||
pso->Shutdown();
|
||||
delete pso;
|
||||
pipelineLayout->Shutdown();
|
||||
delete pipelineLayout;
|
||||
}
|
||||
|
||||
TEST_P(RHITestFixture, CommandList_Dispatch_Basic) {
|
||||
RHICommandList* cmdList = GetDevice()->CreateCommandList({});
|
||||
ASSERT_NE(cmdList, nullptr);
|
||||
|
||||
cmdList->Reset();
|
||||
|
||||
GraphicsPipelineDesc desc = {};
|
||||
RHIPipelineState* pso = GetDevice()->CreatePipelineState(desc);
|
||||
|
||||
ComputePipelineDesc desc = {};
|
||||
desc.computeShader = MakeComputeShaderDesc(GetBackendType());
|
||||
RHIPipelineState* pso = GetDevice()->CreateComputePipelineState(desc);
|
||||
if (pso != nullptr) {
|
||||
ShaderCompileDesc shaderDesc = MakeComputeShaderDesc(GetBackendType());
|
||||
|
||||
RHIShader* computeShader = GetDevice()->CreateShader(shaderDesc);
|
||||
if (computeShader != nullptr) {
|
||||
pso->SetComputeShader(computeShader);
|
||||
cmdList->SetPipelineState(pso);
|
||||
cmdList->Dispatch(1, 1, 1);
|
||||
|
||||
computeShader->Shutdown();
|
||||
delete computeShader;
|
||||
}
|
||||
|
||||
cmdList->SetPipelineState(pso);
|
||||
cmdList->Dispatch(1, 1, 1);
|
||||
pso->Shutdown();
|
||||
delete pso;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user