[Bf-blender-cvs] [9c0d822737f] master: GPU: Compile vulkan shaders to Spir-V binaries.

Jeroen Bakker noreply at git.blender.org
Mon Dec 12 12:25:23 CET 2022


Commit: 9c0d822737ff9bfca05424667ead642581325733
Author: Jeroen Bakker
Date:   Mon Dec 12 12:22:38 2022 +0100
Branches: master
https://developer.blender.org/rB9c0d822737ff9bfca05424667ead642581325733

GPU: Compile vulkan shaders to Spir-V binaries.

Compile each static shader using shaderc to Spir-V binaries.

The main goal is to make sure that the GLSL created using ShaderCreateInfo and able to compile to Spir-V.
For the second stage a correct pipeline needs to be created and some shader would need more
adjustments (push constants size).

With this patch future changes to GLSL sources can already be checked against vulkan, without the
backend finished.

Mechanism has been tested using MacOS and MoltenVK. For other OS, we should finetune CMake
files to find the right location to shaderc.

```
************************************************************
*** Build Mon 12 Dec 2022 11:08:07 CET
************************************************************
Shader Test compilation result: 463 / 463 passed (skipped 118 for compatibility reasons)
OpenGL backend shader compilation succeeded.
Shader Test compilation result: 529 / 529 passed (skipped 52 for compatibility reasons)
Vulkan backend shader compilation succeeded.
```

Reviewed By: fclem

Maniphest Tasks: T102760

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

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

M	build_files/cmake/platform/platform_apple.cmake
M	source/blender/compositor/realtime_compositor/shaders/compositor_screen_lens_distortion.glsl
M	source/blender/compositor/realtime_compositor/shaders/library/gpu_shader_compositor_texture_utilities.glsl
M	source/blender/draw/engines/eevee_next/eevee_defines.hh
M	source/blender/draw/engines/eevee_next/shaders/eevee_depth_of_field_tiles_flatten_comp.glsl
M	source/blender/draw/engines/overlay/shaders/overlay_motion_path_line_vert.glsl
M	source/blender/draw/engines/workbench/shaders/workbench_shadow_vert.glsl
M	source/blender/draw/intern/shaders/common_smaa_lib.glsl
M	source/blender/draw/intern/shaders/common_view_lib.glsl
M	source/blender/gpu/CMakeLists.txt
M	source/blender/gpu/intern/gpu_shader.cc
M	source/blender/gpu/intern/gpu_shader_builder.cc
M	source/blender/gpu/shaders/gpu_shader_cfg_world_clip_lib.glsl
M	source/blender/gpu/vulkan/vk_backend.cc
M	source/blender/gpu/vulkan/vk_backend.hh
M	source/blender/gpu/vulkan/vk_context.cc
M	source/blender/gpu/vulkan/vk_context.hh
M	source/blender/gpu/vulkan/vk_shader.cc
M	source/blender/gpu/vulkan/vk_shader.hh
A	source/blender/gpu/vulkan/vk_shader_log.cc
A	source/blender/gpu/vulkan/vk_shader_log.hh

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

diff --git a/build_files/cmake/platform/platform_apple.cmake b/build_files/cmake/platform/platform_apple.cmake
index 7a39e6ffda3..b2101662969 100644
--- a/build_files/cmake/platform/platform_apple.cmake
+++ b/build_files/cmake/platform/platform_apple.cmake
@@ -105,9 +105,10 @@ if(WITH_VULKAN_BACKEND)
     set(VULKAN_ROOT_DIR ${LIBDIR}/vulkan/macOS)
     set(VULKAN_INCLUDE_DIR ${VULKAN_ROOT_DIR}/include)
     set(VULKAN_LIBRARY ${VULKAN_ROOT_DIR}/lib/libvulkan.1.dylib)
+    set(SHADERC_LIBRARY ${VULKAN_ROOT_DIR}/lib/libshaderc_combined.a)
 
     set(VULKAN_INCLUDE_DIRS ${VULKAN_INCLUDE_DIR} ${MOLTENVK_INCLUDE_DIRS})
