#include "rendering.h"
#include "tier1/utlvector.h"
#include "console.h"
#include "tier1/commandline.h"

#include "X11/X.h"
#include "X11/Xlib.h"
#include "vulkan/vulkan.h"
#include "vulkan/vulkan_core.h"
#include "vulkan/vulkan_xlib.h"

#define VULKAN_RENDERING_IMPL
#include "vk_video.h"

#define VMA_VULKAN_VERSION 1004000
#define VMA_IMPLEMENTATION
#include "vk_mem_alloc.h"

Display* g_xdisplay;
int g_xscreen;
Window g_xroot;
Window g_xwin;

VkInstance g_vkInstance = NULL;
VkPhysicalDevice g_vkPhysicalDevice = NULL;
VkDevice g_vkDevice = NULL;
uint32_t g_drawfamily = 0;
VkQueue g_drawQueue;
uint32_t g_presentfamily = 0;
VkQueue g_presentQueue;
VmaAllocator g_allocator = NULL;


VkSurfaceKHR g_surface;
VkSwapchainKHR g_swapchain;

ConVar vulkan_gpu("vk_gpu", "0", FCVAR_ARCHIVE);


VkCommandPool g_vkCommandPool;

/* more efficient */
CUtlVector<VkCommandBuffer> g_commandBuffers;
VkCommandBuffer g_vkCommandBuffer;
CUtlVector<vk_framedata_t> g_frameData;
CUtlVector<VkImage> g_swapchainImages;
CUtlVector<VkImageView> g_swapchainImageViews;
VkImageView g_swapchainImageView;
VkImage g_swapchainImage;

uint32_t g_nNumSwapchainImages = 0;

#define VK_DEVICE_FUNCTION(name) PFN_##name _##name
#include "vk_external_functions.cpp"
#undef VK_DEVICE_FUNCTION

char g_bConfigNotify = 0;
uint32_t g_nWindowWidth = 1280;
uint32_t g_nWindowHeight = 720;

void IVideo_SwapchainInit()
{

	/* swapchain */
	VkXlibSurfaceCreateInfoKHR surfaceCreateInfo = {};
	surfaceCreateInfo.sType = VK_STRUCTURE_TYPE_XLIB_SURFACE_CREATE_INFO_KHR;
	surfaceCreateInfo.dpy = g_xdisplay;
	surfaceCreateInfo.window = g_xwin;
	vkCreateXlibSurfaceKHR(g_vkInstance, &surfaceCreateInfo, NULL, &g_surface);


	VkSurfaceCapabilitiesKHR surfaceCapatibilities = {};
	
	vkGetPhysicalDeviceSurfaceCapabilitiesKHR(g_vkPhysicalDevice, g_surface, &surfaceCapatibilities);

	const VkFormat preferedSurfaceFormats[] = {
		VK_FORMAT_R8G8B8A8_UNORM,
		VK_FORMAT_B8G8R8A8_UNORM,
	};

	uint32_t numSurfaceFormats = 0;
	vkGetPhysicalDeviceSurfaceFormatsKHR(g_vkPhysicalDevice, g_surface, &numSurfaceFormats, NULL);
	CUtlVector<VkSurfaceFormatKHR> surfaceFormats(numSurfaceFormats);
	vkGetPhysicalDeviceSurfaceFormatsKHR(g_vkPhysicalDevice, g_surface, &numSurfaceFormats, surfaceFormats.GetData());

	VkSurfaceFormatKHR selectedFormat = surfaceFormats[0];
	for (auto &format: surfaceFormats)
	{
		for (int i = 0; i < sizeof(preferedSurfaceFormats)/sizeof(VkFormat); i++)
		{
			selectedFormat = surfaceFormats[i];
		}
	}
	
	uint32_t numSurfacePresentModes = 0;
	vkGetPhysicalDeviceSurfacePresentModesKHR(g_vkPhysicalDevice, g_surface, &numSurfacePresentModes, NULL);
	CUtlVector<VkPresentModeKHR> surfacePresentModes(numSurfacePresentModes);
	vkGetPhysicalDeviceSurfacePresentModesKHR(g_vkPhysicalDevice, g_surface, &numSurfacePresentModes, surfacePresentModes.GetData());

	VkSwapchainCreateInfoKHR swapchainCreateInfo = {};
	swapchainCreateInfo.sType = VK_STRUCTURE_TYPE_SWAPCHAIN_CREATE_INFO_KHR;
	swapchainCreateInfo.surface = g_surface;
	swapchainCreateInfo.imageFormat = selectedFormat.format;
	swapchainCreateInfo.imageColorSpace = selectedFormat.colorSpace;
	swapchainCreateInfo.presentMode = VK_PRESENT_MODE_IMMEDIATE_KHR;
	swapchainCreateInfo.imageUsage = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT;
	swapchainCreateInfo.preTransform = surfaceCapatibilities.currentTransform;
	swapchainCreateInfo.compositeAlpha = VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR;
	swapchainCreateInfo.imageArrayLayers = 1;
	swapchainCreateInfo.imageExtent = surfaceCapatibilities.minImageExtent;
	swapchainCreateInfo.minImageCount = surfaceCapatibilities.minImageCount;
	vkCreateSwapchainKHR(g_vkDevice, &swapchainCreateInfo, NULL, &g_swapchain);

	vkGetSwapchainImagesKHR(g_vkDevice, g_swapchain, &g_nNumSwapchainImages, NULL);
	g_swapchainImages = CUtlVector<VkImage>(g_nNumSwapchainImages);
	g_swapchainImageViews = CUtlVector<VkImageView>(g_nNumSwapchainImages);
	g_commandBuffers = CUtlVector<VkCommandBuffer>(g_nNumSwapchainImages);
	g_frameData = CUtlVector<vk_framedata_t>(g_nNumSwapchainImages);
	vkGetSwapchainImagesKHR(g_vkDevice, g_swapchain, &g_nNumSwapchainImages, g_swapchainImages.GetData());

	for (int i = 0; i < g_nNumSwapchainImages; i++)
	{
		VkImageViewCreateInfo createInfo = {};
		createInfo.sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO;
		createInfo.image = g_swapchainImages[i];
		createInfo.viewType = VK_IMAGE_VIEW_TYPE_2D;
		createInfo.format = selectedFormat.format;
		createInfo.components.r = VK_COMPONENT_SWIZZLE_IDENTITY;
		createInfo.components.g = VK_COMPONENT_SWIZZLE_IDENTITY;
		createInfo.components.b = VK_COMPONENT_SWIZZLE_IDENTITY;
		createInfo.components.a = VK_COMPONENT_SWIZZLE_IDENTITY;
		createInfo.subresourceRange.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
		createInfo.subresourceRange.baseMipLevel = 0;
		createInfo.subresourceRange.levelCount = 1;
		createInfo.subresourceRange.baseArrayLayer = 0;
		createInfo.subresourceRange.layerCount = 1;

		vkCreateImageView(g_vkDevice, &createInfo, NULL, &g_swapchainImageViews[i]);
	}


	/* command buffers */
	VkCommandPoolCreateInfo commandPoolCreateInfo = {};
	commandPoolCreateInfo.sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO;
	commandPoolCreateInfo.queueFamilyIndex = g_drawfamily;
	commandPoolCreateInfo.flags = VK_COMMAND_POOL_CREATE_RESET_COMMAND_BUFFER_BIT;
	vkCreateCommandPool(g_vkDevice, &commandPoolCreateInfo, NULL, &g_vkCommandPool);

	VkCommandBufferAllocateInfo commandBufferAllocInfo = {};
	commandBufferAllocInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO;
	commandBufferAllocInfo.commandPool = g_vkCommandPool;
	commandBufferAllocInfo.level = VK_COMMAND_BUFFER_LEVEL_PRIMARY;
	commandBufferAllocInfo.commandBufferCount = g_nNumSwapchainImages;
	vkAllocateCommandBuffers(g_vkDevice, &commandBufferAllocInfo, g_commandBuffers.GetData());

	/* sync */

	VkFenceCreateInfo fenceCreateInfo = {};
	fenceCreateInfo.sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO;
	fenceCreateInfo.flags = VK_FENCE_CREATE_SIGNALED_BIT;

	VkSemaphoreCreateInfo semaphoreCreateInfo = {};
	semaphoreCreateInfo.sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO;

	for (int i = 0; i < g_nNumSwapchainImages; i++)
	{
		vkCreateFence(g_vkDevice,&fenceCreateInfo, NULL, &g_frameData[i].fence);
		vkCreateSemaphore(g_vkDevice, &semaphoreCreateInfo, NULL, &g_frameData[i].draw);
		vkCreateSemaphore(g_vkDevice, &semaphoreCreateInfo, NULL, &g_frameData[i].present);
	}

};

