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