Beispiel #1
0
void
Scene::loadShader() 
{
	std::string sourcePtr = read("vshader.cg");

	auto optimal = cgD3D11GetOptimalOptions(mCgVertexShaderProfile);

	mVertexShaderId = cgCreateProgram(mCgContext, CG_SOURCE, sourcePtr.c_str(), mCgVertexShaderProfile, "main", optimal);

	if(mVertexShaderId != nullptr)
	{
		optimal = cgD3D11GetOptimalOptions(mCgFragmentShaderProfile);

		HRESULT res = cgD3D11LoadProgram(mVertexShaderId, NULL);

		CGerror error;
		const char *errorString = cgGetLastErrorString(&error);
		sourcePtr = read("fshader.cg");
		mFragmentShaderId = cgCreateProgram(mCgContext, CG_SOURCE, sourcePtr.c_str(), mCgFragmentShaderProfile, "main", optimal);
		errorString = cgGetLastErrorString(&error);
		res = cgD3D11LoadProgram(mFragmentShaderId, NULL);
	}

	// here the uniform can be set 
	CGparameter location = cgGetNamedParameter(mVertexShaderId, "ambient");
	checkForCgError("could not get uniform color location", mCgContext, false);
	cgSetParameter4fv(location, glm::value_ptr(glm::vec4(1.0,1.0,1.0,1.0)));
	checkForCgError("could not set uniform color", mCgContext, false);

	bindShader(mVertexShaderId, mFragmentShaderId);

}
Beispiel #2
0
void _initCgRuntime(ID3D11Device *dev)
{
	if(gCgContext == nullptr) {
		std::cout << "[CgRuntime] Initialize Cg-Runtime-Direct3D 11" << std::endl;
		//qDebug("[Cg] Initialize Cg");
		// register the error handler
		cgSetErrorHandler( &_cgErrorHandler, NULL);
		// create a new Cg Context
		gCgContext = cgCreateContext();

		
		HRESULT hr = cgD3D11SetDevice( gCgContext, dev);
	
		if( hr != S_OK )
			return;

		// Register the default state assignment for OpenGL
		//cgD3D11RegisterStates(gCgContext);
		// This will allow the Cg runtime to manage texture binding
		//cgD3D11SetManageTextureParameters(gCgContext, CG_TRUE);
		
		gCgVertexShaderProfile = cgD3D11GetLatestVertexProfile();
		if(gCgVertexShaderProfile == CG_PROFILE_UNKNOWN)
		{
			// ERROR
			std::cout << "[CgRuntime] Error: Could not get valid Vertex-Profile." << std::endl;
			return;
		}
		
		gCgGeometryShaderProfile = cgD3D11GetLatestGeometryProfile();
		if(gCgGeometryShaderProfile == CG_PROFILE_UNKNOWN)
		{
			// WARNING
			std::cout << "[CgRuntime] Warning: Could not get valid Geometry-Profile." << std::endl;
		}
		
		gCgFragmentShaderProfile = cgD3D11GetLatestPixelProfile();
		if(gCgFragmentShaderProfile == CG_PROFILE_UNKNOWN)
		{
			// ERROR
			std::cout << "[CgRuntime] Error: Could not get valid Fragment-Profile." << std::endl;
			return;
		}

		profileOpts = cgD3D11GetOptimalOptions(gCgVertexShaderProfile);
	}
}
Beispiel #3
0
 bool CgDxShader::initialize(D3D11GraphicDevice* device, const ResourcePtr& resource, const ShaderDesc& desc) {
     mDesc = desc;
     
     StreamPtr stream = resource->readIntoMemory();
     if(!stream)
         return false;
     MemoryStream* memStream = ((MemoryStream*)stream.get());
     std::string content(memStream->data(), memStream->data() + memStream->size());
     
     CGprofile profile = _d3d_feature_level_to_cgprofile(device->getDeviceFeatureLevel(), desc.type);
     mProgram = cgCreateProgram(mContext, 
         CG_SOURCE, 
         content.c_str(), 
         profile, 
         desc.entry.c_str(), 
         cgD3D11GetOptimalOptions(profile));
     if(_check_error(mContext) &&
         D3D11Debug::CHECK_RESULT( cgD3D11LoadProgram(mProgram, 0))) {
             return true;
     }
     return false;
 }
bool CgShaderProgramD3D11::compileCg(
    const io::stringc &SourceCodeString, const io::stringc &EntryPoint, const c8** CompilerOptions)
{
    if (!createProgram(SourceCodeString, EntryPoint, CompilerOptions ? CompilerOptions : cgD3D11GetOptimalOptions(cgProfile_)))
        return false;
    
    if (cgD3D11LoadProgram(cgProgram_, 0) != D3D_OK)
    {
        io::Log::error("Loading D3D11 Cg shader failed");
        return false;
    }
    
    return !CgShaderContext::checkForError("shader program loading");
}
Beispiel #5
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
}