[Bf-blender-cvs] [57552f52b29] master: Metal: Realtime compositor enablement with addition of GPU Compute.

Jason Fielder noreply at git.blender.org
Mon Jan 30 11:12:29 CET 2023


Commit: 57552f52b2987adfd1525c3e4109565e3641ef5d
Author: Jason Fielder
Date:   Mon Jan 30 11:00:26 2023 +0100
Branches: master
https://developer.blender.org/rB57552f52b2987adfd1525c3e4109565e3641ef5d

Metal: Realtime compositor enablement with addition of GPU Compute.

This patch adds support for compilation and execution of GLSL compute shaders. This, along with a few systematic changes and fixes, enable realtime compositor functionality with the Metal backend on macOS. A number of GLSL source modifications have been made to add the required level of type explicitness, allowing all compilations to succeed.

GLSL Compute shader compilation follows a similar path to Vertex/Fragment translation, with added support for shader atomics, shared memory blocks and barriers.

Texture flags have also been updated to ensure correct read/write specification for textures used within the compositor pipeline. GPU command submission changes have also been made in the high level path, when Metal is used, to address command buffer time-outs caused by certain expensive compute shaders.

Authored by Apple: Michael Parkin-White

Ref T96261
Ref T99210

Reviewed By: fclem

Maniphest Tasks: T99210, T96261

Differential Revision: https://developer.blender.org/D16990

===================================================================

M	source/blender/compositor/realtime_compositor/intern/shader_operation.cc
M	source/blender/compositor/realtime_compositor/shaders/compositor_blur.glsl
M	source/blender/compositor/realtime_compositor/shaders/compositor_blur_variable_size.glsl
M	source/blender/compositor/realtime_compositor/shaders/compositor_directional_blur.glsl
M	source/blender/compositor/realtime_compositor/shaders/compositor_edge_filter.glsl
M	source/blender/compositor/realtime_compositor/shaders/compositor_filter.glsl
M	source/blender/compositor/realtime_compositor/shaders/compositor_glare_ghost_accumulate.glsl
M	source/blender/compositor/realtime_compositor/shaders/compositor_glare_ghost_base.glsl
M	source/blender/compositor/realtime_compositor/shaders/compositor_glare_highlights.glsl
M	source/blender/compositor/realtime_compositor/shaders/compositor_glare_mix.glsl
M	source/blender/compositor/realtime_compositor/shaders/compositor_glare_streaks_filter.glsl
M	source/blender/compositor/realtime_compositor/shaders/compositor_morphological_distance_threshold.glsl
M	source/blender/compositor/realtime_compositor/shaders/compositor_parallel_reduction.glsl
M	source/blender/compositor/realtime_compositor/shaders/compositor_realize_on_domain.glsl
M	source/blender/compositor/realtime_compositor/shaders/compositor_screen_lens_distortion.glsl
M	source/blender/compositor/realtime_compositor/shaders/compositor_symmetric_blur_variable_size.glsl
M	source/blender/compositor/realtime_compositor/shaders/infos/compositor_edge_filter_info.hh
M	source/blender/compositor/realtime_compositor/shaders/infos/compositor_filter_info.hh
M	source/blender/draw/engines/compositor/compositor_engine.cc
M	source/blender/draw/engines/eevee_next/eevee_depth_of_field.cc
M	source/blender/draw/engines/eevee_next/eevee_shader_shared.hh
M	source/blender/draw/engines/eevee_next/shaders/eevee_depth_of_field_accumulator_lib.glsl
M	source/blender/draw/engines/eevee_next/shaders/eevee_depth_of_field_downsample_comp.glsl
M	source/blender/draw/engines/eevee_next/shaders/eevee_depth_of_field_filter_comp.glsl
M	source/blender/draw/engines/eevee_next/shaders/eevee_depth_of_field_reduce_comp.glsl
M	source/blender/draw/engines/eevee_next/shaders/eevee_depth_of_field_stabilize_comp.glsl
M	source/blender/draw/engines/eevee_next/shaders/eevee_film_cryptomatte_post_comp.glsl
M	source/blender/draw/engines/eevee_next/shaders/infos/eevee_depth_of_field_info.hh
M	source/blender/draw/intern/draw_curves.cc
M	source/blender/draw/intern/draw_hair.cc
M	source/blender/gpu/intern/gpu_shader_create_info.cc
M	source/blender/gpu/intern/gpu_viewport.c
M	source/blender/gpu/metal/mtl_backend.hh
M	source/blender/gpu/metal/mtl_backend.mm
M	source/blender/gpu/metal/mtl_capabilities.hh
M	source/blender/gpu/metal/mtl_command_buffer.mm
M	source/blender/gpu/metal/mtl_context.hh
M	source/blender/gpu/metal/mtl_context.mm
M	source/blender/gpu/metal/mtl_shader.hh
M	source/blender/gpu/metal/mtl_shader.mm
M	source/blender/gpu/metal/mtl_shader_generator.hh
M	source/blender/gpu/metal/mtl_shader_generator.mm
M	source/blender/gpu/metal/mtl_shader_interface.hh
M	source/blender/gpu/metal/mtl_shader_interface.mm
M	source/blender/gpu/metal/mtl_state.mm
M	source/blender/gpu/metal/mtl_texture.mm
M	source/blender/gpu/shaders/metal/mtl_shader_common.msl
M	source/blender/gpu/shaders/metal/mtl_shader_defines.msl
M	source/blender/nodes/composite/nodes/node_composite_filter.cc

