Skip to content

Commit

Permalink
Refactor IGL Vulkan to use function tables
Browse files Browse the repository at this point in the history
Summary: Use function tables instead of global Vulkan functions

Reviewed By: corporateshark

Differential Revision: D49371367

fbshipit-source-id: 07cc96044454665d6d720cc05682386029f65d13
  • Loading branch information
Roman Kuznetsov authored and facebook-github-bot committed Sep 21, 2023
1 parent f1a1eb7 commit 4dba28c
Show file tree
Hide file tree
Showing 46 changed files with 743 additions and 517 deletions.
4 changes: 2 additions & 2 deletions src/igl/vulkan/CommandBuffer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -161,11 +161,11 @@ void CommandBuffer::present(std::shared_ptr<ITexture> surface) const {
}

void CommandBuffer::pushDebugGroupLabel(const std::string& label, const igl::Color& color) const {
ivkCmdBeginDebugUtilsLabel(wrapper_.cmdBuf_, label.c_str(), color.toFloatPtr());
ivkCmdBeginDebugUtilsLabel(&ctx_.vf_, wrapper_.cmdBuf_, label.c_str(), color.toFloatPtr());
}

void CommandBuffer::popDebugGroupLabel() const {
ivkCmdEndDebugUtilsLabel(wrapper_.cmdBuf_);
ivkCmdEndDebugUtilsLabel(&ctx_.vf_, wrapper_.cmdBuf_);
}

