Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pipeline refactor #11

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
124 changes: 67 additions & 57 deletions CMakePresets.json
Original file line number Diff line number Diff line change
@@ -1,67 +1,77 @@
{
"version": 6,
"cmakeMinimumRequired": {
"major": 3,
"minor": 20,
"patch": 0
"major": 3,
"minor": 20,
"patch": 0
},
"configurePresets": [
{
"name": "default",
"description": "Default build using Ninja Multi-Config generator",
"generator": "Ninja Multi-Config",
"binaryDir": "${sourceDir}/build"
},
{
"name": "msvc-base",
"hidden": true,
"inherits": "default",
"description": "Options specific for MSVC",
"cacheVariables": {
"CMAKE_C_FLAGS_INIT": "-D_ITERATOR_DEBUG_LEVEL=0 /MP",
"CMAKE_CXX_FLAGS_INIT": "-D_ITERATOR_DEBUG_LEVEL=0 /MP"
}
},
{
"name": "msvc",
"inherits": "msvc-base",
"description": "Ninja Multi-Config generator with MSVC",
"generator": "Ninja Multi-Config",
"cacheVariables": {
"CMAKE_C_COMPILER": "cl.exe",
"CMAKE_CXX_COMPILER": "cl.exe"
{
"name": "default",
"description": "Default build using Ninja Multi-Config generator",
"generator": "Ninja Multi-Config",
"binaryDir": "${sourceDir}/build"
},
{
"name": "msvc-base",
"hidden": true,
"inherits": "default",
"description": "Options specific for MSVC",
"cacheVariables": {
"CMAKE_C_FLAGS_INIT": "-D_ITERATOR_DEBUG_LEVEL=0 /MP",
"CMAKE_CXX_FLAGS_INIT": "-D_ITERATOR_DEBUG_LEVEL=0 /MP"
}
},
{
"name": "msvc",
"inherits": "msvc-base",
"description": "Ninja Multi-Config generator with MSVC",
"generator": "Ninja Multi-Config",
"cacheVariables": {
"CMAKE_C_COMPILER": "cl.exe",
"CMAKE_CXX_COMPILER": "cl.exe"
},
"vendor": {
"microsoft.com/VisualStudioSettings/CMake/1.0": {
"hostOS": [
"Windows"
]
}
}
},
{
"name": "vs2019",
"inherits": "msvc-base",
"description": "Visual Studio 2019 project",
"generator": "Visual Studio 16 2019"
},
"vendor": {
"microsoft.com/VisualStudioSettings/CMake/1.0": {
"hostOS": [
"Windows"
]
}
{
"name": "vs2022",
"inherits": "msvc-base",
"description": "Visual Studio 2022 project",
"generator": "Visual Studio 17 2022"
}
},
{
"name": "vs2019",
"inherits": "msvc-base",
"description": "Visual Studio 2019 project",
"generator": "Visual Studio 16 2019"
},
{
"name": "vs2022",
"inherits": "msvc-base",
"description": "Visual Studio 2022 project",
"generator": "Visual Studio 17 2022"
}
],
"buildPresets": [
{
"name": "msvc-debug",
"configurePreset": "msvc",
"configuration": "Debug"
},
{
"name": "msvc-release",
"configurePreset": "default",
"configuration": "Release"
}
{
"name": "debug",
"configurePreset": "default",
"configuration": "Debug"
},
{
"name": "release",
"configurePreset": "default",
"configuration": "Release"
},
{
"name": "msvc-debug",
"configurePreset": "msvc",
"configuration": "Debug"
},
{
"name": "msvc-release",
"configurePreset": "default",
"configuration": "Release"
}
]
}
}
75 changes: 53 additions & 22 deletions include/slang-rhi.h
Original file line number Diff line number Diff line change
Expand Up @@ -1414,6 +1414,27 @@ class IPipeline : public ISlangUnknown
virtual SLANG_NO_THROW Result SLANG_MCALL getNativeHandle(NativeHandle* outHandle) = 0;
};

class IRenderPipeline : public IPipeline
{
SLANG_COM_INTERFACE(0x93d0c872, 0x4268, 0x428f, {0xbc, 0x1f, 0x3f, 0xf9, 0x4a, 0x24, 0x72, 0x17});

public:
};

class IComputePipeline : public IPipeline
{
SLANG_COM_INTERFACE(0xc07449ec, 0x3b82, 0x42b0, {0xb9, 0xdc, 0x98, 0x08, 0x70, 0xe4, 0x1b, 0xf2});

public:
};

