예제 #1
0
파일: Scene.cpp 프로젝트: andyreimann/gear
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);

}
i32	CCGShaderSystem::CompilePixelShader(Resources::IPixelShader* pPS, lpcastr lpszShader, ul32 ulShaderSize, lpcastr lpszEntryPoint, SHADER_PROFILE eProfile)
{
    xst_assert( pPS, "(CCGShaderSystem::CompilePixelShader)" );
    //If profile is not created, create it
    Resources::CCGPixelShader* pShader = (Resources::CCGPixelShader*)pPS;
    if( pShader->GetCGProfile() == CG_PROFILE_UNKNOWN )
    {
        pShader->SetCGProfile( this->m_aeProfiles[ eProfile ] );
    }

    if( XST_FAILED( XSE::CCGShaderSystem::CompilePixelShader( pPS ) ) )
    {
        return XST_FAIL;
    }


    CGprogram CGShader = pShader->GetCGShader();
    if( CGShader == xst_null )
    {
        XST_LOG_ERR( "CG pixel shader: " << pShader->GetResourceName() << " not created" );
        return XST_FAIL;
    }
    cgD3D11LoadProgram( CGShader, pShader->GetShaderFlags() );
    CHECK_CG_ERR( "Loading d3d11 program", pPS->GetResourceName().data() );
    return XST_OK;
}
i32	CCGShaderSystem::CompilePixelShader(Resources::IPixelShader* pPS)
{
    xst_assert( pPS, "(CCGShaderSystem::CompilePixelShader)" );
    //If profile is not created, create it
    Resources::CCGPixelShader* pShader = (Resources::CCGPixelShader*)pPS;
    if( pShader->GetCGProfile() == CG_PROFILE_UNKNOWN )
    {
        pShader->SetCGProfile( this->m_aeProfiles[ pShader->GetProfile() ] );
    }

    /*if( XST_FAILED( XSE::CCGShaderSystem::CompilePixelShader( pPS ) ) )
    {
    	return XST_FAIL;
    }*/

    xst_astring strData = (lpcastr)pShader->GetResourceFile().GetPtr()->GetData().GetPointer();
    CG::CreatePShader( strData, pShader->GetEntryPoint() );

    //if( XST_FAILED( XSE::CCGShaderSystem::CompileVertexShader( pVS ) ) )
    if( XST_FAILED( XSE::CCGShaderSystem::CompilePixelShader( CG::g_strShader.data(), CG::g_strShader.length(), pPS ) ) )
    {
        return XST_FAIL;
    }


    CGprogram CGShader = pShader->GetCGShader();
    if( CGShader == xst_null )
    {
        XST_LOG_ERR( "CG pixel shader: " << pShader->GetResourceName() << " not created" );
        return XST_FAIL;
    }
    cgD3D11LoadProgram( CGShader, pShader->GetShaderFlags() );
    CHECK_CG_ERR( "Loading d3d11 program", pPS->GetResourceName().data() );
    return XST_OK;
}
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");
}
예제 #5
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;
 }
예제 #6
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
}