d3dx9/tests: Remove d3d9 test from d3dx9 tests.
[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 PixelFormatDesc 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_DXT1,        {0,  0,  0,  0}, { 0,  0,  0,  0}, 1, 4, 4,  8, FORMAT_ARGB,    NULL,         NULL      },
67     {D3DFMT_DXT2,        {0,  0,  0,  0}, { 0,  0,  0,  0}, 1, 4, 4, 16, FORMAT_ARGB,    NULL,         NULL      },
68     {D3DFMT_DXT3,        {0,  0,  0,  0}, { 0,  0,  0,  0}, 1, 4, 4, 16, FORMAT_ARGB,    NULL,         NULL      },
69     {D3DFMT_DXT4,        {0,  0,  0,  0}, { 0,  0,  0,  0}, 1, 4, 4, 16, FORMAT_ARGB,    NULL,         NULL      },
70     {D3DFMT_DXT5,        {0,  0,  0,  0}, { 0,  0,  0,  0}, 1, 4, 4, 16, FORMAT_ARGB,    NULL,         NULL      },
71     /* marks last element */
72     {D3DFMT_UNKNOWN,     {0,  0,  0,  0}, { 0,  0,  0,  0}, 0, 1, 1,  0, FORMAT_UNKNOWN, NULL,         NULL      },
73 };
74
75
76 /************************************************************
77  * map_view_of_file
78  *
79  * Loads a file into buffer and stores the number of read bytes in length.
80  *
81  * PARAMS
82  *   filename [I] name of the file to be loaded
83  *   buffer   [O] pointer to destination buffer
84  *   length   [O] size of the obtained data
85  *
86  * RETURNS
87  *   Success: D3D_OK
88  *   Failure:
89  *     see error codes for CreateFileW, GetFileSize, CreateFileMapping and MapViewOfFile
90  *
91  * NOTES
92  *   The caller must UnmapViewOfFile when it doesn't need the data anymore
93  *
94  */
95 HRESULT map_view_of_file(LPCWSTR filename, LPVOID *buffer, DWORD *length)
96 {
97     HANDLE hfile, hmapping = NULL;
98
99     hfile = CreateFileW(filename, GENERIC_READ, FILE_SHARE_READ, 0, OPEN_EXISTING, 0, 0);
100     if(hfile == INVALID_HANDLE_VALUE) goto error;
101
102     *length = GetFileSize(hfile, NULL);
103     if(*length == INVALID_FILE_SIZE) goto error;
104
105     hmapping = CreateFileMappingW(hfile, NULL, PAGE_READONLY, 0, 0, NULL);
106     if(!hmapping) goto error;
107
108     *buffer = MapViewOfFile(hmapping, FILE_MAP_READ, 0, 0, 0);
109     if(*buffer == NULL) goto error;
110
111     CloseHandle(hmapping);
112     CloseHandle(hfile);
113
114     return S_OK;
115
116 error:
117     CloseHandle(hmapping);
118     CloseHandle(hfile);
119     return HRESULT_FROM_WIN32(GetLastError());
120 }
121
122 /************************************************************
123  * load_resource_into_memory
124  *
125  * Loads a resource into buffer and stores the number of
126  * read bytes in length.
127  *
128  * PARAMS
129  *   module  [I] handle to the module
130  *   resinfo [I] handle to the resource's information block
131  *   buffer  [O] pointer to destination buffer
132  *   length  [O] size of the obtained data
133  *
134  * RETURNS
135  *   Success: D3D_OK
136  *   Failure:
137  *     See error codes for SizeofResource, LoadResource and LockResource
138  *
139  * NOTES
140  *   The memory doesn't need to be freed by the caller manually
141  *
142  */
143 HRESULT load_resource_into_memory(HMODULE module, HRSRC resinfo, LPVOID *buffer, DWORD *length)
144 {
145     HGLOBAL resource;
146
147     *length = SizeofResource(module, resinfo);
148     if(*length == 0) return HRESULT_FROM_WIN32(GetLastError());
149
150     resource = LoadResource(module, resinfo);
151     if( !resource ) return HRESULT_FROM_WIN32(GetLastError());
152
153     *buffer = LockResource(resource);
154     if(*buffer == NULL) return HRESULT_FROM_WIN32(GetLastError());
155
156     return S_OK;
157 }
158
159
160 /************************************************************
161  * get_format_info
162  *
163  * Returns information about the specified format.
164  * If the format is unsupported, it's filled with the D3DFMT_UNKNOWN desc.
165  *
166  * PARAMS
167  *   format [I] format whose description is queried
168  *   desc   [O] pointer to a StaticPixelFormatDesc structure
169  *
170  */
171 const PixelFormatDesc *get_format_info(D3DFORMAT format)
172 {
173     unsigned int i = 0;
174     while(formats[i].format != format && formats[i].format != D3DFMT_UNKNOWN) i++;
175     if (formats[i].format == D3DFMT_UNKNOWN)
176         FIXME("Unknown format %#x (as FOURCC %s).\n", format, debugstr_an((const char *)&format, 4));
177     return &formats[i];
178 }
179
180 const PixelFormatDesc *get_format_info_idx(int idx)
181 {
182     if(idx >= sizeof(formats) / sizeof(formats[0]))
183         return NULL;
184     if(formats[idx].format == D3DFMT_UNKNOWN)
185         return NULL;
186     return &formats[idx];
187 }
188
189 #define WINE_D3DX_TO_STR(x) case x: return #x
190
191 const char *debug_d3dxparameter_class(D3DXPARAMETER_CLASS c)
192 {
193     switch (c)
194     {
195         WINE_D3DX_TO_STR(D3DXPC_SCALAR);
196         WINE_D3DX_TO_STR(D3DXPC_VECTOR);
197         WINE_D3DX_TO_STR(D3DXPC_MATRIX_ROWS);
198         WINE_D3DX_TO_STR(D3DXPC_MATRIX_COLUMNS);
199         WINE_D3DX_TO_STR(D3DXPC_OBJECT);
200         WINE_D3DX_TO_STR(D3DXPC_STRUCT);
201         default:
202             FIXME("Unrecognized D3DXPARAMETER_CLASS %#x.\n", c);
203             return "unrecognized";
204     }
205 }
206
207 const char *debug_d3dxparameter_type(D3DXPARAMETER_TYPE t)
208 {
209     switch (t)
210     {
211         WINE_D3DX_TO_STR(D3DXPT_VOID);
212         WINE_D3DX_TO_STR(D3DXPT_BOOL);
213         WINE_D3DX_TO_STR(D3DXPT_INT);
214         WINE_D3DX_TO_STR(D3DXPT_FLOAT);
215         WINE_D3DX_TO_STR(D3DXPT_STRING);
216         WINE_D3DX_TO_STR(D3DXPT_TEXTURE);
217         WINE_D3DX_TO_STR(D3DXPT_TEXTURE1D);
218         WINE_D3DX_TO_STR(D3DXPT_TEXTURE2D);
219         WINE_D3DX_TO_STR(D3DXPT_TEXTURE3D);
220         WINE_D3DX_TO_STR(D3DXPT_TEXTURECUBE);
221         WINE_D3DX_TO_STR(D3DXPT_SAMPLER);
222         WINE_D3DX_TO_STR(D3DXPT_SAMPLER1D);
223         WINE_D3DX_TO_STR(D3DXPT_SAMPLER2D);
224         WINE_D3DX_TO_STR(D3DXPT_SAMPLER3D);
225         WINE_D3DX_TO_STR(D3DXPT_SAMPLERCUBE);
226         WINE_D3DX_TO_STR(D3DXPT_PIXELSHADER);
227         WINE_D3DX_TO_STR(D3DXPT_VERTEXSHADER);
228         WINE_D3DX_TO_STR(D3DXPT_PIXELFRAGMENT);
229         WINE_D3DX_TO_STR(D3DXPT_VERTEXFRAGMENT);
230         WINE_D3DX_TO_STR(D3DXPT_UNSUPPORTED);
231         default:
232            FIXME("Unrecognized D3DXPARAMETER_TYP %#x.\n", t);
233             return "unrecognized";
234     }
235 }
236
237 #undef WINE_D3DX_TO_STR