/*
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at https://mozilla.org/MPL/2.0/.
 */

#pragma once

#include "Math/Math.hpp"
#include "Vulkan/Image.hpp"

namespace OpenVulkano::Vulkan
{
	class Renderer;

	class DepthBufferQuery final
	{
		Renderer& renderer;
		vk::DeviceMemory memory;
		vk::Buffer bufferDepth;
		float* cpuDepthBuffer = nullptr;
		Math::Vector2f depthQueryCoordinates = { 0.5f, 0.5f };
		bool copyDepthBuffer = true;
		
		vk::Offset3D GetCopyOffset() const;

	public:
		DepthBufferQuery(Renderer& renderer): renderer(renderer) {}

		~DepthBufferQuery() { if (cpuDepthBuffer != nullptr) Close(); }

		void Init();

		void Close();

		void Encode(vk::CommandBuffer& commandBuffer);
		
		void Resize(uint32_t width, uint32_t height);

		float GetQueriedValue() const;

		void SetQueryCoordinates(const Math::Vector2f& coords)
		{
			copyDepthBuffer = true;
			depthQueryCoordinates = coords;
		}
	};
}
