1 /**************************************************************************
3 * Copyright 2011 Jose Fonseca
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24 **************************************************************************/
33 #include "d3d11imports.hpp"
34 #include "d3d10state.hpp"
39 #include "DirectXTex.h"
43 * Convert between DXGI formats.
47 ConvertFormat(DXGI_FORMAT SrcFormat,
50 DXGI_FORMAT DstFormat,
53 UINT Width, UINT Height)
57 DirectX::Image SrcImage;
58 DirectX::Image DstImage;
60 SrcImage.width = Width;
61 SrcImage.height = Height;
62 SrcImage.format = SrcFormat;
63 SrcImage.rowPitch = SrcPitch;
64 SrcImage.slicePitch = Height * SrcPitch;
65 SrcImage.pixels = (uint8_t*)SrcData;
67 DstImage.width = Width;
68 DstImage.height = Height;
69 DstImage.format = DstFormat;
70 DstImage.rowPitch = DstPitch;
71 DstImage.slicePitch = Height * DstPitch;
72 DstImage.pixels = (uint8_t*)DstData;
74 DirectX::Rect rect(0, 0, Width, Height);
76 if (SrcFormat != DstFormat) {
77 DirectX::ScratchImage ScratchImage;
78 ScratchImage.Initialize2D(DstFormat, Width, Height, 1, 1);
80 hr = DirectX::Convert(SrcImage, DstFormat, DirectX::TEX_FILTER_DEFAULT, 0.0f, ScratchImage);
82 hr = CopyRectangle(*ScratchImage.GetImage(0, 0, 0), rect, DstImage, DirectX::TEX_FILTER_DEFAULT, 0, 0);
85 hr = CopyRectangle(SrcImage, rect, DstImage, DirectX::TEX_FILTER_DEFAULT, 0, 0);
95 stageResource(ID3D11DeviceContext *pDeviceContext,
96 ID3D11Resource *pResource,
97 ID3D11Resource **ppStagingResource,
98 UINT *pWidth, UINT *pHeight, UINT *pDepth) {
99 D3D11_USAGE Usage = D3D11_USAGE_STAGING;
101 UINT CPUAccessFlags = D3D11_CPU_ACCESS_READ;
104 ID3D11Resource *pStagingResource;
105 ID3D11Buffer *pStagingBuffer;
106 ID3D11Texture1D *pStagingTexture1D;
107 ID3D11Texture2D *pStagingTexture2D;
108 ID3D11Texture3D *pStagingTexture3D;
112 ID3D11Device *pDevice = NULL;
113 pDeviceContext->GetDevice(&pDevice);
115 D3D11_RESOURCE_DIMENSION Type = D3D11_RESOURCE_DIMENSION_UNKNOWN;
116 pResource->GetType(&Type);
118 case D3D11_RESOURCE_DIMENSION_BUFFER:
120 D3D11_BUFFER_DESC Desc;
121 static_cast<ID3D11Buffer *>(pResource)->GetDesc(&Desc);
123 Desc.BindFlags = BindFlags;
124 Desc.CPUAccessFlags = CPUAccessFlags;
125 Desc.MiscFlags = MiscFlags;
127 *pWidth = Desc.ByteWidth;
131 hr = pDevice->CreateBuffer(&Desc, NULL, &pStagingBuffer);
134 case D3D11_RESOURCE_DIMENSION_TEXTURE1D:
136 D3D11_TEXTURE1D_DESC Desc;
137 static_cast<ID3D11Texture1D *>(pResource)->GetDesc(&Desc);
139 Desc.BindFlags = BindFlags;
140 Desc.CPUAccessFlags = CPUAccessFlags;
141 Desc.MiscFlags = MiscFlags;
143 *pWidth = Desc.Width;
147 hr = pDevice->CreateTexture1D(&Desc, NULL, &pStagingTexture1D);
150 case D3D11_RESOURCE_DIMENSION_TEXTURE2D:
152 D3D11_TEXTURE2D_DESC Desc;
153 static_cast<ID3D11Texture2D *>(pResource)->GetDesc(&Desc);
155 Desc.BindFlags = BindFlags;
156 Desc.CPUAccessFlags = CPUAccessFlags;
157 Desc.MiscFlags &= D3D11_RESOURCE_MISC_TEXTURECUBE;
159 *pWidth = Desc.Width;
160 *pHeight = Desc.Height;
163 hr = pDevice->CreateTexture2D(&Desc, NULL, &pStagingTexture2D);
166 case D3D11_RESOURCE_DIMENSION_TEXTURE3D:
168 D3D11_TEXTURE3D_DESC Desc;
169 static_cast<ID3D11Texture3D *>(pResource)->GetDesc(&Desc);
171 Desc.BindFlags = BindFlags;
172 Desc.CPUAccessFlags = CPUAccessFlags;
173 Desc.MiscFlags = MiscFlags;
175 *pWidth = Desc.Width;
176 *pHeight = Desc.Height;
177 *pDepth = Desc.Depth;
179 hr = pDevice->CreateTexture3D(&Desc, NULL, &pStagingTexture3D);
189 *ppStagingResource = pStagingResource;
190 pDeviceContext->CopyResource(pStagingResource, pResource);
199 getRenderTargetViewImage(ID3D11DeviceContext *pDevice,
200 ID3D11RenderTargetView *pRenderTargetView) {
201 image::Image *image = NULL;
202 D3D11_RENDER_TARGET_VIEW_DESC Desc;
203 ID3D11Resource *pResource = NULL;
204 ID3D11Resource *pStagingResource = NULL;
205 UINT Width, Height, Depth;
208 D3D11_MAPPED_SUBRESOURCE MappedSubresource;
211 if (!pRenderTargetView) {
215 pRenderTargetView->GetResource(&pResource);
218 pRenderTargetView->GetDesc(&Desc);
220 hr = stageResource(pDevice, pResource, &pStagingResource, &Width, &Height, &Depth);
225 // TODO: Take the slice in consideration
226 switch (Desc.ViewDimension) {
227 case D3D11_RTV_DIMENSION_BUFFER:
230 case D3D11_RTV_DIMENSION_TEXTURE1D:
231 MipSlice = Desc.Texture1D.MipSlice;
233 case D3D11_RTV_DIMENSION_TEXTURE1DARRAY:
234 MipSlice = Desc.Texture1DArray.MipSlice;
236 case D3D11_RTV_DIMENSION_TEXTURE2D:
237 MipSlice = Desc.Texture2D.MipSlice;
240 case D3D11_RTV_DIMENSION_TEXTURE2DARRAY:
241 MipSlice = Desc.Texture2DArray.MipSlice;
243 case D3D11_RTV_DIMENSION_TEXTURE2DMS:
246 case D3D11_RTV_DIMENSION_TEXTURE2DMSARRAY:
249 case D3D11_RTV_DIMENSION_TEXTURE3D:
250 MipSlice = Desc.Texture3D.MipSlice;
252 case D3D11_SRV_DIMENSION_UNKNOWN:
257 Subresource = MipSlice;
259 Width = std::max(Width >> MipSlice, 1U);
260 Height = std::max(Height >> MipSlice, 1U);
261 Depth = std::max(Depth >> MipSlice, 1U);
263 hr = pDevice->Map(pStagingResource, Subresource, D3D11_MAP_READ, 0, &MappedSubresource);
268 image = new image::Image(Width, Height, 4);
272 assert(image->stride() > 0);
274 hr = ConvertFormat(Desc.Format,
275 MappedSubresource.pData,
276 MappedSubresource.RowPitch,
277 DXGI_FORMAT_R8G8B8A8_UNORM,
287 pDevice->Unmap(pStagingResource, Subresource);
289 if (pStagingResource) {
290 pStagingResource->Release();
294 pResource->Release();
303 getRenderTargetImage(ID3D11DeviceContext *pDevice) {
304 ID3D11RenderTargetView *pRenderTargetView = NULL;
305 pDevice->OMGetRenderTargets(1, &pRenderTargetView, NULL);
307 image::Image *image = NULL;
308 if (pRenderTargetView) {
309 image = getRenderTargetViewImage(pDevice, pRenderTargetView);
310 pRenderTargetView->Release();
318 dumpFramebuffer(JSONWriter &json, ID3D11DeviceContext *pDevice)
320 json.beginMember("framebuffer");
323 ID3D11RenderTargetView *pRenderTargetViews[D3D11_SIMULTANEOUS_RENDER_TARGET_COUNT];
324 pDevice->OMGetRenderTargets(D3D11_SIMULTANEOUS_RENDER_TARGET_COUNT, pRenderTargetViews, NULL);
326 for (UINT i = 0; i < D3D11_SIMULTANEOUS_RENDER_TARGET_COUNT; ++i) {
327 if (!pRenderTargetViews[i]) {
332 image = getRenderTargetViewImage(pDevice, pRenderTargetViews[i]);
335 _snprintf(label, sizeof label, "RENDER_TARGET_%u", i);
336 json.beginMember(label);
337 json.writeImage(image, "UNKNOWN");
338 json.endMember(); // RENDER_TARGET_*
341 pRenderTargetViews[i]->Release();
345 json.endMember(); // framebuffer
349 } /* namespace d3dstate */