text: #include "RaybytePCH.h"
  #include "OpenGLUniformBuffer.h"
  
  #include "OpenGL.h"
  
  #include "Raybyte/Graphics/Renderer.h"
  
  namespace Raybyte {
  
  	OpenGLUniformBuffer::OpenGLUniformBuffer(const UniformBufferConfig& config)
  		: m_Size(config.Size), m_Binding(config.Binding)
  	{
  		m_Storage = new byte[config.Size];
  		memset(m_Storage, 0, config.Size);
  
  		Ref<OpenGLUniformBuffer> instance = this;
  		Renderer::EnqueueCommand([instance]() mutable
  		{
  			glCreateBuffers(1, &instance->m_BufferID);
  			glNamedBufferData(instance->m_BufferID, instance->m_Size, nullptr, GL_DYNAMIC_DRAW);
  
  			glBindBufferBase(GL_UNIFORM_BUFFER, instance->m_Binding, instance->m_BufferID);
  		});
  	}
  
  	OpenGLUniformBuffer::~OpenGLUniformBuffer()
  	{
  		uint32 bufferID = m_BufferID;
  		Renderer::EnqueueCommand([bufferID]()
  		{
  			glDeleteBuffers(1, &bufferID);
  		});
  	}
  
  	void OpenGLUniformBuffer::SetData(const void* data, uint32 size, uint32 offset)
  	{
  		RB_ASSERT(size <= m_Size);
  		void* destPtr = (void*)((char*)m_Storage + (intptr_t)offset);
  		memcpy(destPtr, data, size);
  
  		Ref<OpenGLUniformBuffer> instance = this;
  		Renderer::EnqueueCommand([instance]() mutable
  		{
  			glNamedBufferSubData(instance->m_BufferID, 0, instance->m_Size, instance->m_Storage);
  		});
  	}
  
  	void OpenGLUniformBuffer::RT_SetData(const void* data, uint32 size, uint32 offset)
  	{
  		RB_ASSERT(size <= m_Size);
  		void* destPtr = (void*)((char*)m_Storage + (intptr_t)offset);
  		memcpy(destPtr, data, size);
  		glNamedBufferSubData(m_BufferID, offset, m_Size, m_Storage);
  	}
  
  }