Skip to content

Commit

Permalink
Merged internal pull request "Sampler Feedback and Sparse Textures" (…
Browse files Browse the repository at this point in the history
…!58)
  • Loading branch information
apanteleev committed Oct 29, 2024
2 parents 10a3d84 + 4fa26f2 commit 0a8bdf1
Show file tree
Hide file tree
Showing 18 changed files with 658 additions and 20 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ jobs:
submodules: true

- name: Configure
run: cmake -B ${{github.workspace}}/build "-DCMAKE_SYSTEM_VERSION=10.0.19041.0" -DNVRHI_WITH_RTXMU=${{matrix.rtxmu}} -DNVRHI_BUILD_SHARED=ON -DNVRHI_INSTALL=ON -DCMAKE_INSTALL_PREFIX=${{github.workspace}}/build/install
run: cmake -B ${{github.workspace}}/build "-DCMAKE_SYSTEM_VERSION=10.0.22621.0" -DNVRHI_WITH_RTXMU=${{matrix.rtxmu}} -DNVRHI_BUILD_SHARED=ON -DNVRHI_INSTALL=ON -DCMAKE_INSTALL_PREFIX=${{github.workspace}}/build/install

- name: Build
run: cmake --build ${{github.workspace}}/build --config ${{env.BUILD_TYPE}}
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ Various early versions of NVRHI have been used in various projects created at NV
* Windows or Linux (x64 or ARM64)
* CMake 3.10
* A C++ 17 compiler (Visual Studio 2019, GCC 8 or Clang 6)
* Windows SDK version 10.0.19041.0 or later for DX12 support
* Windows SDK version 10.0.22621.0 or later for DX12 support

## Building NVRHI

Expand Down
6 changes: 6 additions & 0 deletions include/nvrhi/d3d12.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,10 @@ namespace nvrhi::d3d12

virtual void updateGraphicsVolatileBuffers() = 0;
virtual void updateComputeVolatileBuffers() = 0;

virtual void clearSamplerFeedbackTexture(ISamplerFeedbackTexture* texture) = 0;
virtual void decodeSamplerFeedbackTexture(IBuffer* buffer, ISamplerFeedbackTexture* texture, nvrhi::Format format) = 0;
virtual void setSamplerFeedbackTextureState(ISamplerFeedbackTexture* texture, ResourceStates stateBits) = 0;
};

typedef RefCountPtr<ICommandList> CommandListHandle;
Expand Down Expand Up @@ -100,6 +104,8 @@ namespace nvrhi::d3d12
virtual GraphicsPipelineHandle createHandleForNativeGraphicsPipeline(IRootSignature* rootSignature, ID3D12PipelineState* pipelineState, const GraphicsPipelineDesc& desc, const FramebufferInfo& framebufferInfo) = 0;
virtual MeshletPipelineHandle createHandleForNativeMeshletPipeline(IRootSignature* rootSignature, ID3D12PipelineState* pipelineState, const MeshletPipelineDesc& desc, const FramebufferInfo& framebufferInfo) = 0;
[[nodiscard]] virtual IDescriptorHeap* getDescriptorHeap(DescriptorHeapType heapType) = 0;
virtual SamplerFeedbackTextureHandle createSamplerFeedbackTexture(ITexture* pairedTexture, const SamplerFeedbackTextureDesc& desc) = 0;
virtual SamplerFeedbackTextureHandle createSamplerFeedbackForNativeTexture(ObjectType objectType, Object texture, ITexture* pairedTexture) = 0;
};

typedef RefCountPtr<IDevice> DeviceHandle;
Expand Down
94 changes: 93 additions & 1 deletion include/nvrhi/nvrhi.h
Original file line number Diff line number Diff line change
Expand Up @@ -421,6 +421,7 @@ namespace nvrhi
// and memory is bound to the texture later using bindTextureMemory.
// On DX12, the texture resource is created at the time of memory binding.
bool isVirtual = false;
bool isTiled = false;

Color clearValue;
bool useClearValue = false;
Expand Down Expand Up @@ -541,6 +542,79 @@ namespace nvrhi
};
typedef RefCountPtr<IStagingTexture> StagingTextureHandle;

struct TiledTextureCoordinate
{
uint16_t mipLevel = 0;
uint16_t arrayLevel = 0;
uint32_t x = 0;
uint32_t y = 0;
uint32_t z = 0;
};

