From f5510548e52aad024907ac3947ad16e0d490adb7 Mon Sep 17 00:00:00 2001 From: archibate <17721388340@163.com> Date: Sat, 22 Feb 2020 15:13:12 +0800 Subject: [PATCH] [skip ci] obsolete SSBO class: no more unnecessary memcpy Conflicts: taichi/codegen/codegen_opengl.cpp taichi/platform/opengl/opengl_kernel.cpp taichi/platform/opengl/opengl_kernel.h --- taichi/codegen/codegen_opengl.cpp | 48 +++++++++--------------- taichi/platform/opengl/opengl_kernel.cpp | 24 ------------ taichi/platform/opengl/opengl_kernel.h | 39 ------------------- 3 files changed, 17 insertions(+), 94 deletions(-) delete mode 100644 taichi/platform/opengl/opengl_kernel.cpp delete mode 100644 taichi/platform/opengl/opengl_kernel.h diff --git a/taichi/codegen/codegen_opengl.cpp b/taichi/codegen/codegen_opengl.cpp index 9bb595abe0dc9..e4a6202fd82e2 100644 --- a/taichi/codegen/codegen_opengl.cpp +++ b/taichi/codegen/codegen_opengl.cpp @@ -1,7 +1,6 @@ //#define _GLSL_DEBUG 1 #include "codegen_opengl.h" #include -#include #include #include @@ -500,13 +499,15 @@ class KernelGen : public IRVisitor return num_groups_; } - SSBO *create_root_ssbo() + IOV *create_root_buffer() { - static SSBO *root_ssbo; - if (!root_ssbo) { - root_ssbo = new SSBO(struct_compiled_->root_size); + static IOV *root; + if (!root) { + size_t size = struct_compiled_->root_size; + void *buffer = std::calloc(size, 1); + root = new IOV{buffer, size}; } - return root_ssbo; + return root; } void run(const SNode &root_snode) @@ -645,7 +646,7 @@ FunctionType OpenglCodeGen::gen(void) { KernelGen codegen(kernel_, kernel_name_, struct_compiled_); codegen.run(*prog_->snode_root); - SSBO *root_sb = codegen.create_root_ssbo(); + IOV *root_iov = codegen.create_root_buffer(); const std::string kernel_source_code = codegen.kernel_source_code(); int num_groups = codegen.get_num_work_groups(); #ifdef _GLSL_DEBUG @@ -661,38 +662,23 @@ FunctionType OpenglCodeGen::gen(void) if (has_ext_arr) TI_ERROR("external array argument is supported to at most one in OpenGL for now"); TI_INFO("external array argument index {}", i); ext_arr_idx = i; - ext_arr_size = kernel_->args[i].size; + ext_arr_size = kernel_->args[i].size; has_ext_arr = true; TI_INFO("external array size {}", ext_arr_size); } } - return [glsl, num_groups, has_ext_arr, ext_arr_size, ext_arr_idx, root_sb](Context &ctx) { - // TODO(archibate): try implement just new_ssbo_from_buffer(ctx.args) and no free like _IOMYBUF - SSBO *arg_sb = new SSBO(taichi_max_num_args * sizeof(uint64_t)); - SSBO *earg_sb = new SSBO(Context::extra_args_size); - arg_sb->load_from((void *)ctx.args); - earg_sb->load_from((void *)ctx.extra_args); - std::vector iov = {*arg_sb, *root_sb, *earg_sb}; - SSBO *ext_sb = nullptr; - void *extptr = nullptr; + return [glsl, num_groups, has_ext_arr, ext_arr_size, ext_arr_idx, root_iov](Context &ctx) { + std::vector iov(3); + iov[0] = IOV{ctx.args, taichi_max_num_args * sizeof(uint64_t)}; + iov[1] = *root_iov; + iov[2] = IOV{ctx.extra_args, Context::extra_args_size}; if (has_ext_arr) { - uint64_t *arg_data = (uint64_t *)arg_sb->data(); - extptr = (void *)arg_data[ext_arr_idx]; - arg_data[ext_arr_idx] = 0; - ext_sb = new SSBO(ext_arr_size); - ext_sb->load_from(extptr); - iov.push_back(*ext_sb); + void *extptr = (void *)ctx.args[ext_arr_idx]; + ctx.args[ext_arr_idx] = 0; + iov.push_back(IOV{extptr, ext_arr_size}); } launch_glsl_kernel(glsl, iov, num_groups); - arg_sb->save_to((void *)ctx.args); - earg_sb->save_to((void *)ctx.extra_args); - delete arg_sb; - delete earg_sb; - if (has_ext_arr) { - ext_sb->save_to(extptr); - delete ext_sb; - } }; } diff --git a/taichi/platform/opengl/opengl_kernel.cpp b/taichi/platform/opengl/opengl_kernel.cpp deleted file mode 100644 index 5147b16fd8a38..0000000000000 --- a/taichi/platform/opengl/opengl_kernel.cpp +++ /dev/null @@ -1,24 +0,0 @@ -#include "opengl_kernel.h" -#include "opengl_api.h" - -#include - -TLANG_NAMESPACE_BEGIN -namespace opengl { - -SSBO::SSBO(size_t data_size_) - : data_(data_size_), data_size(data_size_) -{} - -void SSBO::load_from(const void *buffer) -{ - std::memcpy(data(), buffer, data_size); -} - -void SSBO::save_to(void *buffer) -{ - std::memcpy(buffer, data(), data_size); -} - -} // namespace opengl -TLANG_NAMESPACE_END diff --git a/taichi/platform/opengl/opengl_kernel.h b/taichi/platform/opengl/opengl_kernel.h deleted file mode 100644 index 92b2acbcdda5d..0000000000000 --- a/taichi/platform/opengl/opengl_kernel.h +++ /dev/null @@ -1,39 +0,0 @@ -#pragma once - -#include -#include - -#include -#include - -#include "opengl_kernel_util.h" - - -TLANG_NAMESPACE_BEGIN - -namespace opengl { - -struct SSBO -{ - std::vector data_; - const size_t data_size; - - SSBO(size_t data_size); - - void load_from(const void *buffer); - void save_to(void *buffer); - - inline void *data() - { - return (void *)data_.data(); - } - - inline operator IOV() - { - return IOV{data(), data_size}; - } -}; - -} // namespace opengl - -TLANG_NAMESPACE_END