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. */
46 _calcDataSize(DXGI_FORMAT Format, UINT Width, UINT Height, UINT RowPitch, UINT Depth = 1, UINT DepthPitch = 0) {
47 if (Width == 0 || Height == 0 || Depth == 0) {
52 case DXGI_FORMAT_BC1_TYPELESS:
53 case DXGI_FORMAT_BC1_UNORM:
54 case DXGI_FORMAT_BC1_UNORM_SRGB:
55 case DXGI_FORMAT_BC2_TYPELESS:
56 case DXGI_FORMAT_BC2_UNORM:
57 case DXGI_FORMAT_BC2_UNORM_SRGB:
58 case DXGI_FORMAT_BC3_TYPELESS:
59 case DXGI_FORMAT_BC3_UNORM:
60 case DXGI_FORMAT_BC3_UNORM_SRGB:
61 case DXGI_FORMAT_BC4_TYPELESS:
62 case DXGI_FORMAT_BC4_UNORM:
63 case DXGI_FORMAT_BC4_SNORM:
64 case DXGI_FORMAT_BC5_TYPELESS:
65 case DXGI_FORMAT_BC5_UNORM:
66 case DXGI_FORMAT_BC5_SNORM:
67 Width = (Width + 3) / 4;
68 Height = (Height + 3) / 4;
71 case DXGI_FORMAT_R8G8_B8G8_UNORM:
72 case DXGI_FORMAT_G8R8_G8B8_UNORM:
73 Width = (Width + 1) / 2;
76 case DXGI_FORMAT_UNKNOWN:
86 size_t size = Height * RowPitch;
89 size += (Depth - 1) * DepthPitch;
96 _calcMipDataSize(UINT MipLevel, DXGI_FORMAT Format, UINT Width, UINT Height, UINT RowPitch, UINT Depth = 1, UINT DepthPitch = 0) {
97 if (Width == 0 || Height == 0 || Depth == 0) {
101 Width = std::max(Width >> MipLevel, UINT(1));
102 Height = std::max(Height >> MipLevel, UINT(1));
103 Depth = std::max(Depth >> MipLevel, UINT(1));
105 return _calcDataSize(Format, Width, Height, RowPitch, Depth, DepthPitch);
110 _getNumMipLevels(UINT Width, UINT Height = 1, UINT Depth = 1) {
117 } while (Width || Height || Depth);
122 _getNumMipLevels(const D3D10_BUFFER_DESC *pDesc) {
127 _getNumMipLevels(const D3D10_TEXTURE1D_DESC *pDesc) {
128 return pDesc->MipLevels != 0 ? pDesc->MipLevels : _getNumMipLevels(pDesc->Width);
132 _getNumMipLevels(const D3D10_TEXTURE2D_DESC *pDesc) {
133 return pDesc->MipLevels != 0 ? pDesc->MipLevels : _getNumMipLevels(pDesc->Width, pDesc->Height);
137 _getNumMipLevels(const D3D10_TEXTURE3D_DESC *pDesc) {
138 return pDesc->MipLevels != 0 ? pDesc->MipLevels : _getNumMipLevels(pDesc->Width, pDesc->Height, pDesc->Depth);
142 _getNumSubResources(const D3D10_BUFFER_DESC *pDesc) {
147 _getNumSubResources(const D3D10_TEXTURE1D_DESC *pDesc) {
148 return _getNumMipLevels(pDesc) * pDesc->ArraySize;
152 _getNumSubResources(const D3D10_TEXTURE2D_DESC *pDesc) {
153 return _getNumMipLevels(pDesc) * pDesc->ArraySize;
157 _getNumSubResources(const D3D10_TEXTURE3D_DESC *pDesc) {
158 return _getNumMipLevels(pDesc);
162 _calcSubresourceSize(const D3D10_BUFFER_DESC *pDesc, UINT Subresource, UINT RowPitch = 0, UINT SlicePitch = 0) {
163 return pDesc->ByteWidth;
167 _calcSubresourceSize(const D3D10_TEXTURE1D_DESC *pDesc, UINT Subresource, UINT RowPitch = 0, UINT SlicePitch = 0) {
168 UINT MipLevel = Subresource % _getNumMipLevels(pDesc);
169 return _calcMipDataSize(MipLevel, pDesc->Format, pDesc->Width, 1, RowPitch, 1, SlicePitch);
173 _calcSubresourceSize(const D3D10_TEXTURE2D_DESC *pDesc, UINT Subresource, UINT RowPitch, UINT SlicePitch = 0) {
174 UINT MipLevel = Subresource % _getNumMipLevels(pDesc);
175 return _calcMipDataSize(MipLevel, pDesc->Format, pDesc->Width, pDesc->Height, RowPitch, 1, SlicePitch);
179 _calcSubresourceSize(const D3D10_TEXTURE3D_DESC *pDesc, UINT Subresource, UINT RowPitch, UINT SlicePitch) {
180 UINT MipLevel = Subresource;
181 return _calcMipDataSize(MipLevel, pDesc->Format, pDesc->Width, pDesc->Height, RowPitch, pDesc->Depth, SlicePitch);
185 _getMapInfo(ID3D10Buffer *pResource, D3D10_MAP MapType, UINT MapFlags, void * * ppData,
186 void * & pMappedData, size_t & MappedSize) {
190 if (MapType == D3D10_MAP_READ) {
194 D3D10_BUFFER_DESC Desc;
195 pResource->GetDesc(&Desc);
197 pMappedData = *ppData;
198 MappedSize = Desc.ByteWidth;
202 _getMapInfo(ID3D10Texture1D *pResource, UINT Subresource, D3D10_MAP MapType, UINT MapFlags, void * * ppData,
203 void * & pMappedData, size_t & MappedSize) {
207 if (MapType == D3D10_MAP_READ) {
211 D3D10_TEXTURE1D_DESC Desc;
212 pResource->GetDesc(&Desc);
214 pMappedData = *ppData;
215 MappedSize = _calcSubresourceSize(&Desc, Subresource);
219 _getMapInfo(ID3D10Texture2D *pResource, UINT Subresource, D3D10_MAP MapType, UINT MapFlags, D3D10_MAPPED_TEXTURE2D * pMappedTex2D,
220 void * & pMappedData, size_t & MappedSize) {
224 if (MapType == D3D10_MAP_READ) {
228 D3D10_TEXTURE2D_DESC Desc;
229 pResource->GetDesc(&Desc);
231 pMappedData = pMappedTex2D->pData;
232 MappedSize = _calcSubresourceSize(&Desc, Subresource, pMappedTex2D->RowPitch);
236 _getMapInfo(ID3D10Texture3D *pResource, UINT Subresource, D3D10_MAP MapType, UINT MapFlags, D3D10_MAPPED_TEXTURE3D * pMappedTex3D,
237 void * & pMappedData, size_t & MappedSize) {
241 if (MapType == D3D10_MAP_READ) {
245 D3D10_TEXTURE3D_DESC Desc;
246 pResource->GetDesc(&Desc);
248 pMappedData = pMappedTex3D->pData;
249 MappedSize = _calcSubresourceSize(&Desc, Subresource, pMappedTex3D->RowPitch, pMappedTex3D->DepthPitch);
254 _getMapInfo(IDXGISurface *pResource, DXGI_MAPPED_RECT * pLockedRect, UINT MapFlags,
255 void * & pMappedData, size_t & MappedSize) {
259 if (!(MapFlags & DXGI_MAP_WRITE)) {
263 DXGI_SURFACE_DESC Desc;
264 HRESULT hr = pResource->GetDesc(&Desc);
269 pMappedData = pLockedRect->pBits;
270 MappedSize = _calcDataSize(Desc.Format, Desc.Width, Desc.Height, pLockedRect->Pitch);
274 #endif /* _D3D10SIZE_HPP_ */