VirtualBox

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

Last change on this file since 100766 was 100008, checked in by vboxsync, 18 months ago

Devices/Graphics: debug logging and assertions. bugref:9830

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 74.3 KB
Line 
1/* $Id: DevVGA-SVGA3d.cpp 100008 2023-05-30 07:05:05Z vboxsync $ */
2/** @file
3 * DevSVGA3d - VMWare SVGA device, 3D parts - Common core code.
4 */
5
6/*
7 * Copyright (C) 2013-2023 Oracle and/or its affiliates.
8 *
9 * This file is part of VirtualBox base platform packages, as
10 * available from https://www.virtualbox.org.
11 *
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation, in version 3 of the
15 * License.
16 *
17 * This program is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, see <https://www.gnu.org/licenses>.
24 *
25 * SPDX-License-Identifier: GPL-3.0-only
26 */
27
28
29/*********************************************************************************************************************************
30* Header Files *
31*********************************************************************************************************************************/
32#define LOG_GROUP LOG_GROUP_DEV_VMSVGA
33#include <VBox/vmm/pdmdev.h>
34#include <iprt/errcore.h>
35#include <VBox/log.h>
36
37#include <iprt/assert.h>
38#include <iprt/mem.h>
39
40#include <VBox/vmm/pgm.h> /* required by DevVGA.h */
41#include <VBoxVideo.h> /* required by DevVGA.h */
42
43/* should go BEFORE any other DevVGA include to make all DevVGA.h config defines be visible */
44#include "DevVGA.h"
45
46#include "DevVGA-SVGA.h"
47#include "DevVGA-SVGA3d.h"
48#define VMSVGA3D_INCL_STRUCTURE_DESCRIPTORS
49#include "DevVGA-SVGA3d-internal.h"
50#include "DevVGA-SVGA-internal.h"
51
52
53static int vmsvga3dSurfaceAllocMipLevels(PVMSVGA3DSURFACE pSurface)
54{
55 /* Allocate buffer to hold the surface data until we can move it into a D3D object */
56 for (uint32_t i = 0; i < pSurface->cLevels * pSurface->surfaceDesc.numArrayElements; ++i)
57 {
58 PVMSVGA3DMIPMAPLEVEL pMipmapLevel = &pSurface->paMipmapLevels[i];
59 AssertReturn(pMipmapLevel->pSurfaceData == NULL, VERR_INVALID_STATE);
60 pMipmapLevel->pSurfaceData = RTMemAllocZ(pMipmapLevel->cbSurface);
61 AssertReturn(pMipmapLevel->pSurfaceData, VERR_NO_MEMORY);
62 }
63 return VINF_SUCCESS;
64}
65
66
67static void vmsvga3dSurfaceFreeMipLevels(PVMSVGA3DSURFACE pSurface)
68{
69 for (uint32_t i = 0; i < pSurface->cLevels * pSurface->surfaceDesc.numArrayElements; ++i)
70 {
71 PVMSVGA3DMIPMAPLEVEL pMipmapLevel = &pSurface->paMipmapLevels[i];
72 RTMemFreeZ(pMipmapLevel->pSurfaceData, pMipmapLevel->cbSurface);
73 pMipmapLevel->pSurfaceData = NULL;
74 }
75}
76
77
78/**
79 * Implements the SVGA_3D_CMD_SURFACE_DEFINE_V2 and SVGA_3D_CMD_SURFACE_DEFINE
80 * commands (fifo).
81 *
82 * @returns VBox status code (currently ignored).
83 * @param pThisCC The VGA/VMSVGA state for ring-3.
84 * @param sid The ID of the surface to (re-)define.
85 * @param surfaceFlags .
86 * @param format .
87 * @param multisampleCount .
88 * @param autogenFilter .
89 * @param numMipLevels .
90 * @param pMipLevel0Size .
91 * @param arraySize Number of elements in a texture array.
92 * @param fAllocMipLevels .
93 */
94int vmsvga3dSurfaceDefine(PVGASTATECC pThisCC, uint32_t sid, SVGA3dSurfaceAllFlags surfaceFlags, SVGA3dSurfaceFormat format,
95 uint32_t multisampleCount, SVGA3dTextureFilter autogenFilter,
96 uint32_t numMipLevels, SVGA3dSize const *pMipLevel0Size, uint32_t arraySize, bool fAllocMipLevels)
97{
98 PVMSVGA3DSURFACE pSurface;
99 PVMSVGA3DSTATE pState = pThisCC->svga.p3dState;
100 AssertReturn(pState, VERR_INVALID_STATE);
101
102 LogFunc(("sid=%u surfaceFlags=0x%RX64 format=%s (%#x) multiSampleCount=%d autogenFilter=%d, numMipLevels=%d size=(%dx%dx%d)\n",
103 sid, surfaceFlags, vmsvgaLookupEnum((int)format, &g_SVGA3dSurfaceFormat2String), format, multisampleCount, autogenFilter,
104 numMipLevels, pMipLevel0Size->width, pMipLevel0Size->height, pMipLevel0Size->depth));
105
106 ASSERT_GUEST_RETURN(sid < SVGA3D_MAX_SURFACE_IDS, VERR_INVALID_PARAMETER);
107 ASSERT_GUEST_RETURN(numMipLevels >= 1 && numMipLevels <= SVGA3D_MAX_MIP_LEVELS, VERR_INVALID_PARAMETER);
108 ASSERT_GUEST_RETURN(arraySize <= SVGA3D_MAX_SURFACE_ARRAYSIZE, VERR_INVALID_PARAMETER);
109
110 if (sid >= pState->cSurfaces)
111 {
112 /* Grow the array. */
113 uint32_t cNew = RT_ALIGN(sid + 15, 16);
114 void *pvNew = RTMemRealloc(pState->papSurfaces, sizeof(pState->papSurfaces[0]) * cNew);
115 AssertReturn(pvNew, VERR_NO_MEMORY);
116 pState->papSurfaces = (PVMSVGA3DSURFACE *)pvNew;
117 while (pState->cSurfaces < cNew)
118 {
119 pSurface = (PVMSVGA3DSURFACE)RTMemAllocZ(sizeof(*pSurface));
120 AssertReturn(pSurface, VERR_NO_MEMORY);
121 pSurface->id = SVGA3D_INVALID_ID;
122 pState->papSurfaces[pState->cSurfaces++] = pSurface;
123 }
124 }
125 pSurface = pState->papSurfaces[sid];
126
127 /* If one already exists with this id, then destroy it now. */
128 if (pSurface->id != SVGA3D_INVALID_ID)
129 vmsvga3dSurfaceDestroy(pThisCC, sid);
130
131 RT_ZERO(*pSurface);
132 // pSurface->pBackendSurface = NULL;
133 pSurface->id = SVGA3D_INVALID_ID; /* Keep this value until the surface init completes */
134 pSurface->idAssociatedContext = SVGA3D_INVALID_ID;
135
136 if (arraySize)
137 pSurface->surfaceDesc.numArrayElements = arraySize; /* Also for an array of cubemaps where arraySize = 6 * numCubes. */
138 else if (surfaceFlags & SVGA3D_SURFACE_CUBEMAP)
139 pSurface->surfaceDesc.numArrayElements = SVGA3D_MAX_SURFACE_FACES;
140 else
141 pSurface->surfaceDesc.numArrayElements = 1;
142
143 /** @todo This 'switch' and the surfaceFlags tweaks should not be necessary.
144 * The actual surface type will be figured out when the surface is actually used later.
145 * The backends code must be reviewed for unnecessary dependencies on the surfaceFlags value.
146 */
147 /* The surface type is sort of undefined now, even though the hints and format can help to clear that up.
148 * In some case we'll have to wait until the surface is used to create the D3D object.
149 */
150 switch (format)
151 {
152 case SVGA3D_Z_D32:
153 case SVGA3D_Z_D16:
154 case SVGA3D_Z_D24S8:
155 case SVGA3D_Z_D15S1:
156 case SVGA3D_Z_D24X8:
157 case SVGA3D_Z_DF16:
158 case SVGA3D_Z_DF24:
159 case SVGA3D_Z_D24S8_INT:
160 Assert(surfaceFlags & SVGA3D_SURFACE_HINT_DEPTHSTENCIL);
161 surfaceFlags |= SVGA3D_SURFACE_HINT_DEPTHSTENCIL;
162 break;
163
164 /* Texture compression formats */
165 case SVGA3D_DXT1:
166 case SVGA3D_DXT2:
167 case SVGA3D_DXT3:
168 case SVGA3D_DXT4:
169 case SVGA3D_DXT5:
170 /* Bump-map formats */
171 case SVGA3D_BUMPU8V8:
172 case SVGA3D_BUMPL6V5U5:
173 case SVGA3D_BUMPX8L8V8U8:
174 case SVGA3D_V8U8:
175 case SVGA3D_Q8W8V8U8:
176 case SVGA3D_CxV8U8:
177 case SVGA3D_X8L8V8U8:
178 case SVGA3D_A2W10V10U10:
179 case SVGA3D_V16U16:
180 /* Typical render target formats; we should allow render target buffers to be used as textures. */
181 case SVGA3D_X8R8G8B8:
182 case SVGA3D_A8R8G8B8:
183 case SVGA3D_R5G6B5:
184 case SVGA3D_X1R5G5B5:
185 case SVGA3D_A1R5G5B5:
186 case SVGA3D_A4R4G4B4:
187 Assert(surfaceFlags & (SVGA3D_SURFACE_HINT_TEXTURE | SVGA3D_SURFACE_SCREENTARGET));
188 surfaceFlags |= SVGA3D_SURFACE_HINT_TEXTURE;
189 break;
190
191 case SVGA3D_LUMINANCE8:
192 case SVGA3D_LUMINANCE4_ALPHA4:
193 case SVGA3D_LUMINANCE16:
194 case SVGA3D_LUMINANCE8_ALPHA8:
195 case SVGA3D_ARGB_S10E5: /* 16-bit floating-point ARGB */
196 case SVGA3D_ARGB_S23E8: /* 32-bit floating-point ARGB */
197 case SVGA3D_A2R10G10B10:
198 case SVGA3D_ALPHA8:
199 case SVGA3D_R_S10E5:
200 case SVGA3D_R_S23E8:
201 case SVGA3D_RG_S10E5:
202 case SVGA3D_RG_S23E8:
203 case SVGA3D_G16R16:
204 case SVGA3D_A16B16G16R16:
205 case SVGA3D_UYVY:
206 case SVGA3D_YUY2:
207 case SVGA3D_NV12:
208 case SVGA3D_FORMAT_DEAD2: /* Old SVGA3D_AYUV */
209 case SVGA3D_ATI1:
210 case SVGA3D_ATI2:
211 break;
212
213 /*
214 * Any surface can be used as a buffer object, but SVGA3D_BUFFER is
215 * the most efficient format to use when creating new surfaces
216 * expressly for index or vertex data.
217 */
218 case SVGA3D_BUFFER:
219 break;
220
221 default:
222 break;
223 }
224
225 pSurface->f.surfaceFlags = surfaceFlags;
226 pSurface->format = format;
227 /* cFaces is 6 for a cubemaps and 1 otherwise. */
228 pSurface->cFaces = (uint32_t)((surfaceFlags & SVGA3D_SURFACE_CUBEMAP) ? 6 : 1);
229 pSurface->cLevels = numMipLevels;
230 pSurface->multiSampleCount = multisampleCount;
231 pSurface->autogenFilter = autogenFilter;
232 Assert(autogenFilter != SVGA3D_TEX_FILTER_FLATCUBIC);
233 Assert(autogenFilter != SVGA3D_TEX_FILTER_GAUSSIANCUBIC);
234 pSurface->paMipmapLevels = (PVMSVGA3DMIPMAPLEVEL)RTMemAllocZ(numMipLevels * pSurface->surfaceDesc.numArrayElements * sizeof(VMSVGA3DMIPMAPLEVEL));
235 AssertReturn(pSurface->paMipmapLevels, VERR_NO_MEMORY);
236
237 pSurface->cbBlock = vmsvga3dSurfaceFormatSize(format, &pSurface->cxBlock, &pSurface->cyBlock);
238 AssertReturn(pSurface->cbBlock, VERR_INVALID_PARAMETER);
239
240 /** @todo cbMemRemaining = value of SVGA_REG_MOB_MAX_SIZE */
241 uint32_t cbMemRemaining = SVGA3D_MAX_SURFACE_MEM_SIZE; /* Do not allow more than this for a surface. */
242 SVGA3dSize mipmapSize = *pMipLevel0Size;
243 int rc = VINF_SUCCESS;
244
245 for (uint32_t i = 0; i < numMipLevels; ++i)
246 {
247 for (uint32_t iArray = 0; iArray < pSurface->surfaceDesc.numArrayElements; ++iArray)
248 {
249 uint32_t const iMipmap = vmsvga3dCalcSubresource(i, iArray, numMipLevels);
250 LogFunc(("[%d] array %d mip level %d (%d,%d,%d) cbBlock=%#x block %dx%d\n",
251 iMipmap, iArray, i, mipmapSize.width, mipmapSize.height, mipmapSize.depth,
252 pSurface->cbBlock, pSurface->cxBlock, pSurface->cyBlock));
253
254 uint32_t cBlocksX;
255 uint32_t cBlocksY;
256 if (RT_LIKELY(pSurface->cxBlock == 1 && pSurface->cyBlock == 1))
257 {
258 cBlocksX = mipmapSize.width;
259 cBlocksY = mipmapSize.height;
260 }
261 else
262 {
263 cBlocksX = mipmapSize.width / pSurface->cxBlock;
264 if (mipmapSize.width % pSurface->cxBlock)
265 ++cBlocksX;
266 cBlocksY = mipmapSize.height / pSurface->cyBlock;
267 if (mipmapSize.height % pSurface->cyBlock)
268 ++cBlocksY;
269 }
270
271 AssertBreakStmt(cBlocksX > 0 && cBlocksY > 0 && mipmapSize.depth > 0, rc = VERR_INVALID_PARAMETER);
272
273 const uint32_t cMaxBlocksX = cbMemRemaining / pSurface->cbBlock;
274 AssertBreakStmt(cBlocksX < cMaxBlocksX, rc = VERR_INVALID_PARAMETER);
275
276 const uint32_t cbSurfacePitch = pSurface->cbBlock * cBlocksX;
277 LogFunc(("cbSurfacePitch=0x%x\n", cbSurfacePitch));
278
279 const uint32_t cMaxBlocksY = cbMemRemaining / cbSurfacePitch;
280 AssertBreakStmt(cBlocksY < cMaxBlocksY, rc = VERR_INVALID_PARAMETER);
281
282 const uint32_t cbSurfacePlane = cbSurfacePitch * cBlocksY;
283
284 const uint32_t cMaxDepth = cbMemRemaining / cbSurfacePlane;
285 AssertBreakStmt(mipmapSize.depth < cMaxDepth, rc = VERR_INVALID_PARAMETER);
286
287 const uint32_t cbSurface = cbSurfacePlane * mipmapSize.depth;
288
289 PVMSVGA3DMIPMAPLEVEL pMipmapLevel = &pSurface->paMipmapLevels[iMipmap];
290 pMipmapLevel->mipmapSize = mipmapSize;
291 pMipmapLevel->cBlocksX = cBlocksX;
292 pMipmapLevel->cBlocksY = cBlocksY;
293 pMipmapLevel->cBlocks = cBlocksX * cBlocksY * mipmapSize.depth;
294 pMipmapLevel->cbSurfacePitch = cbSurfacePitch;
295 pMipmapLevel->cbSurfacePlane = cbSurfacePlane;
296 pMipmapLevel->cbSurface = cbSurface;
297 pMipmapLevel->pSurfaceData = NULL;
298
299 cbMemRemaining -= cbSurface;
300 }
301
302 AssertRCBreak(rc);
303
304 mipmapSize.width >>= 1;
305 if (mipmapSize.width == 0) mipmapSize.width = 1;
306 mipmapSize.height >>= 1;
307 if (mipmapSize.height == 0) mipmapSize.height = 1;
308 mipmapSize.depth >>= 1;
309 if (mipmapSize.depth == 0) mipmapSize.depth = 1;
310 }
311
312 AssertLogRelRCReturnStmt(rc, RTMemFree(pSurface->paMipmapLevels), rc);
313
314 /* Compute the size of one array element. */
315 pSurface->surfaceDesc.cbArrayElement = 0;
316 for (uint32_t i = 0; i < pSurface->cLevels; ++i)
317 {
318 PVMSVGA3DMIPMAPLEVEL pMipLevel = &pSurface->paMipmapLevels[i];
319 pSurface->surfaceDesc.cbArrayElement += pMipLevel->cbSurface;
320 }
321
322 if (vmsvga3dIsLegacyBackend(pThisCC))
323 {
324#ifdef VMSVGA3D_DIRECT3D
325 /* pSurface->hSharedObject = NULL; */
326 /* pSurface->pSharedObjectTree = NULL; */
327 /* Translate the format and usage flags to D3D. */
328 pSurface->d3dfmtRequested = vmsvga3dSurfaceFormat2D3D(format);
329 pSurface->formatD3D = D3D9GetActualFormat(pState, pSurface->d3dfmtRequested);
330 pSurface->multiSampleTypeD3D= vmsvga3dMultipeSampleCount2D3D(multisampleCount);
331 pSurface->fUsageD3D = 0;
332 if (surfaceFlags & SVGA3D_SURFACE_HINT_DYNAMIC)
333 pSurface->fUsageD3D |= D3DUSAGE_DYNAMIC;
334 if (surfaceFlags & SVGA3D_SURFACE_HINT_RENDERTARGET)
335 pSurface->fUsageD3D |= D3DUSAGE_RENDERTARGET;
336 if (surfaceFlags & SVGA3D_SURFACE_HINT_DEPTHSTENCIL)
337 pSurface->fUsageD3D |= D3DUSAGE_DEPTHSTENCIL;
338 if (surfaceFlags & SVGA3D_SURFACE_HINT_WRITEONLY)
339 pSurface->fUsageD3D |= D3DUSAGE_WRITEONLY;
340 if (surfaceFlags & SVGA3D_SURFACE_AUTOGENMIPMAPS)
341 pSurface->fUsageD3D |= D3DUSAGE_AUTOGENMIPMAP;
342 pSurface->enmD3DResType = VMSVGA3D_D3DRESTYPE_NONE;
343 /* pSurface->u.pSurface = NULL; */
344 /* pSurface->bounce.pTexture = NULL; */
345 /* pSurface->emulated.pTexture = NULL; */
346#else
347 /* pSurface->oglId.buffer = OPENGL_INVALID_ID; */
348 /* pSurface->fEmulated = false; */
349 /* pSurface->idEmulated = OPENGL_INVALID_ID; */
350 vmsvga3dSurfaceFormat2OGL(pSurface, format);
351#endif
352 }
353
354#ifdef LOG_ENABLED
355 SVGA3dSurfaceAllFlags const f = surfaceFlags;
356 LogFunc(("surface flags:%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s 0x%RX64\n",
357 (f & SVGA3D_SURFACE_CUBEMAP) ? " CUBEMAP" : "",
358 (f & SVGA3D_SURFACE_HINT_STATIC) ? " HINT_STATIC" : "",
359 (f & SVGA3D_SURFACE_HINT_DYNAMIC) ? " HINT_DYNAMIC" : "",
360 (f & SVGA3D_SURFACE_HINT_INDEXBUFFER) ? " HINT_INDEXBUFFER" : "",
361 (f & SVGA3D_SURFACE_HINT_VERTEXBUFFER) ? " HINT_VERTEXBUFFER" : "",
362 (f & SVGA3D_SURFACE_HINT_TEXTURE) ? " HINT_TEXTURE" : "",
363 (f & SVGA3D_SURFACE_HINT_RENDERTARGET) ? " HINT_RENDERTARGET" : "",
364 (f & SVGA3D_SURFACE_HINT_DEPTHSTENCIL) ? " HINT_DEPTHSTENCIL" : "",
365 (f & SVGA3D_SURFACE_HINT_WRITEONLY) ? " HINT_WRITEONLY" : "",
366 (f & SVGA3D_SURFACE_DEAD2) ? " DEAD2" : "",
367 (f & SVGA3D_SURFACE_AUTOGENMIPMAPS) ? " AUTOGENMIPMAPS" : "",
368 (f & SVGA3D_SURFACE_DEAD1) ? " DEAD1" : "",
369 (f & SVGA3D_SURFACE_MOB_PITCH) ? " MOB_PITCH" : "",
370 (f & SVGA3D_SURFACE_INACTIVE) ? " INACTIVE" : "",
371 (f & SVGA3D_SURFACE_HINT_RT_LOCKABLE) ? " HINT_RT_LOCKABLE" : "",
372 (f & SVGA3D_SURFACE_VOLUME) ? " VOLUME" : "",
373 (f & SVGA3D_SURFACE_SCREENTARGET) ? " SCREENTARGET" : "",
374 (f & SVGA3D_SURFACE_ALIGN16) ? " ALIGN16" : "",
375 (f & SVGA3D_SURFACE_1D) ? " 1D" : "",
376 (f & SVGA3D_SURFACE_ARRAY) ? " ARRAY" : "",
377 (f & SVGA3D_SURFACE_BIND_VERTEX_BUFFER) ? " BIND_VERTEX_BUFFER" : "",
378 (f & SVGA3D_SURFACE_BIND_INDEX_BUFFER) ? " BIND_INDEX_BUFFER" : "",
379 (f & SVGA3D_SURFACE_BIND_CONSTANT_BUFFER) ? " BIND_CONSTANT_BUFFER" : "",
380 (f & SVGA3D_SURFACE_BIND_SHADER_RESOURCE) ? " BIND_SHADER_RESOURCE" : "",
381 (f & SVGA3D_SURFACE_BIND_RENDER_TARGET) ? " BIND_RENDER_TARGET" : "",
382 (f & SVGA3D_SURFACE_BIND_DEPTH_STENCIL) ? " BIND_DEPTH_STENCIL" : "",
383 (f & SVGA3D_SURFACE_BIND_STREAM_OUTPUT) ? " BIND_STREAM_OUTPUT" : "",
384 (f & SVGA3D_SURFACE_STAGING_UPLOAD) ? " STAGING_UPLOAD" : "",
385 (f & SVGA3D_SURFACE_STAGING_DOWNLOAD) ? " STAGING_DOWNLOAD" : "",
386 (f & SVGA3D_SURFACE_HINT_INDIRECT_UPDATE) ? " HINT_INDIRECT_UPDATE" : "",
387 (f & SVGA3D_SURFACE_TRANSFER_FROM_BUFFER) ? " TRANSFER_FROM_BUFFER" : "",
388 (f & SVGA3D_SURFACE_RESERVED1) ? " RESERVED1" : "",
389 (f & SVGA3D_SURFACE_MULTISAMPLE) ? " MULTISAMPLE" : "",
390 (f & SVGA3D_SURFACE_BIND_UAVIEW) ? " BIND_UAVIEW" : "",
391 (f & SVGA3D_SURFACE_TRANSFER_TO_BUFFER) ? " TRANSFER_TO_BUFFER" : "",
392 (f & SVGA3D_SURFACE_BIND_LOGICOPS) ? " BIND_LOGICOPS" : "",
393 (f & SVGA3D_SURFACE_BIND_RAW_VIEWS) ? " BIND_RAW_VIEWS" : "",
394 (f & SVGA3D_SURFACE_BUFFER_STRUCTURED) ? " BUFFER_STRUCTURED" : "",
395 (f & SVGA3D_SURFACE_DRAWINDIRECT_ARGS) ? " DRAWINDIRECT_ARGS" : "",
396 (f & SVGA3D_SURFACE_RESOURCE_CLAMP) ? " RESOURCE_CLAMP" : "",
397 (f & SVGA3D_SURFACE_FLAG_MAX) ? " FLAG_MAX" : "",
398 f & ~(SVGA3D_SURFACE_FLAG_MAX - 1ULL)
399 ));
400#endif
401
402 Assert(!VMSVGA3DSURFACE_HAS_HW_SURFACE(pSurface));
403
404 if (fAllocMipLevels)
405 {
406 rc = vmsvga3dSurfaceAllocMipLevels(pSurface);
407 AssertRCReturn(rc, rc);
408 }
409
410 pSurface->id = sid;
411 return VINF_SUCCESS;
412}
413
414
415/**
416 * Implements the SVGA_3D_CMD_SURFACE_DESTROY command (fifo).
417 *
418 * @returns VBox status code (currently ignored).
419 * @param pThisCC The VGA/VMSVGA state for ring-3.
420 * @param sid The ID of the surface to destroy.
421 */
422int vmsvga3dSurfaceDestroy(PVGASTATECC pThisCC, uint32_t sid)
423{
424 PVMSVGA3DSTATE pState = pThisCC->svga.p3dState;
425 AssertReturn(pState, VERR_NO_MEMORY);
426
427 PVMSVGA3DSURFACE pSurface;
428 int rc = vmsvga3dSurfaceFromSid(pState, sid, &pSurface);
429 AssertRCReturn(rc, rc);
430
431 LogFunc(("sid=%u\n", sid));
432
433 /* Check all contexts if this surface is used as a render target or active texture. */
434 for (uint32_t cid = 0; cid < pState->cContexts; cid++)
435 {
436 PVMSVGA3DCONTEXT pContext = pState->papContexts[cid];
437 if (pContext->id == cid)
438 {
439 for (uint32_t i = 0; i < RT_ELEMENTS(pContext->aSidActiveTextures); ++i)
440 if (pContext->aSidActiveTextures[i] == sid)
441 pContext->aSidActiveTextures[i] = SVGA3D_INVALID_ID;
442 for (uint32_t i = 0; i < RT_ELEMENTS(pContext->state.aRenderTargets); ++i)
443 if (pContext->state.aRenderTargets[i] == sid)
444 pContext->state.aRenderTargets[i] = SVGA3D_INVALID_ID;
445 }
446 }
447
448 PVMSVGAR3STATE const pSvgaR3State = pThisCC->svga.pSvgaR3State;
449 if (pSvgaR3State->pFuncs3D)
450 pSvgaR3State->pFuncs3D->pfnSurfaceDestroy(pThisCC, true, pSurface);
451
452 if (pSurface->paMipmapLevels)
453 {
454 vmsvga3dSurfaceFreeMipLevels(pSurface);
455 RTMemFree(pSurface->paMipmapLevels);
456 }
457
458 memset(pSurface, 0, sizeof(*pSurface));
459 pSurface->id = SVGA3D_INVALID_ID;
460
461 return VINF_SUCCESS;
462}
463
464
465/**
466 * Implements the SVGA_3D_CMD_SURFACE_STRETCHBLT command (fifo).
467 *
468 * @returns VBox status code (currently ignored).
469 * @param pThis The shared VGA/VMSVGA state.
470 * @param pThisCC The VGA/VMSVGA state for ring-3.
471 * @param pDstSfcImg
472 * @param pDstBox
473 * @param pSrcSfcImg
474 * @param pSrcBox
475 * @param enmMode
476 */
477int vmsvga3dSurfaceStretchBlt(PVGASTATE pThis, PVGASTATECC pThisCC, SVGA3dSurfaceImageId const *pDstSfcImg, SVGA3dBox const *pDstBox,
478 SVGA3dSurfaceImageId const *pSrcSfcImg, SVGA3dBox const *pSrcBox, SVGA3dStretchBltMode enmMode)
479{
480 PVMSVGA3DSTATE pState = pThisCC->svga.p3dState;
481 AssertReturn(pState, VERR_NO_MEMORY);
482
483 int rc;
484
485 uint32_t const sidSrc = pSrcSfcImg->sid;
486 PVMSVGA3DSURFACE pSrcSurface;
487 rc = vmsvga3dSurfaceFromSid(pState, sidSrc, &pSrcSurface);
488 AssertRCReturn(rc, rc);
489
490 uint32_t const sidDst = pDstSfcImg->sid;
491 PVMSVGA3DSURFACE pDstSurface;
492 rc = vmsvga3dSurfaceFromSid(pState, sidDst, &pDstSurface);
493 AssertRCReturn(rc, rc);
494
495 AssertReturn(pSrcSfcImg->face < pSrcSurface->cFaces, VERR_INVALID_PARAMETER);
496 AssertReturn(pSrcSfcImg->mipmap < pSrcSurface->cLevels, VERR_INVALID_PARAMETER);
497 AssertReturn(pDstSfcImg->face < pDstSurface->cFaces, VERR_INVALID_PARAMETER);
498 AssertReturn(pDstSfcImg->mipmap < pDstSurface->cLevels, VERR_INVALID_PARAMETER);
499
500 PVMSVGAR3STATE const pSvgaR3State = pThisCC->svga.pSvgaR3State;
501 AssertReturn(pSvgaR3State->pFuncs3D, VERR_NOT_IMPLEMENTED);
502
503 PVMSVGA3DCONTEXT pContext;
504#ifdef VMSVGA3D_OPENGL
505 LogFunc(("src sid=%u (%d,%d)(%d,%d) dest sid=%u (%d,%d)(%d,%d) mode=%x\n",
506 sidSrc, pSrcBox->x, pSrcBox->y, pSrcBox->x + pSrcBox->w, pSrcBox->y + pSrcBox->h,
507 sidDst, pDstBox->x, pDstBox->y, pDstBox->x + pDstBox->w, pDstBox->y + pDstBox->h, enmMode));
508 pContext = &pState->SharedCtx;
509 VMSVGA3D_SET_CURRENT_CONTEXT(pState, pContext);
510#else
511 LogFunc(("src sid=%u cid=%u (%d,%d)(%d,%d) dest sid=%u cid=%u (%d,%d)(%d,%d) mode=%x\n",
512 sidSrc, pSrcSurface->idAssociatedContext, pSrcBox->x, pSrcBox->y, pSrcBox->x + pSrcBox->w, pSrcBox->y + pSrcBox->h,
513 sidDst, pDstSurface->idAssociatedContext, pDstBox->x, pDstBox->y, pDstBox->x + pDstBox->w, pDstBox->y + pDstBox->h, enmMode));
514
515 uint32_t cid = pDstSurface->idAssociatedContext;
516 if (cid == SVGA3D_INVALID_ID)
517 cid = pSrcSurface->idAssociatedContext;
518
519 /* At least one of surfaces must be in hardware. */
520 AssertReturn(cid != SVGA3D_INVALID_ID, VERR_INVALID_PARAMETER);
521
522 rc = vmsvga3dContextFromCid(pState, cid, &pContext);
523 AssertRCReturn(rc, rc);
524#endif
525
526 if (!VMSVGA3DSURFACE_HAS_HW_SURFACE(pSrcSurface))
527 {
528 /* Unknown surface type; turn it into a texture, which can be used for other purposes too. */
529 LogFunc(("unknown src sid=%u type=%d format=%d -> create texture\n", sidSrc, pSrcSurface->f.s.surface1Flags, pSrcSurface->format));
530 rc = pSvgaR3State->pFuncs3D->pfnCreateTexture(pThisCC, pContext, pContext->id, pSrcSurface);
531 AssertRCReturn(rc, rc);
532 }
533
534 if (!VMSVGA3DSURFACE_HAS_HW_SURFACE(pDstSurface))
535 {
536 /* Unknown surface type; turn it into a texture, which can be used for other purposes too. */
537 LogFunc(("unknown dest sid=%u type=%d format=%d -> create texture\n", sidDst, pDstSurface->f.s.surface1Flags, pDstSurface->format));
538 rc = pSvgaR3State->pFuncs3D->pfnCreateTexture(pThisCC, pContext, pContext->id, pDstSurface);
539 AssertRCReturn(rc, rc);
540 }
541
542 PVMSVGA3DMIPMAPLEVEL pSrcMipmapLevel;
543 rc = vmsvga3dMipmapLevel(pSrcSurface, pSrcSfcImg->face, pSrcSfcImg->mipmap, &pSrcMipmapLevel);
544 AssertRCReturn(rc, rc);
545
546 PVMSVGA3DMIPMAPLEVEL pDstMipmapLevel;
547 rc = vmsvga3dMipmapLevel(pDstSurface, pDstSfcImg->face, pDstSfcImg->mipmap, &pDstMipmapLevel);
548 AssertRCReturn(rc, rc);
549
550 SVGA3dBox clipSrcBox = *pSrcBox;
551 SVGA3dBox clipDstBox = *pDstBox;
552 vmsvgaR3ClipBox(&pSrcMipmapLevel->mipmapSize, &clipSrcBox);
553 vmsvgaR3ClipBox(&pDstMipmapLevel->mipmapSize, &clipDstBox);
554
555 return pSvgaR3State->pFuncs3D->pfnSurfaceStretchBlt(pThis, pState,
556 pDstSurface, pDstSfcImg->face, pDstSfcImg->mipmap, &clipDstBox,
557 pSrcSurface, pSrcSfcImg->face, pSrcSfcImg->mipmap, &clipSrcBox,
558 enmMode, pContext);
559}
560
561/**
562 * Implements the SVGA_3D_CMD_SURFACE_DMA command (fifo).
563 *
564 * @returns VBox status code (currently ignored).
565 * @param pThis The shared VGA/VMSVGA instance data.
566 * @param pThisCC The VGA/VMSVGA state for ring-3.
567 * @param guest .
568 * @param host .
569 * @param transfer .
570 * @param cCopyBoxes .
571 * @param paBoxes .
572 */
573int vmsvga3dSurfaceDMA(PVGASTATE pThis, PVGASTATECC pThisCC, SVGAGuestImage guest, SVGA3dSurfaceImageId host,
574 SVGA3dTransferType transfer, uint32_t cCopyBoxes, SVGA3dCopyBox *paBoxes)
575{
576 PVMSVGA3DSTATE pState = pThisCC->svga.p3dState;
577 AssertReturn(pState, VERR_NO_MEMORY);
578
579 PVMSVGA3DSURFACE pSurface;
580 int rc = vmsvga3dSurfaceFromSid(pState, host.sid, &pSurface);
581 AssertRCReturn(rc, rc);
582
583 LogFunc(("%sguestptr gmr=%x offset=%x pitch=%x host sid=%u face=%d mipmap=%d transfer=%s cCopyBoxes=%d\n",
584 (pSurface->f.surfaceFlags & SVGA3D_SURFACE_HINT_TEXTURE) ? "TEXTURE " : "",
585 guest.ptr.gmrId, guest.ptr.offset, guest.pitch,
586 host.sid, host.face, host.mipmap, (transfer == SVGA3D_WRITE_HOST_VRAM) ? "READ" : "WRITE", cCopyBoxes));
587
588 PVMSVGA3DMIPMAPLEVEL pMipLevel;
589 rc = vmsvga3dMipmapLevel(pSurface, host.face, host.mipmap, &pMipLevel);
590 AssertRCReturn(rc, rc);
591
592 PVMSVGAR3STATE const pSvgaR3State = pThisCC->svga.pSvgaR3State;
593 AssertReturn(pSvgaR3State->pFuncs3D, VERR_NOT_IMPLEMENTED);
594
595 PVMSVGA3DCONTEXT pContext = NULL;
596 if (!VMSVGA3DSURFACE_HAS_HW_SURFACE(pSurface))
597 {
598 /*
599 * Not realized in host hardware/library yet, we have to work with
600 * the copy of the data we've got in VMSVGA3DMIMAPLEVEL::pSurfaceData.
601 */
602 if (!pMipLevel->pSurfaceData)
603 {
604 rc = vmsvga3dSurfaceAllocMipLevels(pSurface);
605 AssertRCReturn(rc, rc);
606 }
607 }
608 else if (vmsvga3dIsLegacyBackend(pThisCC))
609 {
610#ifdef VMSVGA3D_DIRECT3D
611 /* Flush the drawing pipeline for this surface as it could be used in a shared context. */
612 vmsvga3dSurfaceFlush(pSurface);
613#else /* VMSVGA3D_OPENGL */
614 pContext = &pState->SharedCtx;
615 VMSVGA3D_SET_CURRENT_CONTEXT(pState, pContext);
616#endif
617 }
618
619 /* SVGA_3D_CMD_SURFACE_DMA:
620 * "define the 'source' in each copyBox as the guest image and the
621 * 'destination' as the host image, regardless of transfer direction."
622 */
623 for (uint32_t i = 0; i < cCopyBoxes; ++i)
624 {
625 Log(("Copy box (%s) %d (%d,%d,%d)(%d,%d,%d) dest (%d,%d)\n",
626 VMSVGA3DSURFACE_HAS_HW_SURFACE(pSurface) ? "hw" : "mem",
627 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));
628
629 /* Apparently we're supposed to clip it (gmr test sample) */
630
631 /* The copybox's "dest" is coords in the host surface. Verify them against the surface's mipmap size. */
632 SVGA3dBox hostBox;
633 hostBox.x = paBoxes[i].x;
634 hostBox.y = paBoxes[i].y;
635 hostBox.z = paBoxes[i].z;
636 hostBox.w = paBoxes[i].w;
637 hostBox.h = paBoxes[i].h;
638 hostBox.d = paBoxes[i].d;
639 vmsvgaR3ClipBox(&pMipLevel->mipmapSize, &hostBox);
640
641 if ( !hostBox.w
642 || !hostBox.h
643 || !hostBox.d)
644 {
645 Log(("Skip empty box\n"));
646 continue;
647 }
648 RT_UNTRUSTED_VALIDATED_FENCE();
649
650 /* Adjust the guest, i.e. "src", point.
651 * Do not try to verify them here because vmsvgaR3GmrTransfer takes care of this.
652 */
653 uint32_t const srcx = paBoxes[i].srcx + (hostBox.x - paBoxes[i].x);
654 uint32_t const srcy = paBoxes[i].srcy + (hostBox.y - paBoxes[i].y);
655 uint32_t const srcz = paBoxes[i].srcz + (hostBox.z - paBoxes[i].z);
656
657 /* Calculate offsets of the image blocks for the transfer. */
658 uint32_t u32HostBlockX;
659 uint32_t u32HostBlockY;
660 uint32_t u32GuestBlockX;
661 uint32_t u32GuestBlockY;
662 uint32_t cBlocksX;
663 uint32_t cBlocksY;
664 if (RT_LIKELY(pSurface->cxBlock == 1 && pSurface->cyBlock == 1))
665 {
666 u32HostBlockX = hostBox.x;
667 u32HostBlockY = hostBox.y;
668
669 u32GuestBlockX = srcx;
670 u32GuestBlockY = srcy;
671
672 cBlocksX = hostBox.w;
673 cBlocksY = hostBox.h;
674 }
675 else
676 {
677 /* Pixels to blocks. */
678 u32HostBlockX = hostBox.x / pSurface->cxBlock;
679 u32HostBlockY = hostBox.y / pSurface->cyBlock;
680 Assert(u32HostBlockX * pSurface->cxBlock == hostBox.x);
681 Assert(u32HostBlockY * pSurface->cyBlock == hostBox.y);
682
683 u32GuestBlockX = srcx / pSurface->cxBlock;
684 u32GuestBlockY = srcy / pSurface->cyBlock;
685 Assert(u32GuestBlockX * pSurface->cxBlock == srcx);
686 Assert(u32GuestBlockY * pSurface->cyBlock == srcy);
687
688 cBlocksX = (hostBox.w + pSurface->cxBlock - 1) / pSurface->cxBlock;
689 cBlocksY = (hostBox.h + pSurface->cyBlock - 1) / pSurface->cyBlock;
690 }
691
692 uint32_t cbGuestPitch = guest.pitch;
693 if (cbGuestPitch == 0)
694 {
695 /* Host must "assume image is tightly packed". Our surfaces are. */
696 cbGuestPitch = pMipLevel->cbSurfacePitch;
697 }
698 else
699 {
700 /* vmsvgaR3GmrTransfer will verify the value, just check it is sane. */
701 AssertReturn(cbGuestPitch <= SVGA3D_MAX_SURFACE_MEM_SIZE, VERR_INVALID_PARAMETER);
702 RT_UNTRUSTED_VALIDATED_FENCE();
703 }
704
705 /* srcx, srcy and srcz values are used to calculate the guest offset.
706 * The offset will be verified by vmsvgaR3GmrTransfer, so just check for overflows here.
707 */
708 AssertReturn(srcz < UINT32_MAX / pMipLevel->mipmapSize.height / cbGuestPitch, VERR_INVALID_PARAMETER);
709 AssertReturn(u32GuestBlockY < UINT32_MAX / cbGuestPitch, VERR_INVALID_PARAMETER);
710 AssertReturn(u32GuestBlockX < UINT32_MAX / pSurface->cbBlock, VERR_INVALID_PARAMETER);
711 RT_UNTRUSTED_VALIDATED_FENCE();
712
713 if ( !VMSVGA3DSURFACE_HAS_HW_SURFACE(pSurface)
714 || VMSVGA3DSURFACE_NEEDS_DATA(pSurface))
715 {
716 uint64_t uGuestOffset = u32GuestBlockX * pSurface->cbBlock +
717 u32GuestBlockY * cbGuestPitch +
718 srcz * pMipLevel->mipmapSize.height * cbGuestPitch;
719 AssertReturn(uGuestOffset < UINT32_MAX, VERR_INVALID_PARAMETER);
720
721 /* vmsvga3dSurfaceDefine verifies the surface dimensions and clipBox is within them. */
722 uint32_t uHostOffset = u32HostBlockX * pSurface->cbBlock +
723 u32HostBlockY * pMipLevel->cbSurfacePitch +
724 hostBox.z * pMipLevel->cbSurfacePlane;
725 AssertReturn(uHostOffset < pMipLevel->cbSurface, VERR_INTERNAL_ERROR);
726
727 for (uint32_t z = 0; z < hostBox.d; ++z)
728 {
729 rc = vmsvgaR3GmrTransfer(pThis,
730 pThisCC,
731 transfer,
732 (uint8_t *)pMipLevel->pSurfaceData,
733 pMipLevel->cbSurface,
734 uHostOffset,
735 (int32_t)pMipLevel->cbSurfacePitch,
736 guest.ptr,
737 (uint32_t)uGuestOffset,
738 cbGuestPitch,
739 cBlocksX * pSurface->cbBlock,
740 cBlocksY);
741 AssertRC(rc);
742
743 Log4(("first line [z=%d] (updated at offset 0x%x):\n%.*Rhxd\n",
744 z, uHostOffset, pMipLevel->cbSurfacePitch, pMipLevel->pSurfaceData));
745
746 uHostOffset += pMipLevel->cbSurfacePlane;
747 uGuestOffset += pMipLevel->mipmapSize.height * cbGuestPitch;
748 AssertReturn(uGuestOffset < UINT32_MAX, VERR_INVALID_PARAMETER);
749 }
750 }
751
752 if (VMSVGA3DSURFACE_HAS_HW_SURFACE(pSurface))
753 {
754 SVGA3dCopyBox clipBox;
755 clipBox.x = hostBox.x;
756 clipBox.y = hostBox.y;
757 clipBox.z = hostBox.z;
758 clipBox.w = hostBox.w;
759 clipBox.h = hostBox.h;
760 clipBox.d = hostBox.d;
761 clipBox.srcx = srcx;
762 clipBox.srcy = srcy;
763 clipBox.srcz = srcz;
764 rc = pSvgaR3State->pFuncs3D->pfnSurfaceDMACopyBox(pThis, pThisCC, pState, pSurface, pMipLevel, host.face, host.mipmap,
765 guest.ptr, cbGuestPitch, transfer,
766 &clipBox, pContext, rc, i);
767 AssertRC(rc);
768 }
769 }
770
771 if (!VMSVGA3DSURFACE_HAS_HW_SURFACE(pSurface))
772 {
773 pMipLevel->fDirty = true;
774 pSurface->fDirty = true;
775 }
776
777 return rc;
778}
779
780static int vmsvga3dQueryWriteResult(PVGASTATE pThis, PVGASTATECC pThisCC, SVGAGuestPtr const *pGuestResult,
781 SVGA3dQueryState enmState, uint32_t u32Result)
782{
783 SVGA3dQueryResult queryResult;
784 queryResult.totalSize = sizeof(queryResult); /* Set by guest before query is ended. */
785 queryResult.state = enmState; /* Set by host or guest. See SVGA3dQueryState. */
786 queryResult.result32 = u32Result;
787
788 int rc = vmsvgaR3GmrTransfer(pThis, pThisCC, SVGA3D_READ_HOST_VRAM,
789 (uint8_t *)&queryResult, sizeof(queryResult), 0, sizeof(queryResult),
790 *pGuestResult, 0, sizeof(queryResult), sizeof(queryResult), 1);
791 AssertRC(rc);
792 return rc;
793}
794
795/* Used with saved state. */
796int vmsvga3dQueryCreate(PVGASTATECC pThisCC, uint32_t cid, SVGA3dQueryType type)
797{
798 PVMSVGAR3STATE const pSvgaR3State = pThisCC->svga.pSvgaR3State;
799 AssertReturn(pSvgaR3State->pFuncsVGPU9, VERR_NOT_IMPLEMENTED);
800
801 PVMSVGA3DSTATE pState = pThisCC->svga.p3dState;
802 AssertReturn(pState, VERR_NO_MEMORY);
803
804 LogFunc(("cid=%u type=%d\n", cid, type));
805
806 PVMSVGA3DCONTEXT pContext;
807 int rc = vmsvga3dContextFromCid(pState, cid, &pContext);
808 AssertRCReturn(rc, rc);
809
810 if (type == SVGA3D_QUERYTYPE_OCCLUSION)
811 {
812 VMSVGA3DQUERY *p = &pContext->occlusion;
813 if (!VMSVGA3DQUERY_EXISTS(p))
814 {
815 rc = pSvgaR3State->pFuncsVGPU9->pfnOcclusionQueryCreate(pThisCC, pContext);
816 AssertRCReturn(rc, rc);
817 }
818
819 return VINF_SUCCESS;
820 }
821
822 /* Nothing else for VGPU9. */
823 AssertFailedReturn(VERR_NOT_IMPLEMENTED);
824}
825
826int vmsvga3dQueryBegin(PVGASTATECC pThisCC, uint32_t cid, SVGA3dQueryType type)
827{
828 PVMSVGAR3STATE const pSvgaR3State = pThisCC->svga.pSvgaR3State;
829 AssertReturn(pSvgaR3State->pFuncsVGPU9, VERR_NOT_IMPLEMENTED);
830
831 PVMSVGA3DSTATE pState = pThisCC->svga.p3dState;
832 AssertReturn(pState, VERR_NO_MEMORY);
833
834 LogFunc(("cid=%u type=%d\n", cid, type));
835
836 PVMSVGA3DCONTEXT pContext;
837 int rc = vmsvga3dContextFromCid(pState, cid, &pContext);
838 AssertRCReturn(rc, rc);
839
840 if (type == SVGA3D_QUERYTYPE_OCCLUSION)
841 {
842 VMSVGA3DQUERY *p = &pContext->occlusion;
843 if (!VMSVGA3DQUERY_EXISTS(p))
844 {
845 /* Lazy creation of the query object. */
846 rc = pSvgaR3State->pFuncsVGPU9->pfnOcclusionQueryCreate(pThisCC, pContext);
847 AssertRCReturn(rc, rc);
848 }
849
850 rc = pSvgaR3State->pFuncsVGPU9->pfnOcclusionQueryBegin(pThisCC, pContext);
851 AssertRCReturn(rc, rc);
852
853 p->enmQueryState = VMSVGA3DQUERYSTATE_BUILDING;
854 p->u32QueryResult = 0;
855
856 return VINF_SUCCESS;
857 }
858
859 /* Nothing else for VGPU9. */
860 AssertFailedReturn(VERR_NOT_IMPLEMENTED);
861}
862
863int vmsvga3dQueryEnd(PVGASTATECC pThisCC, uint32_t cid, SVGA3dQueryType type)
864{
865 PVMSVGAR3STATE const pSvgaR3State = pThisCC->svga.pSvgaR3State;
866 AssertReturn(pSvgaR3State->pFuncsVGPU9, VERR_NOT_IMPLEMENTED);
867
868 PVMSVGA3DSTATE pState = pThisCC->svga.p3dState;
869 AssertReturn(pState, VERR_NO_MEMORY);
870
871 LogFunc(("cid=%u type=%d\n", cid, type));
872
873 PVMSVGA3DCONTEXT pContext;
874 int rc = vmsvga3dContextFromCid(pState, cid, &pContext);
875 AssertRCReturn(rc, rc);
876
877 if (type == SVGA3D_QUERYTYPE_OCCLUSION)
878 {
879 VMSVGA3DQUERY *p = &pContext->occlusion;
880 Assert(p->enmQueryState == VMSVGA3DQUERYSTATE_BUILDING);
881 AssertMsgReturn(VMSVGA3DQUERY_EXISTS(p), ("Query is NULL\n"), VERR_INTERNAL_ERROR);
882
883 rc = pSvgaR3State->pFuncsVGPU9->pfnOcclusionQueryEnd(pThisCC, pContext);
884 AssertRCReturn(rc, rc);
885
886 p->enmQueryState = VMSVGA3DQUERYSTATE_ISSUED;
887 return VINF_SUCCESS;
888 }
889
890 /* Nothing else for VGPU9. */
891 AssertFailedReturn(VERR_NOT_IMPLEMENTED);
892}
893
894int vmsvga3dQueryWait(PVGASTATECC pThisCC, uint32_t cid, SVGA3dQueryType type, PVGASTATE pThis, SVGAGuestPtr const *pGuestResult)
895{
896 PVMSVGAR3STATE const pSvgaR3State = pThisCC->svga.pSvgaR3State;
897 AssertReturn(pSvgaR3State->pFuncsVGPU9, VERR_NOT_IMPLEMENTED);
898
899 PVMSVGA3DSTATE pState = pThisCC->svga.p3dState;
900 AssertReturn(pState, VERR_NO_MEMORY);
901
902 LogFunc(("cid=%u type=%d guestResult GMR%d:0x%x\n", cid, type, pGuestResult->gmrId, pGuestResult->offset));
903
904 PVMSVGA3DCONTEXT pContext;
905 int rc = vmsvga3dContextFromCid(pState, cid, &pContext);
906 AssertRCReturn(rc, rc);
907
908 if (type == SVGA3D_QUERYTYPE_OCCLUSION)
909 {
910 VMSVGA3DQUERY *p = &pContext->occlusion;
911 if (VMSVGA3DQUERY_EXISTS(p))
912 {
913 if (p->enmQueryState == VMSVGA3DQUERYSTATE_ISSUED)
914 {
915 /* Only if not already in SIGNALED state,
916 * i.e. not a second read from the guest or after restoring saved state.
917 */
918 uint32_t u32Pixels = 0;
919 rc = pSvgaR3State->pFuncsVGPU9->pfnOcclusionQueryGetData(pThisCC, pContext, &u32Pixels);
920 if (RT_SUCCESS(rc))
921 {
922 p->enmQueryState = VMSVGA3DQUERYSTATE_SIGNALED;
923 p->u32QueryResult += u32Pixels; /* += because it might contain partial result from saved state. */
924 }
925 }
926
927 if (RT_SUCCESS(rc))
928 {
929 /* pGuestResult can be NULL when saving the state. */
930 if (pGuestResult)
931 {
932 /* Return data to the guest. */
933 vmsvga3dQueryWriteResult(pThis, pThisCC, pGuestResult, SVGA3D_QUERYSTATE_SUCCEEDED, p->u32QueryResult);
934 }
935 return VINF_SUCCESS;
936 }
937 }
938 else
939 {
940 AssertMsgFailed(("GetData Query is NULL\n"));
941 }
942
943 rc = VERR_INTERNAL_ERROR;
944 }
945 else
946 {
947 rc = VERR_NOT_IMPLEMENTED;
948 }
949
950 if (pGuestResult)
951 vmsvga3dQueryWriteResult(pThis, pThisCC, pGuestResult, SVGA3D_QUERYSTATE_FAILED, 0);
952 AssertFailedReturn(rc);
953}
954
955int vmsvga3dSurfaceBlitToScreen(PVGASTATE pThis, PVGASTATECC pThisCC, uint32_t idDstScreen, SVGASignedRect destRect,
956 SVGA3dSurfaceImageId srcImage, SVGASignedRect srcRect, uint32_t cRects, SVGASignedRect *pRect)
957{
958 /* Requires SVGA_FIFO_CAP_SCREEN_OBJECT support */
959 LogFunc(("dest=%d (%d,%d)(%d,%d) sid=%u (face=%d, mipmap=%d) (%d,%d)(%d,%d) cRects=%d\n",
960 idDstScreen, destRect.left, destRect.top, destRect.right, destRect.bottom, srcImage.sid, srcImage.face, srcImage.mipmap,
961 srcRect.left, srcRect.top, srcRect.right, srcRect.bottom, cRects));
962 for (uint32_t i = 0; i < cRects; i++)
963 {
964 LogFunc(("clipping rect[%d] (%d,%d)(%d,%d)\n", i, pRect[i].left, pRect[i].top, pRect[i].right, pRect[i].bottom));
965 }
966
967 VMSVGASCREENOBJECT *pScreen = vmsvgaR3GetScreenObject(pThisCC, idDstScreen);
968 AssertReturn(pScreen, VERR_INTERNAL_ERROR);
969
970 /* vmwgfx driver does not always initialize srcImage.mipmap and srcImage.face. They are assumed to be zero. */
971 SVGA3dSurfaceImageId src;
972 src.sid = srcImage.sid;
973 src.mipmap = 0;
974 src.face = 0;
975
976 PVMSVGAR3STATE const pSvgaR3State = pThisCC->svga.pSvgaR3State;
977 if (pScreen->pHwScreen)
978 {
979 /* Use the backend accelerated method, if available. */
980 if (pSvgaR3State->pFuncs3D)
981 {
982 int rc = pSvgaR3State->pFuncs3D->pfnSurfaceBlitToScreen(pThisCC, pScreen, destRect, src, srcRect, cRects, pRect);
983 if (rc == VINF_SUCCESS)
984 {
985 return VINF_SUCCESS;
986 }
987 }
988 }
989
990 if (pSvgaR3State->pFuncsMap)
991 return vmsvga3dScreenUpdate(pThisCC, idDstScreen, destRect, src, srcRect, cRects, pRect);
992
993 /** @todo scaling */
994 AssertReturn(destRect.right - destRect.left == srcRect.right - srcRect.left && destRect.bottom - destRect.top == srcRect.bottom - srcRect.top, VERR_INVALID_PARAMETER);
995
996 SVGA3dCopyBox box;
997 SVGAGuestImage dest;
998
999 box.srcz = 0;
1000 box.z = 0;
1001 box.d = 1;
1002
1003 dest.ptr.gmrId = SVGA_GMR_FRAMEBUFFER;
1004 dest.ptr.offset = pScreen->offVRAM;
1005 dest.pitch = pScreen->cbPitch;
1006
1007 if (cRects == 0)
1008 {
1009 /* easy case; no clipping */
1010
1011 /* SVGA_3D_CMD_SURFACE_DMA:
1012 * 'define the "source" in each copyBox as the guest image and the
1013 * "destination" as the host image, regardless of transfer direction.'
1014 *
1015 * Since the BlitToScreen operation transfers from a host surface to the guest VRAM,
1016 * it must set the copyBox "source" to the guest destination coords and
1017 * the copyBox "destination" to the host surface source coords.
1018 */
1019 /* Host image. */
1020 box.x = srcRect.left;
1021 box.y = srcRect.top;
1022 box.w = srcRect.right - srcRect.left;
1023 box.h = srcRect.bottom - srcRect.top;
1024 /* Guest image. */
1025 box.srcx = destRect.left;
1026 box.srcy = destRect.top;
1027
1028 int rc = vmsvga3dSurfaceDMA(pThis, pThisCC, dest, src, SVGA3D_READ_HOST_VRAM, 1, &box);
1029 AssertRCReturn(rc, rc);
1030
1031 /* Update the guest image, which is at box.src. */
1032 vmsvgaR3UpdateScreen(pThisCC, pScreen, box.srcx, box.srcy, box.w, box.h);
1033 }
1034 else
1035 {
1036 /** @todo merge into one SurfaceDMA call */
1037 for (uint32_t i = 0; i < cRects; i++)
1038 {
1039 /* "The clip rectangle coordinates are measured
1040 * relative to the top-left corner of destRect."
1041 * Therefore they are relative to the top-left corner of srcRect as well.
1042 */
1043
1044 /* Host image. See 'SVGA_3D_CMD_SURFACE_DMA:' comment in the 'if' branch. */
1045 box.x = srcRect.left + pRect[i].left;
1046 box.y = srcRect.top + pRect[i].top;
1047 box.w = pRect[i].right - pRect[i].left;
1048 box.h = pRect[i].bottom - pRect[i].top;
1049 /* Guest image. The target screen memory is currently in the guest VRAM. */
1050 box.srcx = destRect.left + pRect[i].left;
1051 box.srcy = destRect.top + pRect[i].top;
1052
1053 int rc = vmsvga3dSurfaceDMA(pThis, pThisCC, dest, src, SVGA3D_READ_HOST_VRAM, 1, &box);
1054 AssertRCReturn(rc, rc);
1055
1056 /* Update the guest image, which is at box.src. */
1057 vmsvgaR3UpdateScreen(pThisCC, pScreen, box.srcx, box.srcy, box.w, box.h);
1058 }
1059 }
1060
1061 return VINF_SUCCESS;
1062}
1063
1064int vmsvga3dScreenUpdate(PVGASTATECC pThisCC, uint32_t idDstScreen, SVGASignedRect const &dstRect,
1065 SVGA3dSurfaceImageId const &srcImage, SVGASignedRect const &srcRect,
1066 uint32_t cDstClipRects, SVGASignedRect *paDstClipRect)
1067{
1068 //DEBUG_BREAKPOINT_TEST();
1069 PVMSVGAR3STATE const pSvgaR3State = pThisCC->svga.pSvgaR3State;
1070
1071#ifdef LOG_ENABLED
1072 LogFunc(("[%u] %d,%d %d,%d (%dx%d) -> %d,%d %d,%d (%dx%d), %u clip rects\n",
1073 idDstScreen, srcRect.left, srcRect.top, srcRect.right, srcRect.bottom,
1074 srcRect.right - srcRect.left, srcRect.bottom - srcRect.top,
1075 dstRect.left, dstRect.top, dstRect.right, dstRect.bottom,
1076 dstRect.right - dstRect.left, dstRect.bottom - dstRect.top, cDstClipRects));
1077 for (uint32_t i = 0; i < cDstClipRects; i++)
1078 {
1079 LogFunc((" [%u] %d,%d %d,%d (%dx%d)\n",
1080 i, paDstClipRect[i].left, paDstClipRect[i].top, paDstClipRect[i].right, paDstClipRect[i].bottom,
1081 paDstClipRect[i].right - paDstClipRect[i].left, paDstClipRect[i].bottom - paDstClipRect[i].top));
1082 }
1083#endif
1084
1085 PVMSVGA3DSURFACE pSurface;
1086 int rc = vmsvga3dSurfaceFromSid(pThisCC->svga.p3dState, srcImage.sid, &pSurface);
1087 AssertRCReturn(rc, rc);
1088
1089 /* Update the screen from a surface. */
1090 ASSERT_GUEST_RETURN(idDstScreen < RT_ELEMENTS(pSvgaR3State->aScreens), VERR_INVALID_PARAMETER);
1091 RT_UNTRUSTED_VALIDATED_FENCE();
1092
1093 VMSVGASCREENOBJECT *pScreen = &pSvgaR3State->aScreens[idDstScreen];
1094
1095 uint32_t const cbScreenPixel = (pScreen->cBpp + 7) / 8;
1096 ASSERT_GUEST_RETURN(cbScreenPixel == pSurface->cbBlock,
1097 VERR_INVALID_PARAMETER); /* Format conversion is not supported. */
1098
1099 if ( srcRect.right <= srcRect.left
1100 || srcRect.bottom <= srcRect.top)
1101 return VINF_SUCCESS; /* Empty src rect. */
1102
1103 if ( dstRect.right <= dstRect.left
1104 || dstRect.bottom <= dstRect.top)
1105 return VINF_SUCCESS; /* Empty dst rect. */
1106 RT_UNTRUSTED_VALIDATED_FENCE();
1107
1108 ASSERT_GUEST_RETURN( srcRect.right - srcRect.left == dstRect.right - dstRect.left
1109 && srcRect.bottom - srcRect.top == dstRect.bottom - dstRect.top,
1110 VERR_INVALID_PARAMETER); /* Stretch is not supported. */
1111
1112 /* Destination box should be within the screen rectangle. */
1113 SVGA3dBox dstBox;
1114 dstBox.x = dstRect.left;
1115 dstBox.y = dstRect.top;
1116 dstBox.z = 0;
1117 dstBox.w = dstRect.right - dstRect.left;
1118 dstBox.h = dstRect.bottom - dstRect.top;
1119 dstBox.d = 1;
1120
1121 SVGA3dSize dstClippingSize;
1122 dstClippingSize.width = pScreen->cWidth;
1123 dstClippingSize.height = pScreen->cHeight;
1124 dstClippingSize.depth = 1;
1125
1126 vmsvgaR3ClipBox(&dstClippingSize, &dstBox);
1127 ASSERT_GUEST_RETURN(dstBox.w > 0 && dstBox.h > 0, VERR_INVALID_PARAMETER);
1128 RT_UNTRUSTED_VALIDATED_FENCE();
1129
1130 /* All dst clip rects will be clipped by the dst box because
1131 * "The clip rectangle coordinates are measured relative to the top-left corner of destRect."
1132 */
1133 dstClippingSize.width = dstBox.w;
1134 dstClippingSize.height = dstBox.h;
1135 dstClippingSize.depth = 1;
1136
1137 SVGA3dBox srcBox; /* SurfaceMap will clip the box as necessary (srcMap.box). */
1138 srcBox.x = srcRect.left;
1139 srcBox.y = srcRect.top;
1140 srcBox.z = 0;
1141 srcBox.w = srcRect.right - srcRect.left;
1142 srcBox.h = srcRect.bottom - srcRect.top;
1143 srcBox.d = 1;
1144
1145 VMSVGA3D_MAPPED_SURFACE srcMap;
1146 rc = vmsvga3dSurfaceMap(pThisCC, &srcImage, &srcBox, VMSVGA3D_SURFACE_MAP_READ, &srcMap);
1147 if (RT_SUCCESS(rc))
1148 {
1149 /* Clipping rectangle. */
1150 SVGASignedRect srcBoundRect;
1151 srcBoundRect.left = srcMap.box.x;
1152 srcBoundRect.top = srcMap.box.y;
1153 srcBoundRect.right = srcMap.box.x + srcMap.box.w;
1154 srcBoundRect.bottom = srcMap.box.y + srcMap.box.h;
1155
1156 /* Clipping rectangle relative to the original srcRect. */
1157 srcBoundRect.left -= srcRect.left;
1158 srcBoundRect.top -= srcRect.top;
1159 srcBoundRect.right -= srcRect.left;
1160 srcBoundRect.bottom -= srcRect.top;
1161
1162 uint8_t const *pu8Src = (uint8_t *)srcMap.pvData;
1163
1164 uint32_t const cbDst = pScreen->cHeight * pScreen->cbPitch;
1165 uint8_t *pu8Dst;
1166 if (pScreen->pvScreenBitmap)
1167 pu8Dst = (uint8_t *)pScreen->pvScreenBitmap;
1168 else
1169 pu8Dst = (uint8_t *)pThisCC->pbVRam + pScreen->offVRAM;
1170
1171 SVGASignedRect dstClipRect;
1172 if (cDstClipRects == 0)
1173 {
1174 /* Entire source rect "relative to the top-left corner of destRect." */
1175 dstClipRect.left = 0;
1176 dstClipRect.top = 0;
1177 dstClipRect.right = dstBox.w;
1178 dstClipRect.bottom = dstBox.h;
1179
1180 cDstClipRects = 1;
1181 paDstClipRect = &dstClipRect;
1182 }
1183
1184 for (uint32_t i = 0; i < cDstClipRects; i++)
1185 {
1186 /* Clip rects are relative to corners of src and dst rectangles. */
1187 SVGASignedRect clipRect = paDstClipRect[i];
1188
1189 /* Clip the rectangle by mapped source box. */
1190 vmsvgaR3ClipRect(&srcBoundRect, &clipRect);
1191
1192 SVGA3dBox clipBox;
1193 clipBox.x = clipRect.left;
1194 clipBox.y = clipRect.top;
1195 clipBox.z = 0;
1196 clipBox.w = clipRect.right - clipRect.left;
1197 clipBox.h = clipRect.bottom - clipRect.top;
1198 clipBox.d = 1;
1199
1200 vmsvgaR3ClipBox(&dstClippingSize, &clipBox);
1201 ASSERT_GUEST_CONTINUE(clipBox.w > 0 && clipBox.h > 0);
1202
1203 /* 'pu8Src' points to the mapped 'srcRect'. Take the clipping box into account. */
1204 uint8_t const *pu8SrcBox = pu8Src
1205 + ((clipBox.x + pSurface->cxBlock - 1) / pSurface->cxBlock) * pSurface->cxBlock * pSurface->cbBlock
1206 + ((clipBox.y + pSurface->cyBlock - 1) / pSurface->cyBlock) * pSurface->cyBlock * srcMap.cbRowPitch;
1207
1208 /* Calculate the offset of destination box in the screen buffer. */
1209 uint32_t const offDstBox = (dstBox.x + clipBox.x) * cbScreenPixel + (dstBox.y + clipBox.y) * pScreen->cbPitch;
1210
1211 ASSERT_GUEST_BREAK( offDstBox <= cbDst
1212 && pScreen->cbPitch * (clipBox.h - 1) + cbScreenPixel * clipBox.w <= cbDst - offDstBox);
1213 RT_UNTRUSTED_VALIDATED_FENCE();
1214
1215 uint8_t *pu8DstBox = pu8Dst + offDstBox;
1216
1217 if ( pSurface->format == SVGA3D_R8G8B8A8_UNORM
1218 || pSurface->format == SVGA3D_R8G8B8A8_UNORM_SRGB)
1219 {
1220 for (uint32_t iRow = 0; iRow < clipBox.h; ++iRow)
1221 {
1222 for (uint32_t x = 0; x < clipBox.w * 4; x += 4) /* 'x' is a byte index. */
1223 {
1224 pu8DstBox[x ] = pu8SrcBox[x + 2];
1225 pu8DstBox[x + 1] = pu8SrcBox[x + 1];
1226 pu8DstBox[x + 2] = pu8SrcBox[x ];
1227 pu8DstBox[x + 3] = pu8SrcBox[x + 3];
1228 }
1229
1230 pu8SrcBox += srcMap.cbRowPitch;
1231 pu8DstBox += pScreen->cbPitch;
1232 }
1233 }
1234 else
1235 {
1236 for (uint32_t iRow = 0; iRow < clipBox.h; ++iRow)
1237 {
1238 memcpy(pu8DstBox, pu8SrcBox, cbScreenPixel * clipBox.w);
1239
1240 pu8SrcBox += srcMap.cbRowPitch;
1241 pu8DstBox += pScreen->cbPitch;
1242 }
1243 }
1244 }
1245
1246 vmsvga3dSurfaceUnmap(pThisCC, &srcImage, &srcMap, /* fWritten = */ false);
1247
1248 vmsvgaR3UpdateScreen(pThisCC, pScreen, dstBox.x, dstBox.y, dstBox.w, dstBox.h);
1249 }
1250
1251 return rc;
1252}
1253
1254int vmsvga3dCommandPresent(PVGASTATE pThis, PVGASTATECC pThisCC, uint32_t sid, uint32_t cRects, SVGA3dCopyRect *pRect)
1255{
1256 /* Deprecated according to svga3d_reg.h. */
1257 PVMSVGA3DSTATE pState = pThisCC->svga.p3dState;
1258 AssertReturn(pState, VERR_NO_MEMORY);
1259
1260 PVMSVGA3DSURFACE pSurface;
1261 int rc = vmsvga3dSurfaceFromSid(pState, sid, &pSurface);
1262 AssertRCReturn(rc, rc);
1263
1264 /** @todo Detect screen from coords? Or split rect to screens? */
1265 VMSVGASCREENOBJECT *pScreen = vmsvgaR3GetScreenObject(pThisCC, 0);
1266 AssertReturn(pScreen, VERR_INTERNAL_ERROR);
1267
1268 /* If there are no recangles specified, just grab a screenful. */
1269 SVGA3dCopyRect DummyRect;
1270 if (cRects != 0)
1271 { /* likely */ }
1272 else
1273 {
1274 /** @todo Find the usecase for this or check what the original device does.
1275 * The original code was doing some scaling based on the surface
1276 * size... */
1277 AssertMsgFailed(("No rects to present. Who is doing that and what do they actually expect?\n"));
1278 DummyRect.x = DummyRect.srcx = 0;
1279 DummyRect.y = DummyRect.srcy = 0;
1280 DummyRect.w = pScreen->cWidth;
1281 DummyRect.h = pScreen->cHeight;
1282 cRects = 1;
1283 pRect = &DummyRect;
1284 }
1285
1286 uint32_t i;
1287 for (i = 0; i < cRects; ++i)
1288 {
1289 uint32_t idDstScreen = 0; /** @todo Use virtual coords: SVGA_ID_INVALID. */
1290 SVGASignedRect destRect;
1291 destRect.left = pRect[i].x;
1292 destRect.top = pRect[i].y;
1293 destRect.right = pRect[i].x + pRect[i].w;
1294 destRect.bottom = pRect[i].y + pRect[i].h;
1295
1296 SVGA3dSurfaceImageId src;
1297 src.sid = sid;
1298 src.face = 0;
1299 src.mipmap = 0;
1300
1301 SVGASignedRect srcRect;
1302 srcRect.left = pRect[i].srcx;
1303 srcRect.top = pRect[i].srcy;
1304 srcRect.right = pRect[i].srcx + pRect[i].w;
1305 srcRect.bottom = pRect[i].srcy + pRect[i].h;
1306
1307 /* Entire rect. */
1308 rc = vmsvga3dSurfaceBlitToScreen(pThis, pThisCC, idDstScreen, destRect, src, srcRect, 0, NULL);
1309 AssertRCReturn(rc, rc);
1310 }
1311
1312 return VINF_SUCCESS;
1313}
1314
1315int vmsvga3dDefineScreen(PVGASTATE pThis, PVGASTATECC pThisCC, VMSVGASCREENOBJECT *pScreen)
1316{
1317 PVMSVGAR3STATE const pSvgaR3State = pThisCC->svga.pSvgaR3State;
1318 AssertReturn(pSvgaR3State->pFuncs3D, VERR_NOT_IMPLEMENTED);
1319
1320 if (pScreen->pHwScreen)
1321 {
1322 pSvgaR3State->pFuncs3D->pfnDestroyScreen(pThisCC, pScreen);
1323 }
1324
1325 int rc = pSvgaR3State->pFuncs3D->pfnDefineScreen(pThis, pThisCC, pScreen);
1326 if (RT_SUCCESS(rc))
1327 {
1328 LogRelMax(1, ("VMSVGA: using accelerated graphics output\n"));
1329 }
1330 return rc;
1331}
1332
1333int vmsvga3dDestroyScreen(PVGASTATECC pThisCC, VMSVGASCREENOBJECT *pScreen)
1334{
1335 PVMSVGAR3STATE const pSvgaR3State = pThisCC->svga.pSvgaR3State;
1336 AssertReturn(pSvgaR3State->pFuncs3D, VERR_NOT_IMPLEMENTED);
1337
1338 return pSvgaR3State->pFuncs3D->pfnDestroyScreen(pThisCC, pScreen);
1339}
1340
1341int vmsvga3dSurfaceInvalidate(PVGASTATECC pThisCC, uint32_t sid, uint32_t face, uint32_t mipmap)
1342{
1343 PVMSVGA3DSTATE pState = pThisCC->svga.p3dState;
1344 AssertReturn(pState, VERR_INVALID_STATE);
1345
1346 PVMSVGA3DSURFACE pSurface;
1347 int rc = vmsvga3dSurfaceFromSid(pState, sid, &pSurface);
1348 AssertRCReturn(rc, rc);
1349
1350 if (face == SVGA_ID_INVALID && mipmap == SVGA_ID_INVALID)
1351 {
1352 /* This is a notification that "All images can be lost", i.e. the backend surface is not needed anymore. */
1353 PVMSVGAR3STATE const pSvgaR3State = pThisCC->svga.pSvgaR3State;
1354 if (pSvgaR3State->pFuncs3D)
1355 pSvgaR3State->pFuncs3D->pfnSurfaceDestroy(pThisCC, false, pSurface);
1356
1357 for (uint32_t i = 0; i < pSurface->cLevels * pSurface->surfaceDesc.numArrayElements; ++i)
1358 {
1359 PVMSVGA3DMIPMAPLEVEL pMipmapLevel = &pSurface->paMipmapLevels[i];
1360 pMipmapLevel->fDirty = true;
1361 }
1362 }
1363 else
1364 {
1365 PVMSVGA3DMIPMAPLEVEL pMipmapLevel;
1366 rc = vmsvga3dMipmapLevel(pSurface, face, mipmap, &pMipmapLevel);
1367 AssertRCReturn(rc, rc);
1368
1369 /* Invalidate views, etc. */
1370 PVMSVGAR3STATE const pSvgaR3State = pThisCC->svga.pSvgaR3State;
1371 if (pSvgaR3State->pFuncs3D)
1372 pSvgaR3State->pFuncs3D->pfnSurfaceInvalidateImage(pThisCC, pSurface, face, mipmap);
1373
1374 pMipmapLevel->fDirty = true;
1375 }
1376 pSurface->fDirty = true;
1377
1378 return rc;
1379}
1380
1381
1382/*
1383 *
1384 * 3D
1385 *
1386 */
1387
1388int vmsvga3dQueryCaps(PVGASTATECC pThisCC, SVGA3dDevCapIndex idx3dCaps, uint32_t *pu32Val)
1389{
1390 PVMSVGAR3STATE const pSvgaR3State = pThisCC->svga.pSvgaR3State;
1391 AssertReturn(pSvgaR3State->pFuncs3D, VERR_NOT_IMPLEMENTED);
1392 return pSvgaR3State->pFuncs3D->pfnQueryCaps(pThisCC, idx3dCaps, pu32Val);
1393}
1394
1395int vmsvga3dChangeMode(PVGASTATECC pThisCC)
1396{
1397 PVMSVGAR3STATE const pSvgaR3State = pThisCC->svga.pSvgaR3State;
1398 AssertReturn(pSvgaR3State->pFuncs3D, VERR_NOT_IMPLEMENTED);
1399 return pSvgaR3State->pFuncs3D->pfnChangeMode(pThisCC);
1400}
1401
1402int vmsvga3dSurfaceCopy(PVGASTATECC pThisCC, SVGA3dSurfaceImageId dest, SVGA3dSurfaceImageId src, uint32_t cCopyBoxes, SVGA3dCopyBox *pBox)
1403{
1404 PVMSVGAR3STATE const pSvgaR3State = pThisCC->svga.pSvgaR3State;
1405 AssertReturn(pSvgaR3State->pFuncs3D, VERR_NOT_IMPLEMENTED);
1406 return pSvgaR3State->pFuncs3D->pfnSurfaceCopy(pThisCC, dest, src, cCopyBoxes, pBox);
1407}
1408
1409void vmsvga3dUpdateHostScreenViewport(PVGASTATECC pThisCC, uint32_t idScreen, VMSVGAVIEWPORT const *pOldViewport)
1410{
1411 PVMSVGAR3STATE const pSvgaR3State = pThisCC->svga.pSvgaR3State;
1412 AssertReturnVoid(pSvgaR3State->pFuncs3D);
1413 pSvgaR3State->pFuncs3D->pfnUpdateHostScreenViewport(pThisCC, idScreen, pOldViewport);
1414}
1415
1416/**
1417 * Updates the heap buffers for all surfaces or one specific one.
1418 *
1419 * @param pThisCC The VGA/VMSVGA state for ring-3.
1420 * @param sid The surface ID, UINT32_MAX if all.
1421 * @thread VMSVGAFIFO
1422 */
1423void vmsvga3dUpdateHeapBuffersForSurfaces(PVGASTATECC pThisCC, uint32_t sid)
1424{
1425 PVMSVGAR3STATE const pSvgaR3State = pThisCC->svga.pSvgaR3State;
1426 AssertReturnVoid(pSvgaR3State->pFuncs3D);
1427
1428 PVMSVGA3DSTATE pState = pThisCC->svga.p3dState;
1429 AssertReturnVoid(pState);
1430
1431 if (sid == UINT32_MAX)
1432 {
1433 uint32_t cSurfaces = pState->cSurfaces;
1434 for (sid = 0; sid < cSurfaces; sid++)
1435 {
1436 PVMSVGA3DSURFACE pSurface = pState->papSurfaces[sid];
1437 if (pSurface && pSurface->id == sid)
1438 pSvgaR3State->pFuncs3D->pfnSurfaceUpdateHeapBuffers(pThisCC, pSurface);
1439 }
1440 }
1441 else if (sid < pState->cSurfaces)
1442 {
1443 PVMSVGA3DSURFACE pSurface = pState->papSurfaces[sid];
1444 if (pSurface && pSurface->id == sid)
1445 pSvgaR3State->pFuncs3D->pfnSurfaceUpdateHeapBuffers(pThisCC, pSurface);
1446 }
1447}
1448
1449
1450/*
1451 *
1452 * VGPU9
1453 *
1454 */
1455
1456int vmsvga3dContextDefine(PVGASTATECC pThisCC, uint32_t cid)
1457{
1458 PVMSVGAR3STATE const pSvgaR3State = pThisCC->svga.pSvgaR3State;
1459 AssertReturn(pSvgaR3State->pFuncsVGPU9, VERR_NOT_IMPLEMENTED);
1460 return pSvgaR3State->pFuncsVGPU9->pfnContextDefine(pThisCC, cid);
1461}
1462
1463int vmsvga3dContextDestroy(PVGASTATECC pThisCC, uint32_t cid)
1464{
1465 PVMSVGAR3STATE const pSvgaR3State = pThisCC->svga.pSvgaR3State;
1466 AssertReturn(pSvgaR3State->pFuncsVGPU9, VERR_NOT_IMPLEMENTED);
1467 return pSvgaR3State->pFuncsVGPU9->pfnContextDestroy(pThisCC, cid);
1468}
1469
1470int vmsvga3dSetTransform(PVGASTATECC pThisCC, uint32_t cid, SVGA3dTransformType type, float matrix[16])
1471{
1472 PVMSVGAR3STATE const pSvgaR3State = pThisCC->svga.pSvgaR3State;
1473 AssertReturn(pSvgaR3State->pFuncsVGPU9, VERR_NOT_IMPLEMENTED);
1474 return pSvgaR3State->pFuncsVGPU9->pfnSetTransform(pThisCC, cid, type, matrix);
1475}
1476
1477int vmsvga3dSetZRange(PVGASTATECC pThisCC, uint32_t cid, SVGA3dZRange zRange)
1478{
1479 PVMSVGAR3STATE const pSvgaR3State = pThisCC->svga.pSvgaR3State;
1480 AssertReturn(pSvgaR3State->pFuncsVGPU9, VERR_NOT_IMPLEMENTED);
1481 return pSvgaR3State->pFuncsVGPU9->pfnSetZRange(pThisCC, cid, zRange);
1482}
1483
1484int vmsvga3dSetRenderState(PVGASTATECC pThisCC, uint32_t cid, uint32_t cRenderStates, SVGA3dRenderState *pRenderState)
1485{
1486 PVMSVGAR3STATE const pSvgaR3State = pThisCC->svga.pSvgaR3State;
1487 AssertReturn(pSvgaR3State->pFuncsVGPU9, VERR_NOT_IMPLEMENTED);
1488 return pSvgaR3State->pFuncsVGPU9->pfnSetRenderState(pThisCC, cid, cRenderStates, pRenderState);
1489}
1490
1491int vmsvga3dSetRenderTarget(PVGASTATECC pThisCC, uint32_t cid, SVGA3dRenderTargetType type, SVGA3dSurfaceImageId target)
1492{
1493 PVMSVGAR3STATE const pSvgaR3State = pThisCC->svga.pSvgaR3State;
1494 AssertReturn(pSvgaR3State->pFuncsVGPU9, VERR_NOT_IMPLEMENTED);
1495 return pSvgaR3State->pFuncsVGPU9->pfnSetRenderTarget(pThisCC, cid, type, target);
1496}
1497
1498int vmsvga3dSetTextureState(PVGASTATECC pThisCC, uint32_t cid, uint32_t cTextureStates, SVGA3dTextureState *pTextureState)
1499{
1500 PVMSVGAR3STATE const pSvgaR3State = pThisCC->svga.pSvgaR3State;
1501 AssertReturn(pSvgaR3State->pFuncsVGPU9, VERR_NOT_IMPLEMENTED);
1502 return pSvgaR3State->pFuncsVGPU9->pfnSetTextureState(pThisCC, cid, cTextureStates, pTextureState);
1503}
1504
1505int vmsvga3dSetMaterial(PVGASTATECC pThisCC, uint32_t cid, SVGA3dFace face, SVGA3dMaterial *pMaterial)
1506{
1507 PVMSVGAR3STATE const pSvgaR3State = pThisCC->svga.pSvgaR3State;
1508 AssertReturn(pSvgaR3State->pFuncsVGPU9, VERR_NOT_IMPLEMENTED);
1509 return pSvgaR3State->pFuncsVGPU9->pfnSetMaterial(pThisCC, cid, face, pMaterial);
1510}
1511
1512int vmsvga3dSetLightData(PVGASTATECC pThisCC, uint32_t cid, uint32_t index, SVGA3dLightData *pData)
1513{
1514 PVMSVGAR3STATE const pSvgaR3State = pThisCC->svga.pSvgaR3State;
1515 AssertReturn(pSvgaR3State->pFuncsVGPU9, VERR_NOT_IMPLEMENTED);
1516 return pSvgaR3State->pFuncsVGPU9->pfnSetLightData(pThisCC, cid, index, pData);
1517}
1518
1519int vmsvga3dSetLightEnabled(PVGASTATECC pThisCC, uint32_t cid, uint32_t index, uint32_t enabled)
1520{
1521 PVMSVGAR3STATE const pSvgaR3State = pThisCC->svga.pSvgaR3State;
1522 AssertReturn(pSvgaR3State->pFuncsVGPU9, VERR_NOT_IMPLEMENTED);
1523 return pSvgaR3State->pFuncsVGPU9->pfnSetLightEnabled(pThisCC, cid, index, enabled);
1524}
1525
1526int vmsvga3dSetViewPort(PVGASTATECC pThisCC, uint32_t cid, SVGA3dRect *pRect)
1527{
1528 PVMSVGAR3STATE const pSvgaR3State = pThisCC->svga.pSvgaR3State;
1529 AssertReturn(pSvgaR3State->pFuncsVGPU9, VERR_NOT_IMPLEMENTED);
1530 return pSvgaR3State->pFuncsVGPU9->pfnSetViewPort(pThisCC, cid, pRect);
1531}
1532
1533int vmsvga3dSetClipPlane(PVGASTATECC pThisCC, uint32_t cid, uint32_t index, float plane[4])
1534{
1535 PVMSVGAR3STATE const pSvgaR3State = pThisCC->svga.pSvgaR3State;
1536 AssertReturn(pSvgaR3State->pFuncsVGPU9, VERR_NOT_IMPLEMENTED);
1537 return pSvgaR3State->pFuncsVGPU9->pfnSetClipPlane(pThisCC, cid, index, plane);
1538}
1539
1540int vmsvga3dCommandClear(PVGASTATECC pThisCC, uint32_t cid, SVGA3dClearFlag clearFlag, uint32_t color, float depth, uint32_t stencil, uint32_t cRects, SVGA3dRect *pRect)
1541{
1542 PVMSVGAR3STATE const pSvgaR3State = pThisCC->svga.pSvgaR3State;
1543 AssertReturn(pSvgaR3State->pFuncsVGPU9, VERR_NOT_IMPLEMENTED);
1544 return pSvgaR3State->pFuncsVGPU9->pfnCommandClear(pThisCC, cid, clearFlag, color, depth, stencil, cRects, pRect);
1545}
1546
1547int vmsvga3dDrawPrimitives(PVGASTATECC pThisCC, uint32_t cid, uint32_t numVertexDecls, SVGA3dVertexDecl *pVertexDecl, uint32_t numRanges, SVGA3dPrimitiveRange *pNumRange, uint32_t cVertexDivisor, SVGA3dVertexDivisor *pVertexDivisor)
1548{
1549 PVMSVGAR3STATE const pSvgaR3State = pThisCC->svga.pSvgaR3State;
1550 AssertReturn(pSvgaR3State->pFuncsVGPU9, VERR_NOT_IMPLEMENTED);
1551 return pSvgaR3State->pFuncsVGPU9->pfnDrawPrimitives(pThisCC, cid, numVertexDecls, pVertexDecl, numRanges, pNumRange, cVertexDivisor, pVertexDivisor);
1552}
1553
1554int vmsvga3dSetScissorRect(PVGASTATECC pThisCC, uint32_t cid, SVGA3dRect *pRect)
1555{
1556 PVMSVGAR3STATE const pSvgaR3State = pThisCC->svga.pSvgaR3State;
1557 AssertReturn(pSvgaR3State->pFuncsVGPU9, VERR_NOT_IMPLEMENTED);
1558 return pSvgaR3State->pFuncsVGPU9->pfnSetScissorRect(pThisCC, cid, pRect);
1559}
1560
1561int vmsvga3dGenerateMipmaps(PVGASTATECC pThisCC, uint32_t sid, SVGA3dTextureFilter filter)
1562{
1563 PVMSVGAR3STATE const pSvgaR3State = pThisCC->svga.pSvgaR3State;
1564 AssertReturn(pSvgaR3State->pFuncsVGPU9, VERR_NOT_IMPLEMENTED);
1565 return pSvgaR3State->pFuncsVGPU9->pfnGenerateMipmaps(pThisCC, sid, filter);
1566}
1567
1568int vmsvga3dShaderDefine(PVGASTATECC pThisCC, uint32_t cid, uint32_t shid, SVGA3dShaderType type, uint32_t cbData, uint32_t *pShaderData)
1569{
1570 PVMSVGAR3STATE const pSvgaR3State = pThisCC->svga.pSvgaR3State;
1571 AssertReturn(pSvgaR3State->pFuncsVGPU9, VERR_NOT_IMPLEMENTED);
1572 return pSvgaR3State->pFuncsVGPU9->pfnShaderDefine(pThisCC, cid, shid, type, cbData, pShaderData);
1573}
1574
1575int vmsvga3dShaderDestroy(PVGASTATECC pThisCC, uint32_t cid, uint32_t shid, SVGA3dShaderType type)
1576{
1577 PVMSVGAR3STATE const pSvgaR3State = pThisCC->svga.pSvgaR3State;
1578 AssertReturn(pSvgaR3State->pFuncsVGPU9, VERR_NOT_IMPLEMENTED);
1579 return pSvgaR3State->pFuncsVGPU9->pfnShaderDestroy(pThisCC, cid, shid, type);
1580}
1581
1582int vmsvga3dShaderSet(PVGASTATECC pThisCC, struct VMSVGA3DCONTEXT *pContext, uint32_t cid, SVGA3dShaderType type, uint32_t shid)
1583{
1584 PVMSVGAR3STATE const pSvgaR3State = pThisCC->svga.pSvgaR3State;
1585 AssertReturn(pSvgaR3State->pFuncsVGPU9, VERR_NOT_IMPLEMENTED);
1586 return pSvgaR3State->pFuncsVGPU9->pfnShaderSet(pThisCC, pContext, cid, type, shid);
1587}
1588
1589int vmsvga3dShaderSetConst(PVGASTATECC pThisCC, uint32_t cid, uint32_t reg, SVGA3dShaderType type, SVGA3dShaderConstType ctype, uint32_t cRegisters, uint32_t *pValues)
1590{
1591 PVMSVGAR3STATE const pSvgaR3State = pThisCC->svga.pSvgaR3State;
1592 AssertReturn(pSvgaR3State->pFuncsVGPU9, VERR_NOT_IMPLEMENTED);
1593 return pSvgaR3State->pFuncsVGPU9->pfnShaderSetConst(pThisCC, cid, reg, type, ctype, cRegisters, pValues);
1594}
1595
1596
1597/*
1598 *
1599 * Map
1600 *
1601 */
1602
1603void vmsvga3dSurfaceMapInit(VMSVGA3D_MAPPED_SURFACE *pMap, VMSVGA3D_SURFACE_MAP enmMapType, SVGA3dBox const *pBox,
1604 PVMSVGA3DSURFACE pSurface, void *pvData, uint32_t cbRowPitch, uint32_t cbDepthPitch)
1605{
1606 uint32_t const cxBlocks = (pBox->w + pSurface->cxBlock - 1) / pSurface->cxBlock;
1607 uint32_t const cyBlocks = (pBox->h + pSurface->cyBlock - 1) / pSurface->cyBlock;
1608
1609 pMap->enmMapType = enmMapType;
1610 pMap->format = pSurface->format;
1611 pMap->box = *pBox;
1612 pMap->cbBlock = pSurface->cbBlock;
1613 pMap->cbRow = cxBlocks * pSurface->cbBlock;
1614 pMap->cbRowPitch = cbRowPitch;
1615 pMap->cRows = cyBlocks;
1616 pMap->cbDepthPitch = cbDepthPitch;
1617 pMap->pvData = (uint8_t *)pvData
1618 + (pBox->x / pSurface->cxBlock) * pSurface->cbBlock
1619 + (pBox->y / pSurface->cyBlock) * cbRowPitch
1620 + pBox->z * cbDepthPitch;
1621}
1622
1623
1624int vmsvga3dSurfaceMap(PVGASTATECC pThisCC, SVGA3dSurfaceImageId const *pImage, SVGA3dBox const *pBox,
1625 VMSVGA3D_SURFACE_MAP enmMapType, VMSVGA3D_MAPPED_SURFACE *pMap)
1626{
1627 PVMSVGA3DSURFACE pSurface;
1628 int rc = vmsvga3dSurfaceFromSid(pThisCC->svga.p3dState, pImage->sid, &pSurface);
1629 AssertRCReturn(rc, rc);
1630
1631 if (VMSVGA3DSURFACE_HAS_HW_SURFACE(pSurface))
1632 {
1633 PVMSVGAR3STATE const pSvgaR3State = pThisCC->svga.pSvgaR3State;
1634 AssertReturn(pSvgaR3State->pFuncsMap, VERR_NOT_IMPLEMENTED);
1635 return pSvgaR3State->pFuncsMap->pfnSurfaceMap(pThisCC, pImage, pBox, enmMapType, pMap);
1636 }
1637
1638 PVMSVGA3DMIPMAPLEVEL pMipLevel;
1639 rc = vmsvga3dMipmapLevel(pSurface, pImage->face, pImage->mipmap, &pMipLevel);
1640 ASSERT_GUEST_RETURN(RT_SUCCESS(rc), rc);
1641
1642 if (!pMipLevel->pSurfaceData)
1643 {
1644 rc = vmsvga3dSurfaceAllocMipLevels(pSurface);
1645 AssertRCReturn(rc, rc);
1646 }
1647
1648 SVGA3dBox clipBox;
1649 if (pBox)
1650 {
1651 clipBox = *pBox;
1652 vmsvgaR3ClipBox(&pMipLevel->mipmapSize, &clipBox);
1653 ASSERT_GUEST_RETURN(clipBox.w && clipBox.h && clipBox.d, VERR_INVALID_PARAMETER);
1654 }
1655 else
1656 {
1657 clipBox.x = 0;
1658 clipBox.y = 0;
1659 clipBox.z = 0;
1660 clipBox.w = pMipLevel->mipmapSize.width;
1661 clipBox.h = pMipLevel->mipmapSize.height;
1662 clipBox.d = pMipLevel->mipmapSize.depth;
1663 }
1664
1665 /// @todo Zero the box?
1666 //if (enmMapType == VMSVGA3D_SURFACE_MAP_WRITE_DISCARD)
1667 // RT_BZERO(.);
1668
1669 vmsvga3dSurfaceMapInit(pMap, enmMapType, &clipBox, pSurface,
1670 pMipLevel->pSurfaceData, pMipLevel->cbSurfacePitch, pMipLevel->cbSurfacePlane);
1671
1672 LogFunc(("SysMem: sid = %u, pvData %p\n", pImage->sid, pMap->pvData));
1673 return VINF_SUCCESS;
1674}
1675
1676int vmsvga3dSurfaceUnmap(PVGASTATECC pThisCC, SVGA3dSurfaceImageId const *pImage, VMSVGA3D_MAPPED_SURFACE *pMap, bool fWritten)
1677{
1678 PVMSVGA3DSURFACE pSurface;
1679 int rc = vmsvga3dSurfaceFromSid(pThisCC->svga.p3dState, pImage->sid, &pSurface);
1680 AssertRCReturn(rc, rc);
1681
1682 if (VMSVGA3DSURFACE_HAS_HW_SURFACE(pSurface))
1683 {
1684 PVMSVGAR3STATE const pSvgaR3State = pThisCC->svga.pSvgaR3State;
1685 AssertReturn(pSvgaR3State->pFuncsMap, VERR_NOT_IMPLEMENTED);
1686 return pSvgaR3State->pFuncsMap->pfnSurfaceUnmap(pThisCC, pImage, pMap, fWritten);
1687 }
1688
1689 PVMSVGA3DMIPMAPLEVEL pMipLevel;
1690 rc = vmsvga3dMipmapLevel(pSurface, pImage->face, pImage->mipmap, &pMipLevel);
1691 ASSERT_GUEST_RETURN(RT_SUCCESS(rc), rc);
1692
1693 if ( fWritten
1694 && ( pMap->enmMapType == VMSVGA3D_SURFACE_MAP_WRITE
1695 || pMap->enmMapType == VMSVGA3D_SURFACE_MAP_READ_WRITE
1696 || pMap->enmMapType == VMSVGA3D_SURFACE_MAP_WRITE_DISCARD))
1697 {
1698 pMipLevel->fDirty = true;
1699 pSurface->fDirty = true;
1700 }
1701
1702 return VINF_SUCCESS;
1703}
1704
1705
1706int vmsvga3dCalcSurfaceMipmapAndFace(PVGASTATECC pThisCC, uint32_t sid, uint32_t iSubresource, uint32_t *piMipmap, uint32_t *piFace)
1707{
1708 PVMSVGA3DSURFACE pSurface;
1709 int rc = vmsvga3dSurfaceFromSid(pThisCC->svga.p3dState, sid, &pSurface);
1710 AssertRCReturn(rc, rc);
1711
1712 vmsvga3dCalcMipmapAndFace(pSurface->cLevels, iSubresource, piMipmap, piFace);
1713 return VINF_SUCCESS;
1714}
1715
1716
1717uint32_t vmsvga3dCalcSubresourceOffset(PVGASTATECC pThisCC, SVGA3dSurfaceImageId const *pImage)
1718{
1719 PVMSVGA3DSURFACE pSurface;
1720 int rc = vmsvga3dSurfaceFromSid(pThisCC->svga.p3dState, pImage->sid, &pSurface);
1721 AssertRCReturn(rc, 0);
1722
1723 ASSERT_GUEST_RETURN(pImage->face < pSurface->surfaceDesc.numArrayElements, 0);
1724
1725 uint32_t offMipLevel = 0;
1726 for (uint32_t i = 0; i < pImage->mipmap; ++i)
1727 {
1728 PVMSVGA3DMIPMAPLEVEL pMipmapLevel = &pSurface->paMipmapLevels[i];
1729 offMipLevel += pMipmapLevel->cbSurface;
1730 }
1731
1732 uint32_t offSubresource = pSurface->surfaceDesc.cbArrayElement * pImage->face + offMipLevel;
1733 /** @todo Multisample? */
1734 return offSubresource;
1735}
1736
1737
1738uint32_t vmsvga3dGetArrayElements(PVGASTATECC pThisCC, SVGA3dSurfaceId sid)
1739{
1740 PVMSVGA3DSURFACE pSurface;
1741 int rc = vmsvga3dSurfaceFromSid(pThisCC->svga.p3dState, sid, &pSurface);
1742 AssertRCReturn(rc, 0);
1743
1744 return pSurface->surfaceDesc.numArrayElements;
1745}
1746
1747
1748uint32_t vmsvga3dGetSubresourceCount(PVGASTATECC pThisCC, SVGA3dSurfaceId sid)
1749{
1750 PVMSVGA3DSURFACE pSurface;
1751 int rc = vmsvga3dSurfaceFromSid(pThisCC->svga.p3dState, sid, &pSurface);
1752 AssertRCReturn(rc, 0);
1753
1754 return pSurface->surfaceDesc.numArrayElements * pSurface->cLevels;
1755}
1756
1757
1758/*
1759 * Calculates memory layout of a surface box for memcpy:
1760 */
1761int vmsvga3dGetBoxDimensions(PVGASTATECC pThisCC, SVGA3dSurfaceImageId const *pImage, SVGA3dBox const *pBox,
1762 VMSGA3D_BOX_DIMENSIONS *pResult)
1763{
1764 PVMSVGA3DSURFACE pSurface;
1765 int rc = vmsvga3dSurfaceFromSid(pThisCC->svga.p3dState, pImage->sid, &pSurface);
1766 AssertRCReturn(rc, rc);
1767
1768 PVMSVGA3DMIPMAPLEVEL pMipLevel;
1769 rc = vmsvga3dMipmapLevel(pSurface, pImage->face, pImage->mipmap, &pMipLevel);
1770 ASSERT_GUEST_RETURN(RT_SUCCESS(rc), rc);
1771
1772 /* Clip the box. */
1773 SVGA3dBox clipBox;
1774 if (pBox)
1775 {
1776 clipBox = *pBox;
1777 vmsvgaR3ClipBox(&pMipLevel->mipmapSize, &clipBox);
1778 ASSERT_GUEST_RETURN(clipBox.w && clipBox.h && clipBox.d, VERR_INVALID_PARAMETER);
1779 }
1780 else
1781 {
1782 clipBox.x = 0;
1783 clipBox.y = 0;
1784 clipBox.z = 0;
1785 clipBox.w = pMipLevel->mipmapSize.width;
1786 clipBox.h = pMipLevel->mipmapSize.height;
1787 clipBox.d = pMipLevel->mipmapSize.depth;
1788 }
1789
1790 uint32_t const cBlocksX = (clipBox.w + pSurface->cxBlock - 1) / pSurface->cxBlock;
1791 uint32_t const cBlocksY = (clipBox.h + pSurface->cyBlock - 1) / pSurface->cyBlock;
1792
1793 pResult->offSubresource = vmsvga3dCalcSubresourceOffset(pThisCC, pImage);
1794 pResult->offBox = (clipBox.x / pSurface->cxBlock) * pSurface->cbBlock
1795 + (clipBox.y / pSurface->cyBlock) * pMipLevel->cbSurfacePitch
1796 + clipBox.z * pMipLevel->cbSurfacePlane;
1797 pResult->cbRow = cBlocksX * pSurface->cbBlock;
1798 pResult->cbPitch = pMipLevel->cbSurfacePitch;
1799 pResult->cyBlocks = cBlocksY;
1800 pResult->cbDepthPitch = pMipLevel->cbSurfacePlane;
1801
1802 return VINF_SUCCESS;
1803}
1804
1805
1806/*
1807 * Whether a legacy 3D backend is used.
1808 * The new DX context can be built together with the legacy D3D9 or OpenGL backend.
1809 * The actual backend is selected at the VM startup.
1810 */
1811bool vmsvga3dIsLegacyBackend(PVGASTATECC pThisCC)
1812{
1813 PVMSVGAR3STATE const pSvgaR3State = pThisCC->svga.pSvgaR3State;
1814 return pSvgaR3State->pFuncsDX == NULL;
1815}
1816
1817
1818void vmsvga3dReset(PVGASTATECC pThisCC)
1819{
1820 /* Deal with data from PVMSVGA3DSTATE */
1821 PVMSVGA3DSTATE p3dState = pThisCC->svga.p3dState;
1822 Assert(pThisCC->svga.p3dState);
1823
1824 if ((pThisCC->svga.p3dState))
1825 {
1826 /* Destroy all leftover surfaces. */
1827 for (uint32_t i = 0; i < p3dState->cSurfaces; i++)
1828 {
1829 if (p3dState->papSurfaces[i]->id != SVGA3D_INVALID_ID)
1830 vmsvga3dSurfaceDestroy(pThisCC, p3dState->papSurfaces[i]->id);
1831 RTMemFree(p3dState->papSurfaces[i]);
1832 p3dState->papSurfaces[i] = NULL;
1833 }
1834 RTMemFree(p3dState->papSurfaces);
1835 p3dState->papSurfaces = NULL;
1836 p3dState->cSurfaces = 0;
1837
1838 /* Destroy all leftover contexts. */
1839 for (uint32_t i = 0; i < p3dState->cContexts; i++)
1840 {
1841 if (p3dState->papContexts[i]->id != SVGA3D_INVALID_ID)
1842 vmsvga3dContextDestroy(pThisCC, p3dState->papContexts[i]->id);
1843 RTMemFree(p3dState->papContexts[i]);
1844 p3dState->papContexts[i] = NULL;
1845 }
1846 RTMemFree(p3dState->papContexts);
1847 p3dState->papContexts = NULL;
1848 p3dState->cContexts = 0;
1849
1850 if (!vmsvga3dIsLegacyBackend(pThisCC))
1851 {
1852 /* Destroy all leftover DX contexts. */
1853 for (uint32_t i = 0; i < p3dState->cDXContexts; i++)
1854 {
1855 if (p3dState->papDXContexts[i]->cid != SVGA3D_INVALID_ID)
1856 vmsvga3dDXDestroyContext(pThisCC, p3dState->papDXContexts[i]->cid);
1857 RTMemFree(p3dState->papDXContexts[i]);
1858 p3dState->papDXContexts[i] = NULL;
1859 }
1860 RTMemFree(p3dState->papDXContexts);
1861 p3dState->papDXContexts = NULL;
1862 p3dState->cDXContexts = 0;
1863 }
1864 }
1865
1866 /* Reset the backend. */
1867 PVMSVGAR3STATE pSvgaR3State = pThisCC->svga.pSvgaR3State;
1868 if (pSvgaR3State->pFuncs3D && pSvgaR3State->pFuncs3D->pfnReset)
1869 pSvgaR3State->pFuncs3D->pfnReset(pThisCC);
1870}
1871
1872
1873void vmsvga3dTerminate(PVGASTATECC pThisCC)
1874{
1875 /* Clean up backend. */
1876 vmsvga3dReset(pThisCC);
1877
1878 /* Deal with data from PVMSVGA3DSTATE */
1879 PVMSVGA3DSTATE p3dState = pThisCC->svga.p3dState;
1880 AssertReturnVoid(p3dState);
1881
1882 /* Terminate the backend. */
1883 PVMSVGAR3STATE pSvgaR3State = pThisCC->svga.pSvgaR3State;
1884 if (pSvgaR3State->pFuncs3D && pSvgaR3State->pFuncs3D->pfnTerminate)
1885 pSvgaR3State->pFuncs3D->pfnTerminate(pThisCC);
1886
1887 RTMemFree(p3dState->pBackend);
1888 p3dState->pBackend = NULL;
1889
1890 RTMemFree(p3dState);
1891 pThisCC->svga.p3dState = NULL;
1892}
1893
1894
1895int vmsvga3dInit(PPDMDEVINS pDevIns, PVGASTATE pThis, PVGASTATECC pThisCC)
1896{
1897 PVMSVGAR3STATE pSvgaR3State = pThisCC->svga.pSvgaR3State;
1898
1899 /* 3D interface is required. */
1900 AssertReturn(pSvgaR3State->pFuncs3D && pSvgaR3State->pFuncs3D->pfnInit, VERR_NOT_SUPPORTED);
1901
1902 PVMSVGA3DSTATE p3dState = (PVMSVGA3DSTATE)RTMemAllocZ(sizeof(VMSVGA3DSTATE));
1903 AssertReturn(p3dState, VERR_NO_MEMORY);
1904 pThisCC->svga.p3dState = p3dState;
1905
1906 int rc = pSvgaR3State->pFuncs3D->pfnInit(pDevIns, pThis, pThisCC);
1907 if (RT_SUCCESS(rc))
1908 return VINF_SUCCESS;
1909
1910 pThisCC->svga.p3dState = NULL;
1911 RTMemFree(p3dState);
1912 return rc;
1913}
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette