<commit_before>// Copyright (c) 2017 <NAME>
#include "Jewel3D/Precompiled.h"
#include "UniformBuffer.h"
#include "Jewel3D/Application/Logging.h"
#include "Jewel3D/Math/Vector.h"

#include <algorithm>
#include <GLEW/GL/glew.h>

namespace
{
	// Rounds val up to the nearest multiple.
	unsigned round(unsigned val, unsigned multiple)
	{
		unsigned remainder = val % multiple;
		if (remainder != 0)
		{
			val += multiple - remainder;
		}

		return val;
	}
}

namespace Jwl
{
	UniformBuffer::~UniformBuffer()
	{
		UnLoad();
	}

	UniformBuffer& UniformBuffer::operator=(const UniformBuffer& other)
	{
		UnLoad();
	
		table = other.table;
		bufferSize = other.bufferSize;
	
		if (bufferSize != 0)
		{
			// We have to create our own UBO.
			InitBuffer();
	
			memcpy(buffer, other.buffer, bufferSize);
		}
	
		return *this;
	}
	
	void UniformBuffer::Copy(const UniformBuffer& other)
	{
		*this = other;
	}

	void UniformBuffer::InitBuffer()
	{
		ASSERT(bufferSize != 0, "At least one uniform must be added to the buffer before it can be initialized.");
		ASSERT(UBO == GL_NONE, "UniformBuffer has already been initialized and locked.");

		// The total size of a buffer is always aligned to the size of a vec4.
		bufferSize = round(bufferSize, sizeof(vec4));

		// GPU buffer.
		glGenBuffers(1, &UBO);
		glBindBuffer(GL_UNIFORM_BUFFER, UBO);
		glBufferData(GL_UNIFORM_BUFFER, bufferSize, nullptr, GL_STREAM_DRAW);
		glBindBuffer(GL_UNIFORM_BUFFER, GL_NONE);

		// RAM buffer.
		buffer = malloc(bufferSize);
		memset(buffer, 0, bufferSize);
	}

	void UniformBuffer::UnLoad()
	{
		glDeleteBuffers(1, &UBO);
		UBO = GL_NONE;

		free(buffer);
		buffer = nullptr;

		table.clear();
		bufferSize = 0;
		dirty = true;
	}

	void UniformBuffer::Bind(unsigned slot) const
	{
		glBindBufferBase(GL_UNIFORM_BUFFER, slot, UBO);

		if (dirty)
		{
			glBufferSubData(GL_UNIFORM_BUFFER, 0, bufferSize, buffer);
			dirty = false;
		}
	}

	void UniformBuffer::UnBind(unsigned slot)
	{
		glBindBufferBase(GL_UNIFORM_BUFFER, slot, GL_NONE);<commit_msg>Replaced malloc+memset with calloc<commit_after>// Copyright (c) 2017 <NAME>
#include "Jewel3D/Precompiled.h"
#include "UniformBuffer.h"
#include "Jewel3D/Application/Logging.h"
#include "Jewel3D/Math/Vector.h"

#include <algorithm>
#include <GLEW/GL/glew.h>

namespace
{
	// Rounds val up to the nearest multiple.
	unsigned round(unsigned val, unsigned multiple)
	{
		unsigned remainder = val % multiple;
		if (remainder != 0)
		{
			val += multiple - remainder;
		}

		return val;
	}
}

namespace Jwl
{
	UniformBuffer::~UniformBuffer()
	{
		UnLoad();
	}

	UniformBuffer& UniformBuffer::operator=(const UniformBuffer& other)
	{
		UnLoad();
	
		table = other.table;
		bufferSize = other.bufferSize;
	
		if (bufferSize != 0)
		{
			// We have to create our own UBO.
			InitBuffer();
	
			memcpy(buffer, other.buffer, bufferSize);
		}
	
		return *this;
	}
	
	void UniformBuffer::Copy(const UniformBuffer& other)
	{
		*this = other;
	}

	void UniformBuffer::InitBuffer()
	{
		ASSERT(bufferSize != 0, "At least one uniform must be added to the buffer before it can be initialized.");
		ASSERT(UBO == GL_NONE, "UniformBuffer has already been initialized and locked.");

		// The total size of a buffer is always aligned to the size of a vec4.
		bufferSize = round(bufferSize, sizeof(vec4));

		// GPU buffer.
		glGenBuffers(1, &UBO);
		glBindBuffer(GL_UNIFORM_BUFFER, UBO);
		glBufferData(GL_UNIFORM_BUFFER, bufferSize, nullptr, GL_STREAM_DRAW);
		glBindBuffer(GL_UNIFORM_BUFFER, GL_NONE);

		// RAM buffer.
		buffer = calloc(1, bufferSize);
	}

	void UniformBuffer::UnLoad()
	{
		glDeleteBuffers(1, &UBO);
		UBO = GL_NONE;

		free(buffer);
		buffer = nullptr;

		table.clear();
		bufferSize = 0;
		dirty = true;
	}

	void UniformBuffer::Bind(unsigned slot) const
	{
		glBindBufferBase(GL_UNIFORM_BUFFER, slot, UBO);

		if (dirty)
		{
			glBufferSubData(GL_UNIFORM_BUFFER, 0, bufferSize, buffer);
			dirty = false;
		}
	}

	void UniformBuffer::UnBind(unsigned slot)
	{
		glBindBufferBase(GL_UNIFORM_BUFFER, slot, GL_NONE);
{'fraction_non_alphanumeric': 0.12243383626020282, 'fraction_numerical': 0.008409596834034134, 'mean_word_length': 4.029850746268656, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4726991', 'n_tokens_mistral': 1591, 'n_tokens_neox': 1411, 'n_words': 354}