Files
test2/source/blender/gpu/vulkan/vk_device.hh
Jeroen Bakker bee3f645d8 Vulkan: Rewrite GHOST_ContextVK
This is a rewrite of GHOST_ContextVK to align with Metal backend as described
in #111389 - solution 3 with the adaptation that GHOST is still responsible
for presenting the swap chain image and a post callback is still needed in
case the swapchain is recreated.

This PR also includes some smaller improvements in stability.

Technical documentation: https://developer.blender.org/docs/eevee_and_viewport/gpu/vulkan/swap_chain/

* Renderpasses and framebuffers are not owned anymore by GHOST_ContextVK
* VKFramebuffer doesn't contain a swap chain image.
* Swapchain images can only be used as a blit destination or present source.
  Not as an attachment.
* GHOST_ContextVK::swapBuffers would call a callback with the image the
  GPU module needs to blit the results to.
* Clearing of depth/stencil attachments when no depth write state is set.
* Enable VK_KHR_maintenance4 to relax the stage interface mapping.
* Removes most vulkan validation warnings/errors.
* Detection of frame buffer changes that needs to be applied before
  performing a command requiring render pass (draw/clear attachment)

**Benefits**

* Late retrieval of a swap chain image results in better overall performance as
  Blender doesn't need to wait until the image is presented on the screen.
* Easier API and clearer state (transitions)
* More control over Image layouts and command buffer states. (Better alignment with
  Vulkan API)

Pull Request: https://projects.blender.org/blender/blender/pulls/111473
2023-08-29 15:05:08 +02:00

181 lines
4.2 KiB
C++

/* SPDX-FileCopyrightText: 2023 Blender Authors
*
* SPDX-License-Identifier: GPL-2.0-or-later */
/** \file
* \ingroup gpu
*/
#pragma once
#include "BLI_utility_mixins.hh"
#include "BLI_vector.hh"
#include "vk_buffer.hh"
#include "vk_common.hh"
#include "vk_debug.hh"
#include "vk_descriptor_pools.hh"
#include "vk_sampler.hh"
namespace blender::gpu {
class VKBackend;
struct VKWorkarounds {
/**
* Some devices don't support pixel formats that are aligned to 24 and 48 bits.
* In this case we need to use a different texture format.
*
* If set to true we should work around this issue by using a different texture format.
*/
bool not_aligned_pixel_formats = false;
};
class VKDevice : public NonCopyable {
private:
/** Copies of the handles owned by the GHOST context. */
VkInstance vk_instance_ = VK_NULL_HANDLE;
VkPhysicalDevice vk_physical_device_ = VK_NULL_HANDLE;
VkDevice vk_device_ = VK_NULL_HANDLE;
uint32_t vk_queue_family_ = 0;
VkQueue vk_queue_ = VK_NULL_HANDLE;
VkCommandPool vk_command_pool_ = VK_NULL_HANDLE;
/* Dummy sampler for now. */
VKSampler sampler_;
/**
* Available Contexts for this device.
*
* Device keeps track of each contexts. When buffers/images are freed they need to be removed
* from all contexts state managers.
*
* The contexts inside this list aren't owned by the VKDevice. Caller of `GPU_context_create`
* holds the ownership.
*/
Vector<std::reference_wrapper<VKContext>> contexts_;
/** Allocator used for texture and buffers and other resources. */
VmaAllocator mem_allocator_ = VK_NULL_HANDLE;
VKDescriptorPools descriptor_pools_;
/** Limits of the device linked to this context. */
VkPhysicalDeviceProperties vk_physical_device_properties_ = {};
/** Functions of vk_ext_debugutils for this device/instance. */
debug::VKDebuggingTools debugging_tools_;
/* Workarounds */
VKWorkarounds workarounds_;
/** Buffer to bind to unbound resource locations. */
VKBuffer dummy_buffer_;
public:
VkPhysicalDevice physical_device_get() const
{
return vk_physical_device_;
}
const VkPhysicalDeviceProperties &physical_device_properties_get() const
{
return vk_physical_device_properties_;
}
VkInstance instance_get() const
{
return vk_instance_;
};
VkDevice device_get() const
{
return vk_device_;
}
VkQueue queue_get() const
{
return vk_queue_;
}
VKDescriptorPools &descriptor_pools_get()
{
return descriptor_pools_;
}
const uint32_t *queue_family_ptr_get() const
{
return &vk_queue_family_;
}
VmaAllocator mem_allocator_get() const
{
return mem_allocator_;
}
debug::VKDebuggingTools &debugging_tools_get()
{
return debugging_tools_;
}
const debug::VKDebuggingTools &debugging_tools_get() const
{
return debugging_tools_;
}
const VKSampler &sampler_get() const
{
return sampler_;
}
const VkCommandPool vk_command_pool_get() const
{
return vk_command_pool_;
}
bool is_initialized() const;
void init(void *ghost_context);
/**
* Initialize a dummy buffer that can be bound for missing attributes.
*
* Dummy buffer can only be initialized after the command buffer of the context is retrieved.
*/
void init_dummy_buffer(VKContext &context);
void deinit();
eGPUDeviceType device_type() const;
eGPUDriverType driver_type() const;
std::string vendor_name() const;
std::string driver_version() const;
const VKWorkarounds &workarounds_get() const
{
return workarounds_;
}
/* -------------------------------------------------------------------- */
/** \name Resource management
* \{ */
void context_register(VKContext &context);
void context_unregister(VKContext &context);
const Vector<std::reference_wrapper<VKContext>> &contexts_get() const;
const VKBuffer &dummy_buffer_get() const
{
return dummy_buffer_;
}
/** \} */
private:
void init_physical_device_properties();
void init_debug_callbacks();
void init_memory_allocator();
void init_command_pools();
void init_descriptor_pools();
/* During initialization the backend requires access to update the workarounds. */
friend VKBackend;
};
} // namespace blender::gpu