summaryrefslogtreecommitdiff
path: root/video/archive/pixlib/pixel.hpp
blob: a23a79f624391e9a98bbd7af2dfc6f86af2df2fd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
/*==========================================================================;
 *
 *  Copyright (C) 1999-2002 Microsoft Corporation.  All Rights Reserved.
 *
 *  File:       pixel.hpp
 *  Content:    Utility class for working with pixel formats
 *
 *
 ***************************************************************************/

#ifdef __cplusplus

#ifndef __PIXEL_HPP_CPP__
#define __PIXEL_HPP_CPP__

#ifndef DXPIXELVER
#define DXPIXELVER 100
#endif

#ifndef DXGASSERT
#define DXGASSERT( x ) _Analysis_assume_( x );
#endif

struct IHVFormatInfo
{
    D3DFORMAT       m_Format;
    DWORD           m_BPP;
    IHVFormatInfo  *m_pNext;
};

//2GB max allocation size
#define MAXALLOCSIZE 0x80000000

// This is a utility class that implements useful helpers for
// allocating and accessing various pixel formats. All methods
// are static and hence should be accessed as follows:
//  e.g. CPixel::LockOffset(...)
//

class CPixel
{
public:
    // Allocate helpers

    // Determine the amount of memory that is needed to
    // allocate various things..
    static UINT ComputeSurfaceSize(UINT            cpWidth,
                                   UINT            cpHeight,
                                   D3DFORMAT       Format);

    static UINT ComputeVolumeSize(UINT             cpWidth,
                                  UINT             cpHeight,
                                  UINT             cpDepth,
                                  D3DFORMAT        Format);


    static UINT ComputeMipMapSize(UINT             cpWidth,
                                  UINT             cpHeight,
                                  UINT             cLevels,
                                  D3DFORMAT        Format);

    static UINT ComputeMipVolumeSize(UINT          cpWidth,
                                     UINT          cpHeight,
                                     UINT          cpDepth,
                                     UINT          cLevels,
                                     D3DFORMAT     Format);

    static BOOL ComputeSurfaceSizeChecked(UINT            cpWidth,
                                          UINT            cpHeight,
                                          D3DFORMAT       Format,
                                          UINT            *pSize);

    static BOOL ComputeVolumeSizeChecked(UINT             cpWidth,
                                         UINT             cpHeight,
                                         UINT             cpDepth,
                                         D3DFORMAT        Format,
                                         UINT             *pSize);


    static BOOL ComputeMipMapSizeChecked(UINT             cpWidth,
                                         UINT             cpHeight,
                                         UINT             cLevels,
                                         D3DFORMAT        Format,
                                         UINT             *pSize);

    static BOOL ComputeMipVolumeSizeChecked(UINT          cpWidth,
                                            UINT          cpHeight,
                                            UINT          cpDepth,
                                            UINT          cLevels,
                                            D3DFORMAT     Format,
                                            UINT          *pSize);


    // Lock helpers

    // Given a surface desc, a level, and pointer to
    // bits (pBits in the LockedRectData) and a sub-rect,
    // this will fill in the pLockedRectData structure
    static void ComputeMipMapOffset(const D3DSURFACE_DESC  *pDescTopLevel,
                                    UINT                    iLevel,
                                    BYTE                   *pBits,
                                    CONST RECT             *pRect,
                                    D3DLOCKED_RECT         *pLockedRectData);

    // MipVolume version of ComputeMipMapOffset
    static void ComputeMipVolumeOffset(const D3DVOLUME_DESC  *pDescTopLevel,
                                       UINT                   iLevel,
                                       BYTE                  *pBits,
                                       CONST D3DBOX          *pBox,
                                       D3DLOCKED_BOX         *pLockedBoxData);

    // Surface version of ComputeMipMapOffset
    static void ComputeSurfaceOffset(const D3DSURFACE_DESC  *pDesc,
                                     BYTE                   *pBits,
                                     CONST RECT             *pRect,
                                     D3DLOCKED_RECT         *pLockedRectData);

    // Pixel Stride will return negative for DXT formats
    // Call AdjustForDXT to work with things at the block level
    static UINT ComputePixelStride(D3DFORMAT Format);

