1 /**************************************************************************
3 * Copyright 2012 Jose Fonseca
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:
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
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
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
26 **************************************************************************/
30 * Auxiliary functions to compute the size of array/blob arguments.
33 #ifndef _D3D_SIZE_HPP_
34 #define _D3D_SIZE_HPP_
37 /* We purposedly don't include any D3D header, so that this header can be used
38 * with all D3D versions. */
46 _vertexCount(D3DPRIMITIVETYPE PrimitiveType, UINT PrimitiveCount)
48 switch (PrimitiveType) {
50 return PrimitiveCount;
52 return PrimitiveCount*2;
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;
62 os::log("apitrace: warning: %s: unknown D3DPRIMITIVETYPE %u\n", __FUNCTION__, PrimitiveType);
69 _vertexDataSize(D3DPRIMITIVETYPE PrimitiveType, UINT PrimitiveCount, UINT VertexStride) {
70 return _vertexCount(PrimitiveType, PrimitiveCount) * VertexStride;
75 _indexDataSize(D3DPRIMITIVETYPE PrimitiveType, UINT PrimitiveCount, D3DFORMAT IndexDataFormat) {
77 switch (IndexDataFormat) {
85 os::log("apitrace: warning: %s: unexpected index D3DFORMAT %u\n", __FUNCTION__, IndexDataFormat);
88 return _vertexCount(PrimitiveType, PrimitiveCount) * IndexStride;
92 #if DIRECT3D_VERSION >= 0x0800
95 * Return the number of tokens for a given shader.
98 _shaderSize(const DWORD *pFunction)
103 DWORD dwToken = pFunction[dwLength++];
105 switch (dwToken & D3DSI_OPCODE_MASK) {
107 dwLength += (dwToken & D3DSI_COMMENTSIZE_MASK) >> D3DSI_COMMENTSIZE_SHIFT;
111 if (dwToken != D3DSIO_END) {
112 os::log("apitrace: warning: %s: malformed END token\n", __FUNCTION__);
114 return dwLength * sizeof *pFunction;
121 _getLockSize(D3DFORMAT Format, UINT Width, UINT Height, INT RowPitch, UINT Depth = 1, INT SlicePitch = 0) {
122 if (Width == 0 || Height == 0 || Depth == 0) {
127 os::log("apitrace: warning: %s: negative row pitch %i\n", __FUNCTION__, RowPitch);
131 if (SlicePitch < 0) {
132 os::log("apitrace: warning: %s: negative slice pitch %i\n", __FUNCTION__, SlicePitch);
136 switch ((DWORD)Format) {
142 Width = (Width + 3) / 4;
143 Height = (Height + 3) / 4;
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.
156 case D3DFMT_R8G8_B8G8:
158 case D3DFMT_G8R8_G8B8:
159 Width = (Width + 1) / 2;
163 return (Height + ((Height + 1) / 2)) * RowPitch;
174 size_t size = Height * RowPitch;
177 size += (Depth - 1) * SlicePitch;
184 #endif /* DIRECT3D_VERSION >= 0x0800 */
187 #if DIRECT3D_VERSION >= 0x0900
191 _getLockSize(IDirect3DVertexBuffer9 *pBuffer, UINT OffsetToLock, UINT SizeToLock, void ** ppbData) {
192 if (SizeToLock == 0) {
193 D3DVERTEXBUFFER_DESC Desc;
194 HRESULT hr = pBuffer->GetDesc(&Desc);
198 SizeToLock = Desc.Size;
206 _getLockSize(IDirect3DIndexBuffer9 *pBuffer, UINT OffsetToLock, UINT SizeToLock, void ** ppbData) {
207 if (SizeToLock == 0) {
208 D3DINDEXBUFFER_DESC Desc;
209 HRESULT hr = pBuffer->GetDesc(&Desc);
213 SizeToLock = Desc.Size;
221 _getLockSize(IDirect3DSurface9 *pSurface, const D3DLOCKED_RECT *pLockedRect, const RECT *pRect) {
224 D3DSURFACE_DESC Desc;
225 hr = pSurface->GetDesc(&Desc);
233 Width = pRect->right - pRect->left;
234 Height = pRect->bottom - pRect->top;
237 Height = Desc.Height;
240 return _getLockSize(Desc.Format, Width, Height, pLockedRect->Pitch);
245 _getLockSize(IDirect3DTexture9 *pTexture, UINT Level, const D3DLOCKED_RECT *pLockedRect, const RECT *pRect) {
248 D3DSURFACE_DESC Desc;
249 hr = pTexture->GetLevelDesc(Level, &Desc);
257 Width = pRect->right - pRect->left;
258 Height = pRect->bottom - pRect->top;
261 Height = Desc.Height;
264 return _getLockSize(Desc.Format, Width, Height, pLockedRect->Pitch);
269 _getLockSize(IDirect3DCubeTexture9 *pTexture, D3DCUBEMAP_FACES FaceType, UINT Level, const D3DLOCKED_RECT *pLockedRect, const RECT *pRect) {
274 D3DSURFACE_DESC Desc;
275 hr = pTexture->GetLevelDesc(Level, &Desc);
283 Width = pRect->right - pRect->left;
284 Height = pRect->bottom - pRect->top;
287 Height = Desc.Height;
290 return _getLockSize(Desc.Format, Width, Height, pLockedRect->Pitch);
295 _getLockSize(IDirect3DVolume9 *pVolume, const D3DLOCKED_BOX *pLockedVolume, const D3DBOX *pBox) {
299 hr = pVolume->GetDesc(&Desc);
308 Width = pBox->Right - pBox->Left;
309 Height = pBox->Bottom - pBox->Top;
310 Depth = pBox->Back - pBox->Front;
313 Height = Desc.Height;
317 return _getLockSize(Desc.Format, Width, Height, pLockedVolume->RowPitch, Depth, pLockedVolume->SlicePitch);
322 _getLockSize(IDirect3DVolumeTexture9 *pTexture, UINT Level, const D3DLOCKED_BOX *pLockedVolume, const D3DBOX *pBox) {
326 hr = pTexture->GetLevelDesc(Level, &Desc);
335 Width = pBox->Right - pBox->Left;
336 Height = pBox->Bottom - pBox->Top;
337 Depth = pBox->Back - pBox->Front;
340 Height = Desc.Height;
344 return _getLockSize(Desc.Format, Width, Height, pLockedVolume->RowPitch, Depth, pLockedVolume->SlicePitch);
348 #endif /* DIRECT3D_VERSION >= 0x0900 */
351 #endif /* _D3D_SIZE_HPP_ */