Realtime Compositor: Allow more result types #112414

Merged
Omar Emara merged 5 commits from OmarEmaraDev/blender:allow-more-result-types into main 2023-09-25 15:13:02 +02:00
Member

Previously, the Result class was reserved for inputs and outputs of
operations, so its allowed types were naturally those exposed to the
user. However, we now use the Result class internally for intermediate
results, so it now makes sense to expend the allowed types.

The types are now divided into two categories, those that are user
facing and need to be handled in implicit operations and those that are
internal can be exempt from such handling. Internal types are reserved
for texture results, as the single value mechanism is only there for
user facing results.

The patch merely adjusts the switch cases across the code base, adding
one new internal type as an example.

Previously, the Result class was reserved for inputs and outputs of operations, so its allowed types were naturally those exposed to the user. However, we now use the Result class internally for intermediate results, so it now makes sense to expend the allowed types. The types are now divided into two categories, those that are user facing and need to be handled in implicit operations and those that are internal can be exempt from such handling. Internal types are reserved for texture results, as the single value mechanism is only there for user facing results. The patch merely adjusts the switch cases across the code base, adding one new internal type as an example.
Omar Emara added the
Interest
Compositing
Module
VFX & Video
Interest
VFX & Video
labels 2023-09-15 13:32:21 +02:00
Omar Emara added 1 commit 2023-09-15 13:32:34 +02:00
cee265ee8a Realtime Compositor: Allow more result types
Previously, the Result class was reserved for inputs and outputs of
operations, so its allowed types were naturally those exposed to the
user. However, we now use the Result class internally for intermediate
results, so it now makes sense to expend the allowed types.

The types are now divided into two categories, those that are user
facing and need to be handled in implicit operations and those that are
internal can be exempt from such handling. Internal types are reserved
for texture results, as the single value mechanism is only there for
user facing results.

The patch merely adjusts the switch cases across the codebase, adding
one new internal type as an example.
Omar Emara requested review from Sergey Sharybin 2023-09-15 13:32:45 +02:00
Sergey Sharybin reviewed 2023-09-18 21:48:35 +02:00
Sergey Sharybin left a comment
Owner

Generally looks fine.

Have a little comment about use of default which I try to avoid in own code. Use some common sense though, it might very well be that the places where it is used is not genetic enough (i.e. limited by the socket type of a specific node -- aka, purely user-facing things).

Do not consider it as a stopper and if you believe the code as it is in the current PR is better that trying to do things explicitly please just go ahead and commit :)

Generally looks fine. Have a little comment about use of `default` which I try to avoid in own code. Use some common sense though, it might very well be that the places where it is used is not genetic enough (i.e. limited by the socket type of a specific node -- aka, purely user-facing things). Do not consider it as a stopper and if you believe the code as it is in the current PR is better that trying to do things explicitly please just go ahead and commit :)
@ -42,6 +44,9 @@ static Result detect_edges(Context &context,
GPU_shader_uniform_3fv(shader, "luminance_coefficients", luminance_coefficients);
break;
}
default:

Typically in such situations I try to avoid default as it helps a lot when new types are added and do an assert if none of the explicit cases matched. It might be easier to do if this piece of code is moved to own small function like set_shader_luminance_coefficients so that early return in place of break are possible.

The reason for this is that if a new type is added in the future it is easier to see where its handling might be needed by utilizing compiler's warnings.

Typically in such situations I try to avoid `default` as it helps a lot when new types are added and do an assert if none of the explicit cases matched. It might be easier to do if this piece of code is moved to own small function like `set_shader_luminance_coefficients` so that early return in place of `break` are possible. The reason for this is that if a new type is added in the future it is easier to see where its handling might be needed by utilizing compiler's warnings.
Author
Member

But if we don't have a default, we will always get such warnings:

warning: enumeration value 'Int2' not handled in switch [-Wswitch]

This is the code I have:

