Exemple #1
0
bool InitResourceDX10(void)
{
	g_pDevice = GutGetGraphicsDeviceDX10();
	ID3D10Blob *pVSCode = NULL;

	// 載入Vertex Shader
	g_pVertexShader = GutLoadVertexShaderDX10_HLSL("../../shaders/texture_alphatest_dx10.hlsl", "VS", "vs_4_0", &pVSCode);
	if ( NULL==g_pVertexShader )
		return false;
	// 載入Pixel Shader
	g_pPixelShader = GutLoadPixelShaderDX10_HLSL("../../shaders/texture_alphatest_dx10.hlsl", "PS", "ps_4_0");
	if ( NULL==g_pPixelShader )
		return false;

	g_pTexture0 = GutLoadTexture_DX10("../../textures/brickwall_broken.tga");
	g_pTexture1 = GutLoadTexture_DX10("../../textures/spotlight_effect.tga");
	if ( g_pTexture0==NULL || g_pTexture1==NULL )
		return false;

	// 設定Vertex資料格式
	D3D10_INPUT_ELEMENT_DESC layout[] =
	{
		{ "POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0,  D3D10_INPUT_PER_VERTEX_DATA, 0 },
		{ "TEXCOORD", 0, DXGI_FORMAT_R32G32_FLOAT, 0, 12, D3D10_INPUT_PER_VERTEX_DATA, 0 }
	};

	if ( D3D_OK != g_pDevice->CreateInputLayout( layout, sizeof(layout)/sizeof(D3D10_INPUT_ELEMENT_DESC), pVSCode->GetBufferPointer(), pVSCode->GetBufferSize(), &g_pVertexLayout ) )
		return false;

	SAFE_RELEASE(pVSCode);

	D3D10_BUFFER_DESC cbDesc;

	// vertex buffer
	{
		cbDesc.ByteWidth = sizeof(Vertex_VT) * 4;
		cbDesc.Usage = D3D10_USAGE_DYNAMIC ;
		cbDesc.BindFlags = D3D10_BIND_VERTEX_BUFFER;
		cbDesc.CPUAccessFlags = D3D10_CPU_ACCESS_WRITE;
		cbDesc.MiscFlags = 0;

		D3D10_SUBRESOURCE_DATA subDesc;
		ZeroMemory(&subDesc, sizeof(subDesc));
		subDesc.pSysMem = g_Quad;
		// 配置一塊可以存放Vertex的記憶體, 也就是Vertex Buffer.
		if ( D3D_OK != g_pDevice->CreateBuffer( &cbDesc, &subDesc, &g_pVertexBuffer ) )
			return false;
	}

	// 配置Shader參數的記憶體空間
	{
		cbDesc.ByteWidth = sizeof(Matrix4x4);
		cbDesc.Usage = D3D10_USAGE_DYNAMIC;
		cbDesc.BindFlags = D3D10_BIND_CONSTANT_BUFFER;
		cbDesc.CPUAccessFlags = D3D10_CPU_ACCESS_WRITE;
		cbDesc.MiscFlags = 0;
		if ( D3D_OK != g_pDevice->CreateBuffer( &cbDesc, NULL, &g_pConstantBuffer ) )
			return false;
	}

	// rasterizer state物件
	{
		D3D10_RASTERIZER_DESC desc;
		GutSetDX10DefaultRasterizerDesc(desc);

		desc.CullMode = D3D10_CULL_NONE;
		desc.FrontCounterClockwise = true;

		if ( D3D_OK != g_pDevice->CreateRasterizerState(&desc, &g_pRasterizerState) )
			return false;

		g_pDevice->RSSetState(g_pRasterizerState);
	}

	// depth stencil state
	{
		D3D10_DEPTH_STENCIL_DESC desc;
		GutSetDX10DefaultDepthStencilDesc(desc);

		desc.DepthEnable = FALSE;
		desc.DepthWriteMask = D3D10_DEPTH_WRITE_MASK_ZERO;
		desc.DepthFunc = D3D10_COMPARISON_ALWAYS;

		desc.StencilEnable = TRUE;
		desc.StencilReadMask = 0xff;
		desc.StencilWriteMask = 0xff;

		desc.FrontFace.StencilFailOp = D3D10_STENCIL_OP_KEEP;
		desc.FrontFace.StencilDepthFailOp = D3D10_STENCIL_OP_KEEP;
		desc.FrontFace.StencilPassOp = D3D10_STENCIL_OP_REPLACE;
		desc.FrontFace.StencilFunc = D3D10_COMPARISON_ALWAYS;

		desc.BackFace = desc.FrontFace;
		// 這個設定用來設定像素的stencil值
		g_pDevice->CreateDepthStencilState(&desc, &g_pStencilState_Replace);

		desc.FrontFace.StencilFunc = D3D10_COMPARISON_EQUAL;
		desc.BackFace = desc.FrontFace;
		// 這個設定用來做stencil test測試
		g_pDevice->CreateDepthStencilState(&desc, &g_pStencilState_Test);
	}

	// blend state
	{
		D3D10_BLEND_DESC desc;
		GutSetDX10DefaultBlendDesc(desc);

		desc.BlendEnable[0] = TRUE;

		desc.SrcBlend = D3D10_BLEND_ONE;
		desc.DestBlend = D3D10_BLEND_ONE;
		desc.BlendOp = D3D10_BLEND_OP_ADD;

		desc.SrcBlendAlpha = D3D10_BLEND_ONE;
		desc.DestBlendAlpha = D3D10_BLEND_ONE;
		desc.BlendOpAlpha = D3D10_BLEND_OP_ADD;

		g_pDevice->CreateBlendState(&desc, &g_pBlendState);
	}

	// sampler state / texture filter
	{
		D3D10_SAMPLER_DESC desc;
		GutSetDX10DefaultSamplerDesc(desc);

		desc.Filter = D3D10_FILTER_MIN_MAG_MIP_LINEAR;
		desc.AddressU = D3D10_TEXTURE_ADDRESS_WRAP;
		desc.AddressV = D3D10_TEXTURE_ADDRESS_WRAP;
		desc.AddressW = D3D10_TEXTURE_ADDRESS_WRAP;

		g_pDevice->CreateSamplerState(&desc, &g_pSamplerState);

		for ( int i=0; i<4; i++ )
		{
			g_pDevice->PSSetSamplers(i, 1, &g_pSamplerState);
		}
	}

	// 計算投影矩陣
	g_view_matrix = GutMatrixLookAtRH(g_eye, g_lookat, g_up);
	g_proj_matrix = GutMatrixOrthoRH_DirectX(g_fOrthoWidth, g_fOrthoHeight, 0.1f, 100.0f);

	return true;
}
Exemple #2
0
bool InitResourceDX10(void)
{
	g_pDevice = GutGetGraphicsDeviceDX10();

	CGutModel_DX10::LoadDefaultShader("../../shaders/gmodel_dx10.hlsl");
	CGutModel::SetTexturePath("../../textures/");

	g_Model_DX10.ConvertToDX10Model(&g_Model);
	g_SpotLightModel_DX10.ConvertToDX10Model(&g_SpotLightModel);

	// depth stencil object
	{
		D3D10_DEPTH_STENCIL_DESC desc;
		GutSetDX10DefaultDepthStencilDesc(desc);

		desc.StencilEnable = TRUE;
		desc.DepthWriteMask = D3D10_DEPTH_WRITE_MASK_ZERO;
		desc.FrontFace.StencilFunc = D3D10_COMPARISON_ALWAYS;
		desc.FrontFace.StencilPassOp = D3D10_STENCIL_OP_INCR;
		desc.BackFace.StencilFunc = D3D10_COMPARISON_ALWAYS;
		desc.BackFace.StencilPassOp = D3D10_STENCIL_OP_DECR;

		g_pDevice->CreateDepthStencilState(&desc, &g_pZStencil_Mask);

		desc.FrontFace.StencilFunc = D3D10_COMPARISON_EQUAL;
		desc.FrontFace.StencilPassOp = D3D10_STENCIL_OP_KEEP;
		desc.BackFace = desc.FrontFace;

		g_pDevice->CreateDepthStencilState(&desc, &g_pZStencil_Test);
	}
	// disable rgba output
	{
		D3D10_BLEND_DESC desc;
		GutSetDX10DefaultBlendDesc(desc);

		desc.RenderTargetWriteMask[0] = 0;
		g_pDevice->CreateBlendState(&desc, &g_pBlendState_ColorWriteOff);
	}
	// material overwrite
	{
		g_material_stencilpass.m_bCullFace = false;
		g_material_stencilpass.m_pBlendState = g_pBlendState_ColorWriteOff;
		g_material_stencilpass.m_vDiffuse.Set(0.1f);

		g_material_spotlightpass.m_bCullFace = true;
		g_material_spotlightpass.m_pBlendState = CGutModel_DX10::s_pBlend_Add;
		g_material_spotlightpass.m_vDiffuse.Set(0.3f);
	}
	// sampler state / texture filter
	{
		D3D10_SAMPLER_DESC sampler_desc;
		ZeroMemory(&sampler_desc, sizeof(sampler_desc));

		sampler_desc.Filter = D3D10_FILTER_MIN_MAG_MIP_LINEAR;
		sampler_desc.AddressU = D3D10_TEXTURE_ADDRESS_WRAP;
		sampler_desc.AddressV = D3D10_TEXTURE_ADDRESS_WRAP;
		sampler_desc.AddressW = D3D10_TEXTURE_ADDRESS_WRAP;

		g_pDevice->CreateSamplerState(&sampler_desc, &g_pSamplerState);

		for ( int i=0; i<4; i++ )
		{
			g_pDevice->PSSetSamplers(i, 1, &g_pSamplerState);
		}
	}
	// 投影矩陣
	g_proj_matrix = GutMatrixPerspectiveRH_DirectX(g_fFOV, 1.0f, 0.1f, 100.0f);

	return true;
}
Exemple #3
0
bool InitResourceDX10(void)
{
	g_pDevice = GutGetGraphicsDeviceDX10();
	ID3D10Blob *pVSCode = NULL;

	g_pTexture = GutLoadTexture_DX10("../../textures/space.tga", &g_ImageInfo);
	if ( NULL==g_pTexture )
		return false;

	// 載入Vertex Shader
	{
		g_pBlurVS = GutLoadVertexShaderDX10_HLSL("../../shaders/Posteffect_blur_dx10.hlsl", "VS", "vs_4_0", &pVSCode);
		g_pBlurPS = GutLoadPixelShaderDX10_HLSL("../../shaders/Posteffect_blur_dx10.hlsl", "PS", "ps_4_0");
		g_pBrightnessPS = GutLoadPixelShaderDX10_HLSL("../../shaders/Posteffect_brightness_dx10.hlsl", "PS", "ps_4_0");
		g_pRGBAPixelShader = GutLoadPixelShaderDX10_HLSL("../../shaders/texture_dx10.hlsl", "PS", "ps_4_0");

		if ( NULL==g_pBlurVS || NULL==g_pBlurPS ||
			NULL==g_pRGBAPixelShader || NULL==g_pBrightnessPS )
			return false;
	}

	// 設定Vertex資料格式
	{
		D3D10_INPUT_ELEMENT_DESC layout[] =
		{
			{ "POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0,  D3D10_INPUT_PER_VERTEX_DATA, 0 },
			{ "TEXCOORD", 0, DXGI_FORMAT_R32G32_FLOAT, 0, 12, D3D10_INPUT_PER_VERTEX_DATA, 0 }
		};

		if ( D3D_OK != g_pDevice->CreateInputLayout( layout, 2, pVSCode->GetBufferPointer(), pVSCode->GetBufferSize(), &g_pVertexLayout ) )
			return false;

		SAFE_RELEASE(pVSCode);
	}

	g_pVertexBuffer = GutCreateVertexBuffer_DX10(sizeof(Vertex_VT)*4, g_FullScreenQuad);

	Matrix4x4 ident_mat; ident_mat.Identity();

	g_pVSConstantBuffer = GutCreateShaderConstant_DX10(sizeof(Matrix4x4), &ident_mat);
	g_pBlurConstantBuffer = GutCreateShaderConstant_DX10(sizeof(Vector4)*9);
	g_pBrightnessConstantBuffer = GutCreateShaderConstant_DX10(sizeof(Vector4)*2);

	int w = g_ImageInfo.m_iWidth/4;
	int h = g_ImageInfo.m_iHeight/4;

	if ( !GutCreateRenderTarget_DX10(w, h, 
		DXGI_FORMAT_R8G8B8A8_UNORM, &g_pFrameTexture[0], &g_pFrameSRView[0], &g_pFrameRTView[0]) )
		return false;

	if ( !GutCreateRenderTarget_DX10(w, h,
		DXGI_FORMAT_R8G8B8A8_UNORM, &g_pFrameTexture[1], &g_pFrameSRView[1], &g_pFrameRTView[1]) )
		return false;

	{
		D3D10_RASTERIZER_DESC desc;
		GutSetDX10DefaultRasterizerDesc(desc);
		desc.CullMode = D3D10_CULL_NONE;

		g_pDevice->CreateRasterizerState(&desc, &g_pNoCull);
		g_pDevice->RSSetState(g_pNoCull);
	}

	{
		D3D10_BLEND_DESC desc;
		GutSetDX10DefaultBlendDesc(desc);

		g_pDevice->CreateBlendState(&desc, &g_pNoBlend);

		desc.BlendEnable[0] = TRUE;
		desc.SrcBlend = D3D10_BLEND_ONE;
		desc.DestBlend = D3D10_BLEND_ONE;

		g_pDevice->CreateBlendState(&desc, &g_pAddBlend);
	}

	{
		D3D10_DEPTH_STENCIL_DESC desc;
		GutSetDX10DefaultDepthStencilDesc(desc);

		desc.DepthWriteMask = D3D10_DEPTH_WRITE_MASK_ZERO;
		desc.DepthEnable = FALSE;

		g_pDevice->CreateDepthStencilState(&desc, &g_pNoZWrite);
		g_pDevice->OMSetDepthStencilState(g_pNoZWrite, 0);
	}

	return true;
}