Fix #110255: Cover up CPU/GPU differences with small suns in light tree #110307

Merged
Weizhen Huang merged 5 commits from Alaska/blender:fix-small-sun-light-tree-sampling into main 2023-08-07 07:29:23 +02:00
Member

This pull request covers up a subtle difference between the CPU and GPU
when rendering with a light tree. Specifically a case where the user
has a sun light with a small angle.

The difference was caused by the dot() function being different between
CPU and GPU backends, with the GPU showing more meaningful
floating-point precision losses when working with small suns.

This pull request covers up a subtle difference between the CPU and GPU when rendering with a light tree. Specifically a case where the user has a sun light with a small angle. The difference was caused by the dot() function being different between CPU and GPU backends, with the GPU showing more meaningful floating-point precision losses when working with small suns.
Alaska added the
Module
Render & Cycles
label 2023-07-20 16:28:40 +02:00
Alaska requested review from Brecht Van Lommel 2023-07-20 16:29:06 +02:00
Alaska requested review from Weizhen Huang 2023-07-20 16:29:17 +02:00
Author
Member

This pull request is for the Main branch of Blender, but should be back ported to the blender-v3.6-release branch if accepted.

For reference, here's an example of the issue:
In light_tree_importance there is a section of code that is run if cos_theta >= cos_theta_u. For distant lights this statement is always true. But on the GPU backend under specific circumstances, there were floating point precision losses which resulted in it not being true, resulting in the distant light not being sampled.

This happens because when a sun light has a small angle (0 in this example), it has a high cos_theta_u (1 in this example). cos_theta_u = cos(sun_angle)
cos_theta is calculated as dot(bcone.axis, -point_to_centroid), and in the case of distant lights, bcone.axis = -point_to_centroid and both vectors are normalized, so cos_theta should be 1. But for some orientations of the sun lamp, cos_theta was not equal to 1 on the GPU. Instead cos_theta would be something like 0.99999994. This resulted in the if statement not being true, leading to the distant light having an importance of 0 and not being sampled.


It should also be noted that I went with a "simple" fix to the issue, but it could have subtle effects on lights in other situations. Other fixes could also be implemented if you wish.

Some examples of alternative fixes I can think of are:

  • if (bcone.axis == -point_to_centroid) then (cos_theta = 1) otherwise use cos_theta = dot(bcone.axis, -point_to_centroid). From a quick look into the code, there's the potential for a floating point precision issue resulting in bcone.axis != -point_to_centroid due to how they are calculated. But it should be fixed with #110374.
  • if distance light, then (cos_theta = 1)

Note: The floating point precision issues with the dot function aren't just a weird thing with the GPU. It's actually because the GPU and CPU run different code. Specifically the CPU takes advantage of SSE optimizations. If you swap the CPU over to using the same dot function as the GPU, then it will have the exact same issue.

This pull request is for the Main branch of Blender, but should be back ported to the blender-v3.6-release branch if accepted. For reference, here's an example of the issue: In `light_tree_importance` there is a section of code that is run if `cos_theta >= cos_theta_u`. For distant lights this statement is always true. But on the GPU backend under specific circumstances, there were floating point precision losses which resulted in it not being true, resulting in the distant light not being sampled. This happens because when a sun light has a small angle (0 in this example), it has a high `cos_theta_u` (1 in this example). `cos_theta_u = cos(sun_angle)` `cos_theta` is calculated as `dot(bcone.axis, -point_to_centroid)`, and in the case of distant lights, `bcone.axis = -point_to_centroid` and both vectors are normalized, so `cos_theta` should be `1`. But for some orientations of the sun lamp, `cos_theta` was not equal to `1` on the GPU. Instead `cos_theta` would be something like `0.99999994`. This resulted in the if statement not being true, leading to the distant light having an importance of 0 and not being sampled. --- It should also be noted that I went with a "simple" fix to the issue, but it could have subtle effects on lights in other situations. Other fixes could also be implemented if you wish. Some examples of alternative fixes I can think of are: - `if (bcone.axis == -point_to_centroid)` then `(cos_theta = 1)` otherwise use `cos_theta = dot(bcone.axis, -point_to_centroid)`. From a quick look into the code, there's the potential for a floating point precision issue resulting in `bcone.axis != -point_to_centroid` due to how they are calculated. But it should be fixed with #110374. - `if distance light`, then `(cos_theta = 1)` --- Note: The floating point precision issues with the dot function aren't just a weird thing with the GPU. It's actually because the GPU and CPU run different code. Specifically the CPU takes advantage of SSE optimizations. If you swap the CPU over to using the same dot function as the GPU, then it will have the exact same issue.
Alaska changed title from Fix #110255: Cover up CPU/GPU differences in light tree. to Fix #110255: Cover up CPU/GPU differences with small suns in light tree 2023-07-21 03:29:47 +02:00
Alaska force-pushed fix-small-sun-light-tree-sampling from 5010b1d7d5 to 8896301a7a 2023-07-22 14:24:23 +02:00 Compare
Author
Member

