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 **************************************************************************/
34 #include "d3d11imports.hpp"
35 #include "d3d10state.hpp"
36 #include "dxgistate.hpp"
42 stageResource(ID3D11DeviceContext *pDeviceContext,
43 ID3D11Resource *pResource,
44 ID3D11Resource **ppStagingResource,
45 UINT *pWidth, UINT *pHeight, UINT *pDepth) {
46 D3D11_USAGE Usage = D3D11_USAGE_STAGING;
48 UINT CPUAccessFlags = D3D11_CPU_ACCESS_READ;
51 ID3D11Resource *pStagingResource;
52 ID3D11Buffer *pStagingBuffer;
53 ID3D11Texture1D *pStagingTexture1D;
54 ID3D11Texture2D *pStagingTexture2D;
55 ID3D11Texture3D *pStagingTexture3D;
59 ID3D11Device *pDevice = NULL;
60 pDeviceContext->GetDevice(&pDevice);
62 D3D11_RESOURCE_DIMENSION Type = D3D11_RESOURCE_DIMENSION_UNKNOWN;
63 pResource->GetType(&Type);
65 case D3D11_RESOURCE_DIMENSION_BUFFER:
67 D3D11_BUFFER_DESC Desc;
68 static_cast<ID3D11Buffer *>(pResource)->GetDesc(&Desc);
70 Desc.BindFlags = BindFlags;
71 Desc.CPUAccessFlags = CPUAccessFlags;
72 Desc.MiscFlags = MiscFlags;
74 *pWidth = Desc.ByteWidth;
78 hr = pDevice->CreateBuffer(&Desc, NULL, &pStagingBuffer);
81 case D3D11_RESOURCE_DIMENSION_TEXTURE1D:
83 D3D11_TEXTURE1D_DESC Desc;
84 static_cast<ID3D11Texture1D *>(pResource)->GetDesc(&Desc);
86 Desc.BindFlags = BindFlags;
87 Desc.CPUAccessFlags = CPUAccessFlags;
88 Desc.MiscFlags = MiscFlags;
94 hr = pDevice->CreateTexture1D(&Desc, NULL, &pStagingTexture1D);
97 case D3D11_RESOURCE_DIMENSION_TEXTURE2D:
99 D3D11_TEXTURE2D_DESC Desc;
100 static_cast<ID3D11Texture2D *>(pResource)->GetDesc(&Desc);
102 Desc.BindFlags = BindFlags;
103 Desc.CPUAccessFlags = CPUAccessFlags;
104 Desc.MiscFlags &= D3D11_RESOURCE_MISC_TEXTURECUBE;
106 *pWidth = Desc.Width;
107 *pHeight = Desc.Height;
110 hr = pDevice->CreateTexture2D(&Desc, NULL, &pStagingTexture2D);
113 case D3D11_RESOURCE_DIMENSION_TEXTURE3D:
115 D3D11_TEXTURE3D_DESC Desc;
116 static_cast<ID3D11Texture3D *>(pResource)->GetDesc(&Desc);
118 Desc.BindFlags = BindFlags;
119 Desc.CPUAccessFlags = CPUAccessFlags;
120 Desc.MiscFlags = MiscFlags;
122 *pWidth = Desc.Width;
123 *pHeight = Desc.Height;
124 *pDepth = Desc.Depth;
126 hr = pDevice->CreateTexture3D(&Desc, NULL, &pStagingTexture3D);
136 *ppStagingResource = pStagingResource;
137 pDeviceContext->CopyResource(pStagingResource, pResource);
145 static image::Image *
146 getSubResourceImage(ID3D11DeviceContext *pDevice,
147 ID3D11Resource *pResource,
151 image::Image *image = NULL;
152 ID3D11Resource *pStagingResource = NULL;
153 UINT Width, Height, Depth;
154 UINT SubResource = MipSlice;
155 D3D11_MAPPED_SUBRESOURCE MappedSubResource;
162 hr = stageResource(pDevice, pResource, &pStagingResource, &Width, &Height, &Depth);
167 Width = std::max(Width >> MipSlice, 1U);
168 Height = std::max(Height >> MipSlice, 1U);
169 Depth = std::max(Depth >> MipSlice, 1U);
171 hr = pDevice->Map(pStagingResource, SubResource, D3D11_MAP_READ, 0, &MappedSubResource);
176 image = ConvertImage(Format,
177 MappedSubResource.pData,
178 MappedSubResource.RowPitch,
181 pDevice->Unmap(pStagingResource, SubResource);
183 if (pStagingResource) {
184 pStagingResource->Release();
188 pResource->Release();
194 static image::Image *
195 getShaderResourceViewImage(ID3D11DeviceContext *pDevice,
196 ID3D11ShaderResourceView *pShaderResourceView) {
197 D3D11_SHADER_RESOURCE_VIEW_DESC Desc;
198 ID3D11Resource *pResource = NULL;
201 if (!pShaderResourceView) {
205 pShaderResourceView->GetResource(&pResource);
208 pShaderResourceView->GetDesc(&Desc);
210 // TODO: Take the slice in consideration
211 switch (Desc.ViewDimension) {
212 case D3D11_SRV_DIMENSION_BUFFER:
215 case D3D11_SRV_DIMENSION_TEXTURE1D:
216 MipSlice = Desc.Texture1D.MostDetailedMip;
218 case D3D11_SRV_DIMENSION_TEXTURE1DARRAY:
219 MipSlice = Desc.Texture1DArray.MostDetailedMip;
221 case D3D11_SRV_DIMENSION_TEXTURE2D:
222 MipSlice = Desc.Texture2D.MostDetailedMip;
224 case D3D11_SRV_DIMENSION_TEXTURE2DARRAY:
225 MipSlice = Desc.Texture2DArray.MostDetailedMip;
227 case D3D11_SRV_DIMENSION_TEXTURE2DMS:
230 case D3D11_SRV_DIMENSION_TEXTURE2DMSARRAY:
233 case D3D11_SRV_DIMENSION_TEXTURE3D:
234 MipSlice = Desc.Texture3D.MostDetailedMip;
236 case D3D11_SRV_DIMENSION_TEXTURECUBE:
237 MipSlice = Desc.TextureCube.MostDetailedMip;
239 case D3D11_SRV_DIMENSION_UNKNOWN:
245 return getSubResourceImage(pDevice, pResource, Desc.Format, MipSlice);
249 static image::Image *
250 getRenderTargetViewImage(ID3D11DeviceContext *pDevice,
251 ID3D11RenderTargetView *pRenderTargetView) {
252 D3D11_RENDER_TARGET_VIEW_DESC Desc;
253 ID3D11Resource *pResource = NULL;
256 if (!pRenderTargetView) {
260 pRenderTargetView->GetResource(&pResource);
263 pRenderTargetView->GetDesc(&Desc);
265 // TODO: Take the slice in consideration
266 switch (Desc.ViewDimension) {
267 case D3D11_RTV_DIMENSION_BUFFER:
270 case D3D11_RTV_DIMENSION_TEXTURE1D:
271 MipSlice = Desc.Texture1D.MipSlice;
273 case D3D11_RTV_DIMENSION_TEXTURE1DARRAY:
274 MipSlice = Desc.Texture1DArray.MipSlice;
276 case D3D11_RTV_DIMENSION_TEXTURE2D:
277 MipSlice = Desc.Texture2D.MipSlice;
280 case D3D11_RTV_DIMENSION_TEXTURE2DARRAY:
281 MipSlice = Desc.Texture2DArray.MipSlice;
283 case D3D11_RTV_DIMENSION_TEXTURE2DMS:
286 case D3D11_RTV_DIMENSION_TEXTURE2DMSARRAY:
289 case D3D11_RTV_DIMENSION_TEXTURE3D:
290 MipSlice = Desc.Texture3D.MipSlice;
292 case D3D11_RTV_DIMENSION_UNKNOWN:
298 return getSubResourceImage(pDevice, pResource, Desc.Format, MipSlice);
302 static image::Image *
303 getDepthStencilViewImage(ID3D11DeviceContext *pDevice,
304 ID3D11DepthStencilView *pDepthStencilView) {
305 D3D11_DEPTH_STENCIL_VIEW_DESC Desc;
306 ID3D11Resource *pResource = NULL;
309 if (!pDepthStencilView) {
313 pDepthStencilView->GetResource(&pResource);
316 pDepthStencilView->GetDesc(&Desc);
318 // TODO: Take the slice in consideration
319 switch (Desc.ViewDimension) {
320 case D3D11_DSV_DIMENSION_TEXTURE1D:
321 MipSlice = Desc.Texture1D.MipSlice;
323 case D3D11_DSV_DIMENSION_TEXTURE1DARRAY:
324 MipSlice = Desc.Texture1DArray.MipSlice;
326 case D3D11_DSV_DIMENSION_TEXTURE2D:
327 MipSlice = Desc.Texture2D.MipSlice;
329 case D3D11_DSV_DIMENSION_TEXTURE2DARRAY:
330 MipSlice = Desc.Texture2DArray.MipSlice;
332 case D3D11_DSV_DIMENSION_TEXTURE2DMS:
335 case D3D11_DSV_DIMENSION_TEXTURE2DMSARRAY:
338 case D3D11_DSV_DIMENSION_UNKNOWN:
344 return getSubResourceImage(pDevice, pResource, Desc.Format, MipSlice);
349 dumpTextures(JSONWriter &json, ID3D11DeviceContext *pDevice)
351 json.beginMember("textures");
354 ID3D11ShaderResourceView *pShaderResourceViews[D3D11_COMMONSHADER_SAMPLER_SLOT_COUNT];
355 pDevice->PSGetShaderResources(0, ARRAYSIZE(pShaderResourceViews), pShaderResourceViews);
357 for (UINT i = 0; i < ARRAYSIZE(pShaderResourceViews); ++i) {
358 if (!pShaderResourceViews[i]) {
363 image = getShaderResourceViewImage(pDevice, pShaderResourceViews[i]);
366 _snprintf(label, sizeof label, "PS_RESOURCE_%u", i);
367 json.beginMember(label);
368 json.writeImage(image, "UNKNOWN");
369 json.endMember(); // PS_RESOURCE_*
372 pShaderResourceViews[i]->Release();
376 json.endMember(); // textures
381 getRenderTargetImage(ID3D11DeviceContext *pDevice) {
382 ID3D11RenderTargetView *pRenderTargetView = NULL;
383 pDevice->OMGetRenderTargets(1, &pRenderTargetView, NULL);
385 image::Image *image = NULL;
386 if (pRenderTargetView) {
387 image = getRenderTargetViewImage(pDevice, pRenderTargetView);
388 pRenderTargetView->Release();
396 dumpFramebuffer(JSONWriter &json, ID3D11DeviceContext *pDevice)
398 json.beginMember("framebuffer");
401 ID3D11RenderTargetView *pRenderTargetViews[D3D11_SIMULTANEOUS_RENDER_TARGET_COUNT];
402 ID3D11DepthStencilView *pDepthStencilView;
403 pDevice->OMGetRenderTargets(ARRAYSIZE(pRenderTargetViews), pRenderTargetViews,
406 for (UINT i = 0; i < ARRAYSIZE(pRenderTargetViews); ++i) {
407 if (!pRenderTargetViews[i]) {
412 image = getRenderTargetViewImage(pDevice, pRenderTargetViews[i]);
415 _snprintf(label, sizeof label, "RENDER_TARGET_%u", i);
416 json.beginMember(label);
417 json.writeImage(image, "UNKNOWN");
418 json.endMember(); // RENDER_TARGET_*
421 pRenderTargetViews[i]->Release();
424 if (pDepthStencilView) {
426 image = getDepthStencilViewImage(pDevice, pDepthStencilView);
428 json.beginMember("DEPTH_STENCIL");
429 json.writeImage(image, "UNKNOWN");
433 pDepthStencilView->Release();
438 json.endMember(); // framebuffer
442 } /* namespace d3dstate */