Advertisement
expired6978

D2D Texture Render

Aug 31st, 2017
213
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.78 KB | None | 0 0
  1. bool TextureClass::Initialize(ID3D11Device* device, WCHAR* filename)
  2. {
  3.     HRESULT result;
  4.  
  5.  
  6.     // Load the texture in.
  7.     TexMetadata md;
  8.     ScratchImage si;
  9.  
  10.     result = LoadFromDDSFile(filename, 0, &md, si);
  11.     if (FAILED(result))
  12.     {
  13.         return false;
  14.     }
  15.  
  16.     result = CreateShaderResourceViewEx(device, si.GetImages(), si.GetImageCount(), _In_ md, D3D11_USAGE_DEFAULT, D3D11_BIND_SHADER_RESOURCE | D3D11_BIND_RENDER_TARGET, 0, 0, false, &m_texture);
  17.  
  18.     ID3D11Resource * pResource = nullptr;
  19.     m_texture->GetResource(&pResource);
  20.  
  21.     ID3D11Texture2D * pTexture = nullptr;
  22.     result = pResource->QueryInterface(&pTexture);
  23.     if (FAILED(result))
  24.     {
  25.         return false;
  26.     }
  27.  
  28.     IDXGISurface *pDxgiSurface = NULL;
  29.     result = pTexture->QueryInterface(&pDxgiSurface);
  30.     if (FAILED(result))
  31.     {
  32.         return false;
  33.     }
  34.  
  35.     ID2D1Factory1 * pD2DFactory = nullptr;
  36.     result = D2D1CreateFactory(D2D1_FACTORY_TYPE::D2D1_FACTORY_TYPE_MULTI_THREADED, &pD2DFactory);
  37.     if (FAILED(result))
  38.     {
  39.         return false;
  40.     }
  41.  
  42.     D3D11_TEXTURE2D_DESC desc;
  43.     pTexture->GetDesc(&desc);
  44.  
  45.     ID2D1RenderTarget * pRenderTarget = nullptr;
  46.  
  47.     // Create a D2D render target which can draw into our offscreen D3D
  48.     // surface. Given that we use a constant size for the texture, we
  49.     // fix the DPI at 96.
  50.     D2D1_RENDER_TARGET_PROPERTIES props =
  51.         D2D1::RenderTargetProperties(
  52.             D2D1_RENDER_TARGET_TYPE_HARDWARE,
  53.             D2D1::PixelFormat(DXGI_FORMAT_UNKNOWN, D2D1_ALPHA_MODE_PREMULTIPLIED),
  54.             96,
  55.             96
  56.         );
  57.  
  58.     result = pD2DFactory->CreateDxgiSurfaceRenderTarget(
  59.         pDxgiSurface,
  60.         &props,
  61.         &pRenderTarget
  62.     );
  63.     if (FAILED(result))
  64.     {
  65.         return false;
  66.     }
  67.  
  68.     ID2D1DeviceContext * pD2DContext = nullptr;
  69.     result = pRenderTarget->QueryInterface(&pD2DContext);
  70.     if (FAILED(result))
  71.     {
  72.         return false;
  73.     }
  74.  
  75.     ID2D1Bitmap * pBitmap = nullptr;
  76.     D2D1_BITMAP_PROPERTIES bitmapProps;
  77.  
  78.     bitmapProps.pixelFormat.format = desc.Format;
  79.     bitmapProps.pixelFormat.alphaMode = D2D1_ALPHA_MODE_PREMULTIPLIED;
  80.     bitmapProps.dpiX = 96;
  81.     bitmapProps.dpiY = 96;
  82.     result = pD2DContext->CreateSharedBitmap(__uuidof(IDXGISurface), pDxgiSurface, &bitmapProps, &pBitmap);
  83.     if (FAILED(result))
  84.     {
  85.         return false;
  86.     }
  87.  
  88.     /*ID2D1Effect * colorMatrixEffect = nullptr;
  89.     result = pD2DContext->CreateEffect(CLSID_D2D1ColorMatrix, &colorMatrixEffect);
  90.     if (FAILED(result))
  91.     {
  92.         return false;
  93.     }
  94.  
  95.     colorMatrixEffect->SetInput(0, pBitmap);
  96.  
  97.     D2D1_MATRIX_5X4_F matrix = D2D1::Matrix5x4F(
  98.         0, 0, 1, 0,
  99.         0, 1, 0, 0,
  100.         1, 0, 1, 0,
  101.         0, 0, 0, 1,
  102.         0, 1, 0, 0
  103.     );
  104.     result = colorMatrixEffect->SetValue(D2D1_COLORMATRIX_PROP_COLOR_MATRIX, matrix);
  105.  
  106.     if (FAILED(result))
  107.     {
  108.         return false;
  109.     }*/
  110.  
  111.     /*ID2D1Effect * gaussianBlurEffect;
  112.     pD2DContext->CreateEffect(CLSID_D2D1GaussianBlur, &gaussianBlurEffect);
  113.  
  114.     gaussianBlurEffect->SetInput(0, pBitmap);
  115.     gaussianBlurEffect->SetValue(D2D1_GAUSSIANBLUR_PROP_STANDARD_DEVIATION, 3.0f);*/
  116.  
  117.     ID2D1Effect * directionalBlurEffect;
  118.     pD2DContext->CreateEffect(CLSID_D2D1DirectionalBlur, &directionalBlurEffect);
  119.  
  120.     directionalBlurEffect->SetInput(0, pBitmap);
  121.     directionalBlurEffect->SetValue(D2D1_DIRECTIONALBLUR_PROP_STANDARD_DEVIATION, 7.0f);
  122.  
  123.     /*ID2D1Effect * convolveMatrixEffect;
  124.     pD2DContext->CreateEffect(CLSID_D2D1ConvolveMatrix, &convolveMatrixEffect);
  125.  
  126.     convolveMatrixEffect->SetInput(0, pBitmap);
  127.     float matrix[9] = { -1, 8, -1, 2, 23, 2, -1, 8, -1 };
  128.     convolveMatrixEffect->SetValue(D2D1_CONVOLVEMATRIX_PROP_KERNEL_MATRIX, matrix);*/
  129.  
  130.     /*ID2D1Effect * saturationEffect;
  131.     pD2DContext->CreateEffect(CLSID_D2D1Saturation, &saturationEffect);
  132.  
  133.     saturationEffect->SetInput(0, pBitmap);
  134.  
  135.     saturationEffect->SetValue(D2D1_SATURATION_PROP_SATURATION, 0.0f);*/
  136.  
  137.     pD2DContext->BeginDraw();
  138.     pD2DContext->DrawImage(directionalBlurEffect);
  139.     pD2DContext->EndDraw();
  140.  
  141.     if(FAILED(result))
  142.     {
  143.         return false;
  144.     }
  145.  
  146.     return true;
  147. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement