summaryrefslogtreecommitdiffstats
path: root/src/VBox/Devices/Graphics/DevVGA-SVGA3d-glHlp.cpp
blob: 3baec0f6cdc2d40917d73e81b1bfc67943529f56 (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
/* $Id: DevVGA-SVGA3d-glHlp.cpp $ */
/** @file
 * DevVMWare - VMWare SVGA device OpenGL backend
 */

/*
 * Copyright (C) 2013-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
 */


/*********************************************************************************************************************************
*   Header Files                                                                                                                 *
*********************************************************************************************************************************/
#ifdef DEBUG_bird
//# define RTMEM_WRAP_TO_EF_APIS
#endif
#define LOG_GROUP LOG_GROUP_DEV_VMSVGA
#include <VBox/vmm/pdmdev.h>

#include <iprt/assert.h>
#include <iprt/mem.h>

#include "DevVGA-SVGA.h"
#include "DevVGA-SVGA3d-internal.h"

/* Parameters for glVertexAttribPointer. */
typedef struct VertexAttribDesc
{
    GLint     size;
    GLenum    type;
    GLboolean normalized;
    GLsizei   stride;
    GLsizei   offset;
} VertexAttribDesc;

/* Information about a shader program. */
typedef struct ShaderProgram
{
    GLuint vertexShader;   /* Vertex shader name. */
    GLuint fragmentShader; /* Fragment shader name. */
    GLuint program;        /* Shader program name. */
    GLint sSourceTex;      /* Location of the texture sampler uniform in the shader. */
    GLint uTexInfo;        /* Location of the texture information uniform in the shader. */
} ShaderProgram;

/* Texture format conversion data.
 * Uses a fragment (pixel) shader to render a source texture in one format
 * to the target texture in another format.
 */
typedef struct VMSVGA3DFORMATCONVERTER
{
    PVMSVGA3DSTATE pState;

    ShaderProgram programYUY2ToRGB;         /* From the YUY2 emulated format to the actual RGB texture. */
    ShaderProgram programYUY2FromRGB;       /* From the actual RGB texture to the emulated YUY2 format. */
    ShaderProgram programUYVYToRGB;         /* From the UYVY emulated format to the actual RGB texture. */
    ShaderProgram programUYVYFromRGB;       /* From the actual RGB texture to the emulated UYVY format. */

    GLuint framebuffer;                     /* Framebuffer object name. */

    GLuint vertexBuffer;                    /* Vertex attribute buffer. Position + texcoord. */
} VMSVGA3DFORMATCONVERTER;

/* Parameters for glVertexAttribPointer. */
static const VertexAttribDesc aVertexAttribs[] =
{
    {2, GL_FLOAT, GL_FALSE, 16, 0 }, /* Position. */
    {2, GL_FLOAT, GL_FALSE, 16, 8 }  /* Texcoord. */
};

/* Triangle fan */
static float const aAttribData[] =
{
    /* positions      texcoords */
    -1.0f,  -1.0f,    0.0f,   0.0f,
     1.0f,  -1.0f,    1.0f,   0.0f,
     1.0f,   1.0f,    1.0f,   1.0f,
    -1.0f,   1.0f,    0.0f,   1.0f
};

static const GLchar shaderHeaderSource[] =
{
    "  #version 120\n"
};

static const GLchar vertexShaderSource[] =
{
    "  attribute vec2 attrib0;\n"
    "  attribute vec2 attrib1;\n"
    "  void main(void)\n"
    "  {\n"
    "      gl_TexCoord[0].xy = attrib1;\n"
    "      gl_Position = vec4(attrib0.x, attrib0.y, 0.0f, 1.0f);\n"
    "  }\n"
};

static const GLchar fetchYUY2Source[] =
{
    "  vec4 fetchYUV(vec4 texColor)\n"
    "  {\n"
    "      return vec4(texColor.b, texColor.g, texColor.r, texColor.a);\n"
    "  }\n"
};

static const GLchar fetchUYVYSource[] =
{
    "  vec4 fetchYUV(vec4 texColor)\n"
    "  {\n"
    "      return vec4(texColor.g, texColor.b, texColor.a, texColor.r);\n"
    "  }\n"
};

static const GLchar YUV2RGBShaderSource[] =
{
    "  uniform sampler2D sSourceTex;\n"
    "  uniform vec4 uTexInfo;\n"
    "  \n"
    "  const mat3 yuvCoeffs = mat3\n"
    "  (\n"
    "      1.164383f,       0.0f,  1.596027f, // first column \n"
    "      1.164383f, -0.391762f, -0.812968f, // second column\n"
    "      1.164383f,  2.017232f,  0.0f       // third column\n"
    "  );\n"
    "  \n"
    "  void main() {\n"
    "      // Input texcoords are in [0;1] range for the target.\n"
    "      vec2 texCoord = gl_TexCoord[0].xy;\n"
    "      // Convert to the target coords in pixels: xPixel = texCoord.x * TextureWidth. \n"
    "      float xTargetPixel = texCoord.x * uTexInfo.x;\n"
    "      // Source texture is half width, i.e. it contains data in pixels [0; width / 2 - 1].\n"
    "      float xSourcePixel = xTargetPixel / 2.0f;\n"
    "      // Remainder is about 0.25 for even pixels and about 0.75 for odd pixels.\n"
    "      float remainder = fract(xSourcePixel);\n"
    "      // Back to the normalized coords: texCoord.x = xPixel / Width.\n"
    "      texCoord.x = xSourcePixel * uTexInfo.z;\n"
    "      vec4 texColor = texture2D(sSourceTex, texCoord);\n"
    "      vec4 y0uy1v = fetchYUV(texColor);\n"
    "      // Get y0 for even x coordinates and y1 for odd ones.\n"
    "      float y = remainder < 0.5f ? y0uy1v.x : y0uy1v.z;\n"
    "      // Make a vector for easier calculation.\n"
    "      vec3 yuv = vec3(y, y0uy1v.y, y0uy1v.w);\n"
    "      yuv -= vec3(0.0627f, 0.502f, 0.502f);\n"
    "      vec3 bgr = yuv * yuvCoeffs;\n"
    "      //vec3 bgr;\n"
    "      //bgr.r = 1.164383 * yuv.x                    + 1.596027 * yuv.z;\n"
    "      //bgr.g = 1.164383 * yuv.x - 0.391762 * yuv.y - 0.812968 * yuv.z;\n"
    "      //bgr.b = 1.164383 * yuv.x + 2.017232 * yuv.y;\n"
    "      bgr = clamp(bgr, 0.0f, 1.0f);\n"
    "      gl_FragData[0] = vec4(bgr, 1.0f);\n"
    "  }\n"
};

static const GLchar storeYUY2Source[] =
{
    "  vec4 storeYUV(float y0, float u, float y1, float v)\n"
    "  {\n"
    "      return vec4(y1, u, y0, v);\n"
    "  }\n"
};

static const GLchar storeUYVYSource[] =
{
    "  vec4 storeYUV(float y0, float u, float y1, float v)\n"
    "  {\n"
    "      return vec4(u, y1, v, y0);\n"
    "  }\n"
};

static const GLchar RGB2YUVShaderSource[] =
{
    "  uniform sampler2D sSourceTex;\n"
    "  uniform vec4 uTexInfo;\n"
    "  \n"
    "  const mat3 bgrCoeffs = mat3\n"
    "  (\n"
    "       0.2578f,  0.5039f,  0.0977f, // first column \n"
    "      -0.1484f, -0.2891f,  0.4375f, // second column\n"
    "       0.4375f, -0.3672f, -0.0703f  // third column\n"
    "  );\n"
    "  const vec3 yuvShift = vec3(0.0647f, 0.5039f, 0.5039f);\n"
    "  \n"
    "  void main() {\n"
    "      // Input texcoords are in [0;1] range for the target.\n"
    "      vec2 texCoordDst = gl_TexCoord[0].xy;\n"
    "      // Convert to the target coords in pixels: xPixel = TexCoord.x * TextureWidth.\n"
    "      float xTargetPixel = texCoordDst.x * uTexInfo.x;\n"
    "      vec4 bgraOutputPixel;\n"
    "      if (xTargetPixel < uTexInfo.x / 2.0f)\n"
    "      {\n"
    "          // Target texture is half width, i.e. it contains data in pixels [0; width / 2 - 1].\n"
    "          // Compute the source texture coords for the pixels which will be used to compute the target pixel.\n"
    "          vec2 texCoordSrc = texCoordDst;\n"
    "          texCoordSrc.x *= 2.0f;\n"
    "          // Even pixel. Fetch two BGRA source pixels.\n"
    "          vec4 texColor0 = texture2D(sSourceTex, texCoordSrc);\n"
    "          // Advance one pixel (+ 1/Width)\n"
    "          texCoordSrc.x += uTexInfo.z;\n"
    "          vec4 texColor1 = texture2D(sSourceTex, texCoordSrc);\n"
    "          vec3 yuv0 = texColor0.rgb * bgrCoeffs;\n"
    "          yuv0 += yuvShift;\n"
    "          vec3 yuv1 = texColor1.rgb * bgrCoeffs;\n"
    "          yuv1 += yuvShift;\n"
    "          float y0 = yuv0.r;\n"
    "          float  u = (yuv0.g + yuv1.g) / 2.0f;\n"
    "          float y1 = yuv1.r;\n"
    "          float  v = (yuv0.b + yuv1.b) / 2.0f;\n"
    "          bgraOutputPixel = storeYUV(y0, u, y1, v);\n"
    "      }\n"
    "      else\n"
    "      {\n"
    "          // [width / 2; width - 1] pixels are not used. Set to something.\n"
    "          bgraOutputPixel = vec4(0.0f, 0.0f, 0.0f, 0.0f);\n"
    "      }\n"
    "      bgraOutputPixel = clamp(bgraOutputPixel, 0.0f, 1.0f);\n"
    "      gl_FragData[0] = bgraOutputPixel;\n"
    "  }\n"
};

#define GL_CHECK_ERROR() do { \
    VMSVGA3D_CHECK_LAST_ERROR_WARN(pState, pContext); \
    if (pContext->lastError != GL_NO_ERROR) \
        LogRelMax(10, ("VMSVGA: %s (%d): GL error 0x%x\n", __FUNCTION__, __LINE__, pContext->lastError)); \
} while (0)

