]> git.cworth.org Git - apitrace/blob - helpers/d3d10size.hpp
d3d10: Trace blobs for initial data.
[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
45 static size_t
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) {
48         return 0;
49     }
50
51     switch (Format) {
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;
69         break;
70
71     case DXGI_FORMAT_R8G8_B8G8_UNORM:
72     case DXGI_FORMAT_G8R8_G8B8_UNORM:
73         Width = (Width + 1) / 2;
74         break;
75
76     case DXGI_FORMAT_UNKNOWN:
77         return 0;
78
79     default:
80         break;
81     }
82
83     /* FIXME */
84     (void)Width;
85
86     size_t size = Height * RowPitch;
87
88     if (Depth > 1) {
89         size += (Depth - 1) * DepthPitch;
90     }
91
92     return size;
93 }
94
95 static size_t
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) {
98         return 0;
99     }
100
101     Width  = std::max(Width  >> MipLevel, UINT(1));
102     Height = std::max(Height >> MipLevel, UINT(1));
103     Depth  = std::max(Depth  >> MipLevel, UINT(1));
104
105     return _calcDataSize(Format, Width, Height, RowPitch, Depth, DepthPitch);
106 }
107
108
109 inline UINT
110 _getNumMipLevels(UINT Width, UINT Height = 1, UINT Depth = 1) {
111     UINT MipLevels = 0;
112     do {
113         ++MipLevels;
114         Width  >>= 1;
115         Height >>= 1;
116         Depth  >>= 1;
117     } while (Width || Height || Depth);
118     return MipLevels;
119 }
120
121 inline UINT
122 _getNumMipLevels(const D3D10_BUFFER_DESC *pDesc) {
123     return 1;
124 }
125
126 inline UINT
127 _getNumMipLevels(const D3D10_TEXTURE1D_DESC *pDesc) {
128     return pDesc->MipLevels != 0 ? pDesc->MipLevels : _getNumMipLevels(pDesc->Width);
129 }
130
131 inline UINT
132 _getNumMipLevels(const D3D10_TEXTURE2D_DESC *pDesc) {
133     return pDesc->MipLevels != 0 ? pDesc->MipLevels : _getNumMipLevels(pDesc->Width, pDesc->Height);
134 }
135
136 inline UINT
137 _getNumMipLevels(const D3D10_TEXTURE3D_DESC *pDesc) {
138     return pDesc->MipLevels != 0 ? pDesc->MipLevels : _getNumMipLevels(pDesc->Width, pDesc->Height, pDesc->Depth);
139 }
140
141 inline UINT
142 _getNumSubResources(const D3D10_BUFFER_DESC *pDesc) {
143     return 1;
144 }
145
146 inline UINT
147 _getNumSubResources(const D3D10_TEXTURE1D_DESC *pDesc) {
148     return _getNumMipLevels(pDesc) * pDesc->ArraySize;
149 }
150
151 inline UINT
152 _getNumSubResources(const D3D10_TEXTURE2D_DESC *pDesc) {
153     return _getNumMipLevels(pDesc) * pDesc->ArraySize;
154 }
155
156 inline UINT
157 _getNumSubResources(const D3D10_TEXTURE3D_DESC *pDesc) {
158     return _getNumMipLevels(pDesc);
159 }
160
161 static inline size_t
162 _calcSubresourceSize(const D3D10_BUFFER_DESC *pDesc, UINT Subresource, UINT RowPitch = 0, UINT SlicePitch = 0) {
163     return pDesc->ByteWidth;
164 }
165
166 static inline size_t
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);
170 }
171
172 static inline size_t
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);
176 }
177
178 static inline size_t
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);
182 }
183
184 static inline void
185 _getMapInfo(ID3D10Buffer *pResource, D3D10_MAP MapType, UINT MapFlags, void * * ppData,
186             void * & pMappedData, size_t & MappedSize) {
187     pMappedData = 0;
188     MappedSize = 0;
189
190     if (MapType == D3D10_MAP_READ) {
191         return;
192     }
193
194     D3D10_BUFFER_DESC Desc;
195     pResource->GetDesc(&Desc);
196
197     pMappedData = *ppData;
198     MappedSize = Desc.ByteWidth;
199 }
200
201 static inline void
202 _getMapInfo(ID3D10Texture1D *pResource, UINT Subresource, D3D10_MAP MapType, UINT MapFlags, void * * ppData,
203             void * & pMappedData, size_t & MappedSize) {
204     pMappedData = 0;
205     MappedSize = 0;
206
207     if (MapType == D3D10_MAP_READ) {
208         return;
209     }
210
211     D3D10_TEXTURE1D_DESC Desc;
212     pResource->GetDesc(&Desc);
213
214     pMappedData = *ppData;
215     MappedSize = _calcSubresourceSize(&Desc, Subresource);
216 }
217
218 static inline void
219 _getMapInfo(ID3D10Texture2D *pResource, UINT Subresource, D3D10_MAP MapType, UINT MapFlags, D3D10_MAPPED_TEXTURE2D * pMappedTex2D,
220             void * & pMappedData, size_t & MappedSize) {
221     pMappedData = 0;
222     MappedSize = 0;
223
224     if (MapType == D3D10_MAP_READ) {
225         return;
226     }
227
228     D3D10_TEXTURE2D_DESC Desc;
229     pResource->GetDesc(&Desc);
230
231     pMappedData = pMappedTex2D->pData;
232     MappedSize = _calcSubresourceSize(&Desc, Subresource, pMappedTex2D->RowPitch);
233 }
234
235 static inline void
236 _getMapInfo(ID3D10Texture3D *pResource, UINT Subresource, D3D10_MAP MapType, UINT MapFlags, D3D10_MAPPED_TEXTURE3D * pMappedTex3D,
237             void * & pMappedData, size_t & MappedSize) {
238     pMappedData = 0;
239     MappedSize = 0;
240
241     if (MapType == D3D10_MAP_READ) {
242         return;
243     }
244
245     D3D10_TEXTURE3D_DESC Desc;
246     pResource->GetDesc(&Desc);
247
248     pMappedData = pMappedTex3D->pData;
249     MappedSize = _calcSubresourceSize(&Desc, Subresource, pMappedTex3D->RowPitch, pMappedTex3D->DepthPitch);
250 }
251
252
253 static inline void
254 _getMapInfo(IDXGISurface *pResource, DXGI_MAPPED_RECT * pLockedRect, UINT MapFlags,
255             void * & pMappedData, size_t & MappedSize) {
256     pMappedData = 0;
257     MappedSize = 0;
258
259     if (!(MapFlags & DXGI_MAP_WRITE)) {
260         return;
261     }
262
263     DXGI_SURFACE_DESC Desc;
264     HRESULT hr = pResource->GetDesc(&Desc);
265     if (FAILED(hr)) {
266         return;
267     }
268
269     pMappedData = pLockedRect->pBits;
270     MappedSize = _calcDataSize(Desc.Format, Desc.Width, Desc.Height, pLockedRect->Pitch);
271 }
272
273
274 #endif /* _D3D10SIZE_HPP_ */