VirtualBox

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

最後變更 在這個檔案從95589是 95589,由 vboxsync 提交於 3 年 前

Devices/Graphics: cleanup. bugref:10242

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

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