void IVideo_SwapchainDestroy()
{	
	vkDestroySwapchainKHR(g_vkDevice, g_swapchain, NULL);
	vkDestroySurfaceKHR(g_vkInstance, g_surface, NULL);
	for (int i = 0; i < g_nNumSwapchainImages; i++)
	{
		vkDestroyImageView(g_vkDevice, g_swapchainImageViews[i], NULL);
		vkDestroySemaphore(g_vkDevice, g_frameData[i].draw, NULL);
		vkDestroySemaphore(g_vkDevice, g_frameData[i].present, NULL);
		vkDestroyFence(g_vkDevice, g_frameData[i].fence, NULL);
	}
};

void IVideo::Init()
{
	g_xdisplay = XOpenDisplay(NULL);
	g_xscreen = DefaultScreen(g_xdisplay);
	g_xroot = RootWindow(g_xdisplay, g_xscreen);
	g_xwin = XCreateSimpleWindow(g_xdisplay, g_xroot, 0, 0, 1280, 720, 0, 0, 0);

	XSelectInput(g_xdisplay, g_xwin, StructureNotifyMask);

	vulkan_gpu.SetValue(ICommandLine::ParamValue("-gpu"));
	VkResult r = VK_SUCCESS;
	uint32_t i = 0;

	/* Instance */
	VkApplicationInfo applicationInfo = {};
	applicationInfo.apiVersion = VK_API_VERSION_1_4;
	
	VkInstanceCreateInfo instanceCreateInfo = {};
	instanceCreateInfo.sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO;
	instanceCreateInfo.pApplicationInfo = &applicationInfo;

	const char *szEnabledExtensions[] = {
		VK_KHR_SURFACE_EXTENSION_NAME,
		VK_KHR_XLIB_SURFACE_EXTENSION_NAME,
	};
	instanceCreateInfo.enabledExtensionCount = 2;
	instanceCreateInfo.ppEnabledExtensionNames = szEnabledExtensions;
	r = vkCreateInstance(&instanceCreateInfo, NULL, &g_vkInstance);
	if ( r != VK_SUCCESS )
		Plat_FatalErrorFunc("Failed to create VkInstance!");

	
	/* Physical Devices */
	uint32_t nNumPhysicalDevices = 0;
	vkEnumeratePhysicalDevices(g_vkInstance, &nNumPhysicalDevices, NULL);
	if ( nNumPhysicalDevices == 0 )
		Plat_FatalErrorFunc("No GPU drivers available!");
	CUtlVector<VkPhysicalDevice> PhysicalDevices(nNumPhysicalDevices);

	vkEnumeratePhysicalDevices(g_vkInstance, &nNumPhysicalDevices, PhysicalDevices.GetData());
	
	/* enumerate them for the user */
	for (auto &device: PhysicalDevices)
	{
		VkPhysicalDeviceProperties Properties = {};
		vkGetPhysicalDeviceProperties(device, &Properties);
		V_printf("GPU%i available:    %s\n", i, Properties.deviceName);
		i++;
	}

	/* select one in vk_gpu */
	g_vkPhysicalDevice = PhysicalDevices[vulkan_gpu.GetInt()];
	VkPhysicalDeviceProperties Properties = {};
	vkGetPhysicalDeviceProperties(g_vkPhysicalDevice, &Properties);
	V_printf("Using %s\n", Properties.deviceName);

	/* queue family */
	uint32_t nNumQueueFamilies = 0;
	vkGetPhysicalDeviceQueueFamilyProperties(g_vkPhysicalDevice, &nNumQueueFamilies, NULL);
	CUtlVector<VkQueueFamilyProperties> queueFamilyProperties(nNumQueueFamilies);
	vkGetPhysicalDeviceQueueFamilyProperties(g_vkPhysicalDevice, &nNumQueueFamilies, queueFamilyProperties.GetData());

	i = 0;
	for (auto &family: queueFamilyProperties)
	{
		if (family.queueFlags & VK_QUEUE_GRAPHICS_BIT)
		{
			g_drawfamily = i;
			g_presentfamily = i;
		}
		i++;
	}

	/* create device */
	float queuePriority = 1.0f;
	VkDeviceQueueCreateInfo queueCreateInfo = {};
	queueCreateInfo.sType = VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO;
	queueCreateInfo.queueFamilyIndex = g_drawfamily;
	queueCreateInfo.queueCount = 1;
	queueCreateInfo.pQueuePriorities = &queuePriority;

	VkPhysicalDeviceVertexInputDynamicStateFeaturesEXT pdvidsfe = {
		.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_VERTEX_INPUT_DYNAMIC_STATE_FEATURES_EXT,
		.vertexInputDynamicState = VK_TRUE,
	};

	VkPhysicalDeviceExtendedDynamicState3FeaturesEXT pdeds3fe = {
		.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_EXTENDED_DYNAMIC_STATE_3_FEATURES_EXT,
		.pNext = &pdvidsfe,
		.extendedDynamicState3DepthClampEnable = VK_TRUE,
		.extendedDynamicState3PolygonMode = VK_TRUE,
		.extendedDynamicState3RasterizationSamples = VK_TRUE,
		.extendedDynamicState3SampleMask = VK_TRUE,
		.extendedDynamicState3AlphaToCoverageEnable = VK_TRUE,
		.extendedDynamicState3LogicOpEnable = VK_TRUE,
		.extendedDynamicState3ColorBlendEnable = VK_TRUE,
		.extendedDynamicState3ColorBlendEquation = VK_TRUE,
		.extendedDynamicState3ColorWriteMask = VK_TRUE,
	};

	VkPhysicalDeviceExtendedDynamicState2FeaturesEXT pdeds2fe = {
		.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_EXTENDED_DYNAMIC_STATE_2_FEATURES_EXT,
		.pNext = &pdeds3fe,
		.extendedDynamicState2LogicOp = VK_TRUE,
		.extendedDynamicState2PatchControlPoints = VK_TRUE,
	};

	VkPhysicalDeviceVulkan13Features pdv13f = {
		.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_VULKAN_1_3_FEATURES,
		.pNext = &pdeds2fe,
		.dynamicRendering = VK_TRUE,
	};
	VkPhysicalDeviceVulkan12Features pdv12f = {
		.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_VULKAN_1_2_FEATURES,
		.pNext = &pdv13f,
		.runtimeDescriptorArray = VK_TRUE,
		.bufferDeviceAddress = VK_TRUE,
	};
	VkPhysicalDeviceVulkan11Features pdv11f = {
		.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_VULKAN_1_1_FEATURES,
		.pNext = &pdv12f,
	};

	const char *szEnabledGPUExtensions[] = {
		VK_KHR_SWAPCHAIN_EXTENSION_NAME,
		VK_EXT_VERTEX_INPUT_DYNAMIC_STATE_EXTENSION_NAME,
		VK_EXT_EXTENDED_DYNAMIC_STATE_3_EXTENSION_NAME,
	};
	VkDeviceCreateInfo deviceCreateInfo = {};
	deviceCreateInfo.sType = VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO;
	deviceCreateInfo.pQueueCreateInfos = &queueCreateInfo;
	deviceCreateInfo.queueCreateInfoCount = 1;
	deviceCreateInfo.enabledExtensionCount = sizeof(szEnabledGPUExtensions)/(sizeof(const char*));
	deviceCreateInfo.ppEnabledExtensionNames = szEnabledGPUExtensions;
	deviceCreateInfo.pNext = &pdv11f;

	r = vkCreateDevice(g_vkPhysicalDevice, &deviceCreateInfo, NULL, &g_vkDevice);
	if ( r != VK_SUCCESS )
		Plat_FatalErrorFunc("Failed to create VkDevice!");
	vkGetDeviceQueue(g_vkDevice, g_drawfamily, 0, &g_drawQueue);
	vkGetDeviceQueue(g_vkDevice, g_presentfamily, 0, &g_presentQueue);

	VmaVulkanFunctions vulkanFunctions = {};
	vulkanFunctions.vkGetInstanceProcAddr = &vkGetInstanceProcAddr;
	vulkanFunctions.vkGetDeviceProcAddr = &vkGetDeviceProcAddr;

	VmaAllocatorCreateInfo allocatorCreateInfo = {};
	allocatorCreateInfo.flags = VMA_ALLOCATOR_CREATE_BUFFER_DEVICE_ADDRESS_BIT;
	allocatorCreateInfo.vulkanApiVersion = VK_API_VERSION_1_4;
	allocatorCreateInfo.physicalDevice = g_vkPhysicalDevice;
	allocatorCreateInfo.device = g_vkDevice;
	allocatorCreateInfo.instance = g_vkInstance;
	vmaCreateAllocator(&allocatorCreateInfo, &g_allocator);

	#define VK_DEVICE_FUNCTION(name) _##name = (PFN_##name)vkGetDeviceProcAddr(g_vkDevice, #name)
	#include "vk_external_functions.cpp"
	#undef VK_DEVICE_FUNCTION

	IVideo_SwapchainInit();
	XMapWindow(g_xdisplay, g_xwin);
	XFlush(g_xdisplay);

	IVulkan::Init();

}

