ReactOS  0.4.14-dev-317-g96040ec
util.c
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2009 Tony Wasserka
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
17  *
18  */
19 
20 #include "config.h"
21 #include "wine/port.h"
22 
23 #include "d3dx9_private.h"
24 
26 
27 static void la_from_rgba(const struct vec4 *rgba, struct vec4 *la)
28 {
29  la->x = rgba->x * 0.2125f + rgba->y * 0.7154f + rgba->z * 0.0721f;
30  la->w = rgba->w;
31 }
32 
33 static void la_to_rgba(const struct vec4 *la, struct vec4 *rgba, const PALETTEENTRY *palette)
34 {
35  rgba->x = la->x;
36  rgba->y = la->x;
37  rgba->z = la->x;
38  rgba->w = la->w;
39 }
40 
41 static void index_to_rgba(const struct vec4 *index, struct vec4 *rgba, const PALETTEENTRY *palette)
42 {
43  ULONG idx = (ULONG)(index->x * 255.0f + 0.5f);
44 
45  rgba->x = palette[idx].peRed / 255.0f;
46  rgba->y = palette[idx].peGreen / 255.0f;
47  rgba->z = palette[idx].peBlue / 255.0f;
48  rgba->w = palette[idx].peFlags / 255.0f; /* peFlags is the alpha component in DX8 and higher */
49 }
50 
51 /************************************************************
52  * pixel format table providing info about number of bytes per pixel,
53  * number of bits per channel and format type.
54  *
55  * Call get_format_info to request information about a specific format.
56  */
57 static const struct pixel_format_desc formats[] =
58 {
59  /* format bpc shifts bpp blocks type from_rgba to_rgba */
60  {D3DFMT_R8G8B8, { 0, 8, 8, 8}, { 0, 16, 8, 0}, 3, 1, 1, 3, FORMAT_ARGB, NULL, NULL },
61  {D3DFMT_A8R8G8B8, { 8, 8, 8, 8}, {24, 16, 8, 0}, 4, 1, 1, 4, FORMAT_ARGB, NULL, NULL },
62  {D3DFMT_X8R8G8B8, { 0, 8, 8, 8}, { 0, 16, 8, 0}, 4, 1, 1, 4, FORMAT_ARGB, NULL, NULL },
63  {D3DFMT_A8B8G8R8, { 8, 8, 8, 8}, {24, 0, 8, 16}, 4, 1, 1, 4, FORMAT_ARGB, NULL, NULL },
64  {D3DFMT_X8B8G8R8, { 0, 8, 8, 8}, { 0, 0, 8, 16}, 4, 1, 1, 4, FORMAT_ARGB, NULL, NULL },
65  {D3DFMT_R5G6B5, { 0, 5, 6, 5}, { 0, 11, 5, 0}, 2, 1, 1, 2, FORMAT_ARGB, NULL, NULL },
66  {D3DFMT_X1R5G5B5, { 0, 5, 5, 5}, { 0, 10, 5, 0}, 2, 1, 1, 2, FORMAT_ARGB, NULL, NULL },
67  {D3DFMT_A1R5G5B5, { 1, 5, 5, 5}, {15, 10, 5, 0}, 2, 1, 1, 2, FORMAT_ARGB, NULL, NULL },
68  {D3DFMT_R3G3B2, { 0, 3, 3, 2}, { 0, 5, 2, 0}, 1, 1, 1, 1, FORMAT_ARGB, NULL, NULL },
69  {D3DFMT_A8R3G3B2, { 8, 3, 3, 2}, { 8, 5, 2, 0}, 2, 1, 1, 2, FORMAT_ARGB, NULL, NULL },
70  {D3DFMT_A4R4G4B4, { 4, 4, 4, 4}, {12, 8, 4, 0}, 2, 1, 1, 2, FORMAT_ARGB, NULL, NULL },
71  {D3DFMT_X4R4G4B4, { 0, 4, 4, 4}, { 0, 8, 4, 0}, 2, 1, 1, 2, FORMAT_ARGB, NULL, NULL },
72  {D3DFMT_A2R10G10B10, { 2, 10, 10, 10}, {30, 20, 10, 0}, 4, 1, 1, 4, FORMAT_ARGB, NULL, NULL },
73  {D3DFMT_A2B10G10R10, { 2, 10, 10, 10}, {30, 0, 10, 20}, 4, 1, 1, 4, FORMAT_ARGB, NULL, NULL },
74  {D3DFMT_A16B16G16R16, {16, 16, 16, 16}, {48, 0, 16, 32}, 8, 1, 1, 8, FORMAT_ARGB, NULL, NULL },
75  {D3DFMT_G16R16, { 0, 16, 16, 0}, { 0, 0, 16, 0}, 4, 1, 1, 4, FORMAT_ARGB, NULL, NULL },
76  {D3DFMT_A8, { 8, 0, 0, 0}, { 0, 0, 0, 0}, 1, 1, 1, 1, FORMAT_ARGB, NULL, NULL },
77  {D3DFMT_A8L8, { 8, 8, 0, 0}, { 8, 0, 0, 0}, 2, 1, 1, 2, FORMAT_ARGB, la_from_rgba, la_to_rgba},
78  {D3DFMT_A4L4, { 4, 4, 0, 0}, { 4, 0, 0, 0}, 1, 1, 1, 1, FORMAT_ARGB, la_from_rgba, la_to_rgba},
79  {D3DFMT_L8, { 0, 8, 0, 0}, { 0, 0, 0, 0}, 1, 1, 1, 1, FORMAT_ARGB, la_from_rgba, la_to_rgba},
80  {D3DFMT_L16, { 0, 16, 0, 0}, { 0, 0, 0, 0}, 2, 1, 1, 2, FORMAT_ARGB, la_from_rgba, la_to_rgba},
81  {D3DFMT_DXT1, { 0, 0, 0, 0}, { 0, 0, 0, 0}, 1, 4, 4, 8, FORMAT_DXT, NULL, NULL },
82  {D3DFMT_DXT2, { 0, 0, 0, 0}, { 0, 0, 0, 0}, 1, 4, 4, 16, FORMAT_DXT, NULL, NULL },
83  {D3DFMT_DXT3, { 0, 0, 0, 0}, { 0, 0, 0, 0}, 1, 4, 4, 16, FORMAT_DXT, NULL, NULL },
84  {D3DFMT_DXT4, { 0, 0, 0, 0}, { 0, 0, 0, 0}, 1, 4, 4, 16, FORMAT_DXT, NULL, NULL },
85  {D3DFMT_DXT5, { 0, 0, 0, 0}, { 0, 0, 0, 0}, 1, 4, 4, 16, FORMAT_DXT, NULL, NULL },
86  {D3DFMT_R16F, { 0, 16, 0, 0}, { 0, 0, 0, 0}, 2, 1, 1, 2, FORMAT_ARGBF16, NULL, NULL },
87  {D3DFMT_G16R16F, { 0, 16, 16, 0}, { 0, 0, 16, 0}, 4, 1, 1, 4, FORMAT_ARGBF16, NULL, NULL },
88  {D3DFMT_A16B16G16R16F, {16, 16, 16, 16}, {48, 0, 16, 32}, 8, 1, 1, 8, FORMAT_ARGBF16, NULL, NULL },
89  {D3DFMT_R32F, { 0, 32, 0, 0}, { 0, 0, 0, 0}, 4, 1, 1, 4, FORMAT_ARGBF, NULL, NULL },
90  {D3DFMT_G32R32F, { 0, 32, 32, 0}, { 0, 0, 32, 0}, 8, 1, 1, 8, FORMAT_ARGBF, NULL, NULL },
91  {D3DFMT_A32B32G32R32F, {32, 32, 32, 32}, {96, 0, 32, 64}, 16, 1, 1, 16, FORMAT_ARGBF, NULL, NULL },
92  {D3DFMT_P8, { 8, 8, 8, 8}, { 0, 0, 0, 0}, 1, 1, 1, 1, FORMAT_INDEX, NULL, index_to_rgba},
93  {D3DFMT_X8L8V8U8, { 0, 8, 8, 8}, { 0, 0, 8, 16}, 4, 1, 1, 4, FORMAT_ARGB, NULL, NULL },
94  /* marks last element */
95  {D3DFMT_UNKNOWN, { 0, 0, 0, 0}, { 0, 0, 0, 0}, 0, 1, 1, 0, FORMAT_UNKNOWN, NULL, NULL },
96 };
97 
98 
99 /************************************************************
100  * map_view_of_file
101  *
102  * Loads a file into buffer and stores the number of read bytes in length.
103  *
104  * PARAMS
105  * filename [I] name of the file to be loaded
106  * buffer [O] pointer to destination buffer
107  * length [O] size of the obtained data
108  *
109  * RETURNS
110  * Success: D3D_OK
111  * Failure:
112  * see error codes for CreateFileW, GetFileSize, CreateFileMapping and MapViewOfFile
113  *
114  * NOTES
115  * The caller must UnmapViewOfFile when it doesn't need the data anymore
116  *
117  */
119 {
120  HANDLE hfile, hmapping = NULL;
121 
123  if(hfile == INVALID_HANDLE_VALUE) goto error;
124 
125  *length = GetFileSize(hfile, NULL);
126  if(*length == INVALID_FILE_SIZE) goto error;
127 
128  hmapping = CreateFileMappingW(hfile, NULL, PAGE_READONLY, 0, 0, NULL);
129  if(!hmapping) goto error;
130 
131  *buffer = MapViewOfFile(hmapping, FILE_MAP_READ, 0, 0, 0);
132  if(*buffer == NULL) goto error;
133 
134  CloseHandle(hmapping);
135  CloseHandle(hfile);
136 
137  return S_OK;
138 
139 error:
140  CloseHandle(hmapping);
141  CloseHandle(hfile);
143 }
144 
145 /************************************************************
146  * load_resource_into_memory
147  *
148  * Loads a resource into buffer and stores the number of
149  * read bytes in length.
150  *
151  * PARAMS
152  * module [I] handle to the module
153  * resinfo [I] handle to the resource's information block
154  * buffer [O] pointer to destination buffer
155  * length [O] size of the obtained data
156  *
157  * RETURNS
158  * Success: D3D_OK
159  * Failure:
160  * See error codes for SizeofResource, LoadResource and LockResource
161  *
162  * NOTES
163  * The memory doesn't need to be freed by the caller manually
164  *
165  */
167 {
169 
170  *length = SizeofResource(module, resinfo);
171  if(*length == 0) return HRESULT_FROM_WIN32(GetLastError());
172 
173  resource = LoadResource(module, resinfo);
174  if( !resource ) return HRESULT_FROM_WIN32(GetLastError());
175 
177  if(*buffer == NULL) return HRESULT_FROM_WIN32(GetLastError());
178 
179  return S_OK;
180 }
181 
183 {
184  HRESULT hr = S_OK;
185  void *buffer_pointer;
187  DWORD bytes_written;
189  if (file == INVALID_HANDLE_VALUE)
191 
192  buffer_pointer = ID3DXBuffer_GetBufferPointer(buffer);
194 
195  if (!WriteFile(file, buffer_pointer, buffer_size, &bytes_written, NULL))
197 
198  CloseHandle(file);
199  return hr;
200 }
201 
202 
203 /************************************************************
204  * get_format_info
205  *
206  * Returns information about the specified format.
207  * If the format is unsupported, it's filled with the D3DFMT_UNKNOWN desc.
208  *
209  * PARAMS
210  * format [I] format whose description is queried
211  *
212  */
214 {
215  unsigned int i = 0;
216  while(formats[i].format != format && formats[i].format != D3DFMT_UNKNOWN) i++;
217  if (formats[i].format == D3DFMT_UNKNOWN)
218  FIXME("Unknown format %#x (as FOURCC %s).\n", format, debugstr_an((const char *)&format, 4));
219  return &formats[i];
220 }
221 
223 {
224  if(idx >= ARRAY_SIZE(formats))
225  return NULL;
227  return NULL;
228  return &formats[idx];
229 }
230 
231 #define WINE_D3DX_TO_STR(x) case x: return #x
232 
234 {
235  switch (c)
236  {
243  default:
244  FIXME("Unrecognized D3DXPARAMETER_CLASS %#x.\n", c);
245  return "unrecognized";
246  }
247 }
248 
250 {
251  switch (t)
252  {
273  default:
274  FIXME("Unrecognized D3DXPARAMETER_TYP %#x.\n", t);
275  return "unrecognized";
276  }
277 }
278 
280 {
281  switch (r)
282  {
287  default:
288  FIXME("Unrecognized D3DXREGISTER_SET %#x.\n", r);
289  return "unrecognized";
290  }
291 }
292 
293 #undef WINE_D3DX_TO_STR
294 
295 /***********************************************************************
296  * D3DXDebugMute
297  * Returns always FALSE for us.
298  */
300 {
301  return FALSE;
302 }
303 
304 /***********************************************************************
305  * D3DXGetDriverLevel.
306  * Returns always 900 (DX 9) for us
307  */
308 UINT WINAPI D3DXGetDriverLevel(struct IDirect3DDevice9 *device)
309 {
310  return 900;
311 }
BOOL WINAPI WriteFile(IN HANDLE hFile, IN LPCVOID lpBuffer, IN DWORD nNumberOfBytesToWrite OPTIONAL, OUT LPDWORD lpNumberOfBytesWritten, IN LPOVERLAPPED lpOverlapped OPTIONAL)
Definition: rw.c:24
WINE_DEFAULT_DEBUG_CHANNEL(d3dx)
#define CloseHandle
Definition: compat.h:398
#define HRESULT_FROM_WIN32(x)
Definition: winerror.h:92
#define MapViewOfFile
Definition: compat.h:402
HRESULT hr
Definition: shlfolder.c:183
const struct pixel_format_desc * get_format_info(D3DFORMAT format)
Definition: util.c:213
GLdouble GLdouble GLdouble r
Definition: gl.h:2055
static const struct pixel_format_desc formats[]
Definition: util.c:57
HRESULT map_view_of_file(const WCHAR *filename, void **buffer, DWORD *length)
Definition: util.c:118
static void index_to_rgba(const struct vec4 *index, struct vec4 *rgba, const PALETTEENTRY *palette)
Definition: util.c:41
GLdouble GLdouble t
Definition: gl.h:2047
static void la_to_rgba(const struct vec4 *la, struct vec4 *rgba, const PALETTEENTRY *palette)
Definition: util.c:33
#define INVALID_HANDLE_VALUE
Definition: compat.h:391
DWORD WINAPI GetLastError(VOID)
Definition: except.c:1059
LPVOID WINAPI LockResource(HGLOBAL handle)
Definition: res.c:550
GLuint buffer
Definition: glext.h:5915
#define INVALID_FILE_SIZE
Definition: winbase.h:529
int resource
Definition: rdpsnd_sgi.c:44
const char * filename
Definition: ioapi.h:135
HRESULT write_buffer_to_file(const WCHAR *dst_filename, ID3DXBuffer *buffer)
Definition: util.c:182
GLint GLint GLsizei GLsizei GLsizei GLint GLenum format
Definition: gl.h:1546
#define FILE_SHARE_READ
Definition: compat.h:125
GLsizei GLenum const GLvoid GLsizei GLenum GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLint GLint GLint GLshort GLshort GLshort GLubyte GLubyte GLubyte GLuint GLuint GLuint GLushort GLushort GLushort GLbyte GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLfloat GLint GLint GLint GLint GLshort GLshort GLshort GLshort GLubyte GLubyte GLubyte GLubyte GLuint GLuint GLuint GLuint GLushort GLushort GLushort GLushort GLboolean const GLdouble const GLfloat const GLint const GLshort const GLbyte const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLdouble const GLfloat const GLfloat const GLint const GLint const GLshort const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort GLenum GLenum GLenum GLfloat GLenum GLint GLenum GLenum GLenum GLfloat GLenum GLenum GLint GLenum GLfloat GLenum GLint GLint GLushort GLenum GLenum GLfloat GLenum GLenum GLint GLfloat const GLubyte GLenum GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLint GLint GLsizei GLsizei GLint GLenum GLenum const GLvoid GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLenum const GLdouble GLenum GLenum const GLfloat GLenum GLenum const GLint GLsizei GLuint GLfloat GLuint GLbitfield GLfloat GLint GLuint GLboolean GLenum GLfloat GLenum GLbitfield GLenum GLfloat GLfloat GLint GLint const GLfloat GLenum GLfloat GLfloat GLint GLint GLfloat GLfloat GLint GLint const GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat const GLdouble const GLfloat const GLdouble const GLfloat GLint i
Definition: glfuncs.h:248
static ULONG
Definition: util.c:35
unsigned int BOOL
Definition: ntddk_ex.h:94
Definition: devices.h:37
#define GENERIC_WRITE
Definition: nt_native.h:90
#define FIXME(fmt,...)
Definition: debug.h:110
unsigned int idx
Definition: utils.c:41
static void buffer_size(GLcontext *ctx, GLuint *width, GLuint *height)
Definition: swimpl.c:927
HGLOBAL WINAPI LoadResource(HINSTANCE hModule, HRSRC hRsrc)
Definition: res.c:532
smooth NULL
Definition: ftsmooth.c:416
struct ID3DXBuffer ID3DXBuffer
Definition: d3dx8core.h:51
#define FILE_MAP_READ
Definition: compat.h:427
GLuint index
Definition: glext.h:6031
enum _D3DFORMAT D3DFORMAT
static void la_from_rgba(const struct vec4 *rgba, struct vec4 *la)
Definition: util.c:27
#define OPEN_EXISTING
Definition: compat.h:426
const char * debug_d3dxparameter_class(D3DXPARAMETER_CLASS c)
Definition: util.c:233
#define ID3DXBuffer_GetBufferSize(p)
Definition: d3dx9core.h:86
__wchar_t WCHAR
Definition: xmlstorage.h:180
void error(char *format,...)
Definition: util.c:72
LONG HRESULT
Definition: typedefs.h:77
GLenum GLuint GLenum GLsizei length
Definition: glext.h:5579
#define CreateFileMappingW(a, b, c, d, e, f)
Definition: compat.h:401
const struct pixel_format_desc * get_format_info_idx(int idx)
Definition: util.c:222
#define WINAPI
Definition: msvc.h:8
const GLubyte * c
Definition: glext.h:8905
unsigned long DWORD
Definition: ntddk_ex.h:95
DWORD WINAPI GetFileSize(HANDLE hFile, LPDWORD lpFileSizeHigh)
Definition: fileinfo.c:481
float w
Definition: d3dx9_private.h:37
#define WINE_D3DX_TO_STR(x)
Definition: util.c:231
#define FILE_ATTRIBUTE_NORMAL
Definition: compat.h:126
BOOL WINAPI D3DXDebugMute(BOOL mute)
Definition: util.c:299
static __inline const char * debugstr_an(const char *s, int n)
Definition: compat.h:47
HRESULT load_resource_into_memory(HMODULE module, HRSRC resinfo, void **buffer, DWORD *length)
Definition: util.c:166
enum _D3DXREGISTER_SET D3DXREGISTER_SET
#define GENERIC_READ
Definition: compat.h:124
static HPALETTE palette
Definition: clipboard.c:1345
D3DXPARAMETER_TYPE
Definition: d3dx9shader.h:71
#define S_OK
Definition: intsafe.h:59
#define CREATE_ALWAYS
Definition: disk.h:72
#define ARRAY_SIZE(a)
Definition: main.h:24
unsigned int UINT
Definition: ndis.h:50
#define PAGE_READONLY
Definition: compat.h:127
float z
Definition: d3dx9_private.h:37
DWORD WINAPI SizeofResource(HINSTANCE hModule, HRSRC hRsrc)
Definition: res.c:568
#define CreateFileW
Definition: compat.h:400
UINT WINAPI D3DXGetDriverLevel(struct IDirect3DDevice9 *device)
Definition: util.c:308
unsigned int ULONG
Definition: retypes.h:1
const char * debug_d3dxparameter_type(D3DXPARAMETER_TYPE t)
Definition: util.c:249
float x
Definition: d3dx9_private.h:37
const char * debug_d3dxparameter_registerset(D3DXREGISTER_SET r)
Definition: util.c:279
float y
Definition: d3dx9_private.h:37
#define ID3DXBuffer_GetBufferPointer(p)
Definition: d3dx9core.h:85
D3DXPARAMETER_CLASS
Definition: d3dx9shader.h:60
Definition: fci.c:126