class IRayTracingPipeline : public IPipeline
{
SLANG_COM_INTERFACE(0xe6078d78, 0x3bd3, 0x40e8, {0x90, 0x42, 0x3b, 0x5e, 0x0c, 0x45, 0xde, 0x1f});

public:
};

struct ScissorRect
{
int32_t minX;
Expand Down Expand Up @@ -1681,22 +1702,23 @@ class IRenderCommandEncoder : public IResourceCommandEncoder
SLANG_COM_INTERFACE(0xa2be110e, 0xaed7, 0x43b6, {0x90, 0x01, 0x77, 0x79, 0x1f, 0xea, 0x1d, 0x40});

public:
// Sets the current pipeline state. This method returns a transient shader object for
// Sets the current pipeline. This method returns a transient shader object for
// writing shader parameters. This shader object will not retain any resources or
// sub-shader-objects bound to it. The user must be responsible for ensuring that any
// resources or shader objects that is set into `outRootShaderObject` stays alive during
// the execution of the command buffer.
virtual SLANG_NO_THROW Result SLANG_MCALL bindPipeline(IPipeline* state, IShaderObject** outRootShaderObject) = 0;
inline IShaderObject* bindPipeline(IPipeline* state)
virtual SLANG_NO_THROW Result SLANG_MCALL
bindPipeline(IRenderPipeline* pipeline, IShaderObject** outRootShaderObject) = 0;
inline IShaderObject* bindPipeline(IRenderPipeline* pipeline)
{
IShaderObject* rootObject = nullptr;
SLANG_RETURN_NULL_ON_FAIL(bindPipeline(state, &rootObject));
SLANG_RETURN_NULL_ON_FAIL(bindPipeline(pipeline, &rootObject));
return rootObject;
}

// Sets the current pipeline state along with a pre-created mutable root shader object.
// Sets the current pipeline along with a pre-created mutable root shader object.
virtual SLANG_NO_THROW Result SLANG_MCALL
bindPipelineWithRootObject(IPipeline* state, IShaderObject* rootObject) = 0;
bindPipelineWithRootObject(IRenderPipeline* pipeline, IShaderObject* rootObject) = 0;

virtual SLANG_NO_THROW void SLANG_MCALL setViewports(GfxCount count, const Viewport* viewports) = 0;
virtual SLANG_NO_THROW void SLANG_MCALL setScissorRects(GfxCount count, const ScissorRect* scissors) = 0;
Expand Down Expand Up @@ -1761,21 +1783,22 @@ class IComputeCommandEncoder : public IResourceCommandEncoder
SLANG_COM_INTERFACE(0x46261132, 0xa7f6, 0x439b, {0x82, 0x6b, 0x1e, 0xaf, 0xf2, 0xae, 0xae, 0xa6});

public:
// Sets the current pipeline state. This method returns a transient shader object for
// Sets the current pipeline. This method returns a transient shader object for
// writing shader parameters. This shader object will not retain any resources or
// sub-shader-objects bound to it. The user must be responsible for ensuring that any
// resources or shader objects that is set into `outRooShaderObject` stays alive during
// the execution of the command buffer.
virtual SLANG_NO_THROW Result SLANG_MCALL bindPipeline(IPipeline* state, IShaderObject** outRootShaderObject) = 0;
inline IShaderObject* bindPipeline(IPipeline* state)
virtual SLANG_NO_THROW Result SLANG_MCALL
bindPipeline(IComputePipeline* pipeline, IShaderObject** outRootShaderObject) = 0;
inline IShaderObject* bindPipeline(IComputePipeline* pipeline)
{
IShaderObject* rootObject = nullptr;
SLANG_RETURN_NULL_ON_FAIL(bindPipeline(state, &rootObject));
SLANG_RETURN_NULL_ON_FAIL(bindPipeline(pipeline, &rootObject));
return rootObject;
}
// Sets the current pipeline state along with a pre-created mutable root shader object.
// Sets the current pipeline along with a pre-created mutable root shader object.
virtual SLANG_NO_THROW Result SLANG_MCALL
bindPipelineWithRootObject(IPipeline* state, IShaderObject* rootObject) = 0;
bindPipelineWithRootObject(IComputePipeline* pipeline, IShaderObject* rootObject) = 0;
virtual SLANG_NO_THROW Result SLANG_MCALL dispatchCompute(int x, int y, int z) = 0;
virtual SLANG_NO_THROW Result SLANG_MCALL dispatchComputeIndirect(IBuffer* cmdBuffer, Offset offset) = 0;
};
Expand Down Expand Up @@ -1821,10 +1844,11 @@ class IRayTracingCommandEncoder : public IResourceCommandEncoder
virtual SLANG_NO_THROW void SLANG_MCALL
deserializeAccelerationStructure(IAccelerationStructure* dest, DeviceAddress source) = 0;