void IVideo_HandleX11()
{
	XEvent event;
	while(XPending(g_xdisplay))
	{
		XNextEvent(g_xdisplay, &event);
		switch (event.type)
		{
			case KeyPress:
			case KeyRelease:

				break;
			case MotionNotify:
				break;
			case ButtonPress:
				break;
			case ButtonRelease:
				break;

			case CreateNotify:
				break;
			case ConfigureNotify:
				g_nWindowWidth = event.xconfigure.width;
				g_nWindowHeight = event.xconfigure.height;
				g_bConfigNotify = 2;
				break;
		}
	}
};


void IVideo::Frame( float fDelta )
{
	static uint32_t s_frameID = 0;
	IVideo_HandleX11();

	vk_framedata_t frame = g_frameData[s_frameID];
	vkDeviceWaitIdle(g_vkDevice);

	vkWaitForFences(g_vkDevice, 1, &frame.fence, VK_TRUE, UINT64_MAX);

	uint32_t imageIndex = 0;
	VkResult r = vkAcquireNextImageKHR(g_vkDevice, g_swapchain, UINT64_MAX, frame.draw, VK_NULL_HANDLE, &imageIndex);
	if (r == VK_ERROR_OUT_OF_DATE_KHR || r == VK_SUBOPTIMAL_KHR || g_bConfigNotify == 2)
	{
		g_bConfigNotify=1;
		vkDeviceWaitIdle(g_vkDevice);
		IVideo_SwapchainDestroy();
		IVideo_SwapchainInit();
		return;
	}
	vk_framedata_t frameindex = g_frameData[imageIndex];
	
	vkResetFences(g_vkDevice, 1, &frame.fence);
	
	g_vkCommandBuffer = g_commandBuffers[s_frameID];
	g_swapchainImageView = g_swapchainImageViews[imageIndex];
	g_swapchainImage = g_swapchainImages[imageIndex];
	vkResetCommandBuffer(g_vkCommandBuffer, 0);

	VkCommandBufferBeginInfo beginInfo = {};
	beginInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO;
	vkBeginCommandBuffer(g_vkCommandBuffer, &beginInfo);
	IVulkan::Frame();
	vkEndCommandBuffer(g_vkCommandBuffer);

	VkPipelineStageFlags waitStages[] = {VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT};
	VkSubmitInfo submitInfo = {};
	submitInfo.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO;
	submitInfo.waitSemaphoreCount = 1;
	submitInfo.pWaitSemaphores = &frame.draw;
	submitInfo.pWaitDstStageMask = waitStages;
	submitInfo.commandBufferCount = 1;
	submitInfo.pCommandBuffers = &g_vkCommandBuffer;
	submitInfo.signalSemaphoreCount = 1;
	submitInfo.pSignalSemaphores = &frameindex.present;

	vkQueueSubmit(g_drawQueue, 1, &submitInfo, frame.fence);

	VkPresentInfoKHR presentInfo = {};
	presentInfo.sType = VK_STRUCTURE_TYPE_PRESENT_INFO_KHR;
	presentInfo.waitSemaphoreCount = 1;
	presentInfo.pWaitSemaphores = &frameindex.present;
	presentInfo.swapchainCount = 1;
	presentInfo.pSwapchains = &g_swapchain;
	presentInfo.pImageIndices = &imageIndex;


	r = vkQueuePresentKHR(g_presentQueue, &presentInfo);
	if (r == VK_ERROR_OUT_OF_DATE_KHR || r == VK_SUBOPTIMAL_KHR)
	{
		vkDeviceWaitIdle(g_vkDevice);
		IVideo_SwapchainDestroy();
		IVideo_SwapchainInit();
		return;
	}

	g_bConfigNotify = 0;	
	s_frameID=(s_frameID+1)%g_nNumSwapchainImages;
};
