/*
 * 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/TextDrawable.hpp"
#include "VulkanNode.hpp"
#include "Vulkan/VulkanDrawContext.hpp"
#include "Vulkan/Scene/VulkanUniformBuffer.hpp"
#include "Vulkan/Scene/VulkanVertexBuffer.hpp"
#include "VulkanTexture.hpp"

using namespace OpenVulkano::Scene;

namespace OpenVulkano::Vulkan
{
	void EncodeTextDrawable(Drawable* instance, VulkanDrawContext* drawContext)
	{
		TextDrawable* drawable = static_cast<TextDrawable*>(instance);
		VertexBuffer* vbo = drawable->GetVertexBuffer();
		VulkanVertexBuffer* renderVbo = vbo->GetRenderResource();
		if (!renderVbo) renderVbo = drawContext->renderer->GetResourceManager().PrepareVertexBuffer(vbo);
		renderVbo->RecordBind(drawContext->commandBuffer);

		VulkanTexture* renderTexture = drawable->GetTexture()->GetRenderResource();
		if (!renderTexture)
		{
			renderTexture = drawContext->renderer->GetResourceManager().PrepareTexture(drawable->GetTexture());
		}
		renderTexture->Record(drawContext);

		for(Node* node : instance->GetNodes())
		{
			if (!node->IsEnabled()) [[unlikely]] continue;
			drawContext->EncodeNode(node);
			drawContext->commandBuffer.draw(4, drawable->GetSymbolCount(), 0, 0);
		}
	}
}

namespace
{
	[[maybe_unused]] void* textDrawableVulkanEncoderReg = DrawEncoder::RegisterVulkanEncodeFunction<TextDrawable>(&OpenVulkano::Vulkan::EncodeTextDrawable);
}