Skip to content

Commit cb52e92

Browse files
Copilotalandtse
andauthored
build: add compile-time validation of GPU buffers (#1427)
Co-authored-by: copilot-swe-agent[bot] <[email protected]> Co-authored-by: alandtse <[email protected]>
1 parent c301939 commit cb52e92

File tree

15 files changed

+52
-7
lines changed

15 files changed

+52
-7
lines changed

features/Light Limit Fix/Shaders/LightLimitFix/ClusterBuildingCS.hlsl

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@
44

55
cbuffer PerFrame : register(b0)
66
{
7-
float LightsNear;
8-
float LightsFar;
9-
uint2 pad0;
10-
uint4 ClusterSize;
7+
float LightsNear;
8+
float LightsFar;
9+
uint2 pad0; // Padding for 16-byte alignment: 8 -> 16 bytes
10+
uint4 ClusterSize;
1111
}
1212

1313
float3 GetPositionVS(float2 texcoord, float depth, int eyeIndex = 0)

features/Light Limit Fix/Shaders/LightLimitFix/ClusterCullingCS.hlsl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33

44
cbuffer PerFrame : register(b0)
55
{
6-
uint LightCount;
7-
uint3 pad;
8-
uint4 ClusterSize;
6+
uint LightCount;
7+
uint3 pad0; // Padding for 16-byte alignment: 4 -> 16 bytes
8+
uint4 ClusterSize;
99
}
1010

1111
//references

src/Buffer.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@
88
#include <wrl\client.h>
99
#include <wrl\wrappers\corewrappers.h>
1010

11+
#define STATIC_ASSERT_ALIGNAS_16(structName) \
12+
static_assert(sizeof(structName) % 16 == 0, #structName " is not a multiple of 16.");
13+
1114
template <typename T>
1215
D3D11_BUFFER_DESC StructuredBufferDesc(uint64_t count, bool uav = true, bool dynamic = false)
1316
{

src/Deferred.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
#pragma once
22

3+
#include "Buffer.h"
4+
35
#define ALBEDO RE::RENDER_TARGETS::kINDIRECT
46
#define SPECULAR RE::RENDER_TARGETS::kINDIRECT_DOWNSCALED
57
#define REFLECTANCE RE::RENDER_TARGETS::kRAWINDIRECT
@@ -62,6 +64,7 @@ class Deferred
6264
DirectX::XMFLOAT4X3 ShadowMapProj[2][3];
6365
DirectX::XMFLOAT4X3 CameraViewProjInverse[2];
6466
};
67+
STATIC_ASSERT_ALIGNAS_16(PerGeometry);
6568

6669
ID3D11ComputeShader* copyShadowCS = nullptr;
6770
Buffer* perShadow = nullptr;

src/Features/DynamicCubemaps.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
#pragma once
22

3+
#include "Buffer.h"
4+
35
class MenuOpenCloseEventHandler : public RE::BSTEventSink<RE::MenuOpenCloseEvent>
46
{
57
public:
@@ -21,6 +23,7 @@ struct DynamicCubemaps : Feature
2123
float roughness;
2224
float pad[3];
2325
};
26+
STATIC_ASSERT_ALIGNAS_16(SpecularMapFilterSettingsCB);
2427

2528
ID3D11ComputeShader* specularIrradianceCS = nullptr;
2629
ConstantBuffer* spmapCB = nullptr;
@@ -36,6 +39,7 @@ struct DynamicCubemaps : Feature
3639
float3 CameraPreviousPosAdjust;
3740
uint pad0;
3841
};
42+
STATIC_ASSERT_ALIGNAS_16(UpdateCubemapCB);
3943

4044
ID3D11ComputeShader* updateCubemapCS = nullptr;
4145
ID3D11ComputeShader* updateCubemapReflectionsCS = nullptr;

src/Features/ExtendedMaterials.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
#pragma once
22

3+
#include "Buffer.h"
4+
35
struct ExtendedMaterials : Feature
46
{
57
virtual inline std::string GetName() override { return "Extended Materials"; }
@@ -36,6 +38,7 @@ struct ExtendedMaterials : Feature
3638

3739
float pad[1];
3840
};
41+
STATIC_ASSERT_ALIGNAS_16(Settings);
3942

4043
Settings settings;
4144

src/Features/GrassCollision.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
#pragma once
22

3+
#include "Buffer.h"
4+
35
struct GrassCollision : Feature
46
{
57
private:
@@ -43,6 +45,7 @@ struct GrassCollision : Feature
4345
uint IndexEnd = 0;
4446
float2 pad0;
4547
};
48+
STATIC_ASSERT_ALIGNAS_16(BoundingBoxPacked);
4649

4750
struct alignas(16) PerFrame
4851
{
@@ -56,6 +59,7 @@ struct GrassCollision : Feature
5659
float CameraHeightDelta;
5760
float3 pad0;
5861
};
62+
STATIC_ASSERT_ALIGNAS_16(PerFrame);
5963

6064
Settings settings;
6165

src/Features/GrassLighting.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
#pragma once
22

3+
#include "Buffer.h"
4+
35
struct GrassLighting : Feature
46
{
57
private:
@@ -35,6 +37,7 @@ struct GrassLighting : Feature
3537
float BasicGrassBrightness = 1.0f;
3638
uint pad[3];
3739
};
40+
STATIC_ASSERT_ALIGNAS_16(Settings);
3841

3942
Settings settings;
4043

src/Features/LightLimitFix.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
#pragma once
22

3+
#include "Buffer.h"
4+
35
struct LightLimitFix : Feature
46
{
57
private:
@@ -59,6 +61,7 @@ struct LightLimitFix : Feature
5961
uint pad0;
6062
uint pad1;
6163
};
64+
STATIC_ASSERT_ALIGNAS_16(LightData);
6265

6366
struct ClusterAABB
6467
{
@@ -72,6 +75,7 @@ struct LightLimitFix : Feature
7275
uint lightCount;
7376
uint pad0[2];
7477
};
78+
STATIC_ASSERT_ALIGNAS_16(LightGrid);
7579

7680
struct alignas(16) LightBuildingCB
7781
{
@@ -80,13 +84,15 @@ struct LightLimitFix : Feature
8084
uint pad0[2];
8185
uint ClusterSize[4];
8286
};
87+
STATIC_ASSERT_ALIGNAS_16(LightBuildingCB);
8388

8489
struct alignas(16) LightCullingCB
8590
{
8691
uint LightCount;
8792
uint pad[3];
8893
uint ClusterSize[4];
8994
};
95+
STATIC_ASSERT_ALIGNAS_16(LightCullingCB);
9096

9197
struct alignas(16) PerFrame
9298
{
@@ -95,6 +101,7 @@ struct LightLimitFix : Feature
95101
float pad0[2];
96102
uint ClusterSize[4];
97103
};
104+
STATIC_ASSERT_ALIGNAS_16(PerFrame);
98105

99106
PerFrame GetCommonBufferData();
100107

@@ -106,6 +113,7 @@ struct LightLimitFix : Feature
106113
uint pad0;
107114
LightData StrictLights[15];
108115
};
116+
STATIC_ASSERT_ALIGNAS_16(StrictLightDataCB);
109117

110118
StrictLightDataCB strictLightDataTemp;
111119

src/Features/ScreenSpaceGI.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
#pragma once
22

3+
#include "Buffer.h"
4+
35
struct ScreenSpaceGI : Feature
46
{
57
private:
@@ -126,6 +128,7 @@ struct ScreenSpaceGI : Feature
126128

127129
float2 pad;
128130
};
131+
STATIC_ASSERT_ALIGNAS_16(SSGICB);
129132
eastl::unique_ptr<ConstantBuffer> ssgiCB;
130133

131134
eastl::unique_ptr<Texture2D> texNoise = nullptr;

0 commit comments

Comments
 (0)