Skip to content

Commit

Permalink
[rhi] Add create_image_unique stub & misc RHI bug fixes (#7232)
Browse files Browse the repository at this point in the history
Part of #7218
  • Loading branch information
bobcao3 authored Jan 26, 2023
1 parent 1d95d8d commit 1e324c6
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 6 deletions.
4 changes: 4 additions & 0 deletions taichi/rhi/device.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ DeviceAllocationGuard::~DeviceAllocationGuard() {
device->dealloc_memory(*this);
}

DeviceImageGuard::~DeviceImageGuard() {
dynamic_cast<GraphicsDevice *>(device)->destroy_image(*this);
}

DevicePtr DeviceAllocation::get_ptr(uint64_t offset) const {
return DevicePtr{{device, alloc_id}, offset};
}
Expand Down
14 changes: 14 additions & 0 deletions taichi/rhi/device.h
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,17 @@ struct TI_DLL_EXPORT DeviceAllocationGuard : public DeviceAllocation {
~DeviceAllocationGuard();
};

using DeviceAllocationUnique = std::unique_ptr<DeviceAllocationGuard>;

struct TI_DLL_EXPORT DeviceImageGuard : public DeviceAllocation {
explicit DeviceImageGuard(DeviceAllocation alloc) : DeviceAllocation(alloc) {
}
DeviceImageGuard(const DeviceAllocationGuard &) = delete;
~DeviceImageGuard();
};

using DeviceImageUnique = std::unique_ptr<DeviceImageGuard>;

struct TI_DLL_EXPORT DevicePtr : public DeviceAllocation {
uint64_t offset{0};

Expand Down Expand Up @@ -894,6 +905,9 @@ class TI_DLL_EXPORT GraphicsDevice : public Device {
// `GfxRuntime::create_image`. `GfxRuntime` is available in `ProgramImpl`
// of GPU backends.
virtual DeviceAllocation create_image(const ImageParams &params) = 0;
inline DeviceImageUnique create_image_unique(const ImageParams &params) {
return std::make_unique<DeviceImageGuard>(this->create_image(params));
}
virtual void destroy_image(DeviceAllocation handle) = 0;

virtual void image_transition(DeviceAllocation img,
Expand Down
3 changes: 2 additions & 1 deletion taichi/rhi/opengl/opengl_device.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -847,7 +847,8 @@ void GLCommandList::CmdBindResources::execute() {
}

void GLCommandList::CmdBufferBarrier::execute() {
glMemoryBarrier(GL_SHADER_STORAGE_BARRIER_BIT);
glMemoryBarrier(GL_SHADER_STORAGE_BARRIER_BIT | GL_BUFFER_UPDATE_BARRIER_BIT |
GL_UNIFORM_BARRIER_BIT);
check_opengl_error("glMemoryBarrier");
}

Expand Down
24 changes: 19 additions & 5 deletions taichi/rhi/vulkan/vulkan_device.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -552,10 +552,16 @@ void VulkanPipeline::create_graphics_pipeline(
color_blending.blendConstants[3] = 0.0f;

if (raster_params.blending.size()) {
RHI_ASSERT(raster_params.blending.size() ==
graphics_pipeline_template_->blend_attachments.size() &&
"RasterParams::blending (size={}) must either be zero sized "
"or match the number of fragment shader outputs (size={}).");
if (raster_params.blending.size() != color_blending.attachmentCount) {
std::array<char, 256> buf;
snprintf(buf.data(), buf.size(),
"RasterParams::blending (size=%u) must either be zero sized "
"or match the number of fragment shader outputs (size=%u).",
uint32_t(raster_params.blending.size()),
uint32_t(color_blending.attachmentCount));
RHI_LOG_ERROR(buf.data());
RHI_ASSERT(false);
}

for (int i = 0; i < raster_params.blending.size(); i++) {
auto &state = graphics_pipeline_template_->blend_attachments[i];
Expand Down Expand Up @@ -1694,6 +1700,8 @@ RhiResult VulkanDevice::create_pipeline(Pipeline **out_pipeline,
DeviceAllocation VulkanDevice::allocate_memory(const AllocParams &params) {
AllocationInternal &alloc = allocations_.acquire();

RHI_ASSERT(params.size > 0);

VkBufferCreateInfo buffer_info{};
buffer_info.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO;
buffer_info.pNext = nullptr;
Expand Down Expand Up @@ -1770,7 +1778,13 @@ DeviceAllocation VulkanDevice::allocate_memory(const AllocParams &params) {
alloc_info.requiredFlags = VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT;
}

if (get_caps().get(DeviceCapability::spirv_has_physical_storage_buffer)) {
if (get_caps().get(DeviceCapability::spirv_has_physical_storage_buffer) &&
((alloc_info.usage & VK_BUFFER_USAGE_STORAGE_BUFFER_BIT) ||
(alloc_info.usage &
VK_BUFFER_USAGE_ACCELERATION_STRUCTURE_BUILD_INPUT_READ_ONLY_BIT_KHR) ||
(alloc_info.usage &
VK_BUFFER_USAGE_ACCELERATION_STRUCTURE_STORAGE_BIT_KHR) ||
(alloc_info.usage & VK_BUFFER_USAGE_SHADER_BINDING_TABLE_BIT_KHR))) {
buffer_info.usage |= VK_BUFFER_USAGE_SHADER_DEVICE_ADDRESS_BIT_KHR;
}

Expand Down

0 comments on commit 1e324c6

Please sign in to comment.