-
Notifications
You must be signed in to change notification settings - Fork 31
Expand file tree
/
Copy pathPipeline.h
More file actions
690 lines (592 loc) · 17.9 KB
/
Pipeline.h
File metadata and controls
690 lines (592 loc) · 17.9 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
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
//===- Pipeline.h - GPU Pipeline Description --------------------*- C++ -*-===//
//
// 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_SUPPORT_PIPELINE_H
#define OFFLOADTEST_SUPPORT_PIPELINE_H
#include "API/Enums.h"
#include "llvm/ADT/SmallVector.h"
#include "llvm/ADT/StringRef.h"
#include "llvm/Support/MemoryBuffer.h"
#include "llvm/Support/YAMLTraits.h"
#include <limits>
#include <memory>
#include <string>
#include <variant>
namespace offloadtest {
enum class Stages { Compute, Vertex, Pixel };
enum class Rule { BufferExact, BufferFloatULP, BufferFloatEpsilon };
enum class DenormMode { Any, FTZ, Preserve };
enum class DataFormat {
Hex8,
Hex16,
Hex32,
Hex64,
UInt16,
UInt32,
UInt64,
Int16,
Int32,
Int64,
Float16,
Float32,
Float64,
Depth32,
Bool,
};
enum class FilterMode { Nearest, Linear };
enum class AddressMode { Clamp, Repeat, Mirror, Border, MirrorOnce };
enum class CompareFunction {
Never,
Less,
Equal,
LessEqual,
Greater,
NotEqual,
GreaterEqual,
Always
};
enum class SamplerKind { Sampler, SamplerComparison };
struct Sampler {
std::string Name;
FilterMode MinFilter = FilterMode::Linear;
FilterMode MagFilter = FilterMode::Linear;
AddressMode Address = AddressMode::Clamp;
float MinLOD = 0.0f;
float MaxLOD = std::numeric_limits<float>::max();
float MipLODBias = 0.0f;
CompareFunction ComparisonOp = CompareFunction::Never;
SamplerKind Kind = SamplerKind::Sampler;
};
struct DirectXBinding {
uint32_t Register;
uint32_t Space;
};
struct VulkanBinding {
uint32_t Binding;
std::optional<uint32_t> CounterBinding;
};
struct OutputProperties {
int Height;
int Width;
int Depth;
int MipLevels = 1;
};
static inline uint32_t getFormatSize(DataFormat Format) {
switch (Format) {
case DataFormat::Hex8:
return 1;
case DataFormat::Hex16:
case DataFormat::UInt16:
case DataFormat::Int16:
case DataFormat::Float16:
return 2;
case DataFormat::Hex32:
case DataFormat::UInt32:
case DataFormat::Int32:
case DataFormat::Float32:
case DataFormat::Depth32:
case DataFormat::Bool:
return 4;
case DataFormat::Hex64:
case DataFormat::UInt64:
case DataFormat::Int64:
case DataFormat::Float64:
return 8;
}
llvm_unreachable("All cases covered.");
}
struct CPUBuffer {
std::string Name;
DataFormat Format;
int Channels;
int Stride;
uint32_t ArraySize;
// Data can contain one block of data for a singular resource
// or multiple blocks for a resource array.
llvm::SmallVector<std::unique_ptr<char[]>> Data;
size_t Size;
OutputProperties OutputProps;
// Counters can contain one counter value for a singular resource
// or multiple values for an array of resources with counters.
llvm::SmallVector<uint32_t> Counters;
uint32_t size() const { return Size; }
uint32_t getSingleElementSize() const { return getFormatSize(Format); }
uint32_t getElementSize() const {
if (Stride > 0)
return Stride;
return getSingleElementSize() * Channels;
}
};
struct Result {
std::string Name;
Rule ComparisonRule;
std::string Actual;
std::string Expected;
CPUBuffer *ActualPtr = nullptr;
CPUBuffer *ExpectedPtr = nullptr;
DenormMode DM = DenormMode::Any;
unsigned ULPT; // ULP Tolerance
double Epsilon;
};
struct Resource {
ResourceKind Kind;
std::string Name;
DirectXBinding DXBinding;
std::optional<VulkanBinding> VKBinding;
CPUBuffer *BufferPtr = nullptr;
Sampler *SamplerPtr = nullptr;
bool HasCounter;
std::optional<uint32_t> TilesMapped;
bool IsReserved = false;
bool isRaw() const {
switch (Kind) {
case ResourceKind::Buffer:
case ResourceKind::RWBuffer:
case ResourceKind::Texture2D:
case ResourceKind::RWTexture2D:
case ResourceKind::Sampler:
case ResourceKind::SampledTexture2D:
return false;
case ResourceKind::StructuredBuffer:
case ResourceKind::RWStructuredBuffer:
case ResourceKind::ByteAddressBuffer:
case ResourceKind::RWByteAddressBuffer:
case ResourceKind::ConstantBuffer:
return true;
}
llvm_unreachable("All cases handled");
}
bool isSampler() const {
switch (Kind) {
case ResourceKind::Sampler:
return true;
case ResourceKind::Buffer:
case ResourceKind::RWBuffer:
case ResourceKind::StructuredBuffer:
case ResourceKind::RWStructuredBuffer:
case ResourceKind::ByteAddressBuffer:
case ResourceKind::RWByteAddressBuffer:
case ResourceKind::ConstantBuffer:
case ResourceKind::Texture2D:
case ResourceKind::RWTexture2D:
case ResourceKind::SampledTexture2D:
return false;
}
}
bool isTexture() const {
switch (Kind) {
case ResourceKind::Buffer:
case ResourceKind::RWBuffer:
case ResourceKind::StructuredBuffer:
case ResourceKind::RWStructuredBuffer:
case ResourceKind::ByteAddressBuffer:
case ResourceKind::RWByteAddressBuffer:
case ResourceKind::ConstantBuffer:
case ResourceKind::Sampler:
return false;
case ResourceKind::Texture2D:
case ResourceKind::RWTexture2D:
case ResourceKind::SampledTexture2D:
return true;
}
llvm_unreachable("All cases handled");
}
bool isByteAddressBuffer() const {
switch (Kind) {
case ResourceKind::ByteAddressBuffer:
case ResourceKind::RWByteAddressBuffer:
return true;
default:
return false;
}
}
bool isStructuredBuffer() const {
switch (Kind) {
case ResourceKind::StructuredBuffer:
case ResourceKind::RWStructuredBuffer:
return true;
default:
return false;
}
}
bool isSampledTexture() const {
switch (Kind) {
case ResourceKind::SampledTexture2D:
return true;
default:
return false;
}
}
uint32_t getElementSize() const {
assert(!isSampler() && "Samplers do not have element size");
// ByteAddressBuffers are treated as 4-byte elements to match their memory
// format.
return isByteAddressBuffer() ? 4 : BufferPtr->getElementSize();
}
uint32_t getArraySize() const {
return isSampler() ? 1 : BufferPtr->ArraySize;
}
uint32_t size() const {
assert(!isSampler() && "Samplers do not have size");
return BufferPtr->size();
}
bool isReadWrite() const {
switch (Kind) {
case ResourceKind::Buffer:
case ResourceKind::StructuredBuffer:
case ResourceKind::ByteAddressBuffer:
case ResourceKind::Texture2D:
case ResourceKind::ConstantBuffer:
case ResourceKind::Sampler:
case ResourceKind::SampledTexture2D:
return false;
case ResourceKind::RWBuffer:
case ResourceKind::RWStructuredBuffer:
case ResourceKind::RWByteAddressBuffer:
case ResourceKind::RWTexture2D:
return true;
}
llvm_unreachable("All cases handled");
}
bool isReadOnly() const { return !isReadWrite(); }
};
struct DescriptorSet {
llvm::SmallVector<Resource> Resources;
};
namespace dx {
enum class RootParamKind {
Constant,
DescriptorTable,
RootDescriptor,
};
struct RootResource : public Resource {};
struct RootConstant {
CPUBuffer *BufferPtr;
std::string Name;
};
struct RootParameter {
RootParamKind Kind;
std::variant<RootConstant, RootResource> Data;
};
struct Settings {
llvm::SmallVector<RootParameter> RootParams;
};
} // namespace dx
struct RuntimeSettings {
dx::Settings DX;
};
struct VertexAttribute {
DataFormat Format;
int Channels;
int Offset;
std::string Name;
uint32_t size() const { return getFormatSize(Format) * Channels; }
};
struct IOBindings {
std::string VertexBuffer;
CPUBuffer *VertexBufferPtr;
llvm::SmallVector<VertexAttribute> VertexAttributes;
std::string RenderTarget;
CPUBuffer *RTargetBufferPtr;
uint32_t getVertexStride() const {
uint32_t Stride = 0;
for (auto VA : VertexAttributes)
Stride += VA.size();
return Stride;
}
uint32_t getVertexCount() const {
return VertexBufferPtr->size() / getVertexStride();
}
};
// Describes a contiguous group of bytes in a push constant block.
struct PushConstantValue {
// Format used to describe those bytes in the YAML.
DataFormat Format;
// The bytes of this group.
llvm::SmallVector<char, 4> Data;
// The offset of this group from the start of the push constant buffer.
uint32_t OffsetInBytes;
};
// Describes the content of the push constant buffer.
struct PushConstantBlock {
// The stages this is push constant is active for.
Stages Stage;
// The values/group of values composing this buffer.
llvm::SmallVector<PushConstantValue> Values;
// True if there are no push constants.
bool empty() const { return size() == 0; }
// Layout the push constant content in output.
void getContent(llvm::SmallVectorImpl<uint8_t> &Output) const;
// Returns the size in bytes of the whole push constant once laid out.
uint32_t size() const;
};
struct SpecializationConstant {
uint32_t ConstantID;
DataFormat Type;
std::string Value;
};
struct Shader {
Stages Stage;
std::string Entry;
std::unique_ptr<llvm::MemoryBuffer> Shader;
std::unique_ptr<llvm::MemoryBuffer> Reflection;
int DispatchSize[3];
llvm::SmallVector<SpecializationConstant> SpecializationConstants;
};
struct Pipeline {
llvm::SmallVector<Shader> Shaders;
RuntimeSettings Settings;
IOBindings Bindings;
llvm::SmallVector<PushConstantBlock> PushConstants;
llvm::SmallVector<CPUBuffer> Buffers;
llvm::SmallVector<Sampler> Samplers;
llvm::SmallVector<Result> Results;
llvm::SmallVector<DescriptorSet> Sets;
uint32_t getDescriptorCount() const {
uint32_t DescriptorCount = 0;
for (auto &D : Sets)
DescriptorCount += D.Resources.size();
return DescriptorCount;
}
uint32_t getDescriptorCountWithFlattenedArrays() const {
uint32_t DescriptorCount = 0;
for (auto &D : Sets)
for (auto &R : D.Resources)
DescriptorCount += R.getArraySize();
return DescriptorCount;
}
CPUBuffer *getBuffer(llvm::StringRef Name) {
for (auto &B : Buffers)
if (Name == B.Name)
return &B;
return nullptr;
}
Sampler *getSampler(llvm::StringRef Name) {
for (auto &S : Samplers)
if (Name == S.Name)
return &S;
return nullptr;
}
bool isGraphics() const { return !isCompute(); }
bool isCompute() const {
return Shaders.size() == 1 && Shaders[0].Stage == Stages::Compute;
}
};
} // namespace offloadtest
LLVM_YAML_IS_SEQUENCE_VECTOR(offloadtest::DescriptorSet)
LLVM_YAML_IS_SEQUENCE_VECTOR(offloadtest::Resource)
LLVM_YAML_IS_SEQUENCE_VECTOR(offloadtest::CPUBuffer)
LLVM_YAML_IS_SEQUENCE_VECTOR(offloadtest::Sampler)
LLVM_YAML_IS_SEQUENCE_VECTOR(offloadtest::Shader)
LLVM_YAML_IS_SEQUENCE_VECTOR(offloadtest::dx::RootParameter)
LLVM_YAML_IS_SEQUENCE_VECTOR(offloadtest::Result)
LLVM_YAML_IS_SEQUENCE_VECTOR(offloadtest::VertexAttribute)
LLVM_YAML_IS_SEQUENCE_VECTOR(offloadtest::SpecializationConstant)
LLVM_YAML_IS_SEQUENCE_VECTOR(offloadtest::PushConstantBlock)
LLVM_YAML_IS_SEQUENCE_VECTOR(offloadtest::PushConstantValue)
namespace llvm {
namespace yaml {
template <> struct MappingTraits<offloadtest::Pipeline> {
static void mapping(IO &I, offloadtest::Pipeline &P);
};
template <> struct MappingTraits<offloadtest::DescriptorSet> {
static void mapping(IO &I, offloadtest::DescriptorSet &D);
};
template <> struct MappingTraits<offloadtest::CPUBuffer> {
static void mapping(IO &I, offloadtest::CPUBuffer &R);
};
template <> struct MappingTraits<offloadtest::Sampler> {
static void mapping(IO &I, offloadtest::Sampler &S);
};
template <> struct MappingTraits<offloadtest::Result> {
static void mapping(IO &I, offloadtest::Result &R);
};
template <> struct MappingTraits<offloadtest::Resource> {
static void mapping(IO &I, offloadtest::Resource &R);
};
template <> struct MappingTraits<offloadtest::DirectXBinding> {
static void mapping(IO &I, offloadtest::DirectXBinding &B);
};
template <> struct MappingTraits<offloadtest::VulkanBinding> {
static void mapping(IO &I, offloadtest::VulkanBinding &B);
};
template <> struct MappingTraits<offloadtest::IOBindings> {
static void mapping(IO &I, offloadtest::IOBindings &B);
};
template <> struct MappingTraits<offloadtest::PushConstantValue> {
static void mapping(IO &I, offloadtest::PushConstantValue &B);
};
template <> struct MappingTraits<offloadtest::PushConstantBlock> {
static void mapping(IO &I, offloadtest::PushConstantBlock &B);
};
template <> struct MappingTraits<offloadtest::VertexAttribute> {
static void mapping(IO &I, offloadtest::VertexAttribute &A);
};
template <> struct MappingTraits<offloadtest::OutputProperties> {
static void mapping(IO &I, offloadtest::OutputProperties &P);
};
template <> struct MappingTraits<offloadtest::Shader> {
static void mapping(IO &I, offloadtest::Shader &B);
};
template <> struct MappingTraits<offloadtest::dx::RootResource> {
static void mapping(IO &I, offloadtest::dx::RootResource &R);
};
template <> struct MappingTraits<offloadtest::dx::RootParameter> {
static void mapping(IO &I, offloadtest::dx::RootParameter &S);
};
template <> struct MappingTraits<offloadtest::dx::Settings> {
static void mapping(IO &I, offloadtest::dx::Settings &S);
};
template <> struct MappingTraits<offloadtest::RuntimeSettings> {
static void mapping(IO &I, offloadtest::RuntimeSettings &S);
};
template <> struct MappingTraits<offloadtest::SpecializationConstant> {
static void mapping(IO &I, offloadtest::SpecializationConstant &C);
};
template <> struct ScalarEnumerationTraits<offloadtest::Rule> {
static void enumeration(IO &I, offloadtest::Rule &V) {
#define ENUM_CASE(Val) I.enumCase(V, #Val, offloadtest::Rule::Val)
ENUM_CASE(BufferExact);
ENUM_CASE(BufferFloatULP);
ENUM_CASE(BufferFloatEpsilon);
#undef ENUM_CASE
}
};
template <> struct ScalarEnumerationTraits<offloadtest::DenormMode> {
static void enumeration(IO &I, offloadtest::DenormMode &V) {
#define ENUM_CASE(Val) I.enumCase(V, #Val, offloadtest::DenormMode::Val)
ENUM_CASE(Any);
ENUM_CASE(FTZ);
ENUM_CASE(Preserve);
#undef ENUM_CASE
}
};
template <> struct ScalarEnumerationTraits<offloadtest::FilterMode> {
static void enumeration(IO &I, offloadtest::FilterMode &V) {
#define ENUM_CASE(Val) I.enumCase(V, #Val, offloadtest::FilterMode::Val)
ENUM_CASE(Nearest);
ENUM_CASE(Linear);
#undef ENUM_CASE
}
};
template <> struct ScalarEnumerationTraits<offloadtest::AddressMode> {
static void enumeration(IO &I, offloadtest::AddressMode &V) {
#define ENUM_CASE(Val) I.enumCase(V, #Val, offloadtest::AddressMode::Val)
ENUM_CASE(Clamp);
ENUM_CASE(Repeat);
ENUM_CASE(Mirror);
ENUM_CASE(Border);
ENUM_CASE(MirrorOnce);
#undef ENUM_CASE
}
};
template <> struct ScalarEnumerationTraits<offloadtest::CompareFunction> {
static void enumeration(IO &I, offloadtest::CompareFunction &V) {
#define ENUM_CASE(Val) I.enumCase(V, #Val, offloadtest::CompareFunction::Val)
ENUM_CASE(Never);
ENUM_CASE(Less);
ENUM_CASE(Equal);
ENUM_CASE(LessEqual);
ENUM_CASE(Greater);
ENUM_CASE(NotEqual);
ENUM_CASE(GreaterEqual);
ENUM_CASE(Always);
#undef ENUM_CASE
}
};
template <> struct ScalarEnumerationTraits<offloadtest::SamplerKind> {
static void enumeration(IO &I, offloadtest::SamplerKind &V) {
#define ENUM_CASE(Val) I.enumCase(V, #Val, offloadtest::SamplerKind::Val)
ENUM_CASE(Sampler);
ENUM_CASE(SamplerComparison);
#undef ENUM_CASE
}
};
template <> struct ScalarEnumerationTraits<offloadtest::DataFormat> {
static void enumeration(IO &I, offloadtest::DataFormat &V) {
#define ENUM_CASE(Val) I.enumCase(V, #Val, offloadtest::DataFormat::Val)
ENUM_CASE(Hex8);
ENUM_CASE(Hex16);
ENUM_CASE(Hex32);
ENUM_CASE(Hex64);
ENUM_CASE(UInt16);
ENUM_CASE(UInt32);
ENUM_CASE(UInt64);
ENUM_CASE(Int16);
ENUM_CASE(Int32);
ENUM_CASE(Int64);
ENUM_CASE(Float16);
ENUM_CASE(Float32);
ENUM_CASE(Float64);
ENUM_CASE(Depth32);
ENUM_CASE(Bool);
#undef ENUM_CASE
}
};
template <> struct ScalarEnumerationTraits<offloadtest::ResourceKind> {
static void enumeration(IO &I, offloadtest::ResourceKind &V) {
#define ENUM_CASE(Val) I.enumCase(V, #Val, offloadtest::ResourceKind::Val)
ENUM_CASE(Buffer);
ENUM_CASE(StructuredBuffer);
ENUM_CASE(ByteAddressBuffer);
ENUM_CASE(Texture2D);
ENUM_CASE(RWBuffer);
ENUM_CASE(RWStructuredBuffer);
ENUM_CASE(RWByteAddressBuffer);
ENUM_CASE(RWTexture2D);
ENUM_CASE(ConstantBuffer);
ENUM_CASE(Sampler);
ENUM_CASE(SampledTexture2D);
#undef ENUM_CASE
}
};
template <> struct ScalarEnumerationTraits<offloadtest::Stages> {
static void enumeration(IO &I, offloadtest::Stages &V) {
#define ENUM_CASE(Val) I.enumCase(V, #Val, offloadtest::Stages::Val)
ENUM_CASE(Compute);
ENUM_CASE(Vertex);
ENUM_CASE(Pixel);
#undef ENUM_CASE
}
};
template <> struct ScalarEnumerationTraits<offloadtest::dx::RootParamKind> {
static void enumeration(IO &I, offloadtest::dx::RootParamKind &V) {
#define ENUM_CASE(Val) I.enumCase(V, #Val, offloadtest::dx::RootParamKind::Val)
ENUM_CASE(Constant);
ENUM_CASE(DescriptorTable);
ENUM_CASE(RootDescriptor);
#undef ENUM_CASE
}
};
template <typename T> struct SequenceTraits<SmallVector<SmallVector<T>>> {
static size_t size(IO &Io, SmallVector<SmallVector<T>> &Seq) {
return Seq.size();
}
static SmallVector<T> &element(IO &Io, SmallVector<SmallVector<T>> &Seq,
size_t Index) {
if (Index >= Seq.size())
Seq.resize(Index + 1);
return Seq[Index];
}
};
template <typename T> struct SequenceTraits<SmallVector<MutableArrayRef<T>>> {
static size_t size(IO &Io, SmallVector<MutableArrayRef<T>> &Seq) {
return Seq.size();
}
static MutableArrayRef<T> &
element(IO &Io, SmallVector<MutableArrayRef<T>> &Seq, size_t Index) {
assert(Index < Seq.size());
return Seq[Index];
}
};
} // namespace yaml
} // namespace llvm
#endif // OFFLOADTEST_SUPPORT_PIPELINE_H