Add anti aliasing to transform constraints #107394

Merged
Contributor

Transform constraints were being drawn on screen without anti-aliasing, now fixed to make them look smoother.
See #107038

Adding a comparison of before and after the change

Transform constraints were being drawn on screen without anti-aliasing, now fixed to make them look smoother. See #107038 Adding a comparison of before and after the change
Yonatan-Maor requested review from Pablo Vazquez 2023-04-27 08:25:39 +02:00
Yonatan-Maor changed title from fix_107038_add_anti_aliasing_to_transform_constraints to Fix #107038: Add anti aliasing to transform constraints 2023-04-27 11:10:43 +02:00
Iliya Katushenock added this to the EEVEE & Viewport project 2023-04-27 16:28:52 +02:00
Iliya Katushenock changed title from Fix #107038: Add anti aliasing to transform constraints to Add anti aliasing to transform constraints 2023-04-27 16:29:19 +02:00

Hi, #107038 isn't a bug, but todo design. So name for this PR shouldn't contain Fix... prefix, this just new feature.

Hi, #107038 isn't a bug, but todo design. So name for this PR shouldn't contain `Fix...` prefix, this just new feature.
Author
Contributor

Thanks! I thought this was considered a bug, which is why I added Fix to the title.
If this is not a bug should I add a WIP prefix to the title?

Thanks! I thought this was considered a bug, which is why I added Fix to the title. If this is not a bug should I add a WIP prefix to the title?

WIP would say that this is not ready for review and approving and is still being developed. Some about this: https://wiki.blender.org/wiki/Style_Guide/Commit_Messages

WIP would say that this is not ready for review and approving and is still being developed. Some about this: https://wiki.blender.org/wiki/Style_Guide/Commit_Messages
Author
Contributor

WIP would say that this is not ready for review and approving and is still being developed. Some about this: https://wiki.blender.org/wiki/Style_Guide/Commit_Messages

Cool, I'll take a look

> WIP would say that this is not ready for review and approving and is still being developed. Some about this: https://wiki.blender.org/wiki/Style_Guide/Commit_Messages Cool, I'll take a look
Member

Works well, code looks correct.

Works well, code looks correct.
Author
Contributor

Thanks!
I'm kind of new here, who should be authorizing the pull request?

Thanks! I'm kind of new here, who should be authorizing the pull request?
Harley Acheson requested review from Clément Foucault 2023-04-27 19:35:09 +02:00
Member

I'm kind of new here, who should be authorizing the pull request?

The two reviewers you now have are fine I think, with Clément being the blocking reviewer who will probably have the last word and approval, and would be the one to commit. This process could take as little as a few days to weeks, and you should make another comment in this thread if nothing happens for a while.

> I'm kind of new here, who should be authorizing the pull request? The two reviewers you now have are fine I think, with Clément being the blocking reviewer who will probably have the last word and approval, and would be the one to commit. This process could take as little as a few days to weeks, and you should make another comment in this thread if nothing happens for a while.
Clément Foucault requested changes 2023-04-27 20:19:19 +02:00
Clément Foucault left a comment
Member

Thanks for looking into it! However, this isn't a portable fix. It will not work on MacOS / Metal backend.

The portable fix is to replace the shader program by GPU_SHADER_3D_POLYLINE_UNIFORM_COLOR. See source/blender/editors/gizmo_library/gizmo_types/button2d_gizmo.c line 86 for an example of the uniforms to give to the shader.

Note that GPU_SHADER_3D_POLYLINE_UNIFORM_COLOR also requires GPU_BLEND_ALPHA for correct anti-aliasing.

Thanks for looking into it! However, this isn't a portable fix. It will not work on MacOS / Metal backend. The portable fix is to replace the shader program by `GPU_SHADER_3D_POLYLINE_UNIFORM_COLOR`. See `source/blender/editors/gizmo_library/gizmo_types/button2d_gizmo.c` line 86 for an example of the uniforms to give to the shader. Note that `GPU_SHADER_3D_POLYLINE_UNIFORM_COLOR` also requires `GPU_BLEND_ALPHA` for correct anti-aliasing.
Author
Contributor

