summaryrefslogtreecommitdiffstats
path: root/src/VBox/Devices/Graphics/shaderlib/shaderapi.c
blob: fbd77fd456f929487e5b9dfd71071663eecb92cc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
/* $Id: shaderapi.c $ */
/** @file
 * shaderlib -- interface to WINE's Direct3D shader functions
 */

/*
 * Copyright (C) 2014-2023 Oracle and/or its affiliates.
 *
 * This file is part of VirtualBox base platform packages, as
 * available from https://www.virtualbox.org.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation, in version 3 of the
 * License.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, see <https://www.gnu.org/licenses>.
 *
 * SPDX-License-Identifier: GPL-3.0-only
 */

#include <iprt/errcore.h>
#include <iprt/mem.h>
#include <iprt/assert.h>
#include <iprt/log.h>
#define WINED3D_EXTERN
#include "wined3d_private.h"

#include "shaderlib.h"

#ifdef RT_OS_WINDOWS
# define OGLGETPROCADDRESS      wglGetProcAddress

#elif RT_OS_DARWIN
# include <dlfcn.h>
# define OGLGETPROCADDRESS(x)   MyNSGLGetProcAddress((const char *)x)
void *MyNSGLGetProcAddress(const char *pszSymbol)
{
    /* Another copy in DevVGA-SVGA3d-ogl.cpp. */
    static void *s_pvImage = NULL;
    if (s_pvImage == NULL)
        s_pvImage = dlopen("/System/Library/Frameworks/OpenGL.framework/Versions/Current/OpenGL", RTLD_LAZY);
    return s_pvImage ? dlsym(s_pvImage, pszSymbol) : NULL;
}

#else
extern void (*glXGetProcAddress(const GLubyte *procname))( void );
# define OGLGETPROCADDRESS(x)   glXGetProcAddress((const GLubyte *)x)

#endif

