<commit_before>#include "Material.h"
#include "Logger.h"
#include "DeviceContext.h"
#include "DeletingQueue.h"
#include "SamplerFactory.h"
#include "GraphicsFactory.h"
#include "DynamicBuffer.h"
#include "Texture.h"
#include "IMesh.h"

namespace ve {

	/***********************************/
	/* private - struct Material::Impl */
	/***********************************/

	struct Material::Impl
	{
		DeviceContextPtr deviceContext;

		uint64_t key;
		StringW name;
		uint32_t shaderFlags;

		Material::Uniform uniform;

		uint32_t curTexturesIndex;
		uint32_t nextTexturesIndex;
		collection::Array2<TexturePtr, 2, Material::TEXTURE_TYPE_COUNT> textures;
		collection::Array1<Material::TextureInfo, Material::TEXTURE_TYPE_COUNT> textureInfos;

		V3D_POLYGON_MODE polygonMode;
		V3D_CULL_MODE cullMode;
		BLEND_MODE blendMode;

		uint32_t dynamicOffset;
		DynamicBuffer* pUniformBuffer;
		IV3DDescriptorSet* pNativeDescriptorSet[2];

		uint8_t updateFlags;

		collection::Map<IMesh*, collection::Vector<Material::Session>> sessions;

		Impl(Material* pMaterial) :
			key(0),
			shaderFlags(0),
			uniform({}),
			curTexturesIndex(0),
			nextTexturesIndex(1),
			dynamicOffset(0),
			pUniformBuffer(nullptr),
			updateFlags(Material::UPDATE_ALL),
			polygonMode(V3D_POLYGON_MODE_FILL),
			cullMode(V3D_CULL_MODE_BACK),
			blendMode(BLEND_MODE_COPY)
		{
			key = reinterpret_cast<uint64_t>(pMaterial);

			uniform.diffuseColor = glm::vec4(1.0f, 1.0f, 1.0f, 1.0f);
			uniform.diffuseFactor = 1.0f;
			uniform.specularColor = glm::vec4(1.0f, 1.0f, 1.0f, 1.0f);
			uniform.specularFactor = 0.0f;
			uniform.shininess = 1.0f;
			uniform.emissiveFactor = 0.0f;

			for (size_t i = 0; i < 2; i++)
			{
				for (size_t j = 0; j < textures[i].size(); j++)
				{
					textures[i][j] = {};
				}
			}

			textureInfos[Material::TEXTURE_TYPE_DIFFUSE].filter = V3D_FILTER_LINEAR;
			textureInfos[Material::TEXTURE_TYPE_DIFFUSE].addressMode = V3D_ADDRESS_MODE_REPEAT;
			textureInfos[Material::TEXTURE_TYPE_DIFFUSE].mipLevel = 0;
			textureInfos[Material::TEXTURE_TYPE_DIFFUSE].anisotropyEnable = true;
			textureInfos[Material::TEXTURE_TYPE_DIFFUSE].enable = false;
			textureInfos[Material::TEXTURE_TYPE_DIFFUSE].update = false;

			textureInfos[Material::TEXTURE_TYPE_SPECULAR].filter = V3D_FILTER_LINEAR;
			textureInfos[Material::TEXTURE_TYPE_SPECULAR].addressMode = V3D_ADDRESS_MODE_REPEAT;
			textureInfos[Material::TEXTURE_TYPE_SPECULAR].mipLevel = 0;
			textureInfos[Material::TEXTURE_TYPE_SPECULAR].anisotropyEnable = true;
			textureInfos[Material::TEXTURE_TYPE_SPECULAR].enable = false;
			textureInfos[Material::TEXTURE_TYPE_SPECULAR].update = false;

			textureInfos[Material::TEXTURE_TYPE_BUMP].filter = V3D_FILTER_NEAREST;
			textureInfos[Material::TEXTURE_TYPE_BUMP].addressMode = V3D_ADDRESS_MODE_REPEAT;
			textureInfos[Material::TEXTURE_TYPE_BUMP].mipLevel = 0;
			textureInfos[Material::TEXTURE_TYPE_BUMP].anisotropyEnable = false;
			textureInfos[Material::TEXTURE_TYPE_BUMP].enable = false;
			textureInfos[Material::TEXTURE_TYPE_BUMP].update = false;

			pNativeDescriptorSet[Material::DST_COLOR] = nullptr;
			pNativeDescriptorSet[Material::DST_SHADOW] = nullptr;
		}

		bool Initialize(DeviceContextPtr deviceContext, const wchar_t* pName)
		{
			this->deviceContext = deviceContext;

			if (pName != nullptr)
			{
				name = pName;
			}

			pUniformBuffer = DynamicBuffer::Create(this->deviceContext, V3D_BUFFER_USAGE_UNIFORM, sizeof(Material::Uniform), V3D_PIPELINE_STAGE_FRAGMENT_SHADER, V3D_ACCESS_UNIFORM_READ, VE_INTERFACE_DEBUG_NAME(L"Material"));
			if (pUniformBuffer == nullptr)
			{
				return false;
			}

			dynamicOffset = pUniformBuffer->GetNativeRangeSize();

			return true;
		}

