Skip to content

feat: always use worldspace lighting#1266

Merged
doodlum merged 10 commits into
devfrom
always-worldspace
Jul 20, 2025
Merged

feat: always use worldspace lighting#1266
doodlum merged 10 commits into
devfrom
always-worldspace

Conversation

@doodlum
Copy link
Copy Markdown
Collaborator

@doodlum doodlum commented Jul 15, 2025

Patches the game to only use worldspace lighting in BSLighting. This skips a lot of multiplication, checks and removes a lot of variables which should help reduce the total register usage and math used. As well as making the code simpler to understand.

Note that this breaks vanilla shaders completely. Using xbyak to hook the relevant code would be extremely messy.

Summary by CodeRabbit

  • Refactor
    • Updated internal logic to always use world space rendering instead of view space, improving consistency across different runtime modules. No changes to user-facing interfaces.

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented Jul 15, 2025

Caution

Review failed

The pull request is closed.

Walkthrough

The patch updates the memory patching logic in BSLightingShader::SetupGeometry to force the shader to use world space instead of view space. It replaces a previous single-byte patch that always updated the eye position with a new 4-byte instruction patch, varying the bytes and offset based on the runtime module.

Changes

File(s) Change Summary
src/Hooks.cpp Replaces single-byte eye position update patch with a 4-byte instruction patch to force world space usage in BSLightingShader::SetupGeometry, using module-specific instructions and offsets.

Sequence Diagram(s)

sequenceDiagram
    participant GameRuntime
    participant HooksPatch
    participant BSLightingShader

    GameRuntime->>HooksPatch: Initialize patch for SetupGeometry
    HooksPatch->>BSLightingShader: Write 4-byte instruction at module-specific offset
    BSLightingShader-->>GameRuntime: Always uses world space for rendering
Loading

Possibly related PRs

Suggested reviewers

  • alandtse

Poem

In the world of shaders, bytes align,
Old eye tricks gone, new worlds define.
Four bytes now patch the space,
Forcing world view, a rendering embrace.
Rabbits hop where registers play,
Code and pixels light the way! 🐇✨


📜 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 1474779 and d09020d.

📒 Files selected for processing (1)
  • src/Hooks.cpp (1 hunks)
✨ Finishing Touches
  • 📝 Generate Docstrings

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.

Automated formatting by clang-format, prettier, and other hooks.
See https://pre-commit.ci for details.
@github-actions
Copy link
Copy Markdown

github-actions Bot commented Jul 15, 2025

Using provided base ref: 2c9e3c4
Using base ref: 2c9e3c4
Base commit date: 2025-07-16T17:39:38+01:00 (Wednesday, July 16, 2025 05:39 PM)

Actionable Suggestions

  • Hair Specular: Action required; Needs version bump to 1-0-1

Comment thread src/Hooks.cpp Outdated
@doodlum doodlum mentioned this pull request Jul 16, 2025
4 tasks
@doodlum doodlum marked this pull request as ready for review July 20, 2025 00:23
@doodlum doodlum merged commit 54f456e into dev Jul 20, 2025
8 of 11 checks passed
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: 0

♻️ Duplicate comments (1)
src/Hooks.cpp (1)

1148-1150: Use optimized NOP instruction for better performance.

The SE module currently uses basic NOPs, but could benefit from using optimized multi-byte NOPs for better instruction cache performance.

Apply this diff to use optimized NOPs:

-			std::uint8_t patch[] = { 0x90, 0x90, 0x90, 0x90 };  // 4 NOPs
+			std::uint8_t patch[] = { 0x0F, 0x1F, 0x40, 0x00 };  // 4-byte optimized NOP
🧹 Nitpick comments (1)
src/Hooks.cpp (1)

1135-1137: Clarify the comment about what's being patched.

The comment mentions updateEyePosition but based on the PR objectives and AI summary, this patch is actually modifying render space usage to force world space instead of view space, not specifically the eye position update logic.

Consider updating the comment to be more accurate:

-		// Patch render space in BSLightingShader::SetupGeometry to always use world space
-		// The variable updateEyePosition is set to 1 when not skinned. By patching to be 0 it will always use world space
-		// We offset from the base address of the containing function to the start of the patch
+		// Patch render space in BSLightingShader::SetupGeometry to always use world space
+		// This patches the instruction that controls render space selection to force world space usage
+		// We offset from the base address of the containing function to the start of the patch
📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between 2c9e3c4 and 1474779.

