Beispiel #1
0
// Pipeline initialization
HRESULT displayDevice::InitPipeline(ID3D10Blob *VS, ID3D10Blob *PS, ID3D10Blob *PSt, ID3D10Blob *CS)
{
	HRESULT hr = S_OK;
	// Shaders should already be compiled
	if (&pxShader == nullptr || &vxShader == nullptr || &pxShader2 == nullptr || &cpShader == nullptr) return S_FALSE;

	// encapsulate shaders into shader objects
	dev->CreateVertexShader(VS->GetBufferPointer(), VS->GetBufferSize(), NULL, &vxShader);
	dev->CreatePixelShader(PS->GetBufferPointer(), PS->GetBufferSize(), NULL, &pxShader);
	dev->CreatePixelShader(PSt->GetBufferPointer(), PSt->GetBufferSize(), NULL, &pxShader2);
	dev->CreateComputeShader(CS->GetBufferPointer(), CS->GetBufferSize(), NULL, &cpShader);

	// set the shader objects
	devContext->VSSetShader(vxShader, 0, 0);
	devContext->PSSetShader(pxShader, 0, 0);
	devContext->CSSetShader(cpShader, 0, 0);

	// create the input layout
	D3D11_INPUT_ELEMENT_DESC desc[] =
	{
		{ "POSITION", 0, DXGI_FORMAT_R32G32B32A32_FLOAT, 0, 0, D3D11_INPUT_PER_VERTEX_DATA, 0 },
		{ "COLOR", 0, DXGI_FORMAT_R32G32B32A32_FLOAT, 0, 16, D3D11_INPUT_PER_VERTEX_DATA, 0 },
		{ "TEXCOORD", 0, DXGI_FORMAT_R32G32_FLOAT, 0, 16, D3D11_INPUT_PER_VERTEX_DATA, 0 }
	};
	dev->CreateInputLayout(desc, 3, VS->GetBufferPointer(), VS->GetBufferSize(), &inputLayout);
	devContext->IASetInputLayout(inputLayout);
	
	// Rasterization stage
	D3D11_RASTERIZER_DESC rd =
	{
		D3D11_FILL_SOLID,	// Fill mode
		D3D11_CULL_NONE,	// Backface culling
		TRUE,				// Culling direction (clockwise or not)
		0,					// Depth Bias (?)
		0,					// Depth bias clamp
		0,					// Slope scaled depth bias
		FALSE,				// Depth clip enable
		FALSE,				// Scissor enable (?)
		FALSE,				// Multisample
		FALSE				// Antialiasing
	};
	ID3D11RasterizerState *rs;
	hr = dev->CreateRasterizerState(&rd, &rs); if (FAILED(hr)) return S_FALSE;
	devContext->RSSetState(rs);
	rs->Release();

	// Depth and Stencil initialization
	hr = InitDepthStencil(); if (FAILED(hr)) return hr;

	return S_OK;
}
    GXBOOL RenderTargetImpl::Initialize(GXFormat eColorFormat, GXFormat eDepthStencilFormat)
    {
      GXUINT nWidth = m_nWidth;
      GXUINT nHeight = m_nHeight;
      if(m_nWidth < 0 || m_nHeight < 0)
      {
        GXGRAPHICSDEVICE_DESC sDesc;
        m_pGraphics->GetDesc(&sDesc);
        nWidth = SizeRatioToDimension(m_nWidth, sDesc.BackBufferWidth, 0);
        nHeight = SizeRatioToDimension(m_nHeight, sDesc.BackBufferHeight, 0);
      }

      //
      // 检查
      //
      ID3D11Device* pD3D11Device = m_pGraphics->D3DGetDevice();
      UINT uColorSupport = 0;
      UINT uDepthStencilSupport = 0;
      pD3D11Device->CheckFormatSupport(GrapXToDX11::FormatFrom(eColorFormat), &uColorSupport);
      if(TEST_FLAG_NOT(uColorSupport, D3D11_FORMAT_SUPPORT_RENDER_TARGET)) {
        CLOG_ERROR("RenderTarget: %s 格式不能作为渲染纹理", GrapXToDX11::FormatToString(eColorFormat));
        return FALSE;
      }

      if(eDepthStencilFormat != Format_Unknown) {
        pD3D11Device->CheckFormatSupport(GrapXToDX11::FormatFrom(eDepthStencilFormat), &uDepthStencilSupport);
        if(TEST_FLAG_NOT(uDepthStencilSupport, D3D11_FORMAT_SUPPORT_DEPTH_STENCIL)) {
          CLOG_ERROR("RenderTarget: %s 格式不能作为渲染模板深度缓冲", GrapXToDX11::FormatToString(eDepthStencilFormat));
          return FALSE;
        }
      }


      m_pColorTexture = new TextureImpl_RenderTarget(m_pGraphics, eColorFormat, nWidth, nHeight);
      if(InlIsFailedToNewObject(m_pColorTexture)) {
        return FALSE;
      }

      if(_CL_NOT_(m_pColorTexture->InitRenderTexture(NULL))) {
        SAFE_RELEASE(m_pColorTexture);
        return FALSE;
      }

      if(eDepthStencilFormat != Format_Unknown) {
        return InitDepthStencil(eDepthStencilFormat, nWidth, nHeight);
      }

      return TRUE;
    }
    GXBOOL RenderTargetImpl_BackBuffer::InitializeWithSwapChain(IDXGISwapChain* pD3D11SwapChain)
    {
      ID3D11Device* pD3D11Device = m_pGraphics->D3DGetDevice();
      ID3D11Texture2D* pBackBuffer = NULL;

      HRESULT hval = pD3D11SwapChain->GetBuffer(0, __uuidof(ID3D11Texture2D), (LPVOID*)&pBackBuffer);
      if(FAILED(hval)) {
        return FALSE;
      }

      m_pColorTexture = new TextureImpl_RenderTarget(m_pGraphics, Format_Unknown, 0, 0);
      if(InlIsFailedToNewObject(m_pColorTexture)) {
        return FALSE;
      }

      if(m_pColorTexture->InitRenderTexture(pBackBuffer) == FALSE) {
        return FALSE;
      }
      pBackBuffer->Release();

      GXSIZE sDimension;
      m_pColorTexture->GetDimension(&sDimension);

      // 初始化DepthStencil

      return InitDepthStencil(Format_D24S8, sDimension.cx, sDimension.cy);

      //hval = pD3D11Device->CreateRenderTargetView(pBackBuffer, NULL, &m_pRenderTargetView);
      //m_pDeviceOriginTex->SetTexture(pBackBuffer);
      //if(FAILED(hval))
      //  return hval;
      //// ==========
      //m_pCurRenderTargetView = m_pRenderTargetView;
      //m_pCurRenderTargetView->AddRef();

      //////////////////////////////////////////////////////////////////////////
      // Create depth stencil texture
      //D3D11_TEXTURE2D_DESC descDepth;
      //ZeroMemory(&descDepth, sizeof(descDepth));
      //descDepth.Width = rcWndClient.right;
      //descDepth.Height = rcWndClient.bottom;
      //descDepth.MipLevels = 1;
      //descDepth.ArraySize = 1;
      //descDepth.Format = DXGI_FORMAT_D24_UNORM_S8_UINT;
      //descDepth.SampleDesc.Count = 1;
      //descDepth.SampleDesc.Quality = 0;
      //descDepth.Usage = D3D11_USAGE_DEFAULT;
      //descDepth.BindFlags = D3D11_BIND_DEPTH_STENCIL;
      //descDepth.CPUAccessFlags = 0;
      //descDepth.MiscFlags = 0;
      //hval = m_pd3dDevice->CreateTexture2D(&descDepth, NULL, &m_pDepthStencil);
      //if(FAILED(hval))
      //  return hval;

      //// Create the depth stencil view
      //D3D11_DEPTH_STENCIL_VIEW_DESC descDSV;
      //ZeroMemory(&descDSV, sizeof(descDSV));
      //descDSV.Format = descDepth.Format;
      //descDSV.ViewDimension = D3D11_DSV_DIMENSION_TEXTURE2D;
      //descDSV.Texture2D.MipSlice = 0;
      //hval = m_pd3dDevice->CreateDepthStencilView(m_pDepthStencil, &descDSV, &m_pDepthStencilView);
      //if(FAILED(hval))
      //  return hval;
    }