GPU: Add explicit matrix constructor #119031

Open
opened 2024-03-03 16:10:42 +01:00 by Clément Foucault · 5 comments

MSL doesn't support as much constructor for matrices as GLSL. This creates some error prone situation where only some constructors are added on the MSL side.
Moreover, MSL need string replacement to support these non-native constructors, which slows compilation down.

While the string replacement is still needed for PyGPU shader (for compatibility reason) we can avoid this for internal shaders.

Proposal

  • Add explicit constructors in both GLSL and MSL with something like float3x3 make_float3x3(float3 a, float3 b, float3 c) and only declare a small set of constructors (no mixed scalar / vector constructor). I chose to use cycles convention of make_*.
  • Prohibit usage of builtin constructor (after removing existing usages) by using something like #define mat3() do_not_use.
MSL doesn't support as much constructor for matrices as GLSL. This creates some error prone situation where only some constructors are added on the MSL side. Moreover, MSL need string replacement to support these non-native constructors, which slows compilation down. While the string replacement is still needed for PyGPU shader (for compatibility reason) we can avoid this for internal shaders. ### Proposal - Add explicit constructors in both GLSL and MSL with something like `float3x3 make_float3x3(float3 a, float3 b, float3 c)` and only declare a small set of constructors (no mixed scalar / vector constructor). I chose to use cycles convention of `make_*`. - Prohibit usage of builtin constructor (after removing existing usages) by using something like `#define mat3() do_not_use`.
Clément Foucault added the
Module
EEVEE & Viewport
Type
Design
labels 2024-03-03 16:10:42 +01:00
Member

What types of matrix constructors are supported/unsupported in MSL?

If possible, I would do this only for non supported constructors.

So we still can use:
mat3 m = mat3(i, j, k);
but mat2x3(i, j) triggers a compilation error,
so we have to use :
mat2x3 m = make_mat2x3(i, j);

What types of matrix constructors are supported/unsupported in MSL? If possible, I would do this only for non supported constructors. So we still can use: `mat3 m = mat3(i, j, k);` but `mat2x3(i, j)` triggers a compilation error, so we have to use : `mat2x3 m = make_mat2x3(i, j);`
Author
Member

The goal of prohibiting the use of the builtin constructors is to make sure it triggers an error if it not implemented on both MSL and GLSL. But it is not possible to selectively prohibit the usage of some constructors and allow some others.

MSL does support the following constructors (extended to all matrix types):

float2x2(float)
float2x2(float2,float2)
float2x2(float,float,float,float)

but does not support the following (but GLSL does):

float3x3(float2x2)
float3x3(float4x4)
float2x2(float2,float,float)
The goal of prohibiting the use of the builtin constructors is to make sure it triggers an error if it not implemented on both MSL and GLSL. But it is not possible to selectively prohibit the usage of some constructors and allow some others. MSL does support the following constructors (extended to all matrix types): ``` float2x2(float) float2x2(float2,float2) float2x2(float,float,float,float) ``` but does **not** support the following (but GLSL does): ``` float3x3(float2x2) float3x3(float4x4) float2x2(float2,float,float) ```
Author
Member

The fact that mat2x3(i, j) doesn't work is because it's missing implementation inside mtl_shader_defines.msl. But the current system forces us to re-implement all possible constructor for every types because of the macro/string replacement.

The fact that `mat2x3(i, j)` doesn't work is because it's missing implementation inside `mtl_shader_defines.msl`. But the current system forces us to re-implement all possible constructor for every types because of the macro/string replacement.
Member

Then why not add the missing constructors in mtl_shader_defines.msl?

Then why not add the missing constructors in `mtl_shader_defines.msl`?
Author
Member

Because all of them needs to be added explicitly. Which is both impractical, error prone, and doesn't solve the issue that this still needs string search and replace over the whole shader source, nor does it fix the fact that some invalid constructors in MSL would still be allowed in GLSL.

Because all of them needs to be added explicitly. Which is both impractical, error prone, and doesn't solve the issue that this still needs string search and replace over the whole shader source, nor does it fix the fact that some invalid constructors in MSL would still be allowed in GLSL.
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
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#119031
No description provided.