#undef GL_EXT_FUNCS_GEN
#define GL_EXT_FUNCS_GEN \
    /* GL_ARB_shader_objects */ \
    USE_GL_FUNC(WINED3D_PFNGLGETOBJECTPARAMETERIVARBPROC, \
            glGetObjectParameterivARB,                  ARB_SHADER_OBJECTS,             NULL) \
    USE_GL_FUNC(WINED3D_PFNGLGETOBJECTPARAMETERFVARBPROC, \
            glGetObjectParameterfvARB,                  ARB_SHADER_OBJECTS,             NULL) \
    USE_GL_FUNC(WINED3D_PFNGLGETUNIFORMLOCATIONARBPROC, \
            glGetUniformLocationARB,                    ARB_SHADER_OBJECTS,             NULL) \
    USE_GL_FUNC(WINED3D_PFNGLGETACTIVEUNIFORMARBPROC, \
            glGetActiveUniformARB,                      ARB_SHADER_OBJECTS,             NULL) \
    USE_GL_FUNC(WINED3D_PFNGLUNIFORM1IARBPROC, \
            glUniform1iARB,                             ARB_SHADER_OBJECTS,             NULL) \
    USE_GL_FUNC(WINED3D_PFNGLUNIFORM2IARBPROC, \
            glUniform2iARB,                             ARB_SHADER_OBJECTS,             NULL) \
    USE_GL_FUNC(WINED3D_PFNGLUNIFORM3IARBPROC, \
            glUniform3iARB,                             ARB_SHADER_OBJECTS,             NULL) \
    USE_GL_FUNC(WINED3D_PFNGLUNIFORM4IARBPROC, \
            glUniform4iARB,                             ARB_SHADER_OBJECTS,             NULL) \
    USE_GL_FUNC(WINED3D_PFNGLUNIFORM1FARBPROC, \
            glUniform1fARB,                             ARB_SHADER_OBJECTS,             NULL) \
    USE_GL_FUNC(WINED3D_PFNGLUNIFORM2FARBPROC, \
            glUniform2fARB,                             ARB_SHADER_OBJECTS,             NULL) \
    USE_GL_FUNC(WINED3D_PFNGLUNIFORM3FARBPROC, \
            glUniform3fARB,                             ARB_SHADER_OBJECTS,             NULL) \
    USE_GL_FUNC(WINED3D_PFNGLUNIFORM4FARBPROC, \
            glUniform4fARB,                             ARB_SHADER_OBJECTS,             NULL) \
    USE_GL_FUNC(WINED3D_PFNGLUNIFORM1FVARBPROC, \
            glUniform1fvARB,                            ARB_SHADER_OBJECTS,             NULL) \
    USE_GL_FUNC(WINED3D_PFNGLUNIFORM2FVARBPROC, \
            glUniform2fvARB,                            ARB_SHADER_OBJECTS,             NULL) \
    USE_GL_FUNC(WINED3D_PFNGLUNIFORM3FVARBPROC, \
            glUniform3fvARB,                            ARB_SHADER_OBJECTS,             NULL) \
    USE_GL_FUNC(WINED3D_PFNGLUNIFORM4FVARBPROC, \
            glUniform4fvARB,                            ARB_SHADER_OBJECTS,             NULL) \
    USE_GL_FUNC(WINED3D_PFNGLUNIFORM1IVARBPROC, \
            glUniform1ivARB,                            ARB_SHADER_OBJECTS,             NULL) \
    USE_GL_FUNC(WINED3D_PFNGLUNIFORM2IVARBPROC, \
            glUniform2ivARB,                            ARB_SHADER_OBJECTS,             NULL) \
    USE_GL_FUNC(WINED3D_PFNGLUNIFORM3IVARBPROC, \
            glUniform3ivARB,                            ARB_SHADER_OBJECTS,             NULL) \
    USE_GL_FUNC(WINED3D_PFNGLUNIFORM4IVARBPROC, \
            glUniform4ivARB,                            ARB_SHADER_OBJECTS,             NULL) \
    USE_GL_FUNC(WINED3D_PFNGLUNIFORMMATRIX2FVARBPROC, \
            glUniformMatrix2fvARB,                      ARB_SHADER_OBJECTS,             NULL) \
    USE_GL_FUNC(WINED3D_PFNGLUNIFORMMATRIX3FVARBPROC, \
            glUniformMatrix3fvARB,                      ARB_SHADER_OBJECTS,             NULL) \
    USE_GL_FUNC(WINED3D_PFNGLUNIFORMMATRIX4FVARBPROC, \
            glUniformMatrix4fvARB,                      ARB_SHADER_OBJECTS,             NULL) \
    USE_GL_FUNC(WINED3D_PFNGLGETUNIFORMFVARBPROC, \
            glGetUniformfvARB,                          ARB_SHADER_OBJECTS,             NULL) \
    USE_GL_FUNC(WINED3D_PFNGLGETUNIFORMIVARBPROC, \
            glGetUniformivARB,                          ARB_SHADER_OBJECTS,             NULL) \
    USE_GL_FUNC(WINED3D_PFNGLGETINFOLOGARBPROC, \
            glGetInfoLogARB,                            ARB_SHADER_OBJECTS,             NULL) \
    USE_GL_FUNC(WINED3D_PFNGLUSEPROGRAMOBJECTARBPROC, \
            glUseProgramObjectARB,                      ARB_SHADER_OBJECTS,             NULL) \
    USE_GL_FUNC(WINED3D_PFNGLCREATESHADEROBJECTARBPROC, \
            glCreateShaderObjectARB,                    ARB_SHADER_OBJECTS,             NULL) \
    USE_GL_FUNC(WINED3D_PFNGLSHADERSOURCEARBPROC, \
            glShaderSourceARB,                          ARB_SHADER_OBJECTS,             NULL) \
    USE_GL_FUNC(WINED3D_PFNGLCOMPILESHADERARBPROC, \
            glCompileShaderARB,                         ARB_SHADER_OBJECTS,             NULL) \
    USE_GL_FUNC(WINED3D_PFNGLCREATEPROGRAMOBJECTARBPROC, \
            glCreateProgramObjectARB,                   ARB_SHADER_OBJECTS,             NULL) \
    USE_GL_FUNC(WINED3D_PFNGLATTACHOBJECTARBPROC, \
            glAttachObjectARB,                          ARB_SHADER_OBJECTS,             NULL) \
    USE_GL_FUNC(WINED3D_PFNGLLINKPROGRAMARBPROC, \
            glLinkProgramARB,                           ARB_SHADER_OBJECTS,             NULL) \
    USE_GL_FUNC(WINED3D_PFNGLDETACHOBJECTARBPROC, \
            glDetachObjectARB,                          ARB_SHADER_OBJECTS,             NULL) \
    USE_GL_FUNC(WINED3D_PFNGLDELETEOBJECTARBPROC, \
            glDeleteObjectARB,                          ARB_SHADER_OBJECTS,             NULL) \
    USE_GL_FUNC(WINED3D_PFNGLVALIDATEPROGRAMARBPROC, \
            glValidateProgramARB,                       ARB_SHADER_OBJECTS,             NULL) \
    USE_GL_FUNC(WINED3D_PFNGLGETATTACHEDOBJECTSARBPROC, \
            glGetAttachedObjectsARB,                    ARB_SHADER_OBJECTS,             NULL) \
    USE_GL_FUNC(WINED3D_PFNGLGETHANDLEARBPROC, \
            glGetHandleARB,                             ARB_SHADER_OBJECTS,             NULL) \
    USE_GL_FUNC(WINED3D_PFNGLGETSHADERSOURCEARBPROC, \
            glGetShaderSourceARB,                       ARB_SHADER_OBJECTS,             NULL) \
    USE_GL_FUNC(WINED3D_PFNGLBINDATTRIBLOCATIONARBPROC, \
            glBindAttribLocationARB,                    ARB_SHADER_OBJECTS,             NULL) \
    USE_GL_FUNC(WINED3D_PFNGLGETATTRIBLOCATIONARBPROC, \
            glGetAttribLocationARB,                     ARB_SHADER_OBJECTS,             NULL) \

static struct wined3d_context *g_pCurrentContext = NULL;
static struct wined3d_adapter g_adapter = {0};
static bool g_fInitializedLibrary = false;

