wined3d: Remove COM from the shader implementation.
[wine] / dlls / d3d8 / shader.c
1 /*
2  * Copyright 2002-2003 Jason Edmeades
3  *                     Raphael Junqueira
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
18  */
19
20 #include "config.h"
21 #include "d3d8_private.h"
22
23 WINE_DEFAULT_DEBUG_CHANNEL(d3d8);
24
25 static inline IDirect3DVertexShader8Impl *impl_from_IDirect3DVertexShader8(IDirect3DVertexShader8 *iface)
26 {
27   return CONTAINING_RECORD(iface, IDirect3DVertexShader8Impl, IDirect3DVertexShader8_iface);
28 }
29
30 static HRESULT WINAPI d3d8_vertexshader_QueryInterface(IDirect3DVertexShader8 *iface, REFIID riid, void **object)
31 {
32     TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(riid), object);
33
34     if (IsEqualGUID(riid, &IID_IDirect3DVertexShader8)
35             || IsEqualGUID(riid, &IID_IUnknown))
36     {
37         IUnknown_AddRef(iface);
38         *object = iface;
39         return S_OK;
40     }
41
42     WARN("%s not implemented, returning E_NOINTERFACE.\n", debugstr_guid(riid));
43
44     *object = NULL;
45     return E_NOINTERFACE;
46 }
47
48 static ULONG WINAPI d3d8_vertexshader_AddRef(IDirect3DVertexShader8 *iface)
49 {
50     IDirect3DVertexShader8Impl *shader = impl_from_IDirect3DVertexShader8(iface);
51     ULONG refcount = InterlockedIncrement(&shader->ref);
52
53     TRACE("%p increasing refcount to %u.\n", iface, refcount);
54
55     if (refcount == 1 && shader->wined3d_shader)
56     {
57         wined3d_mutex_lock();
58         wined3d_shader_incref(shader->wined3d_shader);
59         wined3d_mutex_unlock();
60     }
61
62     return refcount;
63 }
64
65 static void STDMETHODCALLTYPE d3d8_vertexshader_wined3d_object_destroyed(void *parent)
66 {
67     IDirect3DVertexShader8Impl *shader = parent;
68     IDirect3DVertexDeclaration8_Release(shader->vertex_declaration);
69     HeapFree(GetProcessHeap(), 0, shader);
70 }
71
72 static ULONG WINAPI d3d8_vertexshader_Release(IDirect3DVertexShader8 *iface)
73 {
74     IDirect3DVertexShader8Impl *shader = impl_from_IDirect3DVertexShader8(iface);
75     ULONG refcount = InterlockedDecrement(&shader->ref);
76
77     TRACE("%p decreasing refcount to %u.\n", iface, refcount);
78
79     if (!refcount)
80     {
81         if (shader->wined3d_shader)
82         {
83             wined3d_mutex_lock();
84             wined3d_shader_decref(shader->wined3d_shader);
85             wined3d_mutex_unlock();
86         }
87         else
88         {
89             d3d8_vertexshader_wined3d_object_destroyed(shader);
90         }
91     }
92
93     return refcount;
94 }
95
96 static const IDirect3DVertexShader8Vtbl d3d8_vertexshader_vtbl =
97 {
98     /* IUnknown */
99     d3d8_vertexshader_QueryInterface,
100     d3d8_vertexshader_AddRef,
101     d3d8_vertexshader_Release,
102 };
103
104 static const struct wined3d_parent_ops d3d8_vertexshader_wined3d_parent_ops =
105 {
106     d3d8_vertexshader_wined3d_object_destroyed,
107 };
108
109 static HRESULT d3d8_vertexshader_create_vertexdeclaration(IDirect3DDevice8Impl *device,
110         const DWORD *declaration, DWORD shader_handle, IDirect3DVertexDeclaration8 **decl_ptr)
111 {
112     IDirect3DVertexDeclaration8Impl *object;
113     HRESULT hr;
114
115     TRACE("device %p, declaration %p, shader_handle %#x, decl_ptr %p.\n",
116             device, declaration, shader_handle, decl_ptr);
117
118     object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
119     if (!object)
120     {
121         ERR("Memory allocation failed.\n");
122         return E_OUTOFMEMORY;
123     }
124
125     hr = vertexdeclaration_init(object, device, declaration, shader_handle);
126     if (FAILED(hr))
127     {
128         WARN("Failed to initialize vertex declaration, hr %#x.\n", hr);
129         HeapFree(GetProcessHeap(), 0, object);
130         return hr;
131     }
132
133     TRACE("Created vertex declaration %p.\n", object);
134     *decl_ptr = (IDirect3DVertexDeclaration8 *)object;
135
136     return D3D_OK;
137 }
138
139 HRESULT vertexshader_init(IDirect3DVertexShader8Impl *shader, IDirect3DDevice8Impl *device,
140         const DWORD *declaration, const DWORD *byte_code, DWORD shader_handle, DWORD usage)
141 {
142     const DWORD *token = declaration;
143     HRESULT hr;
144
145     /* Test if the vertex declaration is valid. */
146     while (D3DVSD_END() != *token)
147     {
148         D3DVSD_TOKENTYPE token_type = ((*token & D3DVSD_TOKENTYPEMASK) >> D3DVSD_TOKENTYPESHIFT);
149
150         if (token_type == D3DVSD_TOKEN_STREAMDATA && !(token_type & 0x10000000))
151         {
152             DWORD type = ((*token & D3DVSD_DATATYPEMASK) >> D3DVSD_DATATYPESHIFT);
153             DWORD reg  = ((*token & D3DVSD_VERTEXREGMASK) >> D3DVSD_VERTEXREGSHIFT);
154
155             if (reg == D3DVSDE_NORMAL && type != D3DVSDT_FLOAT3 && !byte_code)
156             {
157                 WARN("Attempt to use a non-FLOAT3 normal with the fixed function function\n");
158                 return D3DERR_INVALIDCALL;
159             }
160         }
161         token += parse_token(token);
162     }
163
164     shader->ref = 1;
165     shader->IDirect3DVertexShader8_iface.lpVtbl = &d3d8_vertexshader_vtbl;
166
167     hr = d3d8_vertexshader_create_vertexdeclaration(device, declaration, shader_handle, &shader->vertex_declaration);
168     if (FAILED(hr))
169     {
170         WARN("Failed to create vertex declaration, hr %#x.\n", hr);
171         return hr;
172     }
173
174     if (byte_code)
175     {
176         if (usage) FIXME("Usage %#x not implemented.\n", usage);
177
178         wined3d_mutex_lock();
179         hr = IWineD3DDevice_CreateVertexShader(device->WineD3DDevice, byte_code, NULL /* output signature */,
180                 shader, &d3d8_vertexshader_wined3d_parent_ops, &shader->wined3d_shader);
181         wined3d_mutex_unlock();
182         if (FAILED(hr))
183         {
184             WARN("Failed to create wined3d vertex shader, hr %#x.\n", hr);
185             IDirect3DVertexDeclaration8_Release(shader->vertex_declaration);
186             return hr;
187         }
188
189         load_local_constants(declaration, shader->wined3d_shader);
190     }
191
192     return D3D_OK;
193 }
194
195 static inline IDirect3DPixelShader8Impl *impl_from_IDirect3DPixelShader8(IDirect3DPixelShader8 *iface)
196 {
197     return CONTAINING_RECORD(iface, IDirect3DPixelShader8Impl, IDirect3DPixelShader8_iface);
198 }
199
200 static HRESULT WINAPI d3d8_pixelshader_QueryInterface(IDirect3DPixelShader8 *iface, REFIID riid, void **object)
201 {
202     TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(riid), object);
203
204     if (IsEqualGUID(riid, &IID_IDirect3DPixelShader8)
205             || IsEqualGUID(riid, &IID_IUnknown))
206     {
207         IUnknown_AddRef(iface);
208         *object = iface;
209         return S_OK;
210     }
211
212     WARN("%s not implemented, returning E_NOINTERFACE.\n", debugstr_guid(riid));
213
214     *object = NULL;
215     return E_NOINTERFACE;
216 }
217
218 static ULONG WINAPI d3d8_pixelshader_AddRef(IDirect3DPixelShader8 *iface)
219 {
220     IDirect3DPixelShader8Impl *shader = impl_from_IDirect3DPixelShader8(iface);
221     ULONG refcount = InterlockedIncrement(&shader->ref);
222
223     TRACE("%p increasing refcount to %u.\n", iface, refcount);
224
225     if (refcount == 1)
226     {
227         wined3d_mutex_lock();
228         wined3d_shader_incref(shader->wined3d_shader);
229         wined3d_mutex_unlock();
230     }
231
232     return refcount;
233 }
234
235 static ULONG WINAPI d3d8_pixelshader_Release(IDirect3DPixelShader8 *iface)
236 {
237     IDirect3DPixelShader8Impl *shader = impl_from_IDirect3DPixelShader8(iface);
238     ULONG refcount = InterlockedDecrement(&shader->ref);
239
240     TRACE("%p decreasing refcount to %u.\n", iface, refcount);
241
242     if (!refcount)
243     {
244         wined3d_mutex_lock();
245         wined3d_shader_decref(shader->wined3d_shader);
246         wined3d_mutex_unlock();
247     }
248
249     return refcount;
250 }
251
252 static const IDirect3DPixelShader8Vtbl d3d8_pixelshader_vtbl =
253 {
254     /* IUnknown */
255     d3d8_pixelshader_QueryInterface,
256     d3d8_pixelshader_AddRef,
257     d3d8_pixelshader_Release,
258 };
259
260 static void STDMETHODCALLTYPE d3d8_pixelshader_wined3d_object_destroyed(void *parent)
261 {
262     HeapFree(GetProcessHeap(), 0, parent);
263 }
264
265 static const struct wined3d_parent_ops d3d8_pixelshader_wined3d_parent_ops =
266 {
267     d3d8_pixelshader_wined3d_object_destroyed,
268 };
269
270 HRESULT pixelshader_init(IDirect3DPixelShader8Impl *shader, IDirect3DDevice8Impl *device,
271         const DWORD *byte_code, DWORD shader_handle)
272 {
273     HRESULT hr;
274
275     shader->ref = 1;
276     shader->IDirect3DPixelShader8_iface.lpVtbl = &d3d8_pixelshader_vtbl;
277     shader->handle = shader_handle;
278
279     wined3d_mutex_lock();
280     hr = IWineD3DDevice_CreatePixelShader(device->WineD3DDevice, byte_code, NULL, shader,
281             &d3d8_pixelshader_wined3d_parent_ops, &shader->wined3d_shader);
282     wined3d_mutex_unlock();
283     if (FAILED(hr))
284     {
285         WARN("Failed to create wined3d pixel shader, hr %#x.\n", hr);
286         return hr;
287     }
288
289     return D3D_OK;
290 }