Guest User

Untitled

a guest
Jun 25th, 2018
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.10 KB | None | 0 0
  1. int const Width = 16;
  2. int const Height = 16;
  3.  
  4.  
  5. /////////////////////
  6. // Create Textures //
  7. /////////////////////
  8.  
  9. ID3D11Texture2D * Texture = nullptr;
  10. ID3D11Texture2D * Staging = nullptr;
  11.  
  12. D3D11_TEXTURE2D_DESC TexDesc;
  13. TexDesc.Width = Width;
  14. TexDesc.Height = Height;
  15. TexDesc.MipLevels = 0;
  16. TexDesc.ArraySize = 1;
  17. TexDesc.Format = DXGI_FORMAT_R32_FLOAT;
  18. TexDesc.SampleDesc.Count = 1;
  19. TexDesc.SampleDesc.Quality = 0;
  20. TexDesc.BindFlags = 0;
  21. TexDesc.MiscFlags = 0;
  22. TexDesc.Usage = D3D11_USAGE_DEFAULT;
  23. TexDesc.CPUAccessFlags = 0;
  24.  
  25. Device->CreateTexture2D(& TexDesc, NULL, & Texture);
  26.  
  27. TexDesc.Usage = D3D11_USAGE_STAGING;
  28. TexDesc.CPUAccessFlags = D3D11_CPU_ACCESS_READ;
  29.  
  30. Device->CreateTexture2D(& TexDesc, NULL, & Staging);
  31.  
  32.  
  33. /////////////////
  34. // Upload Data //
  35. /////////////////
  36.  
  37. FLOAT * Data = new FLOAT[Width * Height]();
  38.  
  39. for (int i = 0; i < Width; ++ i)
  40. {
  41. for (int j = 0; j < Height; ++ j)
  42. {
  43. Data[i + j * Width] = (float) i + (float) j / 1000.f;
  44. }
  45. }
  46.  
  47. UINT const DataSize = sizeof(FLOAT);
  48. UINT const RowPitch = DataSize * Width;
  49. UINT const DepthPitch = DataSize * Width * Height;
  50.  
  51. D3D11_BOX Box;
  52. Box.left = 0;
  53. Box.right = Width;
  54. Box.top = 0;
  55. Box.bottom = Height;
  56. Box.front = 0;
  57. Box.back = 1;
  58.  
  59. ImmediateContext->UpdateSubresource(Texture, 0, & Box, Data, RowPitch, DepthPitch);
  60. delete[] Data;
  61.  
  62.  
  63. ///////////////////
  64. // Download Data //
  65. ///////////////////
  66.  
  67. ImmediateContext->CopyResource(Staging, Texture);
  68.  
  69. FLOAT * Read = new FLOAT[Width * Height]();
  70.  
  71. for (int i = 0; i < Width; ++ i)
  72. {
  73. for (int j = 0; j < Height; ++ j)
  74. {
  75. Read[i + j * Width] = -1.f;
  76. }
  77. }
  78.  
  79.  
  80. D3D11_MAPPED_SUBRESOURCE ResourceDesc = {};
  81. ImmediateContext->Map(Staging, 0, D3D11_MAP_READ, 0, & ResourceDesc);
  82.  
  83. if (ResourceDesc.pData)
  84. {
  85. int const BytesPerPixel = sizeof(FLOAT);
  86. for (int i = 0; i < Height; ++ i)
  87. {
  88. std::memcpy(
  89. (byte *) Read + Width * BytesPerPixel * i,
  90. (byte *) ResourceDesc.pData + ResourceDesc.RowPitch * i,
  91. Width * BytesPerPixel);
  92. }
  93. }
  94.  
  95. ImmediateContext->Unmap(Staging, 0);
  96.  
  97. for (int i = 0; i < Width; ++ i)
  98. {
  99. for (int j = 0; j < Height; ++ j)
  100. {
  101. printf("[%d, %d] = { %.6f }\n", i, j, Read[i + j * Width]);
  102. }
  103. }
  104.  
  105. delete[] Read;
Add Comment
Please, Sign In to add comment