-    set(VULKAN_LIBRARIES ${VULKAN_LIBRARY} ${MOLTENVK_LIBRARIES})
+    set(VULKAN_LIBRARIES ${VULKAN_LIBRARY} ${SHADERC_LIBRARY} ${MOLTENVK_LIBRARIES})
   else()
     message(WARNING "Vulkan SDK was not found, disabling WITH_VULKAN_BACKEND")
     set(WITH_VULKAN_BACKEND OFF)
diff --git a/source/blender/compositor/realtime_compositor/shaders/compositor_screen_lens_distortion.glsl b/source/blender/compositor/realtime_compositor/shaders/compositor_screen_lens_distortion.glsl
index dc572ea5aaf..58c1d97e81d 100644
--- a/source/blender/compositor/realtime_compositor/shaders/compositor_screen_lens_distortion.glsl
+++ b/source/blender/compositor/realtime_compositor/shaders/compositor_screen_lens_distortion.glsl
@@ -20,9 +20,9 @@ vec3 compute_chromatic_distortion_scale(float distance_squared)
 /* Compute the image coordinates after distortion by the given distortion scale computed by the
  * compute_distortion_scale function. Note that the function expects centered normalized UV
  * coordinates but outputs non-centered image coordinates. */
-vec2 compute_distorted_uv(vec2 uv, float scale)
+vec2 compute_distorted_uv(vec2 uv, float uv_scale)
 {
-  return (uv * scale + 0.5) * texture_size(input_tx) - 0.5;
+  return (uv * uv_scale + 0.5) * texture_size(input_tx) - 0.5;
 }
 
 /* Compute the number of integration steps that should be used to approximate the distorted pixel
diff --git a/source/blender/compositor/realtime_compositor/shaders/library/gpu_shader_compositor_texture_utilities.glsl b/source/blender/compositor/realtime_compositor/shaders/library/gpu_shader_compositor_texture_utilities.glsl
index 128fc6aeaf5..cd99e16add6 100644
--- a/source/blender/compositor/realtime_compositor/shaders/library/gpu_shader_compositor_texture_utilities.glsl
+++ b/source/blender/compositor/realtime_compositor/shaders/library/gpu_shader_compositor_texture_utilities.glsl
@@ -1,35 +1,35 @@
 /* A shorthand for 1D textureSize with a zero LOD. */
-int texture_size(sampler1D sampler)
+int texture_size(sampler1D sampler_1d)
 {
-  return textureSize(sampler, 0);
+  return textureSize(sampler_1d, 0);
 }
 
 /* A shorthand for 1D texelFetch with zero LOD and bounded access clamped to border. */
-vec4 texture_load(sampler1D sampler, int x)
+vec4 texture_load(sampler1D sampler_1d, int x)
 {
-  const int texture_bound = texture_size(sampler) - 1;
-  return texelFetch(sampler, clamp(x, 0, texture_bound), 0);
+  const int texture_bound = texture_size(sampler_1d) - 1;
+  return texelFetch(sampler_1d, clamp(x, 0, texture_bound), 0);
 }
 
 /* A shorthand for 2D textureSize with a zero LOD. */
-ivec2 texture_size(sampler2D sampler)
+ivec2 texture_size(sampler2D sampler_2d)
 {
-  return textureSize(sampler, 0);
+  return textureSize(sampler_2d, 0);
 }
 
 /* A shorthand for 2D texelFetch with zero LOD and bounded access clamped to border. */
-vec4 texture_load(sampler2D sampler, ivec2 texel)
+vec4 texture_load(sampler2D sampler_2d, ivec2 texel)
 {
-  const ivec2 texture_bounds = texture_size(sampler) - ivec2(1);
-  return texelFetch(sampler, clamp(texel, ivec2(0), texture_bounds), 0);
+  const ivec2 texture_bounds = texture_size(sampler_2d) - ivec2(1);
+  return texelFetch(sampler_2d, clamp(texel, ivec2(0), texture_bounds), 0);
 }
 
 /* A shorthand for 2D texelFetch with zero LOD and a fallback value for out-of-bound access. */
