winhttp: Fix up headers with wrong termination.
[wine] / dlls / d3d10core / inputlayout.c
1 /*
2  * Copyright 2009 Henri Verbeet for CodeWeavers
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
17  *
18  */
19
20 #include "config.h"
21 #include "wine/port.h"
22 #include <assert.h>
23
24 #include "d3d10core_private.h"
25
26 WINE_DEFAULT_DEBUG_CHANNEL(d3d10core);
27
28 static HRESULT isgn_handler(const char *data, DWORD data_size, DWORD tag, void *ctx)
29 {
30     struct wined3d_shader_signature *is = ctx;
31
32     switch(tag)
33     {
34         case TAG_ISGN:
35             return shader_parse_signature(data, data_size, is);
36
37         default:
38             FIXME("Unhandled chunk %s.\n", debugstr_an((const char *)&tag, 4));
39             return S_OK;
40     }
41 }
42
43 static HRESULT d3d10_input_layout_to_wined3d_declaration(const D3D10_INPUT_ELEMENT_DESC *element_descs,
44         UINT element_count, const void *shader_byte_code, SIZE_T shader_byte_code_length,
45         struct wined3d_vertex_element **wined3d_elements, UINT *wined3d_element_count)
46 {
47     struct wined3d_shader_signature is;
48     HRESULT hr;
49     UINT i;
50
51     hr = parse_dxbc(shader_byte_code, shader_byte_code_length, isgn_handler, &is);
52     if (FAILED(hr))
53     {
54         ERR("Failed to parse input signature.\n");
55         return E_FAIL;
56     }
57
58     *wined3d_elements = HeapAlloc(GetProcessHeap(), 0, element_count * sizeof(**wined3d_elements));
59     if (!*wined3d_elements)
60     {
61         ERR("Failed to allocate wined3d vertex element array memory.\n");
62         HeapFree(GetProcessHeap(), 0, is.elements);
63         return E_OUTOFMEMORY;
64     }
65     *wined3d_element_count = 0;
66
67     for (i = 0; i < element_count; ++i)
68     {
69         UINT j;
70
71         for (j = 0; j < is.element_count; ++j)
72         {
73             if (!strcmp(element_descs[i].SemanticName, is.elements[j].semantic_name)
74                     && element_descs[i].SemanticIndex == is.elements[j].semantic_idx)
75             {
76                 struct wined3d_vertex_element *e = &(*wined3d_elements)[(*wined3d_element_count)++];
77                 const D3D10_INPUT_ELEMENT_DESC *f = &element_descs[i];
78
79                 e->format = wined3dformat_from_dxgi_format(f->Format);
80                 e->input_slot = f->InputSlot;
81                 e->offset = f->AlignedByteOffset;
82                 e->output_slot = is.elements[j].register_idx;
83                 e->method = WINED3D_DECL_METHOD_DEFAULT;
84                 e->usage = 0;
85                 e->usage_idx = 0;
86
87                 if (f->AlignedByteOffset == D3D10_APPEND_ALIGNED_ELEMENT)
88                     FIXME("D3D10_APPEND_ALIGNED_ELEMENT not supported\n");
89                 if (f->InputSlotClass != D3D10_INPUT_PER_VERTEX_DATA)
90                     FIXME("Ignoring input slot class (%#x)\n", f->InputSlotClass);
91                 if (f->InstanceDataStepRate)
92                     FIXME("Ignoring instace data step rate (%#x)\n", f->InstanceDataStepRate);
93
94                 break;
95             }
96         }
97     }
98
99     shader_free_signature(&is);
100
101     return S_OK;
102 }
103
104 static inline struct d3d10_input_layout *impl_from_ID3D10InputLayout(ID3D10InputLayout *iface)
105 {
106     return CONTAINING_RECORD(iface, struct d3d10_input_layout, ID3D10InputLayout_iface);
107 }
108
109 /* IUnknown methods */
110
111 static HRESULT STDMETHODCALLTYPE d3d10_input_layout_QueryInterface(ID3D10InputLayout *iface,
112         REFIID riid, void **object)
113 {
114     TRACE("iface %p, riid %s, object %p\n", iface, debugstr_guid(riid), object);
115
116     if (IsEqualGUID(riid, &IID_ID3D10InputLayout)
117             || IsEqualGUID(riid, &IID_ID3D10DeviceChild)
118             || IsEqualGUID(riid, &IID_IUnknown))
119     {
120         IUnknown_AddRef(iface);
121         *object = iface;
122         return S_OK;
123     }
124
125     WARN("%s not implemented, returning E_NOINTERFACE\n", debugstr_guid(riid));
126
127     *object = NULL;
128     return E_NOINTERFACE;
129 }
130
131 static ULONG STDMETHODCALLTYPE d3d10_input_layout_AddRef(ID3D10InputLayout *iface)
132 {
133     struct d3d10_input_layout *This = impl_from_ID3D10InputLayout(iface);
134     ULONG refcount = InterlockedIncrement(&This->refcount);
135
136     TRACE("%p increasing refcount to %u\n", This, refcount);
137
138     if (refcount == 1)
139     {
140         wined3d_vertex_declaration_incref(This->wined3d_decl);
141     }
142
143     return refcount;
144 }
145
146 static ULONG STDMETHODCALLTYPE d3d10_input_layout_Release(ID3D10InputLayout *iface)
147 {
148     struct d3d10_input_layout *This = impl_from_ID3D10InputLayout(iface);
149     ULONG refcount = InterlockedDecrement(&This->refcount);
150
151     TRACE("%p decreasing refcount to %u\n", This, refcount);
152
153     if (!refcount)
154     {
155         wined3d_vertex_declaration_decref(This->wined3d_decl);
156     }
157
158     return refcount;
159 }
160
161 /* ID3D10DeviceChild methods */
162
163 static void STDMETHODCALLTYPE d3d10_input_layout_GetDevice(ID3D10InputLayout *iface, ID3D10Device **device)
164 {
165     FIXME("iface %p, device %p stub!\n", iface, device);
166 }
167
168 static HRESULT STDMETHODCALLTYPE d3d10_input_layout_GetPrivateData(ID3D10InputLayout *iface,
169         REFGUID guid, UINT *data_size, void *data)
170 {
171     FIXME("iface %p, guid %s, data_size %p, data %p stub!\n",
172             iface, debugstr_guid(guid), data_size, data);
173
174     return E_NOTIMPL;
175 }
176
177 static HRESULT STDMETHODCALLTYPE d3d10_input_layout_SetPrivateData(ID3D10InputLayout *iface,
178         REFGUID guid, UINT data_size, const void *data)
179 {
180     FIXME("iface %p, guid %s, data_size %u, data %p stub!\n",
181             iface, debugstr_guid(guid), data_size, data);
182
183     return E_NOTIMPL;
184 }
185
186 static HRESULT STDMETHODCALLTYPE d3d10_input_layout_SetPrivateDataInterface(ID3D10InputLayout *iface,
187         REFGUID guid, const IUnknown *data)
188 {
189     FIXME("iface %p, guid %s, data %p stub!\n", iface, debugstr_guid(guid), data);
190
191     return E_NOTIMPL;
192 }
193
194 static const struct ID3D10InputLayoutVtbl d3d10_input_layout_vtbl =
195 {
196     /* IUnknown methods */
197     d3d10_input_layout_QueryInterface,
198     d3d10_input_layout_AddRef,
199     d3d10_input_layout_Release,
200     /* ID3D10DeviceChild methods */
201     d3d10_input_layout_GetDevice,
202     d3d10_input_layout_GetPrivateData,
203     d3d10_input_layout_SetPrivateData,
204     d3d10_input_layout_SetPrivateDataInterface,
205 };
206
207 static void STDMETHODCALLTYPE d3d10_input_layout_wined3d_object_destroyed(void *parent)
208 {
209     HeapFree(GetProcessHeap(), 0, parent);
210 }
211
212 static const struct wined3d_parent_ops d3d10_input_layout_wined3d_parent_ops =
213 {
214     d3d10_input_layout_wined3d_object_destroyed,
215 };
216
217 HRESULT d3d10_input_layout_init(struct d3d10_input_layout *layout, struct d3d10_device *device,
218         const D3D10_INPUT_ELEMENT_DESC *element_descs, UINT element_count,
219         const void *shader_byte_code, SIZE_T shader_byte_code_length)
220 {
221     struct wined3d_vertex_element *wined3d_elements;
222     UINT wined3d_element_count;
223     HRESULT hr;
224
225     layout->ID3D10InputLayout_iface.lpVtbl = &d3d10_input_layout_vtbl;
226     layout->refcount = 1;
227
228     hr = d3d10_input_layout_to_wined3d_declaration(element_descs, element_count,
229             shader_byte_code, shader_byte_code_length, &wined3d_elements, &wined3d_element_count);
230     if (FAILED(hr))
231     {
232         WARN("Failed to create wined3d vertex declaration elements, hr %#x.\n", hr);
233         return hr;
234     }
235
236     hr = wined3d_vertex_declaration_create(device->wined3d_device, wined3d_elements, wined3d_element_count,
237             layout, &d3d10_input_layout_wined3d_parent_ops, &layout->wined3d_decl);
238     HeapFree(GetProcessHeap(), 0, wined3d_elements);
239     if (FAILED(hr))
240     {
241         WARN("Failed to create wined3d vertex declaration, hr %#x.\n", hr);
242         return hr;
243     }
244
245     return S_OK;
246 }
247
248 struct d3d10_input_layout *unsafe_impl_from_ID3D10InputLayout(ID3D10InputLayout *iface)
249 {
250     if (!iface)
251         return NULL;
252     assert(iface->lpVtbl == &d3d10_input_layout_vtbl);
253
254     return impl_from_ID3D10InputLayout(iface);
255 }