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
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
|
// slang-ir-layout.cpp
#include "slang-ir-layout.h"
#include "slang-ir-generics-lowering-context.h"
#include "slang-ir-insts.h"
// This file implements facilities for computing and caching layout
// information on IR types.
//
// Unlike the AST-level layout system, this code currently only
// handles the notion of "natural" layout for IR types, which is
// the layout they use when stored in general-purpose memory
// without additional constraints.
//
// In general, "natural" layout for all targets is assumed to follow
// the same basic rules:
//
// * Scalars are all naturally aligned and have the "obvious" size
//
// * Arrays are laid out by separating elements by their "stride" (size rounded up to alignment)
//
// * Vectors are laid out as arrays of elements
//
// * Matrices are laid out as arrays of rows
//
// * Structures are laid out by packing fields in order, placing each field on the "next"
// suitably aligned offset. The alignment of a structure is the maximum alignment of
// its fields.
//
// Right now this file implements a one-size-fits-all version of natural
// layout that might not be a perfect fit for all targets. In particular
// this code currently assumes:
//
// * The `bool` type is laid out as 4 bytes (equivalent to an `int`)
//
// * The size of a structure or array type is *not* rounded up to a multiple
// of its alignment. This means that fields may be laid out in
// the "tail padding" of previous fields in the same structure. This is
// correct behavior for VK/D3D, but does not match the behavior of typical
// C/C++ compilers.
//
// * All matrices are laid out in row-major order, regardless of any
// settings in user code.
//
// TODO: Addressing the above issues would require extending this file to somehow
// get target-specific layout information as an input. One option would be
// to attach information about "natural" layout on the target to the `IRModuleInst`
// as a decoration, similar to how an LLVM IR module stores a "layout string."
namespace Slang
{
static Result _calcArraySizeAndAlignment(
CompilerOptionSet& optionSet,
IRTypeLayoutRules* rules,
IRType* elementType,
IRInst* elementCountInst,
IRSizeAndAlignment* outSizeAndAlignment)
{
auto elementCountLit = as<IRIntLit>(elementCountInst);
if (!elementCountLit)
return SLANG_FAIL;
auto elementCount = elementCountLit->getValue();
if (elementCount == 0)
{
*outSizeAndAlignment = IRSizeAndAlignment(0, 1);
return SLANG_OK;
}
IRSizeAndAlignment elementTypeLayout;
SLANG_RETURN_ON_FAIL(getSizeAndAlignment(optionSet, rules, elementType, &elementTypeLayout));
elementTypeLayout = rules->alignCompositeElement(elementTypeLayout);
*outSizeAndAlignment = IRSizeAndAlignment(
elementTypeLayout.getStride() * (elementCount - 1) + elementTypeLayout.size,
elementTypeLayout.alignment);
return SLANG_OK;
}
IRIntegerValue getIntegerValueFromInst(IRInst* inst)
{
SLANG_ASSERT(inst->getOp() == kIROp_IntLit);
return as<IRIntLit>(inst)->value.intVal;
}
Result IRTypeLayoutRules::calcSizeAndAlignment(
CompilerOptionSet& optionSet,
IRType* type,
IRSizeAndAlignment* outSizeAndAlignment)
{
int kPointerSize = 8;
switch (optionSet.getTarget())
{
case CodeGenTarget::HostCPPSource:
case CodeGenTarget::HostHostCallable:
case CodeGenTarget::HostExecutable:
case CodeGenTarget::HostSharedLibrary:
kPointerSize = (int)sizeof(void*);
break;
}
switch (type->getOp())
{
#define CASE(TYPE, SIZE, ALIGNMENT) \
case kIROp_##TYPE##Type: \
*outSizeAndAlignment = IRSizeAndAlignment(SIZE, ALIGNMENT); \
return SLANG_OK /* end */
// Most base types are "naturally aligned" (meaning alignment and size are the same)
#define BASE(TYPE, SIZE) CASE(TYPE, SIZE, SIZE)
BASE(Int8, 1);
BASE(UInt8, 1);
BASE(Int16, 2);
BASE(UInt16, 2);
BASE(Half, 2);
BASE(Int, 4);
BASE(UInt, 4);
BASE(Float, 4);
BASE(Int64, 8);
BASE(UInt64, 8);
BASE(IntPtr, kPointerSize);
BASE(UIntPtr, kPointerSize);
BASE(Double, 8);
// We are currently handling `bool` following the HLSL
// precednet of storing it in 4 bytes.
//
BASE(Bool, 4);
// The Slang `void` type is treated as a zero-byte
// type, so that it does not influence layout at all.
//
CASE(Void, 0, 1);
#undef BASE
#undef CASE
case kIROp_StructType:
{
auto structType = cast<IRStructType>(type);
IRSizeAndAlignment structLayout;
IRIntegerValue offset = 0;
IRIntegerValue lastFieldAlignment = 0;
IRType* lastFieldType = NULL;
bool seenFinalUnsizedArrayField = false;
for (auto field : structType->getFields())
{
// If we failed to catch an unsized array earlier in the pipeline,
// this will pick it up before generating nonsense results for
// subsequent offsets
SLANG_ASSERT(!seenFinalUnsizedArrayField);
IRSizeAndAlignment fieldTypeLayout;
SLANG_RETURN_ON_FAIL(
getSizeAndAlignment(optionSet, this, field->getFieldType(), &fieldTypeLayout));
seenFinalUnsizedArrayField =
fieldTypeLayout.size == IRSizeAndAlignment::kIndeterminateSize;
if (auto offsetDecor =
field->getKey()->findDecoration<IRVkStructOffsetDecoration>())
{
offset = offsetDecor->getOffset()->getValue();
}
else
{
offset = adjustOffset(
offset,
fieldTypeLayout.size,
lastFieldType,
lastFieldAlignment);
}
structLayout.size = align(offset, fieldTypeLayout.alignment);
structLayout.alignment =
std::max(structLayout.alignment, fieldTypeLayout.alignment);
IRIntegerValue fieldOffset = structLayout.size;
if (auto module = type->getModule())
{
// If we are in a situation where attaching new
// decorations is possible, then we want to
// cache the field offset on the IR field
// instruction.
//
IRBuilder builder(module);
auto intType = builder.getIntType();
builder.addDecoration(
field,
kIROp_OffsetDecoration,
builder.getIntValue(intType, (IRIntegerValue)ruleName),
builder.getIntValue(intType, fieldOffset));
}
if (!seenFinalUnsizedArrayField)
structLayout.size += fieldTypeLayout.size;
offset = structLayout.size;
lastFieldType = field->getFieldType();
lastFieldAlignment = fieldTypeLayout.alignment;
}
*outSizeAndAlignment = alignCompositeElement(structLayout);
return SLANG_OK;
}
break;
case kIROp_ArrayType:
{
auto arrayType = cast<IRArrayType>(type);
return _calcArraySizeAndAlignment(
optionSet,
this,
arrayType->getElementType(),
arrayType->getElementCount(),
outSizeAndAlignment);
}
break;
case kIROp_AtomicType:
{
auto atomicType = cast<IRAtomicType>(type);
calcSizeAndAlignment(optionSet, atomicType->getElementType(), outSizeAndAlignment);
return SLANG_OK;
}
break;
case kIROp_UnsizedArrayType:
{
auto unsizedArrayType = cast<IRUnsizedArrayType>(type);
getSizeAndAlignment(
optionSet,
this,
unsizedArrayType->getElementType(),
outSizeAndAlignment);
outSizeAndAlignment->size = IRSizeAndAlignment::kIndeterminateSize;
return SLANG_OK;
}
break;
case kIROp_VectorType:
{
auto vecType = cast<IRVectorType>(type);
IRSizeAndAlignment elementTypeLayout;
getSizeAndAlignment(optionSet, this, vecType->getElementType(), &elementTypeLayout);
*outSizeAndAlignment = getVectorSizeAndAlignment(
elementTypeLayout,
getIntegerValueFromInst(vecType->getElementCount()));
return SLANG_OK;
}
break;
case kIROp_AnyValueType:
{
auto anyValType = cast<IRAnyValueType>(type);
outSizeAndAlignment->size = getIntVal(anyValType->getSize());
outSizeAndAlignment->alignment = 4;
*outSizeAndAlignment = alignCompositeElement(*outSizeAndAlignment);
return SLANG_OK;
}
break;
case kIROp_TupleType:
{
auto tupleType = cast<IRTupleType>(type);
IRSizeAndAlignment resultLayout;
IRIntegerValue lastFieldAlignment = 0;
IRType* lastFieldType = NULL;
for (UInt i = 0; i < tupleType->getOperandCount(); i++)
{
auto elementType = tupleType->getOperand(i);
IRSizeAndAlignment fieldTypeLayout;
SLANG_RETURN_ON_FAIL(
getSizeAndAlignment(optionSet, this, (IRType*)elementType, &fieldTypeLayout));
resultLayout.size = adjustOffset(
resultLayout.size,
fieldTypeLayout.size,
lastFieldType,
lastFieldAlignment);
resultLayout.size = align(resultLayout.size, fieldTypeLayout.alignment);
resultLayout.alignment =
std::max(resultLayout.alignment, fieldTypeLayout.alignment);
resultLayout.size += fieldTypeLayout.size;
lastFieldType = as<IRType>(elementType);
lastFieldAlignment = fieldTypeLayout.alignment;
}
*outSizeAndAlignment = alignCompositeElement(resultLayout);
return SLANG_OK;
}
break;
case kIROp_WitnessTableType:
case kIROp_WitnessTableIDType:
case kIROp_RTTIHandleType:
{
outSizeAndAlignment->size = kRTTIHandleSize;
outSizeAndAlignment->alignment = 4;
return SLANG_OK;
}
break;
case kIROp_InterfaceType:
{
auto interfaceType = cast<IRInterfaceType>(type);
auto size = SharedGenericsLoweringContext::getInterfaceAnyValueSize(
interfaceType,
interfaceType->sourceLoc);
size += kRTTIHeaderSize;
size = align(size, 4);
IRSizeAndAlignment resultLayout;
resultLayout.size = size;
resultLayout.alignment = 4;
*outSizeAndAlignment = alignCompositeElement(resultLayout);
return SLANG_OK;
}
break;
case kIROp_MatrixType:
{
auto matType = cast<IRMatrixType>(type);
IRBuilder builder(type->getModule());
if (getIntegerValueFromInst(matType->getLayout()) == SLANG_MATRIX_LAYOUT_COLUMN_MAJOR)
{
auto colVector =
builder.getVectorType(matType->getElementType(), matType->getRowCount());
return _calcArraySizeAndAlignment(
optionSet,
this,
colVector,
matType->getColumnCount(),
outSizeAndAlignment);
}
else
{
auto rowVector =
builder.getVectorType(matType->getElementType(), matType->getColumnCount());
return _calcArraySizeAndAlignment(
optionSet,
this,
rowVector,
matType->getRowCount(),
outSizeAndAlignment);
}
}
break;
case kIROp_OutParamType:
case kIROp_BorrowInOutParamType:
case kIROp_RefParamType:
case kIROp_BorrowInParamType:
case kIROp_RawPointerType:
case kIROp_PtrType:
case kIROp_NativePtrType:
case kIROp_ComPtrType:
case kIROp_NativeStringType:
case kIROp_RaytracingAccelerationStructureType:
case kIROp_FuncType:
{
*outSizeAndAlignment = IRSizeAndAlignment(kPointerSize, kPointerSize);
return SLANG_OK;
}
break;
case kIROp_ScalarBufferLayoutType:
case kIROp_CBufferLayoutType:
case kIROp_Std140BufferLayoutType:
case kIROp_Std430BufferLayoutType:
case kIROp_DefaultBufferLayoutType:
*outSizeAndAlignment = IRSizeAndAlignment(0, 4);
return SLANG_OK;
case kIROp_DescriptorHandleType:
{
IRBuilder builder(type);
builder.setInsertBefore(type);
auto uintType = builder.getUIntType();
auto uint2Type = builder.getVectorType(uintType, 2);
return getSizeAndAlignment(optionSet, this, uint2Type, outSizeAndAlignment);
}
case kIROp_AttributedType:
{
auto attributedType = cast<IRAttributedType>(type);
SLANG_ASSERT(attributedType->getAttr()->getOp() == kIROp_NoDiffAttr);
return getSizeAndAlignment(
optionSet,
this,
attributedType->getBaseType(),
outSizeAndAlignment);
}
case kIROp_EnumType:
{
auto enumType = cast<IREnumType>(type);
auto tagType = enumType->getTagType();
return calcSizeAndAlignment(optionSet, tagType, outSizeAndAlignment);
}
break;
default:
break;
}
if (as<IRResourceTypeBase>(type) || as<IRSamplerStateTypeBase>(type))
{
*outSizeAndAlignment = IRSizeAndAlignment(8, 8);
return SLANG_OK;
}
return SLANG_FAIL;
}
IRSizeAndAlignmentDecoration* findSizeAndAlignmentDecorationForLayout(
IRType* type,
IRTypeLayoutRuleName layoutName)
{
for (auto decorInst : type->getDecorations())
{
if (auto decor = as<IRSizeAndAlignmentDecoration>(decorInst))
{
if (decor->getLayoutName() == layoutName)
return decor;
}
}
return nullptr;
}
Result getSizeAndAlignment(
CompilerOptionSet& optionSet,
IRTypeLayoutRules* rules,
IRType* type,
IRSizeAndAlignment* outSizeAndAlignment)
{
if (auto decor = findSizeAndAlignmentDecorationForLayout(type, rules->ruleName))
{
*outSizeAndAlignment = IRSizeAndAlignment(decor->getSize(), (int)decor->getAlignment());
return SLANG_OK;
}
IRSizeAndAlignment sizeAndAlignment;
SLANG_RETURN_ON_FAIL(rules->calcSizeAndAlignment(optionSet, type, &sizeAndAlignment));
if (auto module = type->getModule())
{
IRBuilder builder(module);
auto intType = builder.getIntType();
auto int64Type = builder.getInt64Type();
builder.addDecoration(
type,
kIROp_SizeAndAlignmentDecoration,
builder.getIntValue(intType, (IRIntegerValue)rules->ruleName),
builder.getIntValue(int64Type, sizeAndAlignment.size),
builder.getIntValue(intType, sizeAndAlignment.alignment));
}
*outSizeAndAlignment = sizeAndAlignment;
return SLANG_OK;
}
IROffsetDecoration* findOffsetDecorationForLayout(
IRStructField* field,
IRTypeLayoutRuleName layoutName)
{
for (auto decorInst : field->getDecorations())
{
if (auto decor = as<IROffsetDecoration>(decorInst))
{
if (decor->getLayoutName() == layoutName)
return decor;
}
}
return nullptr;
}
Result getOffset(
CompilerOptionSet& optionSet,
IRTypeLayoutRules* rules,
IRStructField* field,
IRIntegerValue* outOffset)
{
if (auto decor = findOffsetDecorationForLayout(field, rules->ruleName))
{
*outOffset = decor->getOffset();
return SLANG_OK;
}
// Offsets are computed as part of layout out types,
// so we expect that layout of the "parent" type
// of the field should add an offset to it if
// possible.
auto structType = as<IRStructType>(field->getParent());
if (!structType)
return SLANG_FAIL;
IRSizeAndAlignment structTypeLayout;
SLANG_RETURN_ON_FAIL(getSizeAndAlignment(optionSet, rules, structType, &structTypeLayout));
if (auto decor = findOffsetDecorationForLayout(field, rules->ruleName))
{
*outOffset = decor->getOffset();
return SLANG_OK;
}
// If attempting to lay out the parent type didn't
// cause the field to get an offset, then we are
// in an unexpected case with no easy answer.
//
return SLANG_FAIL;
}
struct NaturalLayoutRules : IRTypeLayoutRules
{
NaturalLayoutRules() { ruleName = IRTypeLayoutRuleName::Natural; }
virtual IRIntegerValue adjustOffset(
IRIntegerValue offset,
IRIntegerValue elementSize,
IRType* lastFieldType,
IRIntegerValue lastFieldAlignment)
{
SLANG_UNUSED(elementSize);
SLANG_UNUSED(lastFieldType);
SLANG_UNUSED(lastFieldAlignment);
return offset;
}
virtual IRSizeAndAlignment alignCompositeElement(IRSizeAndAlignment elementSize)
{
return elementSize;
}
virtual IRSizeAndAlignment getVectorSizeAndAlignment(
IRSizeAndAlignment element,
IRIntegerValue count)
{
return IRSizeAndAlignment(element.size * count, element.alignment);
}
};
struct CLayoutRules : IRTypeLayoutRules
{
CLayoutRules() { ruleName = IRTypeLayoutRuleName::C; }
virtual Result calcSizeAndAlignment(
CompilerOptionSet& optionSet,
IRType* type,
IRSizeAndAlignment* outSizeAndAlignment)
{
if (type->getOp() == kIROp_BoolType)
{
*outSizeAndAlignment = IRSizeAndAlignment(1, 1);
return SLANG_OK;
}
return IRTypeLayoutRules::calcSizeAndAlignment(optionSet, type, outSizeAndAlignment);
}
virtual IRIntegerValue adjustOffset(
IRIntegerValue offset,
IRIntegerValue elementSize,
IRType* lastFieldType,
IRIntegerValue lastFieldAlignment)
{
SLANG_UNUSED(elementSize);
SLANG_UNUSED(lastFieldType);
return align(offset, (int)lastFieldAlignment);
}
virtual IRSizeAndAlignment alignCompositeElement(IRSizeAndAlignment elementSize)
{
IRSizeAndAlignment alignedSize = elementSize;
alignedSize.size = align(alignedSize.size, alignedSize.alignment);
return alignedSize;
}
virtual IRSizeAndAlignment getVectorSizeAndAlignment(
IRSizeAndAlignment element,
IRIntegerValue count)
{
return IRSizeAndAlignment(element.size * count, element.alignment);
}
};
struct ConstantBufferLayoutRules : IRTypeLayoutRules
{
ConstantBufferLayoutRules() { ruleName = IRTypeLayoutRuleName::D3DConstantBuffer; }
/// Next member only aligns to 16 if the next member is an array/matrix/struct
virtual IRSizeAndAlignment alignCompositeElement(IRSizeAndAlignment currentSize)
{
// Matrix/Array/Struct should be aligned on a new register
return IRSizeAndAlignment(currentSize.size, 16);
}
virtual IRIntegerValue adjustOffset(
IRIntegerValue offset,
IRIntegerValue elementSize,
IRType* lastFieldType,
IRIntegerValue lastFieldAlignment)
{
SLANG_UNUSED(lastFieldType);
SLANG_UNUSED(lastFieldAlignment);
// If the element would cross a 16-byte boundary, align to the next boundary
auto currentChunk = offset / 16;
auto endChunk = (offset + elementSize - 1) / 16;
if (currentChunk != endChunk)
{
return align(offset, 16);
}
return offset;
}
virtual IRSizeAndAlignment getVectorSizeAndAlignment(
IRSizeAndAlignment element,
IRIntegerValue count)
{
return IRSizeAndAlignment(element.size * count, element.alignment);
}
};
struct Std430LayoutRules : IRTypeLayoutRules
{
Std430LayoutRules() { ruleName = IRTypeLayoutRuleName::Std430; }
virtual IRIntegerValue adjustOffset(
IRIntegerValue offset,
IRIntegerValue elementSize,
IRType* lastFieldType,
IRIntegerValue lastFieldAlignment)
{
SLANG_UNUSED(elementSize);
if (as<IRMatrixType>(lastFieldType) || as<IRArrayTypeBase>(lastFieldType) ||
as<IRStructType>(lastFieldType))
{
return align(offset, (int)lastFieldAlignment);
}
return offset;
}
virtual IRSizeAndAlignment alignCompositeElement(IRSizeAndAlignment elementSize)
{
return elementSize;
}
virtual IRSizeAndAlignment getVectorSizeAndAlignment(
IRSizeAndAlignment element,
IRIntegerValue count)
{
IRIntegerValue countForAlignment = count;
if (count == 3)
countForAlignment = 4;
return IRSizeAndAlignment(
(int)(element.size * count),
(int)(element.size * countForAlignment));
}
};
struct Std140LayoutRules : IRTypeLayoutRules
{
Std140LayoutRules() { ruleName = IRTypeLayoutRuleName::Std140; }
virtual IRIntegerValue adjustOffset(
IRIntegerValue offset,
IRIntegerValue elementSize,
IRType* lastFieldType,
IRIntegerValue lastFieldAlignment)
{
SLANG_UNUSED(elementSize);
if (as<IRMatrixType>(lastFieldType) || as<IRArrayTypeBase>(lastFieldType) ||
as<IRStructType>(lastFieldType))
{
return align(offset, (int)lastFieldAlignment);
}
return offset;
}
virtual IRSizeAndAlignment alignCompositeElement(IRSizeAndAlignment elementSize)
{
elementSize.alignment = (int)align(elementSize.alignment, 16);
elementSize.size = align(elementSize.size, elementSize.alignment);
return elementSize;
}
virtual IRSizeAndAlignment getVectorSizeAndAlignment(
IRSizeAndAlignment element,
IRIntegerValue count)
{
IRIntegerValue alignmentCount = count;
if (count == 3)
alignmentCount = 4;
return IRSizeAndAlignment(
(int)(element.size * count),
(int)(element.size * alignmentCount));
}
};
Result getNaturalSizeAndAlignment(
CompilerOptionSet& optionSet,
IRType* type,
IRSizeAndAlignment* outSizeAndAlignment)
{
return getSizeAndAlignment(
optionSet,
IRTypeLayoutRules::getNatural(),
type,
outSizeAndAlignment);
}
Result getNaturalOffset(
CompilerOptionSet& optionSet,
IRStructField* field,
IRIntegerValue* outOffset)
{
return getOffset(optionSet, IRTypeLayoutRules::getNatural(), field, outOffset);
}
//////////////////////////
// Std430 Layout
//////////////////////////
Result getStd430SizeAndAlignment(
CompilerOptionSet& optionSet,
IRType* type,
IRSizeAndAlignment* outSizeAndAlignment)
{
return getSizeAndAlignment(
optionSet,
IRTypeLayoutRules::getStd430(),
type,
outSizeAndAlignment);
}
Result getStd430Offset(
CompilerOptionSet& optionSet,
IRStructField* field,
IRIntegerValue* outOffset)
{
return getOffset(optionSet, IRTypeLayoutRules::getStd430(), field, outOffset);
}
IRTypeLayoutRules* IRTypeLayoutRules::getStd430()
{
static Std430LayoutRules rules;
return &rules;
}
IRTypeLayoutRules* IRTypeLayoutRules::getStd140()
{
static Std140LayoutRules rules;
return &rules;
}
IRTypeLayoutRules* IRTypeLayoutRules::getNatural()
{
static NaturalLayoutRules rules;
return &rules;
}
IRTypeLayoutRules* IRTypeLayoutRules::getC()
{
static CLayoutRules rules;
return &rules;
}
IRTypeLayoutRules* IRTypeLayoutRules::getConstantBuffer()
{
static ConstantBufferLayoutRules rules;
return &rules;
}
IRTypeLayoutRules* IRTypeLayoutRules::get(IRTypeLayoutRuleName name)
{
switch (name)
{
case IRTypeLayoutRuleName::Std430:
return getStd430();
case IRTypeLayoutRuleName::Std140:
return getStd140();
case IRTypeLayoutRuleName::Natural:
case IRTypeLayoutRuleName::MetalParameterBlock:
return getNatural();
case IRTypeLayoutRuleName::C:
return getC();
case IRTypeLayoutRuleName::D3DConstantBuffer:
return getConstantBuffer();
default:
return nullptr;
}
}
} // namespace Slang
|