Skip to content

Commit

Permalink
Automatic resource state tracking (#58)
Browse files Browse the repository at this point in the history
* switch API to support automatic state tracking

* initial support for state tracking in D3D12

* initial support for vulkan state tracking

* disable failing tests

* refactor state tracking to support subresources

* remove dead code
  • Loading branch information
skallweitNV authored Sep 30, 2024
1 parent 921039b commit f3787da
Show file tree
Hide file tree
Showing 65 changed files with 1,163 additions and 1,274 deletions.
52 changes: 7 additions & 45 deletions include/slang-rhi.h
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,6 @@ enum class ResourceState
{
Undefined,
General,
PreInitialized,
VertexBuffer,
IndexBuffer,
ConstantBuffer,
Expand All @@ -344,8 +343,6 @@ enum class ResourceState
ResolveDestination,
AccelerationStructure,
AccelerationStructureBuildInput,
PixelShaderResource,
NonPixelShaderResource,
};

/// Describes how memory for the resource should be allocated for CPU access.
Expand Down Expand Up @@ -1399,12 +1396,10 @@ enum class StoreOp
struct RenderPassColorAttachment
{
ITextureView* view = nullptr;
ITextureView* resolveTarget = nullptr;
LoadOp loadOp = LoadOp::DontCare;
StoreOp storeOp = StoreOp::Store;
float clearValue[4] = {0.0f, 0.0f, 0.0f, 0.0f};
// TODO: remove with automatic resource tracking
ResourceState initialState = ResourceState::Undefined;
ResourceState finalState = ResourceState::Undefined;
};

struct RenderPassDepthStencilAttachment
Expand All @@ -1418,9 +1413,6 @@ struct RenderPassDepthStencilAttachment
StoreOp stencilStoreOp = StoreOp::DontCare;
uint8_t stencilClearValue = 0;
bool stencilReadOnly = false;
// TODO: remove with automatic resource tracking
ResourceState initialState = ResourceState::Undefined;
ResourceState finalState = ResourceState::Undefined;
};

struct RenderPassDesc
Expand Down Expand Up @@ -1503,27 +1495,14 @@ class ICommandEncoder : public ISlangUnknown
public:
virtual SLANG_NO_THROW void SLANG_MCALL endEncoding() = 0;

virtual SLANG_NO_THROW void SLANG_MCALL
textureBarrier(GfxCount count, ITexture* const* textures, ResourceState src, ResourceState dst) = 0;

inline void textureBarrier(ITexture* texture, ResourceState src, ResourceState dst)
{
textureBarrier(1, &texture, src, dst);
}

virtual SLANG_NO_THROW void SLANG_MCALL textureSubresourceBarrier(
ITexture* texture,
SubresourceRange subresourceRange,
ResourceState src,
ResourceState dst
) = 0;
virtual SLANG_NO_THROW void SLANG_MCALL setBufferState(IBuffer* buffer, ResourceState state) = 0;

virtual SLANG_NO_THROW void SLANG_MCALL
bufferBarrier(GfxCount count, IBuffer* const* buffers, ResourceState src, ResourceState dst) = 0;
setTextureState(ITexture* texture, SubresourceRange subresourceRange, ResourceState state) = 0;

inline void bufferBarrier(IBuffer* buffer, ResourceState src, ResourceState dst)
inline void setTextureState(ITexture* texture, ResourceState state)
{
bufferBarrier(1, &buffer, src, dst);
setTextureState(texture, kEntireTexture, state);
}

virtual SLANG_NO_THROW void SLANG_MCALL beginDebugEvent(const char* name, float rgbColor[3]) = 0;
Expand All @@ -1545,11 +1524,9 @@ class IResourceCommandEncoder : public ICommandEncoder
/// arguments are ignored.
virtual SLANG_NO_THROW void SLANG_MCALL copyTexture(
ITexture* dst,
ResourceState dstState,
SubresourceRange dstSubresource,
Offset3D dstOffset,
ITexture* src,
ResourceState srcState,
SubresourceRange srcSubresource,
Offset3D srcOffset,
Extents extent
Expand All @@ -1562,7 +1539,6 @@ class IResourceCommandEncoder : public ICommandEncoder
Size dstSize,
Size dstRowStride,
ITexture* src,
ResourceState srcState,
SubresourceRange srcSubresource,
Offset3D srcOffset,
Extents extent
Expand Down Expand Up @@ -1595,15 +1571,6 @@ class IResourceCommandEncoder : public ICommandEncoder
bool clearStencil = true
) = 0;

