VirtualBox

source: vbox/trunk/src/VBox/Devices/Graphics/shaderlib/shaderapi.c@ 96407

Last change on this file since 96407 was 96407, checked in by vboxsync, 2 years ago

scm copyright and license note update

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 33.6 KB
Line 
1/* $Id: shaderapi.c 96407 2022-08-22 17:43:14Z vboxsync $ */
2/** @file
3 * shaderlib -- interface to WINE's Direct3D shader functions
4 */
5
6/*
7 * Copyright (C) 2014-2022 Oracle and/or its affiliates.
8 *
9 * This file is part of VirtualBox base platform packages, as
10 * available from https://www.virtualbox.org.
11 *
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation, in version 3 of the
15 * License.
16 *
17 * This program is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, see <https://www.gnu.org/licenses>.
24 *
25 * SPDX-License-Identifier: GPL-3.0-only
26 */
27
28#include <iprt/errcore.h>
29#include <iprt/mem.h>
30#include <iprt/assert.h>
31#include <iprt/log.h>
32#define WINED3D_EXTERN
33#include "wined3d_private.h"
34
35#include "shaderlib.h"
36
37#ifdef RT_OS_WINDOWS
38# define OGLGETPROCADDRESS wglGetProcAddress
39
40#elif RT_OS_DARWIN
41# include <dlfcn.h>
42# define OGLGETPROCADDRESS(x) MyNSGLGetProcAddress((const char *)x)
43void *MyNSGLGetProcAddress(const char *pszSymbol)
44{
45 /* Another copy in DevVGA-SVGA3d-ogl.cpp. */
46 static void *s_pvImage = NULL;
47 if (s_pvImage == NULL)
48 s_pvImage = dlopen("/System/Library/Frameworks/OpenGL.framework/Versions/Current/OpenGL", RTLD_LAZY);
49 return s_pvImage ? dlsym(s_pvImage, pszSymbol) : NULL;
50}
51
52#else
53extern void (*glXGetProcAddress(const GLubyte *procname))( void );
54# define OGLGETPROCADDRESS(x) glXGetProcAddress((const GLubyte *)x)
55
56#endif
57
58#undef GL_EXT_FUNCS_GEN
59#define GL_EXT_FUNCS_GEN \
60 /* GL_ARB_shader_objects */ \
61 USE_GL_FUNC(WINED3D_PFNGLGETOBJECTPARAMETERIVARBPROC, \
62 glGetObjectParameterivARB, ARB_SHADER_OBJECTS, NULL) \
63 USE_GL_FUNC(WINED3D_PFNGLGETOBJECTPARAMETERFVARBPROC, \
64 glGetObjectParameterfvARB, ARB_SHADER_OBJECTS, NULL) \
65 USE_GL_FUNC(WINED3D_PFNGLGETUNIFORMLOCATIONARBPROC, \
66 glGetUniformLocationARB, ARB_SHADER_OBJECTS, NULL) \
67 USE_GL_FUNC(WINED3D_PFNGLGETACTIVEUNIFORMARBPROC, \
68 glGetActiveUniformARB, ARB_SHADER_OBJECTS, NULL) \
69 USE_GL_FUNC(WINED3D_PFNGLUNIFORM1IARBPROC, \
70 glUniform1iARB, ARB_SHADER_OBJECTS, NULL) \
71 USE_GL_FUNC(WINED3D_PFNGLUNIFORM2IARBPROC, \
72 glUniform2iARB, ARB_SHADER_OBJECTS, NULL) \
73 USE_GL_FUNC(WINED3D_PFNGLUNIFORM3IARBPROC, \
74 glUniform3iARB, ARB_SHADER_OBJECTS, NULL) \
75 USE_GL_FUNC(WINED3D_PFNGLUNIFORM4IARBPROC, \
76 glUniform4iARB, ARB_SHADER_OBJECTS, NULL) \
77 USE_GL_FUNC(WINED3D_PFNGLUNIFORM1FARBPROC, \
78 glUniform1fARB, ARB_SHADER_OBJECTS, NULL) \
79 USE_GL_FUNC(WINED3D_PFNGLUNIFORM2FARBPROC, \
80 glUniform2fARB, ARB_SHADER_OBJECTS, NULL) \
81 USE_GL_FUNC(WINED3D_PFNGLUNIFORM3FARBPROC, \
82 glUniform3fARB, ARB_SHADER_OBJECTS, NULL) \
83 USE_GL_FUNC(WINED3D_PFNGLUNIFORM4FARBPROC, \
84 glUniform4fARB, ARB_SHADER_OBJECTS, NULL) \
85 USE_GL_FUNC(WINED3D_PFNGLUNIFORM1FVARBPROC, \
86 glUniform1fvARB, ARB_SHADER_OBJECTS, NULL) \
87 USE_GL_FUNC(WINED3D_PFNGLUNIFORM2FVARBPROC, \
88 glUniform2fvARB, ARB_SHADER_OBJECTS, NULL) \
89 USE_GL_FUNC(WINED3D_PFNGLUNIFORM3FVARBPROC, \
90 glUniform3fvARB, ARB_SHADER_OBJECTS, NULL) \
91 USE_GL_FUNC(WINED3D_PFNGLUNIFORM4FVARBPROC, \
92 glUniform4fvARB, ARB_SHADER_OBJECTS, NULL) \
93 USE_GL_FUNC(WINED3D_PFNGLUNIFORM1IVARBPROC, \
94 glUniform1ivARB, ARB_SHADER_OBJECTS, NULL) \
95 USE_GL_FUNC(WINED3D_PFNGLUNIFORM2IVARBPROC, \
96 glUniform2ivARB, ARB_SHADER_OBJECTS, NULL) \
97 USE_GL_FUNC(WINED3D_PFNGLUNIFORM3IVARBPROC, \
98 glUniform3ivARB, ARB_SHADER_OBJECTS, NULL) \
99 USE_GL_FUNC(WINED3D_PFNGLUNIFORM4IVARBPROC, \
100 glUniform4ivARB, ARB_SHADER_OBJECTS, NULL) \
101 USE_GL_FUNC(WINED3D_PFNGLUNIFORMMATRIX2FVARBPROC, \
102 glUniformMatrix2fvARB, ARB_SHADER_OBJECTS, NULL) \
103 USE_GL_FUNC(WINED3D_PFNGLUNIFORMMATRIX3FVARBPROC, \
104 glUniformMatrix3fvARB, ARB_SHADER_OBJECTS, NULL) \
105 USE_GL_FUNC(WINED3D_PFNGLUNIFORMMATRIX4FVARBPROC, \
106 glUniformMatrix4fvARB, ARB_SHADER_OBJECTS, NULL) \
107 USE_GL_FUNC(WINED3D_PFNGLGETUNIFORMFVARBPROC, \
108 glGetUniformfvARB, ARB_SHADER_OBJECTS, NULL) \
109 USE_GL_FUNC(WINED3D_PFNGLGETUNIFORMIVARBPROC, \
110 glGetUniformivARB, ARB_SHADER_OBJECTS, NULL) \
111 USE_GL_FUNC(WINED3D_PFNGLGETINFOLOGARBPROC, \
112 glGetInfoLogARB, ARB_SHADER_OBJECTS, NULL) \
113 USE_GL_FUNC(WINED3D_PFNGLUSEPROGRAMOBJECTARBPROC, \
114 glUseProgramObjectARB, ARB_SHADER_OBJECTS, NULL) \
115 USE_GL_FUNC(WINED3D_PFNGLCREATESHADEROBJECTARBPROC, \
116 glCreateShaderObjectARB, ARB_SHADER_OBJECTS, NULL) \
117 USE_GL_FUNC(WINED3D_PFNGLSHADERSOURCEARBPROC, \
118 glShaderSourceARB, ARB_SHADER_OBJECTS, NULL) \
119 USE_GL_FUNC(WINED3D_PFNGLCOMPILESHADERARBPROC, \
120 glCompileShaderARB, ARB_SHADER_OBJECTS, NULL) \
121 USE_GL_FUNC(WINED3D_PFNGLCREATEPROGRAMOBJECTARBPROC, \
122 glCreateProgramObjectARB, ARB_SHADER_OBJECTS, NULL) \
123 USE_GL_FUNC(WINED3D_PFNGLATTACHOBJECTARBPROC, \
124 glAttachObjectARB, ARB_SHADER_OBJECTS, NULL) \
125 USE_GL_FUNC(WINED3D_PFNGLLINKPROGRAMARBPROC, \
126 glLinkProgramARB, ARB_SHADER_OBJECTS, NULL) \
127 USE_GL_FUNC(WINED3D_PFNGLDETACHOBJECTARBPROC, \
128 glDetachObjectARB, ARB_SHADER_OBJECTS, NULL) \
129 USE_GL_FUNC(WINED3D_PFNGLDELETEOBJECTARBPROC, \
130 glDeleteObjectARB, ARB_SHADER_OBJECTS, NULL) \
131 USE_GL_FUNC(WINED3D_PFNGLVALIDATEPROGRAMARBPROC, \
132 glValidateProgramARB, ARB_SHADER_OBJECTS, NULL) \
133 USE_GL_FUNC(WINED3D_PFNGLGETATTACHEDOBJECTSARBPROC, \
134 glGetAttachedObjectsARB, ARB_SHADER_OBJECTS, NULL) \
135 USE_GL_FUNC(WINED3D_PFNGLGETHANDLEARBPROC, \
136 glGetHandleARB, ARB_SHADER_OBJECTS, NULL) \
137 USE_GL_FUNC(WINED3D_PFNGLGETSHADERSOURCEARBPROC, \
138 glGetShaderSourceARB, ARB_SHADER_OBJECTS, NULL) \
139 USE_GL_FUNC(WINED3D_PFNGLBINDATTRIBLOCATIONARBPROC, \
140 glBindAttribLocationARB, ARB_SHADER_OBJECTS, NULL) \
141 USE_GL_FUNC(WINED3D_PFNGLGETATTRIBLOCATIONARBPROC, \
142 glGetAttribLocationARB, ARB_SHADER_OBJECTS, NULL) \
143
144static struct wined3d_context *g_pCurrentContext = NULL;
145static struct wined3d_adapter g_adapter = {0};
146static bool g_fInitializedLibrary = false;
147
148#define SHADER_SET_CURRENT_CONTEXT(ctx) \
149 g_pCurrentContext = (struct wined3d_context *)ctx;
150
151SHADERDECL(int) ShaderInitLib(PVBOXVMSVGASHADERIF pVBoxShaderIf)
152{
153 struct wined3d_gl_info *gl_info = &g_adapter.gl_info;
154
155 /* Dynamically load all GL core functions. */
156#ifdef RT_OS_WINDOWS
157 HANDLE hOpenGl32 = GetModuleHandle("opengl32.dll");
158# define USE_GL_FUNC(pfn) *(FARPROC *)(&pfn) = GetProcAddress(hOpenGl32, #pfn);
159#else
160# define USE_GL_FUNC(pfn) pfn = (void *)OGLGETPROCADDRESS(#pfn);
161#endif
162 GL_FUNCS_GEN;
163#undef USE_GL_FUNC
164
165 /* Dynamically load all GL extension functions. */
166#define USE_GL_FUNC(type, pfn, ext, replace) \
167{ \
168 gl_info->pfn = (type)OGLGETPROCADDRESS(#pfn); \
169}
170 GL_EXT_FUNCS_GEN;
171
172 /* Fill in GL capabilities. */
173 IWineD3DImpl_FillGLCaps(&g_adapter, pVBoxShaderIf);
174
175 LogRel(("shaderlib: GL Limits:\n"));
176 LogRel(("shaderlib: buffers=%-2u lights=%-2u textures=%-2u texture_stages=%u\n",
177 gl_info->limits.buffers, gl_info->limits.lights, gl_info->limits.textures, gl_info->limits.texture_stages));
178 LogRel(("shaderlib: fragment_samplers=%-2u vertex_samplers=%-2u combined_samplers=%-3u general_combiners=%u\n",
179 gl_info->limits.fragment_samplers, gl_info->limits.vertex_samplers, gl_info->limits.combined_samplers, gl_info->limits.general_combiners));
180 LogRel(("shaderlib: sampler_stages=%-2u clipplanes=%-2u texture_size=%-5u texture3d_size=%u\n",
181 gl_info->limits.sampler_stages, gl_info->limits.clipplanes, gl_info->limits.texture_size, gl_info->limits.texture3d_size));
182 LogRel(("shaderlib: pointsize_max=%d.%d pointsize_min=%d.%d point_sprite_units=%-2u blends=%u\n",
183 (int)gl_info->limits.pointsize_max, (int)(gl_info->limits.pointsize_max * 10) % 10,
184 (int)gl_info->limits.pointsize_min, (int)(gl_info->limits.pointsize_min * 10) % 10,
185 gl_info->limits.point_sprite_units, gl_info->limits.blends));
186 LogRel(("shaderlib: anisotropy=%-2u shininess=%d.%02d\n",
187 gl_info->limits.anisotropy, (int)gl_info->limits.shininess, (int)(gl_info->limits.shininess * 100) % 100));
188 LogRel(("shaderlib: glsl_varyings=%-3u glsl_vs_float_constants=%-4u glsl_ps_float_constants=%u\n",
189 gl_info->limits.glsl_varyings, gl_info->limits.glsl_vs_float_constants, gl_info->limits.glsl_ps_float_constants));
190 LogRel(("shaderlib: arb_vs_instructions=%-4u arb_vs_native_constants=%-4u qarb_vs_float_constants=%u\n",
191 gl_info->limits.arb_vs_instructions, gl_info->limits.arb_vs_native_constants, gl_info->limits.arb_vs_float_constants));
192 LogRel(("shaderlib: arb_vs_temps=%-2u arb_ps_float_constants=%-4u arb_ps_local_constants=%u\n",
193 gl_info->limits.arb_vs_temps, gl_info->limits.arb_ps_float_constants, gl_info->limits.arb_ps_local_constants));
194 LogRel(("shaderlib: arb_ps_instructions=%-4u arb_ps_temps=%-2u arb_ps_native_constants=%u\n",
195 gl_info->limits.arb_ps_instructions, gl_info->limits.arb_ps_temps, gl_info->limits.arb_ps_native_constants));
196
197 g_fInitializedLibrary = true;
198 return VINF_SUCCESS;
199}
200
201SHADERDECL(int) ShaderDestroyLib(void)
202{
203 return VINF_SUCCESS;
204}
205
206struct IWineD3DDeviceImpl *context_get_device(const struct wined3d_context *context)
207{
208 return context->pDeviceContext;
209}
210
211struct wined3d_context *context_get_current(void)
212{
213 return g_pCurrentContext;
214}
215
216struct wined3d_context *context_acquire(IWineD3DDeviceImpl *This, IWineD3DSurface *target, enum ContextUsage usage)
217{
218 RT_NOREF(This, target, usage);
219 return g_pCurrentContext;
220}
221
222SHADERDECL(int) ShaderContextCreate(void **ppShaderContext)
223{
224 struct wined3d_context *pContext;
225 HRESULT hr;
226
227 pContext = (struct wined3d_context *)RTMemAllocZ(sizeof(struct wined3d_context));
228 AssertReturn(pContext, VERR_NO_MEMORY);
229 pContext->pDeviceContext = (IWineD3DDeviceImpl *)RTMemAllocZ(sizeof(IWineD3DDeviceImpl));
230 AssertReturn(pContext->pDeviceContext, VERR_NO_MEMORY);
231
232 pContext->gl_info = &g_adapter.gl_info;
233
234 pContext->pDeviceContext->adapter = &g_adapter;
235 pContext->pDeviceContext->shader_backend = &glsl_shader_backend;
236 pContext->pDeviceContext->ps_selected_mode = SHADER_GLSL;
237 pContext->pDeviceContext->vs_selected_mode = SHADER_GLSL;
238#ifndef VBOX_WITH_VMSVGA
239 pContext->render_offscreen = false;
240#else
241 /* VMSVGA always renders offscreen. */
242 pContext->render_offscreen = true;
243#endif
244
245 list_init(&pContext->pDeviceContext->shaders);
246
247 if (g_fInitializedLibrary)
248 {
249 struct shader_caps shader_caps;
250 uint32_t state;
251
252 /* Initialize the shader backend. */
253 hr = pContext->pDeviceContext->shader_backend->shader_alloc_private((IWineD3DDevice *)pContext->pDeviceContext);
254 AssertReturn(hr == S_OK, VERR_INTERNAL_ERROR);
255
256 memset(&shader_caps, 0, sizeof(shader_caps));
257 pContext->pDeviceContext->shader_backend->shader_get_caps(&g_adapter.gl_info, &shader_caps);
258 pContext->pDeviceContext->d3d_vshader_constantF = shader_caps.MaxVertexShaderConst;
259 pContext->pDeviceContext->d3d_pshader_constantF = shader_caps.MaxPixelShaderConst;
260 pContext->pDeviceContext->vs_clipping = shader_caps.VSClipping;
261
262 pContext->pDeviceContext->stateBlock = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*pContext->pDeviceContext->stateBlock));
263 AssertReturn(pContext->pDeviceContext->stateBlock, VERR_NO_MEMORY);
264 hr = stateblock_init(pContext->pDeviceContext->stateBlock, pContext->pDeviceContext, 0);
265 AssertReturn(hr == S_OK, VERR_INTERNAL_ERROR);
266 pContext->pDeviceContext->updateStateBlock = pContext->pDeviceContext->stateBlock;
267
268 pContext->pDeviceContext->stateBlock->vertexDecl = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(IWineD3DVertexDeclarationImpl));
269 AssertReturn(pContext->pDeviceContext->stateBlock->vertexDecl, VERR_NO_MEMORY);
270
271 /* Initialize the texture unit mapping to a 1:1 mapping */
272 for (state = 0; state < MAX_COMBINED_SAMPLERS; ++state)
273 {
274 if (state < pContext->gl_info->limits.fragment_samplers)
275 {
276 pContext->pDeviceContext->texUnitMap[state] = state;
277 pContext->pDeviceContext->rev_tex_unit_map[state] = state;
278 } else {
279 pContext->pDeviceContext->texUnitMap[state] = WINED3D_UNMAPPED_STAGE;
280 pContext->pDeviceContext->rev_tex_unit_map[state] = WINED3D_UNMAPPED_STAGE;
281 }
282 }
283 }
284
285 *ppShaderContext = (void *)pContext;
286 return VINF_SUCCESS;
287}
288
289SHADERDECL(int) ShaderContextDestroy(void *pShaderContext)
290{
291 struct wined3d_context *pContext = (struct wined3d_context *)pShaderContext;
292
293 if (pContext->pDeviceContext)
294 {
295 IWineD3DStateBlockImpl *This = pContext->pDeviceContext->stateBlock;
296
297 /* Fails during init only. */
298 if (pContext->pDeviceContext->shader_priv)
299 pContext->pDeviceContext->shader_backend->shader_free_private((IWineD3DDevice *)pContext->pDeviceContext);
300
301 if (This)
302 {
303 if (This->vertexShaderConstantF)
304 HeapFree(GetProcessHeap(), 0, This->vertexShaderConstantF);
305 if (This->changed.vertexShaderConstantsF)
306 HeapFree(GetProcessHeap(), 0, This->changed.vertexShaderConstantsF);
307 if (This->pixelShaderConstantF)
308 HeapFree(GetProcessHeap(), 0, This->pixelShaderConstantF);
309 if (This->changed.pixelShaderConstantsF)
310 HeapFree(GetProcessHeap(), 0, This->changed.pixelShaderConstantsF);
311 if (This->contained_vs_consts_f)
312 HeapFree(GetProcessHeap(), 0, This->contained_vs_consts_f);
313 if (This->contained_ps_consts_f)
314 HeapFree(GetProcessHeap(), 0, This->contained_ps_consts_f);
315 if (This->vertexDecl)
316 HeapFree(GetProcessHeap(), 0, This->vertexDecl);
317 HeapFree(GetProcessHeap(), 0, This);
318 }
319
320 RTMemFree(pContext->pDeviceContext);
321 }
322 RTMemFree(pShaderContext);
323 return VINF_SUCCESS;
324}
325
326SHADERDECL(int) ShaderCreateVertexShader(void *pShaderContext, const uint32_t *pShaderData, uint32_t cbShaderData, void **pShaderObj)
327{
328 IWineD3DDeviceImpl *This;
329 IWineD3DVertexShaderImpl *object;
330 HRESULT hr;
331
332 SHADER_SET_CURRENT_CONTEXT(pShaderContext);
333 This = g_pCurrentContext->pDeviceContext;
334
335 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
336 if (!object)
337 {
338 Log(("Failed to allocate shader memory.\n"));
339 return VERR_NO_MEMORY;
340 }
341
342 object->baseShader.functionLength = cbShaderData;
343
344 hr = vertexshader_init(object, This, (DWORD const *)pShaderData, NULL, NULL, NULL);
345 if (FAILED(hr))
346 {
347 Log(("Failed to initialize vertex shader, hr %#x.\n", hr));
348 HeapFree(GetProcessHeap(), 0, object);
349 return VERR_INTERNAL_ERROR;
350 }
351
352 /* Tweak the float constants limit to use a greater number of constants.
353 * Keep some space for the internal usage.
354 * The shader creation code artificially sets the limit according to D3D shader version.
355 * But the guest may use more constants and we are not required to strictly follow D3D specs.
356 */
357 object->baseShader.limits.constant_float = RT_MAX(g_adapter.gl_info.limits.glsl_vs_float_constants / 2,
358 object->baseShader.limits.constant_float);
359
360#ifdef VBOX_WINE_WITH_SHADER_CACHE
361 object = vertexshader_check_cached(This, object);
362#endif
363
364 Log(("Created vertex shader %p.\n", object));
365 *pShaderObj = (void *)object;
366
367 return VINF_SUCCESS;
368}
369
370SHADERDECL(int) ShaderCreatePixelShader(void *pShaderContext, const uint32_t *pShaderData, uint32_t cbShaderData, void **pShaderObj)
371{
372 IWineD3DDeviceImpl *This;
373 IWineD3DPixelShaderImpl *object;
374 HRESULT hr;
375
376 SHADER_SET_CURRENT_CONTEXT(pShaderContext);
377 This = g_pCurrentContext->pDeviceContext;
378
379 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
380 if (!object)
381 {
382 Log(("Failed to allocate shader memory.\n"));
383 return VERR_NO_MEMORY;
384 }
385
386 object->baseShader.functionLength = cbShaderData;
387
388 hr = pixelshader_init(object, This, (DWORD const *)pShaderData, NULL, NULL, NULL);
389 if (FAILED(hr))
390 {
391 Log(("Failed to initialize pixel shader, hr %#x.\n", hr));
392 HeapFree(GetProcessHeap(), 0, object);
393 return VERR_INTERNAL_ERROR;
394 }
395
396 /* Tweak the float constants limit to use a greater number of constants.
397 * Keep some space for the internal usage.
398 * The shader creation code artificially sets the limit according to D3D shader version.
399 * But the guest may use more constants and we are not required to strictly follow D3D specs.
400 */
401 object->baseShader.limits.constant_float = RT_MAX(g_adapter.gl_info.limits.glsl_ps_float_constants / 2,
402 object->baseShader.limits.constant_float);
403
404#ifdef VBOX_WINE_WITH_SHADER_CACHE
405 object = pixelshader_check_cached(This, object);
406#endif
407
408 Log(("Created pixel shader %p.\n", object));
409 *pShaderObj = (void *)object;
410 return VINF_SUCCESS;
411}
412
413SHADERDECL(int) ShaderDestroyVertexShader(void *pShaderContext, void *pShaderObj)
414{
415 IWineD3DVertexShaderImpl *object = (IWineD3DVertexShaderImpl *)pShaderObj;
416 AssertReturn(pShaderObj, VERR_INVALID_PARAMETER);
417
418 SHADER_SET_CURRENT_CONTEXT(pShaderContext);
419
420 object->lpVtbl->Release((IWineD3DVertexShader *)object);
421 return VINF_SUCCESS;
422}
423
424SHADERDECL(int) ShaderDestroyPixelShader(void *pShaderContext, void *pShaderObj)
425{
426 IWineD3DPixelShaderImpl *object = (IWineD3DPixelShaderImpl *)pShaderObj;
427 AssertReturn(pShaderObj, VERR_INVALID_PARAMETER);
428
429 SHADER_SET_CURRENT_CONTEXT(pShaderContext);
430
431 object->lpVtbl->Release((IWineD3DPixelShader *)object);
432 return VINF_SUCCESS;
433}
434
435SHADERDECL(int) ShaderSetVertexShader(void *pShaderContext, void *pShaderObj)
436{
437 IWineD3DDeviceImpl *This;
438 IWineD3DVertexShader* pShader;
439 IWineD3DVertexShader* oldShader;
440
441 SHADER_SET_CURRENT_CONTEXT(pShaderContext);
442 This = g_pCurrentContext->pDeviceContext;
443 pShader = (IWineD3DVertexShader* )pShaderObj;
444 oldShader = This->updateStateBlock->vertexShader;
445
446 if(oldShader == pShader) {
447 /* Checked here to allow proper stateblock recording */
448 Log(("App is setting the old shader over, nothing to do\n"));
449 return VINF_SUCCESS;
450 }
451
452 This->updateStateBlock->vertexShader = pShader;
453 This->updateStateBlock->changed.vertexShader = TRUE;
454
455 Log(("(%p) : setting pShader(%p)\n", This, pShader));
456 if(pShader) IWineD3DVertexShader_AddRef(pShader);
457 if(oldShader) IWineD3DVertexShader_Release(oldShader);
458
459 g_pCurrentContext->fChangedVertexShader = true;
460 g_pCurrentContext->fChangedVertexShaderConstant = true; /* force constant reload. */
461
462 return VINF_SUCCESS;
463}
464
465SHADERDECL(int) ShaderSetPixelShader(void *pShaderContext, void *pShaderObj)
466{
467 IWineD3DDeviceImpl *This;
468 IWineD3DPixelShader* pShader;
469 IWineD3DPixelShader* oldShader;
470
471 SHADER_SET_CURRENT_CONTEXT(pShaderContext);
472 This = g_pCurrentContext->pDeviceContext;
473 pShader = (IWineD3DPixelShader* )pShaderObj;
474 oldShader = This->updateStateBlock->pixelShader;
475
476 if(oldShader == pShader) {
477 /* Checked here to allow proper stateblock recording */
478 Log(("App is setting the old shader over, nothing to do\n"));
479 return VINF_SUCCESS;
480 }
481
482 This->updateStateBlock->pixelShader = pShader;
483 This->updateStateBlock->changed.pixelShader = TRUE;
484
485 Log(("(%p) : setting pShader(%p)\n", This, pShader));
486 if(pShader) IWineD3DPixelShader_AddRef(pShader);
487 if(oldShader) IWineD3DPixelShader_Release(oldShader);
488
489 g_pCurrentContext->fChangedPixelShader = true;
490 g_pCurrentContext->fChangedPixelShaderConstant = true; /* force constant reload. */
491 return VINF_SUCCESS;
492}
493
494SHADERDECL(int) ShaderSetVertexShaderConstantB(void *pShaderContext, uint32_t start, const uint8_t *srcData, uint32_t count)
495{
496 IWineD3DDeviceImpl *This;
497 unsigned int i, cnt = min(count, MAX_CONST_B - start);
498
499 SHADER_SET_CURRENT_CONTEXT(pShaderContext);
500 This = g_pCurrentContext->pDeviceContext;
501
502 Log(("(ShaderSetVertexShaderConstantB %p, srcData %p, start %d, count %d)\n", pShaderContext, srcData, start, count));
503
504 if (!srcData || start >= MAX_CONST_B)
505 {
506 Log(("incorrect vertex shader const data: start(%u), srcData(0x%p), count(%u)", start, srcData, count));
507 return VERR_INVALID_PARAMETER;
508 }
509
510 memcpy(&This->updateStateBlock->vertexShaderConstantB[start], srcData, cnt * sizeof(BOOL));
511 for (i = 0; i < cnt; i++)
512 Log(("Set BOOL constant %u to %s\n", start + i, srcData[i]? "true":"false"));
513
514 for (i = start; i < cnt + start; ++i) {
515 This->updateStateBlock->changed.vertexShaderConstantsB |= (1 << i);
516 }
517
518 g_pCurrentContext->fChangedVertexShaderConstant = true;
519
520 return VINF_SUCCESS;
521}
522
523SHADERDECL(int) ShaderSetVertexShaderConstantI(void *pShaderContext, uint32_t start, const int32_t *srcData, uint32_t count)
524{
525 IWineD3DDeviceImpl *This;
526 unsigned int i, cnt = min(count, MAX_CONST_I - start);
527
528 SHADER_SET_CURRENT_CONTEXT(pShaderContext);
529 This = g_pCurrentContext->pDeviceContext;
530
531 Log(("(ShaderSetVertexShaderConstantI %p, srcData %p, start %d, count %d)\n", pShaderContext, srcData, start, count));
532
533 if (!srcData || start >= MAX_CONST_I)
534 {
535 Log(("incorrect vertex shader const data: start(%u), srcData(0x%p), count(%u)", start, srcData, count));
536 return VERR_INVALID_PARAMETER;
537 }
538
539 memcpy(&This->updateStateBlock->vertexShaderConstantI[start * 4], srcData, cnt * sizeof(int32_t) * 4);
540
541 for (i = start; i < cnt + start; ++i) {
542 This->updateStateBlock->changed.vertexShaderConstantsI |= (1 << i);
543 }
544
545 g_pCurrentContext->fChangedVertexShaderConstant = true;
546
547 return VINF_SUCCESS;
548}
549
550SHADERDECL(int) ShaderSetVertexShaderConstantF(void *pShaderContext, uint32_t start, const float *srcData, uint32_t count)
551{
552 IWineD3DDeviceImpl *This;
553
554 SHADER_SET_CURRENT_CONTEXT(pShaderContext);
555 This = g_pCurrentContext->pDeviceContext;
556
557 Log(("(ShaderSetVertexShaderConstantF %p, srcData %p, start %d, count %d)\n", pShaderContext, srcData, start, count));
558
559 if (srcData == NULL || start + count > This->d3d_vshader_constantF || start > This->d3d_vshader_constantF)
560 {
561 Log(("incorrect vertex shader const data: start(%u), srcData(0x%p), count(%u)", start, srcData, count));
562 return VERR_INVALID_PARAMETER;
563 }
564 memcpy(&This->updateStateBlock->vertexShaderConstantF[start * 4], srcData, count * sizeof(float) * 4);
565
566 This->shader_backend->shader_update_float_vertex_constants((IWineD3DDevice *)This, start, count);
567
568 memset(This->updateStateBlock->changed.vertexShaderConstantsF + start, 1,
569 sizeof(*This->updateStateBlock->changed.vertexShaderConstantsF) * count);
570
571 g_pCurrentContext->fChangedVertexShaderConstant = true;
572
573 return VINF_SUCCESS;
574}
575
576SHADERDECL(int) ShaderSetPixelShaderConstantB(void *pShaderContext, uint32_t start, const uint8_t *srcData, uint32_t count)
577{
578 IWineD3DDeviceImpl *This;
579 unsigned int i, cnt = min(count, MAX_CONST_B - start);
580
581 SHADER_SET_CURRENT_CONTEXT(pShaderContext);
582 This = g_pCurrentContext->pDeviceContext;
583
584 Log(("(ShaderSetPixelShaderConstantB %p, srcData %p, start %d, count %d)\n", pShaderContext, srcData, start, count));
585
586 if (!srcData || start >= MAX_CONST_B)
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->pixelShaderConstantB[start], srcData, cnt * sizeof(BOOL));
593 for (i = 0; i < cnt; i++)
594 Log(("Set BOOL constant %u to %s\n", start + i, srcData[i]? "true":"false"));
595
596 for (i = start; i < cnt + start; ++i) {
597 This->updateStateBlock->changed.pixelShaderConstantsB |= (1 << i);
598 }
599
600 g_pCurrentContext->fChangedPixelShaderConstant = true;
601
602 return VINF_SUCCESS;
603}
604
605SHADERDECL(int) ShaderSetPixelShaderConstantI(void *pShaderContext, uint32_t start, const int32_t *srcData, uint32_t count)
606{
607 IWineD3DDeviceImpl *This;
608 unsigned int i, cnt = min(count, MAX_CONST_I - start);
609
610 SHADER_SET_CURRENT_CONTEXT(pShaderContext);
611 This = g_pCurrentContext->pDeviceContext;
612
613 Log(("(ShaderSetPixelShaderConstantI %p, srcData %p, start %d, count %d)\n", pShaderContext, srcData, start, count));
614
615 if (!srcData || start >= MAX_CONST_I)
616 {
617 Log(("incorrect pixel shader const data: start(%u), srcData(0x%p), count(%u)", start, srcData, count));
618 return VERR_INVALID_PARAMETER;
619 }
620
621 memcpy(&This->updateStateBlock->pixelShaderConstantI[start * 4], srcData, cnt * sizeof(int32_t) * 4);
622
623 for (i = start; i < cnt + start; ++i) {
624 This->updateStateBlock->changed.pixelShaderConstantsI |= (1 << i);
625 }
626
627 g_pCurrentContext->fChangedPixelShaderConstant = true;
628
629 return VINF_SUCCESS;
630}
631
632SHADERDECL(int) ShaderSetPixelShaderConstantF(void *pShaderContext, uint32_t start, const float *srcData, uint32_t count)
633{
634 IWineD3DDeviceImpl *This;
635
636 SHADER_SET_CURRENT_CONTEXT(pShaderContext);
637 This = g_pCurrentContext->pDeviceContext;
638
639 Log(("(ShaderSetPixelShaderConstantF %p, srcData %p, start %d, count %d)\n", pShaderContext, srcData, start, count));
640
641 if (srcData == NULL || start + count > This->d3d_pshader_constantF || start > This->d3d_pshader_constantF)
642 {
643 Log(("incorrect pixel shader const data: start(%u), srcData(0x%p), count(%u)", start, srcData, count));
644 return VERR_INVALID_PARAMETER;
645 }
646
647 memcpy(&This->updateStateBlock->pixelShaderConstantF[start * 4], srcData, count * sizeof(float) * 4);
648
649 This->shader_backend->shader_update_float_pixel_constants((IWineD3DDevice *)This, start, count);
650
651 memset(This->updateStateBlock->changed.pixelShaderConstantsF + start, 1,
652 sizeof(*This->updateStateBlock->changed.pixelShaderConstantsF) * count);
653
654 g_pCurrentContext->fChangedPixelShaderConstant = true;
655
656 return VINF_SUCCESS;
657}
658
659SHADERDECL(int) ShaderSetPositionTransformed(void *pShaderContext, unsigned cxViewPort, unsigned cyViewPort, bool fPreTransformed)
660{
661 IWineD3DDeviceImpl *This;
662 int rc;
663
664 SHADER_SET_CURRENT_CONTEXT(pShaderContext);
665 This = g_pCurrentContext->pDeviceContext;
666
667 if (This->strided_streams.position_transformed == fPreTransformed)
668 return VINF_SUCCESS; /* no changes; nothing to do. */
669
670 Log(("ShaderSetPositionTransformed viewport (%d,%d) fPreTransformed=%d\n", cxViewPort, cyViewPort, fPreTransformed));
671
672 if (fPreTransformed)
673 { /* In the pre-transformed vertex coordinate case we need to disable all transformations as we're already using screen coordinates. */
674 /* Load the identity matrix for the model view */
675 glMatrixMode(GL_MODELVIEW);
676 glLoadIdentity();
677
678 /* Reset the projection matrix too */
679 rc = ShaderTransformProjection(cxViewPort, cyViewPort, NULL, fPreTransformed);
680 AssertRCReturn(rc, rc);
681 }
682
683 This->strided_streams.position_transformed = fPreTransformed;
684 ((IWineD3DVertexDeclarationImpl *)(This->stateBlock->vertexDecl))->position_transformed = fPreTransformed;
685 return VINF_SUCCESS;
686}
687
688SHADERDECL(int) ShaderUpdateState(void *pShaderContext, uint32_t rtHeight)
689{
690 IWineD3DDeviceImpl *pThis;
691 GLfloat yoffset;
692 GLint viewport[4];
693
694 SHADER_SET_CURRENT_CONTEXT(pShaderContext);
695 pThis = g_pCurrentContext->pDeviceContext;
696
697 glGetIntegerv(GL_VIEWPORT, viewport);
698#ifdef DEBUG
699 AssertReturn(glGetError() == GL_NO_ERROR, VERR_INTERNAL_ERROR);
700#endif
701
702 yoffset = -(63.0f / 64.0f) / viewport[3] /* height */;
703 pThis->posFixup[0] = 1.0f; /* This is needed to get the x coord unmodified through a MAD. */
704 pThis->posFixup[1] = -1.0f; /* y-inversion */
705 pThis->posFixup[2] = (63.0f / 64.0f) / viewport[2] /* width */;
706 pThis->posFixup[3] = pThis->posFixup[1] * yoffset;
707
708 pThis->rtHeight = rtHeight;
709
710 /** @todo missing state:
711 * - fog enable (stateblock->renderState[WINED3DRS_FOGENABLE])
712 * - fog mode (stateblock->renderState[WINED3DRS_FOGTABLEMODE])
713 * - stateblock->vertexDecl->position_transformed
714 */
715
716 if ( g_pCurrentContext->fChangedPixelShader
717 || g_pCurrentContext->fChangedVertexShader)
718 pThis->shader_backend->shader_select(g_pCurrentContext, !!pThis->updateStateBlock->pixelShader, !!pThis->updateStateBlock->vertexShader);
719 g_pCurrentContext->fChangedPixelShader = g_pCurrentContext->fChangedVertexShader = false;
720
721 if ( g_pCurrentContext->fChangedPixelShaderConstant
722 || g_pCurrentContext->fChangedVertexShaderConstant)
723 pThis->shader_backend->shader_load_constants(g_pCurrentContext, !!pThis->updateStateBlock->pixelShader, !!pThis->updateStateBlock->vertexShader);
724 g_pCurrentContext->fChangedPixelShaderConstant = false;
725 g_pCurrentContext->fChangedVertexShaderConstant = false;
726
727 return VINF_SUCCESS;
728}
729
730SHADERDECL(int) ShaderTransformProjection(unsigned cxViewPort, unsigned cyViewPort, float matrix[16], bool fPretransformed)
731{
732#ifdef DEBUG
733 GLenum lastError;
734#endif
735 GLfloat xoffset, yoffset;
736
737 /* Assumes OpenGL context has been activated. */
738 glMatrixMode(GL_PROJECTION);
739 glLoadIdentity();
740
741 /* The rule is that the window coordinate 0 does not correspond to the
742 beginning of the first pixel, but the center of the first pixel.
743 As a consequence if you want to correctly draw one line exactly from
744 the left to the right end of the viewport (with all matrices set to
745 be identity), the x coords of both ends of the line would be not
746 -1 and 1 respectively but (-1-1/viewport_widh) and (1-1/viewport_width)
747 instead.
748
749 1.0 / Width is used because the coord range goes from -1.0 to 1.0, then we
750 divide by the Width/Height, so we need the half range(1.0) to translate by
751 half a pixel.
752
753 The other fun is that d3d's output z range after the transformation is [0;1],
754 but opengl's is [-1;1]. Since the z buffer is in range [0;1] for both, gl
755 scales [-1;1] to [0;1]. This would mean that we end up in [0.5;1] and loose a lot
756 of Z buffer precision and the clear values do not match in the z test. Thus scale
757 [0;1] to [-1;1], so when gl undoes that we utilize the full z range
758 */
759
760 /*
761 * Careful with the order of operations here, we're essentially working backwards:
762 * x = x + 1/w;
763 * y = (y - 1/h) * flip;
764 * z = z * 2 - 1;
765 *
766 * Becomes:
767 * glTranslatef(0.0, 0.0, -1.0);
768 * glScalef(1.0, 1.0, 2.0);
769 *
770 * glScalef(1.0, flip, 1.0);
771 * glTranslatef(1/w, -1/h, 0.0);
772 *
773 * This is equivalent to:
774 * glTranslatef(1/w, -flip/h, -1.0)
775 * glScalef(1.0, flip, 2.0);
776 */
777 /* Translate by slightly less than a half pixel to force a top-left
778 * filling convention. We want the difference to be large enough that
779 * it doesn't get lost due to rounding inside the driver, but small
780 * enough to prevent it from interfering with any anti-aliasing. */
781 xoffset = (63.0f / 64.0f) / cxViewPort;
782 yoffset = -(63.0f / 64.0f) / cyViewPort;
783
784 glTranslatef(xoffset, -yoffset, -1.0f);
785
786 if (fPretransformed)
787 {
788 /* One world coordinate equals one screen pixel; y-inversion no longer an issue */
789 glOrtho(0, cxViewPort, 0, cyViewPort, -1, 1);
790 }
791 else
792 {
793 /* flip y coordinate origin too */
794 glScalef(1.0f, -1.0f, 2.0f);
795
796 /* Apply the supplied projection matrix */
797 glMultMatrixf(matrix);
798 }
799#ifdef DEBUG
800 lastError = glGetError(); \
801 AssertMsgReturn(lastError == GL_NO_ERROR, ("%s (%d): last error 0x%x\n", __FUNCTION__, __LINE__, lastError), VERR_INTERNAL_ERROR);
802#endif
803 return VINF_SUCCESS;
804}
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette