Release 1.3.7.
[wine/gsoc-2012-control.git] / dlls / d3d8 / shader.c
bloba59f10b57b43edcf38198b75a066f174891ce558
1 /*
2 * Copyright 2002-2003 Jason Edmeades
3 * Raphael Junqueira
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.
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.
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
20 #include "config.h"
21 #include "d3d8_private.h"
23 WINE_DEFAULT_DEBUG_CHANNEL(d3d8);
25 static HRESULT WINAPI d3d8_vertexshader_QueryInterface(IDirect3DVertexShader8 *iface, REFIID riid, void **object)
27 TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(riid), object);
29 if (IsEqualGUID(riid, &IID_IDirect3DVertexShader8)
30 || IsEqualGUID(riid, &IID_IUnknown))
32 IUnknown_AddRef(iface);
33 *object = iface;
34 return S_OK;
37 WARN("%s not implemented, returning E_NOINTERFACE.\n", debugstr_guid(riid));
39 *object = NULL;
40 return E_NOINTERFACE;
43 static ULONG WINAPI d3d8_vertexshader_AddRef(IDirect3DVertexShader8 *iface)
45 IDirect3DVertexShader8Impl *shader = (IDirect3DVertexShader8Impl *)iface;
46 ULONG refcount = InterlockedIncrement(&shader->ref);
48 TRACE("%p increasing refcount to %u.\n", iface, refcount);
50 if (refcount == 1 && shader->wineD3DVertexShader)
52 wined3d_mutex_lock();
53 IWineD3DVertexShader_AddRef(shader->wineD3DVertexShader);
54 wined3d_mutex_unlock();
57 return refcount;
60 static void STDMETHODCALLTYPE d3d8_vertexshader_wined3d_object_destroyed(void *parent)
62 IDirect3DVertexShader8Impl *shader = parent;
63 IDirect3DVertexDeclaration8_Release(shader->vertex_declaration);
64 HeapFree(GetProcessHeap(), 0, shader);
67 static ULONG WINAPI d3d8_vertexshader_Release(IDirect3DVertexShader8 *iface)
69 IDirect3DVertexShader8Impl *shader = (IDirect3DVertexShader8Impl *)iface;
70 ULONG refcount = InterlockedDecrement(&shader->ref);
72 TRACE("%p decreasing refcount to %u.\n", iface, refcount);
74 if (!refcount)
76 if (shader->wineD3DVertexShader)
78 wined3d_mutex_lock();
79 IWineD3DVertexShader_Release(shader->wineD3DVertexShader);
80 wined3d_mutex_unlock();
82 else
84 d3d8_vertexshader_wined3d_object_destroyed(shader);
88 return refcount;
91 static const IDirect3DVertexShader8Vtbl d3d8_vertexshader_vtbl =
93 /* IUnknown */
94 d3d8_vertexshader_QueryInterface,
95 d3d8_vertexshader_AddRef,
96 d3d8_vertexshader_Release,
99 static const struct wined3d_parent_ops d3d8_vertexshader_wined3d_parent_ops =
101 d3d8_vertexshader_wined3d_object_destroyed,
104 static HRESULT d3d8_vertexshader_create_vertexdeclaration(IDirect3DDevice8Impl *device,
105 const DWORD *declaration, DWORD shader_handle, IDirect3DVertexDeclaration8 **decl_ptr)
107 IDirect3DVertexDeclaration8Impl *object;
108 HRESULT hr;
110 TRACE("device %p, declaration %p, shader_handle %#x, decl_ptr %p.\n",
111 device, declaration, shader_handle, decl_ptr);
113 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
114 if (!object)
116 ERR("Memory allocation failed.\n");
117 return E_OUTOFMEMORY;
120 hr = vertexdeclaration_init(object, device, declaration, shader_handle);
121 if (FAILED(hr))
123 WARN("Failed to initialize vertex declaration, hr %#x.\n", hr);
124 HeapFree(GetProcessHeap(), 0, object);
125 return hr;
128 TRACE("Created vertex declaration %p.\n", object);
129 *decl_ptr = (IDirect3DVertexDeclaration8 *)object;
131 return D3D_OK;
134 HRESULT vertexshader_init(IDirect3DVertexShader8Impl *shader, IDirect3DDevice8Impl *device,
135 const DWORD *declaration, const DWORD *byte_code, DWORD shader_handle, DWORD usage)
137 const DWORD *token = declaration;
138 HRESULT hr;
140 /* Test if the vertex declaration is valid. */
141 while (D3DVSD_END() != *token)
143 D3DVSD_TOKENTYPE token_type = ((*token & D3DVSD_TOKENTYPEMASK) >> D3DVSD_TOKENTYPESHIFT);
145 if (token_type == D3DVSD_TOKEN_STREAMDATA && !(token_type & 0x10000000))
147 DWORD type = ((*token & D3DVSD_DATATYPEMASK) >> D3DVSD_DATATYPESHIFT);
148 DWORD reg = ((*token & D3DVSD_VERTEXREGMASK) >> D3DVSD_VERTEXREGSHIFT);
150 if (reg == D3DVSDE_NORMAL && type != D3DVSDT_FLOAT3 && !byte_code)
152 WARN("Attempt to use a non-FLOAT3 normal with the fixed function function\n");
153 return D3DERR_INVALIDCALL;
156 token += parse_token(token);
159 shader->ref = 1;
160 shader->lpVtbl = &d3d8_vertexshader_vtbl;
162 hr = d3d8_vertexshader_create_vertexdeclaration(device, declaration, shader_handle, &shader->vertex_declaration);
163 if (FAILED(hr))
165 WARN("Failed to create vertex declaration, hr %#x.\n", hr);
166 return hr;
169 if (byte_code)
171 if (usage) FIXME("Usage %#x not implemented.\n", usage);
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))
179 WARN("Failed to create wined3d vertex shader, hr %#x.\n", hr);
180 IDirect3DVertexDeclaration8_Release(shader->vertex_declaration);
181 return hr;
184 load_local_constants(declaration, shader->wineD3DVertexShader);
187 return D3D_OK;
190 static HRESULT WINAPI d3d8_pixelshader_QueryInterface(IDirect3DPixelShader8 *iface, REFIID riid, void **object)
192 TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(riid), object);
194 if (IsEqualGUID(riid, &IID_IDirect3DPixelShader8)
195 || IsEqualGUID(riid, &IID_IUnknown))
197 IUnknown_AddRef(iface);
198 *object = iface;
199 return S_OK;
202 WARN("%s not implemented, returning E_NOINTERFACE.\n", debugstr_guid(riid));
204 *object = NULL;
205 return E_NOINTERFACE;
208 static ULONG WINAPI d3d8_pixelshader_AddRef(IDirect3DPixelShader8 *iface)
210 IDirect3DPixelShader8Impl *shader = (IDirect3DPixelShader8Impl *)iface;
211 ULONG refcount = InterlockedIncrement(&shader->ref);
213 TRACE("%p increasing refcount to %u.\n", iface, refcount);
215 if (refcount == 1)
217 wined3d_mutex_lock();
218 IWineD3DPixelShader_AddRef(shader->wineD3DPixelShader);
219 wined3d_mutex_unlock();
222 return refcount;
225 static ULONG WINAPI d3d8_pixelshader_Release(IDirect3DPixelShader8 *iface)
227 IDirect3DPixelShader8Impl *shader = (IDirect3DPixelShader8Impl *)iface;
228 ULONG refcount = InterlockedDecrement(&shader->ref);
230 TRACE("%p decreasing refcount to %u.\n", iface, refcount);
232 if (!refcount)
234 wined3d_mutex_lock();
235 IWineD3DPixelShader_Release(shader->wineD3DPixelShader);
236 wined3d_mutex_unlock();
239 return refcount;
242 static const IDirect3DPixelShader8Vtbl d3d8_pixelshader_vtbl =
244 /* IUnknown */
245 d3d8_pixelshader_QueryInterface,
246 d3d8_pixelshader_AddRef,
247 d3d8_pixelshader_Release,
250 static void STDMETHODCALLTYPE d3d8_pixelshader_wined3d_object_destroyed(void *parent)
252 HeapFree(GetProcessHeap(), 0, parent);
255 static const struct wined3d_parent_ops d3d8_pixelshader_wined3d_parent_ops =
257 d3d8_pixelshader_wined3d_object_destroyed,
260 HRESULT pixelshader_init(IDirect3DPixelShader8Impl *shader, IDirect3DDevice8Impl *device,
261 const DWORD *byte_code, DWORD shader_handle)
263 HRESULT hr;
265 shader->ref = 1;
266 shader->lpVtbl = &d3d8_pixelshader_vtbl;
267 shader->handle = shader_handle;
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))
275 WARN("Failed to create wined3d pixel shader, hr %#x.\n", hr);
276 return hr;
279 return D3D_OK;