2020-03-08 13:38:40 +01:00
|
|
|
#pragma once
|
2020-03-09 12:30:07 +01:00
|
|
|
#include "Graphics/GraphicsResources.h"
|
2020-03-13 12:44:33 +01:00
|
|
|
#include <vulkan/vulkan.h>
|
2020-03-09 12:30:07 +01:00
|
|
|
|
|
|
|
|
namespace Seele
|
|
|
|
|
{
|
2020-03-24 21:05:32 +01:00
|
|
|
namespace Vulkan
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
DECLARE_REF(DescriptorAllocator);
|
|
|
|
|
DECLARE_REF(CommandBufferManager);
|
|
|
|
|
DECLARE_REF(Graphics);
|
2020-04-12 15:47:19 +02:00
|
|
|
DECLARE_REF(SubAllocation);
|
2020-03-24 21:05:32 +01:00
|
|
|
class Semaphore
|
|
|
|
|
{
|
|
|
|
|
public:
|
2020-04-01 02:17:49 +02:00
|
|
|
Semaphore(PGraphics graphics);
|
2020-03-24 21:05:32 +01:00
|
|
|
virtual ~Semaphore();
|
2020-04-01 02:17:49 +02:00
|
|
|
inline VkSemaphore getHandle() const
|
|
|
|
|
{
|
|
|
|
|
return handle;
|
|
|
|
|
}
|
2020-04-12 15:47:19 +02:00
|
|
|
|
2020-03-24 21:05:32 +01:00
|
|
|
private:
|
|
|
|
|
VkSemaphore handle;
|
2020-04-01 02:17:49 +02:00
|
|
|
PGraphics graphics;
|
2020-03-24 21:05:32 +01:00
|
|
|
};
|
|
|
|
|
DEFINE_REF(Semaphore);
|
|
|
|
|
|
2020-04-12 15:47:19 +02:00
|
|
|
class Fence
|
2020-03-24 21:05:32 +01:00
|
|
|
{
|
|
|
|
|
public:
|
2020-04-12 15:47:19 +02:00
|
|
|
Fence(PGraphics graphics);
|
|
|
|
|
~Fence();
|
|
|
|
|
bool isSignaled();
|
|
|
|
|
void reset();
|
|
|
|
|
inline VkFence getHandle() const
|
2020-03-09 12:30:07 +01:00
|
|
|
{
|
2020-04-12 15:47:19 +02:00
|
|
|
return fence;
|
2020-03-20 01:27:40 +01:00
|
|
|
}
|
2020-04-12 15:47:19 +02:00
|
|
|
void wait(uint32 timeout);
|
2020-05-05 01:51:13 +02:00
|
|
|
bool operator<(const Fence &other) const
|
|
|
|
|
{
|
|
|
|
|
return fence < other.fence;
|
|
|
|
|
}
|
2020-03-24 21:05:32 +01:00
|
|
|
|
|
|
|
|
private:
|
2020-04-12 15:47:19 +02:00
|
|
|
bool signaled;
|
|
|
|
|
VkFence fence;
|
2020-04-01 02:17:49 +02:00
|
|
|
PGraphics graphics;
|
2020-03-24 21:05:32 +01:00
|
|
|
};
|
2020-04-12 15:47:19 +02:00
|
|
|
DEFINE_REF(Fence);
|
2020-03-24 21:05:32 +01:00
|
|
|
|
2020-05-05 01:51:13 +02:00
|
|
|
class QueueOwnedResourceDeletion
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
QueueOwnedResourceDeletion();
|
|
|
|
|
virtual ~QueueOwnedResourceDeletion();
|
|
|
|
|
static void addPendingDelete(PFence fence, std::function<void()> function);
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
std::thread worker;
|
|
|
|
|
static volatile bool running;
|
|
|
|
|
static void run();
|
|
|
|
|
struct PendingItem
|
|
|
|
|
{
|
|
|
|
|
PFence fence;
|
|
|
|
|
std::function<void()> func;
|
|
|
|
|
};
|
|
|
|
|
static std::mutex mutex;
|
|
|
|
|
static std::condition_variable cv;
|
|
|
|
|
static List<PendingItem> deletionQueue;
|
|
|
|
|
};
|
2020-03-24 21:05:32 +01:00
|
|
|
|
2020-06-02 11:46:18 +02:00
|
|
|
class Buffer
|
2020-03-24 21:05:32 +01:00
|
|
|
{
|
|
|
|
|
public:
|
2020-04-12 15:47:19 +02:00
|
|
|
Buffer(PGraphics graphics, uint32 size, VkBufferUsageFlags usage, Gfx::QueueType queueType);
|
2020-03-24 21:05:32 +01:00
|
|
|
virtual ~Buffer();
|
2020-04-12 15:47:19 +02:00
|
|
|
VkBuffer getHandle() const
|
|
|
|
|
{
|
|
|
|
|
return buffers[currentBuffer].buffer;
|
|
|
|
|
}
|
|
|
|
|
void advanceBuffer()
|
|
|
|
|
{
|
|
|
|
|
currentBuffer = (currentBuffer + 1) % numBuffers;
|
|
|
|
|
}
|
|
|
|
|
void *lock(bool bWriteOnly = true);
|
|
|
|
|
void unlock();
|
|
|
|
|
|
|
|
|
|
protected:
|
|
|
|
|
struct BufferAllocation
|
|
|
|
|
{
|
|
|
|
|
VkBuffer buffer;
|
|
|
|
|
PSubAllocation allocation;
|
|
|
|
|
};
|
|
|
|
|
BufferAllocation buffers[Gfx::numFramesBuffered];
|
|
|
|
|
uint32 numBuffers;
|
|
|
|
|
uint32 currentBuffer;
|
|
|
|
|
uint32 size;
|
2020-06-02 11:46:18 +02:00
|
|
|
PGraphics graphics;
|
|
|
|
|
Gfx::QueueType currentOwner;
|
|
|
|
|
|
|
|
|
|
void executeOwnershipBarrier(Gfx::QueueType newOwner);
|
|
|
|
|
virtual void requestOwnershipTransfer(Gfx::QueueType newOwner) = 0;
|
2020-03-24 21:05:32 +01:00
|
|
|
|
|
|
|
|
virtual VkAccessFlags getSourceAccessMask() = 0;
|
|
|
|
|
virtual VkAccessFlags getDestAccessMask() = 0;
|
|
|
|
|
};
|
|
|
|
|
DEFINE_REF(Buffer);
|
|
|
|
|
|
|
|
|
|
class UniformBuffer : public Buffer, public Gfx::UniformBuffer
|
|
|
|
|
{
|
2020-04-12 15:47:19 +02:00
|
|
|
public:
|
|
|
|
|
UniformBuffer(PGraphics graphics, const BulkResourceData &resourceData);
|
|
|
|
|
virtual ~UniformBuffer();
|
|
|
|
|
|
|
|
|
|
protected:
|
2020-06-02 11:46:18 +02:00
|
|
|
// Inherited via Vulkan::Buffer
|
|
|
|
|
virtual void requestOwnershipTransfer(Gfx::QueueType newOwner);
|
2020-04-12 15:47:19 +02:00
|
|
|
virtual VkAccessFlags getSourceAccessMask();
|
|
|
|
|
virtual VkAccessFlags getDestAccessMask();
|
2020-06-02 11:46:18 +02:00
|
|
|
// Inherited via QueueOwnedResource
|
|
|
|
|
virtual void executeOwnershipBarrier(Gfx::QueueType newOwner);
|
2020-03-24 21:05:32 +01:00
|
|
|
};
|
|
|
|
|
DEFINE_REF(UniformBuffer);
|
|
|
|
|
|
|
|
|
|
class StructuredBuffer : public Buffer, public Gfx::StructuredBuffer
|
|
|
|
|
{
|
2020-04-12 15:47:19 +02:00
|
|
|
public:
|
|
|
|
|
StructuredBuffer(PGraphics graphics, const BulkResourceData &resourceData);
|
|
|
|
|
virtual ~StructuredBuffer();
|
|
|
|
|
|
|
|
|
|
protected:
|
2020-06-02 11:46:18 +02:00
|
|
|
// Inherited via Vulkan::Buffer
|
2020-04-12 15:47:19 +02:00
|
|
|
virtual VkAccessFlags getSourceAccessMask();
|
|
|
|
|
virtual VkAccessFlags getDestAccessMask();
|
2020-06-02 11:46:18 +02:00
|
|
|
virtual void requestOwnershipTransfer(Gfx::QueueType newOwner);
|
|
|
|
|
// Inherited via QueueOwnedResource
|
|
|
|
|
virtual void executeOwnershipBarrier(Gfx::QueueType newOwner);
|
2020-03-24 21:05:32 +01:00
|
|
|
};
|
|
|
|
|
DEFINE_REF(StructuredBuffer);
|
|
|
|
|
|
2020-04-12 15:47:19 +02:00
|
|
|
class VertexBuffer : public Buffer, public Gfx::VertexBuffer
|
2020-03-24 21:05:32 +01:00
|
|
|
{
|
|
|
|
|
public:
|
2020-05-05 01:51:13 +02:00
|
|
|
VertexBuffer(PGraphics graphics, const VertexBufferCreateInfo &resourceData);
|
2020-04-12 15:47:19 +02:00
|
|
|
virtual ~VertexBuffer();
|
2020-04-01 02:17:49 +02:00
|
|
|
|
2020-04-12 15:47:19 +02:00
|
|
|
protected:
|
2020-06-02 11:46:18 +02:00
|
|
|
// Inherited via Vulkan::Buffer
|
2020-04-12 15:47:19 +02:00
|
|
|
virtual VkAccessFlags getSourceAccessMask();
|
|
|
|
|
virtual VkAccessFlags getDestAccessMask();
|
2020-06-02 11:46:18 +02:00
|
|
|
virtual void requestOwnershipTransfer(Gfx::QueueType newOwner);
|
|
|
|
|
// Inherited via QueueOwnedResource
|
|
|
|
|
virtual void executeOwnershipBarrier(Gfx::QueueType newOwner);
|
2020-04-12 15:47:19 +02:00
|
|
|
};
|
|
|
|
|
DEFINE_REF(VertexBuffer);
|
|
|
|
|
|
|
|
|
|
class IndexBuffer : public Buffer, public Gfx::IndexBuffer
|
|
|
|
|
{
|
|
|
|
|
public:
|
2020-05-05 01:51:13 +02:00
|
|
|
IndexBuffer(PGraphics graphics, const IndexBufferCreateInfo &resourceData);
|
2020-04-12 15:47:19 +02:00
|
|
|
virtual ~IndexBuffer();
|
|
|
|
|
|
|
|
|
|
protected:
|
2020-06-02 11:46:18 +02:00
|
|
|
// Inherited via Vulkan::Buffer
|
|
|
|
|
virtual void requestOwnershipTransfer(Gfx::QueueType newOwner);
|
2020-04-12 15:47:19 +02:00
|
|
|
virtual VkAccessFlags getSourceAccessMask();
|
|
|
|
|
virtual VkAccessFlags getDestAccessMask();
|
2020-06-02 11:46:18 +02:00
|
|
|
// Inherited via QueueOwnedResource
|
|
|
|
|
virtual void executeOwnershipBarrier(Gfx::QueueType newOwner);
|
2020-04-12 15:47:19 +02:00
|
|
|
};
|
|
|
|
|
DEFINE_REF(IndexBuffer);
|
|
|
|
|
|
2020-06-02 11:46:18 +02:00
|
|
|
class TextureHandle
|
2020-04-12 15:47:19 +02:00
|
|
|
{
|
|
|
|
|
public:
|
2020-06-02 11:46:18 +02:00
|
|
|
TextureHandle(PGraphics graphics, VkImageViewType viewType,
|
|
|
|
|
const TextureCreateInfo& createInfo, VkImage existingImage = VK_NULL_HANDLE);
|
2020-04-12 15:47:19 +02:00
|
|
|
virtual ~TextureHandle();
|
|
|
|
|
|
|
|
|
|
inline VkImageView getView() const
|
|
|
|
|
{
|
|
|
|
|
return defaultView;
|
|
|
|
|
}
|
|
|
|
|
inline VkImageLayout getLayout() const
|
|
|
|
|
{
|
|
|
|
|
return layout;
|
|
|
|
|
}
|
|
|
|
|
inline VkImageAspectFlags getAspect() const
|
|
|
|
|
{
|
|
|
|
|
return aspect;
|
|
|
|
|
}
|
|
|
|
|
inline VkImageUsageFlags getUsage() const
|
|
|
|
|
{
|
|
|
|
|
return usage;
|
|
|
|
|
}
|
|
|
|
|
inline Gfx::SeFormat getFormat() const
|
|
|
|
|
{
|
|
|
|
|
return format;
|
|
|
|
|
}
|
|
|
|
|
inline bool isDepthStencil() const
|
|
|
|
|
{
|
|
|
|
|
return aspect & (VK_IMAGE_ASPECT_DEPTH_BIT | VK_IMAGE_ASPECT_STENCIL_BIT);
|
|
|
|
|
}
|
2020-06-02 11:46:18 +02:00
|
|
|
void executeOwnershipBarrier(Gfx::QueueType newOwner);
|
|
|
|
|
void changeLayout(VkImageLayout newLayout);
|
2020-04-12 15:47:19 +02:00
|
|
|
|
2020-06-02 11:46:18 +02:00
|
|
|
Gfx::QueueType currentOwner;
|
2020-04-12 15:47:19 +02:00
|
|
|
private:
|
2020-04-01 02:17:49 +02:00
|
|
|
PGraphics graphics;
|
2020-04-12 15:47:19 +02:00
|
|
|
PSubAllocation allocation;
|
2020-03-24 21:05:32 +01:00
|
|
|
uint32 sizeX;
|
|
|
|
|
uint32 sizeY;
|
|
|
|
|
uint32 sizeZ;
|
|
|
|
|
uint32 arrayCount;
|
2020-04-01 02:17:49 +02:00
|
|
|
uint32 mipLevels;
|
2020-04-12 15:47:19 +02:00
|
|
|
uint32 samples;
|
2020-04-01 02:17:49 +02:00
|
|
|
Gfx::SeFormat format;
|
2020-04-12 15:47:19 +02:00
|
|
|
Gfx::SeImageUsageFlags usage;
|
2020-03-24 21:05:32 +01:00
|
|
|
VkImage image;
|
2020-04-01 02:17:49 +02:00
|
|
|
VkImageView defaultView;
|
|
|
|
|
VkImageAspectFlags aspect;
|
2020-04-12 15:47:19 +02:00
|
|
|
VkImageLayout layout;
|
|
|
|
|
friend class TextureBase;
|
|
|
|
|
friend class Texture2D;
|
|
|
|
|
};
|
|
|
|
|
DEFINE_REF(TextureHandle);
|
|
|
|
|
|
|
|
|
|
DECLARE_REF(TextureBase);
|
|
|
|
|
class TextureBase
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
static PTextureHandle cast(Gfx::PTexture texture)
|
|
|
|
|
{
|
|
|
|
|
PTextureBase base = texture.cast<TextureBase>();
|
|
|
|
|
return base->textureHandle;
|
|
|
|
|
}
|
|
|
|
|
void changeLayout(VkImageLayout newLayout);
|
|
|
|
|
|
|
|
|
|
protected:
|
|
|
|
|
PTextureHandle textureHandle;
|
2020-03-24 21:05:32 +01:00
|
|
|
};
|
|
|
|
|
DEFINE_REF(TextureBase);
|
|
|
|
|
|
2020-04-12 15:47:19 +02:00
|
|
|
class Texture2D : public TextureBase, public Gfx::Texture2D
|
2020-03-24 21:05:32 +01:00
|
|
|
{
|
|
|
|
|
public:
|
2020-06-02 11:46:18 +02:00
|
|
|
Texture2D(PGraphics graphics, const TextureCreateInfo& createInfo, VkImage existingImage = VK_NULL_HANDLE);
|
2020-04-01 02:17:49 +02:00
|
|
|
virtual ~Texture2D();
|
|
|
|
|
inline uint32 getSizeX() const
|
|
|
|
|
{
|
|
|
|
|
return textureHandle->sizeX;
|
|
|
|
|
}
|
|
|
|
|
inline uint32 getSizeY() const
|
|
|
|
|
{
|
|
|
|
|
return textureHandle->sizeY;
|
|
|
|
|
}
|
|
|
|
|
inline Gfx::SeFormat getFormat() const
|
|
|
|
|
{
|
|
|
|
|
return textureHandle->format;
|
|
|
|
|
}
|
|
|
|
|
inline VkImage getHandle() const
|
|
|
|
|
{
|
|
|
|
|
return textureHandle->image;
|
|
|
|
|
}
|
|
|
|
|
inline VkImageView getView() const
|
|
|
|
|
{
|
|
|
|
|
return textureHandle->defaultView;
|
|
|
|
|
}
|
2020-04-12 15:47:19 +02:00
|
|
|
inline bool isDepthStencil() const
|
|
|
|
|
{
|
|
|
|
|
return textureHandle->isDepthStencil();
|
|
|
|
|
}
|
2020-06-02 11:46:18 +02:00
|
|
|
protected:
|
|
|
|
|
// Inherited via QueueOwnedResource
|
|
|
|
|
virtual void executeOwnershipBarrier(Gfx::QueueType newOwner);
|
2020-04-12 15:47:19 +02:00
|
|
|
|
2020-03-24 21:05:32 +01:00
|
|
|
};
|
|
|
|
|
DEFINE_REF(Texture2D);
|
|
|
|
|
|
|
|
|
|
class SamplerState
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
VkSampler sampler;
|
|
|
|
|
};
|
|
|
|
|
DEFINE_REF(SamplerState);
|
|
|
|
|
|
2020-04-12 15:47:19 +02:00
|
|
|
class Window : public Gfx::Window
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
Window(PGraphics graphics, const WindowCreateInfo &createInfo);
|
|
|
|
|
virtual ~Window();
|
|
|
|
|
virtual void beginFrame() override;
|
|
|
|
|
virtual void endFrame() override;
|
|
|
|
|
virtual Gfx::PTexture2D getBackBuffer() override;
|
|
|
|
|
virtual void onWindowCloseEvent() override;
|
|
|
|
|
|
|
|
|
|
protected:
|
|
|
|
|
void advanceBackBuffer();
|
|
|
|
|
void recreateSwapchain(const WindowCreateInfo &createInfo);
|
|
|
|
|
void present();
|
|
|
|
|
void destroySwapchain();
|
|
|
|
|
void createSwapchain();
|
|
|
|
|
void chooseSurfaceFormat(const Array<VkSurfaceFormatKHR> &available, Gfx::SeFormat preferred);
|
|
|
|
|
void choosePresentMode(const Array<VkPresentModeKHR> &modes);
|
|
|
|
|
PTexture2D backBufferImages[Gfx::numFramesBuffered];
|
|
|
|
|
PSemaphore renderFinished[Gfx::numFramesBuffered];
|
|
|
|
|
PSemaphore imageAcquired[Gfx::numFramesBuffered];
|
|
|
|
|
PSemaphore imageAcquiredSemaphore;
|
|
|
|
|
|
|
|
|
|
PGraphics graphics;
|
|
|
|
|
VkFormat pixelFormat;
|
|
|
|
|
VkPresentModeKHR presentMode;
|
|
|
|
|
VkSwapchainKHR swapchain;
|
|
|
|
|
VkSurfaceKHR surface;
|
|
|
|
|
VkSurfaceFormatKHR surfaceFormat;
|
|
|
|
|
void *windowHandle;
|
|
|
|
|
int32 currentImageIndex;
|
|
|
|
|
int32 acquiredImageIndex;
|
|
|
|
|
int32 preAcquiredImageIndex;
|
|
|
|
|
int32 semaphoreIndex;
|
|
|
|
|
VkInstance instance;
|
|
|
|
|
};
|
|
|
|
|
DEFINE_REF(Window);
|
|
|
|
|
|
2020-03-24 21:05:32 +01:00
|
|
|
class Viewport : public Gfx::Viewport
|
|
|
|
|
{
|
|
|
|
|
public:
|
2020-04-12 15:47:19 +02:00
|
|
|
Viewport(PGraphics graphics, PWindow owner, const ViewportCreateInfo &createInfo);
|
|
|
|
|
virtual ~Viewport();
|
2020-04-15 02:03:56 +02:00
|
|
|
virtual void resize(uint32 newX, uint32 newY);
|
|
|
|
|
virtual void move(uint32 newOffsetX, uint32 newOffsetY);
|
2020-05-05 01:51:13 +02:00
|
|
|
|
2020-04-12 15:47:19 +02:00
|
|
|
protected:
|
2020-03-24 21:05:32 +01:00
|
|
|
private:
|
2020-04-12 15:47:19 +02:00
|
|
|
PGraphics graphics;
|
|
|
|
|
friend class Graphics;
|
2020-03-24 21:05:32 +01:00
|
|
|
};
|
|
|
|
|
DECLARE_REF(Viewport);
|
|
|
|
|
} // namespace Vulkan
|
2020-03-26 00:25:33 +01:00
|
|
|
} // namespace Seele
|