-
Notifications
You must be signed in to change notification settings - Fork 33
[Metal] Explicit root signature layout for Metal backend #1061
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
Changes from all commits
ed72897
ea80cc7
5c75274
5be3196
c5a6df1
b668f0d
e88c319
be01698
8fb0183
9aeace2
d0357a2
5b4b465
d88e843
9858a2d
38f1bb9
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,80 @@ | ||
| //===- MTL/MTLDescriptorHeap.cpp - Metal Descriptor Heap ------------------===// | ||
| // | ||
| // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
| // See https://llvm.org/LICENSE.txt for license information. | ||
| // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
| // | ||
| //===----------------------------------------------------------------------===// | ||
|
|
||
| #include "MTLDescriptorHeap.h" | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Missing file header - we do want the license statement in every file. |
||
| #include "MetalIRConverter.h" | ||
|
|
||
| using namespace offloadtest; | ||
|
|
||
| static NS::UInteger getDescriptorHeapBindPoint(MTLDescriptorHeapType Type) { | ||
| switch (Type) { | ||
| case MTLDescriptorHeapType::CBV_SRV_UAV: | ||
| return kIRDescriptorHeapBindPoint; | ||
| case MTLDescriptorHeapType::Sampler: | ||
| return kIRSamplerHeapBindPoint; | ||
| } | ||
| llvm_unreachable("All cases handled."); | ||
| } | ||
|
|
||
| MTLGPUDescriptorHandle & | ||
| MTLGPUDescriptorHandle::addOffset(int32_t OffsetInDescriptors) { | ||
| Ptr = MTL::GPUAddress(int64_t(Ptr) + int64_t(OffsetInDescriptors) * | ||
| sizeof(IRDescriptorTableEntry)); | ||
| return *this; | ||
| } | ||
|
|
||
| llvm::Expected<std::unique_ptr<MTLDescriptorHeap>> | ||
| MTLDescriptorHeap::create(MTL::Device *Device, | ||
| const MTLDescriptorHeapDesc &Desc) { | ||
| if (!Device) | ||
| return llvm::createStringError(std::errc::invalid_argument, | ||
| "Invalid MTL::Device pointer."); | ||
|
|
||
| if (Desc.NumDescriptors == 0) | ||
| return llvm::createStringError(std::errc::invalid_argument, | ||
| "Invalid descriptor heap description."); | ||
|
|
||
| MTL::Buffer *Buf = | ||
| Device->newBuffer(Desc.NumDescriptors * sizeof(IRDescriptorTableEntry), | ||
| MTL::ResourceStorageModeShared); | ||
| if (!Buf) | ||
| return llvm::createStringError(std::errc::not_enough_memory, | ||
| "Failed to create MTLDescriptorHeap."); | ||
| return std::make_unique<MTLDescriptorHeap>(Desc, Buf); | ||
| } | ||
|
|
||
| MTLDescriptorHeap::~MTLDescriptorHeap() { | ||
| if (Buffer) | ||
| Buffer->release(); | ||
| } | ||
|
|
||
| MTLGPUDescriptorHandle | ||
| MTLDescriptorHeap::getGPUDescriptorHandleForHeapStart() const { | ||
| return MTLGPUDescriptorHandle{Buffer->gpuAddress()}; | ||
| } | ||
|
|
||
| IRDescriptorTableEntry * | ||
| MTLDescriptorHeap::getEntryHandle(uint32_t Index) const { | ||
| assert(Index < Desc.NumDescriptors && "Descriptor index out of bounds."); | ||
| return static_cast<IRDescriptorTableEntry *>(Buffer->contents()) + Index; | ||
| } | ||
|
|
||
| void MTLDescriptorHeap::bind(MTL::RenderCommandEncoder *Encoder) { | ||
| Encoder->useResource(Buffer, MTL::ResourceUsageRead); | ||
| // Dynamic resource indexing | ||
| const NS::UInteger BindPoint = getDescriptorHeapBindPoint(Desc.Type); | ||
| Encoder->setVertexBuffer(Buffer, 0, BindPoint); | ||
| Encoder->setFragmentBuffer(Buffer, 0, BindPoint); | ||
| } | ||
|
|
||
| void MTLDescriptorHeap::bind(MTL::ComputeCommandEncoder *Encoder) { | ||
| Encoder->useResource(Buffer, MTL::ResourceUsageRead); | ||
| // Dynamic resource indexing | ||
| const NS::UInteger BindPoint = getDescriptorHeapBindPoint(Desc.Type); | ||
| Encoder->setBuffer(Buffer, 0, BindPoint); | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,66 @@ | ||
| //===- MTLDescriptorHeap.h - Metal Descriptor Heap ------------------------===// | ||
| // | ||
| // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
| // See https://llvm.org/LICENSE.txt for license information. | ||
| // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
| // | ||
| //===----------------------------------------------------------------------===// | ||
|
|
||
| #ifndef OFFLOADTEST_API_MTL_MTLDESCRIPTORHEAP_H | ||
| #define OFFLOADTEST_API_MTL_MTLDESCRIPTORHEAP_H | ||
|
|
||
| #include "llvm/Support/Error.h" | ||
| #include <memory> | ||
|
|
||
| // Forward declarations | ||
| namespace MTL { | ||
| class Device; | ||
| class Buffer; | ||
| class RenderCommandEncoder; | ||
| class ComputeCommandEncoder; | ||
| } // namespace MTL | ||
| struct IRDescriptorTableEntry; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's a bit awkward that |
||
|
|
||
| namespace offloadtest { | ||
| struct MTLGPUDescriptorHandle { | ||
| MTLGPUDescriptorHandle &addOffset(int32_t OffsetInDescriptors); | ||
|
|
||
| uint64_t Ptr; | ||
| }; | ||
|
|
||
| enum class MTLDescriptorHeapType { | ||
| CBV_SRV_UAV, | ||
| Sampler, | ||
| }; | ||
|
|
||
| struct MTLDescriptorHeapDesc { | ||
| MTLDescriptorHeapType Type; | ||
| uint32_t NumDescriptors; | ||
| }; | ||
|
|
||
| // MTLDescriptorHeap mimics the D3D12 descriptor heap concept, except | ||
| // MTLDescriptorHeap is always shader visible and meant to be used | ||
| // by the argument buffer for shader resource binding with the explicit root | ||
| // signature layout. | ||
| class MTLDescriptorHeap { | ||
| MTLDescriptorHeapDesc Desc; | ||
| MTL::Buffer *Buffer; | ||
|
|
||
| public: | ||
| static llvm::Expected<std::unique_ptr<MTLDescriptorHeap>> | ||
| create(MTL::Device *Device, const MTLDescriptorHeapDesc &Desc); | ||
|
|
||
| MTLDescriptorHeap(const MTLDescriptorHeapDesc &Desc, MTL::Buffer *Buffer) | ||
| : Desc(Desc), Buffer(Buffer) {} | ||
| ~MTLDescriptorHeap(); | ||
|
|
||
| MTLGPUDescriptorHandle getGPUDescriptorHandleForHeapStart() const; | ||
|
|
||
| IRDescriptorTableEntry *getEntryHandle(uint32_t Index) const; | ||
|
|
||
| void bind(MTL::RenderCommandEncoder *Encoder); | ||
| void bind(MTL::ComputeCommandEncoder *Encoder); | ||
| }; | ||
| } // namespace offloadtest | ||
|
|
||
| #endif // OFFLOADTEST_API_MTL_MTLDESCRIPTORHEAP_H | ||
Uh oh!
There was an error while loading. Please reload this page.