community-shaders/skyrim-community-shaders

Add compile-time validation for GPU buffer alignment

Open

#1,426 opened on Aug 23, 2025

View on GitHub
 (0 comments) (1 reaction) (1 assignee)C++ (134 forks)github user discovery
help wanted

Repository metrics

Stars
 (1,037 stars)
PR merge metrics
 (PR metrics pending)

Description

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

Contributor guide