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