		void SetTexture(Material::TEXTURE_TYPE type, TexturePtr texture, uint32_t shaderBit)
		{
			if (texture != nullptr)
			{
				if (textures[curTexturesIndex][type] != nullptr)
				{
					if (textures[curTexturesIndex][type] != texture)
					{
						VE_SET_BIT(updateFlags, Material::UPDATE_TEXTURES);

						textures[nextTexturesIndex][type] = texture;
						textureInfos[type].update = true;
					}
				}
				else
				{
					VE_SET_BIT(updateFlags, Material::UPDATE_PIPELINE);
					VE_SET_BIT(updateFlags, Material::RECREATE_DESCRIPTOR_SET);
					VE_SET_BIT(updateFlags, Material::UPDATE_TEXTURES);

					textures[nextTexturesIndex][type] = texture;
					textureInfos[type].update = true;
				}

				textureInfos[type].mipLevel = texture->GetNativeImageViewPtr()->GetDesc().levelCount - 1;
				textureInfos[type].enable = true;

				VE_SET_BIT(shaderFlags, shaderBit);
			}
			else
			{
				if (textures[curTexturesIndex][type] != nullptr)
				{
					VE_SET_BIT(updateFlags, Material::UPDATE_PIPELINE);
					VE_SET_BIT(updateFlags, Material::RECREATE_DESCRIPTOR_SET);
					VE_SET_BIT(updateFlags, Material::UPDATE_TEXTURES);

					textures[nextTexturesIndex][type] = texture;
					textureInfos[type].update = true;
				}

				textureInfos[type].mipLevel = 0;
				textureInfos[type].enable = false;

				VE_RESET_BIT(shaderFlags, shaderBit);
			}
		}

		VE_DECLARE_ALLOCATOR
	};

	/*********************/
	/* public - Material */
	/*********************/

