wined3d: Remove SetFunction() from the public shader interface.
[wine] / dlls / wined3d / vertexshader.c
1 /*
2  * shaders implementation
3  *
4  * Copyright 2002-2003 Jason Edmeades
5  * Copyright 2002-2003 Raphael Junqueira
6  * Copyright 2004 Christian Costa
7  * Copyright 2005 Oliver Stieber
8  * Copyright 2006 Ivan Gyurdiev
9  * Copyright 2007-2008 Stefan Dösinger for CodeWeavers
10  * Copyright 2009 Henri Verbeet for CodeWeavers
11  *
12  * This library is free software; you can redistribute it and/or
13  * modify it under the terms of the GNU Lesser General Public
14  * License as published by the Free Software Foundation; either
15  * version 2.1 of the License, or (at your option) any later version.
16  *
17  * This library is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
20  * Lesser General Public License for more details.
21  *
22  * You should have received a copy of the GNU Lesser General Public
23  * License along with this library; if not, write to the Free Software
24  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
25  */
26
27 #include "config.h"
28
29 #include <math.h>
30 #include <stdio.h>
31
32 #include "wined3d_private.h"
33
34 WINE_DEFAULT_DEBUG_CHANNEL(d3d_shader);
35
36 #define GLINFO_LOCATION ((IWineD3DDeviceImpl *)This->baseShader.device)->adapter->gl_info
37
38 static void vshader_set_limits(IWineD3DVertexShaderImpl *This)
39 {
40     DWORD shader_version = WINED3D_SHADER_VERSION(This->baseShader.reg_maps.shader_version.major,
41             This->baseShader.reg_maps.shader_version.minor);
42
43     This->baseShader.limits.texcoord = 0;
44     This->baseShader.limits.attributes = 16;
45     This->baseShader.limits.packed_input = 0;
46
47     switch (shader_version)
48     {
49         case WINED3D_SHADER_VERSION(1,0):
50         case WINED3D_SHADER_VERSION(1,1):
51             This->baseShader.limits.temporary = 12;
52             This->baseShader.limits.constant_bool = 0;
53             This->baseShader.limits.constant_int = 0;
54             This->baseShader.limits.address = 1;
55             This->baseShader.limits.packed_output = 0;
56             This->baseShader.limits.sampler = 0;
57             This->baseShader.limits.label = 0;
58             /* TODO: vs_1_1 has a minimum of 96 constants. What happens if a vs_1_1 shader is used
59              * on a vs_3_0 capable card that has 256 constants? */
60             This->baseShader.limits.constant_float = min(256, GL_LIMITS(vshader_constantsF));
61             break;
62
63         case WINED3D_SHADER_VERSION(2,0):
64         case WINED3D_SHADER_VERSION(2,1):
65             This->baseShader.limits.temporary = 12;
66             This->baseShader.limits.constant_bool = 16;
67             This->baseShader.limits.constant_int = 16;
68             This->baseShader.limits.address = 1;
69             This->baseShader.limits.packed_output = 0;
70             This->baseShader.limits.sampler = 0;
71             This->baseShader.limits.label = 16;
72             This->baseShader.limits.constant_float = min(256, GL_LIMITS(vshader_constantsF));
73             break;
74
75         case WINED3D_SHADER_VERSION(4,0):
76             FIXME("Using 3.0 limits for 4.0 shader\n");
77             /* Fall through */
78
79         case WINED3D_SHADER_VERSION(3,0):
80             This->baseShader.limits.temporary = 32;
81             This->baseShader.limits.constant_bool = 32;
82             This->baseShader.limits.constant_int = 32;
83             This->baseShader.limits.address = 1;
84             This->baseShader.limits.packed_output = 12;
85             This->baseShader.limits.sampler = 4;
86             This->baseShader.limits.label = 16; /* FIXME: 2048 */
87             /* DX10 cards on Windows advertise a d3d9 constant limit of 256 even though they are capable
88              * of supporting much more(GL drivers advertise 1024). d3d9.dll and d3d8.dll clamp the
89              * wined3d-advertised maximum. Clamp the constant limit for <= 3.0 shaders to 256.s
90              * use constant buffers */
91             This->baseShader.limits.constant_float = min(256, GL_LIMITS(vshader_constantsF));
92             break;
93
94         default:
95             This->baseShader.limits.temporary = 12;
96             This->baseShader.limits.constant_bool = 16;
97             This->baseShader.limits.constant_int = 16;
98             This->baseShader.limits.address = 1;
99             This->baseShader.limits.packed_output = 0;
100             This->baseShader.limits.sampler = 0;
101             This->baseShader.limits.label = 16;
102             This->baseShader.limits.constant_float = min(256, GL_LIMITS(vshader_constantsF));
103             FIXME("Unrecognized vertex shader version %u.%u\n",
104                     This->baseShader.reg_maps.shader_version.major,
105                     This->baseShader.reg_maps.shader_version.minor);
106     }
107 }
108
109 static BOOL match_usage(BYTE usage1, BYTE usage_idx1, BYTE usage2, BYTE usage_idx2) {
110     if (usage_idx1 != usage_idx2) return FALSE;
111     if (usage1 == usage2) return TRUE;
112     if (usage1 == WINED3DDECLUSAGE_POSITION && usage2 == WINED3DDECLUSAGE_POSITIONT) return TRUE;
113     if (usage2 == WINED3DDECLUSAGE_POSITION && usage1 == WINED3DDECLUSAGE_POSITIONT) return TRUE;
114
115     return FALSE;
116 }
117
118 BOOL vshader_get_input(IWineD3DVertexShader* iface, BYTE usage_req, BYTE usage_idx_req, unsigned int *regnum)
119 {
120     IWineD3DVertexShaderImpl *This = (IWineD3DVertexShaderImpl *)iface;
121     WORD map = This->baseShader.reg_maps.input_registers;
122     unsigned int i;
123
124     for (i = 0; map; map >>= 1, ++i)
125     {
126         if (!(map & 1)) continue;
127
128         if (match_usage(This->attributes[i].usage,
129                 This->attributes[i].usage_idx, usage_req, usage_idx_req))
130         {
131             *regnum = i;
132             return TRUE;
133         }
134     }
135     return FALSE;
136 }
137
138 /* *******************************************
139    IWineD3DVertexShader IUnknown parts follow
140    ******************************************* */
141 static HRESULT  WINAPI IWineD3DVertexShaderImpl_QueryInterface(IWineD3DVertexShader *iface, REFIID riid, LPVOID *ppobj) {
142     TRACE("iface %p, riid %s, ppobj %p\n", iface, debugstr_guid(riid), ppobj);
143
144     if (IsEqualGUID(riid, &IID_IWineD3DVertexShader)
145             || IsEqualGUID(riid, &IID_IWineD3DBaseShader)
146             || IsEqualGUID(riid, &IID_IWineD3DBase)
147             || IsEqualGUID(riid, &IID_IUnknown))
148     {
149         IUnknown_AddRef(iface);
150         *ppobj = iface;
151         return S_OK;
152     }
153
154     WARN("%s not implemented, returning E_NOINTERFACE\n", debugstr_guid(riid));
155
156     *ppobj = NULL;
157     return E_NOINTERFACE;
158 }
159
160 static ULONG  WINAPI IWineD3DVertexShaderImpl_AddRef(IWineD3DVertexShader *iface) {
161     IWineD3DVertexShaderImpl *This = (IWineD3DVertexShaderImpl *)iface;
162     ULONG refcount = InterlockedIncrement(&This->baseShader.ref);
163
164     TRACE("%p increasing refcount to %u\n", This, refcount);
165
166     return refcount;
167 }
168
169 static ULONG WINAPI IWineD3DVertexShaderImpl_Release(IWineD3DVertexShader *iface) {
170     IWineD3DVertexShaderImpl *This = (IWineD3DVertexShaderImpl *)iface;
171     ULONG refcount = InterlockedDecrement(&This->baseShader.ref);
172
173     TRACE("%p decreasing refcount to %u\n", This, refcount);
174
175     if (!refcount)
176     {
177         shader_cleanup((IWineD3DBaseShader *)iface);
178         This->parent_ops->wined3d_object_destroyed(This->parent);
179         HeapFree(GetProcessHeap(), 0, This);
180     }
181
182     return refcount;
183 }
184
185 /* *******************************************
186    IWineD3DVertexShader IWineD3DVertexShader parts follow
187    ******************************************* */
188
189 static HRESULT WINAPI IWineD3DVertexShaderImpl_GetParent(IWineD3DVertexShader *iface, IUnknown** parent){
190     IWineD3DVertexShaderImpl *This = (IWineD3DVertexShaderImpl *)iface;
191
192     *parent = This->parent;
193     IUnknown_AddRef(*parent);
194     TRACE("(%p) : returning %p\n", This, *parent);
195     return WINED3D_OK;
196 }
197
198 static HRESULT WINAPI IWineD3DVertexShaderImpl_GetDevice(IWineD3DVertexShader* iface, IWineD3DDevice **pDevice){
199     IWineD3DVertexShaderImpl *This = (IWineD3DVertexShaderImpl *)iface;
200     IWineD3DDevice_AddRef(This->baseShader.device);
201     *pDevice = This->baseShader.device;
202     TRACE("(%p) returning %p\n", This, *pDevice);
203     return WINED3D_OK;
204 }
205
206 static HRESULT WINAPI IWineD3DVertexShaderImpl_GetFunction(IWineD3DVertexShader* impl, VOID* pData, UINT* pSizeOfData) {
207     IWineD3DVertexShaderImpl *This = (IWineD3DVertexShaderImpl *)impl;
208     TRACE("(%p) : pData(%p), pSizeOfData(%p)\n", This, pData, pSizeOfData);
209
210     if (NULL == pData) {
211         *pSizeOfData = This->baseShader.functionLength;
212         return WINED3D_OK;
213     }
214     if (*pSizeOfData < This->baseShader.functionLength) {
215         /* MSDN claims (for d3d8 at least) that if *pSizeOfData is smaller
216          * than the required size we should write the required size and
217          * return D3DERR_MOREDATA. That's not actually true. */
218         return WINED3DERR_INVALIDCALL;
219     }
220
221     TRACE("(%p) : GetFunction copying to %p\n", This, pData);
222     memcpy(pData, This->baseShader.function, This->baseShader.functionLength);
223
224     return WINED3D_OK;
225 }
226
227 static HRESULT vertexshader_set_function(IWineD3DVertexShaderImpl *shader,
228         const DWORD *byte_code, const struct wined3d_shader_signature *output_signature)
229 {
230     IWineD3DDeviceImpl *device = (IWineD3DDeviceImpl *)shader->baseShader.device;
231     const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
232     const struct wined3d_shader_frontend *fe;
233     unsigned int i;
234     HRESULT hr;
235     shader_reg_maps *reg_maps = &shader->baseShader.reg_maps;
236
237     TRACE("shader %p, byte_code %p, output_signature %p.\n", shader, byte_code, output_signature);
238
239     fe = shader_select_frontend(*byte_code);
240     if (!fe)
241     {
242         FIXME("Unable to find frontend for shader.\n");
243         return WINED3DERR_INVALIDCALL;
244     }
245     shader->baseShader.frontend = fe;
246     shader->baseShader.frontend_data = fe->shader_init(byte_code, output_signature);
247     if (!shader->baseShader.frontend_data)
248     {
249         FIXME("Failed to initialize frontend.\n");
250         return WINED3DERR_INVALIDCALL;
251     }
252
253     /* First pass: trace shader */
254     if (TRACE_ON(d3d_shader)) shader_trace_init(fe, shader->baseShader.frontend_data, byte_code);
255
256     /* Initialize immediate constant lists */
257     list_init(&shader->baseShader.constantsF);
258     list_init(&shader->baseShader.constantsB);
259     list_init(&shader->baseShader.constantsI);
260
261     /* Second pass: figure out registers used, semantics, etc.. */
262     shader->min_rel_offset = gl_info->max_vshader_constantsF;
263     shader->max_rel_offset = 0;
264     hr = shader_get_registers_used((IWineD3DBaseShader *)shader, fe,
265             reg_maps, shader->attributes, NULL, shader->output_signature,
266             byte_code, gl_info->max_vshader_constantsF);
267     if (hr != WINED3D_OK) return hr;
268
269     if (output_signature)
270     {
271         for (i = 0; i < output_signature->element_count; ++i)
272         {
273             struct wined3d_shader_signature_element *e = &output_signature->elements[i];
274             reg_maps->output_registers |= 1 << e->register_idx;
275             shader->output_signature[e->register_idx] = *e;
276         }
277     }
278
279     vshader_set_limits(shader);
280
281     if (device->vs_selected_mode == SHADER_ARB
282             && (gl_info->quirks & WINED3D_QUIRK_ARB_VS_OFFSET_LIMIT)
283             && shader->min_rel_offset <= shader->max_rel_offset)
284     {
285         if (shader->max_rel_offset - shader->min_rel_offset > 127)
286         {
287             FIXME("The difference between the minimum and maximum relative offset is > 127\n");
288             FIXME("Which this OpenGL implementation does not support. Try using GLSL\n");
289             FIXME("Min: %d, Max: %d\n", shader->min_rel_offset, shader->max_rel_offset);
290         }
291         else if (shader->max_rel_offset - shader->min_rel_offset > 63)
292         {
293             shader->rel_offset = shader->min_rel_offset + 63;
294         }
295         else if (shader->max_rel_offset > 63)
296         {
297             shader->rel_offset = shader->min_rel_offset;
298         }
299         else
300         {
301             shader->rel_offset = 0;
302         }
303     }
304     shader->baseShader.load_local_constsF = shader->baseShader.reg_maps.usesrelconstF
305             && !list_empty(&shader->baseShader.constantsF);
306
307     /* copy the function ... because it will certainly be released by application */
308     shader->baseShader.function = HeapAlloc(GetProcessHeap(), 0, shader->baseShader.functionLength);
309     if (!shader->baseShader.function) return E_OUTOFMEMORY;
310     memcpy(shader->baseShader.function, byte_code, shader->baseShader.functionLength);
311
312     return WINED3D_OK;
313 }
314
315 /* Set local constants for d3d8 shaders */
316 static HRESULT WINAPI IWIneD3DVertexShaderImpl_SetLocalConstantsF(IWineD3DVertexShader *iface,
317         UINT start_idx, const float *src_data, UINT count) {
318     IWineD3DVertexShaderImpl *This =(IWineD3DVertexShaderImpl *)iface;
319     UINT i, end_idx;
320
321     TRACE("(%p) : start_idx %u, src_data %p, count %u\n", This, start_idx, src_data, count);
322
323     end_idx = start_idx + count;
324     if (end_idx > GL_LIMITS(vshader_constantsF)) {
325         WARN("end_idx %u > float constants limit %u\n", end_idx, GL_LIMITS(vshader_constantsF));
326         end_idx = GL_LIMITS(vshader_constantsF);
327     }
328
329     for (i = start_idx; i < end_idx; ++i) {
330         local_constant* lconst = HeapAlloc(GetProcessHeap(), 0, sizeof(local_constant));
331         if (!lconst) return E_OUTOFMEMORY;
332
333         lconst->idx = i;
334         memcpy(lconst->value, src_data + (i - start_idx) * 4 /* 4 components */, 4 * sizeof(float));
335         list_add_head(&This->baseShader.constantsF, &lconst->entry);
336     }
337
338     return WINED3D_OK;
339 }
340
341 static const IWineD3DVertexShaderVtbl IWineD3DVertexShader_Vtbl =
342 {
343     /*** IUnknown methods ***/
344     IWineD3DVertexShaderImpl_QueryInterface,
345     IWineD3DVertexShaderImpl_AddRef,
346     IWineD3DVertexShaderImpl_Release,
347     /*** IWineD3DBase methods ***/
348     IWineD3DVertexShaderImpl_GetParent,
349     /*** IWineD3DBaseShader methods ***/
350     IWineD3DVertexShaderImpl_GetDevice,
351     IWineD3DVertexShaderImpl_GetFunction,
352     /*** IWineD3DVertexShader methods ***/
353     IWIneD3DVertexShaderImpl_SetLocalConstantsF
354 };
355
356 void find_vs_compile_args(IWineD3DVertexShaderImpl *shader, IWineD3DStateBlockImpl *stateblock, struct vs_compile_args *args) {
357     args->fog_src = stateblock->renderState[WINED3DRS_FOGTABLEMODE] == WINED3DFOG_NONE ? VS_FOG_COORD : VS_FOG_Z;
358     args->swizzle_map = ((IWineD3DDeviceImpl *)shader->baseShader.device)->strided_streams.swizzle_map;
359 }
360
361 HRESULT vertexshader_init(IWineD3DVertexShaderImpl *shader, IWineD3DDeviceImpl *device,
362         const DWORD *byte_code, const struct wined3d_shader_signature *output_signature,
363         IUnknown *parent, const struct wined3d_parent_ops *parent_ops)
364 {
365     HRESULT hr;
366
367     if (!byte_code) return WINED3DERR_INVALIDCALL;
368
369     shader->lpVtbl = &IWineD3DVertexShader_Vtbl;
370     shader->parent = parent;
371     shader->parent_ops = parent_ops;
372     shader_init(&shader->baseShader, (IWineD3DDevice *)device);
373     list_add_head(&device->shaders, &shader->baseShader.shader_list_entry);
374
375     hr = vertexshader_set_function(shader, byte_code, output_signature);
376     if (FAILED(hr))
377     {
378         WARN("Failed to set function, hr %#x.\n", hr);
379         shader_cleanup((IWineD3DBaseShader *)shader);
380         return hr;
381     }
382
383     return WINED3D_OK;
384 }