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