    // This will adjust cbPixel
    // to pixels per block; and width and height will
    // be adjusted to pixels. Assumes the IsDXT(cbPixel).
    static void AdjustForDXT(UINT *pcpWidth,
                             UINT *pcpHeight,
                             UINT *pcbPixel);

    // returns TRUE if cbPixel is "negative" i.e. DXT/V group
    static BOOL IsDXT(UINT cbPixel);

    // returns TRUE if format is one of the DXT/V group
    static BOOL IsDXT(D3DFORMAT Format);

    static UINT BytesPerPixel(D3DFORMAT Format);

    // Register format for later lookup
    static HRESULT Register(D3DFORMAT Format, DWORD BPP);

    // Cleanup registry
    static void Cleanup();

    static UINT ComputeSurfaceStride(UINT cpWidth, UINT cbPixel);

    static BOOL ComputeSurfaceStrideChecked(UINT cpWidth, UINT cbPixel, UINT *pStride);


private:
    // Internal functions

    static UINT ComputeSurfaceSize(UINT            cpWidth,
                                   UINT            cpHeight,
                                   UINT            cbPixel);

    static BOOL ComputeSurfaceSizeChecked(UINT            cpWidth,
                                          UINT            cpHeight,
                                          UINT            cbPixel,
                                          UINT            *pSize);


    static IHVFormatInfo *m_pFormatList;

}; // CPixel


#undef DPF_MODNAME
#define DPF_MODNAME "CPixel::ComputeSurfaceOffset"

inline void CPixel::ComputeSurfaceOffset(const D3DSURFACE_DESC  *pDesc,
                                         BYTE                   *pBits,
                                         CONST RECT             *pRect,
                                         D3DLOCKED_RECT         *pLockedRectData)
{
    ComputeMipMapOffset(pDesc, 0, pBits, pRect, pLockedRectData);
} // ComputeSurfaceOffset


#undef DPF_MODNAME
#define DPF_MODNAME "CPixel::ComputeSurfaceSize"

inline UINT CPixel::ComputeSurfaceSize(UINT            cpWidth,
                                       UINT            cpHeight,
                                       D3DFORMAT       Format)
{
    UINT uSize = 0; // initialize to zero to keep prefix happy win7: 185026
    ComputeSurfaceSizeChecked(cpWidth, cpHeight, Format, &uSize);
    return uSize;
} // ComputeSurfaceSize

#undef DPF_MODNAME
#define DPF_MODNAME "CPixel::ComputeSurfaceSizeChecked"

inline BOOL CPixel::ComputeSurfaceSizeChecked(UINT            cpWidth,
                                              UINT            cpHeight,
                                              D3DFORMAT       Format,
                                              UINT           *uSize)
{
    UINT cbPixel = ComputePixelStride(Format);

    // Adjust pixel->block if necessary
    BOOL isDXT = IsDXT(cbPixel);
    if (isDXT)
    {
        AdjustForDXT(&cpWidth, &cpHeight, &cbPixel);
    }
#if (DXPIXELVER > 8)
    else
    if (Format == D3DFMT_A1)
    {
        cpWidth = (cpWidth + 7) >> 3;
        cbPixel = 1;
    }
#endif

    return ComputeSurfaceSizeChecked(cpWidth,
                                     cpHeight,
                                     cbPixel,
                                     uSize);
} // ComputeSurfaceSize


#undef DPF_MODNAME
#define DPF_MODNAME "CPixel::AdjustForDXT"
inline void CPixel::AdjustForDXT(UINT *pcpWidth,
                                 UINT *pcpHeight,
                                 UINT *pcbPixel)
{
    DXGASSERT(pcbPixel);
    DXGASSERT(pcpWidth);
    DXGASSERT(pcpHeight);
    DXGASSERT(IsDXT(*pcbPixel));

    // Adjust width and height for DXT formats to be in blocks
    // instead of pixels. Blocks are 4x4 pixels.
    *pcpWidth  = (*pcpWidth  + 3) / 4;
    *pcpHeight = (*pcpHeight + 3) / 4;

    // Negate the pcbPixel to determine bytes per block
    *pcbPixel *= (UINT)-1;

    // We only know of two DXT formats right now...
    DXGASSERT(*pcbPixel == 8 || *pcbPixel == 16);

} // CPixel::AdjustForDXT

