GPU: Refactor texture samplers #105642

Merged
Omar Emara merged 9 commits from OmarEmaraDev/blender:texture-sampler-refactor into main 2023-04-04 15:16:20 +02:00
Member

This patch refactors the texture samples code by mainly splitting the
eGPUSamplerState enum into multiple smaller enums and packing them
inside a GPUSamplerState struct. This was done because many members of
the enum were mutually exclusive, which was worked around during setting
up the samplers in the various backends, and additionally made the API
confusing, like the GPU_texture_wrap_mode function, which had two
mutually exclusive parameters.

The new structure also improved and clarified the backend sampler cache,
reducing the cache size from 514 samplers to just 130 samplers, which
also slightly improved the initialization time. Further, the
GPU_SAMPLER_MAX signal value was naturally incorporated into the
structure using the GPU_SAMPLER_STATE_TYPE_INTERNAL type.

The only expected functional change is in the realtime compositor, which
now supports per-axis repetition control, utilizing new API functions
for that purpose.

This patch is loosely based on an older patch D14366 by Ethan Hall.

This patch refactors the texture samples code by mainly splitting the eGPUSamplerState enum into multiple smaller enums and packing them inside a GPUSamplerState struct. This was done because many members of the enum were mutually exclusive, which was worked around during setting up the samplers in the various backends, and additionally made the API confusing, like the GPU_texture_wrap_mode function, which had two mutually exclusive parameters. The new structure also improved and clarified the backend sampler cache, reducing the cache size from 514 samplers to just 130 samplers, which also slightly improved the initialization time. Further, the GPU_SAMPLER_MAX signal value was naturally incorporated into the structure using the GPU_SAMPLER_STATE_TYPE_INTERNAL type. The only expected functional change is in the realtime compositor, which now supports per-axis repetition control, utilizing new API functions for that purpose. This patch is loosely based on an older patch D14366 by Ethan Hall.
Omar Emara added the
Module
EEVEE & Viewport
label 2023-03-10 19:35:17 +01:00
Omar Emara added 1 commit 2023-03-10 19:35:28 +01:00
buildbot/vexp-code-patch-coordinator Build done. Details
a6b32fb8df
GPU: Refactor texture samplers
This patch refactors the texture samples code by mainly splitting the
eGPUSamplerState enum into multiple smaller enums and packing them
inside a GPUSamplerState struct. This was done because many members of
the enum were mutually exclusive, which was worked around during setting
up the samplers in the various backends, and additionally made the API
confusing, like the GPU_texture_wrap_mode function, which had two
mutually exclusive parameters.

The new structure also improved and clarified the backend sampler cache,
reducing the cache size from 514 samplers to just 130 samplers, which
also slightly improved the initialization time. Further, the
GPU_SAMPLER_MAX signal value was naturally incorporated into the
structure using the GPU_SAMPLER_STATE_TYPE_INTERNAL type.

The only expected functional change is in the realtime compositor, which
now supports per-axis repetition control, utilizing new API functions
for that purpose.

This patch is loosely based on an older patch D14366 by Ethan Hall.
Author
Member

@blender-bot package macos

@blender-bot package macos
Member

Package build started. Download here when ready.

