<commit_before>/*
Copyright (c) 2015-2017 Alternative Games Ltd / <NAME>

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/


#ifndef VULKANRENDERER_H
#define VULKANRENDERER_H

#define VULKAN_HPP_TYPESAFE_CONVERSION 1


#include <unordered_set>

// TODO: use std::variant if the compiler has C++17
#include <boost/variant/variant.hpp>
#include <boost/variant/apply_visitor.hpp>
#include <boost/variant/static_visitor.hpp>


#include <vulkan/vulkan.hpp>

#if VK_HEADER_VERSION < 64
#error "Vulkan header too old"
#endif  // VK_HEADER_VERSION < 64

#include <limits.h>  // required but not included by vk_mem_alloc.h

#include "vk_mem_alloc.h"


#if defined _MSC_VER && !defined __x86_64
#define VK_HASH(x) std::hash<uint64_t>()(static_cast<uint64_t>(x))
#else
#define VK_HASH(x) std::hash<uint64_t>()(reinterpret_cast<uint64_t>(x))
#endif


namespace std {

	template <> struct hash<vk::Format> {
		size_t operator()(const vk::Format &f) const {
			return hash<uint32_t>()(static_cast<uint32_t>(VkFormat(f)));
		}
	};

	template <> struct hash<vk::PresentModeKHR> {
		size_t operator()(const vk::PresentModeKHR &m) const {
			return hash<uint32_t>()(static_cast<uint32_t>(VkPresentModeKHR(m)));
		}
	};

}  // namespace std


namespace renderer {


struct Buffer {
	bool           ringBufferAlloc;
	uint32_t       size;
	uint32_t       offset;
	vk::Buffer     buffer;
	VmaAllocation  memory;
	uint32_t       lastUsedFrame;
	BufferType     type;


	Buffer()
	: ringBufferAlloc(false)
	, size(0)
	, offset(0)
	, memory(nullptr)
	, lastUsedFrame(0)
	, type(BufferType::Invalid)
	{}

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

	Buffer(Buffer &&other)
	: ringBufferAlloc(other.ringBufferAlloc)
	, size(other.size)
	, offset(other.offset)
	, buffer(other.buffer)
	, memory(other.memory)
	, lastUsedFrame(other.lastUsedFrame)
	, type(other.type)
	{

		other.ringBufferAlloc = false;
		other.size            = 0;
		other.offset          = 0;
		other.buffer          = vk::Buffer();
		other.memory          = 0;
		other.lastUsedFrame   = 0;
		other.type            = BufferType::Invalid;
	}

	Buffer &operator=(Buffer &&other) {
		if (this == &other) {
			return *this;
		}

		assert(!buffer);
		assert(!memory);

		ringBufferAlloc       = other.ringBufferAlloc;
		size                  = other.size;
		offset                = other.offset;
		buffer                = other.buffer;
		memory                = other.memory;
		lastUsedFrame         = other.lastUsedFrame;
		type                  = other.type;

		other.ringBufferAlloc = false;
		other.size            = 0;
		other.offset          = 0;
		other.buffer          = vk::Buffer();
		other.memory          = 0;
		other.lastUsedFrame   = 0;
		other.type            = BufferType::Invalid;
		assert(type == BufferType::Invalid);

		return *this;
	}

	~Buffer() {
		assert(!ringBufferAlloc);
		assert(size   == 0);
		assert(offset == 0);
		assert(!buffer);
		assert(!memory);
	}

	bool operator==(const Buffer &other) const {
		return this->buffer == other.buffer;
	}

	size_t getHash() const {
		return VK_HASH(VkBuffer(buffer));
	}
};


struct DescriptorSetLayout {
	std::vector<DescriptorLayout>  descriptors;
	vk::DescriptorSetLayout        layout;


	DescriptorSetLayout() {}

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

	DescriptorSetLayout(DescriptorSetLayout &&other)
	: descriptors(std::move(other.descriptors))
	, layout(other.layout)
	{
		other.layout = vk::DescriptorSetLayout();
		assert(descriptors.empty());
	}

	DescriptorSetLayout &operator=(DescriptorSetLayout &&other) {
		if (this == &other) {
			return *this;
		}

		assert(!layout);

		descriptors  = std::move(other.descriptors);
		assert(descriptors.empty());

		layout       = other.layout;
		other.layout = vk::DescriptorSetLayout();

		return *this;
	}

	~DescriptorSetLayout() {
		assert(!layout);
	}
};


struct FragmentShader {
	vk::ShaderModule shaderModule;


	FragmentShader() {}

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

	FragmentShader(FragmentShader &&other)
	: shaderModule(other.shaderModule)
	{
		other.shaderModule = vk::ShaderModule();
	}

	FragmentShader &operator=(FragmentShader &&other) {
		if (this == &other) {
			return *this;
		}

		assert(!shaderModule);
		shaderModule       = other.shaderModule;
		other.shaderModule = vk::ShaderModule();

		return *this;
	}

	~FragmentShader() {
		assert(!shaderModule);
	}
};


struct Framebuffer {
	unsigned int     width, height;
	vk::Framebuffer  framebuffer;
	FramebufferDesc  desc;
	// TODO: store info about attachments to allow tracking layout


	Framebuffer()
	: width(0)
	, height(0)
	{}

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

	Framebuffer(Framebuffer &&other)
	: width(other.width)
	, height(other.height)
	, framebuffer(other.framebuffer)
	, desc(other.desc)
	{
		other.width       = 0;
		other.height      = 0;
		other.framebuffer = vk::Framebuffer();
	}

	Framebuffer &operator=(Framebuffer &&other) {
		if (this == &other) {
			return *this;
		}

		assert(!framebuffer);

		width             = other.width;
		height            = other.height;
		framebuffer       = other.framebuffer;
		desc              = other.desc;

		other.width       = 0;
		other.height      = 0;
		other.framebuffer = vk::Framebuffer();

		return *this;
	}

	~Framebuffer() {
		assert(!framebuffer);
	}


	bool operator==(const Framebuffer &other) const {
		return this->framebuffer == other.framebuffer;
	}

	size_t getHash() const {
		return VK_HASH(VkFramebuffer(framebuffer));
	}
};


struct Pipeline {
	vk::Pipeline       pipeline;
	vk::PipelineLayout layout;
	bool               scissor;


	Pipeline()
	: scissor(false)
	{}

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

	Pipeline(Pipeline &&other)
	: pipeline(other.pipeline)
	, layout(other.layout)
	, scissor(other.scissor)
	{
		other.pipeline = vk::Pipeline();
		other.layout   = vk::PipelineLayout();
		other.scissor  = false;
	}

	Pipeline &operator=(Pipeline &&other) {
		if (this == &other) {
			return *this;
		}

		assert(!pipeline);
		assert(!layout);

		pipeline       = other.pipeline;
		layout         = other.layout;
		scissor        = other.scissor;

		other.pipeline = vk::Pipeline();
		other.layout   = vk::PipelineLayout();
		other.scissor  = false;

		return *this;
	}

	~Pipeline() {
		assert(!pipeline);
		assert(!layout);
	}
};


struct RenderPass {
	vk::RenderPass renderPass;
	unsigned int                   clearValueCount;
	std::array<vk::ClearValue, 2>  clearValues;
	unsigned int                   numSamples;
	RenderPassDesc                 desc;


	RenderPass()
	: clearValueCount(0)
	, numSamples(0)
	{
	}

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

	RenderPass(RenderPass &&other)
	: renderPass(other.renderPass)
	, clearValueCount(other.clearValueCount)
	, numSamples(other.numSamples)
	, desc(other.desc)
	{
		for (unsigned int i = 0; i < other.clearValueCount; i++) {
			clearValues[i] = other.clearValues[i];
		}

		other.renderPass = vk::RenderPass();
		other.clearValueCount = 0;
		other.numSamples      = 0;
	}

	RenderPass &operator=(RenderPass &&other) {
		if (this == &other) {
			return *this;
		}

		assert(!renderPass);

		renderPass       = other.renderPass;
		clearValueCount  = other.clearValueCount;
		numSamples       = other.numSamples;
		desc             = other.desc;

		for (unsigned int i = 0; i < other.clearValueCount; i++) {
			clearValues[i] = other.clearValues[i];
		}

		other.renderPass = vk::RenderPass();
		other.clearValueCount = 0;
		other.numSamples      = 0;

		return *this;
	}

	~RenderPass() {
		assert(!renderPass);
		assert(clearValueCount == 0);
		assert(numSamples == 0);
	}

	bool operator==(const RenderPass &other) const {
		return this->renderPass == other.renderPass;
	}

	size_t getHash() const {
		return VK_HASH(VkRenderPass(renderPass));
	}
};


struct RenderTarget{
	unsigned int  width, height;
	Layout               currentLayout;
	TextureHandle        texture;
	TextureHandle        additionalView;
	vk::Image     image;
	vk::Format    format;
	vk::ImageView imageView;


	RenderTarget()
	: width(0)
	, height(0)
	, currentLayout(Layout::Undefined)
	, format(vk::Format::eUndefined)
	{}

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

	RenderTarget(RenderTarget &&other)
	: width(other.width)
	, height(other.height)
	, currentLayout(other.currentLayout)
	, texture(other.texture)
	, additionalView(other.additionalView)
	, image(other.image)
	, format(other.format)
	, imageView(other.imageView)
	{
		other.width         = 0;
		other.height        = 0;
		other.currentLayout = Layout::Undefined;
		other.texture       = TextureHandle();
		other.additionalView = TextureHandle();
		other.image         = vk::Image();
		other.format        = vk::Format::eUndefined;
		other.imageView     = vk::ImageView();
	}

	RenderTarget &operator=(RenderTarget &&other) {
		if (this == &other) {
			return *this;
		}

		assert(!image);
		assert(!imageView);

		width               = other.width;
		height              = other.height;
		currentLayout       = other.currentLayout;
		texture             = other.texture;
		additionalView      = other.additionalView;
		image               = other.image;
		format              = other.format;
		imageView           = other.imageView;

		other.width         = 0;
		other.height        = 0;
		other.currentLayout = Layout::Undefined;
		other.texture       = TextureHandle();
		other.additionalView = TextureHandle();
		other.image         = vk::Image();
		other.format        = vk::Format::eUndefined;
		other.imageView     = vk::ImageView();

		return *this;
	}

	~RenderTarget() {
		assert(!image);
		assert(!imageView);
	}


	bool operator==(const RenderTarget &other) const {
		return this->image == other.image;
	}

	size_t getHash() const {
		return VK_HASH(VkImage(image));
	}
};


struct Sampler {
	vk::Sampler sampler;


	Sampler()
	{
	}

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

	Sampler(Sampler &&other)
	: sampler(other.sampler)
	{
		other.sampler = vk::Sampler();
	}

	Sampler &operator=(Sampler &&other) {
		if (this == &other) {
			return *this;
		}

		sampler       = other.sampler;

		other.sampler = vk::Sampler();

		return *this;
	}

	~Sampler() {
		assert(!sampler);
	}

	bool operator==(const Sampler &other) const {
		return this->sampler == other.sampler;
	}

	size_t getHash() const {
		return VK_HASH(VkSampler(sampler));
	}
};


struct Texture {
	unsigned int         width, height;
	bool                 renderTarget;
	vk::Image            image;
	vk::ImageView        imageView;
	VmaAllocation        memory;


	Texture()
	: width(0)
	, height(0)
	, renderTarget(false)
	, memory(nullptr)
	{
	}


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

	Texture(Texture &&other)
	: width(other.width)
	, height(other.height)
	, renderTarget(other.renderTarget)
	, image(other.image)
	, imageView(other.imageView)
	, memory(other.memory)
	{
		other.width        = 0;
		other.height       = 0;
		other.image        = vk::Image();
		other.imageView    = vk::ImageView();
		other.memory       = 0;
		other.renderTarget = false;
	}

	Texture &operator=(Texture &&other) {
		if (this == &other) {
			return *this;
		}

		width              = other.width;
		height             = other.height;
		renderTarget       = other.renderTarget;
		image              = other.image;
		imageView          = other.imageView;
		memory             = other.memory;

		other.width        = 0;
		other.height       = 0;
		other.renderTarget = false;
		other.image        = vk::Image();
		other.imageView    = vk::ImageView();
		other.memory       = 0;

		return *this;
	}

	~Texture() {
		assert(!image);
		assert(!imageView);
	}

	bool operator==(const Texture &other) const {
		return this->image == other.image;
	}

	size_t getHash() const {
		return VK_HASH(VkImage(image));
	}
};


struct VertexShader {
	vk::ShaderModule shaderModule;


	VertexShader()
	{
	}

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

	VertexShader(VertexShader &&other)
	: shaderModule(other.shaderModule)
	{
		other.shaderModule = vk::ShaderModule();
	}

	VertexShader &operator=(VertexShader &&other) {
		if (this == &other) {
			return *this;
		}

		assert(!shaderModule);

		shaderModule       = other.shaderModule;
		other.shaderModule = vk::ShaderModule();

		return *this;
	}

	~VertexShader() {
		assert(!shaderModule);
	}
};


typedef boost::variant<Buffer, Framebuffer, RenderPass, RenderTarget, Sampler, Texture> Resource;


struct ResourceHasher final : public boost::static_visitor<size_t> {
	size_t operator()(const Buffer &b) const {
		return b.getHash();
	}

	size_t operator()(const Framebuffer &fb) const {
		return fb.getHash();
	}

	size_t operator()(const RenderPass &rp) const {
		return rp.getHash();
	}

	size_t operator()(const RenderTarget &rt) const {
		return rt.getHash();
	}

	size_t operator()(const Sampler &s) const {
		return s.getHash();
	}

	size_t operator()(const Texture &t) const {
		return t.getHash();
	}
};


}	// namespace renderer


namespace std {

	template <> struct hash<renderer::Resource> {
		size_t operator()(const renderer::Resource &r) const {
			return boost::apply_visitor(renderer::ResourceHasher(), r);
		}
	};

}  // namespace std


namespace renderer {


struct UploadOp {
	vk::CommandBuffer  cmdBuf;
	vk::Semaphore      semaphore;
	vk::PipelineStageFlags  semWaitMask;
	vk::Buffer         stagingBuffer;
	VmaAllocation      memory;
	VmaAllocationInfo  allocationInfo;


	UploadOp() noexcept
	: memory(VK_NULL_HANDLE)
	{
		allocationInfo = {};
	}

	~UploadOp() noexcept {
		assert(!cmdBuf);
		assert(!semaphore);
		assert(!semWaitMask);
		assert(!stagingBuffer);
		assert(!memory);
	}


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


	UploadOp(UploadOp &&other) noexcept
	: cmdBuf(other.cmdBuf)
	, semaphore(other.semaphore)
	, semWaitMask(other.semWaitMask)
	, stagingBuffer(other.stagingBuffer)
	, memory(other.memory)
	, allocationInfo(other.allocationInfo)
	{
		other.cmdBuf = vk::CommandBuffer();
		other.semaphore = vk::Semaphore();
		other.semWaitMask   = vk::PipelineStageFlags();
		other.stagingBuffer = vk::Buffer();
		other.memory        = VK_NULL_HANDLE;
	}


	UploadOp &operator=(UploadOp &&other) noexcept {
		if (this == &other) {
			return *this;
		}

		assert(!cmdBuf);
		assert(!semaphore);
		assert(!semWaitMask);
		assert(!stagingBuffer);
		assert(!memory);

		cmdBuf       = other.cmdBuf;
		other.cmdBuf = vk::CommandBuffer();

		semaphore       = other.semaphore;
		other.semaphore = vk::Semaphore();

		semWaitMask         = other.semWaitMask;
		other.semWaitMask   = vk::PipelineStageFlags();

		stagingBuffer       = other.stagingBuffer;
		other.stagingBuffer = vk::Buffer();

		memory              = other.memory;
		other.memory        = VK_NULL_HANDLE;

		allocationInfo      = other.allocationInfo;

		return *this;
	}
};


struct Frame {<commit_msg>Whitespace<commit_after>/*
Copyright (c) 2015-2017 Alternative Games Ltd / <NAME>

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/


#ifndef VULKANRENDERER_H
#define VULKANRENDERER_H

#define VULKAN_HPP_TYPESAFE_CONVERSION 1


#include <unordered_set>

// TODO: use std::variant if the compiler has C++17
#include <boost/variant/variant.hpp>
#include <boost/variant/apply_visitor.hpp>
#include <boost/variant/static_visitor.hpp>


#include <vulkan/vulkan.hpp>

#if VK_HEADER_VERSION < 64
#error "Vulkan header too old"
#endif  // VK_HEADER_VERSION < 64

#include <limits.h>  // required but not included by vk_mem_alloc.h

#include "vk_mem_alloc.h"


#if defined _MSC_VER && !defined __x86_64
#define VK_HASH(x) std::hash<uint64_t>()(static_cast<uint64_t>(x))
#else
#define VK_HASH(x) std::hash<uint64_t>()(reinterpret_cast<uint64_t>(x))
#endif


namespace std {

	template <> struct hash<vk::Format> {
		size_t operator()(const vk::Format &f) const {
			return hash<uint32_t>()(static_cast<uint32_t>(VkFormat(f)));
		}
	};

	template <> struct hash<vk::PresentModeKHR> {
		size_t operator()(const vk::PresentModeKHR &m) const {
			return hash<uint32_t>()(static_cast<uint32_t>(VkPresentModeKHR(m)));
		}
	};

}  // namespace std


namespace renderer {


struct Buffer {
	bool           ringBufferAlloc;
	uint32_t       size;
	uint32_t       offset;
	vk::Buffer     buffer;
	VmaAllocation  memory;
	uint32_t       lastUsedFrame;
	BufferType     type;


	Buffer()
	: ringBufferAlloc(false)
	, size(0)
	, offset(0)
	, memory(nullptr)
	, lastUsedFrame(0)
	, type(BufferType::Invalid)
	{}

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

	Buffer(Buffer &&other)
	: ringBufferAlloc(other.ringBufferAlloc)
	, size(other.size)
	, offset(other.offset)
	, buffer(other.buffer)
	, memory(other.memory)
	, lastUsedFrame(other.lastUsedFrame)
	, type(other.type)
	{

		other.ringBufferAlloc = false;
		other.size            = 0;
		other.offset          = 0;
		other.buffer          = vk::Buffer();
		other.memory          = 0;
		other.lastUsedFrame   = 0;
		other.type            = BufferType::Invalid;
	}

	Buffer &operator=(Buffer &&other) {
		if (this == &other) {
			return *this;
		}

		assert(!buffer);
		assert(!memory);

		ringBufferAlloc       = other.ringBufferAlloc;
		size                  = other.size;
		offset                = other.offset;
		buffer                = other.buffer;
		memory                = other.memory;
		lastUsedFrame         = other.lastUsedFrame;
		type                  = other.type;

		other.ringBufferAlloc = false;
		other.size            = 0;
		other.offset          = 0;
		other.buffer          = vk::Buffer();
		other.memory          = 0;
		other.lastUsedFrame   = 0;
		other.type            = BufferType::Invalid;
		assert(type == BufferType::Invalid);

		return *this;
	}

	~Buffer() {
		assert(!ringBufferAlloc);
		assert(size   == 0);
		assert(offset == 0);
		assert(!buffer);
		assert(!memory);
	}

	bool operator==(const Buffer &other) const {
		return this->buffer == other.buffer;
	}

	size_t getHash() const {
		return VK_HASH(VkBuffer(buffer));
	}
};


struct DescriptorSetLayout {
	std::vector<DescriptorLayout>  descriptors;
	vk::DescriptorSetLayout        layout;


	DescriptorSetLayout() {}

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

	DescriptorSetLayout(DescriptorSetLayout &&other)
	: descriptors(std::move(other.descriptors))
	, layout(other.layout)
	{
		other.layout = vk::DescriptorSetLayout();
		assert(descriptors.empty());
	}

	DescriptorSetLayout &operator=(DescriptorSetLayout &&other) {
		if (this == &other) {
			return *this;
		}

		assert(!layout);

		descriptors  = std::move(other.descriptors);
		assert(descriptors.empty());

		layout       = other.layout;
		other.layout = vk::DescriptorSetLayout();

		return *this;
	}

	~DescriptorSetLayout() {
		assert(!layout);
	}
};


struct FragmentShader {
	vk::ShaderModule shaderModule;


	FragmentShader() {}

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

	FragmentShader(FragmentShader &&other)
	: shaderModule(other.shaderModule)
	{
		other.shaderModule = vk::ShaderModule();
	}

	FragmentShader &operator=(FragmentShader &&other) {
		if (this == &other) {
			return *this;
		}

		assert(!shaderModule);
		shaderModule       = other.shaderModule;
		other.shaderModule = vk::ShaderModule();

		return *this;
	}

	~FragmentShader() {
		assert(!shaderModule);
	}
};


struct Framebuffer {
	unsigned int     width, height;
	vk::Framebuffer  framebuffer;
	FramebufferDesc  desc;
	// TODO: store info about attachments to allow tracking layout


	Framebuffer()
	: width(0)
	, height(0)
	{}

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

	Framebuffer(Framebuffer &&other)
	: width(other.width)
	, height(other.height)
	, framebuffer(other.framebuffer)
	, desc(other.desc)
	{
		other.width       = 0;
		other.height      = 0;
		other.framebuffer = vk::Framebuffer();
	}

	Framebuffer &operator=(Framebuffer &&other) {
		if (this == &other) {
			return *this;
		}

		assert(!framebuffer);

		width             = other.width;
		height            = other.height;
		framebuffer       = other.framebuffer;
		desc              = other.desc;

		other.width       = 0;
		other.height      = 0;
		other.framebuffer = vk::Framebuffer();

		return *this;
	}

	~Framebuffer() {
		assert(!framebuffer);
	}


	bool operator==(const Framebuffer &other) const {
		return this->framebuffer == other.framebuffer;
	}

	size_t getHash() const {
		return VK_HASH(VkFramebuffer(framebuffer));
	}
};


struct Pipeline {
	vk::Pipeline       pipeline;
	vk::PipelineLayout layout;
	bool               scissor;


	Pipeline()
	: scissor(false)
	{}

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

	Pipeline(Pipeline &&other)
	: pipeline(other.pipeline)
	, layout(other.layout)
	, scissor(other.scissor)
	{
		other.pipeline = vk::Pipeline();
		other.layout   = vk::PipelineLayout();
		other.scissor  = false;
	}

	Pipeline &operator=(Pipeline &&other) {
		if (this == &other) {
			return *this;
		}

		assert(!pipeline);
		assert(!layout);

		pipeline       = other.pipeline;
		layout         = other.layout;
		scissor        = other.scissor;

		other.pipeline = vk::Pipeline();
		other.layout   = vk::PipelineLayout();
		other.scissor  = false;

		return *this;
	}

	~Pipeline() {
		assert(!pipeline);
		assert(!layout);
	}
};


struct RenderPass {
	vk::RenderPass renderPass;
	unsigned int                   clearValueCount;
	std::array<vk::ClearValue, 2>  clearValues;
	unsigned int                   numSamples;
	RenderPassDesc                 desc;


	RenderPass()
	: clearValueCount(0)
	, numSamples(0)
	{
	}

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

	RenderPass(RenderPass &&other)
	: renderPass(other.renderPass)
	, clearValueCount(other.clearValueCount)
	, numSamples(other.numSamples)
	, desc(other.desc)
	{
		for (unsigned int i = 0; i < other.clearValueCount; i++) {
			clearValues[i] = other.clearValues[i];
		}

		other.renderPass = vk::RenderPass();
		other.clearValueCount = 0;
		other.numSamples      = 0;
	}

	RenderPass &operator=(RenderPass &&other) {
		if (this == &other) {
			return *this;
		}

		assert(!renderPass);

		renderPass       = other.renderPass;
		clearValueCount  = other.clearValueCount;
		numSamples       = other.numSamples;
		desc             = other.desc;

		for (unsigned int i = 0; i < other.clearValueCount; i++) {
			clearValues[i] = other.clearValues[i];
		}

		other.renderPass = vk::RenderPass();
		other.clearValueCount = 0;
		other.numSamples      = 0;

		return *this;
	}

	~RenderPass() {
		assert(!renderPass);
		assert(clearValueCount == 0);
		assert(numSamples == 0);
	}

	bool operator==(const RenderPass &other) const {
		return this->renderPass == other.renderPass;
	}

	size_t getHash() const {
		return VK_HASH(VkRenderPass(renderPass));
	}
};


struct RenderTarget{
	unsigned int  width, height;
	Layout               currentLayout;
	TextureHandle        texture;
	TextureHandle        additionalView;
	vk::Image     image;
	vk::Format    format;
	vk::ImageView imageView;


	RenderTarget()
	: width(0)
	, height(0)
	, currentLayout(Layout::Undefined)
	, format(vk::Format::eUndefined)
	{}

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

	RenderTarget(RenderTarget &&other)
	: width(other.width)
	, height(other.height)
	, currentLayout(other.currentLayout)
	, texture(other.texture)
	, additionalView(other.additionalView)
	, image(other.image)
	, format(other.format)
	, imageView(other.imageView)
	{
		other.width         = 0;
		other.height        = 0;
		other.currentLayout = Layout::Undefined;
		other.texture       = TextureHandle();
		other.additionalView = TextureHandle();
		other.image         = vk::Image();
		other.format        = vk::Format::eUndefined;
		other.imageView     = vk::ImageView();
	}

	RenderTarget &operator=(RenderTarget &&other) {
		if (this == &other) {
			return *this;
		}

		assert(!image);
		assert(!imageView);

		width               = other.width;
		height              = other.height;
		currentLayout       = other.currentLayout;
		texture             = other.texture;
		additionalView      = other.additionalView;
		image               = other.image;
		format              = other.format;
		imageView           = other.imageView;

		other.width         = 0;
		other.height        = 0;
		other.currentLayout = Layout::Undefined;
		other.texture       = TextureHandle();
		other.additionalView = TextureHandle();
		other.image         = vk::Image();
		other.format        = vk::Format::eUndefined;
		other.imageView     = vk::ImageView();

		return *this;
	}

	~RenderTarget() {
		assert(!image);
		assert(!imageView);
	}


	bool operator==(const RenderTarget &other) const {
		return this->image == other.image;
	}

	size_t getHash() const {
		return VK_HASH(VkImage(image));
	}
};


struct Sampler {
	vk::Sampler sampler;


	Sampler()
	{
	}

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

	Sampler(Sampler &&other)
	: sampler(other.sampler)
	{
		other.sampler = vk::Sampler();
	}

	Sampler &operator=(Sampler &&other) {
		if (this == &other) {
			return *this;
		}

		sampler       = other.sampler;

		other.sampler = vk::Sampler();

		return *this;
	}

	~Sampler() {
		assert(!sampler);
	}

	bool operator==(const Sampler &other) const {
		return this->sampler == other.sampler;
	}

	size_t getHash() const {
		return VK_HASH(VkSampler(sampler));
	}
};


struct Texture {
	unsigned int         width, height;
	bool                 renderTarget;
	vk::Image            image;
	vk::ImageView        imageView;
	VmaAllocation        memory;


	Texture()
	: width(0)
	, height(0)
	, renderTarget(false)
	, memory(nullptr)
	{
	}


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

	Texture(Texture &&other)
	: width(other.width)
	, height(other.height)
	, renderTarget(other.renderTarget)
	, image(other.image)
	, imageView(other.imageView)
	, memory(other.memory)
	{
		other.width        = 0;
		other.height       = 0;
		other.image        = vk::Image();
		other.imageView    = vk::ImageView();
		other.memory       = 0;
		other.renderTarget = false;
	}

	Texture &operator=(Texture &&other) {
		if (this == &other) {
			return *this;
		}

		width              = other.width;
		height             = other.height;
		renderTarget       = other.renderTarget;
		image              = other.image;
		imageView          = other.imageView;
		memory             = other.memory;

		other.width        = 0;
		other.height       = 0;
		other.renderTarget = false;
		other.image        = vk::Image();
		other.imageView    = vk::ImageView();
		other.memory       = 0;

		return *this;
	}

	~Texture() {
		assert(!image);
		assert(!imageView);
	}

	bool operator==(const Texture &other) const {
		return this->image == other.image;
	}

	size_t getHash() const {
		return VK_HASH(VkImage(image));
	}
};


struct VertexShader {
	vk::ShaderModule shaderModule;


	VertexShader()
	{
	}

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

	VertexShader(VertexShader &&other)
	: shaderModule(other.shaderModule)
	{
		other.shaderModule = vk::ShaderModule();
	}

	VertexShader &operator=(VertexShader &&other) {
		if (this == &other) {
			return *this;
		}

		assert(!shaderModule);

		shaderModule       = other.shaderModule;
		other.shaderModule = vk::ShaderModule();

		return *this;
	}

	~VertexShader() {
		assert(!shaderModule);
	}
};


typedef boost::variant<Buffer, Framebuffer, RenderPass, RenderTarget, Sampler, Texture> Resource;


struct ResourceHasher final : public boost::static_visitor<size_t> {
	size_t operator()(const Buffer &b) const {
		return b.getHash();
	}

	size_t operator()(const Framebuffer &fb) const {
		return fb.getHash();
	}

	size_t operator()(const RenderPass &rp) const {
		return rp.getHash();
	}

	size_t operator()(const RenderTarget &rt) const {
		return rt.getHash();
	}

	size_t operator()(const Sampler &s) const {
		return s.getHash();
	}

	size_t operator()(const Texture &t) const {
		return t.getHash();
	}
};


}	// namespace renderer


namespace std {

	template <> struct hash<renderer::Resource> {
		size_t operator()(const renderer::Resource &r) const {
			return boost::apply_visitor(renderer::ResourceHasher(), r);
		}
	};

}  // namespace std


namespace renderer {


struct UploadOp {
	vk::CommandBuffer       cmdBuf;
	vk::Semaphore           semaphore;
	vk::PipelineStageFlags  semWaitMask;
	vk::Buffer              stagingBuffer;
	VmaAllocation           memory;
	VmaAllocationInfo       allocationInfo;


	UploadOp() noexcept
	: memory(VK_NULL_HANDLE)
	{
		allocationInfo = {};
	}

	~UploadOp() noexcept {
		assert(!cmdBuf);
		assert(!semaphore);
		assert(!semWaitMask);
		assert(!stagingBuffer);
		assert(!memory);
	}


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


	UploadOp(UploadOp &&other) noexcept
	: cmdBuf(other.cmdBuf)
	, semaphore(other.semaphore)
	, semWaitMask(other.semWaitMask)
	, stagingBuffer(other.stagingBuffer)
	, memory(other.memory)
	, allocationInfo(other.allocationInfo)
	{
		other.cmdBuf        = vk::CommandBuffer();
		other.semaphore     = vk::Semaphore();
		other.semWaitMask   = vk::PipelineStageFlags();
		other.stagingBuffer = vk::Buffer();
		other.memory        = VK_NULL_HANDLE;
	}


	UploadOp &operator=(UploadOp &&other) noexcept {
		if (this == &other) {
			return *this;
		}

		assert(!cmdBuf);
		assert(!semaphore);
		assert(!semWaitMask);
		assert(!stagingBuffer);
		assert(!memory);

		cmdBuf              = other.cmdBuf;
		other.cmdBuf        = vk::CommandBuffer();

		semaphore           = other.semaphore;
		other.semaphore     = vk::Semaphore();

		semWaitMask         = other.semWaitMask;
		other.semWaitMask   = vk::PipelineStageFlags();

		stagingBuffer       = other.stagingBuffer;
		other.stagingBuffer = vk::Buffer();

		memory              = other.memory;
		other.memory        = VK_NULL_HANDLE;

		allocationInfo      = other.allocationInfo;

		return *this;
	}
};


struct Frame {
{'fraction_non_alphanumeric': 0.12659765020188835, 'fraction_numerical': 0.005161055284009836, 'mean_word_length': 3.251968503937008, 'pattern_counts': {'":': 0, '<': 61, '<?xml version=': 0, '>': 65, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '891363', 'n_tokens_mistral': 11781, 'n_tokens_neox': 10754, 'n_words': 2280}