Not sure that I'm well acquainted enough with the code to do this, but I'll take a look.
If you have any time, could you explain why this wouldn't work on other platforms? I simply took the code that is used to draw the global axes (I'm guessing that this code works on other platforms)

Not sure that I'm well acquainted enough with the code to do this, but I'll take a look. If you have any time, could you explain why this wouldn't work on other platforms? I simply took the code that is used to draw the global axes (I'm guessing that this code works on other platforms)

could you explain why this wouldn't work on other platforms

The function call GPU_line_smooth(true); enables the deprecated OpenGL feature GL_LINE_SMOOTH that isn't available on Metal. We have a software workaround using a special shader (as mentionned) but it needs a bit more work. And, yes, anything using GPU_line_smooth(true); is currently not anti-aliased on Apple hardware.

> could you explain why this wouldn't work on other platforms The function call `GPU_line_smooth(true);` enables the deprecated OpenGL feature `GL_LINE_SMOOTH` that isn't available on Metal. We have a software workaround using a special shader (as mentionned) but it needs a bit more work. And, yes, anything using `GPU_line_smooth(true);` is currently not anti-aliased on Apple hardware.
Member

Not sure that I'm well acquainted enough with the code to do this, but I'll take a look.

You can do it. If you look at the section of code that Clément referenced you will see there isn't a big difference, just a change of shader that needs a bit more information. It probably would look something like:

diff --git a/source/blender/editors/transform/transform_constraints.c b/source/blender/editors/transform/transform_constraints.c
index 9b92a37abdf..9a2f0c002f8 100644
--- a/source/blender/editors/transform/transform_constraints.c
+++ b/source/blender/editors/transform/transform_constraints.c
@@ -776,11 +776,18 @@ static void drawLine(
   }
   UI_make_axis_color(col, col2, axis);
 
   uint pos = GPU_vertformat_attr_add(immVertexFormat(), "pos", GPU_COMP_F32, 3, GPU_FETCH_FLOAT);
 
-  immBindBuiltinProgram(GPU_SHADER_3D_UNIFORM_COLOR);
+  float viewport[4];
+  GPU_viewport_size_get_f(viewport);
+  GPU_blend(GPU_BLEND_ALPHA);
+
+  immBindBuiltinProgram(GPU_SHADER_3D_POLYLINE_UNIFORM_COLOR);
+  immUniform2fv("viewportSize", &viewport[2]);
+  immUniform1f("lineWidth", U.pixelsize);
+
   immUniformColor3ubv(col2);
 
   immBegin(GPU_PRIM_LINES, 2);
   immVertex3fv(pos, v1);
   immVertex3fv(pos, v2);
> Not sure that I'm well acquainted enough with the code to do this, but I'll take a look. You can do it. If you look at the section of code that Clément referenced you will see there isn't a big difference, just a change of shader that needs a bit more information. It probably would look _something_ like: ```Diff diff --git a/source/blender/editors/transform/transform_constraints.c b/source/blender/editors/transform/transform_constraints.c index 9b92a37abdf..9a2f0c002f8 100644 --- a/source/blender/editors/transform/transform_constraints.c +++ b/source/blender/editors/transform/transform_constraints.c @@ -776,11 +776,18 @@ static void drawLine( } UI_make_axis_color(col, col2, axis); uint pos = GPU_vertformat_attr_add(immVertexFormat(), "pos", GPU_COMP_F32, 3, GPU_FETCH_FLOAT); - immBindBuiltinProgram(GPU_SHADER_3D_UNIFORM_COLOR); + float viewport[4]; + GPU_viewport_size_get_f(viewport); + GPU_blend(GPU_BLEND_ALPHA); + + immBindBuiltinProgram(GPU_SHADER_3D_POLYLINE_UNIFORM_COLOR); + immUniform2fv("viewportSize", &viewport[2]); + immUniform1f("lineWidth", U.pixelsize); + immUniformColor3ubv(col2); immBegin(GPU_PRIM_LINES, 2); immVertex3fv(pos, v1); immVertex3fv(pos, v2); ```
Harley Acheson changed title from Add anti aliasing to transform constraints to WIP: Add anti aliasing to transform constraints 2023-04-27 22:05:42 +02:00
Author
Contributor

