VirtualBox

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

Last change on this file since 89163 was 89163, checked in by vboxsync, 4 years ago

Devices/Graphics: Build new and old 3D backend. bugref:9830

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 268.0 KB
Line 
1/* $Id: DevVGA-SVGA3d-win.cpp 89163 2021-05-19 13:12:44Z vboxsync $ */
2/** @file
3 * DevVMWare - VMWare SVGA device
4 */
5
6/*
7 * Copyright (C) 2013-2020 Oracle Corporation
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.virtualbox.org. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 */
17
18
19/*********************************************************************************************************************************
20* Header Files *
21*********************************************************************************************************************************/
22#define LOG_GROUP LOG_GROUP_DEV_VMSVGA
23#include <VBox/vmm/pdmdev.h>
24#include <VBox/version.h>
25#include <VBox/err.h>
26#include <VBox/log.h>
27#include <VBox/vmm/pgm.h>
28#include <VBox/AssertGuest.h>
29
30#include <iprt/assert.h>
31#include <iprt/semaphore.h>
32#include <iprt/uuid.h>
33#include <iprt/mem.h>
34#include <iprt/avl.h>
35
36#include <VBoxVideo.h> /* required by DevVGA.h */
37
38/* should go BEFORE any other DevVGA include to make all DevVGA.h config defines be visible */
39#include "DevVGA.h"
40
41#include "DevVGA-SVGA.h"
42#include "DevVGA-SVGA3d.h"
43#include "DevVGA-SVGA3d-internal.h"
44
45/* Enable to disassemble defined shaders. */
46#if defined(DEBUG) && 0 /* Disabled as we don't have the DirectX SDK avaible atm. */
47#define DUMP_SHADER_DISASSEMBLY
48#endif
49
50#ifdef DUMP_SHADER_DISASSEMBLY
51#include <d3dx9shader.h>
52#endif
53
54
55/*********************************************************************************************************************************
56* Defined Constants And Macros *
57*********************************************************************************************************************************/
58
59#define FOURCC_INTZ (D3DFORMAT)MAKEFOURCC('I', 'N', 'T', 'Z')
60#define FOURCC_NULL (D3DFORMAT)MAKEFOURCC('N', 'U', 'L', 'L')
61
62
63/*********************************************************************************************************************************
64* Structures and Typedefs *
65*********************************************************************************************************************************/
66
67typedef struct
68{
69 DWORD Usage;
70 D3DRESOURCETYPE ResourceType;
71 SVGA3dFormatOp FormatOp;
72} VMSVGA3DFORMATSUPPORT;
73
74
75/*********************************************************************************************************************************
76* Global Variables *
77*********************************************************************************************************************************/
78static VMSVGA3DFORMATSUPPORT const g_aFormatSupport[] =
79{
80 {
81 0,
82 D3DRTYPE_SURFACE,
83 SVGA3DFORMAT_OP_OFFSCREENPLAIN,
84 },
85 {
86 D3DUSAGE_RENDERTARGET,
87 D3DRTYPE_SURFACE,
88 (SVGA3dFormatOp) (SVGA3DFORMAT_OP_OFFSCREEN_RENDERTARGET | SVGA3DFORMAT_OP_SAME_FORMAT_RENDERTARGET),
89 },
90 {
91 D3DUSAGE_AUTOGENMIPMAP,
92 D3DRTYPE_TEXTURE,
93 SVGA3DFORMAT_OP_AUTOGENMIPMAP,
94 },
95 {
96 D3DUSAGE_DMAP,
97 D3DRTYPE_TEXTURE,
98 SVGA3DFORMAT_OP_DMAP,
99 },
100 {
101 0,
102 D3DRTYPE_TEXTURE,
103 SVGA3DFORMAT_OP_TEXTURE,
104 },
105 {
106 0,
107 D3DRTYPE_CUBETEXTURE,
108 SVGA3DFORMAT_OP_CUBETEXTURE,
109 },
110 {
111 0,
112 D3DRTYPE_VOLUMETEXTURE,
113 SVGA3DFORMAT_OP_VOLUMETEXTURE,
114 },
115 {
116 D3DUSAGE_QUERY_VERTEXTEXTURE,
117 D3DRTYPE_TEXTURE,
118 SVGA3DFORMAT_OP_VERTEXTEXTURE,
119 },
120 {
121 D3DUSAGE_QUERY_LEGACYBUMPMAP,
122 D3DRTYPE_TEXTURE,
123 SVGA3DFORMAT_OP_BUMPMAP,
124 },
125 {
126 D3DUSAGE_QUERY_SRGBREAD,
127 D3DRTYPE_TEXTURE,
128 SVGA3DFORMAT_OP_SRGBREAD,
129 },
130 {
131 D3DUSAGE_QUERY_SRGBWRITE,
132 D3DRTYPE_TEXTURE,
133 SVGA3DFORMAT_OP_SRGBWRITE,
134 }
135};
136
137static VMSVGA3DFORMATSUPPORT const g_aFeatureReject[] =
138{
139 {
140 D3DUSAGE_QUERY_WRAPANDMIP,
141 D3DRTYPE_TEXTURE,
142 SVGA3DFORMAT_OP_NOTEXCOORDWRAPNORMIP
143 },
144 {
145 D3DUSAGE_QUERY_FILTER,
146 D3DRTYPE_TEXTURE,
147 SVGA3DFORMAT_OP_NOFILTER
148 },
149 {
150 D3DUSAGE_QUERY_POSTPIXELSHADER_BLENDING,
151 D3DRTYPE_TEXTURE, /* ?? */
152 SVGA3DFORMAT_OP_NOALPHABLEND
153 },
154};
155
156
157/*********************************************************************************************************************************
158* Internal Functions *
159*********************************************************************************************************************************/
160static void vmsvgaDumpD3DCaps(D3DCAPS9 *pCaps, D3DADAPTER_IDENTIFIER9 const *pai9);
161static DECLCALLBACK(int) vmsvga3dBackContextDestroy(PVGASTATECC pThisCC, uint32_t cid);
162static DECLCALLBACK(int) vmsvga3dBackSetRenderState(PVGASTATECC pThisCC, uint32_t cid, uint32_t cRenderStates, SVGA3dRenderState *pRenderState);
163static DECLCALLBACK(int) vmsvga3dBackSetRenderTarget(PVGASTATECC pThisCC, uint32_t cid, SVGA3dRenderTargetType type, SVGA3dSurfaceImageId target);
164static DECLCALLBACK(int) vmsvga3dBackSetTextureState(PVGASTATECC pThisCC, uint32_t cid, uint32_t cTextureStates, SVGA3dTextureState *pTextureState);
165static DECLCALLBACK(int) vmsvga3dBackSetViewPort(PVGASTATECC pThisCC, uint32_t cid, SVGA3dRect *pRect);
166static DECLCALLBACK(int) vmsvga3dBackSetScissorRect(PVGASTATECC pThisCC, uint32_t cid, SVGA3dRect *pRect);
167static DECLCALLBACK(int) vmsvga3dBackShaderDestroy(PVGASTATECC pThisCC, uint32_t cid, uint32_t shid, SVGA3dShaderType type);
168static DECLCALLBACK(int) vmsvga3dBackShaderSet(PVGASTATECC pThisCC, PVMSVGA3DCONTEXT pContext, uint32_t cid, SVGA3dShaderType type, uint32_t shid);
169static DECLCALLBACK(int) vmsvga3dBackOcclusionQueryDelete(PVGASTATECC pThisCC, PVMSVGA3DCONTEXT pContext);
170static DECLCALLBACK(int) vmsvga3dBackCreateTexture(PVGASTATECC pThisCC, PVMSVGA3DCONTEXT pContext, uint32_t idAssociatedContext, PVMSVGA3DSURFACE pSurface);
171
172
173DECLINLINE(D3DCUBEMAP_FACES) vmsvga3dCubemapFaceFromIndex(uint32_t iFace)
174{
175 D3DCUBEMAP_FACES Face;
176 switch (iFace)
177 {
178 case 0: Face = D3DCUBEMAP_FACE_POSITIVE_X; break;
179 case 1: Face = D3DCUBEMAP_FACE_NEGATIVE_X; break;
180 case 2: Face = D3DCUBEMAP_FACE_POSITIVE_Y; break;
181 case 3: Face = D3DCUBEMAP_FACE_NEGATIVE_Y; break;
182 case 4: Face = D3DCUBEMAP_FACE_POSITIVE_Z; break;
183 default:
184 case 5: Face = D3DCUBEMAP_FACE_NEGATIVE_Z; break;
185 }
186 return Face;
187}
188
189static DECLCALLBACK(int) vmsvga3dBackInit(PPDMDEVINS pDevIns, PVGASTATE pThis, PVGASTATECC pThisCC)
190{
191 RT_NOREF(pDevIns, pThis);
192
193 PVMSVGA3DSTATE pState;
194 pThisCC->svga.p3dState = pState = (PVMSVGA3DSTATE)RTMemAllocZ(sizeof(VMSVGA3DSTATE));
195 AssertReturn(pThisCC->svga.p3dState, VERR_NO_MEMORY);
196
197 /* Create event semaphore. */
198 int rc = RTSemEventCreate(&pState->WndRequestSem);
199 if (RT_FAILURE(rc))
200 {
201 Log(("%s: Failed to create event semaphore for window handling.\n", __FUNCTION__));
202 return rc;
203 }
204
205 /* Create the async IO thread. */
206 rc = RTThreadCreate(&pState->pWindowThread, vmsvga3dWindowThread, pState->WndRequestSem, 0, RTTHREADTYPE_GUI, 0, "VMSVGA3DWND");
207 if (RT_FAILURE(rc))
208 {
209 AssertMsgFailed(("%s: Async IO Thread creation for 3d window handling failed rc=%d\n", __FUNCTION__, rc));
210 return rc;
211 }
212
213 return VINF_SUCCESS;
214}
215
216static DECLCALLBACK(int) vmsvga3dBackPowerOn(PPDMDEVINS pDevIns, PVGASTATE pThis, PVGASTATECC pThisCC)
217{
218 PVMSVGA3DSTATE pState = pThisCC->svga.p3dState;
219 AssertReturn(pThisCC->svga.p3dState, VERR_NO_MEMORY);
220 HRESULT hr;
221 RT_NOREF(pDevIns, pThis);
222
223 if (pState->pD3D9)
224 return VINF_SUCCESS; /* already initialized (load state) */
225
226#ifdef VBOX_VMSVGA3D_WITH_WINE_OPENGL
227 pState->pD3D9 = Direct3DCreate9(D3D_SDK_VERSION);
228 AssertReturn(pState->pD3D9, VERR_INTERNAL_ERROR);
229#else
230 /* Direct3DCreate9Ex was introduced in Vista, so resolve it dynamically. */
231 typedef HRESULT (WINAPI *PFNDIRECT3DCREATE9EX)(UINT, IDirect3D9Ex **);
232 PFNDIRECT3DCREATE9EX pfnDirect3dCreate9Ex = (PFNDIRECT3DCREATE9EX)RTLdrGetSystemSymbol("d3d9.dll", "Direct3DCreate9Ex");
233 if (!pfnDirect3dCreate9Ex)
234 return PDMDevHlpVMSetError(pDevIns, VERR_SYMBOL_NOT_FOUND, RT_SRC_POS,
235 "vmsvga3d: Unable to locate Direct3DCreate9Ex. This feature requires Vista and later.");
236 hr = pfnDirect3dCreate9Ex(D3D_SDK_VERSION, &pState->pD3D9);
237 AssertReturn(hr == D3D_OK, VERR_INTERNAL_ERROR);
238#endif
239 D3DADAPTER_IDENTIFIER9 ai9;
240 hr = pState->pD3D9->GetAdapterIdentifier(D3DADAPTER_DEFAULT, 0, &ai9);
241 AssertReturnStmt(hr == D3D_OK, D3D_RELEASE(pState->pD3D9), VERR_INTERNAL_ERROR);
242
243 hr = pState->pD3D9->GetDeviceCaps(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, &pState->caps);
244 AssertReturnStmt(hr == D3D_OK, D3D_RELEASE(pState->pD3D9), VERR_INTERNAL_ERROR);
245
246 vmsvgaDumpD3DCaps(&pState->caps, &ai9);
247
248 if (!D3D9CheckDeviceFormat(pState->pD3D9, 0, D3DRTYPE_TEXTURE, FOURCC_INTZ))
249 {
250 /* INTZ support is essential to support depth surfaces used as textures. */
251 LogRel(("VMSVGA: texture format INTZ not supported!!!\n"));
252 }
253 else
254 pState->fSupportedSurfaceINTZ = true;
255
256 if (!D3D9CheckDeviceFormat(pState->pD3D9, D3DUSAGE_RENDERTARGET, D3DRTYPE_SURFACE, FOURCC_NULL))
257 {
258 /* NULL is a dummy surface which can be used as a render target to save memory. */
259 LogRel(("VMSVGA: surface format NULL not supported!!!\n"));
260 }
261 else
262 pState->fSupportedSurfaceNULL = true;
263
264 /* Check if DX9 depth stencil textures are supported */
265 if (!D3D9CheckDeviceFormat(pState->pD3D9, D3DUSAGE_DEPTHSTENCIL, D3DRTYPE_TEXTURE, D3DFMT_D16))
266 {
267 LogRel(("VMSVGA: texture format D3DFMT_D16 not supported\n"));
268 }
269
270 if (!D3D9CheckDeviceFormat(pState->pD3D9, D3DUSAGE_DEPTHSTENCIL, D3DRTYPE_TEXTURE, D3DFMT_D24X8))
271 {
272 LogRel(("VMSVGA: texture format D3DFMT_D24X8 not supported\n"));
273 }
274
275 if (!D3D9CheckDeviceFormat(pState->pD3D9, D3DUSAGE_DEPTHSTENCIL, D3DRTYPE_TEXTURE, D3DFMT_D24S8))
276 {
277 LogRel(("VMSVGA: texture format D3DFMT_D24S8 not supported\n"));
278 }
279
280 /* Check some formats must be emulated. */
281 if (D3D9CheckDeviceFormat(pState->pD3D9, 0, D3DRTYPE_TEXTURE, D3DFMT_UYVY))
282 {
283 /* Native UYVY support has better performance. */
284 LogRel(("VMSVGA: texture format D3DFMT_UYVY supported\n"));
285 pState->fSupportedFormatUYVY = true;
286 }
287
288 if (D3D9CheckDeviceFormat(pState->pD3D9, 0, D3DRTYPE_TEXTURE, D3DFMT_YUY2))
289 {
290 /* Native YUY2 support has better performance. */
291 LogRel(("VMSVGA: texture format D3DFMT_YUY2 supported\n"));
292 pState->fSupportedFormatYUY2 = true;
293 }
294
295 if (D3D9CheckDeviceFormat(pState->pD3D9, D3DUSAGE_RENDERTARGET, D3DRTYPE_TEXTURE, D3DFMT_A8B8G8R8))
296 {
297 /* Native A8B8G8R8 support is good for OpenGL application in the guest. */
298 LogRel(("VMSVGA: texture format D3DFMT_A8B8G8R8 supported\n"));
299 pState->fSupportedFormatA8B8G8R8 = true;
300 }
301
302 return VINF_SUCCESS;
303}
304
305static DECLCALLBACK(int) vmsvga3dBackReset(PVGASTATECC pThisCC)
306{
307 PVMSVGA3DSTATE pState = pThisCC->svga.p3dState;
308 AssertReturn(pThisCC->svga.p3dState, VERR_NO_MEMORY);
309
310 /* Destroy all leftover surfaces. */
311 for (uint32_t i = 0; i < pState->cSurfaces; i++)
312 {
313 if (pState->papSurfaces[i]->id != SVGA3D_INVALID_ID)
314 vmsvga3dSurfaceDestroy(pThisCC, pState->papSurfaces[i]->id);
315 }
316
317 /* Destroy all leftover contexts. */
318 for (uint32_t i = 0; i < pState->cContexts; i++)
319 {
320 if (pState->papContexts[i]->id != SVGA3D_INVALID_ID)
321 vmsvga3dBackContextDestroy(pThisCC, pState->papContexts[i]->id);
322 }
323 return VINF_SUCCESS;
324}
325
326static DECLCALLBACK(int) vmsvga3dBackTerminate(PVGASTATECC pThisCC)
327{
328 PVMSVGA3DSTATE pState = pThisCC->svga.p3dState;
329 AssertReturn(pThisCC->svga.p3dState, VERR_NO_MEMORY);
330
331 int rc = vmsvga3dBackReset(pThisCC);
332 AssertRCReturn(rc, rc);
333
334 /* Terminate the window creation thread. */
335 rc = vmsvga3dSendThreadMessage(pState->pWindowThread, pState->WndRequestSem, WM_VMSVGA3D_EXIT, 0, 0);
336 AssertRCReturn(rc, rc);
337
338 RTSemEventDestroy(pState->WndRequestSem);
339
340 D3D_RELEASE(pState->pD3D9);
341
342 return VINF_SUCCESS;
343}
344
345static DECLCALLBACK(void) vmsvga3dBackUpdateHostScreenViewport(PVGASTATECC pThisCC, uint32_t idScreen, VMSVGAVIEWPORT const *pOldViewport)
346{
347 /** @todo Scroll the screen content without requiring the guest to redraw. */
348 RT_NOREF(pThisCC, idScreen, pOldViewport);
349}
350
351static uint32_t vmsvga3dGetSurfaceFormatSupport(PVMSVGA3DSTATE pState3D, uint32_t idx3dCaps, D3DFORMAT format)
352{
353 NOREF(idx3dCaps);
354 HRESULT hr;
355 uint32_t result = 0;
356
357 /* Try if the format can be used for the primary display. */
358 hr = pState3D->pD3D9->CheckDeviceFormat(D3DADAPTER_DEFAULT,
359 D3DDEVTYPE_HAL,
360 format,
361 0,
362 D3DRTYPE_SURFACE,
363 format);
364
365 for (unsigned i = 0; i < RT_ELEMENTS(g_aFormatSupport); i++)
366 {
367 hr = pState3D->pD3D9->CheckDeviceFormat(D3DADAPTER_DEFAULT,
368 D3DDEVTYPE_HAL,
369 D3DFMT_X8R8G8B8, /* assume standard 32-bit display mode */
370 g_aFormatSupport[i].Usage,
371 g_aFormatSupport[i].ResourceType,
372 format);
373 if (hr == D3D_OK)
374 result |= g_aFormatSupport[i].FormatOp;
375 }
376
377 /* Check for features only if the format is supported in any form. */
378 if (result)
379 {
380 for (unsigned i = 0; i < RT_ELEMENTS(g_aFeatureReject); i++)
381 {
382 hr = pState3D->pD3D9->CheckDeviceFormat(D3DADAPTER_DEFAULT,
383 D3DDEVTYPE_HAL,
384 D3DFMT_X8R8G8B8, /* assume standard 32-bit display mode */
385 g_aFeatureReject[i].Usage,
386 g_aFeatureReject[i].ResourceType,
387 format);
388 if (hr != D3D_OK)
389 result |= g_aFeatureReject[i].FormatOp;
390 }
391 }
392
393 /** @todo missing:
394 *
395 * SVGA3DFORMAT_OP_PIXELSIZE
396 */
397
398 switch (idx3dCaps)
399 {
400 case SVGA3D_DEVCAP_SURFACEFMT_X8R8G8B8:
401 case SVGA3D_DEVCAP_SURFACEFMT_X1R5G5B5:
402 case SVGA3D_DEVCAP_SURFACEFMT_R5G6B5:
403 result |= SVGA3DFORMAT_OP_MEMBEROFGROUP_ARGB
404 | SVGA3DFORMAT_OP_CONVERT_TO_ARGB
405 | SVGA3DFORMAT_OP_DISPLAYMODE /* Should not be set for alpha formats. */
406 | SVGA3DFORMAT_OP_3DACCELERATION; /* implies OP_DISPLAYMODE */
407 break;
408
409 case SVGA3D_DEVCAP_SURFACEFMT_A8R8G8B8:
410 case SVGA3D_DEVCAP_SURFACEFMT_A2R10G10B10:
411 case SVGA3D_DEVCAP_SURFACEFMT_A1R5G5B5:
412 case SVGA3D_DEVCAP_SURFACEFMT_A4R4G4B4:
413 result |= SVGA3DFORMAT_OP_MEMBEROFGROUP_ARGB
414 | SVGA3DFORMAT_OP_CONVERT_TO_ARGB
415 | SVGA3DFORMAT_OP_SAME_FORMAT_UP_TO_ALPHA_RENDERTARGET;
416 break;
417 /* These formats can't be used as textures on AMD drivers (Intel works).
418 * Still report them as textures to the guest and emulate them in the device.
419 */
420 case SVGA3D_DEVCAP_SURFACEFMT_UYVY:
421 case SVGA3D_DEVCAP_SURFACEFMT_YUY2:
422 result |= SVGA3DFORMAT_OP_TEXTURE;
423 break;
424 }
425 Log(("CAPS: %s =\n%s\n", vmsvga3dGetCapString(idx3dCaps), vmsvga3dGet3dFormatString(result)));
426
427 return result;
428}
429
430static uint32_t vmsvga3dGetDepthFormatSupport(PVMSVGA3DSTATE pState3D, uint32_t idx3dCaps, D3DFORMAT format)
431{
432 RT_NOREF(idx3dCaps);
433 HRESULT hr;
434 uint32_t result = 0;
435
436 hr = pState3D->pD3D9->CheckDeviceFormat(D3DADAPTER_DEFAULT,
437 D3DDEVTYPE_HAL,
438 D3DFMT_X8R8G8B8, /* assume standard 32-bit display mode */
439 D3DUSAGE_DEPTHSTENCIL,
440 D3DRTYPE_SURFACE,
441 format);
442 if (hr == D3D_OK)
443 result = SVGA3DFORMAT_OP_ZSTENCIL
444 | SVGA3DFORMAT_OP_ZSTENCIL_WITH_ARBITRARY_COLOR_DEPTH
445 | SVGA3DFORMAT_OP_TEXTURE /* Necessary for Ubuntu Unity */;
446
447 Log(("CAPS: %s =\n%s\n", vmsvga3dGetCapString(idx3dCaps), vmsvga3dGet3dFormatString(result)));
448 return result;
449}
450
451
452static DECLCALLBACK(int) vmsvga3dBackQueryCaps(PVGASTATECC pThisCC, SVGA3dDevCapIndex idx3dCaps, uint32_t *pu32Val)
453{
454 PVMSVGA3DSTATE pState = pThisCC->svga.p3dState;
455 AssertReturn(pState, VERR_NO_MEMORY);
456 D3DCAPS9 *pCaps = &pState->caps;
457 int rc = VINF_SUCCESS;
458
459 *pu32Val = 0;
460
461 switch (idx3dCaps)
462 {
463 case SVGA3D_DEVCAP_3D:
464 *pu32Val = 1; /* boolean? */
465 break;
466
467 case SVGA3D_DEVCAP_MAX_LIGHTS:
468 *pu32Val = pCaps->MaxActiveLights;
469 break;
470
471 case SVGA3D_DEVCAP_MAX_TEXTURES:
472 *pu32Val = pCaps->MaxSimultaneousTextures;
473 break;
474
475 case SVGA3D_DEVCAP_MAX_CLIP_PLANES:
476 *pu32Val = pCaps->MaxUserClipPlanes;
477 break;
478
479 case SVGA3D_DEVCAP_VERTEX_SHADER_VERSION:
480 switch (pCaps->VertexShaderVersion)
481 {
482 case D3DVS_VERSION(1,1):
483 *pu32Val = SVGA3DVSVERSION_11;
484 break;
485 case D3DVS_VERSION(2,0):
486 *pu32Val = SVGA3DVSVERSION_20;
487 break;
488 case D3DVS_VERSION(3,0):
489 *pu32Val = SVGA3DVSVERSION_30;
490 break;
491 case D3DVS_VERSION(4,0):
492 *pu32Val = SVGA3DVSVERSION_40;
493 break;
494 default:
495 LogRel(("VMSVGA: Unsupported vertex shader version %x\n", pCaps->VertexShaderVersion));
496 break;
497 }
498 break;
499
500 case SVGA3D_DEVCAP_VERTEX_SHADER:
501 /* boolean? */
502 *pu32Val = 1;
503 break;
504
505 case SVGA3D_DEVCAP_FRAGMENT_SHADER_VERSION:
506 switch (pCaps->PixelShaderVersion)
507 {
508 case D3DPS_VERSION(1,1):
509 *pu32Val = SVGA3DPSVERSION_11;
510 break;
511 case D3DPS_VERSION(1,2):
512 *pu32Val = SVGA3DPSVERSION_12;
513 break;
514 case D3DPS_VERSION(1,3):
515 *pu32Val = SVGA3DPSVERSION_13;
516 break;
517 case D3DPS_VERSION(1,4):
518 *pu32Val = SVGA3DPSVERSION_14;
519 break;
520 case D3DPS_VERSION(2,0):
521 *pu32Val = SVGA3DPSVERSION_20;
522 break;
523 case D3DPS_VERSION(3,0):
524 *pu32Val = SVGA3DPSVERSION_30;
525 break;
526 case D3DPS_VERSION(4,0):
527 *pu32Val = SVGA3DPSVERSION_40;
528 break;
529 default:
530 LogRel(("VMSVGA: Unsupported pixel shader version %x\n", pCaps->PixelShaderVersion));
531 break;
532 }
533 break;
534
535 case SVGA3D_DEVCAP_FRAGMENT_SHADER:
536 /* boolean? */
537 *pu32Val = 1;
538 break;
539
540 case SVGA3D_DEVCAP_S23E8_TEXTURES:
541 case SVGA3D_DEVCAP_S10E5_TEXTURES:
542 /* Must be obsolete by now; surface format caps specify the same thing. */
543 rc = VERR_INVALID_PARAMETER;
544 break;
545
546 case SVGA3D_DEVCAP_MAX_FIXED_VERTEXBLEND:
547 break;
548
549 /*
550 * 2. The BUFFER_FORMAT capabilities are deprecated, and they always
551 * return TRUE. Even on physical hardware that does not support
552 * these formats natively, the SVGA3D device will provide an emulation
553 * which should be invisible to the guest OS.
554 */
555 case SVGA3D_DEVCAP_D16_BUFFER_FORMAT:
556 case SVGA3D_DEVCAP_D24S8_BUFFER_FORMAT:
557 case SVGA3D_DEVCAP_D24X8_BUFFER_FORMAT:
558 *pu32Val = 1;
559 break;
560
561 case SVGA3D_DEVCAP_QUERY_TYPES:
562 break;
563
564 case SVGA3D_DEVCAP_TEXTURE_GRADIENT_SAMPLING:
565 break;
566
567 case SVGA3D_DEVCAP_MAX_POINT_SIZE:
568 AssertCompile(sizeof(uint32_t) == sizeof(float));
569 *(float *)pu32Val = pCaps->MaxPointSize;
570 break;
571
572 case SVGA3D_DEVCAP_MAX_SHADER_TEXTURES:
573 /** @todo ?? */
574 rc = VERR_INVALID_PARAMETER;
575 break;
576
577 case SVGA3D_DEVCAP_MAX_TEXTURE_WIDTH:
578 *pu32Val = pCaps->MaxTextureWidth;
579 break;
580
581 case SVGA3D_DEVCAP_MAX_TEXTURE_HEIGHT:
582 *pu32Val = pCaps->MaxTextureHeight;
583 break;
584
585 case SVGA3D_DEVCAP_MAX_VOLUME_EXTENT:
586 *pu32Val = pCaps->MaxVolumeExtent;
587 break;
588
589 case SVGA3D_DEVCAP_MAX_TEXTURE_REPEAT:
590 *pu32Val = pCaps->MaxTextureRepeat;
591 break;
592
593 case SVGA3D_DEVCAP_MAX_TEXTURE_ASPECT_RATIO:
594 *pu32Val = pCaps->MaxTextureAspectRatio;
595 break;
596
597 case SVGA3D_DEVCAP_MAX_TEXTURE_ANISOTROPY:
598 *pu32Val = pCaps->MaxAnisotropy;
599 break;
600
601 case SVGA3D_DEVCAP_MAX_PRIMITIVE_COUNT:
602 *pu32Val = pCaps->MaxPrimitiveCount;
603 break;
604
605 case SVGA3D_DEVCAP_MAX_VERTEX_INDEX:
606 *pu32Val = pCaps->MaxVertexIndex;
607 break;
608
609 case SVGA3D_DEVCAP_MAX_VERTEX_SHADER_INSTRUCTIONS:
610 *pu32Val = pCaps->MaxVertexShader30InstructionSlots;
611 break;
612
613 case SVGA3D_DEVCAP_MAX_FRAGMENT_SHADER_INSTRUCTIONS:
614 *pu32Val = pCaps->MaxPixelShader30InstructionSlots;
615 break;
616
617 case SVGA3D_DEVCAP_MAX_VERTEX_SHADER_TEMPS:
618 *pu32Val = pCaps->VS20Caps.NumTemps;
619 break;
620
621 case SVGA3D_DEVCAP_MAX_FRAGMENT_SHADER_TEMPS:
622 *pu32Val = pCaps->PS20Caps.NumTemps;
623 break;
624
625 case SVGA3D_DEVCAP_TEXTURE_OPS:
626 break;
627
628 case SVGA3D_DEVCAP_DEAD4: /* SVGA3D_DEVCAP_MULTISAMPLE_NONMASKABLESAMPLES */
629 break;
630
631 case SVGA3D_DEVCAP_DEAD5: /* SVGA3D_DEVCAP_MULTISAMPLE_MASKABLESAMPLES */
632 break;
633
634 case SVGA3D_DEVCAP_DEAD7: /* SVGA3D_DEVCAP_ALPHATOCOVERAGE */
635 break;
636
637 case SVGA3D_DEVCAP_DEAD6: /* SVGA3D_DEVCAP_SUPERSAMPLE */
638 break;
639
640 case SVGA3D_DEVCAP_AUTOGENMIPMAPS:
641 *pu32Val = !!(pCaps->Caps2 & D3DCAPS2_CANAUTOGENMIPMAP);
642 break;
643
644 case SVGA3D_DEVCAP_MAX_VERTEX_SHADER_TEXTURES:
645 break;
646
647 case SVGA3D_DEVCAP_MAX_RENDER_TARGETS: /** @todo same thing? */
648 case SVGA3D_DEVCAP_MAX_SIMULTANEOUS_RENDER_TARGETS:
649 *pu32Val = pCaps->NumSimultaneousRTs;
650 break;
651
652 /*
653 * This is the maximum number of SVGA context IDs that the guest
654 * can define using SVGA_3D_CMD_CONTEXT_DEFINE.
655 */
656 case SVGA3D_DEVCAP_MAX_CONTEXT_IDS:
657 *pu32Val = SVGA3D_MAX_CONTEXT_IDS;
658 break;
659
660 /*
661 * This is the maximum number of SVGA surface IDs that the guest
662 * can define using SVGA_3D_CMD_SURFACE_DEFINE*.
663 */
664 case SVGA3D_DEVCAP_MAX_SURFACE_IDS:
665 *pu32Val = SVGA3D_MAX_SURFACE_IDS;
666 break;
667
668 /* Supported surface formats. */
669 case SVGA3D_DEVCAP_SURFACEFMT_X8R8G8B8:
670 *pu32Val = vmsvga3dGetSurfaceFormatSupport(pState, idx3dCaps, D3DFMT_X8R8G8B8);
671 break;
672
673 case SVGA3D_DEVCAP_SURFACEFMT_A8R8G8B8:
674 *pu32Val = vmsvga3dGetSurfaceFormatSupport(pState, idx3dCaps, D3DFMT_A8R8G8B8);
675 break;
676
677 case SVGA3D_DEVCAP_SURFACEFMT_A2R10G10B10:
678 *pu32Val = vmsvga3dGetSurfaceFormatSupport(pState, idx3dCaps, D3DFMT_A2R10G10B10);
679 break;
680
681 case SVGA3D_DEVCAP_SURFACEFMT_X1R5G5B5:
682 *pu32Val = vmsvga3dGetSurfaceFormatSupport(pState, idx3dCaps, D3DFMT_X1R5G5B5);
683 break;
684
685 case SVGA3D_DEVCAP_SURFACEFMT_A1R5G5B5:
686 *pu32Val = vmsvga3dGetSurfaceFormatSupport(pState, idx3dCaps, D3DFMT_A1R5G5B5);
687 break;
688
689 case SVGA3D_DEVCAP_SURFACEFMT_A4R4G4B4:
690 *pu32Val = vmsvga3dGetSurfaceFormatSupport(pState, idx3dCaps, D3DFMT_A4R4G4B4);
691 break;
692
693 case SVGA3D_DEVCAP_SURFACEFMT_R5G6B5:
694 *pu32Val = vmsvga3dGetSurfaceFormatSupport(pState, idx3dCaps, D3DFMT_A4R4G4B4);
695 break;
696
697 case SVGA3D_DEVCAP_SURFACEFMT_LUMINANCE16:
698 *pu32Val = vmsvga3dGetSurfaceFormatSupport(pState, idx3dCaps, D3DFMT_L16);
699 break;
700
701 case SVGA3D_DEVCAP_SURFACEFMT_LUMINANCE8_ALPHA8:
702 *pu32Val = vmsvga3dGetSurfaceFormatSupport(pState, idx3dCaps, D3DFMT_A8L8);
703 break;
704
705 case SVGA3D_DEVCAP_SURFACEFMT_ALPHA8:
706 *pu32Val = vmsvga3dGetSurfaceFormatSupport(pState, idx3dCaps, D3DFMT_A8);
707 break;
708
709 case SVGA3D_DEVCAP_SURFACEFMT_LUMINANCE8:
710 *pu32Val = vmsvga3dGetSurfaceFormatSupport(pState, idx3dCaps, D3DFMT_L8);
711 break;
712
713 case SVGA3D_DEVCAP_SURFACEFMT_Z_D16:
714 *pu32Val = vmsvga3dGetDepthFormatSupport(pState, idx3dCaps, D3DFMT_D16);
715 break;
716
717 case SVGA3D_DEVCAP_SURFACEFMT_Z_D24S8:
718 case SVGA3D_DEVCAP_SURFACEFMT_Z_D24S8_INT: /** @todo not correct */
719 *pu32Val = vmsvga3dGetDepthFormatSupport(pState, idx3dCaps, D3DFMT_D24S8);
720 break;
721
722 case SVGA3D_DEVCAP_SURFACEFMT_Z_D24X8:
723 *pu32Val = vmsvga3dGetDepthFormatSupport(pState, idx3dCaps, D3DFMT_D24X8);
724 break;
725
726 case SVGA3D_DEVCAP_SURFACEFMT_Z_DF16:
727 /** @todo supposed to be floating-point, but unable to find a match for D3D9... */
728 *pu32Val = 0;
729 break;
730
731 case SVGA3D_DEVCAP_SURFACEFMT_Z_DF24:
732 *pu32Val = vmsvga3dGetDepthFormatSupport(pState, idx3dCaps, D3DFMT_D24FS8);
733 break;
734
735 case SVGA3D_DEVCAP_SURFACEFMT_DXT1:
736 *pu32Val = vmsvga3dGetSurfaceFormatSupport(pState, idx3dCaps, D3DFMT_DXT1);
737 break;
738
739 case SVGA3D_DEVCAP_SURFACEFMT_DXT2:
740 *pu32Val = vmsvga3dGetSurfaceFormatSupport(pState, idx3dCaps, D3DFMT_DXT2);
741 break;
742
743 case SVGA3D_DEVCAP_SURFACEFMT_DXT3:
744 *pu32Val = vmsvga3dGetSurfaceFormatSupport(pState, idx3dCaps, D3DFMT_DXT3);
745 break;
746
747 case SVGA3D_DEVCAP_SURFACEFMT_DXT4:
748 *pu32Val = vmsvga3dGetSurfaceFormatSupport(pState, idx3dCaps, D3DFMT_DXT4);
749 break;
750
751 case SVGA3D_DEVCAP_SURFACEFMT_DXT5:
752 *pu32Val = vmsvga3dGetSurfaceFormatSupport(pState, idx3dCaps, D3DFMT_DXT5);
753 break;
754
755 case SVGA3D_DEVCAP_SURFACEFMT_BUMPX8L8V8U8:
756 *pu32Val = vmsvga3dGetSurfaceFormatSupport(pState, idx3dCaps, D3DFMT_X8L8V8U8);
757 break;
758
759 case SVGA3D_DEVCAP_SURFACEFMT_A2W10V10U10:
760 *pu32Val = vmsvga3dGetSurfaceFormatSupport(pState, idx3dCaps, D3DFMT_A2W10V10U10);
761 break;
762
763 case SVGA3D_DEVCAP_SURFACEFMT_BUMPU8V8:
764 *pu32Val = vmsvga3dGetSurfaceFormatSupport(pState, idx3dCaps, D3DFMT_V8U8);
765 break;
766
767 case SVGA3D_DEVCAP_SURFACEFMT_Q8W8V8U8:
768 *pu32Val = vmsvga3dGetSurfaceFormatSupport(pState, idx3dCaps, D3DFMT_Q8W8V8U8);
769 break;
770
771 case SVGA3D_DEVCAP_SURFACEFMT_CxV8U8:
772 *pu32Val = vmsvga3dGetSurfaceFormatSupport(pState, idx3dCaps, D3DFMT_CxV8U8);
773 break;
774
775 case SVGA3D_DEVCAP_SURFACEFMT_R_S10E5:
776 *pu32Val = vmsvga3dGetSurfaceFormatSupport(pState, idx3dCaps, D3DFMT_R16F);
777 break;
778
779 case SVGA3D_DEVCAP_SURFACEFMT_R_S23E8:
780 *pu32Val = vmsvga3dGetSurfaceFormatSupport(pState, idx3dCaps, D3DFMT_R32F);
781 break;
782
783 case SVGA3D_DEVCAP_SURFACEFMT_RG_S10E5:
784 *pu32Val = vmsvga3dGetSurfaceFormatSupport(pState, idx3dCaps, D3DFMT_G16R16F);
785 break;
786
787 case SVGA3D_DEVCAP_SURFACEFMT_RG_S23E8:
788 *pu32Val = vmsvga3dGetSurfaceFormatSupport(pState, idx3dCaps, D3DFMT_G32R32F);
789 break;
790
791 case SVGA3D_DEVCAP_SURFACEFMT_ARGB_S10E5:
792 *pu32Val = vmsvga3dGetSurfaceFormatSupport(pState, idx3dCaps, D3DFMT_A16B16G16R16F);
793 break;
794
795 case SVGA3D_DEVCAP_SURFACEFMT_ARGB_S23E8:
796 *pu32Val = vmsvga3dGetSurfaceFormatSupport(pState, idx3dCaps, D3DFMT_A32B32G32R32F);
797 break;
798
799 case SVGA3D_DEVCAP_SURFACEFMT_V16U16:
800 *pu32Val = vmsvga3dGetSurfaceFormatSupport(pState, idx3dCaps, D3DFMT_V16U16);
801 break;
802
803 case SVGA3D_DEVCAP_SURFACEFMT_G16R16:
804 *pu32Val = vmsvga3dGetSurfaceFormatSupport(pState, idx3dCaps, D3DFMT_G16R16);
805 break;
806
807 case SVGA3D_DEVCAP_SURFACEFMT_A16B16G16R16:
808 *pu32Val = vmsvga3dGetSurfaceFormatSupport(pState, idx3dCaps, D3DFMT_A16B16G16R16);
809 break;
810
811 case SVGA3D_DEVCAP_SURFACEFMT_UYVY:
812 *pu32Val = vmsvga3dGetSurfaceFormatSupport(pState, idx3dCaps, D3DFMT_UYVY);
813 break;
814
815 case SVGA3D_DEVCAP_SURFACEFMT_YUY2:
816 *pu32Val = vmsvga3dGetSurfaceFormatSupport(pState, idx3dCaps, D3DFMT_YUY2);
817 break;
818
819 case SVGA3D_DEVCAP_SURFACEFMT_NV12:
820 *pu32Val = vmsvga3dGetSurfaceFormatSupport(pState, idx3dCaps, (D3DFORMAT)MAKEFOURCC('N', 'V', '1', '2'));
821 break;
822
823 case SVGA3D_DEVCAP_DEAD10: /* SVGA3D_DEVCAP_SURFACEFMT_AYUV */
824 *pu32Val = vmsvga3dGetSurfaceFormatSupport(pState, idx3dCaps, (D3DFORMAT)MAKEFOURCC('A', 'Y', 'U', 'V'));
825 break;
826
827 case SVGA3D_DEVCAP_SURFACEFMT_ATI1:
828 case SVGA3D_DEVCAP_SURFACEFMT_ATI2:
829 /* Unknown; only in DX10 & 11 */
830 Log(("CAPS: Unknown CAP %s\n", vmsvga3dGetCapString(idx3dCaps)));
831 rc = VERR_INVALID_PARAMETER;
832 *pu32Val = 0;
833 break;
834
835 default:
836 Log(("CAPS: Unexpected CAP %d\n", idx3dCaps));
837 rc = VERR_INVALID_PARAMETER;
838 break;
839 }
840#if 0
841 /* Dump of VMWare Player caps (from their log); for debugging purposes */
842 switch (idx3dCaps)
843 {
844 case 0:
845 *pu32Val = 0x00000001;
846 break;
847 case 1:
848 *pu32Val = 0x0000000a;
849 break;
850 case 2:
851 *pu32Val = 0x00000008;
852 break;
853 case 3: *pu32Val = 0x00000006; break;
854 case 4: *pu32Val = 0x00000007; break;
855 case 5: *pu32Val = 0x00000001; break;
856 case 6: *pu32Val = 0x0000000d; break;
857 case 7: *pu32Val = 0x00000001; break;
858 case 8: *pu32Val = 0x00000004; break;
859 case 9: *pu32Val = 0x00000001; break;
860 case 10: *pu32Val = 0x00000001; break;
861 case 11: *pu32Val = 0x00000004; break;
862 case 12: *pu32Val = 0x00000001; break;
863 case 13: *pu32Val = 0x00000001; break;
864 case 14: *pu32Val = 0x00000001; break;
865 case 15: *pu32Val = 0x00000001; break;
866 case 16: *pu32Val = 0x00000001; break;
867 case 17: *pu32Val = (uint32_t)256.000000; break;
868 case 18: *pu32Val = 0x00000014; break;
869 case 19: *pu32Val = 0x00001000; break;
870 case 20: *pu32Val = 0x00001000; break;
871 case 21: *pu32Val = 0x00000800; break;
872 case 22: *pu32Val = 0x00002000; break;
873 case 23: *pu32Val = 0x00000800; break;
874 case 24: *pu32Val = 0x00000010; break;
875 case 25: *pu32Val = 0x000fffff; break;
876 case 26: *pu32Val = 0x00ffffff; break;
877 case 27: *pu32Val = 0xffffffff; break;
878 case 28: *pu32Val = 0xffffffff; break;
879 case 29: *pu32Val = 0x00000020; break;
880 case 30: *pu32Val = 0x00000020; break;
881 case 31: *pu32Val = 0x03ffffff; break;
882 case 32: *pu32Val = 0x0098ec1f; break;
883 case 33: *pu32Val = 0x0098e11f; break;
884 case 34: *pu32Val = 0x0098e01f; break;
885 case 35: *pu32Val = 0x012c2000; break;
886 case 36: *pu32Val = 0x0098e11f; break;
887 case 37: *pu32Val = 0x0090c11f; break;
888 case 38: *pu32Val = 0x0098ec1f; break;
889 case 39: *pu32Val = 0x00804007; break;
890 case 40: *pu32Val = 0x0080c007; break;
891 case 41: *pu32Val = 0x00804007; break;
892 case 42: *pu32Val = 0x0080c007; break;
893 case 43: *pu32Val = 0x000000c1; break;
894 case 44: *pu32Val = 0x000000c1; break;
895 case 45: *pu32Val = 0x000000c1; break;
896 case 46: *pu32Val = 0x00808005; break;
897 case 47: *pu32Val = 0x00808005; break;
898 case 48: *pu32Val = 0x00808005; break;
899 case 49: *pu32Val = 0x00808005; break;
900 case 50: *pu32Val = 0x00808005; break;
901 case 51: *pu32Val = 0x01240000; break;
902 case 52: *pu32Val = 0x00814007; break;
903 case 53: *pu32Val = 0x00814007; break;
904 case 54: *pu32Val = 0x00814007; break;
905 case 55: *pu32Val = 0x01240000; break;
906 case 56: *pu32Val = 0x0080401f; break;
907 case 57: *pu32Val = 0x0080401f; break;
908 case 58: *pu32Val = 0x0080401f; break;
909 case 59: *pu32Val = 0x0080401f; break;
910 case 60: *pu32Val = 0x0080601f; break;
911 case 61: *pu32Val = 0x0080401f; break;
912 case 62: *pu32Val = 0x00000000; break;
913 case 63: *pu32Val = 0x00000004; break;
914 case 64: *pu32Val = 0x00000004; break;
915 case 65: *pu32Val = 0x00814005; break;
916 case 66: *pu32Val = 0x0080401f; break;
917 case 67: *pu32Val = 0x0080601f; break;
918 case 68: *pu32Val = 0x00006009; break;
919 case 69: *pu32Val = 0x00006001; break;
920 case 70: *pu32Val = 0x00000001; break;
921 case 71: *pu32Val = 0x0000000b; break;
922 case 72: *pu32Val = 0x00000001; break;
923 case 73: *pu32Val = 0x00000000; break;
924 case 74: *pu32Val = 0x00000000; break;
925 case 75: *pu32Val = 0x01246000; break;
926 case 76: *pu32Val = 0x00004009; break;
927 case 77: *pu32Val = 0x00000100; break;
928 case 78: *pu32Val = 0x00008000; break;
929 case 79: *pu32Val = 0x000000c1; break;
930 case 80: *pu32Val = 0x01240000; break;
931 case 81: *pu32Val = 0x000000c1; break;
932 case 82: *pu32Val = 0x00800005; break;
933 case 83: *pu32Val = 0x00800005; break;
934 case 84: *pu32Val = 0x00000000; break;
935 case 85: *pu32Val = 0x00000000; break;
936 case 86: *pu32Val = 0x00000000; break;
937 case 87: *pu32Val = 0x00000000; break;
938 case 88: *pu32Val = 0x00000000; break;
939 case 89: *pu32Val = (uint32_t) 0.000000; break;
940 case 90: *pu32Val = (uint32_t) 0.000000; break;
941 case 91: *pu32Val = 0x00006009; break;
942 default:
943// Log(("CAPS: Unexpected CAP %d\n", idx3dCaps));
944// rc = VERR_INVALID_PARAMETER;
945 break;
946 }
947#endif
948 Log(("CAPS: %d=%s - %x\n", idx3dCaps, vmsvga3dGetCapString(idx3dCaps), *pu32Val));
949 return rc;
950}
951
952/**
953 * Convert SVGA format value to its D3D equivalent
954 */
955D3DFORMAT vmsvga3dSurfaceFormat2D3D(SVGA3dSurfaceFormat format)
956{
957 switch (format)
958 {
959 case SVGA3D_X8R8G8B8:
960 return D3DFMT_X8R8G8B8;
961 case SVGA3D_A8R8G8B8:
962 return D3DFMT_A8R8G8B8;
963 case SVGA3D_R5G6B5:
964 return D3DFMT_R5G6B5;
965 case SVGA3D_X1R5G5B5:
966 return D3DFMT_X1R5G5B5;
967 case SVGA3D_A1R5G5B5:
968 return D3DFMT_A1R5G5B5;
969 case SVGA3D_A4R4G4B4:
970 return D3DFMT_A4R4G4B4;
971
972 case SVGA3D_R8G8B8A8_UNORM:
973 return D3DFMT_A8B8G8R8;
974
975 case SVGA3D_Z_D32:
976 return D3DFMT_D32;
977 case SVGA3D_Z_D16:
978 return D3DFMT_D16;
979 case SVGA3D_Z_D24S8_INT: /** @todo not correct */
980 case SVGA3D_Z_D24S8:
981 return D3DFMT_D24S8;
982 case SVGA3D_Z_D15S1:
983 return D3DFMT_D15S1;
984 case SVGA3D_Z_D24X8:
985 return D3DFMT_D24X8;
986 /* Advanced D3D9 depth formats. */
987 case SVGA3D_Z_DF16:
988 /** @todo supposed to be floating-point, but unable to find a match for D3D9... */
989 AssertFailedReturn(D3DFMT_UNKNOWN);
990 case SVGA3D_Z_DF24:
991 return D3DFMT_D24FS8;
992
993 case SVGA3D_LUMINANCE8:
994 return D3DFMT_L8;
995 case SVGA3D_LUMINANCE4_ALPHA4:
996 return D3DFMT_A4L4;
997 case SVGA3D_LUMINANCE16:
998 return D3DFMT_L16;
999 case SVGA3D_LUMINANCE8_ALPHA8:
1000 return D3DFMT_A8L8;
1001
1002 case SVGA3D_DXT1:
1003 return D3DFMT_DXT1;
1004 case SVGA3D_DXT2:
1005 return D3DFMT_DXT2;
1006 case SVGA3D_DXT3:
1007 return D3DFMT_DXT3;
1008 case SVGA3D_DXT4:
1009 return D3DFMT_DXT4;
1010 case SVGA3D_DXT5:
1011 return D3DFMT_DXT5;
1012
1013 /* Bump-map formats */
1014 case SVGA3D_BUMPU8V8:
1015 return D3DFMT_V8U8;
1016 case SVGA3D_BUMPL6V5U5:
1017 return D3DFMT_L6V5U5;
1018 case SVGA3D_BUMPX8L8V8U8:
1019 return D3DFMT_X8L8V8U8;
1020 case SVGA3D_FORMAT_DEAD1:
1021 /* No corresponding D3D9 equivalent. */
1022 AssertFailedReturn(D3DFMT_UNKNOWN);
1023 /* signed bump-map formats */
1024 case SVGA3D_V8U8:
1025 return D3DFMT_V8U8;
1026 case SVGA3D_Q8W8V8U8:
1027 return D3DFMT_Q8W8V8U8;
1028 case SVGA3D_CxV8U8:
1029 return D3DFMT_CxV8U8;
1030 /* mixed bump-map formats */
1031 case SVGA3D_X8L8V8U8:
1032 return D3DFMT_X8L8V8U8;
1033 case SVGA3D_A2W10V10U10:
1034 return D3DFMT_A2W10V10U10;
1035
1036 case SVGA3D_ARGB_S10E5: /* 16-bit floating-point ARGB */
1037 return D3DFMT_A16B16G16R16F;
1038 case SVGA3D_ARGB_S23E8: /* 32-bit floating-point ARGB */
1039 return D3DFMT_A32B32G32R32F;
1040
1041 case SVGA3D_A2R10G10B10:
1042 return D3DFMT_A2R10G10B10;
1043
1044 case SVGA3D_ALPHA8:
1045 return D3DFMT_A8;
1046
1047 /* Single- and dual-component floating point formats */
1048 case SVGA3D_R_S10E5:
1049 return D3DFMT_R16F;
1050 case SVGA3D_R_S23E8:
1051 return D3DFMT_R32F;
1052 case SVGA3D_RG_S10E5:
1053 return D3DFMT_G16R16F;
1054 case SVGA3D_RG_S23E8:
1055 return D3DFMT_G32R32F;
1056
1057 /*
1058 * Any surface can be used as a buffer object, but SVGA3D_BUFFER is
1059 * the most efficient format to use when creating new surfaces
1060 * expressly for index or vertex data.
1061 */
1062 case SVGA3D_BUFFER:
1063 return D3DFMT_UNKNOWN;
1064
1065 case SVGA3D_V16U16:
1066 return D3DFMT_V16U16;
1067
1068 case SVGA3D_G16R16:
1069 return D3DFMT_G16R16;
1070 case SVGA3D_A16B16G16R16:
1071 return D3DFMT_A16B16G16R16;
1072
1073 /* Packed Video formats */
1074 case SVGA3D_UYVY:
1075 return D3DFMT_UYVY;
1076 case SVGA3D_YUY2:
1077 return D3DFMT_YUY2;
1078
1079 /* Planar video formats */
1080 case SVGA3D_NV12:
1081 return (D3DFORMAT)MAKEFOURCC('N', 'V', '1', '2');
1082
1083 /* Video format with alpha */
1084 case SVGA3D_FORMAT_DEAD2: /* Old SVGA3D_AYUV */
1085 return (D3DFORMAT)MAKEFOURCC('A', 'Y', 'U', 'V');
1086
1087 case SVGA3D_R8G8B8A8_SNORM:
1088 return D3DFMT_Q8W8V8U8;
1089 case SVGA3D_R16G16_UNORM:
1090 return D3DFMT_G16R16;
1091
1092 case SVGA3D_ATI1:
1093 case SVGA3D_ATI2:
1094 /* Unknown; only in DX10 & 11 */
1095 break;
1096
1097 case SVGA3D_FORMAT_MAX: /* shut up MSC */
1098 case SVGA3D_FORMAT_INVALID:
1099 break;
1100 default: /** @todo Other formats. Avoid MSC warning for now. */
1101 break;
1102 }
1103 AssertFailedReturn(D3DFMT_UNKNOWN);
1104}
1105
1106/**
1107 * Convert SVGA multi sample count value to its D3D equivalent
1108 */
1109D3DMULTISAMPLE_TYPE vmsvga3dMultipeSampleCount2D3D(uint32_t multisampleCount)
1110{
1111 AssertCompile(D3DMULTISAMPLE_2_SAMPLES == 2);
1112 AssertCompile(D3DMULTISAMPLE_16_SAMPLES == 16);
1113
1114 if (multisampleCount > 16)
1115 return D3DMULTISAMPLE_NONE;
1116
1117 /** @todo exact same mapping as d3d? */
1118 return (D3DMULTISAMPLE_TYPE)multisampleCount;
1119}
1120
1121
1122/**
1123 * Destroy backend specific surface bits (part of SVGA_3D_CMD_SURFACE_DESTROY).
1124 *
1125 * @param pThisCC The device state.
1126 * @param pSurface The surface being destroyed.
1127 */
1128static DECLCALLBACK(void) vmsvga3dBackSurfaceDestroy(PVGASTATECC pThisCC, PVMSVGA3DSURFACE pSurface)
1129{
1130 RT_NOREF(pThisCC);
1131
1132 RTAvlU32Destroy(&pSurface->pSharedObjectTree, vmsvga3dSharedSurfaceDestroyTree, pSurface);
1133 Assert(pSurface->pSharedObjectTree == NULL);
1134
1135 switch (pSurface->enmD3DResType)
1136 {
1137 case VMSVGA3D_D3DRESTYPE_SURFACE:
1138 D3D_RELEASE(pSurface->u.pSurface);
1139 break;
1140
1141 case VMSVGA3D_D3DRESTYPE_TEXTURE:
1142 D3D_RELEASE(pSurface->u.pTexture);
1143 D3D_RELEASE(pSurface->bounce.pTexture);
1144 D3D_RELEASE(pSurface->emulated.pTexture);
1145 break;
1146
1147 case VMSVGA3D_D3DRESTYPE_CUBE_TEXTURE:
1148 D3D_RELEASE(pSurface->u.pCubeTexture);
1149 D3D_RELEASE(pSurface->bounce.pCubeTexture);
1150 D3D_RELEASE(pSurface->emulated.pCubeTexture);
1151 break;
1152
1153 case VMSVGA3D_D3DRESTYPE_VOLUME_TEXTURE:
1154 D3D_RELEASE(pSurface->u.pVolumeTexture);
1155 D3D_RELEASE(pSurface->bounce.pVolumeTexture);
1156 D3D_RELEASE(pSurface->emulated.pVolumeTexture);
1157 break;
1158
1159 case VMSVGA3D_D3DRESTYPE_VERTEX_BUFFER:
1160 D3D_RELEASE(pSurface->u.pVertexBuffer);
1161 break;
1162
1163 case VMSVGA3D_D3DRESTYPE_INDEX_BUFFER:
1164 D3D_RELEASE(pSurface->u.pIndexBuffer);
1165 break;
1166
1167 default:
1168 AssertMsg(!VMSVGA3DSURFACE_HAS_HW_SURFACE(pSurface),
1169 ("surfaceFlags=0x%x\n", (pSurface->surfaceFlags & VMSVGA3D_SURFACE_HINT_SWITCH_MASK)));
1170 break;
1171 }
1172
1173 D3D_RELEASE(pSurface->pQuery);
1174}
1175
1176
1177/*
1178 * Release all shared surface objects.
1179 */
1180DECLCALLBACK(int) vmsvga3dSharedSurfaceDestroyTree(PAVLU32NODECORE pNode, void *pvParam)
1181{
1182 PVMSVGA3DSHAREDSURFACE pSharedSurface = (PVMSVGA3DSHAREDSURFACE)pNode;
1183 PVMSVGA3DSURFACE pSurface = (PVMSVGA3DSURFACE)pvParam;
1184
1185 switch (pSurface->enmD3DResType)
1186 {
1187 case VMSVGA3D_D3DRESTYPE_TEXTURE:
1188 LogFunc(("release shared texture object for context %d\n", pNode->Key));
1189 Assert(pSharedSurface->u.pTexture);
1190 D3D_RELEASE(pSharedSurface->u.pTexture);
1191 break;
1192
1193 case VMSVGA3D_D3DRESTYPE_CUBE_TEXTURE:
1194 LogFunc(("release shared cube texture object for context %d\n", pNode->Key));
1195 Assert(pSharedSurface->u.pCubeTexture);
1196 D3D_RELEASE(pSharedSurface->u.pCubeTexture);
1197 break;
1198
1199 case VMSVGA3D_D3DRESTYPE_VOLUME_TEXTURE:
1200 LogFunc(("release shared volume texture object for context %d\n", pNode->Key));
1201 Assert(pSharedSurface->u.pVolumeTexture);
1202 D3D_RELEASE(pSharedSurface->u.pVolumeTexture);
1203 break;
1204
1205 default:
1206 AssertFailed();
1207 break;
1208 }
1209 RTMemFree(pNode);
1210 return 0;
1211}
1212
1213/* Get the shared surface copy or create a new one. */
1214static PVMSVGA3DSHAREDSURFACE vmsvga3dSurfaceGetSharedCopy(PVMSVGA3DSTATE pState, PVMSVGA3DCONTEXT pContext, PVMSVGA3DSURFACE pSurface)
1215{
1216 Assert(pSurface->hSharedObject);
1217
1218 PVMSVGA3DSHAREDSURFACE pSharedSurface = (PVMSVGA3DSHAREDSURFACE)RTAvlU32Get(&pSurface->pSharedObjectTree, pContext->id);
1219 if (!pSharedSurface)
1220 {
1221 const uint32_t cWidth = pSurface->paMipmapLevels[0].mipmapSize.width;
1222 const uint32_t cHeight = pSurface->paMipmapLevels[0].mipmapSize.height;
1223 const uint32_t cDepth = pSurface->paMipmapLevels[0].mipmapSize.depth;
1224 const uint32_t numMipLevels = pSurface->cLevels;
1225
1226 LogFunc(("Create shared %stexture copy d3d (%d,%d,%d) cMip=%d usage %x format %x.\n",
1227 pSurface->enmD3DResType == VMSVGA3D_D3DRESTYPE_VOLUME_TEXTURE ? "volume " :
1228 pSurface->enmD3DResType == VMSVGA3D_D3DRESTYPE_CUBE_TEXTURE ? "cube " :
1229 pSurface->enmD3DResType == VMSVGA3D_D3DRESTYPE_TEXTURE ? "" : "UNKNOWN!!!",
1230 cWidth,
1231 cHeight,
1232 cDepth,
1233 numMipLevels,
1234 pSurface->fUsageD3D | D3DUSAGE_RENDERTARGET,
1235 pSurface->formatD3D));
1236
1237 pSharedSurface = (PVMSVGA3DSHAREDSURFACE)RTMemAllocZ(sizeof(*pSharedSurface));
1238 AssertReturn(pSharedSurface, NULL);
1239
1240 pSharedSurface->Core.Key = pContext->id;
1241 bool ret = RTAvlU32Insert(&pSurface->pSharedObjectTree, &pSharedSurface->Core);
1242 AssertReturn(ret, NULL);
1243
1244 /* Create shadow copy of the original shared texture.
1245 * Shared d3d resources require Vista+ and have some restrictions.
1246 * D3DUSAGE_RENDERTARGET is required for use as a StretchRect destination.
1247 */
1248 HRESULT hr;
1249 if (pSurface->enmD3DResType == VMSVGA3D_D3DRESTYPE_VOLUME_TEXTURE)
1250 hr = pContext->pDevice->CreateVolumeTexture(cWidth,
1251 cHeight,
1252 cDepth,
1253 numMipLevels,
1254 pSurface->fUsageD3D | D3DUSAGE_RENDERTARGET,
1255 pSurface->formatD3D,
1256 D3DPOOL_DEFAULT,
1257 &pSharedSurface->u.pVolumeTexture,
1258 &pSurface->hSharedObject);
1259 else if (pSurface->enmD3DResType == VMSVGA3D_D3DRESTYPE_CUBE_TEXTURE)
1260 hr = pContext->pDevice->CreateCubeTexture(cWidth,
1261 numMipLevels,
1262 pSurface->fUsageD3D | D3DUSAGE_RENDERTARGET,
1263 pSurface->formatD3D,
1264 D3DPOOL_DEFAULT,
1265 &pSharedSurface->u.pCubeTexture,
1266 &pSurface->hSharedObject);
1267 else if (pSurface->enmD3DResType == VMSVGA3D_D3DRESTYPE_TEXTURE)
1268 {
1269 if (pSurface->fStencilAsTexture)
1270 {
1271 /* Use the INTZ format for a depth/stencil surface that will be used as a texture */
1272 hr = pContext->pDevice->CreateTexture(cWidth,
1273 cHeight,
1274 1, /* mip levels */
1275 D3DUSAGE_DEPTHSTENCIL,
1276 FOURCC_INTZ,
1277 D3DPOOL_DEFAULT,
1278 &pSharedSurface->u.pTexture,
1279 &pSurface->hSharedObject);
1280 }
1281 else
1282 {
1283 hr = pContext->pDevice->CreateTexture(cWidth,
1284 cHeight,
1285 numMipLevels,
1286 pSurface->fUsageD3D | D3DUSAGE_RENDERTARGET,
1287 pSurface->formatD3D,
1288 D3DPOOL_DEFAULT,
1289 &pSharedSurface->u.pTexture,
1290 &pSurface->hSharedObject);
1291 }
1292 }
1293 else
1294 hr = E_FAIL;
1295
1296 if (RT_LIKELY(hr == D3D_OK))
1297 {
1298 /* Make sure that the created shared copy has the same content as the original. */
1299 PVMSVGA3DCONTEXT pAssociatedContext;
1300 int rc = vmsvga3dContextFromCid(pState, pSurface->idAssociatedContext, &pAssociatedContext);
1301 if (RT_SUCCESS(rc))
1302 AssertStmt(pAssociatedContext->pDevice, rc = VERR_INTERNAL_ERROR);
1303 if (RT_SUCCESS(rc))
1304 {
1305 IDirect3DQuery9 *pQuery;
1306 hr = pAssociatedContext->pDevice->CreateQuery(D3DQUERYTYPE_EVENT, &pQuery);
1307 if (hr == D3D_OK)
1308 {
1309 hr = pQuery->Issue(D3DISSUE_END);
1310 if (hr == D3D_OK)
1311 {
1312 do
1313 {
1314 hr = pQuery->GetData(NULL, 0, D3DGETDATA_FLUSH);
1315 } while (hr == S_FALSE);
1316 }
1317
1318 D3D_RELEASE(pQuery);
1319 }
1320 }
1321 else
1322 AssertMsgFailed(("idAssociatedContext cid=%u, sid=%u\n", pSurface->idAssociatedContext, pSurface->id));
1323 }
1324 else
1325 {
1326 AssertMsgFailed(("CreateTexture type %d failed with %x\n", pSurface->enmD3DResType, hr));
1327 RTAvlU32Remove(&pSurface->pSharedObjectTree, pContext->id);
1328 RTMemFree(pSharedSurface);
1329 return NULL;
1330 }
1331 }
1332 return pSharedSurface;
1333}
1334
1335/* Inject a query event into the D3D pipeline so we can check when usage of this surface has finished.
1336 * (D3D does not synchronize shared surface usage)
1337 */
1338static int vmsvga3dSurfaceTrackUsage(PVMSVGA3DSTATE pState, PVMSVGA3DCONTEXT pContext, PVMSVGA3DSURFACE pSurface)
1339{
1340 RT_NOREF(pState);
1341#ifndef VBOX_VMSVGA3D_WITH_WINE_OPENGL
1342 Assert(pSurface->id != SVGA3D_INVALID_ID);
1343
1344 /* Nothing to do if this surface hasn't been shared. */
1345 if (pSurface->pSharedObjectTree == NULL)
1346 return VINF_SUCCESS;
1347
1348 LogFunc(("track usage of sid=%u (cid=%u) for cid=%u, pQuery %p\n", pSurface->id, pSurface->idAssociatedContext, pContext->id, pSurface->pQuery));
1349
1350 if (pSurface->idQueryContext == pContext->id)
1351 {
1352 /* Release the previous query object, if any. */
1353 D3D_RELEASE(pSurface->pQuery);
1354 }
1355 else
1356 {
1357 /* Different context. There must be no pending drawing operations. If there are any, then a flush is missing. */
1358 if (pSurface->pQuery)
1359 {
1360 /* Should not happen. */
1361 AssertFailed();
1362
1363 /* Make sure that all drawing has completed. */
1364 vmsvga3dSurfaceFlush(pSurface);
1365 }
1366 pSurface->idQueryContext = pContext->id;
1367 }
1368
1369 HRESULT hr = pContext->pDevice->CreateQuery(D3DQUERYTYPE_EVENT, &pSurface->pQuery);
1370 AssertMsgReturn(hr == D3D_OK, ("vmsvga3dSurfaceTrackUsage: CreateQuery failed with %x\n", hr), VERR_INTERNAL_ERROR);
1371
1372 hr = pSurface->pQuery->Issue(D3DISSUE_END);
1373 AssertMsgReturn(hr == D3D_OK, ("vmsvga3dSurfaceTrackUsage: Issue failed with %x\n", hr), VERR_INTERNAL_ERROR);
1374#endif /* !VBOX_VMSVGA3D_WITH_WINE_OPENGL */
1375
1376 return VINF_SUCCESS;
1377}
1378
1379
1380/**
1381 * Surface ID based version of vmsvga3dSurfaceTrackUsage.
1382 *
1383 * @returns VBox status code.
1384 * @param pState The VMSVGA3d state.
1385 * @param pContext The context.
1386 * @param sid The surface ID.
1387 */
1388static int vmsvga3dSurfaceTrackUsageById(PVMSVGA3DSTATE pState, PVMSVGA3DCONTEXT pContext, uint32_t sid)
1389{
1390 PVMSVGA3DSURFACE pSurface;
1391 int rc = vmsvga3dSurfaceFromSid(pState, sid, &pSurface);
1392 AssertRCReturn(rc, rc);
1393
1394 return vmsvga3dSurfaceTrackUsage(pState, pContext, pSurface);
1395}
1396
1397
1398/* Wait for all drawing, that uses this surface, to finish. */
1399int vmsvga3dSurfaceFlush(PVMSVGA3DSURFACE pSurface)
1400{
1401#ifndef VBOX_VMSVGA3D_WITH_WINE_OPENGL
1402 HRESULT hr;
1403
1404 if (!pSurface->pQuery)
1405 {
1406 LogFlow(("vmsvga3dSurfaceFlush: no query object\n"));
1407 return VINF_SUCCESS; /* nothing to wait for */
1408 }
1409 Assert(pSurface->pSharedObjectTree);
1410
1411 Log(("vmsvga3dSurfaceFlush: wait for draw to finish (sid=%u)\n", pSurface->id));
1412 while (true)
1413 {
1414 hr = pSurface->pQuery->GetData(NULL, 0, D3DGETDATA_FLUSH);
1415 if (hr != S_FALSE) break;
1416
1417 RTThreadSleep(1);
1418 }
1419
1420 D3D_RELEASE(pSurface->pQuery);
1421
1422 AssertMsgReturn(hr == S_OK, ("vmsvga3dSurfaceFinishDrawing: GetData failed with %x\n", hr), VERR_INTERNAL_ERROR);
1423#endif /* !VBOX_VMSVGA3D_WITH_WINE_OPENGL */
1424
1425 return VINF_SUCCESS;
1426}
1427
1428/** Get IDirect3DSurface9 for the given face and mipmap.
1429 */
1430int vmsvga3dGetD3DSurface(PVMSVGA3DSTATE pState,
1431 PVMSVGA3DCONTEXT pContext,
1432 PVMSVGA3DSURFACE pSurface,
1433 uint32_t face,
1434 uint32_t mipmap,
1435 bool fLockable,
1436 IDirect3DSurface9 **ppD3DSurf)
1437{
1438 AssertPtrReturn(pSurface->u.pSurface, VERR_INVALID_PARAMETER);
1439
1440 IDirect3DBaseTexture9 *pTexture;
1441 if (fLockable && pSurface->bounce.pTexture)
1442 pTexture = pSurface->bounce.pTexture;
1443 else
1444 pTexture = pSurface->u.pTexture;
1445
1446#ifndef VBOX_VMSVGA3D_WITH_WINE_OPENGL
1447 if (pSurface->idAssociatedContext != pContext->id)
1448 {
1449 AssertMsgReturn(!fLockable,
1450 ("Lockable surface must be from the same context (surface cid = %d, req cid = %d)",
1451 pSurface->idAssociatedContext, pContext->id),
1452 VERR_INVALID_PARAMETER);
1453
1454 if ( pSurface->enmD3DResType == VMSVGA3D_D3DRESTYPE_TEXTURE
1455 || pSurface->enmD3DResType == VMSVGA3D_D3DRESTYPE_CUBE_TEXTURE)
1456 {
1457 LogFunc(("using texture sid=%u created for another context (%d vs %d)\n",
1458 pSurface->id, pSurface->idAssociatedContext, pContext->id));
1459
1460 PVMSVGA3DSHAREDSURFACE pSharedSurface = vmsvga3dSurfaceGetSharedCopy(pState, pContext, pSurface);
1461 AssertReturn(pSharedSurface, VERR_INTERNAL_ERROR);
1462
1463 pTexture = pSharedSurface->u.pTexture;
1464 }
1465 else
1466 {
1467 AssertMsgFailed(("surface sid=%u created for another context (%d vs %d)\n",
1468 pSurface->id, pSurface->idAssociatedContext, pContext->id));
1469 }
1470 }
1471#else
1472 RT_NOREF(pContext);
1473#endif
1474
1475 if (pSurface->enmD3DResType == VMSVGA3D_D3DRESTYPE_CUBE_TEXTURE)
1476 {
1477 Assert(pSurface->cFaces == 6);
1478
1479 IDirect3DCubeTexture9 *p = (IDirect3DCubeTexture9 *)pTexture;
1480 D3DCUBEMAP_FACES FaceType = vmsvga3dCubemapFaceFromIndex(face);
1481 HRESULT hr = p->GetCubeMapSurface(FaceType, mipmap, ppD3DSurf);
1482 AssertMsgReturn(hr == D3D_OK, ("GetCubeMapSurface failed with %x\n", hr), VERR_INTERNAL_ERROR);
1483 }
1484 else if (pSurface->enmD3DResType == VMSVGA3D_D3DRESTYPE_TEXTURE)
1485 {
1486 Assert(pSurface->cFaces == 1);
1487 Assert(face == 0);
1488
1489 IDirect3DTexture9 *p = (IDirect3DTexture9 *)pTexture;
1490 HRESULT hr = p->GetSurfaceLevel(mipmap, ppD3DSurf);
1491 AssertMsgReturn(hr == D3D_OK, ("GetSurfaceLevel failed with %x\n", hr), VERR_INTERNAL_ERROR);
1492 }
1493 else if (pSurface->enmD3DResType == VMSVGA3D_D3DRESTYPE_SURFACE)
1494 {
1495 pSurface->u.pSurface->AddRef();
1496 *ppD3DSurf = pSurface->u.pSurface;
1497 }
1498 else
1499 {
1500 AssertMsgFailedReturn(("No surface for type %d\n", pSurface->enmD3DResType), VERR_INTERNAL_ERROR);
1501 }
1502
1503 return VINF_SUCCESS;
1504}
1505
1506static DECLCALLBACK(int) vmsvga3dBackSurfaceCopy(PVGASTATECC pThisCC, SVGA3dSurfaceImageId dest, SVGA3dSurfaceImageId src,
1507 uint32_t cCopyBoxes, SVGA3dCopyBox *pBox)
1508{
1509 PVMSVGA3DSTATE pState = pThisCC->svga.p3dState;
1510 AssertReturn(pState, VERR_NO_MEMORY);
1511
1512 const uint32_t sidSrc = src.sid;
1513 const uint32_t sidDest = dest.sid;
1514 int rc;
1515
1516 PVMSVGA3DSURFACE pSurfaceSrc;
1517 rc = vmsvga3dSurfaceFromSid(pState, sidSrc, &pSurfaceSrc);
1518 AssertRCReturn(rc, rc);
1519
1520 PVMSVGA3DSURFACE pSurfaceDest;
1521 rc = vmsvga3dSurfaceFromSid(pState, sidDest, &pSurfaceDest);
1522 AssertRCReturn(rc, rc);
1523
1524 PVMSVGA3DMIPMAPLEVEL pMipmapLevelSrc;
1525 rc = vmsvga3dMipmapLevel(pSurfaceSrc, src.face, src.mipmap, &pMipmapLevelSrc);
1526 AssertRCReturn(rc, rc);
1527
1528 PVMSVGA3DMIPMAPLEVEL pMipmapLevelDest;
1529 rc = vmsvga3dMipmapLevel(pSurfaceDest, dest.face, dest.mipmap, &pMipmapLevelDest);
1530 AssertRCReturn(rc, rc);
1531
1532 /* If src is HW and dst is not, then create the dst texture. */
1533 if ( pSurfaceSrc->u.pSurface
1534 && !pSurfaceDest->u.pSurface
1535 && RT_BOOL(pSurfaceDest->surfaceFlags & SVGA3D_SURFACE_HINT_TEXTURE))
1536 {
1537 /* Create the destination texture in the same context as the source texture. */
1538 uint32_t const cidSrc = pSurfaceSrc->idAssociatedContext;
1539
1540 PVMSVGA3DCONTEXT pContextSrc;
1541 rc = vmsvga3dContextFromCid(pState, cidSrc, &pContextSrc);
1542 AssertRCReturn(rc, rc);
1543 AssertReturn(pContextSrc->pDevice, VERR_INTERNAL_ERROR);
1544
1545 LogFunc(("sid=%u type=%x format=%d -> create dest texture\n", sidDest, pSurfaceDest->surfaceFlags, pSurfaceDest->format));
1546 rc = vmsvga3dBackCreateTexture(pThisCC, pContextSrc, cidSrc, pSurfaceDest);
1547 AssertRCReturn(rc, rc);
1548 }
1549
1550 Assert(pSurfaceSrc->enmD3DResType != VMSVGA3D_D3DRESTYPE_VOLUME_TEXTURE); /// @todo
1551 Assert(pSurfaceDest->enmD3DResType != VMSVGA3D_D3DRESTYPE_VOLUME_TEXTURE); /// @todo
1552
1553 if ( pSurfaceSrc->u.pSurface
1554 && pSurfaceDest->u.pSurface)
1555 {
1556 /* Both surfaces in hardware. Use the src context to copy one to another, because the src context may be needed
1557 * to copy data from source texture to the source bounce texture. while only the shared hardware surface is required
1558 * from the dst context.
1559 */
1560 uint32_t const cidSrc = pSurfaceSrc->idAssociatedContext;
1561
1562 PVMSVGA3DCONTEXT pContextSrc;
1563 rc = vmsvga3dContextFromCid(pState, cidSrc, &pContextSrc);
1564 AssertRCReturn(rc, rc);
1565 AssertReturn(pContextSrc->pDevice, VERR_INTERNAL_ERROR);
1566
1567 /* Must flush the other context's 3d pipeline to make sure all drawing is complete for the surface we're about to use. */
1568 vmsvga3dSurfaceFlush(pSurfaceSrc);
1569 vmsvga3dSurfaceFlush(pSurfaceDest);
1570
1571 IDirect3DSurface9 *pSrc;
1572 rc = vmsvga3dGetD3DSurface(pState, pContextSrc, pSurfaceSrc, src.face, src.mipmap, false, &pSrc);
1573 AssertRCReturn(rc, rc);
1574
1575 IDirect3DSurface9 *pDest;
1576 rc = vmsvga3dGetD3DSurface(pState, pContextSrc, pSurfaceDest, dest.face, dest.mipmap, false, &pDest);
1577 AssertRCReturnStmt(rc, D3D_RELEASE(pSrc), rc);
1578
1579 for (uint32_t i = 0; i < cCopyBoxes; ++i)
1580 {
1581 SVGA3dCopyBox clipBox = pBox[i];
1582 vmsvgaR3ClipCopyBox(&pMipmapLevelSrc->mipmapSize, &pMipmapLevelDest->mipmapSize, &clipBox);
1583 if ( !clipBox.w
1584 || !clipBox.h
1585 || !clipBox.d)
1586 {
1587 LogFunc(("Skipped empty box.\n"));
1588 continue;
1589 }
1590
1591 RECT RectSrc;
1592 RectSrc.left = clipBox.srcx;
1593 RectSrc.top = clipBox.srcy;
1594 RectSrc.right = clipBox.srcx + clipBox.w; /* exclusive */
1595 RectSrc.bottom = clipBox.srcy + clipBox.h; /* exclusive */
1596
1597 RECT RectDest;
1598 RectDest.left = clipBox.x;
1599 RectDest.top = clipBox.y;
1600 RectDest.right = clipBox.x + clipBox.w; /* exclusive */
1601 RectDest.bottom = clipBox.y + clipBox.h; /* exclusive */
1602
1603 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));
1604
1605 if ( sidSrc == sidDest
1606 && clipBox.srcx == clipBox.x
1607 && clipBox.srcy == clipBox.y)
1608 {
1609 LogFunc(("redundant copy to the same surface at the same coordinates. Ignore.\n"));
1610 continue;
1611 }
1612 Assert(sidSrc != sidDest);
1613 Assert(!clipBox.srcz && !clipBox.z);
1614
1615 HRESULT hr = pContextSrc->pDevice->StretchRect(pSrc, &RectSrc, pDest, &RectDest, D3DTEXF_NONE);
1616 if (hr != D3D_OK)
1617 {
1618 /* This can happen for compressed texture formats for example. */
1619 LogFunc(("StretchRect failed with %x. Try a slow path.\n", hr));
1620 if ( pSurfaceSrc->bounce.pTexture
1621 && (pSurfaceSrc->fUsageD3D & D3DUSAGE_RENDERTARGET))
1622 {
1623 /* Copy the source texture mipmap level to the source bounce texture. */
1624 hr = D3D9GetRenderTargetData(pContextSrc, pSurfaceSrc, src.face, src.mipmap);
1625 AssertMsg(hr == D3D_OK, ("D3D9GetRenderTargetData failed with %x\n", hr));
1626 if (hr == D3D_OK)
1627 {
1628 /* Copy the source bounce texture to the destination surface. */
1629 IDirect3DSurface9 *pSrcBounce;
1630 rc = vmsvga3dGetD3DSurface(pState, pContextSrc, pSurfaceSrc, src.face, src.mipmap, true, &pSrcBounce);
1631 if (RT_SUCCESS(rc))
1632 {
1633 POINT pointDest;
1634 pointDest.x = clipBox.x;
1635 pointDest.y = clipBox.y;
1636
1637 hr = pContextSrc->pDevice->UpdateSurface(pSrcBounce, &RectSrc, pDest, &pointDest);
1638 Assert(hr == D3D_OK);
1639
1640 D3D_RELEASE(pSrcBounce);
1641 }
1642 else
1643 {
1644 AssertRC(rc);
1645 hr = E_INVALIDARG;
1646 }
1647 }
1648 }
1649 else if ( (pSurfaceSrc->fUsageD3D & D3DUSAGE_RENDERTARGET) == 0
1650 && (pSurfaceDest->fUsageD3D & D3DUSAGE_RENDERTARGET) == 0)
1651 {
1652 /* Can lock both. */
1653 D3DLOCKED_RECT LockedSrcRect;
1654 hr = pSrc->LockRect(&LockedSrcRect, &RectSrc, D3DLOCK_READONLY);
1655 Assert(hr == D3D_OK);
1656 if (SUCCEEDED(hr))
1657 {
1658 D3DLOCKED_RECT LockedDestRect;
1659 hr = pDest->LockRect(&LockedDestRect, &RectDest, 0);
1660 Assert(hr == D3D_OK);
1661 if (SUCCEEDED(hr))
1662 {
1663 uint32_t cBlocksX = (clipBox.w + pSurfaceSrc->cxBlock - 1) / pSurfaceSrc->cxBlock;
1664 uint32_t cBlocksY = (clipBox.h + pSurfaceSrc->cyBlock - 1) / pSurfaceSrc->cyBlock;
1665
1666 uint32_t cbToCopy = cBlocksX * pSurfaceSrc->cbBlock;
1667 cbToCopy = RT_MIN(cbToCopy, (uint32_t)RT_ABS(LockedDestRect.Pitch));
1668 cbToCopy = RT_MIN(cbToCopy, (uint32_t)RT_ABS(LockedSrcRect.Pitch));
1669
1670 uint8_t *pu8Dst = (uint8_t *)LockedDestRect.pBits;
1671 const uint8_t *pu8Src = (uint8_t *)LockedSrcRect.pBits;
1672 for (uint32_t j = 0; j < cBlocksY; ++j)
1673 {
1674 memcpy(pu8Dst, pu8Src, cbToCopy);
1675 pu8Dst += LockedDestRect.Pitch;
1676 pu8Src += LockedSrcRect.Pitch;
1677 }
1678
1679 hr = pDest->UnlockRect();
1680 Assert(hr == D3D_OK);
1681 }
1682
1683 hr = pSrc->UnlockRect();
1684 Assert(hr == D3D_OK);
1685 }
1686 }
1687 }
1688 AssertMsgReturnStmt(hr == D3D_OK,
1689 ("StretchRect failed with %x\n", hr),
1690 D3D_RELEASE(pDest); D3D_RELEASE(pSrc),
1691 VERR_INTERNAL_ERROR);
1692 }
1693
1694 D3D_RELEASE(pDest);
1695 D3D_RELEASE(pSrc);
1696
1697 /* Track the StretchRect operation. */
1698 vmsvga3dSurfaceTrackUsage(pState, pContextSrc, pSurfaceSrc);
1699 vmsvga3dSurfaceTrackUsage(pState, pContextSrc, pSurfaceDest);
1700 }
1701 else
1702 {
1703 /* One of the surfaces is in memory.
1704 *
1705 * Copy from/to memory to/from a HW surface. Or mem->mem.
1706 * Use the context of the HW surface, if any.
1707 */
1708 PVMSVGA3DCONTEXT pContext = NULL;
1709 IDirect3DSurface9 *pD3DSurf = NULL;
1710
1711 if (pSurfaceSrc->u.pSurface)
1712 {
1713 AssertReturn(!pSurfaceDest->u.pSurface, VERR_INTERNAL_ERROR);
1714
1715 rc = vmsvga3dContextFromCid(pState, pSurfaceSrc->idAssociatedContext, &pContext);
1716 AssertRCReturn(rc, rc);
1717 AssertReturn(pContext->pDevice, VERR_INTERNAL_ERROR);
1718
1719 rc = vmsvga3dGetD3DSurface(pState, pContext, pSurfaceSrc, src.face, src.mipmap, true, &pD3DSurf);
1720 AssertRCReturn(rc, rc);
1721 }
1722 else if (pSurfaceDest->u.pSurface)
1723 {
1724 AssertReturn(!pSurfaceSrc->u.pSurface, VERR_INTERNAL_ERROR);
1725
1726 rc = vmsvga3dContextFromCid(pState, pSurfaceDest->idAssociatedContext, &pContext);
1727 AssertRCReturn(rc, rc);
1728 AssertReturn(pContext->pDevice, VERR_INTERNAL_ERROR);
1729
1730 rc = vmsvga3dGetD3DSurface(pState, pContext, pSurfaceDest, dest.face, dest.mipmap, true, &pD3DSurf);
1731 AssertRCReturn(rc, rc);
1732 }
1733
1734 for (uint32_t i = 0; i < cCopyBoxes; ++i)
1735 {
1736 HRESULT hr;
1737
1738 SVGA3dCopyBox clipBox = pBox[i];
1739 vmsvgaR3ClipCopyBox(&pMipmapLevelSrc->mipmapSize, &pMipmapLevelDest->mipmapSize, &clipBox);
1740 if ( !clipBox.w
1741 || !clipBox.h
1742 || !clipBox.d)
1743 {
1744 LogFunc(("Skipped empty box.\n"));
1745 continue;
1746 }
1747
1748 RECT RectSrc;
1749 RectSrc.left = clipBox.srcx;
1750 RectSrc.top = clipBox.srcy;
1751 RectSrc.right = clipBox.srcx + clipBox.w; /* exclusive */
1752 RectSrc.bottom = clipBox.srcy + clipBox.h; /* exclusive */
1753
1754 RECT RectDest;
1755 RectDest.left = clipBox.x;
1756 RectDest.top = clipBox.y;
1757 RectDest.right = clipBox.x + clipBox.w; /* exclusive */
1758 RectDest.bottom = clipBox.y + clipBox.h; /* exclusive */
1759
1760 LogFunc(("(manual) copy sid=%u face=%d mipmap=%d (%d,%d)(%d,%d) to sid=%u face=%d mipmap=%d (%d,%d)\n",
1761 sidSrc, src.face, src.mipmap, RectSrc.left, RectSrc.top, RectSrc.right, RectSrc.bottom,
1762 sidDest, dest.face, dest.mipmap, pBox[i].x, pBox[i].y));
1763
1764 Assert(!clipBox.srcz && !clipBox.z);
1765 Assert(pSurfaceSrc->cbBlock == pSurfaceDest->cbBlock);
1766 Assert(pSurfaceSrc->cxBlock == pSurfaceDest->cxBlock);
1767 Assert(pSurfaceSrc->cyBlock == pSurfaceDest->cyBlock);
1768
1769 uint32_t cBlocksX = (clipBox.w + pSurfaceSrc->cxBlock - 1) / pSurfaceSrc->cxBlock;
1770 uint32_t cBlocksY = (clipBox.h + pSurfaceSrc->cyBlock - 1) / pSurfaceSrc->cyBlock;
1771
1772 D3DLOCKED_RECT LockedSrcRect;
1773 if (!pSurfaceSrc->u.pSurface)
1774 {
1775 uint32_t u32BlockX = clipBox.srcx / pSurfaceSrc->cxBlock;
1776 uint32_t u32BlockY = clipBox.srcy / pSurfaceSrc->cyBlock;
1777 Assert(u32BlockX * pSurfaceSrc->cxBlock == clipBox.srcx);
1778 Assert(u32BlockY * pSurfaceSrc->cyBlock == clipBox.srcy);
1779
1780 LockedSrcRect.pBits = (uint8_t *)pMipmapLevelSrc->pSurfaceData +
1781 pMipmapLevelSrc->cbSurfacePitch * u32BlockY + pSurfaceSrc->cbBlock * u32BlockX;
1782 LockedSrcRect.Pitch = pMipmapLevelSrc->cbSurfacePitch;
1783 }
1784 else
1785 {
1786 /* Must flush the context's 3d pipeline to make sure all drawing is complete for the surface we're about to use. */
1787 vmsvga3dSurfaceFlush(pSurfaceSrc);
1788
1789 hr = pD3DSurf->LockRect(&LockedSrcRect, &RectSrc, D3DLOCK_READONLY);
1790 AssertMsgReturnStmt(hr == D3D_OK, ("LockRect failed with %x\n", hr), D3D_RELEASE(pD3DSurf), VERR_INTERNAL_ERROR);
1791 }
1792
1793 D3DLOCKED_RECT LockedDestRect;
1794 if (!pSurfaceDest->u.pSurface)
1795 {
1796 uint32_t u32BlockX = clipBox.x / pSurfaceDest->cxBlock;
1797 uint32_t u32BlockY = clipBox.y / pSurfaceDest->cyBlock;
1798 Assert(u32BlockX * pSurfaceDest->cxBlock == clipBox.x);
1799 Assert(u32BlockY * pSurfaceDest->cyBlock == clipBox.y);
1800
1801 LockedDestRect.pBits = (uint8_t *)pMipmapLevelDest->pSurfaceData +
1802 pMipmapLevelDest->cbSurfacePitch * u32BlockY + pSurfaceDest->cbBlock * u32BlockX;
1803 LockedDestRect.Pitch = pMipmapLevelDest->cbSurfacePitch;
1804 pSurfaceDest->fDirty = true;
1805 }
1806 else
1807 {
1808 /* Must flush the context's 3d pipeline to make sure all drawing is complete for the surface we're about to use. */
1809 vmsvga3dSurfaceFlush(pSurfaceDest);
1810
1811 hr = pD3DSurf->LockRect(&LockedDestRect, &RectDest, 0);
1812 AssertMsgReturnStmt(hr == D3D_OK, ("LockRect failed with %x\n", hr), D3D_RELEASE(pD3DSurf), VERR_INTERNAL_ERROR);
1813 }
1814
1815 uint8_t *pDest = (uint8_t *)LockedDestRect.pBits;
1816 const uint8_t *pSrc = (uint8_t *)LockedSrcRect.pBits;
1817 for (uint32_t j = 0; j < cBlocksY; ++j)
1818 {
1819 memcpy(pDest, pSrc, cBlocksX * pSurfaceSrc->cbBlock);
1820 pDest += LockedDestRect.Pitch;
1821 pSrc += LockedSrcRect.Pitch;
1822 }
1823
1824 if (pD3DSurf)
1825 {
1826 hr = pD3DSurf->UnlockRect();
1827 AssertMsgReturnStmt(hr == D3D_OK, ("Unlock failed with %x\n", hr), D3D_RELEASE(pD3DSurf), VERR_INTERNAL_ERROR);
1828 }
1829 }
1830
1831 D3D_RELEASE(pD3DSurf);
1832
1833 /* If the destination bounce texture has been used, then update the actual destination texture. */
1834 if ( pSurfaceDest->u.pTexture
1835 && pSurfaceDest->bounce.pTexture
1836 && ( pSurfaceDest->enmD3DResType == VMSVGA3D_D3DRESTYPE_TEXTURE
1837 || pSurfaceDest->enmD3DResType == VMSVGA3D_D3DRESTYPE_CUBE_TEXTURE))
1838 {
1839 AssertMsgReturn(pContext, ("Context is NULL\n"), VERR_INTERNAL_ERROR);
1840
1841 /* Copy the new content to the actual texture object. */
1842 HRESULT hr2 = D3D9UpdateTexture(pContext, pSurfaceDest);
1843 AssertMsg(hr2 == D3D_OK, ("UpdateTexture failed with %x\n", hr2)); RT_NOREF(hr2);
1844
1845 /* Track the UpdateTexture operation. */
1846 vmsvga3dSurfaceTrackUsage(pState, pContext, pSurfaceDest);
1847 }
1848 }
1849
1850 return VINF_SUCCESS;
1851}
1852
1853
1854/**
1855 * Create D3D/OpenGL texture object for the specified surface.
1856 *
1857 * Surfaces are created when needed.
1858 *
1859 * @param pThisCC The device context.
1860 * @param pContext The context.
1861 * @param idAssociatedContext Probably the same as pContext->id.
1862 * @param pSurface The surface to create the texture for.
1863 */
1864static DECLCALLBACK(int) vmsvga3dBackCreateTexture(PVGASTATECC pThisCC, PVMSVGA3DCONTEXT pContext, uint32_t idAssociatedContext,
1865 PVMSVGA3DSURFACE pSurface)
1866
1867{
1868 PVMSVGA3DSTATE pState = pThisCC->svga.p3dState;
1869 AssertReturn(pState, VERR_NO_MEMORY);
1870
1871 HRESULT hr;
1872
1873 LogFunc(("sid=%u\n", pSurface->id));
1874
1875 Assert(pSurface->hSharedObject == NULL);
1876 Assert(pSurface->u.pTexture == NULL);
1877 Assert(pSurface->bounce.pTexture == NULL);
1878 Assert(pSurface->enmD3DResType == VMSVGA3D_D3DRESTYPE_NONE);
1879
1880 const uint32_t cWidth = pSurface->paMipmapLevels[0].mipmapSize.width;
1881 const uint32_t cHeight = pSurface->paMipmapLevels[0].mipmapSize.height;
1882 const uint32_t cDepth = pSurface->paMipmapLevels[0].mipmapSize.depth;
1883 const uint32_t numMipLevels = pSurface->cLevels;
1884
1885 /*
1886 * Create D3D texture object.
1887 */
1888 if (pSurface->surfaceFlags & SVGA3D_SURFACE_CUBEMAP)
1889 {
1890 Assert(pSurface->cFaces == 6);
1891 Assert(cWidth == cHeight);
1892 Assert(cDepth == 1);
1893
1894 hr = pContext->pDevice->CreateCubeTexture(cWidth,
1895 numMipLevels,
1896 pSurface->fUsageD3D,
1897 pSurface->formatD3D,
1898 D3DPOOL_DEFAULT,
1899 &pSurface->u.pCubeTexture,
1900 &pSurface->hSharedObject);
1901 if (hr == D3D_OK)
1902 {
1903 /* Create another texture object to serve as a bounce buffer as the above texture surface can't be locked. */
1904 hr = pContext->pDevice->CreateCubeTexture(cWidth,
1905 numMipLevels,
1906 (pSurface->fUsageD3D & ~D3DUSAGE_RENDERTARGET) | D3DUSAGE_DYNAMIC /* Lockable */,
1907 pSurface->formatD3D,
1908 D3DPOOL_SYSTEMMEM,
1909 &pSurface->bounce.pCubeTexture,
1910 NULL);
1911 AssertMsgReturnStmt(hr == D3D_OK,
1912 ("CreateCubeTexture (systemmem) failed with %x\n", hr),
1913 D3D_RELEASE(pSurface->u.pCubeTexture),
1914 VERR_INTERNAL_ERROR);
1915 }
1916 else
1917 {
1918 Log(("Format not accepted -> try old method\n"));
1919 /* The format was probably not accepted; fall back to our old mode. */
1920 hr = pContext->pDevice->CreateCubeTexture(cWidth,
1921 numMipLevels,
1922 (pSurface->fUsageD3D & ~D3DUSAGE_RENDERTARGET) | D3DUSAGE_DYNAMIC /* Lockable */,
1923 pSurface->formatD3D,
1924 D3DPOOL_DEFAULT,
1925 &pSurface->u.pCubeTexture,
1926 &pSurface->hSharedObject);
1927 AssertMsgReturn(hr == D3D_OK, ("CreateCubeTexture (fallback) failed with %x\n", hr), VERR_INTERNAL_ERROR);
1928 }
1929
1930 pSurface->enmD3DResType = VMSVGA3D_D3DRESTYPE_CUBE_TEXTURE;
1931 }
1932 else if ( pSurface->formatD3D == D3DFMT_D24S8
1933 || pSurface->formatD3D == D3DFMT_D24X8
1934 || pSurface->formatD3D == D3DFMT_D32
1935 || pSurface->formatD3D == D3DFMT_D16)
1936 {
1937 Assert(pSurface->cFaces == 1);
1938 Assert(pSurface->cLevels == 1);
1939 Assert(cDepth == 1);
1940
1941 /* Use the INTZ format for a depth/stencil surface that will be used as a texture */
1942 hr = pContext->pDevice->CreateTexture(cWidth,
1943 cHeight,
1944 1, /* mip levels */
1945 D3DUSAGE_DEPTHSTENCIL,
1946 FOURCC_INTZ,
1947 D3DPOOL_DEFAULT,
1948 &pSurface->u.pTexture,
1949 &pSurface->hSharedObject /* might result in poor performance */);
1950 if ( hr == D3D_OK
1951 && ( pSurface->formatD3D == D3DFMT_D24S8
1952 || pSurface->formatD3D == D3DFMT_D24X8))
1953 {
1954 /* Create another texture object to serve as a bounce buffer as the
1955 * D3DFMT_D24S8 and D3DFMT_D24X8 surface can't be locked apparently (from testing).
1956 */
1957 hr = pContext->pDevice->CreateTexture(cWidth,
1958 cHeight,
1959 1, /* mip levels */
1960 D3DUSAGE_DYNAMIC /* Lockable */,
1961 FOURCC_INTZ,
1962 D3DPOOL_SYSTEMMEM,
1963 &pSurface->bounce.pTexture,
1964 NULL);
1965
1966 AssertMsgReturnStmt(hr == D3D_OK,
1967 ("CreateTexture (systemmem) failed with %x\n", hr),
1968 D3D_RELEASE(pSurface->u.pTexture),
1969 VERR_INTERNAL_ERROR);
1970 }
1971
1972 AssertMsgReturn(hr == D3D_OK, ("CreateTexture INTZ failed with %x\n", hr), VERR_INTERNAL_ERROR);
1973
1974 pSurface->fStencilAsTexture = true;
1975 pSurface->enmD3DResType = VMSVGA3D_D3DRESTYPE_TEXTURE;
1976 }
1977 else
1978 {
1979 if (cDepth > 1)
1980 {
1981 hr = pContext->pDevice->CreateVolumeTexture(cWidth,
1982 cHeight,
1983 cDepth,
1984 numMipLevels,
1985 pSurface->fUsageD3D,
1986 pSurface->formatD3D,
1987 D3DPOOL_DEFAULT,
1988 &pSurface->u.pVolumeTexture,
1989 &pSurface->hSharedObject);
1990 if (hr == D3D_OK)
1991 {
1992 /* Create another texture object to serve as a bounce buffer as the above texture surface can't be locked. */
1993 hr = pContext->pDevice->CreateVolumeTexture(cWidth,
1994 cHeight,
1995 cDepth,
1996 numMipLevels,
1997 (pSurface->fUsageD3D & ~D3DUSAGE_RENDERTARGET) | D3DUSAGE_DYNAMIC /* Lockable */,
1998 pSurface->formatD3D,
1999 D3DPOOL_SYSTEMMEM,
2000 &pSurface->bounce.pVolumeTexture,
2001 NULL);
2002 AssertMsgReturnStmt(hr == D3D_OK,
2003 ("CreateVolumeTexture (systemmem) failed with %x\n", hr),
2004 D3D_RELEASE(pSurface->u.pVolumeTexture),
2005 VERR_INTERNAL_ERROR);
2006 }
2007 else
2008 {
2009 Log(("Format not accepted -> try old method\n"));
2010 /* The format was probably not accepted; fall back to our old mode. */
2011 hr = pContext->pDevice->CreateVolumeTexture(cWidth,
2012 cHeight,
2013 cDepth,
2014 numMipLevels,
2015 (pSurface->fUsageD3D & ~D3DUSAGE_RENDERTARGET) | D3DUSAGE_DYNAMIC /* Lockable */,
2016 pSurface->formatD3D,
2017 D3DPOOL_DEFAULT,
2018 &pSurface->u.pVolumeTexture,
2019 &pSurface->hSharedObject);
2020 AssertMsgReturn(hr == D3D_OK, ("CreateVolumeTexture (fallback) failed with %x\n", hr), VERR_INTERNAL_ERROR);
2021 }
2022
2023 pSurface->enmD3DResType = VMSVGA3D_D3DRESTYPE_VOLUME_TEXTURE;
2024 }
2025 else
2026 {
2027 Assert(pSurface->cFaces == 1);
2028
2029 hr = pContext->pDevice->CreateTexture(cWidth,
2030 cHeight,
2031 numMipLevels,
2032 pSurface->fUsageD3D | D3DUSAGE_RENDERTARGET /* required for use as a StretchRect destination */,
2033 pSurface->formatD3D,
2034 D3DPOOL_DEFAULT,
2035 &pSurface->u.pTexture,
2036 &pSurface->hSharedObject);
2037 if (hr == D3D_OK)
2038 {
2039 /* Create another texture object to serve as a bounce buffer as the above texture surface can't be locked. */
2040 hr = pContext->pDevice->CreateTexture(cWidth,
2041 cHeight,
2042 numMipLevels,
2043 (pSurface->fUsageD3D & ~D3DUSAGE_RENDERTARGET) | D3DUSAGE_DYNAMIC /* Lockable */,
2044 pSurface->formatD3D,
2045 D3DPOOL_SYSTEMMEM,
2046 &pSurface->bounce.pTexture,
2047 NULL);
2048
2049 AssertMsgReturn(hr == D3D_OK, ("CreateTexture (systemmem) failed with %x\n", hr), VERR_INTERNAL_ERROR);
2050
2051 if (pSurface->formatD3D != pSurface->d3dfmtRequested)
2052 {
2053 /* Create a staging texture/render target for format conversion. */
2054 hr = pContext->pDevice->CreateTexture(cWidth,
2055 cHeight,
2056 numMipLevels,
2057 pSurface->fUsageD3D | D3DUSAGE_RENDERTARGET,
2058 pSurface->formatD3D,
2059 D3DPOOL_DEFAULT,
2060 &pSurface->emulated.pTexture,
2061 NULL);
2062 AssertMsgReturn(hr == D3D_OK, ("CreateTexture (emulated) failed with %x\n", hr), VERR_INTERNAL_ERROR);
2063 }
2064 }
2065 else
2066 {
2067 Log(("Format not accepted (%x) -> try old method\n", hr));
2068 /* The format was probably not accepted; fall back to our old mode. */
2069 hr = pContext->pDevice->CreateTexture(cWidth,
2070 cHeight,
2071 numMipLevels,
2072 (pSurface->fUsageD3D & ~D3DUSAGE_RENDERTARGET) | D3DUSAGE_DYNAMIC /* Lockable */,
2073 pSurface->formatD3D,
2074 D3DPOOL_DEFAULT,
2075 &pSurface->u.pTexture,
2076 &pSurface->hSharedObject /* might result in poor performance */);
2077 AssertMsgReturn(hr == D3D_OK, ("CreateTexture failed with %x\n", hr), VERR_INTERNAL_ERROR);
2078 }
2079
2080 pSurface->enmD3DResType = VMSVGA3D_D3DRESTYPE_TEXTURE;
2081 }
2082 }
2083
2084 Assert(hr == D3D_OK);
2085
2086 if (pSurface->autogenFilter != SVGA3D_TEX_FILTER_NONE)
2087 {
2088 /* Set the mip map generation filter settings. */
2089 IDirect3DBaseTexture9 *pBaseTexture;
2090 if (pSurface->enmD3DResType == VMSVGA3D_D3DRESTYPE_VOLUME_TEXTURE)
2091 pBaseTexture = pSurface->u.pVolumeTexture;
2092 else if (pSurface->enmD3DResType == VMSVGA3D_D3DRESTYPE_CUBE_TEXTURE)
2093 pBaseTexture = pSurface->u.pCubeTexture;
2094 else
2095 pBaseTexture = pSurface->u.pTexture;
2096 hr = pBaseTexture->SetAutoGenFilterType((D3DTEXTUREFILTERTYPE)pSurface->autogenFilter);
2097 AssertMsg(hr == D3D_OK, ("vmsvga3dBackCreateTexture: SetAutoGenFilterType failed with %x\n", hr));
2098 }
2099
2100 /*
2101 * Always initialize all mipmap levels using the in memory data
2102 * to make sure that the just created texture has the up-to-date content.
2103 * The OpenGL backend does this too.
2104 */
2105 Log(("vmsvga3dBackCreateTexture: sync texture\n"));
2106
2107 if (pSurface->enmD3DResType == VMSVGA3D_D3DRESTYPE_VOLUME_TEXTURE)
2108 {
2109 IDirect3DVolumeTexture9 *pVolumeTexture = pSurface->bounce.pVolumeTexture ?
2110 pSurface->bounce.pVolumeTexture :
2111 pSurface->u.pVolumeTexture;
2112
2113 for (uint32_t i = 0; i < numMipLevels; ++i)
2114 {
2115 D3DLOCKED_BOX LockedVolume;
2116 hr = pVolumeTexture->LockBox(i, &LockedVolume, NULL, D3DLOCK_DISCARD);
2117 AssertMsgBreak(hr == D3D_OK, ("LockBox failed with %x\n", hr));
2118
2119 PVMSVGA3DMIPMAPLEVEL pMipLevel = &pSurface->paMipmapLevels[i];
2120
2121 LogFunc(("sync volume texture mipmap level %d (pitch row %x vs %x, slice %x vs %x)\n",
2122 i, LockedVolume.RowPitch, pMipLevel->cbSurfacePitch, LockedVolume.SlicePitch, pMipLevel->cbSurfacePlane));
2123
2124
2125 uint8_t *pDst = (uint8_t *)LockedVolume.pBits;
2126 const uint8_t *pSrc = (uint8_t *)pMipLevel->pSurfaceData;
2127 for (uint32_t d = 0; d < cDepth; ++d)
2128 {
2129 uint8_t *pRowDst = pDst;
2130 const uint8_t *pRowSrc = pSrc;
2131 for (uint32_t h = 0; h < pMipLevel->cBlocksY; ++h)
2132 {
2133 memcpy(pRowDst, pRowSrc, pMipLevel->cbSurfacePitch);
2134 pRowDst += LockedVolume.RowPitch;
2135 pRowSrc += pMipLevel->cbSurfacePitch;
2136 }
2137 pDst += LockedVolume.SlicePitch;
2138 pSrc += pMipLevel->cbSurfacePlane;
2139 }
2140
2141 hr = pVolumeTexture->UnlockBox(i);
2142 AssertMsgBreak(hr == D3D_OK, ("UnlockBox failed with %x\n", hr));
2143
2144 pMipLevel->fDirty = false;
2145 }
2146 }
2147 else if (pSurface->enmD3DResType == VMSVGA3D_D3DRESTYPE_CUBE_TEXTURE)
2148 {
2149 IDirect3DCubeTexture9 *pCubeTexture = pSurface->bounce.pCubeTexture ?
2150 pSurface->bounce.pCubeTexture :
2151 pSurface->u.pCubeTexture;
2152
2153 for (uint32_t iFace = 0; iFace < 6; ++iFace)
2154 {
2155 const D3DCUBEMAP_FACES Face = vmsvga3dCubemapFaceFromIndex(iFace);
2156
2157 for (uint32_t i = 0; i < numMipLevels; ++i)
2158 {
2159 D3DLOCKED_RECT LockedRect;
2160 hr = pCubeTexture->LockRect(Face,
2161 i, /* texture level */
2162 &LockedRect,
2163 NULL, /* entire texture */
2164 0);
2165 AssertMsgBreak(hr == D3D_OK, ("LockRect failed with %x\n", hr));
2166
2167 PVMSVGA3DMIPMAPLEVEL pMipLevel = &pSurface->paMipmapLevels[iFace * numMipLevels + i];
2168
2169 LogFunc(("sync texture face %d mipmap level %d (pitch %x vs %x)\n",
2170 iFace, i, LockedRect.Pitch, pMipLevel->cbSurfacePitch));
2171
2172 uint8_t *pDest = (uint8_t *)LockedRect.pBits;
2173 const uint8_t *pSrc = (uint8_t *)pMipLevel->pSurfaceData;
2174 for (uint32_t j = 0; j < pMipLevel->cBlocksY; ++j)
2175 {
2176 memcpy(pDest, pSrc, pMipLevel->cbSurfacePitch);
2177
2178 pDest += LockedRect.Pitch;
2179 pSrc += pMipLevel->cbSurfacePitch;
2180 }
2181
2182 hr = pCubeTexture->UnlockRect(Face, i /* texture level */);
2183 AssertMsgBreak(hr == D3D_OK, ("UnlockRect failed with %x\n", hr));
2184
2185 pMipLevel->fDirty = false;
2186 }
2187
2188 if (hr != D3D_OK)
2189 break;
2190 }
2191
2192 if (hr != D3D_OK)
2193 {
2194 D3D_RELEASE(pSurface->bounce.pCubeTexture);
2195 D3D_RELEASE(pSurface->u.pCubeTexture);
2196 return VERR_INTERNAL_ERROR;
2197 }
2198 }
2199 else if (pSurface->enmD3DResType == VMSVGA3D_D3DRESTYPE_TEXTURE)
2200 {
2201 IDirect3DTexture9 *pTexture;
2202 if (pSurface->bounce.pTexture)
2203 pTexture = pSurface->bounce.pTexture;
2204 else if (pSurface->formatD3D != pSurface->d3dfmtRequested)
2205 pTexture = pSurface->emulated.pTexture;
2206 else
2207 pTexture = pSurface->u.pTexture;
2208
2209 for (uint32_t i = 0; i < numMipLevels; ++i)
2210 {
2211 D3DLOCKED_RECT LockedRect;
2212
2213 hr = pTexture->LockRect(i, /* texture level */
2214 &LockedRect,
2215 NULL, /* entire texture */
2216 0);
2217
2218 AssertMsgReturn(hr == D3D_OK, ("vmsvga3dBackCreateTexture: LockRect failed with %x\n", hr), VERR_INTERNAL_ERROR);
2219
2220 LogFunc(("sync texture mipmap level %d (pitch %x vs %x)\n", i, LockedRect.Pitch, pSurface->paMipmapLevels[i].cbSurfacePitch));
2221
2222 uint8_t *pDest = (uint8_t *)LockedRect.pBits;
2223 const uint8_t *pSrc = (uint8_t *)pSurface->paMipmapLevels[i].pSurfaceData;
2224 for (uint32_t j = 0; j < pSurface->paMipmapLevels[i].cBlocksY; ++j)
2225 {
2226 memcpy(pDest, pSrc, pSurface->paMipmapLevels[i].cbSurfacePitch);
2227
2228 pDest += LockedRect.Pitch;
2229 pSrc += pSurface->paMipmapLevels[i].cbSurfacePitch;
2230 }
2231
2232 hr = pTexture->UnlockRect(i /* texture level */);
2233 AssertMsgReturn(hr == D3D_OK, ("vmsvga3dBackCreateTexture: UnlockRect failed with %x\n", hr), VERR_INTERNAL_ERROR);
2234
2235 pSurface->paMipmapLevels[i].fDirty = false;
2236 }
2237 }
2238 else
2239 {
2240 AssertMsgFailedReturn(("enmD3DResType not expected %d\n", pSurface->enmD3DResType), VERR_INTERNAL_ERROR);
2241 }
2242
2243 if (pSurface->bounce.pTexture)
2244 {
2245 Log(("vmsvga3dBackCreateTexture: sync dirty texture from bounce buffer\n"));
2246 if (pSurface->enmD3DResType == VMSVGA3D_D3DRESTYPE_VOLUME_TEXTURE)
2247 hr = pContext->pDevice->UpdateTexture(pSurface->bounce.pVolumeTexture, pSurface->u.pVolumeTexture);
2248 else
2249 hr = D3D9UpdateTexture(pContext, pSurface);
2250 AssertMsgReturn(hr == D3D_OK, ("UpdateTexture failed with %x\n", hr), VERR_INTERNAL_ERROR);
2251
2252 /* We will now use the bounce texture for all memory accesses, so free our surface memory buffer. */
2253 for (uint32_t i = 0; i < pSurface->cLevels; i++)
2254 {
2255 RTMemFree(pSurface->paMipmapLevels[i].pSurfaceData);
2256 pSurface->paMipmapLevels[i].pSurfaceData = NULL;
2257 }
2258
2259 /* Track the UpdateTexture operation. */
2260 vmsvga3dSurfaceTrackUsage(pState, pContext, pSurface);
2261 }
2262 pSurface->fDirty = false;
2263
2264 Assert(pSurface->enmD3DResType != VMSVGA3D_D3DRESTYPE_NONE);
2265
2266 pSurface->surfaceFlags |= SVGA3D_SURFACE_HINT_TEXTURE;
2267 pSurface->idAssociatedContext = idAssociatedContext;
2268 return VINF_SUCCESS;
2269}
2270
2271
2272/**
2273 * Backend worker for implementing SVGA_3D_CMD_SURFACE_STRETCHBLT.
2274 *
2275 * @returns VBox status code.
2276 * @param pThis The VGA device instance.
2277 * @param pState The VMSVGA3d state.
2278 * @param pDstSurface The destination host surface.
2279 * @param uDstFace The destination face (valid).
2280 * @param uDstMipmap The destination mipmap level (valid).
2281 * @param pDstBox The destination box.
2282 * @param pSrcSurface The source host surface.
2283 * @param uSrcFace The destination face (valid).
2284 * @param uSrcMipmap The source mimap level (valid).
2285 * @param pSrcBox The source box.
2286 * @param enmMode The strecht blt mode .
2287 * @param pContext The VMSVGA3d context (already current for OGL).
2288 */
2289static DECLCALLBACK(int) vmsvga3dBackSurfaceStretchBlt(PVGASTATE pThis, PVMSVGA3DSTATE pState,
2290 PVMSVGA3DSURFACE pDstSurface, uint32_t uDstFace, uint32_t uDstMipmap, SVGA3dBox const *pDstBox,
2291 PVMSVGA3DSURFACE pSrcSurface, uint32_t uSrcFace, uint32_t uSrcMipmap, SVGA3dBox const *pSrcBox,
2292 SVGA3dStretchBltMode enmMode, PVMSVGA3DCONTEXT pContext)
2293{
2294 RT_NOREF(pThis);
2295
2296 HRESULT hr;
2297 int rc;
2298
2299 AssertReturn(pSrcSurface->enmD3DResType != VMSVGA3D_D3DRESTYPE_VOLUME_TEXTURE, VERR_NOT_IMPLEMENTED);
2300 AssertReturn(pDstSurface->enmD3DResType != VMSVGA3D_D3DRESTYPE_VOLUME_TEXTURE, VERR_NOT_IMPLEMENTED);
2301
2302 /* Flush the drawing pipeline for this surface as it could be used in a shared context. */
2303 vmsvga3dSurfaceFlush(pSrcSurface);
2304 vmsvga3dSurfaceFlush(pDstSurface);
2305
2306 RECT RectSrc;
2307 RectSrc.left = pSrcBox->x;
2308 RectSrc.top = pSrcBox->y;
2309 RectSrc.right = pSrcBox->x + pSrcBox->w; /* exclusive */
2310 RectSrc.bottom = pSrcBox->y + pSrcBox->h; /* exclusive */
2311 Assert(!pSrcBox->z);
2312
2313 RECT RectDst;
2314 RectDst.left = pDstBox->x;
2315 RectDst.top = pDstBox->y;
2316 RectDst.right = pDstBox->x + pDstBox->w; /* exclusive */
2317 RectDst.bottom = pDstBox->y + pDstBox->h; /* exclusive */
2318 Assert(!pDstBox->z);
2319
2320 IDirect3DSurface9 *pSrc;
2321 rc = vmsvga3dGetD3DSurface(pState, pContext, pSrcSurface, uSrcFace, uSrcMipmap, false, &pSrc);
2322 AssertRCReturn(rc, rc);
2323
2324 IDirect3DSurface9 *pDst;
2325 rc = vmsvga3dGetD3DSurface(pState, pContext, pDstSurface, uDstFace, uDstMipmap, false, &pDst);
2326 AssertRCReturn(rc, rc);
2327
2328 D3DTEXTUREFILTERTYPE moded3d;
2329 switch (enmMode)
2330 {
2331 case SVGA3D_STRETCH_BLT_POINT:
2332 moded3d = D3DTEXF_POINT;
2333 break;
2334
2335 case SVGA3D_STRETCH_BLT_LINEAR:
2336 moded3d = D3DTEXF_LINEAR;
2337 break;
2338
2339 default:
2340 AssertFailed();
2341 moded3d = D3DTEXF_NONE;
2342 break;
2343 }
2344
2345 hr = pContext->pDevice->StretchRect(pSrc, &RectSrc, pDst, &RectDst, moded3d);
2346
2347 D3D_RELEASE(pDst);
2348 D3D_RELEASE(pSrc);
2349
2350 AssertMsgReturn(hr == D3D_OK, ("StretchRect failed with %x\n", hr), VERR_INTERNAL_ERROR);
2351
2352 /* Track the StretchRect operation. */
2353 vmsvga3dSurfaceTrackUsage(pState, pContext, pSrcSurface);
2354 vmsvga3dSurfaceTrackUsage(pState, pContext, pDstSurface);
2355
2356 return VINF_SUCCESS;
2357}
2358
2359
2360/**
2361 * Backend worker for implementing SVGA_3D_CMD_SURFACE_DMA that copies one box.
2362 *
2363 * @returns Failure status code or @a rc.
2364 * @param pThis The shared VGA/VMSVGA instance data.
2365 * @param pThisCC The VGA/VMSVGA state for ring-3.
2366 * @param pState The VMSVGA3d state.
2367 * @param pSurface The host surface.
2368 * @param pMipLevel Mipmap level. The caller knows it already.
2369 * @param uHostFace The host face (valid).
2370 * @param uHostMipmap The host mipmap level (valid).
2371 * @param GuestPtr The guest pointer.
2372 * @param cbGuestPitch The guest pitch.
2373 * @param transfer The transfer direction.
2374 * @param pBox The box to copy (clipped, valid, except for guest's srcx, srcy, srcz).
2375 * @param pContext The context (for OpenGL).
2376 * @param rc The current rc for all boxes.
2377 * @param iBox The current box number (for Direct 3D).
2378 */
2379static DECLCALLBACK(int) vmsvga3dBackSurfaceDMACopyBox(PVGASTATE pThis, PVGASTATECC pThisCC, PVMSVGA3DSTATE pState, PVMSVGA3DSURFACE pSurface,
2380 PVMSVGA3DMIPMAPLEVEL pMipLevel, uint32_t uHostFace, uint32_t uHostMipmap,
2381 SVGAGuestPtr GuestPtr, uint32_t cbGuestPitch, SVGA3dTransferType transfer,
2382 SVGA3dCopyBox const *pBox, PVMSVGA3DCONTEXT pContext, int rc, int iBox)
2383{
2384 HRESULT hr = D3D_OK;
2385 const DWORD dwFlags = transfer == SVGA3D_READ_HOST_VRAM ? D3DLOCK_READONLY : 0;
2386
2387 AssertReturn(pSurface->enmD3DResType != VMSVGA3D_D3DRESTYPE_VOLUME_TEXTURE, VERR_NOT_IMPLEMENTED);
2388
2389 const bool fTexture = pSurface->enmD3DResType == VMSVGA3D_D3DRESTYPE_TEXTURE
2390 || pSurface->enmD3DResType == VMSVGA3D_D3DRESTYPE_CUBE_TEXTURE;
2391 if ( pSurface->enmD3DResType == VMSVGA3D_D3DRESTYPE_SURFACE
2392 || fTexture)
2393 {
2394 rc = vmsvga3dContextFromCid(pState, pSurface->idAssociatedContext, &pContext);
2395 AssertRCReturn(rc, rc);
2396 AssertReturn(pContext->pDevice, VERR_INTERNAL_ERROR);
2397
2398 /* Get the surface involved in the transfer. */
2399 IDirect3DSurface9 *pSurf;
2400 rc = vmsvga3dGetD3DSurface(pState, pContext, pSurface, uHostFace, uHostMipmap, true, &pSurf);
2401 AssertRCReturn(rc, rc);
2402
2403 if (transfer == SVGA3D_READ_HOST_VRAM)
2404 {
2405 /* Texture data is copied to the host VRAM.
2406 * Update the 'bounce' texture if necessary.
2407 */
2408 if ( fTexture
2409 && pSurface->bounce.pTexture
2410 && iBox == 0 /* only the first time */)
2411 {
2412 /** @todo inefficient for VRAM buffers!! */
2413 if (RT_BOOL(pSurface->surfaceFlags & SVGA3D_SURFACE_HINT_RENDERTARGET))
2414 {
2415 /* Copy the texture mipmap level to the bounce texture. */
2416 hr = D3D9GetRenderTargetData(pContext, pSurface, uHostFace, uHostMipmap);
2417 AssertMsgReturn(hr == D3D_OK, ("D3D9GetRenderTargetData failed with %x\n", hr), VERR_INTERNAL_ERROR);
2418 }
2419 }
2420 }
2421
2422 uint32_t const u32GuestBlockX = pBox->srcx / pSurface->cxBlock;
2423 uint32_t const u32GuestBlockY = pBox->srcy / pSurface->cyBlock;
2424 Assert(u32GuestBlockX * pSurface->cxBlock == pBox->srcx);
2425 Assert(u32GuestBlockY * pSurface->cyBlock == pBox->srcy);
2426 uint32_t const cBlocksX = (pBox->w + pSurface->cxBlock - 1) / pSurface->cxBlock;
2427 uint32_t const cBlocksY = (pBox->h + pSurface->cyBlock - 1) / pSurface->cyBlock;
2428 AssertMsgReturn(cBlocksX && cBlocksY, ("Empty box %dx%d\n", pBox->w, pBox->h), VERR_INTERNAL_ERROR);
2429
2430 /* vmsvgaR3GmrTransfer verifies uGuestOffset.
2431 * srcx(u32GuestBlockX) and srcy(u32GuestBlockY) have been verified in vmsvga3dSurfaceDMA
2432 * to not cause 32 bit overflow when multiplied by cbBlock and cbGuestPitch.
2433 */
2434 uint64_t const uGuestOffset = u32GuestBlockX * pSurface->cbBlock + u32GuestBlockY * cbGuestPitch;
2435 AssertReturn(uGuestOffset < UINT32_MAX, VERR_INVALID_PARAMETER);
2436
2437 RECT Rect;
2438 Rect.left = pBox->x;
2439 Rect.top = pBox->y;
2440 Rect.right = pBox->x + pBox->w; /* exclusive */
2441 Rect.bottom = pBox->y + pBox->h; /* exclusive */
2442
2443 D3DLOCKED_RECT LockedRect;
2444 hr = pSurf->LockRect(&LockedRect, &Rect, dwFlags);
2445 AssertMsgReturn(hr == D3D_OK, ("LockRect failed with %x\n", hr), VERR_INTERNAL_ERROR);
2446
2447 LogFunc(("Lock sid=%u %s(bounce=%d) memory for rectangle (%d,%d)(%d,%d)\n",
2448 pSurface->id, fTexture ? "TEXTURE " : "", RT_BOOL(pSurface->bounce.pTexture),
2449 Rect.left, Rect.top, Rect.right, Rect.bottom));
2450
2451 /* Prepare parameters for vmsvgaR3GmrTransfer, which needs the host buffer address, size
2452 * and offset of the first scanline.
2453 */
2454 uint32_t const cbLockedBuf = RT_ABS(LockedRect.Pitch) * cBlocksY;
2455 uint8_t *pu8LockedBuf = (uint8_t *)LockedRect.pBits;
2456 if (LockedRect.Pitch < 0)
2457 pu8LockedBuf -= cbLockedBuf + LockedRect.Pitch;
2458 uint32_t const offLockedBuf = (uint32_t)((uintptr_t)LockedRect.pBits - (uintptr_t)pu8LockedBuf);
2459
2460 rc = vmsvgaR3GmrTransfer(pThis,
2461 pThisCC,
2462 transfer,
2463 pu8LockedBuf,
2464 cbLockedBuf,
2465 offLockedBuf,
2466 LockedRect.Pitch,
2467 GuestPtr,
2468 (uint32_t)uGuestOffset,
2469 cbGuestPitch,
2470 cBlocksX * pSurface->cbBlock,
2471 cBlocksY);
2472 AssertRC(rc);
2473
2474 Log4(("first line:\n%.*Rhxd\n", cBlocksX * pSurface->cbBlock, LockedRect.pBits));
2475
2476 hr = pSurf->UnlockRect();
2477
2478 D3D_RELEASE(pSurf);
2479
2480 AssertMsgReturn(hr == D3D_OK, ("UnlockRect failed with %x\n", hr), VERR_INTERNAL_ERROR);
2481
2482 if (transfer == SVGA3D_WRITE_HOST_VRAM)
2483 {
2484 /* Data is copied to the texture. Copy updated 'bounce' texture to the actual if necessary.
2485 */
2486 /// @todo for the last iBox only.
2487 if ( fTexture
2488 && pSurface->bounce.pTexture)
2489 {
2490 LogFunc(("Sync texture from bounce buffer\n"));
2491
2492 /* Copy the new contents to the actual texture object. */
2493 hr = D3D9UpdateTexture(pContext, pSurface);
2494 AssertMsgReturn(hr == D3D_OK, ("UpdateTexture failed with %x\n", hr), VERR_INTERNAL_ERROR);
2495
2496 /* Track the copy operation. */
2497 vmsvga3dSurfaceTrackUsage(pState, pContext, pSurface);
2498 }
2499 }
2500 }
2501 else if ( pSurface->enmD3DResType == VMSVGA3D_D3DRESTYPE_VERTEX_BUFFER
2502 || pSurface->enmD3DResType == VMSVGA3D_D3DRESTYPE_INDEX_BUFFER)
2503 {
2504 /*
2505 * Mesa SVGA driver can use the same buffer either for vertex or index data.
2506 * But D3D distinguishes between index and vertex buffer objects.
2507 * Therefore it should be possible to switch the buffer type on the fly.
2508 *
2509 * Always save the data to the memory buffer in pSurface->paMipmapLevels and,
2510 * if necessary, recreate the corresponding D3D object with the data.
2511 */
2512
2513 /* Buffers are uncompressed. */
2514 AssertReturn(pSurface->cxBlock == 1 && pSurface->cyBlock == 1, VERR_INTERNAL_ERROR);
2515
2516 /* Caller already clipped pBox and buffers are 1-dimensional. */
2517 Assert(pBox->y == 0 && pBox->h == 1 && pBox->z == 0 && pBox->d == 1);
2518
2519 /* The caller has already updated pMipLevel->pSurfaceData, see VMSVGA3DSURFACE_NEEDS_DATA. */
2520
2521#ifdef LOG_ENABLED
2522 uint32_t const offHst = pBox->x * pSurface->cbBlock;
2523 uint32_t const cbWidth = pBox->w * pSurface->cbBlock;
2524 Log4(("Buffer updated at [0x%x;0x%x):\n%.*Rhxd\n", offHst, offHst + cbWidth, cbWidth, (uint8_t *)pMipLevel->pSurfaceData + offHst));
2525#endif
2526
2527 /* Do not bother to copy the data to the D3D resource now. vmsvga3dDrawPrimitives will do that.
2528 * The SVGA driver may use the same surface for both index and vertex data.
2529 */
2530
2531 /* Make sure that vmsvga3dDrawPrimitives fetches the new data. */
2532 pMipLevel->fDirty = true;
2533 pSurface->fDirty = true;
2534 }
2535 else
2536 {
2537 AssertMsgFailed(("Unsupported surface flags 0x%08X, type %d\n", pSurface->surfaceFlags, pSurface->enmD3DResType));
2538 }
2539
2540 return rc;
2541}
2542
2543static DECLCALLBACK(int) vmsvga3dBackGenerateMipmaps(PVGASTATECC pThisCC, uint32_t sid, SVGA3dTextureFilter filter)
2544{
2545 PVMSVGA3DSTATE pState = pThisCC->svga.p3dState;
2546 HRESULT hr;
2547
2548 PVMSVGA3DSURFACE pSurface;
2549 int rc = vmsvga3dSurfaceFromSid(pState, sid, &pSurface);
2550 AssertRCReturn(rc, rc);
2551 AssertReturn(pSurface->idAssociatedContext != SVGA3D_INVALID_ID, VERR_INTERNAL_ERROR);
2552
2553 Assert(filter != SVGA3D_TEX_FILTER_FLATCUBIC);
2554 Assert(filter != SVGA3D_TEX_FILTER_GAUSSIANCUBIC);
2555 pSurface->autogenFilter = filter;
2556
2557 Log(("vmsvga3dGenerateMipmaps: sid=%u filter=%d\n", sid, filter));
2558
2559 if (!pSurface->u.pSurface)
2560 {
2561 /** @todo stricter checks for associated context */
2562 uint32_t const cid = pSurface->idAssociatedContext;
2563
2564 PVMSVGA3DCONTEXT pContext;
2565 rc = vmsvga3dContextFromCid(pState, cid, &pContext);
2566 AssertRCReturn(rc, rc);
2567 AssertReturn(pContext->pDevice, VERR_INTERNAL_ERROR);
2568
2569 /* Unknown surface type; turn it into a texture. */
2570 LogFunc(("unknown src surface sid=%u type=%d format=%d -> create texture\n", sid, pSurface->surfaceFlags, pSurface->format));
2571 rc = vmsvga3dBackCreateTexture(pThisCC, pContext, cid, pSurface);
2572 AssertRCReturn(rc, rc);
2573 }
2574
2575 AssertReturn( pSurface->enmD3DResType == VMSVGA3D_D3DRESTYPE_TEXTURE
2576 || pSurface->enmD3DResType == VMSVGA3D_D3DRESTYPE_CUBE_TEXTURE
2577 || pSurface->enmD3DResType == VMSVGA3D_D3DRESTYPE_VOLUME_TEXTURE,
2578 VERR_INVALID_PARAMETER);
2579
2580 hr = pSurface->u.pTexture->SetAutoGenFilterType((D3DTEXTUREFILTERTYPE)filter);
2581 AssertMsg(hr == D3D_OK, ("SetAutoGenFilterType failed with %x\n", hr));
2582
2583 /* Generate the mip maps. */
2584 pSurface->u.pTexture->GenerateMipSubLevels();
2585
2586 return VINF_SUCCESS;
2587}
2588
2589
2590/**
2591 * Create a new 3d context
2592 *
2593 * @returns VBox status code.
2594 * @param pThisCC The VGA/VMSVGA state for ring-3.
2595 * @param cid Context id
2596 */
2597static DECLCALLBACK(int) vmsvga3dBackContextDefine(PVGASTATECC pThisCC, uint32_t cid)
2598{
2599 int rc;
2600 PVMSVGA3DCONTEXT pContext;
2601 HRESULT hr;
2602 PVMSVGA3DSTATE pState = pThisCC->svga.p3dState;
2603
2604 Log(("vmsvga3dContextDefine id %x\n", cid));
2605
2606 AssertReturn(pState, VERR_NO_MEMORY);
2607 AssertReturn(cid < SVGA3D_MAX_CONTEXT_IDS, VERR_INVALID_PARAMETER);
2608
2609 if (cid >= pState->cContexts)
2610 {
2611 /* Grow the array. */
2612 uint32_t cNew = RT_ALIGN(cid + 15, 16);
2613 void *pvNew = RTMemRealloc(pState->papContexts, sizeof(pState->papContexts[0]) * cNew);
2614 AssertReturn(pvNew, VERR_NO_MEMORY);
2615 pState->papContexts = (PVMSVGA3DCONTEXT *)pvNew;
2616 while (pState->cContexts < cNew)
2617 {
2618 pContext = (PVMSVGA3DCONTEXT)RTMemAllocZ(sizeof(*pContext));
2619 AssertReturn(pContext, VERR_NO_MEMORY);
2620 pContext->id = SVGA3D_INVALID_ID;
2621 pState->papContexts[pState->cContexts++] = pContext;
2622 }
2623 }
2624 /* If one already exists with this id, then destroy it now. */
2625 if (pState->papContexts[cid]->id != SVGA3D_INVALID_ID)
2626 vmsvga3dBackContextDestroy(pThisCC, cid);
2627
2628 pContext = pState->papContexts[cid];
2629 memset(pContext, 0, sizeof(*pContext));
2630 pContext->id = cid;
2631 for (uint32_t i = 0; i< RT_ELEMENTS(pContext->aSidActiveTextures); i++)
2632 pContext->aSidActiveTextures[i] = SVGA3D_INVALID_ID;
2633 pContext->state.shidVertex = SVGA3D_INVALID_ID;
2634 pContext->state.shidPixel = SVGA3D_INVALID_ID;
2635
2636 for (uint32_t i = 0; i < RT_ELEMENTS(pContext->state.aRenderTargets); i++)
2637 pContext->state.aRenderTargets[i] = SVGA3D_INVALID_ID;
2638
2639 /* Create a context window with minimal 4x4 size. We will never use the swapchain
2640 * to present the rendered image. Rendered images from the guest will be copied to
2641 * the VMSVGA SCREEN object, which can be either an offscreen render target or
2642 * system memory in the guest VRAM.
2643 */
2644 rc = vmsvga3dContextWindowCreate(pState->hInstance, pState->pWindowThread, pState->WndRequestSem, &pContext->hwnd);
2645 AssertRCReturn(rc, rc);
2646
2647 /* Changed when the function returns. */
2648 D3DPRESENT_PARAMETERS PresParam;
2649 PresParam.BackBufferWidth = 0;
2650 PresParam.BackBufferHeight = 0;
2651 PresParam.BackBufferFormat = D3DFMT_UNKNOWN;
2652 PresParam.BackBufferCount = 0;
2653
2654 PresParam.MultiSampleType = D3DMULTISAMPLE_NONE;
2655 PresParam.MultiSampleQuality = 0;
2656 PresParam.SwapEffect = D3DSWAPEFFECT_DISCARD;
2657 PresParam.hDeviceWindow = pContext->hwnd;
2658 PresParam.Windowed = TRUE;
2659 PresParam.EnableAutoDepthStencil = FALSE;
2660 PresParam.AutoDepthStencilFormat = D3DFMT_UNKNOWN; /* not relevant */
2661 PresParam.Flags = 0;
2662 PresParam.FullScreen_RefreshRateInHz = 0; /* windowed -> 0 */
2663 /** @todo consider using D3DPRESENT_DONOTWAIT so we don't wait for the GPU during Present calls. */
2664 PresParam.PresentationInterval = D3DPRESENT_INTERVAL_IMMEDIATE;
2665
2666#if 0
2667 VMSVGA3DCREATEDEVICEPARAMS Params = { pState, pContext, &PresParam, 0 };
2668 rc = vmsvga3dSendThreadMessage(pState->pWindowThread, pState->WndRequestSem, WM_VMSVGA3D_CREATE_DEVICE, 0, (LPARAM)&Params);
2669 AssertRCReturn(rc, rc);
2670 hr = Params.hrc;
2671
2672#elif defined(VBOX_VMSVGA3D_WITH_WINE_OPENGL)
2673 hr = pState->pD3D9->CreateDevice(D3DADAPTER_DEFAULT,
2674 D3DDEVTYPE_HAL,
2675 pContext->hwnd,
2676 D3DCREATE_MULTITHREADED | D3DCREATE_MIXED_VERTEXPROCESSING, //D3DCREATE_HARDWARE_VERTEXPROCESSING,
2677 &PresParam,
2678 &pContext->pDevice);
2679#else
2680 /** @todo Docs indicates that we should be using
2681 * D3DCREATE_HARDWARE_VERTEXPROCESSING with W10 1607 and higher.
2682 * https://docs.microsoft.com/en-us/windows/win32/direct3d9/d3dcreate */
2683 hr = pState->pD3D9->CreateDeviceEx(D3DADAPTER_DEFAULT,
2684 D3DDEVTYPE_HAL,
2685 pContext->hwnd,
2686 D3DCREATE_MULTITHREADED | D3DCREATE_MIXED_VERTEXPROCESSING, //D3DCREATE_HARDWARE_VERTEXPROCESSING,
2687 &PresParam,
2688 NULL,
2689 &pContext->pDevice);
2690#endif
2691 AssertMsgReturn(hr == D3D_OK, ("vmsvga3dContextDefine: CreateDevice failed with %x\n", hr), VERR_INTERNAL_ERROR);
2692
2693 Log(("vmsvga3dContextDefine: Backbuffer (%d,%d) count=%d format=%x\n", PresParam.BackBufferWidth, PresParam.BackBufferHeight, PresParam.BackBufferCount, PresParam.BackBufferFormat));
2694 return VINF_SUCCESS;
2695}
2696
2697/**
2698 * Destroy an existing 3d context
2699 *
2700 * @returns VBox status code.
2701 * @param pThisCC The VGA/VMSVGA state for ring-3.
2702 * @param cid Context id
2703 */
2704static DECLCALLBACK(int) vmsvga3dBackContextDestroy(PVGASTATECC pThisCC, uint32_t cid)
2705{
2706 PVMSVGA3DSTATE pState = pThisCC->svga.p3dState;
2707 AssertReturn(pState, VERR_NO_MEMORY);
2708
2709 AssertReturn(cid < SVGA3D_MAX_CONTEXT_IDS, VERR_INVALID_PARAMETER);
2710
2711 if ( cid < pState->cContexts
2712 && pState->papContexts[cid]->id == cid)
2713 {
2714 PVMSVGA3DCONTEXT pContext = pState->papContexts[cid];
2715
2716 Log(("vmsvga3dContextDestroy id %x\n", cid));
2717
2718 /* Cleanup the device runtime state. */
2719 if (pContext->pDevice)
2720 pContext->pDevice->SetVertexDeclaration(NULL);
2721 D3D_RELEASE(pContext->d3dState.pVertexDecl);
2722
2723 /* Check for all surfaces that are associated with this context to remove all dependencies */
2724 for (uint32_t sid = 0; sid < pState->cSurfaces; sid++)
2725 {
2726 PVMSVGA3DSURFACE pSurface = pState->papSurfaces[sid];
2727 if ( pSurface->id == sid
2728 && pSurface->idAssociatedContext == cid)
2729 {
2730 int rc;
2731
2732 LogFunc(("Remove all dependencies for surface sid=%u\n", sid));
2733
2734 uint32_t const surfaceFlags = pSurface->surfaceFlags;
2735 SVGA3dSurfaceFormat const format = pSurface->format;
2736 uint32_t const multisampleCount = pSurface->multiSampleCount;
2737 SVGA3dTextureFilter const autogenFilter = pSurface->autogenFilter;
2738 uint32_t const cFaces = pSurface->cFaces;
2739 uint32_t const cMipLevels = pSurface->cLevels;
2740
2741 SVGA3dSize *pMipLevelSize = (SVGA3dSize *)RTMemAllocZ(cMipLevels * cFaces * sizeof(SVGA3dSize));
2742 AssertReturn(pMipLevelSize, VERR_NO_MEMORY);
2743
2744 for (uint32_t face = 0; face < pSurface->cFaces; face++)
2745 {
2746 for (uint32_t i = 0; i < cMipLevels; i++)
2747 {
2748 uint32_t idx = i + face * cMipLevels;
2749 memcpy(&pMipLevelSize[idx], &pSurface->paMipmapLevels[idx].mipmapSize, sizeof(SVGA3dSize));
2750 }
2751 }
2752
2753 /* Recreate the surface with the original settings; destroys the contents, but that seems fairly safe since the context is also destroyed. */
2754#ifdef DEBUG_sunlover
2755 /** @todo not safe with shared objects */
2756 Assert(pSurface->pSharedObjectTree == NULL);
2757#endif
2758
2759 rc = vmsvga3dSurfaceDestroy(pThisCC, sid);
2760 AssertRC(rc);
2761
2762 rc = vmsvga3dSurfaceDefine(pThisCC, sid, surfaceFlags, format, multisampleCount, autogenFilter,
2763 cMipLevels, &pMipLevelSize[0]);
2764 AssertRC(rc);
2765
2766 Assert(!pSurface->u.pSurface);
2767 }
2768 else
2769 {
2770 /* Check for a shared surface object. */
2771 PVMSVGA3DSHAREDSURFACE pSharedSurface = (PVMSVGA3DSHAREDSURFACE)RTAvlU32Get(&pSurface->pSharedObjectTree, cid);
2772 if (pSharedSurface)
2773 {
2774 LogFunc(("Remove shared dependency for surface sid=%u\n", sid));
2775
2776 switch (pSurface->enmD3DResType)
2777 {
2778 case VMSVGA3D_D3DRESTYPE_TEXTURE:
2779 Assert(pSharedSurface->u.pTexture);
2780 D3D_RELEASE(pSharedSurface->u.pTexture);
2781 break;
2782
2783 case VMSVGA3D_D3DRESTYPE_CUBE_TEXTURE:
2784 Assert(pSharedSurface->u.pCubeTexture);
2785 D3D_RELEASE(pSharedSurface->u.pCubeTexture);
2786 break;
2787
2788 case VMSVGA3D_D3DRESTYPE_VOLUME_TEXTURE:
2789 Assert(pSharedSurface->u.pVolumeTexture);
2790 D3D_RELEASE(pSharedSurface->u.pVolumeTexture);
2791 break;
2792
2793 default:
2794 AssertFailed();
2795 break;
2796 }
2797 RTAvlU32Remove(&pSurface->pSharedObjectTree, cid);
2798 RTMemFree(pSharedSurface);
2799 }
2800 }
2801 }
2802
2803 /* Destroy all leftover pixel shaders. */
2804 for (uint32_t i = 0; i < pContext->cPixelShaders; i++)
2805 {
2806 if (pContext->paPixelShader[i].id != SVGA3D_INVALID_ID)
2807 vmsvga3dBackShaderDestroy(pThisCC, pContext->paPixelShader[i].cid, pContext->paPixelShader[i].id, pContext->paPixelShader[i].type);
2808 }
2809 if (pContext->paPixelShader)
2810 RTMemFree(pContext->paPixelShader);
2811
2812 /* Destroy all leftover vertex shaders. */
2813 for (uint32_t i = 0; i < pContext->cVertexShaders; i++)
2814 {
2815 if (pContext->paVertexShader[i].id != SVGA3D_INVALID_ID)
2816 vmsvga3dBackShaderDestroy(pThisCC, pContext->paVertexShader[i].cid, pContext->paVertexShader[i].id, pContext->paVertexShader[i].type);
2817 }
2818 if (pContext->paVertexShader)
2819 RTMemFree(pContext->paVertexShader);
2820
2821 if (pContext->state.paVertexShaderConst)
2822 RTMemFree(pContext->state.paVertexShaderConst);
2823 if (pContext->state.paPixelShaderConst)
2824 RTMemFree(pContext->state.paPixelShaderConst);
2825
2826 vmsvga3dBackOcclusionQueryDelete(pThisCC, pContext);
2827
2828 /* Release the D3D device object */
2829 D3D_RELEASE(pContext->pDevice);
2830
2831 /* Destroy the window we've created. */
2832 int rc = vmsvga3dSendThreadMessage(pState->pWindowThread, pState->WndRequestSem, WM_VMSVGA3D_DESTROYWINDOW, (WPARAM)pContext->hwnd, 0);
2833 AssertRC(rc);
2834
2835 memset(pContext, 0, sizeof(*pContext));
2836 pContext->id = SVGA3D_INVALID_ID;
2837 }
2838 else
2839 AssertFailed();
2840
2841 return VINF_SUCCESS;
2842}
2843
2844static DECLCALLBACK(int) vmsvga3dBackDefineScreen(PVGASTATE pThis, PVGASTATECC pThisCC, VMSVGASCREENOBJECT *pScreen)
2845{
2846 RT_NOREF(pThis, pThisCC, pScreen);
2847 return VERR_NOT_IMPLEMENTED;
2848}
2849
2850static DECLCALLBACK(int) vmsvga3dBackDestroyScreen(PVGASTATECC pThisCC, VMSVGASCREENOBJECT *pScreen)
2851{
2852 RT_NOREF(pThisCC, pScreen);
2853 return VERR_NOT_IMPLEMENTED;
2854}
2855
2856static DECLCALLBACK(int) vmsvga3dBackSurfaceBlitToScreen(PVGASTATECC pThisCC, VMSVGASCREENOBJECT *pScreen,
2857 SVGASignedRect destRect, SVGA3dSurfaceImageId srcImage,
2858 SVGASignedRect srcRect, uint32_t cRects, SVGASignedRect *paRects)
2859{
2860 RT_NOREF(pThisCC, pScreen, destRect, srcImage, srcRect, cRects, paRects);
2861 return VERR_NOT_IMPLEMENTED;
2862}
2863
2864static int vmsvga3dContextTrackUsage(PVGASTATECC pThisCC, PVMSVGA3DCONTEXT pContext)
2865{
2866#ifndef VBOX_VMSVGA3D_WITH_WINE_OPENGL
2867 PVMSVGA3DSTATE pState = pThisCC->svga.p3dState;
2868 AssertReturn(pState, VERR_NO_MEMORY);
2869
2870 /* Inject fences to make sure we can track surface usage in case the client wants to reuse it in another context. */
2871 for (uint32_t i = 0; i < RT_ELEMENTS(pContext->aSidActiveTextures); ++i)
2872 {
2873 if (pContext->aSidActiveTextures[i] != SVGA3D_INVALID_ID)
2874 vmsvga3dSurfaceTrackUsageById(pState, pContext, pContext->aSidActiveTextures[i]);
2875 }
2876 for (uint32_t i = 0; i < RT_ELEMENTS(pContext->state.aRenderTargets); ++i)
2877 if (pContext->state.aRenderTargets[i] != SVGA3D_INVALID_ID)
2878 vmsvga3dSurfaceTrackUsageById(pState, pContext, pContext->state.aRenderTargets[i]);
2879#endif
2880 return VINF_SUCCESS;
2881}
2882
2883/* Handle resize */
2884static DECLCALLBACK(int) vmsvga3dBackChangeMode(PVGASTATECC pThisCC)
2885{
2886 PVMSVGA3DSTATE pState = pThisCC->svga.p3dState;
2887 AssertReturn(pState, VERR_NO_MEMORY);
2888
2889 /* Resize all active contexts. */
2890 for (uint32_t i = 0; i < pState->cContexts; i++)
2891 {
2892 PVMSVGA3DCONTEXT pContext = pState->papContexts[i];
2893 uint32_t cid = pContext->id;
2894
2895 if (cid != SVGA3D_INVALID_ID)
2896 {
2897 D3DPRESENT_PARAMETERS PresParam;
2898 D3DVIEWPORT9 viewportOrg;
2899 HRESULT hr;
2900
2901#ifdef VMSVGA3D_DIRECT3D9_RESET
2902 /* Sync back all surface data as everything is lost after the Reset. */
2903 for (uint32_t sid = 0; sid < pState->cSurfaces; sid++)
2904 {
2905 PVMSVGA3DSURFACE pSurface = pState->papSurfaces[sid];
2906 if ( pSurface->id == sid
2907 && pSurface->idAssociatedContext == cid
2908 && pSurface->u.pSurface)
2909 {
2910 Log(("vmsvga3dChangeMode: sync back data of surface sid=%u (fDirty=%d)\n", sid, pSurface->fDirty));
2911
2912 /* Reallocate our surface memory buffers. */
2913 for (uint32_t i = 0; i < pSurface->cMipLevels; i++)
2914 {
2915 PVMSVGA3DMIPMAPLEVEL pMipmapLevel = &pSurface->paMipmapLevels[i];
2916
2917 pMipmapLevel->pSurfaceData = RTMemAllocZ(pMipmapLevel->cbSurface);
2918 AssertReturn(pMipmapLevel->pSurfaceData, VERR_NO_MEMORY);
2919
2920 if (!pSurface->fDirty)
2921 {
2922 D3DLOCKED_RECT LockedRect;
2923
2924 if (pSurface->bounce.pTexture)
2925 {
2926 IDirect3DSurface9 *pSrc, *pDest;
2927
2928 /** @todo only sync when something was actually rendered (since the last sync) */
2929 Log(("vmsvga3dChangeMode: sync bounce buffer (level %d)\n", i));
2930 hr = pSurface->bounce.pTexture->GetSurfaceLevel(i, &pDest);
2931 AssertMsgReturn(hr == D3D_OK, ("vmsvga3dChangeMode: GetSurfaceLevel failed with %x\n", hr), VERR_INTERNAL_ERROR);
2932
2933 hr = pSurface->u.pTexture->GetSurfaceLevel(i, &pSrc);
2934 AssertMsgReturn(hr == D3D_OK, ("vmsvga3dChangeMode: GetSurfaceLevel failed with %x\n", hr), VERR_INTERNAL_ERROR);
2935
2936 hr = pContext->pDevice->GetRenderTargetData(pSrc, pDest);
2937 AssertMsgReturn(hr == D3D_OK, ("vmsvga3dChangeMode: GetRenderTargetData failed with %x\n", hr), VERR_INTERNAL_ERROR);
2938
2939 D3D_RELEASE(pSrc);
2940 D3D_RELEASE(pDest);
2941
2942 hr = pSurface->bounce.pTexture->LockRect(i,
2943 &LockedRect,
2944 NULL,
2945 D3DLOCK_READONLY);
2946 }
2947 else
2948 hr = pSurface->u.pTexture->LockRect(i,
2949 &LockedRect,
2950 NULL,
2951 D3DLOCK_READONLY);
2952 AssertMsgReturn(hr == D3D_OK, ("vmsvga3dChangeMode: LockRect failed with %x\n", hr), VERR_INTERNAL_ERROR);
2953
2954 /* Copy the data one line at a time in case the internal pitch is different. */
2955 for (uint32_t j = 0; j < pMipmapLevel->size.height; j++)
2956 {
2957 memcpy((uint8_t *)pMipmapLevel->pSurfaceData + j * pMipmapLevel->cbSurfacePitch, (uint8_t *)LockedRect.pBits + j * LockedRect.Pitch, pMipmapLevel->cbSurfacePitch);
2958 }
2959
2960 if (pSurface->bounce.pTexture)
2961 hr = pSurface->bounce.pTexture->UnlockRect(i);
2962 else
2963 hr = pSurface->u.pTexture->UnlockRect(i);
2964 AssertMsgReturn(hr == D3D_OK, ("vmsvga3dChangeMode: UnlockRect failed with %x\n", hr), VERR_INTERNAL_ERROR);
2965 }
2966 }
2967
2968
2969 switch (pSurface->flags & VMSVGA3D_SURFACE_HINT_SWITCH_MASK)
2970 {
2971 case SVGA3D_SURFACE_CUBEMAP:
2972 case SVGA3D_SURFACE_CUBEMAP | SVGA3D_SURFACE_HINT_TEXTURE:
2973 case SVGA3D_SURFACE_CUBEMAP | SVGA3D_SURFACE_HINT_TEXTURE | SVGA3D_SURFACE_HINT_RENDERTARGET:
2974 D3D_RELEASE(pSurface->u.pCubeTexture);
2975 D3D_RELEASE(pSurface->bounce.pCubeTexture);
2976 break;
2977
2978 case SVGA3D_SURFACE_HINT_INDEXBUFFER | SVGA3D_SURFACE_HINT_VERTEXBUFFER:
2979 case SVGA3D_SURFACE_HINT_INDEXBUFFER:
2980 case SVGA3D_SURFACE_HINT_VERTEXBUFFER:
2981 if (pSurface->fu32ActualUsageFlags == SVGA3D_SURFACE_HINT_VERTEXBUFFER)
2982 D3D_RELEASE(pSurface->u.pVertexBuffer);
2983 else if (pSurface->fu32ActualUsageFlags == SVGA3D_SURFACE_HINT_INDEXBUFFER)
2984 D3D_RELEASE(pSurface->u.pIndexBuffer);
2985 else
2986 AssertMsg(pSurface->u.pVertexBuffer == NULL, ("fu32ActualUsageFlags %x\n", pSurface->fu32ActualUsageFlags));
2987 break;
2988
2989 case SVGA3D_SURFACE_HINT_TEXTURE:
2990 case SVGA3D_SURFACE_HINT_TEXTURE | SVGA3D_SURFACE_HINT_RENDERTARGET:
2991 D3D_RELEASE(pSurface->u.pTexture);
2992 D3D_RELEASE(pSurface->bounce.pTexture);
2993 break;
2994
2995 case SVGA3D_SURFACE_HINT_RENDERTARGET:
2996 case SVGA3D_SURFACE_HINT_DEPTHSTENCIL:
2997 if (pSurface->fStencilAsTexture)
2998 D3D_RELEASE(pSurface->u.pTexture);
2999 else
3000 D3D_RELEASE(pSurface->u.pSurface);
3001 break;
3002
3003 default:
3004 AssertFailed();
3005 break;
3006 }
3007 RTAvlU32Destroy(&pSurface->pSharedObjectTree, vmsvga3dSharedSurfaceDestroyTree, pSurface);
3008 Assert(pSurface->pSharedObjectTree == NULL);
3009
3010 pSurface->idAssociatedContext = SVGA3D_INVALID_ID;
3011 pSurface->hSharedObject = 0;
3012 }
3013 }
3014#endif /* #ifdef VMSVGA3D_DIRECT3D9_RESET */
3015
3016 AssertReturn(pContext->pDevice, VERR_INTERNAL_ERROR);
3017
3018 /* Cleanup the device runtime state. */
3019 pContext->pDevice->SetVertexDeclaration(NULL);
3020 D3D_RELEASE(pContext->d3dState.pVertexDecl);
3021
3022 hr = pContext->pDevice->GetViewport(&viewportOrg);
3023 AssertMsgReturn(hr == D3D_OK, ("vmsvga3dChangeMode: GetViewport failed with %x\n", hr), VERR_INTERNAL_ERROR);
3024
3025 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)));
3026
3027 /* Changed when the function returns. */
3028 PresParam.BackBufferWidth = 0;
3029 PresParam.BackBufferHeight = 0;
3030 PresParam.BackBufferFormat = D3DFMT_UNKNOWN;
3031 PresParam.BackBufferCount = 0;
3032
3033 PresParam.MultiSampleType = D3DMULTISAMPLE_NONE;
3034 PresParam.MultiSampleQuality = 0;
3035 PresParam.SwapEffect = D3DSWAPEFFECT_DISCARD;
3036 PresParam.hDeviceWindow = pContext->hwnd;
3037 PresParam.Windowed = TRUE;
3038 PresParam.EnableAutoDepthStencil = FALSE;
3039 PresParam.AutoDepthStencilFormat = D3DFMT_UNKNOWN; /* not relevant */
3040 PresParam.Flags = 0;
3041 PresParam.FullScreen_RefreshRateInHz = 0; /* windowed -> 0 */
3042 /** @todo consider using D3DPRESENT_DONOTWAIT so we don't wait for the GPU during Present calls. */
3043 PresParam.PresentationInterval = D3DPRESENT_INTERVAL_IMMEDIATE;;
3044
3045#ifdef VBOX_VMSVGA3D_WITH_WINE_OPENGL
3046 hr = pContext->pDevice->Reset(&PresParam);
3047 AssertMsgReturn(hr == D3D_OK, ("vmsvga3dChangeMode: Reset failed with %x\n", hr), VERR_INTERNAL_ERROR);
3048#else
3049 /* ResetEx does not trash the device state */
3050 hr = pContext->pDevice->ResetEx(&PresParam, NULL);
3051 AssertMsgReturn(hr == D3D_OK, ("vmsvga3dChangeMode: Reset failed with %x\n", hr), VERR_INTERNAL_ERROR);
3052#endif
3053 Log(("vmsvga3dChangeMode: Backbuffer (%d,%d) count=%d format=%x\n", PresParam.BackBufferWidth, PresParam.BackBufferHeight, PresParam.BackBufferCount, PresParam.BackBufferFormat));
3054
3055 /* ResetEx changes the viewport; restore it again. */
3056 hr = pContext->pDevice->SetViewport(&viewportOrg);
3057 AssertMsgReturn(hr == D3D_OK, ("vmsvga3dChangeMode: SetViewport failed with %x\n", hr), VERR_INTERNAL_ERROR);
3058
3059#ifdef LOG_ENABLED
3060 {
3061 D3DVIEWPORT9 viewport;
3062 hr = pContext->pDevice->GetViewport(&viewport);
3063 AssertMsgReturn(hr == D3D_OK, ("vmsvga3dChangeMode: GetViewport failed with %x\n", hr), VERR_INTERNAL_ERROR);
3064
3065 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)));
3066 }
3067#endif
3068
3069 /* First set the render targets as they change the internal state (reset viewport etc) */
3070 Log(("vmsvga3dChangeMode: Recreate render targets BEGIN\n"));
3071 for (uint32_t j = 0; j < RT_ELEMENTS(pContext->state.aRenderTargets); j++)
3072 {
3073 if (pContext->state.aRenderTargets[j] != SVGA3D_INVALID_ID)
3074 {
3075 SVGA3dSurfaceImageId target;
3076
3077 target.sid = pContext->state.aRenderTargets[j];
3078 target.face = 0;
3079 target.mipmap = 0;
3080 int rc = vmsvga3dBackSetRenderTarget(pThisCC, cid, (SVGA3dRenderTargetType)j, target);
3081 AssertRCReturn(rc, rc);
3082 }
3083 }
3084
3085#ifdef VMSVGA3D_DIRECT3D9_RESET
3086 /* Recreate the render state */
3087 Log(("vmsvga3dChangeMode: Recreate render state BEGIN\n"));
3088 for (uint32_t i = 0; i < RT_ELEMENTS(pContext->state.aRenderState); i++)
3089 {
3090 SVGA3dRenderState *pRenderState = &pContext->state.aRenderState[i];
3091
3092 if (pRenderState->state != SVGA3D_RS_INVALID)
3093 vmsvga3dBackSetRenderState(pThisCC, pContext->id, 1, pRenderState);
3094 }
3095 Log(("vmsvga3dChangeMode: Recreate render state END\n"));
3096
3097 /* Recreate the texture state */
3098 Log(("vmsvga3dChangeMode: Recreate texture state BEGIN\n"));
3099 for (uint32_t iStage = 0; iStage < RT_ELEMENTS(pContext->state.aTextureStates); iStage++)
3100 {
3101 for (uint32_t j = 0; j < RT_ELEMENTS(pContext->state.aTextureStates[0]); j++)
3102 {
3103 SVGA3dTextureState *pTextureState = &pContext->state.aTextureStates[iStage][j];
3104
3105 if (pTextureState->name != SVGA3D_RS_INVALID)
3106 vmsvga3dBackSetTextureState(pThisCC, pContext->id, 1, pTextureState);
3107 }
3108 }
3109 Log(("vmsvga3dChangeMode: Recreate texture state END\n"));
3110
3111 if (pContext->state.u32UpdateFlags & VMSVGA3D_UPDATE_SCISSORRECT)
3112 vmsvga3dBackSetScissorRect(pThis, cid, &pContext->state.RectScissor);
3113 if (pContext->state.u32UpdateFlags & VMSVGA3D_UPDATE_ZRANGE)
3114 vmsvga3dBackSetZRange(pThisCC, cid, pContext->state.zRange);
3115 if (pContext->state.u32UpdateFlags & VMSVGA3D_UPDATE_VIEWPORT)
3116 vmsvga3dBackSetViewPort(pThis, cid, &pContext->state.RectViewPort);
3117 if (pContext->state.u32UpdateFlags & VMSVGA3D_UPDATE_VERTEXSHADER)
3118 vmsvga3dBackShaderSet(pThis, pContext, cid, SVGA3D_SHADERTYPE_VS, pContext->state.shidVertex);
3119 if (pContext->state.u32UpdateFlags & VMSVGA3D_UPDATE_PIXELSHADER)
3120 vmsvga3dBackShaderSet(pThis, pContext, cid, SVGA3D_SHADERTYPE_PS, pContext->state.shidPixel);
3121 /** @todo restore more state data */
3122#endif /* #ifdef VMSVGA3D_DIRECT3D9_RESET */
3123 }
3124 }
3125 return VINF_SUCCESS;
3126}
3127
3128
3129static DECLCALLBACK(int) vmsvga3dBackSetTransform(PVGASTATECC pThisCC, uint32_t cid, SVGA3dTransformType type, float matrix[16])
3130{
3131 D3DTRANSFORMSTATETYPE d3dState;
3132 HRESULT hr;
3133 PVMSVGA3DSTATE pState = pThisCC->svga.p3dState;
3134 AssertReturn(pState, VERR_NO_MEMORY);
3135
3136 Log(("vmsvga3dSetTransform %x %s\n", cid, vmsvgaTransformToString(type)));
3137
3138 ASSERT_GUEST_RETURN((unsigned)type < SVGA3D_TRANSFORM_MAX, VERR_INVALID_PARAMETER);
3139
3140 PVMSVGA3DCONTEXT pContext;
3141 int rc = vmsvga3dContextFromCid(pState, cid, &pContext);
3142 AssertRCReturn(rc, rc);
3143 AssertReturn(pContext->pDevice, VERR_INTERNAL_ERROR);
3144
3145 switch (type)
3146 {
3147 case SVGA3D_TRANSFORM_VIEW:
3148 d3dState = D3DTS_VIEW;
3149 break;
3150 case SVGA3D_TRANSFORM_PROJECTION:
3151 d3dState = D3DTS_PROJECTION;
3152 break;
3153 case SVGA3D_TRANSFORM_TEXTURE0:
3154 d3dState = D3DTS_TEXTURE0;
3155 break;
3156 case SVGA3D_TRANSFORM_TEXTURE1:
3157 d3dState = D3DTS_TEXTURE1;
3158 break;
3159 case SVGA3D_TRANSFORM_TEXTURE2:
3160 d3dState = D3DTS_TEXTURE2;
3161 break;
3162 case SVGA3D_TRANSFORM_TEXTURE3:
3163 d3dState = D3DTS_TEXTURE3;
3164 break;
3165 case SVGA3D_TRANSFORM_TEXTURE4:
3166 d3dState = D3DTS_TEXTURE4;
3167 break;
3168 case SVGA3D_TRANSFORM_TEXTURE5:
3169 d3dState = D3DTS_TEXTURE5;
3170 break;
3171 case SVGA3D_TRANSFORM_TEXTURE6:
3172 d3dState = D3DTS_TEXTURE6;
3173 break;
3174 case SVGA3D_TRANSFORM_TEXTURE7:
3175 d3dState = D3DTS_TEXTURE7;
3176 break;
3177 case SVGA3D_TRANSFORM_WORLD:
3178 d3dState = D3DTS_WORLD;
3179 break;
3180 case SVGA3D_TRANSFORM_WORLD1:
3181 d3dState = D3DTS_WORLD1;
3182 break;
3183 case SVGA3D_TRANSFORM_WORLD2:
3184 d3dState = D3DTS_WORLD2;
3185 break;
3186 case SVGA3D_TRANSFORM_WORLD3:
3187 d3dState = D3DTS_WORLD3;
3188 break;
3189
3190 default:
3191 Log(("vmsvga3dSetTransform: unknown type!!\n"));
3192 return VERR_INVALID_PARAMETER;
3193 }
3194
3195 /* Save this matrix for vm state save/restore. */
3196 pContext->state.aTransformState[type].fValid = true;
3197 memcpy(pContext->state.aTransformState[type].matrix, matrix, sizeof(pContext->state.aTransformState[type].matrix));
3198 pContext->state.u32UpdateFlags |= VMSVGA3D_UPDATE_TRANSFORM;
3199
3200 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)));
3201 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)));
3202 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)));
3203 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)));
3204 hr = pContext->pDevice->SetTransform(d3dState, (const D3DMATRIX *)matrix);
3205 AssertMsgReturn(hr == D3D_OK, ("vmsvga3dSetTransform: SetTransform failed with %x\n", hr), VERR_INTERNAL_ERROR);
3206 return VINF_SUCCESS;
3207}
3208
3209static DECLCALLBACK(int) vmsvga3dBackSetZRange(PVGASTATECC pThisCC, uint32_t cid, SVGA3dZRange zRange)
3210{
3211 D3DVIEWPORT9 viewport;
3212 HRESULT hr;
3213 PVMSVGA3DSTATE pState = pThisCC->svga.p3dState;
3214 AssertReturn(pState, VERR_NO_MEMORY);
3215
3216 Log(("vmsvga3dSetZRange %x min=%d max=%d\n", cid, (uint32_t)(zRange.min * 100.0), (uint32_t)(zRange.max * 100.0)));
3217
3218 PVMSVGA3DCONTEXT pContext;
3219 int rc = vmsvga3dContextFromCid(pState, cid, &pContext);
3220 AssertRCReturn(rc, rc);
3221 AssertReturn(pContext->pDevice, VERR_INTERNAL_ERROR);
3222
3223 pContext->state.zRange = zRange;
3224 pContext->state.u32UpdateFlags |= VMSVGA3D_UPDATE_ZRANGE;
3225
3226 hr = pContext->pDevice->GetViewport(&viewport);
3227 AssertMsgReturn(hr == D3D_OK, ("vmsvga3dSetZRange: GetViewport failed with %x\n", hr), VERR_INTERNAL_ERROR);
3228
3229 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)));
3230 /** @todo convert the depth range from -1-1 to 0-1 although we shouldn't be getting such values in the first place... */
3231 if (zRange.min < 0.0)
3232 zRange.min = 0.0;
3233 if (zRange.max > 1.0)
3234 zRange.max = 1.0;
3235
3236 viewport.MinZ = zRange.min;
3237 viewport.MaxZ = zRange.max;
3238 hr = pContext->pDevice->SetViewport(&viewport);
3239 AssertMsgReturn(hr == D3D_OK, ("vmsvga3dSetZRange: SetViewport failed with %x\n", hr), VERR_INTERNAL_ERROR);
3240 return VINF_SUCCESS;
3241}
3242
3243/**
3244 * Convert SVGA blend op value to its D3D equivalent
3245 */
3246static DWORD vmsvga3dBlendOp2D3D(uint32_t blendOp, DWORD defaultBlendOp)
3247{
3248 switch (blendOp)
3249 {
3250 case SVGA3D_BLENDOP_ZERO:
3251 return D3DBLEND_ZERO;
3252 case SVGA3D_BLENDOP_ONE:
3253 return D3DBLEND_ONE;
3254 case SVGA3D_BLENDOP_SRCCOLOR:
3255 return D3DBLEND_SRCCOLOR;
3256 case SVGA3D_BLENDOP_INVSRCCOLOR:
3257 return D3DBLEND_INVSRCCOLOR;
3258 case SVGA3D_BLENDOP_SRCALPHA:
3259 return D3DBLEND_SRCALPHA;
3260 case SVGA3D_BLENDOP_INVSRCALPHA:
3261 return D3DBLEND_INVSRCALPHA;
3262 case SVGA3D_BLENDOP_DESTALPHA:
3263 return D3DBLEND_DESTALPHA;
3264 case SVGA3D_BLENDOP_INVDESTALPHA:
3265 return D3DBLEND_INVDESTALPHA;
3266 case SVGA3D_BLENDOP_DESTCOLOR:
3267 return D3DBLEND_DESTCOLOR;
3268 case SVGA3D_BLENDOP_INVDESTCOLOR:
3269 return D3DBLEND_INVDESTCOLOR;
3270 case SVGA3D_BLENDOP_SRCALPHASAT:
3271 return D3DBLEND_SRCALPHASAT;
3272 case SVGA3D_BLENDOP_BLENDFACTOR:
3273 return D3DBLEND_BLENDFACTOR;
3274 case SVGA3D_BLENDOP_INVBLENDFACTOR:
3275 return D3DBLEND_INVBLENDFACTOR;
3276 default:
3277 AssertFailed();
3278 return defaultBlendOp;
3279 }
3280}
3281
3282static DECLCALLBACK(int) vmsvga3dBackSetRenderState(PVGASTATECC pThisCC, uint32_t cid, uint32_t cRenderStates, SVGA3dRenderState *pRenderState)
3283{
3284 DWORD val = 0; /* Shut up MSC */
3285 HRESULT hr;
3286 PVMSVGA3DSTATE pState = pThisCC->svga.p3dState;
3287 AssertReturn(pState, VERR_NO_MEMORY);
3288
3289 Log(("vmsvga3dSetRenderState cid=%u cRenderStates=%d\n", cid, cRenderStates));
3290
3291 PVMSVGA3DCONTEXT pContext;
3292 int rc = vmsvga3dContextFromCid(pState, cid, &pContext);
3293 AssertRCReturn(rc, rc);
3294 AssertReturn(pContext->pDevice, VERR_INTERNAL_ERROR);
3295
3296 for (unsigned i = 0; i < cRenderStates; i++)
3297 {
3298 D3DRENDERSTATETYPE renderState = D3DRS_FORCE_DWORD;
3299
3300 Log(("vmsvga3dSetRenderState: state=%s (%d) val=%x\n", vmsvga3dGetRenderStateName(pRenderState[i].state), pRenderState[i].state, pRenderState[i].uintValue));
3301 /* Save the render state for vm state saving. */
3302 ASSERT_GUEST_RETURN((unsigned)pRenderState[i].state < SVGA3D_RS_MAX, VERR_INVALID_PARAMETER);
3303 pContext->state.aRenderState[pRenderState[i].state] = pRenderState[i];
3304
3305 switch (pRenderState[i].state)
3306 {
3307 case SVGA3D_RS_ZENABLE: /* SVGA3dBool */
3308 renderState = D3DRS_ZENABLE;
3309 val = pRenderState[i].uintValue;
3310 Assert(val == D3DZB_FALSE || val == D3DZB_TRUE);
3311 break;
3312
3313 case SVGA3D_RS_ZWRITEENABLE: /* SVGA3dBool */
3314 renderState = D3DRS_ZWRITEENABLE;
3315 val = pRenderState[i].uintValue;
3316 break;
3317
3318 case SVGA3D_RS_ALPHATESTENABLE: /* SVGA3dBool */
3319 renderState = D3DRS_ALPHATESTENABLE;
3320 val = pRenderState[i].uintValue;
3321 break;
3322
3323 case SVGA3D_RS_DITHERENABLE: /* SVGA3dBool */
3324 renderState = D3DRS_DITHERENABLE;
3325 val = pRenderState[i].uintValue;
3326 break;
3327
3328 case SVGA3D_RS_BLENDENABLE: /* SVGA3dBool */
3329 renderState = D3DRS_ALPHABLENDENABLE;
3330 val = pRenderState[i].uintValue;
3331 break;
3332
3333 case SVGA3D_RS_FOGENABLE: /* SVGA3dBool */
3334 renderState = D3DRS_FOGENABLE;
3335 val = pRenderState[i].uintValue;
3336 break;
3337
3338 case SVGA3D_RS_SPECULARENABLE: /* SVGA3dBool */
3339 renderState = D3DRS_SPECULARENABLE;
3340 val = pRenderState[i].uintValue;
3341 break;
3342
3343 case SVGA3D_RS_LIGHTINGENABLE: /* SVGA3dBool */
3344 renderState = D3DRS_LIGHTING;
3345 val = pRenderState[i].uintValue;
3346 break;
3347
3348 case SVGA3D_RS_NORMALIZENORMALS: /* SVGA3dBool */
3349 renderState = D3DRS_NORMALIZENORMALS;
3350 val = pRenderState[i].uintValue;
3351 break;
3352
3353 case SVGA3D_RS_POINTSPRITEENABLE: /* SVGA3dBool */
3354 renderState = D3DRS_POINTSPRITEENABLE;
3355 val = pRenderState[i].uintValue;
3356 break;
3357
3358 case SVGA3D_RS_POINTSCALEENABLE: /* SVGA3dBool */
3359 renderState = D3DRS_POINTSCALEENABLE;
3360 val = pRenderState[i].uintValue;
3361 break;
3362
3363 case SVGA3D_RS_POINTSIZE: /* float */
3364 renderState = D3DRS_POINTSIZE;
3365 val = pRenderState[i].uintValue;
3366 Log(("SVGA3D_RS_POINTSIZE: %d\n", (uint32_t) (pRenderState[i].floatValue * 100.0)));
3367 break;
3368
3369 case SVGA3D_RS_POINTSIZEMIN: /* float */
3370 renderState = D3DRS_POINTSIZE_MIN;
3371 val = pRenderState[i].uintValue;
3372 Log(("SVGA3D_RS_POINTSIZEMIN: %d\n", (uint32_t) (pRenderState[i].floatValue * 100.0)));
3373 break;
3374
3375 case SVGA3D_RS_POINTSIZEMAX: /* float */
3376 renderState = D3DRS_POINTSIZE_MAX;
3377 val = pRenderState[i].uintValue;
3378 Log(("SVGA3D_RS_POINTSIZEMAX: %d\n", (uint32_t) (pRenderState[i].floatValue * 100.0)));
3379 break;
3380
3381 case SVGA3D_RS_POINTSCALE_A: /* float */
3382 renderState = D3DRS_POINTSCALE_A;
3383 val = pRenderState[i].uintValue;
3384 break;
3385
3386 case SVGA3D_RS_POINTSCALE_B: /* float */
3387 renderState = D3DRS_POINTSCALE_B;
3388 val = pRenderState[i].uintValue;
3389 break;
3390
3391 case SVGA3D_RS_POINTSCALE_C: /* float */
3392 renderState = D3DRS_POINTSCALE_C;
3393 val = pRenderState[i].uintValue;
3394 break;
3395
3396 case SVGA3D_RS_AMBIENT: /* SVGA3dColor - identical */
3397 renderState = D3DRS_AMBIENT;
3398 val = pRenderState[i].uintValue;
3399 break;
3400
3401 case SVGA3D_RS_CLIPPLANEENABLE: /* SVGA3dClipPlanes - identical */
3402 renderState = D3DRS_CLIPPLANEENABLE;
3403 val = pRenderState[i].uintValue;
3404 break;
3405
3406 case SVGA3D_RS_FOGCOLOR: /* SVGA3dColor - identical */
3407 renderState = D3DRS_FOGCOLOR;
3408 val = pRenderState[i].uintValue;
3409 break;
3410
3411 case SVGA3D_RS_FOGSTART: /* float */
3412 renderState = D3DRS_FOGSTART;
3413 val = pRenderState[i].uintValue;
3414 break;
3415
3416 case SVGA3D_RS_FOGEND: /* float */
3417 renderState = D3DRS_FOGEND;
3418 val = pRenderState[i].uintValue;
3419 break;
3420
3421 case SVGA3D_RS_FOGDENSITY: /* float */
3422 renderState = D3DRS_FOGDENSITY;
3423 val = pRenderState[i].uintValue;
3424 break;
3425
3426 case SVGA3D_RS_RANGEFOGENABLE: /* SVGA3dBool */
3427 renderState = D3DRS_RANGEFOGENABLE;
3428 val = pRenderState[i].uintValue;
3429 break;
3430
3431 case SVGA3D_RS_FOGMODE: /* SVGA3dFogMode */
3432 {
3433 SVGA3dFogMode mode;
3434 mode.uintValue = pRenderState[i].uintValue;
3435
3436 switch (mode.function)
3437 {
3438 case SVGA3D_FOGFUNC_INVALID:
3439 val = D3DFOG_NONE;
3440 break;
3441 case SVGA3D_FOGFUNC_EXP:
3442 val = D3DFOG_EXP;
3443 break;
3444 case SVGA3D_FOGFUNC_EXP2:
3445 val = D3DFOG_EXP2;
3446 break;
3447 case SVGA3D_FOGFUNC_LINEAR:
3448 val = D3DFOG_LINEAR;
3449 break;
3450 case SVGA3D_FOGFUNC_PER_VERTEX: /* unable to find a d3d9 equivalent */
3451 AssertMsgFailedReturn(("Unsupported fog function SVGA3D_FOGFUNC_PER_VERTEX\n"), VERR_INTERNAL_ERROR);
3452 break;
3453 default:
3454 AssertMsgFailedReturn(("Unexpected fog function %d\n", mode.function), VERR_INTERNAL_ERROR);
3455 break;
3456 }
3457
3458 /* The fog type determines the render state. */
3459 switch (mode.type)
3460 {
3461 case SVGA3D_FOGTYPE_VERTEX:
3462 renderState = D3DRS_FOGVERTEXMODE;
3463 break;
3464 case SVGA3D_FOGTYPE_PIXEL:
3465 renderState = D3DRS_FOGTABLEMODE;
3466 break;
3467 default:
3468 AssertMsgFailedReturn(("Unexpected fog type %d\n", mode.type), VERR_INTERNAL_ERROR);
3469 break;
3470 }
3471
3472 /* Set the fog base to depth or range. */
3473 switch (mode.base)
3474 {
3475 case SVGA3D_FOGBASE_DEPTHBASED:
3476 hr = pContext->pDevice->SetRenderState(D3DRS_RANGEFOGENABLE, FALSE);
3477 AssertMsgReturn(hr == D3D_OK, ("vmsvga3dSetRenderState: SetRenderState SVGA3D_FOGBASE_DEPTHBASED failed with %x\n", hr), VERR_INTERNAL_ERROR);
3478 break;
3479 case SVGA3D_FOGBASE_RANGEBASED:
3480 hr = pContext->pDevice->SetRenderState(D3DRS_RANGEFOGENABLE, TRUE);
3481 AssertMsgReturn(hr == D3D_OK, ("vmsvga3dSetRenderState: SetRenderState SVGA3D_FOGBASE_RANGEBASED failed with %x\n", hr), VERR_INTERNAL_ERROR);
3482 break;
3483 default:
3484 /* ignore */
3485 AssertMsgFailed(("Unexpected fog base %d\n", mode.base));
3486 break;
3487 }
3488 break;
3489 }
3490
3491 case SVGA3D_RS_FILLMODE: /* SVGA3dFillMode */
3492 {
3493 SVGA3dFillMode mode;
3494
3495 mode.uintValue = pRenderState[i].uintValue;
3496
3497 switch (mode.mode)
3498 {
3499 case SVGA3D_FILLMODE_POINT:
3500 val = D3DFILL_POINT;
3501 break;
3502 case SVGA3D_FILLMODE_LINE:
3503 val = D3DFILL_WIREFRAME;
3504 break;
3505 case SVGA3D_FILLMODE_FILL:
3506 val = D3DFILL_SOLID;
3507 break;
3508 default:
3509 AssertMsgFailedReturn(("Unexpected fill mode %d\n", mode.mode), VERR_INTERNAL_ERROR);
3510 break;
3511 }
3512 /** @todo ignoring face for now. */
3513 renderState = D3DRS_FILLMODE;
3514 break;
3515 }
3516
3517 case SVGA3D_RS_SHADEMODE: /* SVGA3dShadeMode */
3518 renderState = D3DRS_SHADEMODE;
3519 AssertCompile(D3DSHADE_FLAT == SVGA3D_SHADEMODE_FLAT);
3520 val = pRenderState[i].uintValue; /* SVGA3dShadeMode == D3DSHADEMODE */
3521 break;
3522
3523 case SVGA3D_RS_LINEPATTERN: /* SVGA3dLinePattern */
3524 /* No longer supported by d3d; mesagl comments suggest not all backends support it */
3525 /** @todo */
3526 Log(("WARNING: SVGA3D_RS_LINEPATTERN %x not supported!!\n", pRenderState[i].uintValue));
3527 /*
3528 renderState = D3DRS_LINEPATTERN;
3529 val = pRenderState[i].uintValue;
3530 */
3531 break;
3532
3533 case SVGA3D_RS_SRCBLEND: /* SVGA3dBlendOp */
3534 renderState = D3DRS_SRCBLEND;
3535 val = vmsvga3dBlendOp2D3D(pRenderState[i].uintValue, D3DBLEND_ONE /* default */);
3536 break;
3537
3538 case SVGA3D_RS_DSTBLEND: /* SVGA3dBlendOp */
3539 renderState = D3DRS_DESTBLEND;
3540 val = vmsvga3dBlendOp2D3D(pRenderState[i].uintValue, D3DBLEND_ZERO /* default */);
3541 break;
3542
3543 case SVGA3D_RS_BLENDEQUATION: /* SVGA3dBlendEquation - identical */
3544 AssertCompile(SVGA3D_BLENDEQ_MAXIMUM == D3DBLENDOP_MAX);
3545 renderState = D3DRS_BLENDOP;
3546 val = pRenderState[i].uintValue;
3547 break;
3548
3549 case SVGA3D_RS_CULLMODE: /* SVGA3dFace */
3550 {
3551 switch (pRenderState[i].uintValue)
3552 {
3553 case SVGA3D_FACE_NONE:
3554 val = D3DCULL_NONE;
3555 break;
3556 case SVGA3D_FACE_FRONT:
3557 val = D3DCULL_CW;
3558 break;
3559 case SVGA3D_FACE_BACK:
3560 val = D3DCULL_CCW;
3561 break;
3562 case SVGA3D_FACE_FRONT_BACK:
3563 AssertFailed();
3564 val = D3DCULL_CW;
3565 break;
3566 default:
3567 AssertMsgFailedReturn(("Unexpected cull mode %d\n", pRenderState[i].uintValue), VERR_INTERNAL_ERROR);
3568 break;
3569 }
3570 renderState = D3DRS_CULLMODE;
3571 break;
3572 }
3573
3574 case SVGA3D_RS_ZFUNC: /* SVGA3dCmpFunc - identical */
3575 AssertCompile(SVGA3D_CMP_ALWAYS == D3DCMP_ALWAYS);
3576 renderState = D3DRS_ZFUNC;
3577 val = pRenderState[i].uintValue;
3578 break;
3579
3580 case SVGA3D_RS_ALPHAFUNC: /* SVGA3dCmpFunc - identical */
3581 renderState = D3DRS_ALPHAFUNC;
3582 val = pRenderState[i].uintValue;
3583 break;
3584
3585 case SVGA3D_RS_STENCILENABLE: /* SVGA3dBool */
3586 renderState = D3DRS_STENCILENABLE;
3587 val = pRenderState[i].uintValue;
3588 break;
3589
3590 case SVGA3D_RS_STENCILREF: /* uint32_t */
3591 renderState = D3DRS_STENCILREF;
3592 val = pRenderState[i].uintValue;
3593 break;
3594
3595 case SVGA3D_RS_STENCILMASK: /* uint32_t */
3596 renderState = D3DRS_STENCILMASK;
3597 val = pRenderState[i].uintValue;
3598 break;
3599
3600 case SVGA3D_RS_STENCILWRITEMASK: /* uint32_t */
3601 renderState = D3DRS_STENCILWRITEMASK;
3602 val = pRenderState[i].uintValue;
3603 break;
3604
3605 case SVGA3D_RS_STENCILFUNC: /* SVGA3dCmpFunc - identical */
3606 renderState = D3DRS_STENCILFUNC;
3607 val = pRenderState[i].uintValue;
3608 break;
3609
3610 case SVGA3D_RS_STENCILFAIL: /* SVGA3dStencilOp - identical */
3611 AssertCompile(D3DSTENCILOP_KEEP == SVGA3D_STENCILOP_KEEP);
3612 AssertCompile(D3DSTENCILOP_DECR == SVGA3D_STENCILOP_DECR);
3613 renderState = D3DRS_STENCILFAIL;
3614 val = pRenderState[i].uintValue;
3615 break;
3616
3617 case SVGA3D_RS_STENCILZFAIL: /* SVGA3dStencilOp - identical */
3618 renderState = D3DRS_STENCILZFAIL;
3619 val = pRenderState[i].uintValue;
3620 break;
3621
3622 case SVGA3D_RS_STENCILPASS: /* SVGA3dStencilOp - identical */
3623 renderState = D3DRS_STENCILPASS;
3624 val = pRenderState[i].uintValue;
3625 break;
3626
3627 case SVGA3D_RS_ALPHAREF: /* float (0.0 .. 1.0) */
3628 renderState = D3DRS_ALPHAREF;
3629 val = (uint8_t)(pRenderState[i].floatValue * 255.0f); /* D3DRS_ALPHAREF 0..255 */
3630 break;
3631
3632 case SVGA3D_RS_FRONTWINDING: /* SVGA3dFrontWinding */
3633 Assert(pRenderState[i].uintValue == SVGA3D_FRONTWINDING_CW);
3634 /*
3635 renderState = D3DRS_FRONTWINDING; //D3DRS_TWOSIDEDSTENCILMODE
3636 val = pRenderState[i].uintValue;
3637 */
3638 break;
3639
3640 case SVGA3D_RS_COORDINATETYPE: /* SVGA3dCoordinateType */
3641 Assert(pRenderState[i].uintValue == SVGA3D_COORDINATE_LEFTHANDED);
3642 /** @todo setup a view matrix to scale the world space by -1 in the z-direction for right handed coordinates. */
3643 /*
3644 renderState = D3DRS_COORDINATETYPE;
3645 val = pRenderState[i].uintValue;
3646 */
3647 break;
3648
3649 case SVGA3D_RS_ZBIAS: /* float */
3650 /** @todo unknown meaning; depth bias is not identical
3651 renderState = D3DRS_DEPTHBIAS;
3652 val = pRenderState[i].uintValue;
3653 */
3654 Log(("vmsvga3dSetRenderState: WARNING unsupported SVGA3D_RS_ZBIAS\n"));
3655 break;
3656
3657 case SVGA3D_RS_SLOPESCALEDEPTHBIAS: /* float */
3658 renderState = D3DRS_SLOPESCALEDEPTHBIAS;
3659 val = pRenderState[i].uintValue;
3660 break;
3661
3662 case SVGA3D_RS_DEPTHBIAS: /* float */
3663 renderState = D3DRS_DEPTHBIAS;
3664 val = pRenderState[i].uintValue;
3665 break;
3666
3667 case SVGA3D_RS_COLORWRITEENABLE: /* SVGA3dColorMask - identical to D3DCOLORWRITEENABLE_* */
3668 renderState = D3DRS_COLORWRITEENABLE;
3669 val = pRenderState[i].uintValue;
3670 break;
3671
3672 case SVGA3D_RS_VERTEXMATERIALENABLE: /* SVGA3dBool */
3673 //AssertFailed();
3674 renderState = D3DRS_INDEXEDVERTEXBLENDENABLE; /* correct?? */
3675 val = pRenderState[i].uintValue;
3676 break;
3677
3678 case SVGA3D_RS_DIFFUSEMATERIALSOURCE: /* SVGA3dVertexMaterial - identical */
3679 AssertCompile(D3DMCS_COLOR2 == SVGA3D_VERTEXMATERIAL_SPECULAR);
3680 renderState = D3DRS_DIFFUSEMATERIALSOURCE;
3681 val = pRenderState[i].uintValue;
3682 break;
3683
3684 case SVGA3D_RS_SPECULARMATERIALSOURCE: /* SVGA3dVertexMaterial - identical */
3685 renderState = D3DRS_SPECULARMATERIALSOURCE;
3686 val = pRenderState[i].uintValue;
3687 break;
3688
3689 case SVGA3D_RS_AMBIENTMATERIALSOURCE: /* SVGA3dVertexMaterial - identical */
3690 renderState = D3DRS_AMBIENTMATERIALSOURCE;
3691 val = pRenderState[i].uintValue;
3692 break;
3693
3694 case SVGA3D_RS_EMISSIVEMATERIALSOURCE: /* SVGA3dVertexMaterial - identical */
3695 renderState = D3DRS_EMISSIVEMATERIALSOURCE;
3696 val = pRenderState[i].uintValue;
3697 break;
3698
3699 case SVGA3D_RS_TEXTUREFACTOR: /* SVGA3dColor - identical */
3700 renderState = D3DRS_TEXTUREFACTOR;
3701 val = pRenderState[i].uintValue;
3702 break;
3703
3704 case SVGA3D_RS_LOCALVIEWER: /* SVGA3dBool */
3705 renderState = D3DRS_LOCALVIEWER;
3706 val = pRenderState[i].uintValue;
3707 break;
3708
3709 case SVGA3D_RS_SCISSORTESTENABLE: /* SVGA3dBool */
3710 renderState = D3DRS_SCISSORTESTENABLE;
3711 val = pRenderState[i].uintValue;
3712 break;
3713
3714 case SVGA3D_RS_BLENDCOLOR: /* SVGA3dColor - identical */
3715 renderState = D3DRS_BLENDFACTOR;
3716 val = pRenderState[i].uintValue;
3717 break;
3718
3719 case SVGA3D_RS_STENCILENABLE2SIDED: /* SVGA3dBool */
3720 renderState = D3DRS_TWOSIDEDSTENCILMODE;
3721 val = pRenderState[i].uintValue;
3722 break;
3723
3724 case SVGA3D_RS_CCWSTENCILFUNC: /* SVGA3dCmpFunc - identical */
3725 renderState = D3DRS_CCW_STENCILFUNC;
3726 val = pRenderState[i].uintValue;
3727 break;
3728
3729 case SVGA3D_RS_CCWSTENCILFAIL: /* SVGA3dStencilOp - identical */
3730 renderState = D3DRS_CCW_STENCILFAIL;
3731 val = pRenderState[i].uintValue;
3732 break;
3733
3734 case SVGA3D_RS_CCWSTENCILZFAIL: /* SVGA3dStencilOp - identical */
3735 renderState = D3DRS_CCW_STENCILZFAIL;
3736 val = pRenderState[i].uintValue;
3737 break;
3738
3739 case SVGA3D_RS_CCWSTENCILPASS: /* SVGA3dStencilOp - identical */
3740 renderState = D3DRS_CCW_STENCILPASS;
3741 val = pRenderState[i].uintValue;
3742 break;
3743
3744 case SVGA3D_RS_VERTEXBLEND: /* SVGA3dVertexBlendFlags - identical */
3745 AssertCompile(SVGA3D_VBLEND_DISABLE == D3DVBF_DISABLE);
3746 renderState = D3DRS_VERTEXBLEND;
3747 val = pRenderState[i].uintValue;
3748 break;
3749
3750 case SVGA3D_RS_OUTPUTGAMMA: /* float */
3751 //AssertFailed();
3752 /*
3753 D3DRS_SRGBWRITEENABLE ??
3754 renderState = D3DRS_OUTPUTGAMMA;
3755 val = pRenderState[i].uintValue;
3756 */
3757 break;
3758
3759 case SVGA3D_RS_ZVISIBLE: /* SVGA3dBool */
3760 AssertFailed();
3761 /*
3762 renderState = D3DRS_ZVISIBLE;
3763 val = pRenderState[i].uintValue;
3764 */
3765 break;
3766
3767 case SVGA3D_RS_LASTPIXEL: /* SVGA3dBool */
3768 renderState = D3DRS_LASTPIXEL;
3769 val = pRenderState[i].uintValue;
3770 break;
3771
3772 case SVGA3D_RS_CLIPPING: /* SVGA3dBool */
3773 renderState = D3DRS_CLIPPING;
3774 val = pRenderState[i].uintValue;
3775 break;
3776
3777 case SVGA3D_RS_WRAP0: /* SVGA3dWrapFlags - identical */
3778 Assert(SVGA3D_WRAPCOORD_3 == D3DWRAPCOORD_3);
3779 renderState = D3DRS_WRAP0;
3780 val = pRenderState[i].uintValue;
3781 break;
3782
3783 case SVGA3D_RS_WRAP1: /* SVGA3dWrapFlags - identical */
3784 renderState = D3DRS_WRAP1;
3785 val = pRenderState[i].uintValue;
3786 break;
3787
3788 case SVGA3D_RS_WRAP2: /* SVGA3dWrapFlags - identical */
3789 renderState = D3DRS_WRAP2;
3790 val = pRenderState[i].uintValue;
3791 break;
3792
3793 case SVGA3D_RS_WRAP3: /* SVGA3dWrapFlags - identical */
3794 renderState = D3DRS_WRAP3;
3795 val = pRenderState[i].uintValue;
3796 break;
3797
3798 case SVGA3D_RS_WRAP4: /* SVGA3dWrapFlags - identical */
3799 renderState = D3DRS_WRAP4;
3800 val = pRenderState[i].uintValue;
3801 break;
3802
3803 case SVGA3D_RS_WRAP5: /* SVGA3dWrapFlags - identical */
3804 renderState = D3DRS_WRAP5;
3805 val = pRenderState[i].uintValue;
3806 break;
3807
3808 case SVGA3D_RS_WRAP6: /* SVGA3dWrapFlags - identical */
3809 renderState = D3DRS_WRAP6;
3810 val = pRenderState[i].uintValue;
3811 break;
3812
3813 case SVGA3D_RS_WRAP7: /* SVGA3dWrapFlags - identical */
3814 renderState = D3DRS_WRAP7;
3815 val = pRenderState[i].uintValue;
3816 break;
3817
3818 case SVGA3D_RS_WRAP8: /* SVGA3dWrapFlags - identical */
3819 renderState = D3DRS_WRAP8;
3820 val = pRenderState[i].uintValue;
3821 break;
3822
3823 case SVGA3D_RS_WRAP9: /* SVGA3dWrapFlags - identical */
3824 renderState = D3DRS_WRAP9;
3825 val = pRenderState[i].uintValue;
3826 break;
3827
3828 case SVGA3D_RS_WRAP10: /* SVGA3dWrapFlags - identical */
3829 renderState = D3DRS_WRAP10;
3830 val = pRenderState[i].uintValue;
3831 break;
3832
3833 case SVGA3D_RS_WRAP11: /* SVGA3dWrapFlags - identical */
3834 renderState = D3DRS_WRAP11;
3835 val = pRenderState[i].uintValue;
3836 break;
3837
3838 case SVGA3D_RS_WRAP12: /* SVGA3dWrapFlags - identical */
3839 renderState = D3DRS_WRAP12;
3840 val = pRenderState[i].uintValue;
3841 break;
3842
3843 case SVGA3D_RS_WRAP13: /* SVGA3dWrapFlags - identical */
3844 renderState = D3DRS_WRAP13;
3845 val = pRenderState[i].uintValue;
3846 break;
3847
3848 case SVGA3D_RS_WRAP14: /* SVGA3dWrapFlags - identical */
3849 renderState = D3DRS_WRAP14;
3850 val = pRenderState[i].uintValue;
3851 break;
3852
3853 case SVGA3D_RS_WRAP15: /* SVGA3dWrapFlags - identical */
3854 renderState = D3DRS_WRAP15;
3855 val = pRenderState[i].uintValue;
3856 break;
3857
3858 case SVGA3D_RS_MULTISAMPLEANTIALIAS: /* SVGA3dBool */
3859 renderState = D3DRS_MULTISAMPLEANTIALIAS;
3860 val = pRenderState[i].uintValue;
3861 break;
3862
3863 case SVGA3D_RS_MULTISAMPLEMASK: /* uint32_t */
3864 renderState = D3DRS_MULTISAMPLEMASK;
3865 val = pRenderState[i].uintValue;
3866 break;
3867
3868 case SVGA3D_RS_INDEXEDVERTEXBLENDENABLE: /* SVGA3dBool */
3869 renderState = D3DRS_INDEXEDVERTEXBLENDENABLE;
3870 val = pRenderState[i].uintValue;
3871 break;
3872
3873 case SVGA3D_RS_TWEENFACTOR: /* float */
3874 renderState = D3DRS_TWEENFACTOR;
3875 val = pRenderState[i].uintValue;
3876 break;
3877
3878 case SVGA3D_RS_ANTIALIASEDLINEENABLE: /* SVGA3dBool */
3879 renderState = D3DRS_ANTIALIASEDLINEENABLE;
3880 val = pRenderState[i].uintValue;
3881 break;
3882
3883 case SVGA3D_RS_COLORWRITEENABLE1: /* SVGA3dColorMask - identical to D3DCOLORWRITEENABLE_* */
3884 renderState = D3DRS_COLORWRITEENABLE1;
3885 val = pRenderState[i].uintValue;
3886 break;
3887
3888 case SVGA3D_RS_COLORWRITEENABLE2: /* SVGA3dColorMask - identical to D3DCOLORWRITEENABLE_* */
3889 renderState = D3DRS_COLORWRITEENABLE2;
3890 val = pRenderState[i].uintValue;
3891 break;
3892
3893 case SVGA3D_RS_COLORWRITEENABLE3: /* SVGA3dColorMask - identical to D3DCOLORWRITEENABLE_* */
3894 renderState = D3DRS_COLORWRITEENABLE3;
3895 val = pRenderState[i].uintValue;
3896 break;
3897
3898 case SVGA3D_RS_SEPARATEALPHABLENDENABLE: /* SVGA3dBool */
3899 renderState = D3DRS_SEPARATEALPHABLENDENABLE;
3900 val = pRenderState[i].uintValue;
3901 break;
3902
3903 case SVGA3D_RS_SRCBLENDALPHA: /* SVGA3dBlendOp */
3904 renderState = D3DRS_SRCBLENDALPHA;
3905 val = vmsvga3dBlendOp2D3D(pRenderState[i].uintValue, D3DBLEND_ONE /* default */);
3906 break;
3907
3908 case SVGA3D_RS_DSTBLENDALPHA: /* SVGA3dBlendOp */
3909 renderState = D3DRS_DESTBLENDALPHA;
3910 val = vmsvga3dBlendOp2D3D(pRenderState[i].uintValue, D3DBLEND_ZERO /* default */);
3911 break;
3912
3913 case SVGA3D_RS_BLENDEQUATIONALPHA: /* SVGA3dBlendEquation - identical */
3914 renderState = D3DRS_BLENDOPALPHA;
3915 val = pRenderState[i].uintValue;
3916 break;
3917
3918 case SVGA3D_RS_TRANSPARENCYANTIALIAS: /* SVGA3dTransparencyAntialiasType */
3919 AssertFailed();
3920 /*
3921 renderState = D3DRS_TRANSPARENCYANTIALIAS;
3922 val = pRenderState[i].uintValue;
3923 */
3924 break;
3925
3926 case SVGA3D_RS_LINEWIDTH: /* float */
3927 AssertFailed();
3928 /*
3929 renderState = D3DRS_LINEWIDTH;
3930 val = pRenderState[i].uintValue;
3931 */
3932 break;
3933
3934 case SVGA3D_RS_MAX: /* shut up MSC */
3935 case SVGA3D_RS_INVALID:
3936 AssertFailedBreak();
3937 }
3938
3939 if (renderState != D3DRS_FORCE_DWORD)
3940 {
3941 hr = pContext->pDevice->SetRenderState(renderState, val);
3942 AssertMsgReturn(hr == D3D_OK, ("vmsvga3dSetRenderState: SetRenderState failed with %x\n", hr), VERR_INTERNAL_ERROR);
3943 }
3944 }
3945
3946 return VINF_SUCCESS;
3947}
3948
3949static DECLCALLBACK(int) vmsvga3dBackSetRenderTarget(PVGASTATECC pThisCC, uint32_t cid, SVGA3dRenderTargetType type, SVGA3dSurfaceImageId target)
3950{
3951 HRESULT hr;
3952 PVMSVGA3DSTATE pState = pThisCC->svga.p3dState;
3953
3954 AssertReturn(pState, VERR_NO_MEMORY);
3955 AssertReturn((unsigned)type < SVGA3D_RT_MAX, VERR_INVALID_PARAMETER);
3956
3957 LogFunc(("cid=%u type=%x sid=%u face=%u mipmap=%u\n", cid, type, target.sid, target.face, target.mipmap));
3958
3959 PVMSVGA3DCONTEXT pContext;
3960 int rc = vmsvga3dContextFromCid(pState, cid, &pContext);
3961 AssertRCReturn(rc, rc);
3962 AssertReturn(pContext->pDevice, VERR_INTERNAL_ERROR);
3963
3964 /* Save for vm state save/restore. */
3965 pContext->state.aRenderTargets[type] = target.sid;
3966 /** @todo Also save target.face and target.mipmap */
3967
3968 if (target.sid == SVGA3D_INVALID_ID)
3969 {
3970 /* Disable render target. */
3971 switch (type)
3972 {
3973 case SVGA3D_RT_DEPTH:
3974 hr = pContext->pDevice->SetDepthStencilSurface(NULL);
3975 AssertMsgReturn(hr == D3D_OK, ("SetDepthStencilSurface failed with %x\n", hr), VERR_INTERNAL_ERROR);
3976 break;
3977
3978 case SVGA3D_RT_STENCIL:
3979 /* ignore; correct?? */
3980 break;
3981
3982 case SVGA3D_RT_COLOR0:
3983 case SVGA3D_RT_COLOR1:
3984 case SVGA3D_RT_COLOR2:
3985 case SVGA3D_RT_COLOR3:
3986 case SVGA3D_RT_COLOR4:
3987 case SVGA3D_RT_COLOR5:
3988 case SVGA3D_RT_COLOR6:
3989 case SVGA3D_RT_COLOR7:
3990 if (pState->fSupportedSurfaceNULL)
3991 {
3992 /* Create a dummy render target to satisfy D3D. This path is usually taken only to render
3993 * into a depth buffer without wishing to update an actual color render target.
3994 * The dimensions of the render target must match the dimensions of the depth render target,
3995 * which is usually equal to the viewport width and height.
3996 */
3997 IDirect3DSurface9 *pDummyRenderTarget;
3998 hr = pContext->pDevice->CreateRenderTarget(pContext->state.RectViewPort.w,
3999 pContext->state.RectViewPort.h,
4000 FOURCC_NULL,
4001 D3DMULTISAMPLE_NONE,
4002 0,
4003 FALSE,
4004 &pDummyRenderTarget,
4005 NULL);
4006
4007 AssertMsgReturn(hr == D3D_OK, ("CreateRenderTarget failed with %x\n", hr), VERR_INTERNAL_ERROR);
4008
4009 hr = pContext->pDevice->SetRenderTarget(type - SVGA3D_RT_COLOR0, pDummyRenderTarget);
4010 D3D_RELEASE(pDummyRenderTarget);
4011 }
4012 else
4013 hr = pContext->pDevice->SetRenderTarget(type - SVGA3D_RT_COLOR0, NULL);
4014
4015 AssertMsgReturn(hr == D3D_OK, ("SetRenderTarget failed with %x\n", hr), VERR_INTERNAL_ERROR);
4016 break;
4017
4018 default:
4019 AssertFailedReturn(VERR_INVALID_PARAMETER);
4020 }
4021 return VINF_SUCCESS;
4022 }
4023
4024 PVMSVGA3DSURFACE pRenderTarget;
4025 rc = vmsvga3dSurfaceFromSid(pState, target.sid, &pRenderTarget);
4026 AssertRCReturn(rc, rc);
4027
4028 switch (type)
4029 {
4030 case SVGA3D_RT_DEPTH:
4031 case SVGA3D_RT_STENCIL:
4032 AssertReturn(target.face == 0 && target.mipmap == 0, VERR_INVALID_PARAMETER);
4033 if (!pRenderTarget->u.pSurface)
4034 {
4035 DWORD cQualityLevels = 0;
4036
4037 /* Query the nr of quality levels for this particular format */
4038 if (pRenderTarget->multiSampleTypeD3D != D3DMULTISAMPLE_NONE)
4039 {
4040 hr = pState->pD3D9->CheckDeviceMultiSampleType(D3DADAPTER_DEFAULT,
4041 D3DDEVTYPE_HAL,
4042 pRenderTarget->formatD3D,
4043 TRUE, /* Windowed */
4044 pRenderTarget->multiSampleTypeD3D,
4045 &cQualityLevels);
4046 Assert(hr == D3D_OK || hr == D3DERR_NOTAVAILABLE);
4047 }
4048
4049 if ( pState->fSupportedSurfaceINTZ
4050 && pRenderTarget->multiSampleTypeD3D == D3DMULTISAMPLE_NONE
4051 && ( pRenderTarget->formatD3D == D3DFMT_D24S8
4052 || pRenderTarget->formatD3D == D3DFMT_D24X8
4053 || pRenderTarget->formatD3D == D3DFMT_D32
4054 || pRenderTarget->formatD3D == D3DFMT_D16))
4055 {
4056 LogFunc(("Creating stencil surface as texture!\n"));
4057 int rc2 = vmsvga3dBackCreateTexture(pThisCC, pContext, cid, pRenderTarget);
4058 AssertRC(rc2); /* non-fatal, will use CreateDepthStencilSurface */
4059 }
4060
4061 if (!pRenderTarget->fStencilAsTexture)
4062 {
4063 Assert(!pRenderTarget->u.pSurface);
4064
4065 LogFunc(("DEPTH/STENCIL; cQualityLevels=%d\n", cQualityLevels));
4066 hr = pContext->pDevice->CreateDepthStencilSurface(pRenderTarget->paMipmapLevels[0].mipmapSize.width,
4067 pRenderTarget->paMipmapLevels[0].mipmapSize.height,
4068 pRenderTarget->formatD3D,
4069 pRenderTarget->multiSampleTypeD3D,
4070 ((cQualityLevels >= 1) ? cQualityLevels - 1 : 0), /* 0 - (levels-1) */
4071 FALSE, /* not discardable */
4072 &pRenderTarget->u.pSurface,
4073 NULL);
4074 AssertMsgReturn(hr == D3D_OK, ("CreateDepthStencilSurface failed with %x\n", hr), VERR_INTERNAL_ERROR);
4075 pRenderTarget->enmD3DResType = VMSVGA3D_D3DRESTYPE_SURFACE;
4076 }
4077
4078 pRenderTarget->idAssociatedContext = cid;
4079
4080#if 0 /* doesn't work */
4081 if ( !pRenderTarget->fStencilAsTexture
4082 && pRenderTarget->fDirty)
4083 {
4084 Log(("vmsvga3dSetRenderTarget: sync dirty depth/stencil buffer\n"));
4085 Assert(pRenderTarget->cLevels == 1);
4086
4087 for (uint32_t i = 0; i < pRenderTarget->cLevels; i++)
4088 {
4089 if (pRenderTarget->paMipmapLevels[i].fDirty)
4090 {
4091 D3DLOCKED_RECT LockedRect;
4092
4093 hr = pRenderTarget->u.pSurface->LockRect(&LockedRect,
4094 NULL, /* entire surface */
4095 0);
4096
4097 AssertMsgReturn(hr == D3D_OK, ("vmsvga3dSetRenderTarget: LockRect failed with %x\n", hr), VERR_INTERNAL_ERROR);
4098
4099 Log(("vmsvga3dSetRenderTarget: sync dirty texture mipmap level %d (pitch %x vs %x)\n", i, LockedRect.Pitch, pRenderTarget->paMipmapLevels[i].cbSurfacePitch));
4100
4101 uint8_t *pDest = (uint8_t *)LockedRect.pBits;
4102 uint8_t *pSrc = (uint8_t *)pRenderTarget->paMipmapLevels[i].pSurfaceData;
4103 for (uint32_t j = 0; j < pRenderTarget->paMipmapLevels[i].size.height; j++)
4104 {
4105 memcpy(pDest, pSrc, pRenderTarget->paMipmapLevels[i].cbSurfacePitch);
4106
4107 pDest += LockedRect.Pitch;
4108 pSrc += pRenderTarget->paMipmapLevels[i].cbSurfacePitch;
4109 }
4110
4111 hr = pRenderTarget->u.pSurface->UnlockRect();
4112 AssertMsgReturn(hr == D3D_OK, ("vmsvga3dSetRenderTarget: UnlockRect failed with %x\n", hr), VERR_INTERNAL_ERROR);
4113
4114 pRenderTarget->paMipmapLevels[i].fDirty = false;
4115 }
4116 }
4117 }
4118#endif
4119 }
4120
4121 /** @todo Assert(!pRenderTarget->fDirty); */
4122
4123 AssertReturn(pRenderTarget->u.pSurface, VERR_INVALID_PARAMETER);
4124
4125 pRenderTarget->fUsageD3D |= D3DUSAGE_DEPTHSTENCIL;
4126 pRenderTarget->surfaceFlags |= SVGA3D_SURFACE_HINT_DEPTHSTENCIL;
4127
4128 if (pRenderTarget->fStencilAsTexture)
4129 {
4130 IDirect3DSurface9 *pStencilSurface;
4131
4132 rc = vmsvga3dGetD3DSurface(pState, pContext, pRenderTarget, target.face, target.mipmap, /*fLockable=*/ false, &pStencilSurface);
4133 AssertRCReturn(rc, rc);
4134
4135 hr = pContext->pDevice->SetDepthStencilSurface(pStencilSurface);
4136 D3D_RELEASE(pStencilSurface);
4137 AssertMsgReturn(hr == D3D_OK, ("SetDepthStencilSurface failed with %x\n", hr), VERR_INTERNAL_ERROR);
4138 }
4139 else
4140 {
4141 Assert(pRenderTarget->idAssociatedContext == cid);
4142 AssertReturn(pRenderTarget->enmD3DResType == VMSVGA3D_D3DRESTYPE_SURFACE, VERR_INVALID_PARAMETER);
4143 hr = pContext->pDevice->SetDepthStencilSurface(pRenderTarget->u.pSurface);
4144 AssertMsgReturn(hr == D3D_OK, ("SetDepthStencilSurface failed with %x\n", hr), VERR_INTERNAL_ERROR);
4145 }
4146 break;
4147
4148 case SVGA3D_RT_COLOR0:
4149 case SVGA3D_RT_COLOR1:
4150 case SVGA3D_RT_COLOR2:
4151 case SVGA3D_RT_COLOR3:
4152 case SVGA3D_RT_COLOR4:
4153 case SVGA3D_RT_COLOR5:
4154 case SVGA3D_RT_COLOR6:
4155 case SVGA3D_RT_COLOR7:
4156 {
4157 IDirect3DSurface9 *pSurface;
4158 bool fTexture = false;
4159
4160 /* Must flush the other context's 3d pipeline to make sure all drawing is complete for the surface we're about to use. */
4161 vmsvga3dSurfaceFlush(pRenderTarget);
4162
4163 if (pRenderTarget->surfaceFlags & SVGA3D_SURFACE_HINT_TEXTURE)
4164 {
4165 fTexture = true;
4166
4167 /* A texture surface can be used as a render target to fill it and later on used as a texture. */
4168 if (!pRenderTarget->u.pTexture)
4169 {
4170 LogFunc(("Create texture to be used as render target; sid=%u type=%d format=%d -> create texture\n", target.sid, pRenderTarget->surfaceFlags, pRenderTarget->format));
4171 rc = vmsvga3dBackCreateTexture(pThisCC, pContext, cid, pRenderTarget);
4172 AssertRCReturn(rc, rc);
4173 }
4174
4175 rc = vmsvga3dGetD3DSurface(pState, pContext, pRenderTarget, target.face, target.mipmap, false, &pSurface);
4176 AssertRCReturn(rc, rc);
4177 }
4178 else
4179 {
4180 AssertReturn(target.face == 0 && target.mipmap == 0, VERR_INVALID_PARAMETER);
4181 if (!pRenderTarget->u.pSurface)
4182 {
4183 DWORD cQualityLevels = 0;
4184
4185 /* Query the nr of quality levels for this particular format */
4186 if (pRenderTarget->multiSampleTypeD3D != D3DMULTISAMPLE_NONE)
4187 {
4188 hr = pState->pD3D9->CheckDeviceMultiSampleType(D3DADAPTER_DEFAULT,
4189 D3DDEVTYPE_HAL,
4190 pRenderTarget->formatD3D,
4191 TRUE, /* Windowed */
4192 pRenderTarget->multiSampleTypeD3D,
4193 &cQualityLevels);
4194 Assert(hr == D3D_OK || hr == D3DERR_NOTAVAILABLE);
4195 }
4196
4197 LogFunc(("COLOR; cQualityLevels=%d\n", cQualityLevels));
4198 LogFunc(("Create rendertarget (%d,%d) formatD3D=%x multisample=%x\n",
4199 pRenderTarget->paMipmapLevels[0].mipmapSize.width, pRenderTarget->paMipmapLevels[0].mipmapSize.height, pRenderTarget->formatD3D, pRenderTarget->multiSampleTypeD3D));
4200
4201 hr = pContext->pDevice->CreateRenderTarget(pRenderTarget->paMipmapLevels[0].mipmapSize.width,
4202 pRenderTarget->paMipmapLevels[0].mipmapSize.height,
4203 pRenderTarget->formatD3D,
4204 pRenderTarget->multiSampleTypeD3D,
4205 ((cQualityLevels >= 1) ? cQualityLevels - 1 : 0), /* 0 - (levels-1) */
4206 TRUE, /* lockable */
4207 &pRenderTarget->u.pSurface,
4208 NULL);
4209 AssertReturn(hr == D3D_OK, VERR_INTERNAL_ERROR);
4210
4211 pRenderTarget->idAssociatedContext = cid;
4212 pRenderTarget->enmD3DResType = VMSVGA3D_D3DRESTYPE_SURFACE;
4213 }
4214 else
4215 AssertReturn(pRenderTarget->fUsageD3D & D3DUSAGE_RENDERTARGET, VERR_INVALID_PARAMETER);
4216
4217 Assert(pRenderTarget->idAssociatedContext == cid);
4218 AssertMsgReturn(pRenderTarget->enmD3DResType == VMSVGA3D_D3DRESTYPE_SURFACE,
4219 ("Invalid render target %#x\n", pRenderTarget->enmD3DResType),
4220 VERR_INVALID_PARAMETER);
4221 pSurface = pRenderTarget->u.pSurface;
4222 }
4223
4224 AssertReturn(pSurface, VERR_INVALID_PARAMETER);
4225 Assert(!pRenderTarget->fDirty);
4226
4227 pRenderTarget->fUsageD3D |= D3DUSAGE_RENDERTARGET;
4228 pRenderTarget->surfaceFlags |= SVGA3D_SURFACE_HINT_RENDERTARGET;
4229
4230 hr = pContext->pDevice->SetRenderTarget(type - SVGA3D_RT_COLOR0, pSurface);
4231 if (fTexture)
4232 D3D_RELEASE(pSurface); /* Release reference to texture level 0 */
4233 AssertMsgReturn(hr == D3D_OK, ("SetRenderTarget failed with %x\n", hr), VERR_INTERNAL_ERROR);
4234
4235 /* Changing the render target resets the viewport; restore it here. */
4236 if (pContext->state.u32UpdateFlags & VMSVGA3D_UPDATE_VIEWPORT)
4237 vmsvga3dBackSetViewPort(pThisCC, cid, &pContext->state.RectViewPort);
4238 if (pContext->state.u32UpdateFlags & VMSVGA3D_UPDATE_ZRANGE)
4239 vmsvga3dBackSetZRange(pThisCC, cid, pContext->state.zRange);
4240 /* Changing the render target also resets the scissor rectangle; restore it as well. */
4241 if (pContext->state.u32UpdateFlags & VMSVGA3D_UPDATE_SCISSORRECT)
4242 vmsvga3dBackSetScissorRect(pThisCC, cid, &pContext->state.RectScissor);
4243
4244 break;
4245 }
4246
4247 default:
4248 AssertFailedReturn(VERR_INVALID_PARAMETER);
4249 }
4250
4251 return VINF_SUCCESS;
4252}
4253
4254/**
4255 * Convert SVGA texture combiner value to its D3D equivalent
4256 */
4257static DWORD vmsvga3dTextureCombiner2D3D(uint32_t value)
4258{
4259 switch (value)
4260 {
4261 case SVGA3D_TC_DISABLE:
4262 return D3DTOP_DISABLE;
4263 case SVGA3D_TC_SELECTARG1:
4264 return D3DTOP_SELECTARG1;
4265 case SVGA3D_TC_SELECTARG2:
4266 return D3DTOP_SELECTARG2;
4267 case SVGA3D_TC_MODULATE:
4268 return D3DTOP_MODULATE;
4269 case SVGA3D_TC_ADD:
4270 return D3DTOP_ADD;
4271 case SVGA3D_TC_ADDSIGNED:
4272 return D3DTOP_ADDSIGNED;
4273 case SVGA3D_TC_SUBTRACT:
4274 return D3DTOP_SUBTRACT;
4275 case SVGA3D_TC_BLENDTEXTUREALPHA:
4276 return D3DTOP_BLENDTEXTUREALPHA;
4277 case SVGA3D_TC_BLENDDIFFUSEALPHA:
4278 return D3DTOP_BLENDDIFFUSEALPHA;
4279 case SVGA3D_TC_BLENDCURRENTALPHA:
4280 return D3DTOP_BLENDCURRENTALPHA;
4281 case SVGA3D_TC_BLENDFACTORALPHA:
4282 return D3DTOP_BLENDFACTORALPHA;
4283 case SVGA3D_TC_MODULATE2X:
4284 return D3DTOP_MODULATE2X;
4285 case SVGA3D_TC_MODULATE4X:
4286 return D3DTOP_MODULATE4X;
4287 case SVGA3D_TC_DSDT:
4288 AssertFailed(); /** @todo ??? */
4289 return D3DTOP_DISABLE;
4290 case SVGA3D_TC_DOTPRODUCT3:
4291 return D3DTOP_DOTPRODUCT3;
4292 case SVGA3D_TC_BLENDTEXTUREALPHAPM:
4293 return D3DTOP_BLENDTEXTUREALPHAPM;
4294 case SVGA3D_TC_ADDSIGNED2X:
4295 return D3DTOP_ADDSIGNED2X;
4296 case SVGA3D_TC_ADDSMOOTH:
4297 return D3DTOP_ADDSMOOTH;
4298 case SVGA3D_TC_PREMODULATE:
4299 return D3DTOP_PREMODULATE;
4300 case SVGA3D_TC_MODULATEALPHA_ADDCOLOR:
4301 return D3DTOP_MODULATEALPHA_ADDCOLOR;
4302 case SVGA3D_TC_MODULATECOLOR_ADDALPHA:
4303 return D3DTOP_MODULATECOLOR_ADDALPHA;
4304 case SVGA3D_TC_MODULATEINVALPHA_ADDCOLOR:
4305 return D3DTOP_MODULATEINVALPHA_ADDCOLOR;
4306 case SVGA3D_TC_MODULATEINVCOLOR_ADDALPHA:
4307 return D3DTOP_MODULATEINVCOLOR_ADDALPHA;
4308 case SVGA3D_TC_BUMPENVMAPLUMINANCE:
4309 return D3DTOP_BUMPENVMAPLUMINANCE;
4310 case SVGA3D_TC_MULTIPLYADD:
4311 return D3DTOP_MULTIPLYADD;
4312 case SVGA3D_TC_LERP:
4313 return D3DTOP_LERP;
4314 default:
4315 AssertFailed();
4316 return D3DTOP_DISABLE;
4317 }
4318}
4319
4320/**
4321 * Convert SVGA texture arg data value to its D3D equivalent
4322 */
4323static DWORD vmsvga3dTextureArgData2D3D(uint32_t value)
4324{
4325 switch (value)
4326 {
4327 case SVGA3D_TA_CONSTANT:
4328 return D3DTA_CONSTANT;
4329 case SVGA3D_TA_PREVIOUS:
4330 return D3DTA_CURRENT; /* current = previous */
4331 case SVGA3D_TA_DIFFUSE:
4332 return D3DTA_DIFFUSE;
4333 case SVGA3D_TA_TEXTURE:
4334 return D3DTA_TEXTURE;
4335 case SVGA3D_TA_SPECULAR:
4336 return D3DTA_SPECULAR;
4337 default:
4338 AssertFailed();
4339 return D3DTA_DIFFUSE;
4340 }
4341}
4342
4343/**
4344 * Convert SVGA texture transform flag value to its D3D equivalent
4345 */
4346static DWORD vmsvga3dTextTransformFlags2D3D(uint32_t value)
4347{
4348 switch (value)
4349 {
4350 case SVGA3D_TEX_TRANSFORM_OFF:
4351 return D3DTTFF_DISABLE;
4352 case SVGA3D_TEX_TRANSFORM_S:
4353 return D3DTTFF_COUNT1; /** @todo correct? */
4354 case SVGA3D_TEX_TRANSFORM_T:
4355 return D3DTTFF_COUNT2; /** @todo correct? */
4356 case SVGA3D_TEX_TRANSFORM_R:
4357 return D3DTTFF_COUNT3; /** @todo correct? */
4358 case SVGA3D_TEX_TRANSFORM_Q:
4359 return D3DTTFF_COUNT4; /** @todo correct? */
4360 case SVGA3D_TEX_PROJECTED:
4361 return D3DTTFF_PROJECTED;
4362 default:
4363 AssertFailed();
4364 return D3DTTFF_DISABLE;
4365 }
4366}
4367
4368static DWORD vmsvga3dSamplerIndex2D3D(uint32_t idxSampler)
4369{
4370 if (idxSampler < SVGA3D_MAX_SAMPLERS_PS)
4371 return idxSampler;
4372 return (idxSampler - SVGA3D_MAX_SAMPLERS_PS) + D3DDMAPSAMPLER;
4373}
4374
4375static DECLCALLBACK(int) vmsvga3dBackSetTextureState(PVGASTATECC pThisCC, uint32_t cid, uint32_t cTextureStates, SVGA3dTextureState *pTextureState)
4376{
4377 DWORD val = 0; /* Shut up MSC */
4378 HRESULT hr;
4379 PVMSVGA3DCONTEXT pContext;
4380 PVMSVGA3DSTATE pState = pThisCC->svga.p3dState;
4381 AssertReturn(pState, VERR_NO_MEMORY);
4382
4383 LogFunc(("%x cTextureState=%d\n", cid, cTextureStates));
4384
4385 int rc = vmsvga3dContextFromCid(pState, cid, &pContext);
4386 AssertRCReturn(rc, rc);
4387 AssertReturn(pContext->pDevice, VERR_INTERNAL_ERROR);
4388
4389 for (unsigned i = 0; i < cTextureStates; i++)
4390 {
4391 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));
4392
4393 if (pTextureState[i].name == SVGA3D_TS_BIND_TEXTURE)
4394 {
4395 /* Special case: binding a texture to a sampler. Stage is the sampler index. */
4396 const uint32_t sid = pTextureState[i].value;
4397 const uint32_t idxSampler = pTextureState[i].stage;
4398
4399 if (RT_UNLIKELY(idxSampler >= SVGA3D_MAX_SAMPLERS))
4400 {
4401 AssertMsgFailed(("pTextureState[%d]: SVGA3D_TS_BIND_TEXTURE idxSampler=%d, sid=%u\n", i, idxSampler, sid));
4402 continue;
4403 }
4404
4405 const DWORD d3dSampler = vmsvga3dSamplerIndex2D3D(idxSampler);
4406 if (sid == SVGA3D_INVALID_ID)
4407 {
4408 LogFunc(("SVGA3D_TS_BIND_TEXTURE: unbind sampler=%d\n", idxSampler));
4409
4410 pContext->aSidActiveTextures[idxSampler] = SVGA3D_INVALID_ID;
4411
4412 /* Unselect the currently associated texture. */
4413 hr = pContext->pDevice->SetTexture(d3dSampler, NULL);
4414 AssertMsgReturn(hr == D3D_OK, ("SetTexture failed with %x\n", hr), VERR_INTERNAL_ERROR);
4415 }
4416 else
4417 {
4418 PVMSVGA3DSURFACE pSurface;
4419 rc = vmsvga3dSurfaceFromSid(pState, sid, &pSurface);
4420 AssertRCReturn(rc, rc);
4421
4422 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));
4423
4424 if (!pSurface->u.pTexture)
4425 {
4426 Assert(pSurface->idAssociatedContext == SVGA3D_INVALID_ID);
4427 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));
4428 rc = vmsvga3dBackCreateTexture(pThisCC, pContext, cid, pSurface);
4429 AssertRCReturn(rc, rc);
4430 }
4431 else
4432 {
4433 /* Must flush the other context's 3d pipeline to make sure all drawing is complete for the surface we're about to use. */
4434 vmsvga3dSurfaceFlush(pSurface);
4435 }
4436
4437 AssertReturn( pSurface->enmD3DResType == VMSVGA3D_D3DRESTYPE_TEXTURE
4438 || pSurface->enmD3DResType == VMSVGA3D_D3DRESTYPE_CUBE_TEXTURE
4439 || pSurface->enmD3DResType == VMSVGA3D_D3DRESTYPE_VOLUME_TEXTURE,
4440 VERR_INVALID_PARAMETER);
4441
4442#ifndef VBOX_VMSVGA3D_WITH_WINE_OPENGL
4443 if (pSurface->idAssociatedContext != cid)
4444 {
4445 LogFunc(("Using texture sid=%u created for another context (%d vs %d)\n", sid, pSurface->idAssociatedContext, cid));
4446
4447 PVMSVGA3DSHAREDSURFACE pSharedSurface = vmsvga3dSurfaceGetSharedCopy(pState, pContext, pSurface);
4448 AssertReturn(pSharedSurface, VERR_INTERNAL_ERROR);
4449
4450 hr = pContext->pDevice->SetTexture(d3dSampler, pSharedSurface->u.pTexture);
4451 }
4452 else
4453#endif
4454 hr = pContext->pDevice->SetTexture(d3dSampler, pSurface->u.pTexture);
4455
4456 AssertMsgReturn(hr == D3D_OK, ("SetTexture failed with %x\n", hr), VERR_INTERNAL_ERROR);
4457
4458 pContext->aSidActiveTextures[idxSampler] = sid;
4459 }
4460 /* Finished; continue with the next one. */
4461 continue;
4462 }
4463
4464 D3DTEXTURESTAGESTATETYPE textureType = D3DTSS_FORCE_DWORD;
4465 D3DSAMPLERSTATETYPE samplerType = D3DSAMP_FORCE_DWORD;
4466 switch (pTextureState[i].name)
4467 {
4468 case SVGA3D_TS_COLOROP: /* SVGA3dTextureCombiner */
4469 textureType = D3DTSS_COLOROP;
4470 val = vmsvga3dTextureCombiner2D3D(pTextureState[i].value);
4471 break;
4472
4473 case SVGA3D_TS_COLORARG0: /* SVGA3dTextureArgData */
4474 textureType = D3DTSS_COLORARG0;
4475 val = vmsvga3dTextureArgData2D3D(pTextureState[i].value);
4476 break;
4477
4478 case SVGA3D_TS_COLORARG1: /* SVGA3dTextureArgData */
4479 textureType = D3DTSS_COLORARG1;
4480 val = vmsvga3dTextureArgData2D3D(pTextureState[i].value);
4481 break;
4482
4483 case SVGA3D_TS_COLORARG2: /* SVGA3dTextureArgData */
4484 textureType = D3DTSS_COLORARG2;
4485 val = vmsvga3dTextureArgData2D3D(pTextureState[i].value);
4486 break;
4487
4488 case SVGA3D_TS_ALPHAOP: /* SVGA3dTextureCombiner */
4489 textureType = D3DTSS_ALPHAOP;
4490 val = vmsvga3dTextureCombiner2D3D(pTextureState[i].value);
4491 break;
4492
4493 case SVGA3D_TS_ALPHAARG0: /* SVGA3dTextureArgData */
4494 textureType = D3DTSS_ALPHAARG0;
4495 val = vmsvga3dTextureArgData2D3D(pTextureState[i].value);
4496 break;
4497
4498 case SVGA3D_TS_ALPHAARG1: /* SVGA3dTextureArgData */
4499 textureType = D3DTSS_ALPHAARG1;
4500 val = vmsvga3dTextureArgData2D3D(pTextureState[i].value);
4501 break;
4502
4503 case SVGA3D_TS_ALPHAARG2: /* SVGA3dTextureArgData */
4504 textureType = D3DTSS_ALPHAARG2;
4505 val = vmsvga3dTextureArgData2D3D(pTextureState[i].value);
4506 break;
4507
4508 case SVGA3D_TS_BUMPENVMAT00: /* float */
4509 textureType = D3DTSS_BUMPENVMAT00;
4510 val = pTextureState[i].value;
4511 break;
4512
4513 case SVGA3D_TS_BUMPENVMAT01: /* float */
4514 textureType = D3DTSS_BUMPENVMAT01;
4515 val = pTextureState[i].value;
4516 break;
4517
4518 case SVGA3D_TS_BUMPENVMAT10: /* float */
4519 textureType = D3DTSS_BUMPENVMAT10;
4520 val = pTextureState[i].value;
4521 break;
4522
4523 case SVGA3D_TS_BUMPENVMAT11: /* float */
4524 textureType = D3DTSS_BUMPENVMAT11;
4525 val = pTextureState[i].value;
4526 break;
4527
4528 case SVGA3D_TS_TEXCOORDINDEX: /* uint32_t */
4529 textureType = D3DTSS_TEXCOORDINDEX;
4530 val = pTextureState[i].value;
4531 break;
4532
4533 case SVGA3D_TS_BUMPENVLSCALE: /* float */
4534 textureType = D3DTSS_BUMPENVLSCALE;
4535 val = pTextureState[i].value;
4536 break;
4537
4538 case SVGA3D_TS_BUMPENVLOFFSET: /* float */
4539 textureType = D3DTSS_BUMPENVLOFFSET;
4540 val = pTextureState[i].value;
4541 break;
4542
4543 case SVGA3D_TS_TEXTURETRANSFORMFLAGS: /* SVGA3dTexTransformFlags */
4544 textureType = D3DTSS_TEXTURETRANSFORMFLAGS;
4545 val = vmsvga3dTextTransformFlags2D3D(pTextureState[i].value);
4546 break;
4547
4548 case SVGA3D_TS_ADDRESSW: /* SVGA3dTextureAddress */
4549 samplerType = D3DSAMP_ADDRESSW;
4550 val = pTextureState[i].value; /* Identical otherwise */
4551 Assert(pTextureState[i].value != SVGA3D_TEX_ADDRESS_EDGE);
4552 break;
4553
4554 case SVGA3D_TS_ADDRESSU: /* SVGA3dTextureAddress */
4555 samplerType = D3DSAMP_ADDRESSU;
4556 val = pTextureState[i].value; /* Identical otherwise */
4557 Assert(pTextureState[i].value != SVGA3D_TEX_ADDRESS_EDGE);
4558 break;
4559
4560 case SVGA3D_TS_ADDRESSV: /* SVGA3dTextureAddress */
4561 samplerType = D3DSAMP_ADDRESSV;
4562 val = pTextureState[i].value; /* Identical otherwise */
4563 Assert(pTextureState[i].value != SVGA3D_TEX_ADDRESS_EDGE);
4564 break;
4565
4566 case SVGA3D_TS_MIPFILTER: /* SVGA3dTextureFilter */
4567 samplerType = D3DSAMP_MIPFILTER;
4568 val = pTextureState[i].value; /* Identical otherwise */
4569 Assert(pTextureState[i].value != SVGA3D_TEX_FILTER_FLATCUBIC);
4570 Assert(pTextureState[i].value != SVGA3D_TEX_FILTER_GAUSSIANCUBIC);
4571 break;
4572
4573 case SVGA3D_TS_MAGFILTER: /* SVGA3dTextureFilter */
4574 samplerType = D3DSAMP_MAGFILTER;
4575 val = pTextureState[i].value; /* Identical otherwise */
4576 Assert(pTextureState[i].value != SVGA3D_TEX_FILTER_FLATCUBIC);
4577 Assert(pTextureState[i].value != SVGA3D_TEX_FILTER_GAUSSIANCUBIC);
4578 break;
4579
4580 case SVGA3D_TS_MINFILTER: /* SVGA3dTextureFilter */
4581 samplerType = D3DSAMP_MINFILTER;
4582 val = pTextureState[i].value; /* Identical otherwise */
4583 Assert(pTextureState[i].value != SVGA3D_TEX_FILTER_FLATCUBIC);
4584 Assert(pTextureState[i].value != SVGA3D_TEX_FILTER_GAUSSIANCUBIC);
4585 break;
4586
4587 case SVGA3D_TS_BORDERCOLOR: /* SVGA3dColor */
4588 samplerType = D3DSAMP_BORDERCOLOR;
4589 val = pTextureState[i].value; /* Identical */
4590 break;
4591
4592 case SVGA3D_TS_TEXTURE_LOD_BIAS: /* float */
4593 samplerType = D3DSAMP_MIPMAPLODBIAS;
4594 val = pTextureState[i].value; /* Identical */
4595 break;
4596
4597 case SVGA3D_TS_TEXTURE_MIPMAP_LEVEL: /* uint32_t */
4598 samplerType = D3DSAMP_MAXMIPLEVEL;
4599 val = pTextureState[i].value; /* Identical?? */
4600 break;
4601
4602 case SVGA3D_TS_TEXTURE_ANISOTROPIC_LEVEL: /* uint32_t */
4603 samplerType = D3DSAMP_MAXANISOTROPY;
4604 val = pTextureState[i].value; /* Identical?? */
4605 break;
4606
4607 case SVGA3D_TS_GAMMA: /* float */
4608 samplerType = D3DSAMP_SRGBTEXTURE;
4609 /* Boolean in D3D */
4610 if (pTextureState[i].floatValue == 1.0f)
4611 val = FALSE;
4612 else
4613 val = TRUE;
4614 break;
4615
4616 /* Internal commands, that don't map directly to the SetTextureStageState API. */
4617 case SVGA3D_TS_TEXCOORDGEN: /* SVGA3dTextureCoordGen */
4618 AssertFailed();
4619 break;
4620
4621 case SVGA3D_TS_MAX: /* shut up MSC */
4622 case SVGA3D_TS_INVALID:
4623 case SVGA3D_TS_BIND_TEXTURE:
4624 AssertFailedBreak();
4625 default: /** @todo Remaining TSs. Avoid MSC warning for now. */
4626 break;
4627 }
4628
4629 const uint32_t currentStage = pTextureState[i].stage;
4630 /* Record the texture state for vm state saving. */
4631 if ( currentStage < RT_ELEMENTS(pContext->state.aTextureStates)
4632 && (unsigned)pTextureState[i].name < RT_ELEMENTS(pContext->state.aTextureStates[0]))
4633 {
4634 pContext->state.aTextureStates[currentStage][pTextureState[i].name] = pTextureState[i];
4635 }
4636
4637 if (textureType != D3DTSS_FORCE_DWORD)
4638 {
4639 if (RT_UNLIKELY(currentStage >= SVGA3D_MAX_TEXTURE_STAGES))
4640 {
4641 AssertMsgFailed(("pTextureState[%d].stage=%#x name=%#x value=%#x\n", i, pTextureState[i].stage, pTextureState[i].name, pTextureState[i].value));
4642 continue;
4643 }
4644
4645 hr = pContext->pDevice->SetTextureStageState(currentStage, textureType, val);
4646 AssertMsg(hr == D3D_OK, ("SetTextureStageState failed with %x\n", hr));
4647 }
4648 else if (samplerType != D3DSAMP_FORCE_DWORD)
4649 {
4650 if (RT_UNLIKELY(currentStage >= SVGA3D_MAX_SAMPLERS))
4651 {
4652 AssertMsgFailed(("pTextureState[%d].stage=%#x name=%#x value=%#x\n", i, pTextureState[i].stage, pTextureState[i].name, pTextureState[i].value));
4653 continue;
4654 }
4655
4656 hr = pContext->pDevice->SetSamplerState(currentStage, samplerType, val);
4657 AssertMsg(hr == D3D_OK, ("SetSamplerState failed with %x\n", hr));
4658 }
4659 else
4660 {
4661 AssertFailed();
4662 }
4663 }
4664
4665 return VINF_SUCCESS;
4666}
4667
4668static DECLCALLBACK(int) vmsvga3dBackSetMaterial(PVGASTATECC pThisCC, uint32_t cid, SVGA3dFace face, SVGA3dMaterial *pMaterial)
4669{
4670 HRESULT hr;
4671 PVMSVGA3DSTATE pState = pThisCC->svga.p3dState;
4672 AssertReturn(pState, VERR_NO_MEMORY);
4673
4674 LogFunc(("cid=%u face %d\n", cid, face));
4675
4676 PVMSVGA3DCONTEXT pContext;
4677 int rc = vmsvga3dContextFromCid(pState, cid, &pContext);
4678 AssertRCReturn(rc, rc);
4679 AssertReturn(pContext->pDevice, VERR_INTERNAL_ERROR);
4680
4681 AssertReturn((unsigned)face < SVGA3D_FACE_MAX, VERR_INVALID_PARAMETER);
4682
4683 /* Save for vm state save/restore. */
4684 pContext->state.aMaterial[face].fValid = true;
4685 pContext->state.aMaterial[face].material = *pMaterial;
4686 pContext->state.u32UpdateFlags |= VMSVGA3D_UPDATE_MATERIAL;
4687
4688 /* @note face not used for D3D9 */
4689 /** @todo ignore everything except SVGA3D_FACE_NONE? */
4690 //Assert(face == SVGA3D_FACE_NONE);
4691 if (face != SVGA3D_FACE_NONE)
4692 Log(("Unsupported face %d!!\n", face));
4693
4694 D3DMATERIAL9 material;
4695 material.Diffuse.r = pMaterial->diffuse[0];
4696 material.Diffuse.g = pMaterial->diffuse[1];
4697 material.Diffuse.b = pMaterial->diffuse[2];
4698 material.Diffuse.a = pMaterial->diffuse[3];
4699 material.Ambient.r = pMaterial->ambient[0];
4700 material.Ambient.g = pMaterial->ambient[1];
4701 material.Ambient.b = pMaterial->ambient[2];
4702 material.Ambient.a = pMaterial->ambient[3];
4703 material.Specular.r = pMaterial->specular[0];
4704 material.Specular.g = pMaterial->specular[1];
4705 material.Specular.b = pMaterial->specular[2];
4706 material.Specular.a = pMaterial->specular[3];
4707 material.Emissive.r = pMaterial->emissive[0];
4708 material.Emissive.g = pMaterial->emissive[1];
4709 material.Emissive.b = pMaterial->emissive[2];
4710 material.Emissive.a = pMaterial->emissive[3];
4711 material.Power = pMaterial->shininess;
4712
4713 hr = pContext->pDevice->SetMaterial(&material);
4714 AssertMsgReturn(hr == D3D_OK, ("SetMaterial failed with %x\n", hr), VERR_INTERNAL_ERROR);
4715
4716 return VINF_SUCCESS;
4717}
4718
4719static DECLCALLBACK(int) vmsvga3dBackSetLightData(PVGASTATECC pThisCC, uint32_t cid, uint32_t index, SVGA3dLightData *pData)
4720{
4721 HRESULT hr;
4722 D3DLIGHT9 light;
4723 PVMSVGA3DCONTEXT pContext;
4724 PVMSVGA3DSTATE pState = pThisCC->svga.p3dState;
4725 AssertReturn(pState, VERR_NO_MEMORY);
4726
4727 Log(("vmsvga3dSetLightData %x index=%d\n", cid, index));
4728 ASSERT_GUEST_RETURN(index < SVGA3D_MAX_LIGHTS, VERR_INVALID_PARAMETER);
4729
4730 int rc = vmsvga3dContextFromCid(pState, cid, &pContext);
4731 AssertRCReturn(rc, rc);
4732 AssertReturn(pContext->pDevice, VERR_INTERNAL_ERROR);
4733
4734 switch (pData->type)
4735 {
4736 case SVGA3D_LIGHTTYPE_POINT:
4737 light.Type = D3DLIGHT_POINT;
4738 break;
4739
4740 case SVGA3D_LIGHTTYPE_SPOT1: /* 1-cone, in degrees */
4741 light.Type = D3DLIGHT_SPOT;
4742 break;
4743
4744 case SVGA3D_LIGHTTYPE_DIRECTIONAL:
4745 light.Type = D3DLIGHT_DIRECTIONAL;
4746 break;
4747
4748 case SVGA3D_LIGHTTYPE_SPOT2: /* 2-cone, in radians */
4749 default:
4750 Log(("Unsupported light type!!\n"));
4751 return VERR_INVALID_PARAMETER;
4752 }
4753
4754 /* Store for vm state save/restore */
4755 pContext->state.aLightData[index].fValidData = true;
4756 pContext->state.aLightData[index].data = *pData;
4757
4758 light.Diffuse.r = pData->diffuse[0];
4759 light.Diffuse.g = pData->diffuse[1];
4760 light.Diffuse.b = pData->diffuse[2];
4761 light.Diffuse.a = pData->diffuse[3];
4762 light.Specular.r = pData->specular[0];
4763 light.Specular.g = pData->specular[1];
4764 light.Specular.b = pData->specular[2];
4765 light.Specular.a = pData->specular[3];
4766 light.Ambient.r = pData->ambient[0];
4767 light.Ambient.g = pData->ambient[1];
4768 light.Ambient.b = pData->ambient[2];
4769 light.Ambient.a = pData->ambient[3];
4770 light.Position.x = pData->position[0];
4771 light.Position.y = pData->position[1];
4772 light.Position.z = pData->position[2]; /* @note 4th position not available in D3D9 */
4773 light.Direction.x = pData->direction[0];
4774 light.Direction.y = pData->direction[1];
4775 light.Direction.z = pData->direction[2]; /* @note 4th position not available in D3D9 */
4776 light.Range = pData->range;
4777 light.Falloff = pData->falloff;
4778 light.Attenuation0 = pData->attenuation0;
4779 light.Attenuation1 = pData->attenuation1;
4780 light.Attenuation2 = pData->attenuation2;
4781 light.Theta = pData->theta;
4782 light.Phi = pData->phi;
4783
4784 hr = pContext->pDevice->SetLight(index, &light);
4785 AssertMsgReturn(hr == D3D_OK, ("SetLight failed with %x\n", hr), VERR_INTERNAL_ERROR);
4786
4787 return VINF_SUCCESS;
4788}
4789
4790static DECLCALLBACK(int) vmsvga3dBackSetLightEnabled(PVGASTATECC pThisCC, uint32_t cid, uint32_t index, uint32_t enabled)
4791{
4792 HRESULT hr;
4793 PVMSVGA3DCONTEXT pContext;
4794 PVMSVGA3DSTATE pState = pThisCC->svga.p3dState;
4795 AssertReturn(pState, VERR_NO_MEMORY);
4796
4797 Log(("vmsvga3dSetLightEnabled %x %d -> %d\n", cid, index, enabled));
4798 AssertReturn(index < SVGA3D_MAX_LIGHTS, VERR_INVALID_PARAMETER);
4799
4800 int rc = vmsvga3dContextFromCid(pState, cid, &pContext);
4801 AssertRCReturn(rc, rc);
4802 AssertReturn(pContext->pDevice, VERR_INTERNAL_ERROR);
4803
4804 /* Store for vm state save/restore */
4805 pContext->state.aLightData[index].fEnabled = !!enabled;
4806
4807 hr = pContext->pDevice->LightEnable(index, (BOOL)enabled);
4808 AssertMsgReturn(hr == D3D_OK, ("LightEnable failed with %x\n", hr), VERR_INTERNAL_ERROR);
4809
4810 return VINF_SUCCESS;
4811}
4812
4813static DECLCALLBACK(int) vmsvga3dBackSetViewPort(PVGASTATECC pThisCC, uint32_t cid, SVGA3dRect *pRect)
4814{
4815 HRESULT hr;
4816 D3DVIEWPORT9 viewPort;
4817 PVMSVGA3DCONTEXT pContext;
4818 PVMSVGA3DSTATE pState = pThisCC->svga.p3dState;
4819 AssertReturn(pState, VERR_NO_MEMORY);
4820
4821 Log(("vmsvga3dSetViewPort %x (%d,%d)(%d,%d)\n", cid, pRect->x, pRect->y, pRect->w, pRect->h));
4822
4823 int rc = vmsvga3dContextFromCid(pState, cid, &pContext);
4824 AssertRCReturn(rc, rc);
4825 AssertReturn(pContext->pDevice, VERR_INTERNAL_ERROR);
4826
4827 /* Save for vm state save/restore. */
4828 pContext->state.RectViewPort = *pRect;
4829 pContext->state.u32UpdateFlags |= VMSVGA3D_UPDATE_VIEWPORT;
4830
4831 hr = pContext->pDevice->GetViewport(&viewPort);
4832 AssertMsgReturn(hr == D3D_OK, ("GetViewport failed with %x\n", hr), VERR_INTERNAL_ERROR);
4833
4834 viewPort.X = pRect->x;
4835 viewPort.Y = pRect->y;
4836 viewPort.Width = pRect->w;
4837 viewPort.Height = pRect->h;
4838 /* viewPort.MinZ & MaxZ are not changed from the current setting. */
4839
4840 hr = pContext->pDevice->SetViewport(&viewPort);
4841 AssertMsgReturn(hr == D3D_OK, ("SetViewport failed with %x\n", hr), VERR_INTERNAL_ERROR);
4842
4843 return VINF_SUCCESS;
4844}
4845
4846static DECLCALLBACK(int) vmsvga3dBackSetClipPlane(PVGASTATECC pThisCC, uint32_t cid, uint32_t index, float plane[4])
4847{
4848 HRESULT hr;
4849 PVMSVGA3DCONTEXT pContext;
4850 PVMSVGA3DSTATE pState = pThisCC->svga.p3dState;
4851 AssertReturn(pState, VERR_NO_MEMORY);
4852
4853 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)));
4854 AssertReturn(index < SVGA3D_NUM_CLIPPLANES, VERR_INVALID_PARAMETER);
4855
4856 int rc = vmsvga3dContextFromCid(pState, cid, &pContext);
4857 AssertRCReturn(rc, rc);
4858 AssertReturn(pContext->pDevice, VERR_INTERNAL_ERROR);
4859
4860 /* Store for vm state save/restore. */
4861 pContext->state.aClipPlane[index].fValid = true;
4862 memcpy(pContext->state.aClipPlane[index].plane, plane, sizeof(pContext->state.aClipPlane[index].plane));
4863
4864 hr = pContext->pDevice->SetClipPlane(index, plane);
4865 AssertMsgReturn(hr == D3D_OK, ("SetClipPlane failed with %x\n", hr), VERR_INTERNAL_ERROR);
4866 return VINF_SUCCESS;
4867}
4868
4869static DECLCALLBACK(int) vmsvga3dBackCommandClear(PVGASTATECC pThisCC, uint32_t cid, SVGA3dClearFlag clearFlag, uint32_t color, float depth,
4870 uint32_t stencil, uint32_t cRects, SVGA3dRect *pRect)
4871{
4872 /* From SVGA3D_BeginClear comments:
4873 *
4874 * Clear is not affected by clipping, depth test, or other
4875 * render state which affects the fragment pipeline.
4876 *
4877 * Therefore this code must ignore the current scissor rect.
4878 */
4879
4880 DWORD clearFlagD3D = 0;
4881 D3DRECT *pRectD3D = NULL;
4882 HRESULT hr;
4883 PVMSVGA3DCONTEXT pContext;
4884 PVMSVGA3DSTATE pState = pThisCC->svga.p3dState;
4885 AssertReturn(pState, VERR_NO_MEMORY);
4886
4887 Log(("vmsvga3dCommandClear %x clearFlag=%x color=%x depth=%d stencil=%x cRects=%d\n", cid, clearFlag, color, (uint32_t)(depth * 100.0), stencil, cRects));
4888
4889 int rc = vmsvga3dContextFromCid(pState, cid, &pContext);
4890 AssertRCReturn(rc, rc);
4891 AssertReturn(pContext->pDevice, VERR_INTERNAL_ERROR);
4892
4893 PVMSVGA3DSURFACE pRT;
4894 rc = vmsvga3dSurfaceFromSid(pState, pContext->state.aRenderTargets[SVGA3D_RT_COLOR0], &pRT);
4895 AssertRCReturn(rc, rc);
4896
4897 if (clearFlag & SVGA3D_CLEAR_COLOR)
4898 clearFlagD3D |= D3DCLEAR_TARGET;
4899 if (clearFlag & SVGA3D_CLEAR_STENCIL)
4900 clearFlagD3D |= D3DCLEAR_STENCIL;
4901 if (clearFlag & SVGA3D_CLEAR_DEPTH)
4902 clearFlagD3D |= D3DCLEAR_ZBUFFER;
4903
4904 if (cRects)
4905 {
4906 pRectD3D = (D3DRECT *)RTMemAlloc(sizeof(D3DRECT) * cRects);
4907 AssertReturn(pRectD3D, VERR_NO_MEMORY);
4908
4909 for (unsigned i=0; i < cRects; i++)
4910 {
4911 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));
4912 pRectD3D[i].x1 = pRect[i].x;
4913 pRectD3D[i].y1 = pRect[i].y;
4914 pRectD3D[i].x2 = pRect[i].x + pRect[i].w; /* exclusive */
4915 pRectD3D[i].y2 = pRect[i].y + pRect[i].h; /* exclusive */
4916 }
4917 }
4918
4919 RECT currentScissorRect;
4920 pContext->pDevice->GetScissorRect(&currentScissorRect);
4921
4922 RECT clearScissorRect;
4923 clearScissorRect.left = 0;
4924 clearScissorRect.top = 0;
4925 clearScissorRect.right = pRT->paMipmapLevels[0].mipmapSize.width;
4926 clearScissorRect.bottom = pRT->paMipmapLevels[0].mipmapSize.height;
4927 pContext->pDevice->SetScissorRect(&clearScissorRect);
4928
4929 hr = pContext->pDevice->Clear(cRects, pRectD3D, clearFlagD3D, (D3DCOLOR)color, depth, stencil);
4930
4931 if (pRectD3D)
4932 RTMemFree(pRectD3D);
4933
4934 pContext->pDevice->SetScissorRect(&currentScissorRect);
4935
4936 AssertMsgReturn(hr == D3D_OK, ("Clear failed with %x\n", hr), VERR_INTERNAL_ERROR);
4937
4938 /* Make sure we can track drawing usage of active render targets. */
4939 for (uint32_t i = 0; i < RT_ELEMENTS(pContext->state.aRenderTargets); ++i)
4940 if (pContext->state.aRenderTargets[i] != SVGA3D_INVALID_ID)
4941 vmsvga3dSurfaceTrackUsageById(pState, pContext, pContext->state.aRenderTargets[i]);
4942
4943 return VINF_SUCCESS;
4944}
4945
4946/* Convert VMWare vertex declaration to its D3D equivalent. */
4947static int vmsvga3dVertexDecl2D3D(const SVGA3dVertexArrayIdentity &identity, D3DVERTEXELEMENT9 *pVertexElement)
4948{
4949 /* usage, method and type are identical; make sure. */
4950 AssertCompile(SVGA3D_DECLTYPE_FLOAT1 == D3DDECLTYPE_FLOAT1);
4951 AssertCompile(SVGA3D_DECLTYPE_FLOAT16_4 == D3DDECLTYPE_FLOAT16_4);
4952 AssertCompile(SVGA3D_DECLMETHOD_DEFAULT == D3DDECLMETHOD_DEFAULT);
4953 AssertCompile(SVGA3D_DECLMETHOD_LOOKUPPRESAMPLED == D3DDECLMETHOD_LOOKUPPRESAMPLED);
4954 AssertCompile(D3DDECLUSAGE_POSITION == SVGA3D_DECLUSAGE_POSITION);
4955 AssertCompile(D3DDECLUSAGE_SAMPLE == SVGA3D_DECLUSAGE_SAMPLE);
4956
4957 pVertexElement->Stream = 0;
4958 pVertexElement->Offset = 0;
4959 pVertexElement->Type = identity.type;
4960 pVertexElement->Method = identity.method;
4961 pVertexElement->Usage = identity.usage;
4962 pVertexElement->UsageIndex = identity.usageIndex;
4963 return VINF_SUCCESS;
4964}
4965
4966/* Convert VMWare primitive type to its D3D equivalent. */
4967static int vmsvga3dPrimitiveType2D3D(SVGA3dPrimitiveType PrimitiveType, D3DPRIMITIVETYPE *pPrimitiveTypeD3D)
4968{
4969 switch (PrimitiveType)
4970 {
4971 case SVGA3D_PRIMITIVE_TRIANGLELIST:
4972 *pPrimitiveTypeD3D = D3DPT_TRIANGLELIST;
4973 break;
4974 case SVGA3D_PRIMITIVE_POINTLIST:
4975 *pPrimitiveTypeD3D = D3DPT_POINTLIST;
4976 break;
4977 case SVGA3D_PRIMITIVE_LINELIST:
4978 *pPrimitiveTypeD3D = D3DPT_LINELIST;
4979 break;
4980 case SVGA3D_PRIMITIVE_LINESTRIP:
4981 *pPrimitiveTypeD3D = D3DPT_LINESTRIP;
4982 break;
4983 case SVGA3D_PRIMITIVE_TRIANGLESTRIP:
4984 *pPrimitiveTypeD3D = D3DPT_TRIANGLESTRIP;
4985 break;
4986 case SVGA3D_PRIMITIVE_TRIANGLEFAN:
4987 *pPrimitiveTypeD3D = D3DPT_TRIANGLEFAN;
4988 break;
4989 default:
4990 return VERR_INVALID_PARAMETER;
4991 }
4992 return VINF_SUCCESS;
4993}
4994
4995
4996static int vmsvga3dDrawPrimitivesSyncVertexBuffer(PVMSVGA3DCONTEXT pContext,
4997 PVMSVGA3DSURFACE pVertexSurface)
4998{
4999 HRESULT hr;
5000 if ( pVertexSurface->u.pSurface
5001 && pVertexSurface->enmD3DResType != VMSVGA3D_D3DRESTYPE_VERTEX_BUFFER)
5002 {
5003 /* The buffer object is not an vertex one. Recreate the D3D resource. */
5004 AssertReturn(pVertexSurface->enmD3DResType == VMSVGA3D_D3DRESTYPE_INDEX_BUFFER, VERR_INVALID_PARAMETER);
5005 D3D_RELEASE(pVertexSurface->u.pIndexBuffer);
5006 pVertexSurface->enmD3DResType = VMSVGA3D_D3DRESTYPE_NONE;
5007
5008 LogFunc(("index -> vertex buffer sid=%u\n", pVertexSurface->id));
5009 }
5010
5011 bool fSync = pVertexSurface->fDirty;
5012 if (!pVertexSurface->u.pVertexBuffer)
5013 {
5014 LogFunc(("Create vertex buffer sid=%u fDirty=%d\n", pVertexSurface->id, pVertexSurface->fDirty));
5015
5016 const DWORD Usage = D3DUSAGE_DYNAMIC | D3DUSAGE_WRITEONLY; /* possible severe performance penalty otherwise (according to d3d debug output */
5017 hr = pContext->pDevice->CreateVertexBuffer(pVertexSurface->paMipmapLevels[0].cbSurface,
5018 Usage,
5019 0, /* non-FVF */
5020 D3DPOOL_DEFAULT,
5021 &pVertexSurface->u.pVertexBuffer,
5022 NULL);
5023 AssertMsgReturn(hr == D3D_OK, ("CreateVertexBuffer failed with %x\n", hr), VERR_INTERNAL_ERROR);
5024
5025 pVertexSurface->enmD3DResType = VMSVGA3D_D3DRESTYPE_VERTEX_BUFFER;
5026 pVertexSurface->idAssociatedContext = pContext->id;
5027 pVertexSurface->surfaceFlags |= SVGA3D_SURFACE_HINT_VERTEXBUFFER;
5028 fSync = true;
5029 }
5030
5031 if (fSync)
5032 {
5033 LogFunc(("sync vertex buffer\n"));
5034 Assert(pVertexSurface->u.pVertexBuffer);
5035
5036 void *pvData;
5037 hr = pVertexSurface->u.pVertexBuffer->Lock(0, 0, &pvData, D3DLOCK_DISCARD);
5038 AssertMsgReturn(hr == D3D_OK, ("Lock vertex failed with %x\n", hr), VERR_INTERNAL_ERROR);
5039
5040 memcpy(pvData, pVertexSurface->paMipmapLevels[0].pSurfaceData, pVertexSurface->paMipmapLevels[0].cbSurface);
5041
5042 hr = pVertexSurface->u.pVertexBuffer->Unlock();
5043 AssertMsgReturn(hr == D3D_OK, ("Unlock vertex failed with %x\n", hr), VERR_INTERNAL_ERROR);
5044 }
5045
5046 return VINF_SUCCESS;
5047}
5048
5049
5050static int vmsvga3dDrawPrimitivesSyncIndexBuffer(PVMSVGA3DCONTEXT pContext,
5051 PVMSVGA3DSURFACE pIndexSurface,
5052 uint32_t indexWidth)
5053{
5054 HRESULT hr;
5055 if ( pIndexSurface->u.pSurface
5056 && pIndexSurface->enmD3DResType != VMSVGA3D_D3DRESTYPE_INDEX_BUFFER)
5057 {
5058 /* The buffer object is not an index one. Must recreate the D3D resource. */
5059 AssertReturn(pIndexSurface->enmD3DResType == VMSVGA3D_D3DRESTYPE_VERTEX_BUFFER, VERR_INVALID_PARAMETER);
5060 D3D_RELEASE(pIndexSurface->u.pVertexBuffer);
5061 pIndexSurface->enmD3DResType = VMSVGA3D_D3DRESTYPE_NONE;
5062
5063 LogFunc(("vertex -> index buffer sid=%u\n", pIndexSurface->id));
5064 }
5065
5066 bool fSync = pIndexSurface->fDirty;
5067 if (!pIndexSurface->u.pIndexBuffer)
5068 {
5069 LogFunc(("Create index buffer fDirty=%d\n", pIndexSurface->fDirty));
5070
5071 const DWORD Usage = D3DUSAGE_DYNAMIC | D3DUSAGE_WRITEONLY; /* possible severe performance penalty otherwise (according to d3d debug output */
5072 const D3DFORMAT Format = (indexWidth == sizeof(uint16_t)) ? D3DFMT_INDEX16 : D3DFMT_INDEX32;
5073 hr = pContext->pDevice->CreateIndexBuffer(pIndexSurface->paMipmapLevels[0].cbSurface,
5074 Usage,
5075 Format,
5076 D3DPOOL_DEFAULT,
5077 &pIndexSurface->u.pIndexBuffer,
5078 NULL);
5079 AssertMsgReturn(hr == D3D_OK, ("CreateIndexBuffer failed with %x\n", hr), VERR_INTERNAL_ERROR);
5080
5081 pIndexSurface->enmD3DResType = VMSVGA3D_D3DRESTYPE_INDEX_BUFFER;
5082 pIndexSurface->idAssociatedContext = pContext->id;
5083 pIndexSurface->surfaceFlags |= SVGA3D_SURFACE_HINT_INDEXBUFFER;
5084 fSync = true;
5085 }
5086
5087 if (fSync)
5088 {
5089 LogFunc(("sync index buffer\n"));
5090 Assert(pIndexSurface->u.pIndexBuffer);
5091
5092 void *pvData;
5093 hr = pIndexSurface->u.pIndexBuffer->Lock(0, 0, &pvData, D3DLOCK_DISCARD);
5094 AssertMsgReturn(hr == D3D_OK, ("Lock index failed with %x\n", hr), VERR_INTERNAL_ERROR);
5095
5096 memcpy(pvData, pIndexSurface->paMipmapLevels[0].pSurfaceData, pIndexSurface->paMipmapLevels[0].cbSurface);
5097
5098 hr = pIndexSurface->u.pIndexBuffer->Unlock();
5099 AssertMsgReturn(hr == D3D_OK, ("Unlock index failed with %x\n", hr), VERR_INTERNAL_ERROR);
5100 }
5101
5102 return VINF_SUCCESS;
5103}
5104
5105static int vmsvga3dDrawPrimitivesProcessVertexDecls(const uint32_t numVertexDecls,
5106 const SVGA3dVertexDecl *pVertexDecl,
5107 const uint32_t idStream,
5108 const uint32_t uVertexMinOffset,
5109 const uint32_t uVertexMaxOffset,
5110 D3DVERTEXELEMENT9 *pVertexElement)
5111{
5112 RT_NOREF(uVertexMaxOffset); /* Logging only. */
5113 Assert(numVertexDecls);
5114
5115 /* Create a vertex declaration array */
5116 for (uint32_t iVertex = 0; iVertex < numVertexDecls; ++iVertex)
5117 {
5118 LogFunc(("vertex %d type=%s (%d) method=%s (%d) usage=%s (%d) usageIndex=%d stride=%d offset=%d (%d min=%d max=%d)\n",
5119 iVertex,
5120 vmsvgaDeclType2String(pVertexDecl[iVertex].identity.type), pVertexDecl[iVertex].identity.type,
5121 vmsvgaDeclMethod2String(pVertexDecl[iVertex].identity.method), pVertexDecl[iVertex].identity.method,
5122 vmsvgaDeclUsage2String(pVertexDecl[iVertex].identity.usage), pVertexDecl[iVertex].identity.usage,
5123 pVertexDecl[iVertex].identity.usageIndex,
5124 pVertexDecl[iVertex].array.stride,
5125 pVertexDecl[iVertex].array.offset - uVertexMinOffset,
5126 pVertexDecl[iVertex].array.offset,
5127 uVertexMinOffset, uVertexMaxOffset));
5128
5129 int rc = vmsvga3dVertexDecl2D3D(pVertexDecl[iVertex].identity, &pVertexElement[iVertex]);
5130 AssertRCReturn(rc, rc);
5131
5132 pVertexElement[iVertex].Stream = idStream;
5133 pVertexElement[iVertex].Offset = pVertexDecl[iVertex].array.offset - uVertexMinOffset;
5134
5135#ifdef LOG_ENABLED
5136 if (pVertexDecl[iVertex].array.stride == 0)
5137 LogFunc(("stride == 0! Can be valid\n"));
5138
5139 if (pVertexElement[iVertex].Offset >= pVertexDecl[0].array.stride)
5140 LogFunc(("WARNING: offset > stride!!\n"));
5141#endif
5142 }
5143
5144 return VINF_SUCCESS;
5145}
5146
5147static DECLCALLBACK(int) vmsvga3dBackDrawPrimitives(PVGASTATECC pThisCC, uint32_t cid, uint32_t numVertexDecls, SVGA3dVertexDecl *pVertexDecl,
5148 uint32_t numRanges, SVGA3dPrimitiveRange *pRange,
5149 uint32_t cVertexDivisor, SVGA3dVertexDivisor *pVertexDivisor)
5150{
5151 static const D3DVERTEXELEMENT9 sVertexEnd = D3DDECL_END();
5152
5153 PVMSVGA3DSTATE pState = pThisCC->svga.p3dState;
5154 AssertReturn(pState, VERR_INTERNAL_ERROR);
5155
5156 PVMSVGA3DCONTEXT pContext;
5157 int rc = vmsvga3dContextFromCid(pState, cid, &pContext);
5158 AssertRCReturn(rc, rc);
5159 AssertReturn(pContext->pDevice, VERR_INTERNAL_ERROR);
5160
5161 HRESULT hr;
5162
5163 /* SVGA driver may use the same surface for both index and vertex data. So we can not clear fDirty flag,
5164 * after updating a vertex buffer for example, because the same surface might be used for index buffer later.
5165 * So keep pointers to all used surfaces in the following two arrays and clear fDirty flag at the end.
5166 */
5167 PVMSVGA3DSURFACE aVertexSurfaces[SVGA3D_MAX_VERTEX_ARRAYS];
5168 PVMSVGA3DSURFACE aIndexSurfaces[SVGA3D_MAX_DRAW_PRIMITIVE_RANGES];
5169 RT_ZERO(aVertexSurfaces);
5170 RT_ZERO(aIndexSurfaces);
5171
5172 LogFunc(("cid=%u numVertexDecls=%d numRanges=%d, cVertexDivisor=%d\n", cid, numVertexDecls, numRanges, cVertexDivisor));
5173
5174 AssertReturn(numVertexDecls && numVertexDecls <= SVGA3D_MAX_VERTEX_ARRAYS, VERR_INVALID_PARAMETER);
5175 AssertReturn(numRanges && numRanges <= SVGA3D_MAX_DRAW_PRIMITIVE_RANGES, VERR_INVALID_PARAMETER);
5176 AssertReturn(!cVertexDivisor || cVertexDivisor == numVertexDecls, VERR_INVALID_PARAMETER);
5177
5178#if 0
5179 /* Dump render target to a bitmap. */
5180 if (pContext->state.aRenderTargets[SVGA3D_RT_COLOR0] != SVGA3D_INVALID_ID)
5181 {
5182 vmsvga3dUpdateHeapBuffersForSurfaces(pThisCC, pContext->state.aRenderTargets[SVGA3D_RT_COLOR0]);
5183 PVMSVGA3DSURFACE pSurface;
5184 int rc2 = vmsvga3dSurfaceFromSid(pState, pContext->state.aRenderTargets[SVGA3D_RT_COLOR0], &pSurface);
5185 if (RT_SUCCESS(rc2))
5186 vmsvga3dInfoSurfaceToBitmap(NULL, pSurface, "bmpd3d", "rt", "-pre");
5187 }
5188 for (int iTex = 0; iTex < SVGA3D_MAX_SAMPLERS_PS; ++iTex)
5189 {
5190 IDirect3DBaseTexture9 *pBaseTexture = 0;
5191 hr = pContext->pDevice->GetTexture(iTex, &pBaseTexture);
5192 if (hr == S_OK && pBaseTexture)
5193 {
5194 D3DSURFACE_DESC surfDesc;
5195 RT_ZERO(surfDesc);
5196
5197 IDirect3DTexture9 *pTexture = (IDirect3DTexture9 *)pBaseTexture;
5198 pTexture->GetLevelDesc(0, &surfDesc);
5199
5200 Log(("Texture[%d]: %dx%d\n",
5201 iTex, surfDesc.Width, surfDesc.Height));
5202 D3D_RELEASE(pBaseTexture);
5203 }
5204 }
5205#endif
5206
5207 /*
5208 * Process all vertex declarations. Each vertex buffer surface is represented by one stream source id.
5209 */
5210 D3DVERTEXELEMENT9 aVertexElements[SVGA3D_MAX_VERTEX_ARRAYS + 1];
5211
5212 uint32_t iCurrentVertex = 0;
5213 uint32_t iCurrentStreamId = 0;
5214 while (iCurrentVertex < numVertexDecls)
5215 {
5216 const uint32_t sidVertex = pVertexDecl[iCurrentVertex].array.surfaceId;
5217 const uint32_t strideVertex = pVertexDecl[iCurrentVertex].array.stride;
5218
5219 PVMSVGA3DSURFACE pVertexSurface;
5220 rc = vmsvga3dSurfaceFromSid(pState, sidVertex, &pVertexSurface);
5221 AssertRCBreak(rc);
5222
5223 rc = vmsvga3dDrawPrimitivesSyncVertexBuffer(pContext, pVertexSurface);
5224 AssertRCBreak(rc);
5225
5226 uint32_t uVertexMinOffset = UINT32_MAX;
5227 uint32_t uVertexMaxOffset = 0;
5228
5229 uint32_t iVertex;
5230 for (iVertex = iCurrentVertex; iVertex < numVertexDecls; ++iVertex)
5231 {
5232 /* Remember, so we can mark it as not dirty later. */
5233 aVertexSurfaces[iVertex] = pVertexSurface;
5234
5235 /* New surface id -> new stream id. */
5236 if (pVertexDecl[iVertex].array.surfaceId != sidVertex)
5237 break;
5238
5239 const uint32_t uVertexOffset = pVertexDecl[iVertex].array.offset;
5240 const uint32_t uNewVertexMinOffset = RT_MIN(uVertexMinOffset, uVertexOffset);
5241 const uint32_t uNewVertexMaxOffset = RT_MAX(uVertexMaxOffset, uVertexOffset);
5242
5243 /* We must put vertex declarations that start at a different element in another stream as d3d only handles offsets < stride. */
5244 if ( uNewVertexMaxOffset - uNewVertexMinOffset >= strideVertex
5245 && strideVertex != 0)
5246 break;
5247
5248 uVertexMinOffset = uNewVertexMinOffset;
5249 uVertexMaxOffset = uNewVertexMaxOffset;
5250 }
5251
5252 rc = vmsvga3dDrawPrimitivesProcessVertexDecls(iVertex - iCurrentVertex,
5253 &pVertexDecl[iCurrentVertex],
5254 iCurrentStreamId,
5255 uVertexMinOffset,
5256 uVertexMaxOffset,
5257 &aVertexElements[iCurrentVertex]);
5258 AssertRCBreak(rc);
5259
5260 LogFunc(("SetStreamSource vertex sid=%u stream %d min offset=%d stride=%d\n",
5261 pVertexSurface->id, iCurrentStreamId, uVertexMinOffset, strideVertex));
5262
5263 hr = pContext->pDevice->SetStreamSource(iCurrentStreamId,
5264 pVertexSurface->u.pVertexBuffer,
5265 uVertexMinOffset,
5266 strideVertex);
5267 AssertMsgBreakStmt(hr == D3D_OK, ("SetStreamSource failed with %x\n", hr), rc = VERR_INTERNAL_ERROR);
5268
5269 if (cVertexDivisor)
5270 {
5271 LogFunc(("SetStreamSourceFreq[%d]=%x\n", iCurrentStreamId, pVertexDivisor[iCurrentVertex].value));
5272 HRESULT hr2 = pContext->pDevice->SetStreamSourceFreq(iCurrentStreamId,
5273 pVertexDivisor[iCurrentVertex].value);
5274 Assert(SUCCEEDED(hr2)); RT_NOREF(hr2);
5275 }
5276
5277 iCurrentVertex = iVertex;
5278 ++iCurrentStreamId;
5279 }
5280
5281 /* iCurrentStreamId is equal to the total number of streams and the value is used for cleanup at the function end. */
5282
5283 AssertRCReturn(rc, rc);
5284
5285 /* Mark the end. */
5286 memcpy(&aVertexElements[numVertexDecls], &sVertexEnd, sizeof(sVertexEnd));
5287
5288 /* Check if this context already has the same vertex declaration. */
5289 if ( pContext->d3dState.pVertexDecl
5290 && pContext->d3dState.cVertexElements == numVertexDecls + 1
5291 && memcmp(pContext->d3dState.aVertexElements,
5292 aVertexElements,
5293 pContext->d3dState.cVertexElements * sizeof(aVertexElements[0])) == 0)
5294 {
5295 /* Same. */
5296 }
5297 else
5298 {
5299 /* Create and set the vertex declaration. */
5300 IDirect3DVertexDeclaration9 *pVertexDecl0;
5301 hr = pContext->pDevice->CreateVertexDeclaration(&aVertexElements[0], &pVertexDecl0);
5302 AssertMsgReturn(hr == D3D_OK, ("CreateVertexDeclaration failed with %x\n", hr), VERR_INTERNAL_ERROR);
5303
5304 hr = pContext->pDevice->SetVertexDeclaration(pVertexDecl0);
5305 AssertMsgReturnStmt(hr == D3D_OK, ("SetVertexDeclaration failed with %x\n", hr),
5306 D3D_RELEASE(pVertexDecl0),
5307 VERR_INTERNAL_ERROR);
5308
5309 /* The new vertex declaration has been successfully set. Delete the old one. */
5310 D3D_RELEASE(pContext->d3dState.pVertexDecl);
5311
5312 /* Remember the new vertext declaration. */
5313 pContext->d3dState.pVertexDecl = pVertexDecl0;
5314 pContext->d3dState.cVertexElements = numVertexDecls + 1;
5315 memcpy(pContext->d3dState.aVertexElements,
5316 aVertexElements,
5317 pContext->d3dState.cVertexElements * sizeof(aVertexElements[0]));
5318 }
5319
5320 /* Begin a scene before rendering anything. */
5321 hr = pContext->pDevice->BeginScene();
5322 AssertMsgReturn(hr == D3D_OK, ("BeginScene failed with %x\n", hr), VERR_INTERNAL_ERROR);
5323
5324 /* Now draw the primitives. */
5325 for (uint32_t iPrimitive = 0; iPrimitive < numRanges; ++iPrimitive)
5326 {
5327 Log(("Primitive %d: type %s\n", iPrimitive, vmsvga3dPrimitiveType2String(pRange[iPrimitive].primType)));
5328
5329 const uint32_t sidIndex = pRange[iPrimitive].indexArray.surfaceId;
5330 PVMSVGA3DSURFACE pIndexSurface = NULL;
5331
5332 D3DPRIMITIVETYPE PrimitiveTypeD3D;
5333 rc = vmsvga3dPrimitiveType2D3D(pRange[iPrimitive].primType, &PrimitiveTypeD3D);
5334 AssertRCBreak(rc);
5335
5336 /* Triangle strips or fans with just one primitive don't make much sense and are identical to triangle lists.
5337 * Workaround for NVidia driver crash when encountering some of these.
5338 */
5339 if ( pRange[iPrimitive].primitiveCount == 1
5340 && ( PrimitiveTypeD3D == D3DPT_TRIANGLESTRIP
5341 || PrimitiveTypeD3D == D3DPT_TRIANGLEFAN))
5342 PrimitiveTypeD3D = D3DPT_TRIANGLELIST;
5343
5344 if (sidIndex != SVGA3D_INVALID_ID)
5345 {
5346 AssertMsg(pRange[iPrimitive].indexWidth == sizeof(uint32_t) || pRange[iPrimitive].indexWidth == sizeof(uint16_t),
5347 ("Unsupported primitive width %d\n", pRange[iPrimitive].indexWidth));
5348
5349 rc = vmsvga3dSurfaceFromSid(pState, sidIndex, &pIndexSurface);
5350 AssertRCBreak(rc);
5351
5352 aIndexSurfaces[iPrimitive] = pIndexSurface;
5353
5354 Log(("vmsvga3dDrawPrimitives: index sid=%u\n", sidIndex));
5355
5356 rc = vmsvga3dDrawPrimitivesSyncIndexBuffer(pContext, pIndexSurface, pRange[iPrimitive].indexWidth);
5357 AssertRCBreak(rc);
5358
5359 hr = pContext->pDevice->SetIndices(pIndexSurface->u.pIndexBuffer);
5360 AssertMsg(hr == D3D_OK, ("SetIndices vertex failed with %x\n", hr));
5361 }
5362 else
5363 {
5364 hr = pContext->pDevice->SetIndices(NULL);
5365 AssertMsg(hr == D3D_OK, ("SetIndices vertex (NULL) failed with %x\n", hr));
5366 }
5367
5368 const uint32_t strideVertex = pVertexDecl[0].array.stride;
5369
5370 if (!pIndexSurface)
5371 {
5372 /* Render without an index buffer */
5373 Log(("DrawPrimitive %x primitivecount=%d index index bias=%d stride=%d\n",
5374 PrimitiveTypeD3D, pRange[iPrimitive].primitiveCount, pRange[iPrimitive].indexBias, strideVertex));
5375
5376 hr = pContext->pDevice->DrawPrimitive(PrimitiveTypeD3D,
5377 pRange[iPrimitive].indexBias,
5378 pRange[iPrimitive].primitiveCount);
5379 AssertMsgBreakStmt(hr == D3D_OK, ("DrawPrimitive failed with %x\n", hr), rc = VERR_INTERNAL_ERROR);
5380 }
5381 else
5382 {
5383 UINT numVertices;
5384 if (pVertexDecl[0].rangeHint.last)
5385 {
5386 /* Both SVGA3dArrayRangeHint definition and the SVGA driver code imply that 'last' is exclusive,
5387 * hence compute the difference.
5388 */
5389 numVertices = pVertexDecl[0].rangeHint.last - pVertexDecl[0].rangeHint.first;
5390 }
5391 else
5392 {
5393 /* Range hint is not provided. */
5394 PVMSVGA3DSURFACE pVertexSurface = aVertexSurfaces[0];
5395 numVertices = pVertexSurface->paMipmapLevels[0].cbSurface / strideVertex
5396 - pVertexDecl[0].array.offset / strideVertex
5397 - pVertexDecl[0].rangeHint.first
5398 - pRange[iPrimitive].indexBias;
5399 }
5400
5401 /* Render with an index buffer */
5402 Log(("DrawIndexedPrimitive %x startindex=%d (indexArray.offset 0x%x) range [%d:%d) numVertices=%d, primitivecount=%d index format=%s index bias=%d stride=%d\n",
5403 PrimitiveTypeD3D, pRange[iPrimitive].indexArray.offset / pRange[iPrimitive].indexWidth, pRange[iPrimitive].indexArray.offset,
5404 pVertexDecl[0].rangeHint.first, pVertexDecl[0].rangeHint.last, numVertices, pRange[iPrimitive].primitiveCount,
5405 (pRange[iPrimitive].indexWidth == sizeof(uint16_t)) ? "D3DFMT_INDEX16" : "D3DFMT_INDEX32",
5406 pRange[iPrimitive].indexBias, strideVertex));
5407
5408 hr = pContext->pDevice->DrawIndexedPrimitive(PrimitiveTypeD3D,
5409 pRange[iPrimitive].indexBias, /* BaseVertexIndex */
5410 0, /* MinVertexIndex */
5411 numVertices,
5412 pRange[iPrimitive].indexArray.offset / pRange[iPrimitive].indexWidth, /* StartIndex */
5413 pRange[iPrimitive].primitiveCount);
5414 AssertMsgBreakStmt(hr == D3D_OK, ("DrawIndexedPrimitive failed with %x\n", hr), rc = VERR_INTERNAL_ERROR);
5415 }
5416 }
5417
5418 /* End the scene and do some cleanup regardless of the rc. */
5419 hr = pContext->pDevice->EndScene();
5420 AssertMsgReturn(hr == D3D_OK, ("EndScene failed with %x\n", hr), VERR_INTERNAL_ERROR);
5421
5422 /* Cleanup. */
5423 uint32_t i;
5424 /* Clear all streams, because they are set at the beginning of this function anyway.
5425 * Now the vertex buffers can be safely deleted/recreated if necessary.
5426 */
5427 for (i = 0; i < iCurrentStreamId; ++i)
5428 {
5429 LogFunc(("clear stream %d\n", i));
5430 HRESULT hr2 = pContext->pDevice->SetStreamSource(i, NULL, 0, 0);
5431 AssertMsg(hr2 == D3D_OK, ("SetStreamSource(%d, NULL) failed with %x\n", i, hr2)); RT_NOREF(hr2);
5432 }
5433
5434 if (cVertexDivisor)
5435 {
5436 /* "When you are finished rendering the instance data, be sure to reset the vertex stream frequency back..." */
5437 for (i = 0; i < iCurrentStreamId; ++i)
5438 {
5439 LogFunc(("reset stream freq %d\n", i));
5440 HRESULT hr2 = pContext->pDevice->SetStreamSourceFreq(i, 1);
5441 AssertMsg(hr2 == D3D_OK, ("SetStreamSourceFreq(%d, 1) failed with %x\n", i, hr2)); RT_NOREF(hr2);
5442 }
5443 }
5444
5445 if (RT_SUCCESS(rc))
5446 {
5447 for (i = 0; i < numVertexDecls; ++i)
5448 {
5449 if (aVertexSurfaces[i])
5450 {
5451 aVertexSurfaces[i]->paMipmapLevels[0].fDirty = false;
5452 aVertexSurfaces[i]->fDirty = false;
5453 }
5454 }
5455
5456 for (i = 0; i < numRanges; ++i)
5457 {
5458 if (aIndexSurfaces[i])
5459 {
5460 aIndexSurfaces[i]->paMipmapLevels[0].fDirty = false;
5461 aIndexSurfaces[i]->fDirty = false;
5462 }
5463 }
5464
5465 /* Make sure we can track drawing usage of active render targets and textures. */
5466 vmsvga3dContextTrackUsage(pThisCC, pContext);
5467 }
5468
5469#if 0
5470 /* Dump render target to a bitmap. */
5471 if (pContext->state.aRenderTargets[SVGA3D_RT_COLOR0] != SVGA3D_INVALID_ID)
5472 {
5473 vmsvga3dUpdateHeapBuffersForSurfaces(pThisCC, pContext->state.aRenderTargets[SVGA3D_RT_COLOR0]);
5474 PVMSVGA3DSURFACE pSurface;
5475 int rc2 = vmsvga3dSurfaceFromSid(pState, pContext->state.aRenderTargets[SVGA3D_RT_COLOR0], &pSurface);
5476 if (RT_SUCCESS(rc2))
5477 vmsvga3dInfoSurfaceToBitmap(NULL, pSurface, "bmpd3d", "rt", "-post");
5478# if 0
5479 for (i = 0; i < SVGA3D_MAX_SAMPLERS_PS; ++i)
5480 {
5481 if (pContext->aSidActiveTextures[i] != SVGA3D_INVALID_ID)
5482 {
5483 char szSuffix[32];
5484 RTStrPrintf2(szSuffix, sizeof(szSuffix), "-tx%d", i);
5485 vmsvga3dUpdateHeapBuffersForSurfaces(pThisCC, pContext->aSidActiveTextures[i]);
5486 rc2 = vmsvga3dSurfaceFromSid(pState, pContext->aSidActiveTextures[i], &pSurface);
5487 if (RT_SUCCESS(rc2))
5488 vmsvga3dInfoSurfaceToBitmap(NULL, pSurface, "bmpd3d", "rt", szSuffix);
5489 }
5490 }
5491# endif
5492 }
5493#endif
5494
5495 return rc;
5496}
5497
5498
5499static DECLCALLBACK(int) vmsvga3dBackSetScissorRect(PVGASTATECC pThisCC, uint32_t cid, SVGA3dRect *pRect)
5500{
5501 HRESULT hr;
5502 RECT rect;
5503 PVMSVGA3DSTATE pState = pThisCC->svga.p3dState;
5504 AssertReturn(pState, VERR_NO_MEMORY);
5505
5506 Log(("vmsvga3dSetScissorRect %x (%d,%d)(%d,%d)\n", cid, pRect->x, pRect->y, pRect->w, pRect->h));
5507
5508 PVMSVGA3DCONTEXT pContext;
5509 int rc = vmsvga3dContextFromCid(pState, cid, &pContext);
5510 AssertRCReturn(rc, rc);
5511 AssertReturn(pContext->pDevice, VERR_INTERNAL_ERROR);
5512
5513 /* Store for vm state save/restore. */
5514 pContext->state.u32UpdateFlags |= VMSVGA3D_UPDATE_SCISSORRECT;
5515 pContext->state.RectScissor = *pRect;
5516
5517 rect.left = pRect->x;
5518 rect.top = pRect->y;
5519 rect.right = rect.left + pRect->w; /* exclusive */
5520 rect.bottom = rect.top + pRect->h; /* exclusive */
5521
5522 hr = pContext->pDevice->SetScissorRect(&rect);
5523 AssertMsgReturn(hr == D3D_OK, ("vmsvga3dSetScissorRect: SetScissorRect failed with %x\n", hr), VERR_INTERNAL_ERROR);
5524
5525 return VINF_SUCCESS;
5526}
5527
5528
5529static DECLCALLBACK(int) vmsvga3dBackShaderDefine(PVGASTATECC pThisCC, uint32_t cid, uint32_t shid, SVGA3dShaderType type,
5530 uint32_t cbData, uint32_t *pShaderData)
5531{
5532 HRESULT hr;
5533 PVMSVGA3DSHADER pShader;
5534 PVMSVGA3DSTATE pState = pThisCC->svga.p3dState;
5535 AssertReturn(pState, VERR_NO_MEMORY);
5536
5537 Log(("vmsvga3dShaderDefine %x shid=%d type=%s cbData=%d\n", cid, shid, (type == SVGA3D_SHADERTYPE_VS) ? "VERTEX" : "PIXEL", cbData));
5538
5539 PVMSVGA3DCONTEXT pContext;
5540 int rc = vmsvga3dContextFromCid(pState, cid, &pContext);
5541 AssertRCReturn(rc, rc);
5542 AssertReturn(pContext->pDevice, VERR_INTERNAL_ERROR);
5543
5544 AssertReturn(shid < SVGA3D_MAX_SHADER_IDS, VERR_INVALID_PARAMETER);
5545
5546 rc = vmsvga3dShaderParse(type, cbData, pShaderData);
5547 if (RT_FAILURE(rc))
5548 {
5549 AssertRC(rc);
5550 vmsvga3dShaderLogRel("Failed to parse", type, cbData, pShaderData);
5551 return rc;
5552 }
5553
5554 if (type == SVGA3D_SHADERTYPE_VS)
5555 {
5556 if (shid >= pContext->cVertexShaders)
5557 {
5558 void *pvNew = RTMemRealloc(pContext->paVertexShader, sizeof(VMSVGA3DSHADER) * (shid + 1));
5559 AssertReturn(pvNew, VERR_NO_MEMORY);
5560 pContext->paVertexShader = (PVMSVGA3DSHADER)pvNew;
5561 memset(&pContext->paVertexShader[pContext->cVertexShaders], 0, sizeof(VMSVGA3DSHADER) * (shid + 1 - pContext->cVertexShaders));
5562 for (uint32_t i = pContext->cVertexShaders; i < shid + 1; i++)
5563 pContext->paVertexShader[i].id = SVGA3D_INVALID_ID;
5564 pContext->cVertexShaders = shid + 1;
5565 }
5566 /* If one already exists with this id, then destroy it now. */
5567 if (pContext->paVertexShader[shid].id != SVGA3D_INVALID_ID)
5568 vmsvga3dBackShaderDestroy(pThisCC, cid, shid, pContext->paVertexShader[shid].type);
5569
5570 pShader = &pContext->paVertexShader[shid];
5571 }
5572 else
5573 {
5574 Assert(type == SVGA3D_SHADERTYPE_PS);
5575 if (shid >= pContext->cPixelShaders)
5576 {
5577 void *pvNew = RTMemRealloc(pContext->paPixelShader, sizeof(VMSVGA3DSHADER) * (shid + 1));
5578 AssertReturn(pvNew, VERR_NO_MEMORY);
5579 pContext->paPixelShader = (PVMSVGA3DSHADER)pvNew;
5580 memset(&pContext->paPixelShader[pContext->cPixelShaders], 0, sizeof(VMSVGA3DSHADER) * (shid + 1 - pContext->cPixelShaders));
5581 for (uint32_t i = pContext->cPixelShaders; i < shid + 1; i++)
5582 pContext->paPixelShader[i].id = SVGA3D_INVALID_ID;
5583 pContext->cPixelShaders = shid + 1;
5584 }
5585 /* If one already exists with this id, then destroy it now. */
5586 if (pContext->paPixelShader[shid].id != SVGA3D_INVALID_ID)
5587 vmsvga3dBackShaderDestroy(pThisCC, cid, shid, pContext->paPixelShader[shid].type);
5588
5589 pShader = &pContext->paPixelShader[shid];
5590 }
5591
5592 memset(pShader, 0, sizeof(*pShader));
5593 pShader->id = shid;
5594 pShader->cid = cid;
5595 pShader->type = type;
5596 pShader->cbData = cbData;
5597 pShader->pShaderProgram = RTMemAllocZ(cbData);
5598 AssertReturn(pShader->pShaderProgram, VERR_NO_MEMORY);
5599 memcpy(pShader->pShaderProgram, pShaderData, cbData);
5600
5601#ifdef DUMP_SHADER_DISASSEMBLY
5602 LPD3DXBUFFER pDisassembly;
5603 hr = D3DXDisassembleShader((const DWORD *)pShaderData, FALSE, NULL, &pDisassembly);
5604 if (hr == D3D_OK)
5605 {
5606 Log(("Shader disassembly:\n%s\n", pDisassembly->GetBufferPointer()));
5607 D3D_RELEASE(pDisassembly);
5608 }
5609#endif
5610
5611 switch (type)
5612 {
5613 case SVGA3D_SHADERTYPE_VS:
5614 hr = pContext->pDevice->CreateVertexShader((const DWORD *)pShaderData, &pShader->u.pVertexShader);
5615 break;
5616
5617 case SVGA3D_SHADERTYPE_PS:
5618 hr = pContext->pDevice->CreatePixelShader((const DWORD *)pShaderData, &pShader->u.pPixelShader);
5619 break;
5620
5621 default:
5622 AssertFailedReturn(VERR_INVALID_PARAMETER);
5623 }
5624 if (hr != D3D_OK)
5625 {
5626 /* Dump the shader code. */
5627 vmsvga3dShaderLogRel("Failed to create", type, cbData, pShaderData);
5628
5629 RTMemFree(pShader->pShaderProgram);
5630 memset(pShader, 0, sizeof(*pShader));
5631 pShader->id = SVGA3D_INVALID_ID;
5632 }
5633
5634 AssertMsgReturn(hr == D3D_OK, ("vmsvga3dShaderDefine: CreateVertex/PixelShader failed with %x\n", hr), VERR_INTERNAL_ERROR);
5635 return VINF_SUCCESS;
5636}
5637
5638static DECLCALLBACK(int) vmsvga3dBackShaderDestroy(PVGASTATECC pThisCC, uint32_t cid, uint32_t shid, SVGA3dShaderType type)
5639{
5640 PVMSVGA3DSTATE pState = pThisCC->svga.p3dState;
5641 AssertReturn(pState, VERR_NO_MEMORY);
5642 PVMSVGA3DSHADER pShader = NULL;
5643
5644 Log(("vmsvga3dShaderDestroy %x shid=%d type=%s\n", cid, shid, (type == SVGA3D_SHADERTYPE_VS) ? "VERTEX" : "PIXEL"));
5645
5646 PVMSVGA3DCONTEXT pContext;
5647 int rc = vmsvga3dContextFromCid(pState, cid, &pContext);
5648 AssertRCReturn(rc, rc);
5649
5650 if (type == SVGA3D_SHADERTYPE_VS)
5651 {
5652 if ( shid < pContext->cVertexShaders
5653 && pContext->paVertexShader[shid].id == shid)
5654 {
5655 pShader = &pContext->paVertexShader[shid];
5656 D3D_RELEASE(pShader->u.pVertexShader);
5657 }
5658 }
5659 else
5660 {
5661 Assert(type == SVGA3D_SHADERTYPE_PS);
5662 if ( shid < pContext->cPixelShaders
5663 && pContext->paPixelShader[shid].id == shid)
5664 {
5665 pShader = &pContext->paPixelShader[shid];
5666 D3D_RELEASE(pShader->u.pPixelShader);
5667 }
5668 }
5669
5670 if (pShader)
5671 {
5672 if (pShader->pShaderProgram)
5673 RTMemFree(pShader->pShaderProgram);
5674
5675 memset(pShader, 0, sizeof(*pShader));
5676 pShader->id = SVGA3D_INVALID_ID;
5677 }
5678 else
5679 AssertFailedReturn(VERR_INVALID_PARAMETER);
5680
5681 return VINF_SUCCESS;
5682}
5683
5684static DECLCALLBACK(int) vmsvga3dBackShaderSet(PVGASTATECC pThisCC, PVMSVGA3DCONTEXT pContext, uint32_t cid, SVGA3dShaderType type, uint32_t shid)
5685{
5686 PVMSVGA3DSTATE pState = pThisCC->svga.p3dState;
5687 AssertReturn(pState, VERR_NO_MEMORY);
5688 HRESULT hr;
5689
5690 Log(("vmsvga3dShaderSet %x type=%s shid=%d\n", cid, (type == SVGA3D_SHADERTYPE_VS) ? "VERTEX" : "PIXEL", shid));
5691
5692 NOREF(pContext);
5693 int rc = vmsvga3dContextFromCid(pState, cid, &pContext);
5694 AssertRCReturn(rc, rc);
5695 AssertReturn(pContext->pDevice, VERR_INTERNAL_ERROR);
5696
5697 if (type == SVGA3D_SHADERTYPE_VS)
5698 {
5699 /* Save for vm state save/restore. */
5700 pContext->state.shidVertex = shid;
5701 pContext->state.u32UpdateFlags |= VMSVGA3D_UPDATE_VERTEXSHADER;
5702
5703 if ( shid < pContext->cVertexShaders
5704 && pContext->paVertexShader[shid].id == shid)
5705 {
5706 PVMSVGA3DSHADER pShader = &pContext->paVertexShader[shid];
5707 Assert(type == pShader->type);
5708
5709 hr = pContext->pDevice->SetVertexShader(pShader->u.pVertexShader);
5710 AssertMsgReturn(hr == D3D_OK, ("vmsvga3dShaderSet: SetVertex/PixelShader failed with %x\n", hr), VERR_INTERNAL_ERROR);
5711 }
5712 else
5713 if (shid == SVGA_ID_INVALID)
5714 {
5715 /* Unselect shader. */
5716 hr = pContext->pDevice->SetVertexShader(NULL);
5717 AssertMsgReturn(hr == D3D_OK, ("vmsvga3dShaderSet: SetVertex/PixelShader failed with %x\n", hr), VERR_INTERNAL_ERROR);
5718 }
5719 else
5720 AssertFailedReturn(VERR_INVALID_PARAMETER);
5721 }
5722 else
5723 {
5724 /* Save for vm state save/restore. */
5725 pContext->state.shidPixel = shid;
5726 pContext->state.u32UpdateFlags |= VMSVGA3D_UPDATE_PIXELSHADER;
5727
5728 Assert(type == SVGA3D_SHADERTYPE_PS);
5729 if ( shid < pContext->cPixelShaders
5730 && pContext->paPixelShader[shid].id == shid)
5731 {
5732 PVMSVGA3DSHADER pShader = &pContext->paPixelShader[shid];
5733 Assert(type == pShader->type);
5734
5735 hr = pContext->pDevice->SetPixelShader(pShader->u.pPixelShader);
5736 AssertMsgReturn(hr == D3D_OK, ("vmsvga3dShaderSet: SetVertex/PixelShader failed with %x\n", hr), VERR_INTERNAL_ERROR);
5737 }
5738 else
5739 if (shid == SVGA_ID_INVALID)
5740 {
5741 /* Unselect shader. */
5742 hr = pContext->pDevice->SetPixelShader(NULL);
5743 AssertMsgReturn(hr == D3D_OK, ("vmsvga3dShaderSet: SetVertex/PixelShader failed with %x\n", hr), VERR_INTERNAL_ERROR);
5744 }
5745 else
5746 AssertFailedReturn(VERR_INVALID_PARAMETER);
5747 }
5748
5749 return VINF_SUCCESS;
5750}
5751
5752static DECLCALLBACK(int) vmsvga3dBackShaderSetConst(PVGASTATECC pThisCC, uint32_t cid, uint32_t reg, SVGA3dShaderType type,
5753 SVGA3dShaderConstType ctype, uint32_t cRegisters, uint32_t *pValues)
5754{
5755 HRESULT hr;
5756 PVMSVGA3DSTATE pState = pThisCC->svga.p3dState;
5757 AssertReturn(pState, VERR_NO_MEMORY);
5758
5759 Log(("vmsvga3dShaderSetConst %x reg=%x type=%s ctype=%x\n", cid, reg, (type == SVGA3D_SHADERTYPE_VS) ? "VERTEX" : "PIXEL", ctype));
5760
5761 PVMSVGA3DCONTEXT pContext;
5762 int rc = vmsvga3dContextFromCid(pState, cid, &pContext);
5763 AssertRCReturn(rc, rc);
5764 AssertReturn(pContext->pDevice, VERR_INTERNAL_ERROR);
5765
5766 for (uint32_t i = 0; i < cRegisters; i++)
5767 {
5768#ifdef LOG_ENABLED
5769 switch (ctype)
5770 {
5771 case SVGA3D_CONST_TYPE_FLOAT:
5772 {
5773 float *pValuesF = (float *)pValues;
5774 Log(("ConstantF %d: value=" FLOAT_FMT_STR ", " FLOAT_FMT_STR ", " FLOAT_FMT_STR ", " FLOAT_FMT_STR "\n",
5775 reg + i, FLOAT_FMT_ARGS(pValuesF[i*4 + 0]), FLOAT_FMT_ARGS(pValuesF[i*4 + 1]), FLOAT_FMT_ARGS(pValuesF[i*4 + 2]), FLOAT_FMT_ARGS(pValuesF[i*4 + 3])));
5776 break;
5777 }
5778
5779 case SVGA3D_CONST_TYPE_INT:
5780 Log(("ConstantI %d: value=%d, %d, %d, %d\n", reg + i, pValues[i*4 + 0], pValues[i*4 + 1], pValues[i*4 + 2], pValues[i*4 + 3]));
5781 break;
5782
5783 case SVGA3D_CONST_TYPE_BOOL:
5784 Log(("ConstantB %d: value=%d, %d, %d, %d\n", reg + i, pValues[i*4 + 0], pValues[i*4 + 1], pValues[i*4 + 2], pValues[i*4 + 3]));
5785 break;
5786
5787 default:
5788 AssertFailedReturn(VERR_INVALID_PARAMETER);
5789 }
5790#endif
5791 vmsvga3dSaveShaderConst(pContext, reg + i, type, ctype, pValues[i*4 + 0], pValues[i*4 + 1], pValues[i*4 + 2], pValues[i*4 + 3]);
5792 }
5793
5794 switch (type)
5795 {
5796 case SVGA3D_SHADERTYPE_VS:
5797 switch (ctype)
5798 {
5799 case SVGA3D_CONST_TYPE_FLOAT:
5800 hr = pContext->pDevice->SetVertexShaderConstantF(reg, (const float *)pValues, cRegisters);
5801 break;
5802
5803 case SVGA3D_CONST_TYPE_INT:
5804 hr = pContext->pDevice->SetVertexShaderConstantI(reg, (const int *)pValues, cRegisters);
5805 break;
5806
5807 case SVGA3D_CONST_TYPE_BOOL:
5808 hr = pContext->pDevice->SetVertexShaderConstantB(reg, (const BOOL *)pValues, cRegisters);
5809 break;
5810
5811 default:
5812 AssertFailedReturn(VERR_INVALID_PARAMETER);
5813 }
5814 AssertMsgReturn(hr == D3D_OK, ("vmsvga3dShaderSetConst: SetVertexShader failed with %x\n", hr), VERR_INTERNAL_ERROR);
5815 break;
5816
5817 case SVGA3D_SHADERTYPE_PS:
5818 switch (ctype)
5819 {
5820 case SVGA3D_CONST_TYPE_FLOAT:
5821 {
5822 hr = pContext->pDevice->SetPixelShaderConstantF(reg, (const float *)pValues, cRegisters);
5823 break;
5824 }
5825
5826 case SVGA3D_CONST_TYPE_INT:
5827 hr = pContext->pDevice->SetPixelShaderConstantI(reg, (const int *)pValues, cRegisters);
5828 break;
5829
5830 case SVGA3D_CONST_TYPE_BOOL:
5831 hr = pContext->pDevice->SetPixelShaderConstantB(reg, (const BOOL *)pValues, cRegisters);
5832 break;
5833
5834 default:
5835 AssertFailedReturn(VERR_INVALID_PARAMETER);
5836 }
5837 AssertMsgReturn(hr == D3D_OK, ("vmsvga3dShaderSetConst: SetPixelShader failed with %x\n", hr), VERR_INTERNAL_ERROR);
5838 break;
5839
5840 default:
5841 AssertFailedReturn(VERR_INVALID_PARAMETER);
5842 }
5843 return VINF_SUCCESS;
5844}
5845
5846static DECLCALLBACK(int) vmsvga3dBackOcclusionQueryCreate(PVGASTATECC pThisCC, PVMSVGA3DCONTEXT pContext)
5847{
5848 RT_NOREF(pThisCC);
5849 HRESULT hr = pContext->pDevice->CreateQuery(D3DQUERYTYPE_OCCLUSION, &pContext->occlusion.pQuery);
5850 AssertMsgReturn(hr == D3D_OK, ("CreateQuery(D3DQUERYTYPE_OCCLUSION) failed with %x\n", hr), VERR_INTERNAL_ERROR);
5851 return VINF_SUCCESS;
5852}
5853
5854static DECLCALLBACK(int) vmsvga3dBackOcclusionQueryDelete(PVGASTATECC pThisCC, PVMSVGA3DCONTEXT pContext)
5855{
5856 RT_NOREF(pThisCC);
5857 D3D_RELEASE(pContext->occlusion.pQuery);
5858 return VINF_SUCCESS;
5859}
5860
5861static DECLCALLBACK(int) vmsvga3dBackOcclusionQueryBegin(PVGASTATECC pThisCC, PVMSVGA3DCONTEXT pContext)
5862{
5863 RT_NOREF(pThisCC);
5864 HRESULT hr = pContext->occlusion.pQuery->Issue(D3DISSUE_BEGIN);
5865 AssertMsgReturnStmt(hr == D3D_OK, ("D3DISSUE_BEGIN(D3DQUERYTYPE_OCCLUSION) failed with %x\n", hr),
5866 D3D_RELEASE(pContext->occlusion.pQuery), VERR_INTERNAL_ERROR);
5867 return VINF_SUCCESS;
5868}
5869
5870static DECLCALLBACK(int) vmsvga3dBackOcclusionQueryEnd(PVGASTATECC pThisCC, PVMSVGA3DCONTEXT pContext)
5871{
5872 RT_NOREF(pThisCC);
5873 HRESULT hr = pContext->occlusion.pQuery->Issue(D3DISSUE_END);
5874 AssertMsgReturnStmt(hr == D3D_OK, ("D3DISSUE_END(D3DQUERYTYPE_OCCLUSION) failed with %x\n", hr),
5875 D3D_RELEASE(pContext->occlusion.pQuery), VERR_INTERNAL_ERROR);
5876 return VINF_SUCCESS;
5877}
5878
5879static DECLCALLBACK(int) vmsvga3dBackOcclusionQueryGetData(PVGASTATECC pThisCC, PVMSVGA3DCONTEXT pContext, uint32_t *pu32Pixels)
5880{
5881 RT_NOREF(pThisCC);
5882 HRESULT hr = D3D_OK;
5883 /* Wait until the data becomes available. */
5884 DWORD dwPixels = 0;
5885 do
5886 {
5887 hr = pContext->occlusion.pQuery->GetData((void *)&dwPixels, sizeof(DWORD), D3DGETDATA_FLUSH);
5888 } while (hr == S_FALSE);
5889
5890 AssertMsgReturnStmt(hr == D3D_OK, ("GetData(D3DQUERYTYPE_OCCLUSION) failed with %x\n", hr),
5891 D3D_RELEASE(pContext->occlusion.pQuery), VERR_INTERNAL_ERROR);
5892
5893 LogFunc(("Query result: dwPixels %d\n", dwPixels));
5894 *pu32Pixels = dwPixels;
5895 return VINF_SUCCESS;
5896}
5897
5898static void vmsvgaDumpD3DCaps(D3DCAPS9 *pCaps, D3DADAPTER_IDENTIFIER9 const *pai9)
5899{
5900 bool const fBufferingSaved = RTLogRelSetBuffering(true /*fBuffered*/);
5901
5902 LogRel(("\nD3D9 adapter: %s %RX16:%RX16 [%s, version %d.%d.%d.%d]\n",
5903 pai9->Description, pai9->VendorId, pai9->DeviceId, pai9->Driver,
5904 RT_HI_U16(pai9->DriverVersion.HighPart), RT_LO_U16(pai9->DriverVersion.HighPart),
5905 RT_HI_U16(pai9->DriverVersion.LowPart), RT_LO_U16(pai9->DriverVersion.LowPart)));
5906
5907 LogRel(("\nD3D device caps: DevCaps2:\n"));
5908 if (pCaps->DevCaps2 & D3DDEVCAPS2_ADAPTIVETESSRTPATCH)
5909 LogRel((" - D3DDEVCAPS2_ADAPTIVETESSRTPATCH\n"));
5910 if (pCaps->DevCaps2 & D3DDEVCAPS2_ADAPTIVETESSNPATCH)
5911 LogRel((" - D3DDEVCAPS2_ADAPTIVETESSNPATCH\n"));
5912 if (pCaps->DevCaps2 & D3DDEVCAPS2_CAN_STRETCHRECT_FROM_TEXTURES)
5913 LogRel((" - D3DDEVCAPS2_CAN_STRETCHRECT_FROM_TEXTURES\n"));
5914 if (pCaps->DevCaps2 & D3DDEVCAPS2_DMAPNPATCH)
5915 LogRel((" - D3DDEVCAPS2_DMAPNPATCH\n"));
5916 if (pCaps->DevCaps2 & D3DDEVCAPS2_PRESAMPLEDDMAPNPATCH)
5917 LogRel((" - D3DDEVCAPS2_PRESAMPLEDDMAPNPATCH\n"));
5918 if (pCaps->DevCaps2 & D3DDEVCAPS2_STREAMOFFSET)
5919 LogRel((" - D3DDEVCAPS2_STREAMOFFSET\n"));
5920 if (pCaps->DevCaps2 & D3DDEVCAPS2_VERTEXELEMENTSCANSHARESTREAMOFFSET)
5921 LogRel((" - D3DDEVCAPS2_VERTEXELEMENTSCANSHARESTREAMOFFSET\n"));
5922
5923 LogRel(("\nCaps2:\n"));
5924 if (pCaps->Caps2 & D3DCAPS2_CANAUTOGENMIPMAP)
5925 LogRel((" - D3DCAPS2_CANAUTOGENMIPMAP\n"));
5926 if (pCaps->Caps2 & D3DCAPS2_CANCALIBRATEGAMMA)
5927 LogRel((" - D3DCAPS2_CANCALIBRATEGAMMA\n"));
5928 if (pCaps->Caps2 & D3DCAPS2_CANSHARERESOURCE)
5929 LogRel((" - D3DCAPS2_CANSHARERESOURCE\n"));
5930 if (pCaps->Caps2 & D3DCAPS2_CANMANAGERESOURCE)
5931 LogRel((" - D3DCAPS2_CANMANAGERESOURCE\n"));
5932 if (pCaps->Caps2 & D3DCAPS2_DYNAMICTEXTURES)
5933 LogRel((" - D3DCAPS2_DYNAMICTEXTURES\n"));
5934 if (pCaps->Caps2 & D3DCAPS2_FULLSCREENGAMMA)
5935 LogRel((" - D3DCAPS2_FULLSCREENGAMMA\n"));
5936
5937 LogRel(("\nCaps3:\n"));
5938 if (pCaps->Caps3 & D3DCAPS3_ALPHA_FULLSCREEN_FLIP_OR_DISCARD)
5939 LogRel((" - D3DCAPS3_ALPHA_FULLSCREEN_FLIP_OR_DISCARD\n"));
5940 if (pCaps->Caps3 & D3DCAPS3_COPY_TO_VIDMEM)
5941 LogRel((" - D3DCAPS3_COPY_TO_VIDMEM\n"));
5942 if (pCaps->Caps3 & D3DCAPS3_COPY_TO_SYSTEMMEM)
5943 LogRel((" - D3DCAPS3_COPY_TO_SYSTEMMEM\n"));
5944 if (pCaps->Caps3 & D3DCAPS3_DXVAHD)
5945 LogRel((" - D3DCAPS3_DXVAHD\n"));
5946 if (pCaps->Caps3 & D3DCAPS3_LINEAR_TO_SRGB_PRESENTATION)
5947 LogRel((" - D3DCAPS3_LINEAR_TO_SRGB_PRESENTATION\n"));
5948
5949 LogRel(("\nPresentationIntervals:\n"));
5950 if (pCaps->PresentationIntervals & D3DPRESENT_INTERVAL_IMMEDIATE)
5951 LogRel((" - D3DPRESENT_INTERVAL_IMMEDIATE\n"));
5952 if (pCaps->PresentationIntervals & D3DPRESENT_INTERVAL_ONE)
5953 LogRel((" - D3DPRESENT_INTERVAL_ONE\n"));
5954 if (pCaps->PresentationIntervals & D3DPRESENT_INTERVAL_TWO)
5955 LogRel((" - D3DPRESENT_INTERVAL_TWO\n"));
5956 if (pCaps->PresentationIntervals & D3DPRESENT_INTERVAL_THREE)
5957 LogRel((" - D3DPRESENT_INTERVAL_THREE\n"));
5958 if (pCaps->PresentationIntervals & D3DPRESENT_INTERVAL_FOUR)
5959 LogRel((" - D3DPRESENT_INTERVAL_FOUR\n"));
5960
5961 LogRel(("\nDevcaps:\n"));
5962 if (pCaps->DevCaps & D3DDEVCAPS_CANBLTSYSTONONLOCAL)
5963 LogRel((" - D3DDEVCAPS_CANBLTSYSTONONLOCAL\n"));
5964 if (pCaps->DevCaps & D3DDEVCAPS_CANRENDERAFTERFLIP)
5965 LogRel((" - D3DDEVCAPS_CANRENDERAFTERFLIP\n"));
5966 if (pCaps->DevCaps & D3DDEVCAPS_DRAWPRIMITIVES2)
5967 LogRel((" - D3DDEVCAPS_DRAWPRIMITIVES2\n"));
5968 if (pCaps->DevCaps & D3DDEVCAPS_DRAWPRIMITIVES2EX)
5969 LogRel((" - D3DDEVCAPS_DRAWPRIMITIVES2EX\n"));
5970 if (pCaps->DevCaps & D3DDEVCAPS_DRAWPRIMTLVERTEX)
5971 LogRel((" - D3DDEVCAPS_DRAWPRIMTLVERTEX\n"));
5972 if (pCaps->DevCaps & D3DDEVCAPS_EXECUTESYSTEMMEMORY)
5973 LogRel((" - D3DDEVCAPS_EXECUTESYSTEMMEMORY\n"));
5974 if (pCaps->DevCaps & D3DDEVCAPS_EXECUTEVIDEOMEMORY)
5975 LogRel((" - D3DDEVCAPS_EXECUTEVIDEOMEMORY\n"));
5976 if (pCaps->DevCaps & D3DDEVCAPS_HWRASTERIZATION)
5977 LogRel((" - D3DDEVCAPS_HWRASTERIZATION\n"));
5978 if (pCaps->DevCaps & D3DDEVCAPS_HWTRANSFORMANDLIGHT)
5979 LogRel((" - D3DDEVCAPS_HWTRANSFORMANDLIGHT\n"));
5980 if (pCaps->DevCaps & D3DDEVCAPS_NPATCHES)
5981 LogRel((" - D3DDEVCAPS_NPATCHES\n"));
5982 if (pCaps->DevCaps & D3DDEVCAPS_PUREDEVICE)
5983 LogRel((" - D3DDEVCAPS_PUREDEVICE\n"));
5984 if (pCaps->DevCaps & D3DDEVCAPS_QUINTICRTPATCHES)
5985 LogRel((" - D3DDEVCAPS_QUINTICRTPATCHES\n"));
5986 if (pCaps->DevCaps & D3DDEVCAPS_RTPATCHES)
5987 LogRel((" - D3DDEVCAPS_RTPATCHES\n"));
5988 if (pCaps->DevCaps & D3DDEVCAPS_RTPATCHHANDLEZERO)
5989 LogRel((" - D3DDEVCAPS_RTPATCHHANDLEZERO\n"));
5990 if (pCaps->DevCaps & D3DDEVCAPS_SEPARATETEXTUREMEMORIES)
5991 LogRel((" - D3DDEVCAPS_SEPARATETEXTUREMEMORIES\n"));
5992 if (pCaps->DevCaps & D3DDEVCAPS_TEXTURENONLOCALVIDMEM)
5993 LogRel((" - D3DDEVCAPS_TEXTURENONLOCALVIDMEM\n"));
5994 if (pCaps->DevCaps & D3DDEVCAPS_TEXTURESYSTEMMEMORY)
5995 LogRel((" - D3DDEVCAPS_TEXTURESYSTEMMEMORY\n"));
5996 if (pCaps->DevCaps & D3DDEVCAPS_TEXTUREVIDEOMEMORY)
5997 LogRel((" - D3DDEVCAPS_TEXTUREVIDEOMEMORY\n"));
5998 if (pCaps->DevCaps & D3DDEVCAPS_TLVERTEXSYSTEMMEMORY)
5999 LogRel((" - D3DDEVCAPS_TLVERTEXSYSTEMMEMORY\n"));
6000 if (pCaps->DevCaps & D3DDEVCAPS_TLVERTEXVIDEOMEMORY)
6001 LogRel((" - D3DDEVCAPS_TLVERTEXVIDEOMEMORY\n"));
6002
6003 LogRel(("\nTextureCaps:\n"));
6004 if (pCaps->TextureCaps & D3DPTEXTURECAPS_ALPHA)
6005 LogRel((" - D3DPTEXTURECAPS_ALPHA\n"));
6006 if (pCaps->TextureCaps & D3DPTEXTURECAPS_ALPHAPALETTE)
6007 LogRel((" - D3DPTEXTURECAPS_ALPHAPALETTE\n"));
6008 if (pCaps->TextureCaps & D3DPTEXTURECAPS_CUBEMAP)
6009 LogRel((" - D3DPTEXTURECAPS_CUBEMAP\n"));
6010 if (pCaps->TextureCaps & D3DPTEXTURECAPS_CUBEMAP_POW2)
6011 LogRel((" - D3DPTEXTURECAPS_CUBEMAP_POW2\n"));
6012 if (pCaps->TextureCaps & D3DPTEXTURECAPS_MIPCUBEMAP)
6013 LogRel((" - D3DPTEXTURECAPS_MIPCUBEMAP\n"));
6014 if (pCaps->TextureCaps & D3DPTEXTURECAPS_MIPMAP)
6015 LogRel((" - D3DPTEXTURECAPS_MIPMAP\n"));
6016 if (pCaps->TextureCaps & D3DPTEXTURECAPS_MIPVOLUMEMAP)
6017 LogRel((" - D3DPTEXTURECAPS_MIPVOLUMEMAP\n"));
6018 if (pCaps->TextureCaps & D3DPTEXTURECAPS_NONPOW2CONDITIONAL)
6019 LogRel((" - D3DPTEXTURECAPS_NONPOW2CONDITIONAL\n"));
6020 if (pCaps->TextureCaps & D3DPTEXTURECAPS_POW2)
6021 LogRel((" - D3DPTEXTURECAPS_POW2\n"));
6022 if (pCaps->TextureCaps & D3DPTEXTURECAPS_NOPROJECTEDBUMPENV)
6023 LogRel((" - D3DPTEXTURECAPS_NOPROJECTEDBUMPENV\n"));
6024 if (pCaps->TextureCaps & D3DPTEXTURECAPS_PERSPECTIVE)
6025 LogRel((" - D3DPTEXTURECAPS_PERSPECTIVE\n"));
6026 if (pCaps->TextureCaps & D3DPTEXTURECAPS_POW2)
6027 LogRel((" - D3DPTEXTURECAPS_POW2\n"));
6028 if (pCaps->TextureCaps & D3DPTEXTURECAPS_PROJECTED)
6029 LogRel((" - D3DPTEXTURECAPS_PROJECTED\n"));
6030 if (pCaps->TextureCaps & D3DPTEXTURECAPS_SQUAREONLY)
6031 LogRel((" - D3DPTEXTURECAPS_SQUAREONLY\n"));
6032 if (pCaps->TextureCaps & D3DPTEXTURECAPS_TEXREPEATNOTSCALEDBYSIZE)
6033 LogRel((" - D3DPTEXTURECAPS_TEXREPEATNOTSCALEDBYSIZE\n"));
6034 if (pCaps->TextureCaps & D3DPTEXTURECAPS_VOLUMEMAP)
6035 LogRel((" - D3DPTEXTURECAPS_VOLUMEMAP\n"));
6036 if (pCaps->TextureCaps & D3DPTEXTURECAPS_VOLUMEMAP_POW2)
6037 LogRel((" - D3DPTEXTURECAPS_VOLUMEMAP_POW2\n"));
6038
6039 LogRel(("\nTextureFilterCaps\n"));
6040 if (pCaps->TextureFilterCaps & D3DPTFILTERCAPS_CONVOLUTIONMONO)
6041 LogRel((" - D3DPTFILTERCAPS_CONVOLUTIONMONO\n"));
6042 if (pCaps->TextureFilterCaps & D3DPTFILTERCAPS_MAGFPOINT)
6043 LogRel((" - D3DPTFILTERCAPS_MAGFPOINT\n"));
6044 if (pCaps->TextureFilterCaps & D3DPTFILTERCAPS_MAGFLINEAR)
6045 LogRel((" - D3DPTFILTERCAPS_MAGFLINEAR\n"));
6046 if (pCaps->TextureFilterCaps & D3DPTFILTERCAPS_MAGFANISOTROPIC)
6047 LogRel((" - D3DPTFILTERCAPS_MAGFANISOTROPIC\n"));
6048 if (pCaps->TextureFilterCaps & D3DPTFILTERCAPS_MAGFPYRAMIDALQUAD)
6049 LogRel((" - D3DPTFILTERCAPS_MAGFPYRAMIDALQUAD\n"));
6050 if (pCaps->TextureFilterCaps & D3DPTFILTERCAPS_MAGFGAUSSIANQUAD)
6051 LogRel((" - D3DPTFILTERCAPS_MAGFGAUSSIANQUAD\n"));
6052 if (pCaps->TextureFilterCaps & D3DPTFILTERCAPS_MINFPOINT)
6053 LogRel((" - D3DPTFILTERCAPS_MINFPOINT\n"));
6054 if (pCaps->TextureFilterCaps & D3DPTFILTERCAPS_MINFLINEAR)
6055 LogRel((" - D3DPTFILTERCAPS_MINFLINEAR\n"));
6056 if (pCaps->TextureFilterCaps & D3DPTFILTERCAPS_MINFANISOTROPIC)
6057 LogRel((" - D3DPTFILTERCAPS_MINFANISOTROPIC\n"));
6058 if (pCaps->TextureFilterCaps & D3DPTFILTERCAPS_MINFPYRAMIDALQUAD)
6059 LogRel((" - D3DPTFILTERCAPS_MINFPYRAMIDALQUAD\n"));
6060 if (pCaps->TextureFilterCaps & D3DPTFILTERCAPS_MINFGAUSSIANQUAD)
6061 LogRel((" - D3DPTFILTERCAPS_MINFGAUSSIANQUAD\n"));
6062 if (pCaps->TextureFilterCaps & D3DPTFILTERCAPS_MIPFPOINT)
6063 LogRel((" - D3DPTFILTERCAPS_MIPFPOINT\n"));
6064 if (pCaps->TextureFilterCaps & D3DPTFILTERCAPS_MIPFLINEAR)
6065 LogRel((" - D3DPTFILTERCAPS_MIPFLINEAR\n"));
6066
6067 LogRel(("\nCubeTextureFilterCaps\n"));
6068 if (pCaps->CubeTextureFilterCaps & D3DPTFILTERCAPS_CONVOLUTIONMONO)
6069 LogRel((" - D3DPTFILTERCAPS_CONVOLUTIONMONO\n"));
6070 if (pCaps->CubeTextureFilterCaps & D3DPTFILTERCAPS_MAGFPOINT)
6071 LogRel((" - D3DPTFILTERCAPS_MAGFPOINT\n"));
6072 if (pCaps->CubeTextureFilterCaps & D3DPTFILTERCAPS_MAGFLINEAR)
6073 LogRel((" - D3DPTFILTERCAPS_MAGFLINEAR\n"));
6074 if (pCaps->CubeTextureFilterCaps & D3DPTFILTERCAPS_MAGFANISOTROPIC)
6075 LogRel((" - D3DPTFILTERCAPS_MAGFANISOTROPIC\n"));
6076 if (pCaps->CubeTextureFilterCaps & D3DPTFILTERCAPS_MAGFPYRAMIDALQUAD)
6077 LogRel((" - D3DPTFILTERCAPS_MAGFPYRAMIDALQUAD\n"));
6078 if (pCaps->CubeTextureFilterCaps & D3DPTFILTERCAPS_MAGFGAUSSIANQUAD)
6079 LogRel((" - D3DPTFILTERCAPS_MAGFGAUSSIANQUAD\n"));
6080 if (pCaps->CubeTextureFilterCaps & D3DPTFILTERCAPS_MINFPOINT)
6081 LogRel((" - D3DPTFILTERCAPS_MINFPOINT\n"));
6082 if (pCaps->CubeTextureFilterCaps & D3DPTFILTERCAPS_MINFLINEAR)
6083 LogRel((" - D3DPTFILTERCAPS_MINFLINEAR\n"));
6084 if (pCaps->CubeTextureFilterCaps & D3DPTFILTERCAPS_MINFANISOTROPIC)
6085 LogRel((" - D3DPTFILTERCAPS_MINFANISOTROPIC\n"));
6086 if (pCaps->CubeTextureFilterCaps & D3DPTFILTERCAPS_MINFPYRAMIDALQUAD)
6087 LogRel((" - D3DPTFILTERCAPS_MINFPYRAMIDALQUAD\n"));
6088 if (pCaps->CubeTextureFilterCaps & D3DPTFILTERCAPS_MINFGAUSSIANQUAD)
6089 LogRel((" - D3DPTFILTERCAPS_MINFGAUSSIANQUAD\n"));
6090 if (pCaps->CubeTextureFilterCaps & D3DPTFILTERCAPS_MIPFPOINT)
6091 LogRel((" - D3DPTFILTERCAPS_MIPFPOINT\n"));
6092 if (pCaps->CubeTextureFilterCaps & D3DPTFILTERCAPS_MIPFLINEAR)
6093 LogRel((" - D3DPTFILTERCAPS_MIPFLINEAR\n"));
6094
6095 LogRel(("\nVolumeTextureFilterCaps\n"));
6096 if (pCaps->VolumeTextureFilterCaps & D3DPTFILTERCAPS_CONVOLUTIONMONO)
6097 LogRel((" - D3DPTFILTERCAPS_CONVOLUTIONMONO\n"));
6098 if (pCaps->VolumeTextureFilterCaps & D3DPTFILTERCAPS_MAGFPOINT)
6099 LogRel((" - D3DPTFILTERCAPS_MAGFPOINT\n"));
6100 if (pCaps->VolumeTextureFilterCaps & D3DPTFILTERCAPS_MAGFLINEAR)
6101 LogRel((" - D3DPTFILTERCAPS_MAGFLINEAR\n"));
6102 if (pCaps->VolumeTextureFilterCaps & D3DPTFILTERCAPS_MAGFANISOTROPIC)
6103 LogRel((" - D3DPTFILTERCAPS_MAGFANISOTROPIC\n"));
6104 if (pCaps->VolumeTextureFilterCaps & D3DPTFILTERCAPS_MAGFPYRAMIDALQUAD)
6105 LogRel((" - D3DPTFILTERCAPS_MAGFPYRAMIDALQUAD\n"));
6106 if (pCaps->VolumeTextureFilterCaps & D3DPTFILTERCAPS_MAGFGAUSSIANQUAD)
6107 LogRel((" - D3DPTFILTERCAPS_MAGFGAUSSIANQUAD\n"));
6108 if (pCaps->VolumeTextureFilterCaps & D3DPTFILTERCAPS_MINFPOINT)
6109 LogRel((" - D3DPTFILTERCAPS_MINFPOINT\n"));
6110 if (pCaps->VolumeTextureFilterCaps & D3DPTFILTERCAPS_MINFLINEAR)
6111 LogRel((" - D3DPTFILTERCAPS_MINFLINEAR\n"));
6112 if (pCaps->VolumeTextureFilterCaps & D3DPTFILTERCAPS_MINFANISOTROPIC)
6113 LogRel((" - D3DPTFILTERCAPS_MINFANISOTROPIC\n"));
6114 if (pCaps->VolumeTextureFilterCaps & D3DPTFILTERCAPS_MINFPYRAMIDALQUAD)
6115 LogRel((" - D3DPTFILTERCAPS_MINFPYRAMIDALQUAD\n"));
6116 if (pCaps->VolumeTextureFilterCaps & D3DPTFILTERCAPS_MINFGAUSSIANQUAD)
6117 LogRel((" - D3DPTFILTERCAPS_MINFGAUSSIANQUAD\n"));
6118 if (pCaps->VolumeTextureFilterCaps & D3DPTFILTERCAPS_MIPFPOINT)
6119 LogRel((" - D3DPTFILTERCAPS_MIPFPOINT\n"));
6120 if (pCaps->VolumeTextureFilterCaps & D3DPTFILTERCAPS_MIPFLINEAR)
6121 LogRel((" - D3DPTFILTERCAPS_MIPFLINEAR\n"));
6122
6123 LogRel(("\nTextureAddressCaps:\n"));
6124 if (pCaps->TextureAddressCaps & D3DPTADDRESSCAPS_BORDER)
6125 LogRel((" - D3DPTADDRESSCAPS_BORDER\n"));
6126 if (pCaps->TextureAddressCaps & D3DPTADDRESSCAPS_CLAMP)
6127 LogRel((" - D3DPTADDRESSCAPS_CLAMP\n"));
6128 if (pCaps->TextureAddressCaps & D3DPTADDRESSCAPS_INDEPENDENTUV)
6129 LogRel((" - D3DPTADDRESSCAPS_INDEPENDENTUV\n"));
6130 if (pCaps->TextureAddressCaps & D3DPTADDRESSCAPS_MIRROR)
6131 LogRel((" - D3DPTADDRESSCAPS_MIRROR\n"));
6132 if (pCaps->TextureAddressCaps & D3DPTADDRESSCAPS_MIRRORONCE)
6133 LogRel((" - D3DPTADDRESSCAPS_MIRRORONCE\n"));
6134 if (pCaps->TextureAddressCaps & D3DPTADDRESSCAPS_WRAP)
6135 LogRel((" - D3DPTADDRESSCAPS_WRAP\n"));
6136
6137 LogRel(("\nTextureOpCaps:\n"));
6138 if (pCaps->TextureOpCaps & D3DTEXOPCAPS_DISABLE)
6139 LogRel((" - D3DTEXOPCAPS_DISABLE\n"));
6140 if (pCaps->TextureOpCaps & D3DTEXOPCAPS_SELECTARG1)
6141 LogRel((" - D3DTEXOPCAPS_SELECTARG1\n"));
6142 if (pCaps->TextureOpCaps & D3DTEXOPCAPS_SELECTARG2)
6143 LogRel((" - D3DTEXOPCAPS_SELECTARG2\n"));
6144 if (pCaps->TextureOpCaps & D3DTEXOPCAPS_MODULATE)
6145 LogRel((" - D3DTEXOPCAPS_MODULATE\n"));
6146 if (pCaps->TextureOpCaps & D3DTEXOPCAPS_MODULATE2X)
6147 LogRel((" - D3DTEXOPCAPS_MODULATE2X\n"));
6148 if (pCaps->TextureOpCaps & D3DTEXOPCAPS_MODULATE4X)
6149 LogRel((" - D3DTEXOPCAPS_MODULATE4X\n"));
6150 if (pCaps->TextureOpCaps & D3DTEXOPCAPS_ADD)
6151 LogRel((" - D3DTEXOPCAPS_ADD\n"));
6152 if (pCaps->TextureOpCaps & D3DTEXOPCAPS_ADDSIGNED)
6153 LogRel((" - D3DTEXOPCAPS_ADDSIGNED\n"));
6154 if (pCaps->TextureOpCaps & D3DTEXOPCAPS_ADDSIGNED2X)
6155 LogRel((" - D3DTEXOPCAPS_ADDSIGNED2X\n"));
6156 if (pCaps->TextureOpCaps & D3DTEXOPCAPS_SUBTRACT)
6157 LogRel((" - D3DTEXOPCAPS_SUBTRACT\n"));
6158 if (pCaps->TextureOpCaps & D3DTEXOPCAPS_ADDSMOOTH)
6159 LogRel((" - D3DTEXOPCAPS_ADDSMOOTH\n"));
6160 if (pCaps->TextureOpCaps & D3DTEXOPCAPS_BLENDDIFFUSEALPHA)
6161 LogRel((" - D3DTEXOPCAPS_BLENDDIFFUSEALPHA\n"));
6162 if (pCaps->TextureOpCaps & D3DTEXOPCAPS_BLENDTEXTUREALPHA)
6163 LogRel((" - D3DTEXOPCAPS_BLENDTEXTUREALPHA\n"));
6164 if (pCaps->TextureOpCaps & D3DTEXOPCAPS_BLENDFACTORALPHA)
6165 LogRel((" - D3DTEXOPCAPS_BLENDFACTORALPHA\n"));
6166 if (pCaps->TextureOpCaps & D3DTEXOPCAPS_BLENDTEXTUREALPHAPM)
6167 LogRel((" - D3DTEXOPCAPS_BLENDTEXTUREALPHAPM\n"));
6168 if (pCaps->TextureOpCaps & D3DTEXOPCAPS_BLENDCURRENTALPHA)
6169 LogRel((" - D3DTEXOPCAPS_BLENDCURRENTALPHA\n"));
6170 if (pCaps->TextureOpCaps & D3DTEXOPCAPS_PREMODULATE)
6171 LogRel((" - D3DTEXOPCAPS_PREMODULATE\n"));
6172 if (pCaps->TextureOpCaps & D3DTEXOPCAPS_MODULATEALPHA_ADDCOLOR)
6173 LogRel((" - D3DTEXOPCAPS_MODULATEALPHA_ADDCOLOR\n"));
6174 if (pCaps->TextureOpCaps & D3DTEXOPCAPS_MODULATECOLOR_ADDALPHA)
6175 LogRel((" - D3DTEXOPCAPS_MODULATECOLOR_ADDALPHA\n"));
6176 if (pCaps->TextureOpCaps & D3DTEXOPCAPS_MODULATEINVALPHA_ADDCOLOR)
6177 LogRel((" - D3DTEXOPCAPS_MODULATEINVALPHA_ADDCOLOR\n"));
6178 if (pCaps->TextureOpCaps & D3DTEXOPCAPS_MODULATEINVCOLOR_ADDALPHA)
6179 LogRel((" - D3DTEXOPCAPS_MODULATEINVCOLOR_ADDALPHA\n"));
6180 if (pCaps->TextureOpCaps & D3DTEXOPCAPS_BUMPENVMAP)
6181 LogRel((" - D3DTEXOPCAPS_BUMPENVMAP\n"));
6182 if (pCaps->TextureOpCaps & D3DTEXOPCAPS_BUMPENVMAPLUMINANCE)
6183 LogRel((" - D3DTEXOPCAPS_BUMPENVMAPLUMINANCE\n"));
6184 if (pCaps->TextureOpCaps & D3DTEXOPCAPS_DOTPRODUCT3)
6185 LogRel((" - D3DTEXOPCAPS_DOTPRODUCT3\n"));
6186 if (pCaps->TextureOpCaps & D3DTEXOPCAPS_MULTIPLYADD)
6187 LogRel((" - D3DTEXOPCAPS_MULTIPLYADD\n"));
6188 if (pCaps->TextureOpCaps & D3DTEXOPCAPS_LERP)
6189 LogRel((" - D3DTEXOPCAPS_LERP\n"));
6190
6191
6192 LogRel(("\n"));
6193 LogRel(("PixelShaderVersion: %#x (%u.%u)\n", pCaps->PixelShaderVersion,
6194 D3DSHADER_VERSION_MAJOR(pCaps->PixelShaderVersion), D3DSHADER_VERSION_MINOR(pCaps->PixelShaderVersion)));
6195 LogRel(("VertexShaderVersion: %#x (%u.%u)\n", pCaps->VertexShaderVersion,
6196 D3DSHADER_VERSION_MAJOR(pCaps->VertexShaderVersion), D3DSHADER_VERSION_MINOR(pCaps->VertexShaderVersion)));
6197
6198 LogRel(("\n"));
6199 RTLogRelSetBuffering(fBufferingSaved);
6200}
6201
6202/**
6203 * Worker for vmsvga3dUpdateHeapBuffersForSurfaces.
6204 *
6205 * This will allocate heap buffers if necessary, thus increasing the memory
6206 * usage of the process.
6207 *
6208 * @todo Would be interesting to share this code with the saved state code.
6209 *
6210 * @returns VBox status code.
6211 * @param pThisCC The VGA/VMSVGA context.
6212 * @param pSurface The surface to refresh the heap buffers for.
6213 */
6214static DECLCALLBACK(int) vmsvga3dBackSurfaceUpdateHeapBuffers(PVGASTATECC pThisCC, PVMSVGA3DSURFACE pSurface)
6215{
6216 PVMSVGA3DSTATE pState = pThisCC->svga.p3dState;
6217 AssertReturn(pState, VERR_INVALID_STATE);
6218
6219 /*
6220 * Currently we've got trouble retreving bit for DEPTHSTENCIL
6221 * surfaces both for OpenGL and D3D, so skip these here (don't
6222 * wast memory on them).
6223 */
6224 uint32_t const fSwitchFlags = pSurface->surfaceFlags & VMSVGA3D_SURFACE_HINT_SWITCH_MASK;
6225 if ( fSwitchFlags != SVGA3D_SURFACE_HINT_DEPTHSTENCIL
6226 && fSwitchFlags != (SVGA3D_SURFACE_HINT_DEPTHSTENCIL | SVGA3D_SURFACE_HINT_TEXTURE))
6227 {
6228
6229 /*
6230 * Work thru each mipmap level for each face.
6231 */
6232 for (uint32_t iFace = 0; iFace < pSurface->cFaces; iFace++)
6233 {
6234 PVMSVGA3DMIPMAPLEVEL pMipmapLevel = &pSurface->paMipmapLevels[iFace * pSurface->cLevels];
6235 for (uint32_t i = 0; i < pSurface->cLevels; i++, pMipmapLevel++)
6236 {
6237 if (VMSVGA3DSURFACE_HAS_HW_SURFACE(pSurface))
6238 {
6239 Assert(pMipmapLevel->cbSurface);
6240 Assert(pMipmapLevel->cbSurface == pMipmapLevel->cbSurfacePlane * pMipmapLevel->mipmapSize.depth);
6241
6242 /*
6243 * Make sure we've got surface memory buffer.
6244 */
6245 uint8_t *pbDst = (uint8_t *)pMipmapLevel->pSurfaceData;
6246 if (!pbDst)
6247 {
6248 pMipmapLevel->pSurfaceData = pbDst = (uint8_t *)RTMemAllocZ(pMipmapLevel->cbSurface);
6249 AssertReturn(pbDst, VERR_NO_MEMORY);
6250 }
6251
6252 /*
6253 * D3D specifics.
6254 */
6255 Assert(pSurface->enmD3DResType != VMSVGA3D_D3DRESTYPE_NONE);
6256
6257 HRESULT hr;
6258 switch (pSurface->enmD3DResType)
6259 {
6260 case VMSVGA3D_D3DRESTYPE_VOLUME_TEXTURE:
6261 AssertFailed(); /// @todo
6262 break;
6263
6264 case VMSVGA3D_D3DRESTYPE_SURFACE:
6265 case VMSVGA3D_D3DRESTYPE_TEXTURE:
6266 case VMSVGA3D_D3DRESTYPE_CUBE_TEXTURE:
6267 {
6268 /*
6269 * Lock the buffer and make it accessible to memcpy.
6270 */
6271 D3DLOCKED_RECT LockedRect;
6272 if (pSurface->enmD3DResType == VMSVGA3D_D3DRESTYPE_CUBE_TEXTURE)
6273 {
6274 hr = pSurface->u.pCubeTexture->LockRect(vmsvga3dCubemapFaceFromIndex(iFace),
6275 i, /* texture level */
6276 &LockedRect,
6277 NULL,
6278 D3DLOCK_READONLY);
6279 }
6280 else if (pSurface->enmD3DResType == VMSVGA3D_D3DRESTYPE_TEXTURE)
6281 {
6282 if (pSurface->bounce.pTexture)
6283 {
6284 if ( !pSurface->fDirty
6285 && RT_BOOL(fSwitchFlags & SVGA3D_SURFACE_HINT_RENDERTARGET))
6286 {
6287 /** @todo stricter checks for associated context */
6288 uint32_t cid = pSurface->idAssociatedContext;
6289 PVMSVGA3DCONTEXT pContext;
6290 int rc = vmsvga3dContextFromCid(pState, cid, &pContext);
6291 AssertRCReturn(rc, rc);
6292
6293 IDirect3DSurface9 *pDst = NULL;
6294 hr = pSurface->bounce.pTexture->GetSurfaceLevel(i, &pDst);
6295 AssertMsgReturn(hr == D3D_OK, ("GetSurfaceLevel failed with %#x\n", hr), VERR_INTERNAL_ERROR);
6296
6297 IDirect3DSurface9 *pSrc = NULL;
6298 hr = pSurface->u.pTexture->GetSurfaceLevel(i, &pSrc);
6299 AssertMsgReturn(hr == D3D_OK, ("GetSurfaceLevel failed with %#x\n", hr), VERR_INTERNAL_ERROR);
6300
6301 hr = pContext->pDevice->GetRenderTargetData(pSrc, pDst);
6302 AssertMsgReturn(hr == D3D_OK, ("GetRenderTargetData failed with %#x\n", hr), VERR_INTERNAL_ERROR);
6303
6304 pSrc->Release();
6305 pDst->Release();
6306 }
6307
6308 hr = pSurface->bounce.pTexture->LockRect(i, /* texture level */
6309 &LockedRect,
6310 NULL,
6311 D3DLOCK_READONLY);
6312 }
6313 else
6314 hr = pSurface->u.pTexture->LockRect(i, /* texture level */
6315 &LockedRect,
6316 NULL,
6317 D3DLOCK_READONLY);
6318 }
6319 else
6320 hr = pSurface->u.pSurface->LockRect(&LockedRect,
6321 NULL,
6322 D3DLOCK_READONLY);
6323 AssertMsgReturn(hr == D3D_OK, ("LockRect failed with %x\n", hr), VERR_INTERNAL_ERROR);
6324
6325 /*
6326 * Copy the data. Take care in case the pitch differs.
6327 */
6328 if (pMipmapLevel->cbSurfacePitch == (uint32_t)LockedRect.Pitch)
6329 memcpy(pbDst, LockedRect.pBits, pMipmapLevel->cbSurface);
6330 else
6331 for (uint32_t j = 0; j < pMipmapLevel->cBlocksY; j++)
6332 memcpy(pbDst + j * pMipmapLevel->cbSurfacePitch,
6333 (uint8_t *)LockedRect.pBits + j * LockedRect.Pitch,
6334 pMipmapLevel->cbSurfacePitch);
6335
6336 /*
6337 * Release the buffer.
6338 */
6339 if (fSwitchFlags & SVGA3D_SURFACE_HINT_TEXTURE)
6340 {
6341 if (pSurface->bounce.pTexture)
6342 {
6343 hr = pSurface->bounce.pTexture->UnlockRect(i);
6344 AssertMsgReturn(hr == D3D_OK, ("UnlockRect failed with %#x\n", hr), VERR_INTERNAL_ERROR);
6345 }
6346 else
6347 hr = pSurface->u.pTexture->UnlockRect(i);
6348 }
6349 else
6350 hr = pSurface->u.pSurface->UnlockRect();
6351 AssertMsgReturn(hr == D3D_OK, ("UnlockRect failed with %#x\n", hr), VERR_INTERNAL_ERROR);
6352 break;
6353 }
6354
6355 case VMSVGA3D_D3DRESTYPE_VERTEX_BUFFER:
6356 case VMSVGA3D_D3DRESTYPE_INDEX_BUFFER:
6357 {
6358 /* Current type of the buffer. */
6359 const bool fVertex = (pSurface->enmD3DResType == VMSVGA3D_D3DRESTYPE_VERTEX_BUFFER);
6360
6361 void *pvD3DData = NULL;
6362 if (fVertex)
6363 hr = pSurface->u.pVertexBuffer->Lock(0, 0, &pvD3DData, D3DLOCK_READONLY);
6364 else
6365 hr = pSurface->u.pIndexBuffer->Lock(0, 0, &pvD3DData, D3DLOCK_READONLY);
6366 AssertMsgReturn(hr == D3D_OK, ("Lock %s failed with %x\n", fVertex ? "vertex" : "index", hr), VERR_INTERNAL_ERROR);
6367
6368 memcpy(pbDst, pvD3DData, pMipmapLevel->cbSurface);
6369
6370 if (fVertex)
6371 hr = pSurface->u.pVertexBuffer->Unlock();
6372 else
6373 hr = pSurface->u.pIndexBuffer->Unlock();
6374 AssertMsg(hr == D3D_OK, ("Unlock %s failed with %x\n", fVertex ? "vertex" : "index", hr));
6375 break;
6376 }
6377
6378 default:
6379 AssertMsgFailed(("flags %#x, type %d\n", fSwitchFlags, pSurface->enmD3DResType));
6380 }
6381
6382 }
6383 /* else: There is no data in hardware yet, so whatever we got is already current. */
6384 }
6385 }
6386 }
6387
6388 return VINF_SUCCESS;
6389}
6390
6391
6392static DECLCALLBACK(int) vmsvga3dBackQueryInterface(PVGASTATECC pThisCC, char const *pszInterfaceName, void *pvInterfaceFuncs, size_t cbInterfaceFuncs)
6393{
6394 RT_NOREF(pThisCC);
6395
6396 int rc = VINF_SUCCESS;
6397 if (RTStrCmp(pszInterfaceName, VMSVGA3D_BACKEND_INTERFACE_NAME_3D) == 0)
6398 {
6399 if (cbInterfaceFuncs == sizeof(VMSVGA3DBACKENDFUNCS3D))
6400 {
6401 if (pvInterfaceFuncs)
6402 {
6403 VMSVGA3DBACKENDFUNCS3D *p = (VMSVGA3DBACKENDFUNCS3D *)pvInterfaceFuncs;
6404 p->pfnInit = vmsvga3dBackInit;
6405 p->pfnPowerOn = vmsvga3dBackPowerOn;
6406 p->pfnTerminate = vmsvga3dBackTerminate;
6407 p->pfnReset = vmsvga3dBackReset;
6408 p->pfnQueryCaps = vmsvga3dBackQueryCaps;
6409 p->pfnChangeMode = vmsvga3dBackChangeMode;
6410 p->pfnCreateTexture = vmsvga3dBackCreateTexture;
6411 p->pfnSurfaceDestroy = vmsvga3dBackSurfaceDestroy;
6412 p->pfnSurfaceCopy = vmsvga3dBackSurfaceCopy;
6413 p->pfnSurfaceDMACopyBox = vmsvga3dBackSurfaceDMACopyBox;
6414 p->pfnSurfaceStretchBlt = vmsvga3dBackSurfaceStretchBlt;
6415 p->pfnUpdateHostScreenViewport = vmsvga3dBackUpdateHostScreenViewport;
6416 p->pfnDefineScreen = vmsvga3dBackDefineScreen;
6417 p->pfnDestroyScreen = vmsvga3dBackDestroyScreen;
6418 p->pfnSurfaceBlitToScreen = vmsvga3dBackSurfaceBlitToScreen;
6419 p->pfnSurfaceUpdateHeapBuffers = vmsvga3dBackSurfaceUpdateHeapBuffers;
6420 }
6421 }
6422 else
6423 {
6424 AssertFailed();
6425 rc = VERR_INVALID_PARAMETER;
6426 }
6427 }
6428 else if (RTStrCmp(pszInterfaceName, VMSVGA3D_BACKEND_INTERFACE_NAME_VGPU9) == 0)
6429 {
6430 if (cbInterfaceFuncs == sizeof(VMSVGA3DBACKENDFUNCSVGPU9))
6431 {
6432 if (pvInterfaceFuncs)
6433 {
6434 VMSVGA3DBACKENDFUNCSVGPU9 *p = (VMSVGA3DBACKENDFUNCSVGPU9 *)pvInterfaceFuncs;
6435 p->pfnContextDefine = vmsvga3dBackContextDefine;
6436 p->pfnContextDestroy = vmsvga3dBackContextDestroy;
6437 p->pfnSetTransform = vmsvga3dBackSetTransform;
6438 p->pfnSetZRange = vmsvga3dBackSetZRange;
6439 p->pfnSetRenderState = vmsvga3dBackSetRenderState;
6440 p->pfnSetRenderTarget = vmsvga3dBackSetRenderTarget;
6441 p->pfnSetTextureState = vmsvga3dBackSetTextureState;
6442 p->pfnSetMaterial = vmsvga3dBackSetMaterial;
6443 p->pfnSetLightData = vmsvga3dBackSetLightData;
6444 p->pfnSetLightEnabled = vmsvga3dBackSetLightEnabled;
6445 p->pfnSetViewPort = vmsvga3dBackSetViewPort;
6446 p->pfnSetClipPlane = vmsvga3dBackSetClipPlane;
6447 p->pfnCommandClear = vmsvga3dBackCommandClear;
6448 p->pfnDrawPrimitives = vmsvga3dBackDrawPrimitives;
6449 p->pfnSetScissorRect = vmsvga3dBackSetScissorRect;
6450 p->pfnGenerateMipmaps = vmsvga3dBackGenerateMipmaps;
6451 p->pfnShaderDefine = vmsvga3dBackShaderDefine;
6452 p->pfnShaderDestroy = vmsvga3dBackShaderDestroy;
6453 p->pfnShaderSet = vmsvga3dBackShaderSet;
6454 p->pfnShaderSetConst = vmsvga3dBackShaderSetConst;
6455 p->pfnOcclusionQueryCreate = vmsvga3dBackOcclusionQueryCreate;
6456 p->pfnOcclusionQueryDelete = vmsvga3dBackOcclusionQueryDelete;
6457 p->pfnOcclusionQueryBegin = vmsvga3dBackOcclusionQueryBegin;
6458 p->pfnOcclusionQueryEnd = vmsvga3dBackOcclusionQueryEnd;
6459 p->pfnOcclusionQueryGetData = vmsvga3dBackOcclusionQueryGetData;
6460 }
6461 }
6462 else
6463 {
6464 AssertFailed();
6465 rc = VERR_INVALID_PARAMETER;
6466 }
6467 }
6468 else
6469 rc = VERR_NOT_IMPLEMENTED;
6470 return rc;
6471}
6472
6473
6474extern VMSVGA3DBACKENDDESC const g_BackendLegacy =
6475{
6476 "LEGACY",
6477 vmsvga3dBackQueryInterface
6478};
Note: See TracBrowser for help on using the repository browser.

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