2 * Copyright 2007 Henri Verbeet
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
19 #include "d3d8_private.h"
21 WINE_DEFAULT_DEBUG_CHANNEL(d3d8
);
23 static const char *debug_d3dvsdt_type(D3DVSDT_TYPE d3dvsdt_type
)
27 #define D3DVSDT_TYPE_TO_STR(u) case u: return #u
28 D3DVSDT_TYPE_TO_STR(D3DVSDT_FLOAT1
);
29 D3DVSDT_TYPE_TO_STR(D3DVSDT_FLOAT2
);
30 D3DVSDT_TYPE_TO_STR(D3DVSDT_FLOAT3
);
31 D3DVSDT_TYPE_TO_STR(D3DVSDT_FLOAT4
);
32 D3DVSDT_TYPE_TO_STR(D3DVSDT_D3DCOLOR
);
33 D3DVSDT_TYPE_TO_STR(D3DVSDT_UBYTE4
);
34 D3DVSDT_TYPE_TO_STR(D3DVSDT_SHORT2
);
35 D3DVSDT_TYPE_TO_STR(D3DVSDT_SHORT4
);
36 #undef D3DVSDT_TYPE_TO_STR
38 FIXME("Unrecognized D3DVSDT_TYPE %#x\n", d3dvsdt_type
);
39 return "unrecognized";
43 static const char *debug_d3dvsde_register(D3DVSDE_REGISTER d3dvsde_register
)
45 switch (d3dvsde_register
)
47 #define D3DVSDE_REGISTER_TO_STR(u) case u: return #u
48 D3DVSDE_REGISTER_TO_STR(D3DVSDE_POSITION
);
49 D3DVSDE_REGISTER_TO_STR(D3DVSDE_BLENDWEIGHT
);
50 D3DVSDE_REGISTER_TO_STR(D3DVSDE_BLENDINDICES
);
51 D3DVSDE_REGISTER_TO_STR(D3DVSDE_NORMAL
);
52 D3DVSDE_REGISTER_TO_STR(D3DVSDE_PSIZE
);
53 D3DVSDE_REGISTER_TO_STR(D3DVSDE_DIFFUSE
);
54 D3DVSDE_REGISTER_TO_STR(D3DVSDE_SPECULAR
);
55 D3DVSDE_REGISTER_TO_STR(D3DVSDE_TEXCOORD0
);
56 D3DVSDE_REGISTER_TO_STR(D3DVSDE_TEXCOORD1
);
57 D3DVSDE_REGISTER_TO_STR(D3DVSDE_TEXCOORD2
);
58 D3DVSDE_REGISTER_TO_STR(D3DVSDE_TEXCOORD3
);
59 D3DVSDE_REGISTER_TO_STR(D3DVSDE_TEXCOORD4
);
60 D3DVSDE_REGISTER_TO_STR(D3DVSDE_TEXCOORD5
);
61 D3DVSDE_REGISTER_TO_STR(D3DVSDE_TEXCOORD6
);
62 D3DVSDE_REGISTER_TO_STR(D3DVSDE_TEXCOORD7
);
63 D3DVSDE_REGISTER_TO_STR(D3DVSDE_POSITION2
);
64 D3DVSDE_REGISTER_TO_STR(D3DVSDE_NORMAL2
);
65 #undef D3DVSDE_REGISTER_TO_STR
67 FIXME("Unrecognized D3DVSDE_REGISTER %#x\n", d3dvsde_register
);
68 return "unrecognized";
72 size_t parse_token(const DWORD
* pToken
)
74 const DWORD token
= *pToken
;
77 switch ((token
& D3DVSD_TOKENTYPEMASK
) >> D3DVSD_TOKENTYPESHIFT
) { /* maybe a macro to inverse ... */
78 case D3DVSD_TOKEN_NOP
:
79 TRACE(" 0x%08x NOP()\n", token
);
82 case D3DVSD_TOKEN_STREAM
:
83 if (token
& D3DVSD_STREAMTESSMASK
)
85 TRACE(" 0x%08x STREAM_TESS()\n", token
);
87 TRACE(" 0x%08x STREAM(%u)\n", token
, ((token
& D3DVSD_STREAMNUMBERMASK
) >> D3DVSD_STREAMNUMBERSHIFT
));
91 case D3DVSD_TOKEN_STREAMDATA
:
92 if (token
& 0x10000000)
94 TRACE(" 0x%08x SKIP(%u)\n", token
, ((token
& D3DVSD_SKIPCOUNTMASK
) >> D3DVSD_SKIPCOUNTSHIFT
));
96 DWORD type
= ((token
& D3DVSD_DATATYPEMASK
) >> D3DVSD_DATATYPESHIFT
);
97 DWORD reg
= ((token
& D3DVSD_VERTEXREGMASK
) >> D3DVSD_VERTEXREGSHIFT
);
98 TRACE(" 0x%08x REG(%s, %s)\n", token
, debug_d3dvsde_register(reg
), debug_d3dvsdt_type(type
));
102 case D3DVSD_TOKEN_TESSELLATOR
:
103 if (token
& 0x10000000)
105 DWORD type
= ((token
& D3DVSD_DATATYPEMASK
) >> D3DVSD_DATATYPESHIFT
);
106 DWORD reg
= ((token
& D3DVSD_VERTEXREGMASK
) >> D3DVSD_VERTEXREGSHIFT
);
107 TRACE(" 0x%08x TESSUV(%s) as %s\n", token
, debug_d3dvsde_register(reg
), debug_d3dvsdt_type(type
));
109 DWORD type
= ((token
& D3DVSD_DATATYPEMASK
) >> D3DVSD_DATATYPESHIFT
);
110 DWORD regout
= ((token
& D3DVSD_VERTEXREGMASK
) >> D3DVSD_VERTEXREGSHIFT
);
111 DWORD regin
= ((token
& D3DVSD_VERTEXREGINMASK
) >> D3DVSD_VERTEXREGINSHIFT
);
112 TRACE(" 0x%08x TESSNORMAL(%s, %s) as %s\n", token
, debug_d3dvsde_register(regin
),
113 debug_d3dvsde_register(regout
), debug_d3dvsdt_type(type
));
117 case D3DVSD_TOKEN_CONSTMEM
:
119 DWORD count
= ((token
& D3DVSD_CONSTCOUNTMASK
) >> D3DVSD_CONSTCOUNTSHIFT
);
120 tokenlen
= (4 * count
) + 1;
124 case D3DVSD_TOKEN_EXT
:
126 DWORD count
= ((token
& D3DVSD_CONSTCOUNTMASK
) >> D3DVSD_CONSTCOUNTSHIFT
);
127 DWORD extinfo
= ((token
& D3DVSD_EXTINFOMASK
) >> D3DVSD_EXTINFOSHIFT
);
128 TRACE(" 0x%08x EXT(%u, %u)\n", token
, count
, extinfo
);
129 /* todo ... print extension */
130 tokenlen
= count
+ 1;
134 case D3DVSD_TOKEN_END
:
135 TRACE(" 0x%08x END()\n", token
);
139 TRACE(" 0x%08x UNKNOWN\n", token
);
146 void load_local_constants(const DWORD
*d3d8_elements
, struct wined3d_shader
*wined3d_vertex_shader
)
148 const DWORD
*token
= d3d8_elements
;
150 while (*token
!= D3DVSD_END())
152 if (((*token
& D3DVSD_TOKENTYPEMASK
) >> D3DVSD_TOKENTYPESHIFT
) == D3DVSD_TOKEN_CONSTMEM
)
154 DWORD count
= ((*token
& D3DVSD_CONSTCOUNTMASK
) >> D3DVSD_CONSTCOUNTSHIFT
);
155 DWORD constant_idx
= ((*token
& D3DVSD_CONSTADDRESSMASK
) >> D3DVSD_CONSTADDRESSSHIFT
);
161 for (i
= 0; i
< count
; ++i
)
163 TRACE("c[%u] = (%8f, %8f, %8f, %8f)\n",
165 *(const float *)(token
+ i
* 4 + 1),
166 *(const float *)(token
+ i
* 4 + 2),
167 *(const float *)(token
+ i
* 4 + 3),
168 *(const float *)(token
+ i
* 4 + 4));
171 hr
= wined3d_shader_set_local_constants_float(wined3d_vertex_shader
,
172 constant_idx
, (const float *)token
+ 1, count
);
173 if (FAILED(hr
)) ERR("Failed setting shader constants\n");
176 token
+= parse_token(token
);
180 /* NOTE: Make sure these are in the correct numerical order. (see /include/wined3d_types.h) */
181 static const size_t wined3d_type_sizes
[] =
183 /*WINED3DDECLTYPE_FLOAT1*/ 1 * sizeof(float),
184 /*WINED3DDECLTYPE_FLOAT2*/ 2 * sizeof(float),
185 /*WINED3DDECLTYPE_FLOAT3*/ 3 * sizeof(float),
186 /*WINED3DDECLTYPE_FLOAT4*/ 4 * sizeof(float),
187 /*WINED3DDECLTYPE_D3DCOLOR*/ 4 * sizeof(BYTE
),
188 /*WINED3DDECLTYPE_UBYTE4*/ 4 * sizeof(BYTE
),
189 /*WINED3DDECLTYPE_SHORT2*/ 2 * sizeof(short int),
190 /*WINED3DDECLTYPE_SHORT4*/ 4 * sizeof(short int),
191 /*WINED3DDECLTYPE_UBYTE4N*/ 4 * sizeof(BYTE
),
192 /*WINED3DDECLTYPE_SHORT2N*/ 2 * sizeof(short int),
193 /*WINED3DDECLTYPE_SHORT4N*/ 4 * sizeof(short int),
194 /*WINED3DDECLTYPE_USHORT2N*/ 2 * sizeof(short int),
195 /*WINED3DDECLTYPE_USHORT4N*/ 4 * sizeof(short int),
196 /*WINED3DDECLTYPE_UDEC3*/ 3 * sizeof(short int),
197 /*WINED3DDECLTYPE_DEC3N*/ 3 * sizeof(short int),
198 /*WINED3DDECLTYPE_FLOAT16_2*/ 2 * sizeof(short int),
199 /*WINED3DDECLTYPE_FLOAT16_4*/ 4 * sizeof(short int)
202 static const enum wined3d_format_id wined3d_format_lookup
[] =
204 /*WINED3DDECLTYPE_FLOAT1*/ WINED3DFMT_R32_FLOAT
,
205 /*WINED3DDECLTYPE_FLOAT2*/ WINED3DFMT_R32G32_FLOAT
,
206 /*WINED3DDECLTYPE_FLOAT3*/ WINED3DFMT_R32G32B32_FLOAT
,
207 /*WINED3DDECLTYPE_FLOAT4*/ WINED3DFMT_R32G32B32A32_FLOAT
,
208 /*WINED3DDECLTYPE_D3DCOLOR*/ WINED3DFMT_B8G8R8A8_UNORM
,
209 /*WINED3DDECLTYPE_UBYTE4*/ WINED3DFMT_R8G8B8A8_UINT
,
210 /*WINED3DDECLTYPE_SHORT2*/ WINED3DFMT_R16G16_SINT
,
211 /*WINED3DDECLTYPE_SHORT4*/ WINED3DFMT_R16G16B16A16_SINT
,
212 /*WINED3DDECLTYPE_UBYTE4N*/ WINED3DFMT_R8G8B8A8_UNORM
,
213 /*WINED3DDECLTYPE_SHORT2N*/ WINED3DFMT_R16G16_SNORM
,
214 /*WINED3DDECLTYPE_SHORT4N*/ WINED3DFMT_R16G16B16A16_SNORM
,
215 /*WINED3DDECLTYPE_USHORT2N*/ WINED3DFMT_R16G16_UNORM
,
216 /*WINED3DDECLTYPE_USHORT4N*/ WINED3DFMT_R16G16B16A16_UNORM
,
217 /*WINED3DDECLTYPE_UDEC3*/ WINED3DFMT_R10G10B10X2_UINT
,
218 /*WINED3DDECLTYPE_DEC3N*/ WINED3DFMT_R10G10B10X2_SNORM
,
219 /*WINED3DDECLTYPE_FLOAT16_2*/ WINED3DFMT_R16G16_FLOAT
,
220 /*WINED3DDECLTYPE_FLOAT16_4*/ WINED3DFMT_R16G16B16A16_FLOAT
,
228 wined3d_usage_lookup
[] =
230 /* D3DVSDE_POSITION */ {WINED3D_DECL_USAGE_POSITION
, 0},
231 /* D3DVSDE_BLENDWEIGHT */ {WINED3D_DECL_USAGE_BLEND_WEIGHT
, 0},
232 /* D3DVSDE_BLENDINDICES */ {WINED3D_DECL_USAGE_BLEND_INDICES
, 0},
233 /* D3DVSDE_NORMAL */ {WINED3D_DECL_USAGE_NORMAL
, 0},
234 /* D3DVSDE_PSIZE */ {WINED3D_DECL_USAGE_PSIZE
, 0},
235 /* D3DVSDE_DIFFUSE */ {WINED3D_DECL_USAGE_COLOR
, 0},
236 /* D3DVSDE_SPECULAR */ {WINED3D_DECL_USAGE_COLOR
, 1},
237 /* D3DVSDE_TEXCOORD0 */ {WINED3D_DECL_USAGE_TEXCOORD
, 0},
238 /* D3DVSDE_TEXCOORD1 */ {WINED3D_DECL_USAGE_TEXCOORD
, 1},
239 /* D3DVSDE_TEXCOORD2 */ {WINED3D_DECL_USAGE_TEXCOORD
, 2},
240 /* D3DVSDE_TEXCOORD3 */ {WINED3D_DECL_USAGE_TEXCOORD
, 3},
241 /* D3DVSDE_TEXCOORD4 */ {WINED3D_DECL_USAGE_TEXCOORD
, 4},
242 /* D3DVSDE_TEXCOORD5 */ {WINED3D_DECL_USAGE_TEXCOORD
, 5},
243 /* D3DVSDE_TEXCOORD6 */ {WINED3D_DECL_USAGE_TEXCOORD
, 6},
244 /* D3DVSDE_TEXCOORD7 */ {WINED3D_DECL_USAGE_TEXCOORD
, 7},
245 /* D3DVSDE_POSITION2 */ {WINED3D_DECL_USAGE_POSITION
, 1},
246 /* D3DVSDE_NORMAL2 */ {WINED3D_DECL_USAGE_NORMAL
, 1},
249 /* TODO: find out where rhw (or positionT) is for declaration8 */
250 static UINT
convert_to_wined3d_declaration(const DWORD
*d3d8_elements
, DWORD
*d3d8_elements_size
,
251 struct wined3d_vertex_element
**wined3d_elements
, DWORD
*stream_map
)
253 struct wined3d_vertex_element
*element
;
254 const DWORD
*token
= d3d8_elements
;
255 D3DVSD_TOKENTYPE token_type
;
256 unsigned int element_count
= 0;
260 TRACE("d3d8_elements %p, d3d8_elements_size %p, wined3d_elements %p\n", d3d8_elements
, d3d8_elements_size
, wined3d_elements
);
263 /* 128 should be enough for anyone... */
264 *wined3d_elements
= heap_alloc_zero(128 * sizeof(**wined3d_elements
));
265 while (D3DVSD_END() != *token
)
267 token_type
= ((*token
& D3DVSD_TOKENTYPEMASK
) >> D3DVSD_TOKENTYPESHIFT
);
269 if (token_type
== D3DVSD_TOKEN_STREAM
&& !(*token
& D3DVSD_STREAMTESSMASK
))
271 stream
= ((*token
& D3DVSD_STREAMNUMBERMASK
) >> D3DVSD_STREAMNUMBERSHIFT
);
273 } else if (token_type
== D3DVSD_TOKEN_STREAMDATA
&& !(*token
& D3DVSD_DATALOADTYPEMASK
)) {
274 DWORD type
= ((*token
& D3DVSD_DATATYPEMASK
) >> D3DVSD_DATATYPESHIFT
);
275 DWORD reg
= ((*token
& D3DVSD_VERTEXREGMASK
) >> D3DVSD_VERTEXREGSHIFT
);
277 TRACE("Adding element %d:\n", element_count
);
279 element
= *wined3d_elements
+ element_count
++;
280 element
->format
= wined3d_format_lookup
[type
];
281 element
->input_slot
= stream
;
282 element
->offset
= offset
;
283 element
->output_slot
= reg
;
284 element
->input_slot_class
= WINED3D_INPUT_PER_VERTEX_DATA
;
285 element
->instance_data_step_rate
= 0;
286 element
->method
= WINED3D_DECL_METHOD_DEFAULT
;
287 element
->usage
= wined3d_usage_lookup
[reg
].usage
;
288 element
->usage_idx
= wined3d_usage_lookup
[reg
].usage_idx
;
290 *stream_map
|= 1u << stream
;
292 offset
+= wined3d_type_sizes
[type
];
293 } else if (token_type
== D3DVSD_TOKEN_STREAMDATA
&& (*token
& D3DVSD_DATALOADTYPEMASK
)) {
294 TRACE(" 0x%08x SKIP(%u)\n", *token
, (*token
& D3DVSD_SKIPCOUNTMASK
) >> D3DVSD_SKIPCOUNTSHIFT
);
295 offset
+= sizeof(DWORD
) * ((*token
& D3DVSD_SKIPCOUNTMASK
) >> D3DVSD_SKIPCOUNTSHIFT
);
298 if (element_count
>= 127) {
299 ERR("More than 127 elements?\n");
303 token
+= parse_token(token
);
306 *d3d8_elements_size
= (++token
- d3d8_elements
) * sizeof(DWORD
);
308 return element_count
;
311 static void STDMETHODCALLTYPE
d3d8_vertexdeclaration_wined3d_object_destroyed(void *parent
)
313 struct d3d8_vertex_declaration
*declaration
= parent
;
314 heap_free(declaration
->elements
);
315 heap_free(declaration
);
318 void d3d8_vertex_declaration_destroy(struct d3d8_vertex_declaration
*declaration
)
320 TRACE("declaration %p.\n", declaration
);
322 wined3d_mutex_lock();
323 wined3d_vertex_declaration_decref(declaration
->wined3d_vertex_declaration
);
324 wined3d_mutex_unlock();
327 static const struct wined3d_parent_ops d3d8_vertexdeclaration_wined3d_parent_ops
=
329 d3d8_vertexdeclaration_wined3d_object_destroyed
,
332 HRESULT
d3d8_vertex_declaration_init(struct d3d8_vertex_declaration
*declaration
,
333 struct d3d8_device
*device
, const DWORD
*elements
, DWORD shader_handle
)
335 struct wined3d_vertex_element
*wined3d_elements
;
336 UINT wined3d_element_count
;
339 declaration
->shader_handle
= shader_handle
;
341 wined3d_element_count
= convert_to_wined3d_declaration(elements
, &declaration
->elements_size
,
342 &wined3d_elements
, &declaration
->stream_map
);
343 if (!(declaration
->elements
= heap_alloc(declaration
->elements_size
)))
345 ERR("Failed to allocate vertex declaration elements memory.\n");
346 heap_free(wined3d_elements
);
347 return E_OUTOFMEMORY
;
350 memcpy(declaration
->elements
, elements
, declaration
->elements_size
);
352 wined3d_mutex_lock();
353 hr
= wined3d_vertex_declaration_create(device
->wined3d_device
, wined3d_elements
, wined3d_element_count
,
354 declaration
, &d3d8_vertexdeclaration_wined3d_parent_ops
, &declaration
->wined3d_vertex_declaration
);
355 wined3d_mutex_unlock();
356 heap_free(wined3d_elements
);
359 WARN("Failed to create wined3d vertex declaration, hr %#x.\n", hr
);
360 heap_free(declaration
->elements
);
367 HRESULT
d3d8_vertex_declaration_init_fvf(struct d3d8_vertex_declaration
*declaration
,
368 struct d3d8_device
*device
, DWORD fvf
)
372 declaration
->elements
= NULL
;
373 declaration
->elements_size
= 0;
374 declaration
->stream_map
= 1;
375 declaration
->shader_handle
= fvf
;
377 hr
= wined3d_vertex_declaration_create_from_fvf(device
->wined3d_device
, fvf
, declaration
,
378 &d3d8_vertexdeclaration_wined3d_parent_ops
, &declaration
->wined3d_vertex_declaration
);
381 WARN("Failed to create wined3d vertex declaration, hr %#x.\n", hr
);