#define SHADER_SET_CURRENT_CONTEXT(ctx) \
    g_pCurrentContext = (struct wined3d_context *)ctx;

SHADERDECL(int) ShaderInitLib(PVBOXVMSVGASHADERIF pVBoxShaderIf)
{
    struct wined3d_gl_info *gl_info = &g_adapter.gl_info;

    /* Dynamically load all GL core functions. */
#ifdef RT_OS_WINDOWS
    HANDLE hOpenGl32 = GetModuleHandle("opengl32.dll");
# define USE_GL_FUNC(pfn) *(FARPROC *)(&pfn) = GetProcAddress(hOpenGl32, #pfn);
#else
# define USE_GL_FUNC(pfn) pfn = (void *)OGLGETPROCADDRESS(#pfn);
#endif
    GL_FUNCS_GEN;
#undef USE_GL_FUNC

    /* Dynamically load all GL extension functions. */
#define USE_GL_FUNC(type, pfn, ext, replace) \
{ \
    gl_info->pfn = (type)OGLGETPROCADDRESS(#pfn); \
}
    GL_EXT_FUNCS_GEN;

    /* Fill in GL capabilities. */
    IWineD3DImpl_FillGLCaps(&g_adapter, pVBoxShaderIf);

    LogRel(("shaderlib: GL Limits:\n"));
    LogRel(("shaderlib:   buffers=%-2u                lights=%-2u                    textures=%-2u            texture_stages=%u\n",
            gl_info->limits.buffers, gl_info->limits.lights, gl_info->limits.textures, gl_info->limits.texture_stages));
    LogRel(("shaderlib:   fragment_samplers=%-2u      vertex_samplers=%-2u           combined_samplers=%-3u  general_combiners=%u\n",
            gl_info->limits.fragment_samplers, gl_info->limits.vertex_samplers, gl_info->limits.combined_samplers, gl_info->limits.general_combiners));
    LogRel(("shaderlib:   sampler_stages=%-2u         clipplanes=%-2u                texture_size=%-5u     texture3d_size=%u\n",
            gl_info->limits.sampler_stages, gl_info->limits.clipplanes, gl_info->limits.texture_size, gl_info->limits.texture3d_size));
    LogRel(("shaderlib:   pointsize_max=%d.%d      pointsize_min=%d.%d            point_sprite_units=%-2u  blends=%u\n",
            (int)gl_info->limits.pointsize_max, (int)(gl_info->limits.pointsize_max * 10) % 10,
            (int)gl_info->limits.pointsize_min, (int)(gl_info->limits.pointsize_min * 10) % 10,
            gl_info->limits.point_sprite_units, gl_info->limits.blends));
    LogRel(("shaderlib:   anisotropy=%-2u             shininess=%d.%02d\n",
            gl_info->limits.anisotropy, (int)gl_info->limits.shininess, (int)(gl_info->limits.shininess * 100) % 100));
    LogRel(("shaderlib:   glsl_varyings=%-3u         glsl_vs_float_constants=%-4u glsl_ps_float_constants=%u\n",
            gl_info->limits.glsl_varyings, gl_info->limits.glsl_vs_float_constants, gl_info->limits.glsl_ps_float_constants));
    LogRel(("shaderlib:   arb_vs_instructions=%-4u  arb_vs_native_constants=%-4u qarb_vs_float_constants=%u\n",
            gl_info->limits.arb_vs_instructions, gl_info->limits.arb_vs_native_constants, gl_info->limits.arb_vs_float_constants));
    LogRel(("shaderlib:   arb_vs_temps=%-2u           arb_ps_float_constants=%-4u  arb_ps_local_constants=%u\n",
            gl_info->limits.arb_vs_temps, gl_info->limits.arb_ps_float_constants, gl_info->limits.arb_ps_local_constants));
    LogRel(("shaderlib:   arb_ps_instructions=%-4u  arb_ps_temps=%-2u              arb_ps_native_constants=%u\n",
            gl_info->limits.arb_ps_instructions, gl_info->limits.arb_ps_temps, gl_info->limits.arb_ps_native_constants));

    g_fInitializedLibrary = true;
    return VINF_SUCCESS;
}

SHADERDECL(int) ShaderDestroyLib(void)
{
    return VINF_SUCCESS;
}

struct IWineD3DDeviceImpl *context_get_device(const struct wined3d_context *context)
{
    return context->pDeviceContext;
}

struct wined3d_context *context_get_current(void)
{
    return g_pCurrentContext;
}

struct wined3d_context *context_acquire(IWineD3DDeviceImpl *This, IWineD3DSurface *target, enum ContextUsage usage)
{
    RT_NOREF(This, target, usage);
    return g_pCurrentContext;
}

SHADERDECL(int) ShaderContextCreate(void **ppShaderContext)
{
    struct wined3d_context *pContext;
    HRESULT hr;

    pContext = (struct wined3d_context *)RTMemAllocZ(sizeof(struct wined3d_context));
    AssertReturn(pContext, VERR_NO_MEMORY);
    pContext->pDeviceContext = (IWineD3DDeviceImpl *)RTMemAllocZ(sizeof(IWineD3DDeviceImpl));
    AssertReturn(pContext->pDeviceContext, VERR_NO_MEMORY);

    pContext->gl_info = &g_adapter.gl_info;

    pContext->pDeviceContext->adapter = &g_adapter;
    pContext->pDeviceContext->shader_backend = &glsl_shader_backend;
    pContext->pDeviceContext->ps_selected_mode = SHADER_GLSL;
    pContext->pDeviceContext->vs_selected_mode = SHADER_GLSL;
#ifndef VBOX_WITH_VMSVGA
    pContext->render_offscreen = false;
#else
    /* VMSVGA always renders offscreen. */
    pContext->render_offscreen = true;
#endif

    list_init(&pContext->pDeviceContext->shaders);

    if (g_fInitializedLibrary)
    {
        struct shader_caps shader_caps;
        uint32_t state;

        /* Initialize the shader backend. */
        hr = pContext->pDeviceContext->shader_backend->shader_alloc_private((IWineD3DDevice *)pContext->pDeviceContext);
        AssertReturn(hr == S_OK, VERR_INTERNAL_ERROR);

        memset(&shader_caps, 0, sizeof(shader_caps));
        pContext->pDeviceContext->shader_backend->shader_get_caps(&g_adapter.gl_info, &shader_caps);
        pContext->pDeviceContext->d3d_vshader_constantF = shader_caps.MaxVertexShaderConst;
        pContext->pDeviceContext->d3d_pshader_constantF = shader_caps.MaxPixelShaderConst;
        pContext->pDeviceContext->vs_clipping = shader_caps.VSClipping;

        pContext->pDeviceContext->stateBlock = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*pContext->pDeviceContext->stateBlock));
        AssertReturn(pContext->pDeviceContext->stateBlock, VERR_NO_MEMORY);
        hr = stateblock_init(pContext->pDeviceContext->stateBlock, pContext->pDeviceContext, 0);
        AssertReturn(hr == S_OK, VERR_INTERNAL_ERROR);
        pContext->pDeviceContext->updateStateBlock = pContext->pDeviceContext->stateBlock;

        pContext->pDeviceContext->stateBlock->vertexDecl = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(IWineD3DVertexDeclarationImpl));
        AssertReturn(pContext->pDeviceContext->stateBlock->vertexDecl, VERR_NO_MEMORY);

        /* Initialize the texture unit mapping to a 1:1 mapping */
        for (state = 0; state < MAX_COMBINED_SAMPLERS; ++state)
        {
            if (state < pContext->gl_info->limits.fragment_samplers)
            {
                pContext->pDeviceContext->texUnitMap[state] = state;
                pContext->pDeviceContext->rev_tex_unit_map[state] = state;
            } else {
                pContext->pDeviceContext->texUnitMap[state] = WINED3D_UNMAPPED_STAGE;
                pContext->pDeviceContext->rev_tex_unit_map[state] = WINED3D_UNMAPPED_STAGE;
            }
        }
    }

    *ppShaderContext = (void *)pContext;
    return VINF_SUCCESS;
}

SHADERDECL(int) ShaderContextDestroy(void *pShaderContext)
{
    struct wined3d_context *pContext = (struct wined3d_context *)pShaderContext;

    if (pContext->pDeviceContext)
    {
        IWineD3DStateBlockImpl *This = pContext->pDeviceContext->stateBlock;

        /* Fails during init only. */
        if (pContext->pDeviceContext->shader_priv)
            pContext->pDeviceContext->shader_backend->shader_free_private((IWineD3DDevice *)pContext->pDeviceContext);

        if (This)
        {
            if (This->vertexShaderConstantF)
                HeapFree(GetProcessHeap(), 0, This->vertexShaderConstantF);
            if (This->changed.vertexShaderConstantsF)
                HeapFree(GetProcessHeap(), 0, This->changed.vertexShaderConstantsF);
            if (This->pixelShaderConstantF)
                HeapFree(GetProcessHeap(), 0, This->pixelShaderConstantF);
            if (This->changed.pixelShaderConstantsF)
                HeapFree(GetProcessHeap(), 0, This->changed.pixelShaderConstantsF);
            if (This->contained_vs_consts_f)
                HeapFree(GetProcessHeap(), 0, This->contained_vs_consts_f);
            if (This->contained_ps_consts_f)
                HeapFree(GetProcessHeap(), 0, This->contained_ps_consts_f);
            if (This->vertexDecl)
                HeapFree(GetProcessHeap(), 0, This->vertexDecl);
            HeapFree(GetProcessHeap(), 0, This);
        }

        RTMemFree(pContext->pDeviceContext);
    }
    RTMemFree(pShaderContext);
    return VINF_SUCCESS;
}

SHADERDECL(int) ShaderCreateVertexShader(void *pShaderContext, const uint32_t *pShaderData, uint32_t cbShaderData, void **pShaderObj)
{
    IWineD3DDeviceImpl *This;
    IWineD3DVertexShaderImpl *object;
    HRESULT hr;

    SHADER_SET_CURRENT_CONTEXT(pShaderContext);
    This = g_pCurrentContext->pDeviceContext;

    object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
    if (!object)
    {
        Log(("Failed to allocate shader memory.\n"));
        return VERR_NO_MEMORY;
    }

    object->baseShader.functionLength = cbShaderData;

    hr = vertexshader_init(object, This, (DWORD const *)pShaderData, NULL, NULL, NULL);
    if (FAILED(hr))
    {
        Log(("Failed to initialize vertex shader, hr %#x.\n", hr));
        HeapFree(GetProcessHeap(), 0, object);
        return VERR_INTERNAL_ERROR;
    }

    /* Tweak the float constants limit to use a greater number of constants.
     * Keep some space for the internal usage.
     * The shader creation code artificially sets the limit according to D3D shader version.
     * But the guest may use more constants and we are not required to strictly follow D3D specs.
     */
    object->baseShader.limits.constant_float = RT_MAX(g_adapter.gl_info.limits.glsl_vs_float_constants / 2,
                                                      object->baseShader.limits.constant_float);

#ifdef VBOX_WINE_WITH_SHADER_CACHE
    object = vertexshader_check_cached(This, object);
#endif

    Log(("Created vertex shader %p.\n", object));
    *pShaderObj = (void *)object;

    return VINF_SUCCESS;
}

SHADERDECL(int) ShaderCreatePixelShader(void *pShaderContext, const uint32_t *pShaderData, uint32_t cbShaderData, void **pShaderObj)
{
    IWineD3DDeviceImpl *This;
    IWineD3DPixelShaderImpl *object;
    HRESULT hr;

    SHADER_SET_CURRENT_CONTEXT(pShaderContext);
    This = g_pCurrentContext->pDeviceContext;

    object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
    if (!object)
    {
        Log(("Failed to allocate shader memory.\n"));
        return VERR_NO_MEMORY;
    }

    object->baseShader.functionLength = cbShaderData;

    hr = pixelshader_init(object, This, (DWORD const *)pShaderData, NULL, NULL, NULL);
    if (FAILED(hr))
    {
        Log(("Failed to initialize pixel shader, hr %#x.\n", hr));
        HeapFree(GetProcessHeap(), 0, object);
        return VERR_INTERNAL_ERROR;
    }

    /* Tweak the float constants limit to use a greater number of constants.
     * Keep some space for the internal usage.
     * The shader creation code artificially sets the limit according to D3D shader version.
     * But the guest may use more constants and we are not required to strictly follow D3D specs.
     */
    object->baseShader.limits.constant_float = RT_MAX(g_adapter.gl_info.limits.glsl_ps_float_constants / 2,
                                                      object->baseShader.limits.constant_float);

#ifdef VBOX_WINE_WITH_SHADER_CACHE
    object = pixelshader_check_cached(This, object);
#endif

    Log(("Created pixel shader %p.\n", object));
    *pShaderObj = (void *)object;
    return VINF_SUCCESS;
}

SHADERDECL(int) ShaderDestroyVertexShader(void *pShaderContext, void *pShaderObj)
{
    IWineD3DVertexShaderImpl *object = (IWineD3DVertexShaderImpl *)pShaderObj;
    AssertReturn(pShaderObj, VERR_INVALID_PARAMETER);

    SHADER_SET_CURRENT_CONTEXT(pShaderContext);

    object->lpVtbl->Release((IWineD3DVertexShader *)object);
        return VINF_SUCCESS;
}

SHADERDECL(int) ShaderDestroyPixelShader(void *pShaderContext, void *pShaderObj)
{
    IWineD3DPixelShaderImpl *object = (IWineD3DPixelShaderImpl *)pShaderObj;
    AssertReturn(pShaderObj, VERR_INVALID_PARAMETER);

    SHADER_SET_CURRENT_CONTEXT(pShaderContext);

    object->lpVtbl->Release((IWineD3DPixelShader *)object);
        return VINF_SUCCESS;
}

SHADERDECL(int) ShaderSetVertexShader(void *pShaderContext, void *pShaderObj)
{
    IWineD3DDeviceImpl *This;
    IWineD3DVertexShader* pShader;
    IWineD3DVertexShader* oldShader;

    SHADER_SET_CURRENT_CONTEXT(pShaderContext);
    This = g_pCurrentContext->pDeviceContext;
    pShader   = (IWineD3DVertexShader* )pShaderObj;
    oldShader = This->updateStateBlock->vertexShader;

    if(oldShader == pShader) {
        /* Checked here to allow proper stateblock recording */
        Log(("App is setting the old shader over, nothing to do\n"));
        return VINF_SUCCESS;
    }

    This->updateStateBlock->vertexShader         = pShader;
    This->updateStateBlock->changed.vertexShader = TRUE;

    Log(("(%p) : setting pShader(%p)\n", This, pShader));
    if(pShader) IWineD3DVertexShader_AddRef(pShader);
    if(oldShader) IWineD3DVertexShader_Release(oldShader);

    g_pCurrentContext->fChangedVertexShader = true;
    g_pCurrentContext->fChangedVertexShaderConstant = true;    /* force constant reload. */

    return VINF_SUCCESS;
}