void set_shader_luminance_coefficients(GPUShader *shader, ResultType type)
{
  switch (type) {
    case ResultType::Color: {
      float luminance_coefficients[3];
      IMB_colormanagement_get_luminance_coefficients(luminance_coefficients);
      GPU_shader_uniform_3fv(shader, "luminance_coefficients", luminance_coefficients);
      return;
    }
    case ResultType::Vector: {
      float luminance_coefficients[3] = {1.0f, 1.0f, 1.0f};
      GPU_shader_uniform_3fv(shader, "luminance_coefficients", luminance_coefficients);
      return;
    }
    case ResultType::Float: {
      float luminance_coefficients[3] = {1.0f, 0.0f, 0.0f};
      GPU_shader_uniform_3fv(shader, "luminance_coefficients", luminance_coefficients);
      return;
    }
  }

  BLI_assert_unreachable();
}

Or should we add empty assert casses for Int2 and other similar types with a comment that says why they aren't handled?

But if we don't have a default, we will always get such warnings: ``` warning: enumeration value 'Int2' not handled in switch [-Wswitch] ``` This is the code I have: ```cpp void set_shader_luminance_coefficients(GPUShader *shader, ResultType type) { switch (type) { case ResultType::Color: { float luminance_coefficients[3]; IMB_colormanagement_get_luminance_coefficients(luminance_coefficients); GPU_shader_uniform_3fv(shader, "luminance_coefficients", luminance_coefficients); return; } case ResultType::Vector: { float luminance_coefficients[3] = {1.0f, 1.0f, 1.0f}; GPU_shader_uniform_3fv(shader, "luminance_coefficients", luminance_coefficients); return; } case ResultType::Float: { float luminance_coefficients[3] = {1.0f, 0.0f, 0.0f}; GPU_shader_uniform_3fv(shader, "luminance_coefficients", luminance_coefficients); return; } } BLI_assert_unreachable(); } ``` Or should we add empty assert casses for `Int2` and other similar types with a comment that says why they aren't handled?
  switch (...) {
    ...
    case ResultType::Int2:
      /* Types which are not supposed to be passed into this node under normal operations. */
      break;
  }
  BLI_assert_unreachable();

But, again, it was more of a generic comment. To keep it practical we can say: if the items to be handled in the switch depends on the node configuration then the current default approach is cleaner and there is no need to make things more verbose than they should be. However, if the addition of a new data type requires update of this switch statement then its better not to have default branch. Hope it makes sense :)

``` switch (...) { ... case ResultType::Int2: /* Types which are not supposed to be passed into this node under normal operations. */ break; } BLI_assert_unreachable(); ``` But, again, it was more of a generic comment. To keep it practical we can say: if the items to be handled in the switch depends on the node configuration then the current `default` approach is cleaner and there is no need to make things more verbose than they should be. However, if the addition of a new data type requires update of this `switch` statement then its better not to have `default` branch. Hope it makes sense :)
OmarEmaraDev marked this conversation as resolved
@ -26,0 +28,4 @@
case ResultType::Vector:
case ResultType::Color:
return "compositor_symmetric_separable_blur_color";
default:

Move the content outside of the switch.

Move the content outside of the `switch`.
OmarEmaraDev marked this conversation as resolved
Omar Emara added 4 commits 2023-09-25 13:53:13 +02:00
Sergey Sharybin approved these changes 2023-09-25 14:48:49 +02:00
Sergey Sharybin left a comment
Owner

Thanks for the update! Makes more sense now. Also nice to see explicit comments about unexpected code paths in certain case branches!

Thanks for the update! Makes more sense now. Also nice to see explicit comments about unexpected code paths in certain `case` branches!
Omar Emara merged commit 6b53fd5cf6 into main 2023-09-25 15:13:02 +02:00
Omar Emara deleted branch allow-more-result-types 2023-09-25 15:13:04 +02:00
Sign in to join this conversation.
No reviewers
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
2 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#112414
No description provided.