VirtualBox

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

Last change on this file since 59010 was 53755, checked in by vboxsync, 10 years ago

VMSVGA3d: Cleaning up some of the dual-profile mess, switched back to using the 2.1 profile by default.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 30.4 KB
Line 
1/* $Id: shaderapi.c 53755 2015-01-06 21:06:53Z vboxsync $ */
2/** @file
3 * shaderlib -- interface to WINE's Direct3D shader functions
4 */
5
6/*
7 * Copyright (C) 2014-2015 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/err.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)
33void *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
43extern 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
134static struct wined3d_context *g_pCurrentContext = NULL;
135static struct wined3d_adapter g_adapter = {0};
136static bool g_fInitializedLibrary = false;
137
138#define SHADER_SET_CURRENT_CONTEXT(ctx) \
139 g_pCurrentContext = (struct wined3d_context *)ctx;
140
141SHADERDECL(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# define USE_GL_FUNC(pfn) pfn = (void*)GetProcAddress(GetModuleHandle("opengl32.dll"), #pfn);
148#else
149# define USE_GL_FUNC(pfn) pfn = (void*)OGLGETPROCADDRESS(#pfn);
150#endif
151 GL_FUNCS_GEN;
152#undef USE_GL_FUNC
153
154 /* Dynamically load all GL extension functions. */
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 /* Fill in GL capabilities. */
162 IWineD3DImpl_FillGLCaps(&g_adapter, pVBoxShaderIf);
163
164 LogRel(("shaderlib: GL Limits:\n"));
165 LogRel(("shaderlib: buffers=%-2u lights=%-2u textures=%-2u texture_stages=%u\n",
166 gl_info->limits.buffers, gl_info->limits.lights, gl_info->limits.textures, gl_info->limits.texture_stages));
167 LogRel(("shaderlib: fragment_samplers=%-2u vertex_samplers=%-2u combined_samplers=%-3u general_combiners=%u\n",
168 gl_info->limits.fragment_samplers, gl_info->limits.vertex_samplers, gl_info->limits.combined_samplers, gl_info->limits.general_combiners));
169 LogRel(("shaderlib: sampler_stages=%-2u clipplanes=%-2u texture_size=%-5u texture3d_size=%u\n",
170 gl_info->limits.sampler_stages, gl_info->limits.clipplanes, gl_info->limits.texture_size, gl_info->limits.texture3d_size));
171 LogRel(("shaderlib: pointsize_max=%d.%d pointsize_min=%d.%d point_sprite_units=%-2u blends=%u\n",
172 (int)gl_info->limits.pointsize_max, (int)(gl_info->limits.pointsize_max * 10) % 10,
173 (int)gl_info->limits.pointsize_min, (int)(gl_info->limits.pointsize_min * 10) % 10,
174 gl_info->limits.point_sprite_units, gl_info->limits.blends));
175 LogRel(("shaderlib: anisotropy=%-2u shininess=%d.%02d\n",
176 gl_info->limits.anisotropy, (int)gl_info->limits.shininess, (int)(gl_info->limits.shininess * 100) % 100));
177 LogRel(("shaderlib: glsl_varyings=%-3u glsl_vs_float_constants=%-4u glsl_ps_float_constants=%u\n",
178 gl_info->limits.glsl_varyings, gl_info->limits.glsl_vs_float_constants, gl_info->limits.glsl_ps_float_constants));
179 LogRel(("shaderlib: arb_vs_instructions=%-4u arb_vs_native_constants=%-4u qarb_vs_float_constants=%u\n",
180 gl_info->limits.arb_vs_instructions, gl_info->limits.arb_vs_native_constants, gl_info->limits.arb_vs_float_constants));
181 LogRel(("shaderlib: arb_vs_temps=%-2u arb_ps_float_constants=%-4u arb_ps_local_constants=%u\n",
182 gl_info->limits.arb_vs_temps, gl_info->limits.arb_ps_float_constants, gl_info->limits.arb_ps_local_constants));
183 LogRel(("shaderlib: arb_ps_instructions=%-4u arb_ps_temps=%-2u arb_ps_native_constants=%u\n",
184 gl_info->limits.arb_ps_instructions, gl_info->limits.arb_ps_temps, gl_info->limits.arb_ps_native_constants));
185
186 g_fInitializedLibrary = true;
187 return VINF_SUCCESS;
188}
189
190SHADERDECL(int) ShaderDestroyLib(void)
191{
192 return VINF_SUCCESS;
193}
194
195struct IWineD3DDeviceImpl *context_get_device(const struct wined3d_context *context)
196{
197 return context->pDeviceContext;
198}
199
200struct wined3d_context *context_get_current(void)
201{
202 return g_pCurrentContext;
203}
204
205struct wined3d_context *context_acquire(IWineD3DDeviceImpl *This, IWineD3DSurface *target, enum ContextUsage usage)
206{
207 return g_pCurrentContext;
208}
209
210SHADERDECL(int) ShaderContextCreate(void **ppShaderContext)
211{
212 struct wined3d_context *pContext;
213 HRESULT hr;
214
215 pContext = (struct wined3d_context *)RTMemAllocZ(sizeof(struct wined3d_context));
216 AssertReturn(pContext, VERR_NO_MEMORY);
217 pContext->pDeviceContext = (IWineD3DDeviceImpl *)RTMemAllocZ(sizeof(IWineD3DDeviceImpl));
218 AssertReturn(pContext->pDeviceContext, VERR_NO_MEMORY);
219
220 pContext->gl_info = &g_adapter.gl_info;
221
222 pContext->pDeviceContext->adapter = &g_adapter;
223 pContext->pDeviceContext->shader_backend = &glsl_shader_backend;
224 pContext->pDeviceContext->ps_selected_mode = SHADER_GLSL;
225 pContext->pDeviceContext->vs_selected_mode = SHADER_GLSL;
226 pContext->render_offscreen = false;
227
228 list_init(&pContext->pDeviceContext->shaders);
229
230 if (g_fInitializedLibrary)
231 {
232 struct shader_caps shader_caps;
233 uint32_t state;
234
235 /* Initialize the shader backend. */
236 hr = pContext->pDeviceContext->shader_backend->shader_alloc_private((IWineD3DDevice *)pContext->pDeviceContext);
237 AssertReturn(hr == S_OK, VERR_INTERNAL_ERROR);
238
239 memset(&shader_caps, 0, sizeof(shader_caps));
240 pContext->pDeviceContext->shader_backend->shader_get_caps(&g_adapter.gl_info, &shader_caps);
241 pContext->pDeviceContext->d3d_vshader_constantF = shader_caps.MaxVertexShaderConst;
242 pContext->pDeviceContext->d3d_pshader_constantF = shader_caps.MaxPixelShaderConst;
243 pContext->pDeviceContext->vs_clipping = shader_caps.VSClipping;
244
245 pContext->pDeviceContext->stateBlock = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*pContext->pDeviceContext->stateBlock));
246 AssertReturn(pContext->pDeviceContext->stateBlock, VERR_NO_MEMORY);
247 hr = stateblock_init(pContext->pDeviceContext->stateBlock, pContext->pDeviceContext, 0);
248 AssertReturn(hr == S_OK, VERR_INTERNAL_ERROR);
249 pContext->pDeviceContext->updateStateBlock = pContext->pDeviceContext->stateBlock;
250
251 pContext->pDeviceContext->stateBlock->vertexDecl = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(IWineD3DVertexDeclarationImpl));
252 AssertReturn(pContext->pDeviceContext->stateBlock->vertexDecl, VERR_NO_MEMORY);
253
254 /* Initialize the texture unit mapping to a 1:1 mapping */
255 for (state = 0; state < MAX_COMBINED_SAMPLERS; ++state)
256 {
257 if (state < pContext->gl_info->limits.fragment_samplers)
258 {
259 pContext->pDeviceContext->texUnitMap[state] = state;
260 pContext->pDeviceContext->rev_tex_unit_map[state] = state;
261 } else {
262 pContext->pDeviceContext->texUnitMap[state] = WINED3D_UNMAPPED_STAGE;
263 pContext->pDeviceContext->rev_tex_unit_map[state] = WINED3D_UNMAPPED_STAGE;
264 }
265 }
266 }
267
268 *ppShaderContext = (void *)pContext;
269 return VINF_SUCCESS;
270}
271
272SHADERDECL(int) ShaderContextDestroy(void *pShaderContext)
273{
274 struct wined3d_context *pContext = (struct wined3d_context *)pShaderContext;
275
276 if (pContext->pDeviceContext)
277 {
278 IWineD3DStateBlockImpl *This = pContext->pDeviceContext->stateBlock;
279
280 /* Fails during init only. */
281 if (pContext->pDeviceContext->shader_priv)
282 pContext->pDeviceContext->shader_backend->shader_free_private((IWineD3DDevice *)pContext->pDeviceContext);
283
284 if (This)
285 {
286 if (This->vertexShaderConstantF)
287 HeapFree(GetProcessHeap(), 0, This->vertexShaderConstantF);
288 if (This->changed.vertexShaderConstantsF)
289 HeapFree(GetProcessHeap(), 0, This->changed.vertexShaderConstantsF);
290 if (This->pixelShaderConstantF)
291 HeapFree(GetProcessHeap(), 0, This->pixelShaderConstantF);
292 if (This->changed.pixelShaderConstantsF)
293 HeapFree(GetProcessHeap(), 0, This->changed.pixelShaderConstantsF);
294 if (This->contained_vs_consts_f)
295 HeapFree(GetProcessHeap(), 0, This->contained_vs_consts_f);
296 if (This->contained_ps_consts_f)
297 HeapFree(GetProcessHeap(), 0, This->contained_ps_consts_f);
298 if (This->vertexDecl)
299 HeapFree(GetProcessHeap(), 0, This->vertexDecl);
300 HeapFree(GetProcessHeap(), 0, This);
301 }
302
303 RTMemFree(pContext->pDeviceContext);
304 }
305 RTMemFree(pShaderContext);
306 return VINF_SUCCESS;
307}
308
309SHADERDECL(int) ShaderCreateVertexShader(void *pShaderContext, const uint32_t *pShaderData, void **pShaderObj)
310{
311 IWineD3DDeviceImpl *This;
312 IWineD3DVertexShaderImpl *object;
313 HRESULT hr;
314
315 SHADER_SET_CURRENT_CONTEXT(pShaderContext);
316 This = g_pCurrentContext->pDeviceContext;
317
318 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
319 if (!object)
320 {
321 Log(("Failed to allocate shader memory.\n"));
322 return VERR_NO_MEMORY;
323 }
324
325 hr = vertexshader_init(object, This, pShaderData, NULL, NULL, NULL);
326 if (FAILED(hr))
327 {
328 Log(("Failed to initialize vertex shader, hr %#x.\n", hr));
329 HeapFree(GetProcessHeap(), 0, object);
330 return VERR_INTERNAL_ERROR;
331 }
332
333#ifdef VBOX_WINE_WITH_SHADER_CACHE
334 object = vertexshader_check_cached(This, object);
335#endif
336
337 Log(("Created vertex shader %p.\n", object));
338 *pShaderObj = (void *)object;
339
340 return VINF_SUCCESS;
341}
342
343SHADERDECL(int) ShaderCreatePixelShader(void *pShaderContext, const uint32_t *pShaderData, void **pShaderObj)
344{
345 IWineD3DDeviceImpl *This;
346 IWineD3DPixelShaderImpl *object;
347 HRESULT hr;
348
349 SHADER_SET_CURRENT_CONTEXT(pShaderContext);
350 This = g_pCurrentContext->pDeviceContext;
351
352 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
353 if (!object)
354 {
355 Log(("Failed to allocate shader memory.\n"));
356 return VERR_NO_MEMORY;
357 }
358
359 hr = pixelshader_init(object, This, pShaderData, NULL, NULL, NULL);
360 if (FAILED(hr))
361 {
362 Log(("Failed to initialize pixel shader, hr %#x.\n", hr));
363 HeapFree(GetProcessHeap(), 0, object);
364 return VERR_INTERNAL_ERROR;
365 }
366
367#ifdef VBOX_WINE_WITH_SHADER_CACHE
368 object = pixelshader_check_cached(This, object);
369#endif
370
371 Log(("Created pixel shader %p.\n", object));
372 *pShaderObj = (void *)object;
373 return VINF_SUCCESS;
374}
375
376SHADERDECL(int) ShaderDestroyVertexShader(void *pShaderContext, void *pShaderObj)
377{
378 IWineD3DVertexShaderImpl *object = (IWineD3DVertexShaderImpl *)pShaderObj;
379 AssertReturn(pShaderObj, VERR_INVALID_PARAMETER);
380
381 SHADER_SET_CURRENT_CONTEXT(pShaderContext);
382
383 object->lpVtbl->Release((IWineD3DVertexShader *)object);
384 return VINF_SUCCESS;
385}
386
387SHADERDECL(int) ShaderDestroyPixelShader(void *pShaderContext, void *pShaderObj)
388{
389 IWineD3DPixelShaderImpl *object = (IWineD3DPixelShaderImpl *)pShaderObj;
390 AssertReturn(pShaderObj, VERR_INVALID_PARAMETER);
391
392 SHADER_SET_CURRENT_CONTEXT(pShaderContext);
393
394 object->lpVtbl->Release((IWineD3DPixelShader *)object);
395 return VINF_SUCCESS;
396}
397
398SHADERDECL(int) ShaderSetVertexShader(void *pShaderContext, void *pShaderObj)
399{
400 IWineD3DDeviceImpl *This;
401 IWineD3DVertexShader* pShader;
402 IWineD3DVertexShader* oldShader;
403
404 SHADER_SET_CURRENT_CONTEXT(pShaderContext);
405 This = g_pCurrentContext->pDeviceContext;
406 pShader = (IWineD3DVertexShader* )pShaderObj;
407 oldShader = This->updateStateBlock->vertexShader;
408
409 if(oldShader == pShader) {
410 /* Checked here to allow proper stateblock recording */
411 Log(("App is setting the old shader over, nothing to do\n"));
412 return VINF_SUCCESS;
413 }
414
415 This->updateStateBlock->vertexShader = pShader;
416 This->updateStateBlock->changed.vertexShader = TRUE;
417
418 Log(("(%p) : setting pShader(%p)\n", This, pShader));
419 if(pShader) IWineD3DVertexShader_AddRef(pShader);
420 if(oldShader) IWineD3DVertexShader_Release(oldShader);
421
422 g_pCurrentContext->fChangedVertexShader = true;
423 g_pCurrentContext->fChangedVertexShaderConstant = true; /* force constant reload. */
424
425 return VINF_SUCCESS;
426}
427
428SHADERDECL(int) ShaderSetPixelShader(void *pShaderContext, void *pShaderObj)
429{
430 IWineD3DDeviceImpl *This;
431 IWineD3DPixelShader* pShader;
432 IWineD3DPixelShader* oldShader;
433
434 SHADER_SET_CURRENT_CONTEXT(pShaderContext);
435 This = g_pCurrentContext->pDeviceContext;
436 pShader = (IWineD3DPixelShader* )pShaderObj;
437 oldShader = This->updateStateBlock->pixelShader;
438
439 if(oldShader == pShader) {
440 /* Checked here to allow proper stateblock recording */
441 Log(("App is setting the old shader over, nothing to do\n"));
442 return VINF_SUCCESS;
443 }
444
445 This->updateStateBlock->pixelShader = pShader;
446 This->updateStateBlock->changed.pixelShader = TRUE;
447
448 Log(("(%p) : setting pShader(%p)\n", This, pShader));
449 if(pShader) IWineD3DPixelShader_AddRef(pShader);
450 if(oldShader) IWineD3DPixelShader_Release(oldShader);
451
452 g_pCurrentContext->fChangedPixelShader = true;
453 g_pCurrentContext->fChangedPixelShaderConstant = true; /* force constant reload. */
454 return VINF_SUCCESS;
455}
456
457SHADERDECL(int) ShaderSetVertexShaderConstantB(void *pShaderContext, uint32_t start, const uint8_t *srcData, uint32_t count)
458{
459 IWineD3DDeviceImpl *This;
460 unsigned int i, cnt = min(count, MAX_CONST_B - start);
461
462 SHADER_SET_CURRENT_CONTEXT(pShaderContext);
463 This = g_pCurrentContext->pDeviceContext;
464
465 Log(("(ShaderSetVertexShaderConstantB %p, srcData %p, start %d, count %d)\n",
466 srcData, start, count));
467
468 if (!srcData || start >= MAX_CONST_B)
469 {
470 Log(("incorrect vertex shader const data: start(%u), srcData(0x%p), count(%u)", start, srcData, count));
471 return VERR_INVALID_PARAMETER;
472 }
473
474 memcpy(&This->updateStateBlock->vertexShaderConstantB[start], srcData, cnt * sizeof(BOOL));
475 for (i = 0; i < cnt; i++)
476 Log(("Set BOOL constant %u to %s\n", start + i, srcData[i]? "true":"false"));
477
478 for (i = start; i < cnt + start; ++i) {
479 This->updateStateBlock->changed.vertexShaderConstantsB |= (1 << i);
480 }
481
482 g_pCurrentContext->fChangedVertexShaderConstant = true;
483
484 return VINF_SUCCESS;
485}
486
487SHADERDECL(int) ShaderSetVertexShaderConstantI(void *pShaderContext, uint32_t start, const int32_t *srcData, uint32_t count)
488{
489 IWineD3DDeviceImpl *This;
490 unsigned int i, cnt = min(count, MAX_CONST_I - start);
491
492 SHADER_SET_CURRENT_CONTEXT(pShaderContext);
493 This = g_pCurrentContext->pDeviceContext;
494
495 Log(("(ShaderSetVertexShaderConstantI %p, srcData %p, start %d, count %d)\n",
496 srcData, start, count));
497
498 if (!srcData || start >= MAX_CONST_I)
499 {
500 Log(("incorrect vertex shader const data: start(%u), srcData(0x%p), count(%u)", start, srcData, count));
501 return VERR_INVALID_PARAMETER;
502 }
503
504 memcpy(&This->updateStateBlock->vertexShaderConstantI[start * 4], srcData, cnt * sizeof(int32_t) * 4);
505
506 for (i = start; i < cnt + start; ++i) {
507 This->updateStateBlock->changed.vertexShaderConstantsI |= (1 << i);
508 }
509
510 g_pCurrentContext->fChangedVertexShaderConstant = true;
511
512 return VINF_SUCCESS;
513}
514
515SHADERDECL(int) ShaderSetVertexShaderConstantF(void *pShaderContext, uint32_t start, const float *srcData, uint32_t count)
516{
517 IWineD3DDeviceImpl *This;
518
519 SHADER_SET_CURRENT_CONTEXT(pShaderContext);
520 This = g_pCurrentContext->pDeviceContext;
521
522 Log(("(ShaderSetVertexShaderConstantF %p, srcData %p, start %d, count %d)\n",
523 srcData, start, count));
524
525 if (srcData == NULL || start + count > This->d3d_vshader_constantF || start > This->d3d_vshader_constantF)
526 {
527 Log(("incorrect vertex shader const data: start(%u), srcData(0x%p), count(%u)", start, srcData, count));
528 return VERR_INVALID_PARAMETER;
529 }
530 memcpy(&This->updateStateBlock->vertexShaderConstantF[start * 4], srcData, count * sizeof(float) * 4);
531
532 This->shader_backend->shader_update_float_vertex_constants((IWineD3DDevice *)This, start, count);
533
534 memset(This->updateStateBlock->changed.vertexShaderConstantsF + start, 1,
535 sizeof(*This->updateStateBlock->changed.vertexShaderConstantsF) * count);
536
537 g_pCurrentContext->fChangedVertexShaderConstant = true;
538
539 return VINF_SUCCESS;
540}
541
542SHADERDECL(int) ShaderSetPixelShaderConstantB(void *pShaderContext, uint32_t start, const uint8_t *srcData, uint32_t count)
543{
544 IWineD3DDeviceImpl *This;
545 unsigned int i, cnt = min(count, MAX_CONST_B - start);
546
547 SHADER_SET_CURRENT_CONTEXT(pShaderContext);
548 This = g_pCurrentContext->pDeviceContext;
549
550 Log(("(ShaderSetPixelShaderConstantB %p, srcData %p, start %d, count %d)\n",
551 srcData, start, count));
552
553 if (!srcData || start >= MAX_CONST_B)
554 {
555 Log(("incorrect pixel shader const data: start(%u), srcData(0x%p), count(%u)", start, srcData, count));
556 return VERR_INVALID_PARAMETER;
557 }
558
559 memcpy(&This->updateStateBlock->pixelShaderConstantB[start], srcData, cnt * sizeof(BOOL));
560 for (i = 0; i < cnt; i++)
561 Log(("Set BOOL constant %u to %s\n", start + i, srcData[i]? "true":"false"));
562
563 for (i = start; i < cnt + start; ++i) {
564 This->updateStateBlock->changed.pixelShaderConstantsB |= (1 << i);
565 }
566
567 g_pCurrentContext->fChangedPixelShaderConstant = true;
568
569 return VINF_SUCCESS;
570}
571
572SHADERDECL(int) ShaderSetPixelShaderConstantI(void *pShaderContext, uint32_t start, const int32_t *srcData, uint32_t count)
573{
574 IWineD3DDeviceImpl *This;
575 unsigned int i, cnt = min(count, MAX_CONST_I - start);
576
577 SHADER_SET_CURRENT_CONTEXT(pShaderContext);
578 This = g_pCurrentContext->pDeviceContext;
579
580 Log(("(ShaderSetPixelShaderConstantI %p, srcData %p, start %d, count %d)\n",
581 srcData, start, count));
582
583 if (!srcData || start >= MAX_CONST_I)
584 {
585 Log(("incorrect pixel shader const data: start(%u), srcData(0x%p), count(%u)", start, srcData, count));
586 return VERR_INVALID_PARAMETER;
587 }
588
589 memcpy(&This->updateStateBlock->pixelShaderConstantI[start * 4], srcData, cnt * sizeof(int32_t) * 4);
590
591 for (i = start; i < cnt + start; ++i) {
592 This->updateStateBlock->changed.pixelShaderConstantsI |= (1 << i);
593 }
594
595 g_pCurrentContext->fChangedPixelShaderConstant = true;
596
597 return VINF_SUCCESS;
598}
599
600SHADERDECL(int) ShaderSetPixelShaderConstantF(void *pShaderContext, uint32_t start, const float *srcData, uint32_t count)
601{
602 IWineD3DDeviceImpl *This;
603
604 SHADER_SET_CURRENT_CONTEXT(pShaderContext);
605 This = g_pCurrentContext->pDeviceContext;
606
607 Log(("(ShaderSetPixelShaderConstantF %p, srcData %p, start %d, count %d)\n",
608 srcData, start, count));
609
610 if (srcData == NULL || start + count > This->d3d_pshader_constantF || start > This->d3d_pshader_constantF)
611 {
612 Log(("incorrect pixel shader const data: start(%u), srcData(0x%p), count(%u)", start, srcData, count));
613 return VERR_INVALID_PARAMETER;
614 }
615
616 memcpy(&This->updateStateBlock->pixelShaderConstantF[start * 4], srcData, count * sizeof(float) * 4);
617
618 This->shader_backend->shader_update_float_pixel_constants((IWineD3DDevice *)This, start, count);
619
620 memset(This->updateStateBlock->changed.pixelShaderConstantsF + start, 1,
621 sizeof(*This->updateStateBlock->changed.pixelShaderConstantsF) * count);
622
623 g_pCurrentContext->fChangedPixelShaderConstant = true;
624
625 return VINF_SUCCESS;
626}
627
628SHADERDECL(int) ShaderUpdateState(void *pShaderContext, uint32_t rtHeight)
629{
630 IWineD3DDeviceImpl *pThis;
631 GLfloat yoffset;
632 GLint viewport[4];
633
634 SHADER_SET_CURRENT_CONTEXT(pShaderContext);
635 pThis = g_pCurrentContext->pDeviceContext;
636
637 glGetIntegerv(GL_VIEWPORT, viewport);
638#ifdef DEBUG
639 AssertReturn(glGetError() == GL_NO_ERROR, VERR_INTERNAL_ERROR);
640#endif
641
642 yoffset = -(63.0f / 64.0f) / viewport[3] /* height */;
643 pThis->posFixup[0] = 1.0f; /* This is needed to get the x coord unmodified through a MAD. */
644 pThis->posFixup[1] = -1.0f; /* y-inversion */
645 pThis->posFixup[2] = (63.0f / 64.0f) / viewport[2] /* width */;
646 pThis->posFixup[3] = pThis->posFixup[1] * yoffset;
647
648 pThis->rtHeight = rtHeight;
649
650 /* @todo missing state:
651 * - fog enable (stateblock->renderState[WINED3DRS_FOGENABLE])
652 * - fog mode (stateblock->renderState[WINED3DRS_FOGTABLEMODE])
653 * - stateblock->vertexDecl->position_transformed
654 */
655
656 if ( g_pCurrentContext->fChangedPixelShader
657 || g_pCurrentContext->fChangedVertexShader)
658 pThis->shader_backend->shader_select(g_pCurrentContext, !!pThis->updateStateBlock->pixelShader, !!pThis->updateStateBlock->vertexShader);
659 g_pCurrentContext->fChangedPixelShader = g_pCurrentContext->fChangedVertexShader = false;
660
661 if ( g_pCurrentContext->fChangedPixelShaderConstant
662 || g_pCurrentContext->fChangedVertexShaderConstant)
663 pThis->shader_backend->shader_load_constants(g_pCurrentContext, !!pThis->updateStateBlock->pixelShader, !!pThis->updateStateBlock->vertexShader);
664 g_pCurrentContext->fChangedPixelShaderConstant = false;
665 g_pCurrentContext->fChangedVertexShaderConstant = false;
666
667 return VINF_SUCCESS;
668}
669
670SHADERDECL(int) ShaderTransformProjection(unsigned cxViewPort, unsigned cyViewPort, float matrix[16])
671{
672#ifdef DEBUG
673 GLenum lastError;
674#endif
675 GLfloat xoffset, yoffset;
676
677 /* Assumes OpenGL context has been activated. */
678 glMatrixMode(GL_PROJECTION);
679 glLoadIdentity();
680
681 /* The rule is that the window coordinate 0 does not correspond to the
682 beginning of the first pixel, but the center of the first pixel.
683 As a consequence if you want to correctly draw one line exactly from
684 the left to the right end of the viewport (with all matrices set to
685 be identity), the x coords of both ends of the line would be not
686 -1 and 1 respectively but (-1-1/viewport_widh) and (1-1/viewport_width)
687 instead.
688
689 1.0 / Width is used because the coord range goes from -1.0 to 1.0, then we
690 divide by the Width/Height, so we need the half range(1.0) to translate by
691 half a pixel.
692
693 The other fun is that d3d's output z range after the transformation is [0;1],
694 but opengl's is [-1;1]. Since the z buffer is in range [0;1] for both, gl
695 scales [-1;1] to [0;1]. This would mean that we end up in [0.5;1] and loose a lot
696 of Z buffer precision and the clear values do not match in the z test. Thus scale
697 [0;1] to [-1;1], so when gl undoes that we utilize the full z range
698 */
699
700 /*
701 * Careful with the order of operations here, we're essentially working backwards:
702 * x = x + 1/w;
703 * y = (y - 1/h) * flip;
704 * z = z * 2 - 1;
705 *
706 * Becomes:
707 * glTranslatef(0.0, 0.0, -1.0);
708 * glScalef(1.0, 1.0, 2.0);
709 *
710 * glScalef(1.0, flip, 1.0);
711 * glTranslatef(1/w, -1/h, 0.0);
712 *
713 * This is equivalent to:
714 * glTranslatef(1/w, -flip/h, -1.0)
715 * glScalef(1.0, flip, 2.0);
716 */
717 /* Translate by slightly less than a half pixel to force a top-left
718 * filling convention. We want the difference to be large enough that
719 * it doesn't get lost due to rounding inside the driver, but small
720 * enough to prevent it from interfering with any anti-aliasing. */
721 xoffset = (63.0f / 64.0f) / cxViewPort;
722 yoffset = -(63.0f / 64.0f) / cyViewPort;
723
724 glTranslatef(xoffset, -yoffset, -1.0f);
725 /* flip y coordinate origin too */
726 glScalef(1.0f, -1.0f, 2.0f);
727
728 glMultMatrixf(matrix);
729
730#ifdef DEBUG
731 lastError = glGetError(); \
732 AssertMsgReturn(lastError == GL_NO_ERROR, ("%s (%d): last error 0x%x\n", __FUNCTION__, __LINE__, lastError), VERR_INTERNAL_ERROR);
733#endif
734 return VINF_SUCCESS;
735}
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