virtual SLANG_NO_THROW Result SLANG_MCALL bindPipeline(IPipeline* state, IShaderObject** outRootObject) = 0;
// Sets the current pipeline state along with a pre-created mutable root shader object.
virtual SLANG_NO_THROW Result SLANG_MCALL
bindPipelineWithRootObject(IPipeline* state, IShaderObject* rootObject) = 0;
bindPipeline(IRayTracingPipeline* pipeline, IShaderObject** outRootObject) = 0;
// Sets the current pipeline along with a pre-created mutable root shader object.
virtual SLANG_NO_THROW Result SLANG_MCALL
bindPipelineWithRootObject(IRayTracingPipeline* pipeline, IShaderObject* rootObject) = 0;

/// Issues a dispatch command to start ray tracing workload with a ray tracing pipeline.
/// `rayGenShaderIndex` specifies the index into the shader table that identifies the ray generation shader.
Expand Down Expand Up @@ -2479,27 +2503,34 @@ class IDevice : public ISlangUnknown
) = 0;

virtual SLANG_NO_THROW Result SLANG_MCALL
createRenderPipeline(const RenderPipelineDesc& desc, IPipeline** outPipeline) = 0;
createRenderPipeline(const RenderPipelineDesc& desc, IRenderPipeline** outPipeline) = 0;

inline ComPtr<IPipeline> createRenderPipeline(const RenderPipelineDesc& desc)
inline ComPtr<IRenderPipeline> createRenderPipeline(const RenderPipelineDesc& desc)
{
ComPtr<IPipeline> pipeline;
ComPtr<IRenderPipeline> pipeline;
SLANG_RETURN_NULL_ON_FAIL(createRenderPipeline(desc, pipeline.writeRef()));
return pipeline;
}

virtual SLANG_NO_THROW Result SLANG_MCALL
createComputePipeline(const ComputePipelineDesc& desc, IPipeline** outPipeline) = 0;
createComputePipeline(const ComputePipelineDesc& desc, IComputePipeline** outPipeline) = 0;

inline ComPtr<IPipeline> createComputePipeline(const ComputePipelineDesc& desc)
inline ComPtr<IComputePipeline> createComputePipeline(const ComputePipelineDesc& desc)
{
ComPtr<IPipeline> pipeline;
ComPtr<IComputePipeline> pipeline;
SLANG_RETURN_NULL_ON_FAIL(createComputePipeline(desc, pipeline.writeRef()));
return pipeline;
}

virtual SLANG_NO_THROW Result SLANG_MCALL
createRayTracingPipeline(const RayTracingPipelineDesc& desc, IPipeline** outPipeline) = 0;
createRayTracingPipeline(const RayTracingPipelineDesc& desc, IRayTracingPipeline** outPipeline) = 0;

inline ComPtr<IRayTracingPipeline> createRayTracingPipeline(const RayTracingPipelineDesc& desc)
{
ComPtr<IRayTracingPipeline> pipeline;
SLANG_RETURN_NULL_ON_FAIL(createRayTracingPipeline(desc, pipeline.writeRef()));
return pipeline;
}

