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 _D3D10SIZE_HPP_
34 #define _D3D10SIZE_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 D3D10_BUFFER_DESC *pDesc) {
53 _getNumMipLevels(const D3D10_TEXTURE1D_DESC *pDesc) {
54 return pDesc->MipLevels != 0 ? pDesc->MipLevels : _getNumMipLevels(pDesc->Width);
58 _getNumMipLevels(const D3D10_TEXTURE2D_DESC *pDesc) {
59 return pDesc->MipLevels != 0 ? pDesc->MipLevels : _getNumMipLevels(pDesc->Width, pDesc->Height);
63 _getNumMipLevels(const D3D10_TEXTURE3D_DESC *pDesc) {
64 return pDesc->MipLevels != 0 ? pDesc->MipLevels : _getNumMipLevels(pDesc->Width, pDesc->Height, pDesc->Depth);
68 _getNumSubResources(const D3D10_BUFFER_DESC *pDesc) {
73 _getNumSubResources(const D3D10_TEXTURE1D_DESC *pDesc) {
74 return _getNumMipLevels(pDesc) * pDesc->ArraySize;
78 _getNumSubResources(const D3D10_TEXTURE2D_DESC *pDesc) {
79 return _getNumMipLevels(pDesc) * pDesc->ArraySize;
83 _getNumSubResources(const D3D10_TEXTURE3D_DESC *pDesc) {
84 return _getNumMipLevels(pDesc);
88 _calcSubresourceSize(const D3D10_BUFFER_DESC *pDesc, UINT Subresource, UINT RowPitch = 0, UINT SlicePitch = 0) {
89 return pDesc->ByteWidth;
93 _calcSubresourceSize(const D3D10_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 D3D10_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 D3D10_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 _getMapInfo(ID3D10Buffer *pResource, D3D10_MAP MapType, UINT MapFlags, void * * ppData,
112 void * & pMappedData, size_t & MappedSize) {
116 if (MapType == D3D10_MAP_READ) {
120 D3D10_BUFFER_DESC Desc;
121 pResource->GetDesc(&Desc);
123 pMappedData = *ppData;
124 MappedSize = Desc.ByteWidth;
128 _getMapInfo(ID3D10Texture1D *pResource, UINT Subresource, D3D10_MAP MapType, UINT MapFlags, void * * ppData,
129 void * & pMappedData, size_t & MappedSize) {
133 if (MapType == D3D10_MAP_READ) {
137 D3D10_TEXTURE1D_DESC Desc;
138 pResource->GetDesc(&Desc);
140 pMappedData = *ppData;
141 MappedSize = _calcSubresourceSize(&Desc, Subresource);
145 _getMapInfo(ID3D10Texture2D *pResource, UINT Subresource, D3D10_MAP MapType, UINT MapFlags, D3D10_MAPPED_TEXTURE2D * pMappedTex2D,
146 void * & pMappedData, size_t & MappedSize) {
150 if (MapType == D3D10_MAP_READ) {
154 D3D10_TEXTURE2D_DESC Desc;
155 pResource->GetDesc(&Desc);
157 pMappedData = pMappedTex2D->pData;
158 MappedSize = _calcSubresourceSize(&Desc, Subresource, pMappedTex2D->RowPitch);
162 _getMapInfo(ID3D10Texture3D *pResource, UINT Subresource, D3D10_MAP MapType, UINT MapFlags, D3D10_MAPPED_TEXTURE3D * pMappedTex3D,
163 void * & pMappedData, size_t & MappedSize) {
167 if (MapType == D3D10_MAP_READ) {
171 D3D10_TEXTURE3D_DESC Desc;
172 pResource->GetDesc(&Desc);
174 pMappedData = pMappedTex3D->pData;
175 MappedSize = _calcSubresourceSize(&Desc, Subresource, pMappedTex3D->RowPitch, pMappedTex3D->DepthPitch);
180 _calcSubresourceSize(ID3D10Resource *pDstResource, UINT DstSubresource, const D3D10_BOX *pDstBox, UINT SrcRowPitch, UINT SrcDepthPitch) {
182 (pDstBox->left >= pDstBox->right ||
183 pDstBox->top >= pDstBox->bottom ||
184 pDstBox->front >= pDstBox->back)) {
188 D3D10_RESOURCE_DIMENSION Type = D3D10_RESOURCE_DIMENSION_UNKNOWN;
189 pDstResource->GetType(&Type);
198 case D3D10_RESOURCE_DIMENSION_BUFFER:
200 return pDstBox->right - pDstBox->left;
202 D3D10_BUFFER_DESC Desc;
203 static_cast<ID3D10Buffer *>(pDstResource)->GetDesc(&Desc);
204 return Desc.ByteWidth;
206 case D3D10_RESOURCE_DIMENSION_TEXTURE1D:
208 D3D10_TEXTURE1D_DESC Desc;
209 static_cast<ID3D10Texture1D *>(pDstResource)->GetDesc(&Desc);
210 Format = Desc.Format;
212 MipLevel = DstSubresource % Desc.MipLevels;
215 case D3D10_RESOURCE_DIMENSION_TEXTURE2D:
217 D3D10_TEXTURE2D_DESC Desc;
218 static_cast<ID3D10Texture2D *>(pDstResource)->GetDesc(&Desc);
219 Format = Desc.Format;
221 Height = Desc.Height;
222 MipLevel = DstSubresource % Desc.MipLevels;
225 case D3D10_RESOURCE_DIMENSION_TEXTURE3D:
227 D3D10_TEXTURE3D_DESC Desc;
228 static_cast<ID3D10Texture3D *>(pDstResource)->GetDesc(&Desc);
229 Format = Desc.Format;
231 Height = Desc.Height;
235 case D3D10_RESOURCE_DIMENSION_UNKNOWN:
241 return _calcMipDataSize(MipLevel, Format, Width, Height, SrcRowPitch, Depth, SrcDepthPitch);
245 #endif /* _D3D10SIZE_HPP_ */