-vec4 texture_load(sampler2D sampler, ivec2 texel, vec4 fallback)
+vec4 texture_load(sampler2D sampler_2d, ivec2 texel, vec4 fallback)
 {
-  const ivec2 texture_bounds = texture_size(sampler) - ivec2(1);
+  const ivec2 texture_bounds = texture_size(sampler_2d) - ivec2(1);
   if (any(lessThan(texel, ivec2(0))) || any(greaterThan(texel, texture_bounds))) {
     return fallback;
   }
-  return texelFetch(sampler, texel, 0);
+  return texelFetch(sampler_2d, texel, 0);
 }
diff --git a/source/blender/draw/engines/eevee_next/eevee_defines.hh b/source/blender/draw/engines/eevee_next/eevee_defines.hh
index fca8737f661..55d3921e39b 100644
--- a/source/blender/draw/engines/eevee_next/eevee_defines.hh
+++ b/source/blender/draw/engines/eevee_next/eevee_defines.hh
@@ -9,7 +9,9 @@
  * dragging larger headers into the createInfo pipeline which would cause problems.
  */
 
-#pragma once
+#ifndef GPU_SHADER
+#  pragma once
+#endif
 
 /* Hierarchical Z down-sampling. */
 #define HIZ_MIP_COUNT 8
diff --git a/source/blender/draw/engines/eevee_next/shaders/eevee_depth_of_field_tiles_flatten_comp.glsl b/source/blender/draw/engines/eevee_next/shaders/eevee_depth_of_field_tiles_flatten_comp.glsl
index 88737ade386..33aefb7f76d 100644
--- a/source/blender/draw/engines/eevee_next/shaders/eevee_depth_of_field_tiles_flatten_comp.glsl
+++ b/source/blender/draw/engines/eevee_next/shaders/eevee_depth_of_field_tiles_flatten_comp.glsl
@@ -26,7 +26,7 @@ shared uint bg_min_coc;
 shared uint bg_max_coc;
 shared uint bg_min_intersectable_coc;
 
-const uint dof_tile_large_coc_uint = floatBitsToUint(dof_tile_large_coc);
+uint dof_tile_large_coc_uint = floatBitsToUint(dof_tile_large_coc);
 
 void main()
 {
diff --git a/source/blender/draw/engines/overlay/shaders/overlay_motion_path_line_vert.glsl b/source/blender/draw/engines/overlay/shaders/overlay_motion_path_line_vert.glsl
index 50c24de0838..331dcdf6519 100644
--- a/source/blender/draw/engines/overlay/shaders/overlay_motion_path_line_vert.glsl
+++ b/source/blender/draw/engines/overlay/shaders/overlay_motion_path_line_vert.glsl
@@ -13,8 +13,12 @@ vec2 proj(vec4 pos)
   return (0.5 * (pos.xy / pos.w) + 0.5) * sizeViewport.xy;
 }
 
