[WINED3D]
[reactos.git] / reactos / dll / directx / wine / 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 HRESULT WINAPI d3d8_vertexshader_QueryInterface(IDirect3DVertexShader8 *iface, REFIID riid, void **object)
26 {
27 TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(riid), object);
28
29 if (IsEqualGUID(riid, &IID_IDirect3DVertexShader8)
30 || IsEqualGUID(riid, &IID_IUnknown))
31 {
32 IUnknown_AddRef(iface);
33 *object = iface;
34 return S_OK;
35 }
36
37 WARN("%s not implemented, returning E_NOINTERFACE.\n", debugstr_guid(riid));
38
39 *object = NULL;
40 return E_NOINTERFACE;
41 }
42
43 static ULONG WINAPI d3d8_vertexshader_AddRef(IDirect3DVertexShader8 *iface)
44 {
45 IDirect3DVertexShader8Impl *shader = (IDirect3DVertexShader8Impl *)iface;
46 ULONG refcount = InterlockedIncrement(&shader->ref);
47
48 TRACE("%p increasing refcount to %u.\n", iface, refcount);
49
50 if (refcount == 1 && shader->wineD3DVertexShader)
51 {
52 wined3d_mutex_lock();
53 IWineD3DVertexShader_AddRef(shader->wineD3DVertexShader);
54 wined3d_mutex_unlock();
55 }
56
57 return refcount;
58 }
59
60 static void STDMETHODCALLTYPE d3d8_vertexshader_wined3d_object_destroyed(void *parent)
61 {
62 IDirect3DVertexShader8Impl *shader = parent;
63 IDirect3DVertexDeclaration8_Release(shader->vertex_declaration);
64 HeapFree(GetProcessHeap(), 0, shader);
65 }
66
67 static ULONG WINAPI d3d8_vertexshader_Release(IDirect3DVertexShader8 *iface)
68 {
69 IDirect3DVertexShader8Impl *shader = (IDirect3DVertexShader8Impl *)iface;
70 ULONG refcount = InterlockedDecrement(&shader->ref);
71
72 TRACE("%p decreasing refcount to %u.\n", iface, refcount);
73
74 if (!refcount)
75 {
76 if (shader->wineD3DVertexShader)
77 {
78 wined3d_mutex_lock();
79 IWineD3DVertexShader_Release(shader->wineD3DVertexShader);
80 wined3d_mutex_unlock();
81 }
82 else
83 {
84 d3d8_vertexshader_wined3d_object_destroyed(shader);
85 }
86 }
87
88 return refcount;
89 }
90
91 static const IDirect3DVertexShader8Vtbl d3d8_vertexshader_vtbl =
92 {
93 /* IUnknown */
94 d3d8_vertexshader_QueryInterface,
95 d3d8_vertexshader_AddRef,
96 d3d8_vertexshader_Release,
97 };
98
99 static const struct wined3d_parent_ops d3d8_vertexshader_wined3d_parent_ops =
100 {
101 d3d8_vertexshader_wined3d_object_destroyed,
102 };
103
104 static HRESULT d3d8_vertexshader_create_vertexdeclaration(IDirect3DDevice8Impl *device,
105 const DWORD *declaration, DWORD shader_handle, IDirect3DVertexDeclaration8 **decl_ptr)
106 {
107 IDirect3DVertexDeclaration8Impl *object;
108 HRESULT hr;
109
110 TRACE("device %p, declaration %p, shader_handle %#x, decl_ptr %p.\n",
111 device, declaration, shader_handle, decl_ptr);
112
113 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
114 if (!object)
115 {
116 ERR("Memory allocation failed.\n");
117 return E_OUTOFMEMORY;
118 }
119
120 hr = vertexdeclaration_init(object, device, declaration, shader_handle);
121 if (FAILED(hr))
122 {
123 WARN("Failed to initialize vertex declaration, hr %#x.\n", hr);
124 HeapFree(GetProcessHeap(), 0, object);
125 return hr;
126 }
127
128 TRACE("Created vertex declaration %p.\n", object);
129 *decl_ptr = (IDirect3DVertexDeclaration8 *)object;
130
131 return D3D_OK;
132 }
133
134 HRESULT vertexshader_init(IDirect3DVertexShader8Impl *shader, IDirect3DDevice8Impl *device,
135 const DWORD *declaration, const DWORD *byte_code, DWORD shader_handle, DWORD usage)
136 {
137 const DWORD *token = declaration;
138 HRESULT hr;
139
140 /* Test if the vertex declaration is valid. */
141 while (D3DVSD_END() != *token)
142 {
143 D3DVSD_TOKENTYPE token_type = ((*token & D3DVSD_TOKENTYPEMASK) >> D3DVSD_TOKENTYPESHIFT);
144
145 if (token_type == D3DVSD_TOKEN_STREAMDATA && !(token_type & 0x10000000))
146 {
147 DWORD type = ((*token & D3DVSD_DATATYPEMASK) >> D3DVSD_DATATYPESHIFT);
148 DWORD reg = ((*token & D3DVSD_VERTEXREGMASK) >> D3DVSD_VERTEXREGSHIFT);
149
150 if (reg == D3DVSDE_NORMAL && type != D3DVSDT_FLOAT3 && !byte_code)
151 {
152 WARN("Attempt to use a non-FLOAT3 normal with the fixed function function\n");
153 return D3DERR_INVALIDCALL;
154 }
155 }
156 token += parse_token(token);
157 }
158
159 shader->ref = 1;
160 shader->lpVtbl = &d3d8_vertexshader_vtbl;
161
162 hr = d3d8_vertexshader_create_vertexdeclaration(device, declaration, shader_handle, &shader->vertex_declaration);
163 if (FAILED(hr))
164 {
165 WARN("Failed to create vertex declaration, hr %#x.\n", hr);
166 return hr;
167 }
168
169 if (byte_code)
170 {
171 if (usage) FIXME("Usage %#x not implemented.\n", usage);
172
173 wined3d_mutex_lock();
174 hr = IWineD3DDevice_CreateVertexShader(device->WineD3DDevice, byte_code, NULL /* output signature */,
175 shader, &d3d8_vertexshader_wined3d_parent_ops, &shader->wineD3DVertexShader);
176 wined3d_mutex_unlock();
177 if (FAILED(hr))
178 {
179 WARN("Failed to create wined3d vertex shader, hr %#x.\n", hr);
180 IDirect3DVertexDeclaration8_Release(shader->vertex_declaration);
181 return hr;
182 }
183
184 load_local_constants(declaration, shader->wineD3DVertexShader);
185 }
186
187 return D3D_OK;
188 }
189
190 static HRESULT WINAPI d3d8_pixelshader_QueryInterface(IDirect3DPixelShader8 *iface, REFIID riid, void **object)
191 {
192 TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(riid), object);
193
194 if (IsEqualGUID(riid, &IID_IDirect3DPixelShader8)
195 || IsEqualGUID(riid, &IID_IUnknown))
196 {
197 IUnknown_AddRef(iface);
198 *object = iface;
199 return S_OK;
200 }
201
202 WARN("%s not implemented, returning E_NOINTERFACE.\n", debugstr_guid(riid));
203
204 *object = NULL;
205 return E_NOINTERFACE;
206 }
207
208 static ULONG WINAPI d3d8_pixelshader_AddRef(IDirect3DPixelShader8 *iface)
209 {
210 IDirect3DPixelShader8Impl *shader = (IDirect3DPixelShader8Impl *)iface;
211 ULONG refcount = InterlockedIncrement(&shader->ref);
212
213 TRACE("%p increasing refcount to %u.\n", iface, refcount);
214
215 if (refcount == 1)
216 {
217 wined3d_mutex_lock();
218 IWineD3DPixelShader_AddRef(shader->wineD3DPixelShader);
219 wined3d_mutex_unlock();
220 }
221
222 return refcount;
223 }
224
225 static ULONG WINAPI d3d8_pixelshader_Release(IDirect3DPixelShader8 *iface)
226 {
227 IDirect3DPixelShader8Impl *shader = (IDirect3DPixelShader8Impl *)iface;
228 ULONG refcount = InterlockedDecrement(&shader->ref);
229
230 TRACE("%p decreasing refcount to %u.\n", iface, refcount);
231
232 if (!refcount)
233 {
234 wined3d_mutex_lock();
235 IWineD3DPixelShader_Release(shader->wineD3DPixelShader);
236 wined3d_mutex_unlock();
237 }
238
239 return refcount;
240 }
241
242 static const IDirect3DPixelShader8Vtbl d3d8_pixelshader_vtbl =
243 {
244 /* IUnknown */
245 d3d8_pixelshader_QueryInterface,
246 d3d8_pixelshader_AddRef,
247 d3d8_pixelshader_Release,
248 };
249
250 static void STDMETHODCALLTYPE d3d8_pixelshader_wined3d_object_destroyed(void *parent)
251 {
252 HeapFree(GetProcessHeap(), 0, parent);
253 }
254
255 static const struct wined3d_parent_ops d3d8_pixelshader_wined3d_parent_ops =
256 {
257 d3d8_pixelshader_wined3d_object_destroyed,
258 };
259
260 HRESULT pixelshader_init(IDirect3DPixelShader8Impl *shader, IDirect3DDevice8Impl *device,
261 const DWORD *byte_code, DWORD shader_handle)
262 {
263 HRESULT hr;
264
265 shader->ref = 1;
266 shader->lpVtbl = &d3d8_pixelshader_vtbl;
267 shader->handle = shader_handle;
268
269 wined3d_mutex_lock();
270 hr = IWineD3DDevice_CreatePixelShader(device->WineD3DDevice, byte_code, NULL, shader,
271 &d3d8_pixelshader_wined3d_parent_ops, &shader->wineD3DPixelShader);
272 wined3d_mutex_unlock();
273 if (FAILED(hr))
274 {
275 WARN("Failed to create wined3d pixel shader, hr %#x.\n", hr);
276 return hr;
277 }
278
279 return D3D_OK;
280 }