1 /**************************************************************************
3 * Copyright 2012 Jose Fonseca
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sub license,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice (including the next
14 * paragraph) shall be included in all copies or substantial portions of the
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
21 * AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
22 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF
23 * OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
26 **************************************************************************/
30 * Auxiliary functions to compute the size of array/blob arguments.
33 #ifndef _D3D11SIZE_HPP_
34 #define _D3D11SIZE_HPP_
37 /* We purposedly don't include any D3D header, so that this header can be used
38 * with all D3D versions. */
44 #include "dxgisize.hpp"
48 _getNumMipLevels(const D3D11_BUFFER_DESC *pDesc) {
53 _getNumMipLevels(const D3D11_TEXTURE1D_DESC *pDesc) {
54 return pDesc->MipLevels != 0 ? pDesc->MipLevels : _getNumMipLevels(pDesc->Width);
58 _getNumMipLevels(const D3D11_TEXTURE2D_DESC *pDesc) {
59 return pDesc->MipLevels != 0 ? pDesc->MipLevels : _getNumMipLevels(pDesc->Width, pDesc->Height);
63 _getNumMipLevels(const D3D11_TEXTURE3D_DESC *pDesc) {
64 return pDesc->MipLevels != 0 ? pDesc->MipLevels : _getNumMipLevels(pDesc->Width, pDesc->Height, pDesc->Depth);
68 _getNumSubResources(const D3D11_BUFFER_DESC *pDesc) {
73 _getNumSubResources(const D3D11_TEXTURE1D_DESC *pDesc) {
74 return _getNumMipLevels(pDesc) * pDesc->ArraySize;
78 _getNumSubResources(const D3D11_TEXTURE2D_DESC *pDesc) {
79 return _getNumMipLevels(pDesc) * pDesc->ArraySize;
83 _getNumSubResources(const D3D11_TEXTURE3D_DESC *pDesc) {
84 return _getNumMipLevels(pDesc);
88 _calcSubresourceSize(const D3D11_BUFFER_DESC *pDesc, UINT Subresource, UINT RowPitch = 0, UINT SlicePitch = 0) {
89 return pDesc->ByteWidth;
93 _calcSubresourceSize(const D3D11_TEXTURE1D_DESC *pDesc, UINT Subresource, UINT RowPitch = 0, UINT SlicePitch = 0) {
94 UINT MipLevel = Subresource % _getNumMipLevels(pDesc);
95 return _calcMipDataSize(MipLevel, pDesc->Format, pDesc->Width, 1, RowPitch, 1, SlicePitch);
99 _calcSubresourceSize(const D3D11_TEXTURE2D_DESC *pDesc, UINT Subresource, UINT RowPitch, UINT SlicePitch = 0) {
100 UINT MipLevel = Subresource % _getNumMipLevels(pDesc);
101 return _calcMipDataSize(MipLevel, pDesc->Format, pDesc->Width, pDesc->Height, RowPitch, 1, SlicePitch);
105 _calcSubresourceSize(const D3D11_TEXTURE3D_DESC *pDesc, UINT Subresource, UINT RowPitch, UINT SlicePitch) {
106 UINT MipLevel = Subresource;
107 return _calcMipDataSize(MipLevel, pDesc->Format, pDesc->Width, pDesc->Height, RowPitch, pDesc->Depth, SlicePitch);
111 _calcSubresourceSize(ID3D11Resource *pDstResource, UINT DstSubresource, const D3D11_BOX *pDstBox, UINT SrcRowPitch, UINT SrcDepthPitch) {
113 (pDstBox->left >= pDstBox->right ||
114 pDstBox->top >= pDstBox->bottom ||
115 pDstBox->front >= pDstBox->back)) {
119 D3D11_RESOURCE_DIMENSION Type = D3D11_RESOURCE_DIMENSION_UNKNOWN;
120 pDstResource->GetType(&Type);
129 case D3D11_RESOURCE_DIMENSION_BUFFER:
131 return pDstBox->right - pDstBox->left;
133 D3D11_BUFFER_DESC Desc;
134 static_cast<ID3D11Buffer *>(pDstResource)->GetDesc(&Desc);
135 return Desc.ByteWidth;
137 case D3D11_RESOURCE_DIMENSION_TEXTURE1D:
139 D3D11_TEXTURE1D_DESC Desc;
140 static_cast<ID3D11Texture1D *>(pDstResource)->GetDesc(&Desc);
141 Format = Desc.Format;
143 MipLevel = DstSubresource % Desc.MipLevels;
146 case D3D11_RESOURCE_DIMENSION_TEXTURE2D:
148 D3D11_TEXTURE2D_DESC Desc;
149 static_cast<ID3D11Texture2D *>(pDstResource)->GetDesc(&Desc);
150 Format = Desc.Format;
152 Height = Desc.Height;
153 MipLevel = DstSubresource % Desc.MipLevels;
156 case D3D11_RESOURCE_DIMENSION_TEXTURE3D:
158 D3D11_TEXTURE3D_DESC Desc;
159 static_cast<ID3D11Texture3D *>(pDstResource)->GetDesc(&Desc);
160 Format = Desc.Format;
162 Height = Desc.Height;
166 case D3D11_RESOURCE_DIMENSION_UNKNOWN:
173 Width = pDstBox->right - pDstBox->left;
174 Height = pDstBox->bottom - pDstBox->top;
175 Depth = pDstBox->back - pDstBox->front;
178 return _calcMipDataSize(MipLevel, Format, Width, Height, SrcRowPitch, Depth, SrcDepthPitch);
183 _getMapInfo(ID3D11DeviceContext* pContext, ID3D11Resource * pResource, UINT Subresource, D3D11_MAP MapType, UINT MapFlags, D3D11_MAPPED_SUBRESOURCE * pMappedResource,
184 void * & pMappedData, size_t & MappedSize) {
188 if (MapType == D3D11_MAP_READ) {
192 pMappedData = pMappedResource->pData;
193 MappedSize = _calcSubresourceSize(pResource, Subresource, NULL, pMappedResource->RowPitch, pMappedResource->DepthPitch);
197 #endif /* _D3D11SIZE_HPP_ */