/* Compile shaders and link a shader program. */
static void createShaderProgram(PVMSVGA3DSTATE pState,
                                ShaderProgram *pProgram,
                                int cVertexSources, const GLchar **apszVertexSources,
                                int cFragmentSources, const GLchar **apszFragmentSources)
{
    AssertReturnVoid(pState->idActiveContext == VMSVGA3D_SHARED_CTX_ID);

    /* Everything is done on the shared context. The pState and pContext are for GL_CHECK_ERROR macro. */
    PVMSVGA3DCONTEXT pContext = &pState->SharedCtx;

    int success;
    char szInfoLog[1024];

    /*
     * VERTEX shader.
     */
    pProgram->vertexShader = pState->ext.glCreateShader(GL_VERTEX_SHADER);
    GL_CHECK_ERROR();

    pState->ext.glShaderSource(pProgram->vertexShader, cVertexSources, apszVertexSources, NULL);
    GL_CHECK_ERROR();

    pState->ext.glCompileShader(pProgram->vertexShader);
    GL_CHECK_ERROR();

    pState->ext.glGetShaderiv(pProgram->vertexShader, GL_COMPILE_STATUS, &success);
    GL_CHECK_ERROR();
    if (!success)
    {
        pState->ext.glGetShaderInfoLog(pProgram->vertexShader, sizeof(szInfoLog), NULL, szInfoLog);
        GL_CHECK_ERROR();
        LogRelMax(10, ("VMSVGA: Vertex shader compilation error:\n%s\n", szInfoLog));
    };

    /*
     * FRAGMENT shader.
     */
    pProgram->fragmentShader = pState->ext.glCreateShader(GL_FRAGMENT_SHADER);
    GL_CHECK_ERROR();

    pState->ext.glShaderSource(pProgram->fragmentShader, cFragmentSources, apszFragmentSources, NULL);
    GL_CHECK_ERROR();

    pState->ext.glCompileShader(pProgram->fragmentShader);
    GL_CHECK_ERROR();

    pState->ext.glGetShaderiv(pProgram->fragmentShader, GL_COMPILE_STATUS, &success);
    GL_CHECK_ERROR();
    if (!success)
    {
        pState->ext.glGetShaderInfoLog(pProgram->fragmentShader, sizeof(szInfoLog), NULL, szInfoLog);
        GL_CHECK_ERROR();
        LogRelMax(10, ("VMSVGA: Fragment shader compilation error:\n%s\n", szInfoLog));
    };

    /*
     * Program
     */
    pProgram->program = pState->ext.glCreateProgram();
    GL_CHECK_ERROR();

    pState->ext.glAttachShader(pProgram->program, pProgram->vertexShader);
    GL_CHECK_ERROR();

    pState->ext.glAttachShader(pProgram->program, pProgram->fragmentShader);
    GL_CHECK_ERROR();

    pState->ext.glLinkProgram(pProgram->program);
    GL_CHECK_ERROR();

    pState->ext.glGetProgramiv(pProgram->program, GL_LINK_STATUS, &success);
    if(!success)
    {
        pState->ext.glGetProgramInfoLog(pProgram->program, sizeof(szInfoLog), NULL, szInfoLog);
        GL_CHECK_ERROR();
        LogRelMax(10, ("VMSVGA: Shader program link error:\n%s\n", szInfoLog));
    }

    pProgram->sSourceTex = pState->ext.glGetUniformLocation(pProgram->program, "sSourceTex");
    GL_CHECK_ERROR();

    pProgram->uTexInfo = pState->ext.glGetUniformLocation(pProgram->program, "uTexInfo");
    GL_CHECK_ERROR();
}

