]> git.cworth.org Git - apitrace/blobdiff - retrace/d3d11state_images.cpp
d3dstate: Dump depth-stencil images.
[apitrace] / retrace / d3d11state_images.cpp
index 6e0d7c9c5169b811e0b23de0eef4e62c3a8e86b1..1fcc80cf1f6c25f88fd35f160630e1ae97ea6ab7 100644 (file)
 #include <iostream>
 #include <algorithm>
 
-#include "image.hpp"
+#include "os.hpp"
+#include "json.hpp"
 #include "d3d11imports.hpp"
+#include "d3d10state.hpp"
+#include "dxgistate.hpp"
 
 
 namespace d3dstate {
@@ -98,7 +101,7 @@ stageResource(ID3D11DeviceContext *pDeviceContext,
             Desc.Usage = Usage;
             Desc.BindFlags = BindFlags;
             Desc.CPUAccessFlags = CPUAccessFlags;
-            Desc.MiscFlags &= ~D3D11_RESOURCE_MISC_TEXTURECUBE;
+            Desc.MiscFlags &= D3D11_RESOURCE_MISC_TEXTURECUBE;
 
             *pWidth = Desc.Width;
             *pHeight = Desc.Height;
@@ -139,10 +142,10 @@ stageResource(ID3D11DeviceContext *pDeviceContext,
     return hr;
 }
 
-image::Image *
-getRenderTargetImage(ID3D11DeviceContext *pDevice) {
+static image::Image *
+getRenderTargetViewImage(ID3D11DeviceContext *pDevice,
+                         ID3D11RenderTargetView *pRenderTargetView) {
     image::Image *image = NULL;
-    ID3D11RenderTargetView *pRenderTargetView = NULL;
     D3D11_RENDER_TARGET_VIEW_DESC Desc;
     ID3D11Resource *pResource = NULL;
     ID3D11Resource *pStagingResource = NULL;
@@ -151,23 +154,15 @@ getRenderTargetImage(ID3D11DeviceContext *pDevice) {
     UINT Subresource;
     D3D11_MAPPED_SUBRESOURCE MappedSubresource;
     HRESULT hr;
-    const unsigned char *src;
-    unsigned char *dst;
 
-    pDevice->OMGetRenderTargets(1, &pRenderTargetView, NULL);
     if (!pRenderTargetView) {
-        goto no_rendertarget;
+        return NULL;
     }
 
     pRenderTargetView->GetResource(&pResource);
     assert(pResource);
 
     pRenderTargetView->GetDesc(&Desc);
-    if (Desc.Format != DXGI_FORMAT_R8G8B8A8_UNORM &&
-        Desc.Format != DXGI_FORMAT_R32G32B32A32_FLOAT) {
-        std::cerr << "warning: unsupported DXGI format " << Desc.Format << "\n";
-        goto no_staging;
-    }
 
     hr = stageResource(pDevice, pResource, &pStagingResource, &Width, &Height, &Depth);
     if (FAILED(hr)) {
@@ -217,32 +212,92 @@ getRenderTargetImage(ID3D11DeviceContext *pDevice) {
         goto no_map;
     }
 
-    image = new image::Image(Width, Height, 4, true);
-    if (!image) {
-        goto no_image;
+    image = ConvertImage(Desc.Format,
+                         MappedSubresource.pData,
+                         MappedSubresource.RowPitch,
+                         Width, Height);
+
+    pDevice->Unmap(pStagingResource, Subresource);
+no_map:
+    if (pStagingResource) {
+        pStagingResource->Release();
+    }
+no_staging:
+    if (pResource) {
+        pResource->Release();
     }
+    return image;
+}
 
-    dst = image->start();
-    src = (const unsigned char *)MappedSubresource.pData;
-    for (unsigned y = 0; y < Height; ++y) {
-        if (Desc.Format == DXGI_FORMAT_R8G8B8A8_UNORM) {
-            memcpy(dst, src, Width * 4);
-        } else if (Desc.Format == DXGI_FORMAT_R32G32B32A32_FLOAT) {
-            float scale = 1.0f/255.0f;
-            for (unsigned x = 0; x < Width; ++x) {
-                dst[4*x + 0] = ((float *)src)[4*x + 0] * scale;
-                dst[4*x + 1] = ((float *)src)[4*x + 1] * scale;
-                dst[4*x + 2] = ((float *)src)[4*x + 2] * scale;
-                dst[4*x + 3] = ((float *)src)[4*x + 3] * scale;
-            }
-        } else {
-            assert(0);
-        }
-        src += MappedSubresource.RowPitch;
-        dst += image->stride();
+static image::Image *
+getDepthStencilViewImage(ID3D11DeviceContext *pDevice,
+                         ID3D11DepthStencilView *pDepthStencilView) {
+    image::Image *image = NULL;
+    D3D11_DEPTH_STENCIL_VIEW_DESC Desc;
+    ID3D11Resource *pResource = NULL;
+    ID3D11Resource *pStagingResource = NULL;
+    UINT Width, Height, Depth;
+    UINT MipSlice;
+    UINT Subresource;
+    D3D11_MAPPED_SUBRESOURCE MappedSubresource;
+    HRESULT hr;
+
+    if (!pDepthStencilView) {
+        return NULL;
+    }
+
+    pDepthStencilView->GetResource(&pResource);
+    assert(pResource);
+
+    pDepthStencilView->GetDesc(&Desc);
+
+    hr = stageResource(pDevice, pResource, &pStagingResource, &Width, &Height, &Depth);
+    if (FAILED(hr)) {
+        goto no_staging;
+    }
+
+    // TODO: Take the slice in consideration
+    switch (Desc.ViewDimension) {
+    case D3D11_DSV_DIMENSION_TEXTURE1D:
+        MipSlice = Desc.Texture1D.MipSlice;
+        break;
+    case D3D11_DSV_DIMENSION_TEXTURE1DARRAY:
+        MipSlice = Desc.Texture1DArray.MipSlice;
+        break;
+    case D3D11_DSV_DIMENSION_TEXTURE2D:
+        MipSlice = Desc.Texture2D.MipSlice;
+        MipSlice = 0;
+        break;
+    case D3D11_DSV_DIMENSION_TEXTURE2DARRAY:
+        MipSlice = Desc.Texture2DArray.MipSlice;
+        break;
+    case D3D11_DSV_DIMENSION_TEXTURE2DMS:
+        MipSlice = 0;
+        break;
+    case D3D11_DSV_DIMENSION_TEXTURE2DMSARRAY:
+        MipSlice = 0;
+        break;
+    case D3D11_SRV_DIMENSION_UNKNOWN:
+    default:
+        assert(0);
+        goto no_map;
+    }
+    Subresource = MipSlice;
+
+    Width  = std::max(Width  >> MipSlice, 1U);
+    Height = std::max(Height >> MipSlice, 1U);
+    Depth  = std::max(Depth  >> MipSlice, 1U);
+
+    hr = pDevice->Map(pStagingResource, Subresource, D3D11_MAP_READ, 0, &MappedSubresource);
+    if (FAILED(hr)) {
+        goto no_map;
     }
 
-no_image:
+    image = ConvertImage(Desc.Format,
+                         MappedSubresource.pData,
+                         MappedSubresource.RowPitch,
+                         Width, Height);
+
     pDevice->Unmap(pStagingResource, Subresource);
 no_map:
     if (pStagingResource) {
@@ -252,12 +307,70 @@ no_staging:
     if (pResource) {
         pResource->Release();
     }
+    return image;
+}
+
+
+image::Image *
+getRenderTargetImage(ID3D11DeviceContext *pDevice) {
+    ID3D11RenderTargetView *pRenderTargetView = NULL;
+    pDevice->OMGetRenderTargets(1, &pRenderTargetView, NULL);
+
+    image::Image *image = NULL;
     if (pRenderTargetView) {
+        image = getRenderTargetViewImage(pDevice, pRenderTargetView);
         pRenderTargetView->Release();
     }
-no_rendertarget:
+
     return image;
 }
 
 
+void
+dumpFramebuffer(JSONWriter &json, ID3D11DeviceContext *pDevice)
+{
+    json.beginMember("framebuffer");
+    json.beginObject();
+
+    ID3D11RenderTargetView *pRenderTargetViews[D3D11_SIMULTANEOUS_RENDER_TARGET_COUNT];
+    ID3D11DepthStencilView *pDepthStencilView;
+    pDevice->OMGetRenderTargets(D3D11_SIMULTANEOUS_RENDER_TARGET_COUNT, pRenderTargetViews,
+                                &pDepthStencilView);
+
+    for (UINT i = 0; i < D3D11_SIMULTANEOUS_RENDER_TARGET_COUNT; ++i) {
+        if (!pRenderTargetViews[i]) {
+            continue;
+        }
+
+        image::Image *image;
+        image = getRenderTargetViewImage(pDevice, pRenderTargetViews[i]);
+        if (image) {
+            char label[64];
+            _snprintf(label, sizeof label, "RENDER_TARGET_%u", i);
+            json.beginMember(label);
+            json.writeImage(image, "UNKNOWN");
+            json.endMember(); // RENDER_TARGET_*
+        }
+
+        pRenderTargetViews[i]->Release();
+    }
+
+    if (pDepthStencilView) {
+        image::Image *image;
+        image = getDepthStencilViewImage(pDevice, pDepthStencilView);
+        if (image) {
+            json.beginMember("DEPTH_STENCIL");
+            json.writeImage(image, "UNKNOWN");
+            json.endMember();
+        }
+
+        pDepthStencilView->Release();
+
+    }
+
+    json.endObject();
+    json.endMember(); // framebuffer
+}
+
+
 } /* namespace d3dstate */