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