screenshotD3D11.cpp 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2016 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #include "platform/platform.h"
  23. #include "gfx/D3D11/screenshotD3D11.h"
  24. #include "gfx/D3D11/gfxD3D11Device.h"
  25. //Note if MSAA is ever enabled this will need fixing
  26. GBitmap* ScreenShotD3D11::_captureBackBuffer()
  27. {
  28. ID3D11Texture2D* backBuf = D3D11->getBackBufferTexture();
  29. D3D11_TEXTURE2D_DESC desc;
  30. backBuf->GetDesc(&desc);
  31. desc.BindFlags = 0;
  32. desc.CPUAccessFlags = D3D11_CPU_ACCESS_READ | D3D11_CPU_ACCESS_WRITE;
  33. desc.Usage = D3D11_USAGE_STAGING;
  34. //create temp texure
  35. ID3D11Texture2D* pNewTexture = NULL;
  36. HRESULT hr = D3D11DEVICE->CreateTexture2D(&desc, NULL, &pNewTexture);
  37. if (FAILED(hr))
  38. return NULL;
  39. U32 width = desc.Width;
  40. U32 height = desc.Height;
  41. // pixel data
  42. U8 *pData = new U8[width * height * 4];
  43. D3D11DEVICECONTEXT->CopyResource(pNewTexture, backBuf);
  44. D3D11_MAPPED_SUBRESOURCE Resource;
  45. hr = D3D11DEVICECONTEXT->Map(pNewTexture, 0, D3D11_MAP_READ, 0, &Resource);
  46. if (FAILED(hr))
  47. {
  48. //cleanup
  49. SAFE_DELETE_ARRAY(pData);
  50. SAFE_RELEASE(pNewTexture);
  51. return NULL;
  52. }
  53. const U32 pitch = width << 2;
  54. const U8* pSource = (U8*)Resource.pData;
  55. U32 totalPitch = 0;
  56. for (U32 i = 0; i < height; ++i)
  57. {
  58. dMemcpy(pData, pSource, width * 4);
  59. pSource += Resource.RowPitch;
  60. pData += pitch;
  61. totalPitch += pitch;
  62. }
  63. D3D11DEVICECONTEXT->Unmap(pNewTexture, 0);
  64. pData -= totalPitch;
  65. GBitmap *gb = new GBitmap(width, height);
  66. //Set GBitmap data and convert from bgr to rgb
  67. ColorI c;
  68. for (S32 i = 0; i<height; i++)
  69. {
  70. const U8 *a = pData + i * width * 4;
  71. for (S32 j = 0; j<width; j++)
  72. {
  73. c.blue = *(a++);
  74. c.green = *(a++);
  75. c.red = *(a++);
  76. a++; // Ignore alpha.
  77. gb->setColor(j, i, c);
  78. }
  79. }
  80. //cleanup
  81. SAFE_DELETE_ARRAY(pData);
  82. SAFE_RELEASE(pNewTexture);
  83. return gb;
  84. }