d3d8: Remove COM from the vertex 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 void STDMETHODCALLTYPE d3d8_vertexshader_wined3d_object_destroyed(void *parent)
26 {
27     struct d3d8_vertex_shader *shader = parent;
28     d3d8_vertex_declaration_destroy(shader->vertex_declaration);
29     HeapFree(GetProcessHeap(), 0, shader);
30 }
31
32 void d3d8_vertex_shader_destroy(struct d3d8_vertex_shader *shader)
33 {
34     TRACE("shader %p.\n", shader);
35
36     if (shader->wined3d_shader)
37     {
38         wined3d_mutex_lock();
39         wined3d_shader_decref(shader->wined3d_shader);
40         wined3d_mutex_unlock();
41     }
42     else
43     {
44         d3d8_vertexshader_wined3d_object_destroyed(shader);
45     }
46 }
47
48 static const struct wined3d_parent_ops d3d8_vertexshader_wined3d_parent_ops =
49 {
50     d3d8_vertexshader_wined3d_object_destroyed,
51 };
52
53 static HRESULT d3d8_vertexshader_create_vertexdeclaration(IDirect3DDevice8Impl *device,
54         const DWORD *declaration, DWORD shader_handle, struct d3d8_vertex_declaration **decl_ptr)
55 {
56     struct d3d8_vertex_declaration *object;
57     HRESULT hr;
58
59     TRACE("device %p, declaration %p, shader_handle %#x, decl_ptr %p.\n",
60             device, declaration, shader_handle, decl_ptr);
61
62     object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
63     if (!object)
64     {
65         ERR("Memory allocation failed.\n");
66         return E_OUTOFMEMORY;
67     }
68
69     hr = d3d8_vertex_declaration_init(object, device, declaration, shader_handle);
70     if (FAILED(hr))
71     {
72         WARN("Failed to initialize vertex declaration, hr %#x.\n", hr);
73         HeapFree(GetProcessHeap(), 0, object);
74         return hr;
75     }
76
77     TRACE("Created vertex declaration %p.\n", object);
78     *decl_ptr = object;
79
80     return D3D_OK;
81 }
82
83 HRESULT d3d8_vertex_shader_init(struct d3d8_vertex_shader *shader, IDirect3DDevice8Impl *device,
84         const DWORD *declaration, const DWORD *byte_code, DWORD shader_handle, DWORD usage)
85 {
86     const DWORD *token = declaration;
87     HRESULT hr;
88
89     /* Test if the vertex declaration is valid. */
90     while (D3DVSD_END() != *token)
91     {
92         D3DVSD_TOKENTYPE token_type = ((*token & D3DVSD_TOKENTYPEMASK) >> D3DVSD_TOKENTYPESHIFT);
93
94         if (token_type == D3DVSD_TOKEN_STREAMDATA && !(token_type & 0x10000000))
95         {
96             DWORD type = ((*token & D3DVSD_DATATYPEMASK) >> D3DVSD_DATATYPESHIFT);
97             DWORD reg  = ((*token & D3DVSD_VERTEXREGMASK) >> D3DVSD_VERTEXREGSHIFT);
98
99             if (reg == D3DVSDE_NORMAL && type != D3DVSDT_FLOAT3 && !byte_code)
100             {
101                 WARN("Attempt to use a non-FLOAT3 normal with the fixed function function\n");
102                 return D3DERR_INVALIDCALL;
103             }
104         }
105         token += parse_token(token);
106     }
107
108     hr = d3d8_vertexshader_create_vertexdeclaration(device, declaration, shader_handle, &shader->vertex_declaration);
109     if (FAILED(hr))
110     {
111         WARN("Failed to create vertex declaration, hr %#x.\n", hr);
112         return hr;
113     }
114
115     if (byte_code)
116     {
117         if (usage) FIXME("Usage %#x not implemented.\n", usage);
118
119         wined3d_mutex_lock();
120         hr = wined3d_shader_create_vs(device->wined3d_device, byte_code, NULL /* output signature */,
121                 shader, &d3d8_vertexshader_wined3d_parent_ops, &shader->wined3d_shader, 1);
122         wined3d_mutex_unlock();
123         if (FAILED(hr))
124         {
125             WARN("Failed to create wined3d vertex shader, hr %#x.\n", hr);
126             d3d8_vertex_declaration_destroy(shader->vertex_declaration);
127             return hr;
128         }
129
130         load_local_constants(declaration, shader->wined3d_shader);
131     }
132
133     return D3D_OK;
134 }
135
136 static inline IDirect3DPixelShader8Impl *impl_from_IDirect3DPixelShader8(IDirect3DPixelShader8 *iface)
137 {
138     return CONTAINING_RECORD(iface, IDirect3DPixelShader8Impl, IDirect3DPixelShader8_iface);
139 }
140
141 static HRESULT WINAPI d3d8_pixelshader_QueryInterface(IDirect3DPixelShader8 *iface, REFIID riid, void **object)
142 {
143     TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(riid), object);
144
145     if (IsEqualGUID(riid, &IID_IDirect3DPixelShader8)
146             || IsEqualGUID(riid, &IID_IUnknown))
147     {
148         IUnknown_AddRef(iface);
149         *object = iface;
150         return S_OK;
151     }
152
153     WARN("%s not implemented, returning E_NOINTERFACE.\n", debugstr_guid(riid));
154
155     *object = NULL;
156     return E_NOINTERFACE;
157 }
158
159 static ULONG WINAPI d3d8_pixelshader_AddRef(IDirect3DPixelShader8 *iface)
160 {
161     IDirect3DPixelShader8Impl *shader = impl_from_IDirect3DPixelShader8(iface);
162     ULONG refcount = InterlockedIncrement(&shader->ref);
163
164     TRACE("%p increasing refcount to %u.\n", iface, refcount);
165
166     if (refcount == 1)
167     {
168         wined3d_mutex_lock();
169         wined3d_shader_incref(shader->wined3d_shader);
170         wined3d_mutex_unlock();
171     }
172
173     return refcount;
174 }
175
176 static ULONG WINAPI d3d8_pixelshader_Release(IDirect3DPixelShader8 *iface)
177 {
178     IDirect3DPixelShader8Impl *shader = impl_from_IDirect3DPixelShader8(iface);
179     ULONG refcount = InterlockedDecrement(&shader->ref);
180
181     TRACE("%p decreasing refcount to %u.\n", iface, refcount);
182
183     if (!refcount)
184     {
185         wined3d_mutex_lock();
186         wined3d_shader_decref(shader->wined3d_shader);
187         wined3d_mutex_unlock();
188     }
189
190     return refcount;
191 }
192
193 static const IDirect3DPixelShader8Vtbl d3d8_pixelshader_vtbl =
194 {
195     /* IUnknown */
196     d3d8_pixelshader_QueryInterface,
197     d3d8_pixelshader_AddRef,
198     d3d8_pixelshader_Release,
199 };
200
201 static void STDMETHODCALLTYPE d3d8_pixelshader_wined3d_object_destroyed(void *parent)
202 {
203     HeapFree(GetProcessHeap(), 0, parent);
204 }
205
206 static const struct wined3d_parent_ops d3d8_pixelshader_wined3d_parent_ops =
207 {
208     d3d8_pixelshader_wined3d_object_destroyed,
209 };
210
211 HRESULT pixelshader_init(IDirect3DPixelShader8Impl *shader, IDirect3DDevice8Impl *device,
212         const DWORD *byte_code, DWORD shader_handle)
213 {
214     HRESULT hr;
215
216     shader->ref = 1;
217     shader->IDirect3DPixelShader8_iface.lpVtbl = &d3d8_pixelshader_vtbl;
218     shader->handle = shader_handle;
219
220     wined3d_mutex_lock();
221     hr = wined3d_shader_create_ps(device->wined3d_device, byte_code, NULL, shader,
222             &d3d8_pixelshader_wined3d_parent_ops, &shader->wined3d_shader, 1);
223     wined3d_mutex_unlock();
224     if (FAILED(hr))
225     {
226         WARN("Failed to create wined3d pixel shader, hr %#x.\n", hr);
227         return hr;
228     }
229
230     return D3D_OK;
231 }