[Bf-blender-cvs] [26dc50ac341] temp-texture-painting-gpu: SculptPaint: Use GPU shaders.

Jeroen Bakker noreply at git.blender.org
Fri Sep 30 15:50:29 CEST 2022


Commit: 26dc50ac3414a54b13150b8d31ab08bcd71a72a3
Author: Jeroen Bakker
Date:   Tue Sep 27 15:34:10 2022 +0200
Branches: temp-texture-painting-gpu
https://developer.blender.org/rB26dc50ac3414a54b13150b8d31ab08bcd71a72a3

SculptPaint: Use GPU shaders.

This is WIP/PoC patch to check how we could increase code reusability between
CPU and GPU versions.

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

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

M	source/blender/blenkernel/BKE_pbvh_pixels.hh
M	source/blender/editors/sculpt_paint/sculpt_paint_image.cc
M	source/blender/gpu/CMakeLists.txt
A	source/blender/gpu/GPU_sculpt_shader_shared.h
A	source/blender/gpu/shaders/sculpt_paint/infos/sculpt_paint_image_info.hh
A	source/blender/gpu/shaders/sculpt_paint/sculpt_paint_image_comp.glsl

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

diff --git a/source/blender/blenkernel/BKE_pbvh_pixels.hh b/source/blender/blenkernel/BKE_pbvh_pixels.hh
index ad8eca2b36f..da6dc368536 100644
--- a/source/blender/blenkernel/BKE_pbvh_pixels.hh
+++ b/source/blender/blenkernel/BKE_pbvh_pixels.hh
@@ -16,28 +16,9 @@
 
 #include "IMB_imbuf_types.h"
 
