Skip to content

fix the shaders, the initial matrix, the column-major draw uniforms and the runtime issues - #2

Open
igagis-bot wants to merge 7 commits into
mainfrom
shaders-initial-matrix-column-major
Open

igagis-bot wants to merge 7 commits into
mainfrom
shaders-initial-matrix-column-major

Conversation

@igagis-bot

@igagis-bot igagis-bot commented Nov 28, 2026 •

Copy link
Copy Markdown

Summary

Fixes for the DirectX 12 renderer that make the test app (ruisapp-directx-test) build, launch and render:

Shaders, vertex input, initial matrix, per-draw matrix upload

  • Shaders: per-draw uniforms are declared as a cbuffer (float4x4 draw_matrix + float4 draw_color) and multiplied with mul() (the old RootConstants style no longer compiles with the current SDK). Fragment shaders declare SV_POSITION as their first (unused) input so it matches the vertex shader's hardware-register layout (otherwise CreateGraphicsPipelineState fails with mismatched hardware registers). Compilation targets vs_5_1/ps_5_1 via a shared compile_hlsl_blob() helper that the pipeline keeps alive.
  • Vertex input: the a1 attribute's semantic is shader-specific (COLOR for pos_clr, TEXCOORD otherwise) and its buffer is read from offset 0 of the second vertex buffer; every buffer of the vertex array is bound (not just the first), which rendered only the right half of textured geometry otherwise. Culling is BACK (never NONE), and the pipeline is created with the real RTV/DSV formats (tracked in the pipeline key) instead of UNKNOWN.
  • Initial matrix: scale(1, -1, 1) -> translate(-1, -1) -> scale(2, 2), so the framework's normalized viewport (y down, origin top-left) maps to the DirectX NDC (was flipped vertically).
  • Column-major draw uniforms: the matrix is transposed before writing to the HLSL float4x4 cbuffer (r4 stores row-major).

Runtime fixes (needed to actually launch the app)

  • D3D12_MEMORY_POOL_UNKNOWN for non-custom heaps (the D3D12 debug layer reports D3D12_MEMORY_POOL_L0 as invalid).
  • D3D_ROOT_SIGNATURE_VERSION_1_0: the SDK 10.0.26100.0 serializer rejects version 1_1; the error blob is now included in the serialization error message.
  • Reset() on a freshly created command allocator/list is skipped (some runtimes reject it until the first work has completed) — frame_first_begin / upload_first_shot guards.
  • Empty vertex/index buffers do not create GPU resources and skip the index-buffer draw path.
  • check_hresult() prints the HRESULT as hex; D3D12_MAX_LOD (removed from the new SDK) is defined locally (2048.0f).
  • Buffer uploads use GENERIC_READ; the index buffer uses INDEX_BUFFER; the GPU readback buffer uses a READBACK heap.
  • Frame and upload fences are signaled via the D3D12 queue, not the fence object (which throws).
  • The swapchain is created from the queue (not the device), and the frame index comes from GetCurrentBackBufferIndex() instead of a manual increment.
  • Sampler: BORDER address mode, MaxAnisotropy = 0, MaxLOD = D3D12_FLOAT32_MAX.
  • Textures use PIXEL_SHADER_RESOURCE (no combined RENDER_TARGET | PIXEL_SHADER_RESOURCE state).

Notes

  • The app was verified launching and rendering (widget tree + gui::render logs) from out/dbg.
  • Untracked scratch/edge-case files were intentionally left out of the diff.

shaders
- declare the per-draw uniforms as a cbuffer (float4x4 draw_matrix + float4
  draw_color) and use mul() for the matrix multiply (the old RootConstants
  style no longer compiles with the current SDK)
- declare SV_POSITION as the first (unused) fragment-shader input so its
  hardware register matches the vertex shader's, which outputs SV_POSITION
  at register 0 and any arbitrary semantic at register 1 (otherwise pipeline
  creation fails with mismatched hardware registers)
- compile to vs_5_1/ps_5_1 and use a shared compile_hlsl_blob() helper so the
  pipeline can keep the blob alive for its lifetime
- fix the vertex input layout: a1 attribute semantic is shader-specific
  (COLOR for pos_clr, TEXCOORD otherwise) and the a1 buffer is read from
  offset 0 of the second vertex buffer (the buffers are bound per-slot, so the
  stride is not a byte offset into a single buffer)
- bind every vertex buffer of the vertex array (not just the first one),
  which rendered only the right half of textured geometry
- use D3D12_CULL_MODE_BACK (never NONE)
- create the pipeline with the real RTV/DSV formats and pass them in the
  pipeline key so the cache does not confuse pipelines for different formats

initial matrix
- scale(1, -1, 1) -> translate(-1, -1) -> scale(2, 2): the framework's
  normalized viewport (x right, y down, (0, 0) top-left) now maps to the
  DirectX NDC instead of coming out flipped vertically

column-major draw uniforms
- the renderer's per-draw matrix is stored row-major (r4), so transpose it
  before writing it into the HLSL cbuffer (float4x4 is column-major by
  default); everything currently drawn was mirrored
// compile the shaders to DXIL (the blobs are kept alive in shader_blobs)
auto* vs_blob = this->compile_shader(vertex_hlsl, "main", "vs_6_0");
auto* fs_blob = this->compile_shader(fragment_hlsl, "main", "ps_6_0");
auto* vs_blob = this->compile_shader(vertex_hlsl, "main", "vs_5_1");

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

make a comment here that vs_5_1 is chosen because not all drivers support 6_0?

- use D3D12_MEMORY_POOL_UNKNOWN for non-custom heaps (the D3D12 debug
  layer reports D3D12_MEMORY_POOL_L0 as invalid for them)
- use D3D_ROOT_SIGNATURE_VERSION_1_0: the SDK 10.0.26100.0 serializer
  rejects version 1_1; include the shader compiler error blob in the
  root signature serialization error message
- skip the Reset() call on a freshly created command allocator and
  command list: some runtimes reject Reset() on them until the first
  work has completed
- do not create GPU resources for empty vertex/index buffers and skip
  the index buffer draw path for them
- format the HRESULT in check_hresult() as a hex number
- use a local D3D12_MAX_LOD constant (removed from the new SDK)
- use GENERIC_READ for the buffer upload resources and INDEX_BUFFER
  for the index buffer resource
- use a READBACK heap for the GPU-visible readback buffer
- use the D3D12 queue to signal the frame and upload fences
- use the swapchain's current back buffer index and pass the swapchain
  queue (not the device) to CreateSwapChainForHwnd
- use a BORDER sampler address mode, MaxAnisotropy 0 and MaxLOD
  D3D12_FLOAT32_MAX
- use PIXEL_SHADER_RESOURCE for the texture barrier (no combined
  RENDER_TARGET | PIXEL_SHADER_RESOURCE state)
@igagis-bot igagis-bot changed the title fix the shaders, the initial matrix and the column-major draw uniforms fix the shaders, the initial matrix, the column-major draw uniforms and the runtime issues Nov 29, 2026
The DirectX renderer is Windows-only and ships no Debian package;
debian/ holds only a placeholder changelog (and a README explaining why)
so that the ci's myci-deb-version scripts can extract the version from
debian/changelog and apply it to the packages.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants