wined3d: Simplify shader interface matching for SM4+ vertex shaders.
[wine.git] / dlls / d3d8 / shader.c
bloba0bb3179c07e03396e47123c46060be274b2e8fd
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 void STDMETHODCALLTYPE d3d8_vertexshader_wined3d_object_destroyed(void *parent)
27 struct d3d8_vertex_shader *shader = parent;
28 d3d8_vertex_declaration_destroy(shader->vertex_declaration);
29 HeapFree(GetProcessHeap(), 0, shader);
32 void d3d8_vertex_shader_destroy(struct d3d8_vertex_shader *shader)
34 TRACE("shader %p.\n", shader);
36 if (shader->wined3d_shader)
38 wined3d_mutex_lock();
39 wined3d_shader_decref(shader->wined3d_shader);
40 wined3d_mutex_unlock();
42 else
44 d3d8_vertexshader_wined3d_object_destroyed(shader);
48 static const struct wined3d_parent_ops d3d8_vertexshader_wined3d_parent_ops =
50 d3d8_vertexshader_wined3d_object_destroyed,
53 static HRESULT d3d8_vertexshader_create_vertexdeclaration(struct d3d8_device *device,
54 const DWORD *declaration, DWORD shader_handle, struct d3d8_vertex_declaration **decl_ptr)
56 struct d3d8_vertex_declaration *object;
57 HRESULT hr;
59 TRACE("device %p, declaration %p, shader_handle %#x, decl_ptr %p.\n",
60 device, declaration, shader_handle, decl_ptr);
62 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
63 if (!object)
64 return E_OUTOFMEMORY;
66 hr = d3d8_vertex_declaration_init(object, device, declaration, shader_handle);
67 if (FAILED(hr))
69 WARN("Failed to initialize vertex declaration, hr %#x.\n", hr);
70 HeapFree(GetProcessHeap(), 0, object);
71 return hr;
74 TRACE("Created vertex declaration %p.\n", object);
75 *decl_ptr = object;
77 return D3D_OK;
80 HRESULT d3d8_vertex_shader_init(struct d3d8_vertex_shader *shader, struct d3d8_device *device,
81 const DWORD *declaration, const DWORD *byte_code, DWORD shader_handle, DWORD usage)
83 const DWORD *token = declaration;
84 HRESULT hr;
86 /* Test if the vertex declaration is valid. */
87 while (D3DVSD_END() != *token)
89 D3DVSD_TOKENTYPE token_type = ((*token & D3DVSD_TOKENTYPEMASK) >> D3DVSD_TOKENTYPESHIFT);
91 if (token_type == D3DVSD_TOKEN_STREAMDATA && !(token_type & 0x10000000))
93 DWORD type = ((*token & D3DVSD_DATATYPEMASK) >> D3DVSD_DATATYPESHIFT);
94 DWORD reg = ((*token & D3DVSD_VERTEXREGMASK) >> D3DVSD_VERTEXREGSHIFT);
96 if (reg == D3DVSDE_NORMAL && type != D3DVSDT_FLOAT3 && !byte_code)
98 WARN("Attempt to use a non-FLOAT3 normal with the fixed-function function\n");
99 return D3DERR_INVALIDCALL;
102 token += parse_token(token);
105 hr = d3d8_vertexshader_create_vertexdeclaration(device, declaration, shader_handle, &shader->vertex_declaration);
106 if (FAILED(hr))
108 WARN("Failed to create vertex declaration, hr %#x.\n", hr);
109 return hr;
112 if (byte_code)
114 struct wined3d_shader_desc desc;
116 if (usage)
117 FIXME("Usage %#x not implemented.\n", usage);
119 desc.byte_code = byte_code;
120 desc.input_signature = NULL;
121 desc.output_signature = NULL;
122 desc.max_version = 1;
124 wined3d_mutex_lock();
125 hr = wined3d_shader_create_vs(device->wined3d_device, &desc, shader,
126 &d3d8_vertexshader_wined3d_parent_ops, &shader->wined3d_shader);
127 wined3d_mutex_unlock();
128 if (FAILED(hr))
130 WARN("Failed to create wined3d vertex shader, hr %#x.\n", hr);
131 d3d8_vertex_declaration_destroy(shader->vertex_declaration);
132 return hr;
135 load_local_constants(declaration, shader->wined3d_shader);
138 return D3D_OK;
141 static void STDMETHODCALLTYPE d3d8_pixelshader_wined3d_object_destroyed(void *parent)
143 HeapFree(GetProcessHeap(), 0, parent);
146 void d3d8_pixel_shader_destroy(struct d3d8_pixel_shader *shader)
148 TRACE("shader %p.\n", shader);
150 wined3d_mutex_lock();
151 wined3d_shader_decref(shader->wined3d_shader);
152 wined3d_mutex_unlock();
155 static const struct wined3d_parent_ops d3d8_pixelshader_wined3d_parent_ops =
157 d3d8_pixelshader_wined3d_object_destroyed,
160 HRESULT d3d8_pixel_shader_init(struct d3d8_pixel_shader *shader, struct d3d8_device *device,
161 const DWORD *byte_code, DWORD shader_handle)
163 struct wined3d_shader_desc desc;
164 HRESULT hr;
166 shader->handle = shader_handle;
168 desc.byte_code = byte_code;
169 desc.input_signature = NULL;
170 desc.output_signature = NULL;
171 desc.max_version = 1;
173 wined3d_mutex_lock();
174 hr = wined3d_shader_create_ps(device->wined3d_device, &desc, shader,
175 &d3d8_pixelshader_wined3d_parent_ops, &shader->wined3d_shader);
176 wined3d_mutex_unlock();
177 if (FAILED(hr))
179 WARN("Failed to create wined3d pixel shader, hr %#x.\n", hr);
180 return hr;
183 return D3D_OK;