virtual SLANG_NO_THROW void SLANG_MCALL resolveResource(
ITexture* source,
ResourceState sourceState,
SubresourceRange sourceRange,
ITexture* dest,
ResourceState destState,
SubresourceRange destRange
) = 0;

virtual SLANG_NO_THROW void SLANG_MCALL
resolveQuery(IQueryPool* queryPool, GfxIndex index, GfxCount count, IBuffer* buffer, Offset offset) = 0;
};
Expand Down Expand Up @@ -2418,13 +2385,8 @@ class IDevice : public ISlangUnknown
createRayTracingPipeline(const RayTracingPipelineDesc& desc, IPipeline** outPipeline) = 0;

/// Read back texture resource and stores the result in `outBlob`.
virtual SLANG_NO_THROW SlangResult SLANG_MCALL readTexture(
ITexture* resource,
ResourceState state,
ISlangBlob** outBlob,
Size* outRowPitch,
Size* outPixelSize
) = 0;
virtual SLANG_NO_THROW SlangResult SLANG_MCALL
readTexture(ITexture* texture, ISlangBlob** outBlob, Size* outRowPitch, Size* outPixelSize) = 0;

virtual SLANG_NO_THROW SlangResult SLANG_MCALL
readBuffer(IBuffer* buffer, Offset offset, Size size, ISlangBlob** outBlob) = 0;
Expand Down
18 changes: 4 additions & 14 deletions src/command-encoder-com-forward.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,24 +13,14 @@
{ \
return CommandEncoderBase::release(); \
} \
virtual SLANG_NO_THROW void SLANG_MCALL \
textureBarrier(GfxCount count, ITexture* const* textures, ResourceState src, ResourceState dst) override \
virtual SLANG_NO_THROW void SLANG_MCALL setBufferState(IBuffer* buffer, ResourceState state) override \
{ \
CommandEncoderBase::textureBarrier(count, textures, src, dst); \
} \
virtual SLANG_NO_THROW void SLANG_MCALL textureSubresourceBarrier( \
ITexture* texture, \
SubresourceRange subresourceRange, \
ResourceState src, \
ResourceState dst \
) override \
{ \
CommandEncoderBase::textureSubresourceBarrier(texture, subresourceRange, src, dst); \
CommandEncoderBase::setBufferState(buffer, state); \
} \
virtual SLANG_NO_THROW void SLANG_MCALL \
bufferBarrier(GfxCount count, IBuffer* const* buffers, ResourceState src, ResourceState dst) override \
setTextureState(ITexture* texture, SubresourceRange subresourceRange, ResourceState state) override \
{ \
CommandEncoderBase::bufferBarrier(count, buffers, src, dst); \
CommandEncoderBase::setTextureState(texture, subresourceRange, state); \
} \
virtual SLANG_NO_THROW void SLANG_MCALL writeTimestamp(IQueryPool* pool, GfxIndex index) override \
{ \
Expand Down
50 changes: 5 additions & 45 deletions src/cuda/cuda-command-encoder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,33 +11,17 @@ void CommandEncoderImpl::init(CommandBufferImpl* cmdBuffer)
m_writer = cmdBuffer;
}

void CommandEncoderImpl::textureBarrier(GfxCount count, ITexture* const* textures, ResourceState src, ResourceState dst)
void CommandEncoderImpl::setBufferState(IBuffer* buffer, ResourceState state)
{
SLANG_UNUSED(count);
SLANG_UNUSED(textures);
SLANG_UNUSED(src);
SLANG_UNUSED(dst);
SLANG_UNUSED(buffer);
SLANG_UNUSED(state);
}

void CommandEncoderImpl::textureSubresourceBarrier(
ITexture* texture,
SubresourceRange subresourceRange,
ResourceState src,
ResourceState dst
)
void CommandEncoderImpl::setTextureState(ITexture* texture, SubresourceRange subresourceRange, ResourceState state)
{
SLANG_UNUSED(texture);
SLANG_UNUSED(subresourceRange);
SLANG_UNUSED(src);
SLANG_UNUSED(dst);
}