#undef DPF_MODNAME
#define DPF_MODNAME "CPixel::ComputeVolumeSize"

inline UINT CPixel::ComputeVolumeSize(UINT             cpWidth,
                                      UINT             cpHeight,
                                      UINT             cpDepth,
                                      D3DFORMAT        Format)
{
    UINT cbPixel = ComputePixelStride(Format);

    if (IsDXT(cbPixel))
    {
            AdjustForDXT(&cpWidth, &cpHeight, &cbPixel);
    }

    return cpDepth * ComputeSurfaceSize(cpWidth, cpHeight, cbPixel);
} // ComputeVolumeSize


#undef DPF_MODNAME
#define DPF_MODNAME "CPixel::IsDXT(cbPixel)"

// returns TRUE if cbPixel is "negative"
inline BOOL CPixel::IsDXT(UINT cbPixel)
{
    if (((INT)cbPixel) < 0)
        return TRUE;
    else
        return FALSE;
} // IsDXT

#undef DPF_MODNAME
#define DPF_MODNAME "CPixel::IsDXT(format)"

// returns TRUE if this is a linear format
// i.e. DXT or DXV
inline BOOL CPixel::IsDXT(D3DFORMAT Format)
{
    // CONSIDER: This is a duplication of Requires4x4 function
    switch (Format)
    {
        // normal DXTs
    case D3DFMT_DXT1:
    case D3DFMT_DXT2:
    case D3DFMT_DXT3:
    case D3DFMT_DXT4:
    case D3DFMT_DXT5:
        return TRUE;
    }

    return FALSE;
} // IsDXT


#endif // __PIXEL_HPP_CPP__

#else

#ifndef __PIXEL_HPP_C__
#define __PIXEL_HPP_C__

UINT CPixel__ComputeSurfaceSize(UINT            cpWidth,
                                UINT            cpHeight,
                                D3DFORMAT       Format);

UINT CPixel__ComputeVolumeSize(UINT             cpWidth,
                               UINT             cpHeight,
                               UINT             cpDepth,
                               D3DFORMAT        Format);

UINT CPixel__ComputeMipMapSize(UINT             cpWidth,
                               UINT             cpHeight,
                               UINT             cLevels,
                               D3DFORMAT        Format);

UINT CPixel__ComputeMipVolumeSize(UINT          cpWidth,
                                  UINT          cpHeight,
                                  UINT          cpDepth,
                                  UINT          cLevels,
                                  D3DFORMAT     Format);

void CPixel__ComputeMipMapOffset(const D3DSURFACE_DESC  *pDescTopLevel,
                                 UINT                    iLevel,
                                 BYTE                   *pBits,
                                 CONST RECT             *pRect,
                                 D3DLOCKED_RECT         *pLockedRectData);

void CPixel__ComputeMipVolumeOffset(const D3DVOLUME_DESC  *pDescTopLevel,
                                    UINT                   iLevel,
                                    BYTE                  *pBits,
                                    CONST D3DBOX          *pBox,
                                    D3DLOCKED_BOX         *pLockedBoxData);

void CPixel__ComputeSurfaceOffset(const D3DSURFACE_DESC  *pDesc,
                                  BYTE                   *pBits,
                                  CONST RECT             *pRect,
                                  D3DLOCKED_RECT         *pLockedRectData);

UINT CPixel__ComputePixelStride(D3DFORMAT Format);

void CPixel__AdjustForDXT(UINT *pcpWidth,
                          UINT *pcpHeight,
                          UINT *pcbPixel);

BOOL CPixel__IsDXT(D3DFORMAT Format);

UINT CPixel__BytesPerPixel(D3DFORMAT Format);

HRESULT CPixel__Register(D3DFORMAT Format, DWORD BPP);

void CPixel__Cleanup();

UINT CPixel__ComputeSurfaceStride(UINT cpWidth, UINT cbPixel);


#endif // __PIXEL_HPP_C__

#endif