void CommandBuffer::waitUntilCompleted() {
Expand Down
15 changes: 8 additions & 7 deletions src/igl/vulkan/CommandQueue.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,8 @@ void CommandQueue::enhancedShaderDebuggingPass(const igl::vulkan::VulkanContext&

// Barrier to ensure we have finished rendering the lines before we clear the buffer
auto lineBuffer = static_cast<vulkan::Buffer*>(debugger->vertexBuffer().get());
ivkBufferMemoryBarrier(vkResetCmdBuffer,
ivkBufferMemoryBarrier(&ctx.vf_,
vkResetCmdBuffer,
lineBuffer->getVkBuffer(),
0, /* src access flag */
0, /* dst access flag */
Expand All @@ -168,12 +169,12 @@ void CommandQueue::enhancedShaderDebuggingPass(const igl::vulkan::VulkanContext&
VK_PIPELINE_STAGE_TRANSFER_BIT);

// Reset instanceCount of the buffer
vkCmdFillBuffer(vkResetCmdBuffer,
lineBuffer->getVkBuffer(),
offsetof(EnhancedShaderDebuggingStore::Header, command_) +
offsetof(VkDrawIndirectCommand, instanceCount),
sizeof(uint32_t), // reset only the instance count
0);
ctx.vf_.vkCmdFillBuffer(vkResetCmdBuffer,
lineBuffer->getVkBuffer(),
offsetof(EnhancedShaderDebuggingStore::Header, command_) +
offsetof(VkDrawIndirectCommand, instanceCount),
sizeof(uint32_t), // reset only the instance count
0);

endCommandBuffer(ctx, resetCmdBuffer, true);
}
Expand Down
6 changes: 1 addition & 5 deletions src/igl/vulkan/Common.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,8 @@
// set to 1 to see very verbose debug console logs with Vulkan commands
#define IGL_VULKAN_PRINT_COMMANDS 0

#if !defined(VK_NO_PROTOTYPES)
#define VK_NO_PROTOTYPES 1
#endif // !defined(VK_NO_PROTOTYPES)

#include <igl/Macros.h>
#include <volk.h>
#include <igl/vulkan/VulkanFunctionTable.h>
#if IGL_PLATFORM_MACOS
#include <vulkan/vulkan_metal.h>
#endif
Expand Down
20 changes: 10 additions & 10 deletions src/igl/vulkan/ComputeCommandEncoder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,26 +76,26 @@ void ComputeCommandEncoder::dispatchThreadGroups(const Dimensions& threadgroupCo

binder_.updateBindings();
// threadgroupSize is controlled inside compute shaders
vkCmdDispatch(
ctx_.vf_.vkCmdDispatch(
cmdBuffer_, threadgroupCount.width, threadgroupCount.height, threadgroupCount.depth);
}

void ComputeCommandEncoder::pushDebugGroupLabel(const std::string& label,
const igl::Color& color) const {
IGL_ASSERT(!label.empty());

ivkCmdBeginDebugUtilsLabel(cmdBuffer_, label.c_str(), color.toFloatPtr());
ivkCmdBeginDebugUtilsLabel(&ctx_.vf_, cmdBuffer_, label.c_str(), color.toFloatPtr());
}

void ComputeCommandEncoder::insertDebugEventLabel(const std::string& label,
const igl::Color& color) const {
IGL_ASSERT(!label.empty());

ivkCmdInsertDebugUtilsLabel(cmdBuffer_, label.c_str(), color.toFloatPtr());
ivkCmdInsertDebugUtilsLabel(&ctx_.vf_, cmdBuffer_, label.c_str(), color.toFloatPtr());
}

void ComputeCommandEncoder::popDebugGroupLabel() const {
ivkCmdEndDebugUtilsLabel(cmdBuffer_);
ivkCmdEndDebugUtilsLabel(&ctx_.vf_, cmdBuffer_);
}

void ComputeCommandEncoder::bindUniform(const UniformDesc& /*uniformDesc*/, const void* /*data*/) {
Expand Down Expand Up @@ -177,12 +177,12 @@ void ComputeCommandEncoder::bindPushConstants(const void* data, size_t length, s
"Push constants size exceeded %u (max %u bytes)", size, limits.maxPushConstantsSize);
}

vkCmdPushConstants(cmdBuffer_,
ctx_.pipelineLayoutCompute_->getVkPipelineLayout(),
VK_SHADER_STAGE_COMPUTE_BIT,
(uint32_t)offset,
(uint32_t)length,
data);
ctx_.vf_.vkCmdPushConstants(cmdBuffer_,
ctx_.pipelineLayoutCompute_->getVkPipelineLayout(),
VK_SHADER_STAGE_COMPUTE_BIT,
(uint32_t)offset,
(uint32_t)length,
data);
}

} // namespace vulkan
Expand Down
27 changes: 14 additions & 13 deletions src/igl/vulkan/ComputePipelineState.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,40 +30,41 @@ ComputePipelineState::ComputePipelineState(const igl::vulkan::Device& device,

ComputePipelineState ::~ComputePipelineState() {
if (pipeline_ != VK_NULL_HANDLE) {
device_.getVulkanContext().deferredTask(std::packaged_task<void()>(
[device = device_.getVulkanContext().device_->getVkDevice(), pipeline = pipeline_]() {
vkDestroyPipeline(device, pipeline, nullptr);
}));
const auto& ctx = device_.getVulkanContext();
ctx.deferredTask(std::packaged_task<void()>(
[vf = &ctx.vf_,
device = device_.getVulkanContext().device_->getVkDevice(),
pipeline = pipeline_]() { vf->vkDestroyPipeline(device, pipeline, nullptr); }));
}
}

VkPipeline ComputePipelineState::getVkPipeline() const {
if (vkPipelineLayout_ !=
device_.getVulkanContext().pipelineLayoutCompute_->getVkPipelineLayout()) {
const VulkanContext& ctx = device_.getVulkanContext();
if (vkPipelineLayout_ != ctx.pipelineLayoutCompute_->getVkPipelineLayout()) {
// there's a new pipeline layout - drop the previous Vulkan pipeline
VkDevice device = device_.getVulkanContext().device_->getVkDevice();
VkDevice device = ctx.device_->getVkDevice();
if (pipeline_ != VK_NULL_HANDLE) {
device_.getVulkanContext().deferredTask(std::packaged_task<void()>(
[device, pipeline = pipeline_]() { vkDestroyPipeline(device, pipeline, nullptr); }));
ctx.deferredTask(std::packaged_task<void()>([vf = &ctx.vf_, device, pipeline = pipeline_]() {
vf->vkDestroyPipeline(device, pipeline, nullptr);
}));
}
pipeline_ = VK_NULL_HANDLE;
vkPipelineLayout_ = device_.getVulkanContext().pipelineLayoutCompute_->getVkPipelineLayout();
vkPipelineLayout_ = ctx.pipelineLayoutCompute_->getVkPipelineLayout();
}

if (pipeline_ != VK_NULL_HANDLE) {
return pipeline_;
}

const VulkanContext& ctx = device_.getVulkanContext();

const auto& shaderModule = desc_.shaderStages->getComputeModule();

igl::vulkan::VulkanComputePipelineBuilder()
.shaderStage(ivkGetPipelineShaderStageCreateInfo(
VK_SHADER_STAGE_COMPUTE_BIT,
igl::vulkan::ShaderModule::getVkShaderModule(shaderModule),
shaderModule->info().entryPoint.c_str()))
.build(ctx.device_->getVkDevice(),
.build(ctx.vf_,
ctx.device_->getVkDevice(),
ctx.pipelineCache_,
ctx.pipelineLayoutCompute_->getVkPipelineLayout(),
&pipeline_,
Expand Down
45 changes: 27 additions & 18 deletions src/igl/vulkan/Device.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,11 @@

namespace {

bool supportsFormat(VkPhysicalDevice physicalDevice, VkFormat format) {
bool supportsFormat(const VulkanFunctionTable& vf,
VkPhysicalDevice physicalDevice,
VkFormat format) {
VkFormatProperties properties;
vkGetPhysicalDeviceFormatProperties(physicalDevice, format, &properties);
vf.vkGetPhysicalDeviceFormatProperties(physicalDevice, format, &properties);
return properties.bufferFeatures != 0 || properties.linearTilingFeatures != 0 ||
properties.optimalTilingFeatures != 0;
}
Expand Down Expand Up @@ -245,7 +247,8 @@ std::shared_ptr<VulkanShaderModule> Device::createShaderModule(const void* data,
#endif // IGL_SHADER_DUMP && IGL_DEBUG

VkShaderModule vkShaderModule = VK_NULL_HANDLE;
const VkResult result = ivkCreateShaderModuleFromSPIRV(device, data, length, &vkShaderModule);
const VkResult result =
ivkCreateShaderModuleFromSPIRV(&ctx_->vf_, device, data, length, &vkShaderModule);

setResultFrom(outResult, result);

Expand All @@ -255,13 +258,16 @@ std::shared_ptr<VulkanShaderModule> Device::createShaderModule(const void* data,

if (!debugName.empty()) {
// set debug name
VK_ASSERT(ivkSetDebugObjectName(
device, VK_OBJECT_TYPE_SHADER_MODULE, (uint64_t)vkShaderModule, debugName.c_str()));
VK_ASSERT(ivkSetDebugObjectName(&ctx_->vf_,
device,
VK_OBJECT_TYPE_SHADER_MODULE,
(uint64_t)vkShaderModule,
debugName.c_str()));
}

// @fb-only
// @lint-ignore CLANGTIDY
return std::make_shared<VulkanShaderModule>(device, vkShaderModule);
return std::make_shared<VulkanShaderModule>(ctx_->vf_, device, vkShaderModule);
}

std::shared_ptr<VulkanShaderModule> Device::createShaderModule(ShaderStage stage,
Expand Down Expand Up @@ -337,8 +343,8 @@ std::shared_ptr<VulkanShaderModule> Device::createShaderModule(ShaderStage stage
ivkGlslangResource(&glslangResource, &ctx_->getVkPhysicalDeviceProperties());

VkShaderModule vkShaderModule = VK_NULL_HANDLE;
const Result result =
igl::vulkan::compileShader(device, vkStage, source, &vkShaderModule, &glslangResource);
const Result result = igl::vulkan::compileShader(
ctx_->vf_, device, vkStage, source, &vkShaderModule, &glslangResource);

Result::setResult(outResult, result);

Expand All @@ -348,13 +354,16 @@ std::shared_ptr<VulkanShaderModule> Device::createShaderModule(ShaderStage stage

if (!debugName.empty()) {
// set debug name
VK_ASSERT(ivkSetDebugObjectName(
device, VK_OBJECT_TYPE_SHADER_MODULE, (uint64_t)vkShaderModule, debugName.c_str()));
VK_ASSERT(ivkSetDebugObjectName(&ctx_->vf_,
device,
VK_OBJECT_TYPE_SHADER_MODULE,
(uint64_t)vkShaderModule,
debugName.c_str()));
}

// @fb-only
// @lint-ignore CLANGTIDY
return std::make_shared<VulkanShaderModule>(device, vkShaderModule);
return std::make_shared<VulkanShaderModule>(ctx_->vf_, device, vkShaderModule);
}

std::shared_ptr<IFramebuffer> Device::createFramebuffer(const FramebufferDesc& desc,
Expand Down Expand Up @@ -427,21 +436,21 @@ bool Device::hasFeature(DeviceFeatures feature) const {
case DeviceFeatures::StandardDerivativeExt:
return false;
case DeviceFeatures::TextureFormatRG:
return supportsFormat(physicalDevice, VK_FORMAT_R8G8_UNORM);
return supportsFormat(ctx_->vf_, physicalDevice, VK_FORMAT_R8G8_UNORM);
case DeviceFeatures::TextureFormatRGB:
return supportsFormat(physicalDevice, VK_FORMAT_R8G8B8_SRGB);
return supportsFormat(ctx_->vf_, physicalDevice, VK_FORMAT_R8G8B8_SRGB);
case DeviceFeatures::ReadWriteFramebuffer:
return true;
case DeviceFeatures::TextureNotPot:
return true;
case DeviceFeatures::UniformBlocks:
return true;
case DeviceFeatures::TextureHalfFloat:
return supportsFormat(physicalDevice, VK_FORMAT_R16G16B16A16_SFLOAT) ||
supportsFormat(physicalDevice, VK_FORMAT_R16_SFLOAT);
return supportsFormat(ctx_->vf_, physicalDevice, VK_FORMAT_R16G16B16A16_SFLOAT) ||
supportsFormat(ctx_->vf_, physicalDevice, VK_FORMAT_R16_SFLOAT);
case DeviceFeatures::TextureFloat:
return supportsFormat(physicalDevice, VK_FORMAT_R32G32B32A32_SFLOAT) ||
supportsFormat(physicalDevice, VK_FORMAT_R32_SFLOAT);
return supportsFormat(ctx_->vf_, physicalDevice, VK_FORMAT_R32G32B32A32_SFLOAT) ||
supportsFormat(ctx_->vf_, physicalDevice, VK_FORMAT_R32_SFLOAT);
case DeviceFeatures::Texture2DArray:
case DeviceFeatures::Texture3D:
return true;
Expand Down Expand Up @@ -602,7 +611,7 @@ ICapabilities::TextureFormatCapabilities Device::getTextureFormatCapabilities(
}

VkFormatProperties properties;
vkGetPhysicalDeviceFormatProperties(ctx_->vkPhysicalDevice_, vkFormat, &properties);
ctx_->vf_.vkGetPhysicalDeviceFormatProperties(ctx_->vkPhysicalDevice_, vkFormat, &properties);

const VkFormatFeatureFlags features = properties.optimalTilingFeatures;

Expand Down
4 changes: 3 additions & 1 deletion src/igl/vulkan/EnhancedShaderDebuggingStore.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,9 @@ void EnhancedShaderDebuggingStore::installBufferBarrier(
if (enabled_) {
auto cmdBuffer = static_cast<const vulkan::CommandBuffer*>(&commandBuffer);
auto* buffer = static_cast<vulkan::Buffer*>(vertexBuffer().get());
ivkBufferMemoryBarrier(cmdBuffer->getVkCommandBuffer(),
const auto& ctx = device_->getVulkanContext();
ivkBufferMemoryBarrier(&ctx.vf_,
cmdBuffer->getVkCommandBuffer(),
buffer->getVkBuffer(),
VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT, /* src access flag */
VK_ACCESS_INDIRECT_COMMAND_READ_BIT, /* dst access flag */
Expand Down
19 changes: 11 additions & 8 deletions src/igl/vulkan/Framebuffer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,8 @@ void Framebuffer::copyTextureColorAttachment(ICommandQueue& cmdQueue,
return;
}

const auto& ctx = device_.getVulkanContext();

// Extract the underlying VkCommandBuffer
CommandBufferDesc cbDesc;
std::shared_ptr<ICommandBuffer> buffer = cmdQueue.createCommandBuffer(cbDesc, nullptr);
Expand All @@ -154,7 +156,8 @@ void Framebuffer::copyTextureColorAttachment(ICommandQueue& cmdQueue,
const igl::vulkan::Texture& dstVkTex = static_cast<Texture&>(*destTexture);

// 1. Transition dst into TRANSFER_DST_OPTIMAL
ivkImageMemoryBarrier(cmdBuf,
ivkImageMemoryBarrier(&ctx.vf_,
cmdBuf,
dstVkTex.getVkImage(),
0, // srcAccessMask
VK_ACCESS_TRANSFER_WRITE_BIT, // dstAccessMask
Expand All @@ -178,13 +181,13 @@ void Framebuffer::copyTextureColorAttachment(ICommandQueue& cmdQueue,
VkImageSubresourceLayers{VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1},
VkExtent2D{static_cast<uint32_t>(range.width), static_cast<uint32_t>(range.height)});

vkCmdCopyImage(cmdBuf,
srcVkTex.getVkImage(),
VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
dstVkTex.getVkImage(),
VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
1,
&copy);
ctx.vf_.vkCmdCopyImage(cmdBuf,
srcVkTex.getVkImage(),
VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
dstVkTex.getVkImage(),
VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
1,
&copy);

// 4. Transition images back
srcVkTex.getVulkanTexture().getVulkanImage().transitionLayout(
Expand Down
5 changes: 3 additions & 2 deletions src/igl/vulkan/PlatformDevice.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -162,8 +162,9 @@ int PlatformDevice::getFenceFdFromSubmitHandle(SubmitHandle handle) const {
getFdInfo.handleType = VK_EXTERNAL_FENCE_HANDLE_TYPE_SYNC_FD_BIT;

int fenceFd = -1;
VkDevice vkDevice = device_.getVulkanContext().device_->getVkDevice();
VkResult result = vkGetFenceFdKHR(vkDevice, &getFdInfo, &fenceFd);
const auto& ctx = device_.getVulkanContext();
VkDevice vkDevice = ctx.device_->getVkDevice();
VkResult result = ctx.vf_.vkGetFenceFdKHR(vkDevice, &getFdInfo, &fenceFd);
if (result != VK_SUCCESS) {
IGL_LOG_ERROR("Unable to get fence fd from submit handle: %lu", handle);
}
Expand Down
Loading

0 comments on commit 4dba28c

Please sign in to comment.