-
Notifications
You must be signed in to change notification settings - Fork 33
Expand file tree
/
Copy pathMTLDescriptorHeap.cpp
More file actions
72 lines (61 loc) · 2.5 KB
/
MTLDescriptorHeap.cpp
File metadata and controls
72 lines (61 loc) · 2.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#include "MTLDescriptorHeap.h"
#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::Offset(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);
}