]> git.cworth.org Git - apitrace/blob - helpers/d3d10size.hpp
Use skiplist-based FastCallSet within trace::CallSet
[apitrace] / helpers / d3d10size.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 _D3D10SIZE_HPP_
34 #define _D3D10SIZE_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 D3D10_BUFFER_DESC *pDesc) {
49     return 1;
50 }
51
52 inline UINT
53 _getNumMipLevels(const D3D10_TEXTURE1D_DESC *pDesc) {
54     return pDesc->MipLevels != 0 ? pDesc->MipLevels : _getNumMipLevels(pDesc->Width);
55 }
56
57 inline UINT
58 _getNumMipLevels(const D3D10_TEXTURE2D_DESC *pDesc) {
59     return pDesc->MipLevels != 0 ? pDesc->MipLevels : _getNumMipLevels(pDesc->Width, pDesc->Height);
60 }
61
62 inline UINT
63 _getNumMipLevels(const D3D10_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 D3D10_BUFFER_DESC *pDesc) {
69     return 1;
70 }
71
72 inline UINT
73 _getNumSubResources(const D3D10_TEXTURE1D_DESC *pDesc) {
74     return _getNumMipLevels(pDesc) * pDesc->ArraySize;
75 }
76
77 inline UINT
78 _getNumSubResources(const D3D10_TEXTURE2D_DESC *pDesc) {
79     return _getNumMipLevels(pDesc) * pDesc->ArraySize;
80 }
81
82 inline UINT
83 _getNumSubResources(const D3D10_TEXTURE3D_DESC *pDesc) {
84     return _getNumMipLevels(pDesc);
85 }
86
87 static inline size_t
88 _calcSubresourceSize(const D3D10_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 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);
96 }
97
98 static inline size_t
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);
102 }
103
104 static inline size_t
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);
108 }
109
110 static inline void
111 _getMapInfo(ID3D10Buffer *pResource, D3D10_MAP MapType, UINT MapFlags, void * * ppData,
112             void * & pMappedData, size_t & MappedSize) {
113     pMappedData = 0;
114     MappedSize = 0;
115
116     if (MapType == D3D10_MAP_READ) {
117         return;
118     }
119
120     D3D10_BUFFER_DESC Desc;
121     pResource->GetDesc(&Desc);
122
123     pMappedData = *ppData;
124     MappedSize = Desc.ByteWidth;
125 }
126
127 static inline void
128 _getMapInfo(ID3D10Texture1D *pResource, UINT Subresource, D3D10_MAP MapType, UINT MapFlags, void * * ppData,
129             void * & pMappedData, size_t & MappedSize) {
130     pMappedData = 0;
131     MappedSize = 0;
132
133     if (MapType == D3D10_MAP_READ) {
134         return;
135     }
136
137     D3D10_TEXTURE1D_DESC Desc;
138     pResource->GetDesc(&Desc);
139
140     pMappedData = *ppData;
141     MappedSize = _calcSubresourceSize(&Desc, Subresource);
142 }
143
144 static inline void
145 _getMapInfo(ID3D10Texture2D *pResource, UINT Subresource, D3D10_MAP MapType, UINT MapFlags, D3D10_MAPPED_TEXTURE2D * pMappedTex2D,
146             void * & pMappedData, size_t & MappedSize) {
147     pMappedData = 0;
148     MappedSize = 0;
149
150     if (MapType == D3D10_MAP_READ) {
151         return;
152     }
153
154     D3D10_TEXTURE2D_DESC Desc;
155     pResource->GetDesc(&Desc);
156
157     pMappedData = pMappedTex2D->pData;
158     MappedSize = _calcSubresourceSize(&Desc, Subresource, pMappedTex2D->RowPitch);
159 }
160
161 static inline void
162 _getMapInfo(ID3D10Texture3D *pResource, UINT Subresource, D3D10_MAP MapType, UINT MapFlags, D3D10_MAPPED_TEXTURE3D * pMappedTex3D,
163             void * & pMappedData, size_t & MappedSize) {
164     pMappedData = 0;
165     MappedSize = 0;
166
167     if (MapType == D3D10_MAP_READ) {
168         return;
169     }
170
171     D3D10_TEXTURE3D_DESC Desc;
172     pResource->GetDesc(&Desc);
173
174     pMappedData = pMappedTex3D->pData;
175     MappedSize = _calcSubresourceSize(&Desc, Subresource, pMappedTex3D->RowPitch, pMappedTex3D->DepthPitch);
176 }
177
178
179 static inline size_t
180 _calcSubresourceSize(ID3D10Resource *pDstResource, UINT DstSubresource, const D3D10_BOX *pDstBox, UINT SrcRowPitch, UINT SrcDepthPitch) {
181     if (pDstBox &&
182         (pDstBox->left  >= pDstBox->right ||
183          pDstBox->top   >= pDstBox->bottom ||
184          pDstBox->front >= pDstBox->back)) {
185         return 0;
186     }
187
188     D3D10_RESOURCE_DIMENSION Type = D3D10_RESOURCE_DIMENSION_UNKNOWN;
189     pDstResource->GetType(&Type);
190
191     DXGI_FORMAT Format;
192     UINT Width;
193     UINT Height = 1;
194     UINT Depth = 1;
195     UINT MipLevel = 0;
196
197     switch (Type) {
198     case D3D10_RESOURCE_DIMENSION_BUFFER:
199         if (pDstBox) {
200             return pDstBox->right - pDstBox->left;
201         } else {
202             D3D10_BUFFER_DESC Desc;
203             static_cast<ID3D10Buffer *>(pDstResource)->GetDesc(&Desc);
204             return Desc.ByteWidth;
205         }
206     case D3D10_RESOURCE_DIMENSION_TEXTURE1D:
207         {
208             D3D10_TEXTURE1D_DESC Desc;
209             static_cast<ID3D10Texture1D *>(pDstResource)->GetDesc(&Desc);
210             Format = Desc.Format;
211             Width = Desc.Width;
212             MipLevel = DstSubresource % Desc.MipLevels;
213         }
214         break;
215     case D3D10_RESOURCE_DIMENSION_TEXTURE2D:
216         {
217             D3D10_TEXTURE2D_DESC Desc;
218             static_cast<ID3D10Texture2D *>(pDstResource)->GetDesc(&Desc);
219             Format = Desc.Format;
220             Width = Desc.Width;
221             Height = Desc.Height;
222             MipLevel = DstSubresource % Desc.MipLevels;
223         }
224         break;
225     case D3D10_RESOURCE_DIMENSION_TEXTURE3D:
226         {
227             D3D10_TEXTURE3D_DESC Desc;
228             static_cast<ID3D10Texture3D *>(pDstResource)->GetDesc(&Desc);
229             Format = Desc.Format;
230             Width = Desc.Width;
231             Height = Desc.Height;
232             Depth = Desc.Depth;
233         }
234         break;
235     case D3D10_RESOURCE_DIMENSION_UNKNOWN:
236     default:
237         assert(0);
238         return 0;
239     }
240
241     if (pDstBox) {
242         Width  = pDstBox->right  - pDstBox->left;
243         Height = pDstBox->bottom - pDstBox->top;
244         Depth  = pDstBox->back   - pDstBox->front;
245     }
246
247     return _calcMipDataSize(MipLevel, Format, Width, Height, SrcRowPitch, Depth, SrcDepthPitch);
248 }
249
250
251 #endif /* _D3D10SIZE_HPP_ */