-namespace blender::bke::pbvh::pixels {
+#include "GPU_sculpt_shader_shared.h"
 
-struct TrianglePaintInput {
-  int3 vert_indices;
-  /**
-   * Delta barycentric coordinates between 2 neighboring UV's in the U direction.
-   *
-   * Only the first two coordinates are stored. The third should be recalculated
-   */
-  float2 delta_barycentric_coord_u;
-
-  /**
-   * Initially only the vert indices are known.
-   *
-   * delta_barycentric_coord_u is initialized in a later stage as it requires image tile
-   * dimensions.
-   */
-  TrianglePaintInput(const int3 vert_indices)
-      : vert_indices(vert_indices), delta_barycentric_coord_u(0.0f, 0.0f)
-  {
-  }
-};
+namespace blender::bke::pbvh::pixels {
 
 /**
  * Data shared between pixels that belong to the same triangle.
@@ -52,7 +33,10 @@ struct Triangles {
  public:
   void append(const int3 vert_indices)
   {
-    this->paint_input.append(TrianglePaintInput(vert_indices));
+    TrianglePaintInput triangle;
+    triangle.vert_indices = int4(vert_indices.x, vert_indices.y, vert_indices.z, 0.0f);
+    triangle.delta_barycentric_coord_u = float2(0.0f);
+    this->paint_input.append(triangle);
   }
 
   TrianglePaintInput &get_paint_input(const int index)
diff --git a/source/blender/editors/sculpt_paint/sculpt_paint_image.cc b/source/blender/editors/sculpt_paint/sculpt_paint_image.cc
index 8a3a3fe7adc..906ab50d51d 100644
--- a/source/blender/editors/sculpt_paint/sculpt_paint_image.cc
+++ b/source/blender/editors/sculpt_paint/sculpt_paint_image.cc
@@ -255,7 +255,7 @@ template<typename ImageBuffer> class PaintingKernel {
   float3 init_pixel_pos(const TrianglePaintInput &triangle,
                         const float2 &barycentric_weights) const
   {
-    const int3 &vert_indices = triangle.vert_indices;
+    const int4 &vert_indices = triangle.vert_indices;
     float3 result;
     const float3 barycentric(barycentric_weights.x,
                              barycentric_weights.y,
diff --git a/source/blender/gpu/CMakeLists.txt b/source/blender/gpu/CMakeLists.txt
index a322922e86e..a65fee13ca4 100644
--- a/source/blender/gpu/CMakeLists.txt
+++ b/source/blender/gpu/CMakeLists.txt
@@ -479,6 +479,9 @@ set(GLSL_SRC
   shaders/gpu_shader_cfg_world_clip_lib.glsl
   shaders/gpu_shader_colorspace_lib.glsl
 
+  GPU_sculpt_shader_shared.h
+  shaders/sculpt_paint/sculpt_paint_image_comp.glsl
+
   GPU_shader_shared_utils.h
 )
 
@@ -634,6 +637,8 @@ set(SRC_SHADER_CREATE_INFOS
   shaders/compositor/infos/compositor_split_viewer_info.hh
   shaders/compositor/infos/compositor_symmetric_blur_info.hh
   shaders/compositor/infos/compositor_symmetric_separable_blur_info.hh
+
+  shaders/sculpt_paint/infos/sculpt_paint_image_info.hh
 )
 
 set(SRC_SHADER_CREATE_INFOS_MTL
diff --git a/source/blender/gpu/GPU_sculpt_shader_shared.h b/source/blender/gpu/GPU_sculpt_shader_shared.h
new file mode 100644
index 00000000000..ac767b69826
--- /dev/null
+++ b/source/blender/gpu/GPU_sculpt_shader_shared.h
@@ -0,0 +1,42 @@
+/* SPDX-License-Identifier: GPL-2.0-or-later
+ * Copyright 2022 Blender Foundation. All rights reserved. */
+
+/** \file
+ * \ingroup gpu
+ */
+
+#ifndef USE_GPU_SHADER_CREATE_INFO
+#  include "GPU_shader_shared_utils.h"
+#endif
+
+struct TrianglePaintInput {
+  int4 vert_indices;
+  /**
+   * Delta barycentric coordinates between 2 neighboring UV's in the U direction.
+   *
+   * Only the first two coordinates are stored. The third should be recalculated on the fly.
+   */
+  float2 delta_barycentric_coord_u;
+  float2 _pad;
+};
+BLI_STATIC_ASSERT_ALIGN(TrianglePaintInput, 16)
+
+struct PackedPixelRow {
+  /** Barycentric coordinate of the first pixel. */
+  float2 start_barycentric_coord;
+  /** Image coordinate starting of the first pixel. First 16 bits is u, other 16 bits is v. */
+  uint start_image_coordinate;
+
+  /**
+   * 16 bits: Number of sequential pixels encoded in this package.
+   * 16 bits: Reference to the pbvh triangle index.
+   */
+  uint encoded;
+};
+
+#define PIXEL_ROW_START_IMAGE_COORD(row) \
+  ivec2((row.start_image_coordinate & 0xffff0000) >> 16, row.start_image_coordinate & 0xffff)
+#define PIXEL_ROW_LEN(row) uint((row.encoded & 0xffff0000) >> 16)
+#define PIXEL_ROW_PRIM_INDEX(row) uint(row.encoded & 0xffff);
+
+BLI_STATIC_ASSERT_ALIGN(TrianglePaintInput, 16)
diff --git a/source/blender/gpu/shaders/sculpt_paint/infos/sculpt_paint_image_info.hh b/source/blender/gpu/shaders/sculpt_paint/infos/sculpt_paint_image_info.hh
new file mode 100644
index 00000000000..bc1f3b4ebf0
--- /dev/null
+++ b/source/blender/gpu/shaders/sculpt_paint/infos/sculpt_paint_image_info.hh
@@ -0,0 +1,17 @@
+/* SPDX-License-Identifier: GPL-2.0-or-later
+ * Copyright 2022 Blender Foundation. All rights reserved. */
+
+/** \file
+ * \ingroup gpu
+ */
+
+#include "gpu_shader_create_info.hh"
+
+GPU_SHADER_CREATE_INFO(sculpt_paint_image_compute)
+    .local_group_size(1, 1, 1)
+    .storage_buf(0, Qualifier::READ, "PackedPixelRow", "pixel_row_buf[]")
+    .storage_buf(1, Qualifier::READ, "TrianglePaintInput", "paint_input[]")
+    .image(0, GPU_RGBA32F, Qualifier::READ_WRITE, ImageType::FLOAT_2D, "out_img")
+    .compute_source("sculpt_paint_image_comp.glsl")
+    .typedef_source("GPU_sculpt_shader_shared.h")
+    .do_static_compilation(true);
diff --git a/source/blender/gpu/shaders/sculpt_paint/sculpt_paint_image_comp.glsl b/source/blender/gpu/shaders/sculpt_paint/sculpt_paint_image_comp.glsl
new file mode 100644
index 00000000000..9954967c87e
--- /dev/null
+++ b/source/blender/gpu/shaders/sculpt_paint/sculpt_paint_image_comp.glsl
@@ -0,0 +1,12 @@
+void main()
+{
+  PackedPixelRow row = pixel_row_buf[gl_GlobalInvocationID.x];
+  ivec2 image_coord = PIXEL_ROW_START_IMAGE_COORD(row);
+
+  uint row_len = PIXEL_ROW_LEN(row);
+
+  for (int i = 0; i < row_len; i++) {
+
+    imageStore(out_img, image_coord + ivec2(i, 0), float4(1.0, 0.0, 1.0, 1.0));
+  }
+}
\ No newline at end of file



More information about the Bf-blender-cvs mailing list