forked from llvm/offload-test-suite
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDevice.h
More file actions
338 lines (273 loc) · 10.2 KB
/
Copy pathDevice.h
File metadata and controls
338 lines (273 loc) · 10.2 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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
//===- Device.h - Offload API Device API ----------------------------------===//
//
// 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_DEVICE_H
#define OFFLOADTEST_API_DEVICE_H
#include "Config.h"
#include "API/API.h"
#include "API/AccelerationStructure.h"
#include "API/Buffer.h"
#include "API/Capabilities.h"
#include "API/CommandBuffer.h"
#include "API/RenderPass.h"
#include "API/Texture.h"
#include "Support/Pipeline.h"
#include "llvm/ADT/ArrayRef.h"
#include "llvm/ADT/StringRef.h"
#include "llvm/ADT/iterator_range.h"
#include "llvm/Support/Error.h"
#include "llvm/Support/ErrorHandling.h"
#include "llvm/Support/MemoryBuffer.h"
#include <cstdint>
#include <memory>
#include <optional>
#include <string>
namespace llvm {
class raw_ostream;
} // namespace llvm
namespace offloadtest {
struct Pipeline;
struct DeviceConfig {
bool EnableDebugLayer = false;
bool EnableValidationLayer = false;
};
struct InputLayoutDesc {
std::string Name;
Format Fmt;
uint32_t OffsetInBytes;
std::optional<uint32_t> InstanceStepRate;
};
struct ResourceBindingDesc {
ResourceKind Kind;
DirectXBinding DXBinding;
std::optional<VulkanBinding> VKBinding;
uint32_t DescriptorCount;
};
struct DescriptorSetLayoutDesc {
llvm::SmallVector<ResourceBindingDesc> ResourceBindings;
};
struct PushConstantsRange {
uint32_t OffsetInBytes; // Must be multiple of 4
uint32_t SizeInBytes; // Must be multiple of 4
};
struct BindingsDesc {
llvm::SmallVector<DescriptorSetLayoutDesc> DescriptorSetDescs;
llvm::SmallVector<PushConstantsRange> PushConstantRanges;
};
struct ShaderContainer {
std::string EntryPoint;
const llvm::MemoryBuffer *Shader;
llvm::SmallVector<SpecializationConstant> SpecializationConstants;
};
struct TraditionalRasterPipelineCreateDesc {
llvm::SmallVector<InputLayoutDesc> InputLayout;
llvm::SmallVector<Format> RTFormats;
std::optional<Format> DSFormat;
PrimitiveTopology Topology;
// Set if Topology == PatchList. Validated in
// Pipeline.cpp::validatePipelineKind.
std::optional<uint32_t> PatchControlPoints;
ShaderContainer VS;
// Hull and Domain are independent optionals here; Pipeline.cpp enforces that
// they must be set as a pair (and only with PatchList topology).
std::optional<ShaderContainer> HS;
std::optional<ShaderContainer> DS;
std::optional<ShaderContainer> GS;
ShaderContainer PS;
void setShader(Stages Stage, ShaderContainer &&SC) {
switch (Stage) {
case Stages::Vertex:
VS = std::move(SC);
break;
case Stages::Hull:
HS = std::move(SC);
break;
case Stages::Domain:
DS = std::move(SC);
break;
case Stages::Geometry:
GS = std::move(SC);
break;
case Stages::Pixel:
PS = std::move(SC);
break;
case Stages::Compute:
case Stages::Amplification:
case Stages::Mesh:
llvm_unreachable("Not a traditional raster pipeline stage.");
}
}
};
struct MeshShaderRasterPipelineCreateDesc {
llvm::SmallVector<Format> RTFormats;
std::optional<Format> DSFormat;
PrimitiveTopology Topology;
ShaderContainer MS;
std::optional<ShaderContainer> AS;
std::optional<ShaderContainer> PS;
void setShader(Stages Stage, ShaderContainer &&SC) {
switch (Stage) {
case Stages::Amplification:
AS = std::move(SC);
break;
case Stages::Mesh:
MS = std::move(SC);
break;
case Stages::Pixel:
PS = std::move(SC);
break;
case Stages::Vertex:
case Stages::Hull:
case Stages::Domain:
case Stages::Geometry:
case Stages::Compute:
llvm_unreachable("Not a mesh raster pipeline stage.");
}
}
};
class PipelineState {
public:
GPUAPI API;
virtual ~PipelineState() = default;
PipelineState(const Buffer &) = delete;
PipelineState &operator=(const Buffer &) = delete;
GPUAPI getAPI() const { return API; }
protected:
explicit PipelineState(GPUAPI API) : API(API) {}
};
class Fence {
public:
virtual ~Fence();
Fence(const Fence &) = delete;
Fence &operator=(const Fence &) = delete;
virtual uint64_t getFenceValue() = 0;
virtual llvm::Error waitForCompletion(uint64_t SignalValue) = 0;
protected:
Fence() = default;
};
/// Returned by Queue::submit() so that callers can decide when to block.
struct SubmitResult {
Fence *F;
uint64_t Value;
llvm::Error waitForCompletion() const { return F->waitForCompletion(Value); }
};
class Queue {
public:
virtual ~Queue() = 0;
Queue(const Queue &) = delete;
Queue &operator=(const Queue &) = delete;
Queue(Queue &&) = default;
Queue &operator=(Queue &&) = default;
/// Submit command buffers for GPU execution. Returns a fence + value that
/// the caller can wait on; the call itself does not block.
virtual llvm::Expected<SubmitResult>
submit(llvm::SmallVector<std::unique_ptr<CommandBuffer>> CBs) = 0;
/// Convenience overload for submitting a single command buffer.
llvm::Expected<SubmitResult> submit(std::unique_ptr<CommandBuffer> CB) {
llvm::SmallVector<std::unique_ptr<CommandBuffer>> CBs;
CBs.push_back(std::move(CB));
return submit(std::move(CBs));
}
protected:
Queue() = default;
};
class Device {
protected:
std::string Description;
std::string DriverName;
std::string DriverVersion;
std::string GPUGeneration;
uint16_t FamilyPrefix = 0;
public:
virtual const Capabilities &getCapabilities() = 0;
virtual llvm::StringRef getAPIName() const = 0;
virtual GPUAPI getAPI() const = 0;
virtual llvm::Error executeProgram(Pipeline &P) = 0;
virtual Queue &getGraphicsQueue() = 0;
virtual llvm::Expected<std::unique_ptr<PipelineState>>
createPipelineCs(llvm::StringRef Name, const BindingsDesc &BindingsDesc,
ShaderContainer CS) = 0;
virtual llvm::Expected<std::unique_ptr<PipelineState>>
createTraditionalRasterPipeline(
llvm::StringRef Name, const BindingsDesc &BindingsDesc,
const TraditionalRasterPipelineCreateDesc &Desc) = 0;
virtual llvm::Expected<std::unique_ptr<PipelineState>>
createMeshShaderRasterPipeline(
llvm::StringRef Name, const BindingsDesc &BindingsDesc,
const MeshShaderRasterPipelineCreateDesc &Desc) = 0;
virtual llvm::Expected<std::unique_ptr<Fence>>
createFence(llvm::StringRef Name) = 0;
virtual llvm::Expected<std::unique_ptr<Buffer>>
createBuffer(std::string Name, const BufferCreateDesc &Desc,
size_t SizeInBytes) = 0;
virtual llvm::Expected<std::unique_ptr<Texture>>
createTexture(std::string Name, const TextureCreateDesc &Desc) = 0;
// The row stride required when uploading data to (or reading back from) a
// texture created with the given description, via an upload buffer.
virtual uint32_t
getTextureUploadRowStrideInBytes(const TextureCreateDesc &Desc) const = 0;
virtual llvm::Expected<std::unique_ptr<RenderPass>>
createRenderPass(const RenderPassDesc &Desc) = 0;
virtual void printExtra(llvm::raw_ostream &OS) {}
virtual llvm::Expected<std::unique_ptr<CommandBuffer>>
createCommandBuffer() = 0;
virtual llvm::Expected<BLASBuildRequest> createTriangleBLASBuildRequest(
llvm::ArrayRef<TriangleGeometryDesc> Triangles) = 0;
virtual llvm::Expected<BLASBuildRequest>
createAABBBLASBuildRequest(llvm::ArrayRef<AABBGeometryDesc> AABBs) = 0;
virtual llvm::Expected<TLASBuildRequest> createTLASBuildRequest(
llvm::ArrayRef<AccelerationStructureInstance> Instances) = 0;
virtual llvm::Expected<std::unique_ptr<AccelerationStructure>>
createAccelerationStructure(const BLASBuildRequest &Request) = 0;
virtual llvm::Expected<std::unique_ptr<AccelerationStructure>>
createAccelerationStructure(const TLASBuildRequest &Request) = 0;
virtual ~Device() = 0;
llvm::StringRef getDescription() const { return Description; }
llvm::StringRef getDriverName() const { return DriverName; }
llvm::StringRef getDriverVersion() const { return DriverVersion; }
llvm::StringRef getGPUGeneration() const { return GPUGeneration; }
uint16_t getFamilyPrefix() const { return FamilyPrefix; }
};
llvm::Error
initializeDX12Devices(const DeviceConfig Config,
llvm::SmallVectorImpl<std::unique_ptr<Device>> &Devices);
llvm::Error initializeVulkanDevices(
const DeviceConfig Config,
llvm::SmallVectorImpl<std::unique_ptr<Device>> &Devices);
llvm::Error
initializeMetalDevices(const DeviceConfig Config,
llvm::SmallVectorImpl<std::unique_ptr<Device>> &Devices);
llvm::Expected<llvm::SmallVector<std::unique_ptr<Device>>>
initializeDevices(const DeviceConfig Config);
// Creates a render target texture using the format and dimensions from a
// CPUBuffer. Does not upload the buffer's data — only uses its description to
// configure the texture.
llvm::Expected<std::unique_ptr<Texture>>
createRenderTargetFromCPUBuffer(Device &Dev, const CPUBuffer &Buf);
// Creates a depth/stencil texture matching the dimensions of a render target.
llvm::Expected<std::unique_ptr<Texture>>
createDefaultDepthStencilTarget(Device &Dev, uint32_t Width, uint32_t Height);
llvm::Expected<std::unique_ptr<offloadtest::Buffer>>
createBufferWithData(Device &Dev, std::string Name,
const BufferCreateDesc &Desc, const void *Data,
size_t SizeInBytes, ComputeEncoder *Encoder,
std::unique_ptr<offloadtest::Buffer> *OutUploadBuffer);
llvm::Expected<std::unique_ptr<offloadtest::Buffer>>
createBufferWithData(Device &Dev, std::string Name,
const BufferCreateDesc &Desc, const void *Data,
size_t SizeInBytes, ComputeEncoder *Encoder,
std::unique_ptr<offloadtest::Buffer> *OutUploadBuffer);
llvm::Expected<std::unique_ptr<offloadtest::Texture>>
createTextureWithData(Device &Dev, std::string Name,
const TextureCreateDesc &Desc, const void *Data,
size_t SizeInBytes, ComputeEncoder *Encoder,
std::unique_ptr<offloadtest::Buffer> *OutUploadBuffer);
} // namespace offloadtest
#endif // OFFLOADTEST_API_DEVICE_H