As pointed out earlier, the rendering error occurs on the GPU but not the CPU and this was due differences between the dot() function on the CPU and GPU. Specifically because the SSE optimisations used on the CPU produce a slightly different result due to floating point precision differences. If you remove the SSE code from the dot() function, relying on the "simple math" that the GPU uses, then the CPU will have the exact same issues as the GPU.

I tried to align the way the "simple math" version of dot() worked with the SSE operation. Specifically changing the order of operations and separating operations into different variables. And making those changes would fix the issue on the CPU with the SSE code removed, but wouldn't fix it on the GPU (I was testing the Metal backend), either due to some other issue or compiler optimisations. For reference, here are some examples of the different things I tried:

// Orignal
return a.x * a.x + a.y * b.y + a.z * b.z;
// Test 1 (Brackets to try and influence order of operations)
return a.x * a.x + (a.y * b.y + a.z * b.z);
// Test 2 (Change the order)
return  a.y * b.y + a.z * b.z + a.x * a.x;
// Test 3 (Separating variables)
float x = a.x * a.x;
float y = a.y * b.y;
float z = a.z * b.z;
return x + y + z;
// Test 4 (Adjust order of operations with separated variables)
float x = a.x * a.x;
float y = a.y * b.y;
float z = a.z * b.z;
float yz = y + z;
return yz + x;

As mentioned above, some tests did work to resolve the issue on the CPU with SSE code removed. But it didn't fix the issue on the GPU. Maybe there was a specific combination I missed?

As pointed out earlier, the rendering error occurs on the GPU but not the CPU and this was due differences between the `dot()` function on the CPU and GPU. Specifically because the SSE optimisations used on the CPU produce a slightly different result due to floating point precision differences. If you remove the SSE code from the `dot()` function, relying on the "simple math" that the GPU uses, then the CPU will have the exact same issues as the GPU. I tried to align the way the "simple math" version of `dot()` worked with the SSE operation. Specifically changing the order of operations and separating operations into different variables. And making those changes would fix the issue on the CPU with the SSE code removed, but wouldn't fix it on the GPU (I was testing the Metal backend), either due to some other issue or compiler optimisations. For reference, here are some examples of the different things I tried: ``` // Orignal return a.x * a.x + a.y * b.y + a.z * b.z; // Test 1 (Brackets to try and influence order of operations) return a.x * a.x + (a.y * b.y + a.z * b.z); // Test 2 (Change the order) return a.y * b.y + a.z * b.z + a.x * a.x; // Test 3 (Separating variables) float x = a.x * a.x; float y = a.y * b.y; float z = a.z * b.z; return x + y + z; // Test 4 (Adjust order of operations with separated variables) float x = a.x * a.x; float y = a.y * b.y; float z = a.z * b.z; float yz = y + z; return yz + x; ``` As mentioned above, some tests did work to resolve the issue on the CPU with SSE code removed. But it didn't fix the issue on the GPU. Maybe there was a specific combination I missed?
Weizhen Huang requested changes 2023-07-31 14:24:26 +02:00
Weizhen Huang left a comment
Member

Such value seems quite arbitrary, I would lean towards assigning 1 to cos_theta when isequal(bcone.axis, -point_to_centroid) and comment that dot product doesn't necessarily returns 1 in such cases due to precision issue.
I guess part of the precision is already lost when normalizing the vector, which we can't fully recover in dot().