/* Delete a shader program and associated shaders. */
static void deleteShaderProgram(PVMSVGA3DSTATE pState,
                                ShaderProgram *pProgram)
{
    AssertReturnVoid(pState->idActiveContext == VMSVGA3D_SHARED_CTX_ID);

    /* Everything is done on the shared context. The pState and pContext are for GL_CHECK_ERROR macro. */
    PVMSVGA3DCONTEXT pContext = &pState->SharedCtx;

    if (pProgram->program)
    {
        if (pProgram->vertexShader)
        {
            pState->ext.glDetachShader(pProgram->program, pProgram->vertexShader);
            GL_CHECK_ERROR();

            pState->ext.glDeleteShader(pProgram->vertexShader);
            GL_CHECK_ERROR();
        }

        if (pProgram->fragmentShader)
        {
            pState->ext.glDetachShader(pProgram->program, pProgram->fragmentShader);
            GL_CHECK_ERROR();

            pState->ext.glDeleteShader(pProgram->fragmentShader);
            GL_CHECK_ERROR();
        }

        pState->ext.glDeleteProgram(pProgram->program);
        GL_CHECK_ERROR();
    }

    RT_ZERO(*pProgram);
}

/* Initialize the format conversion. Allocate and create necessary resources. */
static void formatConversionInit(PVMSVGA3DSTATE pState)
{
    AssertReturnVoid(pState->idActiveContext == VMSVGA3D_SHARED_CTX_ID);

    PVMSVGA3DFORMATCONVERTER pConv = pState->pConv;
    AssertReturnVoid(pConv);

    /* The pState and pContext variables are for GL_CHECK_ERROR macro. */
    PVMSVGA3DCONTEXT pContext = &pState->SharedCtx;

    pConv->pState = pState;

    /*
     * Shader programs.
     */
    static const GLchar *apszVertexShaderSources[] =
    {
        shaderHeaderSource,
        vertexShaderSource
    };

    static const GLchar * apszYUY2ToRGBSources[] =
    {
        shaderHeaderSource,
        fetchYUY2Source,
        YUV2RGBShaderSource
    };

    static const GLchar *apszUYVYToRGBSources[] =
    {
        shaderHeaderSource,
        fetchUYVYSource,
        YUV2RGBShaderSource
    };

    static const GLchar *apszYUY2FromRGBSources[] =
    {
        shaderHeaderSource,
        storeYUY2Source,
        RGB2YUVShaderSource
    };

    static const GLchar *apszUYVYFromRGBSources[] =
    {
        shaderHeaderSource,
        storeUYVYSource,
        RGB2YUVShaderSource
    };

    createShaderProgram(pState, &pConv->programYUY2ToRGB,
                        RT_ELEMENTS(apszVertexShaderSources), apszVertexShaderSources,
                        RT_ELEMENTS(apszYUY2ToRGBSources), apszYUY2ToRGBSources);

    createShaderProgram(pState, &pConv->programUYVYToRGB,
                        RT_ELEMENTS(apszVertexShaderSources), apszVertexShaderSources,
                        RT_ELEMENTS(apszUYVYToRGBSources), apszUYVYToRGBSources);

    createShaderProgram(pState, &pConv->programYUY2FromRGB,
                        RT_ELEMENTS(apszVertexShaderSources), apszVertexShaderSources,
                        RT_ELEMENTS(apszYUY2FromRGBSources), apszYUY2FromRGBSources);

    createShaderProgram(pState, &pConv->programUYVYFromRGB,
                        RT_ELEMENTS(apszVertexShaderSources), apszVertexShaderSources,
                        RT_ELEMENTS(apszUYVYFromRGBSources), apszUYVYFromRGBSources);

    /*
     * Create a framebuffer object which is used for rendering to a texture.
     */
    pState->ext.glGenFramebuffers(1, &pConv->framebuffer);
    GL_CHECK_ERROR();

    pState->ext.glBindFramebuffer(GL_DRAW_FRAMEBUFFER, pConv->framebuffer);
    GL_CHECK_ERROR();

    static GLenum aDrawBuffers[] = { GL_COLOR_ATTACHMENT0 };
    pState->ext.glDrawBuffers(RT_ELEMENTS(aDrawBuffers), aDrawBuffers);
    GL_CHECK_ERROR();

    pState->ext.glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0);
    GL_CHECK_ERROR();

    /*
     * Vertex attribute array.
     */
    pState->ext.glGenBuffers(1, &pConv->vertexBuffer);
    GL_CHECK_ERROR();

    pState->ext.glBindBuffer(GL_ARRAY_BUFFER, pConv->vertexBuffer);
    GL_CHECK_ERROR();

    pState->ext.glBufferData(GL_ARRAY_BUFFER, sizeof(aAttribData), aAttribData, GL_STATIC_DRAW);
    GL_CHECK_ERROR();

    pState->ext.glBindBuffer(GL_ARRAY_BUFFER, 0);
    GL_CHECK_ERROR();
}

