/*
 * 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 "VulkanDrawContext.hpp"
#include "Scene/Shader/Shader.hpp"
#include "Scene/VulkanShader.hpp"
#include "Scene/VulkanCamera.hpp"
#include "Scene/VulkanNode.hpp"

namespace OpenVulkano::Vulkan
{
	void VulkanDrawContext::EncodeShader(Scene::Shader* shader)
	{
		VulkanShader* vkShader = shader->GetRenderResource();
		if (!vkShader)
		{
			vkShader = renderer->GetResourceManager().CreateShader(shader);
		}
		else if (m_lastShader == vkShader) return; // Skip it if shader is already bound
		vkShader->Record(this);
		m_lastShader = vkShader;
		if (m_lastCamera) m_lastCamera->Record(this);
	}

	void VulkanDrawContext::EncodeShader(VulkanShader* vkShader)
	{
		if (m_lastShader == vkShader) return; // Skip it if shader is already bound
		vkShader->Record(this);
		m_lastShader = vkShader;
	}

	void VulkanDrawContext::EncodeNode(Scene::Node* node)
	{
		VulkanNode* vkNode = node->GetRenderResource();
		if (!vkNode)
		{
			vkNode = renderer->GetResourceManager().PrepareNode(node);
		}
		vkNode->Record(this);
	}

	void VulkanDrawContext::SetCamera(Scene::Camera* camera)
	{
		if (!camera->HasRenderResource()) m_lastCamera = ResourceManager::INSTANCE->PrepareCamera(camera);
		else m_lastCamera = camera->GetRenderResource();
		if (m_lastShader) m_lastCamera->Record(this);
	}
}