]> git.cworth.org Git - apitrace/blob - helpers/d3dsize.hpp
Support dumping uniform buffer objects (issue #75).
[apitrace] / helpers / d3dsize.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 _D3D_SIZE_HPP_
34 #define _D3D_SIZE_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 "os.hpp"
43
44
45 static inline size_t
46 _vertexCount(D3DPRIMITIVETYPE PrimitiveType, UINT PrimitiveCount)
47 {
48     switch (PrimitiveType) {
49     case D3DPT_POINTLIST:
50         return PrimitiveCount;
51     case D3DPT_LINELIST:
52         return PrimitiveCount*2;
53     case D3DPT_LINESTRIP:
54         return PrimitiveCount + 1;
55     case D3DPT_TRIANGLELIST:
56         return PrimitiveCount * 3;
57     case D3DPT_TRIANGLESTRIP:
58         return PrimitiveCount + 2;
59     case D3DPT_TRIANGLEFAN:
60         return PrimitiveCount + 1;
61     default:
62         os::log("apitrace: warning: %s: unknown D3DPRIMITIVETYPE %u\n", __FUNCTION__, PrimitiveType);
63         return 0;
64     }
65 }
66
67
68 static inline size_t
69 _vertexDataSize(D3DPRIMITIVETYPE PrimitiveType, UINT PrimitiveCount, UINT VertexStride) {
70     return _vertexCount(PrimitiveType, PrimitiveCount) * VertexStride;
71 }
72
73
74 static inline size_t
75 _indexDataSize(D3DPRIMITIVETYPE PrimitiveType, UINT PrimitiveCount, D3DFORMAT IndexDataFormat) {
76     UINT IndexStride;
77     switch (IndexDataFormat) {
78     case D3DFMT_INDEX16:
79         IndexStride = 2;
80         break;
81     case D3DFMT_INDEX32:
82         IndexStride = 4;
83         break;
84     default:
85         os::log("apitrace: warning: %s: unexpected index D3DFORMAT %u\n", __FUNCTION__, IndexDataFormat);
86         return 0;
87     }
88     return _vertexCount(PrimitiveType, PrimitiveCount) * IndexStride;
89 }
90
91
92 #if DIRECT3D_VERSION >= 0x0800
93
94 /*
95  * Return the number of tokens for a given shader.
96  */
97 static inline size_t
98 _shaderSize(const DWORD *pFunction)
99 {
100     DWORD dwLength = 0;
101
102     while (true) {
103         DWORD dwToken = pFunction[dwLength++];
104
105         switch (dwToken & D3DSI_OPCODE_MASK) {
106         case D3DSIO_COMMENT:
107             dwLength += (dwToken & D3DSI_COMMENTSIZE_MASK) >> D3DSI_COMMENTSIZE_SHIFT;
108             break;
109
110         case D3DSIO_END:
111             if (dwToken != D3DSIO_END) {
112                 os::log("apitrace: warning: %s: malformed END token\n", __FUNCTION__);
113             }
114             return dwLength * sizeof *pFunction;
115         }
116     }
117 }
118
119
120 static size_t
121 _getLockSize(D3DFORMAT Format, UINT Width, UINT Height, INT RowPitch, UINT Depth = 1, INT SlicePitch = 0) {
122     if (Width == 0 || Height == 0 || Depth == 0) {
123         return 0;
124     }
125
126     if (RowPitch < 0) {
127         os::log("apitrace: warning: %s: negative row pitch %i\n", __FUNCTION__, RowPitch);
128         return 0;
129     }
130
131     if (SlicePitch < 0) {
132         os::log("apitrace: warning: %s: negative slice pitch %i\n", __FUNCTION__, SlicePitch);
133         return 0;
134     }
135
136     switch ((DWORD)Format) {
137     case D3DFMT_DXT1:
138     case D3DFMT_DXT2:
139     case D3DFMT_DXT3:
140     case D3DFMT_DXT4:
141     case D3DFMT_DXT5:
142         Width  = (Width  + 3) / 4;
143         Height = (Height + 3) / 4;
144         break;
145
146     case D3DFMT_ATI1N:
147     case D3DFMT_ATI2N:
148         /*
149          * Because these are unsupported formats, RowPitch is not set to the
150          * number of bytes between row of blocks, but instead in such way that
151          * Height * RowPitch will match the expected size.
152          */
153         break;
154
155     case D3DFMT_UYVY:
156     case D3DFMT_R8G8_B8G8:
157     case D3DFMT_YUY2:
158     case D3DFMT_G8R8_G8B8:
159         Width = (Width + 1) / 2;
160         break;
161
162     case D3DFMT_NV12:
163         return (Height + ((Height + 1) / 2)) * RowPitch;
164
165     case D3DFMT_NULL:
166         return 0;
167
168     default:
169         break;
170     }
171
172     (void)Width;
173
174     size_t size = Height * RowPitch;
175
176     if (Depth > 1) {
177         size += (Depth - 1) * SlicePitch;
178     }
179
180     return size;
181 }
182
183
184 #endif /* DIRECT3D_VERSION >= 0x0800 */
185
186
187 #if DIRECT3D_VERSION >= 0x0900
188
189
190 static inline void
191 _getLockInfo(IDirect3DVertexBuffer9 *pBuffer, UINT OffsetToLock, UINT SizeToLock, void ** ppbData,
192              void * & pLockedData, size_t & LockedSize) {
193     pLockedData = *ppbData;
194     LockedSize = 0;
195
196     if (SizeToLock == 0) {
197         D3DVERTEXBUFFER_DESC Desc;
198         HRESULT hr = pBuffer->GetDesc(&Desc);
199         if (FAILED(hr)) {
200             return;
201         }
202         LockedSize = Desc.Size;
203     } else {
204         LockedSize = SizeToLock;
205     }
206 }
207
208
209 static inline void
210 _getLockInfo(IDirect3DIndexBuffer9 *pBuffer, UINT OffsetToLock, UINT SizeToLock, void ** ppbData,
211              void * & pLockedData, size_t & LockedSize) {
212     pLockedData = *ppbData;
213     LockedSize = 0;
214
215     if (SizeToLock == 0) {
216         D3DINDEXBUFFER_DESC Desc;
217         HRESULT hr = pBuffer->GetDesc(&Desc);
218         if (FAILED(hr)) {
219             return;
220         }
221         LockedSize = Desc.Size;
222     } else {
223         LockedSize = SizeToLock;
224     }
225 }
226
227
228 static inline void
229 _getLockInfo(IDirect3DSurface9 *pSurface, const D3DLOCKED_RECT *pLockedRect, const RECT *pRect,
230              void * & pLockedData, size_t & LockedSize) {
231     pLockedData = pLockedRect->pBits;
232     LockedSize = 0;
233
234     HRESULT hr;
235
236     D3DSURFACE_DESC Desc;
237     hr = pSurface->GetDesc(&Desc);
238     if (FAILED(hr)) {
239         return;
240     }
241
242     UINT Width;
243     UINT Height;
244     if (pRect) {
245         Width  = pRect->right  - pRect->left;
246         Height = pRect->bottom - pRect->top;
247     } else {
248         Width  = Desc.Width;
249         Height = Desc.Height;
250     }
251
252     LockedSize = _getLockSize(Desc.Format, Width, Height, pLockedRect->Pitch);
253 }
254
255
256 static inline void
257 _getLockInfo(IDirect3DTexture9 *pTexture, UINT Level, const D3DLOCKED_RECT *pLockedRect, const RECT *pRect,
258              void * & pLockedData, size_t & LockedSize) {
259     pLockedData = pLockedRect->pBits;
260     LockedSize = 0;
261
262     HRESULT hr;
263
264     D3DSURFACE_DESC Desc;
265     hr = pTexture->GetLevelDesc(Level, &Desc);
266     if (FAILED(hr)) {
267         return;
268     }
269
270     UINT Width;
271     UINT Height;
272     if (pRect) {
273         Width  = pRect->right  - pRect->left;
274         Height = pRect->bottom - pRect->top;
275     } else {
276         Width  = Desc.Width;
277         Height = Desc.Height;
278     }
279
280     LockedSize = _getLockSize(Desc.Format, Width, Height, pLockedRect->Pitch);
281 }
282
283
284 static inline void
285 _getLockInfo(IDirect3DCubeTexture9 *pTexture, D3DCUBEMAP_FACES FaceType, UINT Level, const D3DLOCKED_RECT *pLockedRect, const RECT *pRect,
286              void * & pLockedData, size_t & LockedSize) {
287     pLockedData = pLockedRect->pBits;
288     LockedSize = 0;
289
290     HRESULT hr;
291
292     (void)FaceType;
293
294     D3DSURFACE_DESC Desc;
295     hr = pTexture->GetLevelDesc(Level, &Desc);
296     if (FAILED(hr)) {
297         return;
298     }
299
300     UINT Width;
301     UINT Height;
302     if (pRect) {
303         Width  = pRect->right  - pRect->left;
304         Height = pRect->bottom - pRect->top;
305     } else {
306         Width  = Desc.Width;
307         Height = Desc.Height;
308     }
309
310     LockedSize = _getLockSize(Desc.Format, Width, Height, pLockedRect->Pitch);
311 }
312
313
314 static inline void
315 _getLockInfo(IDirect3DVolume9 *pVolume, const D3DLOCKED_BOX *pLockedVolume, const D3DBOX *pBox,
316              void * & pLockedData, size_t & LockedSize) {
317     pLockedData = pLockedVolume->pBits;
318     LockedSize = 0;
319
320     HRESULT hr;
321
322     D3DVOLUME_DESC Desc;
323     hr = pVolume->GetDesc(&Desc);
324     if (FAILED(hr)) {
325         return;
326     }
327
328     UINT Width;
329     UINT Height;
330     UINT Depth;
331     if (pBox) {
332         Width  = pBox->Right  - pBox->Left;
333         Height = pBox->Bottom - pBox->Top;
334         Depth  = pBox->Back   - pBox->Front;
335     } else {
336         Width  = Desc.Width;
337         Height = Desc.Height;
338         Depth  = Desc.Depth;
339     }
340
341     LockedSize = _getLockSize(Desc.Format, Width, Height, pLockedVolume->RowPitch, Depth, pLockedVolume->SlicePitch);
342 }
343
344
345 static inline void
346 _getLockInfo(IDirect3DVolumeTexture9 *pTexture, UINT Level, const D3DLOCKED_BOX *pLockedVolume, const D3DBOX *pBox,
347              void * & pLockedData, size_t & LockedSize) {
348     pLockedData = pLockedVolume->pBits;
349     LockedSize = 0;
350
351     HRESULT hr;
352
353     D3DVOLUME_DESC Desc;
354     hr = pTexture->GetLevelDesc(Level, &Desc);
355     if (FAILED(hr)) {
356         return;
357     }
358
359     UINT Width;
360     UINT Height;
361     UINT Depth;
362     if (pBox) {
363         Width  = pBox->Right  - pBox->Left;
364         Height = pBox->Bottom - pBox->Top;
365         Depth  = pBox->Back   - pBox->Front;
366     } else {
367         Width  = Desc.Width;
368         Height = Desc.Height;
369         Depth  = Desc.Depth;
370     }
371
372     LockedSize = _getLockSize(Desc.Format, Width, Height, pLockedVolume->RowPitch, Depth, pLockedVolume->SlicePitch);
373 }
374
375
376 #endif /* DIRECT3D_VERSION >= 0x0900 */
377
378
379 #endif /* _D3D_SIZE_HPP_ */