struct TiledTextureRegion
{
uint32_t tilesNum = 0;
uint32_t width = 0;
uint32_t height = 0;
uint32_t depth = 0;
};

struct TextureTilesMapping
{
TiledTextureCoordinate* tiledTextureCoordinates = nullptr;
TiledTextureRegion* tiledTextureRegions = nullptr;
uint64_t* byteOffsets = nullptr;
uint32_t numTextureRegions = 0;
IHeap* heap = nullptr;
};

struct PackedMipDesc
{
uint32_t numStandardMips = 0;
uint32_t numPackedMips = 0;
uint32_t numTilesForPackedMips = 0;
uint32_t startTileIndexInOverallResource = 0;
};

struct TileShape
{
uint32_t widthInTexels = 0;
uint32_t heightInTexels = 0;
uint32_t depthInTexels = 0;
};

struct SubresourceTiling
{
uint32_t widthInTiles = 0;
uint32_t heightInTiles = 0;
uint32_t depthInTiles = 0;
uint32_t startTileIndexInOverallResource = 0;
};

enum SamplerFeedbackFormat : uint8_t
{
MinMipOpaque = 0x0,
MipRegionUsedOpaque = 0x1,
};

struct SamplerFeedbackTextureDesc
{
SamplerFeedbackFormat samplerFeedbackFormat = SamplerFeedbackFormat::MinMipOpaque;
uint32_t samplerFeedbackMipRegionX = 0;
uint32_t samplerFeedbackMipRegionY = 0;
uint32_t samplerFeedbackMipRegionZ = 0;
ResourceStates initialState = ResourceStates::Unknown;
bool keepInitialState = false;
};

class ISamplerFeedbackTexture : public IResource
{
public:
[[nodiscard]] virtual const SamplerFeedbackTextureDesc& getDesc() const = 0;
virtual TextureHandle getPairedTexture() = 0;
};
typedef RefCountPtr<ISamplerFeedbackTexture> SamplerFeedbackTextureHandle;

//////////////////////////////////////////////////////////////////////////
// Input Layout
//////////////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -600,7 +674,7 @@ namespace nvrhi
bool isVolatile = false;

// Indicates that the buffer is created with no backing memory,
// and memory is bound to the texture later using bindBufferMemory.
// and memory is bound to the buffer later using bindBufferMemory.
// On DX12, the buffer resource is created at the time of memory binding.
bool isVirtual = false;

Expand Down Expand Up @@ -1560,6 +1634,7 @@ namespace nvrhi
Sampler,
RayTracingAccelStruct,
PushConstants,
SamplerFeedbackTexture_UAV,

Count
};
Expand Down Expand Up @@ -1601,6 +1676,7 @@ namespace nvrhi
NVRHI_BINDING_LAYOUT_ITEM_INITIALIZER(VolatileConstantBuffer)
NVRHI_BINDING_LAYOUT_ITEM_INITIALIZER(Sampler)
NVRHI_BINDING_LAYOUT_ITEM_INITIALIZER(RayTracingAccelStruct)
NVRHI_BINDING_LAYOUT_ITEM_INITIALIZER(SamplerFeedbackTexture_UAV)

static BindingLayoutItem PushConstants(const uint32_t slot, const size_t size)
{
Expand Down Expand Up @@ -1918,6 +1994,19 @@ namespace nvrhi
return result;
}

static BindingSetItem SamplerFeedbackTexture_UAV(uint32_t slot, ISamplerFeedbackTexture* texture)
{
BindingSetItem result;
result.slot = slot;
result.type = ResourceType::SamplerFeedbackTexture_UAV;
result.resourceHandle = texture;
result.format = Format::UNKNOWN;
result.dimension = TextureDimension::Unknown;
result.subresources = AllSubresources;
result.unused = 0;
return result;
}

BindingSetItem& setFormat(Format value) { format = value; return *this; }
BindingSetItem& setDimension(TextureDimension value) { dimension = value; return *this; }
BindingSetItem& setSubresources(TextureSubresourceSet value) { subresources = value; return *this; }
Expand Down Expand Up @@ -2652,6 +2741,9 @@ namespace nvrhi
virtual void *mapStagingTexture(IStagingTexture* tex, const TextureSlice& slice, CpuAccessMode cpuAccess, size_t *outRowPitch) = 0;
virtual void unmapStagingTexture(IStagingTexture* tex) = 0;

