#include <assert.h>
+#include <algorithm>
-static size_t
-_getMapSize(DXGI_FORMAT Format, UINT Width, UINT Height, INT RowPitch, UINT Depth = 1, INT DepthPitch = 0) {
- if (Width == 0 || Height == 0 || Depth == 0) {
- return 0;
- }
+#include "dxgisize.hpp"
- if (RowPitch < 0) {
- os::log("apitrace: warning: %s: negative row pitch %i\n", __FUNCTION__, RowPitch);
- return 0;
- }
- if (DepthPitch < 0) {
- os::log("apitrace: warning: %s: negative slice pitch %i\n", __FUNCTION__, DepthPitch);
- return 0;
- }
+inline UINT
+_getNumMipLevels(const D3D10_BUFFER_DESC *pDesc) {
+ return 1;
+}
- switch (Format) {
- case DXGI_FORMAT_BC1_TYPELESS:
- case DXGI_FORMAT_BC1_UNORM:
- case DXGI_FORMAT_BC1_UNORM_SRGB:
- case DXGI_FORMAT_BC2_TYPELESS:
- case DXGI_FORMAT_BC2_UNORM:
- case DXGI_FORMAT_BC2_UNORM_SRGB:
- case DXGI_FORMAT_BC3_TYPELESS:
- case DXGI_FORMAT_BC3_UNORM:
- case DXGI_FORMAT_BC3_UNORM_SRGB:
- case DXGI_FORMAT_BC4_TYPELESS:
- case DXGI_FORMAT_BC4_UNORM:
- case DXGI_FORMAT_BC4_SNORM:
- case DXGI_FORMAT_BC5_TYPELESS:
- case DXGI_FORMAT_BC5_UNORM:
- case DXGI_FORMAT_BC5_SNORM:
- Width = (Width + 3) / 4;
- Height = (Height + 3) / 4;
- break;
+inline UINT
+_getNumMipLevels(const D3D10_TEXTURE1D_DESC *pDesc) {
+ return pDesc->MipLevels != 0 ? pDesc->MipLevels : _getNumMipLevels(pDesc->Width);
+}
- case DXGI_FORMAT_R8G8_B8G8_UNORM:
- case DXGI_FORMAT_G8R8_G8B8_UNORM:
- Width = (Width + 1) / 2;
- break;
+inline UINT
+_getNumMipLevels(const D3D10_TEXTURE2D_DESC *pDesc) {
+ return pDesc->MipLevels != 0 ? pDesc->MipLevels : _getNumMipLevels(pDesc->Width, pDesc->Height);
+}
- case DXGI_FORMAT_UNKNOWN:
- return 0;
+inline UINT
+_getNumMipLevels(const D3D10_TEXTURE3D_DESC *pDesc) {
+ return pDesc->MipLevels != 0 ? pDesc->MipLevels : _getNumMipLevels(pDesc->Width, pDesc->Height, pDesc->Depth);
+}
- default:
- break;
- }
+inline UINT
+_getNumSubResources(const D3D10_BUFFER_DESC *pDesc) {
+ return 1;
+}
- /* FIXME */
- (void)Width;
+inline UINT
+_getNumSubResources(const D3D10_TEXTURE1D_DESC *pDesc) {
+ return _getNumMipLevels(pDesc) * pDesc->ArraySize;
+}
- size_t size = Height * RowPitch;
+inline UINT
+_getNumSubResources(const D3D10_TEXTURE2D_DESC *pDesc) {
+ return _getNumMipLevels(pDesc) * pDesc->ArraySize;
+}
- if (Depth > 1) {
- size += (Depth - 1) * DepthPitch;
- }
+inline UINT
+_getNumSubResources(const D3D10_TEXTURE3D_DESC *pDesc) {
+ return _getNumMipLevels(pDesc);
+}
- return size;
+static inline size_t
+_calcSubresourceSize(const D3D10_BUFFER_DESC *pDesc, UINT Subresource, UINT RowPitch = 0, UINT SlicePitch = 0) {
+ return pDesc->ByteWidth;
+}
+
+static inline size_t
+_calcSubresourceSize(const D3D10_TEXTURE1D_DESC *pDesc, UINT Subresource, UINT RowPitch = 0, UINT SlicePitch = 0) {
+ UINT MipLevel = Subresource % _getNumMipLevels(pDesc);
+ return _calcMipDataSize(MipLevel, pDesc->Format, pDesc->Width, 1, RowPitch, 1, SlicePitch);
+}
+
+static inline size_t
+_calcSubresourceSize(const D3D10_TEXTURE2D_DESC *pDesc, UINT Subresource, UINT RowPitch, UINT SlicePitch = 0) {
+ UINT MipLevel = Subresource % _getNumMipLevels(pDesc);
+ return _calcMipDataSize(MipLevel, pDesc->Format, pDesc->Width, pDesc->Height, RowPitch, 1, SlicePitch);
+}
+
+static inline size_t
+_calcSubresourceSize(const D3D10_TEXTURE3D_DESC *pDesc, UINT Subresource, UINT RowPitch, UINT SlicePitch) {
+ UINT MipLevel = Subresource;
+ return _calcMipDataSize(MipLevel, pDesc->Format, pDesc->Width, pDesc->Height, RowPitch, pDesc->Depth, SlicePitch);
}
static inline void
-_getMapInfo(ID3D10Buffer *pResource, D3D10_MAP MapType, UINT MapFlags, void * * ppData,
- void * & pMappedData, size_t & MappedSize) {
- pMappedData = 0;
- MappedSize = 0;
+_getMapDesc(ID3D10Buffer *pResource, D3D10_MAP MapType, UINT MapFlags, void * * ppData,
+ _MAP_DESC & MapDesc) {
+ MapDesc.pData = 0;
+ MapDesc.Size = 0;
if (MapType == D3D10_MAP_READ) {
return;
D3D10_BUFFER_DESC Desc;
pResource->GetDesc(&Desc);
- pMappedData = *ppData;
- MappedSize = Desc.ByteWidth;
+ MapDesc.pData = *ppData;
+ MapDesc.Size = Desc.ByteWidth;
}
static inline void
-_getMapInfo(ID3D10Texture1D *pResource, UINT Subresource, D3D10_MAP MapType, UINT MapFlags, void * * ppData,
- void * & pMappedData, size_t & MappedSize) {
- pMappedData = 0;
- MappedSize = 0;
+_getMapDesc(ID3D10Texture1D *pResource, UINT Subresource, D3D10_MAP MapType, UINT MapFlags, void * * ppData,
+ _MAP_DESC & MapDesc) {
+ MapDesc.pData = 0;
+ MapDesc.Size = 0;
if (MapType == D3D10_MAP_READ) {
return;
D3D10_TEXTURE1D_DESC Desc;
pResource->GetDesc(&Desc);
- pMappedData = *ppData;
- MappedSize = _getMapSize(Desc.Format, Desc.Width, 1, 0);
+ MapDesc.pData = *ppData;
+ MapDesc.Size = _calcSubresourceSize(&Desc, Subresource);
}
static inline void
-_getMapInfo(ID3D10Texture2D *pResource, UINT Subresource, D3D10_MAP MapType, UINT MapFlags, D3D10_MAPPED_TEXTURE2D * pMappedTex2D,
- void * & pMappedData, size_t & MappedSize) {
- pMappedData = 0;
- MappedSize = 0;
+_getMapDesc(ID3D10Texture2D *pResource, UINT Subresource, D3D10_MAP MapType, UINT MapFlags, D3D10_MAPPED_TEXTURE2D * pMappedTex2D,
+ _MAP_DESC & MapDesc) {
+ MapDesc.pData = 0;
+ MapDesc.Size = 0;
if (MapType == D3D10_MAP_READ) {
return;
D3D10_TEXTURE2D_DESC Desc;
pResource->GetDesc(&Desc);
- pMappedData = pMappedTex2D->pData;
- MappedSize = _getMapSize(Desc.Format, Desc.Width, Desc.Height, pMappedTex2D->RowPitch);
+ MapDesc.pData = pMappedTex2D->pData;
+ MapDesc.Size = _calcSubresourceSize(&Desc, Subresource, pMappedTex2D->RowPitch);
}
static inline void
-_getMapInfo(ID3D10Texture3D *pResource, UINT Subresource, D3D10_MAP MapType, UINT MapFlags, D3D10_MAPPED_TEXTURE3D * pMappedTex3D,
- void * & pMappedData, size_t & MappedSize) {
- pMappedData = 0;
- MappedSize = 0;
+_getMapDesc(ID3D10Texture3D *pResource, UINT Subresource, D3D10_MAP MapType, UINT MapFlags, D3D10_MAPPED_TEXTURE3D * pMappedTex3D,
+ _MAP_DESC & MapDesc) {
+ MapDesc.pData = 0;
+ MapDesc.Size = 0;
if (MapType == D3D10_MAP_READ) {
return;
D3D10_TEXTURE3D_DESC Desc;
pResource->GetDesc(&Desc);
- pMappedData = pMappedTex3D->pData;
- MappedSize = _getMapSize(Desc.Format, Desc.Width, Desc.Height, pMappedTex3D->RowPitch, Desc.Depth, pMappedTex3D->DepthPitch);
+ MapDesc.pData = pMappedTex3D->pData;
+ MapDesc.Size = _calcSubresourceSize(&Desc, Subresource, pMappedTex3D->RowPitch, pMappedTex3D->DepthPitch);
}
-static inline void
-_getMapInfo(IDXGISurface *pResource, DXGI_MAPPED_RECT * pLockedRect, UINT MapFlags,
- void * & pMappedData, size_t & MappedSize) {
- pMappedData = 0;
- MappedSize = 0;
+static inline size_t
+_calcSubresourceSize(ID3D10Resource *pDstResource, UINT DstSubresource, const D3D10_BOX *pDstBox, UINT SrcRowPitch, UINT SrcDepthPitch) {
+ if (pDstBox &&
+ (pDstBox->left >= pDstBox->right ||
+ pDstBox->top >= pDstBox->bottom ||
+ pDstBox->front >= pDstBox->back)) {
+ return 0;
+ }
- if (!(MapFlags & DXGI_MAP_WRITE)) {
- return;
+ D3D10_RESOURCE_DIMENSION Type = D3D10_RESOURCE_DIMENSION_UNKNOWN;
+ pDstResource->GetType(&Type);
+
+ DXGI_FORMAT Format;
+ UINT Width;
+ UINT Height = 1;
+ UINT Depth = 1;
+ UINT MipLevel = 0;
+
+ switch (Type) {
+ case D3D10_RESOURCE_DIMENSION_BUFFER:
+ if (pDstBox) {
+ return pDstBox->right - pDstBox->left;
+ } else {
+ D3D10_BUFFER_DESC Desc;
+ static_cast<ID3D10Buffer *>(pDstResource)->GetDesc(&Desc);
+ return Desc.ByteWidth;
+ }
+ case D3D10_RESOURCE_DIMENSION_TEXTURE1D:
+ {
+ D3D10_TEXTURE1D_DESC Desc;
+ static_cast<ID3D10Texture1D *>(pDstResource)->GetDesc(&Desc);
+ Format = Desc.Format;
+ Width = Desc.Width;
+ MipLevel = DstSubresource % Desc.MipLevels;
+ }
+ break;
+ case D3D10_RESOURCE_DIMENSION_TEXTURE2D:
+ {
+ D3D10_TEXTURE2D_DESC Desc;
+ static_cast<ID3D10Texture2D *>(pDstResource)->GetDesc(&Desc);
+ Format = Desc.Format;
+ Width = Desc.Width;
+ Height = Desc.Height;
+ MipLevel = DstSubresource % Desc.MipLevels;
+ }
+ break;
+ case D3D10_RESOURCE_DIMENSION_TEXTURE3D:
+ {
+ D3D10_TEXTURE3D_DESC Desc;
+ static_cast<ID3D10Texture3D *>(pDstResource)->GetDesc(&Desc);
+ Format = Desc.Format;
+ Width = Desc.Width;
+ Height = Desc.Height;
+ Depth = Desc.Depth;
+ }
+ break;
+ case D3D10_RESOURCE_DIMENSION_UNKNOWN:
+ default:
+ assert(0);
+ return 0;
}
- DXGI_SURFACE_DESC Desc;
- HRESULT hr = pResource->GetDesc(&Desc);
- if (FAILED(hr)) {
- return;
+ if (pDstBox) {
+ Width = pDstBox->right - pDstBox->left;
+ Height = pDstBox->bottom - pDstBox->top;
+ Depth = pDstBox->back - pDstBox->front;
}
- pMappedData = pLockedRect->pBits;
- MappedSize = _getMapSize(Desc.Format, Desc.Width, Desc.Height, pLockedRect->Pitch);
+ return _calcMipDataSize(MipLevel, Format, Width, Height, SrcRowPitch, Depth, SrcDepthPitch);
}