VirtualBox

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

Last change on this file since 103914 was 102808, checked in by vboxsync, 9 months ago

Devices/Graphics: multisampling.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 267.2 KB
<
Line 
1/* $Id: DevVGA-SVGA3d-win.cpp 102808 2024-01-10 08:16:30Z vboxsync $ */
2/** @file
3 * DevVMWare - VMWare SVGA device
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 <VBox/version.h>
35#include <VBox/err.h>
36#include <VBox/log.h>
37#include <VBox/vmm/pgm.h>
38#include <VBox/AssertGuest.h>
39
40#include <iprt/assert.h>
41#include <iprt/semaphore.h>
42#include <iprt/uuid.h>
43#include <iprt/mem.h>
44#include <iprt/avl.h>
45
46#include <VBoxVideo.h> /* required by DevVGA.h */
47
48/* should go BEFORE any other DevVGA include to make all DevVGA.h config defines be visible */
49#include "DevVGA.h"
50
51#include "DevVGA-SVGA.h"
52#include "DevVGA-SVGA3d.h"
53#include "DevVGA-SVGA3d-internal.h"
54
55/* Enable to disassemble defined shaders. */
56#if defined(DEBUG) && 0 /* Disabled as we don't have the DirectX SDK avaible atm. */
57#define DUMP_SHADER_DISASSEMBLY
58#endif
59
60#ifdef DUMP_SHADER_DISASSEMBLY
61#include <d3dx9shader.h>
62#endif
63
64
65/*********************************************************************************************************************************
66* Defined Constants And Macros *
67*********************************************************************************************************************************/
68
69#define FOURCC_INTZ (D3DFORMAT)MAKEFOURCC('I', 'N', 'T', 'Z')
70#define FOURCC_NULL (D3DFORMAT)MAKEFOURCC('N', 'U', 'L', 'L')
71
72
73/*********************************************************************************************************************************
74* Structures and Typedefs *
75*********************************************************************************************************************************/
76
77typedef struct
78{
79 DWORD Usage;
80 D3DRESOURCETYPE ResourceType;
81 SVGA3dFormatOp FormatOp;
82} VMSVGA3DFORMATSUPPORT;
83
84
85/*********************************************************************************************************************************
86* Global Variables *
87*********************************************************************************************************************************/
88static VMSVGA3DFORMATSUPPORT const g_aFormatSupport[] =
89{
90 {
91 0,
92 D3DRTYPE_SURFACE,
93 SVGA3DFORMAT_OP_OFFSCREENPLAIN,
94 },
95 {
96 D3DUSAGE_RENDERTARGET,
97 D3DRTYPE_SURFACE,
98 (SVGA3dFormatOp) (SVGA3DFORMAT_OP_OFFSCREEN_RENDERTARGET | SVGA3DFORMAT_OP_SAME_FORMAT_RENDERTARGET),
99 },
100 {
101 D3DUSAGE_AUTOGENMIPMAP,
102 D3DRTYPE_TEXTURE,
103 SVGA3DFORMAT_OP_AUTOGENMIPMAP,
104 },
105 {
106 D3DUSAGE_DMAP,
107 D3DRTYPE_TEXTURE,
108 SVGA3DFORMAT_OP_DMAP,
109 },
110 {
111 0,
112 D3DRTYPE_TEXTURE,
113 SVGA3DFORMAT_OP_TEXTURE,
114 },
115 {
116 0,
117 D3DRTYPE_CUBETEXTURE,
118 SVGA3DFORMAT_OP_CUBETEXTURE,
119 },
120 {
121 0,
122 D3DRTYPE_VOLUMETEXTURE,
123 SVGA3DFORMAT_OP_VOLUMETEXTURE,
124 },
125 {
126 D3DUSAGE_QUERY_VERTEXTEXTURE,
127 D3DRTYPE_TEXTURE,
128 SVGA3DFORMAT_OP_VERTEXTEXTURE,
129 },
130 {
131 D3DUSAGE_QUERY_LEGACYBUMPMAP,
132 D3DRTYPE_TEXTURE,
133 SVGA3DFORMAT_OP_BUMPMAP,
134 },
135 {
136 D3DUSAGE_QUERY_SRGBREAD,
137 D3DRTYPE_TEXTURE,
138 SVGA3DFORMAT_OP_SRGBREAD,
139 },
140 {
141 D3DUSAGE_QUERY_SRGBWRITE,
142 D3DRTYPE_TEXTURE,
143 SVGA3DFORMAT_OP_SRGBWRITE,
144 }
145};
146
147static VMSVGA3DFORMATSUPPORT const g_aFeatureReject[] =
148{
149 {
150 D3DUSAGE_QUERY_WRAPANDMIP,
151 D3DRTYPE_TEXTURE,
152 SVGA3DFORMAT_OP_NOTEXCOORDWRAPNORMIP
153 },
154 {
155 D3DUSAGE_QUERY_FILTER,
156 D3DRTYPE_TEXTURE,
157 SVGA3DFORMAT_OP_NOFILTER
158 },
159 {
160 D3DUSAGE_QUERY_POSTPIXELSHADER_BLENDING,
161 D3DRTYPE_TEXTURE, /* ?? */
162 SVGA3DFORMAT_OP_NOALPHABLEND
163 },
164};
165
166
167/*********************************************************************************************************************************
168* Internal Functions *
169*********************************************************************************************************************************/
170static void vmsvgaDumpD3DCaps(D3DCAPS9 *pCaps, D3DADAPTER_IDENTIFIER9 const *pai9);
171static DECLCALLBACK(int) vmsvga3dBackContextDestroy(PVGASTATECC pThisCC, uint32_t cid);
172static DECLCALLBACK(int) vmsvga3dBackSetRenderState(PVGASTATECC pThisCC, uint32_t cid, uint32_t cRenderStates, SVGA3dRenderState *pRenderState);
173static DECLCALLBACK(int) vmsvga3dBackSetRenderTarget(PVGASTATECC pThisCC, uint32_t cid, SVGA3dRenderTargetType type, SVGA3dSurfaceImageId target);
174static DECLCALLBACK(int) vmsvga3dBackSetTextureState(PVGASTATECC pThisCC, uint32_t cid, uint32_t cTextureStates, SVGA3dTextureState *pTextureState);
175static DECLCALLBACK(int) vmsvga3dBackSetViewPort(PVGASTATECC pThisCC, uint32_t cid, SVGA3dRect *pRect);
176static DECLCALLBACK(int) vmsvga3dBackSetScissorRect(PVGASTATECC pThisCC, uint32_t cid, SVGA3dRect *pRect);
177static DECLCALLBACK(int) vmsvga3dBackShaderDestroy(PVGASTATECC pThisCC, uint32_t cid, uint32_t shid, SVGA3dShaderType type);
178static DECLCALLBACK(int) vmsvga3dBackShaderSet(PVGASTATECC pThisCC, PVMSVGA3DCONTEXT pContext, uint32_t cid, SVGA3dShaderType type, uint32_t shid);
179static DECLCALLBACK(int) vmsvga3dBackOcclusionQueryDelete(PVGASTATECC pThisCC, PVMSVGA3DCONTEXT pContext);
180static DECLCALLBACK(int) vmsvga3dBackCreateTexture(PVGASTATECC pThisCC, PVMSVGA3DCONTEXT pContext, uint32_t idAssociatedContext, PVMSVGA3DSURFACE pSurface);
181
182
183DECLINLINE(D3DCUBEMAP_FACES) vmsvga3dCubemapFaceFromIndex(uint32_t iFace)
184{
185 D3DCUBEMAP_FACES Face;
186 switch (iFace)
187 {
188 case 0: Face = D3DCUBEMAP_FACE_POSITIVE_X; break;
189 case 1: Face = D3DCUBEMAP_FACE_NEGATIVE_X; break;
190 case 2: Face = D3DCUBEMAP_FACE_POSITIVE_Y; break;
191 case 3: Face = D3DCUBEMAP_FACE_NEGATIVE_Y; break;
192 case 4: Face = D3DCUBEMAP_FACE_POSITIVE_Z; break;
193 default:
194 case 5: Face = D3DCUBEMAP_FACE_NEGATIVE_Z; break;
195 }
196 return Face;
197}
198
199static DECLCALLBACK(int) vmsvga3dBackInit(PPDMDEVINS pDevIns, PVGASTATE pThis, PVGASTATECC pThisCC)
200{
201 RT_NOREF(pDevIns, pThis);
202
203 PVMSVGA3DSTATE pState = pThisCC->svga.p3dState;
204
205 /* Create event semaphore. */
206 int rc = RTSemEventCreate(&pState->WndRequestSem);
207 if (RT_FAILURE(rc))
208 {
209 Log(("%s: Failed to create event semaphore for window handling.\n", __FUNCTION__));
210 return rc;
211 }
212
213 /* Create the async IO thread. */
214 rc = RTThreadCreate(&pState->pWindowThread, vmsvga3dWindowThread, pState->WndRequestSem, 0, RTTHREADTYPE_GUI, 0, "VMSVGA3DWND");
215 if (RT_FAILURE(rc))
216 {
217 AssertMsgFailed(("%s: Async IO Thread creation for 3d window handling failed rc=%d\n", __FUNCTION__, rc));
218 return rc;
219 }
220
221 return VINF_SUCCESS;
222}
223
224static DECLCALLBACK(int) vmsvga3dBackPowerOn(PPDMDEVINS pDevIns, PVGASTATE pThis, PVGASTATECC pThisCC)
225{
226 PVMSVGA3DSTATE pState = pThisCC->svga.p3dState;
227 AssertReturn(pThisCC->svga.p3dState, VERR_NO_MEMORY);
228 HRESULT hr;
229 RT_NOREF(pDevIns, pThis);
230
231 if (pState->pD3D9)
232 return VINF_SUCCESS; /* already initialized (load state) */
233
234 /* Direct3DCreate9Ex was introduced in Vista, so resolve it dynamically. */
235 typedef HRESULT (WINAPI *PFNDIRECT3DCREATE9EX)(UINT, IDirect3D9Ex **);
236 PFNDIRECT3DCREATE9EX pfnDirect3dCreate9Ex = (PFNDIRECT3DCREATE9EX)RTLdrGetSystemSymbol("d3d9.dll", "Direct3DCreate9Ex");
237 if (!pfnDirect3dCreate9Ex)
238 return PDMDevHlpVMSetError(pDevIns, VERR_SYMBOL_NOT_FOUND, RT_SRC_POS,
239 "vmsvga3d: Unable to locate Direct3DCreate9Ex. This feature requires Vista and later.");
240 hr = pfnDirect3dCreate9Ex(D3D_SDK_VERSION, &pState->pD3D9);
241 AssertReturn(hr == D3D_OK, VERR_INTERNAL_ERROR);
242
243 D3DADAPTER_IDENTIFIER9 ai9;
244 hr = pState->pD3D9->GetAdapterIdentifier(D3DADAPTER_DEFAULT, 0, &ai9);
245 AssertReturnStmt(hr == D3D_OK, D3D_RELEASE(pState->pD3D9), VERR_INTERNAL_ERROR);
246
247 hr = pState->pD3D9->GetDeviceCaps(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, &pState->caps);
248 AssertReturnStmt(hr == D3D_OK, D3D_RELEASE(pState->pD3D9), VERR_INTERNAL_ERROR);
249
250 vmsvgaDumpD3DCaps(&pState->caps, &ai9);
251
252 if (!D3D9CheckDeviceFormat(pState->pD3D9, 0, D3DRTYPE_TEXTURE, FOURCC_INTZ))
253 {
254 /* INTZ support is essential to support depth surfaces used as textures. */
255 LogRel(("VMSVGA: texture format INTZ not supported!!!\n"));
256 }
257 else
258 pState->fSupportedSurfaceINTZ = true;
259
260 if (!D3D9CheckDeviceFormat(pState->pD3D9, D3DUSAGE_RENDERTARGET, D3DRTYPE_SURFACE, FOURCC_NULL))
261 {
262 /* NULL is a dummy surface which can be used as a render target to save memory. */
263 LogRel(("VMSVGA: surface format NULL not supported!!!\n"));
264 }
265 else
266 pState->fSupportedSurfaceNULL = true;
267
268 /* Check if DX9 depth stencil textures are supported */
269 if (!D3D9CheckDeviceFormat(pState->pD3D9, D3DUSAGE_DEPTHSTENCIL, D3DRTYPE_TEXTURE, D3DFMT_D16))
270 {
271 LogRel(("VMSVGA: texture format D3DFMT_D16 not supported\n"));
272 }
273
274 if (!D3D9CheckDeviceFormat(pState->pD3D9, D3DUSAGE_DEPTHSTENCIL, D3DRTYPE_TEXTURE, D3DFMT_D24X8))
275 {
276 LogRel(("VMSVGA: texture format D3DFMT_D24X8 not supported\n"));
277 }
278
279 if (!D3D9CheckDeviceFormat(pState->pD3D9, D3DUSAGE_DEPTHSTENCIL, D3DRTYPE_TEXTURE, D3DFMT_D24S8))
280 {
281 LogRel(("VMSVGA: texture format D3DFMT_D24S8 not supported\n"));
282 }
283
284 /* Check some formats must be emulated. */
285 if (D3D9CheckDeviceFormat(pState->pD3D9, 0, D3DRTYPE_TEXTURE, D3DFMT_UYVY))
286 {
287 /* Native UYVY support has better performance. */
288 LogRel(("VMSVGA: texture format D3DFMT_UYVY supported\n"));
289 pState->fSupportedFormatUYVY = true;
290 }
291
292 if (D3D9CheckDeviceFormat(pState->pD3D9, 0, D3DRTYPE_TEXTURE, D3DFMT_YUY2))
293 {
294 /* Native YUY2 support has better performance. */
295 LogRel(("VMSVGA: texture format D3DFMT_YUY2 supported\n"));
296 pState->fSupportedFormatYUY2 = true;
297 }
298
299 if (D3D9CheckDeviceFormat(pState->pD3D9, D3DUSAGE_RENDERTARGET, D3DRTYPE_TEXTURE, D3DFMT_A8B8G8R8))
300 {
301 /* Native A8B8G8R8 support is good for OpenGL application in the guest. */
302 LogRel(("VMSVGA: texture format D3DFMT_A8B8G8R8 supported\n"));
303 pState->fSupportedFormatA8B8G8R8 = true;
304 }
305
306 return VINF_SUCCESS;
307}
308
309static DECLCALLBACK(int) vmsvga3dBackReset(PVGASTATECC pThisCC)
310{
311 RT_NOREF(pThisCC);
312 return VINF_SUCCESS;
313}
314
315static DECLCALLBACK(int) vmsvga3dBackTerminate(PVGASTATECC pThisCC)
316{
317 PVMSVGA3DSTATE pState = pThisCC->svga.p3dState;
318 AssertReturn(pThisCC->svga.p3dState, VERR_NO_MEMORY);
319
320 /* Terminate the window creation thread. */
321 int rc = vmsvga3dSendThreadMessage(pState->pWindowThread, pState->WndRequestSem, WM_VMSVGA3D_EXIT, 0, 0);
322 AssertRCReturn(rc, rc);
323
324 RTSemEventDestroy(pState->WndRequestSem);
325
326 D3D_RELEASE(pState->pD3D9);
327
328 return VINF_SUCCESS;
329}
330
331static DECLCALLBACK(void) vmsvga3dBackUpdateHostScreenViewport(PVGASTATECC pThisCC, uint32_t idScreen, VMSVGAVIEWPORT const *pOldViewport)
332{
333 /** @todo Scroll the screen content without requiring the guest to redraw. */
334 RT_NOREF(pThisCC, idScreen, pOldViewport);
335}
336
337static uint32_t vmsvga3dGetSurfaceFormatSupport(PVMSVGA3DSTATE pState3D, uint32_t idx3dCaps, D3DFORMAT format)
338{
339 NOREF(idx3dCaps);
340 HRESULT hr;
341 uint32_t result = 0;
342
343 /* Try if the format can be used for the primary display. */
344 hr = pState3D->pD3D9->CheckDeviceFormat(D3DADAPTER_DEFAULT,
345 D3DDEVTYPE_HAL,
346 format,
347 0,
348 D3DRTYPE_SURFACE,
349 format);
350
351 for (unsigned i = 0; i < RT_ELEMENTS(g_aFormatSupport); i++)
352 {
353 hr = pState3D->pD3D9->CheckDeviceFormat(D3DADAPTER_DEFAULT,
354 D3DDEVTYPE_HAL,
355 D3DFMT_X8R8G8B8, /* assume standard 32-bit display mode */
356 g_aFormatSupport[i].Usage,
357 g_aFormatSupport[i].ResourceType,
358 format);
359 if (hr == D3D_OK)
360 result |= g_aFormatSupport[i].FormatOp;
361 }
362
363 /* Check for features only if the format is supported in any form. */
364 if (result)
365 {
366 for (unsigned i = 0; i < RT_ELEMENTS(g_aFeatureReject); i++)
367 {
368 hr = pState3D->pD3D9->CheckDeviceFormat(D3DADAPTER_DEFAULT,
369 D3DDEVTYPE_HAL,
370 D3DFMT_X8R8G8B8, /* assume standard 32-bit display mode */
371 g_aFeatureReject[i].Usage,
372 g_aFeatureReject[i].ResourceType,
373 format);
374 if (hr != D3D_OK)
375 result |= g_aFeatureReject[i].FormatOp;
376 }
377 }
378
379 /** @todo missing:
380 *
381 * SVGA3DFORMAT_OP_PIXELSIZE
382 */
383
384 switch (idx3dCaps)
385 {
386 case SVGA3D_DEVCAP_SURFACEFMT_X8R8G8B8:
387 case SVGA3D_DEVCAP_SURFACEFMT_X1R5G5B5:
388 case SVGA3D_DEVCAP_SURFACEFMT_R5G6B5:
389 result |= SVGA3DFORMAT_OP_MEMBEROFGROUP_ARGB
390 | SVGA3DFORMAT_OP_CONVERT_TO_ARGB
391 | SVGA3DFORMAT_OP_DISPLAYMODE /* Should not be set for alpha formats. */
392 | SVGA3DFORMAT_OP_3DACCELERATION; /* implies OP_DISPLAYMODE */
393 break;
394
395 case SVGA3D_DEVCAP_SURFACEFMT_A8R8G8B8:
396 case SVGA3D_DEVCAP_SURFACEFMT_A2R10G10B10:
397 case SVGA3D_DEVCAP_SURFACEFMT_A1R5G5B5:
398 case SVGA3D_DEVCAP_SURFACEFMT_A4R4G4B4:
399 result |= SVGA3DFORMAT_OP_MEMBEROFGROUP_ARGB
400 | SVGA3DFORMAT_OP_CONVERT_TO_ARGB
401 | SVGA3DFORMAT_OP_SAME_FORMAT_UP_TO_ALPHA_RENDERTARGET;
402 break;
403 /* These formats can't be used as textures on AMD drivers (Intel works).
404 * Still report them as textures to the guest and emulate them in the device.
405 */
406 case SVGA3D_DEVCAP_SURFACEFMT_UYVY:
407 case SVGA3D_DEVCAP_SURFACEFMT_YUY2:
408 result |= SVGA3DFORMAT_OP_TEXTURE;
409 break;
410 }
411 Log(("CAPS: %s =\n%s\n", vmsvga3dGetCapString(idx3dCaps), vmsvga3dGet3dFormatString(result)));
412
413 return result;
414}
415
416static uint32_t vmsvga3dGetDepthFormatSupport(PVMSVGA3DSTATE pState3D, uint32_t idx3dCaps, D3DFORMAT format)
417{
418 RT_NOREF(idx3dCaps);
419 HRESULT hr;
420 uint32_t result = 0;
421
422 hr = pState3D->pD3D9->CheckDeviceFormat(D3DADAPTER_DEFAULT,
423 D3DDEVTYPE_HAL,
424 D3DFMT_X8R8G8B8, /* assume standard 32-bit display mode */
425 D3DUSAGE_DEPTHSTENCIL,
426 D3DRTYPE_SURFACE,
427 format);
428 if (hr == D3D_OK)
429 result = SVGA3DFORMAT_OP_ZSTENCIL
430 | SVGA3DFORMAT_OP_ZSTENCIL_WITH_ARBITRARY_COLOR_DEPTH
431 | SVGA3DFORMAT_OP_TEXTURE /* Necessary for Ubuntu Unity */;
432
433 Log(("CAPS: %s =\n%s\n", vmsvga3dGetCapString(idx3dCaps), vmsvga3dGet3dFormatString(result)));
434 return result;
435}
436
437
438static DECLCALLBACK(int) vmsvga3dBackQueryCaps(PVGASTATECC pThisCC, SVGA3dDevCapIndex idx3dCaps, uint32_t *pu32Val)
439{
440 PVMSVGA3DSTATE pState = pThisCC->svga.p3dState;
441 AssertReturn(pState, VERR_NO_MEMORY);
442 D3DCAPS9 *pCaps = &pState->caps;
443 int rc = VINF_SUCCESS;
444
445 *pu32Val = 0;
446
447 switch (idx3dCaps)
448 {
449 case SVGA3D_DEVCAP_3D:
450 *pu32Val = 1; /* boolean? */
451 break;
452
453 case SVGA3D_DEVCAP_MAX_LIGHTS:
454 *pu32Val = pCaps->MaxActiveLights;
455 break;
456
457 case SVGA3D_DEVCAP_MAX_TEXTURES:
458 *pu32Val = pCaps->MaxSimultaneousTextures;
459 break;
460
461 case SVGA3D_DEVCAP_MAX_CLIP_PLANES:
462 *pu32Val = pCaps->MaxUserClipPlanes;
463 break;
464
465 case SVGA3D_DEVCAP_VERTEX_SHADER_VERSION:
466 switch (pCaps->VertexShaderVersion)
467 {
468 case D3DVS_VERSION(1,1):
469 *pu32Val = SVGA3DVSVERSION_11;
470 break;
471 case D3DVS_VERSION(2,0):
472 *pu32Val = SVGA3DVSVERSION_20;
473 break;
474 case D3DVS_VERSION(3,0):
475 *pu32Val = SVGA3DVSVERSION_30;
476 break;
477 case D3DVS_VERSION(4,0):
478 *pu32Val = SVGA3DVSVERSION_40;
479 break;
480 default:
481 LogRel(("VMSVGA: Unsupported vertex shader version %x\n", pCaps->VertexShaderVersion));
482 break;
483 }
484 break;
485
486 case SVGA3D_DEVCAP_VERTEX_SHADER:
487 /* boolean? */
488 *pu32Val = 1;
489 break;
490
491 case SVGA3D_DEVCAP_FRAGMENT_SHADER_VERSION:
492 switch (pCaps->PixelShaderVersion)
493 {
494 case D3DPS_VERSION(1,1):
495 *pu32Val = SVGA3DPSVERSION_11;
496 break;
497 case D3DPS_VERSION(1,2):
498 *pu32Val = SVGA3DPSVERSION_12;
499 break;
500 case D3DPS_VERSION(1,3):
501 *pu32Val = SVGA3DPSVERSION_13;
502 break;
503 case D3DPS_VERSION(1,4):
504 *pu32Val = SVGA3DPSVERSION_14;
505 break;
506 case D3DPS_VERSION(2,0):
507 *pu32Val = SVGA3DPSVERSION_20;
508 break;
509 case D3DPS_VERSION(3,0):
510 *pu32Val = SVGA3DPSVERSION_30;
511 break;
512 case D3DPS_VERSION(4,0):
513 *pu32Val = SVGA3DPSVERSION_40;
514 break;
515 default:
516 LogRel(("VMSVGA: Unsupported pixel shader version %x\n", pCaps->PixelShaderVersion));
517 break;
518 }
519 break;
520
521 case SVGA3D_DEVCAP_FRAGMENT_SHADER:
522 /* boolean? */
523 *pu32Val = 1;
524 break;
525
526 case SVGA3D_DEVCAP_S23E8_TEXTURES:
527 case SVGA3D_DEVCAP_S10E5_TEXTURES:
528 /* Must be obsolete by now; surface format caps specify the same thing. */
529 rc = VERR_INVALID_PARAMETER;
530 break;
531
532 case SVGA3D_DEVCAP_MAX_FIXED_VERTEXBLEND:
533 break;
534
535 /*
536 * 2. The BUFFER_FORMAT capabilities are deprecated, and they always
537 * return TRUE. Even on physical hardware that does not support
538 * these formats natively, the SVGA3D device will provide an emulation
539 * which should be invisible to the guest OS.
540 */
541 case SVGA3D_DEVCAP_D16_BUFFER_FORMAT:
542 case SVGA3D_DEVCAP_D24S8_BUFFER_FORMAT:
543 case SVGA3D_DEVCAP_D24X8_BUFFER_FORMAT:
544 *pu32Val = 1;
545 break;
546
547 case SVGA3D_DEVCAP_QUERY_TYPES:
548 break;
549
550 case SVGA3D_DEVCAP_TEXTURE_GRADIENT_SAMPLING:
551 break;
552
553 case SVGA3D_DEVCAP_MAX_POINT_SIZE:
554 AssertCompile(sizeof(uint32_t) == sizeof(float));
555 *(float *)pu32Val = pCaps->MaxPointSize;
556 break;
557
558 case SVGA3D_DEVCAP_MAX_SHADER_TEXTURES:
559 /** @todo ?? */
560 rc = VERR_INVALID_PARAMETER;
561 break;
562
563 case SVGA3D_DEVCAP_MAX_TEXTURE_WIDTH:
564 *pu32Val = pCaps->MaxTextureWidth;
565 break;
566
567 case SVGA3D_DEVCAP_MAX_TEXTURE_HEIGHT:
568 *pu32Val = pCaps->MaxTextureHeight;
569 break;
570
571 case SVGA3D_DEVCAP_MAX_VOLUME_EXTENT:
572 *pu32Val = pCaps->MaxVolumeExtent;
573 break;
574
575 case SVGA3D_DEVCAP_MAX_TEXTURE_REPEAT:
576 *pu32Val = pCaps->MaxTextureRepeat;
577 break;
578
579 case SVGA3D_DEVCAP_MAX_TEXTURE_ASPECT_RATIO:
580 *pu32Val = pCaps->MaxTextureAspectRatio;
581 break;
582
583 case SVGA3D_DEVCAP_MAX_TEXTURE_ANISOTROPY:
584 *pu32Val = pCaps->MaxAnisotropy;
585 break;
586
587 case SVGA3D_DEVCAP_MAX_PRIMITIVE_COUNT:
588 *pu32Val = pCaps->MaxPrimitiveCount;
589 break;
590
591 case SVGA3D_DEVCAP_MAX_VERTEX_INDEX:
592 *pu32Val = pCaps->MaxVertexIndex;
593 break;
594
595 case SVGA3D_DEVCAP_MAX_VERTEX_SHADER_INSTRUCTIONS:
596 *pu32Val = pCaps->MaxVertexShader30InstructionSlots;
597 break;
598
599 case SVGA3D_DEVCAP_MAX_FRAGMENT_SHADER_INSTRUCTIONS:
600 *pu32Val = pCaps->MaxPixelShader30InstructionSlots;
601 break;
602
603 case SVGA3D_DEVCAP_MAX_VERTEX_SHADER_TEMPS:
604 *pu32Val = pCaps->VS20Caps.NumTemps;
605 break;
606
607 case SVGA3D_DEVCAP_MAX_FRAGMENT_SHADER_TEMPS:
608 *pu32Val = pCaps->PS20Caps.NumTemps;
609 break;
610
611 case SVGA3D_DEVCAP_TEXTURE_OPS:
612 break;
613
614 case SVGA3D_DEVCAP_DEAD4: /* SVGA3D_DEVCAP_MULTISAMPLE_NONMASKABLESAMPLES */
615 break;
616
617 case SVGA3D_DEVCAP_DEAD5: /* SVGA3D_DEVCAP_MULTISAMPLE_MASKABLESAMPLES */
618 break;
619
620 case SVGA3D_DEVCAP_DEAD7: /* SVGA3D_DEVCAP_ALPHATOCOVERAGE */
621 break;
622
623 case SVGA3D_DEVCAP_DEAD6: /* SVGA3D_DEVCAP_SUPERSAMPLE */
624 break;
625
626 case SVGA3D_DEVCAP_AUTOGENMIPMAPS:
627 *pu32Val = !!(pCaps->Caps2 & D3DCAPS2_CANAUTOGENMIPMAP);
628 break;
629
630 case SVGA3D_DEVCAP_MAX_VERTEX_SHADER_TEXTURES:
631 break;
632
633 case SVGA3D_DEVCAP_MAX_RENDER_TARGETS: /** @todo same thing? */
634 case SVGA3D_DEVCAP_MAX_SIMULTANEOUS_RENDER_TARGETS:
635 *pu32Val = pCaps->NumSimultaneousRTs;
636 break;
637
638 /*
639 * This is the maximum number of SVGA context IDs that the guest
640 * can define using SVGA_3D_CMD_CONTEXT_DEFINE.
641 */
642 case SVGA3D_DEVCAP_MAX_CONTEXT_IDS:
643 *pu32Val = SVGA3D_MAX_CONTEXT_IDS;
644 break;
645
646 /*
647 * This is the maximum number of SVGA surface IDs that the guest
648 * can define using SVGA_3D_CMD_SURFACE_DEFINE*.
649 */
650 case SVGA3D_DEVCAP_MAX_SURFACE_IDS:
651 *pu32Val = SVGA3D_MAX_SURFACE_IDS;
652 break;
653
654 /* Supported surface formats. */
655 case SVGA3D_DEVCAP_SURFACEFMT_X8R8G8B8:
656 *pu32Val = vmsvga3dGetSurfaceFormatSupport(pState, idx3dCaps, D3DFMT_X8R8G8B8);
657 break;
658
659 case SVGA3D_DEVCAP_SURFACEFMT_A8R8G8B8:
660 *pu32Val = vmsvga3dGetSurfaceFormatSupport(pState, idx3dCaps, D3DFMT_A8R8G8B8);
661 break;
662
663 case SVGA3D_DEVCAP_SURFACEFMT_A2R10G10B10:
664 *pu32Val = vmsvga3dGetSurfaceFormatSupport(pState, idx3dCaps, D3DFMT_A2R10G10B10);
665 break;
666
667 case SVGA3D_DEVCAP_SURFACEFMT_X1R5G5B5:
668 *pu32Val = vmsvga3dGetSurfaceFormatSupport(pState, idx3dCaps, D3DFMT_X1R5G5B5);
669 break;
670
671 case SVGA3D_DEVCAP_SURFACEFMT_A1R5G5B5:
672 *pu32Val = vmsvga3dGetSurfaceFormatSupport(pState, idx3dCaps, D3DFMT_A1R5G5B5);
673 break;
674
675 case SVGA3D_DEVCAP_SURFACEFMT_A4R4G4B4:
676 *pu32Val = vmsvga3dGetSurfaceFormatSupport(pState, idx3dCaps, D3DFMT_A4R4G4B4);
677 break;
678
679 case SVGA3D_DEVCAP_SURFACEFMT_R5G6B5:
680 *pu32Val = vmsvga3dGetSurfaceFormatSupport(pState, idx3dCaps, D3DFMT_R5G6B5);
681 break;
682
683 case SVGA3D_DEVCAP_SURFACEFMT_LUMINANCE16:
684 *pu32Val = vmsvga3dGetSurfaceFormatSupport(pState, idx3dCaps, D3DFMT_L16);
685 break;
686
687 case SVGA3D_DEVCAP_SURFACEFMT_LUMINANCE8_ALPHA8:
688 *pu32Val = vmsvga3dGetSurfaceFormatSupport(pState, idx3dCaps, D3DFMT_A8L8);
689 break;
690
691 case SVGA3D_DEVCAP_SURFACEFMT_ALPHA8:
692 *pu32Val = vmsvga3dGetSurfaceFormatSupport(pState, idx3dCaps, D3DFMT_A8);
693 break;
694
695 case SVGA3D_DEVCAP_SURFACEFMT_LUMINANCE8:
696 *pu32Val = vmsvga3dGetSurfaceFormatSupport(pState, idx3dCaps, D3DFMT_L8);
697 break;
698
699 case SVGA3D_DEVCAP_SURFACEFMT_Z_D16:
700 *pu32Val = vmsvga3dGetDepthFormatSupport(pState, idx3dCaps, D3DFMT_D16);
701 break;
702
703 case SVGA3D_DEVCAP_SURFACEFMT_Z_D24S8:
704 case SVGA3D_DEVCAP_SURFACEFMT_Z_D24S8_INT: /** @todo not correct */
705 *pu32Val = vmsvga3dGetDepthFormatSupport(pState, idx3dCaps, D3DFMT_D24S8);
706 break;
707
708 case SVGA3D_DEVCAP_SURFACEFMT_Z_D24X8:
709 *pu32Val = vmsvga3dGetDepthFormatSupport(pState, idx3dCaps, D3DFMT_D24X8);
710 break;
711
712 case SVGA3D_DEVCAP_SURFACEFMT_Z_DF16:
713 /** @todo supposed to be floating-point, but unable to find a match for D3D9... */
714 *pu32Val = 0;
715 break;
716
717 case SVGA3D_DEVCAP_SURFACEFMT_Z_DF24:
718 *pu32Val = vmsvga3dGetDepthFormatSupport(pState, idx3dCaps, D3DFMT_D24FS8);
719 break;
720
721 case SVGA3D_DEVCAP_SURFACEFMT_DXT1:
722 *pu32Val = vmsvga3dGetSurfaceFormatSupport(pState, idx3dCaps, D3DFMT_DXT1);
723 break;
724
725 case SVGA3D_DEVCAP_SURFACEFMT_DXT2:
726 *pu32Val = vmsvga3dGetSurfaceFormatSupport(pState, idx3dCaps, D3DFMT_DXT2);
727 break;
728
729 case SVGA3D_DEVCAP_SURFACEFMT_DXT3:
730 *pu32Val = vmsvga3dGetSurfaceFormatSupport(pState, idx3dCaps, D3DFMT_DXT3);
731 break;
732
733 case SVGA3D_DEVCAP_SURFACEFMT_DXT4:
734 *pu32Val = vmsvga3dGetSurfaceFormatSupport(pState, idx3dCaps, D3DFMT_DXT4);
735 break;
736
737 case SVGA3D_DEVCAP_SURFACEFMT_DXT5:
738 *pu32Val = vmsvga3dGetSurfaceFormatSupport(pState, idx3dCaps, D3DFMT_DXT5);
739 break;
740
741 case SVGA3D_DEVCAP_SURFACEFMT_BUMPX8L8V8U8:
742 *pu32Val = vmsvga3dGetSurfaceFormatSupport(pState, idx3dCaps, D3DFMT_X8L8V8U8);
743 break;
744
745 case SVGA3D_DEVCAP_SURFACEFMT_A2W10V10U10:
746 *pu32Val = vmsvga3dGetSurfaceFormatSupport(pState, idx3dCaps, D3DFMT_A2W10V10U10);
747 break;
748
749 case SVGA3D_DEVCAP_SURFACEFMT_BUMPU8V8:
750 *pu32Val = vmsvga3dGetSurfaceFormatSupport(pState, idx3dCaps, D3DFMT_V8U8);
751 break;
752
753 case SVGA3D_DEVCAP_SURFACEFMT_Q8W8V8U8:
754 *pu32Val = vmsvga3dGetSurfaceFormatSupport(pState, idx3dCaps, D3DFMT_Q8W8V8U8);
755 break;
756
757 case SVGA3D_DEVCAP_SURFACEFMT_CxV8U8:
758 *pu32Val = vmsvga3dGetSurfaceFormatSupport(pState, idx3dCaps, D3DFMT_CxV8U8);
759 break;
760
761 case SVGA3D_DEVCAP_SURFACEFMT_R_S10E5:
762 *pu32Val = vmsvga3dGetSurfaceFormatSupport(pState, idx3dCaps, D3DFMT_R16F);
763 break;
764
765 case SVGA3D_DEVCAP_SURFACEFMT_R_S23E8:
766 *pu32Val = vmsvga3dGetSurfaceFormatSupport(pState, idx3dCaps, D3DFMT_R32F);
767 break;
768
769 case SVGA3D_DEVCAP_SURFACEFMT_RG_S10E5:
770 *pu32Val = vmsvga3dGetSurfaceFormatSupport(pState, idx3dCaps, D3DFMT_G16R16F);
771 break;
772
773 case SVGA3D_DEVCAP_SURFACEFMT_RG_S23E8:
774 *pu32Val = vmsvga3dGetSurfaceFormatSupport(pState, idx3dCaps, D3DFMT_G32R32F);
775 break;
776
777 case SVGA3D_DEVCAP_SURFACEFMT_ARGB_S10E5:
778 *pu32Val = vmsvga3dGetSurfaceFormatSupport(pState, idx3dCaps, D3DFMT_A16B16G16R16F);
779 break;
780
781 case SVGA3D_DEVCAP_SURFACEFMT_ARGB_S23E8:
782 *pu32Val = vmsvga3dGetSurfaceFormatSupport(pState, idx3dCaps, D3DFMT_A32B32G32R32F);
783 break;
784
785 case SVGA3D_DEVCAP_SURFACEFMT_V16U16:
786 *pu32Val = vmsvga3dGetSurfaceFormatSupport(pState, idx3dCaps, D3DFMT_V16U16);
787 break;
788
789 case SVGA3D_DEVCAP_SURFACEFMT_G16R16:
790 *pu32Val = vmsvga3dGetSurfaceFormatSupport(pState, idx3dCaps, D3DFMT_G16R16);
791 break;
792
793 case SVGA3D_DEVCAP_SURFACEFMT_A16B16G16R16:
794 *pu32Val = vmsvga3dGetSurfaceFormatSupport(pState, idx3dCaps, D3DFMT_A16B16G16R16);
795 break;
796
797 case SVGA3D_DEVCAP_SURFACEFMT_UYVY:
798 *pu32Val = vmsvga3dGetSurfaceFormatSupport(pState, idx3dCaps, D3DFMT_UYVY);
799 break;
800
801 case SVGA3D_DEVCAP_SURFACEFMT_YUY2:
802 *pu32Val = vmsvga3dGetSurfaceFormatSupport(pState, idx3dCaps, D3DFMT_YUY2);
803 break;
804
805 case SVGA3D_DEVCAP_SURFACEFMT_NV12:
806 *pu32Val = vmsvga3dGetSurfaceFormatSupport(pState, idx3dCaps, (D3DFORMAT)MAKEFOURCC('N', 'V', '1', '2'));
807 break;
808
809 case SVGA3D_DEVCAP_DEAD10: /* SVGA3D_DEVCAP_SURFACEFMT_AYUV */
810 *pu32Val = vmsvga3dGetSurfaceFormatSupport(pState, idx3dCaps, (D3DFORMAT)MAKEFOURCC('A', 'Y', 'U', 'V'));
811 break;
812
813 case SVGA3D_DEVCAP_SURFACEFMT_ATI1:
814 case SVGA3D_DEVCAP_SURFACEFMT_ATI2:
815 /* Unknown; only in DX10 & 11 */
816 Log(("CAPS: Unknown CAP %s\n", vmsvga3dGetCapString(idx3dCaps)));
817 rc = VERR_INVALID_PARAMETER;
818 *pu32Val = 0;
819 break;
820
821 default:
822 Log(("CAPS: Unexpected CAP %d\n", idx3dCaps));
823 rc = VERR_INVALID_PARAMETER;
824 break;
825 }
826#if 0
827 /* Dump of VMWare Player caps (from their log); for debugging purposes */
828 switch (idx3dCaps)
829 {
830 case 0:
831 *pu32Val = 0x00000001;
832 break;
833 case 1:
834 *pu32Val = 0x0000000a;
835 break;
836 case 2:
837 *pu32Val = 0x00000008;
838 break;
839 case 3: *pu32Val = 0x00000006; break;
840 case 4: *pu32Val = 0x00000007; break;
841 case 5: *pu32Val = 0x00000001; break;
842 case 6: *pu32Val = 0x0000000d; break;
843 case 7: *pu32Val = 0x00000001; break;
844 case 8: *pu32Val = 0x00000004; break;
845 case 9: *pu32Val = 0x00000001; break;
846 case 10: *pu32Val = 0x00000001; break;
847 case 11: *pu32Val = 0x00000004; break;
848 case 12: *pu32Val = 0x00000001; break;
849 case 13: *pu32Val = 0x00000001; break;
850 case 14: *pu32Val = 0x00000001; break;
851 case 15: *pu32Val = 0x00000001; break;
852 case 16: *pu32Val = 0x00000001; break;
853 case 17: *pu32Val = (uint32_t)256.000000; break;
854 case 18: *pu32Val = 0x00000014; break;
855 case 19: *pu32Val = 0x00001000; break;
856 case 20: *pu32Val = 0x00001000; break;
857 case 21: *pu32Val = 0x00000800; break;
858 case 22: *pu32Val = 0x00002000; break;
859 case 23: *pu32Val = 0x00000800; break;
860 case 24: *pu32Val = 0x00000010; break;
861 case 25: *pu32Val = 0x000fffff; break;
862 case 26: *pu32Val = 0x00ffffff; break;
863 case 27: *pu32Val = 0xffffffff; break;
864 case 28: *pu32Val = 0xffffffff; break;
865 case 29: *pu32Val = 0x00000020; break;
866 case 30: *pu32Val = 0x00000020; break;
867 case 31: *pu32Val = 0x03ffffff; break;
868 case 32: *pu32Val = 0x0098ec1f; break;
869 case 33: *pu32Val = 0x0098e11f; break;
870 case 34: *pu32Val = 0x0098e01f; break;
871 case 35: *pu32Val = 0x012c2000; break;
872 case 36: *pu32Val = 0x0098e11f; break;
873 case 37: *pu32Val = 0x0090c11f; break;
874 case 38: *pu32Val = 0x0098ec1f; break;
875 case 39: *pu32Val = 0x00804007; break;
876 case 40: *pu32Val = 0x0080c007; break;
877 case 41: *pu32Val = 0x00804007; break;
878 case 42: *pu32Val = 0x0080c007; break;
879 case 43: *pu32Val = 0x000000c1; break;
880 case 44: *pu32Val = 0x000000c1; break;
881 case 45: *pu32Val = 0x000000c1; break;
882 case 46: *pu32Val = 0x00808005; break;
883 case 47: *pu32Val = 0x00808005; break;
884 case 48: *pu32Val = 0x00808005; break;
885 case 49: *pu32Val = 0x00808005; break;
886 case 50: *pu32Val = 0x00808005; break;
887 case 51: *pu32Val = 0x01240000; break;
888 case 52: *pu32Val = 0x00814007; break;
889 case 53: *pu32Val = 0x00814007; break;
890 case 54: *pu32Val = 0x00814007; break;
891 case 55: *pu32Val = 0x01240000; break;
892 case 56: *pu32Val = 0x0080401f; break;
893 case 57: *pu32Val = 0x0080401f; break;
894 case 58: *pu32Val = 0x0080401f; break;
895 case 59: *pu32Val = 0x0080401f; break;
896 case 60: *pu32Val = 0x0080601f; break;
897 case 61: *pu32Val = 0x0080401f; break;
898 case 62: *pu32Val = 0x00000000; break;
899 case 63: *pu32Val = 0x00000004; break;
900 case 64: *pu32Val = 0x00000004; break;
901 case 65: *pu32Val = 0x00814005; break;
902 case 66: *pu32Val = 0x0080401f; break;
903 case 67: *pu32Val = 0x0080601f; break;
904 case 68: *pu32Val = 0x00006009; break;
905 case 69: *pu32Val = 0x00006001; break;
906 case 70: *pu32Val = 0x00000001; break;
907 case 71: *pu32Val = 0x0000000b; break;
908 case 72: *pu32Val = 0x00000001; break;
909 case 73: *pu32Val = 0x00000000; break;
910 case 74: *pu32Val = 0x00000000; break;
911 case 75: *pu32Val = 0x01246000; break;
912 case 76: *pu32Val = 0x00004009; break;
913 case 77: *pu32Val = 0x00000100; break;
914 case 78: *pu32Val = 0x00008000; break;
915 case 79: *pu32Val = 0x000000c1; break;
916 case 80: *pu32Val = 0x01240000; break;
917 case 81: *pu32Val = 0x000000c1; break;
918 case 82: *pu32Val = 0x00800005; break;
919 case 83: *pu32Val = 0x00800005; break;
920 case 84: *pu32Val = 0x00000000; break;
921 case 85: *pu32Val = 0x00000000; break;
922 case 86: *pu32Val = 0x00000000; break;
923 case 87: *pu32Val = 0x00000000; break;
924 case 88: *pu32Val = 0x00000000; break;
925 case 89: *pu32Val = (uint32_t) 0.000000; break;
926 case 90: *pu32Val = (uint32_t) 0.000000; break;
927 case 91: *pu32Val = 0x00006009; break;
928 default:
929// Log(("CAPS: Unexpected CAP %d\n", idx3dCaps));
930// rc = VERR_INVALID_PARAMETER;
931 break;
932 }
933#endif
934 Log(("CAPS: %d=%s - %x\n", idx3dCaps, vmsvga3dGetCapString(idx3dCaps), *pu32Val));
935 return rc;
936}
937
938/**
939 * Convert SVGA format value to its D3D equivalent
940 */
941D3DFORMAT vmsvga3dSurfaceFormat2D3D(SVGA3dSurfaceFormat format)
942{
943 switch (format)
944 {
945 case SVGA3D_X8R8G8B8:
946 return D3DFMT_X8R8G8B8;
947 case SVGA3D_A8R8G8B8:
948 return D3DFMT_A8R8G8B8;
949 case SVGA3D_R5G6B5:
950 return D3DFMT_R5G6B5;
951 case SVGA3D_X1R5G5B5:
952 return D3DFMT_X1R5G5B5;
953 case SVGA3D_A1R5G5B5:
954 return D3DFMT_A1R5G5B5;
955 case SVGA3D_A4R4G4B4:
956 return D3DFMT_A4R4G4B4;
957
958 case SVGA3D_R8G8B8A8_UNORM:
959 return D3DFMT_A8B8G8R8;
960
961 case SVGA3D_Z_D32:
962 return D3DFMT_D32;
963 case SVGA3D_Z_D16:
964 return D3DFMT_D16;
965 case SVGA3D_Z_D24S8_INT: /** @todo not correct */
966 case SVGA3D_Z_D24S8:
967 return D3DFMT_D24S8;
968 case SVGA3D_Z_D15S1:
969 return D3DFMT_D15S1;
970 case SVGA3D_Z_D24X8:
971 return D3DFMT_D24X8;
972 /* Advanced D3D9 depth formats. */
973 case SVGA3D_Z_DF16:
974 /** @todo supposed to be floating-point, but unable to find a match for D3D9... */
975 AssertFailedReturn(D3DFMT_UNKNOWN);
976 case SVGA3D_Z_DF24:
977 return D3DFMT_D24FS8;
978
979 case SVGA3D_LUMINANCE8:
980 return D3DFMT_L8;
981 case SVGA3D_LUMINANCE4_ALPHA4:
982 return D3DFMT_A4L4;
983 case SVGA3D_LUMINANCE16:
984 return D3DFMT_L16;
985 case SVGA3D_LUMINANCE8_ALPHA8:
986 return D3DFMT_A8L8;
987
988 case SVGA3D_DXT1:
989 return D3DFMT_DXT1;
990 case SVGA3D_DXT2:
991 return D3DFMT_DXT2;
992 case SVGA3D_DXT3:
993 return D3DFMT_DXT3;
994 case SVGA3D_DXT4:
995 return D3DFMT_DXT4;
996 case SVGA3D_DXT5:
997 return D3DFMT_DXT5;
998
999 /* Bump-map formats */
1000 case SVGA3D_BUMPU8V8:
1001 return D3DFMT_V8U8;
1002 case SVGA3D_BUMPL6V5U5:
1003 return D3DFMT_L6V5U5;
1004 case SVGA3D_BUMPX8L8V8U8:
1005 return D3DFMT_X8L8V8U8;
1006 case SVGA3D_FORMAT_DEAD1:
1007 /* No corresponding D3D9 equivalent. */
1008 AssertFailedReturn(D3DFMT_UNKNOWN);
1009 /* signed bump-map formats */
1010 case SVGA3D_V8U8:
1011 return D3DFMT_V8U8;
1012 case SVGA3D_Q8W8V8U8:
1013 return D3DFMT_Q8W8V8U8;
1014 case SVGA3D_CxV8U8:
1015 return D3DFMT_CxV8U8;
1016 /* mixed bump-map formats */
1017 case SVGA3D_X8L8V8U8:
1018 return D3DFMT_X8L8V8U8;
1019 case SVGA3D_A2W10V10U10:
1020 return D3DFMT_A2W10V10U10;
1021
1022 case SVGA3D_ARGB_S10E5: /* 16-bit floating-point ARGB */
1023 return D3DFMT_A16B16G16R16F;
1024 case SVGA3D_ARGB_S23E8: /* 32-bit floating-point ARGB */
1025 return D3DFMT_A32B32G32R32F;
1026
1027 case SVGA3D_A2R10G10B10:
1028 return D3DFMT_A2R10G10B10;
1029
1030 case SVGA3D_ALPHA8:
1031 return D3DFMT_A8;
1032
1033 /* Single- and dual-component floating point formats */
1034 case SVGA3D_R_S10E5:
1035 return D3DFMT_R16F;
1036 case SVGA3D_R_S23E8:
1037 return D3DFMT_R32F;
1038 case SVGA3D_RG_S10E5:
1039 return D3DFMT_G16R16F;
1040 case SVGA3D_RG_S23E8:
1041 return D3DFMT_G32R32F;
1042
1043 /*
1044 * Any surface can be used as a buffer object, but SVGA3D_BUFFER is
1045 * the most efficient format to use when creating new surfaces
1046 * expressly for index or vertex data.
1047 */
1048 case SVGA3D_BUFFER:
1049 return D3DFMT_UNKNOWN;
1050
1051 case SVGA3D_V16U16:
1052 return D3DFMT_V16U16;
1053
1054 case SVGA3D_G16R16:
1055 return D3DFMT_G16R16;
1056 case SVGA3D_A16B16G16R16:
1057 return D3DFMT_A16B16G16R16;
1058
1059 /* Packed Video formats */
1060 case SVGA3D_UYVY:
1061 return D3DFMT_UYVY;
1062 case SVGA3D_YUY2:
1063 return D3DFMT_YUY2;
1064
1065 /* Planar video formats */
1066 case SVGA3D_NV12:
1067 return (D3DFORMAT)MAKEFOURCC('N', 'V', '1', '2');
1068
1069 /* Video format with alpha */
1070 case SVGA3D_FORMAT_DEAD2: /* Old SVGA3D_AYUV */
1071 return (D3DFORMAT)MAKEFOURCC('A', 'Y', 'U', 'V');
1072
1073 case SVGA3D_R8G8B8A8_SNORM:
1074 return D3DFMT_Q8W8V8U8;
1075 case SVGA3D_R16G16_UNORM:
1076 return D3DFMT_G16R16;
1077
1078 case SVGA3D_ATI1:
1079 case SVGA3D_ATI2:
1080 /* Unknown; only in DX10 & 11 */
1081 break;
1082
1083 case SVGA3D_FORMAT_MAX: /* shut up MSC */
1084 case SVGA3D_FORMAT_INVALID:
1085 break;
1086 default: /** @todo Other formats. Avoid MSC warning for now. */
1087 break;
1088 }
1089 AssertFailedReturn(D3DFMT_UNKNOWN);
1090}
1091
1092/**
1093 * Convert SVGA multi sample count value to its D3D equivalent
1094 */
1095D3DMULTISAMPLE_TYPE vmsvga3dMultipeSampleCount2D3D(uint32_t multisampleCount)
1096{
1097 AssertCompile(D3DMULTISAMPLE_2_SAMPLES == 2);
1098 AssertCompile(D3DMULTISAMPLE_16_SAMPLES == 16);
1099
1100 if (multisampleCount > 16)
1101 return D3DMULTISAMPLE_NONE;
1102
1103 /** @todo exact same mapping as d3d? */
1104 return (D3DMULTISAMPLE_TYPE)multisampleCount;
1105}
1106
1107
1108/**
1109 * Destroy backend specific surface bits (part of SVGA_3D_CMD_SURFACE_DESTROY).
1110 *
1111 * @param pThisCC The device state.
1112 * @param fClearCOTableEntry Not relevant for this backend.
1113 * @param pSurface The surface being destroyed.
1114 */
1115static DECLCALLBACK(void) vmsvga3dBackSurfaceDestroy(PVGASTATECC pThisCC, bool fClearCOTableEntry, PVMSVGA3DSURFACE pSurface)
1116{
1117 RT_NOREF(pThisCC, fClearCOTableEntry);
1118
1119 RTAvlU32Destroy(&pSurface->pSharedObjectTree, vmsvga3dSharedSurfaceDestroyTree, pSurface);
1120 Assert(pSurface->pSharedObjectTree == NULL);
1121
1122 switch (pSurface->enmD3DResType)
1123 {
1124 case VMSVGA3D_D3DRESTYPE_SURFACE:
1125 D3D_RELEASE(pSurface->u.pSurface);
1126 break;
1127
1128 case VMSVGA3D_D3DRESTYPE_TEXTURE:
1129 D3D_RELEASE(pSurface->u.pTexture);
1130 D3D_RELEASE(pSurface->bounce.pTexture);
1131 D3D_RELEASE(pSurface->emulated.pTexture);
1132 break;
1133
1134 case VMSVGA3D_D3DRESTYPE_CUBE_TEXTURE:
1135 D3D_RELEASE(pSurface->u.pCubeTexture);
1136 D3D_RELEASE(pSurface->bounce.pCubeTexture);
1137 D3D_RELEASE(pSurface->emulated.pCubeTexture);
1138 break;
1139
1140 case VMSVGA3D_D3DRESTYPE_VOLUME_TEXTURE:
1141 D3D_RELEASE(pSurface->u.pVolumeTexture);
1142 D3D_RELEASE(pSurface->bounce.pVolumeTexture);
1143 D3D_RELEASE(pSurface->emulated.pVolumeTexture);
1144 break;
1145
1146 case VMSVGA3D_D3DRESTYPE_VERTEX_BUFFER:
1147 D3D_RELEASE(pSurface->u.pVertexBuffer);
1148 break;
1149
1150 case VMSVGA3D_D3DRESTYPE_INDEX_BUFFER:
1151 D3D_RELEASE(pSurface->u.pIndexBuffer);
1152 break;
1153
1154 default:
1155 AssertMsg(!VMSVGA3DSURFACE_HAS_HW_SURFACE(pSurface),
1156 ("surfaceFlags=0x%x\n", (pSurface->f.s.surface1Flags & VMSVGA3D_SURFACE_HINT_SWITCH_MASK)));
1157 break;
1158 }
1159
1160 D3D_RELEASE(pSurface->pQuery);
1161}
1162
1163
1164static DECLCALLBACK(void) vmsvga3dBackSurfaceInvalidateImage(PVGASTATECC pThisCC, PVMSVGA3DSURFACE pSurface, uint32_t uFace, uint32_t uMipmap)
1165{
1166 RT_NOREF(pThisCC, pSurface, uFace, uMipmap);
1167}
1168
1169
1170/*
1171 * Release all shared surface objects.
1172 */
1173DECLCALLBACK(int) vmsvga3dSharedSurfaceDestroyTree(PAVLU32NODECORE pNode, void *pvParam)
1174{
1175 PVMSVGA3DSHAREDSURFACE pSharedSurface = (PVMSVGA3DSHAREDSURFACE)pNode;
1176 PVMSVGA3DSURFACE pSurface = (PVMSVGA3DSURFACE)pvParam;
1177
1178 switch (pSurface->enmD3DResType)
1179 {
1180 case VMSVGA3D_D3DRESTYPE_TEXTURE:
1181 LogFunc(("release shared texture object for context %d\n", pNode->Key));
1182 Assert(pSharedSurface->u.pTexture);
1183 D3D_RELEASE(pSharedSurface->u.pTexture);
1184 break;
1185
1186 case VMSVGA3D_D3DRESTYPE_CUBE_TEXTURE:
1187 LogFunc(("release shared cube texture object for context %d\n", pNode->Key));
1188 Assert(pSharedSurface->u.pCubeTexture);
1189 D3D_RELEASE(pSharedSurface->u.pCubeTexture);
1190 break;
1191
1192 case VMSVGA3D_D3DRESTYPE_VOLUME_TEXTURE:
1193 LogFunc(("release shared volume texture object for context %d\n", pNode->Key));
1194 Assert(pSharedSurface->u.pVolumeTexture);
1195 D3D_RELEASE(pSharedSurface->u.pVolumeTexture);
1196 break;
1197
1198 default:
1199 AssertFailed();
1200 break;
1201 }
1202 RTMemFree(pNode);
1203 return 0;
1204}
1205
1206/* Get the shared surface copy or create a new one. */
1207static PVMSVGA3DSHAREDSURFACE vmsvga3dSurfaceGetSharedCopy(PVMSVGA3DSTATE pState, PVMSVGA3DCONTEXT pContext, PVMSVGA3DSURFACE pSurface)
1208{
1209 Assert(pSurface->hSharedObject);
1210
1211 PVMSVGA3DSHAREDSURFACE pSharedSurface = (PVMSVGA3DSHAREDSURFACE)RTAvlU32Get(&pSurface->pSharedObjectTree, pContext->id);
1212 if (!pSharedSurface)
1213 {
1214 const uint32_t cWidth = pSurface->paMipmapLevels[0].mipmapSize.width;
1215 const uint32_t cHeight = pSurface->paMipmapLevels[0].mipmapSize.height;
1216 const uint32_t cDepth = pSurface->paMipmapLevels[0].mipmapSize.depth;
1217 const uint32_t numMipLevels = pSurface->cLevels;
1218
1219 LogFunc(("Create shared %stexture copy d3d (%d,%d,%d) cMip=%d usage %x format %x.\n",
1220 pSurface->enmD3DResType == VMSVGA3D_D3DRESTYPE_VOLUME_TEXTURE ? "volume " :
1221 pSurface->enmD3DResType == VMSVGA3D_D3DRESTYPE_CUBE_TEXTURE ? "cube " :
1222 pSurface->enmD3DResType == VMSVGA3D_D3DRESTYPE_TEXTURE ? "" : "UNKNOWN!!!",
1223 cWidth,
1224 cHeight,
1225 cDepth,
1226 numMipLevels,
1227 pSurface->fUsageD3D | D3DUSAGE_RENDERTARGET,
1228 pSurface->formatD3D));
1229
1230 pSharedSurface = (PVMSVGA3DSHAREDSURFACE)RTMemAllocZ(sizeof(*pSharedSurface));
1231 AssertReturn(pSharedSurface, NULL);
1232
1233 pSharedSurface->Core.Key = pContext->id;
1234 bool ret = RTAvlU32Insert(&pSurface->pSharedObjectTree, &pSharedSurface->Core);
1235 AssertReturn(ret, NULL);
1236
1237 /* Create shadow copy of the original shared texture.
1238 * Shared d3d resources require Vista+ and have some restrictions.
1239 * D3DUSAGE_RENDERTARGET is required for use as a StretchRect destination.
1240 */
1241 HRESULT hr;
1242 if (pSurface->enmD3DResType == VMSVGA3D_D3DRESTYPE_VOLUME_TEXTURE)
1243 hr = pContext->pDevice->CreateVolumeTexture(cWidth,
1244 cHeight,
1245 cDepth,
1246 numMipLevels,
1247 pSurface->fUsageD3D | D3DUSAGE_RENDERTARGET,
1248 pSurface->formatD3D,
1249 D3DPOOL_DEFAULT,
1250 &pSharedSurface->u.pVolumeTexture,
1251 &pSurface->hSharedObject);
1252 else if (pSurface->enmD3DResType == VMSVGA3D_D3DRESTYPE_CUBE_TEXTURE)
1253 hr = pContext->pDevice->CreateCubeTexture(cWidth,
1254 numMipLevels,
1255 pSurface->fUsageD3D | D3DUSAGE_RENDERTARGET,
1256 pSurface->formatD3D,
1257 D3DPOOL_DEFAULT,
1258 &pSharedSurface->u.pCubeTexture,
1259 &pSurface->hSharedObject);
1260 else if (pSurface->enmD3DResType == VMSVGA3D_D3DRESTYPE_TEXTURE)
1261 {
1262 if (pSurface->fStencilAsTexture)
1263 {
1264 /* Use the INTZ format for a depth/stencil surface that will be used as a texture */
1265 hr = pContext->pDevice->CreateTexture(cWidth,
1266 cHeight,
1267 1, /* mip levels */
1268 D3DUSAGE_DEPTHSTENCIL,
1269 FOURCC_INTZ,
1270 D3DPOOL_DEFAULT,
1271 &pSharedSurface->u.pTexture,
1272 &pSurface->hSharedObject);
1273 }
1274 else
1275 {
1276 hr = pContext->pDevice->CreateTexture(cWidth,
1277 cHeight,
1278 numMipLevels,
1279 pSurface->fUsageD3D | D3DUSAGE_RENDERTARGET,
1280 pSurface->formatD3D,
1281 D3DPOOL_DEFAULT,
1282 &pSharedSurface->u.pTexture,
1283 &pSurface->hSharedObject);
1284 }
1285 }
1286 else
1287 hr = E_FAIL;
1288
1289 if (RT_LIKELY(hr == D3D_OK))
1290 {
1291 /* Make sure that the created shared copy has the same content as the original. */
1292 PVMSVGA3DCONTEXT pAssociatedContext;
1293 int rc = vmsvga3dContextFromCid(pState, pSurface->idAssociatedContext, &pAssociatedContext);
1294 if (RT_SUCCESS(rc))
1295 AssertStmt(pAssociatedContext->pDevice, rc = VERR_INTERNAL_ERROR);
1296 if (RT_SUCCESS(rc))
1297 {
1298 IDirect3DQuery9 *pQuery;
1299 hr = pAssociatedContext->pDevice->CreateQuery(D3DQUERYTYPE_EVENT, &pQuery);
1300 if (hr == D3D_OK)
1301 {
1302 hr = pQuery->Issue(D3DISSUE_END);
1303 if (hr == D3D_OK)
1304 {
1305 do
1306 {
1307 hr = pQuery->GetData(NULL, 0, D3DGETDATA_FLUSH);
1308 } while (hr == S_FALSE);
1309 }
1310
1311 D3D_RELEASE(pQuery);
1312 }
1313 }
1314 else
1315 AssertMsgFailed(("idAssociatedContext cid=%u, sid=%u\n", pSurface->idAssociatedContext, pSurface->id));
1316 }
1317 else
1318 {
1319 AssertMsgFailed(("CreateTexture type %d failed with %x\n", pSurface->enmD3DResType, hr));
1320 RTAvlU32Remove(&pSurface->pSharedObjectTree, pContext->id);
1321 RTMemFree(pSharedSurface);
1322 return NULL;
1323 }
1324 }
1325 return pSharedSurface;
1326}
1327
1328/* Inject a query event into the D3D pipeline so we can check when usage of this surface has finished.
1329 * (D3D does not synchronize shared surface usage)
1330 */
1331static int vmsvga3dSurfaceTrackUsage(PVMSVGA3DSTATE pState, PVMSVGA3DCONTEXT pContext, PVMSVGA3DSURFACE pSurface)
1332{
1333 RT_NOREF(pState);
1334
1335 Assert(pSurface->id != SVGA3D_INVALID_ID);
1336
1337 /* Nothing to do if this surface hasn't been shared. */
1338 if (pSurface->pSharedObjectTree == NULL)
1339 return VINF_SUCCESS;
1340
1341 LogFunc(("track usage of sid=%u (cid=%u) for cid=%u, pQuery %p\n", pSurface->id, pSurface->idAssociatedContext, pContext->id, pSurface->pQuery));
1342
1343 if (pSurface->idQueryContext == pContext->id)
1344 {
1345 /* Release the previous query object, if any. */
1346 D3D_RELEASE(pSurface->pQuery);
1347 }
1348 else
1349 {
1350 /* Different context. There must be no pending drawing operations. If there are any, then a flush is missing. */
1351 if (pSurface->pQuery)
1352 {
1353 /* Should not happen. */
1354 AssertFailed();
1355
1356 /* Make sure that all drawing has completed. */
1357 vmsvga3dSurfaceFlush(pSurface);
1358 }
1359 pSurface->idQueryContext = pContext->id;
1360 }
1361
1362 HRESULT hr = pContext->pDevice->CreateQuery(D3DQUERYTYPE_EVENT, &pSurface->pQuery);
1363 AssertMsgReturn(hr == D3D_OK, ("vmsvga3dSurfaceTrackUsage: CreateQuery failed with %x\n", hr), VERR_INTERNAL_ERROR);
1364
1365 hr = pSurface->pQuery->Issue(D3DISSUE_END);
1366 AssertMsgReturn(hr == D3D_OK, ("vmsvga3dSurfaceTrackUsage: Issue failed with %x\n", hr), VERR_INTERNAL_ERROR);
1367
1368 return VINF_SUCCESS;
1369}
1370
1371
1372/**
1373 * Surface ID based version of vmsvga3dSurfaceTrackUsage.
1374 *
1375 * @returns VBox status code.
1376 * @param pState The VMSVGA3d state.
1377 * @param pContext The context.
1378 * @param sid The surface ID.
1379 */
1380static int vmsvga3dSurfaceTrackUsageById(PVMSVGA3DSTATE pState, PVMSVGA3DCONTEXT pContext, uint32_t sid)
1381{
1382 PVMSVGA3DSURFACE pSurface;
1383 int rc = vmsvga3dSurfaceFromSid(pState, sid, &pSurface);
1384 AssertRCReturn(rc, rc);
1385
1386 return vmsvga3dSurfaceTrackUsage(pState, pContext, pSurface);
1387}
1388
1389
1390/* Wait for all drawing, that uses this surface, to finish. */
1391int vmsvga3dSurfaceFlush(PVMSVGA3DSURFACE pSurface)
1392{
1393 HRESULT hr;
1394
1395 if (!pSurface->pQuery)
1396 {
1397 LogFlow(("vmsvga3dSurfaceFlush: no query object\n"));
1398 return VINF_SUCCESS; /* nothing to wait for */
1399 }
1400 Assert(pSurface->pSharedObjectTree);
1401
1402 Log(("vmsvga3dSurfaceFlush: wait for draw to finish (sid=%u)\n", pSurface->id));
1403 while (true)
1404 {
1405 hr = pSurface->pQuery->GetData(NULL, 0, D3DGETDATA_FLUSH);
1406 if (hr != S_FALSE) break;
1407
1408 RTThreadSleep(1);
1409 }
1410
1411 D3D_RELEASE(pSurface->pQuery);
1412
1413 AssertMsgReturn(hr == S_OK, ("vmsvga3dSurfaceFinishDrawing: GetData failed with %x\n", hr), VERR_INTERNAL_ERROR);
1414
1415 return VINF_SUCCESS;
1416}
1417
1418/** Get IDirect3DSurface9 for the given face and mipmap.
1419 */
1420int vmsvga3dGetD3DSurface(PVMSVGA3DSTATE pState,
1421 PVMSVGA3DCONTEXT pContext,
1422 PVMSVGA3DSURFACE pSurface,
1423 uint32_t face,
1424 uint32_t mipmap,
1425 bool fLockable,
1426 IDirect3DSurface9 **ppD3DSurf)
1427{
1428 AssertPtrReturn(pSurface->u.pSurface, VERR_INVALID_PARAMETER);
1429
1430 IDirect3DBaseTexture9 *pTexture;
1431 if (fLockable && pSurface->bounce.pTexture)
1432 pTexture = pSurface->bounce.pTexture;
1433 else
1434 pTexture = pSurface->u.pTexture;
1435
1436 if (pSurface->idAssociatedContext != pContext->id)
1437 {
1438 AssertMsgReturn(!fLockable,
1439 ("Lockable surface must be from the same context (surface cid = %d, req cid = %d)",
1440 pSurface->idAssociatedContext, pContext->id),
1441 VERR_INVALID_PARAMETER);
1442
1443 if ( pSurface->enmD3DResType == VMSVGA3D_D3DRESTYPE_TEXTURE
1444 || pSurface->enmD3DResType == VMSVGA3D_D3DRESTYPE_CUBE_TEXTURE)
1445 {
1446 LogFunc(("using texture sid=%u created for another context (%d vs %d)\n",
1447 pSurface->id, pSurface->idAssociatedContext, pContext->id));
1448
1449 PVMSVGA3DSHAREDSURFACE pSharedSurface = vmsvga3dSurfaceGetSharedCopy(pState, pContext, pSurface);
1450 AssertReturn(pSharedSurface, VERR_INTERNAL_ERROR);
1451
1452 pTexture = pSharedSurface->u.pTexture;
1453 }
1454 else
1455 {
1456 AssertMsgFailed(("surface sid=%u created for another context (%d vs %d)\n",
1457 pSurface->id, pSurface->idAssociatedContext, pContext->id));
1458 }
1459 }
1460
1461 if (pSurface->enmD3DResType == VMSVGA3D_D3DRESTYPE_CUBE_TEXTURE)
1462 {
1463 Assert(pSurface->cFaces == 6);
1464
1465 IDirect3DCubeTexture9 *p = (IDirect3DCubeTexture9 *)pTexture;
1466 D3DCUBEMAP_FACES FaceType = vmsvga3dCubemapFaceFromIndex(face);
1467 HRESULT hr = p->GetCubeMapSurface(FaceType, mipmap, ppD3DSurf);
1468 AssertMsgReturn(hr == D3D_OK, ("GetCubeMapSurface failed with %x\n", hr), VERR_INTERNAL_ERROR);
1469 }
1470 else if (pSurface->enmD3DResType == VMSVGA3D_D3DRESTYPE_TEXTURE)
1471 {
1472 Assert(pSurface->cFaces == 1);
1473 Assert(face == 0);
1474
1475 IDirect3DTexture9 *p = (IDirect3DTexture9 *)pTexture;
1476 HRESULT hr = p->GetSurfaceLevel(mipmap, ppD3DSurf);
1477 AssertMsgReturn(hr == D3D_OK, ("GetSurfaceLevel failed with %x\n", hr), VERR_INTERNAL_ERROR);
1478 }
1479 else if (pSurface->enmD3DResType == VMSVGA3D_D3DRESTYPE_SURFACE)
1480 {
1481 pSurface->u.pSurface->AddRef();
1482 *ppD3DSurf = pSurface->u.pSurface;
1483 }
1484 else
1485 {
1486 AssertMsgFailedReturn(("No surface for type %d\n", pSurface->enmD3DResType), VERR_INTERNAL_ERROR);
1487 }
1488
1489 return VINF_SUCCESS;
1490}
1491
1492static DECLCALLBACK(int) vmsvga3dBackSurfaceCopy(PVGASTATECC pThisCC, SVGA3dSurfaceImageId dest, SVGA3dSurfaceImageId src,
1493 uint32_t cCopyBoxes, SVGA3dCopyBox *pBox)
1494{
1495 PVMSVGA3DSTATE pState = pThisCC->svga.p3dState;
1496 AssertReturn(pState, VERR_NO_MEMORY);
1497
1498 const uint32_t sidSrc = src.sid;
1499 const uint32_t sidDest = dest.sid;
1500 int rc;
1501
1502 PVMSVGA3DSURFACE pSurfaceSrc;
1503 rc = vmsvga3dSurfaceFromSid(pState, sidSrc, &pSurfaceSrc);
1504 AssertRCReturn(rc, rc);
1505
1506 PVMSVGA3DSURFACE pSurfaceDest;
1507 rc = vmsvga3dSurfaceFromSid(pState, sidDest, &pSurfaceDest);
1508 AssertRCReturn(rc, rc);
1509
1510 PVMSVGA3DMIPMAPLEVEL pMipmapLevelSrc;
1511 rc = vmsvga3dMipmapLevel(pSurfaceSrc, src.face, src.mipmap, &pMipmapLevelSrc);
1512 AssertRCReturn(rc, rc);
1513
1514 PVMSVGA3DMIPMAPLEVEL pMipmapLevelDest;
1515 rc = vmsvga3dMipmapLevel(pSurfaceDest, dest.face, dest.mipmap, &pMipmapLevelDest);
1516 AssertRCReturn(rc, rc);
1517
1518 /* If src is HW and dst is not, then create the dst texture. */
1519 if ( pSurfaceSrc->u.pSurface
1520 && !pSurfaceDest->u.pSurface
1521 && RT_BOOL(pSurfaceDest->f.surfaceFlags & SVGA3D_SURFACE_HINT_TEXTURE))
1522 {
1523 /* Create the destination texture in the same context as the source texture. */
1524 uint32_t const cidSrc = pSurfaceSrc->idAssociatedContext;
1525
1526 PVMSVGA3DCONTEXT pContextSrc;
1527 rc = vmsvga3dContextFromCid(pState, cidSrc, &pContextSrc);
1528 AssertRCReturn(rc, rc);
1529 AssertReturn(pContextSrc->pDevice, VERR_INTERNAL_ERROR);
1530
1531 LogFunc(("sid=%u type=%x format=%d -> create dest texture\n", sidDest, pSurfaceDest->f.s.surface1Flags, pSurfaceDest->format));
1532 rc = vmsvga3dBackCreateTexture(pThisCC, pContextSrc, cidSrc, pSurfaceDest);
1533 AssertRCReturn(rc, rc);
1534 }
1535
1536 AssertReturn(pSurfaceSrc->enmD3DResType != VMSVGA3D_D3DRESTYPE_VOLUME_TEXTURE, VERR_NOT_IMPLEMENTED); /// @todo
1537 AssertReturn(pSurfaceDest->enmD3DResType != VMSVGA3D_D3DRESTYPE_VOLUME_TEXTURE, VERR_NOT_IMPLEMENTED); /// @todo
1538
1539 /* Surface copy only makes sense between surfaces with identical layout. */
1540 AssertReturn(pSurfaceSrc->cbBlock == pSurfaceDest->cbBlock, VERR_INVALID_PARAMETER);
1541 AssertReturn(pSurfaceSrc->cxBlock == pSurfaceDest->cxBlock, VERR_INVALID_PARAMETER);
1542 AssertReturn(pSurfaceSrc->cyBlock == pSurfaceDest->cyBlock, VERR_INVALID_PARAMETER);
1543
1544 if ( pSurfaceSrc->u.pSurface
1545 && pSurfaceDest->u.pSurface)
1546 {
1547 /* Both surfaces in hardware. Use the src context to copy one to another, because the src context may be needed
1548 * to copy data from source texture to the source bounce texture. while only the shared hardware surface is required
1549 * from the dst context.
1550 */
1551 uint32_t const cidSrc = pSurfaceSrc->idAssociatedContext;
1552
1553 PVMSVGA3DCONTEXT pContextSrc;
1554 rc = vmsvga3dContextFromCid(pState, cidSrc, &pContextSrc);
1555 AssertRCReturn(rc, rc);
1556 AssertReturn(pContextSrc->pDevice, VERR_INTERNAL_ERROR);
1557
1558 /* Must flush the other context's 3d pipeline to make sure all drawing is complete for the surface we're about to use. */
1559 vmsvga3dSurfaceFlush(pSurfaceSrc);
1560 vmsvga3dSurfaceFlush(pSurfaceDest);
1561
1562 IDirect3DSurface9 *pSrc;
1563 rc = vmsvga3dGetD3DSurface(pState, pContextSrc, pSurfaceSrc, src.face, src.mipmap, false, &pSrc);
1564 AssertRCReturn(rc, rc);
1565
1566 IDirect3DSurface9 *pDest;
1567 rc = vmsvga3dGetD3DSurface(pState, pContextSrc, pSurfaceDest, dest.face, dest.mipmap, false, &pDest);
1568 AssertRCReturnStmt(rc, D3D_RELEASE(pSrc), rc);
1569
1570 for (uint32_t i = 0; i < cCopyBoxes; ++i)
1571 {
1572 SVGA3dCopyBox clipBox = pBox[i];
1573 vmsvgaR3ClipCopyBox(&pMipmapLevelSrc->mipmapSize, &pMipmapLevelDest->mipmapSize, &clipBox);
1574 if ( !clipBox.w
1575 || !clipBox.h
1576 || !clipBox.d)
1577 {
1578 LogFunc(("Skipped empty box.\n"));
1579 continue;
1580 }
1581
1582 RECT RectSrc;
1583 RectSrc.left = clipBox.srcx;
1584 RectSrc.top = clipBox.srcy;
1585 RectSrc.right = clipBox.srcx + clipBox.w; /* exclusive */
1586 RectSrc.bottom = clipBox.srcy + clipBox.h; /* exclusive */
1587
1588 RECT RectDest;
1589 RectDest.left = clipBox.x;
1590 RectDest.top = clipBox.y;
1591 RectDest.right = clipBox.x + clipBox.w; /* exclusive */
1592 RectDest.bottom = clipBox.y + clipBox.h; /* exclusive */
1593
1594 LogFunc(("StretchRect copy src sid=%u face=%d mipmap=%d (%d,%d)(%d,%d) to dest sid=%u face=%d mipmap=%d (%d,%d)\n", sidSrc, src.face, src.mipmap, RectSrc.left, RectSrc.top, RectSrc.right, RectSrc.bottom, sidDest, dest.face, dest.mipmap, pBox[i].x, pBox[i].y));
1595
1596 if ( sidSrc == sidDest
1597 && clipBox.srcx == clipBox.x
1598 && clipBox.srcy == clipBox.y)
1599 {
1600 LogFunc(("redundant copy to the same surface at the same coordinates. Ignore.\n"));
1601 continue;
1602 }
1603 Assert(sidSrc != sidDest);
1604 Assert(!clipBox.srcz && !clipBox.z);
1605
1606 HRESULT hr = pContextSrc->pDevice->StretchRect(pSrc, &RectSrc, pDest, &RectDest, D3DTEXF_NONE);
1607 if (hr != D3D_OK)
1608 {
1609 /* This can happen for compressed texture formats for example. */
1610 LogFunc(("StretchRect failed with %x. Try a slow path.\n", hr));
1611 if ( pSurfaceSrc->bounce.pTexture
1612 && (pSurfaceSrc->fUsageD3D & D3DUSAGE_RENDERTARGET))
1613 {
1614 /* Copy the source texture mipmap level to the source bounce texture. */
1615 hr = D3D9GetRenderTargetData(pContextSrc, pSurfaceSrc, src.face, src.mipmap);
1616 AssertMsg(hr == D3D_OK, ("D3D9GetRenderTargetData failed with %x\n", hr));
1617 if (hr == D3D_OK)
1618 {
1619 /* Copy the source bounce texture to the destination surface. */
1620 IDirect3DSurface9 *pSrcBounce;
1621 rc = vmsvga3dGetD3DSurface(pState, pContextSrc, pSurfaceSrc, src.face, src.mipmap, true, &pSrcBounce);
1622 if (RT_SUCCESS(rc))
1623 {
1624 POINT pointDest;
1625 pointDest.x = clipBox.x;
1626 pointDest.y = clipBox.y;
1627
1628 hr = pContextSrc->pDevice->UpdateSurface(pSrcBounce, &RectSrc, pDest, &pointDest);
1629 Assert(hr == D3D_OK);
1630
1631 D3D_RELEASE(pSrcBounce);
1632 }
1633 else
1634 {
1635 AssertRC(rc);
1636 hr = E_INVALIDARG;
1637 }
1638 }
1639 }
1640 else if ( (pSurfaceSrc->fUsageD3D & D3DUSAGE_RENDERTARGET) == 0
1641 && (pSurfaceDest->fUsageD3D & D3DUSAGE_RENDERTARGET) == 0)
1642 {
1643 /* Can lock both. */
1644 D3DLOCKED_RECT LockedSrcRect;
1645 hr = pSrc->LockRect(&LockedSrcRect, &RectSrc, D3DLOCK_READONLY);
1646 Assert(hr == D3D_OK);
1647 if (SUCCEEDED(hr))
1648 {
1649 D3DLOCKED_RECT LockedDestRect;
1650 hr = pDest->LockRect(&LockedDestRect, &RectDest, 0);
1651 Assert(hr == D3D_OK);
1652 if (SUCCEEDED(hr))
1653 {
1654 uint32_t cBlocksX = (clipBox.w + pSurfaceSrc->cxBlock - 1) / pSurfaceSrc->cxBlock;
1655 uint32_t cBlocksY = (clipBox.h + pSurfaceSrc->cyBlock - 1) / pSurfaceSrc->cyBlock;
1656
1657 uint32_t cbToCopy = cBlocksX * pSurfaceSrc->cbBlock;
1658 cbToCopy = RT_MIN(cbToCopy, (uint32_t)RT_ABS(LockedDestRect.Pitch));
1659 cbToCopy = RT_MIN(cbToCopy, (uint32_t)RT_ABS(LockedSrcRect.Pitch));
1660
1661 uint8_t *pu8Dst = (uint8_t *)LockedDestRect.pBits;
1662 const uint8_t *pu8Src = (uint8_t *)LockedSrcRect.pBits;
1663 for (uint32_t j = 0; j < cBlocksY; ++j)
1664 {
1665 memcpy(pu8Dst, pu8Src, cbToCopy);
1666 pu8Dst += LockedDestRect.Pitch;
1667 pu8Src += LockedSrcRect.Pitch;
1668 }
1669
1670 hr = pDest->UnlockRect();
1671 Assert(hr == D3D_OK);
1672 }
1673
1674 hr = pSrc->UnlockRect();
1675 Assert(hr == D3D_OK);
1676 }
1677 }
1678 }
1679 AssertMsgReturnStmt(hr == D3D_OK,
1680 ("StretchRect failed with %x\n", hr),
1681 D3D_RELEASE(pDest); D3D_RELEASE(pSrc),
1682 VERR_INTERNAL_ERROR);
1683 }
1684
1685 D3D_RELEASE(pDest);
1686 D3D_RELEASE(pSrc);
1687
1688 /* Track the StretchRect operation. */
1689 vmsvga3dSurfaceTrackUsage(pState, pContextSrc, pSurfaceSrc);
1690 vmsvga3dSurfaceTrackUsage(pState, pContextSrc, pSurfaceDest);
1691 }
1692 else
1693 {
1694 /* One of the surfaces is in memory.
1695 *
1696 * Copy from/to memory to/from a HW surface. Or mem->mem.
1697 * Use the context of the HW surface, if any.
1698 */
1699 PVMSVGA3DCONTEXT pContext = NULL;
1700 IDirect3DSurface9 *pD3DSurf = NULL;
1701
1702 if (pSurfaceSrc->u.pSurface)
1703 {
1704 AssertReturn(!pSurfaceDest->u.pSurface, VERR_INTERNAL_ERROR);
1705
1706 rc = vmsvga3dContextFromCid(pState, pSurfaceSrc->idAssociatedContext, &pContext);
1707 AssertRCReturn(rc, rc);
1708 AssertReturn(pContext->pDevice, VERR_INTERNAL_ERROR);
1709
1710 rc = vmsvga3dGetD3DSurface(pState, pContext, pSurfaceSrc, src.face, src.mipmap, true, &pD3DSurf);
1711 AssertRCReturn(rc, rc);
1712 }
1713 else if (pSurfaceDest->u.pSurface)
1714 {
1715 AssertReturn(!pSurfaceSrc->u.pSurface, VERR_INTERNAL_ERROR);
1716
1717 rc = vmsvga3dContextFromCid(pState, pSurfaceDest->idAssociatedContext, &pContext);
1718 AssertRCReturn(rc, rc);
1719 AssertReturn(pContext->pDevice, VERR_INTERNAL_ERROR);
1720
1721 rc = vmsvga3dGetD3DSurface(pState, pContext, pSurfaceDest, dest.face, dest.mipmap, true, &pD3DSurf);
1722 AssertRCReturn(rc, rc);
1723 }
1724
1725 for (uint32_t i = 0; i < cCopyBoxes; ++i)
1726 {
1727 HRESULT hr;
1728
1729 SVGA3dCopyBox clipBox = pBox[i];
1730 vmsvgaR3ClipCopyBox(&pMipmapLevelSrc->mipmapSize, &pMipmapLevelDest->mipmapSize, &clipBox);
1731 if ( !clipBox.w
1732 || !clipBox.h
1733 || !clipBox.d)
1734 {
1735 LogFunc(("Skipped empty box.\n"));
1736 continue;
1737 }
1738
1739 RECT RectSrc;
1740 RectSrc.left = clipBox.srcx;
1741 RectSrc.top = clipBox.srcy;
1742 RectSrc.right = clipBox.srcx + clipBox.w; /* exclusive */
1743 RectSrc.bottom = clipBox.srcy + clipBox.h; /* exclusive */
1744
1745 RECT RectDest;
1746 RectDest.left = clipBox.x;
1747 RectDest.top = clipBox.y;
1748 RectDest.right = clipBox.x + clipBox.w; /* exclusive */
1749 RectDest.bottom = clipBox.y + clipBox.h; /* exclusive */
1750
1751 LogFunc(("(manual) copy sid=%u face=%d mipmap=%d (%d,%d)(%d,%d) to sid=%u face=%d mipmap=%d (%d,%d)\n",
1752 sidSrc, src.face, src.mipmap, RectSrc.left, RectSrc.top, RectSrc.right, RectSrc.bottom,
1753 sidDest, dest.face, dest.mipmap, pBox[i].x, pBox[i].y));
1754
1755 Assert(!clipBox.srcz && !clipBox.z);
1756
1757 uint32_t cBlocksX = (clipBox.w + pSurfaceSrc->cxBlock - 1) / pSurfaceSrc->cxBlock;
1758 uint32_t cBlocksY = (clipBox.h + pSurfaceSrc->cyBlock - 1) / pSurfaceSrc->cyBlock;
1759
1760 D3DLOCKED_RECT LockedSrcRect;
1761 if (!pSurfaceSrc->u.pSurface)
1762 {
1763 uint32_t u32BlockX = clipBox.srcx / pSurfaceSrc->cxBlock;
1764 uint32_t u32BlockY = clipBox.srcy / pSurfaceSrc->cyBlock;
1765 Assert(u32BlockX * pSurfaceSrc->cxBlock == clipBox.srcx);
1766 Assert(u32BlockY * pSurfaceSrc->cyBlock == clipBox.srcy);
1767
1768 LockedSrcRect.pBits = (uint8_t *)pMipmapLevelSrc->pSurfaceData +
1769 pMipmapLevelSrc->cbSurfacePitch * u32BlockY + pSurfaceSrc->cbBlock * u32BlockX;
1770 LockedSrcRect.Pitch = pMipmapLevelSrc->cbSurfacePitch;
1771 }
1772 else
1773 {
1774 /* Must flush the context's 3d pipeline to make sure all drawing is complete for the surface we're about to use. */
1775 vmsvga3dSurfaceFlush(pSurfaceSrc);
1776
1777 hr = pD3DSurf->LockRect(&LockedSrcRect, &RectSrc, D3DLOCK_READONLY);
1778 AssertMsgReturnStmt(hr == D3D_OK, ("LockRect failed with %x\n", hr), D3D_RELEASE(pD3DSurf), VERR_INTERNAL_ERROR);
1779 }
1780
1781 D3DLOCKED_RECT LockedDestRect;
1782 if (!pSurfaceDest->u.pSurface)
1783 {
1784 uint32_t u32BlockX = clipBox.x / pSurfaceDest->cxBlock;
1785 uint32_t u32BlockY = clipBox.y / pSurfaceDest->cyBlock;
1786 Assert(u32BlockX * pSurfaceDest->cxBlock == clipBox.x);
1787 Assert(u32BlockY * pSurfaceDest->cyBlock == clipBox.y);
1788
1789 LockedDestRect.pBits = (uint8_t *)pMipmapLevelDest->pSurfaceData +
1790 pMipmapLevelDest->cbSurfacePitch * u32BlockY + pSurfaceDest->cbBlock * u32BlockX;
1791 LockedDestRect.Pitch = pMipmapLevelDest->cbSurfacePitch;
1792 pSurfaceDest->fDirty = true;
1793 }
1794 else
1795 {
1796 /* Must flush the context's 3d pipeline to make sure all drawing is complete for the surface we're about to use. */
1797 vmsvga3dSurfaceFlush(pSurfaceDest);
1798
1799 hr = pD3DSurf->LockRect(&LockedDestRect, &RectDest, 0);
1800 AssertMsgReturnStmt(hr == D3D_OK, ("LockRect failed with %x\n", hr), D3D_RELEASE(pD3DSurf), VERR_INTERNAL_ERROR);
1801 }
1802
1803 uint8_t *pDest = (uint8_t *)LockedDestRect.pBits;
1804 const uint8_t *pSrc = (uint8_t *)LockedSrcRect.pBits;
1805 for (uint32_t j = 0; j < cBlocksY; ++j)
1806 {
1807 memcpy(pDest, pSrc, cBlocksX * pSurfaceSrc->cbBlock);
1808 pDest += LockedDestRect.Pitch;
1809 pSrc += LockedSrcRect.Pitch;
1810 }
1811
1812 if (pD3DSurf)
1813 {
1814 hr = pD3DSurf->UnlockRect();
1815 AssertMsgReturnStmt(hr == D3D_OK, ("Unlock failed with %x\n", hr), D3D_RELEASE(pD3DSurf), VERR_INTERNAL_ERROR);
1816 }
1817 }
1818
1819 D3D_RELEASE(pD3DSurf);
1820
1821 /* If the destination bounce texture has been used, then update the actual destination texture. */
1822 if ( pSurfaceDest->u.pTexture
1823 && pSurfaceDest->bounce.pTexture
1824 && ( pSurfaceDest->enmD3DResType == VMSVGA3D_D3DRESTYPE_TEXTURE
1825 || pSurfaceDest->enmD3DResType == VMSVGA3D_D3DRESTYPE_CUBE_TEXTURE))
1826 {
1827 AssertMsgReturn(pContext, ("Context is NULL\n"), VERR_INTERNAL_ERROR);
1828
1829 /* Copy the new content to the actual texture object. */
1830 HRESULT hr2 = D3D9UpdateTexture(pContext, pSurfaceDest);
1831 AssertMsg(hr2 == D3D_OK, ("UpdateTexture failed with %x\n", hr2)); RT_NOREF(hr2);
1832
1833 /* Track the UpdateTexture operation. */
1834 vmsvga3dSurfaceTrackUsage(pState, pContext, pSurfaceDest);
1835 }
1836 }
1837
1838 return VINF_SUCCESS;
1839}
1840
1841
1842/**
1843 * Create D3D/OpenGL texture object for the specified surface.
1844 *
1845 * Surfaces are created when needed.
1846 *
1847 * @param pThisCC The device context.
1848 * @param pContext The context.
1849 * @param idAssociatedContext Probably the same as pContext->id.
1850 * @param pSurface The surface to create the texture for.
1851 */
1852static DECLCALLBACK(int) vmsvga3dBackCreateTexture(PVGASTATECC pThisCC, PVMSVGA3DCONTEXT pContext, uint32_t idAssociatedContext,
1853 PVMSVGA3DSURFACE pSurface)
1854
1855{
1856 PVMSVGA3DSTATE pState = pThisCC->svga.p3dState;
1857 AssertReturn(pState, VERR_NO_MEMORY);
1858
1859 HRESULT hr;
1860
1861 LogFunc(("sid=%u\n", pSurface->id));
1862
1863 Assert(pSurface->hSharedObject == NULL);
1864 Assert(pSurface->u.pTexture == NULL);
1865 Assert(pSurface->bounce.pTexture == NULL);
1866 Assert(pSurface->enmD3DResType == VMSVGA3D_D3DRESTYPE_NONE);
1867
1868 const uint32_t cWidth = pSurface->paMipmapLevels[0].mipmapSize.width;
1869 const uint32_t cHeight = pSurface->paMipmapLevels[0].mipmapSize.height;
1870 const uint32_t cDepth = pSurface->paMipmapLevels[0].mipmapSize.depth;
1871 const uint32_t numMipLevels = pSurface->cLevels;
1872
1873 /*
1874 * Create D3D texture object.
1875 */
1876 if (pSurface->f.surfaceFlags & SVGA3D_SURFACE_CUBEMAP)
1877 {
1878 Assert(pSurface->cFaces == 6);
1879 Assert(cWidth == cHeight);
1880 Assert(cDepth == 1);
1881
1882 hr = pContext->pDevice->CreateCubeTexture(cWidth,
1883 numMipLevels,
1884 pSurface->fUsageD3D,
1885 pSurface->formatD3D,
1886 D3DPOOL_DEFAULT,
1887 &pSurface->u.pCubeTexture,
1888 &pSurface->hSharedObject);
1889 if (hr == D3D_OK)
1890 {
1891 /* Create another texture object to serve as a bounce buffer as the above texture surface can't be locked. */
1892 hr = pContext->pDevice->CreateCubeTexture(cWidth,
1893 numMipLevels,
1894 (pSurface->fUsageD3D & ~D3DUSAGE_RENDERTARGET) | D3DUSAGE_DYNAMIC /* Lockable */,
1895 pSurface->formatD3D,
1896 D3DPOOL_SYSTEMMEM,
1897 &pSurface->bounce.pCubeTexture,
1898 NULL);
1899 AssertMsgReturnStmt(hr == D3D_OK,
1900 ("CreateCubeTexture (systemmem) failed with %x\n", hr),
1901 D3D_RELEASE(pSurface->u.pCubeTexture),
1902 VERR_INTERNAL_ERROR);
1903 }
1904 else
1905 {
1906 Log(("Format not accepted -> try old method\n"));
1907 /* The format was probably not accepted; fall back to our old mode. */
1908 hr = pContext->pDevice->CreateCubeTexture(cWidth,
1909 numMipLevels,
1910 (pSurface->fUsageD3D & ~D3DUSAGE_RENDERTARGET) | D3DUSAGE_DYNAMIC /* Lockable */,
1911 pSurface->formatD3D,
1912 D3DPOOL_DEFAULT,
1913 &pSurface->u.pCubeTexture,
1914 &pSurface->hSharedObject);
1915 AssertMsgReturn(hr == D3D_OK, ("CreateCubeTexture (fallback) failed with %x\n", hr), VERR_INTERNAL_ERROR);
1916 }
1917
1918 pSurface->enmD3DResType = VMSVGA3D_D3DRESTYPE_CUBE_TEXTURE;
1919 }
1920 else if ( pSurface->formatD3D == D3DFMT_D24S8
1921 || pSurface->formatD3D == D3DFMT_D24X8
1922 || pSurface->formatD3D == D3DFMT_D32
1923 || pSurface->formatD3D == D3DFMT_D16)
1924 {
1925 Assert(pSurface->cFaces == 1);
1926 Assert(pSurface->cLevels == 1);
1927 Assert(cDepth == 1);
1928
1929 /* Use the INTZ format for a depth/stencil surface that will be used as a texture */
1930 hr = pContext->pDevice->CreateTexture(cWidth,
1931 cHeight,
1932 1, /* mip levels */
1933 D3DUSAGE_DEPTHSTENCIL,
1934 FOURCC_INTZ,
1935 D3DPOOL_DEFAULT,
1936 &pSurface->u.pTexture,
1937 &pSurface->hSharedObject /* might result in poor performance */);
1938 if ( hr == D3D_OK
1939 && ( pSurface->formatD3D == D3DFMT_D24S8
1940 || pSurface->formatD3D == D3DFMT_D24X8))
1941 {
1942 /* Create another texture object to serve as a bounce buffer as the
1943 * D3DFMT_D24S8 and D3DFMT_D24X8 surface can't be locked apparently (from testing).
1944 */
1945 hr = pContext->pDevice->CreateTexture(cWidth,
1946 cHeight,
1947 1, /* mip levels */
1948 D3DUSAGE_DYNAMIC /* Lockable */,
1949 FOURCC_INTZ,
1950 D3DPOOL_SYSTEMMEM,
1951 &pSurface->bounce.pTexture,
1952 NULL);
1953
1954 AssertMsgReturnStmt(hr == D3D_OK,
1955 ("CreateTexture (systemmem) failed with %x\n", hr),
1956 D3D_RELEASE(pSurface->u.pTexture),
1957 VERR_INTERNAL_ERROR);
1958 }
1959
1960 AssertMsgReturn(hr == D3D_OK, ("CreateTexture INTZ failed with %x\n", hr), VERR_INTERNAL_ERROR);
1961
1962 pSurface->fStencilAsTexture = true;
1963 pSurface->enmD3DResType = VMSVGA3D_D3DRESTYPE_TEXTURE;
1964 }
1965 else
1966 {
1967 if (cDepth > 1)
1968 {
1969 hr = pContext->pDevice->CreateVolumeTexture(cWidth,
1970 cHeight,
1971 cDepth,
1972 numMipLevels,
1973 pSurface->fUsageD3D,
1974 pSurface->formatD3D,
1975 D3DPOOL_DEFAULT,
1976 &pSurface->u.pVolumeTexture,
1977 &pSurface->hSharedObject);
1978 if (hr == D3D_OK)
1979 {
1980 /* Create another texture object to serve as a bounce buffer as the above texture surface can't be locked. */
1981 hr = pContext->pDevice->CreateVolumeTexture(cWidth,
1982 cHeight,
1983 cDepth,
1984 numMipLevels,
1985 (pSurface->fUsageD3D & ~D3DUSAGE_RENDERTARGET) | D3DUSAGE_DYNAMIC /* Lockable */,
1986 pSurface->formatD3D,
1987 D3DPOOL_SYSTEMMEM,
1988 &pSurface->bounce.pVolumeTexture,
1989 NULL);
1990 AssertMsgReturnStmt(hr == D3D_OK,
1991 ("CreateVolumeTexture (systemmem) failed with %x\n", hr),
1992 D3D_RELEASE(pSurface->u.pVolumeTexture),
1993 VERR_INTERNAL_ERROR);
1994 }
1995 else
1996 {
1997 Log(("Format not accepted -> try old method\n"));
1998 /* The format was probably not accepted; fall back to our old mode. */
1999 hr = pContext->pDevice->CreateVolumeTexture(cWidth,
2000 cHeight,
2001 cDepth,
2002 numMipLevels,
2003 (pSurface->fUsageD3D & ~D3DUSAGE_RENDERTARGET) | D3DUSAGE_DYNAMIC /* Lockable */,
2004 pSurface->formatD3D,
2005 D3DPOOL_DEFAULT,
2006 &pSurface->u.pVolumeTexture,
2007 &pSurface->hSharedObject);
2008 AssertMsgReturn(hr == D3D_OK, ("CreateVolumeTexture (fallback) failed with %x\n", hr), VERR_INTERNAL_ERROR);
2009 }
2010
2011 pSurface->enmD3DResType = VMSVGA3D_D3DRESTYPE_VOLUME_TEXTURE;
2012 }
2013 else
2014 {
2015 Assert(pSurface->cFaces == 1);
2016
2017 hr = pContext->pDevice->CreateTexture(cWidth,
2018 cHeight,
2019 numMipLevels,
2020 pSurface->fUsageD3D | D3DUSAGE_RENDERTARGET /* required for use as a StretchRect destination */,
2021 pSurface->formatD3D,
2022 D3DPOOL_DEFAULT,
2023 &pSurface->u.pTexture,
2024 &pSurface->hSharedObject);
2025 if (hr == D3D_OK)
2026 {
2027 /* Create another texture object to serve as a bounce buffer as the above texture surface can't be locked. */
2028 hr = pContext->pDevice->CreateTexture(cWidth,
2029 cHeight,
2030 numMipLevels,
2031 (pSurface->fUsageD3D & ~D3DUSAGE_RENDERTARGET) | D3DUSAGE_DYNAMIC /* Lockable */,
2032 pSurface->formatD3D,
2033 D3DPOOL_SYSTEMMEM,
2034 &pSurface->bounce.pTexture,
2035 NULL);
2036
2037 AssertMsgReturn(hr == D3D_OK, ("CreateTexture (systemmem) failed with %x\n", hr), VERR_INTERNAL_ERROR);
2038
2039 if (pSurface->formatD3D != pSurface->d3dfmtRequested)
2040 {
2041 /* Create a staging texture/render target for format conversion. */
2042 hr = pContext->pDevice->CreateTexture(cWidth,
2043 cHeight,
2044 numMipLevels,
2045 pSurface->fUsageD3D | D3DUSAGE_RENDERTARGET,
2046 pSurface->formatD3D,
2047 D3DPOOL_DEFAULT,
2048 &pSurface->emulated.pTexture,
2049 NULL);
2050 AssertMsgReturn(hr == D3D_OK, ("CreateTexture (emulated) failed with %x\n", hr), VERR_INTERNAL_ERROR);
2051 }
2052 }
2053 else
2054 {
2055 Log(("Format not accepted (%x) -> try old method\n", hr));
2056 /* The format was probably not accepted; fall back to our old mode. */
2057 hr = pContext->pDevice->CreateTexture(cWidth,
2058 cHeight,
2059 numMipLevels,
2060 (pSurface->fUsageD3D & ~D3DUSAGE_RENDERTARGET) | D3DUSAGE_DYNAMIC /* Lockable */,
2061 pSurface->formatD3D,
2062 D3DPOOL_DEFAULT,
2063 &pSurface->u.pTexture,
2064 &pSurface->hSharedObject /* might result in poor performance */);
2065 AssertMsgReturn(hr == D3D_OK, ("CreateTexture failed with %x\n", hr), VERR_INTERNAL_ERROR);
2066 }
2067
2068 pSurface->enmD3DResType = VMSVGA3D_D3DRESTYPE_TEXTURE;
2069 }
2070 }
2071
2072 Assert(hr == D3D_OK);
2073
2074 if (pSurface->autogenFilter != SVGA3D_TEX_FILTER_NONE)
2075 {
2076 /* Set the mip map generation filter settings. */
2077 IDirect3DBaseTexture9 *pBaseTexture;
2078 if (pSurface->enmD3DResType == VMSVGA3D_D3DRESTYPE_VOLUME_TEXTURE)
2079 pBaseTexture = pSurface->u.pVolumeTexture;
2080 else if (pSurface->enmD3DResType == VMSVGA3D_D3DRESTYPE_CUBE_TEXTURE)
2081 pBaseTexture = pSurface->u.pCubeTexture;
2082 else
2083 pBaseTexture = pSurface->u.pTexture;
2084 hr = pBaseTexture->SetAutoGenFilterType((D3DTEXTUREFILTERTYPE)pSurface->autogenFilter);
2085 AssertMsg(hr == D3D_OK, ("vmsvga3dBackCreateTexture: SetAutoGenFilterType failed with %x\n", hr));
2086 }
2087
2088 /*
2089 * Always initialize all mipmap levels using the in memory data
2090 * to make sure that the just created texture has the up-to-date content.
2091 * The OpenGL backend does this too.
2092 */
2093 Log(("vmsvga3dBackCreateTexture: sync texture\n"));
2094
2095 if (pSurface->enmD3DResType == VMSVGA3D_D3DRESTYPE_VOLUME_TEXTURE)
2096 {
2097 IDirect3DVolumeTexture9 *pVolumeTexture = pSurface->bounce.pVolumeTexture ?
2098 pSurface->bounce.pVolumeTexture :
2099 pSurface->u.pVolumeTexture;
2100
2101 for (uint32_t i = 0; i < numMipLevels; ++i)
2102 {
2103 D3DLOCKED_BOX LockedVolume;
2104 hr = pVolumeTexture->LockBox(i, &LockedVolume, NULL, D3DLOCK_DISCARD);
2105 AssertMsgBreak(hr == D3D_OK, ("LockBox failed with %x\n", hr));
2106
2107 PVMSVGA3DMIPMAPLEVEL pMipLevel = &pSurface->paMipmapLevels[i];
2108
2109 LogFunc(("sync volume texture mipmap level %d (pitch row %x vs %x, slice %x vs %x)\n",
2110 i, LockedVolume.RowPitch, pMipLevel->cbSurfacePitch, LockedVolume.SlicePitch, pMipLevel->cbSurfacePlane));
2111
2112
2113 uint8_t *pDst = (uint8_t *)LockedVolume.pBits;
2114 const uint8_t *pSrc = (uint8_t *)pMipLevel->pSurfaceData;
2115 for (uint32_t d = 0; d < cDepth; ++d)
2116 {
2117 uint8_t *pRowDst = pDst;
2118 const uint8_t *pRowSrc = pSrc;
2119 for (uint32_t h = 0; h < pMipLevel->cBlocksY; ++h)
2120 {
2121 memcpy(pRowDst, pRowSrc, pMipLevel->cbSurfacePitch);
2122 pRowDst += LockedVolume.RowPitch;
2123 pRowSrc += pMipLevel->cbSurfacePitch;
2124 }
2125 pDst += LockedVolume.SlicePitch;
2126 pSrc += pMipLevel->cbSurfacePlane;
2127 }
2128
2129 hr = pVolumeTexture->UnlockBox(i);
2130 AssertMsgBreak(hr == D3D_OK, ("UnlockBox failed with %x\n", hr));
2131
2132 pMipLevel->fDirty = false;
2133 }
2134 }
2135 else if (pSurface->enmD3DResType == VMSVGA3D_D3DRESTYPE_CUBE_TEXTURE)
2136 {
2137 IDirect3DCubeTexture9 *pCubeTexture = pSurface->bounce.pCubeTexture ?
2138 pSurface->bounce.pCubeTexture :
2139 pSurface->u.pCubeTexture;
2140
2141 for (uint32_t iFace = 0; iFace < 6; ++iFace)
2142 {
2143 const D3DCUBEMAP_FACES Face = vmsvga3dCubemapFaceFromIndex(iFace);
2144
2145 for (uint32_t i = 0; i < numMipLevels; ++i)
2146 {
2147 D3DLOCKED_RECT LockedRect;
2148 hr = pCubeTexture->LockRect(Face,
2149 i, /* texture level */
2150 &LockedRect,
2151 NULL, /* entire texture */
2152 0);
2153 AssertMsgBreak(hr == D3D_OK, ("LockRect failed with %x\n", hr));
2154
2155 PVMSVGA3DMIPMAPLEVEL pMipLevel = &pSurface->paMipmapLevels[iFace * numMipLevels + i];
2156
2157 LogFunc(("sync texture face %d mipmap level %d (pitch %x vs %x)\n",
2158 iFace, i, LockedRect.Pitch, pMipLevel->cbSurfacePitch));
2159
2160 uint8_t *pDest = (uint8_t *)LockedRect.pBits;
2161 const uint8_t *pSrc = (uint8_t *)pMipLevel->pSurfaceData;
2162 for (uint32_t j = 0; j < pMipLevel->cBlocksY; ++j)
2163 {
2164 memcpy(pDest, pSrc, pMipLevel->cbSurfacePitch);
2165
2166 pDest += LockedRect.Pitch;
2167 pSrc += pMipLevel->cbSurfacePitch;
2168 }
2169
2170 hr = pCubeTexture->UnlockRect(Face, i /* texture level */);
2171 AssertMsgBreak(hr == D3D_OK, ("UnlockRect failed with %x\n", hr));
2172
2173 pMipLevel->fDirty = false;
2174 }
2175
2176 if (hr != D3D_OK)
2177 break;
2178 }
2179
2180 if (hr != D3D_OK)
2181 {
2182 D3D_RELEASE(pSurface->bounce.pCubeTexture);
2183 D3D_RELEASE(pSurface->u.pCubeTexture);
2184 return VERR_INTERNAL_ERROR;
2185 }
2186 }
2187 else if (pSurface->enmD3DResType == VMSVGA3D_D3DRESTYPE_TEXTURE)
2188 {
2189 IDirect3DTexture9 *pTexture;
2190 if (pSurface->bounce.pTexture)
2191 pTexture = pSurface->bounce.pTexture;
2192 else if (pSurface->formatD3D != pSurface->d3dfmtRequested)
2193 pTexture = pSurface->emulated.pTexture;
2194 else
2195 pTexture = pSurface->u.pTexture;
2196
2197 for (uint32_t i = 0; i < numMipLevels; ++i)
2198 {
2199 D3DLOCKED_RECT LockedRect;
2200
2201 hr = pTexture->LockRect(i, /* texture level */
2202 &LockedRect,
2203 NULL, /* entire texture */
2204 0);
2205
2206 AssertMsgReturn(hr == D3D_OK, ("vmsvga3dBackCreateTexture: LockRect failed with %x\n", hr), VERR_INTERNAL_ERROR);
2207
2208 LogFunc(("sync texture mipmap level %d (pitch %x vs %x)\n", i, LockedRect.Pitch, pSurface->paMipmapLevels[i].cbSurfacePitch));
2209
2210 uint8_t *pDest = (uint8_t *)LockedRect.pBits;
2211 const uint8_t *pSrc = (uint8_t *)pSurface->paMipmapLevels[i].pSurfaceData;
2212 for (uint32_t j = 0; j < pSurface->paMipmapLevels[i].cBlocksY; ++j)
2213 {
2214 memcpy(pDest, pSrc, pSurface->paMipmapLevels[i].cbSurfacePitch);
2215
2216 pDest += LockedRect.Pitch;
2217 pSrc += pSurface->paMipmapLevels[i].cbSurfacePitch;
2218 }
2219
2220 hr = pTexture->UnlockRect(i /* texture level */);
2221 AssertMsgReturn(hr == D3D_OK, ("vmsvga3dBackCreateTexture: UnlockRect failed with %x\n", hr), VERR_INTERNAL_ERROR);
2222
2223 pSurface->paMipmapLevels[i].fDirty = false;
2224 }
2225 }
2226 else
2227 {
2228 AssertMsgFailedReturn(("enmD3DResType not expected %d\n", pSurface->enmD3DResType), VERR_INTERNAL_ERROR);
2229 }
2230
2231 if (pSurface->bounce.pTexture)
2232 {
2233 Log(("vmsvga3dBackCreateTexture: sync dirty texture from bounce buffer\n"));
2234 if (pSurface->enmD3DResType == VMSVGA3D_D3DRESTYPE_VOLUME_TEXTURE)
2235 hr = pContext->pDevice->UpdateTexture(pSurface->bounce.pVolumeTexture, pSurface->u.pVolumeTexture);
2236 else
2237 hr = D3D9UpdateTexture(pContext, pSurface);
2238 AssertMsgReturn(hr == D3D_OK, ("UpdateTexture failed with %x\n", hr), VERR_INTERNAL_ERROR);
2239
2240 /* We will now use the bounce texture for all memory accesses, so free our surface memory buffer. */
2241 for (uint32_t i = 0; i < pSurface->cLevels; i++)
2242 {
2243 RTMemFree(pSurface->paMipmapLevels[i].pSurfaceData);
2244 pSurface->paMipmapLevels[i].pSurfaceData = NULL;
2245 }
2246
2247 /* Track the UpdateTexture operation. */
2248 vmsvga3dSurfaceTrackUsage(pState, pContext, pSurface);
2249 }
2250 pSurface->fDirty = false;
2251
2252 Assert(pSurface->enmD3DResType != VMSVGA3D_D3DRESTYPE_NONE);
2253
2254 pSurface->f.surfaceFlags |= SVGA3D_SURFACE_HINT_TEXTURE;
2255 pSurface->idAssociatedContext = idAssociatedContext;
2256 return VINF_SUCCESS;
2257}
2258
2259
2260/**
2261 * Backend worker for implementing SVGA_3D_CMD_SURFACE_STRETCHBLT.
2262 *
2263 * @returns VBox status code.
2264 * @param pThis The VGA device instance.
2265 * @param pState The VMSVGA3d state.
2266 * @param pDstSurface The destination host surface.
2267 * @param uDstFace The destination face (valid).
2268 * @param uDstMipmap The destination mipmap level (valid).
2269 * @param pDstBox The destination box.
2270 * @param pSrcSurface The source host surface.
2271 * @param uSrcFace The destination face (valid).
2272 * @param uSrcMipmap The source mimap level (valid).
2273 * @param pSrcBox The source box.
2274 * @param enmMode The strecht blt mode .
2275 * @param pContext The VMSVGA3d context (already current for OGL).
2276 */
2277static DECLCALLBACK(int) vmsvga3dBackSurfaceStretchBlt(PVGASTATE pThis, PVMSVGA3DSTATE pState,
2278 PVMSVGA3DSURFACE pDstSurface, uint32_t uDstFace, uint32_t uDstMipmap, SVGA3dBox const *pDstBox,
2279 PVMSVGA3DSURFACE pSrcSurface, uint32_t uSrcFace, uint32_t uSrcMipmap, SVGA3dBox const *pSrcBox,
2280 SVGA3dStretchBltMode enmMode, PVMSVGA3DCONTEXT pContext)
2281{
2282 RT_NOREF(pThis);
2283
2284 HRESULT hr;
2285 int rc;
2286
2287 AssertReturn(pSrcSurface->enmD3DResType != VMSVGA3D_D3DRESTYPE_VOLUME_TEXTURE, VERR_NOT_IMPLEMENTED);
2288 AssertReturn(pDstSurface->enmD3DResType != VMSVGA3D_D3DRESTYPE_VOLUME_TEXTURE, VERR_NOT_IMPLEMENTED);
2289
2290 /* Flush the drawing pipeline for this surface as it could be used in a shared context. */
2291 vmsvga3dSurfaceFlush(pSrcSurface);
2292 vmsvga3dSurfaceFlush(pDstSurface);
2293
2294 RECT RectSrc;
2295 RectSrc.left = pSrcBox->x;
2296 RectSrc.top = pSrcBox->y;
2297 RectSrc.right = pSrcBox->x + pSrcBox->w; /* exclusive */
2298 RectSrc.bottom = pSrcBox->y + pSrcBox->h; /* exclusive */
2299 Assert(!pSrcBox->z);
2300
2301 RECT RectDst;
2302 RectDst.left = pDstBox->x;
2303 RectDst.top = pDstBox->y;
2304 RectDst.right = pDstBox->x + pDstBox->w; /* exclusive */
2305 RectDst.bottom = pDstBox->y + pDstBox->h; /* exclusive */
2306 Assert(!pDstBox->z);
2307
2308 IDirect3DSurface9 *pSrc;
2309 rc = vmsvga3dGetD3DSurface(pState, pContext, pSrcSurface, uSrcFace, uSrcMipmap, false, &pSrc);
2310 AssertRCReturn(rc, rc);
2311
2312 IDirect3DSurface9 *pDst;
2313 rc = vmsvga3dGetD3DSurface(pState, pContext, pDstSurface, uDstFace, uDstMipmap, false, &pDst);
2314 AssertRCReturn(rc, rc);
2315
2316 D3DTEXTUREFILTERTYPE moded3d;
2317 switch (enmMode)
2318 {
2319 case SVGA3D_STRETCH_BLT_POINT:
2320 moded3d = D3DTEXF_POINT;
2321 break;
2322
2323 case SVGA3D_STRETCH_BLT_LINEAR:
2324 moded3d = D3DTEXF_LINEAR;
2325 break;
2326
2327 default:
2328 AssertFailed();
2329 moded3d = D3DTEXF_NONE;
2330 break;
2331 }
2332
2333 hr = pContext->pDevice->StretchRect(pSrc, &RectSrc, pDst, &RectDst, moded3d);
2334
2335 D3D_RELEASE(pDst);
2336 D3D_RELEASE(pSrc);
2337
2338 AssertMsgReturn(hr == D3D_OK, ("StretchRect failed with %x\n", hr), VERR_INTERNAL_ERROR);
2339
2340 /* Track the StretchRect operation. */
2341 vmsvga3dSurfaceTrackUsage(pState, pContext, pSrcSurface);
2342 vmsvga3dSurfaceTrackUsage(pState, pContext, pDstSurface);
2343
2344 return VINF_SUCCESS;
2345}
2346
2347
2348/**
2349 * Backend worker for implementing SVGA_3D_CMD_SURFACE_DMA that copies one box.
2350 *
2351 * @returns Failure status code or @a rc.
2352 * @param pThis The shared VGA/VMSVGA instance data.
2353 * @param pThisCC The VGA/VMSVGA state for ring-3.
2354 * @param pState The VMSVGA3d state.
2355 * @param pSurface The host surface.
2356 * @param pMipLevel Mipmap level. The caller knows it already.
2357 * @param uHostFace The host face (valid).
2358 * @param uHostMipmap The host mipmap level (valid).
2359 * @param GuestPtr The guest pointer.
2360 * @param cbGuestPitch The guest pitch.
2361 * @param transfer The transfer direction.
2362 * @param pBox The box to copy (clipped, valid, except for guest's srcx, srcy, srcz).
2363 * @param pContext The context (for OpenGL).
2364 * @param rc The current rc for all boxes.
2365 * @param iBox The current box number (for Direct 3D).
2366 */
2367static DECLCALLBACK(int) vmsvga3dBackSurfaceDMACopyBox(PVGASTATE pThis, PVGASTATECC pThisCC, PVMSVGA3DSTATE pState, PVMSVGA3DSURFACE pSurface,
2368 PVMSVGA3DMIPMAPLEVEL pMipLevel, uint32_t uHostFace, uint32_t uHostMipmap,
2369 SVGAGuestPtr GuestPtr, uint32_t cbGuestPitch, SVGA3dTransferType transfer,
2370 SVGA3dCopyBox const *pBox, PVMSVGA3DCONTEXT pContext, int rc, int iBox)
2371{
2372 HRESULT hr = D3D_OK;
2373 const DWORD dwFlags = transfer == SVGA3D_READ_HOST_VRAM ? D3DLOCK_READONLY : 0;
2374
2375 AssertReturn(pSurface->enmD3DResType != VMSVGA3D_D3DRESTYPE_VOLUME_TEXTURE, VERR_NOT_IMPLEMENTED);
2376
2377 const bool fTexture = pSurface->enmD3DResType == VMSVGA3D_D3DRESTYPE_TEXTURE
2378 || pSurface->enmD3DResType == VMSVGA3D_D3DRESTYPE_CUBE_TEXTURE;
2379 if ( pSurface->enmD3DResType == VMSVGA3D_D3DRESTYPE_SURFACE
2380 || fTexture)
2381 {
2382 rc = vmsvga3dContextFromCid(pState, pSurface->idAssociatedContext, &pContext);
2383 AssertRCReturn(rc, rc);
2384 AssertReturn(pContext->pDevice, VERR_INTERNAL_ERROR);
2385
2386 /* Get the surface involved in the transfer. */
2387 IDirect3DSurface9 *pSurf;
2388 rc = vmsvga3dGetD3DSurface(pState, pContext, pSurface, uHostFace, uHostMipmap, true, &pSurf);
2389 AssertRCReturn(rc, rc);
2390
2391 if (transfer == SVGA3D_READ_HOST_VRAM)
2392 {
2393 /* Texture data is copied to the host VRAM.
2394 * Update the 'bounce' texture if necessary.
2395 */
2396 if ( fTexture
2397 && pSurface->bounce.pTexture
2398 && iBox == 0 /* only the first time */)
2399 {
2400 /** @todo inefficient for VRAM buffers!! */
2401 if (RT_BOOL(pSurface->f.surfaceFlags & SVGA3D_SURFACE_HINT_RENDERTARGET))
2402 {
2403 /* Copy the texture mipmap level to the bounce texture. */
2404 hr = D3D9GetRenderTargetData(pContext, pSurface, uHostFace, uHostMipmap);
2405 AssertMsgReturn(hr == D3D_OK, ("D3D9GetRenderTargetData failed with %x\n", hr), VERR_INTERNAL_ERROR);
2406 }
2407 }
2408 }
2409
2410 uint32_t const u32GuestBlockX = pBox->srcx / pSurface->cxBlock;
2411 uint32_t const u32GuestBlockY = pBox->srcy / pSurface->cyBlock;
2412 Assert(u32GuestBlockX * pSurface->cxBlock == pBox->srcx);
2413 Assert(u32GuestBlockY * pSurface->cyBlock == pBox->srcy);
2414 uint32_t const cBlocksX = (pBox->w + pSurface->cxBlock - 1) / pSurface->cxBlock;
2415 uint32_t const cBlocksY = (pBox->h + pSurface->cyBlock - 1) / pSurface->cyBlock;
2416 AssertMsgReturn(cBlocksX && cBlocksY, ("Empty box %dx%d\n", pBox->w, pBox->h), VERR_INTERNAL_ERROR);
2417
2418 /* vmsvgaR3GmrTransfer verifies uGuestOffset.
2419 * srcx(u32GuestBlockX) and srcy(u32GuestBlockY) have been verified in vmsvga3dSurfaceDMA
2420 * to not cause 32 bit overflow when multiplied by cbBlock and cbGuestPitch.
2421 */
2422 uint64_t const uGuestOffset = u32GuestBlockX * pSurface->cbBlock + u32GuestBlockY * cbGuestPitch;
2423 AssertReturn(uGuestOffset < UINT32_MAX, VERR_INVALID_PARAMETER);
2424
2425 RECT Rect;
2426 Rect.left = pBox->x;
2427 Rect.top = pBox->y;
2428 Rect.right = pBox->x + pBox->w; /* exclusive */
2429 Rect.bottom = pBox->y + pBox->h; /* exclusive */
2430
2431 D3DLOCKED_RECT LockedRect;
2432 hr = pSurf->LockRect(&LockedRect, &Rect, dwFlags);
2433 AssertMsgReturn(hr == D3D_OK, ("LockRect failed with %x\n", hr), VERR_INTERNAL_ERROR);
2434
2435 LogFunc(("Lock sid=%u %s(bounce=%d) memory for rectangle (%d,%d)(%d,%d)\n",
2436 pSurface->id, fTexture ? "TEXTURE " : "", RT_BOOL(pSurface->bounce.pTexture),
2437 Rect.left, Rect.top, Rect.right, Rect.bottom));
2438
2439 /* Prepare parameters for vmsvgaR3GmrTransfer, which needs the host buffer address, size
2440 * and offset of the first scanline.
2441 */
2442 uint32_t const cbLockedBuf = RT_ABS(LockedRect.Pitch) * cBlocksY;
2443 uint8_t *pu8LockedBuf = (uint8_t *)LockedRect.pBits;
2444 if (LockedRect.Pitch < 0)
2445 pu8LockedBuf -= cbLockedBuf + LockedRect.Pitch;
2446 uint32_t const offLockedBuf = (uint32_t)((uintptr_t)LockedRect.pBits - (uintptr_t)pu8LockedBuf);
2447
2448 rc = vmsvgaR3GmrTransfer(pThis,
2449 pThisCC,
2450 transfer,
2451 pu8LockedBuf,
2452 cbLockedBuf,
2453 offLockedBuf,
2454 LockedRect.Pitch,
2455 GuestPtr,
2456 (uint32_t)uGuestOffset,
2457 cbGuestPitch,
2458 cBlocksX * pSurface->cbBlock,
2459 cBlocksY);
2460 AssertRC(rc);
2461
2462 Log4(("first line:\n%.*Rhxd\n", cBlocksX * pSurface->cbBlock, LockedRect.pBits));
2463
2464 hr = pSurf->UnlockRect();
2465
2466 D3D_RELEASE(pSurf);
2467
2468 AssertMsgReturn(hr == D3D_OK, ("UnlockRect failed with %x\n", hr), VERR_INTERNAL_ERROR);
2469
2470 if (transfer == SVGA3D_WRITE_HOST_VRAM)
2471 {
2472 /* Data is copied to the texture. Copy updated 'bounce' texture to the actual if necessary.
2473 */
2474 /// @todo for the last iBox only.
2475 if ( fTexture
2476 && pSurface->bounce.pTexture)
2477 {
2478 LogFunc(("Sync texture from bounce buffer\n"));
2479
2480 /* Copy the new contents to the actual texture object. */
2481 hr = D3D9UpdateTexture(pContext, pSurface);
2482 AssertMsgReturn(hr == D3D_OK, ("UpdateTexture failed with %x\n", hr), VERR_INTERNAL_ERROR);
2483
2484 /* Track the copy operation. */
2485 vmsvga3dSurfaceTrackUsage(pState, pContext, pSurface);
2486 }
2487 }
2488 }
2489 else if ( pSurface->enmD3DResType == VMSVGA3D_D3DRESTYPE_VERTEX_BUFFER
2490 || pSurface->enmD3DResType == VMSVGA3D_D3DRESTYPE_INDEX_BUFFER)
2491 {
2492 /*
2493 * Mesa SVGA driver can use the same buffer either for vertex or index data.
2494 * But D3D distinguishes between index and vertex buffer objects.
2495 * Therefore it should be possible to switch the buffer type on the fly.
2496 *
2497 * Always save the data to the memory buffer in pSurface->paMipmapLevels and,
2498 * if necessary, recreate the corresponding D3D object with the data.
2499 */
2500
2501 /* Buffers are uncompressed. */
2502 AssertReturn(pSurface->cxBlock == 1 && pSurface->cyBlock == 1, VERR_INTERNAL_ERROR);
2503
2504 /* Caller already clipped pBox and buffers are 1-dimensional. */
2505 Assert(pBox->y == 0 && pBox->h == 1 && pBox->z == 0 && pBox->d == 1);
2506
2507 /* The caller has already updated pMipLevel->pSurfaceData, see VMSVGA3DSURFACE_NEEDS_DATA. */
2508
2509#ifdef LOG_ENABLED
2510 uint32_t const offHst = pBox->x * pSurface->cbBlock;
2511 uint32_t const cbWidth = pBox->w * pSurface->cbBlock;
2512 Log4(("Buffer updated at [0x%x;0x%x):\n%.*Rhxd\n", offHst, offHst + cbWidth, cbWidth, (uint8_t *)pMipLevel->pSurfaceData + offHst));
2513#endif
2514
2515 /* Do not bother to copy the data to the D3D resource now. vmsvga3dDrawPrimitives will do that.
2516 * The SVGA driver may use the same surface for both index and vertex data.
2517 */
2518
2519 /* Make sure that vmsvga3dDrawPrimitives fetches the new data. */
2520 pMipLevel->fDirty = true;
2521 pSurface->fDirty = true;
2522 }
2523 else
2524 {
2525 AssertMsgFailed(("Unsupported surface flags 0x%08X, type %d\n", pSurface->f.s.surface1Flags, pSurface->enmD3DResType));
2526 }
2527
2528 return rc;
2529}
2530
2531static DECLCALLBACK(int) vmsvga3dBackGenerateMipmaps(PVGASTATECC pThisCC, uint32_t sid, SVGA3dTextureFilter filter)
2532{
2533 PVMSVGA3DSTATE pState = pThisCC->svga.p3dState;
2534 HRESULT hr;
2535
2536 PVMSVGA3DSURFACE pSurface;
2537 int rc = vmsvga3dSurfaceFromSid(pState, sid, &pSurface);
2538 AssertRCReturn(rc, rc);
2539 AssertReturn(pSurface->idAssociatedContext != SVGA3D_INVALID_ID, VERR_INTERNAL_ERROR);
2540
2541 Assert(filter != SVGA3D_TEX_FILTER_FLATCUBIC);
2542 Assert(filter != SVGA3D_TEX_FILTER_GAUSSIANCUBIC);
2543 pSurface->autogenFilter = filter;
2544
2545 Log(("vmsvga3dGenerateMipmaps: sid=%u filter=%d\n", sid, filter));
2546
2547 if (!pSurface->u.pSurface)
2548 {
2549 /** @todo stricter checks for associated context */
2550 uint32_t const cid = pSurface->idAssociatedContext;
2551
2552 PVMSVGA3DCONTEXT pContext;
2553 rc = vmsvga3dContextFromCid(pState, cid, &pContext);
2554 AssertRCReturn(rc, rc);
2555 AssertReturn(pContext->pDevice, VERR_INTERNAL_ERROR);
2556
2557 /* Unknown surface type; turn it into a texture. */
2558 LogFunc(("unknown src surface sid=%u type=%d format=%d -> create texture\n", sid, pSurface->f.s.surface1Flags, pSurface->format));
2559 rc = vmsvga3dBackCreateTexture(pThisCC, pContext, cid, pSurface);
2560 AssertRCReturn(rc, rc);
2561 }
2562
2563 AssertReturn( pSurface->enmD3DResType == VMSVGA3D_D3DRESTYPE_TEXTURE
2564 || pSurface->enmD3DResType == VMSVGA3D_D3DRESTYPE_CUBE_TEXTURE
2565 || pSurface->enmD3DResType == VMSVGA3D_D3DRESTYPE_VOLUME_TEXTURE,
2566 VERR_INVALID_PARAMETER);
2567
2568 hr = pSurface->u.pTexture->SetAutoGenFilterType((D3DTEXTUREFILTERTYPE)filter);
2569 AssertMsg(hr == D3D_OK, ("SetAutoGenFilterType failed with %x\n", hr));
2570
2571 /* Generate the mip maps. */
2572 pSurface->u.pTexture->GenerateMipSubLevels();
2573
2574 return VINF_SUCCESS;
2575}
2576
2577
2578/**
2579 * Create a new 3d context
2580 *
2581 * @returns VBox status code.
2582 * @param pThisCC The VGA/VMSVGA state for ring-3.
2583 * @param cid Context id
2584 */
2585static DECLCALLBACK(int) vmsvga3dBackContextDefine(PVGASTATECC pThisCC, uint32_t cid)
2586{
2587 int rc;
2588 PVMSVGA3DCONTEXT pContext;
2589 HRESULT hr;
2590 PVMSVGA3DSTATE pState = pThisCC->svga.p3dState;
2591
2592 Log(("vmsvga3dContextDefine id %x\n", cid));
2593
2594 AssertReturn(pState, VERR_NO_MEMORY);
2595 AssertReturn(cid < SVGA3D_MAX_CONTEXT_IDS, VERR_INVALID_PARAMETER);
2596
2597 if (cid >= pState->cContexts)
2598 {
2599 /* Grow the array. */
2600 uint32_t cNew = RT_ALIGN(cid + 15, 16);
2601 void *pvNew = RTMemRealloc(pState->papContexts, sizeof(pState->papContexts[0]) * cNew);
2602 AssertReturn(pvNew, VERR_NO_MEMORY);
2603 pState->papContexts = (PVMSVGA3DCONTEXT *)pvNew;
2604 while (pState->cContexts < cNew)
2605 {
2606 pContext = (PVMSVGA3DCONTEXT)RTMemAllocZ(sizeof(*pContext));
2607 AssertReturn(pContext, VERR_NO_MEMORY);
2608 pContext->id = SVGA3D_INVALID_ID;
2609 pState->papContexts[pState->cContexts++] = pContext;
2610 }
2611 }
2612 /* If one already exists with this id, then destroy it now. */
2613 if (pState->papContexts[cid]->id != SVGA3D_INVALID_ID)
2614 vmsvga3dBackContextDestroy(pThisCC, cid);
2615
2616 pContext = pState->papContexts[cid];
2617 memset(pContext, 0, sizeof(*pContext));
2618 pContext->id = cid;
2619 for (uint32_t i = 0; i< RT_ELEMENTS(pContext->aSidActiveTextures); i++)
2620 pContext->aSidActiveTextures[i] = SVGA3D_INVALID_ID;
2621 pContext->state.shidVertex = SVGA3D_INVALID_ID;
2622 pContext->state.shidPixel = SVGA3D_INVALID_ID;
2623
2624 for (uint32_t i = 0; i < RT_ELEMENTS(pContext->state.aRenderTargets); i++)
2625 pContext->state.aRenderTargets[i] = SVGA3D_INVALID_ID;
2626
2627 /* Create a context window with minimal 4x4 size. We will never use the swapchain
2628 * to present the rendered image. Rendered images from the guest will be copied to
2629 * the VMSVGA SCREEN object, which can be either an offscreen render target or
2630 * system memory in the guest VRAM.
2631 */
2632 rc = vmsvga3dContextWindowCreate(pState->hInstance, pState->pWindowThread, pState->WndRequestSem, &pContext->hwnd);
2633 AssertRCReturn(rc, rc);
2634
2635 /* Changed when the function returns. */
2636 D3DPRESENT_PARAMETERS PresParam;
2637 PresParam.BackBufferWidth = 0;
2638 PresParam.BackBufferHeight = 0;
2639 PresParam.BackBufferFormat = D3DFMT_UNKNOWN;
2640 PresParam.BackBufferCount = 0;
2641
2642 PresParam.MultiSampleType = D3DMULTISAMPLE_NONE;
2643 PresParam.MultiSampleQuality = 0;
2644 PresParam.SwapEffect = D3DSWAPEFFECT_DISCARD;
2645 PresParam.hDeviceWindow = pContext->hwnd;
2646 PresParam.Windowed = TRUE;
2647 PresParam.EnableAutoDepthStencil = FALSE;
2648 PresParam.AutoDepthStencilFormat = D3DFMT_UNKNOWN; /* not relevant */
2649 PresParam.Flags = 0;
2650 PresParam.FullScreen_RefreshRateInHz = 0; /* windowed -> 0 */
2651 /** @todo consider using D3DPRESENT_DONOTWAIT so we don't wait for the GPU during Present calls. */
2652 PresParam.PresentationInterval = D3DPRESENT_INTERVAL_IMMEDIATE;
2653
2654#if 0
2655 VMSVGA3DCREATEDEVICEPARAMS Params = { pState, pContext, &PresParam, 0 };
2656 rc = vmsvga3dSendThreadMessage(pState->pWindowThread, pState->WndRequestSem, WM_VMSVGA3D_CREATE_DEVICE, 0, (LPARAM)&Params);
2657 AssertRCReturn(rc, rc);
2658 hr = Params.hrc;
2659
2660#else
2661 /** @todo Docs indicates that we should be using
2662 * D3DCREATE_HARDWARE_VERTEXPROCESSING with W10 1607 and higher.
2663 * https://docs.microsoft.com/en-us/windows/win32/direct3d9/d3dcreate */
2664 hr = pState->pD3D9->CreateDeviceEx(D3DADAPTER_DEFAULT,
2665 D3DDEVTYPE_HAL,
2666 pContext->hwnd,
2667 D3DCREATE_MULTITHREADED | D3DCREATE_MIXED_VERTEXPROCESSING, //D3DCREATE_HARDWARE_VERTEXPROCESSING,
2668 &PresParam,
2669 NULL,
2670 &pContext->pDevice);
2671#endif
2672 AssertMsgReturn(hr == D3D_OK, ("vmsvga3dContextDefine: CreateDevice failed with %x\n", hr), VERR_INTERNAL_ERROR);
2673
2674 Log(("vmsvga3dContextDefine: Backbuffer (%d,%d) count=%d format=%x\n", PresParam.BackBufferWidth, PresParam.BackBufferHeight, PresParam.BackBufferCount, PresParam.BackBufferFormat));
2675 return VINF_SUCCESS;
2676}
2677
2678/**
2679 * Destroy an existing 3d context
2680 *
2681 * @returns VBox status code.
2682 * @param pThisCC The VGA/VMSVGA state for ring-3.
2683 * @param cid Context id
2684 */
2685static DECLCALLBACK(int) vmsvga3dBackContextDestroy(PVGASTATECC pThisCC, uint32_t cid)
2686{
2687 PVMSVGA3DSTATE pState = pThisCC->svga.p3dState;
2688 AssertReturn(pState, VERR_NO_MEMORY);
2689
2690 AssertReturn(cid < SVGA3D_MAX_CONTEXT_IDS, VERR_INVALID_PARAMETER);
2691
2692 if ( cid < pState->cContexts
2693 && pState->papContexts[cid]->id == cid)
2694 {
2695 PVMSVGA3DCONTEXT pContext = pState->papContexts[cid];
2696
2697 Log(("vmsvga3dContextDestroy id %x\n", cid));
2698
2699 /* Cleanup the device runtime state. */
2700 if (pContext->pDevice)
2701 pContext->pDevice->SetVertexDeclaration(NULL);
2702 D3D_RELEASE(pContext->d3dState.pVertexDecl);
2703
2704 /* Check for all surfaces that are associated with this context to remove all dependencies */
2705 for (uint32_t sid = 0; sid < pState->cSurfaces; sid++)
2706 {
2707 PVMSVGA3DSURFACE pSurface = pState->papSurfaces[sid];
2708 if ( pSurface->id == sid
2709 && pSurface->idAssociatedContext == cid)
2710 {
2711 int rc;
2712
2713 LogFunc(("Remove all dependencies for surface sid=%u\n", sid));
2714
2715 SVGA3dSurfaceAllFlags const surfaceFlags = pSurface->f.surfaceFlags;
2716 SVGA3dSurfaceFormat const format = pSurface->format;
2717 uint32_t const multisampleCount = pSurface->multiSampleCount;
2718 SVGA3dTextureFilter const autogenFilter = pSurface->autogenFilter;
2719 uint32_t const cFaces = pSurface->cFaces;
2720 uint32_t const cMipLevels = pSurface->cLevels;
2721
2722 SVGA3dSize *pMipLevelSize = (SVGA3dSize *)RTMemAllocZ(cMipLevels * cFaces * sizeof(SVGA3dSize));
2723 AssertReturn(pMipLevelSize, VERR_NO_MEMORY);
2724
2725 for (uint32_t face = 0; face < pSurface->cFaces; face++)
2726 {
2727 for (uint32_t i = 0; i < cMipLevels; i++)
2728 {
2729 uint32_t idx = i + face * cMipLevels;
2730 memcpy(&pMipLevelSize[idx], &pSurface->paMipmapLevels[idx].mipmapSize, sizeof(SVGA3dSize));
2731 }
2732 }
2733
2734 /* Recreate the surface with the original settings; destroys the contents, but that seems fairly safe since the context is also destroyed. */
2735#ifdef DEBUG_sunlover
2736 /** @todo not safe with shared objects */
2737 Assert(pSurface->pSharedObjectTree == NULL);
2738#endif
2739
2740 rc = vmsvga3dSurfaceDestroy(pThisCC, sid);
2741 AssertRC(rc);
2742
2743 rc = vmsvga3dSurfaceDefine(pThisCC, sid, surfaceFlags, format, multisampleCount, SVGA3D_MS_PATTERN_NONE, SVGA3D_MS_QUALITY_NONE, autogenFilter,
2744 cMipLevels, &pMipLevelSize[0], /* arraySize = */ 0, /* bufferByteStride = */ 0, /* fAllocMipLevels = */ true);
2745 AssertRC(rc);
2746
2747 Assert(!pSurface->u.pSurface);
2748 }
2749 else
2750 {
2751 /* Check for a shared surface object. */
2752 PVMSVGA3DSHAREDSURFACE pSharedSurface = (PVMSVGA3DSHAREDSURFACE)RTAvlU32Get(&pSurface->pSharedObjectTree, cid);
2753 if (pSharedSurface)
2754 {
2755 LogFunc(("Remove shared dependency for surface sid=%u\n", sid));
2756
2757 switch (pSurface->enmD3DResType)
2758 {
2759 case VMSVGA3D_D3DRESTYPE_TEXTURE:
2760 Assert(pSharedSurface->u.pTexture);
2761 D3D_RELEASE(pSharedSurface->u.pTexture);
2762 break;
2763
2764 case VMSVGA3D_D3DRESTYPE_CUBE_TEXTURE:
2765 Assert(pSharedSurface->u.pCubeTexture);
2766 D3D_RELEASE(pSharedSurface->u.pCubeTexture);
2767 break;
2768
2769 case VMSVGA3D_D3DRESTYPE_VOLUME_TEXTURE:
2770 Assert(pSharedSurface->u.pVolumeTexture);
2771 D3D_RELEASE(pSharedSurface->u.pVolumeTexture);
2772 break;
2773
2774 default:
2775 AssertFailed();
2776 break;
2777 }
2778 RTAvlU32Remove(&pSurface->pSharedObjectTree, cid);
2779 RTMemFree(pSharedSurface);
2780 }
2781 }
2782 }
2783
2784 /* Destroy all leftover pixel shaders. */
2785 for (uint32_t i = 0; i < pContext->cPixelShaders; i++)
2786 {
2787 if (pContext->paPixelShader[i].id != SVGA3D_INVALID_ID)
2788 vmsvga3dBackShaderDestroy(pThisCC, pContext->paPixelShader[i].cid, pContext->paPixelShader[i].id, pContext->paPixelShader[i].type);
2789 }
2790 if (pContext->paPixelShader)
2791 RTMemFree(pContext->paPixelShader);
2792
2793 /* Destroy all leftover vertex shaders. */
2794 for (uint32_t i = 0; i < pContext->cVertexShaders; i++)
2795 {
2796 if (pContext->paVertexShader[i].id != SVGA3D_INVALID_ID)
2797 vmsvga3dBackShaderDestroy(pThisCC, pContext->paVertexShader[i].cid, pContext->paVertexShader[i].id, pContext->paVertexShader[i].type);
2798 }
2799 if (pContext->paVertexShader)
2800 RTMemFree(pContext->paVertexShader);
2801
2802 if (pContext->state.paVertexShaderConst)
2803 RTMemFree(pContext->state.paVertexShaderConst);
2804 if (pContext->state.paPixelShaderConst)
2805 RTMemFree(pContext->state.paPixelShaderConst);
2806
2807 vmsvga3dBackOcclusionQueryDelete(pThisCC, pContext);
2808
2809 /* Release the D3D device object */
2810 D3D_RELEASE(pContext->pDevice);
2811
2812 /* Destroy the window we've created. */
2813 int rc = vmsvga3dSendThreadMessage(pState->pWindowThread, pState->WndRequestSem, WM_VMSVGA3D_DESTROYWINDOW, (WPARAM)pContext->hwnd, 0);
2814 AssertRC(rc);
2815
2816 memset(pContext, 0, sizeof(*pContext));
2817 pContext->id = SVGA3D_INVALID_ID;
2818 }
2819 else
2820 AssertFailed();
2821
2822 return VINF_SUCCESS;
2823}
2824
2825static DECLCALLBACK(int) vmsvga3dBackDefineScreen(PVGASTATE pThis, PVGASTATECC pThisCC, VMSVGASCREENOBJECT *pScreen)
2826{
2827 RT_NOREF(pThis, pThisCC, pScreen);
2828 return VERR_NOT_IMPLEMENTED;
2829}
2830
2831static DECLCALLBACK(int) vmsvga3dBackDestroyScreen(PVGASTATECC pThisCC, VMSVGASCREENOBJECT *pScreen)
2832{
2833 RT_NOREF(pThisCC, pScreen);
2834 return VERR_NOT_IMPLEMENTED;
2835}
2836
2837static DECLCALLBACK(int) vmsvga3dBackSurfaceBlitToScreen(PVGASTATECC pThisCC, VMSVGASCREENOBJECT *pScreen,
2838 SVGASignedRect destRect, SVGA3dSurfaceImageId srcImage,
2839 SVGASignedRect srcRect, uint32_t cRects, SVGASignedRect *paRects)
2840{
2841 RT_NOREF(pThisCC, pScreen, destRect, srcImage, srcRect, cRects, paRects);
2842 return VERR_NOT_IMPLEMENTED;
2843}
2844
2845static int vmsvga3dContextTrackUsage(PVGASTATECC pThisCC, PVMSVGA3DCONTEXT pContext)
2846{
2847 PVMSVGA3DSTATE pState = pThisCC->svga.p3dState;
2848 AssertReturn(pState, VERR_NO_MEMORY);
2849
2850 /* Inject fences to make sure we can track surface usage in case the client wants to reuse it in another context. */
2851 for (uint32_t i = 0; i < RT_ELEMENTS(pContext->aSidActiveTextures); ++i)
2852 {
2853 if (pContext->aSidActiveTextures[i] != SVGA3D_INVALID_ID)
2854 vmsvga3dSurfaceTrackUsageById(pState, pContext, pContext->aSidActiveTextures[i]);
2855 }
2856 for (uint32_t i = 0; i < RT_ELEMENTS(pContext->state.aRenderTargets); ++i)
2857 if (pContext->state.aRenderTargets[i] != SVGA3D_INVALID_ID)
2858 vmsvga3dSurfaceTrackUsageById(pState, pContext, pContext->state.aRenderTargets[i]);
2859
2860 return VINF_SUCCESS;
2861}
2862
2863/* Handle resize */
2864static DECLCALLBACK(int) vmsvga3dBackChangeMode(PVGASTATECC pThisCC)
2865{
2866 PVMSVGA3DSTATE pState = pThisCC->svga.p3dState;
2867 AssertReturn(pState, VERR_NO_MEMORY);
2868
2869 /* Resize all active contexts. */
2870 for (uint32_t i = 0; i < pState->cContexts; i++)
2871 {
2872 PVMSVGA3DCONTEXT pContext = pState->papContexts[i];
2873 uint32_t cid = pContext->id;
2874
2875 if (cid != SVGA3D_INVALID_ID)
2876 {
2877 D3DPRESENT_PARAMETERS PresParam;
2878 D3DVIEWPORT9 viewportOrg;
2879 HRESULT hr;
2880
2881#ifdef VMSVGA3D_DIRECT3D9_RESET
2882 /* Sync back all surface data as everything is lost after the Reset. */
2883 for (uint32_t sid = 0; sid < pState->cSurfaces; sid++)
2884 {
2885 PVMSVGA3DSURFACE pSurface = pState->papSurfaces[sid];
2886 if ( pSurface->id == sid
2887 && pSurface->idAssociatedContext == cid
2888 && pSurface->u.pSurface)
2889 {
2890 Log(("vmsvga3dChangeMode: sync back data of surface sid=%u (fDirty=%d)\n", sid, pSurface->fDirty));
2891
2892 /* Reallocate our surface memory buffers. */
2893 for (uint32_t i = 0; i < pSurface->cMipLevels; i++)
2894 {
2895 PVMSVGA3DMIPMAPLEVEL pMipmapLevel = &pSurface->paMipmapLevels[i];
2896
2897 pMipmapLevel->pSurfaceData = RTMemAllocZ(pMipmapLevel->cbSurface);
2898 AssertReturn(pMipmapLevel->pSurfaceData, VERR_NO_MEMORY);
2899
2900 if (!pSurface->fDirty)
2901 {
2902 D3DLOCKED_RECT LockedRect;
2903
2904 if (pSurface->bounce.pTexture)
2905 {
2906 IDirect3DSurface9 *pSrc, *pDest;
2907
2908 /** @todo only sync when something was actually rendered (since the last sync) */
2909 Log(("vmsvga3dChangeMode: sync bounce buffer (level %d)\n", i));
2910 hr = pSurface->bounce.pTexture->GetSurfaceLevel(i, &pDest);
2911 AssertMsgReturn(hr == D3D_OK, ("vmsvga3dChangeMode: GetSurfaceLevel failed with %x\n", hr), VERR_INTERNAL_ERROR);
2912
2913 hr = pSurface->u.pTexture->GetSurfaceLevel(i, &pSrc);
2914 AssertMsgReturn(hr == D3D_OK, ("vmsvga3dChangeMode: GetSurfaceLevel failed with %x\n", hr), VERR_INTERNAL_ERROR);
2915
2916 hr = pContext->pDevice->GetRenderTargetData(pSrc, pDest);
2917 AssertMsgReturn(hr == D3D_OK, ("vmsvga3dChangeMode: GetRenderTargetData failed with %x\n", hr), VERR_INTERNAL_ERROR);
2918
2919 D3D_RELEASE(pSrc);
2920 D3D_RELEASE(pDest);
2921
2922 hr = pSurface->bounce.pTexture->LockRect(i,
2923 &LockedRect,
2924 NULL,
2925 D3DLOCK_READONLY);
2926 }
2927 else
2928 hr = pSurface->u.pTexture->LockRect(i,
2929 &LockedRect,
2930 NULL,
2931 D3DLOCK_READONLY);
2932 AssertMsgReturn(hr == D3D_OK, ("vmsvga3dChangeMode: LockRect failed with %x\n", hr), VERR_INTERNAL_ERROR);
2933
2934 /* Copy the data one line at a time in case the internal pitch is different. */
2935 for (uint32_t j = 0; j < pMipmapLevel->size.height; j++)
2936 {
2937 memcpy((uint8_t *)pMipmapLevel->pSurfaceData + j * pMipmapLevel->cbSurfacePitch, (uint8_t *)LockedRect.pBits + j * LockedRect.Pitch, pMipmapLevel->cbSurfacePitch);
2938 }
2939
2940 if (pSurface->bounce.pTexture)
2941 hr = pSurface->bounce.pTexture->UnlockRect(i);
2942 else
2943 hr = pSurface->u.pTexture->UnlockRect(i);
2944 AssertMsgReturn(hr == D3D_OK, ("vmsvga3dChangeMode: UnlockRect failed with %x\n", hr), VERR_INTERNAL_ERROR);
2945 }
2946 }
2947
2948
2949 switch (pSurface->flags & VMSVGA3D_SURFACE_HINT_SWITCH_MASK)
2950 {
2951 case SVGA3D_SURFACE_CUBEMAP:
2952 case SVGA3D_SURFACE_CUBEMAP | SVGA3D_SURFACE_HINT_TEXTURE:
2953 case SVGA3D_SURFACE_CUBEMAP | SVGA3D_SURFACE_HINT_TEXTURE | SVGA3D_SURFACE_HINT_RENDERTARGET:
2954 D3D_RELEASE(pSurface->u.pCubeTexture);
2955 D3D_RELEASE(pSurface->bounce.pCubeTexture);
2956 break;
2957
2958 case SVGA3D_SURFACE_HINT_INDEXBUFFER | SVGA3D_SURFACE_HINT_VERTEXBUFFER:
2959 case SVGA3D_SURFACE_HINT_INDEXBUFFER:
2960 case SVGA3D_SURFACE_HINT_VERTEXBUFFER:
2961 if (pSurface->fu32ActualUsageFlags == SVGA3D_SURFACE_HINT_VERTEXBUFFER)
2962 D3D_RELEASE(pSurface->u.pVertexBuffer);
2963 else if (pSurface->fu32ActualUsageFlags == SVGA3D_SURFACE_HINT_INDEXBUFFER)
2964 D3D_RELEASE(pSurface->u.pIndexBuffer);
2965 else
2966 AssertMsg(pSurface->u.pVertexBuffer == NULL, ("fu32ActualUsageFlags %x\n", pSurface->fu32ActualUsageFlags));
2967 break;
2968
2969 case SVGA3D_SURFACE_HINT_TEXTURE:
2970 case SVGA3D_SURFACE_HINT_TEXTURE | SVGA3D_SURFACE_HINT_RENDERTARGET:
2971 D3D_RELEASE(pSurface->u.pTexture);
2972 D3D_RELEASE(pSurface->bounce.pTexture);
2973 break;
2974
2975 case SVGA3D_SURFACE_HINT_RENDERTARGET:
2976 case SVGA3D_SURFACE_HINT_DEPTHSTENCIL:
2977 if (pSurface->fStencilAsTexture)
2978 D3D_RELEASE(pSurface->u.pTexture);
2979 else
2980 D3D_RELEASE(pSurface->u.pSurface);
2981 break;
2982
2983 default:
2984 AssertFailed();
2985 break;
2986 }
2987 RTAvlU32Destroy(&pSurface->pSharedObjectTree, vmsvga3dSharedSurfaceDestroyTree, pSurface);
2988 Assert(pSurface->pSharedObjectTree == NULL);
2989
2990 pSurface->idAssociatedContext = SVGA3D_INVALID_ID;
2991 pSurface->hSharedObject = 0;
2992 }
2993 }
2994#endif /* #ifdef VMSVGA3D_DIRECT3D9_RESET */
2995
2996 AssertReturn(pContext->pDevice, VERR_INTERNAL_ERROR);
2997
2998 /* Cleanup the device runtime state. */
2999 pContext->pDevice->SetVertexDeclaration(NULL);
3000 D3D_RELEASE(pContext->d3dState.pVertexDecl);
3001
3002 hr = pContext->pDevice->GetViewport(&viewportOrg);
3003 AssertMsgReturn(hr == D3D_OK, ("vmsvga3dChangeMode: GetViewport failed with %x\n", hr), VERR_INTERNAL_ERROR);
3004
3005 Log(("vmsvga3dChangeMode: old viewport settings (%d,%d)(%d,%d) z=%d/%d\n", viewportOrg.X, viewportOrg.Y, viewportOrg.Width, viewportOrg.Height, (uint32_t)(viewportOrg.MinZ * 100.0), (uint32_t)(viewportOrg.MaxZ * 100.0)));
3006
3007 /* Changed when the function returns. */
3008 PresParam.BackBufferWidth = 0;
3009 PresParam.BackBufferHeight = 0;
3010 PresParam.BackBufferFormat = D3DFMT_UNKNOWN;
3011 PresParam.BackBufferCount = 0;
3012
3013 PresParam.MultiSampleType = D3DMULTISAMPLE_NONE;
3014 PresParam.MultiSampleQuality = 0;
3015 PresParam.SwapEffect = D3DSWAPEFFECT_DISCARD;
3016 PresParam.hDeviceWindow = pContext->hwnd;
3017 PresParam.Windowed = TRUE;
3018 PresParam.EnableAutoDepthStencil = FALSE;
3019 PresParam.AutoDepthStencilFormat = D3DFMT_UNKNOWN; /* not relevant */
3020 PresParam.Flags = 0;
3021 PresParam.FullScreen_RefreshRateInHz = 0; /* windowed -> 0 */
3022 /** @todo consider using D3DPRESENT_DONOTWAIT so we don't wait for the GPU during Present calls. */
3023 PresParam.PresentationInterval = D3DPRESENT_INTERVAL_IMMEDIATE;;
3024
3025 /* ResetEx does not trash the device state */
3026 hr = pContext->pDevice->ResetEx(&PresParam, NULL);
3027 AssertMsgReturn(hr == D3D_OK, ("vmsvga3dChangeMode: Reset failed with %x\n", hr), VERR_INTERNAL_ERROR);
3028
3029 Log(("vmsvga3dChangeMode: Backbuffer (%d,%d) count=%d format=%x\n", PresParam.BackBufferWidth, PresParam.BackBufferHeight, PresParam.BackBufferCount, PresParam.BackBufferFormat));
3030
3031 /* ResetEx changes the viewport; restore it again. */
3032 hr = pContext->pDevice->SetViewport(&viewportOrg);
3033 AssertMsgReturn(hr == D3D_OK, ("vmsvga3dChangeMode: SetViewport failed with %x\n", hr), VERR_INTERNAL_ERROR);
3034
3035#ifdef LOG_ENABLED
3036 {
3037 D3DVIEWPORT9 viewport;
3038 hr = pContext->pDevice->GetViewport(&viewport);
3039 AssertMsgReturn(hr == D3D_OK, ("vmsvga3dChangeMode: GetViewport failed with %x\n", hr), VERR_INTERNAL_ERROR);
3040
3041 Log(("vmsvga3dChangeMode: changed viewport settings (%d,%d)(%d,%d) z=%d/%d\n", viewport.X, viewport.Y, viewport.Width, viewport.Height, (uint32_t)(viewport.MinZ * 100.0), (uint32_t)(viewport.MaxZ * 100.0)));
3042 }
3043#endif
3044
3045 /* First set the render targets as they change the internal state (reset viewport etc) */
3046 Log(("vmsvga3dChangeMode: Recreate render targets BEGIN\n"));
3047 for (uint32_t j = 0; j < RT_ELEMENTS(pContext->state.aRenderTargets); j++)
3048 {
3049 if (pContext->state.aRenderTargets[j] != SVGA3D_INVALID_ID)
3050 {
3051 SVGA3dSurfaceImageId target;
3052
3053 target.sid = pContext->state.aRenderTargets[j];
3054 target.face = 0;
3055 target.mipmap = 0;
3056 int rc = vmsvga3dBackSetRenderTarget(pThisCC, cid, (SVGA3dRenderTargetType)j, target);
3057 AssertRCReturn(rc, rc);
3058 }
3059 }
3060
3061#ifdef VMSVGA3D_DIRECT3D9_RESET
3062 /* Recreate the render state */
3063 Log(("vmsvga3dChangeMode: Recreate render state BEGIN\n"));
3064 for (uint32_t i = 0; i < RT_ELEMENTS(pContext->state.aRenderState); i++)
3065 {
3066 SVGA3dRenderState *pRenderState = &pContext->state.aRenderState[i];
3067
3068 if (pRenderState->state != SVGA3D_RS_INVALID)
3069 vmsvga3dBackSetRenderState(pThisCC, pContext->id, 1, pRenderState);
3070 }
3071 Log(("vmsvga3dChangeMode: Recreate render state END\n"));
3072
3073 /* Recreate the texture state */
3074 Log(("vmsvga3dChangeMode: Recreate texture state BEGIN\n"));
3075 for (uint32_t iStage = 0; iStage < RT_ELEMENTS(pContext->state.aTextureStates); iStage++)
3076 {
3077 for (uint32_t j = 0; j < RT_ELEMENTS(pContext->state.aTextureStates[0]); j++)
3078 {
3079 SVGA3dTextureState *pTextureState = &pContext->state.aTextureStates[iStage][j];
3080
3081 if (pTextureState->name != SVGA3D_RS_INVALID)
3082 vmsvga3dBackSetTextureState(pThisCC, pContext->id, 1, pTextureState);
3083 }
3084 }
3085 Log(("vmsvga3dChangeMode: Recreate texture state END\n"));
3086
3087 if (pContext->state.u32UpdateFlags & VMSVGA3D_UPDATE_SCISSORRECT)
3088 vmsvga3dBackSetScissorRect(pThis, cid, &pContext->state.RectScissor);
3089 if (pContext->state.u32UpdateFlags & VMSVGA3D_UPDATE_ZRANGE)
3090 vmsvga3dBackSetZRange(pThisCC, cid, pContext->state.zRange);
3091 if (pContext->state.u32UpdateFlags & VMSVGA3D_UPDATE_VIEWPORT)
3092 vmsvga3dBackSetViewPort(pThis, cid, &pContext->state.RectViewPort);
3093 if (pContext->state.u32UpdateFlags & VMSVGA3D_UPDATE_VERTEXSHADER)
3094 vmsvga3dBackShaderSet(pThis, pContext, cid, SVGA3D_SHADERTYPE_VS, pContext->state.shidVertex);
3095 if (pContext->state.u32UpdateFlags & VMSVGA3D_UPDATE_PIXELSHADER)
3096 vmsvga3dBackShaderSet(pThis, pContext, cid, SVGA3D_SHADERTYPE_PS, pContext->state.shidPixel);
3097 /** @todo restore more state data */
3098#endif /* #ifdef VMSVGA3D_DIRECT3D9_RESET */
3099 }
3100 }
3101 return VINF_SUCCESS;
3102}
3103
3104
3105static DECLCALLBACK(int) vmsvga3dBackSetTransform(PVGASTATECC pThisCC, uint32_t cid, SVGA3dTransformType type, float matrix[16])
3106{
3107 D3DTRANSFORMSTATETYPE d3dState;
3108 HRESULT hr;
3109 PVMSVGA3DSTATE pState = pThisCC->svga.p3dState;
3110 AssertReturn(pState, VERR_NO_MEMORY);
3111
3112 Log(("vmsvga3dSetTransform %x %s\n", cid, vmsvgaTransformToString(type)));
3113
3114 ASSERT_GUEST_RETURN((unsigned)type < SVGA3D_TRANSFORM_MAX, VERR_INVALID_PARAMETER);
3115
3116 PVMSVGA3DCONTEXT pContext;
3117 int rc = vmsvga3dContextFromCid(pState, cid, &pContext);
3118 AssertRCReturn(rc, rc);
3119 AssertReturn(pContext->pDevice, VERR_INTERNAL_ERROR);
3120
3121 switch (type)
3122 {
3123 case SVGA3D_TRANSFORM_VIEW:
3124 d3dState = D3DTS_VIEW;
3125 break;
3126 case SVGA3D_TRANSFORM_PROJECTION:
3127 d3dState = D3DTS_PROJECTION;
3128 break;
3129 case SVGA3D_TRANSFORM_TEXTURE0:
3130 d3dState = D3DTS_TEXTURE0;
3131 break;
3132 case SVGA3D_TRANSFORM_TEXTURE1:
3133 d3dState = D3DTS_TEXTURE1;
3134 break;
3135 case SVGA3D_TRANSFORM_TEXTURE2:
3136 d3dState = D3DTS_TEXTURE2;
3137 break;
3138 case SVGA3D_TRANSFORM_TEXTURE3:
3139 d3dState = D3DTS_TEXTURE3;
3140 break;
3141 case SVGA3D_TRANSFORM_TEXTURE4:
3142 d3dState = D3DTS_TEXTURE4;
3143 break;
3144 case SVGA3D_TRANSFORM_TEXTURE5:
3145 d3dState = D3DTS_TEXTURE5;
3146 break;
3147 case SVGA3D_TRANSFORM_TEXTURE6:
3148 d3dState = D3DTS_TEXTURE6;
3149 break;
3150 case SVGA3D_TRANSFORM_TEXTURE7:
3151 d3dState = D3DTS_TEXTURE7;
3152 break;
3153 case SVGA3D_TRANSFORM_WORLD:
3154 d3dState = D3DTS_WORLD;
3155 break;
3156 case SVGA3D_TRANSFORM_WORLD1:
3157 d3dState = D3DTS_WORLD1;
3158 break;
3159 case SVGA3D_TRANSFORM_WORLD2:
3160 d3dState = D3DTS_WORLD2;
3161 break;
3162 case SVGA3D_TRANSFORM_WORLD3:
3163 d3dState = D3DTS_WORLD3;
3164 break;
3165
3166 default:
3167 Log(("vmsvga3dSetTransform: unknown type!!\n"));
3168 return VERR_INVALID_PARAMETER;
3169 }
3170
3171 /* Save this matrix for vm state save/restore. */
3172 pContext->state.aTransformState[type].fValid = true;
3173 memcpy(pContext->state.aTransformState[type].matrix, matrix, sizeof(pContext->state.aTransformState[type].matrix));
3174 pContext->state.u32UpdateFlags |= VMSVGA3D_UPDATE_TRANSFORM;
3175
3176 Log(("Matrix [%d %d %d %d]\n", (int)(matrix[0] * 10.0), (int)(matrix[1] * 10.0), (int)(matrix[2] * 10.0), (int)(matrix[3] * 10.0)));
3177 Log((" [%d %d %d %d]\n", (int)(matrix[4] * 10.0), (int)(matrix[5] * 10.0), (int)(matrix[6] * 10.0), (int)(matrix[7] * 10.0)));
3178 Log((" [%d %d %d %d]\n", (int)(matrix[8] * 10.0), (int)(matrix[9] * 10.0), (int)(matrix[10] * 10.0), (int)(matrix[11] * 10.0)));
3179 Log((" [%d %d %d %d]\n", (int)(matrix[12] * 10.0), (int)(matrix[13] * 10.0), (int)(matrix[14] * 10.0), (int)(matrix[15] * 10.0)));
3180 hr = pContext->pDevice->SetTransform(d3dState, (const D3DMATRIX *)matrix);
3181 AssertMsgReturn(hr == D3D_OK, ("vmsvga3dSetTransform: SetTransform failed with %x\n", hr), VERR_INTERNAL_ERROR);
3182 return VINF_SUCCESS;
3183}
3184
3185static DECLCALLBACK(int) vmsvga3dBackSetZRange(PVGASTATECC pThisCC, uint32_t cid, SVGA3dZRange zRange)
3186{
3187 D3DVIEWPORT9 viewport;
3188 HRESULT hr;
3189 PVMSVGA3DSTATE pState = pThisCC->svga.p3dState;
3190 AssertReturn(pState, VERR_NO_MEMORY);
3191
3192 Log(("vmsvga3dSetZRange %x min=%d max=%d\n", cid, (uint32_t)(zRange.min * 100.0), (uint32_t)(zRange.max * 100.0)));
3193
3194 PVMSVGA3DCONTEXT pContext;
3195 int rc = vmsvga3dContextFromCid(pState, cid, &pContext);
3196 AssertRCReturn(rc, rc);
3197 AssertReturn(pContext->pDevice, VERR_INTERNAL_ERROR);
3198
3199 pContext->state.zRange = zRange;
3200 pContext->state.u32UpdateFlags |= VMSVGA3D_UPDATE_ZRANGE;
3201
3202 hr = pContext->pDevice->GetViewport(&viewport);
3203 AssertMsgReturn(hr == D3D_OK, ("vmsvga3dSetZRange: GetViewport failed with %x\n", hr), VERR_INTERNAL_ERROR);
3204
3205 Log(("vmsvga3dSetZRange: old viewport settings (%d,%d)(%d,%d) z=%d/%d\n", viewport.X, viewport.Y, viewport.Width, viewport.Height, (uint32_t)(viewport.MinZ * 100.0), (uint32_t)(viewport.MaxZ * 100.0)));
3206 /** @todo convert the depth range from -1-1 to 0-1 although we shouldn't be getting such values in the first place... */
3207 if (zRange.min < 0.0)
3208 zRange.min = 0.0;
3209 if (zRange.max > 1.0)
3210 zRange.max = 1.0;
3211
3212 viewport.MinZ = zRange.min;
3213 viewport.MaxZ = zRange.max;
3214 hr = pContext->pDevice->SetViewport(&viewport);
3215 AssertMsgReturn(hr == D3D_OK, ("vmsvga3dSetZRange: SetViewport failed with %x\n", hr), VERR_INTERNAL_ERROR);
3216 return VINF_SUCCESS;
3217}
3218
3219/**
3220 * Convert SVGA blend op value to its D3D equivalent
3221 */
3222static DWORD vmsvga3dBlendOp2D3D(uint32_t blendOp, DWORD defaultBlendOp)
3223{
3224 switch (blendOp)
3225 {
3226 case SVGA3D_BLENDOP_ZERO:
3227 return D3DBLEND_ZERO;
3228 case SVGA3D_BLENDOP_ONE:
3229 return D3DBLEND_ONE;
3230 case SVGA3D_BLENDOP_SRCCOLOR:
3231 return D3DBLEND_SRCCOLOR;
3232 case SVGA3D_BLENDOP_INVSRCCOLOR:
3233 return D3DBLEND_INVSRCCOLOR;
3234 case SVGA3D_BLENDOP_SRCALPHA:
3235 return D3DBLEND_SRCALPHA;
3236 case SVGA3D_BLENDOP_INVSRCALPHA:
3237 return D3DBLEND_INVSRCALPHA;
3238 case SVGA3D_BLENDOP_DESTALPHA:
3239 return D3DBLEND_DESTALPHA;
3240 case SVGA3D_BLENDOP_INVDESTALPHA:
3241 return D3DBLEND_INVDESTALPHA;
3242 case SVGA3D_BLENDOP_DESTCOLOR:
3243 return D3DBLEND_DESTCOLOR;
3244 case SVGA3D_BLENDOP_INVDESTCOLOR:
3245 return D3DBLEND_INVDESTCOLOR;
3246 case SVGA3D_BLENDOP_SRCALPHASAT:
3247 return D3DBLEND_SRCALPHASAT;
3248 case SVGA3D_BLENDOP_BLENDFACTOR:
3249 return D3DBLEND_BLENDFACTOR;
3250 case SVGA3D_BLENDOP_INVBLENDFACTOR:
3251 return D3DBLEND_INVBLENDFACTOR;
3252 default:
3253 AssertFailed();
3254 return defaultBlendOp;
3255 }
3256}
3257
3258static DECLCALLBACK(int) vmsvga3dBackSetRenderState(PVGASTATECC pThisCC, uint32_t cid, uint32_t cRenderStates, SVGA3dRenderState *pRenderState)
3259{
3260 DWORD val = 0; /* Shut up MSC */
3261 HRESULT hr;
3262 PVMSVGA3DSTATE pState = pThisCC->svga.p3dState;
3263 AssertReturn(pState, VERR_NO_MEMORY);
3264
3265 Log(("vmsvga3dSetRenderState cid=%u cRenderStates=%d\n", cid, cRenderStates));
3266
3267 PVMSVGA3DCONTEXT pContext;
3268 int rc = vmsvga3dContextFromCid(pState, cid, &pContext);
3269 AssertRCReturn(rc, rc);
3270 AssertReturn(pContext->pDevice, VERR_INTERNAL_ERROR);
3271
3272 for (unsigned i = 0; i < cRenderStates; i++)
3273 {
3274 D3DRENDERSTATETYPE renderState = D3DRS_FORCE_DWORD;
3275
3276 Log(("vmsvga3dSetRenderState: state=%s (%d) val=%x\n", vmsvga3dGetRenderStateName(pRenderState[i].state), pRenderState[i].state, pRenderState[i].uintValue));
3277 /* Save the render state for vm state saving. */
3278 ASSERT_GUEST_RETURN((unsigned)pRenderState[i].state < SVGA3D_RS_MAX, VERR_INVALID_PARAMETER);
3279 pContext->state.aRenderState[pRenderState[i].state] = pRenderState[i];
3280
3281 switch (pRenderState[i].state)
3282 {
3283 case SVGA3D_RS_ZENABLE: /* SVGA3dBool */
3284 renderState = D3DRS_ZENABLE;
3285 val = pRenderState[i].uintValue;
3286 Assert(val == D3DZB_FALSE || val == D3DZB_TRUE);
3287 break;
3288
3289 case SVGA3D_RS_ZWRITEENABLE: /* SVGA3dBool */
3290 renderState = D3DRS_ZWRITEENABLE;
3291 val = pRenderState[i].uintValue;
3292 break;
3293
3294 case SVGA3D_RS_ALPHATESTENABLE: /* SVGA3dBool */
3295 renderState = D3DRS_ALPHATESTENABLE;
3296 val = pRenderState[i].uintValue;
3297 break;
3298
3299 case SVGA3D_RS_DITHERENABLE: /* SVGA3dBool */
3300 renderState = D3DRS_DITHERENABLE;
3301 val = pRenderState[i].uintValue;
3302 break;
3303
3304 case SVGA3D_RS_BLENDENABLE: /* SVGA3dBool */
3305 renderState = D3DRS_ALPHABLENDENABLE;
3306 val = pRenderState[i].uintValue;
3307 break;
3308
3309 case SVGA3D_RS_FOGENABLE: /* SVGA3dBool */
3310 renderState = D3DRS_FOGENABLE;
3311 val = pRenderState[i].uintValue;
3312 break;
3313
3314 case SVGA3D_RS_SPECULARENABLE: /* SVGA3dBool */
3315 renderState = D3DRS_SPECULARENABLE;
3316 val = pRenderState[i].uintValue;
3317 break;
3318
3319 case SVGA3D_RS_LIGHTINGENABLE: /* SVGA3dBool */
3320 renderState = D3DRS_LIGHTING;
3321 val = pRenderState[i].uintValue;
3322 break;
3323
3324 case SVGA3D_RS_NORMALIZENORMALS: /* SVGA3dBool */
3325 renderState = D3DRS_NORMALIZENORMALS;
3326 val = pRenderState[i].uintValue;
3327 break;
3328
3329 case SVGA3D_RS_POINTSPRITEENABLE: /* SVGA3dBool */
3330 renderState = D3DRS_POINTSPRITEENABLE;
3331 val = pRenderState[i].uintValue;
3332 break;
3333
3334 case SVGA3D_RS_POINTSCALEENABLE: /* SVGA3dBool */
3335 renderState = D3DRS_POINTSCALEENABLE;
3336 val = pRenderState[i].uintValue;
3337 break;
3338
3339 case SVGA3D_RS_POINTSIZE: /* float */
3340 renderState = D3DRS_POINTSIZE;
3341 val = pRenderState[i].uintValue;
3342 Log(("SVGA3D_RS_POINTSIZE: %d\n", (uint32_t) (pRenderState[i].floatValue * 100.0)));
3343 break;
3344
3345 case SVGA3D_RS_POINTSIZEMIN: /* float */
3346 renderState = D3DRS_POINTSIZE_MIN;
3347 val = pRenderState[i].uintValue;
3348 Log(("SVGA3D_RS_POINTSIZEMIN: %d\n", (uint32_t) (pRenderState[i].floatValue * 100.0)));
3349 break;
3350
3351 case SVGA3D_RS_POINTSIZEMAX: /* float */
3352 renderState = D3DRS_POINTSIZE_MAX;
3353 val = pRenderState[i].uintValue;
3354 Log(("SVGA3D_RS_POINTSIZEMAX: %d\n", (uint32_t) (pRenderState[i].floatValue * 100.0)));
3355 break;
3356
3357 case SVGA3D_RS_POINTSCALE_A: /* float */
3358 renderState = D3DRS_POINTSCALE_A;
3359 val = pRenderState[i].uintValue;
3360 break;
3361
3362 case SVGA3D_RS_POINTSCALE_B: /* float */
3363 renderState = D3DRS_POINTSCALE_B;
3364 val = pRenderState[i].uintValue;
3365 break;
3366
3367 case SVGA3D_RS_POINTSCALE_C: /* float */
3368 renderState = D3DRS_POINTSCALE_C;
3369 val = pRenderState[i].uintValue;
3370 break;
3371
3372 case SVGA3D_RS_AMBIENT: /* SVGA3dColor - identical */
3373 renderState = D3DRS_AMBIENT;
3374 val = pRenderState[i].uintValue;
3375 break;
3376
3377 case SVGA3D_RS_CLIPPLANEENABLE: /* SVGA3dClipPlanes - identical */
3378 renderState = D3DRS_CLIPPLANEENABLE;
3379 val = pRenderState[i].uintValue;
3380 break;
3381
3382 case SVGA3D_RS_FOGCOLOR: /* SVGA3dColor - identical */
3383 renderState = D3DRS_FOGCOLOR;
3384 val = pRenderState[i].uintValue;
3385 break;
3386
3387 case SVGA3D_RS_FOGSTART: /* float */
3388 renderState = D3DRS_FOGSTART;
3389 val = pRenderState[i].uintValue;
3390 break;
3391
3392 case SVGA3D_RS_FOGEND: /* float */
3393 renderState = D3DRS_FOGEND;
3394 val = pRenderState[i].uintValue;
3395 break;
3396
3397 case SVGA3D_RS_FOGDENSITY: /* float */
3398 renderState = D3DRS_FOGDENSITY;
3399 val = pRenderState[i].uintValue;
3400 break;
3401
3402 case SVGA3D_RS_RANGEFOGENABLE: /* SVGA3dBool */
3403 renderState = D3DRS_RANGEFOGENABLE;
3404 val = pRenderState[i].uintValue;
3405 break;
3406
3407 case SVGA3D_RS_FOGMODE: /* SVGA3dFogMode */
3408 {
3409 SVGA3dFogMode mode;
3410 mode.uintValue = pRenderState[i].uintValue;
3411
3412 switch (mode.function)
3413 {
3414 case SVGA3D_FOGFUNC_INVALID:
3415 val = D3DFOG_NONE;
3416 break;
3417 case SVGA3D_FOGFUNC_EXP:
3418 val = D3DFOG_EXP;
3419 break;
3420 case SVGA3D_FOGFUNC_EXP2:
3421 val = D3DFOG_EXP2;
3422 break;
3423 case SVGA3D_FOGFUNC_LINEAR:
3424 val = D3DFOG_LINEAR;
3425 break;
3426 case SVGA3D_FOGFUNC_PER_VERTEX: /* unable to find a d3d9 equivalent */
3427 AssertMsgFailedReturn(("Unsupported fog function SVGA3D_FOGFUNC_PER_VERTEX\n"), VERR_INTERNAL_ERROR);
3428 break;
3429 default:
3430 AssertMsgFailedReturn(("Unexpected fog function %d\n", mode.function), VERR_INTERNAL_ERROR);
3431 break;
3432 }
3433
3434 /* The fog type determines the render state. */
3435 switch (mode.type)
3436 {
3437 case SVGA3D_FOGTYPE_VERTEX:
3438 renderState = D3DRS_FOGVERTEXMODE;
3439 break;
3440 case SVGA3D_FOGTYPE_PIXEL:
3441 renderState = D3DRS_FOGTABLEMODE;
3442 break;
3443 default:
3444 AssertMsgFailedReturn(("Unexpected fog type %d\n", mode.type), VERR_INTERNAL_ERROR);
3445 break;
3446 }
3447
3448 /* Set the fog base to depth or range. */
3449 switch (mode.base)
3450 {
3451 case SVGA3D_FOGBASE_DEPTHBASED:
3452 hr = pContext->pDevice->SetRenderState(D3DRS_RANGEFOGENABLE, FALSE);
3453 AssertMsgReturn(hr == D3D_OK, ("vmsvga3dSetRenderState: SetRenderState SVGA3D_FOGBASE_DEPTHBASED failed with %x\n", hr), VERR_INTERNAL_ERROR);
3454 break;
3455 case SVGA3D_FOGBASE_RANGEBASED:
3456 hr = pContext->pDevice->SetRenderState(D3DRS_RANGEFOGENABLE, TRUE);
3457 AssertMsgReturn(hr == D3D_OK, ("vmsvga3dSetRenderState: SetRenderState SVGA3D_FOGBASE_RANGEBASED failed with %x\n", hr), VERR_INTERNAL_ERROR);
3458 break;
3459 default:
3460 /* ignore */
3461 AssertMsgFailed(("Unexpected fog base %d\n", mode.base));
3462 break;
3463 }
3464 break;
3465 }
3466
3467 case SVGA3D_RS_FILLMODE: /* SVGA3dFillMode */
3468 {
3469 SVGA3dFillMode mode;
3470
3471 mode.uintValue = pRenderState[i].uintValue;
3472
3473 switch (mode.mode)
3474 {
3475 case SVGA3D_FILLMODE_POINT:
3476 val = D3DFILL_POINT;
3477 break;
3478 case SVGA3D_FILLMODE_LINE:
3479 val = D3DFILL_WIREFRAME;
3480 break;
3481 case SVGA3D_FILLMODE_FILL:
3482 val = D3DFILL_SOLID;
3483 break;
3484 default:
3485 AssertMsgFailedReturn(("Unexpected fill mode %d\n", mode.mode), VERR_INTERNAL_ERROR);
3486 break;
3487 }
3488 /** @todo ignoring face for now. */
3489 renderState = D3DRS_FILLMODE;
3490 break;
3491 }
3492
3493 case SVGA3D_RS_SHADEMODE: /* SVGA3dShadeMode */
3494 renderState = D3DRS_SHADEMODE;
3495 AssertCompile(D3DSHADE_FLAT == SVGA3D_SHADEMODE_FLAT);
3496 val = pRenderState[i].uintValue; /* SVGA3dShadeMode == D3DSHADEMODE */
3497 break;
3498
3499 case SVGA3D_RS_LINEPATTERN: /* SVGA3dLinePattern */
3500 /* No longer supported by d3d; mesagl comments suggest not all backends support it */
3501 /** @todo */
3502 Log(("WARNING: SVGA3D_RS_LINEPATTERN %x not supported!!\n", pRenderState[i].uintValue));
3503 /*
3504 renderState = D3DRS_LINEPATTERN;
3505 val = pRenderState[i].uintValue;
3506 */
3507 break;
3508
3509 case SVGA3D_RS_SRCBLEND: /* SVGA3dBlendOp */
3510 renderState = D3DRS_SRCBLEND;
3511 val = vmsvga3dBlendOp2D3D(pRenderState[i].uintValue, D3DBLEND_ONE /* default */);
3512 break;
3513
3514 case SVGA3D_RS_DSTBLEND: /* SVGA3dBlendOp */
3515 renderState = D3DRS_DESTBLEND;
3516 val = vmsvga3dBlendOp2D3D(pRenderState[i].uintValue, D3DBLEND_ZERO /* default */);
3517 break;
3518
3519 case SVGA3D_RS_BLENDEQUATION: /* SVGA3dBlendEquation - identical */
3520 AssertCompile(SVGA3D_BLENDEQ_MAXIMUM == D3DBLENDOP_MAX);
3521 renderState = D3DRS_BLENDOP;
3522 val = pRenderState[i].uintValue;
3523 break;
3524
3525 case SVGA3D_RS_CULLMODE: /* SVGA3dFace */
3526 {
3527 switch (pRenderState[i].uintValue)
3528 {
3529 case SVGA3D_FACE_NONE:
3530 val = D3DCULL_NONE;
3531 break;
3532 case SVGA3D_FACE_FRONT:
3533 val = D3DCULL_CW;
3534 break;
3535 case SVGA3D_FACE_BACK:
3536 val = D3DCULL_CCW;
3537 break;
3538 case SVGA3D_FACE_FRONT_BACK:
3539 AssertFailed();
3540 val = D3DCULL_CW;
3541 break;
3542 default:
3543 AssertMsgFailedReturn(("Unexpected cull mode %d\n", pRenderState[i].uintValue), VERR_INTERNAL_ERROR);
3544 break;
3545 }
3546 renderState = D3DRS_CULLMODE;
3547 break;
3548 }
3549
3550 case SVGA3D_RS_ZFUNC: /* SVGA3dCmpFunc - identical */
3551 AssertCompile(SVGA3D_CMP_ALWAYS == D3DCMP_ALWAYS);
3552 renderState = D3DRS_ZFUNC;
3553 val = pRenderState[i].uintValue;
3554 break;
3555
3556 case SVGA3D_RS_ALPHAFUNC: /* SVGA3dCmpFunc - identical */
3557 renderState = D3DRS_ALPHAFUNC;
3558 val = pRenderState[i].uintValue;
3559 break;
3560
3561 case SVGA3D_RS_STENCILENABLE: /* SVGA3dBool */
3562 renderState = D3DRS_STENCILENABLE;
3563 val = pRenderState[i].uintValue;
3564 break;
3565
3566 case SVGA3D_RS_STENCILREF: /* uint32_t */
3567 renderState = D3DRS_STENCILREF;
3568 val = pRenderState[i].uintValue;
3569 break;
3570
3571 case SVGA3D_RS_STENCILMASK: /* uint32_t */
3572 renderState = D3DRS_STENCILMASK;
3573 val = pRenderState[i].uintValue;
3574 break;
3575
3576 case SVGA3D_RS_STENCILWRITEMASK: /* uint32_t */
3577 renderState = D3DRS_STENCILWRITEMASK;
3578 val = pRenderState[i].uintValue;
3579 break;
3580
3581 case SVGA3D_RS_STENCILFUNC: /* SVGA3dCmpFunc - identical */
3582 renderState = D3DRS_STENCILFUNC;
3583 val = pRenderState[i].uintValue;
3584 break;
3585
3586 case SVGA3D_RS_STENCILFAIL: /* SVGA3dStencilOp - identical */
3587 AssertCompile(D3DSTENCILOP_KEEP == SVGA3D_STENCILOP_KEEP);
3588 AssertCompile(D3DSTENCILOP_DECR == SVGA3D_STENCILOP_DECR);
3589 renderState = D3DRS_STENCILFAIL;
3590 val = pRenderState[i].uintValue;
3591 break;
3592
3593 case SVGA3D_RS_STENCILZFAIL: /* SVGA3dStencilOp - identical */
3594 renderState = D3DRS_STENCILZFAIL;
3595 val = pRenderState[i].uintValue;
3596 break;
3597
3598 case SVGA3D_RS_STENCILPASS: /* SVGA3dStencilOp - identical */
3599 renderState = D3DRS_STENCILPASS;
3600 val = pRenderState[i].uintValue;
3601 break;
3602
3603 case SVGA3D_RS_ALPHAREF: /* float (0.0 .. 1.0) */
3604 renderState = D3DRS_ALPHAREF;
3605 val = (uint8_t)(pRenderState[i].floatValue * 255.0f); /* D3DRS_ALPHAREF 0..255 */
3606 break;
3607
3608 case SVGA3D_RS_FRONTWINDING: /* SVGA3dFrontWinding */
3609 Assert(pRenderState[i].uintValue == SVGA3D_FRONTWINDING_CW);
3610 /*
3611 renderState = D3DRS_FRONTWINDING; //D3DRS_TWOSIDEDSTENCILMODE
3612 val = pRenderState[i].uintValue;
3613 */
3614 break;
3615
3616 case SVGA3D_RS_COORDINATETYPE: /* SVGA3dCoordinateType */
3617 Assert(pRenderState[i].uintValue == SVGA3D_COORDINATE_LEFTHANDED);
3618 /** @todo setup a view matrix to scale the world space by -1 in the z-direction for right handed coordinates. */
3619 /*
3620 renderState = D3DRS_COORDINATETYPE;
3621 val = pRenderState[i].uintValue;
3622 */
3623 break;
3624
3625 case SVGA3D_RS_ZBIAS: /* float */
3626 /** @todo unknown meaning; depth bias is not identical
3627 renderState = D3DRS_DEPTHBIAS;
3628 val = pRenderState[i].uintValue;
3629 */
3630 Log(("vmsvga3dSetRenderState: WARNING unsupported SVGA3D_RS_ZBIAS\n"));
3631 break;
3632
3633 case SVGA3D_RS_SLOPESCALEDEPTHBIAS: /* float */
3634 renderState = D3DRS_SLOPESCALEDEPTHBIAS;
3635 val = pRenderState[i].uintValue;
3636 break;
3637
3638 case SVGA3D_RS_DEPTHBIAS: /* float */
3639 renderState = D3DRS_DEPTHBIAS;
3640 val = pRenderState[i].uintValue;
3641 break;
3642
3643 case SVGA3D_RS_COLORWRITEENABLE: /* SVGA3dColorMask - identical to D3DCOLORWRITEENABLE_* */
3644 renderState = D3DRS_COLORWRITEENABLE;
3645 val = pRenderState[i].uintValue;
3646 break;
3647
3648 case SVGA3D_RS_VERTEXMATERIALENABLE: /* SVGA3dBool */
3649 //AssertFailed();
3650 renderState = D3DRS_INDEXEDVERTEXBLENDENABLE; /* correct?? */
3651 val = pRenderState[i].uintValue;
3652 break;
3653
3654 case SVGA3D_RS_DIFFUSEMATERIALSOURCE: /* SVGA3dVertexMaterial - identical */
3655 AssertCompile(D3DMCS_COLOR2 == SVGA3D_VERTEXMATERIAL_SPECULAR);
3656 renderState = D3DRS_DIFFUSEMATERIALSOURCE;
3657 val = pRenderState[i].uintValue;
3658 break;
3659
3660 case SVGA3D_RS_SPECULARMATERIALSOURCE: /* SVGA3dVertexMaterial - identical */
3661 renderState = D3DRS_SPECULARMATERIALSOURCE;
3662 val = pRenderState[i].uintValue;
3663 break;
3664
3665 case SVGA3D_RS_AMBIENTMATERIALSOURCE: /* SVGA3dVertexMaterial - identical */
3666 renderState = D3DRS_AMBIENTMATERIALSOURCE;
3667 val = pRenderState[i].uintValue;
3668 break;
3669
3670 case SVGA3D_RS_EMISSIVEMATERIALSOURCE: /* SVGA3dVertexMaterial - identical */
3671 renderState = D3DRS_EMISSIVEMATERIALSOURCE;
3672 val = pRenderState[i].uintValue;
3673 break;
3674
3675 case SVGA3D_RS_TEXTUREFACTOR: /* SVGA3dColor - identical */
3676 renderState = D3DRS_TEXTUREFACTOR;
3677 val = pRenderState[i].uintValue;
3678 break;
3679
3680 case SVGA3D_RS_LOCALVIEWER: /* SVGA3dBool */
3681 renderState = D3DRS_LOCALVIEWER;
3682 val = pRenderState[i].uintValue;
3683 break;
3684
3685 case SVGA3D_RS_SCISSORTESTENABLE: /* SVGA3dBool */
3686 renderState = D3DRS_SCISSORTESTENABLE;
3687 val = pRenderState[i].uintValue;
3688 break;
3689
3690 case SVGA3D_RS_BLENDCOLOR: /* SVGA3dColor - identical */
3691 renderState = D3DRS_BLENDFACTOR;
3692 val = pRenderState[i].uintValue;
3693 break;
3694
3695 case SVGA3D_RS_STENCILENABLE2SIDED: /* SVGA3dBool */
3696 renderState = D3DRS_TWOSIDEDSTENCILMODE;
3697 val = pRenderState[i].uintValue;
3698 break;
3699
3700 case SVGA3D_RS_CCWSTENCILFUNC: /* SVGA3dCmpFunc - identical */
3701 renderState = D3DRS_CCW_STENCILFUNC;
3702 val = pRenderState[i].uintValue;
3703 break;
3704
3705 case SVGA3D_RS_CCWSTENCILFAIL: /* SVGA3dStencilOp - identical */
3706 renderState = D3DRS_CCW_STENCILFAIL;
3707 val = pRenderState[i].uintValue;
3708 break;
3709
3710 case SVGA3D_RS_CCWSTENCILZFAIL: /* SVGA3dStencilOp - identical */
3711 renderState = D3DRS_CCW_STENCILZFAIL;
3712 val = pRenderState[i].uintValue;
3713 break;
3714
3715 case SVGA3D_RS_CCWSTENCILPASS: /* SVGA3dStencilOp - identical */
3716 renderState = D3DRS_CCW_STENCILPASS;
3717 val = pRenderState[i].uintValue;
3718 break;
3719
3720 case SVGA3D_RS_VERTEXBLEND: /* SVGA3dVertexBlendFlags - identical */
3721 AssertCompile(SVGA3D_VBLEND_DISABLE == D3DVBF_DISABLE);
3722 renderState = D3DRS_VERTEXBLEND;
3723 val = pRenderState[i].uintValue;
3724 break;
3725
3726 case SVGA3D_RS_OUTPUTGAMMA: /* float */
3727 //AssertFailed();
3728 /*
3729 D3DRS_SRGBWRITEENABLE ??
3730 renderState = D3DRS_OUTPUTGAMMA;
3731 val = pRenderState[i].uintValue;
3732 */
3733 break;
3734
3735 case SVGA3D_RS_ZVISIBLE: /* SVGA3dBool */
3736 AssertFailed();
3737 /*
3738 renderState = D3DRS_ZVISIBLE;
3739 val = pRenderState[i].uintValue;
3740 */
3741 break;
3742
3743 case SVGA3D_RS_LASTPIXEL: /* SVGA3dBool */
3744 renderState = D3DRS_LASTPIXEL;
3745 val = pRenderState[i].uintValue;
3746 break;
3747
3748 case SVGA3D_RS_CLIPPING: /* SVGA3dBool */
3749 renderState = D3DRS_CLIPPING;
3750 val = pRenderState[i].uintValue;
3751 break;
3752
3753 case SVGA3D_RS_WRAP0: /* SVGA3dWrapFlags - identical */
3754 Assert(SVGA3D_WRAPCOORD_3 == D3DWRAPCOORD_3);
3755 renderState = D3DRS_WRAP0;
3756 val = pRenderState[i].uintValue;
3757 break;
3758
3759 case SVGA3D_RS_WRAP1: /* SVGA3dWrapFlags - identical */
3760 renderState = D3DRS_WRAP1;
3761 val = pRenderState[i].uintValue;
3762 break;
3763
3764 case SVGA3D_RS_WRAP2: /* SVGA3dWrapFlags - identical */
3765 renderState = D3DRS_WRAP2;
3766 val = pRenderState[i].uintValue;
3767 break;
3768
3769 case SVGA3D_RS_WRAP3: /* SVGA3dWrapFlags - identical */
3770 renderState = D3DRS_WRAP3;
3771 val = pRenderState[i].uintValue;
3772 break;
3773
3774 case SVGA3D_RS_WRAP4: /* SVGA3dWrapFlags - identical */
3775 renderState = D3DRS_WRAP4;
3776 val = pRenderState[i].uintValue;
3777 break;
3778
3779 case SVGA3D_RS_WRAP5: /* SVGA3dWrapFlags - identical */
3780 renderState = D3DRS_WRAP5;
3781 val = pRenderState[i].uintValue;
3782 break;
3783
3784 case SVGA3D_RS_WRAP6: /* SVGA3dWrapFlags - identical */
3785 renderState = D3DRS_WRAP6;
3786 val = pRenderState[i].uintValue;
3787 break;
3788
3789 case SVGA3D_RS_WRAP7: /* SVGA3dWrapFlags - identical */
3790 renderState = D3DRS_WRAP7;
3791 val = pRenderState[i].uintValue;
3792 break;
3793
3794 case SVGA3D_RS_WRAP8: /* SVGA3dWrapFlags - identical */
3795 renderState = D3DRS_WRAP8;
3796 val = pRenderState[i].uintValue;
3797 break;
3798
3799 case SVGA3D_RS_WRAP9: /* SVGA3dWrapFlags - identical */
3800 renderState = D3DRS_WRAP9;
3801 val = pRenderState[i].uintValue;
3802 break;
3803
3804 case SVGA3D_RS_WRAP10: /* SVGA3dWrapFlags - identical */
3805 renderState = D3DRS_WRAP10;
3806 val = pRenderState[i].uintValue;
3807 break;
3808
3809 case SVGA3D_RS_WRAP11: /* SVGA3dWrapFlags - identical */
3810 renderState = D3DRS_WRAP11;
3811 val = pRenderState[i].uintValue;
3812 break;
3813
3814 case SVGA3D_RS_WRAP12: /* SVGA3dWrapFlags - identical */
3815 renderState = D3DRS_WRAP12;
3816 val = pRenderState[i].uintValue;
3817 break;
3818
3819 case SVGA3D_RS_WRAP13: /* SVGA3dWrapFlags - identical */
3820 renderState = D3DRS_WRAP13;
3821 val = pRenderState[i].uintValue;
3822 break;
3823
3824 case SVGA3D_RS_WRAP14: /* SVGA3dWrapFlags - identical */
3825 renderState = D3DRS_WRAP14;
3826 val = pRenderState[i].uintValue;
3827 break;
3828
3829 case SVGA3D_RS_WRAP15: /* SVGA3dWrapFlags - identical */
3830 renderState = D3DRS_WRAP15;
3831 val = pRenderState[i].uintValue;
3832 break;
3833
3834 case SVGA3D_RS_MULTISAMPLEANTIALIAS: /* SVGA3dBool */
3835 renderState = D3DRS_MULTISAMPLEANTIALIAS;
3836 val = pRenderState[i].uintValue;
3837 break;
3838
3839 case SVGA3D_RS_MULTISAMPLEMASK: /* uint32_t */
3840 renderState = D3DRS_MULTISAMPLEMASK;
3841 val = pRenderState[i].uintValue;
3842 break;
3843
3844 case SVGA3D_RS_INDEXEDVERTEXBLENDENABLE: /* SVGA3dBool */
3845 renderState = D3DRS_INDEXEDVERTEXBLENDENABLE;
3846 val = pRenderState[i].uintValue;
3847 break;
3848
3849 case SVGA3D_RS_TWEENFACTOR: /* float */
3850 renderState = D3DRS_TWEENFACTOR;
3851 val = pRenderState[i].uintValue;
3852 break;
3853
3854 case SVGA3D_RS_ANTIALIASEDLINEENABLE: /* SVGA3dBool */
3855 renderState = D3DRS_ANTIALIASEDLINEENABLE;
3856 val = pRenderState[i].uintValue;
3857 break;
3858
3859 case SVGA3D_RS_COLORWRITEENABLE1: /* SVGA3dColorMask - identical to D3DCOLORWRITEENABLE_* */
3860 renderState = D3DRS_COLORWRITEENABLE1;
3861 val = pRenderState[i].uintValue;
3862 break;
3863
3864 case SVGA3D_RS_COLORWRITEENABLE2: /* SVGA3dColorMask - identical to D3DCOLORWRITEENABLE_* */
3865 renderState = D3DRS_COLORWRITEENABLE2;
3866 val = pRenderState[i].uintValue;
3867 break;
3868
3869 case SVGA3D_RS_COLORWRITEENABLE3: /* SVGA3dColorMask - identical to D3DCOLORWRITEENABLE_* */
3870 renderState = D3DRS_COLORWRITEENABLE3;
3871 val = pRenderState[i].uintValue;
3872 break;
3873
3874 case SVGA3D_RS_SEPARATEALPHABLENDENABLE: /* SVGA3dBool */
3875 renderState = D3DRS_SEPARATEALPHABLENDENABLE;
3876 val = pRenderState[i].uintValue;
3877 break;
3878
3879 case SVGA3D_RS_SRCBLENDALPHA: /* SVGA3dBlendOp */
3880 renderState = D3DRS_SRCBLENDALPHA;
3881 val = vmsvga3dBlendOp2D3D(pRenderState[i].uintValue, D3DBLEND_ONE /* default */);
3882 break;
3883
3884 case SVGA3D_RS_DSTBLENDALPHA: /* SVGA3dBlendOp */
3885 renderState = D3DRS_DESTBLENDALPHA;
3886 val = vmsvga3dBlendOp2D3D(pRenderState[i].uintValue, D3DBLEND_ZERO /* default */);
3887 break;
3888
3889 case SVGA3D_RS_BLENDEQUATIONALPHA: /* SVGA3dBlendEquation - identical */
3890 renderState = D3DRS_BLENDOPALPHA;
3891 val = pRenderState[i].uintValue;
3892 break;
3893
3894 case SVGA3D_RS_TRANSPARENCYANTIALIAS: /* SVGA3dTransparencyAntialiasType */
3895 AssertFailed();
3896 /*
3897 renderState = D3DRS_TRANSPARENCYANTIALIAS;
3898 val = pRenderState[i].uintValue;
3899 */
3900 break;
3901
3902 case SVGA3D_RS_LINEWIDTH: /* float */
3903 AssertFailed();
3904 /*
3905 renderState = D3DRS_LINEWIDTH;
3906 val = pRenderState[i].uintValue;
3907 */
3908 break;
3909
3910 case SVGA3D_RS_MAX: /* shut up MSC */
3911 case SVGA3D_RS_INVALID:
3912 AssertFailedBreak();
3913 }
3914
3915 if (renderState != D3DRS_FORCE_DWORD)
3916 {
3917 hr = pContext->pDevice->SetRenderState(renderState, val);
3918 AssertMsgReturn(hr == D3D_OK, ("vmsvga3dSetRenderState: SetRenderState failed with %x\n", hr), VERR_INTERNAL_ERROR);
3919 }
3920 }
3921
3922 return VINF_SUCCESS;
3923}
3924
3925static DECLCALLBACK(int) vmsvga3dBackSetRenderTarget(PVGASTATECC pThisCC, uint32_t cid, SVGA3dRenderTargetType type, SVGA3dSurfaceImageId target)
3926{
3927 HRESULT hr;
3928 PVMSVGA3DSTATE pState = pThisCC->svga.p3dState;
3929
3930 AssertReturn(pState, VERR_NO_MEMORY);
3931 AssertReturn((unsigned)type < SVGA3D_RT_MAX, VERR_INVALID_PARAMETER);
3932
3933 LogFunc(("cid=%u type=%x sid=%u face=%u mipmap=%u\n", cid, type, target.sid, target.face, target.mipmap));
3934
3935 PVMSVGA3DCONTEXT pContext;
3936 int rc = vmsvga3dContextFromCid(pState, cid, &pContext);
3937 AssertRCReturn(rc, rc);
3938 AssertReturn(pContext->pDevice, VERR_INTERNAL_ERROR);
3939
3940 /* Save for vm state save/restore. */
3941 pContext->state.aRenderTargets[type] = target.sid;
3942 /** @todo Also save target.face and target.mipmap */
3943
3944 if (target.sid == SVGA3D_INVALID_ID)
3945 {
3946 /* Disable render target. */
3947 switch (type)
3948 {
3949 case SVGA3D_RT_DEPTH:
3950 hr = pContext->pDevice->SetDepthStencilSurface(NULL);
3951 AssertMsgReturn(hr == D3D_OK, ("SetDepthStencilSurface failed with %x\n", hr), VERR_INTERNAL_ERROR);
3952 break;
3953
3954 case SVGA3D_RT_STENCIL:
3955 /* ignore; correct?? */
3956 break;
3957
3958 case SVGA3D_RT_COLOR0:
3959 case SVGA3D_RT_COLOR1:
3960 case SVGA3D_RT_COLOR2:
3961 case SVGA3D_RT_COLOR3:
3962 case SVGA3D_RT_COLOR4:
3963 case SVGA3D_RT_COLOR5:
3964 case SVGA3D_RT_COLOR6:
3965 case SVGA3D_RT_COLOR7:
3966 if (pState->fSupportedSurfaceNULL)
3967 {
3968 /* Create a dummy render target to satisfy D3D. This path is usually taken only to render
3969 * into a depth buffer without wishing to update an actual color render target.
3970 * The dimensions of the render target must match the dimensions of the depth render target,
3971 * which is usually equal to the viewport width and height.
3972 */
3973 IDirect3DSurface9 *pDummyRenderTarget;
3974 hr = pContext->pDevice->CreateRenderTarget(pContext->state.RectViewPort.w,
3975 pContext->state.RectViewPort.h,
3976 FOURCC_NULL,
3977 D3DMULTISAMPLE_NONE,
3978 0,
3979 FALSE,
3980 &pDummyRenderTarget,
3981 NULL);
3982
3983 AssertMsgReturn(hr == D3D_OK, ("CreateRenderTarget failed with %x\n", hr), VERR_INTERNAL_ERROR);
3984
3985 hr = pContext->pDevice->SetRenderTarget(type - SVGA3D_RT_COLOR0, pDummyRenderTarget);
3986 D3D_RELEASE(pDummyRenderTarget);
3987 }
3988 else
3989 hr = pContext->pDevice->SetRenderTarget(type - SVGA3D_RT_COLOR0, NULL);
3990
3991 AssertMsgReturn(hr == D3D_OK, ("SetRenderTarget failed with %x\n", hr), VERR_INTERNAL_ERROR);
3992 break;
3993
3994 default:
3995 AssertFailedReturn(VERR_INVALID_PARAMETER);
3996 }
3997 return VINF_SUCCESS;
3998 }
3999
4000 PVMSVGA3DSURFACE pRenderTarget;
4001 rc = vmsvga3dSurfaceFromSid(pState, target.sid, &pRenderTarget);
4002 AssertRCReturn(rc, rc);
4003
4004 switch (type)
4005 {
4006 case SVGA3D_RT_DEPTH:
4007 case SVGA3D_RT_STENCIL:
4008 AssertReturn(target.face == 0 && target.mipmap == 0, VERR_INVALID_PARAMETER);
4009 if (!pRenderTarget->u.pSurface)
4010 {
4011 DWORD cQualityLevels = 0;
4012
4013 /* Query the nr of quality levels for this particular format */
4014 if (pRenderTarget->multiSampleTypeD3D != D3DMULTISAMPLE_NONE)
4015 {
4016 hr = pState->pD3D9->CheckDeviceMultiSampleType(D3DADAPTER_DEFAULT,
4017 D3DDEVTYPE_HAL,
4018 pRenderTarget->formatD3D,
4019 TRUE, /* Windowed */
4020 pRenderTarget->multiSampleTypeD3D,
4021 &cQualityLevels);
4022 Assert(hr == D3D_OK || hr == D3DERR_NOTAVAILABLE);
4023 }
4024
4025 if ( pState->fSupportedSurfaceINTZ
4026 && pRenderTarget->multiSampleTypeD3D == D3DMULTISAMPLE_NONE
4027 && ( pRenderTarget->formatD3D == D3DFMT_D24S8
4028 || pRenderTarget->formatD3D == D3DFMT_D24X8
4029 || pRenderTarget->formatD3D == D3DFMT_D32
4030 || pRenderTarget->formatD3D == D3DFMT_D16))
4031 {
4032 LogFunc(("Creating stencil surface as texture!\n"));
4033 int rc2 = vmsvga3dBackCreateTexture(pThisCC, pContext, cid, pRenderTarget);
4034 AssertRC(rc2); /* non-fatal, will use CreateDepthStencilSurface */
4035 }
4036
4037 if (!pRenderTarget->fStencilAsTexture)
4038 {
4039 Assert(!pRenderTarget->u.pSurface);
4040
4041 LogFunc(("DEPTH/STENCIL; cQualityLevels=%d\n", cQualityLevels));
4042 hr = pContext->pDevice->CreateDepthStencilSurface(pRenderTarget->paMipmapLevels[0].mipmapSize.width,
4043 pRenderTarget->paMipmapLevels[0].mipmapSize.height,
4044 pRenderTarget->formatD3D,
4045 pRenderTarget->multiSampleTypeD3D,
4046 ((cQualityLevels >= 1) ? cQualityLevels - 1 : 0), /* 0 - (levels-1) */
4047 FALSE, /* not discardable */
4048 &pRenderTarget->u.pSurface,
4049 NULL);
4050 AssertMsgReturn(hr == D3D_OK, ("CreateDepthStencilSurface failed with %x\n", hr), VERR_INTERNAL_ERROR);
4051 pRenderTarget->enmD3DResType = VMSVGA3D_D3DRESTYPE_SURFACE;
4052 }
4053
4054 pRenderTarget->idAssociatedContext = cid;
4055
4056#if 0 /* doesn't work */
4057 if ( !pRenderTarget->fStencilAsTexture
4058 && pRenderTarget->fDirty)
4059 {
4060 Log(("vmsvga3dSetRenderTarget: sync dirty depth/stencil buffer\n"));
4061 Assert(pRenderTarget->cLevels == 1);
4062
4063 for (uint32_t i = 0; i < pRenderTarget->cLevels; i++)
4064 {
4065 if (pRenderTarget->paMipmapLevels[i].fDirty)
4066 {
4067 D3DLOCKED_RECT LockedRect;
4068
4069 hr = pRenderTarget->u.pSurface->LockRect(&LockedRect,
4070 NULL, /* entire surface */
4071 0);
4072
4073 AssertMsgReturn(hr == D3D_OK, ("vmsvga3dSetRenderTarget: LockRect failed with %x\n", hr), VERR_INTERNAL_ERROR);
4074
4075 Log(("vmsvga3dSetRenderTarget: sync dirty texture mipmap level %d (pitch %x vs %x)\n", i, LockedRect.Pitch, pRenderTarget->paMipmapLevels[i].cbSurfacePitch));
4076
4077 uint8_t *pDest = (uint8_t *)LockedRect.pBits;
4078 uint8_t *pSrc = (uint8_t *)pRenderTarget->paMipmapLevels[i].pSurfaceData;
4079 for (uint32_t j = 0; j < pRenderTarget->paMipmapLevels[i].size.height; j++)
4080 {
4081 memcpy(pDest, pSrc, pRenderTarget->paMipmapLevels[i].cbSurfacePitch);
4082
4083 pDest += LockedRect.Pitch;
4084 pSrc += pRenderTarget->paMipmapLevels[i].cbSurfacePitch;
4085 }
4086
4087 hr = pRenderTarget->u.pSurface->UnlockRect();
4088 AssertMsgReturn(hr == D3D_OK, ("vmsvga3dSetRenderTarget: UnlockRect failed with %x\n", hr), VERR_INTERNAL_ERROR);
4089
4090 pRenderTarget->paMipmapLevels[i].fDirty = false;
4091 }
4092 }
4093 }
4094#endif
4095 }
4096
4097 /** @todo Assert(!pRenderTarget->fDirty); */
4098
4099 AssertReturn(pRenderTarget->u.pSurface, VERR_INVALID_PARAMETER);
4100
4101 pRenderTarget->fUsageD3D |= D3DUSAGE_DEPTHSTENCIL;
4102 pRenderTarget->f.surfaceFlags |= SVGA3D_SURFACE_HINT_DEPTHSTENCIL;
4103
4104 if (pRenderTarget->fStencilAsTexture)
4105 {
4106 IDirect3DSurface9 *pStencilSurface;
4107
4108 rc = vmsvga3dGetD3DSurface(pState, pContext, pRenderTarget, target.face, target.mipmap, /*fLockable=*/ false, &pStencilSurface);
4109 AssertRCReturn(rc, rc);
4110
4111 hr = pContext->pDevice->SetDepthStencilSurface(pStencilSurface);
4112 D3D_RELEASE(pStencilSurface);
4113 AssertMsgReturn(hr == D3D_OK, ("SetDepthStencilSurface failed with %x\n", hr), VERR_INTERNAL_ERROR);
4114 }
4115 else
4116 {
4117 Assert(pRenderTarget->idAssociatedContext == cid);
4118 AssertReturn(pRenderTarget->enmD3DResType == VMSVGA3D_D3DRESTYPE_SURFACE, VERR_INVALID_PARAMETER);
4119 hr = pContext->pDevice->SetDepthStencilSurface(pRenderTarget->u.pSurface);
4120 AssertMsgReturn(hr == D3D_OK, ("SetDepthStencilSurface failed with %x\n", hr), VERR_INTERNAL_ERROR);
4121 }
4122 break;
4123
4124 case SVGA3D_RT_COLOR0:
4125 case SVGA3D_RT_COLOR1:
4126 case SVGA3D_RT_COLOR2:
4127 case SVGA3D_RT_COLOR3:
4128 case SVGA3D_RT_COLOR4:
4129 case SVGA3D_RT_COLOR5:
4130 case SVGA3D_RT_COLOR6:
4131 case SVGA3D_RT_COLOR7:
4132 {
4133 IDirect3DSurface9 *pSurface;
4134 bool fTexture = false;
4135
4136 /* Must flush the other context's 3d pipeline to make sure all drawing is complete for the surface we're about to use. */
4137 vmsvga3dSurfaceFlush(pRenderTarget);
4138
4139 if (pRenderTarget->f.surfaceFlags & SVGA3D_SURFACE_HINT_TEXTURE)
4140 {
4141 fTexture = true;
4142
4143 /* A texture surface can be used as a render target to fill it and later on used as a texture. */
4144 if (!pRenderTarget->u.pTexture)
4145 {
4146 LogFunc(("Create texture to be used as render target; sid=%u type=%d format=%d -> create texture\n", target.sid, pRenderTarget->f.s.surface1Flags, pRenderTarget->format));
4147 rc = vmsvga3dBackCreateTexture(pThisCC, pContext, cid, pRenderTarget);
4148 AssertRCReturn(rc, rc);
4149 }
4150
4151 rc = vmsvga3dGetD3DSurface(pState, pContext, pRenderTarget, target.face, target.mipmap, false, &pSurface);
4152 AssertRCReturn(rc, rc);
4153 }
4154 else
4155 {
4156 AssertReturn(target.face == 0 && target.mipmap == 0, VERR_INVALID_PARAMETER);
4157 if (!pRenderTarget->u.pSurface)
4158 {
4159 DWORD cQualityLevels = 0;
4160
4161 /* Query the nr of quality levels for this particular format */
4162 if (pRenderTarget->multiSampleTypeD3D != D3DMULTISAMPLE_NONE)
4163 {
4164 hr = pState->pD3D9->CheckDeviceMultiSampleType(D3DADAPTER_DEFAULT,
4165 D3DDEVTYPE_HAL,
4166 pRenderTarget->formatD3D,
4167 TRUE, /* Windowed */
4168 pRenderTarget->multiSampleTypeD3D,
4169 &cQualityLevels);
4170 Assert(hr == D3D_OK || hr == D3DERR_NOTAVAILABLE);
4171 }
4172
4173 LogFunc(("COLOR; cQualityLevels=%d\n", cQualityLevels));
4174 LogFunc(("Create rendertarget (%d,%d) formatD3D=%x multisample=%x\n",
4175 pRenderTarget->paMipmapLevels[0].mipmapSize.width, pRenderTarget->paMipmapLevels[0].mipmapSize.height, pRenderTarget->formatD3D, pRenderTarget->multiSampleTypeD3D));
4176
4177 hr = pContext->pDevice->CreateRenderTarget(pRenderTarget->paMipmapLevels[0].mipmapSize.width,
4178 pRenderTarget->paMipmapLevels[0].mipmapSize.height,
4179 pRenderTarget->formatD3D,
4180 pRenderTarget->multiSampleTypeD3D,
4181 ((cQualityLevels >= 1) ? cQualityLevels - 1 : 0), /* 0 - (levels-1) */
4182 TRUE, /* lockable */
4183 &pRenderTarget->u.pSurface,
4184 NULL);
4185 AssertReturn(hr == D3D_OK, VERR_INTERNAL_ERROR);
4186
4187 pRenderTarget->idAssociatedContext = cid;
4188 pRenderTarget->enmD3DResType = VMSVGA3D_D3DRESTYPE_SURFACE;
4189 }
4190 else
4191 AssertReturn(pRenderTarget->fUsageD3D & D3DUSAGE_RENDERTARGET, VERR_INVALID_PARAMETER);
4192
4193 Assert(pRenderTarget->idAssociatedContext == cid);
4194 AssertMsgReturn(pRenderTarget->enmD3DResType == VMSVGA3D_D3DRESTYPE_SURFACE,
4195 ("Invalid render target %#x\n", pRenderTarget->enmD3DResType),
4196 VERR_INVALID_PARAMETER);
4197 pSurface = pRenderTarget->u.pSurface;
4198 }
4199
4200 AssertReturn(pSurface, VERR_INVALID_PARAMETER);
4201 Assert(!pRenderTarget->fDirty);
4202
4203 pRenderTarget->fUsageD3D |= D3DUSAGE_RENDERTARGET;
4204 pRenderTarget->f.surfaceFlags |= SVGA3D_SURFACE_HINT_RENDERTARGET;
4205
4206 hr = pContext->pDevice->SetRenderTarget(type - SVGA3D_RT_COLOR0, pSurface);
4207 if (fTexture)
4208 D3D_RELEASE(pSurface); /* Release reference to texture level 0 */
4209 AssertMsgReturn(hr == D3D_OK, ("SetRenderTarget failed with %x\n", hr), VERR_INTERNAL_ERROR);
4210
4211 /* Changing the render target resets the viewport; restore it here. */
4212 if (pContext->state.u32UpdateFlags & VMSVGA3D_UPDATE_VIEWPORT)
4213 vmsvga3dBackSetViewPort(pThisCC, cid, &pContext->state.RectViewPort);
4214 if (pContext->state.u32UpdateFlags & VMSVGA3D_UPDATE_ZRANGE)
4215 vmsvga3dBackSetZRange(pThisCC, cid, pContext->state.zRange);
4216 /* Changing the render target also resets the scissor rectangle; restore it as well. */
4217 if (pContext->state.u32UpdateFlags & VMSVGA3D_UPDATE_SCISSORRECT)
4218 vmsvga3dBackSetScissorRect(pThisCC, cid, &pContext->state.RectScissor);
4219
4220 break;
4221 }
4222
4223 default:
4224 AssertFailedReturn(VERR_INVALID_PARAMETER);
4225 }
4226
4227 return VINF_SUCCESS;
4228}
4229
4230/**
4231 * Convert SVGA texture combiner value to its D3D equivalent
4232 */
4233static DWORD vmsvga3dTextureCombiner2D3D(uint32_t value)
4234{
4235 switch (value)
4236 {
4237 case SVGA3D_TC_DISABLE:
4238 return D3DTOP_DISABLE;
4239 case SVGA3D_TC_SELECTARG1:
4240 return D3DTOP_SELECTARG1;
4241 case SVGA3D_TC_SELECTARG2:
4242 return D3DTOP_SELECTARG2;
4243 case SVGA3D_TC_MODULATE:
4244 return D3DTOP_MODULATE;
4245 case SVGA3D_TC_ADD:
4246 return D3DTOP_ADD;
4247 case SVGA3D_TC_ADDSIGNED:
4248 return D3DTOP_ADDSIGNED;
4249 case SVGA3D_TC_SUBTRACT:
4250 return D3DTOP_SUBTRACT;
4251 case SVGA3D_TC_BLENDTEXTUREALPHA:
4252 return D3DTOP_BLENDTEXTUREALPHA;
4253 case SVGA3D_TC_BLENDDIFFUSEALPHA:
4254 return D3DTOP_BLENDDIFFUSEALPHA;
4255 case SVGA3D_TC_BLENDCURRENTALPHA:
4256 return D3DTOP_BLENDCURRENTALPHA;
4257 case SVGA3D_TC_BLENDFACTORALPHA:
4258 return D3DTOP_BLENDFACTORALPHA;
4259 case SVGA3D_TC_MODULATE2X:
4260 return D3DTOP_MODULATE2X;
4261 case SVGA3D_TC_MODULATE4X:
4262 return D3DTOP_MODULATE4X;
4263 case SVGA3D_TC_DSDT:
4264 AssertFailed(); /** @todo ??? */
4265 return D3DTOP_DISABLE;
4266 case SVGA3D_TC_DOTPRODUCT3:
4267 return D3DTOP_DOTPRODUCT3;
4268 case SVGA3D_TC_BLENDTEXTUREALPHAPM:
4269 return D3DTOP_BLENDTEXTUREALPHAPM;
4270 case SVGA3D_TC_ADDSIGNED2X:
4271 return D3DTOP_ADDSIGNED2X;
4272 case SVGA3D_TC_ADDSMOOTH:
4273 return D3DTOP_ADDSMOOTH;
4274 case SVGA3D_TC_PREMODULATE:
4275 return D3DTOP_PREMODULATE;
4276 case SVGA3D_TC_MODULATEALPHA_ADDCOLOR:
4277 return D3DTOP_MODULATEALPHA_ADDCOLOR;
4278 case SVGA3D_TC_MODULATECOLOR_ADDALPHA:
4279 return D3DTOP_MODULATECOLOR_ADDALPHA;
4280 case SVGA3D_TC_MODULATEINVALPHA_ADDCOLOR:
4281 return D3DTOP_MODULATEINVALPHA_ADDCOLOR;
4282 case SVGA3D_TC_MODULATEINVCOLOR_ADDALPHA:
4283 return D3DTOP_MODULATEINVCOLOR_ADDALPHA;
4284 case SVGA3D_TC_BUMPENVMAPLUMINANCE:
4285 return D3DTOP_BUMPENVMAPLUMINANCE;
4286 case SVGA3D_TC_MULTIPLYADD:
4287 return D3DTOP_MULTIPLYADD;
4288 case SVGA3D_TC_LERP:
4289 return D3DTOP_LERP;
4290 default:
4291 AssertFailed();
4292 return D3DTOP_DISABLE;
4293 }
4294}
4295
4296/**
4297 * Convert SVGA texture arg data value to its D3D equivalent
4298 */
4299static DWORD vmsvga3dTextureArgData2D3D(uint32_t value)
4300{
4301 switch (value)
4302 {
4303 case SVGA3D_TA_CONSTANT:
4304 return D3DTA_CONSTANT;
4305 case SVGA3D_TA_PREVIOUS:
4306 return D3DTA_CURRENT; /* current = previous */
4307 case SVGA3D_TA_DIFFUSE:
4308 return D3DTA_DIFFUSE;
4309 case SVGA3D_TA_TEXTURE:
4310 return D3DTA_TEXTURE;
4311 case SVGA3D_TA_SPECULAR:
4312 return D3DTA_SPECULAR;
4313 default:
4314 AssertFailed();
4315 return D3DTA_DIFFUSE;
4316 }
4317}
4318
4319/**
4320 * Convert SVGA texture transform flag value to its D3D equivalent
4321 */
4322static DWORD vmsvga3dTextTransformFlags2D3D(uint32_t value)
4323{
4324 switch (value)
4325 {
4326 case SVGA3D_TEX_TRANSFORM_OFF:
4327 return D3DTTFF_DISABLE;
4328 case SVGA3D_TEX_TRANSFORM_S:
4329 return D3DTTFF_COUNT1; /** @todo correct? */
4330 case SVGA3D_TEX_TRANSFORM_T:
4331 return D3DTTFF_COUNT2; /** @todo correct? */
4332 case SVGA3D_TEX_TRANSFORM_R:
4333 return D3DTTFF_COUNT3; /** @todo correct? */
4334 case SVGA3D_TEX_TRANSFORM_Q:
4335 return D3DTTFF_COUNT4; /** @todo correct? */
4336 case SVGA3D_TEX_PROJECTED:
4337 return D3DTTFF_PROJECTED;
4338 default:
4339 AssertFailed();
4340 return D3DTTFF_DISABLE;
4341 }
4342}
4343
4344static DWORD vmsvga3dSamplerIndex2D3D(uint32_t idxSampler)
4345{
4346 if (idxSampler < SVGA3D_MAX_SAMPLERS_PS)
4347 return idxSampler;
4348 return (idxSampler - SVGA3D_MAX_SAMPLERS_PS) + D3DDMAPSAMPLER;
4349}
4350
4351static DECLCALLBACK(int) vmsvga3dBackSetTextureState(PVGASTATECC pThisCC, uint32_t cid, uint32_t cTextureStates, SVGA3dTextureState *pTextureState)
4352{
4353 DWORD val = 0; /* Shut up MSC */
4354 HRESULT hr;
4355 PVMSVGA3DCONTEXT pContext;
4356 PVMSVGA3DSTATE pState = pThisCC->svga.p3dState;
4357 AssertReturn(pState, VERR_NO_MEMORY);
4358
4359 LogFunc(("%x cTextureState=%d\n", cid, cTextureStates));
4360
4361 int rc = vmsvga3dContextFromCid(pState, cid, &pContext);
4362 AssertRCReturn(rc, rc);
4363 AssertReturn(pContext->pDevice, VERR_INTERNAL_ERROR);
4364
4365 for (unsigned i = 0; i < cTextureStates; i++)
4366 {
4367 LogFunc(("cid=%u stage=%d type=%s (%x) val=%x\n", cid, pTextureState[i].stage, vmsvga3dTextureStateToString(pTextureState[i].name), pTextureState[i].name, pTextureState[i].value));
4368
4369 if (pTextureState[i].name == SVGA3D_TS_BIND_TEXTURE)
4370 {
4371 /* Special case: binding a texture to a sampler. Stage is the sampler index. */
4372 const uint32_t sid = pTextureState[i].value;
4373 const uint32_t idxSampler = pTextureState[i].stage;
4374
4375 if (RT_UNLIKELY(idxSampler >= SVGA3D_MAX_SAMPLERS))
4376 {
4377 AssertMsgFailed(("pTextureState[%d]: SVGA3D_TS_BIND_TEXTURE idxSampler=%d, sid=%u\n", i, idxSampler, sid));
4378 continue;
4379 }
4380
4381 const DWORD d3dSampler = vmsvga3dSamplerIndex2D3D(idxSampler);
4382 if (sid == SVGA3D_INVALID_ID)
4383 {
4384 LogFunc(("SVGA3D_TS_BIND_TEXTURE: unbind sampler=%d\n", idxSampler));
4385
4386 pContext->aSidActiveTextures[idxSampler] = SVGA3D_INVALID_ID;
4387
4388 /* Unselect the currently associated texture. */
4389 hr = pContext->pDevice->SetTexture(d3dSampler, NULL);
4390 AssertMsgReturn(hr == D3D_OK, ("SetTexture failed with %x\n", hr), VERR_INTERNAL_ERROR);
4391 }
4392 else
4393 {
4394 PVMSVGA3DSURFACE pSurface;
4395 rc = vmsvga3dSurfaceFromSid(pState, sid, &pSurface);
4396 AssertRCReturn(rc, rc);
4397
4398 LogFunc(("SVGA3D_TS_BIND_TEXTURE: bind idxSampler=%d, texture sid=%u (%d,%d)\n", idxSampler, sid, pSurface->paMipmapLevels[0].mipmapSize.width, pSurface->paMipmapLevels[0].mipmapSize.height));
4399
4400 if (!pSurface->u.pTexture)
4401 {
4402 Assert(pSurface->idAssociatedContext == SVGA3D_INVALID_ID);
4403 LogFunc(("CreateTexture (%d,%d) level=%d fUsage=%x format=%x\n", pSurface->paMipmapLevels[0].mipmapSize.width, pSurface->paMipmapLevels[0].mipmapSize.height, pSurface->cLevels, pSurface->fUsageD3D, pSurface->formatD3D));
4404 rc = vmsvga3dBackCreateTexture(pThisCC, pContext, cid, pSurface);
4405 AssertRCReturn(rc, rc);
4406 }
4407 else
4408 {
4409 /* Must flush the other context's 3d pipeline to make sure all drawing is complete for the surface we're about to use. */
4410 vmsvga3dSurfaceFlush(pSurface);
4411 }
4412
4413 AssertReturn( pSurface->enmD3DResType == VMSVGA3D_D3DRESTYPE_TEXTURE
4414 || pSurface->enmD3DResType == VMSVGA3D_D3DRESTYPE_CUBE_TEXTURE
4415 || pSurface->enmD3DResType == VMSVGA3D_D3DRESTYPE_VOLUME_TEXTURE,
4416 VERR_INVALID_PARAMETER);
4417
4418 if (pSurface->idAssociatedContext != cid)
4419 {
4420 LogFunc(("Using texture sid=%u created for another context (%d vs %d)\n", sid, pSurface->idAssociatedContext, cid));
4421
4422 PVMSVGA3DSHAREDSURFACE pSharedSurface = vmsvga3dSurfaceGetSharedCopy(pState, pContext, pSurface);
4423 AssertReturn(pSharedSurface, VERR_INTERNAL_ERROR);
4424
4425 hr = pContext->pDevice->SetTexture(d3dSampler, pSharedSurface->u.pTexture);
4426 }
4427 else
4428 hr = pContext->pDevice->SetTexture(d3dSampler, pSurface->u.pTexture);
4429
4430 AssertMsgReturn(hr == D3D_OK, ("SetTexture failed with %x\n", hr), VERR_INTERNAL_ERROR);
4431
4432 pContext->aSidActiveTextures[idxSampler] = sid;
4433 }
4434 /* Finished; continue with the next one. */
4435 continue;
4436 }
4437
4438 D3DTEXTURESTAGESTATETYPE textureType = D3DTSS_FORCE_DWORD;
4439 D3DSAMPLERSTATETYPE samplerType = D3DSAMP_FORCE_DWORD;
4440 switch (pTextureState[i].name)
4441 {
4442 case SVGA3D_TS_COLOROP: /* SVGA3dTextureCombiner */
4443 textureType = D3DTSS_COLOROP;
4444 val = vmsvga3dTextureCombiner2D3D(pTextureState[i].value);
4445 break;
4446
4447 case SVGA3D_TS_COLORARG0: /* SVGA3dTextureArgData */
4448 textureType = D3DTSS_COLORARG0;
4449 val = vmsvga3dTextureArgData2D3D(pTextureState[i].value);
4450 break;
4451
4452 case SVGA3D_TS_COLORARG1: /* SVGA3dTextureArgData */
4453 textureType = D3DTSS_COLORARG1;
4454 val = vmsvga3dTextureArgData2D3D(pTextureState[i].value);
4455 break;
4456
4457 case SVGA3D_TS_COLORARG2: /* SVGA3dTextureArgData */
4458 textureType = D3DTSS_COLORARG2;
4459 val = vmsvga3dTextureArgData2D3D(pTextureState[i].value);
4460 break;
4461
4462 case SVGA3D_TS_ALPHAOP: /* SVGA3dTextureCombiner */
4463 textureType = D3DTSS_ALPHAOP;
4464 val = vmsvga3dTextureCombiner2D3D(pTextureState[i].value);
4465 break;
4466
4467 case SVGA3D_TS_ALPHAARG0: /* SVGA3dTextureArgData */
4468 textureType = D3DTSS_ALPHAARG0;
4469 val = vmsvga3dTextureArgData2D3D(pTextureState[i].value);
4470 break;
4471
4472 case SVGA3D_TS_ALPHAARG1: /* SVGA3dTextureArgData */
4473 textureType = D3DTSS_ALPHAARG1;
4474 val = vmsvga3dTextureArgData2D3D(pTextureState[i].value);
4475 break;
4476
4477 case SVGA3D_TS_ALPHAARG2: /* SVGA3dTextureArgData */
4478 textureType = D3DTSS_ALPHAARG2;
4479 val = vmsvga3dTextureArgData2D3D(pTextureState[i].value);
4480 break;
4481
4482 case SVGA3D_TS_BUMPENVMAT00: /* float */
4483 textureType = D3DTSS_BUMPENVMAT00;
4484 val = pTextureState[i].value;
4485 break;
4486
4487 case SVGA3D_TS_BUMPENVMAT01: /* float */
4488 textureType = D3DTSS_BUMPENVMAT01;
4489 val = pTextureState[i].value;
4490 break;
4491
4492 case SVGA3D_TS_BUMPENVMAT10: /* float */
4493 textureType = D3DTSS_BUMPENVMAT10;
4494 val = pTextureState[i].value;
4495 break;
4496
4497 case SVGA3D_TS_BUMPENVMAT11: /* float */
4498 textureType = D3DTSS_BUMPENVMAT11;
4499 val = pTextureState[i].value;
4500 break;
4501
4502 case SVGA3D_TS_TEXCOORDINDEX: /* uint32_t */
4503 textureType = D3DTSS_TEXCOORDINDEX;
4504 val = pTextureState[i].value;
4505 break;
4506
4507 case SVGA3D_TS_BUMPENVLSCALE: /* float */
4508 textureType = D3DTSS_BUMPENVLSCALE;
4509 val = pTextureState[i].value;
4510 break;
4511
4512 case SVGA3D_TS_BUMPENVLOFFSET: /* float */
4513 textureType = D3DTSS_BUMPENVLOFFSET;
4514 val = pTextureState[i].value;
4515 break;
4516
4517 case SVGA3D_TS_TEXTURETRANSFORMFLAGS: /* SVGA3dTexTransformFlags */
4518 textureType = D3DTSS_TEXTURETRANSFORMFLAGS;
4519 val = vmsvga3dTextTransformFlags2D3D(pTextureState[i].value);
4520 break;
4521
4522 case SVGA3D_TS_ADDRESSW: /* SVGA3dTextureAddress */
4523 samplerType = D3DSAMP_ADDRESSW;
4524 val = pTextureState[i].value; /* Identical otherwise */
4525 Assert(pTextureState[i].value != SVGA3D_TEX_ADDRESS_EDGE);
4526 break;
4527
4528 case SVGA3D_TS_ADDRESSU: /* SVGA3dTextureAddress */
4529 samplerType = D3DSAMP_ADDRESSU;
4530 val = pTextureState[i].value; /* Identical otherwise */
4531 Assert(pTextureState[i].value != SVGA3D_TEX_ADDRESS_EDGE);
4532 break;
4533
4534 case SVGA3D_TS_ADDRESSV: /* SVGA3dTextureAddress */
4535 samplerType = D3DSAMP_ADDRESSV;
4536 val = pTextureState[i].value; /* Identical otherwise */
4537 Assert(pTextureState[i].value != SVGA3D_TEX_ADDRESS_EDGE);
4538 break;
4539
4540 case SVGA3D_TS_MIPFILTER: /* SVGA3dTextureFilter */
4541 samplerType = D3DSAMP_MIPFILTER;
4542 val = pTextureState[i].value; /* Identical otherwise */
4543 Assert(pTextureState[i].value != SVGA3D_TEX_FILTER_FLATCUBIC);
4544 Assert(pTextureState[i].value != SVGA3D_TEX_FILTER_GAUSSIANCUBIC);
4545 break;
4546
4547 case SVGA3D_TS_MAGFILTER: /* SVGA3dTextureFilter */
4548 samplerType = D3DSAMP_MAGFILTER;
4549 val = pTextureState[i].value; /* Identical otherwise */
4550 Assert(pTextureState[i].value != SVGA3D_TEX_FILTER_FLATCUBIC);
4551 Assert(pTextureState[i].value != SVGA3D_TEX_FILTER_GAUSSIANCUBIC);
4552 break;
4553
4554 case SVGA3D_TS_MINFILTER: /* SVGA3dTextureFilter */
4555 samplerType = D3DSAMP_MINFILTER;
4556 val = pTextureState[i].value; /* Identical otherwise */
4557 Assert(pTextureState[i].value != SVGA3D_TEX_FILTER_FLATCUBIC);
4558 Assert(pTextureState[i].value != SVGA3D_TEX_FILTER_GAUSSIANCUBIC);
4559 break;
4560
4561 case SVGA3D_TS_BORDERCOLOR: /* SVGA3dColor */
4562 samplerType = D3DSAMP_BORDERCOLOR;
4563 val = pTextureState[i].value; /* Identical */
4564 break;
4565
4566 case SVGA3D_TS_TEXTURE_LOD_BIAS: /* float */
4567 samplerType = D3DSAMP_MIPMAPLODBIAS;
4568 val = pTextureState[i].value; /* Identical */
4569 break;
4570
4571 case SVGA3D_TS_TEXTURE_MIPMAP_LEVEL: /* uint32_t */
4572 samplerType = D3DSAMP_MAXMIPLEVEL;
4573 val = pTextureState[i].value; /* Identical?? */
4574 break;
4575
4576 case SVGA3D_TS_TEXTURE_ANISOTROPIC_LEVEL: /* uint32_t */
4577 samplerType = D3DSAMP_MAXANISOTROPY;
4578 val = pTextureState[i].value; /* Identical?? */
4579 break;
4580
4581 case SVGA3D_TS_GAMMA: /* float */
4582 samplerType = D3DSAMP_SRGBTEXTURE;
4583 /* Boolean in D3D */
4584 if (pTextureState[i].floatValue == 1.0f)
4585 val = FALSE;
4586 else
4587 val = TRUE;
4588 break;
4589
4590 /* Internal commands, that don't map directly to the SetTextureStageState API. */
4591 case SVGA3D_TS_TEXCOORDGEN: /* SVGA3dTextureCoordGen */
4592 AssertFailed();
4593 break;
4594
4595 case SVGA3D_TS_MAX: /* shut up MSC */
4596 case SVGA3D_TS_INVALID:
4597 case SVGA3D_TS_BIND_TEXTURE:
4598 AssertFailedBreak();
4599 default: /** @todo Remaining TSs. Avoid MSC warning for now. */
4600 break;
4601 }
4602
4603 const uint32_t currentStage = pTextureState[i].stage;
4604 /* Record the texture state for vm state saving. */
4605 if ( currentStage < RT_ELEMENTS(pContext->state.aTextureStates)
4606 && (unsigned)pTextureState[i].name < RT_ELEMENTS(pContext->state.aTextureStates[0]))
4607 {
4608 pContext->state.aTextureStates[currentStage][pTextureState[i].name] = pTextureState[i];
4609 }
4610
4611 if (textureType != D3DTSS_FORCE_DWORD)
4612 {
4613 if (RT_UNLIKELY(currentStage >= SVGA3D_MAX_TEXTURE_STAGES))
4614 {
4615 AssertMsgFailed(("pTextureState[%d].stage=%#x name=%#x value=%#x\n", i, pTextureState[i].stage, pTextureState[i].name, pTextureState[i].value));
4616 continue;
4617 }
4618
4619 hr = pContext->pDevice->SetTextureStageState(currentStage, textureType, val);
4620 AssertMsg(hr == D3D_OK, ("SetTextureStageState failed with %x\n", hr));
4621 }
4622 else if (samplerType != D3DSAMP_FORCE_DWORD)
4623 {
4624 if (RT_UNLIKELY(currentStage >= SVGA3D_MAX_SAMPLERS))
4625 {
4626 AssertMsgFailed(("pTextureState[%d].stage=%#x name=%#x value=%#x\n", i, pTextureState[i].stage, pTextureState[i].name, pTextureState[i].value));
4627 continue;
4628 }
4629
4630 hr = pContext->pDevice->SetSamplerState(currentStage, samplerType, val);
4631 AssertMsg(hr == D3D_OK, ("SetSamplerState failed with %x\n", hr));
4632 }
4633 else
4634 {
4635 AssertFailed();
4636 }
4637 }
4638
4639 return VINF_SUCCESS;
4640}
4641
4642static DECLCALLBACK(int) vmsvga3dBackSetMaterial(PVGASTATECC pThisCC, uint32_t cid, SVGA3dFace face, SVGA3dMaterial *pMaterial)
4643{
4644 HRESULT hr;
4645 PVMSVGA3DSTATE pState = pThisCC->svga.p3dState;
4646 AssertReturn(pState, VERR_NO_MEMORY);
4647
4648 LogFunc(("cid=%u face %d\n", cid, face));
4649
4650 PVMSVGA3DCONTEXT pContext;
4651 int rc = vmsvga3dContextFromCid(pState, cid, &pContext);
4652 AssertRCReturn(rc, rc);
4653 AssertReturn(pContext->pDevice, VERR_INTERNAL_ERROR);
4654
4655 AssertReturn((unsigned)face < SVGA3D_FACE_MAX, VERR_INVALID_PARAMETER);
4656
4657 /* Save for vm state save/restore. */
4658 pContext->state.aMaterial[face].fValid = true;
4659 pContext->state.aMaterial[face].material = *pMaterial;
4660 pContext->state.u32UpdateFlags |= VMSVGA3D_UPDATE_MATERIAL;
4661
4662 /* @note face not used for D3D9 */
4663 /** @todo ignore everything except SVGA3D_FACE_NONE? */
4664 //Assert(face == SVGA3D_FACE_NONE);
4665 if (face != SVGA3D_FACE_NONE)
4666 Log(("Unsupported face %d!!\n", face));
4667
4668 D3DMATERIAL9 material;
4669 material.Diffuse.r = pMaterial->diffuse[0];
4670 material.Diffuse.g = pMaterial->diffuse[1];
4671 material.Diffuse.b = pMaterial->diffuse[2];
4672 material.Diffuse.a = pMaterial->diffuse[3];
4673 material.Ambient.r = pMaterial->ambient[0];
4674 material.Ambient.g = pMaterial->ambient[1];
4675 material.Ambient.b = pMaterial->ambient[2];
4676 material.Ambient.a = pMaterial->ambient[3];
4677 material.Specular.r = pMaterial->specular[0];
4678 material.Specular.g = pMaterial->specular[1];
4679 material.Specular.b = pMaterial->specular[2];
4680 material.Specular.a = pMaterial->specular[3];
4681 material.Emissive.r = pMaterial->emissive[0];
4682 material.Emissive.g = pMaterial->emissive[1];
4683 material.Emissive.b = pMaterial->emissive[2];
4684 material.Emissive.a = pMaterial->emissive[3];
4685 material.Power = pMaterial->shininess;
4686
4687 hr = pContext->pDevice->SetMaterial(&material);
4688 AssertMsgReturn(hr == D3D_OK, ("SetMaterial failed with %x\n", hr), VERR_INTERNAL_ERROR);
4689
4690 return VINF_SUCCESS;
4691}
4692
4693static DECLCALLBACK(int) vmsvga3dBackSetLightData(PVGASTATECC pThisCC, uint32_t cid, uint32_t index, SVGA3dLightData *pData)
4694{
4695 HRESULT hr;
4696 D3DLIGHT9 light;
4697 PVMSVGA3DCONTEXT pContext;
4698 PVMSVGA3DSTATE pState = pThisCC->svga.p3dState;
4699 AssertReturn(pState, VERR_NO_MEMORY);
4700
4701 Log(("vmsvga3dSetLightData %x index=%d\n", cid, index));
4702 ASSERT_GUEST_RETURN(index < SVGA3D_MAX_LIGHTS, VERR_INVALID_PARAMETER);
4703
4704 int rc = vmsvga3dContextFromCid(pState, cid, &pContext);
4705 AssertRCReturn(rc, rc);
4706 AssertReturn(pContext->pDevice, VERR_INTERNAL_ERROR);
4707
4708 switch (pData->type)
4709 {
4710 case SVGA3D_LIGHTTYPE_POINT:
4711 light.Type = D3DLIGHT_POINT;
4712 break;
4713
4714 case SVGA3D_LIGHTTYPE_SPOT1: /* 1-cone, in degrees */
4715 light.Type = D3DLIGHT_SPOT;
4716 break;
4717
4718 case SVGA3D_LIGHTTYPE_DIRECTIONAL:
4719 light.Type = D3DLIGHT_DIRECTIONAL;
4720 break;
4721
4722 case SVGA3D_LIGHTTYPE_SPOT2: /* 2-cone, in radians */
4723 default:
4724 Log(("Unsupported light type!!\n"));
4725 return VERR_INVALID_PARAMETER;
4726 }
4727
4728 /* Store for vm state save/restore */
4729 pContext->state.aLightData[index].fValidData = true;
4730 pContext->state.aLightData[index].data = *pData;
4731
4732 light.Diffuse.r = pData->diffuse[0];
4733 light.Diffuse.g = pData->diffuse[1];
4734 light.Diffuse.b = pData->diffuse[2];
4735 light.Diffuse.a = pData->diffuse[3];
4736 light.Specular.r = pData->specular[0];
4737 light.Specular.g = pData->specular[1];
4738 light.Specular.b = pData->specular[2];
4739 light.Specular.a = pData->specular[3];
4740 light.Ambient.r = pData->ambient[0];
4741 light.Ambient.g = pData->ambient[1];
4742 light.Ambient.b = pData->ambient[2];
4743 light.Ambient.a = pData->ambient[3];
4744 light.Position.x = pData->position[0];
4745 light.Position.y = pData->position[1];
4746 light.Position.z = pData->position[2]; /* @note 4th position not available in D3D9 */
4747 light.Direction.x = pData->direction[0];
4748 light.Direction.y = pData->direction[1];
4749 light.Direction.z = pData->direction[2]; /* @note 4th position not available in D3D9 */
4750 light.Range = pData->range;
4751 light.Falloff = pData->falloff;
4752 light.Attenuation0 = pData->attenuation0;
4753 light.Attenuation1 = pData->attenuation1;
4754 light.Attenuation2 = pData->attenuation2;
4755 light.Theta = pData->theta;
4756 light.Phi = pData->phi;
4757
4758 hr = pContext->pDevice->SetLight(index, &light);
4759 AssertMsgReturn(hr == D3D_OK, ("SetLight failed with %x\n", hr), VERR_INTERNAL_ERROR);
4760
4761 return VINF_SUCCESS;
4762}
4763
4764static DECLCALLBACK(int) vmsvga3dBackSetLightEnabled(PVGASTATECC pThisCC, uint32_t cid, uint32_t index, uint32_t enabled)
4765{
4766 HRESULT hr;
4767 PVMSVGA3DCONTEXT pContext;
4768 PVMSVGA3DSTATE pState = pThisCC->svga.p3dState;
4769 AssertReturn(pState, VERR_NO_MEMORY);
4770
4771 Log(("vmsvga3dSetLightEnabled %x %d -> %d\n", cid, index, enabled));
4772 AssertReturn(index < SVGA3D_MAX_LIGHTS, VERR_INVALID_PARAMETER);
4773
4774 int rc = vmsvga3dContextFromCid(pState, cid, &pContext);
4775 AssertRCReturn(rc, rc);
4776 AssertReturn(pContext->pDevice, VERR_INTERNAL_ERROR);
4777
4778 /* Store for vm state save/restore */
4779 pContext->state.aLightData[index].fEnabled = !!enabled;
4780
4781 hr = pContext->pDevice->LightEnable(index, (BOOL)enabled);
4782 AssertMsgReturn(hr == D3D_OK, ("LightEnable failed with %x\n", hr), VERR_INTERNAL_ERROR);
4783
4784 return VINF_SUCCESS;
4785}
4786
4787static DECLCALLBACK(int) vmsvga3dBackSetViewPort(PVGASTATECC pThisCC, uint32_t cid, SVGA3dRect *pRect)
4788{
4789 HRESULT hr;
4790 D3DVIEWPORT9 viewPort;
4791 PVMSVGA3DCONTEXT pContext;
4792 PVMSVGA3DSTATE pState = pThisCC->svga.p3dState;
4793 AssertReturn(pState, VERR_NO_MEMORY);
4794
4795 Log(("vmsvga3dSetViewPort %x (%d,%d)(%d,%d)\n", cid, pRect->x, pRect->y, pRect->w, pRect->h));
4796
4797 int rc = vmsvga3dContextFromCid(pState, cid, &pContext);
4798 AssertRCReturn(rc, rc);
4799 AssertReturn(pContext->pDevice, VERR_INTERNAL_ERROR);
4800
4801 /* Save for vm state save/restore. */
4802 pContext->state.RectViewPort = *pRect;
4803 pContext->state.u32UpdateFlags |= VMSVGA3D_UPDATE_VIEWPORT;
4804
4805 hr = pContext->pDevice->GetViewport(&viewPort);
4806 AssertMsgReturn(hr == D3D_OK, ("GetViewport failed with %x\n", hr), VERR_INTERNAL_ERROR);
4807
4808 viewPort.X = pRect->x;
4809 viewPort.Y = pRect->y;
4810 viewPort.Width = pRect->w;
4811 viewPort.Height = pRect->h;
4812 /* viewPort.MinZ & MaxZ are not changed from the current setting. */
4813
4814 hr = pContext->pDevice->SetViewport(&viewPort);
4815 AssertMsgReturn(hr == D3D_OK, ("SetViewport failed with %x\n", hr), VERR_INTERNAL_ERROR);
4816
4817 return VINF_SUCCESS;
4818}
4819
4820static DECLCALLBACK(int) vmsvga3dBackSetClipPlane(PVGASTATECC pThisCC, uint32_t cid, uint32_t index, float plane[4])
4821{
4822 HRESULT hr;
4823 PVMSVGA3DCONTEXT pContext;
4824 PVMSVGA3DSTATE pState = pThisCC->svga.p3dState;
4825 AssertReturn(pState, VERR_NO_MEMORY);
4826
4827 Log(("vmsvga3dSetClipPlane %x %d (%d,%d)(%d,%d)\n", cid, index, (unsigned)(plane[0] * 100.0), (unsigned)(plane[1] * 100.0), (unsigned)(plane[2] * 100.0), (unsigned)(plane[3] * 100.0)));
4828 AssertReturn(index < SVGA3D_NUM_CLIPPLANES, VERR_INVALID_PARAMETER);
4829
4830 int rc = vmsvga3dContextFromCid(pState, cid, &pContext);
4831 AssertRCReturn(rc, rc);
4832 AssertReturn(pContext->pDevice, VERR_INTERNAL_ERROR);
4833
4834 /* Store for vm state save/restore. */
4835 pContext->state.aClipPlane[index].fValid = true;
4836 memcpy(pContext->state.aClipPlane[index].plane, plane, sizeof(pContext->state.aClipPlane[index].plane));
4837
4838 hr = pContext->pDevice->SetClipPlane(index, plane);
4839 AssertMsgReturn(hr == D3D_OK, ("SetClipPlane failed with %x\n", hr), VERR_INTERNAL_ERROR);
4840 return VINF_SUCCESS;
4841}
4842
4843static DECLCALLBACK(int) vmsvga3dBackCommandClear(PVGASTATECC pThisCC, uint32_t cid, SVGA3dClearFlag clearFlag, uint32_t color, float depth,
4844 uint32_t stencil, uint32_t cRects, SVGA3dRect *pRect)
4845{
4846 /* From SVGA3D_BeginClear comments:
4847 *
4848 * Clear is not affected by clipping, depth test, or other
4849 * render state which affects the fragment pipeline.
4850 *
4851 * Therefore this code must ignore the current scissor rect.
4852 */
4853
4854 DWORD clearFlagD3D = 0;
4855 D3DRECT *pRectD3D = NULL;
4856 HRESULT hr;
4857 PVMSVGA3DCONTEXT pContext;
4858 PVMSVGA3DSTATE pState = pThisCC->svga.p3dState;
4859 AssertReturn(pState, VERR_NO_MEMORY);
4860
4861 Log(("vmsvga3dCommandClear %x clearFlag=%x color=%x depth=%d stencil=%x cRects=%d\n", cid, clearFlag, color, (uint32_t)(depth * 100.0), stencil, cRects));
4862
4863 int rc = vmsvga3dContextFromCid(pState, cid, &pContext);
4864 AssertRCReturn(rc, rc);
4865 AssertReturn(pContext->pDevice, VERR_INTERNAL_ERROR);
4866
4867 PVMSVGA3DSURFACE pRT;
4868 rc = vmsvga3dSurfaceFromSid(pState, pContext->state.aRenderTargets[SVGA3D_RT_COLOR0], &pRT);
4869 AssertRCReturn(rc, rc);
4870
4871 if (clearFlag & SVGA3D_CLEAR_COLOR)
4872 clearFlagD3D |= D3DCLEAR_TARGET;
4873 if (clearFlag & SVGA3D_CLEAR_STENCIL)
4874 clearFlagD3D |= D3DCLEAR_STENCIL;
4875 if (clearFlag & SVGA3D_CLEAR_DEPTH)
4876 clearFlagD3D |= D3DCLEAR_ZBUFFER;
4877
4878 if (cRects)
4879 {
4880 pRectD3D = (D3DRECT *)RTMemAlloc(sizeof(D3DRECT) * cRects);
4881 AssertReturn(pRectD3D, VERR_NO_MEMORY);
4882
4883 for (unsigned i=0; i < cRects; i++)
4884 {
4885 Log(("vmsvga3dCommandClear: rect %d (%d,%d)(%d,%d)\n", i, pRect[i].x, pRect[i].y, pRect[i].x + pRect[i].w, pRect[i].y + pRect[i].h));
4886 pRectD3D[i].x1 = pRect[i].x;
4887 pRectD3D[i].y1 = pRect[i].y;
4888 pRectD3D[i].x2 = pRect[i].x + pRect[i].w; /* exclusive */
4889 pRectD3D[i].y2 = pRect[i].y + pRect[i].h; /* exclusive */
4890 }
4891 }
4892
4893 RECT currentScissorRect;
4894 pContext->pDevice->GetScissorRect(&currentScissorRect);
4895
4896 RECT clearScissorRect;
4897 clearScissorRect.left = 0;
4898 clearScissorRect.top = 0;
4899 clearScissorRect.right = pRT->paMipmapLevels[0].mipmapSize.width;
4900 clearScissorRect.bottom = pRT->paMipmapLevels[0].mipmapSize.height;
4901 pContext->pDevice->SetScissorRect(&clearScissorRect);
4902
4903 hr = pContext->pDevice->Clear(cRects, pRectD3D, clearFlagD3D, (D3DCOLOR)color, depth, stencil);
4904
4905 if (pRectD3D)
4906 RTMemFree(pRectD3D);
4907
4908 pContext->pDevice->SetScissorRect(&currentScissorRect);
4909
4910 AssertMsgReturn(hr == D3D_OK, ("Clear failed with %x\n", hr), VERR_INTERNAL_ERROR);
4911
4912 /* Make sure we can track drawing usage of active render targets. */
4913 for (uint32_t i = 0; i < RT_ELEMENTS(pContext->state.aRenderTargets); ++i)
4914 if (pContext->state.aRenderTargets[i] != SVGA3D_INVALID_ID)
4915 vmsvga3dSurfaceTrackUsageById(pState, pContext, pContext->state.aRenderTargets[i]);
4916
4917 return VINF_SUCCESS;
4918}
4919
4920/* Convert VMWare vertex declaration to its D3D equivalent. */
4921static int vmsvga3dVertexDecl2D3D(const SVGA3dVertexArrayIdentity &identity, D3DVERTEXELEMENT9 *pVertexElement)
4922{
4923 /* usage, method and type are identical; make sure. */
4924 AssertCompile(SVGA3D_DECLTYPE_FLOAT1 == D3DDECLTYPE_FLOAT1);
4925 AssertCompile(SVGA3D_DECLTYPE_FLOAT16_4 == D3DDECLTYPE_FLOAT16_4);
4926 AssertCompile(SVGA3D_DECLMETHOD_DEFAULT == D3DDECLMETHOD_DEFAULT);
4927 AssertCompile(SVGA3D_DECLMETHOD_LOOKUPPRESAMPLED == D3DDECLMETHOD_LOOKUPPRESAMPLED);
4928 AssertCompile(D3DDECLUSAGE_POSITION == SVGA3D_DECLUSAGE_POSITION);
4929 AssertCompile(D3DDECLUSAGE_SAMPLE == SVGA3D_DECLUSAGE_SAMPLE);
4930
4931 pVertexElement->Stream = 0;
4932 pVertexElement->Offset = 0;
4933 pVertexElement->Type = identity.type;
4934 pVertexElement->Method = identity.method;
4935 pVertexElement->Usage = identity.usage;
4936 pVertexElement->UsageIndex = identity.usageIndex;
4937 return VINF_SUCCESS;
4938}
4939
4940/* Convert VMWare primitive type to its D3D equivalent. */
4941static int vmsvga3dPrimitiveType2D3D(SVGA3dPrimitiveType PrimitiveType, D3DPRIMITIVETYPE *pPrimitiveTypeD3D)
4942{
4943 switch (PrimitiveType)
4944 {
4945 case SVGA3D_PRIMITIVE_TRIANGLELIST:
4946 *pPrimitiveTypeD3D = D3DPT_TRIANGLELIST;
4947 break;
4948 case SVGA3D_PRIMITIVE_POINTLIST:
4949 *pPrimitiveTypeD3D = D3DPT_POINTLIST;
4950 break;
4951 case SVGA3D_PRIMITIVE_LINELIST:
4952 *pPrimitiveTypeD3D = D3DPT_LINELIST;
4953 break;
4954 case SVGA3D_PRIMITIVE_LINESTRIP:
4955 *pPrimitiveTypeD3D = D3DPT_LINESTRIP;
4956 break;
4957 case SVGA3D_PRIMITIVE_TRIANGLESTRIP:
4958 *pPrimitiveTypeD3D = D3DPT_TRIANGLESTRIP;
4959 break;
4960 case SVGA3D_PRIMITIVE_TRIANGLEFAN:
4961 *pPrimitiveTypeD3D = D3DPT_TRIANGLEFAN;
4962 break;
4963 default:
4964 return VERR_INVALID_PARAMETER;
4965 }
4966 return VINF_SUCCESS;
4967}
4968
4969
4970static int vmsvga3dDrawPrimitivesSyncVertexBuffer(PVMSVGA3DCONTEXT pContext,
4971 PVMSVGA3DSURFACE pVertexSurface)
4972{
4973 HRESULT hr;
4974 if ( pVertexSurface->u.pSurface
4975 && pVertexSurface->enmD3DResType != VMSVGA3D_D3DRESTYPE_VERTEX_BUFFER)
4976 {
4977 /* The buffer object is not an vertex one. Recreate the D3D resource. */
4978 AssertReturn(pVertexSurface->enmD3DResType == VMSVGA3D_D3DRESTYPE_INDEX_BUFFER, VERR_INVALID_PARAMETER);
4979 D3D_RELEASE(pVertexSurface->u.pIndexBuffer);
4980 pVertexSurface->enmD3DResType = VMSVGA3D_D3DRESTYPE_NONE;
4981
4982 LogFunc(("index -> vertex buffer sid=%u\n", pVertexSurface->id));
4983 }
4984
4985 bool fSync = pVertexSurface->fDirty;
4986 if (!pVertexSurface->u.pVertexBuffer)
4987 {
4988 LogFunc(("Create vertex buffer sid=%u fDirty=%d\n", pVertexSurface->id, pVertexSurface->fDirty));
4989
4990 const DWORD Usage = D3DUSAGE_DYNAMIC | D3DUSAGE_WRITEONLY; /* possible severe performance penalty otherwise (according to d3d debug output */
4991 hr = pContext->pDevice->CreateVertexBuffer(pVertexSurface->paMipmapLevels[0].cbSurface,
4992 Usage,
4993 0, /* non-FVF */
4994 D3DPOOL_DEFAULT,
4995 &pVertexSurface->u.pVertexBuffer,
4996 NULL);
4997 AssertMsgReturn(hr == D3D_OK, ("CreateVertexBuffer failed with %x\n", hr), VERR_INTERNAL_ERROR);
4998
4999 pVertexSurface->enmD3DResType = VMSVGA3D_D3DRESTYPE_VERTEX_BUFFER;
5000 pVertexSurface->idAssociatedContext = pContext->id;
5001 pVertexSurface->f.surfaceFlags |= SVGA3D_SURFACE_HINT_VERTEXBUFFER;
5002 fSync = true;
5003 }
5004
5005 if (fSync)
5006 {
5007 LogFunc(("sync vertex buffer\n"));
5008 Assert(pVertexSurface->u.pVertexBuffer);
5009
5010 void *pvData;
5011 hr = pVertexSurface->u.pVertexBuffer->Lock(0, 0, &pvData, D3DLOCK_DISCARD);
5012 AssertMsgReturn(hr == D3D_OK, ("Lock vertex failed with %x\n", hr), VERR_INTERNAL_ERROR);
5013
5014 memcpy(pvData, pVertexSurface->paMipmapLevels[0].pSurfaceData, pVertexSurface->paMipmapLevels[0].cbSurface);
5015
5016 hr = pVertexSurface->u.pVertexBuffer->Unlock();
5017 AssertMsgReturn(hr == D3D_OK, ("Unlock vertex failed with %x\n", hr), VERR_INTERNAL_ERROR);
5018 }
5019
5020 return VINF_SUCCESS;
5021}
5022
5023
5024static int vmsvga3dDrawPrimitivesSyncIndexBuffer(PVMSVGA3DCONTEXT pContext,
5025 PVMSVGA3DSURFACE pIndexSurface,
5026 uint32_t indexWidth)
5027{
5028 HRESULT hr;
5029 if ( pIndexSurface->u.pSurface
5030 && pIndexSurface->enmD3DResType != VMSVGA3D_D3DRESTYPE_INDEX_BUFFER)
5031 {
5032 /* The buffer object is not an index one. Must recreate the D3D resource. */
5033 AssertReturn(pIndexSurface->enmD3DResType == VMSVGA3D_D3DRESTYPE_VERTEX_BUFFER, VERR_INVALID_PARAMETER);
5034 D3D_RELEASE(pIndexSurface->u.pVertexBuffer);
5035 pIndexSurface->enmD3DResType = VMSVGA3D_D3DRESTYPE_NONE;
5036
5037 LogFunc(("vertex -> index buffer sid=%u\n", pIndexSurface->id));
5038 }
5039
5040 bool fSync = pIndexSurface->fDirty;
5041 if (!pIndexSurface->u.pIndexBuffer)
5042 {
5043 LogFunc(("Create index buffer fDirty=%d\n", pIndexSurface->fDirty));
5044
5045 const DWORD Usage = D3DUSAGE_DYNAMIC | D3DUSAGE_WRITEONLY; /* possible severe performance penalty otherwise (according to d3d debug output */
5046 const D3DFORMAT Format = (indexWidth == sizeof(uint16_t)) ? D3DFMT_INDEX16 : D3DFMT_INDEX32;
5047 hr = pContext->pDevice->CreateIndexBuffer(pIndexSurface->paMipmapLevels[0].cbSurface,
5048 Usage,
5049 Format,
5050 D3DPOOL_DEFAULT,
5051 &pIndexSurface->u.pIndexBuffer,
5052 NULL);
5053 AssertMsgReturn(hr == D3D_OK, ("CreateIndexBuffer failed with %x\n", hr), VERR_INTERNAL_ERROR);
5054
5055 pIndexSurface->enmD3DResType = VMSVGA3D_D3DRESTYPE_INDEX_BUFFER;
5056 pIndexSurface->idAssociatedContext = pContext->id;
5057 pIndexSurface->f.surfaceFlags |= SVGA3D_SURFACE_HINT_INDEXBUFFER;
5058 fSync = true;