/// Read back texture resource and stores the result in `outBlob`.
virtual SLANG_NO_THROW SlangResult SLANG_MCALL readTexture(
Expand Down
15 changes: 11 additions & 4 deletions src/command-writer.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ namespace rhi {

enum class CommandName
{
SetPipeline,
SetRenderPipeline,
SetComputePipeline,
BindRootShaderObject,
SetFramebuffer,
ClearFrame,
Expand Down Expand Up @@ -123,10 +124,16 @@ class CommandWriter
return reinterpret_cast<T*>(m_data.data() + offset);
}

void setPipeline(IPipeline* state)
void setRenderPipeline(IRenderPipeline* pipeline)
{
auto offset = encodeObject(static_cast<PipelineBase*>(state));
m_commands.push_back(Command(CommandName::SetPipeline, (uint32_t)offset));
auto offset = encodeObject(static_cast<RenderPipelineBase*>(pipeline));
m_commands.push_back(Command(CommandName::SetRenderPipeline, (uint32_t)offset));
}

void setComputePipeline(IComputePipeline* pipeline)
{
auto offset = encodeObject(static_cast<ComputePipelineBase*>(pipeline));
m_commands.push_back(Command(CommandName::SetComputePipeline, (uint32_t)offset));
}

void bindRootShaderObject(IShaderObject* object)
Expand Down
24 changes: 12 additions & 12 deletions src/cpu/cpu-device.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -167,12 +167,12 @@ DeviceImpl::createProgram(const IShaderProgram::Desc& desc, IShaderProgram** out
}

SLANG_NO_THROW Result SLANG_MCALL
DeviceImpl::createComputePipeline(const ComputePipelineDesc& desc, IPipeline** outPipeline)
DeviceImpl::createComputePipeline(const ComputePipelineDesc& desc, IComputePipeline** outPipeline)
{
RefPtr<PipelineImpl> state = new PipelineImpl();
state->init(desc);
returnComPtr(outPipeline, state);
return Result();
RefPtr<ComputePipelineImpl> pipeline = new ComputePipelineImpl();
SLANG_RETURN_ON_FAIL(pipeline->init(desc));
returnComPtr(outPipeline, pipeline);
return SLANG_OK;
}

SLANG_NO_THROW Result SLANG_MCALL DeviceImpl::createQueryPool(const IQueryPool::Desc& desc, IQueryPool** outPool)
Expand Down Expand Up @@ -215,9 +215,14 @@ void DeviceImpl::unmap(IBuffer* buffer, size_t offsetWritten, size_t sizeWritten
SLANG_UNUSED(sizeWritten);
}

void DeviceImpl::setPipeline(IPipeline* state)
void DeviceImpl::setRenderPipeline(IRenderPipeline* pipeline)
{
m_currentPipeline = static_cast<PipelineImpl*>(state);
SLANG_UNUSED(pipeline);
}

void DeviceImpl::setComputePipeline(IComputePipeline* pipeline)
{
m_currentPipeline = static_cast<ComputePipelineImpl*>(pipeline);
}

void DeviceImpl::bindRootShaderObject(IShaderObject* object)
Expand All @@ -230,11 +235,6 @@ void DeviceImpl::dispatchCompute(int x, int y, int z)
int entryPointIndex = 0;
int targetIndex = 0;

// Specialize the compute kernel based on the shader object bindings.
RefPtr<PipelineBase> newPipeline;
maybeSpecializePipeline(m_currentPipeline, m_currentRootObject, newPipeline);
m_currentPipeline = static_cast<PipelineImpl*>(newPipeline.Ptr());

auto program = m_currentPipeline->getProgram();
auto entryPointLayout = m_currentRootObject->getLayout()->getEntryPoint(entryPointIndex);
auto entryPointName = entryPointLayout->getEntryPointName();
Expand Down
7 changes: 4 additions & 3 deletions src/cpu/cpu-device.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ class DeviceImpl : public ImmediateComputeDeviceBase
) override;

virtual SLANG_NO_THROW Result SLANG_MCALL
createComputePipeline(const ComputePipelineDesc& desc, IPipeline** outPipeline) override;
createComputePipeline(const ComputePipelineDesc& desc, IComputePipeline** outPipeline) override;

virtual SLANG_NO_THROW Result SLANG_MCALL
createQueryPool(const IQueryPool::Desc& desc, IQueryPool** outPool) override;
Expand All @@ -65,11 +65,12 @@ class DeviceImpl : public ImmediateComputeDeviceBase
virtual void unmap(IBuffer* buffer, size_t offsetWritten, size_t sizeWritten) override;

private:
RefPtr<PipelineImpl> m_currentPipeline = nullptr;
RefPtr<ComputePipelineImpl> m_currentPipeline = nullptr;
RefPtr<RootShaderObjectImpl> m_currentRootObject = nullptr;
DeviceInfo m_info;

virtual void setPipeline(IPipeline* state) override;
virtual void setRenderPipeline(IRenderPipeline* pipeline) override;
virtual void setComputePipeline(IComputePipeline* pipeline) override;

virtual void bindRootShaderObject(IShaderObject* object) override;

Expand Down
Loading