void CommandEncoderImpl::bufferBarrier(GfxCount count, IBuffer* const* buffers, ResourceState src, ResourceState dst)
{
SLANG_UNUSED(count);
SLANG_UNUSED(buffers);
SLANG_UNUSED(src);
SLANG_UNUSED(dst);
SLANG_UNUSED(state);
}

void CommandEncoderImpl::beginDebugEvent(const char* name, float rgbColor[3])
Expand Down Expand Up @@ -67,22 +51,18 @@ void ResourceCommandEncoderImpl::uploadBufferData(IBuffer* dst, Offset offset, S

void ResourceCommandEncoderImpl::copyTexture(
ITexture* dst,
ResourceState dstState,
SubresourceRange dstSubresource,
Offset3D dstOffset,
ITexture* src,
ResourceState srcState,
SubresourceRange srcSubresource,
Offset3D srcOffset,
Extents extent
)
{
SLANG_UNUSED(dst);
SLANG_UNUSED(dstState);
SLANG_UNUSED(dstSubresource);
SLANG_UNUSED(dstOffset);
SLANG_UNUSED(src);
SLANG_UNUSED(srcState);
SLANG_UNUSED(srcSubresource);
SLANG_UNUSED(srcOffset);
SLANG_UNUSED(extent);
Expand Down Expand Up @@ -130,24 +110,6 @@ void ResourceCommandEncoderImpl::clearTexture(
SLANG_RHI_UNIMPLEMENTED("clearBuffer");
}

void ResourceCommandEncoderImpl::resolveResource(
ITexture* source,
ResourceState sourceState,
SubresourceRange sourceRange,
ITexture* dest,
ResourceState destState,
SubresourceRange destRange
)
{
SLANG_UNUSED(source);
SLANG_UNUSED(sourceState);
SLANG_UNUSED(sourceRange);
SLANG_UNUSED(dest);
SLANG_UNUSED(destState);
SLANG_UNUSED(destRange);
SLANG_RHI_UNIMPLEMENTED("resolveResource");
}

void ResourceCommandEncoderImpl::resolveQuery(
IQueryPool* queryPool,
GfxIndex index,
Expand All @@ -170,7 +132,6 @@ void ResourceCommandEncoderImpl::copyTextureToBuffer(
Size dstSize,
Size dstRowStride,
ITexture* src,
ResourceState srcState,
SubresourceRange srcSubresource,
Offset3D srcOffset,
Extents extent
Expand All @@ -181,7 +142,6 @@ void ResourceCommandEncoderImpl::copyTextureToBuffer(
SLANG_UNUSED(dstSize);
SLANG_UNUSED(dstRowStride);
SLANG_UNUSED(src);
SLANG_UNUSED(srcState);
SLANG_UNUSED(srcSubresource);
SLANG_UNUSED(srcOffset);
SLANG_UNUSED(extent);
Expand Down
24 changes: 2 additions & 22 deletions src/cuda/cuda-command-encoder.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,18 +32,10 @@ class CommandEncoderImpl : public ICommandEncoder
public:
void init(CommandBufferImpl* cmdBuffer);

virtual SLANG_NO_THROW void SLANG_MCALL
textureBarrier(GfxCount count, ITexture* const* textures, ResourceState src, ResourceState dst) override;

virtual SLANG_NO_THROW void SLANG_MCALL textureSubresourceBarrier(
ITexture* texture,
SubresourceRange subresourceRange,
ResourceState src,
ResourceState dst
) override;
virtual SLANG_NO_THROW void SLANG_MCALL setBufferState(IBuffer* buffer, ResourceState state) override;

virtual SLANG_NO_THROW void SLANG_MCALL
bufferBarrier(GfxCount count, IBuffer* const* buffers, ResourceState src, ResourceState dst) override;
setTextureState(ITexture* texture, SubresourceRange subresourceRange, ResourceState state) override;

virtual SLANG_NO_THROW void SLANG_MCALL beginDebugEvent(const char* name, float rgbColor[3]) override;
virtual SLANG_NO_THROW void SLANG_MCALL endDebugEvent() override;
Expand Down Expand Up @@ -75,11 +67,9 @@ class ResourceCommandEncoderImpl : public IResourceCommandEncoder, public Comman

virtual SLANG_NO_THROW void SLANG_MCALL copyTexture(
ITexture* dst,
ResourceState dstState,
SubresourceRange dstSubresource,
Offset3D dstOffset,
ITexture* src,
ResourceState srcState,
SubresourceRange srcSubresource,
Offset3D srcOffset,
Extents extent
Expand All @@ -104,15 +94,6 @@ class ResourceCommandEncoderImpl : public IResourceCommandEncoder, public Comman
bool clearStencil
) override;

virtual SLANG_NO_THROW void SLANG_MCALL resolveResource(
ITexture* source,
ResourceState sourceState,
SubresourceRange sourceRange,
ITexture* dest,
ResourceState destState,
SubresourceRange destRange
) override;

virtual SLANG_NO_THROW void SLANG_MCALL
resolveQuery(IQueryPool* queryPool, GfxIndex index, GfxCount count, IBuffer* buffer, Offset offset) override;

Expand All @@ -122,7 +103,6 @@ class ResourceCommandEncoderImpl : public IResourceCommandEncoder, public Comman
Size dstSize,
Size dstRowStride,
ITexture* src,
ResourceState srcState,
SubresourceRange srcSubresource,
Offset3D srcOffset,
Extents extent
Expand Down
8 changes: 1 addition & 7 deletions src/cuda/cuda-device.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1079,13 +1079,7 @@ Result DeviceImpl::createRenderPipeline(const RenderPipelineDesc& desc, IPipelin
return SLANG_E_NOT_AVAILABLE;
}

Result DeviceImpl::readTexture(
ITexture* texture,
ResourceState state,
ISlangBlob** outBlob,
size_t* outRowPitch,
size_t* outPixelSize
)
Result DeviceImpl::readTexture(ITexture* texture, ISlangBlob** outBlob, size_t* outRowPitch, size_t* outPixelSize)
{
auto textureImpl = static_cast<TextureImpl*>(texture);

Expand Down
3 changes: 1 addition & 2 deletions src/cuda/cuda-device.h
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,7 @@ class DeviceImpl : public Device
createRenderPipeline(const RenderPipelineDesc& desc, IPipeline** outPipeline) override;

virtual SLANG_NO_THROW Result SLANG_MCALL
readTexture(ITexture* texture, ResourceState state, ISlangBlob** outBlob, size_t* outRowPitch, size_t* outPixelSize)
override;
readTexture(ITexture* texture, ISlangBlob** outBlob, size_t* outRowPitch, size_t* outPixelSize) override;

virtual SLANG_NO_THROW Result SLANG_MCALL
readBuffer(IBuffer* buffer, size_t offset, size_t size, ISlangBlob** outBlob) override;
Expand Down
6 changes: 0 additions & 6 deletions src/d3d/d3d-util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -922,8 +922,6 @@ D3D12_RESOURCE_STATES D3DUtil::getResourceState(ResourceState state)
return D3D12_RESOURCE_STATE_COMMON;
case ResourceState::General:
return D3D12_RESOURCE_STATE_COMMON;
case ResourceState::PreInitialized:
return D3D12_RESOURCE_STATE_COMMON;
case ResourceState::VertexBuffer:
return D3D12_RESOURCE_STATE_VERTEX_AND_CONSTANT_BUFFER;
case ResourceState::IndexBuffer:
Expand All @@ -935,10 +933,6 @@ D3D12_RESOURCE_STATES D3DUtil::getResourceState(ResourceState state)
case ResourceState::ShaderResource:
case ResourceState::AccelerationStructureBuildInput:
return D3D12_RESOURCE_STATE_PIXEL_SHADER_RESOURCE | D3D12_RESOURCE_STATE_NON_PIXEL_SHADER_RESOURCE;
case ResourceState::PixelShaderResource:
return D3D12_RESOURCE_STATE_PIXEL_SHADER_RESOURCE;
case ResourceState::NonPixelShaderResource:
return D3D12_RESOURCE_STATE_NON_PIXEL_SHADER_RESOURCE;
case ResourceState::UnorderedAccess:
return D3D12_RESOURCE_STATE_UNORDERED_ACCESS;
case ResourceState::RenderTarget:
Expand Down
Loading

0 comments on commit f3787da

Please sign in to comment.