1 | /** @file
|
---|
2 | * shaderlib -- interface to WINE's Direct3D shader functions
|
---|
3 | */
|
---|
4 |
|
---|
5 | /*
|
---|
6 | * Copyright (C) 2014 Oracle Corporation
|
---|
7 | *
|
---|
8 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
9 | * available from http://www.virtualbox.org. This file is free software;
|
---|
10 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
11 | * General Public License (GPL) as published by the Free Software
|
---|
12 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
13 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
14 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
15 | */
|
---|
16 |
|
---|
17 | #include <iprt/err.h>
|
---|
18 | #include <iprt/mem.h>
|
---|
19 | #include <iprt/assert.h>
|
---|
20 | #include <iprt/log.h>
|
---|
21 | #include "wined3d_private.h"
|
---|
22 |
|
---|
23 | #include "shaderlib.h"
|
---|
24 |
|
---|
25 | #ifdef RT_OS_WINDOWS
|
---|
26 | #define OGLGETPROCADDRESS wglGetProcAddress
|
---|
27 | #elif RT_OS_DARWIN
|
---|
28 | #include <mach-o/dyld.h>
|
---|
29 | void *MyNSGLGetProcAddress(const char *name)
|
---|
30 | {
|
---|
31 | NSSymbol symbol = NULL;
|
---|
32 | char *symbolName = malloc(strlen(name) + 2);
|
---|
33 | strcpy(symbolName + 1, name);
|
---|
34 | symbolName[0] = '_';
|
---|
35 | if (NSIsSymbolNameDefined(symbolName))
|
---|
36 | symbol = NSLookupAndBindSymbol(symbolName);
|
---|
37 | free(symbolName);
|
---|
38 | return symbol ? NSAddressOfSymbol(symbol) : NULL;
|
---|
39 | }
|
---|
40 | #define OGLGETPROCADDRESS(x) MyNSGLGetProcAddress((const char *)x)
|
---|
41 | #else
|
---|
42 | extern void (*glXGetProcAddress(const GLubyte *procname))( void );
|
---|
43 | #define OGLGETPROCADDRESS(x) glXGetProcAddress((const GLubyte *)x)
|
---|
44 | #endif
|
---|
45 |
|
---|
46 | #undef GL_EXT_FUNCS_GEN
|
---|
47 | #define GL_EXT_FUNCS_GEN \
|
---|
48 | /* GL_ARB_shader_objects */ \
|
---|
49 | USE_GL_FUNC(WINED3D_PFNGLGETOBJECTPARAMETERIVARBPROC, \
|
---|
50 | glGetObjectParameterivARB, ARB_SHADER_OBJECTS, NULL) \
|
---|
51 | USE_GL_FUNC(WINED3D_PFNGLGETOBJECTPARAMETERFVARBPROC, \
|
---|
52 | glGetObjectParameterfvARB, ARB_SHADER_OBJECTS, NULL) \
|
---|
53 | USE_GL_FUNC(WINED3D_PFNGLGETUNIFORMLOCATIONARBPROC, \
|
---|
54 | glGetUniformLocationARB, ARB_SHADER_OBJECTS, NULL) \
|
---|
55 | USE_GL_FUNC(WINED3D_PFNGLGETACTIVEUNIFORMARBPROC, \
|
---|
56 | glGetActiveUniformARB, ARB_SHADER_OBJECTS, NULL) \
|
---|
57 | USE_GL_FUNC(WINED3D_PFNGLUNIFORM1IARBPROC, \
|
---|
58 | glUniform1iARB, ARB_SHADER_OBJECTS, NULL) \
|
---|
59 | USE_GL_FUNC(WINED3D_PFNGLUNIFORM2IARBPROC, \
|
---|
60 | glUniform2iARB, ARB_SHADER_OBJECTS, NULL) \
|
---|
61 | USE_GL_FUNC(WINED3D_PFNGLUNIFORM3IARBPROC, \
|
---|
62 | glUniform3iARB, ARB_SHADER_OBJECTS, NULL) \
|
---|
63 | USE_GL_FUNC(WINED3D_PFNGLUNIFORM4IARBPROC, \
|
---|
64 | glUniform4iARB, ARB_SHADER_OBJECTS, NULL) \
|
---|
65 | USE_GL_FUNC(WINED3D_PFNGLUNIFORM1FARBPROC, \
|
---|
66 | glUniform1fARB, ARB_SHADER_OBJECTS, NULL) \
|
---|
67 | USE_GL_FUNC(WINED3D_PFNGLUNIFORM2FARBPROC, \
|
---|
68 | glUniform2fARB, ARB_SHADER_OBJECTS, NULL) \
|
---|
69 | USE_GL_FUNC(WINED3D_PFNGLUNIFORM3FARBPROC, \
|
---|
70 | glUniform3fARB, ARB_SHADER_OBJECTS, NULL) \
|
---|
71 | USE_GL_FUNC(WINED3D_PFNGLUNIFORM4FARBPROC, \
|
---|
72 | glUniform4fARB, ARB_SHADER_OBJECTS, NULL) \
|
---|
73 | USE_GL_FUNC(WINED3D_PFNGLUNIFORM1FVARBPROC, \
|
---|
74 | glUniform1fvARB, ARB_SHADER_OBJECTS, NULL) \
|
---|
75 | USE_GL_FUNC(WINED3D_PFNGLUNIFORM2FVARBPROC, \
|
---|
76 | glUniform2fvARB, ARB_SHADER_OBJECTS, NULL) \
|
---|
77 | USE_GL_FUNC(WINED3D_PFNGLUNIFORM3FVARBPROC, \
|
---|
78 | glUniform3fvARB, ARB_SHADER_OBJECTS, NULL) \
|
---|
79 | USE_GL_FUNC(WINED3D_PFNGLUNIFORM4FVARBPROC, \
|
---|
80 | glUniform4fvARB, ARB_SHADER_OBJECTS, NULL) \
|
---|
81 | USE_GL_FUNC(WINED3D_PFNGLUNIFORM1IVARBPROC, \
|
---|
82 | glUniform1ivARB, ARB_SHADER_OBJECTS, NULL) \
|
---|
83 | USE_GL_FUNC(WINED3D_PFNGLUNIFORM2IVARBPROC, \
|
---|
84 | glUniform2ivARB, ARB_SHADER_OBJECTS, NULL) \
|
---|
85 | USE_GL_FUNC(WINED3D_PFNGLUNIFORM3IVARBPROC, \
|
---|
86 | glUniform3ivARB, ARB_SHADER_OBJECTS, NULL) \
|
---|
87 | USE_GL_FUNC(WINED3D_PFNGLUNIFORM4IVARBPROC, \
|
---|
88 | glUniform4ivARB, ARB_SHADER_OBJECTS, NULL) \
|
---|
89 | USE_GL_FUNC(WINED3D_PFNGLUNIFORMMATRIX2FVARBPROC, \
|
---|
90 | glUniformMatrix2fvARB, ARB_SHADER_OBJECTS, NULL) \
|
---|
91 | USE_GL_FUNC(WINED3D_PFNGLUNIFORMMATRIX3FVARBPROC, \
|
---|
92 | glUniformMatrix3fvARB, ARB_SHADER_OBJECTS, NULL) \
|
---|
93 | USE_GL_FUNC(WINED3D_PFNGLUNIFORMMATRIX4FVARBPROC, \
|
---|
94 | glUniformMatrix4fvARB, ARB_SHADER_OBJECTS, NULL) \
|
---|
95 | USE_GL_FUNC(WINED3D_PFNGLGETUNIFORMFVARBPROC, \
|
---|
96 | glGetUniformfvARB, ARB_SHADER_OBJECTS, NULL) \
|
---|
97 | USE_GL_FUNC(WINED3D_PFNGLGETUNIFORMIVARBPROC, \
|
---|
98 | glGetUniformivARB, ARB_SHADER_OBJECTS, NULL) \
|
---|
99 | USE_GL_FUNC(WINED3D_PFNGLGETINFOLOGARBPROC, \
|
---|
100 | glGetInfoLogARB, ARB_SHADER_OBJECTS, NULL) \
|
---|
101 | USE_GL_FUNC(WINED3D_PFNGLUSEPROGRAMOBJECTARBPROC, \
|
---|
102 | glUseProgramObjectARB, ARB_SHADER_OBJECTS, NULL) \
|
---|
103 | USE_GL_FUNC(WINED3D_PFNGLCREATESHADEROBJECTARBPROC, \
|
---|
104 | glCreateShaderObjectARB, ARB_SHADER_OBJECTS, NULL) \
|
---|
105 | USE_GL_FUNC(WINED3D_PFNGLSHADERSOURCEARBPROC, \
|
---|
106 | glShaderSourceARB, ARB_SHADER_OBJECTS, NULL) \
|
---|
107 | USE_GL_FUNC(WINED3D_PFNGLCOMPILESHADERARBPROC, \
|
---|
108 | glCompileShaderARB, ARB_SHADER_OBJECTS, NULL) \
|
---|
109 | USE_GL_FUNC(WINED3D_PFNGLCREATEPROGRAMOBJECTARBPROC, \
|
---|
110 | glCreateProgramObjectARB, ARB_SHADER_OBJECTS, NULL) \
|
---|
111 | USE_GL_FUNC(WINED3D_PFNGLATTACHOBJECTARBPROC, \
|
---|
112 | glAttachObjectARB, ARB_SHADER_OBJECTS, NULL) \
|
---|
113 | USE_GL_FUNC(WINED3D_PFNGLLINKPROGRAMARBPROC, \
|
---|
114 | glLinkProgramARB, ARB_SHADER_OBJECTS, NULL) \
|
---|
115 | USE_GL_FUNC(WINED3D_PFNGLDETACHOBJECTARBPROC, \
|
---|
116 | glDetachObjectARB, ARB_SHADER_OBJECTS, NULL) \
|
---|
117 | USE_GL_FUNC(WINED3D_PFNGLDELETEOBJECTARBPROC, \
|
---|
118 | glDeleteObjectARB, ARB_SHADER_OBJECTS, NULL) \
|
---|
119 | USE_GL_FUNC(WINED3D_PFNGLVALIDATEPROGRAMARBPROC, \
|
---|
120 | glValidateProgramARB, ARB_SHADER_OBJECTS, NULL) \
|
---|
121 | USE_GL_FUNC(WINED3D_PFNGLGETATTACHEDOBJECTSARBPROC, \
|
---|
122 | glGetAttachedObjectsARB, ARB_SHADER_OBJECTS, NULL) \
|
---|
123 | USE_GL_FUNC(WINED3D_PFNGLGETHANDLEARBPROC, \
|
---|
124 | glGetHandleARB, ARB_SHADER_OBJECTS, NULL) \
|
---|
125 | USE_GL_FUNC(WINED3D_PFNGLGETSHADERSOURCEARBPROC, \
|
---|
126 | glGetShaderSourceARB, ARB_SHADER_OBJECTS, NULL) \
|
---|
127 | USE_GL_FUNC(WINED3D_PFNGLBINDATTRIBLOCATIONARBPROC, \
|
---|
128 | glBindAttribLocationARB, ARB_SHADER_OBJECTS, NULL) \
|
---|
129 | USE_GL_FUNC(WINED3D_PFNGLGETATTRIBLOCATIONARBPROC, \
|
---|
130 | glGetAttribLocationARB, ARB_SHADER_OBJECTS, NULL) \
|
---|
131 |
|
---|
132 | extern BOOL IWineD3DImpl_FillGLCaps(struct wined3d_adapter *adapter);
|
---|
133 |
|
---|
134 | static struct wined3d_context *pCurrentContext = NULL;
|
---|
135 | static struct wined3d_adapter adapter = {0};
|
---|
136 | static bool fInitializedLibrary = false;
|
---|
137 |
|
---|
138 | #define SHADER_SET_CURRENT_CONTEXT(ctx) \
|
---|
139 | pCurrentContext = (struct wined3d_context *)ctx;
|
---|
140 |
|
---|
141 | SHADERDECL(int) ShaderInitLib()
|
---|
142 | {
|
---|
143 | struct wined3d_gl_info *gl_info = &adapter.gl_info;
|
---|
144 |
|
---|
145 | #ifdef RT_OS_WINDOWS
|
---|
146 | #define USE_GL_FUNC(pfn) pfn = (void*)GetProcAddress(GetModuleHandle("opengl32.dll"), #pfn);
|
---|
147 | #else
|
---|
148 | #define USE_GL_FUNC(pfn) pfn = (void*)OGLGETPROCADDRESS(#pfn);
|
---|
149 | #endif
|
---|
150 |
|
---|
151 | /* Dynamically load all GL core functions */
|
---|
152 | GL_FUNCS_GEN;
|
---|
153 | #undef USE_GL_FUNC
|
---|
154 |
|
---|
155 | #define USE_GL_FUNC(type, pfn, ext, replace) \
|
---|
156 | { \
|
---|
157 | gl_info->pfn = (void*)OGLGETPROCADDRESS(#pfn); \
|
---|
158 | }
|
---|
159 | GL_EXT_FUNCS_GEN;
|
---|
160 |
|
---|
161 | IWineD3DImpl_FillGLCaps(&adapter);
|
---|
162 | fInitializedLibrary = true;
|
---|
163 | return VINF_SUCCESS;
|
---|
164 | }
|
---|
165 |
|
---|
166 | SHADERDECL(int) ShaderDestroyLib()
|
---|
167 | {
|
---|
168 | return VINF_SUCCESS;
|
---|
169 | }
|
---|
170 |
|
---|
171 | struct IWineD3DDeviceImpl *context_get_device(const struct wined3d_context *context)
|
---|
172 | {
|
---|
173 | return context->pDeviceContext;
|
---|
174 | }
|
---|
175 |
|
---|
176 | struct wined3d_context *context_get_current(void)
|
---|
177 | {
|
---|
178 | return pCurrentContext;
|
---|
179 | }
|
---|
180 |
|
---|
181 | struct wined3d_context *context_acquire(IWineD3DDeviceImpl *This, IWineD3DSurface *target, enum ContextUsage usage)
|
---|
182 | {
|
---|
183 | return pCurrentContext;
|
---|
184 | }
|
---|
185 |
|
---|
186 | SHADERDECL(int) ShaderContextCreate(void **ppShaderContext)
|
---|
187 | {
|
---|
188 | struct wined3d_context *pContext;
|
---|
189 | HRESULT hr;
|
---|
190 |
|
---|
191 | pContext = (struct wined3d_context *)RTMemAllocZ(sizeof(struct wined3d_context));
|
---|
192 | AssertReturn(pContext, VERR_NO_MEMORY);
|
---|
193 | pContext->pDeviceContext = (IWineD3DDeviceImpl *)RTMemAllocZ(sizeof(IWineD3DDeviceImpl));
|
---|
194 | AssertReturn(pContext->pDeviceContext, VERR_NO_MEMORY);
|
---|
195 |
|
---|
196 | pContext->gl_info = &adapter.gl_info;
|
---|
197 |
|
---|
198 | pContext->pDeviceContext->adapter = &adapter;
|
---|
199 | pContext->pDeviceContext->shader_backend = &glsl_shader_backend;
|
---|
200 | pContext->pDeviceContext->ps_selected_mode = SHADER_GLSL;
|
---|
201 | pContext->pDeviceContext->vs_selected_mode = SHADER_GLSL;
|
---|
202 | pContext->render_offscreen = false;
|
---|
203 |
|
---|
204 | list_init(&pContext->pDeviceContext->shaders);
|
---|
205 |
|
---|
206 | if (fInitializedLibrary)
|
---|
207 | {
|
---|
208 | struct shader_caps shader_caps;
|
---|
209 | uint32_t state;
|
---|
210 |
|
---|
211 | /* Initialize the shader backend. */
|
---|
212 | hr = pContext->pDeviceContext->shader_backend->shader_alloc_private((IWineD3DDevice *)pContext->pDeviceContext);
|
---|
213 | AssertReturn(hr == S_OK, VERR_INTERNAL_ERROR);
|
---|
214 |
|
---|
215 | memset(&shader_caps, 0, sizeof(shader_caps));
|
---|
216 | pContext->pDeviceContext->shader_backend->shader_get_caps(&adapter.gl_info, &shader_caps);
|
---|
217 | pContext->pDeviceContext->d3d_vshader_constantF = shader_caps.MaxVertexShaderConst;
|
---|
218 | pContext->pDeviceContext->d3d_pshader_constantF = shader_caps.MaxPixelShaderConst;
|
---|
219 | pContext->pDeviceContext->vs_clipping = shader_caps.VSClipping;
|
---|
220 |
|
---|
221 | pContext->pDeviceContext->stateBlock = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*pContext->pDeviceContext->stateBlock));
|
---|
222 | AssertReturn(pContext->pDeviceContext->stateBlock, VERR_NO_MEMORY);
|
---|
223 | hr = stateblock_init(pContext->pDeviceContext->stateBlock, pContext->pDeviceContext, 0);
|
---|
224 | AssertReturn(hr == S_OK, VERR_INTERNAL_ERROR);
|
---|
225 | pContext->pDeviceContext->updateStateBlock = pContext->pDeviceContext->stateBlock;
|
---|
226 |
|
---|
227 | pContext->pDeviceContext->stateBlock->vertexDecl = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(IWineD3DVertexDeclarationImpl));
|
---|
228 | AssertReturn(pContext->pDeviceContext->stateBlock->vertexDecl, VERR_NO_MEMORY);
|
---|
229 |
|
---|
230 | /* Initialize the texture unit mapping to a 1:1 mapping */
|
---|
231 | for (state = 0; state < MAX_COMBINED_SAMPLERS; ++state)
|
---|
232 | {
|
---|
233 | if (state < pContext->gl_info->limits.fragment_samplers)
|
---|
234 | {
|
---|
235 | pContext->pDeviceContext->texUnitMap[state] = state;
|
---|
236 | pContext->pDeviceContext->rev_tex_unit_map[state] = state;
|
---|
237 | } else {
|
---|
238 | pContext->pDeviceContext->texUnitMap[state] = WINED3D_UNMAPPED_STAGE;
|
---|
239 | pContext->pDeviceContext->rev_tex_unit_map[state] = WINED3D_UNMAPPED_STAGE;
|
---|
240 | }
|
---|
241 | }
|
---|
242 | }
|
---|
243 |
|
---|
244 | *ppShaderContext = (void *)pContext;
|
---|
245 | return VINF_SUCCESS;
|
---|
246 | }
|
---|
247 |
|
---|
248 | SHADERDECL(int) ShaderContextDestroy(void *pShaderContext)
|
---|
249 | {
|
---|
250 | struct wined3d_context *pContext = (struct wined3d_context *)pShaderContext;
|
---|
251 |
|
---|
252 | if (pContext->pDeviceContext)
|
---|
253 | {
|
---|
254 | IWineD3DStateBlockImpl *This = pContext->pDeviceContext->stateBlock;
|
---|
255 |
|
---|
256 | /* Fails during init only. */
|
---|
257 | if (pContext->pDeviceContext->shader_priv)
|
---|
258 | pContext->pDeviceContext->shader_backend->shader_free_private((IWineD3DDevice *)pContext->pDeviceContext);
|
---|
259 |
|
---|
260 | if (This)
|
---|
261 | {
|
---|
262 | if (This->vertexShaderConstantF)
|
---|
263 | HeapFree(GetProcessHeap(), 0, This->vertexShaderConstantF);
|
---|
264 | if (This->changed.vertexShaderConstantsF)
|
---|
265 | HeapFree(GetProcessHeap(), 0, This->changed.vertexShaderConstantsF);
|
---|
266 | if (This->pixelShaderConstantF)
|
---|
267 | HeapFree(GetProcessHeap(), 0, This->pixelShaderConstantF);
|
---|
268 | if (This->changed.pixelShaderConstantsF)
|
---|
269 | HeapFree(GetProcessHeap(), 0, This->changed.pixelShaderConstantsF);
|
---|
270 | if (This->contained_vs_consts_f)
|
---|
271 | HeapFree(GetProcessHeap(), 0, This->contained_vs_consts_f);
|
---|
272 | if (This->contained_ps_consts_f)
|
---|
273 | HeapFree(GetProcessHeap(), 0, This->contained_ps_consts_f);
|
---|
274 | if (This->vertexDecl)
|
---|
275 | HeapFree(GetProcessHeap(), 0, This->vertexDecl);
|
---|
276 | HeapFree(GetProcessHeap(), 0, This);
|
---|
277 | }
|
---|
278 |
|
---|
279 | RTMemFree(pContext->pDeviceContext);
|
---|
280 | }
|
---|
281 | RTMemFree(pShaderContext);
|
---|
282 | return VINF_SUCCESS;
|
---|
283 | }
|
---|
284 |
|
---|
285 | SHADERDECL(int) ShaderCreateVertexShader(void *pShaderContext, const uint32_t *pShaderData, void **pShaderObj)
|
---|
286 | {
|
---|
287 | IWineD3DDeviceImpl *This;
|
---|
288 | IWineD3DVertexShaderImpl *object;
|
---|
289 | HRESULT hr;
|
---|
290 |
|
---|
291 | SHADER_SET_CURRENT_CONTEXT(pShaderContext);
|
---|
292 | This = pCurrentContext->pDeviceContext;
|
---|
293 |
|
---|
294 | object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
|
---|
295 | if (!object)
|
---|
296 | {
|
---|
297 | Log(("Failed to allocate shader memory.\n"));
|
---|
298 | return VERR_NO_MEMORY;
|
---|
299 | }
|
---|
300 |
|
---|
301 | hr = vertexshader_init(object, This, pShaderData, NULL, NULL, NULL);
|
---|
302 | if (FAILED(hr))
|
---|
303 | {
|
---|
304 | Log(("Failed to initialize vertex shader, hr %#x.\n", hr));
|
---|
305 | HeapFree(GetProcessHeap(), 0, object);
|
---|
306 | return VERR_INTERNAL_ERROR;
|
---|
307 | }
|
---|
308 |
|
---|
309 | #ifdef VBOX_WINE_WITH_SHADER_CACHE
|
---|
310 | object = vertexshader_check_cached(This, object);
|
---|
311 | #endif
|
---|
312 |
|
---|
313 | Log(("Created vertex shader %p.\n", object));
|
---|
314 | *pShaderObj = (void *)object;
|
---|
315 |
|
---|
316 | return VINF_SUCCESS;
|
---|
317 | }
|
---|
318 |
|
---|
319 | SHADERDECL(int) ShaderCreatePixelShader(void *pShaderContext, const uint32_t *pShaderData, void **pShaderObj)
|
---|
320 | {
|
---|
321 | IWineD3DDeviceImpl *This;
|
---|
322 | IWineD3DPixelShaderImpl *object;
|
---|
323 | HRESULT hr;
|
---|
324 |
|
---|
325 | SHADER_SET_CURRENT_CONTEXT(pShaderContext);
|
---|
326 | This = pCurrentContext->pDeviceContext;
|
---|
327 |
|
---|
328 | object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
|
---|
329 | if (!object)
|
---|
330 | {
|
---|
331 | Log(("Failed to allocate shader memory.\n"));
|
---|
332 | return VERR_NO_MEMORY;
|
---|
333 | }
|
---|
334 |
|
---|
335 | hr = pixelshader_init(object, This, pShaderData, NULL, NULL, NULL);
|
---|
336 | if (FAILED(hr))
|
---|
337 | {
|
---|
338 | Log(("Failed to initialize pixel shader, hr %#x.\n", hr));
|
---|
339 | HeapFree(GetProcessHeap(), 0, object);
|
---|
340 | return VERR_INTERNAL_ERROR;
|
---|
341 | }
|
---|
342 |
|
---|
343 | #ifdef VBOX_WINE_WITH_SHADER_CACHE
|
---|
344 | object = pixelshader_check_cached(This, object);
|
---|
345 | #endif
|
---|
346 |
|
---|
347 | Log(("Created pixel shader %p.\n", object));
|
---|
348 | *pShaderObj = (void *)object;
|
---|
349 | return VINF_SUCCESS;
|
---|
350 | }
|
---|
351 |
|
---|
352 | SHADERDECL(int) ShaderDestroyVertexShader(void *pShaderContext, void *pShaderObj)
|
---|
353 | {
|
---|
354 | IWineD3DVertexShaderImpl *object = (IWineD3DVertexShaderImpl *)pShaderObj;
|
---|
355 | AssertReturn(pShaderObj, VERR_INVALID_PARAMETER);
|
---|
356 |
|
---|
357 | SHADER_SET_CURRENT_CONTEXT(pShaderContext);
|
---|
358 |
|
---|
359 | object->lpVtbl->Release((IWineD3DVertexShader *)object);
|
---|
360 | return VINF_SUCCESS;
|
---|
361 | }
|
---|
362 |
|
---|
363 | SHADERDECL(int) ShaderDestroyPixelShader(void *pShaderContext, void *pShaderObj)
|
---|
364 | {
|
---|
365 | IWineD3DPixelShaderImpl *object = (IWineD3DPixelShaderImpl *)pShaderObj;
|
---|
366 | AssertReturn(pShaderObj, VERR_INVALID_PARAMETER);
|
---|
367 |
|
---|
368 | SHADER_SET_CURRENT_CONTEXT(pShaderContext);
|
---|
369 |
|
---|
370 | object->lpVtbl->Release((IWineD3DPixelShader *)object);
|
---|
371 | return VINF_SUCCESS;
|
---|
372 | }
|
---|
373 |
|
---|
374 | SHADERDECL(int) ShaderSetVertexShader(void *pShaderContext, void *pShaderObj)
|
---|
375 | {
|
---|
376 | IWineD3DDeviceImpl *This;
|
---|
377 | IWineD3DVertexShader* pShader;
|
---|
378 | IWineD3DVertexShader* oldShader;
|
---|
379 |
|
---|
380 | SHADER_SET_CURRENT_CONTEXT(pShaderContext);
|
---|
381 | This = pCurrentContext->pDeviceContext;
|
---|
382 | pShader = (IWineD3DVertexShader* )pShaderObj;
|
---|
383 | oldShader = This->updateStateBlock->vertexShader;
|
---|
384 |
|
---|
385 | if(oldShader == pShader) {
|
---|
386 | /* Checked here to allow proper stateblock recording */
|
---|
387 | Log(("App is setting the old shader over, nothing to do\n"));
|
---|
388 | return VINF_SUCCESS;
|
---|
389 | }
|
---|
390 |
|
---|
391 | This->updateStateBlock->vertexShader = pShader;
|
---|
392 | This->updateStateBlock->changed.vertexShader = TRUE;
|
---|
393 |
|
---|
394 | Log(("(%p) : setting pShader(%p)\n", This, pShader));
|
---|
395 | if(pShader) IWineD3DVertexShader_AddRef(pShader);
|
---|
396 | if(oldShader) IWineD3DVertexShader_Release(oldShader);
|
---|
397 |
|
---|
398 | pCurrentContext->fChangedVertexShader = true;
|
---|
399 | pCurrentContext->fChangedVertexShaderConstant = true; /* force constant reload. */
|
---|
400 |
|
---|
401 | return VINF_SUCCESS;
|
---|
402 | }
|
---|
403 |
|
---|
404 | SHADERDECL(int) ShaderSetPixelShader(void *pShaderContext, void *pShaderObj)
|
---|
405 | {
|
---|
406 | IWineD3DDeviceImpl *This;
|
---|
407 | IWineD3DPixelShader* pShader;
|
---|
408 | IWineD3DPixelShader* oldShader;
|
---|
409 |
|
---|
410 | SHADER_SET_CURRENT_CONTEXT(pShaderContext);
|
---|
411 | This = pCurrentContext->pDeviceContext;
|
---|
412 | pShader = (IWineD3DPixelShader* )pShaderObj;
|
---|
413 | oldShader = This->updateStateBlock->pixelShader;
|
---|
414 |
|
---|
415 | if(oldShader == pShader) {
|
---|
416 | /* Checked here to allow proper stateblock recording */
|
---|
417 | Log(("App is setting the old shader over, nothing to do\n"));
|
---|
418 | return VINF_SUCCESS;
|
---|
419 | }
|
---|
420 |
|
---|
421 | This->updateStateBlock->pixelShader = pShader;
|
---|
422 | This->updateStateBlock->changed.pixelShader = TRUE;
|
---|
423 |
|
---|
424 | Log(("(%p) : setting pShader(%p)\n", This, pShader));
|
---|
425 | if(pShader) IWineD3DPixelShader_AddRef(pShader);
|
---|
426 | if(oldShader) IWineD3DPixelShader_Release(oldShader);
|
---|
427 |
|
---|
428 | pCurrentContext->fChangedPixelShader = true;
|
---|
429 | pCurrentContext->fChangedPixelShaderConstant = true; /* force constant reload. */
|
---|
430 | return VINF_SUCCESS;
|
---|
431 | }
|
---|
432 |
|
---|
433 | SHADERDECL(int) ShaderSetVertexShaderConstantB(void *pShaderContext, uint32_t start, const uint8_t *srcData, uint32_t count)
|
---|
434 | {
|
---|
435 | IWineD3DDeviceImpl *This;
|
---|
436 | unsigned int i, cnt = min(count, MAX_CONST_B - start);
|
---|
437 |
|
---|
438 | SHADER_SET_CURRENT_CONTEXT(pShaderContext);
|
---|
439 | This = pCurrentContext->pDeviceContext;
|
---|
440 |
|
---|
441 | Log(("(ShaderSetVertexShaderConstantB %p, srcData %p, start %d, count %d)\n",
|
---|
442 | srcData, start, count));
|
---|
443 |
|
---|
444 | if (!srcData || start >= MAX_CONST_B)
|
---|
445 | {
|
---|
446 | Log(("incorrect vertex shader const data: start(%u), srcData(0x%p), count(%u)", start, srcData, count));
|
---|
447 | return VERR_INVALID_PARAMETER;
|
---|
448 | }
|
---|
449 |
|
---|
450 | memcpy(&This->updateStateBlock->vertexShaderConstantB[start], srcData, cnt * sizeof(BOOL));
|
---|
451 | for (i = 0; i < cnt; i++)
|
---|
452 | Log(("Set BOOL constant %u to %s\n", start + i, srcData[i]? "true":"false"));
|
---|
453 |
|
---|
454 | for (i = start; i < cnt + start; ++i) {
|
---|
455 | This->updateStateBlock->changed.vertexShaderConstantsB |= (1 << i);
|
---|
456 | }
|
---|
457 |
|
---|
458 | pCurrentContext->fChangedVertexShaderConstant = true;
|
---|
459 |
|
---|
460 | return VINF_SUCCESS;
|
---|
461 | }
|
---|
462 |
|
---|
463 | SHADERDECL(int) ShaderSetVertexShaderConstantI(void *pShaderContext, uint32_t start, const int32_t *srcData, uint32_t count)
|
---|
464 | {
|
---|
465 | IWineD3DDeviceImpl *This;
|
---|
466 | unsigned int i, cnt = min(count, MAX_CONST_I - start);
|
---|
467 |
|
---|
468 | SHADER_SET_CURRENT_CONTEXT(pShaderContext);
|
---|
469 | This = pCurrentContext->pDeviceContext;
|
---|
470 |
|
---|
471 | Log(("(ShaderSetVertexShaderConstantI %p, srcData %p, start %d, count %d)\n",
|
---|
472 | srcData, start, count));
|
---|
473 |
|
---|
474 | if (!srcData || start >= MAX_CONST_I)
|
---|
475 | {
|
---|
476 | Log(("incorrect vertex shader const data: start(%u), srcData(0x%p), count(%u)", start, srcData, count));
|
---|
477 | return VERR_INVALID_PARAMETER;
|
---|
478 | }
|
---|
479 |
|
---|
480 | memcpy(&This->updateStateBlock->vertexShaderConstantI[start * 4], srcData, cnt * sizeof(int32_t) * 4);
|
---|
481 |
|
---|
482 | for (i = start; i < cnt + start; ++i) {
|
---|
483 | This->updateStateBlock->changed.vertexShaderConstantsI |= (1 << i);
|
---|
484 | }
|
---|
485 |
|
---|
486 | pCurrentContext->fChangedVertexShaderConstant = true;
|
---|
487 |
|
---|
488 | return VINF_SUCCESS;
|
---|
489 | }
|
---|
490 |
|
---|
491 | SHADERDECL(int) ShaderSetVertexShaderConstantF(void *pShaderContext, uint32_t start, const float *srcData, uint32_t count)
|
---|
492 | {
|
---|
493 | IWineD3DDeviceImpl *This;
|
---|
494 |
|
---|
495 | SHADER_SET_CURRENT_CONTEXT(pShaderContext);
|
---|
496 | This = pCurrentContext->pDeviceContext;
|
---|
497 |
|
---|
498 | Log(("(ShaderSetVertexShaderConstantF %p, srcData %p, start %d, count %d)\n",
|
---|
499 | srcData, start, count));
|
---|
500 |
|
---|
501 | if (srcData == NULL || start + count > This->d3d_vshader_constantF || start > This->d3d_vshader_constantF)
|
---|
502 | {
|
---|
503 | Log(("incorrect vertex shader const data: start(%u), srcData(0x%p), count(%u)", start, srcData, count));
|
---|
504 | return VERR_INVALID_PARAMETER;
|
---|
505 | }
|
---|
506 | memcpy(&This->updateStateBlock->vertexShaderConstantF[start * 4], srcData, count * sizeof(float) * 4);
|
---|
507 |
|
---|
508 | This->shader_backend->shader_update_float_vertex_constants((IWineD3DDevice *)This, start, count);
|
---|
509 |
|
---|
510 | memset(This->updateStateBlock->changed.vertexShaderConstantsF + start, 1,
|
---|
511 | sizeof(*This->updateStateBlock->changed.vertexShaderConstantsF) * count);
|
---|
512 |
|
---|
513 | pCurrentContext->fChangedVertexShaderConstant = true;
|
---|
514 |
|
---|
515 | return VINF_SUCCESS;
|
---|
516 | }
|
---|
517 |
|
---|
518 | SHADERDECL(int) ShaderSetPixelShaderConstantB(void *pShaderContext, uint32_t start, const uint8_t *srcData, uint32_t count)
|
---|
519 | {
|
---|
520 | IWineD3DDeviceImpl *This;
|
---|
521 | unsigned int i, cnt = min(count, MAX_CONST_B - start);
|
---|
522 |
|
---|
523 | SHADER_SET_CURRENT_CONTEXT(pShaderContext);
|
---|
524 | This = pCurrentContext->pDeviceContext;
|
---|
525 |
|
---|
526 | Log(("(ShaderSetPixelShaderConstantB %p, srcData %p, start %d, count %d)\n",
|
---|
527 | srcData, start, count));
|
---|
528 |
|
---|
529 | if (!srcData || start >= MAX_CONST_B)
|
---|
530 | {
|
---|
531 | Log(("incorrect pixel shader const data: start(%u), srcData(0x%p), count(%u)", start, srcData, count));
|
---|
532 | return VERR_INVALID_PARAMETER;
|
---|
533 | }
|
---|
534 |
|
---|
535 | memcpy(&This->updateStateBlock->pixelShaderConstantB[start], srcData, cnt * sizeof(BOOL));
|
---|
536 | for (i = 0; i < cnt; i++)
|
---|
537 | Log(("Set BOOL constant %u to %s\n", start + i, srcData[i]? "true":"false"));
|
---|
538 |
|
---|
539 | for (i = start; i < cnt + start; ++i) {
|
---|
540 | This->updateStateBlock->changed.pixelShaderConstantsB |= (1 << i);
|
---|
541 | }
|
---|
542 |
|
---|
543 | pCurrentContext->fChangedPixelShaderConstant = true;
|
---|
544 |
|
---|
545 | return VINF_SUCCESS;
|
---|
546 | }
|
---|
547 |
|
---|
548 | SHADERDECL(int) ShaderSetPixelShaderConstantI(void *pShaderContext, uint32_t start, const int32_t *srcData, uint32_t count)
|
---|
549 | {
|
---|
550 | IWineD3DDeviceImpl *This;
|
---|
551 | unsigned int i, cnt = min(count, MAX_CONST_I - start);
|
---|
552 |
|
---|
553 | SHADER_SET_CURRENT_CONTEXT(pShaderContext);
|
---|
554 | This = pCurrentContext->pDeviceContext;
|
---|
555 |
|
---|
556 | Log(("(ShaderSetPixelShaderConstantI %p, srcData %p, start %d, count %d)\n",
|
---|
557 | srcData, start, count));
|
---|
558 |
|
---|
559 | if (!srcData || start >= MAX_CONST_I)
|
---|
560 | {
|
---|
561 | Log(("incorrect pixel shader const data: start(%u), srcData(0x%p), count(%u)", start, srcData, count));
|
---|
562 | return VERR_INVALID_PARAMETER;
|
---|
563 | }
|
---|
564 |
|
---|
565 | memcpy(&This->updateStateBlock->pixelShaderConstantI[start * 4], srcData, cnt * sizeof(int32_t) * 4);
|
---|
566 |
|
---|
567 | for (i = start; i < cnt + start; ++i) {
|
---|
568 | This->updateStateBlock->changed.pixelShaderConstantsI |= (1 << i);
|
---|
569 | }
|
---|
570 |
|
---|
571 | pCurrentContext->fChangedPixelShaderConstant = true;
|
---|
572 |
|
---|
573 | return VINF_SUCCESS;
|
---|
574 | }
|
---|
575 |
|
---|
576 | SHADERDECL(int) ShaderSetPixelShaderConstantF(void *pShaderContext, uint32_t start, const float *srcData, uint32_t count)
|
---|
577 | {
|
---|
578 | IWineD3DDeviceImpl *This;
|
---|
579 |
|
---|
580 | SHADER_SET_CURRENT_CONTEXT(pShaderContext);
|
---|
581 | This = pCurrentContext->pDeviceContext;
|
---|
582 |
|
---|
583 | Log(("(ShaderSetPixelShaderConstantF %p, srcData %p, start %d, count %d)\n",
|
---|
584 | srcData, start, count));
|
---|
585 |
|
---|
586 | if (srcData == NULL || start + count > This->d3d_pshader_constantF || start > This->d3d_pshader_constantF)
|
---|
587 | {
|
---|
588 | Log(("incorrect pixel shader const data: start(%u), srcData(0x%p), count(%u)", start, srcData, count));
|
---|
589 | return VERR_INVALID_PARAMETER;
|
---|
590 | }
|
---|
591 |
|
---|
592 | memcpy(&This->updateStateBlock->pixelShaderConstantF[start * 4], srcData, count * sizeof(float) * 4);
|
---|
593 |
|
---|
594 | This->shader_backend->shader_update_float_pixel_constants((IWineD3DDevice *)This, start, count);
|
---|
595 |
|
---|
596 | memset(This->updateStateBlock->changed.pixelShaderConstantsF + start, 1,
|
---|
597 | sizeof(*This->updateStateBlock->changed.pixelShaderConstantsF) * count);
|
---|
598 |
|
---|
599 | pCurrentContext->fChangedPixelShaderConstant = true;
|
---|
600 |
|
---|
601 | return VINF_SUCCESS;
|
---|
602 | }
|
---|
603 |
|
---|
604 | SHADERDECL(int) ShaderUpdateState(void *pShaderContext, uint32_t rtHeight)
|
---|
605 | {
|
---|
606 | IWineD3DDeviceImpl *pThis;
|
---|
607 | GLfloat yoffset;
|
---|
608 | GLint viewport[4];
|
---|
609 |
|
---|
610 | SHADER_SET_CURRENT_CONTEXT(pShaderContext);
|
---|
611 | pThis = pCurrentContext->pDeviceContext;
|
---|
612 |
|
---|
613 | glGetIntegerv(GL_VIEWPORT, viewport);
|
---|
614 | #ifdef DEBUG
|
---|
615 | AssertReturn(glGetError() == GL_NO_ERROR, VERR_INTERNAL_ERROR);
|
---|
616 | #endif
|
---|
617 |
|
---|
618 | yoffset = -(63.0f / 64.0f) / viewport[3] /* height */;
|
---|
619 | pThis->posFixup[0] = 1.0f; /* This is needed to get the x coord unmodified through a MAD. */
|
---|
620 | pThis->posFixup[1] = -1.0f; /* y-inversion */
|
---|
621 | pThis->posFixup[2] = (63.0f / 64.0f) / viewport[2] /* width */;
|
---|
622 | pThis->posFixup[3] = pThis->posFixup[1] * yoffset;
|
---|
623 |
|
---|
624 | pThis->rtHeight = rtHeight;
|
---|
625 |
|
---|
626 | /* @todo missing state:
|
---|
627 | * - fog enable (stateblock->renderState[WINED3DRS_FOGENABLE])
|
---|
628 | * - fog mode (stateblock->renderState[WINED3DRS_FOGTABLEMODE])
|
---|
629 | * - stateblock->vertexDecl->position_transformed
|
---|
630 | */
|
---|
631 |
|
---|
632 | if ( pCurrentContext->fChangedPixelShader
|
---|
633 | || pCurrentContext->fChangedVertexShader)
|
---|
634 | pThis->shader_backend->shader_select(pCurrentContext, !!pThis->updateStateBlock->pixelShader, !!pThis->updateStateBlock->vertexShader);
|
---|
635 | pCurrentContext->fChangedPixelShader = pCurrentContext->fChangedVertexShader = false;
|
---|
636 |
|
---|
637 | if ( pCurrentContext->fChangedPixelShaderConstant
|
---|
638 | || pCurrentContext->fChangedVertexShaderConstant)
|
---|
639 | pThis->shader_backend->shader_load_constants(pCurrentContext, !!pThis->updateStateBlock->pixelShader, !!pThis->updateStateBlock->vertexShader);
|
---|
640 | pCurrentContext->fChangedPixelShaderConstant = false;
|
---|
641 | pCurrentContext->fChangedVertexShaderConstant = false;
|
---|
642 |
|
---|
643 | return VINF_SUCCESS;
|
---|
644 | }
|
---|
645 |
|
---|
646 | SHADERDECL(int) ShaderTransformProjection(unsigned cxViewPort, unsigned cyViewPort, float matrix[16])
|
---|
647 | {
|
---|
648 | #ifdef DEBUG
|
---|
649 | GLenum lastError;
|
---|
650 | #endif
|
---|
651 | GLfloat xoffset, yoffset;
|
---|
652 |
|
---|
653 | /* Assumes OpenGL context has been activated. */
|
---|
654 | glMatrixMode(GL_PROJECTION);
|
---|
655 | glLoadIdentity();
|
---|
656 |
|
---|
657 | /* The rule is that the window coordinate 0 does not correspond to the
|
---|
658 | beginning of the first pixel, but the center of the first pixel.
|
---|
659 | As a consequence if you want to correctly draw one line exactly from
|
---|
660 | the left to the right end of the viewport (with all matrices set to
|
---|
661 | be identity), the x coords of both ends of the line would be not
|
---|
662 | -1 and 1 respectively but (-1-1/viewport_widh) and (1-1/viewport_width)
|
---|
663 | instead.
|
---|
664 |
|
---|
665 | 1.0 / Width is used because the coord range goes from -1.0 to 1.0, then we
|
---|
666 | divide by the Width/Height, so we need the half range(1.0) to translate by
|
---|
667 | half a pixel.
|
---|
668 |
|
---|
669 | The other fun is that d3d's output z range after the transformation is [0;1],
|
---|
670 | but opengl's is [-1;1]. Since the z buffer is in range [0;1] for both, gl
|
---|
671 | scales [-1;1] to [0;1]. This would mean that we end up in [0.5;1] and loose a lot
|
---|
672 | of Z buffer precision and the clear values do not match in the z test. Thus scale
|
---|
673 | [0;1] to [-1;1], so when gl undoes that we utilize the full z range
|
---|
674 | */
|
---|
675 |
|
---|
676 | /*
|
---|
677 | * Careful with the order of operations here, we're essentially working backwards:
|
---|
678 | * x = x + 1/w;
|
---|
679 | * y = (y - 1/h) * flip;
|
---|
680 | * z = z * 2 - 1;
|
---|
681 | *
|
---|
682 | * Becomes:
|
---|
683 | * glTranslatef(0.0, 0.0, -1.0);
|
---|
684 | * glScalef(1.0, 1.0, 2.0);
|
---|
685 | *
|
---|
686 | * glScalef(1.0, flip, 1.0);
|
---|
687 | * glTranslatef(1/w, -1/h, 0.0);
|
---|
688 | *
|
---|
689 | * This is equivalent to:
|
---|
690 | * glTranslatef(1/w, -flip/h, -1.0)
|
---|
691 | * glScalef(1.0, flip, 2.0);
|
---|
692 | */
|
---|
693 | /* Translate by slightly less than a half pixel to force a top-left
|
---|
694 | * filling convention. We want the difference to be large enough that
|
---|
695 | * it doesn't get lost due to rounding inside the driver, but small
|
---|
696 | * enough to prevent it from interfering with any anti-aliasing. */
|
---|
697 | xoffset = (63.0f / 64.0f) / cxViewPort;
|
---|
698 | yoffset = -(63.0f / 64.0f) / cyViewPort;
|
---|
699 |
|
---|
700 | glTranslatef(xoffset, -yoffset, -1.0f);
|
---|
701 | /* flip y coordinate origin too */
|
---|
702 | glScalef(1.0f, -1.0f, 2.0f);
|
---|
703 |
|
---|
704 | glMultMatrixf(matrix);
|
---|
705 |
|
---|
706 | #ifdef DEBUG
|
---|
707 | lastError = glGetError(); \
|
---|
708 | AssertMsgReturn(lastError == GL_NO_ERROR, ("%s (%d): last error 0x%x\n", __FUNCTION__, __LINE__, lastError), VERR_INTERNAL_ERROR);
|
---|
709 | #endif
|
---|
710 | return VINF_SUCCESS;
|
---|
711 | }
|
---|