SHADERDECL(int) ShaderSetPixelShader(void *pShaderContext, void *pShaderObj)
{
    IWineD3DDeviceImpl *This;
    IWineD3DPixelShader* pShader;
    IWineD3DPixelShader* oldShader;

    SHADER_SET_CURRENT_CONTEXT(pShaderContext);
    This = g_pCurrentContext->pDeviceContext;
    pShader   = (IWineD3DPixelShader* )pShaderObj;
    oldShader = This->updateStateBlock->pixelShader;

    if(oldShader == pShader) {
        /* Checked here to allow proper stateblock recording */
        Log(("App is setting the old shader over, nothing to do\n"));
        return VINF_SUCCESS;
    }

    This->updateStateBlock->pixelShader         = pShader;
    This->updateStateBlock->changed.pixelShader = TRUE;

    Log(("(%p) : setting pShader(%p)\n", This, pShader));
    if(pShader) IWineD3DPixelShader_AddRef(pShader);
    if(oldShader) IWineD3DPixelShader_Release(oldShader);

    g_pCurrentContext->fChangedPixelShader = true;
    g_pCurrentContext->fChangedPixelShaderConstant = true;    /* force constant reload. */
    return VINF_SUCCESS;
}

SHADERDECL(int) ShaderSetVertexShaderConstantB(void *pShaderContext, uint32_t start, const uint8_t *srcData, uint32_t count)
{
    IWineD3DDeviceImpl *This;
    unsigned int i, cnt = min(count, MAX_CONST_B - start);

    SHADER_SET_CURRENT_CONTEXT(pShaderContext);
    This = g_pCurrentContext->pDeviceContext;

    Log(("(ShaderSetVertexShaderConstantB %p, srcData %p, start %d, count %d)\n", pShaderContext, srcData, start, count));

    if (!srcData || start >= MAX_CONST_B)
    {
        Log(("incorrect vertex shader const data: start(%u), srcData(0x%p), count(%u)", start, srcData, count));
        return VERR_INVALID_PARAMETER;
    }

    memcpy(&This->updateStateBlock->vertexShaderConstantB[start], srcData, cnt * sizeof(BOOL));
    for (i = 0; i < cnt; i++)
        Log(("Set BOOL constant %u to %s\n", start + i, srcData[i]? "true":"false"));

    for (i = start; i < cnt + start; ++i) {
        This->updateStateBlock->changed.vertexShaderConstantsB |= (1 << i);
    }

    g_pCurrentContext->fChangedVertexShaderConstant = true;

    return VINF_SUCCESS;
}

SHADERDECL(int) ShaderSetVertexShaderConstantI(void *pShaderContext, uint32_t start, const int32_t *srcData, uint32_t count)
{
    IWineD3DDeviceImpl *This;
    unsigned int i, cnt = min(count, MAX_CONST_I - start);

    SHADER_SET_CURRENT_CONTEXT(pShaderContext);
    This = g_pCurrentContext->pDeviceContext;

    Log(("(ShaderSetVertexShaderConstantI %p, srcData %p, start %d, count %d)\n", pShaderContext, srcData, start, count));

    if (!srcData || start >= MAX_CONST_I)
    {
        Log(("incorrect vertex shader const data: start(%u), srcData(0x%p), count(%u)", start, srcData, count));
        return VERR_INVALID_PARAMETER;
    }

    memcpy(&This->updateStateBlock->vertexShaderConstantI[start * 4], srcData, cnt * sizeof(int32_t) * 4);

    for (i = start; i < cnt + start; ++i) {
        This->updateStateBlock->changed.vertexShaderConstantsI |= (1 << i);
    }

    g_pCurrentContext->fChangedVertexShaderConstant = true;

    return VINF_SUCCESS;
}

SHADERDECL(int) ShaderSetVertexShaderConstantF(void *pShaderContext, uint32_t start, const float *srcData, uint32_t count)
{
    IWineD3DDeviceImpl *This;

    SHADER_SET_CURRENT_CONTEXT(pShaderContext);
    This = g_pCurrentContext->pDeviceContext;

    Log(("(ShaderSetVertexShaderConstantF %p, srcData %p, start %d, count %d)\n", pShaderContext, srcData, start, count));

    if (srcData == NULL || start + count > This->d3d_vshader_constantF || start > This->d3d_vshader_constantF)
    {
        Log(("incorrect vertex shader const data: start(%u), srcData(0x%p), count(%u)", start, srcData, count));
        return VERR_INVALID_PARAMETER;
    }
    memcpy(&This->updateStateBlock->vertexShaderConstantF[start * 4], srcData, count * sizeof(float) * 4);

    This->shader_backend->shader_update_float_vertex_constants((IWineD3DDevice *)This, start, count);

    memset(This->updateStateBlock->changed.vertexShaderConstantsF + start, 1,
           sizeof(*This->updateStateBlock->changed.vertexShaderConstantsF) * count);

    g_pCurrentContext->fChangedVertexShaderConstant = true;

    return VINF_SUCCESS;
}

