Skip to content

Fixed forward function declarations not getting global resources added. - #163

Merged
ccpisheden merged 4 commits into
mainfrom
metal-forward-declaration-fix
Nov 27, 2026
Merged

ccpisheden merged 4 commits into
mainfrom
metal-forward-declaration-fix

Conversation

@ccpisheden

@ccpisheden ccpisheden commented Nov 26, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Forward declared function callbacks are used by the CORTAO shaders to allow the user to include the CORTAO headers, then implement the callbacks using the defines and resources from the header.

Metal does not have global resources; instead the resources must be passed in as function parameters (similar to e.g. HLSL vertex attributes). The Metal effect compiler automatically adds these parameters as needed, both to function definitions and to call sites. However, the compiler fails to do this for forward declared functions, causing the CORTAO shaders to fail to compile, as the unmodified forward declared callbacks don't match what the updated call sites expect.

This PR adds an extra step to the compilation after the parameters have been added. This step finds all forward declared functions (NT_FUNCTION_HEADER type nodes) that don't match their eventual function implementation (because the implementation got a new parameter). These forward declarations are updated to match their implementations. In addition, the forward declarations are moved to right after the global variable definitions so that they always occur before any usage.

This PR includes a test that verifies that all APIs correctly handle forward-declared functions both with and without various types of global resources.

Edge case for static initializers

  1. Forward declare a function that doesn't use any global resources.
  2. Define a global static constant and use the function to initialize its value.
  3. Define the implementation of the function.

Moving all forward declarations to after the globals would break this. Fortunately, static constant initializers cannot reference any global resources (as there are none available at compile time), so the fix only moves forward declarations that have globals added to them. There is a test covering this and verifying that this works as intended.

Linked issue (optional)

  • Fixes CORTAO compilation errors.

Testing

  • Full recompile of all shaders, no errors.
  • All CORTAO WIP shaders now compile correctly and produce the expected result.
  • The new tests run without any errors on any API.

AI assistance

  • Root cause (forward declarations not being updated) diagnosed by me.
  • Claude produced the initial fix.
  • Tweaks and improvements by me.

ccpisheden and others added 3 commits November 26, 2026 16:14
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…X12 and Metal.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@ccpisheden
ccpisheden merged commit 259e233 into main Nov 27, 2026
1 check passed
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