#include <assert.h>
+#include <algorithm>
+
static size_t
-_getMapSize(DXGI_FORMAT Format, UINT Width, UINT Height, INT RowPitch, UINT Depth = 1, INT DepthPitch = 0) {
+_calcDataSize(DXGI_FORMAT Format, UINT Width, UINT Height, UINT RowPitch, UINT Depth = 1, UINT DepthPitch = 0) {
if (Width == 0 || Height == 0 || Depth == 0) {
return 0;
}
- 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;
- }
-
switch (Format) {
case DXGI_FORMAT_BC1_TYPELESS:
case DXGI_FORMAT_BC1_UNORM:
return size;
}
+static size_t
+_calcMipDataSize(UINT MipLevel, DXGI_FORMAT Format, UINT Width, UINT Height, UINT RowPitch, UINT Depth = 1, UINT DepthPitch = 0) {
+ if (Width == 0 || Height == 0 || Depth == 0) {
+ return 0;
+ }
+
+ Width = std::max(Width >> MipLevel, UINT(1));
+ Height = std::max(Height >> MipLevel, UINT(1));
+ Depth = std::max(Depth >> MipLevel, UINT(1));
+
+ return _calcDataSize(Format, Width, Height, RowPitch, Depth, DepthPitch);
+}
+
+
+inline UINT
+_getNumMipLevels(UINT Width, UINT Height = 1, UINT Depth = 1) {
+ UINT MipLevels = 0;
+ do {
+ ++MipLevels;
+ Width >>= 1;
+ Height >>= 1;
+ Depth >>= 1;
+ } while (Width || Height || Depth);
+ return MipLevels;
+}
+
+inline UINT
+_getNumMipLevels(const D3D10_BUFFER_DESC *pDesc) {
+ return 1;
+}
+
+inline UINT
+_getNumMipLevels(const D3D10_TEXTURE1D_DESC *pDesc) {
+ return pDesc->MipLevels != 0 ? pDesc->MipLevels : _getNumMipLevels(pDesc->Width);
+}
+
+inline UINT
+_getNumMipLevels(const D3D10_TEXTURE2D_DESC *pDesc) {
+ return pDesc->MipLevels != 0 ? pDesc->MipLevels : _getNumMipLevels(pDesc->Width, pDesc->Height);
+}
+
+inline UINT
+_getNumMipLevels(const D3D10_TEXTURE3D_DESC *pDesc) {
+ return pDesc->MipLevels != 0 ? pDesc->MipLevels : _getNumMipLevels(pDesc->Width, pDesc->Height, pDesc->Depth);
+}
+
+inline UINT
+_getNumSubResources(const D3D10_BUFFER_DESC *pDesc) {
+ return 1;
+}
+
+inline UINT
+_getNumSubResources(const D3D10_TEXTURE1D_DESC *pDesc) {
+ return _getNumMipLevels(pDesc) * pDesc->ArraySize;
+}
+
+inline UINT
+_getNumSubResources(const D3D10_TEXTURE2D_DESC *pDesc) {
+ return _getNumMipLevels(pDesc) * pDesc->ArraySize;
+}
+
+inline UINT
+_getNumSubResources(const D3D10_TEXTURE3D_DESC *pDesc) {
+ return _getNumMipLevels(pDesc);
+}
+
+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) {
pResource->GetDesc(&Desc);
pMappedData = *ppData;
- MappedSize = _getMapSize(Desc.Format, Desc.Width, 1, 0);
+ MappedSize = _calcSubresourceSize(&Desc, Subresource);
}
static inline void
pResource->GetDesc(&Desc);
pMappedData = pMappedTex2D->pData;
- MappedSize = _getMapSize(Desc.Format, Desc.Width, Desc.Height, pMappedTex2D->RowPitch);
+ MappedSize = _calcSubresourceSize(&Desc, Subresource, pMappedTex2D->RowPitch);
}
static inline void
pResource->GetDesc(&Desc);
pMappedData = pMappedTex3D->pData;
- MappedSize = _getMapSize(Desc.Format, Desc.Width, Desc.Height, pMappedTex3D->RowPitch, Desc.Depth, pMappedTex3D->DepthPitch);
+ MappedSize = _calcSubresourceSize(&Desc, Subresource, pMappedTex3D->RowPitch, pMappedTex3D->DepthPitch);
}
}
pMappedData = pLockedRect->pBits;
- MappedSize = _getMapSize(Desc.Format, Desc.Width, Desc.Height, pLockedRect->Pitch);
+ MappedSize = _calcDataSize(Desc.Format, Desc.Width, Desc.Height, pLockedRect->Pitch);
}