bool CResourceLoader::loadPipeline(const std::string& fullpath, const IResourceXmlParser::SPipelineCreateParams& createParams) const
{
	/* if the pipeline with the same name has already been loaded,
	maybe some mistake has occurred. such as two pipeline file with the same
	pipeline name. */
	IPipeline* pipeline = mPipelineManager->get(createParams.Name);
	if (pipeline)
	{
		GF_PRINT_CONSOLE_INFO("Pipeline '%s' (in the file '%s') has already been loaded. It can't been loaded again. \
							  			Do you put the pipelines with same names in different files ?\n",
										createParams.Name, fullpath.c_str());
		return false;
	}


	u32 shaderCount = createParams.Shaders.size();
	IShader* shaders[5];
	IShader* vertexShader;
	for (u32 i = 0; i < shaderCount; i++)
	{
		const IResourceXmlParser::SShaderCreateParams shaderCreateParams = createParams.Shaders[i];
		std::string shaderName = shaderCreateParams.FileName + std::string("-") + shaderCreateParams.FunctionName;
		IShader* shader = mShaderManager->get(shaderName);

		/* if the shader has not been loaded. Load it first. */
		if (!shader)
		{
			std::string shaderFullPath;
			if (!mResourceGroupManager->getFullPath(shaderCreateParams.FileName, shaderFullPath))
			{
				GF_PRINT_CONSOLE_INFO("Pipeline '%s' creation failed. Because the shader file '%s' doesn't exist.\n",
					createParams.Name.c_str(), shaderCreateParams.FileName.c_str());
				return false;
			}

			shader = mShaderManager->load(shaderCreateParams.Type, shaderFullPath, shaderCreateParams.FunctionName, shaderName);
			if (shader == nullptr)
			{
				GF_PRINT_CONSOLE_INFO("Pipeline '%s' creation failed. Due to the '%s' function in '%s' shader file.\n",
					createParams.Name.c_str(), shaderCreateParams.FunctionName.c_str(), shaderFullPath.c_str());
				return false;
			}
		}

		shaders[i] = shader;

		/* find the vertex shader, which will be used to create input-layout soon.*/
		if (shader->getType() == EST_VERTEX_SHADER)
		{
			vertexShader = shader;
		}
	}

	/* create the input-layout. */
	/* if the input-layout with the same layout has been created before, just get it.*/
	IInputLayout* inputLayout = mInputlayoutManager->get(createParams.InputLayoutElements);
	// if there is no input-layout with the same vertex formats. just create it.
	if (!inputLayout)
	{
		inputLayout = mInputlayoutManager->create(createParams.InputLayoutElements, vertexShader);
		if (!inputLayout)
		{
			GF_PRINT_CONSOLE_INFO("Pipeline '%s' creation failed. Due to the input-layout create failure in file '%s'.\n",
				createParams.Name, fullpath.c_str());
			return false;
		}
	}


	/* create render state */
	std::string rsname = createParams.Name + ".rs";
	IRenderState* pRenderState = mRenderStateManager->get(rsname);
	if (!pRenderState)
	{
		pRenderState = mRenderStateManager->create(rsname);
		if (!pRenderState)
		{
			GF_PRINT_CONSOLE_INFO("Pipeline '%s' creation failed. Due to the render-state create failure in file '%s'.\n",
				createParams.Name, fullpath.c_str());
			return false;
		}
	}

	// set all the render states.
	for (u32 i = 0; i < createParams.RenderStates.size(); i++)
	{
		const IResourceXmlParser::SRenderStateCreateParams& rsCreateParam = createParams.RenderStates[i];
		switch (rsCreateParam.StateType)
		{
			/* if the render-state need a float value */
		case ERS_DEPTH_BIAS_CLAMP:
			pRenderState->setFloat(rsCreateParam.StateType, rsCreateParam.FloatValue);
			break;
			/* others are unsigned int. */
		default:
			pRenderState->set(rsCreateParam.StateType, rsCreateParam.DwordValue);
			break;
		}
	}

	pRenderState->confirm();

	// create the pipeline object
	pipeline = mPipelineManager->create(createParams.Name, shaders, shaderCount, inputLayout,
		createParams.PrimitiveType, pRenderState);
	if (!pipeline)
	{
		GF_PRINT_CONSOLE_INFO("Pipeline '%s' creation failed (in file '%s').\n",
			createParams.Name, fullpath.c_str());

		// TODO: should drop pRenderState object.
		return false;
	}

	/* set the shader auto-inject variables. */
	for (u32 i = 0; i < createParams.ShaderAutoVariables.size(); i++)
	{
		pipeline->addShaderAutoVariable(createParams.ShaderAutoVariables[i]);
	}

	return true;
}
Example #2
0
int main()
{

	int d = 1;
	GF_PRINT_CONSOLE_INFO("Hello:%d\n", d);

	SDeviceContextSettings settings;
	settings.MultiSamplingCount = 4;
	settings.MultiSamplingQuality = 32;

	IDevice* device = createDevice(EDT_DIRECT3D11, 800, 600, EWS_NONE, true, settings);
	IVideoDriver* driver = device->getVideoDriver();
	IShaderManager* shaderManager = driver->getShaderManager();
	IInputLayoutManager* inputlayoutManager = driver->getInputLayoutManager();
	IPipelineManager* pipelineMgr = driver->getPipelineManager();

	IShader* vs = shaderManager->load(EST_VERTEX_SHADER, "color.hlsl", "ColorVertexShader");
	IShader* ps = shaderManager->load(EST_PIXEL_SHADER, "PixelShader.hlsl", "ColorPixelShader");

	std::vector<SInputLayoutElement> elements;
	elements.resize(2);
	elements[0].SemanticName = "POSITION";
	elements[0].SemanticIndex = 0;
	elements[0].Format = EGF_R32G32B32_FLOAT;
	elements[0].Offset = 0;
	elements[1].SemanticName = "COLOR";
	elements[1].SemanticIndex = 0;
	elements[1].Format = EGF_R32G32B32A32_FLOAT;
	elements[1].Offset = 12;

	IInputLayout* layout = inputlayoutManager->create(elements, vs);


	IShader* shaders[2] = { vs, ps };
	IPipeline* pipeline = pipelineMgr->create("color", shaders, 2, layout, EPT_TRIANGLELIST);

	ISceneManager* smgr = device->getSceneManager();
	
	Vertex vertices[3];
	vertices[0] = Vertex(XMFLOAT3(-1.0f, -0.6f, 0.0f), XMFLOAT4(1.0f, 0.0f, 0.0f, 1.0f));
	vertices[1] = Vertex(XMFLOAT3(0.0f,   0.6f, 0.0f), XMFLOAT4(0.0f, 1.0f, 0.0f, 1.0f));
	vertices[2] = Vertex(XMFLOAT3(1.0f,  -0.6f, 0.0f), XMFLOAT4(1.0f, 0.0f, 1.0f, 1.0f));
	//IMesh* mesh = smgr->createSimpleMesh(&vertices, 3, sizeof(Vertex), nullptr, 0, 0);
	IMesh* mesh = smgr->createCubeMesh();
	IMeshNode* meshNode = smgr->addMeshNode(mesh, pipeline);

	//CD3D11ShaderManager* s = new CD3D11ShaderManager(nullptr);

	XMVECTOR eye = XMVectorSet(0.0f, 0.0f, -5.0f, 1.0f);
	XMVECTOR up = XMVectorSet(0.0f, 1.0f, 0.0f, 0.0f);
	XMVECTOR at = XMVectorZero();

	XMMATRIX view = XMMatrixLookAtLH(eye, at, up);

	XMMATRIX proj = XMMatrixPerspectiveFovLH(0.25f * 3.14f,
		static_cast<float>(SCREEN_WIDTH) / static_cast<float>(SCREEN_HEIGHT),
		1.0f, 1000.0f);


	meshNode->translate(0, 0, 5.0f);
	XMMATRIX world = meshNode->getAbsoluteTransformation();
	//XMMATRIX world = XMMatrixIdentity();

	
	pipeline->setMatrix("viewMatrix", reinterpret_cast<f32*>(&view));
	pipeline->setMatrix("projectionMatrix", reinterpret_cast<f32*>(&proj));

	SShaderAutoVariable var;
	var.Type = ESAVT_WORLD_MATRIX;
	var.ShaderType = EST_VERTEX_SHADER;
	var.VariableName = "worldMatrix";

	pipeline->addShaderAutoVariable(var);

	std::cout << "Hello World" << std::endl;

	ITimer* timer = device->createTimer();
	timer->reset();
	while (device->run())
	{
		const float clearColor[] = { 0.0f, 0.0f, 0.0f, 1.0f };
		driver->beginScene(true, true, clearColor);

		f32 dt = timer->tick();
		//std::cout << dt << std::endl;

		meshNode->setPosition(0, 0, -2.0f);
		meshNode->yaw(1.0f * dt);

		smgr->drawAll();

		//XMMATRIX world = meshNode->getAbsoluteTransformation();
		//pipeline->setMatrix("worldMatrix", reinterpret_cast<f32*>(&world));

		driver->endScene();
	}

	return 0;
}