]> git.cworth.org Git - apitrace/blob - helpers/d3d10size.hpp
Trace d3d10.x maps.
[apitrace] / helpers / d3d10size.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 _D3D10SIZE_HPP_
34 #define _D3D10SIZE_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
43 static size_t
44 _getMapSize(DXGI_FORMAT Format, UINT Width, UINT Height, INT RowPitch, UINT Depth = 1, INT DepthPitch = 0) {
45     if (Width == 0 || Height == 0 || Depth == 0) {
46         return 0;
47     }
48
49     if (RowPitch < 0) {
50         os::log("apitrace: warning: %s: negative row pitch %i\n", __FUNCTION__, RowPitch);
51         return 0;
52     }
53
54     if (DepthPitch < 0) {
55         os::log("apitrace: warning: %s: negative slice pitch %i\n", __FUNCTION__, DepthPitch);
56         return 0;
57     }
58
59     switch (Format) {
60     case DXGI_FORMAT_BC1_TYPELESS:
61     case DXGI_FORMAT_BC1_UNORM:
62     case DXGI_FORMAT_BC1_UNORM_SRGB:
63     case DXGI_FORMAT_BC2_TYPELESS:
64     case DXGI_FORMAT_BC2_UNORM:
65     case DXGI_FORMAT_BC2_UNORM_SRGB:
66     case DXGI_FORMAT_BC3_TYPELESS:
67     case DXGI_FORMAT_BC3_UNORM:
68     case DXGI_FORMAT_BC3_UNORM_SRGB:
69     case DXGI_FORMAT_BC4_TYPELESS:
70     case DXGI_FORMAT_BC4_UNORM:
71     case DXGI_FORMAT_BC4_SNORM:
72     case DXGI_FORMAT_BC5_TYPELESS:
73     case DXGI_FORMAT_BC5_UNORM:
74     case DXGI_FORMAT_BC5_SNORM:
75         Width  = (Width  + 3) / 4;
76         Height = (Height + 3) / 4;
77         break;
78
79     case DXGI_FORMAT_R8G8_B8G8_UNORM:
80     case DXGI_FORMAT_G8R8_G8B8_UNORM:
81         Width = (Width + 1) / 2;
82         break;
83
84     case DXGI_FORMAT_UNKNOWN:
85         return 0;
86
87     default:
88         break;
89     }
90
91     /* FIXME */
92     (void)Width;
93
94     size_t size = Height * RowPitch;
95
96     if (Depth > 1) {
97         size += (Depth - 1) * DepthPitch;
98     }
99
100     return size;
101 }
102
103 static inline void
104 _getMapInfo(ID3D10Buffer *pResource, D3D10_MAP MapType, UINT MapFlags, void * * ppData,
105             void * & pMappedData, size_t & MappedSize) {
106     pMappedData = 0;
107     MappedSize = 0;
108
109     if (MapType == D3D10_MAP_READ) {
110         return;
111     }
112
113     D3D10_BUFFER_DESC Desc;
114     pResource->GetDesc(&Desc);
115
116     pMappedData = *ppData;
117     MappedSize = Desc.ByteWidth;
118 }
119
120 static inline void
121 _getMapInfo(ID3D10Texture1D *pResource, UINT Subresource, D3D10_MAP MapType, UINT MapFlags, void * * ppData,
122             void * & pMappedData, size_t & MappedSize) {
123     pMappedData = 0;
124     MappedSize = 0;
125
126     if (MapType == D3D10_MAP_READ) {
127         return;
128     }
129
130     D3D10_TEXTURE1D_DESC Desc;
131     pResource->GetDesc(&Desc);
132
133     pMappedData = *ppData;
134     MappedSize = _getMapSize(Desc.Format, Desc.Width, 1, 0);
135 }
136
137 static inline void
138 _getMapInfo(ID3D10Texture2D *pResource, UINT Subresource, D3D10_MAP MapType, UINT MapFlags, D3D10_MAPPED_TEXTURE2D * pMappedTex2D,
139             void * & pMappedData, size_t & MappedSize) {
140     pMappedData = 0;
141     MappedSize = 0;
142
143     if (MapType == D3D10_MAP_READ) {
144         return;
145     }
146
147     D3D10_TEXTURE2D_DESC Desc;
148     pResource->GetDesc(&Desc);
149
150     pMappedData = pMappedTex2D->pData;
151     MappedSize = _getMapSize(Desc.Format, Desc.Width, Desc.Height, pMappedTex2D->RowPitch);
152 }
153
154 static inline void
155 _getMapInfo(ID3D10Texture3D *pResource, UINT Subresource, D3D10_MAP MapType, UINT MapFlags, D3D10_MAPPED_TEXTURE3D * pMappedTex3D,
156             void * & pMappedData, size_t & MappedSize) {
157     pMappedData = 0;
158     MappedSize = 0;
159
160     if (MapType == D3D10_MAP_READ) {
161         return;
162     }
163
164     D3D10_TEXTURE3D_DESC Desc;
165     pResource->GetDesc(&Desc);
166
167     pMappedData = pMappedTex3D->pData;
168     MappedSize = _getMapSize(Desc.Format, Desc.Width, Desc.Height, pMappedTex3D->RowPitch, Desc.Depth, pMappedTex3D->DepthPitch);
169 }
170
171
172 static inline void
173 _getMapInfo(IDXGISurface *pResource, DXGI_MAPPED_RECT * pLockedRect, UINT MapFlags,
174             void * & pMappedData, size_t & MappedSize) {
175     pMappedData = 0;
176     MappedSize = 0;
177
178     if (!(MapFlags & DXGI_MAP_WRITE)) {
179         return;
180     }
181
182     DXGI_SURFACE_DESC Desc;
183     HRESULT hr = pResource->GetDesc(&Desc);
184     if (FAILED(hr)) {
185         return;
186     }
187
188     pMappedData = pLockedRect->pBits;
189     MappedSize = _getMapSize(Desc.Format, Desc.Width, Desc.Height, pLockedRect->Pitch);
190 }
191
192
193 #endif /* _D3D10SIZE_HPP_ */