2 * vertex declaration implementation
4 * Copyright 2002-2005 Raphael Junqueira
5 * Copyright 2004 Jason Edmeades
6 * Copyright 2004 Christian Costa
7 * Copyright 2005 Oliver Stieber
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
25 #include "wined3d_private.h"
27 WINE_DEFAULT_DEBUG_CHANNEL(d3d_decl
);
29 #define GLINFO_LOCATION This->wineD3DDevice->adapter->gl_info
31 static void dump_wined3dvertexelement(const WINED3DVERTEXELEMENT
*element
) {
32 TRACE(" Stream: %d\n", element
->Stream
);
33 TRACE(" Offset: %d\n", element
->Offset
);
34 TRACE(" Type: %s (%#x)\n", debug_d3ddecltype(element
->Type
), element
->Type
);
35 TRACE(" Method: %s (%#x)\n", debug_d3ddeclmethod(element
->Method
), element
->Method
);
36 TRACE(" Usage: %s (%#x)\n", debug_d3ddeclusage(element
->Usage
), element
->Usage
);
37 TRACE("Usage index: %d\n", element
->UsageIndex
);
38 TRACE(" Register: %d\n", element
->Reg
);
41 /* *******************************************
42 IWineD3DVertexDeclaration IUnknown parts follow
43 ******************************************* */
44 static HRESULT WINAPI
IWineD3DVertexDeclarationImpl_QueryInterface(IWineD3DVertexDeclaration
*iface
, REFIID riid
, LPVOID
*ppobj
)
46 IWineD3DVertexDeclarationImpl
*This
= (IWineD3DVertexDeclarationImpl
*)iface
;
47 TRACE("(%p)->(%s,%p)\n",This
,debugstr_guid(riid
),ppobj
);
48 if (IsEqualGUID(riid
, &IID_IUnknown
)
49 || IsEqualGUID(riid
, &IID_IWineD3DBase
)
50 || IsEqualGUID(riid
, &IID_IWineD3DVertexDeclaration
)){
51 IUnknown_AddRef(iface
);
59 static ULONG WINAPI
IWineD3DVertexDeclarationImpl_AddRef(IWineD3DVertexDeclaration
*iface
) {
60 IWineD3DVertexDeclarationImpl
*This
= (IWineD3DVertexDeclarationImpl
*)iface
;
61 TRACE("(%p) : AddRef increasing from %d\n", This
, This
->ref
);
62 return InterlockedIncrement(&This
->ref
);
65 static ULONG WINAPI
IWineD3DVertexDeclarationImpl_Release(IWineD3DVertexDeclaration
*iface
) {
66 IWineD3DVertexDeclarationImpl
*This
= (IWineD3DVertexDeclarationImpl
*)iface
;
68 TRACE("(%p) : Releasing from %d\n", This
, This
->ref
);
69 ref
= InterlockedDecrement(&This
->ref
);
71 if(iface
== This
->wineD3DDevice
->stateBlock
->vertexDecl
) {
72 /* See comment in PixelShader::Release */
73 IWineD3DDeviceImpl_MarkStateDirty(This
->wineD3DDevice
, STATE_VDECL
);
76 HeapFree(GetProcessHeap(), 0, This
->elements
);
77 HeapFree(GetProcessHeap(), 0, This
);
82 /* *******************************************
83 IWineD3DVertexDeclaration parts follow
84 ******************************************* */
86 static HRESULT WINAPI
IWineD3DVertexDeclarationImpl_GetParent(IWineD3DVertexDeclaration
*iface
, IUnknown
** parent
){
87 IWineD3DVertexDeclarationImpl
*This
= (IWineD3DVertexDeclarationImpl
*)iface
;
89 *parent
= This
->parent
;
90 IUnknown_AddRef(*parent
);
91 TRACE("(%p) : returning %p\n", This
, *parent
);
95 static HRESULT WINAPI
IWineD3DVertexDeclarationImpl_GetDevice(IWineD3DVertexDeclaration
*iface
, IWineD3DDevice
** ppDevice
) {
96 IWineD3DVertexDeclarationImpl
*This
= (IWineD3DVertexDeclarationImpl
*)iface
;
97 TRACE("(%p) : returning %p\n", This
, This
->wineD3DDevice
);
99 *ppDevice
= (IWineD3DDevice
*) This
->wineD3DDevice
;
100 IWineD3DDevice_AddRef(*ppDevice
);
105 static BOOL
declaration_element_valid_ffp(const WINED3DVERTEXELEMENT
*element
)
107 switch(element
->Usage
)
109 case WINED3DDECLUSAGE_POSITION
:
110 case WINED3DDECLUSAGE_POSITIONT
:
111 switch(element
->Type
)
113 case WINED3DDECLTYPE_FLOAT2
:
114 case WINED3DDECLTYPE_FLOAT3
:
115 case WINED3DDECLTYPE_FLOAT4
:
116 case WINED3DDECLTYPE_SHORT2
:
117 case WINED3DDECLTYPE_SHORT4
:
118 case WINED3DDECLTYPE_FLOAT16_2
:
119 case WINED3DDECLTYPE_FLOAT16_4
:
125 case WINED3DDECLUSAGE_BLENDWEIGHT
:
126 switch(element
->Type
)
128 case WINED3DDECLTYPE_FLOAT1
:
129 case WINED3DDECLTYPE_FLOAT2
:
130 case WINED3DDECLTYPE_FLOAT3
:
131 case WINED3DDECLTYPE_FLOAT4
:
132 case WINED3DDECLTYPE_D3DCOLOR
:
133 case WINED3DDECLTYPE_UBYTE4
:
134 case WINED3DDECLTYPE_SHORT2
:
135 case WINED3DDECLTYPE_SHORT4
:
136 case WINED3DDECLTYPE_FLOAT16_2
:
137 case WINED3DDECLTYPE_FLOAT16_4
:
143 case WINED3DDECLUSAGE_NORMAL
:
144 switch(element
->Type
)
146 case WINED3DDECLTYPE_FLOAT3
:
147 case WINED3DDECLTYPE_FLOAT4
:
148 case WINED3DDECLTYPE_SHORT4
:
149 case WINED3DDECLTYPE_FLOAT16_4
:
155 case WINED3DDECLUSAGE_TEXCOORD
:
156 switch(element
->Type
)
158 case WINED3DDECLTYPE_FLOAT1
:
159 case WINED3DDECLTYPE_FLOAT2
:
160 case WINED3DDECLTYPE_FLOAT3
:
161 case WINED3DDECLTYPE_FLOAT4
:
162 case WINED3DDECLTYPE_SHORT2
:
163 case WINED3DDECLTYPE_SHORT4
:
164 case WINED3DDECLTYPE_FLOAT16_2
:
165 case WINED3DDECLTYPE_FLOAT16_4
:
171 case WINED3DDECLUSAGE_COLOR
:
172 switch(element
->Type
)
174 case WINED3DDECLTYPE_FLOAT3
:
175 case WINED3DDECLTYPE_FLOAT4
:
176 case WINED3DDECLTYPE_D3DCOLOR
:
177 case WINED3DDECLTYPE_UBYTE4
:
178 case WINED3DDECLTYPE_SHORT4
:
179 case WINED3DDECLTYPE_UBYTE4N
:
180 case WINED3DDECLTYPE_SHORT4N
:
181 case WINED3DDECLTYPE_USHORT4N
:
182 case WINED3DDECLTYPE_FLOAT16_4
:
193 HRESULT
vertexdeclaration_init(IWineD3DVertexDeclarationImpl
*This
,
194 const WINED3DVERTEXELEMENT
*elements
, UINT element_count
)
196 HRESULT hr
= WINED3D_OK
;
198 char isPreLoaded
[MAX_STREAMS
];
200 TRACE("(%p) : d3d version %d\n", This
, ((IWineD3DImpl
*)This
->wineD3DDevice
->wineD3D
)->dxVersion
);
201 memset(isPreLoaded
, 0, sizeof(isPreLoaded
));
203 if (TRACE_ON(d3d_decl
)) {
204 for (i
= 0; i
< element_count
; ++i
) {
205 dump_wined3dvertexelement(elements
+i
);
209 This
->element_count
= element_count
;
210 This
->elements
= HeapAlloc(GetProcessHeap(), 0, sizeof(*This
->elements
) * element_count
);
213 ERR("Memory allocation failed\n");
214 return WINED3DERR_OUTOFVIDEOMEMORY
;
217 /* Do some static analysis on the elements to make reading the declaration more comfortable
218 * for the drawing code
220 This
->num_streams
= 0;
221 This
->position_transformed
= FALSE
;
222 for (i
= 0; i
< element_count
; ++i
) {
223 struct wined3d_vertex_declaration_element
*e
= &This
->elements
[i
];
225 e
->type
= elements
[i
].Type
;
226 e
->ffp_valid
= declaration_element_valid_ffp(&elements
[i
]);
227 e
->input_slot
= elements
[i
].Stream
;
228 e
->offset
= elements
[i
].Offset
;
229 e
->output_slot
= elements
[i
].Reg
;
230 e
->method
= elements
[i
].Method
;
231 e
->usage
= elements
[i
].Usage
;
232 e
->usage_idx
= elements
[i
].UsageIndex
;
234 if (e
->usage
== WINED3DDECLUSAGE_POSITIONT
) This
->position_transformed
= TRUE
;
236 /* Find the Streams used in the declaration. The vertex buffers have to be loaded
237 * when drawing, but filter tesselation pseudo streams
239 if (e
->input_slot
>= MAX_STREAMS
) continue;
241 if (e
->type
== WINED3DDECLTYPE_UNUSED
)
243 WARN("The application tries to use WINED3DDECLTYPE_UNUSED, returning E_FAIL\n");
244 /* The caller will release the vdecl, which will free This->elements */
250 WARN("Declaration element %u is not 4 byte aligned(%u), returning E_FAIL\n", i
, e
->offset
);
254 if (!isPreLoaded
[e
->input_slot
])
256 This
->streams
[This
->num_streams
] = e
->input_slot
;
258 isPreLoaded
[e
->input_slot
] = 1;
261 if (e
->type
== WINED3DDECLTYPE_FLOAT16_2
|| e
->type
== WINED3DDECLTYPE_FLOAT16_4
)
263 if (!GL_SUPPORT(NV_HALF_FLOAT
)) This
->half_float_conv_needed
= TRUE
;
267 TRACE("Returning\n");
271 const IWineD3DVertexDeclarationVtbl IWineD3DVertexDeclaration_Vtbl
=
274 IWineD3DVertexDeclarationImpl_QueryInterface
,
275 IWineD3DVertexDeclarationImpl_AddRef
,
276 IWineD3DVertexDeclarationImpl_Release
,
277 /* IWineD3DVertexDeclaration */
278 IWineD3DVertexDeclarationImpl_GetParent
,
279 IWineD3DVertexDeclarationImpl_GetDevice
,