Package build started. [Download here](https://builder.blender.org/download/patch/PR105642) when ready.
Omar Emara added 1 commit 2023-03-11 17:45:41 +01:00
buildbot/vexp-code-patch-coordinator Build done. Details
0d4a76e8fa
Fix Metal compile errors
Author
Member

@blender-bot package macos

@blender-bot package macos
Member

Package build started. Download here when ready.

Package build started. [Download here](https://builder.blender.org/download/patch/PR105642) when ready.
Omar Emara added 1 commit 2023-03-11 17:54:46 +01:00
buildbot/vexp-code-patch-coordinator Build done. Details
532e781fff
Fix more Metal compile errors
Author
Member

@blender-bot package macos

@blender-bot package macos
Member

Package build started. Download here when ready.

Package build started. [Download here](https://builder.blender.org/download/patch/PR105642) when ready.
Clément Foucault reviewed 2023-03-11 18:43:51 +01:00
@ -40,3 +37,1 @@
* extension / wrap mode etc...
*/
typedef enum eGPUSamplerState {
typedef enum eGPUSamplerFiltering {

I think it is a good opportunity to remove the e prefix which is not part of the code-style anymore (because it doesn't add more information).

I think it is a good opportunity to remove the `e` prefix which is not part of the code-style anymore (because it doesn't add more information).
OmarEmaraDev marked this conversation as resolved
@ -46,2 +41,3 @@
*/
GPU_SAMPLER_DEFAULT = 0,
GPU_SAMPLER_FILTERING_DEFAULT = 0,

All these empty lines can be remove.

All these empty lines can be remove.
OmarEmaraDev marked this conversation as resolved
@ -70,0 +83,4 @@
*/
typedef enum eGPUSamplerWrapType {
/** Extrapolate by extending the edge pixels of the texture, in other words, the texture
coordinates are clamped. */

Comment style.

Comment style.
OmarEmaraDev marked this conversation as resolved
@ -70,0 +87,4 @@
GPU_SAMPLER_WRAP_EXTEND = 0,
/** Extrapolate by repeating the texture. */
GPU_SAMPLER_WRAP_REPEAT = 1,

I think you can omit the actual value set after the first one. Keep = 0 but remove = 1, = 2, ...

I think you can omit the actual value set after the first one. Keep ` = 0` but remove ` = 1`, ` = 2`, ...
OmarEmaraDev marked this conversation as resolved
@ -75,0 +98,4 @@
* */
GPU_SAMPLER_WRAP_CLIP = 3,
GPU_SAMPLER_WRAP_TYPES_COUNT = 4,

It was discussed that these counts should be defined like this:
#define GPU_SAMPLER_WRAP_TYPES_COUNT (GPU_SAMPLER_WRAP_CLIP + 1)

This is because it allows to skip this case in switch statements and not have a compiler warning.

It was discussed that these counts should be defined like this: `#define GPU_SAMPLER_WRAP_TYPES_COUNT (GPU_SAMPLER_WRAP_CLIP + 1)` This is because it allows to skip this case in `switch` statements and not have a compiler warning.
OmarEmaraDev marked this conversation as resolved
Omar Emara added 1 commit 2023-03-11 20:26:59 +01:00
Clément Foucault reviewed 2023-03-11 21:07:06 +01:00
@ -97,3 +185,1 @@
static constexpr eGPUSamplerState GPU_SAMPLER_MAX = eGPUSamplerState(GPU_SAMPLER_ICON + 1);
#else
static const int GPU_SAMPLER_MAX = (GPU_SAMPLER_ICON + 1);
static constexpr GPUSamplerState default_sampler()

These should be documented. For example, I'm not sure what an internal_sampler is right away.

These should be documented. For example, I'm not sure what an `internal_sampler` is right away.
OmarEmaraDev marked this conversation as resolved
@ -100,0 +222,4 @@
* Enables the given filtering options if and only if the given condition is true, otherwise,
* disables it.
*/
void set_filtering(GPUSamplerFiltering target_filtering, bool condition = true)

I think name of the function is confusing.

To me, you should remove the condition and have an unset_filtering equivalent.
But this does make the callers use if statements most of the time.

Another possibility is to use a more descriptive name like set_filtering_flag_from_test.

I think name of the function is confusing. To me, you should remove the condition and have an `unset_filtering` equivalent. But this does make the callers use if statements most of the time. Another possibility is to use a more descriptive name like `set_filtering_flag_from_test`.
Author
Member

I tried couple of names and signatures for this function and eventually went for set_filtering because it apparently hides the underlying data and makes sense when reading it, for instance:

set_filtering(GPU_SAMPLER_FILTERING_MIPMAP, true);
Set mipmap filtering to true.

set_filtering(GPU_SAMPLER_FILTERING_MIPMAP, false);
Set mipmap filtering to false.

The user does not know how filtering is stored internally, the function just promises that it can be enabled or disabled, and reading the function feels natural. What do you think?

I tried couple of names and signatures for this function and eventually went for set_filtering because it apparently hides the underlying data and makes sense when reading it, for instance: ``` set_filtering(GPU_SAMPLER_FILTERING_MIPMAP, true); Set mipmap filtering to true. set_filtering(GPU_SAMPLER_FILTERING_MIPMAP, false); Set mipmap filtering to false. ``` The user does not know how filtering is stored internally, the function just promises that it can be enabled or disabled, and reading the function feels natural. What do you think?

The thing is, filtering is too generic of a name. I don't know if this will set the whole enum value or just one option if I see set_filtering(GPU_SAMPLER_FILTERING_MIPMAP). And I don't immediatly know what
false refers to when seeing set_filtering(GPU_SAMPLER_FILTERING_MIPMAP, false).

I would say enable_filtering_flag(flag), disable_filtering_flag(flag) and set_filtering_flag_from_test(flag, test) would be better from a semantical point of view.

The thing is, filtering is too generic of a name. I don't know if this will set the whole `enum` value or just one option if I see `set_filtering(GPU_SAMPLER_FILTERING_MIPMAP)`. And I don't immediatly know what `false` refers to when seeing `set_filtering(GPU_SAMPLER_FILTERING_MIPMAP, false)`. I would say `enable_filtering_flag(flag)`, `disable_filtering_flag(flag)` and `set_filtering_flag_from_test(flag, test)` would be better from a semantical point of view.
OmarEmaraDev marked this conversation as resolved
Clément Foucault reviewed 2023-03-12 10:06:07 +01:00
Clément Foucault left a comment
Member

I still have to review all the usages. But the overall approach looks good.

I still have to review all the usages. But the overall approach looks good.
Omar Emara added 1 commit 2023-03-12 15:48:13 +01:00
Author
Member

@fclem One thing to note is that my Metal changes are blindly made, so it would be good if someone can test Metal.

@fclem One thing to note is that my Metal changes are blindly made, so it would be good if someone can test Metal.
Clément Foucault reviewed 2023-03-12 17:57:24 +01:00
@ -453,7 +453,7 @@ static bool createGPUCurveMapping(OCIO_GPUCurveMappping &curvemap,
curvemap.texture = GPU_texture_create_1d(
"OCIOCurveMap", lut_size, 1, GPU_RGBA16F, GPU_TEXTURE_USAGE_SHADER_READ, nullptr);
GPU_texture_filter_mode(curvemap.texture, false);
GPU_texture_wrap_mode(curvemap.texture, false, true);

It seems you did not took into account the clamp to border color case in all of these.

It seems you did not took into account the clamp to border color case in all of these.
fclem marked this conversation as resolved
@ -70,0 +78,4 @@
* The `GPUSamplerWrapType` specifies how the texture will be extrapolated for out-of-bound
* texture sampling.
*/
typedef enum GPUSamplerWrapType {

Wrap is a bit misleading terminology here. So maybe GPUSamplerExtendMode?

Wrap is a bit misleading terminology here. So maybe `GPUSamplerExtendMode`?
OmarEmaraDev marked this conversation as resolved
@ -75,0 +92,4 @@
* Extrapolate using the value of TEXTURE_BORDER_COLOR, which is always set to a transparent
* black color (0, 0, 0, 0) and can't be changed.
*/
GPU_SAMPLER_WRAP_CLIP,

Rename to CLAMP_TO_BORDER instead of CLIP.

Rename to `CLAMP_TO_BORDER` instead of `CLIP`.
OmarEmaraDev marked this conversation as resolved
@ -96,0 +178,4 @@
typedef struct GPUSamplerState {
GPUSamplerFiltering filtering : 8;
GPUSamplerWrapType wrapping_x : 4;
GPUSamplerWrapType wrapping_y : 4;

You are missing the wrapping_z for 3D texture.
Prefer using extend_x/y/z.

You are missing the `wrapping_z` for 3D texture. Prefer using `extend_x/y/z`.
fclem marked this conversation as resolved
Omar Emara reviewed 2023-03-13 08:12:28 +01:00
@ -453,7 +453,7 @@ static bool createGPUCurveMapping(OCIO_GPUCurveMappping &curvemap,
curvemap.texture = GPU_texture_create_1d(
"OCIOCurveMap", lut_size, 1, GPU_RGBA16F, GPU_TEXTURE_USAGE_SHADER_READ, nullptr);
GPU_texture_filter_mode(curvemap.texture, false);
GPU_texture_wrap_mode(curvemap.texture, false, true);
Author
Member

Can you clarify this? Where is the clamp to border case here?

Can you clarify this? Where is the clamp to border case here?
fclem marked this conversation as resolved
@ -96,0 +178,4 @@
typedef struct GPUSamplerState {
GPUSamplerFiltering filtering : 8;
GPUSamplerWrapType wrapping_x : 4;
GPUSamplerWrapType wrapping_y : 4;
Author
Member

I did not add a z wrapping option for now because it had no use in the code base at the moment. So I though maybe we should add it when we really need it.

I did not add a z wrapping option for now because it had no use in the code base at the moment. So I though maybe we should add it when we really need it.
OmarEmaraDev marked this conversation as resolved
Omar Emara added 1 commit 2023-03-13 09:54:24 +01:00
Clément Foucault requested changes 2023-03-15 12:54:06 +01:00
@ -453,7 +453,7 @@ static bool createGPUCurveMapping(OCIO_GPUCurveMappping &curvemap,
curvemap.texture = GPU_texture_create_1d(
"OCIOCurveMap", lut_size, 1, GPU_RGBA16F, GPU_TEXTURE_USAGE_SHADER_READ, nullptr);
GPU_texture_filter_mode(curvemap.texture, false);
GPU_texture_wrap_mode(curvemap.texture, false, true);

Disregard that. I did check all usage and they look right.

Disregard that. I did check all usage and they look right.
OmarEmaraDev marked this conversation as resolved
@ -91,0 +157,4 @@
* the texture will be used. In other words, this is a signal value and stores no useful or
* actual data.
*/
GPU_SAMPLER_STATE_TYPE_INTERNAL = 2,

Remaining value.

Remaining value.
OmarEmaraDev marked this conversation as resolved
@ -96,0 +178,4 @@
typedef struct GPUSamplerState {
GPUSamplerFiltering filtering : 8;
GPUSamplerExtendMode extend_x : 4;
GPUSamplerExtendMode extend_y : 4;

The thing is, we already use 3D textures, and some of them use CLAMP_TO_BORDER (ex: Volumes Attributes) and some use EXTEND (ex: EEVEE Volume temp textures). If the choice to omit wrapping_z is to save a dimension, it should be documented. Maybe rename wrapping_y to wrapping_yz in this case and make Z follow Y extend mode.

The thing is, we already use 3D textures, and some of them use `CLAMP_TO_BORDER` (ex: Volumes Attributes) and some use `EXTEND` (ex: EEVEE Volume temp textures). If the choice to omit `wrapping_z` is to save a dimension, it should be documented. Maybe rename `wrapping_y` to `wrapping_yz` in this case and make Z follow Y extend mode.
OmarEmaraDev marked this conversation as resolved
@ -545,2 +547,3 @@
GLuint GLTexture::samplers_[GPU_SAMPLER_MAX] = {0};
/** A static array that maps GPUSamplerExtendMode values to their OpenGL enum counterparts. */
static const GLenum gl_texture_wrap_mode_map[] = {

Replace by GLenum to_gl(GPUSamplerExtendMode mode) inline function. See inline GLenum to_gl(eGPUDataFormat format) for example.

Doing so will produce a compiler error if any new enum value is being added.

Replace by `GLenum to_gl(GPUSamplerExtendMode mode)` inline function. See `inline GLenum to_gl(eGPUDataFormat format)` for example. Doing so will produce a compiler error if any new enum value is being added.
OmarEmaraDev marked this conversation as resolved

It might be better to commit this after the 3.5 release to avoid merge conflict.

It might be better to commit this after the 3.5 release to avoid merge conflict.
Omar Emara added 1 commit 2023-03-21 14:11:29 +01:00
Clément Foucault approved these changes 2023-03-21 16:57:19 +01:00
Omar Emara added 2 commits 2023-04-04 13:12:08 +02:00
Author
Member

@blender-bot package macos

@blender-bot package macos
Member

Package build started. Download here when ready.

Package build started. [Download here](https://builder.blender.org/download/patch/PR105642) when ready.
Omar Emara merged commit ff3b2226fb into main 2023-04-04 15:16:20 +02:00
Omar Emara deleted branch texture-sampler-refactor 2023-04-04 15:16:20 +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
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#105642
No description provided.