Esempio n. 1
0
    CgDxEffect::CgDxEffect(D3D11GraphicDevice* device):
        mContext(0),
        mDevice(device) {
            mContext = cgCreateContext();
            mist_assert(mContext);

            cgD3D11SetDevice(mContext, device->getD3DDevice());
            cgD3D11SetManageTextureParameters(mContext, CG_TRUE);
    }
Esempio n. 2
0
void DXWindow::initPipeline()
{
	HRESULT hr;

#ifdef USE_CG
	// INIT CG
	{
		std::cout << "Initialize Cg-Runtime-Direct3D 11" << std::endl;
		//qDebug("[Cg] Initialize Cg");
		// register the error handler
		cgSetErrorHandler(&_cgErrorHandler, NULL);
		// create a new Cg Context
		mCgContext = cgCreateContext();


		HRESULT hr = cgD3D11SetDevice(mCgContext, mDevice);

		if (hr != S_OK)
			return;

		// Register the default state assignment for OpenGL
		cgD3D11RegisterStates(mCgContext);
		// This will allow the Cg runtime to manage texture binding
		cgD3D11SetManageTextureParameters(mCgContext, CG_TRUE);
	}


	// LOAD SHADER Cg
	{
		std::string sourcePtr = read("vshader.cg");
		int num = sizeof(CG_VERTEX_SHADER_PROFILES) / sizeof(CG_VERTEX_SHADER_PROFILES[0]);
		for (int i = 0; i < num; ++i)
		{
			auto optimal = cgD3D11GetOptimalOptions(CG_VERTEX_SHADER_PROFILES[i]);
			mVertexShaderId = cgCreateProgram(mCgContext, CG_SOURCE, sourcePtr.c_str(), CG_VERTEX_SHADER_PROFILES[i], "main", optimal);
			hr = cgD3D11LoadProgram(mVertexShaderId, NULL);

			if (SUCCEEDED(hr))
				break;
		}

		if (SUCCEEDED(hr))
		{
			std::cout << "Loaded vertex shader!\n";
		}
		else
		{
			std::cout << "Could not load vertex shader!\n";
			exit(-1);
		}

		sourcePtr = read("fshader.cg");
		num = sizeof(CG_PIXEL_SHADER_PROFILES) / sizeof(CG_PIXEL_SHADER_PROFILES[0]);
		for (int i = 0; i < num; ++i)
		{
			auto optimal = cgD3D11GetOptimalOptions(CG_PIXEL_SHADER_PROFILES[i]);
			mFragmentShaderId = cgCreateProgram(mCgContext, CG_SOURCE, sourcePtr.c_str(), CG_PIXEL_SHADER_PROFILES[i], "main", optimal);

			hr = cgD3D11LoadProgram(mFragmentShaderId, NULL);

			if (SUCCEEDED(hr))
				break;
		}

		if (SUCCEEDED(hr))
		{
			std::cout << "Loaded pixel shader!\n";
		}
		else
		{
			std::cout << "Could not load pixel shader!\n";
			exit(-1);
		}
		// get a D3D shader resource from CG shader resource
		ID3D10Blob* VS = cgD3D11GetCompiledProgram(mVertexShaderId);

		// activate the shader objects
		cgD3D11BindProgram(mVertexShaderId);
		cgD3D11BindProgram(mFragmentShaderId);

		// create the input layout object
		D3D11_INPUT_ELEMENT_DESC ied[] =
		{
			{ "POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0, D3D11_INPUT_PER_VERTEX_DATA, 0 },
			{ "COLOR", 0, DXGI_FORMAT_R32G32B32A32_FLOAT, 0, 12, D3D11_INPUT_PER_VERTEX_DATA, 0 },
		};

		hr = mDevice->CreateInputLayout(ied, 2, VS->GetBufferPointer(), VS->GetBufferSize(), &mLayout);

		if (SUCCEEDED(hr))
		{
			std::cout << "Created vertex layout for shader!\n";
		}
		else
		{
			std::cout << "Could not create vertex layout for shader!\n";
			exit(-1);
		}

		mDeviceContext->IASetInputLayout(mLayout);
	}
	// Load Shader HLSL
#else
	{
		//Compile Shaders from shader file
		hr = D3DX11CompileFromFile("shaders.shader", 0, 0, "VS", "vs_4_0", 0, 0, 0, &VS_Buffer, 0, 0);
		hr = D3DX11CompileFromFile("shaders.shader", 0, 0, "PS", "ps_4_0", 0, 0, 0, &PS_Buffer, 0, 0);

		//Create the Shader Objects
		hr = mDevice->CreateVertexShader(VS_Buffer->GetBufferPointer(), VS_Buffer->GetBufferSize(), NULL, &VS);
		hr = mDevice->CreatePixelShader(PS_Buffer->GetBufferPointer(), PS_Buffer->GetBufferSize(), NULL, &PS);

		//Set Vertex and Pixel Shaders
		mDeviceContext->VSSetShader(VS, 0, 0);
		mDeviceContext->PSSetShader(PS, 0, 0);

		// create the input layout object
		D3D11_INPUT_ELEMENT_DESC ied[] =
		{
			{ "POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0, D3D11_INPUT_PER_VERTEX_DATA, 0 },
			{ "COLOR", 0, DXGI_FORMAT_R32G32B32A32_FLOAT, 0, 12, D3D11_INPUT_PER_VERTEX_DATA, 0 },
		};

		hr = mDevice->CreateInputLayout(ied, 2, VS_Buffer->GetBufferPointer(), VS_Buffer->GetBufferSize(), &mLayout);

		if (SUCCEEDED(hr))
		{
			std::cout << "Created vertex layout for shader!\n";
		}
		else
		{
			std::cout << "Could not create vertex layout for shader!\n";
			exit(-1);
		}

		mDeviceContext->IASetInputLayout(mLayout);
	}
#endif
}