/* Delete everything. */
static void formatConversionDestroy(PVMSVGA3DSTATE pState)
{
    AssertReturnVoid(pState->idActiveContext == VMSVGA3D_SHARED_CTX_ID);

    PVMSVGA3DFORMATCONVERTER pConv = pState->pConv;
    AssertReturnVoid(pConv);

    /* The pState and pContext variables are for GL_CHECK_ERROR macro. */
    PVMSVGA3DCONTEXT pContext = &pState->SharedCtx;

    if (pConv->framebuffer != 0)
    {
        /* The code keeps nothing attached. */
        pState->ext.glBindFramebuffer(GL_DRAW_FRAMEBUFFER, pConv->framebuffer);
        GL_CHECK_ERROR();

        GLint texture = -1;
        pState->ext.glGetFramebufferAttachmentParameteriv(GL_DRAW_FRAMEBUFFER, GL_COLOR_ATTACHMENT0,
                                                          GL_FRAMEBUFFER_ATTACHMENT_OBJECT_NAME, &texture);
        GL_CHECK_ERROR();
        AssertMsg(texture == 0, ("texture %d\n", texture));

        pState->ext.glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0);
        GL_CHECK_ERROR();

        pState->ext.glDeleteFramebuffers(1, &pConv->framebuffer);
        GL_CHECK_ERROR();

        pConv->framebuffer = 0;
    }

    deleteShaderProgram(pState, &pConv->programUYVYFromRGB);
    deleteShaderProgram(pState, &pConv->programYUY2FromRGB);
    deleteShaderProgram(pState, &pConv->programUYVYToRGB);
    deleteShaderProgram(pState, &pConv->programYUY2ToRGB);

    if (pConv->vertexBuffer)
    {
        pState->ext.glDeleteBuffers(1, &pConv->vertexBuffer);
        GL_CHECK_ERROR();

        pConv->vertexBuffer = 0;
    }

    pConv->pState = 0;
}

