/*
 * 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/.
 */

#include "Scene/Prefabs/ArBackgroundDrawable.hpp"
#include "VulkanGeometry.hpp"
#include "Vulkan/VulkanDrawContext.hpp"
#include "Vulkan/Scene/VulkanTexture.hpp"
#include "Vulkan/Scene/VulkanUniformBuffer.hpp"

using namespace OpenVulkano::Scene;

namespace OpenVulkano::Vulkan
{
	void EncodeArBackgroundDrawable(Drawable* instance, Vulkan::VulkanDrawContext* drawContext)
	{
		ArBackgroundDrawable* bgDrawable = static_cast<ArBackgroundDrawable*>(instance);
		bgDrawable->Tick();
		const Texture* texture = bgDrawable->GetTexture();
		VulkanTexture* vkTexture = texture->GetRenderResource();
		if (!vkTexture)
		{
			vkTexture = drawContext->renderer->GetResourceManager().PrepareTexture(const_cast<Texture*>(texture));
		}
		VulkanUniformBuffer* vkBuffer = bgDrawable->GetBuffer().GetRenderResource();
		if (!vkBuffer)
		{
			vkBuffer = drawContext->renderer->GetResourceManager().PrepareUniformBuffer(&bgDrawable->GetBuffer());
		}
		vkBuffer->Record(drawContext);
		vkTexture->Record(drawContext, 3);
		drawContext->commandBuffer.draw(4, 1, 0, 0);
	}
}

namespace
{
	void* arBgDrawableVulkanEncoderReg = DrawEncoder::RegisterVulkanEncodeFunction<ArBackgroundDrawable>(&OpenVulkano::Vulkan::EncodeArBackgroundDrawable);
}