]> git.cworth.org Git - apitrace/blobdiff - helpers/d3dsize.hpp
Handle variations of LockRect.
[apitrace] / helpers / d3dsize.hpp
index 79095011873501ad6791988fbb25247775102b16..97fccd3e5d99f5a176042354431d46f775df562a 100644 (file)
@@ -37,6 +37,8 @@
 /* We purposedly don't include any D3D header, so that this header can be used
  * with all D3D versions. */
 
+#include <assert.h>
+
 #include "os.hpp"
 
 
@@ -87,4 +89,152 @@ _indexDataSize(D3DPRIMITIVETYPE PrimitiveType, UINT PrimitiveCount, D3DFORMAT In
 }
 
 
+#if DIRECT3D_VERSION >= 0x0800
+
+/*
+ * Return the number of tokens for a given shader.
+ */
+static inline size_t
+_shaderSize(const DWORD *pFunction)
+{
+    DWORD dwLength = 0;
+
+    while (true) {
+        DWORD dwToken = pFunction[dwLength++];
+
+        switch (dwToken & D3DSI_OPCODE_MASK) {
+        case D3DSIO_COMMENT:
+            dwLength += (dwToken & D3DSI_COMMENTSIZE_MASK) >> D3DSI_COMMENTSIZE_SHIFT;
+            break;
+
+        case D3DSIO_END:
+            if (dwToken != D3DSIO_END) {
+                os::log("apitrace: warning: %s: malformed END token\n", __FUNCTION__);
+            }
+            return dwLength * sizeof *pFunction;
+        }
+    }
+}
+
+
+static size_t
+_formatSize(D3DFORMAT Format, UINT Width, UINT Height, INT Pitch) {
+    switch ((DWORD)Format) {
+    case D3DFMT_DXT1:
+    case D3DFMT_DXT2:
+    case D3DFMT_DXT3:
+    case D3DFMT_DXT4:
+    case D3DFMT_DXT5:
+    case D3DFMT_ATI1:
+    case D3DFMT_ATI2:
+        Width /= 4;
+        Height /= 4;
+        break;
+
+    case D3DFMT_UYVY:
+    case D3DFMT_R8G8_B8G8:
+    case D3DFMT_YUY2:
+    case D3DFMT_G8R8_G8B8:
+        Width /= 2;
+        break;
+
+    case D3DFMT_NV12:
+        return (Height + ((Height + 1) / 2)) * Pitch;
+
+    case D3DFMT_NULL:
+        return 0;
+
+    default:
+        break;
+    }
+
+    (void)Width;
+
+    return Height * Pitch;
+}
+
+
+#endif /* DIRECT3D_VERSION >= 0x0800 */
+
+
+#if DIRECT3D_VERSION >= 0x0900
+
+
+static inline size_t
+_getLockSize(IDirect3DSurface9 *pSurface, const D3DLOCKED_RECT *pLockedRect, const RECT *pRect) {
+    HRESULT hr;
+
+    D3DSURFACE_DESC Desc;
+    hr = pSurface->GetDesc(&Desc);
+    if (FAILED(hr)) {
+        return 0;
+    } 
+
+    UINT Width;
+    UINT Height;
+    if (pRect) {
+        Width  = pRect->right  - pRect->left;
+        Height = pRect->bottom - pRect->top;
+    } else {
+        Width  = Desc.Width;
+        Height = Desc.Height;
+    }
+
+    return _formatSize(Desc.Format, Width, Height, pLockedRect->Pitch);
+}
+
+
+static inline size_t
+_getLockSize(IDirect3DTexture9 *pTexture, UINT Level, const D3DLOCKED_RECT *pLockedRect, const RECT *pRect) {
+    HRESULT hr;
+
+    D3DSURFACE_DESC Desc;
+    hr = pTexture->GetLevelDesc(Level, &Desc);
+    if (FAILED(hr)) {
+        return 0;
+    }
+
+    UINT Width;
+    UINT Height;
+    if (pRect) {
+        Width  = pRect->right  - pRect->left;
+        Height = pRect->bottom - pRect->top;
+    } else {
+        Width  = Desc.Width;
+        Height = Desc.Height;
+    }
+
+    return _formatSize(Desc.Format, Width, Height, pLockedRect->Pitch);
+}
+
+
+static inline size_t
+_getLockSize(IDirect3DCubeTexture9 *pTexture, D3DCUBEMAP_FACES FaceType, UINT Level, const D3DLOCKED_RECT *pLockedRect, const RECT *pRect) {
+    HRESULT hr;
+
+    (void)FaceType;
+
+    D3DSURFACE_DESC Desc;
+    hr = pTexture->GetLevelDesc(Level, &Desc);
+    if (FAILED(hr)) {
+        return 0;
+    }
+
+    UINT Width;
+    UINT Height;
+    if (pRect) {
+        Width  = pRect->right  - pRect->left;
+        Height = pRect->bottom - pRect->top;
+    } else {
+        Width  = Desc.Width;
+        Height = Desc.Height;
+    }
+
+    return _formatSize(Desc.Format, Width, Height, pLockedRect->Pitch);
+}
+
+
+#endif /* DIRECT3D_VERSION >= 0x0900 */
+
+
 #endif /* _D3D_SIZE_HPP_ */