===================================================================

diff --git a/source/blender/compositor/realtime_compositor/intern/shader_operation.cc b/source/blender/compositor/realtime_compositor/intern/shader_operation.cc
index 88efdae1872..4ac997db42b 100644
--- a/source/blender/compositor/realtime_compositor/intern/shader_operation.cc
+++ b/source/blender/compositor/realtime_compositor/intern/shader_operation.cc
@@ -10,6 +10,7 @@
 
 #include "DNA_customdata_types.h"
 
+#include "GPU_context.h"
 #include "GPU_material.h"
 #include "GPU_shader.h"
 #include "GPU_texture.h"
@@ -329,8 +330,11 @@ void ShaderOperation::generate_code(void *thunk,
   shader_create_info.compute_source("gpu_shader_compositor_main.glsl");
 
   /* The main function is emitted in the shader before the evaluate function, so the evaluate
-   * function needs to be forward declared here. */
-  shader_create_info.typedef_source_generated += "void evaluate();\n";
+   * function needs to be forward declared here.
+   * NOTE(Metal): Metal does not require forward declarations. */
+  if (GPU_backend_get_type() != GPU_BACKEND_METAL) {
+    shader_create_info.typedef_source_generated += "void evaluate();\n";
+  }
 
   operation->generate_code_for_outputs(shader_create_info);
 
@@ -383,10 +387,13 @@ void ShaderOperation::generate_code_for_outputs(ShaderCreateInfo &shader_create_
 
   /* The store functions are used by the node_compositor_store_output_[float|vector|color]
    * functions but are only defined later as part of the compute source, so they need to be forward
-   * declared. */
-  shader_create_info.typedef_source_generated += store_float_function_header + ";\n";
-  shader_create_info.typedef_source_generated += store_vector_function_header + ";\n";
-  shader_create_info.typedef_source_generated += store_color_function_header + ";\n";
+   * declared.
+   * NOTE(Metal): Metal does not require forward declarations. */
+  if (GPU_backend_get_type() != GPU_BACKEND_METAL) {
+    shader_create_info.typedef_source_generated += store_float_function_header + ";\n";
+    shader_create_info.typedef_source_generated += store_vector_function_header + ";\n";
+    shader_create_info.typedef_source_generated += store_color_function_header + ";\n";
+  }
 
   /* Each of the store functions is essentially a single switch case on the given ID, so start by
    * opening the function with a curly bracket followed by opening a switch statement in each of
diff --git a/source/blender/compositor/realtime_compositor/shaders/compositor_blur.glsl b/source/blender/compositor/realtime_compositor/shaders/compositor_blur.glsl
index c7ac620f99b..056ef91b08e 100644
--- a/source/blender/compositor/realtime_compositor/shaders/compositor_blur.glsl
+++ b/source/blender/compositor/realtime_compositor/shaders/compositor_blur.glsl
@@ -35,7 +35,7 @@ vec4 load_weight(ivec2 texel)
    * the texel into the normalized range [0, 1] needed to sample the weights sampler. Finally,
    * invert the textures coordinates by subtracting from 1 to maintain the shape of the weights as
    * mentioned in the function description. */
-  return texture(weights_tx, 1.0 - ((texel + vec2(radius + 0.5)) / (radius * 2 + 1)));
+  return texture(weights_tx, 1.0 - ((vec2(texel) + vec2(radius + 0.5)) / (radius * 2.0 + 1.0)));
 }
 
 void main()
diff --git a/source/blender/compositor/realtime_compositor/shaders/compositor_blur_variable_size.glsl b/source/blender/compositor/realtime_compositor/shaders/compositor_blur_variable_size.glsl
index 9383bbf9825..117991141b9 100644
--- a/source/blender/compositor/realtime_compositor/shaders/compositor_blur_variable_size.glsl
+++ b/source/blender/compositor/realtime_compositor/shaders/compositor_blur_variable_size.glsl
@@ -17,7 +17,7 @@ vec4 load_weight(ivec2 texel, float radius)
   /* The center zero texel is always assigned a unit weight regardless of the corresponding weight
    * in the weights texture. That's to guarantee that at last the center pixel will be accumulated
    * even if the weights texture is zero at its center. */
-  if (texel == ivec2(0)) {
+  if (texel.x == 0 && texel.y == 0) {
     return vec4(1.0);
   }
 
@@ -26,7 +26,7 @@ vec4 load_weight(ivec2 texel, float radius)
    * the texel into the normalized range [0, 1] needed to sample the weights sampler. Finally,
    * invert the textures coordinates by subtracting from 1 to maintain the shape of the weights as
    * mentioned in the function description. */
-  return texture(weights_tx, 1.0 - ((texel + vec2(radius + 0.5)) / (radius * 2 + 1)));
+  return texture(weights_tx, 1.0 - ((vec2(texel) + vec2(radius + 0.5)) / (radius * 2.0 + 1.0)));
 }
 
 void main()
diff --git a/source/blender/compositor/realtime_compositor/shaders/compositor_directional_blur.glsl b/source/blender/compositor/realtime_compositor/shaders/compositor_directional_blur.glsl
index 1805cb5a7f5..75ca7c056a8 100644
--- a/source/blender/compositor/realtime_compositor/shaders/compositor_directional_blur.glsl
+++ b/source/blender/compositor/realtime_compositor/shaders/compositor_directional_blur.glsl
@@ -12,7 +12,7 @@ void main()
    * input size, then transform the coordinates for the next iteration. */
   vec4 accumulated_color = vec4(0.0);
   for (int i = 0; i < iterations; i++) {
-    accumulated_color += texture(input_tx, coordinates / input_size);
+    accumulated_color += texture(input_tx, coordinates / vec2(input_size));
     coordinates = (mat3(inverse_transformation) * vec3(coordinates, 1.0)).xy;
   }
 
diff --git a/source/blender/compositor/realtime_compositor/shaders/compositor_edge_filter.glsl b/source/blender/compositor/realtime_compositor/shaders/compositor_edge_filter.glsl
index 67e27c22602..29a46790b2a 100644
--- a/source/blender/compositor/realtime_compositor/shaders/compositor_edge_filter.glsl
+++ b/source/blender/compositor/realtime_compositor/shaders/compositor_edge_filter.glsl
@@ -12,8 +12,8 @@ void main()
   for (int j = 0; j < 3; j++) {
     for (int i = 0; i < 3; i++) {
       vec3 color = texture_load(input_tx, texel + ivec2(i - 1, j - 1)).rgb;
-      color_x += color * kernel[j][i];
-      color_y += color * kernel[i][j];
+      color_x += color * ukernel[j][i];
+      color_y += color * ukernel[i][j];
     }
   }
 
diff --git a/source/blender/compositor/realtime_compositor/shaders/compositor_filter.glsl b/source/blender/compositor/realtime_compositor/shaders/compositor_filter.glsl
index e501c563dda..b09470defda 100644
--- a/source/blender/compositor/realtime_compositor/shaders/compositor_filter.glsl
+++ b/source/blender/compositor/realtime_compositor/shaders/compositor_filter.glsl
@@ -8,7 +8,7 @@ void main()
   vec4 color = vec4(0);
   for (int j = 0; j < 3; j++) {
     for (int i = 0; i < 3; i++) {
-      color += texture_load(input_tx, texel + ivec2(i - 1, j - 1)) * kernel[j][i];
+      color += texture_load(input_tx, texel + ivec2(i - 1, j - 1)) * ukernel[j][i];
     }
   }
 
diff --git a/source/blender/compositor/realtime_compositor/shaders/compositor_glare_ghost_accumulate.glsl b/source/blender/compositor/realtime_compositor/shaders/compositor_glare_ghost_accumulate.glsl
index 83598fb8a63..0e6353cc066 100644
--- a/source/blender/compositor/realtime_compositor/shaders/compositor_glare_ghost_accumulate.glsl
+++ b/source/blender/compositor/realtime_compositor/shaders/compositor_glare_ghost_accumulate.glsl
@@ -7,7 +7,7 @@ void main()
 
   /* Add 0.5 to evaluate the input sampler at the center of the pixel and divide by the image size
    * to get the coordinates into the sampler's expected [0, 1] range. */
-  vec2 coordinates = (vec2(texel) + vec2(0.5)) / input_size;
+  vec2 coordinates = (vec2(texel) + vec2(0.5)) / vec2(input_size);
 
   /* We accumulate four variants of the input ghost texture, each is scaled by some amount and
    * possibly multiplied by some color as a form of color modulation. */
diff --git a/source/blender/compositor/realtime_compositor/shaders/compositor_glare_ghost_base.glsl b/source/blender/compositor/realtime_compositor/shaders/compositor_glare_ghost_base.glsl
index 177b9a86210..ab53c5753b6 100644
--- a/source/blender/compositor/realtime_compositor/shaders/compositor_glare_ghost_base.glsl
+++ b/source/blender/compositor/realtime_compositor/shaders/compositor_glare_ghost_base.glsl
@@ -7,7 +7,7 @@ void main()
 
   /* Add 0.5 to evaluate the input sampler at the center of the pixel and divide by the image size
    * to get the coordinates into the sampler's expected [0, 1] range. */
-  vec2 coordinates = (vec2(texel) + vec2(0.5)) / input_size;
+  vec2 coordinates = (vec2(texel) + vec2(0.5)) / vec2(input_size);
 
   /* The small ghost is scaled down with the origin as the center of the image by a factor of 2.13,
    * while the big ghost is flipped and scaled up with the origin as the center of the image by a
diff --git a/source/blender/compositor/realtime_compositor/shaders/compositor_glare_highlights.glsl b/source/blender/compositor/realtime_compositor/shaders/compositor_glare_highlights.glsl
index c08b1ece366..6c1a7c20495 100644
--- a/source/blender/compositor/realtime_compositor/shaders/compositor_glare_highlights.glsl
+++ b/source/blender/compositor/realtime_compositor/shaders/compositor_glare_highlights.glsl
@@ -12,11 +12,11 @@ void main()
    * the number of input pixels that covers a single output pixel. In case the input and output
    * have the same size, this will be 0.5, which is the offset required to evaluate the sampler at
    * the center of the pixel. */
-  vec2 offset = (texture_size(input_tx) / imageSize(output_img)) / 2.0;
+  vec2 offset = vec2(texture_size(input_tx) / imageSize(output_img)) / 2.0;
 
   /* Add the aforementioned offset and divide by the output image size to get the coordinates into
    * the sampler's expected [0, 1] range. */
-  vec2 normalized_coordinates = (vec2(texel) + offset) / imageSize(output_img);
+  vec2 normalized_coordinates = (vec2(texel) + offset) / vec2(imageSize(output_img));
 
   vec4 input_color = texture(input_tx, normalized_coordinates);
   float luminance = dot(input_color.rgb, luminance_coefficients);
diff --git a/source/blender/compositor/realtime_compositor/shaders/compositor_glare_mix.glsl b/source/blender/compositor/realtime_compositor/shaders/compositor_glare_mix.glsl
index ab287be3f0b..598d6047315 100644
--- a/s

@@ Diff output truncated at 10240 characters. @@



More information about the Bf-blender-cvs mailing list