From 96ca6ca967d41a2adfe1316d9f89d733ebf21a14 Mon Sep 17 00:00:00 2001 From: Eugene Kuznetsov Date: Wed, 27 Dec 2023 00:40:36 -0800 Subject: [PATCH 1/6] Using a compute shader to generate curve vertex index arrays --- .../draw/intern/draw_cache_impl_curves.cc | 63 +------------------ source/blender/gpu/CMakeLists.txt | 4 ++ source/blender/gpu/GPU_index_buffer.h | 4 ++ source/blender/gpu/GPU_shader_builtin.h | 9 ++- source/blender/gpu/intern/gpu_index_buffer.cc | 49 +++++++++++++++ .../blender/gpu/intern/gpu_shader_builtin.cc | 6 ++ .../shaders/gpu_compute_2d_array_lines.glsl | 26 ++++++++ .../shaders/gpu_compute_2d_array_points.glsl | 22 +++++++ .../shaders/gpu_compute_2d_array_tris.glsl | 26 ++++++++ .../shaders/infos/gpu_compute_shaders_info.hh | 34 ++++++++++ 10 files changed, 182 insertions(+), 61 deletions(-) create mode 100644 source/blender/gpu/shaders/gpu_compute_2d_array_lines.glsl create mode 100644 source/blender/gpu/shaders/gpu_compute_2d_array_points.glsl create mode 100644 source/blender/gpu/shaders/gpu_compute_2d_array_tris.glsl create mode 100644 source/blender/gpu/shaders/infos/gpu_compute_shaders_info.hh diff --git a/source/blender/draw/intern/draw_cache_impl_curves.cc b/source/blender/draw/intern/draw_cache_impl_curves.cc index 4f055696f04..079c100f446 100644 --- a/source/blender/draw/intern/draw_cache_impl_curves.cc +++ b/source/blender/draw/intern/draw_cache_impl_curves.cc @@ -42,7 +42,6 @@ #include "draw_cache_impl.hh" /* own include */ #include "draw_cache_inline.hh" #include "draw_curves_private.hh" /* own include */ -#include "draw_shader.hh" namespace blender::draw { @@ -417,57 +416,6 @@ static void curves_batch_cache_ensure_procedural_final_points(CurvesEvalCache &c cache.final[subdiv].strands_res * cache.strands_len); } -static void curves_batch_cache_fill_segments_indices(GPUPrimType prim_type, - const bke::CurvesGeometry &curves, - const int res, - GPUIndexBufBuilder &elb) -{ - switch (prim_type) { - /* Populate curves using compressed restart-compatible types. */ - case GPU_PRIM_LINE_STRIP: - case GPU_PRIM_TRI_STRIP: { - uint curr_point = 0; - for ([[maybe_unused]] const int i : IndexRange(curves.curves_num())) { - for (int k = 0; k < res; k++) { - GPU_indexbuf_add_generic_vert(&elb, curr_point++); - } - GPU_indexbuf_add_primitive_restart(&elb); - } - break; - } - /* Generate curves using independent line segments. */ - case GPU_PRIM_LINES: { - uint curr_point = 0; - for ([[maybe_unused]] const int i : IndexRange(curves.curves_num())) { - for (int k = 0; k < res / 2; k++) { - GPU_indexbuf_add_line_verts(&elb, curr_point, curr_point + 1); - curr_point++; - } - /* Skip to next primitive base index. */ - curr_point++; - } - break; - } - /* Generate curves using independent two-triangle segments. */ - case GPU_PRIM_TRIS: { - uint curr_point = 0; - for ([[maybe_unused]] const int i : IndexRange(curves.curves_num())) { - for (int k = 0; k < res / 6; k++) { - GPU_indexbuf_add_tri_verts(&elb, curr_point, curr_point + 1, curr_point + 2); - GPU_indexbuf_add_tri_verts(&elb, curr_point + 1, curr_point + 3, curr_point + 2); - curr_point += 2; - } - /* Skip to next primitive base index. */ - curr_point += 2; - } - break; - } - default: - BLI_assert_unreachable(); - break; - } -} - static void curves_batch_cache_ensure_procedural_indices(const bke::CurvesGeometry &curves, CurvesEvalCache &cache, const int thickness_res, @@ -483,13 +431,11 @@ static void curves_batch_cache_ensure_procedural_indices(const bke::CurvesGeomet * NOTE: Metal backend uses non-restart prim types for optimal HW performance. */ bool use_strip_prims = (GPU_backend_get_type() != GPU_BACKEND_METAL); int verts_per_curve; - int element_count; GPUPrimType prim_type; if (use_strip_prims) { /* +1 for primitive restart */ verts_per_curve = cache.final[subdiv].strands_res * thickness_res; - element_count = (verts_per_curve + 1) * cache.strands_len; prim_type = (thickness_res == 1) ? GPU_PRIM_LINE_STRIP : GPU_PRIM_TRI_STRIP; } else { @@ -497,7 +443,6 @@ static void curves_batch_cache_ensure_procedural_indices(const bke::CurvesGeomet prim_type = (thickness_res == 1) ? GPU_PRIM_LINES : GPU_PRIM_TRIS; int verts_per_segment = ((prim_type == GPU_PRIM_LINES) ? 2 : 6); verts_per_curve = (cache.final[subdiv].strands_res - 1) * verts_per_segment; - element_count = verts_per_curve * cache.strands_len; } static GPUVertFormat format = {0}; @@ -509,13 +454,11 @@ static void curves_batch_cache_ensure_procedural_indices(const bke::CurvesGeomet GPUVertBuf *vbo = GPU_vertbuf_create_with_format(&format); GPU_vertbuf_data_alloc(vbo, 1); - GPUIndexBufBuilder elb; - GPU_indexbuf_init_ex(&elb, prim_type, element_count, element_count); - - curves_batch_cache_fill_segments_indices(prim_type, curves, verts_per_curve, elb); + GPUIndexBuf *ib = GPU_indexbuf_build_curves_on_device( + prim_type, curves.curves_num(), verts_per_curve); cache.final[subdiv].proc_hairs[thickness_res - 1] = GPU_batch_create_ex( - prim_type, vbo, GPU_indexbuf_build(&elb), GPU_BATCH_OWNS_VBO | GPU_BATCH_OWNS_INDEX); + prim_type, vbo, ib, GPU_BATCH_OWNS_VBO | GPU_BATCH_OWNS_INDEX); } static bool curves_ensure_attributes(const Curves &curves, diff --git a/source/blender/gpu/CMakeLists.txt b/source/blender/gpu/CMakeLists.txt index 6b88866de4e..485e3c2c965 100644 --- a/source/blender/gpu/CMakeLists.txt +++ b/source/blender/gpu/CMakeLists.txt @@ -590,6 +590,9 @@ set(GLSL_SRC shaders/gpu_shader_cfg_world_clip_lib.glsl shaders/gpu_shader_colorspace_lib.glsl + shaders/gpu_compute_2d_array_points.glsl + shaders/gpu_compute_2d_array_lines.glsl + shaders/gpu_compute_2d_array_tris.glsl GPU_shader_shared_utils.h ) @@ -734,6 +737,7 @@ set(SRC_SHADER_CREATE_INFOS ../draw/intern/shaders/draw_view_info.hh shaders/infos/gpu_clip_planes_info.hh + shaders/infos/gpu_compute_shaders_info.hh shaders/infos/gpu_shader_2D_area_borders_info.hh shaders/infos/gpu_shader_2D_checker_info.hh shaders/infos/gpu_shader_2D_diag_stripes_info.hh diff --git a/source/blender/gpu/GPU_index_buffer.h b/source/blender/gpu/GPU_index_buffer.h index ff22c653ab9..d8d33ddc841 100644 --- a/source/blender/gpu/GPU_index_buffer.h +++ b/source/blender/gpu/GPU_index_buffer.h @@ -72,6 +72,10 @@ void GPU_indexbuf_build_in_place(GPUIndexBufBuilder *, GPUIndexBuf *); void GPU_indexbuf_bind_as_ssbo(GPUIndexBuf *elem, int binding); +GPUIndexBuf *GPU_indexbuf_build_curves_on_device(GPUPrimType prim_type, + uint curves_num, + uint verts_per_curve); + /* Upload data to the GPU (if not built on the device) and bind the buffer to its default target. */ void GPU_indexbuf_use(GPUIndexBuf *elem); diff --git a/source/blender/gpu/GPU_shader_builtin.h b/source/blender/gpu/GPU_shader_builtin.h index d369d7a9017..ed65b4bc2f5 100644 --- a/source/blender/gpu/GPU_shader_builtin.h +++ b/source/blender/gpu/GPU_shader_builtin.h @@ -127,8 +127,15 @@ typedef enum eGPUBuiltinShader { * \param pos: in vec3 */ GPU_SHADER_3D_IMAGE_COLOR, + + /** + * Compute shaders to generate 2d index buffers (mainly for curve drawing). + * */ + GPU_SHADER_INDEXBUF_POINTS, + GPU_SHADER_INDEXBUF_LINES, + GPU_SHADER_INDEXBUF_TRIS, } eGPUBuiltinShader; -#define GPU_SHADER_BUILTIN_LEN (GPU_SHADER_3D_IMAGE_COLOR + 1) +#define GPU_SHADER_BUILTIN_LEN (GPU_SHADER_INDEXBUF_TRIS + 1) /** Support multiple configurations. */ typedef enum eGPUShaderConfig { diff --git a/source/blender/gpu/intern/gpu_index_buffer.cc b/source/blender/gpu/intern/gpu_index_buffer.cc index 162f7cc3f20..19871fc7115 100644 --- a/source/blender/gpu/intern/gpu_index_buffer.cc +++ b/source/blender/gpu/intern/gpu_index_buffer.cc @@ -17,6 +17,8 @@ #include "gpu_index_buffer_private.hh" +#include "GPU_capabilities.h" +#include "GPU_compute.h" #include "GPU_platform.h" #include /* For `min/max`. */ @@ -241,6 +243,53 @@ void GPU_indexbuf_set_tri_restart(GPUIndexBufBuilder *builder, uint elem) builder->uses_restart_indices = true; } +GPUIndexBuf *GPU_indexbuf_build_curves_on_device(GPUPrimType prim_type, + uint curves_num, + uint verts_per_curve) +{ + uint64_t dispatch_x_dim = verts_per_curve; + if (prim_type == GPU_PRIM_LINE_STRIP || prim_type == GPU_PRIM_TRI_STRIP) { + dispatch_x_dim += 1; + } + uint64_t grid_x, grid_y, grid_z; + uint64_t max_grid_x = GPU_max_work_group_count(0), max_grid_y = GPU_max_work_group_count(1), + max_grid_z = GPU_max_work_group_count(2); + grid_x = min_uu(max_grid_x, (dispatch_x_dim + 15) / 16); + grid_y = (curves_num + 15) / 16; + if (grid_y <= max_grid_y) { + grid_z = 1; + } + else { + grid_y = grid_z = uint64_t(ceil(sqrt(double(grid_y)))); + grid_y = min_uu(grid_y, max_grid_y); + grid_z = min_uu(grid_z, max_grid_z); + } + GPUIndexBuf *ib; + bool tris = (prim_type == GPU_PRIM_TRIS); + bool lines = (prim_type == GPU_PRIM_LINES); + GPUShader *shader = GPU_shader_get_builtin_shader( + tris ? GPU_SHADER_INDEXBUF_TRIS : + (lines ? GPU_SHADER_INDEXBUF_LINES : GPU_SHADER_INDEXBUF_POINTS)); + GPU_shader_bind(shader); + GPU_memory_barrier(GPU_BARRIER_BUFFER_UPDATE); + ib = GPU_indexbuf_build_on_device(curves_num * dispatch_x_dim); + int resolution; + if (tris) + resolution = 6; + else if (lines) + resolution = 2; + else + resolution = 1; + GPU_shader_uniform_1i(shader, "elements_per_curve", dispatch_x_dim / resolution); + GPU_shader_uniform_1i(shader, "ncurves", curves_num); + GPU_indexbuf_bind_as_ssbo(ib, GPU_shader_get_ssbo_binding(shader, "out_indices")); + GPU_compute_dispatch(shader, grid_x, grid_y, grid_z); + + GPU_memory_barrier(GPU_BARRIER_BUFFER_UPDATE); + GPU_shader_unbind(); + return ib; +} + /** \} */ /* -------------------------------------------------------------------- */ diff --git a/source/blender/gpu/intern/gpu_shader_builtin.cc b/source/blender/gpu/intern/gpu_shader_builtin.cc index 2f7f78cb428..01d2fcfbfa8 100644 --- a/source/blender/gpu/intern/gpu_shader_builtin.cc +++ b/source/blender/gpu/intern/gpu_shader_builtin.cc @@ -87,6 +87,12 @@ static const char *builtin_shader_create_info_name(eGPUBuiltinShader shader) return "gpu_shader_2D_nodelink_inst"; case GPU_SHADER_GPENCIL_STROKE: return "gpu_shader_gpencil_stroke"; + case GPU_SHADER_INDEXBUF_POINTS: + return "gpu_compute_2d_array_points"; + case GPU_SHADER_INDEXBUF_LINES: + return "gpu_compute_2d_array_lines"; + case GPU_SHADER_INDEXBUF_TRIS: + return "gpu_compute_2d_array_tris"; default: BLI_assert_unreachable(); return ""; diff --git a/source/blender/gpu/shaders/gpu_compute_2d_array_lines.glsl b/source/blender/gpu/shaders/gpu_compute_2d_array_lines.glsl new file mode 100644 index 00000000000..0cddbafb5be --- /dev/null +++ b/source/blender/gpu/shaders/gpu_compute_2d_array_lines.glsl @@ -0,0 +1,26 @@ +/* SPDX-FileCopyrightText: 2023 Blender Authors + * + * SPDX-License-Identifier: GPL-2.0-or-later */ + +/** + * Constructs a 2D array index buffer with 'ncurves' rows and 'elements_per_curve*2' + * columns. Each row contains 'elements_per_curve' pairs of indexes. + * e.g., for elements_per_curve=32, first two rows are + * 0 1 1 2 2 3 ... 31 32 + * 33 34 34 35 35 36 .. 64 65 + * The index buffer can then be used to draw 'ncurves' curves with 'elements_per_curve+1' + * vertexes each, using GL_LINES primitives. Intended to be used if GL_LINE_STRIP + * primitives can't be used for some reason. + */ +void main() +{ + uvec3 gid = gl_GlobalInvocationID; + uvec3 nthreads = gl_NumWorkGroups * gl_WorkGroupSize; + for (uint y = gid.y + gid.z * nthreads.y; y < ncurves; y += nthreads.y * nthreads.z) { + for (uint x = gid.x; x < elements_per_curve; x += nthreads.x) { + uint store_index = (x + y * elements_per_curve) * 2; + out_indices[store_index] = x + y * (elements_per_curve + 1); + out_indices[store_index + 1] = x + y * (elements_per_curve + 1) + 1; + } + } +} diff --git a/source/blender/gpu/shaders/gpu_compute_2d_array_points.glsl b/source/blender/gpu/shaders/gpu_compute_2d_array_points.glsl new file mode 100644 index 00000000000..707e124f22e --- /dev/null +++ b/source/blender/gpu/shaders/gpu_compute_2d_array_points.glsl @@ -0,0 +1,22 @@ +/* SPDX-FileCopyrightText: 2023 Blender Authors + * + * SPDX-License-Identifier: GPL-2.0-or-later */ + +/** + * Constructs a simple 2D array index buffer, with 'ncurves' rows and 'elements_per_curve' + * columns. Each row contains 'elements_per_curve-1' indexes and a restart index. + * The index buffer can then be used to draw either 'ncurves' lines with 'elements_per_curve-1' + * vertexes each, or 'ncurves' triangle strips with 'elements_per_curve-3' triangles each. + */ +void main() +{ + uvec3 gid = gl_GlobalInvocationID; + uvec3 nthreads = gl_NumWorkGroups * gl_WorkGroupSize; + for (uint y = gid.y + gid.z * nthreads.y; y < ncurves; y += nthreads.y * nthreads.z) { + for (uint x = gid.x; x < elements_per_curve; x += nthreads.x) { + uint store_index = x + y * elements_per_curve; + out_indices[store_index] = (x + 1 < elements_per_curve) ? x + y * (elements_per_curve - 1) : + 0xFFFFFFFF; + } + } +} diff --git a/source/blender/gpu/shaders/gpu_compute_2d_array_tris.glsl b/source/blender/gpu/shaders/gpu_compute_2d_array_tris.glsl new file mode 100644 index 00000000000..9a8d330f297 --- /dev/null +++ b/source/blender/gpu/shaders/gpu_compute_2d_array_tris.glsl @@ -0,0 +1,26 @@ +/* SPDX-FileCopyrightText: 2023 Blender Authors + * + * SPDX-License-Identifier: GPL-2.0-or-later */ + +/** + * Constructs a 2D array index buffer, with 'ncurves' rows and 'elements_per_curve*6' columns. + * The index buffer can be used to draw 'ncurves' triangle strips with 'elements_per_curve*2' + * triangles each, using GL_TRIANGLES primitives. Intended to be used if GL_TRIANGLE_STRIP + * primitives can't be used for some reason. + */ +void main() +{ + uvec3 gid = gl_GlobalInvocationID; + uvec3 nthreads = gl_NumWorkGroups * gl_WorkGroupSize; + for (uint y = gid.y + gid.z * nthreads.y; y < ncurves; y += nthreads.y * nthreads.z) + for (uint x = gid.x; x < elements_per_curve; x += nthreads.x) { + uint store_index = (x + y * elements_per_curve) * 6; + uint t = x + y * (elements_per_curve * 2 + 2); + out_indices[store_index + 0] = t; + out_indices[store_index + 1] = t + 1; + out_indices[store_index + 2] = t + 2; + out_indices[store_index + 3] = t + 1; + out_indices[store_index + 4] = t + 3; + out_indices[store_index + 5] = t + 2; + } +} diff --git a/source/blender/gpu/shaders/infos/gpu_compute_shaders_info.hh b/source/blender/gpu/shaders/infos/gpu_compute_shaders_info.hh new file mode 100644 index 00000000000..51cd6b1f1ad --- /dev/null +++ b/source/blender/gpu/shaders/infos/gpu_compute_shaders_info.hh @@ -0,0 +1,34 @@ +/* SPDX-FileCopyrightText: 2022 Blender Authors + * + * SPDX-License-Identifier: GPL-2.0-or-later */ + +/** \file + * \ingroup gpu + */ + +#include "gpu_interface_info.hh" +#include "gpu_shader_create_info.hh" + +GPU_SHADER_CREATE_INFO(gpu_compute_2d_array_points) + .local_group_size(16, 16, 1) + .push_constant(Type::INT, "elements_per_curve") + .push_constant(Type::INT, "ncurves") + .storage_buf(0, Qualifier::WRITE, "uint", "out_indices[]") + .compute_source("gpu_compute_2d_array_points.glsl") + .do_static_compilation(true); + +GPU_SHADER_CREATE_INFO(gpu_compute_2d_array_lines) + .local_group_size(16, 16, 1) + .push_constant(Type::INT, "elements_per_curve") + .push_constant(Type::INT, "ncurves") + .storage_buf(0, Qualifier::WRITE, "uint", "out_indices[]") + .compute_source("gpu_compute_2d_array_lines.glsl") + .do_static_compilation(true); + +GPU_SHADER_CREATE_INFO(gpu_compute_2d_array_tris) + .local_group_size(16, 16, 1) + .push_constant(Type::INT, "elements_per_curve") + .push_constant(Type::INT, "ncurves") + .storage_buf(0, Qualifier::WRITE, "uint", "out_indices[]") + .compute_source("gpu_compute_2d_array_tris.glsl") + .do_static_compilation(true); -- 2.30.2 From f662fffe452a7cb935ea279f9bf253d0922cbfd4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cle=CC=81ment=20Foucault?= Date: Sun, 25 Feb 2024 15:53:33 +0100 Subject: [PATCH 2/6] Fix compilation on Metal and avoid mixed int type casts --- .../shaders/gpu_compute_2d_array_lines.glsl | 15 +++++++------- .../shaders/gpu_compute_2d_array_points.glsl | 15 +++++++------- .../shaders/gpu_compute_2d_array_tris.glsl | 20 +++++++++---------- 3 files changed, 26 insertions(+), 24 deletions(-) diff --git a/source/blender/gpu/shaders/gpu_compute_2d_array_lines.glsl b/source/blender/gpu/shaders/gpu_compute_2d_array_lines.glsl index 0cddbafb5be..71c3e596756 100644 --- a/source/blender/gpu/shaders/gpu_compute_2d_array_lines.glsl +++ b/source/blender/gpu/shaders/gpu_compute_2d_array_lines.glsl @@ -14,13 +14,14 @@ */ void main() { - uvec3 gid = gl_GlobalInvocationID; - uvec3 nthreads = gl_NumWorkGroups * gl_WorkGroupSize; - for (uint y = gid.y + gid.z * nthreads.y; y < ncurves; y += nthreads.y * nthreads.z) { - for (uint x = gid.x; x < elements_per_curve; x += nthreads.x) { - uint store_index = (x + y * elements_per_curve) * 2; - out_indices[store_index] = x + y * (elements_per_curve + 1); - out_indices[store_index + 1] = x + y * (elements_per_curve + 1) + 1; + ivec3 gid = ivec3(gl_GlobalInvocationID); + ivec3 nthreads = ivec3(gl_NumWorkGroups) * ivec3(gl_WorkGroupSize); + for (int y = gid.y + gid.z * nthreads.y; y < ncurves; y += nthreads.y * nthreads.z) { + for (int x = gid.x; x < elements_per_curve; x += nthreads.x) { + int store_index = (x + y * elements_per_curve) * 2; + uint t = uint(x + y * (elements_per_curve + 1)); + out_indices[store_index] = t; + out_indices[store_index + 1] = t + 1u; } } } diff --git a/source/blender/gpu/shaders/gpu_compute_2d_array_points.glsl b/source/blender/gpu/shaders/gpu_compute_2d_array_points.glsl index 707e124f22e..5cdf4ad257b 100644 --- a/source/blender/gpu/shaders/gpu_compute_2d_array_points.glsl +++ b/source/blender/gpu/shaders/gpu_compute_2d_array_points.glsl @@ -10,13 +10,14 @@ */ void main() { - uvec3 gid = gl_GlobalInvocationID; - uvec3 nthreads = gl_NumWorkGroups * gl_WorkGroupSize; - for (uint y = gid.y + gid.z * nthreads.y; y < ncurves; y += nthreads.y * nthreads.z) { - for (uint x = gid.x; x < elements_per_curve; x += nthreads.x) { - uint store_index = x + y * elements_per_curve; - out_indices[store_index] = (x + 1 < elements_per_curve) ? x + y * (elements_per_curve - 1) : - 0xFFFFFFFF; + ivec3 gid = ivec3(gl_GlobalInvocationID); + ivec3 nthreads = ivec3(gl_NumWorkGroups) * ivec3(gl_WorkGroupSize); + for (int y = gid.y + gid.z * nthreads.y; y < ncurves; y += nthreads.y * nthreads.z) { + for (int x = gid.x; x < elements_per_curve; x += nthreads.x) { + int store_index = x + y * elements_per_curve; + out_indices[store_index] = (x + 1 < elements_per_curve) ? + uint(x + y * (elements_per_curve - 1)) : + 0xFFFFFFFFu; } } } diff --git a/source/blender/gpu/shaders/gpu_compute_2d_array_tris.glsl b/source/blender/gpu/shaders/gpu_compute_2d_array_tris.glsl index 9a8d330f297..bd230cd00ac 100644 --- a/source/blender/gpu/shaders/gpu_compute_2d_array_tris.glsl +++ b/source/blender/gpu/shaders/gpu_compute_2d_array_tris.glsl @@ -10,17 +10,17 @@ */ void main() { - uvec3 gid = gl_GlobalInvocationID; - uvec3 nthreads = gl_NumWorkGroups * gl_WorkGroupSize; - for (uint y = gid.y + gid.z * nthreads.y; y < ncurves; y += nthreads.y * nthreads.z) - for (uint x = gid.x; x < elements_per_curve; x += nthreads.x) { - uint store_index = (x + y * elements_per_curve) * 6; + ivec3 gid = ivec3(gl_GlobalInvocationID); + ivec3 nthreads = ivec3(gl_NumWorkGroups) * ivec3(gl_WorkGroupSize); + for (int y = gid.y + gid.z * nthreads.y; y < ncurves; y += nthreads.y * nthreads.z) + for (int x = gid.x; x < elements_per_curve; x += nthreads.x) { + int store_index = (x + y * elements_per_curve) * 6; uint t = x + y * (elements_per_curve * 2 + 2); out_indices[store_index + 0] = t; - out_indices[store_index + 1] = t + 1; - out_indices[store_index + 2] = t + 2; - out_indices[store_index + 3] = t + 1; - out_indices[store_index + 4] = t + 3; - out_indices[store_index + 5] = t + 2; + out_indices[store_index + 1] = t + 1u; + out_indices[store_index + 2] = t + 2u; + out_indices[store_index + 3] = t + 1u; + out_indices[store_index + 4] = t + 3u; + out_indices[store_index + 5] = t + 2u; } } -- 2.30.2 From 8548a04868b3b6d23e82bddeb32c4c192fe6df69 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cle=CC=81ment=20Foucault?= Date: Sun, 25 Feb 2024 15:54:00 +0100 Subject: [PATCH 3/6] Cleanups --- source/blender/gpu/GPU_shader_builtin.h | 14 ++++++-------- source/blender/gpu/intern/gpu_index_buffer.cc | 9 ++++++--- 2 files changed, 12 insertions(+), 11 deletions(-) diff --git a/source/blender/gpu/GPU_shader_builtin.h b/source/blender/gpu/GPU_shader_builtin.h index ed65b4bc2f5..2b3cfbe7e1e 100644 --- a/source/blender/gpu/GPU_shader_builtin.h +++ b/source/blender/gpu/GPU_shader_builtin.h @@ -79,6 +79,11 @@ typedef enum eGPUBuiltinShader { /** Draw wide lines with uniform color. Has an additional clip plane parameter. */ GPU_SHADER_3D_POLYLINE_CLIPPED_UNIFORM_COLOR, + /** Compute shaders to generate 2d index buffers (mainly for curve drawing). */ + GPU_SHADER_INDEXBUF_POINTS, + GPU_SHADER_INDEXBUF_LINES, + GPU_SHADER_INDEXBUF_TRIS, + /** * ----------------------- Shaders exposed through pyGPU module ----------------------- * @@ -127,15 +132,8 @@ typedef enum eGPUBuiltinShader { * \param pos: in vec3 */ GPU_SHADER_3D_IMAGE_COLOR, - - /** - * Compute shaders to generate 2d index buffers (mainly for curve drawing). - * */ - GPU_SHADER_INDEXBUF_POINTS, - GPU_SHADER_INDEXBUF_LINES, - GPU_SHADER_INDEXBUF_TRIS, } eGPUBuiltinShader; -#define GPU_SHADER_BUILTIN_LEN (GPU_SHADER_INDEXBUF_TRIS + 1) +#define GPU_SHADER_BUILTIN_LEN (GPU_SHADER_3D_IMAGE_COLOR + 1) /** Support multiple configurations. */ typedef enum eGPUShaderConfig { diff --git a/source/blender/gpu/intern/gpu_index_buffer.cc b/source/blender/gpu/intern/gpu_index_buffer.cc index 19871fc7115..81c32ebb275 100644 --- a/source/blender/gpu/intern/gpu_index_buffer.cc +++ b/source/blender/gpu/intern/gpu_index_buffer.cc @@ -274,12 +274,15 @@ GPUIndexBuf *GPU_indexbuf_build_curves_on_device(GPUPrimType prim_type, GPU_memory_barrier(GPU_BARRIER_BUFFER_UPDATE); ib = GPU_indexbuf_build_on_device(curves_num * dispatch_x_dim); int resolution; - if (tris) + if (tris) { resolution = 6; - else if (lines) + } + else if (lines) { resolution = 2; - else + } + else { resolution = 1; + } GPU_shader_uniform_1i(shader, "elements_per_curve", dispatch_x_dim / resolution); GPU_shader_uniform_1i(shader, "ncurves", curves_num); GPU_indexbuf_bind_as_ssbo(ib, GPU_shader_get_ssbo_binding(shader, "out_indices")); -- 2.30.2 From 74b1faa8ac7fd6816f9e51b030a5cb356c8df819 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cle=CC=81ment=20Foucault?= Date: Sun, 25 Feb 2024 15:54:16 +0100 Subject: [PATCH 4/6] Fix memory barrier types --- source/blender/gpu/intern/gpu_index_buffer.cc | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/source/blender/gpu/intern/gpu_index_buffer.cc b/source/blender/gpu/intern/gpu_index_buffer.cc index 81c32ebb275..57341ba8c22 100644 --- a/source/blender/gpu/intern/gpu_index_buffer.cc +++ b/source/blender/gpu/intern/gpu_index_buffer.cc @@ -271,7 +271,6 @@ GPUIndexBuf *GPU_indexbuf_build_curves_on_device(GPUPrimType prim_type, tris ? GPU_SHADER_INDEXBUF_TRIS : (lines ? GPU_SHADER_INDEXBUF_LINES : GPU_SHADER_INDEXBUF_POINTS)); GPU_shader_bind(shader); - GPU_memory_barrier(GPU_BARRIER_BUFFER_UPDATE); ib = GPU_indexbuf_build_on_device(curves_num * dispatch_x_dim); int resolution; if (tris) { @@ -288,7 +287,7 @@ GPUIndexBuf *GPU_indexbuf_build_curves_on_device(GPUPrimType prim_type, GPU_indexbuf_bind_as_ssbo(ib, GPU_shader_get_ssbo_binding(shader, "out_indices")); GPU_compute_dispatch(shader, grid_x, grid_y, grid_z); - GPU_memory_barrier(GPU_BARRIER_BUFFER_UPDATE); + GPU_memory_barrier(GPU_BARRIER_ELEMENT_ARRAY); GPU_shader_unbind(); return ib; } -- 2.30.2 From 6bf37ab5ecc93a99c2f725ae08a068bc647358e4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cle=CC=81ment=20Foucault?= Date: Sun, 25 Feb 2024 16:00:48 +0100 Subject: [PATCH 5/6] Renaming --- source/blender/gpu/CMakeLists.txt | 8 ++++---- source/blender/gpu/intern/gpu_shader_builtin.cc | 6 +++--- ...nes.glsl => gpu_shader_index_2d_array_lines.glsl} | 0 ...ts.glsl => gpu_shader_index_2d_array_points.glsl} | 0 ...tris.glsl => gpu_shader_index_2d_array_tris.glsl} | 0 ...pute_shaders_info.hh => gpu_shader_index_info.hh} | 12 ++++++------ 6 files changed, 13 insertions(+), 13 deletions(-) rename source/blender/gpu/shaders/{gpu_compute_2d_array_lines.glsl => gpu_shader_index_2d_array_lines.glsl} (100%) rename source/blender/gpu/shaders/{gpu_compute_2d_array_points.glsl => gpu_shader_index_2d_array_points.glsl} (100%) rename source/blender/gpu/shaders/{gpu_compute_2d_array_tris.glsl => gpu_shader_index_2d_array_tris.glsl} (100%) rename source/blender/gpu/shaders/infos/{gpu_compute_shaders_info.hh => gpu_shader_index_info.hh} (71%) diff --git a/source/blender/gpu/CMakeLists.txt b/source/blender/gpu/CMakeLists.txt index 63f483bb119..e3012742514 100644 --- a/source/blender/gpu/CMakeLists.txt +++ b/source/blender/gpu/CMakeLists.txt @@ -590,9 +590,9 @@ set(GLSL_SRC shaders/gpu_shader_cfg_world_clip_lib.glsl shaders/gpu_shader_colorspace_lib.glsl - shaders/gpu_compute_2d_array_points.glsl - shaders/gpu_compute_2d_array_lines.glsl - shaders/gpu_compute_2d_array_tris.glsl + shaders/gpu_shader_index_2d_array_points.glsl + shaders/gpu_shader_index_2d_array_lines.glsl + shaders/gpu_shader_index_2d_array_tris.glsl GPU_shader_shared_utils.h ) @@ -737,7 +737,6 @@ set(SRC_SHADER_CREATE_INFOS ../draw/intern/shaders/draw_view_info.hh shaders/infos/gpu_clip_planes_info.hh - shaders/infos/gpu_compute_shaders_info.hh shaders/infos/gpu_shader_2D_area_borders_info.hh shaders/infos/gpu_shader_2D_checker_info.hh shaders/infos/gpu_shader_2D_diag_stripes_info.hh @@ -761,6 +760,7 @@ set(SRC_SHADER_CREATE_INFOS shaders/infos/gpu_shader_3D_uniform_color_info.hh shaders/infos/gpu_shader_gpencil_stroke_info.hh shaders/infos/gpu_shader_icon_info.hh + shaders/infos/gpu_shader_index_info.hh shaders/infos/gpu_shader_instance_varying_color_varying_size_info.hh shaders/infos/gpu_shader_keyframe_shape_info.hh shaders/infos/gpu_shader_line_dashed_uniform_color_info.hh diff --git a/source/blender/gpu/intern/gpu_shader_builtin.cc b/source/blender/gpu/intern/gpu_shader_builtin.cc index 01d2fcfbfa8..442fa484582 100644 --- a/source/blender/gpu/intern/gpu_shader_builtin.cc +++ b/source/blender/gpu/intern/gpu_shader_builtin.cc @@ -88,11 +88,11 @@ static const char *builtin_shader_create_info_name(eGPUBuiltinShader shader) case GPU_SHADER_GPENCIL_STROKE: return "gpu_shader_gpencil_stroke"; case GPU_SHADER_INDEXBUF_POINTS: - return "gpu_compute_2d_array_points"; + return "gpu_shader_index_2d_array_points"; case GPU_SHADER_INDEXBUF_LINES: - return "gpu_compute_2d_array_lines"; + return "gpu_shader_index_2d_array_lines"; case GPU_SHADER_INDEXBUF_TRIS: - return "gpu_compute_2d_array_tris"; + return "gpu_shader_index_2d_array_tris"; default: BLI_assert_unreachable(); return ""; diff --git a/source/blender/gpu/shaders/gpu_compute_2d_array_lines.glsl b/source/blender/gpu/shaders/gpu_shader_index_2d_array_lines.glsl similarity index 100% rename from source/blender/gpu/shaders/gpu_compute_2d_array_lines.glsl rename to source/blender/gpu/shaders/gpu_shader_index_2d_array_lines.glsl diff --git a/source/blender/gpu/shaders/gpu_compute_2d_array_points.glsl b/source/blender/gpu/shaders/gpu_shader_index_2d_array_points.glsl similarity index 100% rename from source/blender/gpu/shaders/gpu_compute_2d_array_points.glsl rename to source/blender/gpu/shaders/gpu_shader_index_2d_array_points.glsl diff --git a/source/blender/gpu/shaders/gpu_compute_2d_array_tris.glsl b/source/blender/gpu/shaders/gpu_shader_index_2d_array_tris.glsl similarity index 100% rename from source/blender/gpu/shaders/gpu_compute_2d_array_tris.glsl rename to source/blender/gpu/shaders/gpu_shader_index_2d_array_tris.glsl diff --git a/source/blender/gpu/shaders/infos/gpu_compute_shaders_info.hh b/source/blender/gpu/shaders/infos/gpu_shader_index_info.hh similarity index 71% rename from source/blender/gpu/shaders/infos/gpu_compute_shaders_info.hh rename to source/blender/gpu/shaders/infos/gpu_shader_index_info.hh index 51cd6b1f1ad..4204416b032 100644 --- a/source/blender/gpu/shaders/infos/gpu_compute_shaders_info.hh +++ b/source/blender/gpu/shaders/infos/gpu_shader_index_info.hh @@ -9,26 +9,26 @@ #include "gpu_interface_info.hh" #include "gpu_shader_create_info.hh" -GPU_SHADER_CREATE_INFO(gpu_compute_2d_array_points) +GPU_SHADER_CREATE_INFO(gpu_shader_index_2d_array_points) .local_group_size(16, 16, 1) .push_constant(Type::INT, "elements_per_curve") .push_constant(Type::INT, "ncurves") .storage_buf(0, Qualifier::WRITE, "uint", "out_indices[]") - .compute_source("gpu_compute_2d_array_points.glsl") + .compute_source("gpu_shader_index_2d_array_points.glsl") .do_static_compilation(true); -GPU_SHADER_CREATE_INFO(gpu_compute_2d_array_lines) +GPU_SHADER_CREATE_INFO(gpu_shader_index_2d_array_lines) .local_group_size(16, 16, 1) .push_constant(Type::INT, "elements_per_curve") .push_constant(Type::INT, "ncurves") .storage_buf(0, Qualifier::WRITE, "uint", "out_indices[]") - .compute_source("gpu_compute_2d_array_lines.glsl") + .compute_source("gpu_shader_index_2d_array_lines.glsl") .do_static_compilation(true); -GPU_SHADER_CREATE_INFO(gpu_compute_2d_array_tris) +GPU_SHADER_CREATE_INFO(gpu_shader_index_2d_array_tris) .local_group_size(16, 16, 1) .push_constant(Type::INT, "elements_per_curve") .push_constant(Type::INT, "ncurves") .storage_buf(0, Qualifier::WRITE, "uint", "out_indices[]") - .compute_source("gpu_compute_2d_array_tris.glsl") + .compute_source("gpu_shader_index_2d_array_tris.glsl") .do_static_compilation(true); -- 2.30.2 From 3415a625c9e3622eeccd026c088dde790615014f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cle=CC=81ment=20Foucault?= Date: Sun, 25 Feb 2024 16:05:54 +0100 Subject: [PATCH 6/6] Cleanup codestyle discrepancy --- source/blender/draw/intern/draw_cache_impl_curves.cc | 4 ++-- source/blender/gpu/intern/gpu_index_buffer.cc | 7 +++---- 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/source/blender/draw/intern/draw_cache_impl_curves.cc b/source/blender/draw/intern/draw_cache_impl_curves.cc index 451725b560f..be261fe513b 100644 --- a/source/blender/draw/intern/draw_cache_impl_curves.cc +++ b/source/blender/draw/intern/draw_cache_impl_curves.cc @@ -454,11 +454,11 @@ static void curves_batch_cache_ensure_procedural_indices(const bke::CurvesGeomet GPUVertBuf *vbo = GPU_vertbuf_create_with_format(&format); GPU_vertbuf_data_alloc(vbo, 1); - GPUIndexBuf *ib = GPU_indexbuf_build_curves_on_device( + GPUIndexBuf *ibo = GPU_indexbuf_build_curves_on_device( prim_type, curves.curves_num(), verts_per_curve); cache.final[subdiv].proc_hairs[thickness_res - 1] = GPU_batch_create_ex( - prim_type, vbo, ib, GPU_BATCH_OWNS_VBO | GPU_BATCH_OWNS_INDEX); + prim_type, vbo, ibo, GPU_BATCH_OWNS_VBO | GPU_BATCH_OWNS_INDEX); } static bool curves_ensure_attributes(const Curves &curves, diff --git a/source/blender/gpu/intern/gpu_index_buffer.cc b/source/blender/gpu/intern/gpu_index_buffer.cc index 57341ba8c22..f073939df18 100644 --- a/source/blender/gpu/intern/gpu_index_buffer.cc +++ b/source/blender/gpu/intern/gpu_index_buffer.cc @@ -264,14 +264,13 @@ GPUIndexBuf *GPU_indexbuf_build_curves_on_device(GPUPrimType prim_type, grid_y = min_uu(grid_y, max_grid_y); grid_z = min_uu(grid_z, max_grid_z); } - GPUIndexBuf *ib; bool tris = (prim_type == GPU_PRIM_TRIS); bool lines = (prim_type == GPU_PRIM_LINES); GPUShader *shader = GPU_shader_get_builtin_shader( tris ? GPU_SHADER_INDEXBUF_TRIS : (lines ? GPU_SHADER_INDEXBUF_LINES : GPU_SHADER_INDEXBUF_POINTS)); GPU_shader_bind(shader); - ib = GPU_indexbuf_build_on_device(curves_num * dispatch_x_dim); + GPUIndexBuf *ibo = GPU_indexbuf_build_on_device(curves_num * dispatch_x_dim); int resolution; if (tris) { resolution = 6; @@ -284,12 +283,12 @@ GPUIndexBuf *GPU_indexbuf_build_curves_on_device(GPUPrimType prim_type, } GPU_shader_uniform_1i(shader, "elements_per_curve", dispatch_x_dim / resolution); GPU_shader_uniform_1i(shader, "ncurves", curves_num); - GPU_indexbuf_bind_as_ssbo(ib, GPU_shader_get_ssbo_binding(shader, "out_indices")); + GPU_indexbuf_bind_as_ssbo(ibo, GPU_shader_get_ssbo_binding(shader, "out_indices")); GPU_compute_dispatch(shader, grid_x, grid_y, grid_z); GPU_memory_barrier(GPU_BARRIER_ELEMENT_ARRAY); GPU_shader_unbind(); - return ib; + return ibo; } /** \} */ -- 2.30.2