⛔ Files ignored due to path filters (1)
  • package/Shaders/Lighting.hlsl is excluded by !**/*.hlsl
📒 Files selected for processing (1)
  • src/Hooks.cpp (1 hunks)
🧰 Additional context used
🧠 Learnings (2)
📓 Common learnings
Learnt from: alandtse
PR: doodlum/skyrim-community-shaders#0
File: :0-0
Timestamp: 2025-07-05T05:20:45.823Z
Learning: In the skyrim-community-shaders repository, file deletion error handling improvements that replace existence checks and try-catch blocks with std::filesystem::remove error-code-based approaches are considered bug fixes rather than refactoring, as they address inadequate error handling and misleading log messages.
Learnt from: alandtse
PR: doodlum/skyrim-community-shaders#0
File: :0-0
Timestamp: 2025-06-24T07:17:36.604Z
Learning: When reviewing PRs, always clarify the scope if there are multiple related features or dependencies. WeatherPicker was a separate PR that was already merged, while this PR focuses specifically on WetnessEffects climate preset system enhancements.
Learnt from: jiayev
PR: doodlum/skyrim-community-shaders#0
File: :0-0
Timestamp: 2025-07-18T15:21:03.641Z
Learning: In the skyrim-community-shaders rendering pipeline, materials with alpha < 1 or alpha blending enabled are rendered in non-deferred mode rather than deferred mode. This means issues with dynamic cubemaps on transparent materials are actually non-deferred rendering issues.
Learnt from: alandtse
PR: doodlum/skyrim-community-shaders#577
File: src/Features/WetnessEffects.h:36-36
Timestamp: 2025-06-08T11:25:14.536Z
Learning: In the skyrim-community-shaders project, boolean flags in C++ structs that interface with HLSL shaders use `uint` type instead of `bool` for compatibility reasons. This ensures consistent 4-byte size, proper 16-byte alignment in constant buffers, and cross-platform compatibility when passing data between C++ and HLSL shaders.
Learnt from: alandtse
PR: doodlum/skyrim-community-shaders#577
File: src/Features/WetnessEffects.h:36-36
Timestamp: 2025-06-08T11:25:14.536Z
Learning: In the skyrim-community-shaders project, boolean flags in C++ structs that interface with HLSL shaders use `uint` type instead of `bool` for compatibility reasons. This ensures consistent size, alignment, and cross-platform compatibility when passing data to shader constant buffers.
Learnt from: alandtse
PR: doodlum/skyrim-community-shaders#577
File: features/Wetness Effects/Shaders/WetnessEffects/WetnessEffects.hlsli:57-61
Timestamp: 2025-06-17T05:40:22.785Z
Learning: Default parameter values are supported in the HLSL compiler used by the skyrim-community-shaders project, contrary to standard HLSL (FXC/DXC) limitations.
src/Hooks.cpp (3)
Learnt from: alandtse
PR: doodlum/skyrim-community-shaders#0
File: :0-0
Timestamp: 2025-07-05T05:20:45.823Z
Learning: In the skyrim-community-shaders repository, file deletion error handling improvements that replace existence checks and try-catch blocks with std::filesystem::remove error-code-based approaches are considered bug fixes rather than refactoring, as they address inadequate error handling and misleading log messages.
Learnt from: alandtse
PR: doodlum/skyrim-community-shaders#577
File: src/Features/WetnessEffects.h:36-36
Timestamp: 2025-06-08T11:25:14.536Z
Learning: In the skyrim-community-shaders project, boolean flags in C++ structs that interface with HLSL shaders use `uint` type instead of `bool` for compatibility reasons. This ensures consistent 4-byte size, proper 16-byte alignment in constant buffers, and cross-platform compatibility when passing data between C++ and HLSL shaders.
Learnt from: alandtse
PR: doodlum/skyrim-community-shaders#577
File: src/Features/WetnessEffects.h:36-36
Timestamp: 2025-06-08T11:25:14.536Z
Learning: In the skyrim-community-shaders project, boolean flags in C++ structs that interface with HLSL shaders use `uint` type instead of `bool` for compatibility reasons. This ensures consistent size, alignment, and cross-platform compatibility when passing data to shader constant buffers.
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (3)
  • GitHub Check: Validate shader compilation (Flatrim, .github/configs/shader-validation.yaml)
  • GitHub Check: Validate shader compilation (VR, .github/configs/shader-validation-vr.yaml)
  • GitHub Check: Build plugin and addons
🔇 Additional comments (1)
src/Hooks.cpp (1)

1138-1151: Verification complete – memory patch addresses and byte sequences are correct

  • src/Hooks.cpp (Lines 1138–1151):
    • REL::RelocationID(100565, 107300).address() + REL::Relocate(0x76, 0x71, 0x65) and the four-byte patches (0x41,0x83,0xE7,0x00 / 0x41,0x83,0xE4,0x00 / 0x90,0x90,0x90,0x90) match the patterns and usage of REL::safe_write found elsewhere (InteriorSunShadows, VR, VolumetricLighting).

No inconsistencies detected—approving these changes.

alandtse pushed a commit to alandtse/open-shaders that referenced this pull request Jul 20, 2025
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
@Pentalimbed Pentalimbed deleted the always-worldspace branch July 30, 2025 15:50
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