Skip to content

Add compile-time validation for GPU buffer alignmentย #1426

@coderabbitai

Description

@coderabbitai

Problem

GPU constant buffers (cbuffer structures) in HLSL must be aligned to 16-byte boundaries for optimal performance and correctness. Currently, the project relies on manual padding (e.g., uint b0pad0[2] in ClusterBuildingCS.hlsl) to ensure proper alignment, but this approach is error-prone and lacks compile-time validation.

Current State

The codebase contains various cbuffer structures across shader files that use manual padding:

cbuffer PerFrame : register(b0)
{
    float LightsNear;
    float LightsFar;
    uint b0pad0[2];  // Manual padding for alignment
}

Some settings may be combined from multiple features into a single cbuffer so misalignment may break later feature settings.

Goal

Implement a system that provides compile-time validation to ensure all GPU buffers maintain proper 16-byte alignment without relying solely on manual padding techniques.

Requirements

  • Detect misaligned buffer structures at compile time
  • Work with existing HLSL cbuffer declarations
  • Minimize impact on current shader code
  • Provide clear error messages when alignment issues are detected
  • Consider compatibility with DirectX shader compilation pipeline
  • Should be automatic or otherwise easy for developers to add

Context

This issue stems from PR #542 which attempted to add static asserts for buffer alignment. However, the solution should not be limited to static asserts and should explore various approaches to solve the compile-time alignment validation problem.

Related

/cc @alandtse

Metadata

Metadata

Assignees

Labels

help wantedExtra attention is needed

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions