Fix integer overflow in custom shader Time calculation#20360
Open
Zish19 wants to merge 1 commit into
Open
Conversation
Author
|
@microsoft-github-policy-service agree |
DHowett
requested changes
Jun 24, 2026
DHowett
left a comment
Member
There was a problem hiding this comment.
Thanks for the fix! I am not convinced that we should move to double precision for the math, because now is only ever going to be a whole number, and _customShaderSecsPerPerfTick can truly represent all seconds per tick with a float. I am also not convinced that this is the cause of the reporter's delay; rather, it should only cause stuttering around the overflow points in shaders that use the Time component.
So I guess I'd say... can we just use uint64_t for the math and be done with it?
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary of the Pull Request
Fix an integer overflow in custom shader
Timeuniform calculation inBackendD3D.This PR removes an unnecessary 32-bit integer truncation in shader time computation and preserves the original modulo-based architecture while improving precision in the tick-to-seconds conversion.
References and Relevant Issues
Related to #20226
This PR addresses a confirmed overflow bug in custom shader
Timecomputation, although it has not been confirmed as the sole cause of the CPU spike / laggy input reported in the issue.Detailed Description of the Pull Request / Additional comments
The current implementation computes shader time using:
Here,
nowis a 64-bit performance counter value.At a typical QPC frequency of ~10 MHz:
_customShaderPerfTickMod = freq * 1000 = 10,000,000,000INT_MAX = 2,147,483,647As a result,
now % _customShaderPerfTickModcan exceedINT_MAX.Casting that 64-bit modulo result to a signed 32-bit integer causes overflow, producing corrupted (including negative)
Timevalues passed to custom shaders.This PR fixes the issue by:
static_cast<int>_customShaderSecsPerPerfTickfromf32todoubleto preserve maximum precision before the final boundedfloatcast for the shader constant bufferThis preserves the original design:
Validation Steps Performed
Investigated the rendering pipeline for custom shader execution in
BackendD3DVerified
_customShaderPerfTickModcan exceedINT_MAXon typical systemsConfirmed overflow mathematically (~214 seconds at 10 MHz QPC)
Verified only the following files were modified:
src/renderer/atlas/BackendD3D.cppsrc/renderer/atlas/BackendD3D.hConfirmed no unrelated code paths or dependencies were affected
Performed static code validation and diff review to ensure semantic correctness
Note: Full local build validation via
tools/bcz.cmdcould not be completed in my environment due to missing Visual Studio/MSBuild toolchain setup.PR Checklist