Ejemplo n.º 1
0
HRESULT ShaderProgram::loadShader(WCHAR* fileName)
{
	if(!m_layout){
		return S_FALSE;
	}
    ID3DBlob* pVSBlob = NULL;
	HRESULT hr = compileShaderFromFile( fileName, "VS", "vs_4_0", &pVSBlob );
    if( FAILED( hr ) )
    {
        MessageBox( NULL,
                    L"The FX file cannot be compiled.  Please run this executable from the directory that contains the FX file.", L"Error", MB_OK );
		return hr;
	
    }
	hr = m_pd3dDevice->CreateVertexShader( pVSBlob->GetBufferPointer(), pVSBlob->GetBufferSize(), NULL, &m_pVertexShader );
	if( FAILED( hr ) )
	{	
		pVSBlob->Release();
		return hr;
	}

    // Create the input layout
	hr = m_pd3dDevice->CreateInputLayout( m_layout, m_numElemnts, pVSBlob->GetBufferPointer(),
                                          pVSBlob->GetBufferSize(), &m_pVertexLayout );
	if( FAILED( hr ) )
	{	
		pVSBlob->Release();
		return hr;
	}
	pVSBlob->Release();
	
	// Compile the pixel shader
	ID3DBlob* pPSBlob = NULL;
    hr = compileShaderFromFile( fileName, "PS", "ps_4_0", &pPSBlob );
    if( FAILED( hr ) )
    {
        MessageBox( NULL,
                    L"The FX file cannot be compiled.  Please run this executable from the directory that contains the FX file.", L"Error", MB_OK );
		return hr;
    }

	// Create the pixel shader
	hr = m_pd3dDevice->CreatePixelShader( pPSBlob->GetBufferPointer(), pPSBlob->GetBufferSize(), NULL, &m_pPixelShader );
	pPSBlob->Release();
	return S_OK;
}
Ejemplo n.º 2
0
HRESULT GenerationShader::InitShader(ID3D11Device * device, ID3D11DeviceContext * devcon)
{

	HRESULT hr = S_OK;

	//Vertex shader
	ID3DBlob* vsBlob = NULL;
	hr = compileShaderFromFile(L"Shaders/MarchingCubesVs.hlsl", "main", "vs_5_0", &vsBlob);

	if (FAILED(hr))
	{
		vsBlob->Release();
		return hr;
	}

	hr = device->CreateVertexShader(vsBlob->GetBufferPointer(),
		vsBlob->GetBufferSize(), NULL, &m_VertexShader);

	if (FAILED(hr))
	{
		vsBlob->Release();
		return hr;
	}

	//Create input layout
	D3D11_INPUT_ELEMENT_DESC layout[] =
	{
		{ "POSITION", 0, DXGI_FORMAT_R32G32B32A32_FLOAT, 0, 0, D3D11_INPUT_PER_VERTEX_DATA, 0 },
		{ "COLOR", 0, DXGI_FORMAT_R32G32B32A32_FLOAT, 0, D3D11_APPEND_ALIGNED_ELEMENT, D3D11_INPUT_PER_VERTEX_DATA, 0 },
	};
	UINT numElements = ARRAYSIZE(layout);
	hr = device->CreateInputLayout(layout, numElements, vsBlob->GetBufferPointer(),
		vsBlob->GetBufferSize(), &m_VertexLayout);
	vsBlob->Release();
	if (FAILED(hr))
		return hr;
	//set input layout to use
	devcon->IASetInputLayout(m_VertexLayout);


	//compile geometry shader
	ID3DBlob* gsBlob = NULL;
	hr = compileShaderFromFile(L"Shaders/MarchingCubesGs.hlsl", "main", "gs_5_0", &gsBlob);

	if (FAILED(hr))
		return hr;

	//stream output stage input signature declaration
	D3D11_SO_DECLARATION_ENTRY decl[] =
	{
		{ 0, "SV_POSITION", 0, 0, 4, 0 },
		{ 0, "COLOR", 0, 0, 4, 0 },
		{ 0, "TEXCOORD", 0, 0, 4, 0 },
	};
	UINT stream = (UINT)0;
	hr = device->CreateGeometryShaderWithStreamOutput
		(
			gsBlob->GetBufferPointer(),
			gsBlob->GetBufferSize(),
			decl,  //so declaration
			(UINT)3, //numentries
			NULL,
			0,
			stream, //rasterized stream
			NULL, //pointert to class linkage (not needed)
			&m_GeometryShader
			);

	gsBlob->Release();
	if (FAILED(hr))
	{
		return hr;
	}

	//compile and create pixel shader
	ID3DBlob* psBlob = NULL;
	hr = compileShaderFromFile(L"Shaders/MarchingCubesPs.hlsl", "main", "ps_5_0", &psBlob);
	if (FAILED(hr))
		return hr;
	hr = device->CreatePixelShader(psBlob->GetBufferPointer(), psBlob->GetBufferSize(), NULL, &m_PixelShader);
	psBlob->Release();
	if (FAILED(hr))
	{
		return hr;
	}


	//Create a basic point sampler for sampling our density data in the gpu
	//should refactor this elsewhere
	D3D11_SAMPLER_DESC sampDesc;
	ZeroMemory(&sampDesc, sizeof(sampDesc));
	sampDesc.Filter = D3D11_FILTER_MIN_MAG_MIP_LINEAR;
	sampDesc.AddressU = D3D11_TEXTURE_ADDRESS_WRAP;
	sampDesc.AddressV = D3D11_TEXTURE_ADDRESS_WRAP;
	sampDesc.AddressW = D3D11_TEXTURE_ADDRESS_WRAP;
	sampDesc.ComparisonFunc = D3D11_COMPARISON_NEVER;
	sampDesc.MinLOD = 0;
	sampDesc.MaxLOD = 0;
	hr = device->CreateSamplerState(&sampDesc, &m_SamplerPoint);
	if (FAILED(hr))
		return hr;

	// Create a texture sampler state description.
	sampDesc.Filter = D3D11_FILTER_ANISOTROPIC;
	sampDesc.AddressU = D3D11_TEXTURE_ADDRESS_WRAP;
	sampDesc.AddressV = D3D11_TEXTURE_ADDRESS_WRAP;
	sampDesc.AddressW = D3D11_TEXTURE_ADDRESS_WRAP;
	sampDesc.MipLODBias = 0.0f;
	sampDesc.MaxAnisotropy = 16;
	sampDesc.ComparisonFunc = D3D11_COMPARISON_ALWAYS;
	sampDesc.BorderColor[0] = 0;
	sampDesc.BorderColor[1] = 0;
	sampDesc.BorderColor[2] = 0;
	sampDesc.BorderColor[3] = 0;
	sampDesc.MinLOD = 0;
	sampDesc.MaxLOD = D3D11_FLOAT32_MAX;

	// Create the texture sampler state.
	hr = device->CreateSamplerState(&sampDesc, &m_SamplerText);
	if (FAILED(hr))
		return hr;


	return S_OK;
}