]> git.cworth.org Git - apitrace/blob - helpers/dxgisize.hpp
Use skiplist-based FastCallSet within trace::CallSet
[apitrace] / helpers / dxgisize.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 _DXGISIZE_HPP_
34 #define _DXGISIZE_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 "os.hpp"
45
46
47 static size_t
48 _calcDataSize(DXGI_FORMAT Format, UINT Width, UINT Height, UINT RowPitch, UINT Depth = 1, UINT DepthPitch = 0) {
49     if (Width == 0 || Height == 0 || Depth == 0) {
50         return 0;
51     }
52
53     size_t BlockSize = 0;
54     UINT BlockWidth = 1;
55     UINT BlockHeight = 1;
56     switch (Format) {
57     case DXGI_FORMAT_UNKNOWN:
58         return 0;
59     case DXGI_FORMAT_R32G32B32A32_TYPELESS:
60     case DXGI_FORMAT_R32G32B32A32_FLOAT:
61     case DXGI_FORMAT_R32G32B32A32_UINT:
62     case DXGI_FORMAT_R32G32B32A32_SINT:
63         BlockSize = 128;
64         break;
65     case DXGI_FORMAT_R32G32B32_TYPELESS:
66     case DXGI_FORMAT_R32G32B32_FLOAT:
67     case DXGI_FORMAT_R32G32B32_UINT:
68     case DXGI_FORMAT_R32G32B32_SINT:
69         BlockSize = 96;
70         break;
71     case DXGI_FORMAT_R16G16B16A16_TYPELESS:
72     case DXGI_FORMAT_R16G16B16A16_FLOAT:
73     case DXGI_FORMAT_R16G16B16A16_UNORM:
74     case DXGI_FORMAT_R16G16B16A16_UINT:
75     case DXGI_FORMAT_R16G16B16A16_SNORM:
76     case DXGI_FORMAT_R16G16B16A16_SINT:
77     case DXGI_FORMAT_R32G32_TYPELESS:
78     case DXGI_FORMAT_R32G32_FLOAT:
79     case DXGI_FORMAT_R32G32_UINT:
80     case DXGI_FORMAT_R32G32_SINT:
81     case DXGI_FORMAT_R32G8X24_TYPELESS:
82     case DXGI_FORMAT_D32_FLOAT_S8X24_UINT:
83     case DXGI_FORMAT_R32_FLOAT_X8X24_TYPELESS:
84     case DXGI_FORMAT_X32_TYPELESS_G8X24_UINT:
85         BlockSize = 64;
86         break;
87     case DXGI_FORMAT_R10G10B10A2_TYPELESS:
88     case DXGI_FORMAT_R10G10B10A2_UNORM:
89     case DXGI_FORMAT_R10G10B10A2_UINT:
90     case DXGI_FORMAT_R11G11B10_FLOAT:
91     case DXGI_FORMAT_R8G8B8A8_TYPELESS:
92     case DXGI_FORMAT_R8G8B8A8_UNORM:
93     case DXGI_FORMAT_R8G8B8A8_UNORM_SRGB:
94     case DXGI_FORMAT_R8G8B8A8_UINT:
95     case DXGI_FORMAT_R8G8B8A8_SNORM:
96     case DXGI_FORMAT_R8G8B8A8_SINT:
97     case DXGI_FORMAT_R16G16_TYPELESS:
98     case DXGI_FORMAT_R16G16_FLOAT:
99     case DXGI_FORMAT_R16G16_UNORM:
100     case DXGI_FORMAT_R16G16_UINT:
101     case DXGI_FORMAT_R16G16_SNORM:
102     case DXGI_FORMAT_R16G16_SINT:
103     case DXGI_FORMAT_R32_TYPELESS:
104     case DXGI_FORMAT_D32_FLOAT:
105     case DXGI_FORMAT_R32_FLOAT:
106     case DXGI_FORMAT_R32_UINT:
107     case DXGI_FORMAT_R32_SINT:
108     case DXGI_FORMAT_R24G8_TYPELESS:
109     case DXGI_FORMAT_D24_UNORM_S8_UINT:
110     case DXGI_FORMAT_R24_UNORM_X8_TYPELESS:
111     case DXGI_FORMAT_X24_TYPELESS_G8_UINT:
112     case DXGI_FORMAT_R9G9B9E5_SHAREDEXP:
113     case DXGI_FORMAT_B8G8R8A8_UNORM:
114     case DXGI_FORMAT_B8G8R8X8_UNORM:
115     case DXGI_FORMAT_R10G10B10_XR_BIAS_A2_UNORM:
116     case DXGI_FORMAT_B8G8R8A8_TYPELESS:
117     case DXGI_FORMAT_B8G8R8A8_UNORM_SRGB:
118     case DXGI_FORMAT_B8G8R8X8_TYPELESS:
119     case DXGI_FORMAT_B8G8R8X8_UNORM_SRGB:
120         BlockSize = 32;
121         break;
122     case DXGI_FORMAT_R8G8_TYPELESS:
123     case DXGI_FORMAT_R8G8_UNORM:
124     case DXGI_FORMAT_R8G8_UINT:
125     case DXGI_FORMAT_R8G8_SNORM:
126     case DXGI_FORMAT_R8G8_SINT:
127     case DXGI_FORMAT_R16_TYPELESS:
128     case DXGI_FORMAT_R16_FLOAT:
129     case DXGI_FORMAT_D16_UNORM:
130     case DXGI_FORMAT_R16_UNORM:
131     case DXGI_FORMAT_R16_UINT:
132     case DXGI_FORMAT_R16_SNORM:
133     case DXGI_FORMAT_R16_SINT:
134     case DXGI_FORMAT_B5G6R5_UNORM:
135     case DXGI_FORMAT_B5G5R5A1_UNORM:
136         BlockSize = 16;
137         break;
138     case DXGI_FORMAT_R8_TYPELESS:
139     case DXGI_FORMAT_R8_UNORM:
140     case DXGI_FORMAT_R8_UINT:
141     case DXGI_FORMAT_R8_SNORM:
142     case DXGI_FORMAT_R8_SINT:
143     case DXGI_FORMAT_A8_UNORM:
144         BlockSize = 8;
145         break;
146     case DXGI_FORMAT_R1_UNORM:
147         BlockSize = 1;
148         break;
149     case DXGI_FORMAT_R8G8_B8G8_UNORM:
150     case DXGI_FORMAT_G8R8_G8B8_UNORM:
151         BlockSize = 32;
152         BlockWidth = 2;
153         break;
154     case DXGI_FORMAT_BC1_TYPELESS:
155     case DXGI_FORMAT_BC1_UNORM:
156     case DXGI_FORMAT_BC1_UNORM_SRGB:
157     case DXGI_FORMAT_BC4_TYPELESS:
158     case DXGI_FORMAT_BC4_UNORM:
159     case DXGI_FORMAT_BC4_SNORM:
160         BlockSize = 64;
161         BlockWidth = 4;
162         BlockHeight = 4;
163         break;
164     case DXGI_FORMAT_BC2_TYPELESS:
165     case DXGI_FORMAT_BC2_UNORM:
166     case DXGI_FORMAT_BC2_UNORM_SRGB:
167     case DXGI_FORMAT_BC3_TYPELESS:
168     case DXGI_FORMAT_BC3_UNORM:
169     case DXGI_FORMAT_BC3_UNORM_SRGB:
170     case DXGI_FORMAT_BC5_TYPELESS:
171     case DXGI_FORMAT_BC5_UNORM:
172     case DXGI_FORMAT_BC5_SNORM:
173     case DXGI_FORMAT_BC6H_TYPELESS:
174     case DXGI_FORMAT_BC6H_UF16:
175     case DXGI_FORMAT_BC6H_SF16:
176     case DXGI_FORMAT_BC7_TYPELESS:
177     case DXGI_FORMAT_BC7_UNORM:
178     case DXGI_FORMAT_BC7_UNORM_SRGB:
179         BlockSize = 128;
180         BlockWidth = 4;
181         BlockHeight = 4;
182         break;
183     default:
184         os::log("apitrace: warning: %s: unknown format 0x%04X\n", __FUNCTION__, Format);
185         return 0;
186     }
187
188     Width  = (Width  + BlockWidth - 1) / BlockWidth;
189     Height = (Height + BlockHeight - 1) / BlockHeight;
190
191     size_t size = (Width * BlockSize + 7)/ 8;
192
193     if (Height > 1) {
194         size += (Height - 1) * RowPitch;
195     }
196
197     if (Depth > 1) {
198         size += (Depth - 1) * DepthPitch;
199     }
200
201     return size;
202 }
203
204 static size_t
205 _calcMipDataSize(UINT MipLevel, DXGI_FORMAT Format, UINT Width, UINT Height, UINT RowPitch, UINT Depth = 1, UINT DepthPitch = 0) {
206     if (Width == 0 || Height == 0 || Depth == 0) {
207         return 0;
208     }
209
210     Width  = std::max(Width  >> MipLevel, UINT(1));
211     Height = std::max(Height >> MipLevel, UINT(1));
212     Depth  = std::max(Depth  >> MipLevel, UINT(1));
213
214     return _calcDataSize(Format, Width, Height, RowPitch, Depth, DepthPitch);
215 }
216
217
218 inline UINT
219 _getNumMipLevels(UINT Width, UINT Height = 1, UINT Depth = 1) {
220     UINT MipLevels = 0;
221     do {
222         ++MipLevels;
223         Width  >>= 1;
224         Height >>= 1;
225         Depth  >>= 1;
226     } while (Width || Height || Depth);
227     return MipLevels;
228 }
229
230
231 static inline void
232 _getMapInfo(IDXGISurface *pResource, DXGI_MAPPED_RECT * pLockedRect, UINT MapFlags,
233             void * & pMappedData, size_t & MappedSize) {
234     pMappedData = 0;
235     MappedSize = 0;
236
237     if (!(MapFlags & DXGI_MAP_WRITE)) {
238         return;
239     }
240
241     DXGI_SURFACE_DESC Desc;
242     HRESULT hr = pResource->GetDesc(&Desc);
243     if (FAILED(hr)) {
244         return;
245     }
246
247     pMappedData = pLockedRect->pBits;
248     MappedSize = _calcDataSize(Desc.Format, Desc.Width, Desc.Height, pLockedRect->Pitch);
249 }
250
251
252 #endif /* _DXGISIZE_HPP_ */