xref: /reactos/dll/directx/wine/d3dx9_36/util.c (revision 6760065e)
1 #ifdef __REACTOS__
2 #include "precomp.h"
3 #else
4 /*
5  * Copyright (C) 2009 Tony Wasserka
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this library; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20  *
21  */
22 
23 #include "config.h"
24 #include "wine/port.h"
25 
26 #include "d3dx9_private.h"
27 #endif /* __REACTOS__ */
28 
29 WINE_DEFAULT_DEBUG_CHANNEL(d3dx);
30 
31 static void la_from_rgba(const struct vec4 *rgba, struct vec4 *la)
32 {
33     la->x = rgba->x * 0.2125f + rgba->y * 0.7154f + rgba->z * 0.0721f;
34     la->w = rgba->w;
35 }
36 
37 static void la_to_rgba(const struct vec4 *la, struct vec4 *rgba, const PALETTEENTRY *palette)
38 {
39     rgba->x = la->x;
40     rgba->y = la->x;
41     rgba->z = la->x;
42     rgba->w = la->w;
43 }
44 
45 static void index_to_rgba(const struct vec4 *index, struct vec4 *rgba, const PALETTEENTRY *palette)
46 {
47     ULONG idx = (ULONG)(index->x * 255.0f + 0.5f);
48 
49     rgba->x = palette[idx].peRed / 255.0f;
50     rgba->y = palette[idx].peGreen / 255.0f;
51     rgba->z = palette[idx].peBlue / 255.0f;
52     rgba->w = palette[idx].peFlags / 255.0f; /* peFlags is the alpha component in DX8 and higher */
53 }
54 
55 /************************************************************
56  * pixel format table providing info about number of bytes per pixel,
57  * number of bits per channel and format type.
58  *
59  * Call get_format_info to request information about a specific format.
60  */
61 static const struct pixel_format_desc formats[] =
62 {
63     /* format              bpc               shifts             bpp blocks   type            from_rgba     to_rgba */
64     {D3DFMT_R8G8B8,        { 0,  8,  8,  8}, { 0, 16,  8,  0},  3, 1, 1,  3, FORMAT_ARGB,    NULL,         NULL      },
65     {D3DFMT_A8R8G8B8,      { 8,  8,  8,  8}, {24, 16,  8,  0},  4, 1, 1,  4, FORMAT_ARGB,    NULL,         NULL      },
66     {D3DFMT_X8R8G8B8,      { 0,  8,  8,  8}, { 0, 16,  8,  0},  4, 1, 1,  4, FORMAT_ARGB,    NULL,         NULL      },
67     {D3DFMT_A8B8G8R8,      { 8,  8,  8,  8}, {24,  0,  8, 16},  4, 1, 1,  4, FORMAT_ARGB,    NULL,         NULL      },
68     {D3DFMT_X8B8G8R8,      { 0,  8,  8,  8}, { 0,  0,  8, 16},  4, 1, 1,  4, FORMAT_ARGB,    NULL,         NULL      },
69     {D3DFMT_R5G6B5,        { 0,  5,  6,  5}, { 0, 11,  5,  0},  2, 1, 1,  2, FORMAT_ARGB,    NULL,         NULL      },
70     {D3DFMT_X1R5G5B5,      { 0,  5,  5,  5}, { 0, 10,  5,  0},  2, 1, 1,  2, FORMAT_ARGB,    NULL,         NULL      },
71     {D3DFMT_A1R5G5B5,      { 1,  5,  5,  5}, {15, 10,  5,  0},  2, 1, 1,  2, FORMAT_ARGB,    NULL,         NULL      },
72     {D3DFMT_R3G3B2,        { 0,  3,  3,  2}, { 0,  5,  2,  0},  1, 1, 1,  1, FORMAT_ARGB,    NULL,         NULL      },
73     {D3DFMT_A8R3G3B2,      { 8,  3,  3,  2}, { 8,  5,  2,  0},  2, 1, 1,  2, FORMAT_ARGB,    NULL,         NULL      },
74     {D3DFMT_A4R4G4B4,      { 4,  4,  4,  4}, {12,  8,  4,  0},  2, 1, 1,  2, FORMAT_ARGB,    NULL,         NULL      },
75     {D3DFMT_X4R4G4B4,      { 0,  4,  4,  4}, { 0,  8,  4,  0},  2, 1, 1,  2, FORMAT_ARGB,    NULL,         NULL      },
76     {D3DFMT_A2R10G10B10,   { 2, 10, 10, 10}, {30, 20, 10,  0},  4, 1, 1,  4, FORMAT_ARGB,    NULL,         NULL      },
77     {D3DFMT_A2B10G10R10,   { 2, 10, 10, 10}, {30,  0, 10, 20},  4, 1, 1,  4, FORMAT_ARGB,    NULL,         NULL      },
78     {D3DFMT_A16B16G16R16,  {16, 16, 16, 16}, {48,  0, 16, 32},  8, 1, 1,  8, FORMAT_ARGB,    NULL,         NULL      },
79     {D3DFMT_G16R16,        { 0, 16, 16,  0}, { 0,  0, 16,  0},  4, 1, 1,  4, FORMAT_ARGB,    NULL,         NULL      },
80     {D3DFMT_A8,            { 8,  0,  0,  0}, { 0,  0,  0,  0},  1, 1, 1,  1, FORMAT_ARGB,    NULL,         NULL      },
81     {D3DFMT_A8L8,          { 8,  8,  0,  0}, { 8,  0,  0,  0},  2, 1, 1,  2, FORMAT_ARGB,    la_from_rgba, la_to_rgba},
82     {D3DFMT_A4L4,          { 4,  4,  0,  0}, { 4,  0,  0,  0},  1, 1, 1,  1, FORMAT_ARGB,    la_from_rgba, la_to_rgba},
83     {D3DFMT_L8,            { 0,  8,  0,  0}, { 0,  0,  0,  0},  1, 1, 1,  1, FORMAT_ARGB,    la_from_rgba, la_to_rgba},
84     {D3DFMT_L16,           { 0, 16,  0,  0}, { 0,  0,  0,  0},  2, 1, 1,  2, FORMAT_ARGB,    la_from_rgba, la_to_rgba},
85     {D3DFMT_DXT1,          { 0,  0,  0,  0}, { 0,  0,  0,  0},  1, 4, 4,  8, FORMAT_DXT,     NULL,         NULL      },
86     {D3DFMT_DXT2,          { 0,  0,  0,  0}, { 0,  0,  0,  0},  1, 4, 4, 16, FORMAT_DXT,     NULL,         NULL      },
87     {D3DFMT_DXT3,          { 0,  0,  0,  0}, { 0,  0,  0,  0},  1, 4, 4, 16, FORMAT_DXT,     NULL,         NULL      },
88     {D3DFMT_DXT4,          { 0,  0,  0,  0}, { 0,  0,  0,  0},  1, 4, 4, 16, FORMAT_DXT,     NULL,         NULL      },
89     {D3DFMT_DXT5,          { 0,  0,  0,  0}, { 0,  0,  0,  0},  1, 4, 4, 16, FORMAT_DXT,     NULL,         NULL      },
90     {D3DFMT_R16F,          { 0, 16,  0,  0}, { 0,  0,  0,  0},  2, 1, 1,  2, FORMAT_ARGBF16, NULL,         NULL      },
91     {D3DFMT_G16R16F,       { 0, 16, 16,  0}, { 0,  0, 16,  0},  4, 1, 1,  4, FORMAT_ARGBF16, NULL,         NULL      },
92     {D3DFMT_A16B16G16R16F, {16, 16, 16, 16}, {48,  0, 16, 32},  8, 1, 1,  8, FORMAT_ARGBF16, NULL,         NULL      },
93     {D3DFMT_R32F,          { 0, 32,  0,  0}, { 0,  0,  0,  0},  4, 1, 1,  4, FORMAT_ARGBF,   NULL,         NULL      },
94     {D3DFMT_G32R32F,       { 0, 32, 32,  0}, { 0,  0, 32,  0},  8, 1, 1,  8, FORMAT_ARGBF,   NULL,         NULL      },
95     {D3DFMT_A32B32G32R32F, {32, 32, 32, 32}, {96,  0, 32, 64}, 16, 1, 1, 16, FORMAT_ARGBF,   NULL,         NULL      },
96     {D3DFMT_P8,            { 8,  8,  8,  8}, { 0,  0,  0,  0},  1, 1, 1,  1, FORMAT_INDEX,   NULL,         index_to_rgba},
97     /* marks last element */
98     {D3DFMT_UNKNOWN,       { 0,  0,  0,  0}, { 0,  0,  0,  0},  0, 1, 1,  0, FORMAT_UNKNOWN, NULL,         NULL      },
99 };
100 
101 
102 /************************************************************
103  * map_view_of_file
104  *
105  * Loads a file into buffer and stores the number of read bytes in length.
106  *
107  * PARAMS
108  *   filename [I] name of the file to be loaded
109  *   buffer   [O] pointer to destination buffer
110  *   length   [O] size of the obtained data
111  *
112  * RETURNS
113  *   Success: D3D_OK
114  *   Failure:
115  *     see error codes for CreateFileW, GetFileSize, CreateFileMapping and MapViewOfFile
116  *
117  * NOTES
118  *   The caller must UnmapViewOfFile when it doesn't need the data anymore
119  *
120  */
121 HRESULT map_view_of_file(const WCHAR *filename, void **buffer, DWORD *length)
122 {
123     HANDLE hfile, hmapping = NULL;
124 
125     hfile = CreateFileW(filename, GENERIC_READ, FILE_SHARE_READ, 0, OPEN_EXISTING, 0, 0);
126     if(hfile == INVALID_HANDLE_VALUE) goto error;
127 
128     *length = GetFileSize(hfile, NULL);
129     if(*length == INVALID_FILE_SIZE) goto error;
130 
131     hmapping = CreateFileMappingW(hfile, NULL, PAGE_READONLY, 0, 0, NULL);
132     if(!hmapping) goto error;
133 
134     *buffer = MapViewOfFile(hmapping, FILE_MAP_READ, 0, 0, 0);
135     if(*buffer == NULL) goto error;
136 
137     CloseHandle(hmapping);
138     CloseHandle(hfile);
139 
140     return S_OK;
141 
142 error:
143     CloseHandle(hmapping);
144     CloseHandle(hfile);
145     return HRESULT_FROM_WIN32(GetLastError());
146 }
147 
148 /************************************************************
149  * load_resource_into_memory
150  *
151  * Loads a resource into buffer and stores the number of
152  * read bytes in length.
153  *
154  * PARAMS
155  *   module  [I] handle to the module
156  *   resinfo [I] handle to the resource's information block
157  *   buffer  [O] pointer to destination buffer
158  *   length  [O] size of the obtained data
159  *
160  * RETURNS
161  *   Success: D3D_OK
162  *   Failure:
163  *     See error codes for SizeofResource, LoadResource and LockResource
164  *
165  * NOTES
166  *   The memory doesn't need to be freed by the caller manually
167  *
168  */
169 HRESULT load_resource_into_memory(HMODULE module, HRSRC resinfo, void **buffer, DWORD *length)
170 {
171     HGLOBAL resource;
172 
173     *length = SizeofResource(module, resinfo);
174     if(*length == 0) return HRESULT_FROM_WIN32(GetLastError());
175 
176     resource = LoadResource(module, resinfo);
177     if( !resource ) return HRESULT_FROM_WIN32(GetLastError());
178 
179     *buffer = LockResource(resource);
180     if(*buffer == NULL) return HRESULT_FROM_WIN32(GetLastError());
181 
182     return S_OK;
183 }
184 
185 HRESULT write_buffer_to_file(const WCHAR *dst_filename, ID3DXBuffer *buffer)
186 {
187     HRESULT hr = S_OK;
188     void *buffer_pointer;
189     DWORD buffer_size;
190     DWORD bytes_written;
191     HANDLE file = CreateFileW(dst_filename, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
192     if (file == INVALID_HANDLE_VALUE)
193         return HRESULT_FROM_WIN32(GetLastError());
194 
195     buffer_pointer = ID3DXBuffer_GetBufferPointer(buffer);
196     buffer_size = ID3DXBuffer_GetBufferSize(buffer);
197 
198     if (!WriteFile(file, buffer_pointer, buffer_size, &bytes_written, NULL))
199         hr = HRESULT_FROM_WIN32(GetLastError());
200 
201     CloseHandle(file);
202     return hr;
203 }
204 
205 
206 /************************************************************
207  * get_format_info
208  *
209  * Returns information about the specified format.
210  * If the format is unsupported, it's filled with the D3DFMT_UNKNOWN desc.
211  *
212  * PARAMS
213  *   format [I] format whose description is queried
214  *
215  */
216 const struct pixel_format_desc *get_format_info(D3DFORMAT format)
217 {
218     unsigned int i = 0;
219     while(formats[i].format != format && formats[i].format != D3DFMT_UNKNOWN) i++;
220     if (formats[i].format == D3DFMT_UNKNOWN)
221         FIXME("Unknown format %#x (as FOURCC %s).\n", format, debugstr_an((const char *)&format, 4));
222     return &formats[i];
223 }
224 
225 const struct pixel_format_desc *get_format_info_idx(int idx)
226 {
227     if(idx >= ARRAY_SIZE(formats))
228         return NULL;
229     if(formats[idx].format == D3DFMT_UNKNOWN)
230         return NULL;
231     return &formats[idx];
232 }
233 
234 #define WINE_D3DX_TO_STR(x) case x: return #x
235 
236 const char *debug_d3dxparameter_class(D3DXPARAMETER_CLASS c)
237 {
238     switch (c)
239     {
240         WINE_D3DX_TO_STR(D3DXPC_SCALAR);
241         WINE_D3DX_TO_STR(D3DXPC_VECTOR);
242         WINE_D3DX_TO_STR(D3DXPC_MATRIX_ROWS);
243         WINE_D3DX_TO_STR(D3DXPC_MATRIX_COLUMNS);
244         WINE_D3DX_TO_STR(D3DXPC_OBJECT);
245         WINE_D3DX_TO_STR(D3DXPC_STRUCT);
246         default:
247             FIXME("Unrecognized D3DXPARAMETER_CLASS %#x.\n", c);
248             return "unrecognized";
249     }
250 }
251 
252 const char *debug_d3dxparameter_type(D3DXPARAMETER_TYPE t)
253 {
254     switch (t)
255     {
256         WINE_D3DX_TO_STR(D3DXPT_VOID);
257         WINE_D3DX_TO_STR(D3DXPT_BOOL);
258         WINE_D3DX_TO_STR(D3DXPT_INT);
259         WINE_D3DX_TO_STR(D3DXPT_FLOAT);
260         WINE_D3DX_TO_STR(D3DXPT_STRING);
261         WINE_D3DX_TO_STR(D3DXPT_TEXTURE);
262         WINE_D3DX_TO_STR(D3DXPT_TEXTURE1D);
263         WINE_D3DX_TO_STR(D3DXPT_TEXTURE2D);
264         WINE_D3DX_TO_STR(D3DXPT_TEXTURE3D);
265         WINE_D3DX_TO_STR(D3DXPT_TEXTURECUBE);
266         WINE_D3DX_TO_STR(D3DXPT_SAMPLER);
267         WINE_D3DX_TO_STR(D3DXPT_SAMPLER1D);
268         WINE_D3DX_TO_STR(D3DXPT_SAMPLER2D);
269         WINE_D3DX_TO_STR(D3DXPT_SAMPLER3D);
270         WINE_D3DX_TO_STR(D3DXPT_SAMPLERCUBE);
271         WINE_D3DX_TO_STR(D3DXPT_PIXELSHADER);
272         WINE_D3DX_TO_STR(D3DXPT_VERTEXSHADER);
273         WINE_D3DX_TO_STR(D3DXPT_PIXELFRAGMENT);
274         WINE_D3DX_TO_STR(D3DXPT_VERTEXFRAGMENT);
275         WINE_D3DX_TO_STR(D3DXPT_UNSUPPORTED);
276         default:
277             FIXME("Unrecognized D3DXPARAMETER_TYP %#x.\n", t);
278             return "unrecognized";
279     }
280 }
281 
282 const char *debug_d3dxparameter_registerset(D3DXREGISTER_SET r)
283 {
284     switch (r)
285     {
286         WINE_D3DX_TO_STR(D3DXRS_BOOL);
287         WINE_D3DX_TO_STR(D3DXRS_INT4);
288         WINE_D3DX_TO_STR(D3DXRS_FLOAT4);
289         WINE_D3DX_TO_STR(D3DXRS_SAMPLER);
290         default:
291             FIXME("Unrecognized D3DXREGISTER_SET %#x.\n", r);
292             return "unrecognized";
293     }
294 }
295 
296 #undef WINE_D3DX_TO_STR
297 
298 /***********************************************************************
299  * D3DXDebugMute
300  * Returns always FALSE for us.
301  */
302 BOOL WINAPI D3DXDebugMute(BOOL mute)
303 {
304     return FALSE;
305 }
306 
307 /***********************************************************************
308  * D3DXGetDriverLevel.
309  * Returns always 900 (DX 9) for us
310  */
311 UINT WINAPI D3DXGetDriverLevel(struct IDirect3DDevice9 *device)
312 {
313     return 900;
314 }
315