1 /*
2  * Copyright (C) 2002 Raphael Junqueira
3  * Copyright (C) 2008 David Adam
4  * Copyright (C) 2008 Tony Wasserka
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19  *
20  */
21 
22 #ifndef __WINE_D3DX9_PRIVATE_H
23 #define __WINE_D3DX9_PRIVATE_H
24 
25 #define NONAMELESSUNION
26 #include "wine/debug.h"
27 #include "wine/heap.h"
28 #include "wine/rbtree.h"
29 
30 #define COBJMACROS
31 #include "d3dx9.h"
32 
33 #define ULONG64_MAX (~(ULONG64)0)
34 
35 struct vec4
36 {
37     float x, y, z, w;
38 };
39 
40 struct volume
41 {
42     UINT width;
43     UINT height;
44     UINT depth;
45 };
46 
47 /* for internal use */
48 enum format_type {
49     FORMAT_ARGB,   /* unsigned */
50     FORMAT_ARGBF16,/* float 16 */
51     FORMAT_ARGBF,  /* float */
52     FORMAT_DXT,
53     FORMAT_INDEX,
54     FORMAT_UNKNOWN
55 };
56 
57 struct pixel_format_desc {
58     D3DFORMAT format;
59     BYTE bits[4];
60     BYTE shift[4];
61     UINT bytes_per_pixel;
62     UINT block_width;
63     UINT block_height;
64     UINT block_byte_count;
65     enum format_type type;
66     void (*from_rgba)(const struct vec4 *src, struct vec4 *dst);
67     void (*to_rgba)(const struct vec4 *src, struct vec4 *dst, const PALETTEENTRY *palette);
68 };
69 
70 struct d3dx_include_from_file
71 {
72     ID3DXInclude ID3DXInclude_iface;
73 };
74 
75 extern CRITICAL_SECTION from_file_mutex DECLSPEC_HIDDEN;
76 extern const struct ID3DXIncludeVtbl d3dx_include_from_file_vtbl DECLSPEC_HIDDEN;
77 
78 static inline BOOL is_conversion_from_supported(const struct pixel_format_desc *format)
79 {
80     if (format->type == FORMAT_ARGB || format->type == FORMAT_ARGBF16
81             || format->type == FORMAT_ARGBF)
82         return TRUE;
83     return !!format->to_rgba;
84 }
85 
86 static inline BOOL is_conversion_to_supported(const struct pixel_format_desc *format)
87 {
88     if (format->type == FORMAT_ARGB || format->type == FORMAT_ARGBF16
89             || format->type == FORMAT_ARGBF)
90         return TRUE;
91     return !!format->from_rgba;
92 }
93 
94 HRESULT map_view_of_file(const WCHAR *filename, void **buffer, DWORD *length) DECLSPEC_HIDDEN;
95 HRESULT load_resource_into_memory(HMODULE module, HRSRC resinfo, void **buffer, DWORD *length) DECLSPEC_HIDDEN;
96 
97 HRESULT write_buffer_to_file(const WCHAR *filename, ID3DXBuffer *buffer) DECLSPEC_HIDDEN;
98 
99 const struct pixel_format_desc *get_format_info(D3DFORMAT format) DECLSPEC_HIDDEN;
100 const struct pixel_format_desc *get_format_info_idx(int idx) DECLSPEC_HIDDEN;
101 
102 void copy_pixels(const BYTE *src, UINT src_row_pitch, UINT src_slice_pitch,
103     BYTE *dst, UINT dst_row_pitch, UINT dst_slice_pitch, const struct volume *size,
104     const struct pixel_format_desc *format) DECLSPEC_HIDDEN;
105 void convert_argb_pixels(const BYTE *src, UINT src_row_pitch, UINT src_slice_pitch,
106     const struct volume *src_size, const struct pixel_format_desc *src_format,
107     BYTE *dst, UINT dst_row_pitch, UINT dst_slice_pitch, const struct volume *dst_size,
108     const struct pixel_format_desc *dst_format, D3DCOLOR color_key, const PALETTEENTRY *palette) DECLSPEC_HIDDEN;
109 void point_filter_argb_pixels(const BYTE *src, UINT src_row_pitch, UINT src_slice_pitch,
110     const struct volume *src_size, const struct pixel_format_desc *src_format,
111     BYTE *dst, UINT dst_row_pitch, UINT dst_slice_pitch, const struct volume *dst_size,
112     const struct pixel_format_desc *dst_format, D3DCOLOR color_key, const PALETTEENTRY *palette) DECLSPEC_HIDDEN;
113 
114 HRESULT load_texture_from_dds(IDirect3DTexture9 *texture, const void *src_data, const PALETTEENTRY *palette,
115         DWORD filter, D3DCOLOR color_key, const D3DXIMAGE_INFO *src_info, unsigned int skip_levels,
116         unsigned int *loaded_miplevels) DECLSPEC_HIDDEN;
117 HRESULT load_cube_texture_from_dds(IDirect3DCubeTexture9 *cube_texture, const void *src_data,
118     const PALETTEENTRY *palette, DWORD filter, D3DCOLOR color_key, const D3DXIMAGE_INFO *src_info) DECLSPEC_HIDDEN;
119 HRESULT load_volume_from_dds(IDirect3DVolume9 *dst_volume, const PALETTEENTRY *dst_palette,
120     const D3DBOX *dst_box, const void *src_data, const D3DBOX *src_box, DWORD filter, D3DCOLOR color_key,
121     const D3DXIMAGE_INFO *src_info) DECLSPEC_HIDDEN;
122 HRESULT load_volume_texture_from_dds(IDirect3DVolumeTexture9 *volume_texture, const void *src_data,
123     const PALETTEENTRY *palette, DWORD filter, DWORD color_key, const D3DXIMAGE_INFO *src_info) DECLSPEC_HIDDEN;
124 HRESULT lock_surface(IDirect3DSurface9 *surface, D3DLOCKED_RECT *lock,
125         IDirect3DSurface9 **temp_surface, BOOL write) DECLSPEC_HIDDEN;
126 HRESULT unlock_surface(IDirect3DSurface9 *surface, D3DLOCKED_RECT *lock,
127         IDirect3DSurface9 *temp_surface, BOOL update) DECLSPEC_HIDDEN;
128 
129 unsigned short float_32_to_16(const float in) DECLSPEC_HIDDEN;
130 float float_16_to_32(const unsigned short in) DECLSPEC_HIDDEN;
131 
132 /* debug helpers */
133 const char *debug_d3dxparameter_class(D3DXPARAMETER_CLASS c) DECLSPEC_HIDDEN;
134 const char *debug_d3dxparameter_type(D3DXPARAMETER_TYPE t) DECLSPEC_HIDDEN;
135 const char *debug_d3dxparameter_registerset(D3DXREGISTER_SET r) DECLSPEC_HIDDEN;
136 
137 /* parameter type conversion helpers */
138 static inline BOOL get_bool(D3DXPARAMETER_TYPE type, const void *data)
139 {
140     switch (type)
141     {
142         case D3DXPT_FLOAT:
143         case D3DXPT_INT:
144         case D3DXPT_BOOL:
145             return !!*(DWORD *)data;
146 
147         case D3DXPT_VOID:
148             return *(BOOL *)data;
149 
150         default:
151             return FALSE;
152     }
153 }
154 
155 static inline int get_int(D3DXPARAMETER_TYPE type, const void *data)
156 {
157     switch (type)
158     {
159         case D3DXPT_FLOAT:
160             return (int)(*(float *)data);
161 
162         case D3DXPT_INT:
163         case D3DXPT_VOID:
164             return *(int *)data;
165 
166         case D3DXPT_BOOL:
167             return get_bool(type, data);
168 
169         default:
170             return 0;
171     }
172 }
173 
174 static inline float get_float(D3DXPARAMETER_TYPE type, const void *data)
175 {
176     switch (type)
177     {
178         case D3DXPT_FLOAT:
179         case D3DXPT_VOID:
180             return *(float *)data;
181 
182         case D3DXPT_INT:
183             return (float)(*(int *)data);
184 
185         case D3DXPT_BOOL:
186             return (float)get_bool(type, data);
187 
188         default:
189             return 0.0f;
190     }
191 }
192 
193 static inline void set_number(void *outdata, D3DXPARAMETER_TYPE outtype, const void *indata, D3DXPARAMETER_TYPE intype)
194 {
195     if (outtype == intype)
196     {
197         *(DWORD *)outdata = *(DWORD *)indata;
198         return;
199     }
200 
201     switch (outtype)
202     {
203         case D3DXPT_FLOAT:
204             *(float *)outdata = get_float(intype, indata);
205             break;
206 
207         case D3DXPT_BOOL:
208             *(BOOL *)outdata = get_bool(intype, indata);
209             break;
210 
211         case D3DXPT_INT:
212             *(int *)outdata = get_int(intype, indata);
213             break;
214 
215         default:
216             *(DWORD *)outdata = 0;
217             break;
218     }
219 }
220 
221 static inline BOOL is_param_type_sampler(D3DXPARAMETER_TYPE type)
222 {
223     return type == D3DXPT_SAMPLER
224             || type == D3DXPT_SAMPLER1D || type == D3DXPT_SAMPLER2D
225             || type == D3DXPT_SAMPLER3D || type == D3DXPT_SAMPLERCUBE;
226 }
227 
228 struct d3dx_parameter;
229 
230 enum pres_reg_tables
231 {
232     PRES_REGTAB_IMMED,
233     PRES_REGTAB_CONST,
234     PRES_REGTAB_OCONST,
235     PRES_REGTAB_OBCONST,
236     PRES_REGTAB_OICONST,
237     PRES_REGTAB_TEMP,
238     PRES_REGTAB_COUNT,
239     PRES_REGTAB_FIRST_SHADER = PRES_REGTAB_CONST,
240 };
241 
242 struct d3dx_const_param_eval_output
243 {
244     struct d3dx_parameter *param;
245     enum pres_reg_tables table;
246     enum D3DXPARAMETER_CLASS constant_class;
247     unsigned int register_index;
248     unsigned int register_count;
249     BOOL direct_copy;
250     unsigned int element_count;
251 };
252 
253 struct d3dx_const_tab
254 {
255     unsigned int input_count;
256     D3DXCONSTANT_DESC *inputs;
257     struct d3dx_parameter **inputs_param;
258     unsigned int const_set_count;
259     unsigned int const_set_size;
260     struct d3dx_const_param_eval_output *const_set;
261     const enum pres_reg_tables *regset2table;
262     ULONG64 update_version;
263 };
264 
265 struct d3dx_regstore
266 {
267     void *tables[PRES_REGTAB_COUNT];
268     unsigned int table_sizes[PRES_REGTAB_COUNT]; /* registers count */
269 };
270 
271 struct d3dx_pres_ins;
272 
273 struct d3dx_preshader
274 {
275     struct d3dx_regstore regs;
276 
277     unsigned int ins_count;
278     struct d3dx_pres_ins *ins;
279 
280     struct d3dx_const_tab inputs;
281 };
282 
283 struct d3dx_param_eval
284 {
285     D3DXPARAMETER_TYPE param_type;
286 
287     struct d3dx_preshader pres;
288     struct d3dx_const_tab shader_inputs;
289 
290     ULONG64 *version_counter;
291 };
292 
293 struct param_rb_entry
294 {
295     struct wine_rb_entry entry;
296     char *full_name;
297     struct d3dx_parameter *param;
298 };
299 
300 struct d3dx_shared_data;
301 struct d3dx_top_level_parameter;
302 
303 struct d3dx_parameter
304 {
305     char magic_string[4];
306     struct d3dx_top_level_parameter *top_level_param;
307     struct d3dx_param_eval *param_eval;
308     char *name;
309     void *data;
310     D3DXPARAMETER_CLASS class;
311     D3DXPARAMETER_TYPE  type;
312     UINT rows;
313     UINT columns;
314     UINT element_count;
315     UINT member_count;
316     DWORD flags;
317     UINT bytes;
318     DWORD object_id;
319 
320     struct d3dx_parameter *members;
321     char *semantic;
322 
323     char *full_name;
324     struct wine_rb_entry rb_entry;
325 };
326 
327 struct d3dx_top_level_parameter
328 {
329     struct d3dx_parameter param;
330     UINT annotation_count;
331     struct d3dx_parameter *annotations;
332     ULONG64 update_version;
333     ULONG64 *version_counter;
334     struct d3dx_shared_data *shared_data;
335 };
336 
337 struct d3dx_shared_data
338 {
339     void *data;
340     struct d3dx_top_level_parameter **parameters;
341     unsigned int size, count;
342     ULONG64 update_version;
343 };
344 
345 struct d3dx9_base_effect;
346 
347 static inline BOOL is_top_level_parameter(struct d3dx_parameter *param)
348 {
349     return &param->top_level_param->param == param;
350 }
351 
352 static inline struct d3dx_top_level_parameter
353         *top_level_parameter_from_parameter(struct d3dx_parameter *param)
354 {
355     return CONTAINING_RECORD(param, struct d3dx_top_level_parameter, param);
356 }
357 
358 static inline ULONG64 next_update_version(ULONG64 *version_counter)
359 {
360     return ++*version_counter;
361 }
362 
363 static inline BOOL is_top_level_param_dirty(struct d3dx_top_level_parameter *param, ULONG64 update_version)
364 {
365     struct d3dx_shared_data *shared_data;
366 
367     if ((shared_data = param->shared_data))
368         return update_version < shared_data->update_version;
369     else
370         return update_version < param->update_version;
371 }
372 
373 static inline BOOL is_param_dirty(struct d3dx_parameter *param, ULONG64 update_version)
374 {
375     return is_top_level_param_dirty(param->top_level_param, update_version);
376 }
377 
378 struct d3dx_parameter *get_parameter_by_name(struct d3dx9_base_effect *base,
379         struct d3dx_parameter *parameter, const char *name) DECLSPEC_HIDDEN;
380 
381 #ifdef __REACTOS__
382 #define SET_D3D_STATE_(_manager, _device, _method, ...) ((_manager) ? (_manager)->lpVtbl->_method((_manager), __VA_ARGS__) \
383         : (_device)->lpVtbl->_method((_device), __VA_ARGS__))
384 #define SET_D3D_STATE(_base_effect, _method, ...) SET_D3D_STATE_((_base_effect)->manager, (_base_effect)->device, _method, __VA_ARGS__)
385 #else
386 #define SET_D3D_STATE_(manager, device, method, args...) (manager ? manager->lpVtbl->method(manager, args) \
387         : device->lpVtbl->method(device, args))
388 #define SET_D3D_STATE(base_effect, args...) SET_D3D_STATE_(base_effect->manager, base_effect->device, args)
389 #endif
390 
391 HRESULT d3dx_create_param_eval(struct d3dx9_base_effect *base_effect, void *byte_code,
392         unsigned int byte_code_size, D3DXPARAMETER_TYPE type,
393         struct d3dx_param_eval **peval, ULONG64 *version_counter,
394         const char **skip_constants, unsigned int skip_constants_count) DECLSPEC_HIDDEN;
395 void d3dx_free_param_eval(struct d3dx_param_eval *peval) DECLSPEC_HIDDEN;
396 HRESULT d3dx_evaluate_parameter(struct d3dx_param_eval *peval,
397         const struct d3dx_parameter *param, void *param_value) DECLSPEC_HIDDEN;
398 HRESULT d3dx_param_eval_set_shader_constants(ID3DXEffectStateManager *manager, struct IDirect3DDevice9 *device,
399         struct d3dx_param_eval *peval, BOOL update_all) DECLSPEC_HIDDEN;
400 BOOL is_param_eval_input_dirty(struct d3dx_param_eval *peval, ULONG64 update_version) DECLSPEC_HIDDEN;
401 
402 struct ctab_constant {
403     D3DXCONSTANT_DESC desc;
404     WORD constantinfo_reserved;
405     struct ctab_constant *constants;
406 };
407 
408 const struct ctab_constant *d3dx_shader_get_ctab_constant(ID3DXConstantTable *iface,
409         D3DXHANDLE constant) DECLSPEC_HIDDEN;
410 
411 #endif /* __WINE_D3DX9_PRIVATE_H */
412