d3dx9: Add support for D3DFMT_A16B16G16R16F.
[wine] / dlls / d3dx9_36 / util.c
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 "wine/debug.h"
21 #include "d3dx9_36_private.h"
22
23 WINE_DEFAULT_DEBUG_CHANNEL(d3dx);
24
25 static void la_from_rgba(const struct vec4 *rgba, struct vec4 *la)
26 {
27     la->x = rgba->x * 0.2125f + rgba->y * 0.7154f + rgba->z * 0.0721f;
28     la->w = rgba->w;
29 }
30
31 static void la_to_rgba(const struct vec4 *la, struct vec4 *rgba)
32 {
33     rgba->x = la->x;
34     rgba->y = la->x;
35     rgba->z = la->x;
36     rgba->w = la->w;
37 }
38
39 /************************************************************
40  * pixel format table providing info about number of bytes per pixel,
41  * number of bits per channel and format type.
42  *
43  * Call get_format_info to request information about a specific format.
44  */
45 static const struct pixel_format_desc formats[] =
46 {
47     /* format              bpc               shifts             bpp blocks   type            from_rgba     to_rgba */
48     {D3DFMT_R8G8B8,        { 0,  8,  8,  8}, { 0, 16,  8,  0},  3, 1, 1,  3, FORMAT_ARGB,    NULL,         NULL      },
49     {D3DFMT_A8R8G8B8,      { 8,  8,  8,  8}, {24, 16,  8,  0},  4, 1, 1,  4, FORMAT_ARGB,    NULL,         NULL      },
50     {D3DFMT_X8R8G8B8,      { 0,  8,  8,  8}, { 0, 16,  8,  0},  4, 1, 1,  4, FORMAT_ARGB,    NULL,         NULL      },
51     {D3DFMT_A8B8G8R8,      { 8,  8,  8,  8}, {24,  0,  8, 16},  4, 1, 1,  4, FORMAT_ARGB,    NULL,         NULL      },
52     {D3DFMT_X8B8G8R8,      { 0,  8,  8,  8}, { 0,  0,  8, 16},  4, 1, 1,  4, FORMAT_ARGB,    NULL,         NULL      },
53     {D3DFMT_R5G6B5,        { 0,  5,  6,  5}, { 0, 11,  5,  0},  2, 1, 1,  2, FORMAT_ARGB,    NULL,         NULL      },
54     {D3DFMT_X1R5G5B5,      { 0,  5,  5,  5}, { 0, 10,  5,  0},  2, 1, 1,  2, FORMAT_ARGB,    NULL,         NULL      },
55     {D3DFMT_A1R5G5B5,      { 1,  5,  5,  5}, {15, 10,  5,  0},  2, 1, 1,  2, FORMAT_ARGB,    NULL,         NULL      },
56     {D3DFMT_R3G3B2,        { 0,  3,  3,  2}, { 0,  5,  2,  0},  1, 1, 1,  1, FORMAT_ARGB,    NULL,         NULL      },
57     {D3DFMT_A8R3G3B2,      { 8,  3,  3,  2}, { 8,  5,  2,  0},  2, 1, 1,  2, FORMAT_ARGB,    NULL,         NULL      },
58     {D3DFMT_A4R4G4B4,      { 4,  4,  4,  4}, {12,  8,  4,  0},  2, 1, 1,  2, FORMAT_ARGB,    NULL,         NULL      },
59     {D3DFMT_X4R4G4B4,      { 0,  4,  4,  4}, { 0,  8,  4,  0},  2, 1, 1,  2, FORMAT_ARGB,    NULL,         NULL      },
60     {D3DFMT_A2R10G10B10,   { 2, 10, 10, 10}, {30, 20, 10,  0},  4, 1, 1,  4, FORMAT_ARGB,    NULL,         NULL      },
61     {D3DFMT_A2B10G10R10,   { 2, 10, 10, 10}, {30,  0, 10, 20},  4, 1, 1,  4, FORMAT_ARGB,    NULL,         NULL      },
62     {D3DFMT_G16R16,        { 0, 16, 16,  0}, { 0,  0, 16,  0},  4, 1, 1,  4, FORMAT_ARGB,    NULL,         NULL      },
63     {D3DFMT_A8,            { 8,  0,  0,  0}, { 0,  0,  0,  0},  1, 1, 1,  1, FORMAT_ARGB,    NULL,         NULL      },
64     {D3DFMT_A8L8,          { 8,  8,  0,  0}, { 8,  0,  0,  0},  2, 1, 1,  2, FORMAT_ARGB,    la_from_rgba, la_to_rgba},
65     {D3DFMT_A4L4,          { 4,  4,  0,  0}, { 4,  0,  0,  0},  1, 1, 1,  1, FORMAT_ARGB,    la_from_rgba, la_to_rgba},
66     {D3DFMT_L8,            { 0,  8,  0,  0}, { 0,  0,  0,  0},  1, 1, 1,  1, FORMAT_ARGB,    la_from_rgba, la_to_rgba},
67     {D3DFMT_L16,           { 0, 16,  0,  0}, { 0,  0,  0,  0},  2, 1, 1,  2, FORMAT_ARGB,    la_from_rgba, la_to_rgba},
68     {D3DFMT_DXT1,          { 0,  0,  0,  0}, { 0,  0,  0,  0},  1, 4, 4,  8, FORMAT_DXT,     NULL,         NULL      },
69     {D3DFMT_DXT2,          { 0,  0,  0,  0}, { 0,  0,  0,  0},  1, 4, 4, 16, FORMAT_DXT,     NULL,         NULL      },
70     {D3DFMT_DXT3,          { 0,  0,  0,  0}, { 0,  0,  0,  0},  1, 4, 4, 16, FORMAT_DXT,     NULL,         NULL      },
71     {D3DFMT_DXT4,          { 0,  0,  0,  0}, { 0,  0,  0,  0},  1, 4, 4, 16, FORMAT_DXT,     NULL,         NULL      },
72     {D3DFMT_DXT5,          { 0,  0,  0,  0}, { 0,  0,  0,  0},  1, 4, 4, 16, FORMAT_DXT,     NULL,         NULL      },
73     {D3DFMT_A16B16G16R16F, {16, 16, 16, 16}, {48,  0, 16, 32},  8, 1, 1,  8, FORMAT_ARGBF16, NULL,         NULL      },
74     /* marks last element */
75     {D3DFMT_UNKNOWN,       { 0,  0,  0,  0}, { 0,  0,  0,  0},  0, 1, 1,  0, FORMAT_UNKNOWN, NULL,         NULL      },
76 };
77
78
79 /************************************************************
80  * map_view_of_file
81  *
82  * Loads a file into buffer and stores the number of read bytes in length.
83  *
84  * PARAMS
85  *   filename [I] name of the file to be loaded
86  *   buffer   [O] pointer to destination buffer
87  *   length   [O] size of the obtained data
88  *
89  * RETURNS
90  *   Success: D3D_OK
91  *   Failure:
92  *     see error codes for CreateFileW, GetFileSize, CreateFileMapping and MapViewOfFile
93  *
94  * NOTES
95  *   The caller must UnmapViewOfFile when it doesn't need the data anymore
96  *
97  */
98 HRESULT map_view_of_file(LPCWSTR filename, LPVOID *buffer, DWORD *length)
99 {
100     HANDLE hfile, hmapping = NULL;
101
102     hfile = CreateFileW(filename, GENERIC_READ, FILE_SHARE_READ, 0, OPEN_EXISTING, 0, 0);
103     if(hfile == INVALID_HANDLE_VALUE) goto error;
104
105     *length = GetFileSize(hfile, NULL);
106     if(*length == INVALID_FILE_SIZE) goto error;
107
108     hmapping = CreateFileMappingW(hfile, NULL, PAGE_READONLY, 0, 0, NULL);
109     if(!hmapping) goto error;
110
111     *buffer = MapViewOfFile(hmapping, FILE_MAP_READ, 0, 0, 0);
112     if(*buffer == NULL) goto error;
113
114     CloseHandle(hmapping);
115     CloseHandle(hfile);
116
117     return S_OK;
118
119 error:
120     CloseHandle(hmapping);
121     CloseHandle(hfile);
122     return HRESULT_FROM_WIN32(GetLastError());
123 }
124
125 /************************************************************
126  * load_resource_into_memory
127  *
128  * Loads a resource into buffer and stores the number of
129  * read bytes in length.
130  *
131  * PARAMS
132  *   module  [I] handle to the module
133  *   resinfo [I] handle to the resource's information block
134  *   buffer  [O] pointer to destination buffer
135  *   length  [O] size of the obtained data
136  *
137  * RETURNS
138  *   Success: D3D_OK
139  *   Failure:
140  *     See error codes for SizeofResource, LoadResource and LockResource
141  *
142  * NOTES
143  *   The memory doesn't need to be freed by the caller manually
144  *
145  */
146 HRESULT load_resource_into_memory(HMODULE module, HRSRC resinfo, LPVOID *buffer, DWORD *length)
147 {
148     HGLOBAL resource;
149
150     *length = SizeofResource(module, resinfo);
151     if(*length == 0) return HRESULT_FROM_WIN32(GetLastError());
152
153     resource = LoadResource(module, resinfo);
154     if( !resource ) return HRESULT_FROM_WIN32(GetLastError());
155
156     *buffer = LockResource(resource);
157     if(*buffer == NULL) return HRESULT_FROM_WIN32(GetLastError());
158
159     return S_OK;
160 }
161
162 HRESULT write_buffer_to_file(const WCHAR *dst_filename, ID3DXBuffer *buffer)
163 {
164     HRESULT hr = S_OK;
165     void *buffer_pointer;
166     DWORD buffer_size;
167     HANDLE file = CreateFileW(dst_filename, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
168     if (file == INVALID_HANDLE_VALUE)
169         return HRESULT_FROM_WIN32(GetLastError());
170
171     buffer_pointer = ID3DXBuffer_GetBufferPointer(buffer);
172     buffer_size = ID3DXBuffer_GetBufferSize(buffer);
173
174     if (!WriteFile(file, buffer_pointer, buffer_size, NULL, NULL))
175         hr = HRESULT_FROM_WIN32(GetLastError());
176
177     CloseHandle(file);
178     return hr;
179 }
180
181
182 /************************************************************
183  * get_format_info
184  *
185  * Returns information about the specified format.
186  * If the format is unsupported, it's filled with the D3DFMT_UNKNOWN desc.
187  *
188  * PARAMS
189  *   format [I] format whose description is queried
190  *
191  */
192 const struct pixel_format_desc *get_format_info(D3DFORMAT format)
193 {
194     unsigned int i = 0;
195     while(formats[i].format != format && formats[i].format != D3DFMT_UNKNOWN) i++;
196     if (formats[i].format == D3DFMT_UNKNOWN)
197         FIXME("Unknown format %#x (as FOURCC %s).\n", format, debugstr_an((const char *)&format, 4));
198     return &formats[i];
199 }
200
201 const struct pixel_format_desc *get_format_info_idx(int idx)
202 {
203     if(idx >= sizeof(formats) / sizeof(formats[0]))
204         return NULL;
205     if(formats[idx].format == D3DFMT_UNKNOWN)
206         return NULL;
207     return &formats[idx];
208 }
209
210 #define WINE_D3DX_TO_STR(x) case x: return #x
211
212 const char *debug_d3dxparameter_class(D3DXPARAMETER_CLASS c)
213 {
214     switch (c)
215     {
216         WINE_D3DX_TO_STR(D3DXPC_SCALAR);
217         WINE_D3DX_TO_STR(D3DXPC_VECTOR);
218         WINE_D3DX_TO_STR(D3DXPC_MATRIX_ROWS);
219         WINE_D3DX_TO_STR(D3DXPC_MATRIX_COLUMNS);
220         WINE_D3DX_TO_STR(D3DXPC_OBJECT);
221         WINE_D3DX_TO_STR(D3DXPC_STRUCT);
222         default:
223             FIXME("Unrecognized D3DXPARAMETER_CLASS %#x.\n", c);
224             return "unrecognized";
225     }
226 }
227
228 const char *debug_d3dxparameter_type(D3DXPARAMETER_TYPE t)
229 {
230     switch (t)
231     {
232         WINE_D3DX_TO_STR(D3DXPT_VOID);
233         WINE_D3DX_TO_STR(D3DXPT_BOOL);
234         WINE_D3DX_TO_STR(D3DXPT_INT);
235         WINE_D3DX_TO_STR(D3DXPT_FLOAT);
236         WINE_D3DX_TO_STR(D3DXPT_STRING);
237         WINE_D3DX_TO_STR(D3DXPT_TEXTURE);
238         WINE_D3DX_TO_STR(D3DXPT_TEXTURE1D);
239         WINE_D3DX_TO_STR(D3DXPT_TEXTURE2D);
240         WINE_D3DX_TO_STR(D3DXPT_TEXTURE3D);
241         WINE_D3DX_TO_STR(D3DXPT_TEXTURECUBE);
242         WINE_D3DX_TO_STR(D3DXPT_SAMPLER);
243         WINE_D3DX_TO_STR(D3DXPT_SAMPLER1D);
244         WINE_D3DX_TO_STR(D3DXPT_SAMPLER2D);
245         WINE_D3DX_TO_STR(D3DXPT_SAMPLER3D);
246         WINE_D3DX_TO_STR(D3DXPT_SAMPLERCUBE);
247         WINE_D3DX_TO_STR(D3DXPT_PIXELSHADER);
248         WINE_D3DX_TO_STR(D3DXPT_VERTEXSHADER);
249         WINE_D3DX_TO_STR(D3DXPT_PIXELFRAGMENT);
250         WINE_D3DX_TO_STR(D3DXPT_VERTEXFRAGMENT);
251         WINE_D3DX_TO_STR(D3DXPT_UNSUPPORTED);
252         default:
253             FIXME("Unrecognized D3DXPARAMETER_TYP %#x.\n", t);
254             return "unrecognized";
255     }
256 }
257
258 const char *debug_d3dxparameter_registerset(D3DXREGISTER_SET r)
259 {
260     switch (r)
261     {
262         WINE_D3DX_TO_STR(D3DXRS_BOOL);
263         WINE_D3DX_TO_STR(D3DXRS_INT4);
264         WINE_D3DX_TO_STR(D3DXRS_FLOAT4);
265         WINE_D3DX_TO_STR(D3DXRS_SAMPLER);
266         default:
267             FIXME("Unrecognized D3DXREGISTER_SET %#x.\n", r);
268             return "unrecognized";
269     }
270 }
271
272 #undef WINE_D3DX_TO_STR
273
274 /* parameter type conversion helpers */
275 static BOOL get_bool(D3DXPARAMETER_TYPE type, LPCVOID data)
276 {
277     switch (type)
278     {
279         case D3DXPT_FLOAT:
280         case D3DXPT_INT:
281         case D3DXPT_BOOL:
282             return *(DWORD *)data != 0;
283
284         case D3DXPT_VOID:
285             return *(BOOL *)data;
286
287         default:
288             FIXME("Unhandled type %s.\n", debug_d3dxparameter_type(type));
289             return FALSE;
290     }
291 }
292
293 static INT get_int(D3DXPARAMETER_TYPE type, LPCVOID data)
294 {
295     switch (type)
296     {
297         case D3DXPT_FLOAT:
298             return *(FLOAT *)data;
299
300         case D3DXPT_INT:
301         case D3DXPT_VOID:
302             return *(INT *)data;
303
304         case D3DXPT_BOOL:
305             return get_bool(type, data);
306
307         default:
308             FIXME("Unhandled type %s.\n", debug_d3dxparameter_type(type));
309             return 0;
310     }
311 }
312
313 static FLOAT get_float(D3DXPARAMETER_TYPE type, LPCVOID data)
314 {
315     switch (type)
316     {
317         case D3DXPT_FLOAT:
318         case D3DXPT_VOID:
319             return *(FLOAT *)data;
320
321         case D3DXPT_INT:
322             return *(INT *)data;
323
324         case D3DXPT_BOOL:
325             return get_bool(type, data);
326
327         default:
328             FIXME("Unhandled type %s.\n", debug_d3dxparameter_type(type));
329             return 0.0f;
330     }
331 }
332
333 void set_number(LPVOID outdata, D3DXPARAMETER_TYPE outtype, LPCVOID indata, D3DXPARAMETER_TYPE intype)
334 {
335     TRACE("Changing from type %s to type %s\n", debug_d3dxparameter_type(intype), debug_d3dxparameter_type(outtype));
336
337     if (outtype == intype)
338     {
339         *(DWORD *)outdata = *(DWORD *)indata;
340         return;
341     }
342
343     switch (outtype)
344     {
345         case D3DXPT_FLOAT:
346             *(FLOAT *)outdata = get_float(intype, indata);
347             break;
348
349         case D3DXPT_BOOL:
350             *(BOOL *)outdata = get_bool(intype, indata);
351             break;
352
353         case D3DXPT_INT:
354             *(INT *)outdata = get_int(intype, indata);
355             break;
356
357         default:
358             FIXME("Unhandled type %s.\n", debug_d3dxparameter_type(outtype));
359             *(DWORD *)outdata = 0;
360             break;
361     }
362 }