-#define SET_INTENSITY(A, B, C, min, max) \
-  (((1.0 - (float(C - B) / float(C - A))) * (max - min)) + min)
+float calc_intensity(int segment_start, int segment_current, int segment_end, float min, float max)
+{
+  return ((1.0 - (float(segment_end - segment_current) / float(segment_end - segment_start))) *
+          (max - min)) +
+         min;
+}
 
 void main()
 {
@@ -39,10 +43,10 @@ void main()
     else {
       /* black - before frameCurrent */
       if (selected) {
-        intensity = SET_INTENSITY(frameStart, frame, frameCurrent, 0.25, 0.75);
+        intensity = calc_intensity(frameStart, frame, frameCurrent, 0.25, 0.75);
       }
       else {
-        intensity = SET_INTENSITY(frameStart, frame, frameCurrent, 0.68, 0.92);
+        intensity = calc_intensity(frameStart, frame, frameCurrent, 0.68, 0.92);
       }
       interp.color.rgb = mix(colorWire.rgb, blend_base, intensity);
     }
@@ -55,10 +59,10 @@ void main()
     else {
       /* blue - after frameCurrent */
       if (selected) {
-        intensity = SET_INTENSITY(frameCurrent, frame, frameEnd, 0.25, 0.75);
+        intensity = calc_intensity(frameCurrent, frame, frameEnd, 0.25, 0.75);
       }
       else {
-        intensity = SET_INTENSITY(frameCurrent, frame, frameEnd, 0.68, 0.92);
+        intensity = calc_intensity(frameCurrent, frame, frameEnd, 0.68, 0.92);
       }
 
       interp.color.rgb = mix(colorBonePose.rgb, blend_base, intensity);
diff --git a/source/blender/draw/engines/workbench/shaders/workbench_shadow_vert.glsl b/source/blender/draw/engines/workbench/shaders/workbench_shadow_vert.glsl
index a220434ec45..fa4d5ef4d96 100644
--- a/source/blender/draw/engines/workbench/shaders/workbench_shadow_vert.glsl
+++ b/source/blender/draw/engines/workbench/shaders/workbench_shadow_vert.glsl
@@ -5,5 +5,6 @@ void main()
 {
   vData.pos = pos;
   vData.frontPosition = point_object_to_ndc(pos);
-  vData.backPosition = point_object_to_ndc(pos + lightDirection * lightDistance);
+  vec3 back_pos = pos + lightDirection * lightDistance;
+  vData.backPosition = point_object_to_ndc(back_pos);
 }
diff --git a/source/blender/draw/intern/shaders/common_smaa_lib.glsl b/source/blender/draw/intern/shaders/common_smaa_lib.glsl
index 0c040c9acfe..1b8a8202166 100644
--- a/source/blender/draw/intern/shaders/common_smaa_lib.glsl
+++ b/source/blender/draw/intern/shaders/common_smaa_lib.glsl
@@ -569,7 +569,7 @@ SamplerState PointSampler
 #    define SMAAGather(tex, coord) tex.Gather(LinearSampler, coord, 0)
 #  endif
 #endif
-#if defined(SMAA_GLSL_3) || defined(SMAA_GLSL_4) || defined(GPU_METAL)
+#if defined(SMAA_GLSL_3) || defined(SMAA_GLSL_4) || defined(GPU_METAL) || defined(GPU_VULKAN)
 #  define SMAATexture2D(tex) sampler2D tex
 #  define SMAATexturePass2D(tex) tex
 #  define SMAASampleLevelZero(tex, coord) textureLod(tex, coord, 0.0)
@@ -583,8 +583,28 @@ SamplerState PointSampler
 #  define lerp(a, b, t) mix(a, b, t)
 #  define saturate(a) clamp(a, 0.0, 1.0)
 #  if defined(SMAA_GLSL_4)
-#    define mad(a, b, c) fma(a, b, c)
 #    define SMAAGather(tex, coord) textureGather(tex, coord)
+#  endif
+#  if defined(SMAA_GLSL_4)
+#    define mad(a, b, c) fma(a, b, c)
+#  elif defined(GPU_VULKAN)
+/* NOTE(Vulkan) mad macro doesn't work, define each override as work-around. */
+vec4 mad(vec4 a, vec4 b, vec4 c)
+{
+  return fma(a, b, c);
+}
+vec3 mad(vec3 a, vec3 b, vec3 c)
+{
+  return fma(a, b, c);
+}
+vec2 mad(vec2 a, vec2 b, vec2 c)
+{
+  return fma(a, b, c);
+}
+float mad(float a, float b, float c)
+{
+  return fma(a, b, c);
+}
 #  else
 #    define mad(a, b, c) (a * b + c)
 #  endif
diff --git a/source/blender/draw/intern/shaders/common_view_lib.glsl b/source/blender/draw/intern/shaders/common_view_lib.glsl
index d6985f86ddc..53ef194b4f5 100644
--- a/source/blender/draw/intern/shaders/common_view_lib.glsl
+++ b/source/blender/draw/intern/shaders/common_view_lib.glsl
@@ -234,12 +234,28 @@ uniform mat4 ModelMatrixInverse;
   (ProjectionMatrix * (ViewMatrix * vec4((ModelMatrix * vec4(p, 1.0)).xyz, 1.0)))
 #define point_object_to_view(p) ((ViewMatrix * vec4((ModelMatri

@@ Diff output truncated at 10240 characters. @@



More information about the Bf-blender-cvs mailing list