@@ -176,9 +176,9 @@ Decl *SemaHLSL::ActOnStartBuffer(Scope *BufferScope, bool CBuffer,
176
176
// https://learn.microsoft.com/en-us/windows/win32/direct3dhlsl/dx-graphics-hlsl-packing-rules
177
177
static unsigned calculateLegacyCbufferSize (const ASTContext &Context,
178
178
QualType T) {
179
- unsigned Size = 0 ;
180
179
constexpr unsigned CBufferAlign = 16 ;
181
180
if (const RecordType *RT = T->getAs <RecordType>()) {
181
+ unsigned Size = 0 ;
182
182
const RecordDecl *RD = RT->getDecl ();
183
183
for (const FieldDecl *Field : RD->fields ()) {
184
184
QualType Ty = Field->getType ();
@@ -191,22 +191,28 @@ static unsigned calculateLegacyCbufferSize(const ASTContext &Context,
191
191
Size = llvm::alignTo (Size, FieldAlign);
192
192
Size += FieldSize;
193
193
}
194
- } else if (const ConstantArrayType *AT = Context.getAsConstantArrayType (T)) {
195
- if (unsigned ElementCount = AT->getSize ().getZExtValue ()) {
196
- unsigned ElementSize =
197
- calculateLegacyCbufferSize (Context, AT->getElementType ());
198
- unsigned AlignedElementSize = llvm::alignTo (ElementSize, CBufferAlign);
199
- Size = AlignedElementSize * (ElementCount - 1 ) + ElementSize;
200
- }
201
- } else if (const VectorType *VT = T->getAs <VectorType>()) {
194
+ return Size;
195
+ }
196
+
197
+ if (const ConstantArrayType *AT = Context.getAsConstantArrayType (T)) {
198
+ unsigned ElementCount = AT->getSize ().getZExtValue ();
199
+ if (ElementCount == 0 )
200
+ return 0 ;
201
+
202
+ unsigned ElementSize =
203
+ calculateLegacyCbufferSize (Context, AT->getElementType ());
204
+ unsigned AlignedElementSize = llvm::alignTo (ElementSize, CBufferAlign);
205
+ return AlignedElementSize * (ElementCount - 1 ) + ElementSize;
206
+ }
207
+
208
+ if (const VectorType *VT = T->getAs <VectorType>()) {
202
209
unsigned ElementCount = VT->getNumElements ();
203
210
unsigned ElementSize =
204
211
calculateLegacyCbufferSize (Context, VT->getElementType ());
205
- Size = ElementSize * ElementCount;
206
- } else {
207
- Size = Context.getTypeSize (T) / 8 ;
212
+ return ElementSize * ElementCount;
208
213
}
209
- return Size;
214
+
215
+ return Context.getTypeSize (T) / 8 ;
210
216
}
211
217
212
218
// Validate packoffset:
0 commit comments