virtual void getTextureTiling(ITexture* texture, uint32_t* numTiles, PackedMipDesc* desc, TileShape* tileShape, uint32_t* subresourceTilingsNum, SubresourceTiling* subresourceTilings) = 0;
virtual void updateTextureTileMappings(ITexture* texture, const TextureTilesMapping* tileMappings, uint32_t numTileMappings, CommandQueue executionQueue = CommandQueue::Graphics) = 0;

virtual BufferHandle createBuffer(const BufferDesc& d) = 0;
virtual void *mapBuffer(IBuffer* buffer, CpuAccessMode cpuAccess) = 0;
virtual void unmapBuffer(IBuffer* buffer) = 0;
Expand Down
1 change: 1 addition & 0 deletions src/common/state-tracking.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ namespace nvrhi
const TextureDesc& descRef;
ResourceStates permanentState = ResourceStates::Unknown;
bool stateInitialized = false;
bool isSamplerFeedback = false;

explicit TextureStateExtension(const TextureDesc& desc)
: descRef(desc)
Expand Down
3 changes: 3 additions & 0 deletions src/d3d11/d3d11-backend.h
Original file line number Diff line number Diff line change
Expand Up @@ -442,6 +442,9 @@ namespace nvrhi::d3d11
void *mapStagingTexture(IStagingTexture* tex, const TextureSlice& slice, CpuAccessMode cpuAccess, size_t *outRowPitch) override;
void unmapStagingTexture(IStagingTexture* tex) override;

void getTextureTiling(ITexture* texture, uint32_t* numTiles, PackedMipDesc* desc, TileShape* tileShape, uint32_t* subresourceTilingsNum, SubresourceTiling* subresourceTilings) override;
void updateTextureTileMappings(ITexture* texture, const TextureTilesMapping* tileMappings, uint32_t numTileMappings, CommandQueue executionQueue = CommandQueue::Graphics) override;

BufferHandle createBuffer(const BufferDesc& d) override;
void *mapBuffer(IBuffer* b, CpuAccessMode mapFlags) override;
void unmapBuffer(IBuffer* b) override;
Expand Down
22 changes: 22 additions & 0 deletions src/d3d11/d3d11-device.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,28 @@ namespace nvrhi::d3d11
return m_ImmediateCommandList;
}

void Device::getTextureTiling(ITexture* texture, uint32_t* numTiles, PackedMipDesc* desc, TileShape* tileShape, uint32_t* subresourceTilingsNum, SubresourceTiling* subresourceTilings)
{
(void)texture;
(void)numTiles;
(void)desc;
(void)tileShape;
(void)subresourceTilingsNum;
(void)subresourceTilings;

utils::NotSupported();
}

void Device::updateTextureTileMappings(ITexture* texture, const TextureTilesMapping* tileMappings, uint32_t numTileMappings, CommandQueue executionQueue)
{
(void)texture;
(void)tileMappings;
(void)numTileMappings;
(void)executionQueue;

utils::NotSupported();
}

bool Device::queryFeatureSupport(Feature feature, void* pInfo, size_t infoSize)
{
(void)pInfo;
Expand Down
47 changes: 46 additions & 1 deletion src/d3d12/d3d12-backend.h
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ namespace nvrhi::d3d12
RefCountPtr<ID3D12Device> device;
RefCountPtr<ID3D12Device2> device2;
RefCountPtr<ID3D12Device5> device5;
RefCountPtr<ID3D12Device8> device8;
#ifdef NVRHI_WITH_RTXMU
std::unique_ptr<rtxmu::DxAccelStructManager> rtxMemUtil;
#endif
Expand Down Expand Up @@ -243,6 +244,7 @@ namespace nvrhi::d3d12
HANDLE sharedHandle = nullptr;
HeapHandle heap;


Texture(const Context& context, DeviceResources& resources, TextureDesc desc, const D3D12_RESOURCE_DESC& resourceDesc)
: TextureStateExtension(this->desc)
, desc(std::move(desc))
Expand All @@ -254,7 +256,7 @@ namespace nvrhi::d3d12
}

~Texture() override;

const TextureDesc& getDesc() const override { return desc; }

Object getNativeObject(ObjectType objectType) override;
Expand Down Expand Up @@ -356,6 +358,38 @@ namespace nvrhi::d3d12
Object getNativeObject(ObjectType objectType) override;
};

