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
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
|
/* $Id: DevVGA-SVGA3d.cpp $ */
/** @file
* DevSVGA3d - VMWare SVGA device, 3D parts - Common core code.
*/
/*
* Copyright (C) 2013-2019 Oracle Corporation
*
* This file is part of VirtualBox Open Source Edition (OSE), as
* available from http://www.virtualbox.org. This file is free software;
* you can redistribute it and/or modify it under the terms of the GNU
* General Public License (GPL) as published by the Free Software
* Foundation, in version 2 as it comes in the "COPYING" file of the
* VirtualBox OSE distribution. VirtualBox OSE is distributed in the
* hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
*/
/*********************************************************************************************************************************
* Header Files *
*********************************************************************************************************************************/
#define LOG_GROUP LOG_GROUP_DEV_VMSVGA
#include <VBox/vmm/pdmdev.h>
#include <iprt/errcore.h>
#include <VBox/log.h>
#include <iprt/assert.h>
#include <iprt/mem.h>
#include <VBox/vmm/pgm.h> /* required by DevVGA.h */
#include <VBoxVideo.h> /* required by DevVGA.h */
/* should go BEFORE any other DevVGA include to make all DevVGA.h config defines be visible */
#include "DevVGA.h"
#include "DevVGA-SVGA.h"
#include "DevVGA-SVGA3d.h"
#define VMSVGA3D_INCL_STRUCTURE_DESCRIPTORS
#include "DevVGA-SVGA3d-internal.h"
/**
* Implements the SVGA_3D_CMD_SURFACE_DEFINE_V2 and SVGA_3D_CMD_SURFACE_DEFINE
* commands (fifo).
*
* @returns VBox status code (currently ignored).
* @param pThis The VGA device instance data.
* @param sid The ID of the surface to (re-)define.
* @param surfaceFlags .
* @param format .
* @param face .
* @param multisampleCount .
* @param autogenFilter .
* @param cMipLevels .
* @param paMipLevelSizes .
*/
int vmsvga3dSurfaceDefine(PVGASTATE pThis, uint32_t sid, uint32_t surfaceFlags, SVGA3dSurfaceFormat format,
SVGA3dSurfaceFace face[SVGA3D_MAX_SURFACE_FACES], uint32_t multisampleCount,
SVGA3dTextureFilter autogenFilter, uint32_t cMipLevels, SVGA3dSize *paMipLevelSizes)
{
PVMSVGA3DSURFACE pSurface;
PVMSVGA3DSTATE pState = pThis->svga.p3dState;
AssertReturn(pState, VERR_NO_MEMORY);
Log(("vmsvga3dSurfaceDefine: sid=%x surfaceFlags=%x format=%s (%x) multiSampleCount=%d autogenFilter=%d, cMipLevels=%d size=(%d,%d,%d)\n",
sid, surfaceFlags, vmsvgaLookupEnum((int)format, &g_SVGA3dSurfaceFormat2String), format, multisampleCount, autogenFilter,
cMipLevels, paMipLevelSizes->width, paMipLevelSizes->height, paMipLevelSizes->depth));
AssertReturn(sid < SVGA3D_MAX_SURFACE_IDS, VERR_INVALID_PARAMETER);
AssertReturn(cMipLevels >= 1, VERR_INVALID_PARAMETER);
/* Number of faces (cFaces) is specified as the number of the first non-zero elements in the 'face' array.
* Since only plain surfaces (cFaces == 1) and cubemaps (cFaces == 6) are supported
* (see also SVGA3dCmdDefineSurface definition in svga3d_reg.h), we ignore anything else.
*/
uint32_t cRemainingMipLevels = cMipLevels;
uint32_t cFaces = 0;
for (uint32_t i = 0; i < SVGA3D_MAX_SURFACE_FACES; ++i)
{
if (face[i].numMipLevels == 0)
break;
/* All SVGA3dSurfaceFace structures must have the same value of numMipLevels field */
AssertReturn(face[i].numMipLevels == face[0].numMipLevels, VERR_INVALID_PARAMETER);
/* numMipLevels value can't be greater than the number of remaining elements in the paMipLevelSizes array. */
AssertReturn(face[i].numMipLevels <= cRemainingMipLevels, VERR_INVALID_PARAMETER);
cRemainingMipLevels -= face[i].numMipLevels;
++cFaces;
}
for (uint32_t i = cFaces; i < SVGA3D_MAX_SURFACE_FACES; ++i)
AssertReturn(face[i].numMipLevels == 0, VERR_INVALID_PARAMETER);
/* cFaces must be 6 for a cubemap and 1 otherwise. */
AssertReturn(cFaces == (uint32_t)((surfaceFlags & SVGA3D_SURFACE_CUBEMAP) ? 6 : 1), VERR_INVALID_PARAMETER);
/* Sum of face[i].numMipLevels must be equal to cMipLevels. */
AssertReturn(cRemainingMipLevels == 0, VERR_INVALID_PARAMETER);
if (sid >= pState->cSurfaces)
{
/* Grow the array. */
uint32_t cNew = RT_ALIGN(sid + 15, 16);
void *pvNew = RTMemRealloc(pState->papSurfaces, sizeof(pState->papSurfaces[0]) * cNew);
AssertReturn(pvNew, VERR_NO_MEMORY);
pState->papSurfaces = (PVMSVGA3DSURFACE *)pvNew;
while (pState->cSurfaces < cNew)
{
pSurface = (PVMSVGA3DSURFACE)RTMemAllocZ(sizeof(*pSurface));
AssertReturn(pSurface, VERR_NO_MEMORY);
pSurface->id = SVGA3D_INVALID_ID;
pState->papSurfaces[pState->cSurfaces++] = pSurface;
}
}
pSurface = pState->papSurfaces[sid];
/* If one already exists with this id, then destroy it now. */
if (pSurface->id != SVGA3D_INVALID_ID)
vmsvga3dSurfaceDestroy(pThis, sid);
RT_ZERO(*pSurface);
pSurface->id = sid;
#ifdef VMSVGA3D_OPENGL
pSurface->idWeakContextAssociation = SVGA3D_INVALID_ID;
pSurface->oglId.buffer = OPENGL_INVALID_ID;
#else /* VMSVGA3D_DIRECT3D */
pSurface->idAssociatedContext = SVGA3D_INVALID_ID;
pSurface->hSharedObject = NULL;
pSurface->pSharedObjectTree = NULL;
#endif
/* The surface type is sort of undefined now, even though the hints and format can help to clear that up.
* In some case we'll have to wait until the surface is used to create the D3D object.
*/
switch (format)
{
case SVGA3D_Z_D32:
case SVGA3D_Z_D16:
case SVGA3D_Z_D24S8:
case SVGA3D_Z_D15S1:
case SVGA3D_Z_D24X8:
case SVGA3D_Z_DF16:
case SVGA3D_Z_DF24:
case SVGA3D_Z_D24S8_INT:
surfaceFlags |= SVGA3D_SURFACE_HINT_DEPTHSTENCIL;
break;
/* Texture compression formats */
case SVGA3D_DXT1:
case SVGA3D_DXT2:
case SVGA3D_DXT3:
case SVGA3D_DXT4:
case SVGA3D_DXT5:
/* Bump-map formats */
case SVGA3D_BUMPU8V8:
case SVGA3D_BUMPL6V5U5:
case SVGA3D_BUMPX8L8V8U8:
case SVGA3D_BUMPL8V8U8:
case SVGA3D_V8U8:
case SVGA3D_Q8W8V8U8:
case SVGA3D_CxV8U8:
case SVGA3D_X8L8V8U8:
case SVGA3D_A2W10V10U10:
case SVGA3D_V16U16:
/* Typical render target formats; we should allow render target buffers to be used as textures. */
case SVGA3D_X8R8G8B8:
case SVGA3D_A8R8G8B8:
case SVGA3D_R5G6B5:
case SVGA3D_X1R5G5B5:
case SVGA3D_A1R5G5B5:
case SVGA3D_A4R4G4B4:
surfaceFlags |= SVGA3D_SURFACE_HINT_TEXTURE;
break;
case SVGA3D_LUMINANCE8:
case SVGA3D_LUMINANCE4_ALPHA4:
case SVGA3D_LUMINANCE16:
case SVGA3D_LUMINANCE8_ALPHA8:
case SVGA3D_ARGB_S10E5: /* 16-bit floating-point ARGB */
case SVGA3D_ARGB_S23E8: /* 32-bit floating-point ARGB */
case SVGA3D_A2R10G10B10:
case SVGA3D_ALPHA8:
case SVGA3D_R_S10E5:
case SVGA3D_R_S23E8:
case SVGA3D_RG_S10E5:
case SVGA3D_RG_S23E8:
case SVGA3D_G16R16:
case SVGA3D_A16B16G16R16:
case SVGA3D_UYVY:
case SVGA3D_YUY2:
case SVGA3D_NV12:
case SVGA3D_AYUV:
case SVGA3D_BC4_UNORM:
case SVGA3D_BC5_UNORM:
break;
/*
* Any surface can be used as a buffer object, but SVGA3D_BUFFER is
* the most efficient format to use when creating new surfaces
* expressly for index or vertex data.
*/
case SVGA3D_BUFFER:
break;
default:
break;
}
pSurface->surfaceFlags = surfaceFlags;
pSurface->format = format;
memcpy(pSurface->faces, face, sizeof(pSurface->faces));
pSurface->cFaces = cFaces;
pSurface->multiSampleCount = multisampleCount;
pSurface->autogenFilter = autogenFilter;
Assert(autogenFilter != SVGA3D_TEX_FILTER_FLATCUBIC);
Assert(autogenFilter != SVGA3D_TEX_FILTER_GAUSSIANCUBIC);
pSurface->cMipmapLevels = cMipLevels;
pSurface->pMipmapLevels = (PVMSVGA3DMIPMAPLEVEL)RTMemAllocZ(cMipLevels * sizeof(VMSVGA3DMIPMAPLEVEL));
AssertReturn(pSurface->pMipmapLevels, VERR_NO_MEMORY);
for (uint32_t i=0; i < cMipLevels; i++)
pSurface->pMipmapLevels[i].mipmapSize = paMipLevelSizes[i];
pSurface->cbBlock = vmsvga3dSurfaceFormatSize(format, &pSurface->cxBlock, &pSurface->cyBlock);
AssertReturn(pSurface->cbBlock, VERR_INVALID_PARAMETER);
#ifdef VMSVGA3D_DIRECT3D
/* Translate the format and usage flags to D3D. */
pSurface->formatD3D = vmsvga3dSurfaceFormat2D3D(format);
pSurface->multiSampleTypeD3D= vmsvga3dMultipeSampleCount2D3D(multisampleCount);
pSurface->fUsageD3D = 0;
if (surfaceFlags & SVGA3D_SURFACE_HINT_DYNAMIC)
pSurface->fUsageD3D |= D3DUSAGE_DYNAMIC;
if (surfaceFlags & SVGA3D_SURFACE_HINT_RENDERTARGET)
pSurface->fUsageD3D |= D3DUSAGE_RENDERTARGET;
if (surfaceFlags & SVGA3D_SURFACE_HINT_DEPTHSTENCIL)
pSurface->fUsageD3D |= D3DUSAGE_DEPTHSTENCIL;
if (surfaceFlags & SVGA3D_SURFACE_HINT_WRITEONLY)
pSurface->fUsageD3D |= D3DUSAGE_WRITEONLY;
if (surfaceFlags & SVGA3D_SURFACE_AUTOGENMIPMAPS)
pSurface->fUsageD3D |= D3DUSAGE_AUTOGENMIPMAP;
pSurface->enmD3DResType = VMSVGA3D_D3DRESTYPE_NONE;
/* pSurface->u.pSurface = NULL; */
/* pSurface->bounce.pTexture = NULL; */
#else
vmsvga3dSurfaceFormat2OGL(pSurface, format);
#endif
LogFunc(("surface hint(s):%s%s%s%s%s%s\n",
(surfaceFlags & SVGA3D_SURFACE_HINT_INDEXBUFFER) ? " SVGA3D_SURFACE_HINT_INDEXBUFFER" : "",
(surfaceFlags & SVGA3D_SURFACE_HINT_VERTEXBUFFER) ? " SVGA3D_SURFACE_HINT_VERTEXBUFFER" : "",
(surfaceFlags & SVGA3D_SURFACE_HINT_TEXTURE) ? " SVGA3D_SURFACE_HINT_TEXTURE" : "",
(surfaceFlags & SVGA3D_SURFACE_HINT_DEPTHSTENCIL) ? " SVGA3D_SURFACE_HINT_DEPTHSTENCIL" : "",
(surfaceFlags & SVGA3D_SURFACE_HINT_RENDERTARGET) ? " SVGA3D_SURFACE_HINT_RENDERTARGET" : "",
(surfaceFlags & SVGA3D_SURFACE_CUBEMAP) ? " SVGA3D_SURFACE_CUBEMAP" : ""
));
Assert(!VMSVGA3DSURFACE_HAS_HW_SURFACE(pSurface));
/* Allocate buffer to hold the surface data until we can move it into a D3D object */
uint32_t cbMemRemaining = SVGA3D_MAX_SURFACE_MEM_SIZE; /* Do not allow more than this for a surface. */
for (uint32_t i = 0; i < cMipLevels; ++i)
{
PVMSVGA3DMIPMAPLEVEL pMipmapLevel = &pSurface->pMipmapLevels[i];
LogFunc(("[%d] face %d mip level %d (%d,%d,%d) cbBlock=0x%x block %dx%d\n",
i, i / pSurface->faces[0].numMipLevels, i % pSurface->faces[0].numMipLevels,
pMipmapLevel->mipmapSize.width, pMipmapLevel->mipmapSize.height, pMipmapLevel->mipmapSize.depth,
pSurface->cbBlock, pSurface->cxBlock, pSurface->cyBlock));
uint32_t cBlocksX;
uint32_t cBlocksY;
if (RT_LIKELY(pSurface->cxBlock == 1 && pSurface->cyBlock == 1))
{
cBlocksX = pMipmapLevel->mipmapSize.width;
cBlocksY = pMipmapLevel->mipmapSize.height;
}
else
{
cBlocksX = pMipmapLevel->mipmapSize.width / pSurface->cxBlock;
if (pMipmapLevel->mipmapSize.width % pSurface->cxBlock)
++cBlocksX;
cBlocksY = pMipmapLevel->mipmapSize.height / pSurface->cyBlock;
if (pMipmapLevel->mipmapSize.height % pSurface->cyBlock)
++cBlocksY;
}
if ( cBlocksX == 0
|| cBlocksY == 0
|| pMipmapLevel->mipmapSize.depth == 0)
return VERR_INVALID_PARAMETER;
const uint32_t cMaxBlocksX = cbMemRemaining / pSurface->cbBlock;
if (cBlocksX > cMaxBlocksX)
return VERR_INVALID_PARAMETER;
const uint32_t cbSurfacePitch = pSurface->cbBlock * cBlocksX;
LogFunc(("cbSurfacePitch=0x%x\n", cbSurfacePitch));
const uint32_t cMaxBlocksY = cbMemRemaining / cbSurfacePitch;
if (cBlocksY > cMaxBlocksY)
return VERR_INVALID_PARAMETER;
const uint32_t cbSurfacePlane = cbSurfacePitch * cBlocksY;
const uint32_t cMaxDepth = cbMemRemaining / cbSurfacePlane;
if (pMipmapLevel->mipmapSize.depth > cMaxDepth)
return VERR_INVALID_PARAMETER;
const uint32_t cbSurface = cbSurfacePlane * pMipmapLevel->mipmapSize.depth;
pMipmapLevel->cBlocksX = cBlocksX;
pMipmapLevel->cBlocksY = cBlocksY;
pMipmapLevel->cbSurfacePitch = cbSurfacePitch;
pMipmapLevel->cbSurfacePlane = cbSurfacePlane;
pMipmapLevel->cbSurface = cbSurface;
pMipmapLevel->pSurfaceData = RTMemAllocZ(cbSurface);
AssertReturn(pMipmapLevel->pSurfaceData, VERR_NO_MEMORY);
cbMemRemaining -= cbSurface;
}
return VINF_SUCCESS;
}
/**
* Implements the SVGA_3D_CMD_SURFACE_DESTROY command (fifo).
*
* @returns VBox status code (currently ignored).
* @param pThis The VGA device instance data.
* @param sid The ID of the surface to destroy.
*/
int vmsvga3dSurfaceDestroy(PVGASTATE pThis, uint32_t sid)
{
PVMSVGA3DSTATE pState = pThis->svga.p3dState;
AssertReturn(pState, VERR_NO_MEMORY);
PVMSVGA3DSURFACE pSurface;
int rc = vmsvga3dSurfaceFromSid(pState, sid, &pSurface);
AssertRCReturn(rc, rc);
LogFunc(("sid=%x\n", sid));
/* Check all contexts if this surface is used as a render target or active texture. */
for (uint32_t cid = 0; cid < pState->cContexts; cid++)
{
PVMSVGA3DCONTEXT pContext = pState->papContexts[cid];
if (pContext->id == cid)
{
for (uint32_t i = 0; i < RT_ELEMENTS(pContext->aSidActiveTextures); ++i)
if (pContext->aSidActiveTextures[i] == sid)
pContext->aSidActiveTextures[i] = SVGA3D_INVALID_ID;
for (uint32_t i = 0; i < RT_ELEMENTS(pContext->state.aRenderTargets); ++i)
if (pContext->state.aRenderTargets[i] == sid)
pContext->state.aRenderTargets[i] = SVGA3D_INVALID_ID;
}
}
vmsvga3dBackSurfaceDestroy(pState, pSurface);
if (pSurface->pMipmapLevels)
{
for (uint32_t i = 0; i < pSurface->cMipmapLevels; ++i)
RTMemFree(pSurface->pMipmapLevels[i].pSurfaceData);
RTMemFree(pSurface->pMipmapLevels);
}
memset(pSurface, 0, sizeof(*pSurface));
pSurface->id = SVGA3D_INVALID_ID;
return VINF_SUCCESS;
}
/**
* Implements the SVGA_3D_CMD_SURFACE_STRETCHBLT command (fifo).
*
* @returns VBox status code (currently ignored).
* @param pThis The VGA device instance data.
* @param pDstSfcImg
* @param pDstBox
* @param pSrcSfcImg
* @param pSrcBox
* @param enmMode
*/
int vmsvga3dSurfaceStretchBlt(PVGASTATE pThis, SVGA3dSurfaceImageId const *pDstSfcImg, SVGA3dBox const *pDstBox,
SVGA3dSurfaceImageId const *pSrcSfcImg, SVGA3dBox const *pSrcBox, SVGA3dStretchBltMode enmMode)
{
PVMSVGA3DSTATE pState = pThis->svga.p3dState;
AssertReturn(pState, VERR_NO_MEMORY);
int rc;
uint32_t const sidSrc = pSrcSfcImg->sid;
PVMSVGA3DSURFACE pSrcSurface;
rc = vmsvga3dSurfaceFromSid(pState, sidSrc, &pSrcSurface);
AssertRCReturn(rc, rc);
uint32_t const sidDst = pDstSfcImg->sid;
PVMSVGA3DSURFACE pDstSurface;
rc = vmsvga3dSurfaceFromSid(pState, sidDst, &pDstSurface);
AssertRCReturn(rc, rc);
/* Can use faces[0].numMipLevels, because numMipLevels is the same for all faces. */
AssertReturn(pSrcSfcImg->face < pSrcSurface->cFaces, VERR_INVALID_PARAMETER);
AssertReturn(pSrcSfcImg->mipmap < pSrcSurface->faces[0].numMipLevels, VERR_INVALID_PARAMETER);
AssertReturn(pDstSfcImg->face < pDstSurface->cFaces, VERR_INVALID_PARAMETER);
AssertReturn(pDstSfcImg->mipmap < pDstSurface->faces[0].numMipLevels, VERR_INVALID_PARAMETER);
PVMSVGA3DCONTEXT pContext;
#ifdef VMSVGA3D_OPENGL
LogFunc(("src sid=%x (%d,%d)(%d,%d) dest sid=%x (%d,%d)(%d,%d) mode=%x\n",
sidSrc, pSrcBox->x, pSrcBox->y, pSrcBox->x + pSrcBox->w, pSrcBox->y + pSrcBox->h,
sidDst, pDstBox->x, pDstBox->y, pDstBox->x + pDstBox->w, pDstBox->y + pDstBox->h, enmMode));
pContext = &pState->SharedCtx;
VMSVGA3D_SET_CURRENT_CONTEXT(pState, pContext);
#else
LogFunc(("src sid=%x cid=%x (%d,%d)(%d,%d) dest sid=%x cid=%x (%d,%d)(%d,%d) mode=%x\n",
sidSrc, pSrcSurface->idAssociatedContext, pSrcBox->x, pSrcBox->y, pSrcBox->x + pSrcBox->w, pSrcBox->y + pSrcBox->h,
sidDst, pDstSurface->idAssociatedContext, pDstBox->x, pDstBox->y, pDstBox->x + pDstBox->w, pDstBox->y + pDstBox->h, enmMode));
uint32_t cid = pDstSurface->idAssociatedContext;
if (cid == SVGA3D_INVALID_ID)
cid = pSrcSurface->idAssociatedContext;
/* At least one of surfaces must be in hardware. */
AssertReturn(cid != SVGA3D_INVALID_ID, VERR_INVALID_PARAMETER);
rc = vmsvga3dContextFromCid(pState, cid, &pContext);
AssertRCReturn(rc, rc);
#endif
if (!VMSVGA3DSURFACE_HAS_HW_SURFACE(pSrcSurface))
{
/* Unknown surface type; turn it into a texture, which can be used for other purposes too. */
LogFunc(("unknown src sid=%x type=%d format=%d -> create texture\n", sidSrc, pSrcSurface->surfaceFlags, pSrcSurface->format));
rc = vmsvga3dBackCreateTexture(pState, pContext, pContext->id, pSrcSurface);
AssertRCReturn(rc, rc);
}
if (!VMSVGA3DSURFACE_HAS_HW_SURFACE(pDstSurface))
{
/* Unknown surface type; turn it into a texture, which can be used for other purposes too. */
LogFunc(("unknown dest sid=%x type=%d format=%d -> create texture\n", sidDst, pDstSurface->surfaceFlags, pDstSurface->format));
rc = vmsvga3dBackCreateTexture(pState, pContext, pContext->id, pDstSurface);
AssertRCReturn(rc, rc);
}
PVMSVGA3DMIPMAPLEVEL pSrcMipmapLevel;
rc = vmsvga3dMipmapLevel(pSrcSurface, pSrcSfcImg->face, pSrcSfcImg->mipmap, &pSrcMipmapLevel);
AssertRCReturn(rc, rc);
PVMSVGA3DMIPMAPLEVEL pDstMipmapLevel;
rc = vmsvga3dMipmapLevel(pDstSurface, pDstSfcImg->face, pDstSfcImg->mipmap, &pDstMipmapLevel);
AssertRCReturn(rc, rc);
SVGA3dBox clipSrcBox = *pSrcBox;
SVGA3dBox clipDstBox = *pDstBox;
vmsvgaClipBox(&pSrcMipmapLevel->mipmapSize, &clipSrcBox);
vmsvgaClipBox(&pDstMipmapLevel->mipmapSize, &clipDstBox);
return vmsvga3dBackSurfaceStretchBlt(pThis, pState,
pDstSurface, pDstSfcImg->face, pDstSfcImg->mipmap, &clipDstBox,
pSrcSurface, pSrcSfcImg->face, pSrcSfcImg->mipmap, &clipSrcBox,
enmMode, pContext);
}
/**
* Implements the SVGA_3D_CMD_SURFACE_DMA command (fifo).
*
* @returns VBox status code (currently ignored).
* @param pThis The VGA device instance data.
* @param guest .
* @param host .
* @param transfer .
* @param cCopyBoxes .
* @param paBoxes .
*/
int vmsvga3dSurfaceDMA(PVGASTATE pThis, SVGA3dGuestImage guest, SVGA3dSurfaceImageId host, SVGA3dTransferType transfer,
uint32_t cCopyBoxes, SVGA3dCopyBox *paBoxes)
{
PVMSVGA3DSTATE pState = pThis->svga.p3dState;
AssertReturn(pState, VERR_NO_MEMORY);
PVMSVGA3DSURFACE pSurface;
int rc = vmsvga3dSurfaceFromSid(pState, host.sid, &pSurface);
AssertRCReturn(rc, rc);
LogFunc(("%sguestptr gmr=%x offset=%x pitch=%x host sid=%x face=%d mipmap=%d transfer=%s cCopyBoxes=%d\n",
(pSurface->surfaceFlags & SVGA3D_SURFACE_HINT_TEXTURE) ? "TEXTURE " : "",
guest.ptr.gmrId, guest.ptr.offset, guest.pitch,
host.sid, host.face, host.mipmap, (transfer == SVGA3D_WRITE_HOST_VRAM) ? "READ" : "WRITE", cCopyBoxes));
PVMSVGA3DMIPMAPLEVEL pMipLevel;
rc = vmsvga3dMipmapLevel(pSurface, host.face, host.mipmap, &pMipLevel);
AssertRCReturn(rc, rc);
PVMSVGA3DCONTEXT pContext = NULL;
if (!VMSVGA3DSURFACE_HAS_HW_SURFACE(pSurface))
{
/*
* Not realized in host hardware/library yet, we have to work with
* the copy of the data we've got in VMSVGA3DMIMAPLEVEL::pSurfaceData.
*/
AssertReturn(pMipLevel->pSurfaceData, VERR_INTERNAL_ERROR);
}
else
{
#ifdef VMSVGA3D_DIRECT3D
/* Flush the drawing pipeline for this surface as it could be used in a shared context. */
vmsvga3dSurfaceFlush(pThis, pSurface);
#else /* VMSVGA3D_OPENGL */
pContext = &pState->SharedCtx;
VMSVGA3D_SET_CURRENT_CONTEXT(pState, pContext);
#endif
}
/* SVGA_3D_CMD_SURFACE_DMA:
* "define the 'source' in each copyBox as the guest image and the
* 'destination' as the host image, regardless of transfer direction."
*/
for (uint32_t i = 0; i < cCopyBoxes; ++i)
{
Log(("Copy box (%s) %d (%d,%d,%d)(%d,%d,%d) dest (%d,%d)\n",
VMSVGA3DSURFACE_HAS_HW_SURFACE(pSurface) ? "hw" : "mem",
i, paBoxes[i].srcx, paBoxes[i].srcy, paBoxes[i].srcz, paBoxes[i].w, paBoxes[i].h, paBoxes[i].d, paBoxes[i].x, paBoxes[i].y));
/* Apparently we're supposed to clip it (gmr test sample) */
/* The copybox's "dest" is coords in the host surface. Verify them against the surface's mipmap size. */
SVGA3dBox hostBox;
hostBox.x = paBoxes[i].x;
hostBox.y = paBoxes[i].y;
hostBox.z = paBoxes[i].z;
hostBox.w = paBoxes[i].w;
hostBox.h = paBoxes[i].h;
hostBox.d = paBoxes[i].d;
vmsvgaClipBox(&pMipLevel->mipmapSize, &hostBox);
if ( !hostBox.w
|| !hostBox.h
|| !hostBox.d)
{
Log(("Skip empty box\n"));
continue;
}
RT_UNTRUSTED_VALIDATED_FENCE();
/* Adjust the guest, i.e. "src", point.
* Do not try to verify them here because vmsvgaGMRTransfer takes care of this.
*/
uint32_t const srcx = paBoxes[i].srcx + (hostBox.x - paBoxes[i].x);
uint32_t const srcy = paBoxes[i].srcy + (hostBox.y - paBoxes[i].y);
uint32_t const srcz = paBoxes[i].srcz + (hostBox.z - paBoxes[i].z);
/* Calculate offsets of the image blocks for the transfer. */
uint32_t u32HostBlockX;
uint32_t u32HostBlockY;
uint32_t u32GuestBlockX;
uint32_t u32GuestBlockY;
uint32_t cBlocksX;
uint32_t cBlocksY;
if (RT_LIKELY(pSurface->cxBlock == 1 && pSurface->cyBlock == 1))
{
u32HostBlockX = hostBox.x;
u32HostBlockY = hostBox.y;
u32GuestBlockX = srcx;
u32GuestBlockY = srcy;
cBlocksX = hostBox.w;
cBlocksY = hostBox.h;
}
else
{
/* Pixels to blocks. */
u32HostBlockX = hostBox.x / pSurface->cxBlock;
u32HostBlockY = hostBox.y / pSurface->cyBlock;
Assert(u32HostBlockX * pSurface->cxBlock == hostBox.x);
Assert(u32HostBlockY * pSurface->cyBlock == hostBox.y);
u32GuestBlockX = srcx / pSurface->cxBlock;
u32GuestBlockY = srcy / pSurface->cyBlock;
Assert(u32GuestBlockX * pSurface->cxBlock == srcx);
Assert(u32GuestBlockY * pSurface->cyBlock == srcy);
cBlocksX = (hostBox.w + pSurface->cxBlock - 1) / pSurface->cxBlock;
cBlocksY = (hostBox.h + pSurface->cyBlock - 1) / pSurface->cyBlock;
}
uint32_t cbGuestPitch = guest.pitch;
if (cbGuestPitch == 0)
{
/* Host must "assume image is tightly packed". Our surfaces are. */
cbGuestPitch = pMipLevel->cbSurfacePitch;
}
else
{
/* vmsvgaGMRTransfer will verify the value, just check it is sane. */
AssertReturn(cbGuestPitch <= SVGA3D_MAX_SURFACE_MEM_SIZE, VERR_INVALID_PARAMETER);
RT_UNTRUSTED_VALIDATED_FENCE();
}
/* srcx, srcy and srcz values are used to calculate the guest offset.
* The offset will be verified by vmsvgaGMRTransfer, so just check for overflows here.
*/
AssertReturn(srcz < UINT32_MAX / pMipLevel->mipmapSize.height / cbGuestPitch, VERR_INVALID_PARAMETER);
AssertReturn(u32GuestBlockY < UINT32_MAX / cbGuestPitch, VERR_INVALID_PARAMETER);
AssertReturn(u32GuestBlockX < UINT32_MAX / pSurface->cbBlock, VERR_INVALID_PARAMETER);
RT_UNTRUSTED_VALIDATED_FENCE();
if (!VMSVGA3DSURFACE_HAS_HW_SURFACE(pSurface))
{
uint64_t uGuestOffset = u32GuestBlockX * pSurface->cbBlock +
u32GuestBlockY * cbGuestPitch +
srcz * pMipLevel->mipmapSize.height * cbGuestPitch;
AssertReturn(uGuestOffset < UINT32_MAX, VERR_INVALID_PARAMETER);
/* vmsvga3dSurfaceDefine verifies the surface dimensions and clipBox is within them. */
uint32_t uHostOffset = u32HostBlockX * pSurface->cbBlock +
u32HostBlockY * pMipLevel->cbSurfacePitch +
hostBox.z * pMipLevel->cbSurfacePlane;
AssertReturn(uHostOffset < pMipLevel->cbSurface, VERR_INTERNAL_ERROR);
for (uint32_t z = 0; z < hostBox.d; ++z)
{
rc = vmsvgaGMRTransfer(pThis,
transfer,
(uint8_t *)pMipLevel->pSurfaceData,
pMipLevel->cbSurface,
uHostOffset,
(int32_t)pMipLevel->cbSurfacePitch,
guest.ptr,
(uint32_t)uGuestOffset,
cbGuestPitch,
cBlocksX * pSurface->cbBlock,
cBlocksY);
AssertRC(rc);
Log4(("first line [z=%d]:\n%.*Rhxd\n",
z, pMipLevel->cbSurfacePitch, (uint8_t *)pMipLevel->pSurfaceData + uHostOffset));
uHostOffset += pMipLevel->cbSurfacePlane;
uGuestOffset += pMipLevel->mipmapSize.height * cbGuestPitch;
AssertReturn(uGuestOffset < UINT32_MAX, VERR_INVALID_PARAMETER);
}
}
else
{
SVGA3dCopyBox clipBox;
clipBox.x = hostBox.x;
clipBox.y = hostBox.y;
clipBox.z = hostBox.z;
clipBox.w = hostBox.w;
clipBox.h = hostBox.h;
clipBox.d = hostBox.d;
clipBox.srcx = srcx;
clipBox.srcy = srcy;
clipBox.srcz = srcz;
rc = vmsvga3dBackSurfaceDMACopyBox(pThis, pState, pSurface, pMipLevel, host.face, host.mipmap,
guest.ptr, cbGuestPitch, transfer,
&clipBox, pContext, rc, i);
AssertRC(rc);
}
}
if (!VMSVGA3DSURFACE_HAS_HW_SURFACE(pSurface))
{
pMipLevel->fDirty = true;
pSurface->fDirty = true;
}
return rc;
}
static int vmsvga3dQueryWriteResult(PVGASTATE pThis, SVGAGuestPtr guestResult, SVGA3dQueryState enmState, uint32_t u32Result)
{
SVGA3dQueryResult queryResult;
queryResult.totalSize = sizeof(queryResult); /* Set by guest before query is ended. */
queryResult.state = enmState; /* Set by host or guest. See SVGA3dQueryState. */
queryResult.result32 = u32Result;
int rc = vmsvgaGMRTransfer(pThis, SVGA3D_READ_HOST_VRAM,
(uint8_t *)&queryResult, sizeof(queryResult), 0, sizeof(queryResult),
guestResult, 0, sizeof(queryResult), sizeof(queryResult), 1);
AssertRC(rc);
return rc;
}
int vmsvga3dQueryBegin(PVGASTATE pThis, uint32_t cid, SVGA3dQueryType type)
{
PVMSVGA3DSTATE pState = pThis->svga.p3dState;
AssertReturn(pState, VERR_NO_MEMORY);
LogFunc(("cid=%x type=%d\n", cid, type));
PVMSVGA3DCONTEXT pContext;
int rc = vmsvga3dContextFromCid(pState, cid, &pContext);
AssertRCReturn(rc, rc);
if (type == SVGA3D_QUERYTYPE_OCCLUSION)
{
VMSVGA3DQUERY *p = &pContext->occlusion;
if (!VMSVGA3DQUERY_EXISTS(p))
{
/* Lazy creation of the query object. */
rc = vmsvga3dOcclusionQueryCreate(pState, pContext);
AssertRCReturn(rc, rc);
}
rc = vmsvga3dOcclusionQueryBegin(pState, pContext);
AssertRCReturn(rc, rc);
p->enmQueryState = VMSVGA3DQUERYSTATE_BUILDING;
p->u32QueryResult = 0;
return VINF_SUCCESS;
}
/* Nothing else for VGPU9. */
AssertFailedReturn(VERR_NOT_IMPLEMENTED);
}
int vmsvga3dQueryEnd(PVGASTATE pThis, uint32_t cid, SVGA3dQueryType type, SVGAGuestPtr guestResult)
{
RT_NOREF(guestResult);
PVMSVGA3DSTATE pState = pThis->svga.p3dState;
AssertReturn(pState, VERR_NO_MEMORY);
LogFunc(("cid=%x type=%d guestResult %d:0x%x\n", cid, type, guestResult.gmrId, guestResult.offset));
PVMSVGA3DCONTEXT pContext;
int rc = vmsvga3dContextFromCid(pState, cid, &pContext);
AssertRCReturn(rc, rc);
if (type == SVGA3D_QUERYTYPE_OCCLUSION)
{
VMSVGA3DQUERY *p = &pContext->occlusion;
Assert(p->enmQueryState == VMSVGA3DQUERYSTATE_BUILDING);
AssertMsgReturn(VMSVGA3DQUERY_EXISTS(p), ("Query is NULL\n"), VERR_INTERNAL_ERROR);
rc = vmsvga3dOcclusionQueryEnd(pState, pContext);
AssertRCReturn(rc, rc);
p->enmQueryState = VMSVGA3DQUERYSTATE_ISSUED;
/* Do not touch guestResult, because the guest will call WaitForQuery. */
return VINF_SUCCESS;
}
/* Nothing else for VGPU9. */
AssertFailedReturn(VERR_NOT_IMPLEMENTED);
}
int vmsvga3dQueryWait(PVGASTATE pThis, uint32_t cid, SVGA3dQueryType type, SVGAGuestPtr guestResult)
{
PVMSVGA3DSTATE pState = pThis->svga.p3dState;
AssertReturn(pState, VERR_NO_MEMORY);
LogFunc(("cid=%x type=%d guestResult GMR%d:0x%x\n", cid, type, guestResult.gmrId, guestResult.offset));
PVMSVGA3DCONTEXT pContext;
int rc = vmsvga3dContextFromCid(pState, cid, &pContext);
AssertRCReturn(rc, rc);
if (type == SVGA3D_QUERYTYPE_OCCLUSION)
{
VMSVGA3DQUERY *p = &pContext->occlusion;
if (VMSVGA3DQUERY_EXISTS(p))
{
if (p->enmQueryState == VMSVGA3DQUERYSTATE_ISSUED)
{
/* Only if not already in SIGNALED state,
* i.e. not a second read from the guest or after restoring saved state.
*/
uint32_t u32Pixels = 0;
rc = vmsvga3dOcclusionQueryGetData(pState, pContext, &u32Pixels);
if (RT_SUCCESS(rc))
{
p->enmQueryState = VMSVGA3DQUERYSTATE_SIGNALED;
p->u32QueryResult += u32Pixels; /* += because it might contain partial result from saved state. */
}
}
if (RT_SUCCESS(rc))
{
/* Return data to the guest. */
vmsvga3dQueryWriteResult(pThis, guestResult, SVGA3D_QUERYSTATE_SUCCEEDED, p->u32QueryResult);
return VINF_SUCCESS;
}
}
else
{
AssertMsgFailed(("GetData Query is NULL\n"));
}
rc = VERR_INTERNAL_ERROR;
}
else
{
rc = VERR_NOT_IMPLEMENTED;
}
vmsvga3dQueryWriteResult(pThis, guestResult, SVGA3D_QUERYSTATE_FAILED, 0);
AssertFailedReturn(rc);
}
int vmsvga3dSurfaceBlitToScreen(PVGASTATE pThis, uint32_t idDstScreen, SVGASignedRect destRect, SVGA3dSurfaceImageId src, SVGASignedRect srcRect, uint32_t cRects, SVGASignedRect *pRect)
{
/* Requires SVGA_FIFO_CAP_SCREEN_OBJECT support */
LogFunc(("dest=%d (%d,%d)(%d,%d) sid=%x (face=%d, mipmap=%d) (%d,%d)(%d,%d) cRects=%d\n",
idDstScreen, destRect.left, destRect.top, destRect.right, destRect.bottom, src.sid, src.face, src.mipmap,
srcRect.left, srcRect.top, srcRect.right, srcRect.bottom, cRects));
for (uint32_t i = 0; i < cRects; i++)
{
LogFunc(("clipping rect %d (%d,%d)(%d,%d)\n", i, pRect[i].left, pRect[i].top, pRect[i].right, pRect[i].bottom));
}
VMSVGASCREENOBJECT *pScreen = vmsvgaGetScreenObject(pThis, idDstScreen);
AssertReturn(pScreen, VERR_INTERNAL_ERROR);
AssertReturn(src.mipmap == 0 && src.face == 0, VERR_INVALID_PARAMETER);
/** @todo scaling */
AssertReturn(destRect.right - destRect.left == srcRect.right - srcRect.left && destRect.bottom - destRect.top == srcRect.bottom - srcRect.top, VERR_INVALID_PARAMETER);
SVGA3dCopyBox box;
SVGA3dGuestImage dest;
box.srcz = 0;
box.z = 0;
box.d = 1;
dest.ptr.gmrId = SVGA_GMR_FRAMEBUFFER;
dest.ptr.offset = pScreen->offVRAM;
dest.pitch = pScreen->cbPitch;
if (cRects == 0)
{
/* easy case; no clipping */
/* SVGA_3D_CMD_SURFACE_DMA:
* 'define the "source" in each copyBox as the guest image and the
* "destination" as the host image, regardless of transfer direction.'
*
* Since the BlitToScreen operation transfers from a host surface to the guest VRAM,
* it must set the copyBox "source" to the guest destination coords and
* the copyBox "destination" to the host surface source coords.
*/
/* Host image. */
box.x = srcRect.left;
box.y = srcRect.top;
box.w = srcRect.right - srcRect.left;
box.h = srcRect.bottom - srcRect.top;
/* Guest image. */
box.srcx = destRect.left;
box.srcy = destRect.top;
int rc = vmsvga3dSurfaceDMA(pThis, dest, src, SVGA3D_READ_HOST_VRAM, 1, &box);
AssertRCReturn(rc, rc);
/* Update the guest image, which is at box.src. */
vmsvgaUpdateScreen(pThis, pScreen, box.srcx, box.srcy, box.w, box.h);
}
else
{
/** @todo merge into one SurfaceDMA call */
for (uint32_t i = 0; i < cRects; i++)
{
/* "The clip rectangle coordinates are measured
* relative to the top-left corner of destRect."
* Therefore they are relative to the top-left corner of srcRect as well.
*/
/* Host image. See 'SVGA_3D_CMD_SURFACE_DMA:' comment in the 'if' branch. */
box.x = srcRect.left + pRect[i].left;
box.y = srcRect.top + pRect[i].top;
box.w = pRect[i].right - pRect[i].left;
box.h = pRect[i].bottom - pRect[i].top;
/* Guest image. The target screen memory is currently in the guest VRAM. */
box.srcx = destRect.left + pRect[i].left;
box.srcy = destRect.top + pRect[i].top;
int rc = vmsvga3dSurfaceDMA(pThis, dest, src, SVGA3D_READ_HOST_VRAM, 1, &box);
AssertRCReturn(rc, rc);
/* Update the guest image, which is at box.src. */
vmsvgaUpdateScreen(pThis, pScreen, box.srcx, box.srcy, box.w, box.h);
}
}
return VINF_SUCCESS;
}
int vmsvga3dCommandPresent(PVGASTATE pThis, uint32_t sid, uint32_t cRects, SVGA3dCopyRect *pRect)
{
/* Deprecated according to svga3d_reg.h. */
PVMSVGA3DSTATE pState = pThis->svga.p3dState;
AssertReturn(pState, VERR_NO_MEMORY);
PVMSVGA3DSURFACE pSurface;
int rc = vmsvga3dSurfaceFromSid(pState, sid, &pSurface);
AssertRCReturn(rc, rc);
/** @todo Detect screen from coords? Or split rect to screens? */
VMSVGASCREENOBJECT *pScreen = vmsvgaGetScreenObject(pThis, 0);
AssertReturn(pScreen, VERR_INTERNAL_ERROR);
/* If there are no recangles specified, just grab a screenful. */
SVGA3dCopyRect DummyRect;
if (cRects != 0)
{ /* likely */ }
else
{
/** @todo Find the usecase for this or check what the original device does.
* The original code was doing some scaling based on the surface
* size... */
AssertMsgFailed(("No rects to present. Who is doing that and what do they actually expect?\n"));
DummyRect.x = DummyRect.srcx = 0;
DummyRect.y = DummyRect.srcy = 0;
DummyRect.w = pScreen->cWidth;
DummyRect.h = pScreen->cHeight;
cRects = 1;
pRect = &DummyRect;
}
uint32_t i;
for (i = 0; i < cRects; ++i)
{
uint32_t idDstScreen = 0; /** @todo Use virtual coords: SVGA_ID_INVALID. */
SVGASignedRect destRect;
destRect.left = pRect[i].x;
destRect.top = pRect[i].y;
destRect.right = pRect[i].x + pRect[i].w;
destRect.bottom = pRect[i].y + pRect[i].h;
SVGA3dSurfaceImageId src;
src.sid = sid;
src.face = 0;
src.mipmap = 0;
SVGASignedRect srcRect;
srcRect.left = pRect[i].srcx;
srcRect.top = pRect[i].srcy;
srcRect.right = pRect[i].srcx + pRect[i].w;
srcRect.bottom = pRect[i].srcy + pRect[i].h;
/* Entire rect. */
rc = vmsvga3dSurfaceBlitToScreen(pThis, idDstScreen, destRect, src, srcRect, 0, NULL);
AssertRCReturn(rc, rc);
}
return VINF_SUCCESS;
}
|