/* Make use of a shader program for the current context and initialize the program uniforms. */
static void setShaderProgram(PVMSVGA3DSTATE pState,
                             ShaderProgram *pProgram,
                             uint32_t cWidth,
                             uint32_t cHeight)
{
    AssertReturnVoid(pState->idActiveContext == VMSVGA3D_SHARED_CTX_ID);

    /* Everything is done on the shared context. The pState and pContext are for GL_CHECK_ERROR macro. */
    PVMSVGA3DCONTEXT pContext = &pState->SharedCtx;

    pState->ext.glUseProgram(pProgram->program);
    GL_CHECK_ERROR();

    pState->ext.glUniform1i(pProgram->sSourceTex, 0);
    GL_CHECK_ERROR();

    float aTextureInfo[4];
    aTextureInfo[0] = (float)cWidth;
    aTextureInfo[1] = (float)cHeight;
    aTextureInfo[2] = 1.0f / (float)cWidth;  /* Pixel width in texture coords. */
    aTextureInfo[3] = 1.0f / (float)cHeight; /* Pixel height in texture coords. */

    pState->ext.glUniform4fv(pProgram->uTexInfo, 1, aTextureInfo);
    GL_CHECK_ERROR();
}

/* Attach the texture which must be used as the render target
 * to the GL_DRAW_FRAMEBUFFER as GL_COLOR_ATTACHMENT0.
 */