class SamplerFeedbackTexture : public RefCounter<ISamplerFeedbackTexture>, public TextureStateExtension
{
public:
const SamplerFeedbackTextureDesc desc;
const TextureDesc textureDesc; // used with state tracking
RefCountPtr<ID3D12Resource> resource;
TextureHandle pairedTexture;

SamplerFeedbackTexture(const Context& context, DeviceResources& resources, SamplerFeedbackTextureDesc desc, TextureDesc textureDesc, ITexture* pairedTexture)
: desc(std::move(desc))
, textureDesc(std::move(textureDesc))
, m_Context(context)
, m_Resources(resources)
, pairedTexture(pairedTexture)
, TextureStateExtension(SamplerFeedbackTexture::textureDesc)
{
TextureStateExtension::stateInitialized = true;
TextureStateExtension::isSamplerFeedback = true;
}

const SamplerFeedbackTextureDesc& getDesc() const override { return desc; }
TextureHandle getPairedTexture() override { return pairedTexture; }

void createUAV(size_t descriptor) const;

Object getNativeObject(ObjectType objectType) override;

private:
const Context& m_Context;
DeviceResources& m_Resources;
};

class Sampler : public RefCounter<ISampler>
{
public:
Expand Down Expand Up @@ -868,6 +902,7 @@ namespace nvrhi::d3d12
~CommandList() override;
std::shared_ptr<CommandListInstance> executed(Queue* pQueue);
void requireTextureState(ITexture* texture, TextureSubresourceSet subresources, ResourceStates state);
void requireSamplerFeedbackTextureState(ISamplerFeedbackTexture* texture, ResourceStates state);
void requireBufferState(IBuffer* buffer, ResourceStates state);
ID3D12CommandList* getD3D12CommandList() const { return m_ActiveCommandList->commandList; }

Expand All @@ -884,6 +919,9 @@ namespace nvrhi::d3d12
void clearTextureFloat(ITexture* t, TextureSubresourceSet subresources, const Color& clearColor) override;
void clearDepthStencilTexture(ITexture* t, TextureSubresourceSet subresources, bool clearDepth, float depth, bool clearStencil, uint8_t stencil) override;
void clearTextureUInt(ITexture* t, TextureSubresourceSet subresources, uint32_t clearColor) override;
void clearSamplerFeedbackTexture(ISamplerFeedbackTexture* texture) override;
void decodeSamplerFeedbackTexture(IBuffer* buffer, ISamplerFeedbackTexture* texture, Format format) override;
void setSamplerFeedbackTextureState(ISamplerFeedbackTexture* texture, ResourceStates stateBits) override;

void copyTexture(ITexture* dest, const TextureSlice& destSlice, ITexture* src, const TextureSlice& srcSlice) override;
void copyTexture(IStagingTexture* dest, const TextureSlice& destSlice, ITexture* src, const TextureSlice& srcSlice) override;
Expand Down Expand Up @@ -1058,6 +1096,12 @@ namespace nvrhi::d3d12
void *mapStagingTexture(IStagingTexture* tex, const TextureSlice& slice, CpuAccessMode cpuAccess, size_t *outRowPitch) override;
void unmapStagingTexture(IStagingTexture* tex) override;

void getTextureTiling(ITexture* texture, uint32_t* numTiles, PackedMipDesc* desc, TileShape* tileShape, uint32_t* subresourceTilingsNum, SubresourceTiling* subresourceTilings) override;
void updateTextureTileMappings(ITexture* texture, const TextureTilesMapping* tileMappings, uint32_t numTileMappings, CommandQueue executionQueue = CommandQueue::Graphics) override;

SamplerFeedbackTextureHandle createSamplerFeedbackTexture(ITexture* pairedTexture, const SamplerFeedbackTextureDesc& desc) override;
SamplerFeedbackTextureHandle createSamplerFeedbackForNativeTexture(ObjectType objectType, Object texture, ITexture* pairedTexture) override;

BufferHandle createBuffer(const BufferDesc& d) override;
void *mapBuffer(IBuffer* b, CpuAccessMode mapFlags) override;
void unmapBuffer(IBuffer* b) override;
Expand Down Expand Up @@ -1160,6 +1204,7 @@ namespace nvrhi::d3d12
bool m_VariableRateShadingSupported = false;
bool m_OpacityMicromapSupported = false;
bool m_ShaderExecutionReorderingSupported = false;
bool m_SamplerFeedbackSupported = false;
bool m_AftermathEnabled = false;
AftermathCrashDumpHelper m_AftermathCrashDumpHelper;

Expand Down
Loading

0 comments on commit 0a8bdf1

Please sign in to comment.