d3dcompiler: Implement D3DGetInputSignatureBlob().
[wine] / dlls / d3dcompiler_43 / blob.c
1 /*
2  * Direct3D blob file
3  *
4  * Copyright 2010 Rico Schüller
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19  *
20  */
21
22 #include "config.h"
23 #include "wine/port.h"
24
25 #include "d3dcompiler_private.h"
26
27 WINE_DEFAULT_DEBUG_CHANNEL(d3dcompiler);
28
29 /* IUnknown methods */
30
31 static HRESULT STDMETHODCALLTYPE d3dcompiler_blob_QueryInterface(ID3DBlob *iface, REFIID riid, void **object)
32 {
33     TRACE("iface %p, riid %s, object %p\n", iface, debugstr_guid(riid), object);
34
35     if (IsEqualGUID(riid, &IID_ID3D10Blob)
36             || IsEqualGUID(riid, &IID_IUnknown))
37     {
38         IUnknown_AddRef(iface);
39         *object = iface;
40         return S_OK;
41     }
42
43     WARN("%s not implemented, returning E_NOINTERFACE\n", debugstr_guid(riid));
44
45     *object = NULL;
46     return E_NOINTERFACE;
47 }
48
49 static ULONG STDMETHODCALLTYPE d3dcompiler_blob_AddRef(ID3DBlob *iface)
50 {
51     struct d3dcompiler_blob *blob = (struct d3dcompiler_blob *)iface;
52     ULONG refcount = InterlockedIncrement(&blob->refcount);
53
54     TRACE("%p increasing refcount to %u\n", blob, refcount);
55
56     return refcount;
57 }
58
59 static ULONG STDMETHODCALLTYPE d3dcompiler_blob_Release(ID3DBlob *iface)
60 {
61     struct d3dcompiler_blob *blob = (struct d3dcompiler_blob *)iface;
62     ULONG refcount = InterlockedDecrement(&blob->refcount);
63
64     TRACE("%p decreasing refcount to %u\n", blob, refcount);
65
66     if (!refcount)
67     {
68         HeapFree(GetProcessHeap(), 0, blob->data);
69         HeapFree(GetProcessHeap(), 0, blob);
70     }
71
72     return refcount;
73 }
74
75 /* ID3DBlob methods */
76
77 static void * STDMETHODCALLTYPE d3dcompiler_blob_GetBufferPointer(ID3DBlob *iface)
78 {
79     struct d3dcompiler_blob *blob = (struct d3dcompiler_blob *)iface;
80
81     TRACE("iface %p\n", iface);
82
83     return blob->data;
84 }
85
86 static SIZE_T STDMETHODCALLTYPE d3dcompiler_blob_GetBufferSize(ID3DBlob *iface)
87 {
88     struct d3dcompiler_blob *blob = (struct d3dcompiler_blob *)iface;
89
90     TRACE("iface %p\n", iface);
91
92     return blob->size;
93 }
94
95 const struct ID3D10BlobVtbl d3dcompiler_blob_vtbl =
96 {
97     /* IUnknown methods */
98     d3dcompiler_blob_QueryInterface,
99     d3dcompiler_blob_AddRef,
100     d3dcompiler_blob_Release,
101     /* ID3DBlob methods */
102     d3dcompiler_blob_GetBufferPointer,
103     d3dcompiler_blob_GetBufferSize,
104 };
105
106 HRESULT d3dcompiler_blob_init(struct d3dcompiler_blob *blob, SIZE_T data_size)
107 {
108     blob->vtbl = &d3dcompiler_blob_vtbl;
109     blob->refcount = 1;
110     blob->size = data_size;
111
112     blob->data = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, data_size);
113     if (!blob->data)
114     {
115         ERR("Failed to allocate D3D blob data memory\n");
116         return E_OUTOFMEMORY;
117     }
118
119     return S_OK;
120 }
121
122 static BOOL check_blob_part(DWORD tag, D3D_BLOB_PART part)
123 {
124     BOOL add = FALSE;
125
126     switch(part)
127     {
128         case D3D_BLOB_INPUT_SIGNATURE_BLOB:
129             if (tag == TAG_ISGN) add = TRUE;
130             break;
131
132         default:
133             FIXME("Unhandled D3D_BLOB_PART %s.\n", debug_d3dcompiler_d3d_blob_part(part));
134             break;
135     }
136
137     TRACE("%s tag %s\n", add ? "Add" : "Skip", debugstr_an((const char *)&tag, 4));
138
139     return add;
140 }
141
142 HRESULT d3dcompiler_get_blob_part(const void *data, SIZE_T data_size, D3D_BLOB_PART part, UINT flags, ID3DBlob **blob)
143 {
144     struct dxbc src_dxbc, dst_dxbc;
145     HRESULT hr;
146     unsigned int i, count;
147
148     if (!data || !data_size || flags || !blob)
149     {
150         WARN("Invalid arguments: data %p, data_size %lu, flags %#x, blob %p\n", data, data_size, flags, blob);
151         return D3DERR_INVALIDCALL;
152     }
153
154     if (part > D3D_BLOB_TEST_COMPILE_PERF
155             || (part < D3D_BLOB_TEST_ALTERNATE_SHADER && part > D3D_BLOB_XNA_SHADER))
156     {
157         WARN("Invalid D3D_BLOB_PART: part %s\n", debug_d3dcompiler_d3d_blob_part(part));
158         return D3DERR_INVALIDCALL;
159     }
160
161     hr = dxbc_parse(data, data_size, &src_dxbc);
162     if (FAILED(hr))
163     {
164         WARN("Failed to parse blob part\n");
165         return hr;
166     }
167
168     hr = dxbc_init(&dst_dxbc, 0);
169     if (FAILED(hr))
170     {
171         dxbc_destroy(&src_dxbc);
172         WARN("Failed to init dxbc\n");
173         return hr;
174     }
175
176     for (i = 0; i < src_dxbc.count; ++i)
177     {
178         struct dxbc_section *section = &src_dxbc.sections[i];
179
180         if (check_blob_part(section->tag, part))
181         {
182             hr = dxbc_add_section(&dst_dxbc, section->tag, section->data, section->data_size);
183             if (FAILED(hr))
184             {
185                 dxbc_destroy(&src_dxbc);
186                 dxbc_destroy(&dst_dxbc);
187                 WARN("Failed to add section to dxbc\n");
188                 return hr;
189             }
190         }
191     }
192
193     count = dst_dxbc.count;
194
195     switch(part)
196     {
197         case D3D_BLOB_INPUT_SIGNATURE_BLOB:
198             if (count != 1) count = 0;
199             break;
200
201         default:
202             FIXME("Unhandled D3D_BLOB_PART %s.\n", debug_d3dcompiler_d3d_blob_part(part));
203             break;
204     }
205
206     if (count == 0)
207     {
208         dxbc_destroy(&src_dxbc);
209         dxbc_destroy(&dst_dxbc);
210         WARN("Nothing to write into the blob (count = 0)\n");
211         return E_FAIL;
212     }
213
214     hr = dxbc_write_blob(&dst_dxbc, blob);
215     if (FAILED(hr))
216     {
217         WARN("Failed to write blob part\n");
218     }
219
220     dxbc_destroy(&src_dxbc);
221     dxbc_destroy(&dst_dxbc);
222
223     return hr;
224 }