static void setRenderTarget(PVMSVGA3DSTATE pState,
                            GLuint texture,
                            uint32_t iMipmap)
{
    AssertReturnVoid(pState->idActiveContext == VMSVGA3D_SHARED_CTX_ID);

    PVMSVGA3DFORMATCONVERTER pConv = pState->pConv;
    AssertReturnVoid(pConv);

    /* Everything is done on the shared context. The pState and pContext are for GL_CHECK_ERROR macro. */
    PVMSVGA3DCONTEXT pContext = &pState->SharedCtx;

    pState->ext.glBindFramebuffer(GL_DRAW_FRAMEBUFFER, pConv->framebuffer);
    GL_CHECK_ERROR();

    glBindTexture(GL_TEXTURE_2D, texture);
    GL_CHECK_ERROR();

    pState->ext.glFramebufferTexture2D(GL_DRAW_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, texture, iMipmap);
    GL_CHECK_ERROR();

    glBindTexture(GL_TEXTURE_2D, 0);
    GL_CHECK_ERROR();

    Assert(pState->ext.glCheckFramebufferStatus(GL_FRAMEBUFFER) == GL_FRAMEBUFFER_COMPLETE);
}

/* Undo what setRenderTarget did. */
static void unsetRenderTarget(PVMSVGA3DSTATE pState,
                              GLuint texture)
{
    AssertReturnVoid(pState->idActiveContext == VMSVGA3D_SHARED_CTX_ID);

    /* Everything is done on the shared context. The pState and pContext are for GL_CHECK_ERROR macro. */
    PVMSVGA3DCONTEXT pContext = &pState->SharedCtx;

    RT_NOREF(texture);

    pState->ext.glFramebufferTexture2D(GL_DRAW_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, 0, 0);
    GL_CHECK_ERROR();

    pState->ext.glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0);
    GL_CHECK_ERROR();
}