Such value seems quite arbitrary, I would lean towards assigning 1 to `cos_theta` when `isequal(bcone.axis, -point_to_centroid)` and comment that dot product doesn't necessarily returns 1 in such cases due to precision issue. I guess part of the precision is already lost when normalizing the vector, which we can't fully recover in `dot()`.
@ -187,3 +187,2 @@
* precision losses in the dot() function used to calculate cos_theta. */
float cos_min_outgoing_angle;
if ((cos_theta >= cos_theta_u) || (cos_theta_minus_theta_u >= cos_theta_o)) {
/* theta - theta_o - theta_u <= 0 */
Member

Please keep this comment, thank you.

Please keep this comment, thank you.
Alaska force-pushed fix-small-sun-light-tree-sampling from 8896301a7a to 90e276682b 2023-07-31 15:24:09 +02:00 Compare
Weizhen Huang approved these changes 2023-08-04 20:33:29 +02:00
Weizhen Huang merged commit 52ed6a216f into main 2023-08-07 07:29:23 +02:00
Member

@Alaska / @weizhen , added this fix in 3.6 backporting list: #109399 :)
Feel free to remove the entry if commit includes breaking changes.

@Alaska / @weizhen , added this fix in 3.6 backporting list: #109399 :) Feel free to remove the entry if commit includes breaking changes.
Alaska deleted branch fix-small-sun-light-tree-sampling 2023-08-07 09:35:02 +02:00
Sign in to join this conversation.
No Label
Interest
Alembic
Interest
Animation & Rigging
Interest
Asset Browser
Interest
Asset Browser Project Overview
Interest
Audio
Interest
Automated Testing
Interest
Blender Asset Bundle
Interest
BlendFile
Interest
Collada
Interest
Compatibility
Interest
Compositing
Interest
Core
Interest
Cycles
Interest
Dependency Graph
Interest
Development Management
Interest
EEVEE
Interest
EEVEE & Viewport
Interest
Freestyle
Interest
Geometry Nodes
Interest
Grease Pencil
Interest
ID Management
Interest
Images & Movies
Interest
Import Export
Interest
Line Art
Interest
Masking
Interest
Metal
Interest
Modeling
Interest
Modifiers
Interest
Motion Tracking
Interest
Nodes & Physics
Interest
OpenGL
Interest
Overlay
Interest
Overrides
Interest
Performance
Interest
Physics
Interest
Pipeline, Assets & IO
Interest
Platforms, Builds & Tests
Interest
Python API
Interest
Render & Cycles
Interest
Render Pipeline
Interest
Sculpt, Paint & Texture
Interest
Text Editor
Interest
Translations
Interest
Triaging
Interest
Undo
Interest
USD
Interest
User Interface
Interest
UV Editing
Interest
VFX & Video
Interest
Video Sequencer
Interest
Virtual Reality
Interest
Vulkan
Interest
Wayland
Interest
Workbench
Interest: X11
Legacy
Blender 2.8 Project
Legacy
Milestone 1: Basic, Local Asset Browser
Legacy
OpenGL Error
Meta
Good First Issue
Meta
Papercut
Meta
Retrospective
Meta
Security
Module
Animation & Rigging
Module
Core
Module
Development Management
Module
EEVEE & Viewport
Module
Grease Pencil
Module
Modeling
Module
Nodes & Physics
Module
Pipeline, Assets & IO
Module
Platforms, Builds & Tests
Module
Python API
Module
Render & Cycles
Module
Sculpt, Paint & Texture
Module
Triaging
Module
User Interface
Module
VFX & Video
Platform
FreeBSD
Platform
Linux
Platform
macOS
Platform
Windows
Priority
High
Priority
Low
Priority
Normal
Priority
Unbreak Now!
Status
Archived
Status
Confirmed
Status
Duplicate
Status
Needs Info from Developers
Status
Needs Information from User
Status
Needs Triage
Status
Resolved
Type
Bug
Type
Design
Type
Known Issue
Type
Patch
Type
Report
Type
To Do
No Milestone
No project
No Assignees
3 Participants
Notifications
Due Date
The due date is invalid or out of range. Please use the format 'yyyy-mm-dd'.

No due date set.

Dependencies

No dependencies set.

Reference: blender/blender#110307
No description provided.