<commit_before>/*
 * Copyright (c) 2012-2016 <NAME> and individual contributors.
 * License: https://github.com/taylor001/crown/blob/master/LICENSE
 */

#include "file.h"
#include "material_manager.h"
#include "material_resource.h"
#include "resource_manager.h"
#include "sort_map.h"
#include <string.h> // memcpy
#include <bgfx/bgfx.h>

namespace crown
{
MaterialManager::MaterialManager(Allocator& a, ResourceManager& rm)
	: _allocator(&a)
	, _resource_manager(&rm)
	, _materials(a)
{
}

MaterialManager::~MaterialManager()
{
	auto begin = sort_map::begin(_materials);
	auto end = sort_map::end(_materials);

	for (; begin != end; ++begin)
	{
		_allocator->deallocate(begin->pair.second);
	}
}

void* MaterialManager::load(File& file, Allocator& a)
{
	const u32 file_size = file.size();
	void* res = a.allocate(file_size);
	file.read(res, file_size);
	CE_ASSERT(*(u32*)res == MATERIAL_VERSION, "Wrong version");
	return res;
}

void MaterialManager::online(StringId64 id, ResourceManager& rm)
{
	using namespace material_resource;

	MaterialResource* mr = (MaterialResource*) rm.get(MATERIAL_TYPE, id);

	char* base = (char*)mr + mr->dynamic_data_offset;

	for (u32 i = 0; i < mr->num_textures; ++i)
	{
		TextureData* td    = get_texture_data(mr, i);
		TextureHandle* th  = get_texture_handle(mr, i, base);
		th->sampler_handle = bgfx::createUniform(get_texture_name(mr, td), bgfx::UniformType::Int1).idx;
	}

	for (u32 i = 0; i < mr->num_uniforms; ++i)
	{
		UniformData* ud    = get_uniform_data(mr, i);
		UniformHandle* uh  = get_uniform_handle(mr, i, base);
		uh->uniform_handle = bgfx::createUniform(get_uniform_name(mr, ud), bgfx::UniformType::Vec4).idx;
	}

	create_material(id);
}

void MaterialManager::offline(StringId64 id, ResourceManager& rm)
{
	using namespace material_resource;

	MaterialResource* mr = (MaterialResource*) rm.get(MATERIAL_TYPE, id);

	char* base = (char*) mr + mr->dynamic_data_offset;

	for (u32 i = 0; i < mr->num_textures; ++i)
	{
		TextureHandle* th = get_texture_handle(mr, i, base);
		bgfx::UniformHandle sh;
		sh.idx = th->sampler_handle;
		bgfx::destroyUniform(sh);
	}

	for (u32 i = 0; i < mr->num_uniforms; ++i)
	{
		UniformHandle* uh = get_uniform_handle(mr, i, base);
		bgfx::UniformHandle bgfx_uh;
		bgfx_uh.idx = uh->uniform_handle;
		bgfx::destroyUniform(bgfx_uh);<commit_msg>Do not create material<commit_after>/*
 * Copyright (c) 2012-2016 <NAME> and individual contributors.
 * License: https://github.com/taylor001/crown/blob/master/LICENSE
 */

#include "file.h"
#include "material_manager.h"
#include "material_resource.h"
#include "resource_manager.h"
#include "sort_map.h"
#include <string.h> // memcpy
#include <bgfx/bgfx.h>

namespace crown
{
MaterialManager::MaterialManager(Allocator& a, ResourceManager& rm)
	: _allocator(&a)
	, _resource_manager(&rm)
	, _materials(a)
{
}

MaterialManager::~MaterialManager()
{
	auto begin = sort_map::begin(_materials);
	auto end = sort_map::end(_materials);

	for (; begin != end; ++begin)
	{
		_allocator->deallocate(begin->pair.second);
	}
}

void* MaterialManager::load(File& file, Allocator& a)
{
	const u32 file_size = file.size();
	void* res = a.allocate(file_size);
	file.read(res, file_size);
	CE_ASSERT(*(u32*)res == MATERIAL_VERSION, "Wrong version");
	return res;
}

void MaterialManager::online(StringId64 id, ResourceManager& rm)
{
	using namespace material_resource;

	MaterialResource* mr = (MaterialResource*) rm.get(MATERIAL_TYPE, id);

	char* base = (char*)mr + mr->dynamic_data_offset;

	for (u32 i = 0; i < mr->num_textures; ++i)
	{
		TextureData* td    = get_texture_data(mr, i);
		TextureHandle* th  = get_texture_handle(mr, i, base);
		th->sampler_handle = bgfx::createUniform(get_texture_name(mr, td), bgfx::UniformType::Int1).idx;
	}

	for (u32 i = 0; i < mr->num_uniforms; ++i)
	{
		UniformData* ud    = get_uniform_data(mr, i);
		UniformHandle* uh  = get_uniform_handle(mr, i, base);
		uh->uniform_handle = bgfx::createUniform(get_uniform_name(mr, ud), bgfx::UniformType::Vec4).idx;
	}
}

void MaterialManager::offline(StringId64 id, ResourceManager& rm)
{
	using namespace material_resource;

	MaterialResource* mr = (MaterialResource*) rm.get(MATERIAL_TYPE, id);

	char* base = (char*) mr + mr->dynamic_data_offset;

	for (u32 i = 0; i < mr->num_textures; ++i)
	{
		TextureHandle* th = get_texture_handle(mr, i, base);
		bgfx::UniformHandle sh;
		sh.idx = th->sampler_handle;
		bgfx::destroyUniform(sh);
	}

	for (u32 i = 0; i < mr->num_uniforms; ++i)
	{
		UniformHandle* uh = get_uniform_handle(mr, i, base);
		bgfx::UniformHandle bgfx_uh;
		bgfx_uh.idx = uh->uniform_handle;
		bgfx::destroyUniform(bgfx_uh);
{'fraction_non_alphanumeric': 0.15379637618636754, 'fraction_numerical': 0.014236410698878344, 'mean_word_length': 5.36076817558299, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 33, 'https://': 2, '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': '5300586', 'n_tokens_mistral': 1829, 'n_tokens_neox': 1713, 'n_words': 353}