VirtualBox

source: vbox/trunk/src/VBox/Additions/WINNT/Graphics/Wine/d3d9/vertexdeclaration.c@ 19844

最後變更 在這個檔案從19844是 19678,由 vboxsync 提交於 16 年 前

opengl: update wine to 1.1.21, add d3d9.dll to build list

  • 屬性 svn:eol-style 設為 native
檔案大小: 17.3 KB
 
1/*
2 * IDirect3DVertexDeclaration9 implementation
3 *
4 * Copyright 2002-2003 Raphael Junqueira
5 * Jason Edmeades
6 *
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
11 *
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20 */
21
22/*
23 * Sun LGPL Disclaimer: For the avoidance of doubt, except that if any license choice
24 * other than GPL or LGPL is available it will apply instead, Sun elects to use only
25 * the Lesser General Public License version 2.1 (LGPLv2) at this time for any software where
26 * a choice of LGPL license versions is made available with the language indicating
27 * that LGPLv2 or any later version may be used, or where a choice of which version
28 * of the LGPL is applied is otherwise unspecified.
29 */
30
31#include "config.h"
32#include "d3d9_private.h"
33
34WINE_DEFAULT_DEBUG_CHANNEL(d3d9);
35
36typedef struct _D3DDECLTYPE_INFO {
37 D3DDECLTYPE d3dType;
38 WINED3DFORMAT format;
39 int size;
40 int typesize;
41} D3DDECLTYPE_INFO;
42
43static D3DDECLTYPE_INFO const d3d_dtype_lookup[D3DDECLTYPE_UNUSED] = {
44 {D3DDECLTYPE_FLOAT1, WINED3DFMT_R32_FLOAT, 1, sizeof(float)},
45 {D3DDECLTYPE_FLOAT2, WINED3DFMT_R32G32_FLOAT, 2, sizeof(float)},
46 {D3DDECLTYPE_FLOAT3, WINED3DFMT_R32G32B32_FLOAT, 3, sizeof(float)},
47 {D3DDECLTYPE_FLOAT4, WINED3DFMT_R32G32B32A32_FLOAT, 4, sizeof(float)},
48 {D3DDECLTYPE_D3DCOLOR, WINED3DFMT_A8R8G8B8, 4, sizeof(BYTE)},
49 {D3DDECLTYPE_UBYTE4, WINED3DFMT_R8G8B8A8_UINT, 4, sizeof(BYTE)},
50 {D3DDECLTYPE_SHORT2, WINED3DFMT_R16G16_SINT, 2, sizeof(short int)},
51 {D3DDECLTYPE_SHORT4, WINED3DFMT_R16G16B16A16_SINT, 4, sizeof(short int)},
52 {D3DDECLTYPE_UBYTE4N, WINED3DFMT_R8G8B8A8_UNORM, 4, sizeof(BYTE)},
53 {D3DDECLTYPE_SHORT2N, WINED3DFMT_R16G16_SNORM, 2, sizeof(short int)},
54 {D3DDECLTYPE_SHORT4N, WINED3DFMT_R16G16B16A16_SNORM, 4, sizeof(short int)},
55 {D3DDECLTYPE_USHORT2N, WINED3DFMT_R16G16_UNORM, 2, sizeof(short int)},
56 {D3DDECLTYPE_USHORT4N, WINED3DFMT_R16G16B16A16_UNORM, 4, sizeof(short int)},
57 {D3DDECLTYPE_UDEC3, WINED3DFMT_R10G10B10A2_UINT, 3, sizeof(short int)},
58 {D3DDECLTYPE_DEC3N, WINED3DFMT_R10G10B10A2_SNORM, 3, sizeof(short int)},
59 {D3DDECLTYPE_FLOAT16_2, WINED3DFMT_R16G16_FLOAT, 2, sizeof(short int)},
60 {D3DDECLTYPE_FLOAT16_4, WINED3DFMT_R16G16B16A16_FLOAT, 4, sizeof(short int)}};
61
62#define D3D_DECL_SIZE(type) d3d_dtype_lookup[type].size
63#define D3D_DECL_TYPESIZE(type) d3d_dtype_lookup[type].typesize
64
65HRESULT vdecl_convert_fvf(
66 DWORD fvf,
67 D3DVERTEXELEMENT9** ppVertexElements) {
68
69 unsigned int idx, idx2;
70 unsigned int offset;
71 BOOL has_pos = (fvf & D3DFVF_POSITION_MASK) != 0;
72 BOOL has_blend = (fvf & D3DFVF_XYZB5) > D3DFVF_XYZRHW;
73 BOOL has_blend_idx = has_blend &&
74 (((fvf & D3DFVF_XYZB5) == D3DFVF_XYZB5) ||
75 (fvf & D3DFVF_LASTBETA_D3DCOLOR) ||
76 (fvf & D3DFVF_LASTBETA_UBYTE4));
77 BOOL has_normal = (fvf & D3DFVF_NORMAL) != 0;
78 BOOL has_psize = (fvf & D3DFVF_PSIZE) != 0;
79
80 BOOL has_diffuse = (fvf & D3DFVF_DIFFUSE) != 0;
81 BOOL has_specular = (fvf & D3DFVF_SPECULAR) !=0;
82
83 DWORD num_textures = (fvf & D3DFVF_TEXCOUNT_MASK) >> D3DFVF_TEXCOUNT_SHIFT;
84 DWORD texcoords = (fvf & 0xFFFF0000) >> 16;
85
86 D3DVERTEXELEMENT9 end_element = D3DDECL_END();
87 D3DVERTEXELEMENT9 *elements = NULL;
88
89 unsigned int size;
90 DWORD num_blends = 1 + (((fvf & D3DFVF_XYZB5) - D3DFVF_XYZB1) >> 1);
91 if (has_blend_idx) num_blends--;
92
93 /* Compute declaration size */
94 size = has_pos + (has_blend && num_blends > 0) + has_blend_idx + has_normal +
95 has_psize + has_diffuse + has_specular + num_textures + 1;
96
97 /* convert the declaration */
98 elements = HeapAlloc(GetProcessHeap(), 0, size * sizeof(D3DVERTEXELEMENT9));
99 if (!elements)
100 return D3DERR_OUTOFVIDEOMEMORY;
101
102 elements[size-1] = end_element;
103 idx = 0;
104 if (has_pos) {
105 if (!has_blend && (fvf & D3DFVF_XYZRHW)) {
106 elements[idx].Type = D3DDECLTYPE_FLOAT4;
107 elements[idx].Usage = D3DDECLUSAGE_POSITIONT;
108 }
109 else if (!has_blend && (fvf & D3DFVF_XYZW) == D3DFVF_XYZW) {
110 elements[idx].Type = D3DDECLTYPE_FLOAT4;
111 elements[idx].Usage = D3DDECLUSAGE_POSITION;
112 }
113 else {
114 elements[idx].Type = D3DDECLTYPE_FLOAT3;
115 elements[idx].Usage = D3DDECLUSAGE_POSITION;
116 }
117 elements[idx].UsageIndex = 0;
118 idx++;
119 }
120 if (has_blend && (num_blends > 0)) {
121 if (((fvf & D3DFVF_XYZB5) == D3DFVF_XYZB2) && (fvf & D3DFVF_LASTBETA_D3DCOLOR))
122 elements[idx].Type = D3DDECLTYPE_D3DCOLOR;
123 else {
124 switch(num_blends) {
125 case 1: elements[idx].Type = D3DDECLTYPE_FLOAT1; break;
126 case 2: elements[idx].Type = D3DDECLTYPE_FLOAT2; break;
127 case 3: elements[idx].Type = D3DDECLTYPE_FLOAT3; break;
128 case 4: elements[idx].Type = D3DDECLTYPE_FLOAT4; break;
129 default:
130 ERR("Unexpected amount of blend values: %u\n", num_blends);
131 }
132 }
133 elements[idx].Usage = D3DDECLUSAGE_BLENDWEIGHT;
134 elements[idx].UsageIndex = 0;
135 idx++;
136 }
137 if (has_blend_idx) {
138 if (fvf & D3DFVF_LASTBETA_UBYTE4 ||
139 (((fvf & D3DFVF_XYZB5) == D3DFVF_XYZB2) && (fvf & D3DFVF_LASTBETA_D3DCOLOR)))
140 elements[idx].Type = D3DDECLTYPE_UBYTE4;
141 else if (fvf & D3DFVF_LASTBETA_D3DCOLOR)
142 elements[idx].Type = D3DDECLTYPE_D3DCOLOR;
143 else
144 elements[idx].Type = D3DDECLTYPE_FLOAT1;
145 elements[idx].Usage = D3DDECLUSAGE_BLENDINDICES;
146 elements[idx].UsageIndex = 0;
147 idx++;
148 }
149 if (has_normal) {
150 elements[idx].Type = D3DDECLTYPE_FLOAT3;
151 elements[idx].Usage = D3DDECLUSAGE_NORMAL;
152 elements[idx].UsageIndex = 0;
153 idx++;
154 }
155 if (has_psize) {
156 elements[idx].Type = D3DDECLTYPE_FLOAT1;
157 elements[idx].Usage = D3DDECLUSAGE_PSIZE;
158 elements[idx].UsageIndex = 0;
159 idx++;
160 }
161 if (has_diffuse) {
162 elements[idx].Type = D3DDECLTYPE_D3DCOLOR;
163 elements[idx].Usage = D3DDECLUSAGE_COLOR;
164 elements[idx].UsageIndex = 0;
165 idx++;
166 }
167 if (has_specular) {
168 elements[idx].Type = D3DDECLTYPE_D3DCOLOR;
169 elements[idx].Usage = D3DDECLUSAGE_COLOR;
170 elements[idx].UsageIndex = 1;
171 idx++;
172 }
173 for (idx2 = 0; idx2 < num_textures; idx2++) {
174 unsigned int numcoords = (texcoords >> (idx2*2)) & 0x03;
175 switch (numcoords) {
176 case D3DFVF_TEXTUREFORMAT1:
177 elements[idx].Type = D3DDECLTYPE_FLOAT1;
178 break;
179 case D3DFVF_TEXTUREFORMAT2:
180 elements[idx].Type = D3DDECLTYPE_FLOAT2;
181 break;
182 case D3DFVF_TEXTUREFORMAT3:
183 elements[idx].Type = D3DDECLTYPE_FLOAT3;
184 break;
185 case D3DFVF_TEXTUREFORMAT4:
186 elements[idx].Type = D3DDECLTYPE_FLOAT4;
187 break;
188 }
189 elements[idx].Usage = D3DDECLUSAGE_TEXCOORD;
190 elements[idx].UsageIndex = idx2;
191 idx++;
192 }
193
194 /* Now compute offsets, and initialize the rest of the fields */
195 for (idx = 0, offset = 0; idx < size-1; idx++) {
196 elements[idx].Stream = 0;
197 elements[idx].Method = D3DDECLMETHOD_DEFAULT;
198 elements[idx].Offset = offset;
199 offset += D3D_DECL_SIZE(elements[idx].Type) * D3D_DECL_TYPESIZE(elements[idx].Type);
200 }
201
202 *ppVertexElements = elements;
203 return D3D_OK;
204}
205
206/* IDirect3DVertexDeclaration9 IUnknown parts follow: */
207static HRESULT WINAPI IDirect3DVertexDeclaration9Impl_QueryInterface(LPDIRECT3DVERTEXDECLARATION9 iface, REFIID riid, LPVOID* ppobj) {
208 IDirect3DVertexDeclaration9Impl *This = (IDirect3DVertexDeclaration9Impl *)iface;
209
210 if (IsEqualGUID(riid, &IID_IUnknown)
211 || IsEqualGUID(riid, &IID_IDirect3DVertexDeclaration9)) {
212 IDirect3DVertexDeclaration9_AddRef(iface);
213 *ppobj = This;
214 return S_OK;
215 }
216
217 WARN("(%p)->(%s,%p),not found\n", This, debugstr_guid(riid), ppobj);
218 *ppobj = NULL;
219 return E_NOINTERFACE;
220}
221
222static ULONG WINAPI IDirect3DVertexDeclaration9Impl_AddRef(LPDIRECT3DVERTEXDECLARATION9 iface) {
223 IDirect3DVertexDeclaration9Impl *This = (IDirect3DVertexDeclaration9Impl *)iface;
224 ULONG ref = InterlockedIncrement(&This->ref);
225
226 TRACE("(%p) : AddRef from %d\n", This, ref - 1);
227
228 if(ref == 1) {
229 IDirect3DDevice9Ex_AddRef(This->parentDevice);
230 }
231
232 return ref;
233}
234
235void IDirect3DVertexDeclaration9Impl_Destroy(LPDIRECT3DVERTEXDECLARATION9 iface) {
236 IDirect3DVertexDeclaration9Impl *This = (IDirect3DVertexDeclaration9Impl *)iface;
237
238 if(This->ref != 0) {
239 /* Should not happen unless wine has a bug or the application releases references it does not own */
240 ERR("Destroying vdecl with ref != 0\n");
241 }
242 EnterCriticalSection(&d3d9_cs);
243 IWineD3DVertexDeclaration_Release(This->wineD3DVertexDeclaration);
244 LeaveCriticalSection(&d3d9_cs);
245 HeapFree(GetProcessHeap(), 0, This->elements);
246 HeapFree(GetProcessHeap(), 0, This);
247}
248
249static ULONG WINAPI IDirect3DVertexDeclaration9Impl_Release(LPDIRECT3DVERTEXDECLARATION9 iface) {
250 IDirect3DVertexDeclaration9Impl *This = (IDirect3DVertexDeclaration9Impl *)iface;
251 ULONG ref = InterlockedDecrement(&This->ref);
252
253 TRACE("(%p) : ReleaseRef to %d\n", This, ref);
254
255 if (ref == 0) {
256 IDirect3DDevice9Ex *parentDevice = This->parentDevice;
257
258 if(!This->convFVF) {
259 IDirect3DVertexDeclaration9Impl_Release(iface);
260 }
261 IDirect3DDevice9Ex_Release(parentDevice);
262 }
263 return ref;
264}
265
266/* IDirect3DVertexDeclaration9 Interface follow: */
267static HRESULT WINAPI IDirect3DVertexDeclaration9Impl_GetDevice(LPDIRECT3DVERTEXDECLARATION9 iface, IDirect3DDevice9** ppDevice) {
268 IDirect3DVertexDeclaration9Impl *This = (IDirect3DVertexDeclaration9Impl *)iface;
269 IWineD3DDevice *myDevice = NULL;
270 HRESULT hr = D3D_OK;
271
272 TRACE("(%p) : Relay\n", iface);
273
274 EnterCriticalSection(&d3d9_cs);
275 hr = IWineD3DVertexDeclaration_GetDevice(This->wineD3DVertexDeclaration, &myDevice);
276 if (hr == D3D_OK && myDevice != NULL) {
277 hr = IWineD3DDevice_GetParent(myDevice, (IUnknown **)ppDevice);
278 IWineD3DDevice_Release(myDevice);
279 }
280 LeaveCriticalSection(&d3d9_cs);
281 return hr;
282}
283
284static HRESULT WINAPI IDirect3DVertexDeclaration9Impl_GetDeclaration(LPDIRECT3DVERTEXDECLARATION9 iface, D3DVERTEXELEMENT9* pDecl, UINT* pNumElements) {
285 IDirect3DVertexDeclaration9Impl *This = (IDirect3DVertexDeclaration9Impl *)iface;
286
287 TRACE("(%p) : pDecl %p, pNumElements %p)\n", This, pDecl, pNumElements);
288
289 *pNumElements = This->element_count;
290
291 /* Passing a NULL pDecl is used to just retrieve the number of elements */
292 if (!pDecl) {
293 TRACE("NULL pDecl passed. Returning D3D_OK.\n");
294 return D3D_OK;
295 }
296
297 TRACE("Copying %p to %p\n", This->elements, pDecl);
298 CopyMemory(pDecl, This->elements, This->element_count * sizeof(D3DVERTEXELEMENT9));
299
300 return D3D_OK;
301}
302
303static const IDirect3DVertexDeclaration9Vtbl Direct3DVertexDeclaration9_Vtbl =
304{
305 /* IUnknown */
306 IDirect3DVertexDeclaration9Impl_QueryInterface,
307 IDirect3DVertexDeclaration9Impl_AddRef,
308 IDirect3DVertexDeclaration9Impl_Release,
309 /* IDirect3DVertexDeclaration9 */
310 IDirect3DVertexDeclaration9Impl_GetDevice,
311 IDirect3DVertexDeclaration9Impl_GetDeclaration
312};
313
314static HRESULT convert_to_wined3d_declaration(const D3DVERTEXELEMENT9* d3d9_elements,
315 WINED3DVERTEXELEMENT **wined3d_elements, UINT *element_count)
316{
317 const D3DVERTEXELEMENT9* element;
318 UINT count = 1;
319 UINT i;
320
321 TRACE("d3d9_elements %p, wined3d_elements %p\n", d3d9_elements, wined3d_elements);
322
323 element = d3d9_elements;
324 while (element++->Stream != 0xff && count++ < 128);
325
326 if (count == 128) return E_FAIL;
327
328 /* Skip the END element */
329 --count;
330
331 *wined3d_elements = HeapAlloc(GetProcessHeap(), 0, count * sizeof(WINED3DVERTEXELEMENT));
332 if (!*wined3d_elements) {
333 FIXME("Memory allocation failed\n");
334 return D3DERR_OUTOFVIDEOMEMORY;
335 }
336
337 for (i = 0; i < count; ++i)
338 {
339 if (d3d9_elements[i].Type >= (sizeof(d3d_dtype_lookup) / sizeof(*d3d_dtype_lookup)))
340 {
341 WARN("Invalid element type %#x.\n", d3d9_elements[i].Type);
342 HeapFree(GetProcessHeap(), 0, *wined3d_elements);
343 return E_FAIL;
344 }
345 (*wined3d_elements)[i].format = d3d_dtype_lookup[d3d9_elements[i].Type].format;
346 (*wined3d_elements)[i].input_slot = d3d9_elements[i].Stream;
347 (*wined3d_elements)[i].offset = d3d9_elements[i].Offset;
348 (*wined3d_elements)[i].output_slot = ~0U;
349 (*wined3d_elements)[i].method = d3d9_elements[i].Method;
350 (*wined3d_elements)[i].usage = d3d9_elements[i].Usage;
351 (*wined3d_elements)[i].usage_idx = d3d9_elements[i].UsageIndex;
352 }
353
354 *element_count = count;
355
356 return D3D_OK;
357}
358
359/* IDirect3DDevice9 IDirect3DVertexDeclaration9 Methods follow: */
360HRESULT WINAPI IDirect3DDevice9Impl_CreateVertexDeclaration(LPDIRECT3DDEVICE9EX iface, CONST D3DVERTEXELEMENT9* pVertexElements, IDirect3DVertexDeclaration9** ppDecl) {
361
362 IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
363 IDirect3DVertexDeclaration9Impl *object = NULL;
364 WINED3DVERTEXELEMENT* wined3d_elements;
365 UINT wined3d_element_count;
366 UINT element_count;
367 HRESULT hr;
368
369 TRACE("(%p) : Relay\n", iface);
370 if (NULL == ppDecl) {
371 WARN("(%p) : Caller passed NULL As ppDecl, returning D3DERR_INVALIDCALL\n",This);
372 return D3DERR_INVALIDCALL;
373 }
374
375 hr = convert_to_wined3d_declaration(pVertexElements, &wined3d_elements, &wined3d_element_count);
376 if (FAILED(hr))
377 {
378 WARN("(%p) : Error parsing vertex declaration\n", This);
379 return hr;
380 }
381
382 /* Allocate the storage for the device */
383 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(IDirect3DVertexDeclaration9Impl));
384 if (NULL == object) {
385 HeapFree(GetProcessHeap(), 0, wined3d_elements);
386 FIXME("Allocation of memory failed, returning D3DERR_OUTOFVIDEOMEMORY\n");
387 return D3DERR_OUTOFVIDEOMEMORY;
388 }
389
390 object->lpVtbl = &Direct3DVertexDeclaration9_Vtbl;
391 object->ref = 0;
392
393 element_count = wined3d_element_count + 1;
394 object->elements = HeapAlloc(GetProcessHeap(), 0, element_count * sizeof(D3DVERTEXELEMENT9));
395 if (!object->elements) {
396 HeapFree(GetProcessHeap(), 0, wined3d_elements);
397 HeapFree(GetProcessHeap(), 0, object);
398 ERR("Memory allocation failed\n");
399 return D3DERR_OUTOFVIDEOMEMORY;
400 }
401 CopyMemory(object->elements, pVertexElements, element_count * sizeof(D3DVERTEXELEMENT9));
402 object->element_count = element_count;
403
404 EnterCriticalSection(&d3d9_cs);
405 hr = IWineD3DDevice_CreateVertexDeclaration(This->WineD3DDevice, &object->wineD3DVertexDeclaration,
406 (IUnknown *)object, wined3d_elements, wined3d_element_count);
407 LeaveCriticalSection(&d3d9_cs);
408
409 HeapFree(GetProcessHeap(), 0, wined3d_elements);
410
411 if (FAILED(hr)) {
412
413 /* free up object */
414 WARN("(%p) call to IWineD3DDevice_CreateVertexDeclaration failed\n", This);
415 HeapFree(GetProcessHeap(), 0, object->elements);
416 HeapFree(GetProcessHeap(), 0, object);
417 } else {
418 object->parentDevice = iface;
419 *ppDecl = (LPDIRECT3DVERTEXDECLARATION9) object;
420 IDirect3DVertexDeclaration9_AddRef(*ppDecl);
421 TRACE("(%p) : Created vertex declaration %p\n", This, object);
422 }
423 return hr;
424}
425
426HRESULT WINAPI IDirect3DDevice9Impl_SetVertexDeclaration(LPDIRECT3DDEVICE9EX iface, IDirect3DVertexDeclaration9* pDecl) {
427 IDirect3DDevice9Impl *This = (IDirect3DDevice9Impl *)iface;
428 IDirect3DVertexDeclaration9Impl *pDeclImpl = (IDirect3DVertexDeclaration9Impl *)pDecl;
429 HRESULT hr = D3D_OK;
430
431 TRACE("(%p) : Relay\n", iface);
432
433 EnterCriticalSection(&d3d9_cs);
434 hr = IWineD3DDevice_SetVertexDeclaration(This->WineD3DDevice, pDeclImpl == NULL ? NULL : pDeclImpl->wineD3DVertexDeclaration);
435 LeaveCriticalSection(&d3d9_cs);
436 return hr;
437}
438
439HRESULT WINAPI IDirect3DDevice9Impl_GetVertexDeclaration(LPDIRECT3DDEVICE9EX iface, IDirect3DVertexDeclaration9** ppDecl) {
440 IDirect3DDevice9Impl* This = (IDirect3DDevice9Impl*) iface;
441 IWineD3DVertexDeclaration* pTest = NULL;
442 HRESULT hr = D3D_OK;
443
444 TRACE("(%p) : Relay+\n", iface);
445
446 if (NULL == ppDecl) {
447 return D3DERR_INVALIDCALL;
448 }
449
450 *ppDecl = NULL;
451 EnterCriticalSection(&d3d9_cs);
452 hr = IWineD3DDevice_GetVertexDeclaration(This->WineD3DDevice, &pTest);
453 if (hr == D3D_OK && NULL != pTest) {
454 IWineD3DVertexDeclaration_GetParent(pTest, (IUnknown **)ppDecl);
455 IWineD3DVertexDeclaration_Release(pTest);
456 } else {
457 *ppDecl = NULL;
458 }
459 LeaveCriticalSection(&d3d9_cs);
460 TRACE("(%p) : returning %p\n", This, *ppDecl);
461 return hr;
462}
注意: 瀏覽 TracBrowser 來幫助您使用儲存庫瀏覽器

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette