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