+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);
+}
+
+