I think that I'm getting the expected result using this code snippet (thanks @Harley )
Thing is, the lines are extremely thin, which makes them look aliased again (I tried increasing the linewidth to make sure my results look ok).

Is the required line width documented anywhere?

I think that I'm getting the expected result using this code snippet (thanks @Harley ) Thing is, the lines are extremely thin, which makes them look aliased again (I tried increasing the linewidth to make sure my results look ok). Is the required line width documented anywhere?
Member

U.pixelsize should be consider our minimum feature size, so dots or lines shouldn't get smaller than that.

For most users you can just go to Preferences / Interface and set "Resolution Scale" to 1.0 and "Line Width" to "Default" and you should see most lines 1 pixel wide and U.pixelsize will be 1. Change to "Thick" lines and that value will be 2. A Mac user with a Retina (high DPI) display will get a U.pixelsize of 2 when scale is 1 and using default line width.

Although U.pixelsize is a float it is actually rounded to nearest int. That shader's "lineWidth" can accept any float value so you can really just eyeball it and set that to any multiplier against it that looks good to you, like U.pixelsize * 1.12f or whatever. I'd probably make it have the same fullness as the grid axes lines, but pick something you think looks good, just make sure to test at various sizes. If you pick badly we'll just let you know during review.

U.pixelsize should be consider our minimum feature size, so dots or lines shouldn't get smaller than that. For most users you can just go to Preferences / Interface and set "Resolution Scale" to 1.0 and "Line Width" to "Default" and you should see most lines 1 pixel wide and U.pixelsize will be 1. Change to "Thick" lines and that value will be 2. A Mac user with a Retina (high DPI) display will get a U.pixelsize of 2 when scale is 1 and using default line width. Although U.pixelsize is a float it is actually rounded to nearest int. That shader's "lineWidth" can accept any float value so you can really just eyeball it and set that to any multiplier against it that looks good to you, like `U.pixelsize * 1.12f` or whatever. I'd probably make it have the same fullness as the grid axes lines, but pick something you think looks good, just make sure to test at various sizes. If you pick badly we'll just let you know during review.
Author
Contributor

Made the requested changes.
Adding new before/after images:

Made the requested changes. Adding new before/after images:
Yonatan-Maor requested review from Clément Foucault 2023-04-28 05:54:56 +02:00
Clément Foucault requested changes 2023-04-28 09:44:14 +02:00
Clément Foucault left a comment
Member

This looks good! Just need to cleanup the unused line and it's good to be merged.

This looks good! Just need to cleanup the unused line and it's good to be merged.
@ -779,3 +779,3 @@
uint pos = GPU_vertformat_attr_add(immVertexFormat(), "pos", GPU_COMP_F32, 3, GPU_FETCH_FLOAT);
immBindBuiltinProgram(GPU_SHADER_3D_UNIFORM_COLOR);
// immBindBuiltinProgram(GPU_SHADER_3D_UNIFORM_COLOR);

Remove the commented line.

Remove the commented line.
Yonatan-Maor closed this pull request 2023-04-28 15:26:59 +02:00
Yonatan-Maor reopened this pull request 2023-04-28 15:43:05 +02:00
Yonatan-Maor requested review from Clément Foucault 2023-04-28 15:43:48 +02:00
Harley Acheson changed title from WIP: Add anti aliasing to transform constraints to Add anti aliasing to transform constraints 2023-04-28 17:16:22 +02:00
Clément Foucault approved these changes 2023-04-28 19:23:05 +02:00
Clément Foucault added 1 commit 2023-04-29 21:56:46 +02:00
Clément Foucault merged commit adb63a5102 into main 2023-04-29 22:06:44 +02:00
Yonatan-Maor deleted branch fix_107038_add_anti_aliasing_to_transform_constraints 2023-04-30 06:01:52 +02:00
Howard Trickey referenced this issue from a commit 2023-05-29 02:51:43 +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 Assignees
4 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#107394
No description provided.