Skip to content

Commit

Permalink
vulkan: clean-up includes and refactor handle allocator
Browse files Browse the repository at this point in the history
  • Loading branch information
poweifeng committed Aug 9, 2023
1 parent bcfdf2f commit e0aa46f
Show file tree
Hide file tree
Showing 8 changed files with 179 additions and 169 deletions.
2 changes: 2 additions & 0 deletions filament/backend/src/vulkan/VulkanBlitter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@

#include "VulkanBlitter.h"
#include "VulkanContext.h"
#include "VulkanFboCache.h"
#include "VulkanHandles.h"
#include "VulkanSamplerCache.h"

#include <utils/FixedCapacityVector.h>
#include <utils/Panic.h>
Expand Down
1 change: 0 additions & 1 deletion filament/backend/src/vulkan/VulkanCommands.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@

#include "VulkanConstants.h"
#include "VulkanContext.h"
#include "VulkanDriver.h"

#include <utils/Log.h>
#include <utils/Panic.h>
Expand Down
2 changes: 2 additions & 0 deletions filament/backend/src/vulkan/VulkanCommands.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@

#include <bluevk/BlueVK.h>

#include "DriverBase.h"

#include "VulkanConstants.h"

#include <utils/Condition.h>
Expand Down
191 changes: 96 additions & 95 deletions filament/backend/src/vulkan/VulkanDriver.cpp

Large diffs are not rendered by default.

136 changes: 69 additions & 67 deletions filament/backend/src/vulkan/VulkanDriver.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,37 +17,92 @@
#ifndef TNT_FILAMENT_BACKEND_VULKANDRIVER_H
#define TNT_FILAMENT_BACKEND_VULKANDRIVER_H

#include "VulkanPipelineCache.h"
#include "VulkanBlitter.h"
#include "VulkanDisposer.h"
#include "VulkanConstants.h"
#include "VulkanContext.h"
#include "VulkanDisposer.h"
#include "VulkanFboCache.h"
#include "VulkanHandles.h"
#include "VulkanPipelineCache.h"
#include "VulkanReadPixels.h"
#include "VulkanSamplerCache.h"
#include "VulkanStagePool.h"
#include "VulkanUtility.h"

#include "DriverBase.h"
#include "private/backend/Driver.h"
#include "private/backend/HandleAllocator.h"
#include "DriverBase.h"

#include <utils/compiler.h>
#include <utils/Allocator.h>
#include <utils/compiler.h>

