From 283551aa08de851453ec8ac378efdbd920a26364 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Jos=C3=A9=20Fonseca?= Date: Wed, 3 Jul 2013 15:48:30 +0100 Subject: [PATCH] d3dretrace: Dump d3d10 PS resource shader views. --- retrace/d3d10state.cpp | 5 +- retrace/d3d10state_images.cpp | 240 ++++++++++++++++++++++------------ retrace/d3dstate.hpp | 3 + retrace/dxgistate.hpp | 5 + 4 files changed, 163 insertions(+), 90 deletions(-) diff --git a/retrace/d3d10state.cpp b/retrace/d3d10state.cpp index d5e41da..0f6cf80 100644 --- a/retrace/d3d10state.cpp +++ b/retrace/d3d10state.cpp @@ -83,10 +83,7 @@ dumpDevice(std::ostream &os, ID3D10Device *pDevice) dumpShaders(json, pDevice); - json.beginMember("textures"); - json.beginObject(); - json.endObject(); - json.endMember(); // textures + dumpTextures(json, pDevice); dumpFramebuffer(json, pDevice); } diff --git a/retrace/d3d10state_images.cpp b/retrace/d3d10state_images.cpp index 1fc4492..5c9060e 100644 --- a/retrace/d3d10state_images.cpp +++ b/retrace/d3d10state_images.cpp @@ -139,20 +139,20 @@ stageResource(ID3D10Device *pDevice, static HRESULT mapResource(ID3D10Resource *pResource, - UINT Subresource, D3D10_MAP MapType, UINT MapFlags, - D3D10_MAPPED_TEXTURE3D *pMappedSubresource) { + UINT SubResource, D3D10_MAP MapType, UINT MapFlags, + D3D10_MAPPED_TEXTURE3D *pMappedSubResource) { D3D10_RESOURCE_DIMENSION Type = D3D10_RESOURCE_DIMENSION_UNKNOWN; pResource->GetType(&Type); switch (Type) { case D3D10_RESOURCE_DIMENSION_BUFFER: - assert(Subresource == 0); - return static_cast(pResource)->Map(MapType, MapFlags, &pMappedSubresource->pData); + assert(SubResource == 0); + return static_cast(pResource)->Map(MapType, MapFlags, &pMappedSubResource->pData); case D3D10_RESOURCE_DIMENSION_TEXTURE1D: - return static_cast(pResource)->Map(Subresource, MapType, MapFlags, &pMappedSubresource->pData); + return static_cast(pResource)->Map(SubResource, MapType, MapFlags, &pMappedSubResource->pData); case D3D10_RESOURCE_DIMENSION_TEXTURE2D: - return static_cast(pResource)->Map(Subresource, MapType, MapFlags, reinterpret_cast(pMappedSubresource)); + return static_cast(pResource)->Map(SubResource, MapType, MapFlags, reinterpret_cast(pMappedSubResource)); case D3D10_RESOURCE_DIMENSION_TEXTURE3D: - return static_cast(pResource)->Map(Subresource, MapType, MapFlags, pMappedSubresource); + return static_cast(pResource)->Map(SubResource, MapType, MapFlags, pMappedSubResource); default: assert(0); return E_NOTIMPL; @@ -160,40 +160,139 @@ mapResource(ID3D10Resource *pResource, } static void -unmapResource(ID3D10Resource *pResource, UINT Subresource) { +unmapResource(ID3D10Resource *pResource, UINT SubResource) { D3D10_RESOURCE_DIMENSION Type = D3D10_RESOURCE_DIMENSION_UNKNOWN; pResource->GetType(&Type); switch (Type) { case D3D10_RESOURCE_DIMENSION_BUFFER: - assert(Subresource == 0); + assert(SubResource == 0); static_cast(pResource)->Unmap(); break; case D3D10_RESOURCE_DIMENSION_TEXTURE1D: - static_cast(pResource)->Unmap(Subresource); + static_cast(pResource)->Unmap(SubResource); break; case D3D10_RESOURCE_DIMENSION_TEXTURE2D: - static_cast(pResource)->Unmap(Subresource); + static_cast(pResource)->Unmap(SubResource); break; case D3D10_RESOURCE_DIMENSION_TEXTURE3D: - static_cast(pResource)->Unmap(Subresource); + static_cast(pResource)->Unmap(SubResource); break; default: assert(0); } } +static image::Image * +getSubResourceImage(ID3D10Device *pDevice, + ID3D10Resource *pResource, + DXGI_FORMAT Format, + UINT MipSlice) +{ + image::Image *image = NULL; + ID3D10Resource *pStagingResource = NULL; + UINT Width, Height, Depth; + UINT SubResource = MipSlice; + D3D10_MAPPED_TEXTURE3D MappedSubResource; + HRESULT hr; + + if (!pResource) { + return NULL; + } + + hr = stageResource(pDevice, pResource, &pStagingResource, &Width, &Height, &Depth); + if (FAILED(hr)) { + goto no_staging; + } + + Width = std::max(Width >> MipSlice, 1U); + Height = std::max(Height >> MipSlice, 1U); + Depth = std::max(Depth >> MipSlice, 1U); + + hr = mapResource(pStagingResource, SubResource, D3D10_MAP_READ, 0, &MappedSubResource); + if (FAILED(hr)) { + goto no_map; + } + + image = ConvertImage(Format, + MappedSubResource.pData, + MappedSubResource.RowPitch, + Width, Height); + + unmapResource(pStagingResource, SubResource); +no_map: + if (pStagingResource) { + pStagingResource->Release(); + } +no_staging: + if (pResource) { + pResource->Release(); + } + return image; +} + + +static image::Image * +getShaderResourceViewImage(ID3D10Device *pDevice, + ID3D10ShaderResourceView *pShaderResourceView) { + D3D10_SHADER_RESOURCE_VIEW_DESC Desc; + ID3D10Resource *pResource = NULL; + UINT MipSlice; + + if (!pShaderResourceView) { + return NULL; + } + + pShaderResourceView->GetResource(&pResource); + assert(pResource); + + pShaderResourceView->GetDesc(&Desc); + + // TODO: Take the slice in consideration + switch (Desc.ViewDimension) { + case D3D10_SRV_DIMENSION_BUFFER: + MipSlice = 0; + break; + case D3D10_SRV_DIMENSION_TEXTURE1D: + MipSlice = Desc.Texture1D.MostDetailedMip; + break; + case D3D10_SRV_DIMENSION_TEXTURE1DARRAY: + MipSlice = Desc.Texture1DArray.MostDetailedMip; + break; + case D3D10_SRV_DIMENSION_TEXTURE2D: + MipSlice = Desc.Texture2D.MostDetailedMip; + MipSlice = 0; + break; + case D3D10_SRV_DIMENSION_TEXTURE2DARRAY: + MipSlice = Desc.Texture2DArray.MostDetailedMip; + break; + case D3D10_SRV_DIMENSION_TEXTURE2DMS: + MipSlice = 0; + break; + case D3D10_SRV_DIMENSION_TEXTURE2DMSARRAY: + MipSlice = 0; + break; + case D3D10_SRV_DIMENSION_TEXTURE3D: + MipSlice = Desc.Texture3D.MostDetailedMip; + break; + case D3D10_SRV_DIMENSION_TEXTURECUBE: + MipSlice = Desc.TextureCube.MostDetailedMip; + break; + case D3D10_SRV_DIMENSION_UNKNOWN: + default: + assert(0); + return NULL; + } + + return getSubResourceImage(pDevice, pResource, Desc.Format, MipSlice); +} + + static image::Image * getRenderTargetViewImage(ID3D10Device *pDevice, ID3D10RenderTargetView *pRenderTargetView) { - image::Image *image = NULL; D3D10_RENDER_TARGET_VIEW_DESC Desc; ID3D10Resource *pResource = NULL; - ID3D10Resource *pStagingResource = NULL; - UINT Width, Height, Depth; UINT MipSlice; - UINT Subresource; - D3D10_MAPPED_TEXTURE3D MappedSubresource; - HRESULT hr; if (!pRenderTargetView) { return NULL; @@ -204,11 +303,6 @@ getRenderTargetViewImage(ID3D10Device *pDevice, pRenderTargetView->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 D3D10_RTV_DIMENSION_BUFFER: @@ -236,51 +330,22 @@ getRenderTargetViewImage(ID3D10Device *pDevice, case D3D10_RTV_DIMENSION_TEXTURE3D: MipSlice = Desc.Texture3D.MipSlice; break; - case D3D10_SRV_DIMENSION_UNKNOWN: + case D3D10_RTV_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 = mapResource(pStagingResource, Subresource, D3D10_MAP_READ, 0, &MappedSubresource); - if (FAILED(hr)) { - goto no_map; + return NULL; } - image = ConvertImage(Desc.Format, - MappedSubresource.pData, - MappedSubresource.RowPitch, - Width, Height); - - unmapResource(pStagingResource, Subresource); -no_map: - if (pStagingResource) { - pStagingResource->Release(); - } -no_staging: - if (pResource) { - pResource->Release(); - } - return image; + return getSubResourceImage(pDevice, pResource, Desc.Format, MipSlice); } + static image::Image * getDepthStencilViewImage(ID3D10Device *pDevice, ID3D10DepthStencilView *pDepthStencilView) { - image::Image *image = NULL; D3D10_DEPTH_STENCIL_VIEW_DESC Desc; ID3D10Resource *pResource = NULL; - ID3D10Resource *pStagingResource = NULL; - UINT Width, Height, Depth; UINT MipSlice; - UINT Subresource; - D3D10_MAPPED_TEXTURE3D MappedSubresource; - HRESULT hr; if (!pDepthStencilView) { return NULL; @@ -291,11 +356,6 @@ getDepthStencilViewImage(ID3D10Device *pDevice, 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 D3D10_DSV_DIMENSION_TEXTURE1D: @@ -317,37 +377,45 @@ getDepthStencilViewImage(ID3D10Device *pDevice, case D3D10_DSV_DIMENSION_TEXTURE2DMSARRAY: MipSlice = 0; break; - case D3D10_SRV_DIMENSION_UNKNOWN: + case D3D10_DSV_DIMENSION_UNKNOWN: default: assert(0); - goto no_map; + return NULL; } - Subresource = MipSlice; - Width = std::max(Width >> MipSlice, 1U); - Height = std::max(Height >> MipSlice, 1U); - Depth = std::max(Depth >> MipSlice, 1U); + return getSubResourceImage(pDevice, pResource, Desc.Format, MipSlice); +} - hr = mapResource(pStagingResource, Subresource, D3D10_MAP_READ, 0, &MappedSubresource); - if (FAILED(hr)) { - goto no_map; - } - image = ConvertImage(Desc.Format, - MappedSubresource.pData, - MappedSubresource.RowPitch, - Width, Height); +void +dumpTextures(JSONWriter &json, ID3D10Device *pDevice) +{ + json.beginMember("textures"); + json.beginObject(); - unmapResource(pStagingResource, Subresource); -no_map: - if (pStagingResource) { - pStagingResource->Release(); - } -no_staging: - if (pResource) { - pResource->Release(); + ID3D10ShaderResourceView *pShaderResourceViews[D3D10_COMMONSHADER_SAMPLER_SLOT_COUNT]; + pDevice->PSGetShaderResources(0, ARRAYSIZE(pShaderResourceViews), pShaderResourceViews); + + for (UINT i = 0; i < ARRAYSIZE(pShaderResourceViews); ++i) { + if (!pShaderResourceViews[i]) { + continue; + } + + image::Image *image; + image = getShaderResourceViewImage(pDevice, pShaderResourceViews[i]); + if (image) { + char label[64]; + _snprintf(label, sizeof label, "PS_RESOURCE_%u", i); + json.beginMember(label); + json.writeImage(image, "UNKNOWN"); + json.endMember(); // PS_RESOURCE_* + } + + pShaderResourceViews[i]->Release(); } - return image; + + json.endObject(); + json.endMember(); // textures } @@ -374,10 +442,10 @@ dumpFramebuffer(JSONWriter &json, ID3D10Device *pDevice) ID3D10RenderTargetView *pRenderTargetViews[D3D10_SIMULTANEOUS_RENDER_TARGET_COUNT]; ID3D10DepthStencilView *pDepthStencilView; - pDevice->OMGetRenderTargets(D3D10_SIMULTANEOUS_RENDER_TARGET_COUNT, pRenderTargetViews, + pDevice->OMGetRenderTargets(ARRAYSIZE(pRenderTargetViews), pRenderTargetViews, &pDepthStencilView); - for (UINT i = 0; i < D3D10_SIMULTANEOUS_RENDER_TARGET_COUNT; ++i) { + for (UINT i = 0; i < ARRAYSIZE(pRenderTargetViews); ++i) { if (!pRenderTargetViews[i]) { continue; } diff --git a/retrace/d3dstate.hpp b/retrace/d3dstate.hpp index eb0e3cd..93ba874 100644 --- a/retrace/d3dstate.hpp +++ b/retrace/d3dstate.hpp @@ -74,6 +74,9 @@ dumpDevice(std::ostream &os, IDirect3DDevice9 *pDevice); image::Image * getRenderTargetImage(ID3D10Device *pDevice); +void +dumpTextures(JSONWriter &json, ID3D10Device *pDevice); + void dumpFramebuffer(JSONWriter &json, ID3D10Device *pDevice); diff --git a/retrace/dxgistate.hpp b/retrace/dxgistate.hpp index 326b50f..eb3a6fe 100644 --- a/retrace/dxgistate.hpp +++ b/retrace/dxgistate.hpp @@ -34,6 +34,11 @@ #include +#ifndef ARRAYSIZE +#define ARRAYSIZE(_x) (sizeof(_x)/sizeof((_x)[0])) +#endif + + namespace image { class Image; } -- 2.43.0