]> git.cworth.org Git - apitrace/blob - helpers/d3dsize.hpp
Trace LockBox blobs.
[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     case D3DFMT_ATI1:
143     case D3DFMT_ATI2:
144         Width /= 4;
145         Height /= 4;
146         break;
147
148     case D3DFMT_UYVY:
149     case D3DFMT_R8G8_B8G8:
150     case D3DFMT_YUY2:
151     case D3DFMT_G8R8_G8B8:
152         Width /= 2;
153         break;
154
155     case D3DFMT_NV12:
156         return (Height + ((Height + 1) / 2)) * RowPitch;
157
158     case D3DFMT_NULL:
159         return 0;
160
161     default:
162         break;
163     }
164
165     (void)Width;
166
167     size_t size = Height * RowPitch;
168
169     if (Depth > 1) {
170         size += (Depth - 1) * SlicePitch;
171     }
172
173     return size;
174 }
175
176
177 #endif /* DIRECT3D_VERSION >= 0x0800 */
178
179
180 #if DIRECT3D_VERSION >= 0x0900
181
182
183 static inline size_t
184 _getLockSize(IDirect3DVertexBuffer9 *pBuffer, UINT OffsetToLock, UINT SizeToLock, void ** ppbData) {
185     if (SizeToLock == 0) {
186         D3DVERTEXBUFFER_DESC Desc;
187         HRESULT hr = pBuffer->GetDesc(&Desc);
188         if (FAILED(hr)) {
189             return 0;
190         } 
191         SizeToLock = Desc.Size;
192     }
193
194     return SizeToLock;
195 }
196
197
198 static inline size_t
199 _getLockSize(IDirect3DIndexBuffer9 *pBuffer, UINT OffsetToLock, UINT SizeToLock, void ** ppbData) {
200     if (SizeToLock == 0) {
201         D3DINDEXBUFFER_DESC Desc;
202         HRESULT hr = pBuffer->GetDesc(&Desc);
203         if (FAILED(hr)) {
204             return 0;
205         } 
206         SizeToLock = Desc.Size;
207     }
208
209     return SizeToLock;
210 }
211
212
213 static inline size_t
214 _getLockSize(IDirect3DSurface9 *pSurface, const D3DLOCKED_RECT *pLockedRect, const RECT *pRect) {
215     HRESULT hr;
216
217     D3DSURFACE_DESC Desc;
218     hr = pSurface->GetDesc(&Desc);
219     if (FAILED(hr)) {
220         return 0;
221     } 
222
223     UINT Width;
224     UINT Height;
225     if (pRect) {
226         Width  = pRect->right  - pRect->left;
227         Height = pRect->bottom - pRect->top;
228     } else {
229         Width  = Desc.Width;
230         Height = Desc.Height;
231     }
232
233     return _getLockSize(Desc.Format, Width, Height, pLockedRect->Pitch);
234 }
235
236
237 static inline size_t
238 _getLockSize(IDirect3DTexture9 *pTexture, UINT Level, const D3DLOCKED_RECT *pLockedRect, const RECT *pRect) {
239     HRESULT hr;
240
241     D3DSURFACE_DESC Desc;
242     hr = pTexture->GetLevelDesc(Level, &Desc);
243     if (FAILED(hr)) {
244         return 0;
245     }
246
247     UINT Width;
248     UINT Height;
249     if (pRect) {
250         Width  = pRect->right  - pRect->left;
251         Height = pRect->bottom - pRect->top;
252     } else {
253         Width  = Desc.Width;
254         Height = Desc.Height;
255     }
256
257     return _getLockSize(Desc.Format, Width, Height, pLockedRect->Pitch);
258 }
259
260
261 static inline size_t
262 _getLockSize(IDirect3DCubeTexture9 *pTexture, D3DCUBEMAP_FACES FaceType, UINT Level, const D3DLOCKED_RECT *pLockedRect, const RECT *pRect) {
263     HRESULT hr;
264
265     (void)FaceType;
266
267     D3DSURFACE_DESC Desc;
268     hr = pTexture->GetLevelDesc(Level, &Desc);
269     if (FAILED(hr)) {
270         return 0;
271     }
272
273     UINT Width;
274     UINT Height;
275     if (pRect) {
276         Width  = pRect->right  - pRect->left;
277         Height = pRect->bottom - pRect->top;
278     } else {
279         Width  = Desc.Width;
280         Height = Desc.Height;
281     }
282
283     return _getLockSize(Desc.Format, Width, Height, pLockedRect->Pitch);
284 }
285
286
287 static inline size_t
288 _getLockSize(IDirect3DVolume9 *pVolume, const D3DLOCKED_BOX *pLockedBox, const D3DBOX *pBox) {
289     HRESULT hr;
290
291     D3DVOLUME_DESC Desc;
292     hr = pVolume->GetDesc(&Desc);
293     if (FAILED(hr)) {
294         return 0;
295     }
296
297     UINT Width;
298     UINT Height;
299     UINT Depth;
300     if (pBox) {
301         Width  = pBox->Right  - pBox->Left;
302         Height = pBox->Bottom - pBox->Top;
303         Depth  = pBox->Back   - pBox->Front;
304     } else {
305         Width  = Desc.Width;
306         Height = Desc.Height;
307         Depth  = Desc.Depth;
308     }
309
310     return _getLockSize(Desc.Format, Width, Height, pLockedBox->RowPitch, Depth, pLockedBox->SlicePitch);
311 }
312
313
314 static inline size_t
315 _getLockSize(IDirect3DVolumeTexture9 *pTexture, UINT Level, const D3DLOCKED_BOX *pLockedBox, const D3DBOX *pBox) {
316     HRESULT hr;
317
318     D3DVOLUME_DESC Desc;
319     hr = pTexture->GetLevelDesc(Level, &Desc);
320     if (FAILED(hr)) {
321         return 0;
322     }
323
324     UINT Width;
325     UINT Height;
326     UINT Depth;
327     if (pBox) {
328         Width  = pBox->Right  - pBox->Left;
329         Height = pBox->Bottom - pBox->Top;
330         Depth  = pBox->Back   - pBox->Front;
331     } else {
332         Width  = Desc.Width;
333         Height = Desc.Height;
334         Depth  = Desc.Depth;
335     }
336
337     return _getLockSize(Desc.Format, Width, Height, pLockedBox->RowPitch, Depth, pLockedBox->SlicePitch);
338 }
339
340
341 #endif /* DIRECT3D_VERSION >= 0x0900 */
342
343
344 #endif /* _D3D_SIZE_HPP_ */