namespace filament::backend {

class VulkanPlatform;
struct VulkanSamplerGroup;

class VulkanHandleAllocator {
public:
VulkanHandleAllocator(size_t arenaSize)
: mHandleAllocatorImpl("Handles", arenaSize) {}

template<typename D, typename... ARGS>
inline Handle<D> initHandle(ARGS&&... args) noexcept {
return mHandleAllocatorImpl.allocateAndConstruct<D>(std::forward<ARGS>(args)...);
}

template<typename D>
inline Handle<D> allocHandle() noexcept {
return mHandleAllocatorImpl.allocate<D>();
}

template<typename D, typename B, typename... ARGS>
inline typename std::enable_if<std::is_base_of<B, D>::value, D>::type* construct(
Handle<B> const& handle, ARGS&&... args) noexcept {
return mHandleAllocatorImpl.construct<D, B>(handle, std::forward<ARGS>(args)...);
}

template<typename B, typename D,
typename = typename std::enable_if<std::is_base_of<B, D>::value, D>::type>
inline void destruct(Handle<B> handle, D const* p) noexcept {
return mHandleAllocatorImpl.deallocate(handle, p);
}

template<typename Dp, typename B>
inline typename std::enable_if_t<
std::is_pointer_v<Dp> && std::is_base_of_v<B, typename std::remove_pointer_t<Dp>>, Dp>
handle_cast(Handle<B>& handle) noexcept {
return mHandleAllocatorImpl.handle_cast<Dp, B>(handle);
}

template<typename Dp, typename B>
inline typename std::enable_if_t<
std::is_pointer_v<Dp> && std::is_base_of_v<B, typename std::remove_pointer_t<Dp>>, Dp>
handle_cast(Handle<B> const& handle) noexcept {
return mHandleAllocatorImpl.handle_cast<Dp, B>(handle);
}

template<typename D, typename B>
inline void destruct(Handle<B> handle) noexcept {
if constexpr (std::is_base_of_v<VulkanIndexBuffer, D>
|| std::is_base_of_v<VulkanBufferObject, D>) {
auto ptr = handle_cast<D*>(handle);
ptr->terminate();
}
destruct(handle, handle_cast<D const*>(handle));
}

HandleAllocatorVK mHandleAllocatorImpl;
};

class VulkanDriver final : public DriverBase {
public:
static Driver* create(VulkanPlatform* platform, VulkanContext const& context,
Platform::DriverConfig const& driverConfig) noexcept;

private:

void debugCommandBegin(CommandStream* cmds, bool synchronous, const char* methodName) noexcept override;
void debugCommandBegin(CommandStream* cmds, bool synchronous,
const char* methodName) noexcept override;

inline VulkanDriver(VulkanPlatform* platform, VulkanContext const& context,
Platform::DriverConfig const& driverConfig) noexcept;
Expand All @@ -61,77 +116,24 @@ class VulkanDriver final : public DriverBase {
template<typename T>
friend class ConcreteDispatcher;

#define DECL_DRIVER_API(methodName, paramsDecl, params) \
#define DECL_DRIVER_API(methodName, paramsDecl, params) \
UTILS_ALWAYS_INLINE inline void methodName(paramsDecl);

#define DECL_DRIVER_API_SYNCHRONOUS(RetType, methodName, paramsDecl, params) \
#define DECL_DRIVER_API_SYNCHRONOUS(RetType, methodName, paramsDecl, params) \
RetType methodName(paramsDecl) override;

#define DECL_DRIVER_API_RETURN(RetType, methodName, paramsDecl, params) \
RetType methodName##S() noexcept override; \
#define DECL_DRIVER_API_RETURN(RetType, methodName, paramsDecl, params) \
RetType methodName##S() noexcept override; \
UTILS_ALWAYS_INLINE inline void methodName##R(RetType, paramsDecl);

#include "private/backend/DriverAPI.inc"

VulkanDriver(VulkanDriver const&) = delete;
VulkanDriver& operator = (VulkanDriver const&) = delete;
VulkanDriver& operator=(VulkanDriver const&) = delete;

private:

template<typename D, typename ... ARGS>
Handle<D> initHandle(ARGS&& ... args) noexcept {
return mHandleAllocator.allocateAndConstruct<D>(std::forward<ARGS>(args) ...);
}

template<typename D>
Handle<D> allocHandle() noexcept {
return mHandleAllocator.allocate<D>();
}

template<typename D, typename B, typename ... ARGS>
typename std::enable_if<std::is_base_of<B, D>::value, D>::type*
construct(Handle<B> const& handle, ARGS&& ... args) noexcept {
return mHandleAllocator.construct<D, B>(handle, std::forward<ARGS>(args) ...);
}

template<typename B, typename D,
typename = typename std::enable_if<std::is_base_of<B, D>::value, D>::type>
void destruct(Handle<B> handle, D const* p) noexcept {
return mHandleAllocator.deallocate(handle, p);
}

template<typename Dp, typename B>
typename std::enable_if_t<
std::is_pointer_v<Dp> &&
std::is_base_of_v<B, typename std::remove_pointer_t<Dp>>, Dp>
handle_cast(Handle<B>& handle) noexcept {
return mHandleAllocator.handle_cast<Dp, B>(handle);
}

template<typename Dp, typename B>
inline typename std::enable_if_t<
std::is_pointer_v<Dp> &&
std::is_base_of_v<B, typename std::remove_pointer_t<Dp>>, Dp>
handle_cast(Handle<B> const& handle) noexcept {
return mHandleAllocator.handle_cast<Dp, B>(handle);
}

template<typename D, typename B>
void destruct(Handle<B> handle) noexcept {
destruct(handle, handle_cast<D const*>(handle));
}

// This version of destruct takes a VulkanContext and calls a terminate(VulkanContext&)
// on the handle before calling the dtor
template<typename Dp, typename B>
void destructBuffer(Handle<B> handle) noexcept {
auto ptr = handle_cast<Dp*>(handle);
ptr->terminate();
mHandleAllocator.deallocate(handle, ptr);
}

inline void setRenderPrimitiveBuffer(Handle<HwRenderPrimitive> rph,
Handle<HwVertexBuffer> vbh, Handle<HwIndexBuffer> ibh);
inline void setRenderPrimitiveBuffer(Handle<HwRenderPrimitive> rph, Handle<HwVertexBuffer> vbh,
Handle<HwIndexBuffer> ibh);

inline void setRenderPrimitiveRange(Handle<HwRenderPrimitive> rph, PrimitiveType pt,
uint32_t offset, uint32_t minIndex, uint32_t maxIndex, uint32_t count);
Expand All @@ -151,7 +153,7 @@ class VulkanDriver final : public DriverBase {
VkDebugUtilsMessengerEXT mDebugMessenger = VK_NULL_HANDLE;

VulkanContext mContext = {};
HandleAllocatorVK mHandleAllocator;
VulkanHandleAllocator mHandleAllocator;
VulkanPipelineCache mPipelineCache;
VulkanDisposer mDisposer;
VulkanStagePool mStagePool;
Expand Down
10 changes: 6 additions & 4 deletions filament/backend/src/vulkan/VulkanHandles.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,12 @@
* limitations under the License.
*/

#ifndef TNT_FILAMENT_BACKEND_VULKANHANDLES_H
#define TNT_FILAMENT_BACKEND_VULKANHANDLES_H
#ifndef TNT_FILAMENT_BACKEND_VULKANHANDLES_H
#define TNT_FILAMENT_BACKEND_VULKANHANDLES_H

// This needs to be at the top
#include "DriverBase.h"

#include "VulkanDriver.h"
#include "VulkanPipelineCache.h"
#include "VulkanBuffer.h"
#include "VulkanSwapChain.h"
Expand All @@ -26,7 +28,7 @@

#include "private/backend/SamplerGroup.h"

#include "utils/Mutex.h"
#include <utils/Mutex.h>

namespace filament::backend {

Expand Down
3 changes: 2 additions & 1 deletion filament/backend/src/vulkan/VulkanSwapChain.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@
#ifndef TNT_FILAMENT_BACKEND_VULKANSWAPCHAIN_H
#define TNT_FILAMENT_BACKEND_VULKANSWAPCHAIN_H

#include "DriverBase.h"

#include "VulkanContext.h"
#include "VulkanDriver.h"

#include <backend/platforms/VulkanPlatform.h>

Expand Down
3 changes: 2 additions & 1 deletion filament/backend/src/vulkan/VulkanTexture.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
#ifndef TNT_FILAMENT_BACKEND_VULKANTEXTURE_H
#define TNT_FILAMENT_BACKEND_VULKANTEXTURE_H

#include "VulkanDriver.h"
#include "DriverBase.h"

#include "VulkanBuffer.h"
#include "VulkanImageUtility.h"

Expand Down

0 comments on commit e0aa46f

Please sign in to comment.