Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[vulkan] Improve Vulkan RHI impl with lower overhead internal implementations #6912

Merged
merged 14 commits into from
Dec 18, 2022
Merged
Show file tree
Hide file tree
Changes from 10 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion taichi/rhi/device.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ enum class RhiResult {
error = -1,
invalid_usage = -2,
not_supported = -3,
out_of_memory = -4,
bobcao3 marked this conversation as resolved.
Show resolved Hide resolved
};

constexpr size_t kBufferSizeEntireSize = size_t(-1);
Expand Down Expand Up @@ -54,7 +55,7 @@ struct LLVMRuntime;

// TODO: Figure out how to support images. Temporary solutions is to have all
// opque types such as images work as an allocation
using DeviceAllocationId = uint32_t;
using DeviceAllocationId = uint64_t;

struct TI_DLL_EXPORT DeviceAllocation {
Device *device{nullptr};
Expand Down
36 changes: 36 additions & 0 deletions taichi/rhi/impl_support.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

#include "taichi/rhi/device.h"
#include <assert.h>
#include <forward_list>
#include <mutex>

namespace taichi::lang {

Expand Down Expand Up @@ -89,5 +91,39 @@ struct BidirMap {
}
};

// A synchronized list of objects that is pointer stable & reuse objects
// It does not mark objects as used, and it does not free objects (destructor is
// not called)
template <class T>
class SyncedPtrStableObjectList {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

so to my understanding the the contained objects should be explicitly constructed/destructed?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's a good point. It seems like we should add a check that T is a POD-ish type, which doesn't have fancy ctor/dtor. (Dtor is especially important here, IMO. Right now the implementation would require the caller to call ~T())

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed it now, the the object list now handles ctor/dtor & uses RAII.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: I suggest that we add some unit test on this (https://github.com/taichi-dev/taichi/tree/master/tests/cpp), given that this is a very low-level infra, and has certain behaviors that is not trivial (manually calling ~T(), placement new, ...)

public:
T &acquire() {
std::lock_guard<std::mutex> _(lock_);
if (free_nodes_.empty()) {
return objects_.emplace_front();
} else {
T *obj = free_nodes_.back();
free_nodes_.pop_back();
return *obj;
}
}

void release(T *ptr) {
std::lock_guard<std::mutex> _(lock_);
free_nodes_.push_back(ptr);
}

void clear() {
std::lock_guard<std::mutex> _(lock_);
objects_.clear();
free_nodes_.clear();
}

private:
std::mutex lock_;
std::forward_list<T> objects_;
std::vector<T *> free_nodes_;
};

} // namespace rhi_impl
} // namespace taichi::lang
6 changes: 4 additions & 2 deletions taichi/rhi/opengl/opengl_device.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -562,7 +562,8 @@ DeviceAllocation GLDevice::allocate_memory(const AllocParams &params) {
}

void GLDevice::dealloc_memory(DeviceAllocation handle) {
glDeleteBuffers(1, &handle.alloc_id);
GLuint buffer = GLuint(handle.alloc_id);
glDeleteBuffers(1, &buffer);
check_opengl_error("glDeleteBuffers");
}

Expand Down Expand Up @@ -714,7 +715,8 @@ DeviceAllocation GLDevice::create_image(const ImageParams &params) {
}

void GLDevice::destroy_image(DeviceAllocation handle) {
glDeleteTextures(1, &handle.alloc_id);
GLuint texture = GLuint(handle.alloc_id);
glDeleteTextures(1, &texture);
check_opengl_error("glDeleteTextures");
image_to_dims_.erase(handle.alloc_id);
image_to_int_format_.erase(handle.alloc_id);
Expand Down
32 changes: 32 additions & 0 deletions taichi/rhi/vulkan/vulkan_api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -369,6 +369,38 @@ IVkPipeline create_graphics_pipeline(VkDevice device,
return obj;
}

IVkPipeline create_graphics_pipeline_dynamic(
VkDevice device,
VkGraphicsPipelineCreateInfo *create_info,
VkPipelineRenderingCreateInfoKHR *rendering_info,
IVkPipelineLayout layout,
IVkPipelineCache cache,
IVkPipeline base_pipeline) {
IVkPipeline obj = std::make_shared<DeviceObjVkPipeline>();
obj->device = device;
obj->ref_layout = layout;
obj->ref_cache = cache;
obj->ref_renderpass = nullptr;

create_info->pNext = rendering_info;
create_info->layout = layout->layout;

if (base_pipeline) {
create_info->basePipelineHandle = base_pipeline->pipeline;
create_info->basePipelineIndex = -1;
} else {
create_info->basePipelineHandle = VK_NULL_HANDLE;
create_info->basePipelineIndex = 0;
}

VkResult res =
vkCreateGraphicsPipelines(device, cache ? cache->cache : VK_NULL_HANDLE,
1, create_info, nullptr, &obj->pipeline);
BAIL_ON_VK_BAD_RESULT_NO_RETURN(res, "failed to create graphics pipeline");

return obj;
}

IVkPipeline create_raytracing_pipeline(
VkDevice device,
VkRayTracingPipelineCreateInfoKHR *create_info,
Expand Down
7 changes: 7 additions & 0 deletions taichi/rhi/vulkan/vulkan_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,13 @@ IVkPipeline create_graphics_pipeline(VkDevice device,
IVkPipelineLayout layout,
IVkPipelineCache cache = nullptr,
IVkPipeline base_pipeline = nullptr);
IVkPipeline create_graphics_pipeline_dynamic(
VkDevice device,
VkGraphicsPipelineCreateInfo *create_info,
VkPipelineRenderingCreateInfoKHR *rendering_info,
IVkPipelineLayout layout,
IVkPipelineCache cache = nullptr,
IVkPipeline base_pipeline = nullptr);
IVkPipeline create_raytracing_pipeline(
VkDevice device,
VkRayTracingPipelineCreateInfoKHR *create_info,
Expand Down
Loading