]> git.cworth.org Git - apitrace/blobdiff - retrace/d3d11state_images.cpp
glstate: Handle GL_COMPUTE_SHADER.
[apitrace] / retrace / d3d11state_images.cpp
index e08bc29e71c86e295e585df1dd0b777d736d2ee1..1fcc80cf1f6c25f88fd35f160630e1ae97ea6ab7 100644 (file)
 #include <iostream>
 #include <algorithm>
 
-#include "image.hpp"
+#include "os.hpp"
+#include "json.hpp"
 #include "d3d11imports.hpp"
 #include "d3d10state.hpp"
-
-#ifdef __MINGW32__
-#define nullptr NULL
-#endif
-#include "DirectXTex.h"
-
-
-/**
- * Convert between DXGI formats.
- *
- */
-static HRESULT
-ConvertFormat(DXGI_FORMAT SrcFormat,
-              void *SrcData,
-              UINT SrcPitch,
-              DXGI_FORMAT DstFormat,
-              void *DstData,
-              UINT DstPitch,
-              UINT Width, UINT Height)
-{
-    HRESULT hr;
-
-    DirectX::Image SrcImage;
-    DirectX::Image DstImage;
-    
-    SrcImage.width = Width;
-    SrcImage.height = Height;
-    SrcImage.format = SrcFormat;
-    SrcImage.rowPitch = SrcPitch;
-    SrcImage.slicePitch = Height * SrcPitch;
-    SrcImage.pixels = (uint8_t*)SrcData;
-    
-    DstImage.width = Width;
-    DstImage.height = Height;
-    DstImage.format = DstFormat;
-    DstImage.rowPitch = DstPitch;
-    DstImage.slicePitch = Height * DstPitch;
-    DstImage.pixels = (uint8_t*)DstData;
-    DirectX::Rect rect(0, 0, Width, Height);
-    if (SrcFormat != DstFormat) {
-        DirectX::ScratchImage ScratchImage;
-        ScratchImage.Initialize2D(DstFormat, Width, Height, 1, 1);
-  
-        hr = DirectX::Convert(SrcImage, DstFormat, DirectX::TEX_FILTER_DEFAULT, 0.0f, ScratchImage);
-        if (SUCCEEDED(hr)) {
-            hr = CopyRectangle(*ScratchImage.GetImage(0, 0, 0), rect, DstImage, DirectX::TEX_FILTER_DEFAULT, 0, 0);
-        }
-    } else {
-        hr = CopyRectangle(SrcImage, rect, DstImage, DirectX::TEX_FILTER_DEFAULT, 0, 0);
-    }
-    return hr;
-}
+#include "dxgistate.hpp"
 
 
 namespace d3dstate {
@@ -195,7 +142,7 @@ stageResource(ID3D11DeviceContext *pDeviceContext,
     return hr;
 }
 
-image::Image *
+static image::Image *
 getRenderTargetViewImage(ID3D11DeviceContext *pDevice,
                          ID3D11RenderTargetView *pRenderTargetView) {
     image::Image *image = NULL;
@@ -265,25 +212,92 @@ getRenderTargetViewImage(ID3D11DeviceContext *pDevice,
         goto no_map;
     }
 
-    image = new image::Image(Width, Height, 4);
-    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;
+}
+
+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;
     }
-    assert(image->stride() > 0);
-
-    hr = ConvertFormat(Desc.Format,
-                       MappedSubresource.pData,
-                       MappedSubresource.RowPitch,
-                       DXGI_FORMAT_R8G8B8A8_UNORM,
-                       image->start(),
-                       image->stride(),
-                       Width, Height);
+    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)) {
-        delete image;
-        image = NULL;
+        goto no_map;
     }
 
-no_image:
+    image = ConvertImage(Desc.Format,
+                         MappedSubresource.pData,
+                         MappedSubresource.RowPitch,
+                         Width, Height);
+
     pDevice->Unmap(pStagingResource, Subresource);
 no_map:
     if (pStagingResource) {
@@ -297,8 +311,6 @@ no_staging:
 }
 
 
-
-
 image::Image *
 getRenderTargetImage(ID3D11DeviceContext *pDevice) {
     ID3D11RenderTargetView *pRenderTargetView = NULL;
@@ -321,7 +333,9 @@ dumpFramebuffer(JSONWriter &json, ID3D11DeviceContext *pDevice)
     json.beginObject();
 
     ID3D11RenderTargetView *pRenderTargetViews[D3D11_SIMULTANEOUS_RENDER_TARGET_COUNT];
-    pDevice->OMGetRenderTargets(D3D11_SIMULTANEOUS_RENDER_TARGET_COUNT, pRenderTargetViews, NULL);
+    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]) {
@@ -341,6 +355,19 @@ dumpFramebuffer(JSONWriter &json, ID3D11DeviceContext *pDevice)
         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
 }