SHADERDECL(int) ShaderSetPixelShaderConstantB(void *pShaderContext, uint32_t start, const uint8_t *srcData, uint32_t count)
{
    IWineD3DDeviceImpl *This;
    unsigned int i, cnt = min(count, MAX_CONST_B - start);

    SHADER_SET_CURRENT_CONTEXT(pShaderContext);
    This = g_pCurrentContext->pDeviceContext;

    Log(("(ShaderSetPixelShaderConstantB %p, srcData %p, start %d, count %d)\n", pShaderContext, srcData, start, count));

    if (!srcData || start >= MAX_CONST_B)
    {
        Log(("incorrect pixel shader const data: start(%u), srcData(0x%p), count(%u)", start, srcData, count));
        return VERR_INVALID_PARAMETER;
    }

    memcpy(&This->updateStateBlock->pixelShaderConstantB[start], srcData, cnt * sizeof(BOOL));
    for (i = 0; i < cnt; i++)
        Log(("Set BOOL constant %u to %s\n", start + i, srcData[i]? "true":"false"));

    for (i = start; i < cnt + start; ++i) {
        This->updateStateBlock->changed.pixelShaderConstantsB |= (1 << i);
    }

    g_pCurrentContext->fChangedPixelShaderConstant = true;

    return VINF_SUCCESS;
}

SHADERDECL(int) ShaderSetPixelShaderConstantI(void *pShaderContext, uint32_t start, const int32_t *srcData, uint32_t count)
{
    IWineD3DDeviceImpl *This;
    unsigned int i, cnt = min(count, MAX_CONST_I - start);

    SHADER_SET_CURRENT_CONTEXT(pShaderContext);
    This = g_pCurrentContext->pDeviceContext;

    Log(("(ShaderSetPixelShaderConstantI %p, srcData %p, start %d, count %d)\n", pShaderContext, srcData, start, count));

    if (!srcData || start >= MAX_CONST_I)
    {
        Log(("incorrect pixel shader const data: start(%u), srcData(0x%p), count(%u)", start, srcData, count));
        return VERR_INVALID_PARAMETER;
    }

    memcpy(&This->updateStateBlock->pixelShaderConstantI[start * 4], srcData, cnt * sizeof(int32_t) * 4);

    for (i = start; i < cnt + start; ++i) {
        This->updateStateBlock->changed.pixelShaderConstantsI |= (1 << i);
    }

    g_pCurrentContext->fChangedPixelShaderConstant = true;

    return VINF_SUCCESS;
}

SHADERDECL(int) ShaderSetPixelShaderConstantF(void *pShaderContext, uint32_t start, const float *srcData, uint32_t count)
{
    IWineD3DDeviceImpl *This;

    SHADER_SET_CURRENT_CONTEXT(pShaderContext);
    This = g_pCurrentContext->pDeviceContext;

    Log(("(ShaderSetPixelShaderConstantF %p, srcData %p, start %d, count %d)\n", pShaderContext, srcData, start, count));

    if (srcData == NULL || start + count > This->d3d_pshader_constantF || start > This->d3d_pshader_constantF)
    {
        Log(("incorrect pixel shader const data: start(%u), srcData(0x%p), count(%u)", start, srcData, count));
        return VERR_INVALID_PARAMETER;
    }

    memcpy(&This->updateStateBlock->pixelShaderConstantF[start * 4], srcData, count * sizeof(float) * 4);

    This->shader_backend->shader_update_float_pixel_constants((IWineD3DDevice *)This, start, count);

    memset(This->updateStateBlock->changed.pixelShaderConstantsF + start, 1,
            sizeof(*This->updateStateBlock->changed.pixelShaderConstantsF) * count);

    g_pCurrentContext->fChangedPixelShaderConstant = true;

    return VINF_SUCCESS;
}

SHADERDECL(int) ShaderSetPositionTransformed(void *pShaderContext, unsigned cxViewPort, unsigned cyViewPort, bool fPreTransformed)
{
    IWineD3DDeviceImpl *This;
    int rc;

    SHADER_SET_CURRENT_CONTEXT(pShaderContext);
    This = g_pCurrentContext->pDeviceContext;

    if (This->strided_streams.position_transformed == fPreTransformed)
        return VINF_SUCCESS;    /* no changes; nothing to do. */

    Log(("ShaderSetPositionTransformed viewport (%d,%d) fPreTransformed=%d\n", cxViewPort, cyViewPort, fPreTransformed));

    if (fPreTransformed)
    {   /* In the pre-transformed vertex coordinate case we need to disable all transformations as we're already using screen coordinates. */
        /* Load the identity matrix for the model view */
        glMatrixMode(GL_MODELVIEW);
        glLoadIdentity();

        /* Reset the projection matrix too */
        rc = ShaderTransformProjection(cxViewPort, cyViewPort, NULL, fPreTransformed);
        AssertRCReturn(rc, rc);
    }

    This->strided_streams.position_transformed = fPreTransformed;
    ((IWineD3DVertexDeclarationImpl *)(This->stateBlock->vertexDecl))->position_transformed = fPreTransformed;
    return VINF_SUCCESS;
}

