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 2 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
34 changes: 34 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,37 @@ 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>
struct SyncedPtrStableObjectList {
std::mutex lock;
bobcao3 marked this conversation as resolved.
Show resolved Hide resolved
std::forward_list<T> objects;
std::vector<T *> free_nodes;

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();
}
};

} // 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