d3dx9/tests: Add effect parameter value GetMatrixArray() test.
[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 /************************************************************
26  * pixel format table providing info about number of bytes per pixel,
27  * number of bits per channel and format type.
28  *
29  * Call get_format_info to request information about a specific format.
30  */
31 static const PixelFormatDesc formats[] =
32 {
33    /* format                    bits per channel        shifts per channel   bpp   type        */
34     { D3DFMT_R8G8B8,          {  0,   8,   8,   8 },  {  0,  16,   8,   0 },   3,  FORMAT_ARGB },
35     { D3DFMT_A8R8G8B8,        {  8,   8,   8,   8 },  { 24,  16,   8,   0 },   4,  FORMAT_ARGB },
36     { D3DFMT_X8R8G8B8,        {  0,   8,   8,   8 },  {  0,  16,   8,   0 },   4,  FORMAT_ARGB },
37     { D3DFMT_A8B8G8R8,        {  8,   8,   8,   8 },  { 24,   0,   8,  16 },   4,  FORMAT_ARGB },
38     { D3DFMT_X8B8G8R8,        {  0,   8,   8,   8 },  {  0,   0,   8,  16 },   4,  FORMAT_ARGB },
39     { D3DFMT_R5G6B5,          {  0,   5,   6,   5 },  {  0,  11,   5,   0 },   2,  FORMAT_ARGB },
40     { D3DFMT_X1R5G5B5,        {  0,   5,   5,   5 },  {  0,  10,   5,   0 },   2,  FORMAT_ARGB },
41     { D3DFMT_A1R5G5B5,        {  1,   5,   5,   5 },  { 15,  10,   5,   0 },   2,  FORMAT_ARGB },
42     { D3DFMT_R3G3B2,          {  0,   3,   3,   2 },  {  0,   5,   2,   0 },   1,  FORMAT_ARGB },
43     { D3DFMT_A8R3G3B2,        {  8,   3,   3,   2 },  {  8,   5,   2,   0 },   2,  FORMAT_ARGB },
44     { D3DFMT_A4R4G4B4,        {  4,   4,   4,   4 },  { 12,   8,   4,   0 },   2,  FORMAT_ARGB },
45     { D3DFMT_X4R4G4B4,        {  0,   4,   4,   4 },  {  0,   8,   4,   0 },   2,  FORMAT_ARGB },
46     { D3DFMT_A2R10G10B10,     {  2,  10,  10,  10 },  { 30,  20,  10,   0 },   4,  FORMAT_ARGB },
47     { D3DFMT_A2B10G10R10,     {  2,  10,  10,  10 },  { 30,   0,  10,  20 },   4,  FORMAT_ARGB },
48     { D3DFMT_G16R16,          {  0,  16,  16,   0 },  {  0,   0,  16,   0 },   4,  FORMAT_ARGB },
49     { D3DFMT_A8,              {  8,   0,   0,   0 },  {  0,   0,   0,   0 },   1,  FORMAT_ARGB },
50
51     { D3DFMT_UNKNOWN,         {  0,   0,   0,   0 },  {  0,   0,   0,   0 },   0,  FORMAT_UNKNOWN }, /* marks last element */
52 };
53
54
55 /************************************************************
56  * map_view_of_file
57  *
58  * Loads a file into buffer and stores the number of read bytes in length.
59  *
60  * PARAMS
61  *   filename [I] name of the file to be loaded
62  *   buffer   [O] pointer to destination buffer
63  *   length   [O] size of the obtained data
64  *
65  * RETURNS
66  *   Success: D3D_OK
67  *   Failure:
68  *     see error codes for CreateFileW, GetFileSize, CreateFileMapping and MapViewOfFile
69  *
70  * NOTES
71  *   The caller must UnmapViewOfFile when it doesn't need the data anymore
72  *
73  */
74 HRESULT map_view_of_file(LPCWSTR filename, LPVOID *buffer, DWORD *length)
75 {
76     HANDLE hfile, hmapping = NULL;
77
78     hfile = CreateFileW(filename, GENERIC_READ, FILE_SHARE_READ, 0, OPEN_EXISTING, 0, 0);
79     if(hfile == INVALID_HANDLE_VALUE) goto error;
80
81     *length = GetFileSize(hfile, NULL);
82     if(*length == INVALID_FILE_SIZE) goto error;
83
84     hmapping = CreateFileMappingW(hfile, NULL, PAGE_READONLY, 0, 0, NULL);
85     if(!hmapping) goto error;
86
87     *buffer = MapViewOfFile(hmapping, FILE_MAP_READ, 0, 0, 0);
88     if(*buffer == NULL) goto error;
89
90     CloseHandle(hmapping);
91     CloseHandle(hfile);
92
93     return S_OK;
94
95 error:
96     CloseHandle(hmapping);
97     CloseHandle(hfile);
98     return HRESULT_FROM_WIN32(GetLastError());
99 }
100
101 /************************************************************
102  * load_resource_into_memory
103  *
104  * Loads a resource into buffer and stores the number of
105  * read bytes in length.
106  *
107  * PARAMS
108  *   module  [I] handle to the module
109  *   resinfo [I] handle to the resource's information block
110  *   buffer  [O] pointer to destination buffer
111  *   length  [O] size of the obtained data
112  *
113  * RETURNS
114  *   Success: D3D_OK
115  *   Failure:
116  *     See error codes for SizeofResource, LoadResource and LockResource
117  *
118  * NOTES
119  *   The memory doesn't need to be freed by the caller manually
120  *
121  */
122 HRESULT load_resource_into_memory(HMODULE module, HRSRC resinfo, LPVOID *buffer, DWORD *length)
123 {
124     HGLOBAL resource;
125
126     *length = SizeofResource(module, resinfo);
127     if(*length == 0) return HRESULT_FROM_WIN32(GetLastError());
128
129     resource = LoadResource(module, resinfo);
130     if( !resource ) return HRESULT_FROM_WIN32(GetLastError());
131
132     *buffer = LockResource(resource);
133     if(*buffer == NULL) return HRESULT_FROM_WIN32(GetLastError());
134
135     return S_OK;
136 }
137
138
139 /************************************************************
140  * get_format_info
141  *
142  * Returns information about the specified format.
143  * If the format is unsupported, it's filled with the D3DFMT_UNKNOWN desc.
144  *
145  * PARAMS
146  *   format [I] format whose description is queried
147  *   desc   [O] pointer to a StaticPixelFormatDesc structure
148  *
149  */
150 const PixelFormatDesc *get_format_info(D3DFORMAT format)
151 {
152     unsigned int i = 0;
153     while(formats[i].format != format && formats[i].format != D3DFMT_UNKNOWN) i++;
154     if(formats[i].format == D3DFMT_UNKNOWN)
155         FIXME("Unknown format %#x\n", format);
156     return &formats[i];
157 }
158
159 const PixelFormatDesc *get_format_info_idx(int idx)
160 {
161     if(idx >= sizeof(formats) / sizeof(formats[0]))
162         return NULL;
163     if(formats[idx].format == D3DFMT_UNKNOWN)
164         return NULL;
165     return &formats[idx];
166 }
167
168 #define WINE_D3DX_TO_STR(x) case x: return #x
169
170 const char *debug_d3dxparameter_class(D3DXPARAMETER_CLASS c)
171 {
172     switch (c)
173     {
174         WINE_D3DX_TO_STR(D3DXPC_SCALAR);
175         WINE_D3DX_TO_STR(D3DXPC_VECTOR);
176         WINE_D3DX_TO_STR(D3DXPC_MATRIX_ROWS);
177         WINE_D3DX_TO_STR(D3DXPC_MATRIX_COLUMNS);
178         WINE_D3DX_TO_STR(D3DXPC_OBJECT);
179         WINE_D3DX_TO_STR(D3DXPC_STRUCT);
180         default:
181             FIXME("Unrecognized D3DXPARAMETER_CLASS %#x.\n", c);
182             return "unrecognized";
183     }
184 }
185
186 const char *debug_d3dxparameter_type(D3DXPARAMETER_TYPE t)
187 {
188     switch (t)
189     {
190         WINE_D3DX_TO_STR(D3DXPT_VOID);
191         WINE_D3DX_TO_STR(D3DXPT_BOOL);
192         WINE_D3DX_TO_STR(D3DXPT_INT);
193         WINE_D3DX_TO_STR(D3DXPT_FLOAT);
194         WINE_D3DX_TO_STR(D3DXPT_STRING);
195         WINE_D3DX_TO_STR(D3DXPT_TEXTURE);
196         WINE_D3DX_TO_STR(D3DXPT_TEXTURE1D);
197         WINE_D3DX_TO_STR(D3DXPT_TEXTURE2D);
198         WINE_D3DX_TO_STR(D3DXPT_TEXTURE3D);
199         WINE_D3DX_TO_STR(D3DXPT_TEXTURECUBE);
200         WINE_D3DX_TO_STR(D3DXPT_SAMPLER);
201         WINE_D3DX_TO_STR(D3DXPT_SAMPLER1D);
202         WINE_D3DX_TO_STR(D3DXPT_SAMPLER2D);
203         WINE_D3DX_TO_STR(D3DXPT_SAMPLER3D);
204         WINE_D3DX_TO_STR(D3DXPT_SAMPLERCUBE);
205         WINE_D3DX_TO_STR(D3DXPT_PIXELSHADER);
206         WINE_D3DX_TO_STR(D3DXPT_VERTEXSHADER);
207         WINE_D3DX_TO_STR(D3DXPT_PIXELFRAGMENT);
208         WINE_D3DX_TO_STR(D3DXPT_VERTEXFRAGMENT);
209         WINE_D3DX_TO_STR(D3DXPT_UNSUPPORTED);
210         default:
211            FIXME("Unrecognized D3DXPARAMETER_TYP %#x.\n", t);
212             return "unrecognized";
213     }
214 }
215
216 #undef WINE_D3DX_TO_STR