/** Convert one texture to another.
 *
 * @param pState          The backend.
 * @param pCurrentContext The current context, which must be restored before returning.
 * @param pSurface        The surface which needs conversion.
 * @param iMipmap         The mipmap level which needs to be converted.
 * @param fToRGB          True for conversion from the intermediate texture emulated format
 *                        to the RGB format of the actual texture.
 *                        False for conversion from the actual RGB texture to the intermediate texture.
 */
static void doRender(PVMSVGA3DSTATE pState,
                     PVMSVGA3DCONTEXT pCurrentContext,
                     PVMSVGA3DSURFACE pSurface,
                     uint32_t iMipmap,
                     bool fToRGB)
{
    if (!fToRGB)
    {
        /** @todo Disable readback transfers for now. They cause crash in glDrawArrays with Mesa 19.2 after
         * a previously converted texture is deleted and another texture is being converted.
         * Such transfer are useless anyway for the emulated YUV formats and the guest should not need them usually.
         */
        return;
    }

    LogFunc(("formatConversion: idActiveContext %u, pConv %p, sid=%u, oglid=%u, oglidEmul=%u, mm=%u, %s\n",
             pState->idActiveContext, pState->pConv, pSurface->id, pSurface->oglId.texture, pSurface->idEmulated, iMipmap, fToRGB ? "ToRGB" : "FromRGB"));

    PVMSVGA3DFORMATCONVERTER pConv = pState->pConv;
    AssertReturnVoid(pConv);

    ShaderProgram *pProgram = NULL;
    GLuint sourceTexture = 0;
    GLuint targetTexture = 0;
    if (fToRGB)
    {
        if (pSurface->format == SVGA3D_YUY2)
        {
            pProgram = &pConv->programYUY2ToRGB;
        }
        else if (pSurface->format == SVGA3D_UYVY)
        {
            pProgram = &pConv->programUYVYToRGB;
        }
        sourceTexture = pSurface->idEmulated;
        targetTexture = pSurface->oglId.texture;
    }
    else
    {
        if (pSurface->format == SVGA3D_YUY2)
        {
            pProgram = &pConv->programYUY2FromRGB;
        }
        else if (pSurface->format == SVGA3D_UYVY)
        {
            pProgram = &pConv->programUYVYFromRGB;
        }
        sourceTexture = pSurface->oglId.texture;
        targetTexture = pSurface->idEmulated;
    }

    AssertReturnVoid(pProgram);

    PVMSVGA3DMIPMAPLEVEL pMipmapLevel;
    int rc = vmsvga3dMipmapLevel(pSurface, 0, iMipmap, &pMipmapLevel);
    AssertRCReturnVoid(rc);

    uint32_t const cWidth = pMipmapLevel->mipmapSize.width;
    uint32_t const cHeight = pMipmapLevel->mipmapSize.height;

    /* Use the shared context, where all textures are created. */
    PVMSVGA3DCONTEXT pContext = &pState->SharedCtx;
    VMSVGA3D_SET_CURRENT_CONTEXT(pState, pContext);

    setShaderProgram(pState, pProgram, cWidth, cHeight);

    setRenderTarget(pState, targetTexture, iMipmap);

    glViewport(0, 0, cWidth, cHeight);
    GL_CHECK_ERROR();

    glDisable(GL_DEPTH_TEST);
    GL_CHECK_ERROR();

    pState->ext.glActiveTexture(GL_TEXTURE0);
    GL_CHECK_ERROR();

    glBindTexture(GL_TEXTURE_2D, sourceTexture);
    GL_CHECK_ERROR();

    /* Make sure to set the simplest filter. Otherwise the conversion will not work. */
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
    GL_CHECK_ERROR();

    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
    GL_CHECK_ERROR();

    pState->ext.glBindBuffer(GL_ARRAY_BUFFER, pConv->vertexBuffer);
    GL_CHECK_ERROR();

    GLuint index;
    for (index = 0; index < RT_ELEMENTS(aVertexAttribs); ++index)
    {
        pState->ext.glEnableVertexAttribArray(index);
        GL_CHECK_ERROR();

        pState->ext.glVertexAttribPointer(index, aVertexAttribs[index].size, aVertexAttribs[index].type,
                                          aVertexAttribs[index].normalized, aVertexAttribs[index].stride,
                                          (const GLvoid *)(uintptr_t)aVertexAttribs[index].offset);
        GL_CHECK_ERROR();
    }

    glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
    GL_CHECK_ERROR();

    pState->ext.glBindBuffer(GL_ARRAY_BUFFER, 0);
    GL_CHECK_ERROR();

    glBindTexture(GL_TEXTURE_2D, 0);
    GL_CHECK_ERROR();

    unsetRenderTarget(pState, targetTexture);

    pState->ext.glUseProgram(0);
    GL_CHECK_ERROR();

    for (index = 0; index < RT_ELEMENTS(aVertexAttribs); ++index)
    {
        pState->ext.glDisableVertexAttribArray(index);
        GL_CHECK_ERROR();
    }

    /* Restore the current context. */
    VMSVGA3D_SET_CURRENT_CONTEXT(pState, pCurrentContext);
}

