Skip to content

feat: add fast random float gen based on pcg#1158

Merged
alandtse merged 2 commits into
community-shaders:devfrom
sicsix:rand
Jun 18, 2025
Merged

feat: add fast random float gen based on pcg#1158
alandtse merged 2 commits into
community-shaders:devfrom
sicsix:rand

Conversation

@sicsix
Copy link
Copy Markdown
Collaborator

@sicsix sicsix commented Jun 17, 2025

  • adds random float generators in range [0-1
  • uses pcg as a base to produce random bits then shifts those bits to produce multiple output floats cheaply

Summary by CodeRabbit

  • New Features

    • Introduced new functions for generating random floating-point values, supporting single values as well as 2D, 3D, and 4D vectors.
  • Refactor

    • Updated the random number generation logic to allow the random state to persist and be updated across calls.

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented Jun 17, 2025

Walkthrough

The PCG random number generator function was refactored to update and persist its state externally via an inout parameter. New functions were introduced to generate 1D, 2D, 3D, and 4D floating-point random values from the PCG state, supporting both single-value and vector outputs, with optional access to the underlying random word.

Changes

File Change Summary
package/Shaders/Common/Random.hlsli Refactored pcg to use inout uint state; added f1, f2, f3, f4 functions for float/vector random output

Sequence Diagram(s)

sequenceDiagram
    participant Caller
    participant Random

    Caller->>Random: f1/f2/f3/f4(inout seed [, out word])
    Random->>Random: pcg(inout seed)
    Random-->>Caller: float/float2/float3/float4 (random values), [word]
Loading

Poem

A shuffle of bits, a dance of the seed,
Now PCG remembers each deed.
Floats in ones, twos, threes, and fours,
Randomness knocking at shader doors.
With every call, the numbers bloom—
A rabbit’s delight in a shader room!
🐇✨


📜 Recent review details

Configuration used: .coderabbit.yaml
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between f94bc88 and fcb0d0e.

📒 Files selected for processing (1)
  • package/Shaders/Common/Random.hlsli (2 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • package/Shaders/Common/Random.hlsli
⏰ Context from checks skipped due to timeout of 90000ms (3)
  • GitHub Check: Validate shader compilation (VR, .github/configs/shader-validation-vr.yaml)
  • GitHub Check: Build plugin and addons
  • GitHub Check: Validate shader compilation (Flatrim, .github/configs/shader-validation.yaml)

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Explain this complex logic.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai explain this code block.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and explain its main purpose.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🧹 Nitpick comments (2)
package/Shaders/Common/Random.hlsli (2)

170-175: Mask mantissa for the second component for consistency & clarity

bits1 is built without the mantissa mask:

uint bits1 = word >> 9 | 0x3F800000u;

While word >> 9 is only 23 bits wide, masking expresses the intent and protects against accidental changes:

-    uint bits1 = word >> 9 | 0x3F800000u;
+    uint bits1 = (word >> 9) & 0x007FFFFFu | 0x3F800000u;

Apply the same pattern wherever a rotated/shifted word is converted to mantissa bits.


155-160: Minor: mark helper word as uint only when needed

word is an out parameter, but you often need it only when the caller asks for it.
An alternative is to overload with a defaulted parameter:

float f1(inout uint seed, out uint word = uint(0));

This would avoid duplicating wrapper overloads. Not critical, just a size/readability consideration.

Also applies to: 168-176, 184-194, 202-214

📜 Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 111b074 and f94bc88.

📒 Files selected for processing (1)
  • package/Shaders/Common/Random.hlsli (2 hunks)
⏰ Context from checks skipped due to timeout of 90000ms (3)
  • GitHub Check: Build plugin and addons
  • GitHub Check: Validate shader compilation (Flatrim, .github/configs/shader-validation.yaml)
  • GitHub Check: Validate shader compilation (VR, .github/configs/shader-validation-vr.yaml)
🔇 Additional comments (1)
package/Shaders/Common/Random.hlsli (1)

105-111: pcg stateful variant looks correct

The math matches the canonical PCG-XSH-RR 32-bit step and the state is correctly updated via the inout parameter.
No issues noticed here.

Comment thread package/Shaders/Common/Random.hlsli Outdated
@github-actions
Copy link
Copy Markdown

✅ A pre-release build is available for this PR:
Download

@alandtse alandtse merged commit 33ab238 into community-shaders:dev Jun 18, 2025
12 checks passed
davo0411 pushed a commit to davo0411/skyrim-community-shaders that referenced this pull request Jun 21, 2025
alandtse pushed a commit to alandtse/open-shaders that referenced this pull request Jul 20, 2025
@coderabbitai coderabbitai Bot mentioned this pull request Aug 13, 2025
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.

3 participants