VirtualBox

source: vbox/trunk/src/VBox/Devices/Graphics/DevVGA-SVGA3d.cpp@ 75149

Last change on this file since 75149 was 74261, checked in by vboxsync, 6 years ago

DevVGA-SVGA3d: corrected guest pitch; comments.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 36.8 KB
Line 
1/* $Id: DevVGA-SVGA3d.cpp 74261 2018-09-14 11:48:47Z vboxsync $ */
2/** @file
3 * DevSVGA3d - VMWare SVGA device, 3D parts - Common core code.
4 */
5
6/*
7 * Copyright (C) 2013-2017 Oracle Corporation
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.virtualbox.org. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 */
17
18
19/*********************************************************************************************************************************
20* Header Files *
21*********************************************************************************************************************************/
22#define LOG_GROUP LOG_GROUP_DEV_VMSVGA
23#include <VBox/vmm/pdmdev.h>
24#include <VBox/err.h>
25#include <VBox/log.h>
26
27#include <iprt/assert.h>
28#include <iprt/mem.h>
29
30#include <VBox/vmm/pgm.h> /* required by DevVGA.h */
31#include <VBoxVideo.h> /* required by DevVGA.h */
32
33/* should go BEFORE any other DevVGA include to make all DevVGA.h config defines be visible */
34#include "DevVGA.h"
35
36#include "DevVGA-SVGA.h"
37#include "DevVGA-SVGA3d.h"
38#define VMSVGA3D_INCL_STRUCTURE_DESCRIPTORS
39#include "DevVGA-SVGA3d-internal.h"
40
41
42
43/**
44 * Implements the SVGA_3D_CMD_SURFACE_DEFINE_V2 and SVGA_3D_CMD_SURFACE_DEFINE
45 * commands (fifo).
46 *
47 * @returns VBox status code (currently ignored).
48 * @param pThis The VGA device instance data.
49 * @param sid The ID of the surface to (re-)define.
50 * @param surfaceFlags .
51 * @param format .
52 * @param face .
53 * @param multisampleCount .
54 * @param autogenFilter .
55 * @param cMipLevels .
56 * @param paMipLevelSizes .
57 */
58int vmsvga3dSurfaceDefine(PVGASTATE pThis, uint32_t sid, uint32_t surfaceFlags, SVGA3dSurfaceFormat format,
59 SVGA3dSurfaceFace face[SVGA3D_MAX_SURFACE_FACES], uint32_t multisampleCount,
60 SVGA3dTextureFilter autogenFilter, uint32_t cMipLevels, SVGA3dSize *paMipLevelSizes)
61{
62 PVMSVGA3DSURFACE pSurface;
63 PVMSVGA3DSTATE pState = pThis->svga.p3dState;
64 AssertReturn(pState, VERR_NO_MEMORY);
65
66 Log(("vmsvga3dSurfaceDefine: sid=%x surfaceFlags=%x format=%s (%x) multiSampleCount=%d autogenFilter=%d, cMipLevels=%d size=(%d,%d,%d)\n",
67 sid, surfaceFlags, vmsvgaLookupEnum((int)format, &g_SVGA3dSurfaceFormat2String), format, multisampleCount, autogenFilter,
68 cMipLevels, paMipLevelSizes->width, paMipLevelSizes->height, paMipLevelSizes->depth));
69
70 AssertReturn(sid < SVGA3D_MAX_SURFACE_IDS, VERR_INVALID_PARAMETER);
71 AssertReturn(cMipLevels >= 1, VERR_INVALID_PARAMETER);
72
73 /* Number of faces (cFaces) is specified as the number of the first non-zero elements in the 'face' array.
74 * Since only plain surfaces (cFaces == 1) and cubemaps (cFaces == 6) are supported
75 * (see also SVGA3dCmdDefineSurface definition in svga3d_reg.h), we ignore anything else.
76 */
77 uint32_t cRemainingMipLevels = cMipLevels;
78 uint32_t cFaces = 0;
79 for (uint32_t i = 0; i < SVGA3D_MAX_SURFACE_FACES; ++i)
80 {
81 if (face[i].numMipLevels == 0)
82 break;
83
84 /* All SVGA3dSurfaceFace structures must have the same value of numMipLevels field */
85 AssertReturn(face[i].numMipLevels == face[0].numMipLevels, VERR_INVALID_PARAMETER);
86
87 /* numMipLevels value can't be greater than the number of remaining elements in the paMipLevelSizes array. */
88 AssertReturn(face[i].numMipLevels <= cRemainingMipLevels, VERR_INVALID_PARAMETER);
89 cRemainingMipLevels -= face[i].numMipLevels;
90
91 ++cFaces;
92 }
93 for (uint32_t i = cFaces; i < SVGA3D_MAX_SURFACE_FACES; ++i)
94 AssertReturn(face[i].numMipLevels == 0, VERR_INVALID_PARAMETER);
95
96 /* cFaces must be 6 for a cubemap and 1 otherwise. */
97 AssertReturn(cFaces == (uint32_t)((surfaceFlags & SVGA3D_SURFACE_CUBEMAP) ? 6 : 1), VERR_INVALID_PARAMETER);
98
99 /* Sum of face[i].numMipLevels must be equal to cMipLevels. */
100 AssertReturn(cRemainingMipLevels == 0, VERR_INVALID_PARAMETER);
101
102 if (sid >= pState->cSurfaces)
103 {
104 /* Grow the array. */
105 uint32_t cNew = RT_ALIGN(sid + 15, 16);
106 void *pvNew = RTMemRealloc(pState->papSurfaces, sizeof(pState->papSurfaces[0]) * cNew);
107 AssertReturn(pvNew, VERR_NO_MEMORY);
108 pState->papSurfaces = (PVMSVGA3DSURFACE *)pvNew;
109 while (pState->cSurfaces < cNew)
110 {
111 pSurface = (PVMSVGA3DSURFACE)RTMemAllocZ(sizeof(*pSurface));
112 AssertReturn(pSurface, VERR_NO_MEMORY);
113 pSurface->id = SVGA3D_INVALID_ID;
114 pState->papSurfaces[pState->cSurfaces++] = pSurface;
115 }
116 }
117 pSurface = pState->papSurfaces[sid];
118
119 /* If one already exists with this id, then destroy it now. */
120 if (pSurface->id != SVGA3D_INVALID_ID)
121 vmsvga3dSurfaceDestroy(pThis, sid);
122
123 RT_ZERO(*pSurface);
124 pSurface->id = sid;
125#ifdef VMSVGA3D_OPENGL
126 pSurface->idWeakContextAssociation = SVGA3D_INVALID_ID;
127 pSurface->oglId.buffer = OPENGL_INVALID_ID;
128#else /* VMSVGA3D_DIRECT3D */
129 pSurface->idAssociatedContext = SVGA3D_INVALID_ID;
130 pSurface->hSharedObject = NULL;
131 pSurface->pSharedObjectTree = NULL;
132#endif
133
134 /* The surface type is sort of undefined now, even though the hints and format can help to clear that up.
135 * In some case we'll have to wait until the surface is used to create the D3D object.
136 */
137 switch (format)
138 {
139 case SVGA3D_Z_D32:
140 case SVGA3D_Z_D16:
141 case SVGA3D_Z_D24S8:
142 case SVGA3D_Z_D15S1:
143 case SVGA3D_Z_D24X8:
144 case SVGA3D_Z_DF16:
145 case SVGA3D_Z_DF24:
146 case SVGA3D_Z_D24S8_INT:
147 surfaceFlags |= SVGA3D_SURFACE_HINT_DEPTHSTENCIL;
148 break;
149
150 /* Texture compression formats */
151 case SVGA3D_DXT1:
152 case SVGA3D_DXT2:
153 case SVGA3D_DXT3:
154 case SVGA3D_DXT4:
155 case SVGA3D_DXT5:
156 /* Bump-map formats */
157 case SVGA3D_BUMPU8V8:
158 case SVGA3D_BUMPL6V5U5:
159 case SVGA3D_BUMPX8L8V8U8:
160 case SVGA3D_BUMPL8V8U8:
161 case SVGA3D_V8U8:
162 case SVGA3D_Q8W8V8U8:
163 case SVGA3D_CxV8U8:
164 case SVGA3D_X8L8V8U8:
165 case SVGA3D_A2W10V10U10:
166 case SVGA3D_V16U16:
167 /* Typical render target formats; we should allow render target buffers to be used as textures. */
168 case SVGA3D_X8R8G8B8:
169 case SVGA3D_A8R8G8B8:
170 case SVGA3D_R5G6B5:
171 case SVGA3D_X1R5G5B5:
172 case SVGA3D_A1R5G5B5:
173 case SVGA3D_A4R4G4B4:
174 surfaceFlags |= SVGA3D_SURFACE_HINT_TEXTURE;
175 break;
176
177 case SVGA3D_LUMINANCE8:
178 case SVGA3D_LUMINANCE4_ALPHA4:
179 case SVGA3D_LUMINANCE16:
180 case SVGA3D_LUMINANCE8_ALPHA8:
181 case SVGA3D_ARGB_S10E5: /* 16-bit floating-point ARGB */
182 case SVGA3D_ARGB_S23E8: /* 32-bit floating-point ARGB */
183 case SVGA3D_A2R10G10B10:
184 case SVGA3D_ALPHA8:
185 case SVGA3D_R_S10E5:
186 case SVGA3D_R_S23E8:
187 case SVGA3D_RG_S10E5:
188 case SVGA3D_RG_S23E8:
189 case SVGA3D_G16R16:
190 case SVGA3D_A16B16G16R16:
191 case SVGA3D_UYVY:
192 case SVGA3D_YUY2:
193 case SVGA3D_NV12:
194 case SVGA3D_AYUV:
195 case SVGA3D_BC4_UNORM:
196 case SVGA3D_BC5_UNORM:
197 break;
198
199 /*
200 * Any surface can be used as a buffer object, but SVGA3D_BUFFER is
201 * the most efficient format to use when creating new surfaces
202 * expressly for index or vertex data.
203 */
204 case SVGA3D_BUFFER:
205 break;
206
207 default:
208 break;
209 }
210
211 pSurface->surfaceFlags = surfaceFlags;
212 pSurface->format = format;
213 memcpy(pSurface->faces, face, sizeof(pSurface->faces));
214 pSurface->cFaces = cFaces;
215 pSurface->multiSampleCount = multisampleCount;
216 pSurface->autogenFilter = autogenFilter;
217 Assert(autogenFilter != SVGA3D_TEX_FILTER_FLATCUBIC);
218 Assert(autogenFilter != SVGA3D_TEX_FILTER_GAUSSIANCUBIC);
219 pSurface->cMipmapLevels = cMipLevels;
220 pSurface->pMipmapLevels = (PVMSVGA3DMIPMAPLEVEL)RTMemAllocZ(cMipLevels * sizeof(VMSVGA3DMIPMAPLEVEL));
221 AssertReturn(pSurface->pMipmapLevels, VERR_NO_MEMORY);
222
223 for (uint32_t i=0; i < cMipLevels; i++)
224 pSurface->pMipmapLevels[i].mipmapSize = paMipLevelSizes[i];
225
226 pSurface->cbBlock = vmsvga3dSurfaceFormatSize(format, &pSurface->cxBlock, &pSurface->cyBlock);
227 AssertReturn(pSurface->cbBlock, VERR_INVALID_PARAMETER);
228
229#ifdef VMSVGA3D_DIRECT3D
230 /* Translate the format and usage flags to D3D. */
231 pSurface->formatD3D = vmsvga3dSurfaceFormat2D3D(format);
232 pSurface->multiSampleTypeD3D= vmsvga3dMultipeSampleCount2D3D(multisampleCount);
233 pSurface->fUsageD3D = 0;
234 if (surfaceFlags & SVGA3D_SURFACE_HINT_DYNAMIC)
235 pSurface->fUsageD3D |= D3DUSAGE_DYNAMIC;
236 if (surfaceFlags & SVGA3D_SURFACE_HINT_RENDERTARGET)
237 pSurface->fUsageD3D |= D3DUSAGE_RENDERTARGET;
238 if (surfaceFlags & SVGA3D_SURFACE_HINT_DEPTHSTENCIL)
239 pSurface->fUsageD3D |= D3DUSAGE_DEPTHSTENCIL;
240 if (surfaceFlags & SVGA3D_SURFACE_HINT_WRITEONLY)
241 pSurface->fUsageD3D |= D3DUSAGE_WRITEONLY;
242 if (surfaceFlags & SVGA3D_SURFACE_AUTOGENMIPMAPS)
243 pSurface->fUsageD3D |= D3DUSAGE_AUTOGENMIPMAP;
244 pSurface->enmD3DResType = VMSVGA3D_D3DRESTYPE_NONE;
245 /* pSurface->u.pSurface = NULL; */
246 /* pSurface->bounce.pTexture = NULL; */
247#else
248 vmsvga3dSurfaceFormat2OGL(pSurface, format);
249#endif
250
251 LogFunc(("surface hint(s):%s%s%s%s%s%s\n",
252 (surfaceFlags & SVGA3D_SURFACE_HINT_INDEXBUFFER) ? " SVGA3D_SURFACE_HINT_INDEXBUFFER" : "",
253 (surfaceFlags & SVGA3D_SURFACE_HINT_VERTEXBUFFER) ? " SVGA3D_SURFACE_HINT_VERTEXBUFFER" : "",
254 (surfaceFlags & SVGA3D_SURFACE_HINT_TEXTURE) ? " SVGA3D_SURFACE_HINT_TEXTURE" : "",
255 (surfaceFlags & SVGA3D_SURFACE_HINT_DEPTHSTENCIL) ? " SVGA3D_SURFACE_HINT_DEPTHSTENCIL" : "",
256 (surfaceFlags & SVGA3D_SURFACE_HINT_RENDERTARGET) ? " SVGA3D_SURFACE_HINT_RENDERTARGET" : "",
257 (surfaceFlags & SVGA3D_SURFACE_CUBEMAP) ? " SVGA3D_SURFACE_CUBEMAP" : ""
258 ));
259
260 Assert(!VMSVGA3DSURFACE_HAS_HW_SURFACE(pSurface));
261
262 /* Allocate buffer to hold the surface data until we can move it into a D3D object */
263 uint32_t cbMemRemaining = SVGA3D_MAX_SURFACE_MEM_SIZE; /* Do not allow more than this for a surface. */
264 for (uint32_t i = 0; i < cMipLevels; ++i)
265 {
266 PVMSVGA3DMIPMAPLEVEL pMipmapLevel = &pSurface->pMipmapLevels[i];
267 LogFunc(("[%d] face %d mip level %d (%d,%d,%d) cbBlock=0x%x block %dx%d\n",
268 i, i / pSurface->faces[0].numMipLevels, i % pSurface->faces[0].numMipLevels,
269 pMipmapLevel->mipmapSize.width, pMipmapLevel->mipmapSize.height, pMipmapLevel->mipmapSize.depth,
270 pSurface->cbBlock, pSurface->cxBlock, pSurface->cyBlock));
271
272 uint32_t cBlocksX;
273 uint32_t cBlocksY;
274 if (RT_LIKELY(pSurface->cxBlock == 1 && pSurface->cyBlock == 1))
275 {
276 cBlocksX = pMipmapLevel->mipmapSize.width;
277 cBlocksY = pMipmapLevel->mipmapSize.height;
278 }
279 else
280 {
281 cBlocksX = pMipmapLevel->mipmapSize.width / pSurface->cxBlock;
282 if (pMipmapLevel->mipmapSize.width % pSurface->cxBlock)
283 ++cBlocksX;
284 cBlocksY = pMipmapLevel->mipmapSize.height / pSurface->cyBlock;
285 if (pMipmapLevel->mipmapSize.height % pSurface->cyBlock)
286 ++cBlocksY;
287 }
288
289 if ( cBlocksX == 0
290 || cBlocksY == 0
291 || pMipmapLevel->mipmapSize.depth == 0)
292 return VERR_INVALID_PARAMETER;
293
294 const uint32_t cMaxBlocksX = cbMemRemaining / pSurface->cbBlock;
295 if (cBlocksX > cMaxBlocksX)
296 return VERR_INVALID_PARAMETER;
297 const uint32_t cbSurfacePitch = pSurface->cbBlock * cBlocksX;
298 LogFunc(("cbSurfacePitch=0x%x\n", cbSurfacePitch));
299
300 const uint32_t cMaxBlocksY = cbMemRemaining / cbSurfacePitch;
301 if (cBlocksY > cMaxBlocksY)
302 return VERR_INVALID_PARAMETER;
303 const uint32_t cbSurfacePlane = cbSurfacePitch * cBlocksY;
304
305 const uint32_t cMaxDepth = cbMemRemaining / cbSurfacePlane;
306 if (pMipmapLevel->mipmapSize.depth > cMaxDepth)
307 return VERR_INVALID_PARAMETER;
308 const uint32_t cbSurface = cbSurfacePlane * pMipmapLevel->mipmapSize.depth;
309
310 pMipmapLevel->cBlocksX = cBlocksX;
311 pMipmapLevel->cBlocksY = cBlocksY;
312 pMipmapLevel->cbSurfacePitch = cbSurfacePitch;
313 pMipmapLevel->cbSurfacePlane = cbSurfacePlane;
314 pMipmapLevel->cbSurface = cbSurface;
315 pMipmapLevel->pSurfaceData = RTMemAllocZ(cbSurface);
316 AssertReturn(pMipmapLevel->pSurfaceData, VERR_NO_MEMORY);
317
318 cbMemRemaining -= cbSurface;
319 }
320 return VINF_SUCCESS;
321}
322
323
324/**
325 * Implements the SVGA_3D_CMD_SURFACE_DESTROY command (fifo).
326 *
327 * @returns VBox status code (currently ignored).
328 * @param pThis The VGA device instance data.
329 * @param sid The ID of the surface to destroy.
330 */
331int vmsvga3dSurfaceDestroy(PVGASTATE pThis, uint32_t sid)
332{
333 PVMSVGA3DSTATE pState = pThis->svga.p3dState;
334 AssertReturn(pState, VERR_NO_MEMORY);
335
336 PVMSVGA3DSURFACE pSurface;
337 int rc = vmsvga3dSurfaceFromSid(pState, sid, &pSurface);
338 AssertRCReturn(rc, rc);
339
340 LogFunc(("sid=%x\n", sid));
341
342 /* Check all contexts if this surface is used as a render target or active texture. */
343 for (uint32_t cid = 0; cid < pState->cContexts; cid++)
344 {
345 PVMSVGA3DCONTEXT pContext = pState->papContexts[cid];
346 if (pContext->id == cid)
347 {
348 for (uint32_t i = 0; i < RT_ELEMENTS(pContext->aSidActiveTextures); ++i)
349 if (pContext->aSidActiveTextures[i] == sid)
350 pContext->aSidActiveTextures[i] = SVGA3D_INVALID_ID;
351 for (uint32_t i = 0; i < RT_ELEMENTS(pContext->state.aRenderTargets); ++i)
352 if (pContext->state.aRenderTargets[i] == sid)
353 pContext->state.aRenderTargets[i] = SVGA3D_INVALID_ID;
354 }
355 }
356
357 vmsvga3dBackSurfaceDestroy(pState, pSurface);
358
359 if (pSurface->pMipmapLevels)
360 {
361 for (uint32_t i = 0; i < pSurface->cMipmapLevels; ++i)
362 RTMemFree(pSurface->pMipmapLevels[i].pSurfaceData);
363 RTMemFree(pSurface->pMipmapLevels);
364 }
365
366 memset(pSurface, 0, sizeof(*pSurface));
367 pSurface->id = SVGA3D_INVALID_ID;
368
369 return VINF_SUCCESS;
370}
371
372
373/**
374 * Implements the SVGA_3D_CMD_SURFACE_STRETCHBLT command (fifo).
375 *
376 * @returns VBox status code (currently ignored).
377 * @param pThis The VGA device instance data.
378 * @param pDstSfcImg
379 * @param pDstBox
380 * @param pSrcSfcImg
381 * @param pSrcBox
382 * @param enmMode
383 */
384int vmsvga3dSurfaceStretchBlt(PVGASTATE pThis, SVGA3dSurfaceImageId const *pDstSfcImg, SVGA3dBox const *pDstBox,
385 SVGA3dSurfaceImageId const *pSrcSfcImg, SVGA3dBox const *pSrcBox, SVGA3dStretchBltMode enmMode)
386{
387 PVMSVGA3DSTATE pState = pThis->svga.p3dState;
388 AssertReturn(pState, VERR_NO_MEMORY);
389
390 int rc;
391
392 uint32_t const sidSrc = pSrcSfcImg->sid;
393 PVMSVGA3DSURFACE pSrcSurface;
394 rc = vmsvga3dSurfaceFromSid(pState, sidSrc, &pSrcSurface);
395 AssertRCReturn(rc, rc);
396
397 uint32_t const sidDst = pDstSfcImg->sid;
398 PVMSVGA3DSURFACE pDstSurface;
399 rc = vmsvga3dSurfaceFromSid(pState, sidDst, &pDstSurface);
400 AssertRCReturn(rc, rc);
401
402 /* Can use faces[0].numMipLevels, because numMipLevels is the same for all faces. */
403 AssertReturn(pSrcSfcImg->face < pSrcSurface->cFaces, VERR_INVALID_PARAMETER);
404 AssertReturn(pSrcSfcImg->mipmap < pSrcSurface->faces[0].numMipLevels, VERR_INVALID_PARAMETER);
405 AssertReturn(pDstSfcImg->face < pDstSurface->cFaces, VERR_INVALID_PARAMETER);
406 AssertReturn(pDstSfcImg->mipmap < pDstSurface->faces[0].numMipLevels, VERR_INVALID_PARAMETER);
407
408 PVMSVGA3DCONTEXT pContext;
409#ifdef VMSVGA3D_OPENGL
410 LogFunc(("src sid=%x (%d,%d)(%d,%d) dest sid=%x (%d,%d)(%d,%d) mode=%x\n",
411 sidSrc, pSrcBox->x, pSrcBox->y, pSrcBox->x + pSrcBox->w, pSrcBox->y + pSrcBox->h,
412 sidDst, pDstBox->x, pDstBox->y, pDstBox->x + pDstBox->w, pDstBox->y + pDstBox->h, enmMode));
413 pContext = &pState->SharedCtx;
414 VMSVGA3D_SET_CURRENT_CONTEXT(pState, pContext);
415#else
416 LogFunc(("src sid=%x cid=%x (%d,%d)(%d,%d) dest sid=%x cid=%x (%d,%d)(%d,%d) mode=%x\n",
417 sidSrc, pSrcSurface->idAssociatedContext, pSrcBox->x, pSrcBox->y, pSrcBox->x + pSrcBox->w, pSrcBox->y + pSrcBox->h,
418 sidDst, pDstSurface->idAssociatedContext, pDstBox->x, pDstBox->y, pDstBox->x + pDstBox->w, pDstBox->y + pDstBox->h, enmMode));
419
420 uint32_t cid = pDstSurface->idAssociatedContext;
421 if (cid == SVGA3D_INVALID_ID)
422 cid = pSrcSurface->idAssociatedContext;
423
424 /* At least one of surfaces must be in hardware. */
425 AssertReturn(cid != SVGA3D_INVALID_ID, VERR_INVALID_PARAMETER);
426
427 rc = vmsvga3dContextFromCid(pState, cid, &pContext);
428 AssertRCReturn(rc, rc);
429#endif
430
431 if (!VMSVGA3DSURFACE_HAS_HW_SURFACE(pSrcSurface))
432 {
433 /* Unknown surface type; turn it into a texture, which can be used for other purposes too. */
434 LogFunc(("unknown src sid=%x type=%d format=%d -> create texture\n", sidSrc, pSrcSurface->surfaceFlags, pSrcSurface->format));
435 rc = vmsvga3dBackCreateTexture(pState, pContext, pContext->id, pSrcSurface);
436 AssertRCReturn(rc, rc);
437 }
438
439 if (!VMSVGA3DSURFACE_HAS_HW_SURFACE(pDstSurface))
440 {
441 /* Unknown surface type; turn it into a texture, which can be used for other purposes too. */
442 LogFunc(("unknown dest sid=%x type=%d format=%d -> create texture\n", sidDst, pDstSurface->surfaceFlags, pDstSurface->format));
443 rc = vmsvga3dBackCreateTexture(pState, pContext, pContext->id, pDstSurface);
444 AssertRCReturn(rc, rc);
445 }
446
447 PVMSVGA3DMIPMAPLEVEL pSrcMipmapLevel;
448 rc = vmsvga3dMipmapLevel(pSrcSurface, pSrcSfcImg->face, pSrcSfcImg->mipmap, &pSrcMipmapLevel);
449 AssertRCReturn(rc, rc);
450
451 PVMSVGA3DMIPMAPLEVEL pDstMipmapLevel;
452 rc = vmsvga3dMipmapLevel(pDstSurface, pDstSfcImg->face, pDstSfcImg->mipmap, &pDstMipmapLevel);
453 AssertRCReturn(rc, rc);
454
455 SVGA3dBox clipSrcBox = *pSrcBox;
456 SVGA3dBox clipDstBox = *pDstBox;
457 vmsvgaClipBox(&pSrcMipmapLevel->mipmapSize, &clipSrcBox);
458 vmsvgaClipBox(&pDstMipmapLevel->mipmapSize, &clipDstBox);
459
460 return vmsvga3dBackSurfaceStretchBlt(pThis, pState,
461 pDstSurface, pDstSfcImg->face, pDstSfcImg->mipmap, &clipDstBox,
462 pSrcSurface, pSrcSfcImg->face, pSrcSfcImg->mipmap, &clipSrcBox,
463 enmMode, pContext);
464}
465
466/**
467 * Implements the SVGA_3D_CMD_SURFACE_DMA command (fifo).
468 *
469 * @returns VBox status code (currently ignored).
470 * @param pThis The VGA device instance data.
471 * @param guest .
472 * @param host .
473 * @param transfer .
474 * @param cCopyBoxes .
475 * @param paBoxes .
476 */
477int vmsvga3dSurfaceDMA(PVGASTATE pThis, SVGA3dGuestImage guest, SVGA3dSurfaceImageId host, SVGA3dTransferType transfer,
478 uint32_t cCopyBoxes, SVGA3dCopyBox *paBoxes)
479{
480 PVMSVGA3DSTATE pState = pThis->svga.p3dState;
481 AssertReturn(pState, VERR_NO_MEMORY);
482
483 PVMSVGA3DSURFACE pSurface;
484 int rc = vmsvga3dSurfaceFromSid(pState, host.sid, &pSurface);
485 AssertRCReturn(rc, rc);
486
487 LogFunc(("%sguestptr gmr=%x offset=%x pitch=%x host sid=%x face=%d mipmap=%d transfer=%s cCopyBoxes=%d\n",
488 (pSurface->surfaceFlags & SVGA3D_SURFACE_HINT_TEXTURE) ? "TEXTURE " : "",
489 guest.ptr.gmrId, guest.ptr.offset, guest.pitch,
490 host.sid, host.face, host.mipmap, (transfer == SVGA3D_WRITE_HOST_VRAM) ? "READ" : "WRITE", cCopyBoxes));
491
492 PVMSVGA3DMIPMAPLEVEL pMipLevel;
493 rc = vmsvga3dMipmapLevel(pSurface, host.face, host.mipmap, &pMipLevel);
494 AssertRCReturn(rc, rc);
495
496 PVMSVGA3DCONTEXT pContext = NULL;
497 if (!VMSVGA3DSURFACE_HAS_HW_SURFACE(pSurface))
498 {
499 /*
500 * Not realized in host hardware/library yet, we have to work with
501 * the copy of the data we've got in VMSVGA3DMIMAPLEVEL::pSurfaceData.
502 */
503 AssertReturn(pMipLevel->pSurfaceData, VERR_INTERNAL_ERROR);
504 }
505 else
506 {
507#ifdef VMSVGA3D_DIRECT3D
508 /* Flush the drawing pipeline for this surface as it could be used in a shared context. */
509 vmsvga3dSurfaceFlush(pThis, pSurface);
510
511#else /* VMSVGA3D_OPENGL */
512 pContext = &pState->SharedCtx;
513 VMSVGA3D_SET_CURRENT_CONTEXT(pState, pContext);
514#endif
515 }
516
517 /* SVGA_3D_CMD_SURFACE_DMA:
518 * "define the 'source' in each copyBox as the guest image and the
519 * 'destination' as the host image, regardless of transfer direction."
520 */
521 for (uint32_t i = 0; i < cCopyBoxes; ++i)
522 {
523 Log(("Copy box (%s) %d (%d,%d,%d)(%d,%d,%d) dest (%d,%d)\n",
524 VMSVGA3DSURFACE_HAS_HW_SURFACE(pSurface) ? "hw" : "mem",
525 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));
526
527 /* Apparently we're supposed to clip it (gmr test sample) */
528
529 /* The copybox's "dest" is coords in the host surface. Verify them against the surface's mipmap size. */
530 SVGA3dBox hostBox;
531 hostBox.x = paBoxes[i].x;
532 hostBox.y = paBoxes[i].y;
533 hostBox.z = paBoxes[i].z;
534 hostBox.w = paBoxes[i].w;
535 hostBox.h = paBoxes[i].h;
536 hostBox.d = paBoxes[i].d;
537 vmsvgaClipBox(&pMipLevel->mipmapSize, &hostBox);
538
539 if ( !hostBox.w
540 || !hostBox.h
541 || !hostBox.d)
542 {
543 Log(("Skip empty box\n"));
544 continue;
545 }
546 RT_UNTRUSTED_VALIDATED_FENCE();
547
548 /* Adjust the guest, i.e. "src", point.
549 * Do not try to verify them here because vmsvgaGMRTransfer takes care of this.
550 */
551 uint32_t const srcx = paBoxes[i].srcx + (hostBox.x - paBoxes[i].x);
552 uint32_t const srcy = paBoxes[i].srcy + (hostBox.y - paBoxes[i].y);
553 uint32_t const srcz = paBoxes[i].srcz + (hostBox.z - paBoxes[i].z);
554
555 /* Calculate offsets of the image blocks for the transfer. */
556 uint32_t u32HostBlockX;
557 uint32_t u32HostBlockY;
558 uint32_t u32GuestBlockX;
559 uint32_t u32GuestBlockY;
560 uint32_t cBlocksX;
561 uint32_t cBlocksY;
562 if (RT_LIKELY(pSurface->cxBlock == 1 && pSurface->cyBlock == 1))
563 {
564 u32HostBlockX = hostBox.x;
565 u32HostBlockY = hostBox.y;
566
567 u32GuestBlockX = srcx;
568 u32GuestBlockY = srcy;
569
570 cBlocksX = hostBox.w;
571 cBlocksY = hostBox.h;
572 }
573 else
574 {
575 /* Pixels to blocks. */
576 u32HostBlockX = hostBox.x / pSurface->cxBlock;
577 u32HostBlockY = hostBox.y / pSurface->cyBlock;
578 Assert(u32HostBlockX * pSurface->cxBlock == hostBox.x);
579 Assert(u32HostBlockY * pSurface->cyBlock == hostBox.y);
580
581 u32GuestBlockX = srcx / pSurface->cxBlock;
582 u32GuestBlockY = srcy / pSurface->cyBlock;
583 Assert(u32GuestBlockX * pSurface->cxBlock == srcx);
584 Assert(u32GuestBlockY * pSurface->cyBlock == srcy);
585
586 cBlocksX = (hostBox.w + pSurface->cxBlock - 1) / pSurface->cxBlock;
587 cBlocksY = (hostBox.h + pSurface->cyBlock - 1) / pSurface->cyBlock;
588 }
589
590 uint32_t cbGuestPitch = guest.pitch;
591 if (cbGuestPitch == 0)
592 {
593 /* Host must "assume image is tightly packed". Our surfaces are. */
594 cbGuestPitch = pMipLevel->cbSurfacePitch;
595 }
596 else
597 {
598 /* vmsvgaGMRTransfer will verify the value, just check it is sane. */
599 AssertReturn(cbGuestPitch <= SVGA3D_MAX_SURFACE_MEM_SIZE, VERR_INVALID_PARAMETER);
600 RT_UNTRUSTED_VALIDATED_FENCE();
601 }
602
603 /* srcx, srcy and srcz values are used to calculate the guest offset.
604 * The offset will be verified by vmsvgaGMRTransfer, so just check for overflows here.
605 */
606 AssertReturn(srcz < UINT32_MAX / pMipLevel->mipmapSize.height / cbGuestPitch, VERR_INVALID_PARAMETER);
607 AssertReturn(u32GuestBlockY < UINT32_MAX / cbGuestPitch, VERR_INVALID_PARAMETER);
608 AssertReturn(u32GuestBlockX < UINT32_MAX / pSurface->cbBlock, VERR_INVALID_PARAMETER);
609 RT_UNTRUSTED_VALIDATED_FENCE();
610
611 if (!VMSVGA3DSURFACE_HAS_HW_SURFACE(pSurface))
612 {
613 uint64_t uGuestOffset = u32GuestBlockX * pSurface->cbBlock +
614 u32GuestBlockY * cbGuestPitch +
615 srcz * pMipLevel->mipmapSize.height * cbGuestPitch;
616 AssertReturn(uGuestOffset < UINT32_MAX, VERR_INVALID_PARAMETER);
617
618 /* vmsvga3dSurfaceDefine verifies the surface dimensions and clipBox is within them. */
619 uint32_t uHostOffset = u32HostBlockX * pSurface->cbBlock +
620 u32HostBlockY * pMipLevel->cbSurfacePitch +
621 hostBox.z * pMipLevel->cbSurfacePlane;
622 AssertReturn(uHostOffset < pMipLevel->cbSurface, VERR_INTERNAL_ERROR);
623
624 for (uint32_t z = 0; z < hostBox.d; ++z)
625 {
626 rc = vmsvgaGMRTransfer(pThis,
627 transfer,
628 (uint8_t *)pMipLevel->pSurfaceData,
629 pMipLevel->cbSurface,
630 uHostOffset,
631 (int32_t)pMipLevel->cbSurfacePitch,
632 guest.ptr,
633 (uint32_t)uGuestOffset,
634 cbGuestPitch,
635 cBlocksX * pSurface->cbBlock,
636 cBlocksY);
637 AssertRC(rc);
638
639 Log4(("first line [z=%d]:\n%.*Rhxd\n",
640 z, pMipLevel->cbSurfacePitch, (uint8_t *)pMipLevel->pSurfaceData + uHostOffset));
641
642 uHostOffset += pMipLevel->cbSurfacePlane;
643 uGuestOffset += pMipLevel->mipmapSize.height * cbGuestPitch;
644 AssertReturn(uGuestOffset < UINT32_MAX, VERR_INVALID_PARAMETER);
645 }
646 }
647 else
648 {
649 SVGA3dCopyBox clipBox;
650 clipBox.x = hostBox.x;
651 clipBox.y = hostBox.y;
652 clipBox.z = hostBox.z;
653 clipBox.w = hostBox.w;
654 clipBox.h = hostBox.h;
655 clipBox.d = hostBox.d;
656 clipBox.srcx = srcx;
657 clipBox.srcy = srcy;
658 clipBox.srcz = srcz;
659 rc = vmsvga3dBackSurfaceDMACopyBox(pThis, pState, pSurface, pMipLevel, host.face, host.mipmap,
660 guest.ptr, cbGuestPitch, transfer,
661 &clipBox, pContext, rc, i);
662 AssertRC(rc);
663 }
664 }
665
666 if (!VMSVGA3DSURFACE_HAS_HW_SURFACE(pSurface))
667 {
668 pMipLevel->fDirty = true;
669 pSurface->fDirty = true;
670 }
671
672 return rc;
673}
674
675static int vmsvga3dQueryWriteResult(PVGASTATE pThis, SVGAGuestPtr guestResult, SVGA3dQueryState enmState, uint32_t u32Result)
676{
677 SVGA3dQueryResult queryResult;
678 queryResult.totalSize = sizeof(queryResult); /* Set by guest before query is ended. */
679 queryResult.state = enmState; /* Set by host or guest. See SVGA3dQueryState. */
680 queryResult.result32 = u32Result;
681
682 int rc = vmsvgaGMRTransfer(pThis, SVGA3D_READ_HOST_VRAM,
683 (uint8_t *)&queryResult, sizeof(queryResult), 0, sizeof(queryResult),
684 guestResult, 0, sizeof(queryResult), sizeof(queryResult), 1);
685 AssertRC(rc);
686 return rc;
687}
688
689int vmsvga3dQueryBegin(PVGASTATE pThis, uint32_t cid, SVGA3dQueryType type)
690{
691 PVMSVGA3DSTATE pState = pThis->svga.p3dState;
692 AssertReturn(pState, VERR_NO_MEMORY);
693
694 LogFunc(("cid=%x type=%d\n", cid, type));
695
696 PVMSVGA3DCONTEXT pContext;
697 int rc = vmsvga3dContextFromCid(pState, cid, &pContext);
698 AssertRCReturn(rc, rc);
699
700 if (type == SVGA3D_QUERYTYPE_OCCLUSION)
701 {
702 VMSVGA3DQUERY *p = &pContext->occlusion;
703 if (!VMSVGA3DQUERY_EXISTS(p))
704 {
705 /* Lazy creation of the query object. */
706 rc = vmsvga3dOcclusionQueryCreate(pState, pContext);
707 AssertRCReturn(rc, rc);
708 }
709
710 rc = vmsvga3dOcclusionQueryBegin(pState, pContext);
711 AssertRCReturn(rc, rc);
712
713 p->enmQueryState = VMSVGA3DQUERYSTATE_BUILDING;
714 p->u32QueryResult = 0;
715
716 return VINF_SUCCESS;
717 }
718
719 /* Nothing else for VGPU9. */
720 AssertFailedReturn(VERR_NOT_IMPLEMENTED);
721}
722
723int vmsvga3dQueryEnd(PVGASTATE pThis, uint32_t cid, SVGA3dQueryType type, SVGAGuestPtr guestResult)
724{
725 RT_NOREF(guestResult);
726 PVMSVGA3DSTATE pState = pThis->svga.p3dState;
727 AssertReturn(pState, VERR_NO_MEMORY);
728
729 LogFunc(("cid=%x type=%d guestResult %d:0x%x\n", cid, type, guestResult.gmrId, guestResult.offset));
730
731 PVMSVGA3DCONTEXT pContext;
732 int rc = vmsvga3dContextFromCid(pState, cid, &pContext);
733 AssertRCReturn(rc, rc);
734
735 if (type == SVGA3D_QUERYTYPE_OCCLUSION)
736 {
737 VMSVGA3DQUERY *p = &pContext->occlusion;
738 Assert(p->enmQueryState == VMSVGA3DQUERYSTATE_BUILDING);
739 AssertMsgReturn(VMSVGA3DQUERY_EXISTS(p), ("Query is NULL\n"), VERR_INTERNAL_ERROR);
740
741 rc = vmsvga3dOcclusionQueryEnd(pState, pContext);
742 AssertRCReturn(rc, rc);
743
744 p->enmQueryState = VMSVGA3DQUERYSTATE_ISSUED;
745
746 /* Do not touch guestResult, because the guest will call WaitForQuery. */
747 return VINF_SUCCESS;
748 }
749
750 /* Nothing else for VGPU9. */
751 AssertFailedReturn(VERR_NOT_IMPLEMENTED);
752}
753
754int vmsvga3dQueryWait(PVGASTATE pThis, uint32_t cid, SVGA3dQueryType type, SVGAGuestPtr guestResult)
755{
756 PVMSVGA3DSTATE pState = pThis->svga.p3dState;
757 AssertReturn(pState, VERR_NO_MEMORY);
758
759 LogFunc(("cid=%x type=%d guestResult GMR%d:0x%x\n", cid, type, guestResult.gmrId, guestResult.offset));
760
761 PVMSVGA3DCONTEXT pContext;
762 int rc = vmsvga3dContextFromCid(pState, cid, &pContext);
763 AssertRCReturn(rc, rc);
764
765 if (type == SVGA3D_QUERYTYPE_OCCLUSION)
766 {
767 VMSVGA3DQUERY *p = &pContext->occlusion;
768 if (VMSVGA3DQUERY_EXISTS(p))
769 {
770 if (p->enmQueryState == VMSVGA3DQUERYSTATE_ISSUED)
771 {
772 /* Only if not already in SIGNALED state,
773 * i.e. not a second read from the guest or after restoring saved state.
774 */
775 uint32_t u32Pixels = 0;
776 rc = vmsvga3dOcclusionQueryGetData(pState, pContext, &u32Pixels);
777 if (RT_SUCCESS(rc))
778 {
779 p->enmQueryState = VMSVGA3DQUERYSTATE_SIGNALED;
780 p->u32QueryResult += u32Pixels; /* += because it might contain partial result from saved state. */
781 }
782 }
783
784 if (RT_SUCCESS(rc))
785 {
786 /* Return data to the guest. */
787 vmsvga3dQueryWriteResult(pThis, guestResult, SVGA3D_QUERYSTATE_SUCCEEDED, p->u32QueryResult);
788 return VINF_SUCCESS;
789 }
790 }
791 else
792 {
793 AssertMsgFailed(("GetData Query is NULL\n"));
794 }
795
796 rc = VERR_INTERNAL_ERROR;
797 }
798 else
799 {
800 rc = VERR_NOT_IMPLEMENTED;
801 }
802
803 vmsvga3dQueryWriteResult(pThis, guestResult, SVGA3D_QUERYSTATE_FAILED, 0);
804 AssertFailedReturn(rc);
805}
806
807int vmsvga3dSurfaceBlitToScreen(PVGASTATE pThis, uint32_t idDstScreen, SVGASignedRect destRect, SVGA3dSurfaceImageId src, SVGASignedRect srcRect, uint32_t cRects, SVGASignedRect *pRect)
808{
809 /* Requires SVGA_FIFO_CAP_SCREEN_OBJECT support */
810 LogFunc(("dest=%d (%d,%d)(%d,%d) sid=%x (face=%d, mipmap=%d) (%d,%d)(%d,%d) cRects=%d\n",
811 idDstScreen, destRect.left, destRect.top, destRect.right, destRect.bottom, src.sid, src.face, src.mipmap,
812 srcRect.left, srcRect.top, srcRect.right, srcRect.bottom, cRects));
813 for (uint32_t i = 0; i < cRects; i++)
814 {
815 LogFunc(("clipping rect %d (%d,%d)(%d,%d)\n", i, pRect[i].left, pRect[i].top, pRect[i].right, pRect[i].bottom));
816 }
817
818 /** @todo Only screen 0 for now. */
819 AssertReturn(idDstScreen == 0, VERR_INTERNAL_ERROR);
820 AssertReturn(src.mipmap == 0 && src.face == 0, VERR_INVALID_PARAMETER);
821 /** @todo scaling */
822 AssertReturn(destRect.right - destRect.left == srcRect.right - srcRect.left && destRect.bottom - destRect.top == srcRect.bottom - srcRect.top, VERR_INVALID_PARAMETER);
823
824 SVGA3dCopyBox box;
825 SVGA3dGuestImage dest;
826
827 box.srcz = 0;
828 box.z = 0;
829 box.d = 1;
830
831 /** @todo SVGA_GMR_FRAMEBUFFER is not the screen object
832 * and might not point to the start of VRAM as assumed here.
833 */
834 dest.ptr.gmrId = SVGA_GMR_FRAMEBUFFER;
835 dest.ptr.offset = pThis->svga.uScreenOffset;
836 dest.pitch = pThis->svga.cbScanline;
837
838 if (cRects == 0)
839 {
840 /* easy case; no clipping */
841
842 /* SVGA_3D_CMD_SURFACE_DMA:
843 * 'define the "source" in each copyBox as the guest image and the
844 * "destination" as the host image, regardless of transfer direction.'
845 *
846 * Since the BlitToScreen operation transfers from a host surface to the guest VRAM,
847 * it must set the copyBox "source" to the guest destination coords and
848 * the copyBox "destination" to the host surface source coords.
849 */
850 /* Host image. */
851 box.x = srcRect.left;
852 box.y = srcRect.top;
853 box.w = srcRect.right - srcRect.left;
854 box.h = srcRect.bottom - srcRect.top;
855 /* Guest image. */
856 box.srcx = destRect.left;
857 box.srcy = destRect.top;
858
859 int rc = vmsvga3dSurfaceDMA(pThis, dest, src, SVGA3D_READ_HOST_VRAM, 1, &box);
860 AssertRCReturn(rc, rc);
861
862 /* Update the guest image, which is at box.src. */
863 vgaR3UpdateDisplay(pThis, box.srcx, box.srcy, box.w, box.h);
864 }
865 else
866 {
867 /** @todo merge into one SurfaceDMA call */
868 for (uint32_t i = 0; i < cRects; i++)
869 {
870 /* "The clip rectangle coordinates are measured
871 * relative to the top-left corner of destRect."
872 * Therefore they are relative to the top-left corner of srcRect as well.
873 */
874
875 /* Host image. See 'SVGA_3D_CMD_SURFACE_DMA:' comment in the 'if' branch. */
876 box.x = srcRect.left + pRect[i].left;
877 box.y = srcRect.top + pRect[i].top;
878 box.w = pRect[i].right - pRect[i].left;
879 box.h = pRect[i].bottom - pRect[i].top;
880 /* Guest image. The target screen memory is currently in the guest VRAM. */
881 box.srcx = destRect.left + pRect[i].left;
882 box.srcy = destRect.top + pRect[i].top;
883
884 int rc = vmsvga3dSurfaceDMA(pThis, dest, src, SVGA3D_READ_HOST_VRAM, 1, &box);
885 AssertRCReturn(rc, rc);
886
887 /* Update the guest image, which is at box.src. */
888 vgaR3UpdateDisplay(pThis, box.srcx, box.srcy, box.w, box.h);
889 }
890 }
891
892 return VINF_SUCCESS;
893}
894
895int vmsvga3dCommandPresent(PVGASTATE pThis, uint32_t sid, uint32_t cRects, SVGA3dCopyRect *pRect)
896{
897 /* Deprecated according to svga3d_reg.h. */
898 PVMSVGA3DSTATE pState = pThis->svga.p3dState;
899 AssertReturn(pState, VERR_NO_MEMORY);
900
901 PVMSVGA3DSURFACE pSurface;
902 int rc = vmsvga3dSurfaceFromSid(pState, sid, &pSurface);
903 AssertRCReturn(rc, rc);
904
905 /* If there are no recangles specified, just grab a screenful. */
906 SVGA3dCopyRect DummyRect;
907 if (cRects != 0)
908 { /* likely */ }
909 else
910 {
911 /** @todo Find the usecase for this or check what the original device does.
912 * The original code was doing some scaling based on the surface
913 * size... */
914 AssertMsgFailed(("No rects to present. Who is doing that and what do they actually expect?\n"));
915 DummyRect.x = DummyRect.srcx = 0;
916 DummyRect.y = DummyRect.srcy = 0;
917 DummyRect.w = pThis->svga.uWidth;
918 DummyRect.h = pThis->svga.uHeight;
919 cRects = 1;
920 pRect = &DummyRect;
921 }
922
923 uint32_t i;
924 for (i = 0; i < cRects; ++i)
925 {
926 uint32_t idDstScreen = 0; /** @todo Use virtual coords: SVGA_ID_INVALID. */
927 SVGASignedRect destRect;
928 destRect.left = pRect[i].x;
929 destRect.top = pRect[i].y;
930 destRect.right = pRect[i].x + pRect[i].w;
931 destRect.bottom = pRect[i].y + pRect[i].h;
932
933 SVGA3dSurfaceImageId src;
934 src.sid = sid;
935 src.face = 0;
936 src.mipmap = 0;
937
938 SVGASignedRect srcRect;
939 srcRect.left = pRect[i].srcx;
940 srcRect.top = pRect[i].srcy;
941 srcRect.right = pRect[i].srcx + pRect[i].w;
942 srcRect.bottom = pRect[i].srcy + pRect[i].h;
943
944 /* Entire rect. */
945 rc = vmsvga3dSurfaceBlitToScreen(pThis, idDstScreen, destRect, src, srcRect, 0, NULL);
946 AssertRCReturn(rc, rc);
947 }
948
949 return VINF_SUCCESS;
950}
Note: See TracBrowser for help on using the repository browser.

© 2023 Oracle
ContactPrivacy policyTerms of Use