]> git.cworth.org Git - apitrace/blob - helpers/d3d11size.hpp
Use skiplist-based FastCallSet within trace::CallSet
[apitrace] / helpers / d3d11size.hpp
1 /**************************************************************************
2  *
3  * Copyright 2012 Jose Fonseca
4  * All Rights Reserved.
5  * 
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:
12  * 
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
15  * Software.
16  * 
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
20  * AUTHORS,
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
24  * SOFTWARE.
25  *
26  **************************************************************************/
27
28
29 /*
30  * Auxiliary functions to compute the size of array/blob arguments.
31  */
32
33 #ifndef _D3D11SIZE_HPP_
34 #define _D3D11SIZE_HPP_
35
36
37 /* We purposedly don't include any D3D header, so that this header can be used
38  * with all D3D versions. */
39
40 #include <assert.h>
41
42 #include <algorithm>
43
44 #include "dxgisize.hpp"
45
46
47 inline UINT
48 _getNumMipLevels(const D3D11_BUFFER_DESC *pDesc) {
49     return 1;
50 }
51
52 inline UINT
53 _getNumMipLevels(const D3D11_TEXTURE1D_DESC *pDesc) {
54     return pDesc->MipLevels != 0 ? pDesc->MipLevels : _getNumMipLevels(pDesc->Width);
55 }
56
57 inline UINT
58 _getNumMipLevels(const D3D11_TEXTURE2D_DESC *pDesc) {
59     return pDesc->MipLevels != 0 ? pDesc->MipLevels : _getNumMipLevels(pDesc->Width, pDesc->Height);
60 }
61
62 inline UINT
63 _getNumMipLevels(const D3D11_TEXTURE3D_DESC *pDesc) {
64     return pDesc->MipLevels != 0 ? pDesc->MipLevels : _getNumMipLevels(pDesc->Width, pDesc->Height, pDesc->Depth);
65 }
66
67 inline UINT
68 _getNumSubResources(const D3D11_BUFFER_DESC *pDesc) {
69     return 1;
70 }
71
72 inline UINT
73 _getNumSubResources(const D3D11_TEXTURE1D_DESC *pDesc) {
74     return _getNumMipLevels(pDesc) * pDesc->ArraySize;
75 }
76
77 inline UINT
78 _getNumSubResources(const D3D11_TEXTURE2D_DESC *pDesc) {
79     return _getNumMipLevels(pDesc) * pDesc->ArraySize;
80 }
81
82 inline UINT
83 _getNumSubResources(const D3D11_TEXTURE3D_DESC *pDesc) {
84     return _getNumMipLevels(pDesc);
85 }
86
87 static inline size_t
88 _calcSubresourceSize(const D3D11_BUFFER_DESC *pDesc, UINT Subresource, UINT RowPitch = 0, UINT SlicePitch = 0) {
89     return pDesc->ByteWidth;
90 }
91
92 static inline size_t
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);
96 }
97
98 static inline size_t
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);
102 }
103
104 static inline size_t
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);
108 }
109
110 static inline size_t
111 _calcSubresourceSize(ID3D11Resource *pDstResource, UINT DstSubresource, const D3D11_BOX *pDstBox, UINT SrcRowPitch, UINT SrcDepthPitch) {
112     if (pDstBox &&
113         (pDstBox->left  >= pDstBox->right ||
114          pDstBox->top   >= pDstBox->bottom ||
115          pDstBox->front >= pDstBox->back)) {
116         return 0;
117     }
118
119     D3D11_RESOURCE_DIMENSION Type = D3D11_RESOURCE_DIMENSION_UNKNOWN;
120     pDstResource->GetType(&Type);
121
122     DXGI_FORMAT Format;
123     UINT Width;
124     UINT Height = 1;
125     UINT Depth = 1;
126     UINT MipLevel = 0;
127
128     switch (Type) {
129     case D3D11_RESOURCE_DIMENSION_BUFFER:
130         if (pDstBox) {
131             return pDstBox->right - pDstBox->left;
132         } else {
133             D3D11_BUFFER_DESC Desc;
134             static_cast<ID3D11Buffer *>(pDstResource)->GetDesc(&Desc);
135             return Desc.ByteWidth;
136         }
137     case D3D11_RESOURCE_DIMENSION_TEXTURE1D:
138         {
139             D3D11_TEXTURE1D_DESC Desc;
140             static_cast<ID3D11Texture1D *>(pDstResource)->GetDesc(&Desc);
141             Format = Desc.Format;
142             Width = Desc.Width;
143             MipLevel = DstSubresource % Desc.MipLevels;
144         }
145         break;
146     case D3D11_RESOURCE_DIMENSION_TEXTURE2D:
147         {
148             D3D11_TEXTURE2D_DESC Desc;
149             static_cast<ID3D11Texture2D *>(pDstResource)->GetDesc(&Desc);
150             Format = Desc.Format;
151             Width = Desc.Width;
152             Height = Desc.Height;
153             MipLevel = DstSubresource % Desc.MipLevels;
154         }
155         break;
156     case D3D11_RESOURCE_DIMENSION_TEXTURE3D:
157         {
158             D3D11_TEXTURE3D_DESC Desc;
159             static_cast<ID3D11Texture3D *>(pDstResource)->GetDesc(&Desc);
160             Format = Desc.Format;
161             Width = Desc.Width;
162             Height = Desc.Height;
163             Depth = Desc.Depth;
164         }
165         break;
166     case D3D11_RESOURCE_DIMENSION_UNKNOWN:
167     default:
168         assert(0);
169         return 0;
170     }
171
172     if (pDstBox) {
173         Width  = pDstBox->right  - pDstBox->left;
174         Height = pDstBox->bottom - pDstBox->top;
175         Depth  = pDstBox->back   - pDstBox->front;
176     }
177
178     return _calcMipDataSize(MipLevel, Format, Width, Height, SrcRowPitch, Depth, SrcDepthPitch);
179 }
180
181
182 static inline void
183 _getMapInfo(ID3D11DeviceContext* pContext, ID3D11Resource * pResource, UINT Subresource, D3D11_MAP MapType, UINT MapFlags, D3D11_MAPPED_SUBRESOURCE * pMappedResource,
184             void * & pMappedData, size_t & MappedSize) {
185     pMappedData = 0;
186     MappedSize = 0;
187
188     if (MapType == D3D11_MAP_READ) {
189         return;
190     }
191
192     pMappedData = pMappedResource->pData;
193     MappedSize = _calcSubresourceSize(pResource, Subresource, NULL, pMappedResource->RowPitch, pMappedResource->DepthPitch);
194 }
195
196
197 #endif /* _D3D11SIZE_HPP_ */