	MaterialPtr Material::Create(DeviceContextPtr deviceContext)
	{
		MaterialPtr material = std::make_shared<Material>();
<commit_msg>テクスチャを設定しない定義を追加<commit_after>#include "Material.h"
#include "Logger.h"
#include "DeviceContext.h"
#include "DeletingQueue.h"
#include "SamplerFactory.h"
#include "GraphicsFactory.h"
#include "DynamicBuffer.h"
#include "Texture.h"
#include "IMesh.h"

//#define VE_MATERIAL_NO_TEXTURE

namespace ve {

	/***********************************/
	/* private - struct Material::Impl */
	/***********************************/

	struct Material::Impl
	{
		DeviceContextPtr deviceContext;

		uint64_t key;
		StringW name;
		uint32_t shaderFlags;

		Material::Uniform uniform;

		uint32_t curTexturesIndex;
		uint32_t nextTexturesIndex;
		collection::Array2<TexturePtr, 2, Material::TEXTURE_TYPE_COUNT> textures;
		collection::Array1<Material::TextureInfo, Material::TEXTURE_TYPE_COUNT> textureInfos;

		V3D_POLYGON_MODE polygonMode;
		V3D_CULL_MODE cullMode;
		BLEND_MODE blendMode;

		uint32_t dynamicOffset;
		DynamicBuffer* pUniformBuffer;
		IV3DDescriptorSet* pNativeDescriptorSet[2];

		uint8_t updateFlags;

		collection::Map<IMesh*, collection::Vector<Material::Session>> sessions;

		Impl(Material* pMaterial) :
			key(0),
			shaderFlags(0),
			uniform({}),
			curTexturesIndex(0),
			nextTexturesIndex(1),
			dynamicOffset(0),
			pUniformBuffer(nullptr),
			updateFlags(Material::UPDATE_ALL),
			polygonMode(V3D_POLYGON_MODE_FILL),
			cullMode(V3D_CULL_MODE_BACK),
			blendMode(BLEND_MODE_COPY)
		{
			key = reinterpret_cast<uint64_t>(pMaterial);

			uniform.diffuseColor = glm::vec4(1.0f, 1.0f, 1.0f, 1.0f);
			uniform.diffuseFactor = 1.0f;
			uniform.specularColor = glm::vec4(1.0f, 1.0f, 1.0f, 1.0f);
			uniform.specularFactor = 0.0f;
			uniform.shininess = 1.0f;
			uniform.emissiveFactor = 0.0f;

			for (size_t i = 0; i < 2; i++)
			{
				for (size_t j = 0; j < textures[i].size(); j++)
				{
					textures[i][j] = {};
				}
			}

			textureInfos[Material::TEXTURE_TYPE_DIFFUSE].filter = V3D_FILTER_LINEAR;
			textureInfos[Material::TEXTURE_TYPE_DIFFUSE].addressMode = V3D_ADDRESS_MODE_REPEAT;
			textureInfos[Material::TEXTURE_TYPE_DIFFUSE].mipLevel = 0;
			textureInfos[Material::TEXTURE_TYPE_DIFFUSE].anisotropyEnable = true;
			textureInfos[Material::TEXTURE_TYPE_DIFFUSE].enable = false;
			textureInfos[Material::TEXTURE_TYPE_DIFFUSE].update = false;

			textureInfos[Material::TEXTURE_TYPE_SPECULAR].filter = V3D_FILTER_LINEAR;
			textureInfos[Material::TEXTURE_TYPE_SPECULAR].addressMode = V3D_ADDRESS_MODE_REPEAT;
			textureInfos[Material::TEXTURE_TYPE_SPECULAR].mipLevel = 0;
			textureInfos[Material::TEXTURE_TYPE_SPECULAR].anisotropyEnable = true;
			textureInfos[Material::TEXTURE_TYPE_SPECULAR].enable = false;
			textureInfos[Material::TEXTURE_TYPE_SPECULAR].update = false;

			textureInfos[Material::TEXTURE_TYPE_BUMP].filter = V3D_FILTER_NEAREST;
			textureInfos[Material::TEXTURE_TYPE_BUMP].addressMode = V3D_ADDRESS_MODE_REPEAT;
			textureInfos[Material::TEXTURE_TYPE_BUMP].mipLevel = 0;
			textureInfos[Material::TEXTURE_TYPE_BUMP].anisotropyEnable = false;
			textureInfos[Material::TEXTURE_TYPE_BUMP].enable = false;
			textureInfos[Material::TEXTURE_TYPE_BUMP].update = false;

			pNativeDescriptorSet[Material::DST_COLOR] = nullptr;
			pNativeDescriptorSet[Material::DST_SHADOW] = nullptr;
		}

		bool Initialize(DeviceContextPtr deviceContext, const wchar_t* pName)
		{
			this->deviceContext = deviceContext;

			if (pName != nullptr)
			{
				name = pName;
			}

			pUniformBuffer = DynamicBuffer::Create(this->deviceContext, V3D_BUFFER_USAGE_UNIFORM, sizeof(Material::Uniform), V3D_PIPELINE_STAGE_FRAGMENT_SHADER, V3D_ACCESS_UNIFORM_READ, VE_INTERFACE_DEBUG_NAME(L"Material"));
			if (pUniformBuffer == nullptr)
			{
				return false;
			}

			dynamicOffset = pUniformBuffer->GetNativeRangeSize();

			return true;
		}

		void SetTexture(Material::TEXTURE_TYPE type, TexturePtr texture, uint32_t shaderBit)
		{
#ifndef VE_MATERIAL_NO_TEXTURE
			if (texture != nullptr)
			{
				if (textures[curTexturesIndex][type] != nullptr)
				{
					if (textures[curTexturesIndex][type] != texture)
					{
						VE_SET_BIT(updateFlags, Material::UPDATE_TEXTURES);

						textures[nextTexturesIndex][type] = texture;
						textureInfos[type].update = true;
					}
				}
				else
				{
					VE_SET_BIT(updateFlags, Material::UPDATE_PIPELINE);
					VE_SET_BIT(updateFlags, Material::RECREATE_DESCRIPTOR_SET);
					VE_SET_BIT(updateFlags, Material::UPDATE_TEXTURES);

					textures[nextTexturesIndex][type] = texture;
					textureInfos[type].update = true;
				}

				textureInfos[type].mipLevel = texture->GetNativeImageViewPtr()->GetDesc().levelCount - 1;
				textureInfos[type].enable = true;

				VE_SET_BIT(shaderFlags, shaderBit);
			}
			else
			{
				if (textures[curTexturesIndex][type] != nullptr)
				{
					VE_SET_BIT(updateFlags, Material::UPDATE_PIPELINE);
					VE_SET_BIT(updateFlags, Material::RECREATE_DESCRIPTOR_SET);
					VE_SET_BIT(updateFlags, Material::UPDATE_TEXTURES);

					textures[nextTexturesIndex][type] = texture;
					textureInfos[type].update = true;
				}

				textureInfos[type].mipLevel = 0;
				textureInfos[type].enable = false;

				VE_RESET_BIT(shaderFlags, shaderBit);
			}
#endif //VE_MATERIAL_NO_TEXTURE
		}

		VE_DECLARE_ALLOCATOR
	};

	/*********************/
	/* public - Material */
	/*********************/

	MaterialPtr Material::Create(DeviceContextPtr deviceContext)
	{
		MaterialPtr material = std::make_shared<Material>();

{'fraction_non_alphanumeric': 0.13522365712146733, 'fraction_numerical': 0.01347557551937114, 'mean_word_length': 5.184606481481482, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 25, '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': '3601925', 'n_tokens_mistral': 4398, 'n_tokens_neox': 3651, 'n_words': 504}