void FormatConvUpdateTexture(PVMSVGA3DSTATE pState,
                             PVMSVGA3DCONTEXT pCurrentContext,
                             PVMSVGA3DSURFACE pSurface,
                             uint32_t iMipmap)
{
    doRender(pState, pCurrentContext, pSurface, iMipmap, true);
}

void FormatConvReadTexture(PVMSVGA3DSTATE pState,
                           PVMSVGA3DCONTEXT pCurrentContext,
                           PVMSVGA3DSURFACE pSurface,
                           uint32_t iMipmap)
{
    doRender(pState, pCurrentContext, pSurface, iMipmap, false);
}

void vmsvga3dOnSharedContextDefine(PVMSVGA3DSTATE pState)
{
    /* Use the shared context, where all textures are created. */
    PVMSVGA3DCONTEXT pContext = &pState->SharedCtx;
    VMSVGA3D_SET_CURRENT_CONTEXT(pState, pContext);

    /*
     * Format conversion.
     */
    Assert(pState->pConv == NULL);

    pState->pConv = (VMSVGA3DFORMATCONVERTER *)RTMemAllocZ(sizeof(VMSVGA3DFORMATCONVERTER));
    AssertReturnVoid(pState->pConv);

    formatConversionInit(pState);
}

void vmsvga3dOnSharedContextDestroy(PVMSVGA3DSTATE pState)
{
    /* Use the shared context, where all textures are created. */
    PVMSVGA3DCONTEXT pContext = &pState->SharedCtx;
    VMSVGA3D_SET_CURRENT_CONTEXT(pState, pContext);

    if (pState->pConv)
    {
        formatConversionDestroy(pState);

        RTMemFree(pState->pConv);
        pState->pConv = NULL;
    }
}