Пример #1
0
//==============================================================================
Error Material::populateVariables(const MaterialProgramCreator& loader)
{
	Error err = ErrorCode::NONE;

	U varCount = 0;
	for(const auto& in : loader.getInputVariables())
	{
		if(!in.m_constant)
		{
			++varCount;
		}
	}

	ANKI_CHECK(m_vars.create(m_resources->_getAllocator(), varCount));

	varCount = 0;
	for(const auto& in : loader.getInputVariables())
	{
		if(in.m_constant)
		{
			continue;
		}

		MaterialVariable* mtlvar = nullptr;

		switch(in.m_type)
		{
		// samplers
		case ShaderVariableDataType::SAMPLER_2D:
		case ShaderVariableDataType::SAMPLER_CUBE:
			{
				TextureResourcePointer tp;
				
				if(in.m_value.getSize() > 0)
				{
					ANKI_CHECK(tp.load(
						in.m_value.getBegin()->toCString(), m_resources));
				}

				auto alloc = m_resources->_getAllocator();
				MaterialVariableTemplate<TextureResourcePointer>* tvar = 
					alloc.newInstance<
					MaterialVariableTemplate<TextureResourcePointer>>();

				if(tvar)
				{
					err = tvar->create(alloc, in.m_name.toCString(), &tp, 1);

					if(err)
					{
						alloc.deleteInstance(tvar);
						tvar = nullptr;
					}
				}

				mtlvar = tvar;
			}
			break;
		case ShaderVariableDataType::FLOAT:
			mtlvar = MaterialVariableTemplate<F32>::_newInstance(in,
				m_resources->_getAllocator(), m_resources->_getTempAllocator());
			break;
		case ShaderVariableDataType::VEC2:
			mtlvar = MaterialVariableTemplate<Vec2>::_newInstance(in,
				m_resources->_getAllocator(), m_resources->_getTempAllocator());
			break;
		case ShaderVariableDataType::VEC3:
			mtlvar = MaterialVariableTemplate<Vec3>::_newInstance(in,
				m_resources->_getAllocator(), m_resources->_getTempAllocator());
			break;
		case ShaderVariableDataType::VEC4:
			mtlvar = MaterialVariableTemplate<Vec4>::_newInstance(in,
				m_resources->_getAllocator(), m_resources->_getTempAllocator());
			break;
		case ShaderVariableDataType::MAT3:
			mtlvar = MaterialVariableTemplate<Mat3>::_newInstance(in,
				m_resources->_getAllocator(), m_resources->_getTempAllocator());
			break;
		case ShaderVariableDataType::MAT4:
			mtlvar = MaterialVariableTemplate<Mat4>::_newInstance(in,
				m_resources->_getAllocator(), m_resources->_getTempAllocator());
			break;
		// default is error
		default:
			ANKI_ASSERT(0);
		}

		if(mtlvar == nullptr)
		{
			return ErrorCode::USER_DATA;
		}

		m_vars[varCount++] = mtlvar;
	}

	return ErrorCode::NONE;
}
Пример #2
0
//==============================================================================
void Material::populateVariables(const MaterialProgramCreator& mspc)
{
	for(auto in : mspc.getInputVariables())
	{
		if(in.m_constant)
		{
			continue;
		}

		MaterialVariable* mtlvar = nullptr;
		const GlProgramVariable* glvar = nullptr;

		// Find the input variable in one of the programs
		for(const ProgramResourcePointer& progr : m_progs)
		{
			const GlProgramHandle& prog = progr->getGlProgram();

			glvar = prog.tryFindVariable(in.m_name.toCString());
			if(glvar)
			{
				break;
			}
		}

		// Check if variable found
		if(glvar == nullptr)
		{
			throw ANKI_EXCEPTION("Variable not found in "
				"at least one program: %s", &in.m_name[0]);
		}

		switch(glvar->getDataType())
		{
		// samplers
		case GL_SAMPLER_2D:
		case GL_SAMPLER_CUBE:
			{
				TextureResourcePointer tp;
				
				if(in.m_value.size() > 0)
				{
					tp = TextureResourcePointer(
						in.m_value[0].toCString(),
						m_resources);
				}

				auto alloc = m_resources->_getAllocator();
				mtlvar = alloc.newInstance<
					MaterialVariableTemplate<TextureResourcePointer>>(
					glvar, false, &tp, 1, alloc);
			}
			break;
		// F32
		case GL_FLOAT:
			mtlvar = newMaterialVariable<F32>(*glvar, in,
				m_resources->_getAllocator(), m_resources->_getTempAllocator());
			break;
		// vec2
		case GL_FLOAT_VEC2:
			mtlvar = newMaterialVariable<Vec2>(*glvar, in,
				m_resources->_getAllocator(), m_resources->_getTempAllocator());
			break;
		// vec3
		case GL_FLOAT_VEC3:
			mtlvar = newMaterialVariable<Vec3>(*glvar, in,
				m_resources->_getAllocator(), m_resources->_getTempAllocator());
			break;
		// vec4
		case GL_FLOAT_VEC4:
			mtlvar = newMaterialVariable<Vec4>(*glvar, in,
				m_resources->_getAllocator(), m_resources->_getTempAllocator());
			break;
		// mat3
		case GL_FLOAT_MAT3:
			mtlvar = newMaterialVariable<Mat3>(*glvar, in,
				m_resources->_getAllocator(), m_resources->_getTempAllocator());
			break;
		// mat4
		case GL_FLOAT_MAT4:
			mtlvar = newMaterialVariable<Mat4>(*glvar, in,
				m_resources->_getAllocator(), m_resources->_getTempAllocator());
			break;
		// default is error
		default:
			ANKI_ASSERT(0);
		}

		m_vars.push_back(mtlvar);
	}
}