Skip to content

Commit

Permalink
Debug and cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
adrian-cojocaru committed Dec 16, 2024
1 parent 4fc0705 commit c6f08c6
Show file tree
Hide file tree
Showing 5 changed files with 99 additions and 53 deletions.
2 changes: 2 additions & 0 deletions include/mbgl/vulkan/renderer_backend.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ class RendererBackend : public gfx::RendererBackend {

void startFrameCapture();
void endFrameCapture();
void setFrameCaptureLoop(bool value);
void triggerFrameCapture(uint32_t frameCount = 1, uint32_t frameDelay = 0);

protected:
std::unique_ptr<gfx::Context> createContext() override;
Expand Down
13 changes: 10 additions & 3 deletions include/mbgl/vulkan/uniform_buffer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ class UniformBuffer final : public gfx::UniformBuffer {
~UniformBuffer() override;

const BufferResource& getBufferResource() const { return buffer; }
BufferResource& mutableBufferResource() { return buffer; }

UniformBuffer clone() const { return {buffer.clone()}; }

Expand All @@ -35,14 +36,20 @@ class UniformBufferArray final : public gfx::UniformBufferArray {
descriptorStartIndex(descriptorStartIndex_),
descriptorBindingCount(descriptorBindingCount_) {}

UniformBufferArray(UniformBufferArray&& other)
: gfx::UniformBufferArray(std::move(other)) {}
UniformBufferArray(UniformBufferArray&& other) noexcept
: gfx::UniformBufferArray(std::move(other)),
descriptorSetType(other.descriptorSetType),
descriptorStartIndex(other.descriptorStartIndex),
descriptorBindingCount(other.descriptorBindingCount),
descriptorSet(std::move(other.descriptorSet)) {}

UniformBufferArray(const UniformBufferArray&) = delete;

UniformBufferArray& operator=(UniformBufferArray&& other) {
UniformBufferArray& operator=(UniformBufferArray&& other) noexcept {
gfx::UniformBufferArray::operator=(std::move(other));
return *this;
}

UniformBufferArray& operator=(const UniformBufferArray& other) {
gfx::UniformBufferArray::operator=(other);
return *this;
Expand Down
2 changes: 1 addition & 1 deletion src/mbgl/vulkan/context.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -559,7 +559,7 @@ bool Context::renderTileClippingMasks(gfx::RenderPass& renderPass,
const std::unique_ptr<BufferResource>& Context::getDummyVertexBuffer() {
if (!dummyVertexBuffer)
dummyVertexBuffer = std::make_unique<BufferResource>(
*this, nullptr, 16, VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT, false);
*this, nullptr, 16, VK_BUFFER_USAGE_VERTEX_BUFFER_BIT, false);
return dummyVertexBuffer;
}

Expand Down
87 changes: 45 additions & 42 deletions src/mbgl/vulkan/renderable_resource.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,8 @@ void SurfaceRenderableResource::initDepthStencil() {
.setViewType(vk::ImageViewType::e2D)
.setFormat(depthFormat)
.setComponents(vk::ComponentMapping()) // defaults to vk::ComponentSwizzle::eIdentity
.setSubresourceRange(vk::ImageSubresourceRange(vk::ImageAspectFlagBits::eDepth, 0, 1, 0, 1));
.setSubresourceRange(vk::ImageSubresourceRange(
vk::ImageAspectFlagBits::eDepth | vk::ImageAspectFlagBits::eStencil, 0, 1, 0, 1));

depthAllocation->imageView = device->createImageViewUnique(imageViewCreateInfo);

Expand Down Expand Up @@ -307,28 +308,30 @@ void SurfaceRenderableResource::init(uint32_t w, uint32_t h) {

// create render pass
const auto colorLayout = surface ? vk::ImageLayout::ePresentSrcKHR : vk::ImageLayout::eTransferSrcOptimal;
const auto colorAttachment = vk::AttachmentDescription(vk::AttachmentDescriptionFlags())
.setFormat(colorFormat)
.setSamples(vk::SampleCountFlagBits::e1)
.setLoadOp(vk::AttachmentLoadOp::eClear)
.setStoreOp(vk::AttachmentStoreOp::eStore)
.setStencilLoadOp(vk::AttachmentLoadOp::eDontCare)
.setStencilStoreOp(vk::AttachmentStoreOp::eDontCare)
.setInitialLayout(vk::ImageLayout::eUndefined)
.setFinalLayout(colorLayout);

const vk::AttachmentReference colorAttachmentRef(0, vk::ImageLayout::eColorAttachmentOptimal);

const auto depthAttachment = vk::AttachmentDescription()
.setFormat(depthFormat)
.setSamples(vk::SampleCountFlagBits::e1)
.setLoadOp(vk::AttachmentLoadOp::eClear)
.setStoreOp(vk::AttachmentStoreOp::eDontCare)
.setStencilLoadOp(vk::AttachmentLoadOp::eClear)
.setStencilStoreOp(vk::AttachmentStoreOp::eDontCare)
.setInitialLayout(vk::ImageLayout::eUndefined)
.setFinalLayout(vk::ImageLayout::eDepthStencilAttachmentOptimal);
const std::array<vk::AttachmentDescription, 2> attachments = {
vk::AttachmentDescription()
.setFormat(colorFormat)
.setSamples(vk::SampleCountFlagBits::e1)
.setLoadOp(vk::AttachmentLoadOp::eClear)
.setStoreOp(vk::AttachmentStoreOp::eStore)
.setStencilLoadOp(vk::AttachmentLoadOp::eDontCare)
.setStencilStoreOp(vk::AttachmentStoreOp::eDontCare)
.setInitialLayout(vk::ImageLayout::eUndefined)
.setFinalLayout(colorLayout),

vk::AttachmentDescription()
.setFormat(depthFormat)
.setSamples(vk::SampleCountFlagBits::e1)
.setLoadOp(vk::AttachmentLoadOp::eClear)
.setStoreOp(vk::AttachmentStoreOp::eDontCare)
.setStencilLoadOp(vk::AttachmentLoadOp::eClear)
.setStencilStoreOp(vk::AttachmentStoreOp::eDontCare)
.setInitialLayout(vk::ImageLayout::eUndefined)
.setFinalLayout(vk::ImageLayout::eDepthStencilAttachmentOptimal)
};

const vk::AttachmentReference colorAttachmentRef(0, vk::ImageLayout::eColorAttachmentOptimal);
const vk::AttachmentReference depthAttachmentRef(1, vk::ImageLayout::eDepthStencilAttachmentOptimal);

const auto subpass = vk::SubpassDescription()
Expand All @@ -337,28 +340,28 @@ void SurfaceRenderableResource::init(uint32_t w, uint32_t h) {
.setColorAttachments(colorAttachmentRef)
.setPDepthStencilAttachment(&depthAttachmentRef);

const auto subpassSrcStageMask = vk::PipelineStageFlags() | vk::PipelineStageFlagBits::eColorAttachmentOutput |
vk::PipelineStageFlagBits::eLateFragmentTests;

const auto subpassDstStageMask = vk::PipelineStageFlags() | vk::PipelineStageFlagBits::eColorAttachmentOutput |
vk::PipelineStageFlagBits::eEarlyFragmentTests;

const auto subpassSrcAccessMask = vk::AccessFlags() | vk::AccessFlagBits::eDepthStencilAttachmentWrite;

const auto subpassDstAccessMask = vk::AccessFlags() | vk::AccessFlagBits::eColorAttachmentWrite |
vk::AccessFlagBits::eDepthStencilAttachmentWrite;

const auto subpassDependency = vk::SubpassDependency()
.setSrcSubpass(VK_SUBPASS_EXTERNAL)
.setDstSubpass(0)
.setSrcStageMask(subpassSrcStageMask)
.setDstStageMask(subpassDstStageMask)
.setSrcAccessMask(subpassSrcAccessMask)
.setDstAccessMask(subpassDstAccessMask);

const std::array<vk::AttachmentDescription, 2> attachments = {colorAttachment, depthAttachment};
const std::array<vk::SubpassDependency, 2> dependencies = {
vk::SubpassDependency()
.setSrcSubpass(VK_SUBPASS_EXTERNAL)
.setDstSubpass(0)
.setSrcStageMask(vk::PipelineStageFlagBits::eColorAttachmentOutput)
.setDstStageMask(vk::PipelineStageFlagBits::eColorAttachmentOutput)
.setSrcAccessMask({})
.setDstAccessMask(vk::AccessFlagBits::eColorAttachmentWrite),
vk::SubpassDependency()
.setSrcSubpass(VK_SUBPASS_EXTERNAL)
.setDstSubpass(0)
.setSrcStageMask(vk::PipelineStageFlagBits::eEarlyFragmentTests |
vk::PipelineStageFlagBits::eLateFragmentTests)
.setDstStageMask(vk::PipelineStageFlagBits::eEarlyFragmentTests |
vk::PipelineStageFlagBits::eLateFragmentTests)
.setSrcAccessMask({})
.setDstAccessMask(vk::AccessFlagBits::eDepthStencilAttachmentWrite),
};

const auto renderPassCreateInfo =
vk::RenderPassCreateInfo().setAttachments(attachments).setSubpasses(subpass).setDependencies(subpassDependency);
vk::RenderPassCreateInfo().setAttachments(attachments).setSubpasses(subpass).setDependencies(dependencies);

renderPass = device->createRenderPassUnique(renderPassCreateInfo);

Expand Down
48 changes: 41 additions & 7 deletions src/mbgl/vulkan/renderer_backend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,13 @@ VULKAN_HPP_DEFAULT_DISPATCH_LOADER_DYNAMIC_STORAGE
#endif

#include "renderdoc_app.h"
static RENDERDOC_API_1_1_2* g_rdoc_api = nullptr;

static struct {
RENDERDOC_API_1_1_2* api = nullptr;
bool loop = false;
int32_t frameDelay = 0;
uint32_t frameCaptureCount = 0;
} g_rdoc;

#endif

Expand Down Expand Up @@ -183,13 +189,13 @@ void RendererBackend::initFrameCapture() {
#ifdef _WIN32
if (HMODULE mod = GetModuleHandleA("renderdoc.dll")) {
pRENDERDOC_GetAPI RENDERDOC_GetAPI = (pRENDERDOC_GetAPI)GetProcAddress(mod, "RENDERDOC_GetAPI");
int ret = RENDERDOC_GetAPI(eRENDERDOC_API_Version_1_1_2, (void**)&g_rdoc_api);
int ret = RENDERDOC_GetAPI(eRENDERDOC_API_Version_1_1_2, (void**)&g_rdoc.api);
assert(ret == 1);
}
#elif __unix__
if (void* mod = dlopen("librenderdoc.so", RTLD_NOW | RTLD_NOLOAD)) {
pRENDERDOC_GetAPI RENDERDOC_GetAPI = (pRENDERDOC_GetAPI)dlsym(mod, "RENDERDOC_GetAPI");
int ret = RENDERDOC_GetAPI(eRENDERDOC_API_Version_1_1_2, (void**)&g_rdoc_api);
int ret = RENDERDOC_GetAPI(eRENDERDOC_API_Version_1_1_2, (void**)&g_rdoc.api);
assert(ret == 1);
}
#endif
Expand All @@ -199,19 +205,47 @@ void RendererBackend::initFrameCapture() {

void RendererBackend::startFrameCapture() {
#ifdef ENABLE_RENDERDOC_FRAME_CAPTURE
if (g_rdoc_api) {
if (!g_rdoc.api) {
return;
}

if (g_rdoc.loop) {
RENDERDOC_DevicePointer devicePtr = RENDERDOC_DEVICEPOINTER_FROM_VKINSTANCE(instance->operator VkInstance_T*());
g_rdoc_api->StartFrameCapture(devicePtr, nullptr);
g_rdoc.api->StartFrameCapture(devicePtr, nullptr);
} else {

if (g_rdoc.frameCaptureCount > 0 && g_rdoc.frameDelay == 0) {
g_rdoc.api->TriggerMultiFrameCapture(g_rdoc.frameCaptureCount);
}

--g_rdoc.frameDelay;
}
#endif
}

void RendererBackend::endFrameCapture() {
#ifdef ENABLE_RENDERDOC_FRAME_CAPTURE
if (g_rdoc_api) {
if (g_rdoc.api && g_rdoc.loop) {
RENDERDOC_DevicePointer devicePtr = RENDERDOC_DEVICEPOINTER_FROM_VKINSTANCE(instance->operator VkInstance_T*());
g_rdoc_api->EndFrameCapture(devicePtr, nullptr);
g_rdoc.api->EndFrameCapture(devicePtr, nullptr);
}
#endif
}

void RendererBackend::setFrameCaptureLoop(bool value) {
#ifdef ENABLE_RENDERDOC_FRAME_CAPTURE
g_rdoc.loop = value;
#endif
}

void RendererBackend::triggerFrameCapture(uint32_t frameCount, uint32_t frameDelay) {
#ifdef ENABLE_RENDERDOC_FRAME_CAPTURE
if (!g_rdoc.api) {
return;
}

g_rdoc.frameCaptureCount = frameCount;
g_rdoc.frameDelay = frameDelay;
#endif
}

Expand Down

0 comments on commit c6f08c6

Please sign in to comment.