SHADERDECL(int) ShaderUpdateState(void *pShaderContext, uint32_t rtHeight)
{
    IWineD3DDeviceImpl *pThis;
    GLfloat yoffset;
    GLint viewport[4];

    SHADER_SET_CURRENT_CONTEXT(pShaderContext);
    pThis = g_pCurrentContext->pDeviceContext;

    glGetIntegerv(GL_VIEWPORT, viewport);
#ifdef DEBUG
    AssertReturn(glGetError() == GL_NO_ERROR, VERR_INTERNAL_ERROR);
#endif

    yoffset = -(63.0f / 64.0f) / viewport[3] /* height */;
    pThis->posFixup[0] = 1.0f;  /* This is needed to get the x coord unmodified through a MAD. */
    pThis->posFixup[1] = -1.0f;  /* y-inversion */
    pThis->posFixup[2] = (63.0f / 64.0f) / viewport[2] /* width */;
    pThis->posFixup[3] = pThis->posFixup[1] * yoffset;

    pThis->rtHeight = rtHeight;

    /** @todo missing state:
     * - fog enable (stateblock->renderState[WINED3DRS_FOGENABLE])
     * - fog mode (stateblock->renderState[WINED3DRS_FOGTABLEMODE])
     * - stateblock->vertexDecl->position_transformed
     */

    if (    g_pCurrentContext->fChangedPixelShader
        ||  g_pCurrentContext->fChangedVertexShader)
        pThis->shader_backend->shader_select(g_pCurrentContext, !!pThis->updateStateBlock->pixelShader, !!pThis->updateStateBlock->vertexShader);
    g_pCurrentContext->fChangedPixelShader = g_pCurrentContext->fChangedVertexShader = false;

    if (    g_pCurrentContext->fChangedPixelShaderConstant
        ||  g_pCurrentContext->fChangedVertexShaderConstant)
        pThis->shader_backend->shader_load_constants(g_pCurrentContext, !!pThis->updateStateBlock->pixelShader, !!pThis->updateStateBlock->vertexShader);
    g_pCurrentContext->fChangedPixelShaderConstant  = false;
    g_pCurrentContext->fChangedVertexShaderConstant = false;

    return VINF_SUCCESS;
}

SHADERDECL(int) ShaderTransformProjection(unsigned cxViewPort, unsigned cyViewPort, float matrix[16], bool fPretransformed)
{
#ifdef DEBUG
    GLenum lastError;
#endif
    GLfloat xoffset, yoffset;

    /* Assumes OpenGL context has been activated. */
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();

    /* The rule is that the window coordinate 0 does not correspond to the
       beginning of the first pixel, but the center of the first pixel.
       As a consequence if you want to correctly draw one line exactly from
       the left to the right end of the viewport (with all matrices set to
       be identity), the x coords of both ends of the line would be not
       -1 and 1 respectively but (-1-1/viewport_widh) and (1-1/viewport_width)
       instead.

       1.0 / Width is used because the coord range goes from -1.0 to 1.0, then we
       divide by the Width/Height, so we need the half range(1.0) to translate by
       half a pixel.

       The other fun is that d3d's output z range after the transformation is [0;1],
       but opengl's is [-1;1]. Since the z buffer is in range [0;1] for both, gl
       scales [-1;1] to [0;1]. This would mean that we end up in [0.5;1] and loose a lot
       of Z buffer precision and the clear values do not match in the z test. Thus scale
       [0;1] to [-1;1], so when gl undoes that we utilize the full z range
    */

    /*
     * Careful with the order of operations here, we're essentially working backwards:
     * x = x + 1/w;
     * y = (y - 1/h) * flip;
     * z = z * 2 - 1;
     *
     * Becomes:
     * glTranslatef(0.0, 0.0, -1.0);
     * glScalef(1.0, 1.0, 2.0);
     *
     * glScalef(1.0, flip, 1.0);
     * glTranslatef(1/w, -1/h, 0.0);
     *
     * This is equivalent to:
     * glTranslatef(1/w, -flip/h, -1.0)
     * glScalef(1.0, flip, 2.0);
     */
    /* Translate by slightly less than a half pixel to force a top-left
     * filling convention. We want the difference to be large enough that
     * it doesn't get lost due to rounding inside the driver, but small
     * enough to prevent it from interfering with any anti-aliasing. */
    xoffset = (63.0f / 64.0f) / cxViewPort;
    yoffset = -(63.0f / 64.0f) / cyViewPort;

    glTranslatef(xoffset, -yoffset, -1.0f);

    if (fPretransformed)
    {
        /* One world coordinate equals one screen pixel; y-inversion no longer an issue */
        glOrtho(0, cxViewPort, 0, cyViewPort, -1, 1);
    }
    else
    {
        /* flip y coordinate origin too */
        glScalef(1.0f, -1.0f, 2.0f);

        /* Apply the supplied projection matrix */
        glMultMatrixf(matrix);
    }
#ifdef DEBUG
    lastError = glGetError();                                     \
    AssertMsgReturn(lastError == GL_NO_ERROR, ("%s (%d): last error 0x%x\n", __FUNCTION__, __LINE__, lastError), VERR_INTERNAL_ERROR);
#endif
    return VINF_SUCCESS;
}