Single sources for shader constants, bindings and layouts #25

Open
UnflavoredMeson/Primal_Plus_Plus
opened 2026-11-24 20:15:55 +00:00 by UnflavoredMeson · 0 comments
Owner

Why

A constant defined once in C++ and once in HLSL drifts; a struct whose layout differs between C++ and the shader shows up as garbage on screen with no error. One source per constant and a build-time check make those impossible.

Decided

  • Shared constants in CommonConstants.hlsli as PRIMAL_SHARED_CONSTANT uint Name = ...;; C++ reads hlsl::Name through SharedTypes.h. No -D defines for them.
  • One engine shader table, Engine/Graphics/Shaders/EngineShaders.h: a new engine shader is an id and a row.
  • The binding contract (register to Vulkan set/binding, the -fvk-*-shift values) in Engine/Graphics/Shaders/ShaderBindings.h, documented for HLSL at the top of Common.hlsli. The bindless heap is at set 1 binding 2; heap casts carry the type (Texture2D<float4>(ResourceDescriptorHeap[i])).
  • Layout check: on every run the compiler compiles each struct of CommonTypes.hlsli to SPIR-V, reads the offsets and strides, and compares them with offsetof/sizeof; a difference fails the build.

Read first

  • UnflavoredMeson/Primal#17 (items 1-4).
  • Engine/Graphics/Direct3D12/Shaders/CommonTypes.hlsli and SharedTypes.h on master.

Steps

  • 1. Move the shared constants into CommonConstants.hlsli and delete the C++ copies.
  • 2. Write the engine shader table and use it on both sides.
  • 3. Write ShaderBindings.h and make the compiler pass its shifts.
  • 4. Write the layout check; break one struct on purpose and watch the build fail.

Done when

  • The layout check runs on every build and passes; a deliberate mismatch fails it; 0 warnings; tidy 0.

Claude can help with

  • Reading SPIR-V reflection output for the offsets.

Learn

  • HLSL constant buffer packing rules and why float3 followed by float is the classic trap.
## Why A constant defined once in C++ and once in HLSL drifts; a struct whose layout differs between C++ and the shader shows up as garbage on screen with no error. One source per constant and a build-time check make those impossible. ## Decided - Shared constants in `CommonConstants.hlsli` as `PRIMAL_SHARED_CONSTANT uint Name = ...;`; C++ reads `hlsl::Name` through `SharedTypes.h`. No `-D` defines for them. - One engine shader table, `Engine/Graphics/Shaders/EngineShaders.h`: a new engine shader is an id and a row. - The binding contract (register to Vulkan set/binding, the `-fvk-*-shift` values) in `Engine/Graphics/Shaders/ShaderBindings.h`, documented for HLSL at the top of `Common.hlsli`. The bindless heap is at set 1 binding 2; heap casts carry the type (`Texture2D<float4>(ResourceDescriptorHeap[i])`). - Layout check: on every run the compiler compiles each struct of `CommonTypes.hlsli` to SPIR-V, reads the offsets and strides, and compares them with `offsetof`/`sizeof`; a difference fails the build. ## Read first - `UnflavoredMeson/Primal#17` (items 1-4). - `Engine/Graphics/Direct3D12/Shaders/CommonTypes.hlsli` and `SharedTypes.h` on `master`. ## Steps - [ ] 1. Move the shared constants into `CommonConstants.hlsli` and delete the C++ copies. - [ ] 2. Write the engine shader table and use it on both sides. - [ ] 3. Write `ShaderBindings.h` and make the compiler pass its shifts. - [ ] 4. Write the layout check; break one struct on purpose and watch the build fail. ## Done when - [ ] The layout check runs on every build and passes; a deliberate mismatch fails it; 0 warnings; tidy 0. ## Claude can help with - Reading SPIR-V reflection output for the offsets. ## Learn - HLSL constant buffer packing rules and why `float3` followed by `float` is the classic trap.
Sign in to join this conversation.
No assignees
No project
No milestone
Blocks Depends on
#24 ShaderCompilerTool at build time
UnflavoredMeson/Primal_Plus_Plus
Notifications
1 participant
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
UnflavoredMeson/Primal_Plus_Plus#25
No description provided.