VirtualBox

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

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

Devices/Graphics: track constant, vertex, index buffers; clear resource view entriee when surface is deleted (doxygen fix). bugref:9830

  • 屬性 svn:eol-style 設為 native
  • 屬性 svn:keywords 設為 Author Date Id Revision
檔案大小: 267.4 KB
 
1/* $Id: DevVGA-SVGA3d-win.cpp 95232 2022-06-08 15:34:49Z 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 Assert(pSurfaceSrc->enmD3DResType != VMSVGA3D_D3DRESTYPE_VOLUME_TEXTURE); /// @todo
1547 Assert(pSurfaceDest->enmD3DResType != VMSVGA3D_D3DRESTYPE_VOLUME_TEXTURE); /// @todo
1548
1549 if ( pSurfaceSrc->u.pSurface
1550 && pSurfaceDest->u.pSurface)
1551 {
1552 /* Both surfaces in hardware. Use the src context to copy one to another, because the src context may be needed
1553 * to copy data from source texture to the source bounce texture. while only the shared hardware surface is required
1554 * from the dst context.
1555 */
1556 uint32_t const cidSrc = pSurfaceSrc->idAssociatedContext;
1557
1558 PVMSVGA3DCONTEXT pContextSrc;
1559 rc = vmsvga3dContextFromCid(pState, cidSrc, &pContextSrc);
1560 AssertRCReturn(rc, rc);
1561 AssertReturn(pContextSrc->pDevice, VERR_INTERNAL_ERROR);
1562
1563 /* Must flush the other context's 3d pipeline to make sure all drawing is complete for the surface we're about to use. */
1564 vmsvga3dSurfaceFlush(pSurfaceSrc);
1565 vmsvga3dSurfaceFlush(pSurfaceDest);
1566
1567 IDirect3DSurface9 *pSrc;
1568 rc = vmsvga3dGetD3DSurface(pState, pContextSrc, pSurfaceSrc, src.face, src.mipmap, false, &pSrc);
1569 AssertRCReturn(rc, rc);
1570
1571 IDirect3DSurface9 *pDest;
1572 rc = vmsvga3dGetD3DSurface(pState, pContextSrc, pSurfaceDest, dest.face, dest.mipmap, false, &pDest);
1573 AssertRCReturnStmt(rc, D3D_RELEASE(pSrc), rc);
1574
1575 for (uint32_t i = 0; i < cCopyBoxes; ++i)
1576 {
1577 SVGA3dCopyBox clipBox = pBox[i];
1578 vmsvgaR3ClipCopyBox(&pMipmapLevelSrc->mipmapSize, &pMipmapLevelDest->mipmapSize, &clipBox);
1579 if ( !clipBox.w
1580 || !clipBox.h
1581 || !clipBox.d)
1582 {
1583 LogFunc(("Skipped empty box.\n"));
1584 continue;
1585 }
1586
1587 RECT RectSrc;
1588 RectSrc.left = clipBox.srcx;
1589 RectSrc.top = clipBox.srcy;
1590 RectSrc.right = clipBox.srcx + clipBox.w; /* exclusive */
1591 RectSrc.bottom = clipBox.srcy + clipBox.h; /* exclusive */
1592
1593 RECT RectDest;
1594 RectDest.left = clipBox.x;
1595 RectDest.top = clipBox.y;
1596 RectDest.right = clipBox.x + clipBox.w; /* exclusive */
1597 RectDest.bottom = clipBox.y + clipBox.h; /* exclusive */
1598
1599 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));
1600
1601 if ( sidSrc == sidDest
1602 && clipBox.srcx == clipBox.x
1603 && clipBox.srcy == clipBox.y)
1604 {
1605 LogFunc(("redundant copy to the same surface at the same coordinates. Ignore.\n"));
1606 continue;
1607 }
1608 Assert(sidSrc != sidDest);
1609 Assert(!clipBox.srcz && !clipBox.z);
1610
1611 HRESULT hr = pContextSrc->pDevice->StretchRect(pSrc, &RectSrc, pDest, &RectDest, D3DTEXF_NONE);
1612 if (hr != D3D_OK)
1613 {
1614 /* This can happen for compressed texture formats for example. */
1615 LogFunc(("StretchRect failed with %x. Try a slow path.\n", hr));
1616 if ( pSurfaceSrc->bounce.pTexture
1617 && (pSurfaceSrc->fUsageD3D & D3DUSAGE_RENDERTARGET))
1618 {
1619 /* Copy the source texture mipmap level to the source bounce texture. */
1620 hr = D3D9GetRenderTargetData(pContextSrc, pSurfaceSrc, src.face, src.mipmap);
1621 AssertMsg(hr == D3D_OK, ("D3D9GetRenderTargetData failed with %x\n", hr));
1622 if (hr == D3D_OK)
1623 {
1624 /* Copy the source bounce texture to the destination surface. */
1625 IDirect3DSurface9 *pSrcBounce;
1626 rc = vmsvga3dGetD3DSurface(pState, pContextSrc, pSurfaceSrc, src.face, src.mipmap, true, &pSrcBounce);
1627 if (RT_SUCCESS(rc))
1628 {
1629 POINT pointDest;
1630 pointDest.x = clipBox.x;
1631 pointDest.y = clipBox.y;
1632
1633 hr = pContextSrc->pDevice->UpdateSurface(pSrcBounce, &RectSrc, pDest, &pointDest);
1634 Assert(hr == D3D_OK);
1635
1636 D3D_RELEASE(pSrcBounce);
1637 }
1638 else
1639 {
1640 AssertRC(rc);
1641 hr = E_INVALIDARG;
1642 }
1643 }
1644 }
1645 else if ( (pSurfaceSrc->fUsageD3D & D3DUSAGE_RENDERTARGET) == 0
1646 && (pSurfaceDest->fUsageD3D & D3DUSAGE_RENDERTARGET) == 0)
1647 {
1648 /* Can lock both. */
1649 D3DLOCKED_RECT LockedSrcRect;
1650 hr = pSrc->LockRect(&LockedSrcRect, &RectSrc, D3DLOCK_READONLY);
1651 Assert(hr == D3D_OK);
1652 if (SUCCEEDED(hr))
1653 {
1654 D3DLOCKED_RECT LockedDestRect;
1655 hr = pDest->LockRect(&LockedDestRect, &RectDest, 0);
1656 Assert(hr == D3D_OK);
1657 if (SUCCEEDED(hr))
1658 {
1659 uint32_t cBlocksX = (clipBox.w + pSurfaceSrc->cxBlock - 1) / pSurfaceSrc->cxBlock;
1660 uint32_t cBlocksY = (clipBox.h + pSurfaceSrc->cyBlock - 1) / pSurfaceSrc->cyBlock;
1661
1662 uint32_t cbToCopy = cBlocksX * pSurfaceSrc->cbBlock;
1663 cbToCopy = RT_MIN(cbToCopy, (uint32_t)RT_ABS(LockedDestRect.Pitch));
1664 cbToCopy = RT_MIN(cbToCopy, (uint32_t)RT_ABS(LockedSrcRect.Pitch));
1665
1666 uint8_t *pu8Dst = (uint8_t *)LockedDestRect.pBits;
1667 const uint8_t *pu8Src = (uint8_t *)LockedSrcRect.pBits;
1668 for (uint32_t j = 0; j < cBlocksY; ++j)
1669 {
1670 memcpy(pu8Dst, pu8Src, cbToCopy);
1671 pu8Dst += LockedDestRect.Pitch;
1672 pu8Src += LockedSrcRect.Pitch;
1673 }
1674
1675 hr = pDest->UnlockRect();
1676 Assert(hr == D3D_OK);
1677 }
1678
1679 hr = pSrc->UnlockRect();
1680 Assert(hr == D3D_OK);
1681 }
1682 }
1683 }
1684 AssertMsgReturnStmt(hr == D3D_OK,
1685 ("StretchRect failed with %x\n", hr),
1686 D3D_RELEASE(pDest); D3D_RELEASE(pSrc),
1687 VERR_INTERNAL_ERROR);
1688 }
1689
1690 D3D_RELEASE(pDest);
1691 D3D_RELEASE(pSrc);
1692
1693 /* Track the StretchRect operation. */
1694 vmsvga3dSurfaceTrackUsage(pState, pContextSrc, pSurfaceSrc);
1695 vmsvga3dSurfaceTrackUsage(pState, pContextSrc, pSurfaceDest);
1696 }
1697 else
1698 {
1699 /* One of the surfaces is in memory.
1700 *
1701 * Copy from/to memory to/from a HW surface. Or mem->mem.
1702 * Use the context of the HW surface, if any.
1703 */
1704 PVMSVGA3DCONTEXT pContext = NULL;
1705 IDirect3DSurface9 *pD3DSurf = NULL;
1706
1707 if (pSurfaceSrc->u.pSurface)
1708 {
1709 AssertReturn(!pSurfaceDest->u.pSurface, VERR_INTERNAL_ERROR);
1710
1711 rc = vmsvga3dContextFromCid(pState, pSurfaceSrc->idAssociatedContext, &pContext);
1712 AssertRCReturn(rc, rc);
1713 AssertReturn(pContext->pDevice, VERR_INTERNAL_ERROR);
1714
1715 rc = vmsvga3dGetD3DSurface(pState, pContext, pSurfaceSrc, src.face, src.mipmap, true, &pD3DSurf);
1716 AssertRCReturn(rc, rc);
1717 }
1718 else if (pSurfaceDest->u.pSurface)
1719 {
1720 AssertReturn(!pSurfaceSrc->u.pSurface, VERR_INTERNAL_ERROR);
1721
1722 rc = vmsvga3dContextFromCid(pState, pSurfaceDest->idAssociatedContext, &pContext);
1723 AssertRCReturn(rc, rc);
1724 AssertReturn(pContext->pDevice, VERR_INTERNAL_ERROR);
1725
1726 rc = vmsvga3dGetD3DSurface(pState, pContext, pSurfaceDest, dest.face, dest.mipmap, true, &pD3DSurf);
1727 AssertRCReturn(rc, rc);
1728 }
1729
1730 for (uint32_t i = 0; i < cCopyBoxes; ++i)
1731 {
1732 HRESULT hr;
1733
1734 SVGA3dCopyBox clipBox = pBox[i];
1735 vmsvgaR3ClipCopyBox(&pMipmapLevelSrc->mipmapSize, &pMipmapLevelDest->mipmapSize, &clipBox);
1736 if ( !clipBox.w
1737 || !clipBox.h
1738 || !clipBox.d)
1739 {
1740 LogFunc(("Skipped empty box.\n"));
1741 continue;
1742 }
1743
1744 RECT RectSrc;
1745 RectSrc.left = clipBox.srcx;
1746 RectSrc.top = clipBox.srcy;
1747 RectSrc.right = clipBox.srcx + clipBox.w; /* exclusive */
1748 RectSrc.bottom = clipBox.srcy + clipBox.h; /* exclusive */
1749
1750 RECT RectDest;
1751 RectDest.left = clipBox.x;
1752 RectDest.top = clipBox.y;
1753 RectDest.right = clipBox.x + clipBox.w; /* exclusive */
1754 RectDest.bottom = clipBox.y + clipBox.h; /* exclusive */
1755
1756 LogFunc(("(manual) copy sid=%u face=%d mipmap=%d (%d,%d)(%d,%d) to sid=%u face=%d mipmap=%d (%d,%d)\n",
1757 sidSrc, src.face, src.mipmap, RectSrc.left, RectSrc.top, RectSrc.right, RectSrc.bottom,
1758 sidDest, dest.face, dest.mipmap, pBox[i].x, pBox[i].y));
1759
1760 Assert(!clipBox.srcz && !clipBox.z);
1761 Assert(pSurfaceSrc->cbBlock == pSurfaceDest->cbBlock);
1762 Assert(pSurfaceSrc->cxBlock == pSurfaceDest->cxBlock);
1763 Assert(pSurfaceSrc->cyBlock == pSurfaceDest->cyBlock);
1764
1765 uint32_t cBlocksX = (clipBox.w + pSurfaceSrc->cxBlock - 1) / pSurfaceSrc->cxBlock;
1766 uint32_t cBlocksY = (clipBox.h + pSurfaceSrc->cyBlock - 1) / pSurfaceSrc->cyBlock;
1767
1768 D3DLOCKED_RECT LockedSrcRect;
1769 if (!pSurfaceSrc->u.pSurface)
1770 {
1771 uint32_t u32BlockX = clipBox.srcx / pSurfaceSrc->cxBlock;
1772 uint32_t u32BlockY = clipBox.srcy / pSurfaceSrc->cyBlock;
1773 Assert(u32BlockX * pSurfaceSrc->cxBlock == clipBox.srcx);
1774 Assert(u32BlockY * pSurfaceSrc->cyBlock == clipBox.srcy);
1775
1776 LockedSrcRect.pBits = (uint8_t *)pMipmapLevelSrc->pSurfaceData +
1777 pMipmapLevelSrc->cbSurfacePitch * u32BlockY + pSurfaceSrc->cbBlock * u32BlockX;
1778 LockedSrcRect.Pitch = pMipmapLevelSrc->cbSurfacePitch;
1779 }
1780 else
1781 {
1782 /* Must flush the context's 3d pipeline to make sure all drawing is complete for the surface we're about to use. */
1783 vmsvga3dSurfaceFlush(pSurfaceSrc);
1784
1785 hr = pD3DSurf->LockRect(&LockedSrcRect, &RectSrc, D3DLOCK_READONLY);
1786 AssertMsgReturnStmt(hr == D3D_OK, ("LockRect failed with %x\n", hr), D3D_RELEASE(pD3DSurf), VERR_INTERNAL_ERROR);
1787 }
1788
1789 D3DLOCKED_RECT LockedDestRect;
1790 if (!pSurfaceDest->u.pSurface)
1791 {
1792 uint32_t u32BlockX = clipBox.x / pSurfaceDest->cxBlock;
1793 uint32_t u32BlockY = clipBox.y / pSurfaceDest->cyBlock;
1794 Assert(u32BlockX * pSurfaceDest->cxBlock == clipBox.x);
1795 Assert(u32BlockY * pSurfaceDest->cyBlock == clipBox.y);
1796
1797 LockedDestRect.pBits = (uint8_t *)pMipmapLevelDest->pSurfaceData +
1798 pMipmapLevelDest->cbSurfacePitch * u32BlockY + pSurfaceDest->cbBlock * u32BlockX;
1799 LockedDestRect.Pitch = pMipmapLevelDest->cbSurfacePitch;
1800 pSurfaceDest->fDirty = true;
1801 }
1802 else
1803 {
1804 /* Must flush the context's 3d pipeline to make sure all drawing is complete for the surface we're about to use. */
1805 vmsvga3dSurfaceFlush(pSurfaceDest);
1806
1807 hr = pD3DSurf->LockRect(&LockedDestRect, &RectDest, 0);
1808 AssertMsgReturnStmt(hr == D3D_OK, ("LockRect failed with %x\n", hr), D3D_RELEASE(pD3DSurf), VERR_INTERNAL_ERROR);
1809 }
1810
1811 uint8_t *pDest = (uint8_t *)LockedDestRect.pBits;
1812 const uint8_t *pSrc = (uint8_t *)LockedSrcRect.pBits;
1813 for (uint32_t j = 0; j < cBlocksY; ++j)
1814 {
1815 memcpy(pDest, pSrc, cBlocksX * pSurfaceSrc->cbBlock);
1816 pDest += LockedDestRect.Pitch;
1817 pSrc += LockedSrcRect.Pitch;
1818 }
1819
1820 if (pD3DSurf)
1821 {
1822 hr = pD3DSurf->UnlockRect();
1823 AssertMsgReturnStmt(hr == D3D_OK, ("Unlock failed with %x\n", hr), D3D_RELEASE(pD3DSurf), VERR_INTERNAL_ERROR);
1824 }
1825 }
1826
1827 D3D_RELEASE(pD3DSurf);
1828
1829 /* If the destination bounce texture has been used, then update the actual destination texture. */
1830 if ( pSurfaceDest->u.pTexture
1831 && pSurfaceDest->bounce.pTexture
1832 && ( pSurfaceDest->enmD3DResType == VMSVGA3D_D3DRESTYPE_TEXTURE
1833 || pSurfaceDest->enmD3DResType == VMSVGA3D_D3DRESTYPE_CUBE_TEXTURE))
1834 {
1835 AssertMsgReturn(pContext, ("Context is NULL\n"), VERR_INTERNAL_ERROR);
1836
1837 /* Copy the new content to the actual texture object. */
1838 HRESULT hr2 = D3D9UpdateTexture(pContext, pSurfaceDest);
1839 AssertMsg(hr2 == D3D_OK, ("UpdateTexture failed with %x\n", hr2)); RT_NOREF(hr2);
1840
1841 /* Track the UpdateTexture operation. */
1842 vmsvga3dSurfaceTrackUsage(pState, pContext, pSurfaceDest);
1843 }
1844 }
1845
1846 return VINF_SUCCESS;
1847}
1848
1849
1850/**
1851 * Create D3D/OpenGL texture object for the specified surface.
1852 *
1853 * Surfaces are created when needed.
1854 *
1855 * @param pThisCC The device context.
1856 * @param pContext The context.
1857 * @param idAssociatedContext Probably the same as pContext->id.
1858 * @param pSurface The surface to create the texture for.
1859 */
1860static DECLCALLBACK(int) vmsvga3dBackCreateTexture(PVGASTATECC pThisCC, PVMSVGA3DCONTEXT pContext, uint32_t idAssociatedContext,
1861 PVMSVGA3DSURFACE pSurface)
1862
1863{
1864 PVMSVGA3DSTATE pState = pThisCC->svga.p3dState;
1865 AssertReturn(pState, VERR_NO_MEMORY);
1866
1867 HRESULT hr;
1868
1869 LogFunc(("sid=%u\n", pSurface->id));
1870
1871 Assert(pSurface->hSharedObject == NULL);
1872 Assert(pSurface->u.pTexture == NULL);
1873 Assert(pSurface->bounce.pTexture == NULL);
1874 Assert(pSurface->enmD3DResType == VMSVGA3D_D3DRESTYPE_NONE);
1875
1876 const uint32_t cWidth = pSurface->paMipmapLevels[0].mipmapSize.width;
1877 const uint32_t cHeight = pSurface->paMipmapLevels[0].mipmapSize.height;
1878 const uint32_t cDepth = pSurface->paMipmapLevels[0].mipmapSize.depth;
1879 const uint32_t numMipLevels = pSurface->cLevels;
1880
1881 /*
1882 * Create D3D texture object.
1883 */
1884 if (pSurface->f.surfaceFlags & SVGA3D_SURFACE_CUBEMAP)
1885 {
1886 Assert(pSurface->cFaces == 6);
1887 Assert(cWidth == cHeight);
1888 Assert(cDepth == 1);
1889
1890 hr = pContext->pDevice->CreateCubeTexture(cWidth,
1891 numMipLevels,
1892 pSurface->fUsageD3D,
1893 pSurface->formatD3D,
1894 D3DPOOL_DEFAULT,
1895 &pSurface->u.pCubeTexture,
1896 &pSurface->hSharedObject);
1897 if (hr == D3D_OK)
1898 {
1899 /* Create another texture object to serve as a bounce buffer as the above texture surface can't be locked. */
1900 hr = pContext->pDevice->CreateCubeTexture(cWidth,
1901 numMipLevels,
1902 (pSurface->fUsageD3D & ~D3DUSAGE_RENDERTARGET) | D3DUSAGE_DYNAMIC /* Lockable */,
1903 pSurface->formatD3D,
1904 D3DPOOL_SYSTEMMEM,
1905 &pSurface->bounce.pCubeTexture,
1906 NULL);
1907 AssertMsgReturnStmt(hr == D3D_OK,
1908 ("CreateCubeTexture (systemmem) failed with %x\n", hr),
1909 D3D_RELEASE(pSurface->u.pCubeTexture),
1910 VERR_INTERNAL_ERROR);
1911 }
1912 else
1913 {
1914 Log(("Format not accepted -> try old method\n"));
1915 /* The format was probably not accepted; fall back to our old mode. */
1916 hr = pContext->pDevice->CreateCubeTexture(cWidth,
1917 numMipLevels,
1918 (pSurface->fUsageD3D & ~D3DUSAGE_RENDERTARGET) | D3DUSAGE_DYNAMIC /* Lockable */,
1919 pSurface->formatD3D,
1920 D3DPOOL_DEFAULT,
1921 &pSurface->u.pCubeTexture,
1922 &pSurface->hSharedObject);
1923 AssertMsgReturn(hr == D3D_OK, ("CreateCubeTexture (fallback) failed with %x\n", hr), VERR_INTERNAL_ERROR);
1924 }
1925
1926 pSurface->enmD3DResType = VMSVGA3D_D3DRESTYPE_CUBE_TEXTURE;
1927 }
1928 else if ( pSurface->formatD3D == D3DFMT_D24S8
1929 || pSurface->formatD3D == D3DFMT_D24X8
1930 || pSurface->formatD3D == D3DFMT_D32
1931 || pSurface->formatD3D == D3DFMT_D16)
1932 {
1933 Assert(pSurface->cFaces == 1);
1934 Assert(pSurface->cLevels == 1);
1935 Assert(cDepth == 1);
1936
1937 /* Use the INTZ format for a depth/stencil surface that will be used as a texture */
1938 hr = pContext->pDevice->CreateTexture(cWidth,
1939 cHeight,
1940 1, /* mip levels */
1941 D3DUSAGE_DEPTHSTENCIL,
1942 FOURCC_INTZ,
1943 D3DPOOL_DEFAULT,
1944 &pSurface->u.pTexture,
1945 &pSurface->hSharedObject /* might result in poor performance */);
1946 if ( hr == D3D_OK
1947 && ( pSurface->formatD3D == D3DFMT_D24S8
1948 || pSurface->formatD3D == D3DFMT_D24X8))
1949 {
1950 /* Create another texture object to serve as a bounce buffer as the
1951 * D3DFMT_D24S8 and D3DFMT_D24X8 surface can't be locked apparently (from testing).
1952 */
1953 hr = pContext->pDevice->CreateTexture(cWidth,
1954 cHeight,
1955 1, /* mip levels */
1956 D3DUSAGE_DYNAMIC /* Lockable */,
1957 FOURCC_INTZ,
1958 D3DPOOL_SYSTEMMEM,
1959 &pSurface->bounce.pTexture,
1960 NULL);
1961
1962 AssertMsgReturnStmt(hr == D3D_OK,
1963 ("CreateTexture (systemmem) failed with %x\n", hr),
1964 D3D_RELEASE(pSurface->u.pTexture),
1965 VERR_INTERNAL_ERROR);
1966 }
1967
1968 AssertMsgReturn(hr == D3D_OK, ("CreateTexture INTZ failed with %x\n", hr), VERR_INTERNAL_ERROR);
1969
1970 pSurface->fStencilAsTexture = true;
1971 pSurface->enmD3DResType = VMSVGA3D_D3DRESTYPE_TEXTURE;
1972 }
1973 else
1974 {
1975 if (cDepth > 1)
1976 {
1977 hr = pContext->pDevice->CreateVolumeTexture(cWidth,
1978 cHeight,
1979 cDepth,
1980 numMipLevels,
1981 pSurface->fUsageD3D,
1982 pSurface->formatD3D,
1983 D3DPOOL_DEFAULT,
1984 &pSurface->u.pVolumeTexture,
1985 &pSurface->hSharedObject);
1986 if (hr == D3D_OK)
1987 {
1988 /* Create another texture object to serve as a bounce buffer as the above texture surface can't be locked. */
1989 hr = pContext->pDevice->CreateVolumeTexture(cWidth,
1990 cHeight,
1991 cDepth,
1992 numMipLevels,
1993 (pSurface->fUsageD3D & ~D3DUSAGE_RENDERTARGET) | D3DUSAGE_DYNAMIC /* Lockable */,
1994 pSurface->formatD3D,
1995 D3DPOOL_SYSTEMMEM,
1996 &pSurface->bounce.pVolumeTexture,
1997 NULL);
1998 AssertMsgReturnStmt(hr == D3D_OK,
1999 ("CreateVolumeTexture (systemmem) failed with %x\n", hr),
2000 D3D_RELEASE(pSurface->u.pVolumeTexture),
2001 VERR_INTERNAL_ERROR);
2002 }
2003 else
2004 {
2005 Log(("Format not accepted -> try old method\n"));
2006 /* The format was probably not accepted; fall back to our old mode. */
2007 hr = pContext->pDevice->CreateVolumeTexture(cWidth,
2008 cHeight,
2009 cDepth,
2010 numMipLevels,
2011 (pSurface->fUsageD3D & ~D3DUSAGE_RENDERTARGET) | D3DUSAGE_DYNAMIC /* Lockable */,
2012 pSurface->formatD3D,
2013 D3DPOOL_DEFAULT,
2014 &pSurface->u.pVolumeTexture,
2015 &pSurface->hSharedObject);
2016 AssertMsgReturn(hr == D3D_OK, ("CreateVolumeTexture (fallback) failed with %x\n", hr), VERR_INTERNAL_ERROR);
2017 }
2018
2019 pSurface->enmD3DResType = VMSVGA3D_D3DRESTYPE_VOLUME_TEXTURE;
2020 }
2021 else
2022 {
2023 Assert(pSurface->cFaces == 1);
2024
2025 hr = pContext->pDevice->CreateTexture(cWidth,
2026 cHeight,
2027 numMipLevels,
2028 pSurface->fUsageD3D | D3DUSAGE_RENDERTARGET /* required for use as a StretchRect destination */,
2029 pSurface->formatD3D,
2030 D3DPOOL_DEFAULT,
2031 &pSurface->u.pTexture,
2032 &pSurface->hSharedObject);
2033 if (hr == D3D_OK)
2034 {
2035 /* Create another texture object to serve as a bounce buffer as the above texture surface can't be locked. */
2036 hr = pContext->pDevice->CreateTexture(cWidth,
2037 cHeight,
2038 numMipLevels,
2039 (pSurface->fUsageD3D & ~D3DUSAGE_RENDERTARGET) | D3DUSAGE_DYNAMIC /* Lockable */,
2040 pSurface->formatD3D,
2041 D3DPOOL_SYSTEMMEM,
2042 &pSurface->bounce.pTexture,
2043 NULL);
2044
2045 AssertMsgReturn(hr == D3D_OK, ("CreateTexture (systemmem) failed with %x\n", hr), VERR_INTERNAL_ERROR);
2046
2047 if (pSurface->formatD3D != pSurface->d3dfmtRequested)
2048 {
2049 /* Create a staging texture/render target for format conversion. */
2050 hr = pContext->pDevice->CreateTexture(cWidth,
2051 cHeight,
2052 numMipLevels,
2053 pSurface->fUsageD3D | D3DUSAGE_RENDERTARGET,
2054 pSurface->formatD3D,
2055 D3DPOOL_DEFAULT,
2056 &pSurface->emulated.pTexture,
2057 NULL);
2058 AssertMsgReturn(hr == D3D_OK, ("CreateTexture (emulated) failed with %x\n", hr), VERR_INTERNAL_ERROR);
2059 }
2060 }
2061 else
2062 {
2063 Log(("Format not accepted (%x) -> try old method\n", hr));
2064 /* The format was probably not accepted; fall back to our old mode. */
2065 hr = pContext->pDevice->CreateTexture(cWidth,
2066 cHeight,
2067 numMipLevels,
2068 (pSurface->fUsageD3D & ~D3DUSAGE_RENDERTARGET) | D3DUSAGE_DYNAMIC /* Lockable */,
2069 pSurface->formatD3D,
2070 D3DPOOL_DEFAULT,
2071 &pSurface->u.pTexture,
2072 &pSurface->hSharedObject /* might result in poor performance */);
2073 AssertMsgReturn(hr == D3D_OK, ("CreateTexture failed with %x\n", hr), VERR_INTERNAL_ERROR);
2074 }
2075
2076 pSurface->enmD3DResType = VMSVGA3D_D3DRESTYPE_TEXTURE;
2077 }
2078 }
2079
2080 Assert(hr == D3D_OK);
2081
2082 if (pSurface->autogenFilter != SVGA3D_TEX_FILTER_NONE)
2083 {
2084 /* Set the mip map generation filter settings. */
2085 IDirect3DBaseTexture9 *pBaseTexture;
2086 if (pSurface->enmD3DResType == VMSVGA3D_D3DRESTYPE_VOLUME_TEXTURE)
2087 pBaseTexture = pSurface->u.pVolumeTexture;
2088 else if (pSurface->enmD3DResType == VMSVGA3D_D3DRESTYPE_CUBE_TEXTURE)
2089 pBaseTexture = pSurface->u.pCubeTexture;
2090 else
2091 pBaseTexture = pSurface->u.pTexture;
2092 hr = pBaseTexture->SetAutoGenFilterType((D3DTEXTUREFILTERTYPE)pSurface->autogenFilter);
2093 AssertMsg(hr == D3D_OK, ("vmsvga3dBackCreateTexture: SetAutoGenFilterType failed with %x\n", hr));
2094 }
2095
2096 /*
2097 * Always initialize all mipmap levels using the in memory data
2098 * to make sure that the just created texture has the up-to-date content.
2099 * The OpenGL backend does this too.
2100 */
2101 Log(("vmsvga3dBackCreateTexture: sync texture\n"));
2102
2103 if (pSurface->enmD3DResType == VMSVGA3D_D3DRESTYPE_VOLUME_TEXTURE)
2104 {
2105 IDirect3DVolumeTexture9 *pVolumeTexture = pSurface->bounce.pVolumeTexture ?
2106 pSurface->bounce.pVolumeTexture :
2107 pSurface->u.pVolumeTexture;
2108
2109 for (uint32_t i = 0; i < numMipLevels; ++i)
2110 {
2111 D3DLOCKED_BOX LockedVolume;
2112 hr = pVolumeTexture->LockBox(i, &LockedVolume, NULL, D3DLOCK_DISCARD);
2113 AssertMsgBreak(hr == D3D_OK, ("LockBox failed with %x\n", hr));
2114
2115 PVMSVGA3DMIPMAPLEVEL pMipLevel = &pSurface->paMipmapLevels[i];
2116
2117 LogFunc(("sync volume texture mipmap level %d (pitch row %x vs %x, slice %x vs %x)\n",
2118 i, LockedVolume.RowPitch, pMipLevel->cbSurfacePitch, LockedVolume.SlicePitch, pMipLevel->cbSurfacePlane));
2119
2120
2121 uint8_t *pDst = (uint8_t *)LockedVolume.pBits;
2122 const uint8_t *pSrc = (uint8_t *)pMipLevel->pSurfaceData;
2123 for (uint32_t d = 0; d < cDepth; ++d)
2124 {
2125 uint8_t *pRowDst = pDst;
2126 const uint8_t *pRowSrc = pSrc;
2127 for (uint32_t h = 0; h < pMipLevel->cBlocksY; ++h)
2128 {
2129 memcpy(pRowDst, pRowSrc, pMipLevel->cbSurfacePitch);
2130 pRowDst += LockedVolume.RowPitch;
2131 pRowSrc += pMipLevel->cbSurfacePitch;
2132 }
2133 pDst += LockedVolume.SlicePitch;
2134 pSrc += pMipLevel->cbSurfacePlane;
2135 }
2136
2137 hr = pVolumeTexture->UnlockBox(i);
2138 AssertMsgBreak(hr == D3D_OK, ("UnlockBox failed with %x\n", hr));
2139
2140 pMipLevel->fDirty = false;
2141 }
2142 }
2143 else if (pSurface->enmD3DResType == VMSVGA3D_D3DRESTYPE_CUBE_TEXTURE)
2144 {
2145 IDirect3DCubeTexture9 *pCubeTexture = pSurface->bounce.pCubeTexture ?
2146 pSurface->bounce.pCubeTexture :
2147 pSurface->u.pCubeTexture;
2148
2149 for (uint32_t iFace = 0; iFace < 6; ++iFace)
2150 {
2151 const D3DCUBEMAP_FACES Face = vmsvga3dCubemapFaceFromIndex(iFace);
2152
2153 for (uint32_t i = 0; i < numMipLevels; ++i)
2154 {
2155 D3DLOCKED_RECT LockedRect;
2156 hr = pCubeTexture->LockRect(Face,
2157 i, /* texture level */
2158 &LockedRect,
2159 NULL, /* entire texture */
2160 0);
2161 AssertMsgBreak(hr == D3D_OK, ("LockRect failed with %x\n", hr));
2162
2163 PVMSVGA3DMIPMAPLEVEL pMipLevel = &pSurface->paMipmapLevels[iFace * numMipLevels + i];
2164
2165 LogFunc(("sync texture face %d mipmap level %d (pitch %x vs %x)\n",
2166 iFace, i, LockedRect.Pitch, pMipLevel->cbSurfacePitch));
2167
2168 uint8_t *pDest = (uint8_t *)LockedRect.pBits;
2169 const uint8_t *pSrc = (uint8_t *)pMipLevel->pSurfaceData;
2170 for (uint32_t j = 0; j < pMipLevel->cBlocksY; ++j)
2171 {
2172 memcpy(pDest, pSrc, pMipLevel->cbSurfacePitch);
2173
2174 pDest += LockedRect.Pitch;
2175 pSrc += pMipLevel->cbSurfacePitch;
2176 }
2177
2178 hr = pCubeTexture->UnlockRect(Face, i /* texture level */);
2179 AssertMsgBreak(hr == D3D_OK, ("UnlockRect failed with %x\n", hr));
2180
2181 pMipLevel->fDirty = false;
2182 }
2183
2184 if (hr != D3D_OK)
2185 break;
2186 }
2187
2188 if (hr != D3D_OK)
2189 {
2190 D3D_RELEASE(pSurface->bounce.pCubeTexture);
2191 D3D_RELEASE(pSurface->u.pCubeTexture);
2192 return VERR_INTERNAL_ERROR;
2193 }
2194 }
2195 else if (pSurface->enmD3DResType == VMSVGA3D_D3DRESTYPE_TEXTURE)
2196 {
2197 IDirect3DTexture9 *pTexture;
2198 if (pSurface->bounce.pTexture)
2199 pTexture = pSurface->bounce.pTexture;
2200 else if (pSurface->formatD3D != pSurface->d3dfmtRequested)
2201 pTexture = pSurface->emulated.pTexture;
2202 else
2203 pTexture = pSurface->u.pTexture;
2204
2205 for (uint32_t i = 0; i < numMipLevels; ++i)
2206 {
2207 D3DLOCKED_RECT LockedRect;
2208
2209 hr = pTexture->LockRect(i, /* texture level */
2210 &LockedRect,
2211 NULL, /* entire texture */
2212 0);
2213
2214 AssertMsgReturn(hr == D3D_OK, ("vmsvga3dBackCreateTexture: LockRect failed with %x\n", hr), VERR_INTERNAL_ERROR);
2215
2216 LogFunc(("sync texture mipmap level %d (pitch %x vs %x)\n", i, LockedRect.Pitch, pSurface->paMipmapLevels[i].cbSurfacePitch));
2217
2218 uint8_t *pDest = (uint8_t *)LockedRect.pBits;
2219 const uint8_t *pSrc = (uint8_t *)pSurface->paMipmapLevels[i].pSurfaceData;
2220 for (uint32_t j = 0; j < pSurface->paMipmapLevels[i].cBlocksY; ++j)
2221 {
2222 memcpy(pDest, pSrc, pSurface->paMipmapLevels[i].cbSurfacePitch);
2223
2224 pDest += LockedRect.Pitch;
2225 pSrc += pSurface->paMipmapLevels[i].cbSurfacePitch;
2226 }
2227
2228 hr = pTexture->UnlockRect(i /* texture level */);
2229 AssertMsgReturn(hr == D3D_OK, ("vmsvga3dBackCreateTexture: UnlockRect failed with %x\n", hr), VERR_INTERNAL_ERROR);
2230
2231 pSurface->paMipmapLevels[i].fDirty = false;
2232 }
2233 }
2234 else
2235 {
2236 AssertMsgFailedReturn(("enmD3DResType not expected %d\n", pSurface->enmD3DResType), VERR_INTERNAL_ERROR);
2237 }
2238
2239 if (pSurface->bounce.pTexture)
2240 {
2241 Log(("vmsvga3dBackCreateTexture: sync dirty texture from bounce buffer\n"));
2242 if (pSurface->enmD3DResType == VMSVGA3D_D3DRESTYPE_VOLUME_TEXTURE)
2243 hr = pContext->pDevice->UpdateTexture(pSurface->bounce.pVolumeTexture, pSurface->u.pVolumeTexture);
2244 else
2245 hr = D3D9UpdateTexture(pContext, pSurface);
2246 AssertMsgReturn(hr == D3D_OK, ("UpdateTexture failed with %x\n", hr), VERR_INTERNAL_ERROR);
2247
2248 /* We will now use the bounce texture for all memory accesses, so free our surface memory buffer. */
2249 for (uint32_t i = 0; i < pSurface->cLevels; i++)
2250 {
2251 RTMemFree(pSurface->paMipmapLevels[i].pSurfaceData);
2252 pSurface->paMipmapLevels[i].pSurfaceData = NULL;
2253 }
2254
2255 /* Track the UpdateTexture operation. */
2256 vmsvga3dSurfaceTrackUsage(pState, pContext, pSurface);
2257 }
2258 pSurface->fDirty = false;
2259
2260 Assert(pSurface->enmD3DResType != VMSVGA3D_D3DRESTYPE_NONE);
2261
2262 pSurface->f.surfaceFlags |= SVGA3D_SURFACE_HINT_TEXTURE;
2263 pSurface->idAssociatedContext = idAssociatedContext;
2264 return VINF_SUCCESS;
2265}
2266
2267
2268/**
2269 * Backend worker for implementing SVGA_3D_CMD_SURFACE_STRETCHBLT.
2270 *
2271 * @returns VBox status code.
2272 * @param pThis The VGA device instance.
2273 * @param pState The VMSVGA3d state.
2274 * @param pDstSurface The destination host surface.
2275 * @param uDstFace The destination face (valid).
2276 * @param uDstMipmap The destination mipmap level (valid).
2277 * @param pDstBox The destination box.
2278 * @param pSrcSurface The source host surface.
2279 * @param uSrcFace The destination face (valid).
2280 * @param uSrcMipmap The source mimap level (valid).
2281 * @param pSrcBox The source box.
2282 * @param enmMode The strecht blt mode .
2283 * @param pContext The VMSVGA3d context (already current for OGL).
2284 */
2285static DECLCALLBACK(int) vmsvga3dBackSurfaceStretchBlt(PVGASTATE pThis, PVMSVGA3DSTATE pState,
2286 PVMSVGA3DSURFACE pDstSurface, uint32_t uDstFace, uint32_t uDstMipmap, SVGA3dBox const *pDstBox,
2287 PVMSVGA3DSURFACE pSrcSurface, uint32_t uSrcFace, uint32_t uSrcMipmap, SVGA3dBox const *pSrcBox,
2288 SVGA3dStretchBltMode enmMode, PVMSVGA3DCONTEXT pContext)
2289{
2290 RT_NOREF(pThis);
2291
2292 HRESULT hr;
2293 int rc;
2294
2295 AssertReturn(pSrcSurface->enmD3DResType != VMSVGA3D_D3DRESTYPE_VOLUME_TEXTURE, VERR_NOT_IMPLEMENTED);
2296 AssertReturn(pDstSurface->enmD3DResType != VMSVGA3D_D3DRESTYPE_VOLUME_TEXTURE, VERR_NOT_IMPLEMENTED);
2297
2298 /* Flush the drawing pipeline for this surface as it could be used in a shared context. */
2299 vmsvga3dSurfaceFlush(pSrcSurface);
2300 vmsvga3dSurfaceFlush(pDstSurface);
2301
2302 RECT RectSrc;
2303 RectSrc.left = pSrcBox->x;
2304 RectSrc.top = pSrcBox->y;
2305 RectSrc.right = pSrcBox->x + pSrcBox->w; /* exclusive */
2306 RectSrc.bottom = pSrcBox->y + pSrcBox->h; /* exclusive */
2307 Assert(!pSrcBox->z);
2308
2309 RECT RectDst;
2310 RectDst.left = pDstBox->x;
2311 RectDst.top = pDstBox->y;
2312 RectDst.right = pDstBox->x + pDstBox->w; /* exclusive */
2313 RectDst.bottom = pDstBox->y + pDstBox->h; /* exclusive */
2314 Assert(!pDstBox->z);
2315
2316 IDirect3DSurface9 *pSrc;
2317 rc = vmsvga3dGetD3DSurface(pState, pContext, pSrcSurface, uSrcFace, uSrcMipmap, false, &pSrc);
2318 AssertRCReturn(rc, rc);
2319
2320 IDirect3DSurface9 *pDst;
2321 rc = vmsvga3dGetD3DSurface(pState, pContext, pDstSurface, uDstFace, uDstMipmap, false, &pDst);
2322 AssertRCReturn(rc, rc);
2323
2324 D3DTEXTUREFILTERTYPE moded3d;
2325 switch (enmMode)
2326 {
2327 case SVGA3D_STRETCH_BLT_POINT:
2328 moded3d = D3DTEXF_POINT;
2329 break;
2330
2331 case SVGA3D_STRETCH_BLT_LINEAR:
2332 moded3d = D3DTEXF_LINEAR;
2333 break;
2334
2335 default:
2336 AssertFailed();
2337 moded3d = D3DTEXF_NONE;
2338 break;
2339 }
2340
2341 hr = pContext->pDevice->StretchRect(pSrc, &RectSrc, pDst, &RectDst, moded3d);
2342
2343 D3D_RELEASE(pDst);
2344 D3D_RELEASE(pSrc);
2345
2346 AssertMsgReturn(hr == D3D_OK, ("StretchRect failed with %x\n", hr), VERR_INTERNAL_ERROR);
2347
2348 /* Track the StretchRect operation. */
2349 vmsvga3dSurfaceTrackUsage(pState, pContext, pSrcSurface);
2350 vmsvga3dSurfaceTrackUsage(pState, pContext, pDstSurface);
2351
2352 return VINF_SUCCESS;
2353}
2354
2355
2356/**
2357 * Backend worker for implementing SVGA_3D_CMD_SURFACE_DMA that copies one box.
2358 *
2359 * @returns Failure status code or @a rc.
2360 * @param pThis The shared VGA/VMSVGA instance data.
2361 * @param pThisCC The VGA/VMSVGA state for ring-3.
2362 * @param pState The VMSVGA3d state.
2363 * @param pSurface The host surface.
2364 * @param pMipLevel Mipmap level. The caller knows it already.
2365 * @param uHostFace The host face (valid).
2366 * @param uHostMipmap The host mipmap level (valid).
2367 * @param GuestPtr The guest pointer.
2368 * @param cbGuestPitch The guest pitch.
2369 * @param transfer The transfer direction.
2370 * @param pBox The box to copy (clipped, valid, except for guest's srcx, srcy, srcz).
2371 * @param pContext The context (for OpenGL).
2372 * @param rc The current rc for all boxes.
2373 * @param iBox The current box number (for Direct 3D).
2374 */
2375static DECLCALLBACK(int) vmsvga3dBackSurfaceDMACopyBox(PVGASTATE pThis, PVGASTATECC pThisCC, PVMSVGA3DSTATE pState, PVMSVGA3DSURFACE pSurface,
2376 PVMSVGA3DMIPMAPLEVEL pMipLevel, uint32_t uHostFace, uint32_t uHostMipmap,
2377 SVGAGuestPtr GuestPtr, uint32_t cbGuestPitch, SVGA3dTransferType transfer,
2378 SVGA3dCopyBox const *pBox, PVMSVGA3DCONTEXT pContext, int rc, int iBox)
2379{
2380 HRESULT hr = D3D_OK;
2381 const DWORD dwFlags = transfer == SVGA3D_READ_HOST_VRAM ? D3DLOCK_READONLY : 0;
2382
2383 AssertReturn(pSurface->enmD3DResType != VMSVGA3D_D3DRESTYPE_VOLUME_TEXTURE, VERR_NOT_IMPLEMENTED);
2384
2385 const bool fTexture = pSurface->enmD3DResType == VMSVGA3D_D3DRESTYPE_TEXTURE
2386 || pSurface->enmD3DResType == VMSVGA3D_D3DRESTYPE_CUBE_TEXTURE;
2387 if ( pSurface->enmD3DResType == VMSVGA3D_D3DRESTYPE_SURFACE
2388 || fTexture)
2389 {
2390 rc = vmsvga3dContextFromCid(pState, pSurface->idAssociatedContext, &pContext);
2391 AssertRCReturn(rc, rc);
2392 AssertReturn(pContext->pDevice, VERR_INTERNAL_ERROR);
2393
2394 /* Get the surface involved in the transfer. */
2395 IDirect3DSurface9 *pSurf;
2396 rc = vmsvga3dGetD3DSurface(pState, pContext, pSurface, uHostFace, uHostMipmap, true, &pSurf);
2397 AssertRCReturn(rc, rc);
2398
2399 if (transfer == SVGA3D_READ_HOST_VRAM)
2400 {
2401 /* Texture data is copied to the host VRAM.
2402 * Update the 'bounce' texture if necessary.
2403 */
2404 if ( fTexture
2405 && pSurface->bounce.pTexture
2406 && iBox == 0 /* only the first time */)
2407 {
2408 /** @todo inefficient for VRAM buffers!! */
2409 if (RT_BOOL(pSurface->f.surfaceFlags & SVGA3D_SURFACE_HINT_RENDERTARGET))
2410 {
2411 /* Copy the texture mipmap level to the bounce texture. */
2412 hr = D3D9GetRenderTargetData(pContext, pSurface, uHostFace, uHostMipmap);
2413 AssertMsgReturn(hr == D3D_OK, ("D3D9GetRenderTargetData failed with %x\n", hr), VERR_INTERNAL_ERROR);
2414 }
2415 }
2416 }
2417
2418 uint32_t const u32GuestBlockX = pBox->srcx / pSurface->cxBlock;
2419 uint32_t const u32GuestBlockY = pBox->srcy / pSurface->cyBlock;
2420 Assert(u32GuestBlockX * pSurface->cxBlock == pBox->srcx);
2421 Assert(u32GuestBlockY * pSurface->cyBlock == pBox->srcy);
2422 uint32_t const cBlocksX = (pBox->w + pSurface->cxBlock - 1) / pSurface->cxBlock;
2423 uint32_t const cBlocksY = (pBox->h + pSurface->cyBlock - 1) / pSurface->cyBlock;
2424 AssertMsgReturn(cBlocksX && cBlocksY, ("Empty box %dx%d\n", pBox->w, pBox->h), VERR_INTERNAL_ERROR);
2425
2426 /* vmsvgaR3GmrTransfer verifies uGuestOffset.
2427 * srcx(u32GuestBlockX) and srcy(u32GuestBlockY) have been verified in vmsvga3dSurfaceDMA
2428 * to not cause 32 bit overflow when multiplied by cbBlock and cbGuestPitch.
2429 */
2430 uint64_t const uGuestOffset = u32GuestBlockX * pSurface->cbBlock + u32GuestBlockY * cbGuestPitch;
2431 AssertReturn(uGuestOffset < UINT32_MAX, VERR_INVALID_PARAMETER);
2432
2433 RECT Rect;
2434 Rect.left = pBox->x;
2435 Rect.top = pBox->y;
2436 Rect.right = pBox->x + pBox->w; /* exclusive */
2437 Rect.bottom = pBox->y + pBox->h; /* exclusive */
2438
2439 D3DLOCKED_RECT LockedRect;
2440 hr = pSurf->LockRect(&LockedRect, &Rect, dwFlags);
2441 AssertMsgReturn(hr == D3D_OK, ("LockRect failed with %x\n", hr), VERR_INTERNAL_ERROR);
2442
2443 LogFunc(("Lock sid=%u %s(bounce=%d) memory for rectangle (%d,%d)(%d,%d)\n",
2444 pSurface->id, fTexture ? "TEXTURE " : "", RT_BOOL(pSurface->bounce.pTexture),
2445 Rect.left, Rect.top, Rect.right, Rect.bottom));
2446
2447 /* Prepare parameters for vmsvgaR3GmrTransfer, which needs the host buffer address, size
2448 * and offset of the first scanline.
2449 */
2450 uint32_t const cbLockedBuf = RT_ABS(LockedRect.Pitch) * cBlocksY;
2451 uint8_t *pu8LockedBuf = (uint8_t *)LockedRect.pBits;
2452 if (LockedRect.Pitch < 0)
2453 pu8LockedBuf -= cbLockedBuf + LockedRect.Pitch;
2454 uint32_t const offLockedBuf = (uint32_t)((uintptr_t)LockedRect.pBits - (uintptr_t)pu8LockedBuf);
2455
2456 rc = vmsvgaR3GmrTransfer(pThis,
2457 pThisCC,
2458 transfer,
2459 pu8LockedBuf,
2460 cbLockedBuf,
2461 offLockedBuf,
2462 LockedRect.Pitch,
2463 GuestPtr,
2464 (uint32_t)uGuestOffset,
2465 cbGuestPitch,
2466 cBlocksX * pSurface->cbBlock,
2467 cBlocksY);
2468 AssertRC(rc);
2469
2470 Log4(("first line:\n%.*Rhxd\n", cBlocksX * pSurface->cbBlock, LockedRect.pBits));
2471
2472 hr = pSurf->UnlockRect();
2473
2474 D3D_RELEASE(pSurf);
2475
2476 AssertMsgReturn(hr == D3D_OK, ("UnlockRect failed with %x\n", hr), VERR_INTERNAL_ERROR);
2477
2478 if (transfer == SVGA3D_WRITE_HOST_VRAM)
2479 {
2480 /* Data is copied to the texture. Copy updated 'bounce' texture to the actual if necessary.
2481 */
2482 /// @todo for the last iBox only.
2483 if ( fTexture
2484 && pSurface->bounce.pTexture)
2485 {
2486 LogFunc(("Sync texture from bounce buffer\n"));
2487
2488 /* Copy the new contents to the actual texture object. */
2489 hr = D3D9UpdateTexture(pContext, pSurface);
2490 AssertMsgReturn(hr == D3D_OK, ("UpdateTexture failed with %x\n", hr), VERR_INTERNAL_ERROR);
2491
2492 /* Track the copy operation. */
2493 vmsvga3dSurfaceTrackUsage(pState, pContext, pSurface);
2494 }
2495 }
2496 }
2497 else if ( pSurface->enmD3DResType == VMSVGA3D_D3DRESTYPE_VERTEX_BUFFER
2498 || pSurface->enmD3DResType == VMSVGA3D_D3DRESTYPE_INDEX_BUFFER)
2499 {
2500 /*
2501 * Mesa SVGA driver can use the same buffer either for vertex or index data.
2502 * But D3D distinguishes between index and vertex buffer objects.
2503 * Therefore it should be possible to switch the buffer type on the fly.
2504 *
2505 * Always save the data to the memory buffer in pSurface->paMipmapLevels and,
2506 * if necessary, recreate the corresponding D3D object with the data.
2507 */
2508
2509 /* Buffers are uncompressed. */
2510 AssertReturn(pSurface->cxBlock == 1 && pSurface->cyBlock == 1, VERR_INTERNAL_ERROR);
2511
2512 /* Caller already clipped pBox and buffers are 1-dimensional. */
2513 Assert(pBox->y == 0 && pBox->h == 1 && pBox->z == 0 && pBox->d == 1);
2514
2515 /* The caller has already updated pMipLevel->pSurfaceData, see VMSVGA3DSURFACE_NEEDS_DATA. */
2516
2517#ifdef LOG_ENABLED
2518 uint32_t const offHst = pBox->x * pSurface->cbBlock;
2519 uint32_t const cbWidth = pBox->w * pSurface->cbBlock;
2520 Log4(("Buffer updated at [0x%x;0x%x):\n%.*Rhxd\n", offHst, offHst + cbWidth, cbWidth, (uint8_t *)pMipLevel->pSurfaceData + offHst));
2521#endif
2522
2523 /* Do not bother to copy the data to the D3D resource now. vmsvga3dDrawPrimitives will do that.
2524 * The SVGA driver may use the same surface for both index and vertex data.
2525 */
2526
2527 /* Make sure that vmsvga3dDrawPrimitives fetches the new data. */
2528 pMipLevel->fDirty = true;
2529 pSurface->fDirty = true;
2530 }
2531 else
2532 {
2533 AssertMsgFailed(("Unsupported surface flags 0x%08X, type %d\n", pSurface->f.s.surface1Flags, pSurface->enmD3DResType));
2534 }
2535
2536 return rc;
2537}
2538
2539static DECLCALLBACK(int) vmsvga3dBackGenerateMipmaps(PVGASTATECC pThisCC, uint32_t sid, SVGA3dTextureFilter filter)
2540{
2541 PVMSVGA3DSTATE pState = pThisCC->svga.p3dState;
2542 HRESULT hr;
2543
2544 PVMSVGA3DSURFACE pSurface;
2545 int rc = vmsvga3dSurfaceFromSid(pState, sid, &pSurface);
2546 AssertRCReturn(rc, rc);
2547 AssertReturn(pSurface->idAssociatedContext != SVGA3D_INVALID_ID, VERR_INTERNAL_ERROR);
2548
2549 Assert(filter != SVGA3D_TEX_FILTER_FLATCUBIC);
2550 Assert(filter != SVGA3D_TEX_FILTER_GAUSSIANCUBIC);
2551 pSurface->autogenFilter = filter;
2552
2553 Log(("vmsvga3dGenerateMipmaps: sid=%u filter=%d\n", sid, filter));
2554
2555 if (!pSurface->u.pSurface)
2556 {
2557 /** @todo stricter checks for associated context */
2558 uint32_t const cid = pSurface->idAssociatedContext;
2559
2560 PVMSVGA3DCONTEXT pContext;
2561 rc = vmsvga3dContextFromCid(pState, cid, &pContext);
2562 AssertRCReturn(rc, rc);
2563 AssertReturn(pContext->pDevice, VERR_INTERNAL_ERROR);
2564
2565 /* Unknown surface type; turn it into a texture. */
2566 LogFunc(("unknown src surface sid=%u type=%d format=%d -> create texture\n", sid, pSurface->f.s.surface1Flags, pSurface->format));
2567 rc = vmsvga3dBackCreateTexture(pThisCC, pContext, cid, pSurface);
2568 AssertRCReturn(rc, rc);
2569 }
2570
2571 AssertReturn( pSurface->enmD3DResType == VMSVGA3D_D3DRESTYPE_TEXTURE
2572 || pSurface->enmD3DResType == VMSVGA3D_D3DRESTYPE_CUBE_TEXTURE
2573 || pSurface->enmD3DResType == VMSVGA3D_D3DRESTYPE_VOLUME_TEXTURE,
2574 VERR_INVALID_PARAMETER);
2575
2576 hr = pSurface->u.pTexture->SetAutoGenFilterType((D3DTEXTUREFILTERTYPE)filter);
2577 AssertMsg(hr == D3D_OK, ("SetAutoGenFilterType failed with %x\n", hr));
2578
2579 /* Generate the mip maps. */
2580 pSurface->u.pTexture->GenerateMipSubLevels();
2581
2582 return VINF_SUCCESS;
2583}
2584
2585
2586/**
2587 * Create a new 3d context
2588 *
2589 * @returns VBox status code.
2590 * @param pThisCC The VGA/VMSVGA state for ring-3.
2591 * @param cid Context id
2592 */
2593static DECLCALLBACK(int) vmsvga3dBackContextDefine(PVGASTATECC pThisCC, uint32_t cid)
2594{
2595 int rc;
2596 PVMSVGA3DCONTEXT pContext;
2597 HRESULT hr;
2598 PVMSVGA3DSTATE pState = pThisCC->svga.p3dState;
2599
2600 Log(("vmsvga3dContextDefine id %x\n", cid));
2601
2602 AssertReturn(pState, VERR_NO_MEMORY);
2603 AssertReturn(cid < SVGA3D_MAX_CONTEXT_IDS, VERR_INVALID_PARAMETER);
2604
2605 if (cid >= pState->cContexts)
2606 {
2607 /* Grow the array. */
2608 uint32_t cNew = RT_ALIGN(cid + 15, 16);
2609 void *pvNew = RTMemRealloc(pState->papContexts, sizeof(pState->papContexts[0]) * cNew);
2610 AssertReturn(pvNew, VERR_NO_MEMORY);
2611 pState->papContexts = (PVMSVGA3DCONTEXT *)pvNew;
2612 while (pState->cContexts < cNew)
2613 {
2614 pContext = (PVMSVGA3DCONTEXT)RTMemAllocZ(sizeof(*pContext));
2615 AssertReturn(pContext, VERR_NO_MEMORY);
2616 pContext->id = SVGA3D_INVALID_ID;
2617 pState->papContexts[pState->cContexts++] = pContext;
2618 }
2619 }
2620 /* If one already exists with this id, then destroy it now. */
2621 if (pState->papContexts[cid]->id != SVGA3D_INVALID_ID)
2622 vmsvga3dBackContextDestroy(pThisCC, cid);
2623
2624 pContext = pState->papContexts[cid];
2625 memset(pContext, 0, sizeof(*pContext));
2626 pContext->id = cid;
2627 for (uint32_t i = 0; i< RT_ELEMENTS(pContext->aSidActiveTextures); i++)
2628 pContext->aSidActiveTextures[i] = SVGA3D_INVALID_ID;
2629 pContext->state.shidVertex = SVGA3D_INVALID_ID;
2630 pContext->state.shidPixel = SVGA3D_INVALID_ID;
2631
2632 for (uint32_t i = 0; i < RT_ELEMENTS(pContext->state.aRenderTargets); i++)
2633 pContext->state.aRenderTargets[i] = SVGA3D_INVALID_ID;
2634
2635 /* Create a context window with minimal 4x4 size. We will never use the swapchain
2636 * to present the rendered image. Rendered images from the guest will be copied to
2637 * the VMSVGA SCREEN object, which can be either an offscreen render target or
2638 * system memory in the guest VRAM.
2639 */
2640 rc = vmsvga3dContextWindowCreate(pState->hInstance, pState->pWindowThread, pState->WndRequestSem, &pContext->hwnd);
2641 AssertRCReturn(rc, rc);
2642
2643 /* Changed when the function returns. */
2644 D3DPRESENT_PARAMETERS PresParam;
2645 PresParam.BackBufferWidth = 0;
2646 PresParam.BackBufferHeight = 0;
2647 PresParam.BackBufferFormat = D3DFMT_UNKNOWN;
2648 PresParam.BackBufferCount = 0;
2649
2650 PresParam.MultiSampleType = D3DMULTISAMPLE_NONE;
2651 PresParam.MultiSampleQuality = 0;
2652 PresParam.SwapEffect = D3DSWAPEFFECT_DISCARD;
2653 PresParam.hDeviceWindow = pContext->hwnd;
2654 PresParam.Windowed = TRUE;
2655 PresParam.EnableAutoDepthStencil = FALSE;
2656 PresParam.AutoDepthStencilFormat = D3DFMT_UNKNOWN; /* not relevant */
2657 PresParam.Flags = 0;
2658 PresParam.FullScreen_RefreshRateInHz = 0; /* windowed -> 0 */
2659 /** @todo consider using D3DPRESENT_DONOTWAIT so we don't wait for the GPU during Present calls. */
2660 PresParam.PresentationInterval = D3DPRESENT_INTERVAL_IMMEDIATE;
2661
2662#if 0
2663 VMSVGA3DCREATEDEVICEPARAMS Params = { pState, pContext, &PresParam, 0 };
2664 rc = vmsvga3dSendThreadMessage(pState->pWindowThread, pState->WndRequestSem, WM_VMSVGA3D_CREATE_DEVICE, 0, (LPARAM)&Params);
2665 AssertRCReturn(rc, rc);
2666 hr = Params.hrc;
2667
2668#else
2669 /** @todo Docs indicates that we should be using
2670 * D3DCREATE_HARDWARE_VERTEXPROCESSING with W10 1607 and higher.
2671 * https://docs.microsoft.com/en-us/windows/win32/direct3d9/d3dcreate */
2672 hr = pState->pD3D9->CreateDeviceEx(D3DADAPTER_DEFAULT,
2673 D3DDEVTYPE_HAL,
2674 pContext->hwnd,
2675 D3DCREATE_MULTITHREADED | D3DCREATE_MIXED_VERTEXPROCESSING, //D3DCREATE_HARDWARE_VERTEXPROCESSING,
2676 &PresParam,
2677 NULL,
2678 &pContext->pDevice);
2679#endif
2680 AssertMsgReturn(hr == D3D_OK, ("vmsvga3dContextDefine: CreateDevice failed with %x\n", hr), VERR_INTERNAL_ERROR);
2681
2682 Log(("vmsvga3dContextDefine: Backbuffer (%d,%d) count=%d format=%x\n", PresParam.BackBufferWidth, PresParam.BackBufferHeight, PresParam.BackBufferCount, PresParam.BackBufferFormat));
2683 return VINF_SUCCESS;
2684}
2685
2686/**
2687 * Destroy an existing 3d context
2688 *
2689 * @returns VBox status code.
2690 * @param pThisCC The VGA/VMSVGA state for ring-3.
2691 * @param cid Context id
2692 */
2693static DECLCALLBACK(int) vmsvga3dBackContextDestroy(PVGASTATECC pThisCC, uint32_t cid)
2694{
2695 PVMSVGA3DSTATE pState = pThisCC->svga.p3dState;
2696 AssertReturn(pState, VERR_NO_MEMORY);
2697
2698 AssertReturn(cid < SVGA3D_MAX_CONTEXT_IDS, VERR_INVALID_PARAMETER);
2699
2700 if ( cid < pState->cContexts
2701 && pState->papContexts[cid]->id == cid)
2702 {
2703 PVMSVGA3DCONTEXT pContext = pState->papContexts[cid];
2704
2705 Log(("vmsvga3dContextDestroy id %x\n", cid));
2706
2707 /* Cleanup the device runtime state. */
2708 if (pContext->pDevice)
2709 pContext->pDevice->SetVertexDeclaration(NULL);
2710 D3D_RELEASE(pContext->d3dState.pVertexDecl);
2711
2712 /* Check for all surfaces that are associated with this context to remove all dependencies */
2713 for (uint32_t sid = 0; sid < pState->cSurfaces; sid++)
2714 {
2715 PVMSVGA3DSURFACE pSurface = pState->papSurfaces[sid];
2716 if ( pSurface->id == sid
2717 && pSurface->idAssociatedContext == cid)
2718 {
2719 int rc;
2720
2721 LogFunc(("Remove all dependencies for surface sid=%u\n", sid));
2722
2723 SVGA3dSurfaceAllFlags const surfaceFlags = pSurface->f.surfaceFlags;
2724 SVGA3dSurfaceFormat const format = pSurface->format;
2725 uint32_t const multisampleCount = pSurface->multiSampleCount;
2726 SVGA3dTextureFilter const autogenFilter = pSurface->autogenFilter;
2727 uint32_t const cFaces = pSurface->cFaces;
2728 uint32_t const cMipLevels = pSurface->cLevels;
2729
2730 SVGA3dSize *pMipLevelSize = (SVGA3dSize *)RTMemAllocZ(cMipLevels * cFaces * sizeof(SVGA3dSize));
2731 AssertReturn(pMipLevelSize, VERR_NO_MEMORY);
2732
2733 for (uint32_t face = 0; face < pSurface->cFaces; face++)
2734 {
2735 for (uint32_t i = 0; i < cMipLevels; i++)
2736 {
2737 uint32_t idx = i + face * cMipLevels;
2738 memcpy(&pMipLevelSize[idx], &pSurface->paMipmapLevels[idx].mipmapSize, sizeof(SVGA3dSize));
2739 }
2740 }
2741
2742 /* Recreate the surface with the original settings; destroys the contents, but that seems fairly safe since the context is also destroyed. */
2743#ifdef DEBUG_sunlover
2744 /** @todo not safe with shared objects */
2745 Assert(pSurface->pSharedObjectTree == NULL);
2746#endif
2747
2748 rc = vmsvga3dSurfaceDestroy(pThisCC, sid);
2749 AssertRC(rc);
2750
2751 rc = vmsvga3dSurfaceDefine(pThisCC, sid, surfaceFlags, format, multisampleCount, autogenFilter,
2752 cMipLevels, &pMipLevelSize[0], /* arraySize = */ 0, /* fAllocMipLevels = */ true);
2753 AssertRC(rc);
2754
2755 Assert(!pSurface->u.pSurface);
2756 }
2757 else
2758 {
2759 /* Check for a shared surface object. */
2760 PVMSVGA3DSHAREDSURFACE pSharedSurface = (PVMSVGA3DSHAREDSURFACE)RTAvlU32Get(&pSurface->pSharedObjectTree, cid);
2761 if (pSharedSurface)
2762 {
2763 LogFunc(("Remove shared dependency for surface sid=%u\n", sid));
2764
2765 switch (pSurface->enmD3DResType)
2766 {
2767 case VMSVGA3D_D3DRESTYPE_TEXTURE:
2768 Assert(pSharedSurface->u.pTexture);
2769 D3D_RELEASE(pSharedSurface->u.pTexture);
2770 break;
2771
2772 case VMSVGA3D_D3DRESTYPE_CUBE_TEXTURE:
2773 Assert(pSharedSurface->u.pCubeTexture);
2774 D3D_RELEASE(pSharedSurface->u.pCubeTexture);
2775 break;
2776
2777 case VMSVGA3D_D3DRESTYPE_VOLUME_TEXTURE:
2778 Assert(pSharedSurface->u.pVolumeTexture);
2779 D3D_RELEASE(pSharedSurface->u.pVolumeTexture);
2780 break;
2781
2782 default:
2783 AssertFailed();
2784 break;
2785 }
2786 RTAvlU32Remove(&pSurface->pSharedObjectTree, cid);
2787 RTMemFree(pSharedSurface);
2788 }
2789 }
2790 }
2791
2792 /* Destroy all leftover pixel shaders. */
2793 for (uint32_t i = 0; i < pContext->cPixelShaders; i++)
2794 {
2795 if (pContext->paPixelShader[i].id != SVGA3D_INVALID_ID)
2796 vmsvga3dBackShaderDestroy(pThisCC, pContext->paPixelShader[i].cid, pContext->paPixelShader[i].id, pContext->paPixelShader[i].type);
2797 }
2798 if (pContext->paPixelShader)
2799 RTMemFree(pContext->paPixelShader);
2800
2801 /* Destroy all leftover vertex shaders. */
2802 for (uint32_t i = 0; i < pContext->cVertexShaders; i++)
2803 {
2804 if (pContext->paVertexShader[i].id != SVGA3D_INVALID_ID)
2805 vmsvga3dBackShaderDestroy(pThisCC, pContext->paVertexShader[i].cid, pContext->paVertexShader[i].id, pContext->paVertexShader[i].type);
2806 }
2807 if (pContext->paVertexShader)
2808 RTMemFree(pContext->paVertexShader);
2809
2810 if (pContext->state.paVertexShaderConst)
2811 RTMemFree(pContext->state.paVertexShaderConst);
2812 if (pContext->state.paPixelShaderConst)
2813 RTMemFree(pContext->state.paPixelShaderConst);
2814
2815 vmsvga3dBackOcclusionQueryDelete(pThisCC, pContext);
2816
2817 /* Release the D3D device object */
2818 D3D_RELEASE(pContext->pDevice);
2819
2820 /* Destroy the window we've created. */
2821 int rc = vmsvga3dSendThreadMessage(pState->pWindowThread, pState->WndRequestSem, WM_VMSVGA3D_DESTROYWINDOW, (WPARAM)pContext->hwnd, 0);
2822 AssertRC(rc);
2823
2824 memset(pContext, 0, sizeof(*pContext));
2825 pContext->id = SVGA3D_INVALID_ID;
2826 }
2827 else
2828 AssertFailed();
2829
2830 return VINF_SUCCESS;
2831}
2832
2833static DECLCALLBACK(int) vmsvga3dBackDefineScreen(PVGASTATE pThis, PVGASTATECC pThisCC, VMSVGASCREENOBJECT *pScreen)
2834{
2835 RT_NOREF(pThis, pThisCC, pScreen);
2836 return VERR_NOT_IMPLEMENTED;
2837}
2838
2839static DECLCALLBACK(int) vmsvga3dBackDestroyScreen(PVGASTATECC pThisCC, VMSVGASCREENOBJECT *pScreen)
2840{
2841 RT_NOREF(pThisCC, pScreen);
2842 return VERR_NOT_IMPLEMENTED;
2843}
2844
2845static DECLCALLBACK(int) vmsvga3dBackSurfaceBlitToScreen(PVGASTATECC pThisCC, VMSVGASCREENOBJECT *pScreen,
2846 SVGASignedRect destRect, SVGA3dSurfaceImageId srcImage,
2847 SVGASignedRect srcRect, uint32_t cRects, SVGASignedRect *paRects)
2848{
2849 RT_NOREF(pThisCC, pScreen, destRect, srcImage, srcRect, cRects, paRects);
2850 return VERR_NOT_IMPLEMENTED;
2851}
2852
2853static int vmsvga3dContextTrackUsage(PVGASTATECC pThisCC, PVMSVGA3DCONTEXT pContext)
2854{
2855 PVMSVGA3DSTATE pState = pThisCC->svga.p3dState;
2856 AssertReturn(pState, VERR_NO_MEMORY);
2857
2858 /* Inject fences to make sure we can track surface usage in case the client wants to reuse it in another context. */
2859 for (uint32_t i = 0; i < RT_ELEMENTS(pContext->aSidActiveTextures); ++i)
2860 {
2861 if (pContext->aSidActiveTextures[i] != SVGA3D_INVALID_ID)
2862 vmsvga3dSurfaceTrackUsageById(pState, pContext, pContext->aSidActiveTextures[i]);
2863 }
2864 for (uint32_t i = 0; i < RT_ELEMENTS(pContext->state.aRenderTargets); ++i)
2865 if (pContext->state.aRenderTargets[i] != SVGA3D_INVALID_ID)
2866 vmsvga3dSurfaceTrackUsageById(pState, pContext, pContext->state.aRenderTargets[i]);
2867
2868 return VINF_SUCCESS;
2869}
2870
2871/* Handle resize */
2872static DECLCALLBACK(int) vmsvga3dBackChangeMode(PVGASTATECC pThisCC)
2873{
2874 PVMSVGA3DSTATE pState = pThisCC->svga.p3dState;
2875 AssertReturn(pState, VERR_NO_MEMORY);
2876
2877 /* Resize all active contexts. */
2878 for (uint32_t i = 0; i < pState->cContexts; i++)
2879 {
2880 PVMSVGA3DCONTEXT pContext = pState->papContexts[i];
2881 uint32_t cid = pContext->id;
2882
2883 if (cid != SVGA3D_INVALID_ID)
2884 {
2885 D3DPRESENT_PARAMETERS PresParam;
2886 D3DVIEWPORT9 viewportOrg;
2887 HRESULT hr;
2888
2889#ifdef VMSVGA3D_DIRECT3D9_RESET
2890 /* Sync back all surface data as everything is lost after the Reset. */
2891 for (uint32_t sid = 0; sid < pState->cSurfaces; sid++)
2892 {
2893 PVMSVGA3DSURFACE pSurface = pState->papSurfaces[sid];
2894 if ( pSurface->id == sid
2895 && pSurface->idAssociatedContext == cid
2896 && pSurface->u.pSurface)
2897 {
2898 Log(("vmsvga3dChangeMode: sync back data of surface sid=%u (fDirty=%d)\n", sid, pSurface->fDirty));
2899
2900 /* Reallocate our surface memory buffers. */
2901 for (uint32_t i = 0; i < pSurface->cMipLevels; i++)
2902 {
2903 PVMSVGA3DMIPMAPLEVEL pMipmapLevel = &pSurface->paMipmapLevels[i];
2904
2905 pMipmapLevel->pSurfaceData = RTMemAllocZ(pMipmapLevel->cbSurface);
2906 AssertReturn(pMipmapLevel->pSurfaceData, VERR_NO_MEMORY);
2907
2908 if (!pSurface->fDirty)
2909 {
2910 D3DLOCKED_RECT LockedRect;
2911
2912 if (pSurface->bounce.pTexture)
2913 {
2914 IDirect3DSurface9 *pSrc, *pDest;
2915
2916 /** @todo only sync when something was actually rendered (since the last sync) */
2917 Log(("vmsvga3dChangeMode: sync bounce buffer (level %d)\n", i));
2918 hr = pSurface->bounce.pTexture->GetSurfaceLevel(i, &pDest);
2919 AssertMsgReturn(hr == D3D_OK, ("vmsvga3dChangeMode: GetSurfaceLevel failed with %x\n", hr), VERR_INTERNAL_ERROR);
2920
2921 hr = pSurface->u.pTexture->GetSurfaceLevel(i, &pSrc);
2922 AssertMsgReturn(hr == D3D_OK, ("vmsvga3dChangeMode: GetSurfaceLevel failed with %x\n", hr), VERR_INTERNAL_ERROR);
2923
2924 hr = pContext->pDevice->GetRenderTargetData(pSrc, pDest);
2925 AssertMsgReturn(hr == D3D_OK, ("vmsvga3dChangeMode: GetRenderTargetData failed with %x\n", hr), VERR_INTERNAL_ERROR);
2926
2927 D3D_RELEASE(pSrc);
2928 D3D_RELEASE(pDest);
2929
2930 hr = pSurface->bounce.pTexture->LockRect(i,
2931 &LockedRect,
2932 NULL,
2933 D3DLOCK_READONLY);
2934 }
2935 else
2936 hr = pSurface->u.pTexture->LockRect(i,
2937 &LockedRect,
2938 NULL,
2939 D3DLOCK_READONLY);
2940 AssertMsgReturn(hr == D3D_OK, ("vmsvga3dChangeMode: LockRect failed with %x\n", hr), VERR_INTERNAL_ERROR);
2941
2942 /* Copy the data one line at a time in case the internal pitch is different. */
2943 for (uint32_t j = 0; j < pMipmapLevel->size.height; j++)
2944 {
2945 memcpy((uint8_t *)pMipmapLevel->pSurfaceData + j * pMipmapLevel->cbSurfacePitch, (uint8_t *)LockedRect.pBits + j * LockedRect.Pitch, pMipmapLevel->cbSurfacePitch);
2946 }
2947
2948 if (pSurface->bounce.pTexture)
2949 hr = pSurface->bounce.pTexture->UnlockRect(i);
2950 else
2951 hr = pSurface->u.pTexture->UnlockRect(i);
2952 AssertMsgReturn(hr == D3D_OK, ("vmsvga3dChangeMode: UnlockRect failed with %x\n", hr), VERR_INTERNAL_ERROR);
2953 }
2954 }
2955
2956
2957 switch (pSurface->flags & VMSVGA3D_SURFACE_HINT_SWITCH_MASK)
2958 {
2959 case SVGA3D_SURFACE_CUBEMAP:
2960 case SVGA3D_SURFACE_CUBEMAP | SVGA3D_SURFACE_HINT_TEXTURE:
2961 case SVGA3D_SURFACE_CUBEMAP | SVGA3D_SURFACE_HINT_TEXTURE | SVGA3D_SURFACE_HINT_RENDERTARGET:
2962 D3D_RELEASE(pSurface->u.pCubeTexture);
2963 D3D_RELEASE(pSurface->bounce.pCubeTexture);
2964 break;
2965
2966 case SVGA3D_SURFACE_HINT_INDEXBUFFER | SVGA3D_SURFACE_HINT_VERTEXBUFFER:
2967 case SVGA3D_SURFACE_HINT_INDEXBUFFER:
2968 case SVGA3D_SURFACE_HINT_VERTEXBUFFER:
2969 if (pSurface->fu32ActualUsageFlags == SVGA3D_SURFACE_HINT_VERTEXBUFFER)
2970 D3D_RELEASE(pSurface->u.pVertexBuffer);
2971 else if (pSurface->fu32ActualUsageFlags == SVGA3D_SURFACE_HINT_INDEXBUFFER)
2972 D3D_RELEASE(pSurface->u.pIndexBuffer);
2973 else
2974 AssertMsg(pSurface->u.pVertexBuffer == NULL, ("fu32ActualUsageFlags %x\n", pSurface->fu32ActualUsageFlags));
2975 break;
2976
2977 case SVGA3D_SURFACE_HINT_TEXTURE:
2978 case SVGA3D_SURFACE_HINT_TEXTURE | SVGA3D_SURFACE_HINT_RENDERTARGET:
2979 D3D_RELEASE(pSurface->u.pTexture);
2980 D3D_RELEASE(pSurface->bounce.pTexture);
2981 break;
2982
2983 case SVGA3D_SURFACE_HINT_RENDERTARGET:
2984 case SVGA3D_SURFACE_HINT_DEPTHSTENCIL:
2985 if (pSurface->fStencilAsTexture)
2986 D3D_RELEASE(pSurface->u.pTexture);
2987 else
2988 D3D_RELEASE(pSurface->u.pSurface);
2989 break;
2990
2991 default:
2992 AssertFailed();
2993 break;
2994 }
2995 RTAvlU32Destroy(&pSurface->pSharedObjectTree, vmsvga3dSharedSurfaceDestroyTree, pSurface);
2996 Assert(pSurface->pSharedObjectTree == NULL);
2997
2998 pSurface->idAssociatedContext = SVGA3D_INVALID_ID;
2999 pSurface->hSharedObject = 0;
3000 }
3001 }
3002#endif /* #ifdef VMSVGA3D_DIRECT3D9_RESET */
3003
3004 AssertReturn(pContext->pDevice, VERR_INTERNAL_ERROR);
3005
3006 /* Cleanup the device runtime state. */
3007 pContext->pDevice->SetVertexDeclaration(NULL);
3008 D3D_RELEASE(pContext->d3dState.pVertexDecl);
3009
3010 hr = pContext->pDevice->GetViewport(&viewportOrg);
3011 AssertMsgReturn(hr == D3D_OK, ("vmsvga3dChangeMode: GetViewport failed with %x\n", hr), VERR_INTERNAL_ERROR);
3012
3013 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)));
3014
3015 /* Changed when the function returns. */
3016 PresParam.BackBufferWidth = 0;
3017 PresParam.BackBufferHeight = 0;
3018 PresParam.BackBufferFormat = D3DFMT_UNKNOWN;
3019 PresParam.BackBufferCount = 0;
3020
3021 PresParam.MultiSampleType = D3DMULTISAMPLE_NONE;
3022 PresParam.MultiSampleQuality = 0;
3023 PresParam.SwapEffect = D3DSWAPEFFECT_DISCARD;
3024 PresParam.hDeviceWindow = pContext->hwnd;
3025 PresParam.Windowed = TRUE;
3026 PresParam.EnableAutoDepthStencil = FALSE;
3027 PresParam.AutoDepthStencilFormat = D3DFMT_UNKNOWN; /* not relevant */
3028 PresParam.Flags = 0;
3029 PresParam.FullScreen_RefreshRateInHz = 0; /* windowed -> 0 */
3030 /** @todo consider using D3DPRESENT_DONOTWAIT so we don't wait for the GPU during Present calls. */
3031 PresParam.PresentationInterval = D3DPRESENT_INTERVAL_IMMEDIATE;;
3032
3033 /* ResetEx does not trash the device state */
3034 hr = pContext->pDevice->ResetEx(&PresParam, NULL);
3035 AssertMsgReturn(hr == D3D_OK, ("vmsvga3dChangeMode: Reset failed with %x\n", hr), VERR_INTERNAL_ERROR);
3036
3037 Log(("vmsvga3dChangeMode: Backbuffer (%d,%d) count=%d format=%x\n", PresParam.BackBufferWidth, PresParam.BackBufferHeight, PresParam.BackBufferCount, PresParam.BackBufferFormat));
3038
3039 /* ResetEx changes the viewport; restore it again. */
3040 hr = pContext->pDevice->SetViewport(&viewportOrg);
3041 AssertMsgReturn(hr == D3D_OK, ("vmsvga3dChangeMode: SetViewport failed with %x\n", hr), VERR_INTERNAL_ERROR);
3042
3043#ifdef LOG_ENABLED
3044 {
3045 D3DVIEWPORT9 viewport;
3046 hr = pContext->pDevice->GetViewport(&viewport);
3047 AssertMsgReturn(hr == D3D_OK, ("vmsvga3dChangeMode: GetViewport failed with %x\n", hr), VERR_INTERNAL_ERROR);
3048
3049 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)));
3050 }
3051#endif
3052
3053 /* First set the render targets as they change the internal state (reset viewport etc) */
3054 Log(("vmsvga3dChangeMode: Recreate render targets BEGIN\n"));
3055 for (uint32_t j = 0; j < RT_ELEMENTS(pContext->state.aRenderTargets); j++)
3056 {
3057 if (pContext->state.aRenderTargets[j] != SVGA3D_INVALID_ID)
3058 {
3059 SVGA3dSurfaceImageId target;
3060
3061 target.sid = pContext->state.aRenderTargets[j];
3062 target.face = 0;
3063 target.mipmap = 0;
3064 int rc = vmsvga3dBackSetRenderTarget(pThisCC, cid, (SVGA3dRenderTargetType)j, target);
3065 AssertRCReturn(rc, rc);
3066 }
3067 }
3068
3069#ifdef VMSVGA3D_DIRECT3D9_RESET
3070 /* Recreate the render state */
3071 Log(("vmsvga3dChangeMode: Recreate render state BEGIN\n"));
3072 for (uint32_t i = 0; i < RT_ELEMENTS(pContext->state.aRenderState); i++)
3073 {
3074 SVGA3dRenderState *pRenderState = &pContext->state.aRenderState[i];
3075
3076 if (pRenderState->state != SVGA3D_RS_INVALID)
3077 vmsvga3dBackSetRenderState(pThisCC, pContext->id, 1, pRenderState);
3078 }
3079 Log(("vmsvga3dChangeMode: Recreate render state END\n"));
3080
3081 /* Recreate the texture state */
3082 Log(("vmsvga3dChangeMode: Recreate texture state BEGIN\n"));
3083 for (uint32_t iStage = 0; iStage < RT_ELEMENTS(pContext->state.aTextureStates); iStage++)
3084 {
3085 for (uint32_t j = 0; j < RT_ELEMENTS(pContext->state.aTextureStates[0]); j++)
3086 {
3087 SVGA3dTextureState *pTextureState = &pContext->state.aTextureStates[iStage][j];
3088
3089 if (pTextureState->name != SVGA3D_RS_INVALID)
3090 vmsvga3dBackSetTextureState(pThisCC, pContext->id, 1, pTextureState);
3091 }
3092 }
3093 Log(("vmsvga3dChangeMode: Recreate texture state END\n"));
3094
3095 if (pContext->state.u32UpdateFlags & VMSVGA3D_UPDATE_SCISSORRECT)
3096 vmsvga3dBackSetScissorRect(pThis, cid, &pContext->state.RectScissor);
3097 if (pContext->state.u32UpdateFlags & VMSVGA3D_UPDATE_ZRANGE)
3098 vmsvga3dBackSetZRange(pThisCC, cid, pContext->state.zRange);
3099 if (pContext->state.u32UpdateFlags & VMSVGA3D_UPDATE_VIEWPORT)
3100 vmsvga3dBackSetViewPort(pThis, cid, &pContext->state.RectViewPort);
3101 if (pContext->state.u32UpdateFlags & VMSVGA3D_UPDATE_VERTEXSHADER)
3102 vmsvga3dBackShaderSet(pThis, pContext, cid, SVGA3D_SHADERTYPE_VS, pContext->state.shidVertex);
3103 if (pContext->state.u32UpdateFlags & VMSVGA3D_UPDATE_PIXELSHADER)
3104 vmsvga3dBackShaderSet(pThis, pContext, cid, SVGA3D_SHADERTYPE_PS, pContext->state.shidPixel);
3105 /** @todo restore more state data */
3106#endif /* #ifdef VMSVGA3D_DIRECT3D9_RESET */
3107 }
3108 }
3109 return VINF_SUCCESS;
3110}
3111
3112
3113static DECLCALLBACK(int) vmsvga3dBackSetTransform(PVGASTATECC pThisCC, uint32_t cid, SVGA3dTransformType type, float matrix[16])
3114{
3115 D3DTRANSFORMSTATETYPE d3dState;
3116 HRESULT hr;
3117 PVMSVGA3DSTATE pState = pThisCC->svga.p3dState;
3118 AssertReturn(pState, VERR_NO_MEMORY);
3119
3120 Log(("vmsvga3dSetTransform %x %s\n", cid, vmsvgaTransformToString(type)));
3121
3122 ASSERT_GUEST_RETURN((unsigned)type < SVGA3D_TRANSFORM_MAX, VERR_INVALID_PARAMETER);
3123
3124 PVMSVGA3DCONTEXT pContext;
3125 int rc = vmsvga3dContextFromCid(pState, cid, &pContext);
3126 AssertRCReturn(rc, rc);
3127 AssertReturn(pContext->pDevice, VERR_INTERNAL_ERROR);
3128
3129 switch (type)
3130 {
3131 case SVGA3D_TRANSFORM_VIEW:
3132 d3dState = D3DTS_VIEW;
3133 break;
3134 case SVGA3D_TRANSFORM_PROJECTION:
3135 d3dState = D3DTS_PROJECTION;
3136 break;
3137 case SVGA3D_TRANSFORM_TEXTURE0:
3138 d3dState = D3DTS_TEXTURE0;
3139 break;
3140 case SVGA3D_TRANSFORM_TEXTURE1:
3141 d3dState = D3DTS_TEXTURE1;
3142 break;
3143 case SVGA3D_TRANSFORM_TEXTURE2:
3144 d3dState = D3DTS_TEXTURE2;
3145 break;
3146 case SVGA3D_TRANSFORM_TEXTURE3:
3147 d3dState = D3DTS_TEXTURE3;
3148 break;
3149 case SVGA3D_TRANSFORM_TEXTURE4:
3150 d3dState = D3DTS_TEXTURE4;
3151 break;
3152 case SVGA3D_TRANSFORM_TEXTURE5:
3153 d3dState = D3DTS_TEXTURE5;
3154 break;
3155 case SVGA3D_TRANSFORM_TEXTURE6:
3156 d3dState = D3DTS_TEXTURE6;
3157 break;
3158 case SVGA3D_TRANSFORM_TEXTURE7:
3159 d3dState = D3DTS_TEXTURE7;
3160 break;
3161 case SVGA3D_TRANSFORM_WORLD:
3162 d3dState = D3DTS_WORLD;
3163 break;
3164 case SVGA3D_TRANSFORM_WORLD1:
3165 d3dState = D3DTS_WORLD1;
3166 break;
3167 case SVGA3D_TRANSFORM_WORLD2:
3168 d3dState = D3DTS_WORLD2;
3169 break;
3170 case SVGA3D_TRANSFORM_WORLD3:
3171 d3dState = D3DTS_WORLD3;
3172 break;
3173
3174 default:
3175 Log(("vmsvga3dSetTransform: unknown type!!\n"));
3176 return VERR_INVALID_PARAMETER;
3177 }
3178
3179 /* Save this matrix for vm state save/restore. */
3180 pContext->state.aTransformState[type].fValid = true;
3181 memcpy(pContext->state.aTransformState[type].matrix, matrix, sizeof(pContext->state.aTransformState[type].matrix));
3182 pContext->state.u32UpdateFlags |= VMSVGA3D_UPDATE_TRANSFORM;
3183
3184 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)));
3185 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)));
3186 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)));
3187 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)));
3188 hr = pContext->pDevice->SetTransform(d3dState, (const D3DMATRIX *)matrix);
3189 AssertMsgReturn(hr == D3D_OK, ("vmsvga3dSetTransform: SetTransform failed with %x\n", hr), VERR_INTERNAL_ERROR);
3190 return VINF_SUCCESS;
3191}
3192
3193static DECLCALLBACK(int) vmsvga3dBackSetZRange(PVGASTATECC pThisCC, uint32_t cid, SVGA3dZRange zRange)
3194{
3195 D3DVIEWPORT9 viewport;
3196 HRESULT hr;
3197 PVMSVGA3DSTATE pState = pThisCC->svga.p3dState;
3198 AssertReturn(pState, VERR_NO_MEMORY);
3199
3200 Log(("vmsvga3dSetZRange %x min=%d max=%d\n", cid, (uint32_t)(zRange.min * 100.0), (uint32_t)(zRange.max * 100.0)));
3201
3202 PVMSVGA3DCONTEXT pContext;
3203 int rc = vmsvga3dContextFromCid(pState, cid, &pContext);
3204 AssertRCReturn(rc, rc);
3205 AssertReturn(pContext->pDevice, VERR_INTERNAL_ERROR);
3206
3207 pContext->state.zRange = zRange;
3208 pContext->state.u32UpdateFlags |= VMSVGA3D_UPDATE_ZRANGE;
3209
3210 hr = pContext->pDevice->GetViewport(&viewport);
3211 AssertMsgReturn(hr == D3D_OK, ("vmsvga3dSetZRange: GetViewport failed with %x\n", hr), VERR_INTERNAL_ERROR);
3212
3213 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)));
3214 /** @todo convert the depth range from -1-1 to 0-1 although we shouldn't be getting such values in the first place... */
3215 if (zRange.min < 0.0)
3216 zRange.min = 0.0;
3217 if (zRange.max > 1.0)
3218 zRange.max = 1.0;
3219
3220 viewport.MinZ = zRange.min;
3221 viewport.MaxZ = zRange.max;
3222 hr = pContext->pDevice->SetViewport(&viewport);
3223 AssertMsgReturn(hr == D3D_OK, ("vmsvga3dSetZRange: SetViewport failed with %x\n", hr), VERR_INTERNAL_ERROR);
3224 return VINF_SUCCESS;
3225}
3226
3227/**
3228 * Convert SVGA blend op value to its D3D equivalent
3229 */
3230static DWORD vmsvga3dBlendOp2D3D(uint32_t blendOp, DWORD defaultBlendOp)
3231{
3232 switch (blendOp)
3233 {
3234 case SVGA3D_BLENDOP_ZERO:
3235 return D3DBLEND_ZERO;
3236 case SVGA3D_BLENDOP_ONE:
3237 return D3DBLEND_ONE;
3238 case SVGA3D_BLENDOP_SRCCOLOR:
3239 return D3DBLEND_SRCCOLOR;
3240 case SVGA3D_BLENDOP_INVSRCCOLOR:
3241 return D3DBLEND_INVSRCCOLOR;
3242 case SVGA3D_BLENDOP_SRCALPHA:
3243 return D3DBLEND_SRCALPHA;
3244 case SVGA3D_BLENDOP_INVSRCALPHA:
3245 return D3DBLEND_INVSRCALPHA;
3246 case SVGA3D_BLENDOP_DESTALPHA:
3247 return D3DBLEND_DESTALPHA;
3248 case SVGA3D_BLENDOP_INVDESTALPHA:
3249 return D3DBLEND_INVDESTALPHA;
3250 case SVGA3D_BLENDOP_DESTCOLOR:
3251 return D3DBLEND_DESTCOLOR;
3252 case SVGA3D_BLENDOP_INVDESTCOLOR:
3253 return D3DBLEND_INVDESTCOLOR;
3254 case SVGA3D_BLENDOP_SRCALPHASAT:
3255 return D3DBLEND_SRCALPHASAT;
3256 case SVGA3D_BLENDOP_BLENDFACTOR:
3257 return D3DBLEND_BLENDFACTOR;
3258 case SVGA3D_BLENDOP_INVBLENDFACTOR:
3259 return D3DBLEND_INVBLENDFACTOR;
3260 default:
3261 AssertFailed();
3262 return defaultBlendOp;
3263 }
3264}
3265
3266static DECLCALLBACK(int) vmsvga3dBackSetRenderState(PVGASTATECC pThisCC, uint32_t cid, uint32_t cRenderStates, SVGA3dRenderState *pRenderState)
3267{
3268 DWORD val = 0; /* Shut up MSC */
3269 HRESULT hr;
3270 PVMSVGA3DSTATE pState = pThisCC->svga.p3dState;
3271 AssertReturn(pState, VERR_NO_MEMORY);
3272
3273 Log(("vmsvga3dSetRenderState cid=%u cRenderStates=%d\n", cid, cRenderStates));
3274
3275 PVMSVGA3DCONTEXT pContext;
3276 int rc = vmsvga3dContextFromCid(pState, cid, &pContext);
3277 AssertRCReturn(rc, rc);
3278 AssertReturn(pContext->pDevice, VERR_INTERNAL_ERROR);
3279
3280 for (unsigned i = 0; i < cRenderStates; i++)
3281 {
3282 D3DRENDERSTATETYPE renderState = D3DRS_FORCE_DWORD;
3283
3284 Log(("vmsvga3dSetRenderState: state=%s (%d) val=%x\n", vmsvga3dGetRenderStateName(pRenderState[i].state), pRenderState[i].state, pRenderState[i].uintValue));
3285 /* Save the render state for vm state saving. */
3286 ASSERT_GUEST_RETURN((unsigned)pRenderState[i].state < SVGA3D_RS_MAX, VERR_INVALID_PARAMETER);
3287 pContext->state.aRenderState[pRenderState[i].state] = pRenderState[i];
3288
3289 switch (pRenderState[i].state)
3290 {
3291 case SVGA3D_RS_ZENABLE: /* SVGA3dBool */
3292 renderState = D3DRS_ZENABLE;
3293 val = pRenderState[i].uintValue;
3294 Assert(val == D3DZB_FALSE || val == D3DZB_TRUE);
3295 break;
3296
3297 case SVGA3D_RS_ZWRITEENABLE: /* SVGA3dBool */
3298 renderState = D3DRS_ZWRITEENABLE;
3299 val = pRenderState[i].uintValue;
3300 break;
3301
3302 case SVGA3D_RS_ALPHATESTENABLE: /* SVGA3dBool */
3303 renderState = D3DRS_ALPHATESTENABLE;
3304 val = pRenderState[i].uintValue;
3305 break;
3306
3307 case SVGA3D_RS_DITHERENABLE: /* SVGA3dBool */
3308 renderState = D3DRS_DITHERENABLE;
3309 val = pRenderState[i].uintValue;
3310 break;
3311
3312 case SVGA3D_RS_BLENDENABLE: /* SVGA3dBool */
3313 renderState = D3DRS_ALPHABLENDENABLE;
3314 val = pRenderState[i].uintValue;
3315 break;
3316
3317 case SVGA3D_RS_FOGENABLE: /* SVGA3dBool */
3318 renderState = D3DRS_FOGENABLE;
3319 val = pRenderState[i].uintValue;
3320 break;
3321
3322 case SVGA3D_RS_SPECULARENABLE: /* SVGA3dBool */
3323 renderState = D3DRS_SPECULARENABLE;
3324 val = pRenderState[i].uintValue;
3325 break;
3326
3327 case SVGA3D_RS_LIGHTINGENABLE: /* SVGA3dBool */
3328 renderState = D3DRS_LIGHTING;
3329 val = pRenderState[i].uintValue;
3330 break;
3331
3332 case SVGA3D_RS_NORMALIZENORMALS: /* SVGA3dBool */
3333 renderState = D3DRS_NORMALIZENORMALS;
3334 val = pRenderState[i].uintValue;
3335 break;
3336
3337 case SVGA3D_RS_POINTSPRITEENABLE: /* SVGA3dBool */
3338 renderState = D3DRS_POINTSPRITEENABLE;
3339 val = pRenderState[i].uintValue;
3340 break;
3341
3342 case SVGA3D_RS_POINTSCALEENABLE: /* SVGA3dBool */
3343 renderState = D3DRS_POINTSCALEENABLE;
3344 val = pRenderState[i].uintValue;
3345 break;
3346
3347 case SVGA3D_RS_POINTSIZE: /* float */
3348 renderState = D3DRS_POINTSIZE;
3349 val = pRenderState[i].uintValue;
3350 Log(("SVGA3D_RS_POINTSIZE: %d\n", (uint32_t) (pRenderState[i].floatValue * 100.0)));
3351 break;
3352
3353 case SVGA3D_RS_POINTSIZEMIN: /* float */
3354 renderState = D3DRS_POINTSIZE_MIN;
3355 val = pRenderState[i].uintValue;
3356 Log(("SVGA3D_RS_POINTSIZEMIN: %d\n", (uint32_t) (pRenderState[i].floatValue * 100.0)));
3357 break;
3358
3359 case SVGA3D_RS_POINTSIZEMAX: /* float */
3360 renderState = D3DRS_POINTSIZE_MAX;
3361 val = pRenderState[i].uintValue;
3362 Log(("SVGA3D_RS_POINTSIZEMAX: %d\n", (uint32_t) (pRenderState[i].floatValue * 100.0)));
3363 break;
3364
3365 case SVGA3D_RS_POINTSCALE_A: /* float */
3366 renderState = D3DRS_POINTSCALE_A;
3367 val = pRenderState[i].uintValue;
3368 break;
3369
3370 case SVGA3D_RS_POINTSCALE_B: /* float */
3371 renderState = D3DRS_POINTSCALE_B;
3372 val = pRenderState[i].uintValue;
3373 break;
3374
3375 case SVGA3D_RS_POINTSCALE_C: /* float */
3376 renderState = D3DRS_POINTSCALE_C;
3377 val = pRenderState[i].uintValue;
3378 break;
3379
3380 case SVGA3D_RS_AMBIENT: /* SVGA3dColor - identical */
3381 renderState = D3DRS_AMBIENT;
3382 val = pRenderState[i].uintValue;
3383 break;
3384
3385 case SVGA3D_RS_CLIPPLANEENABLE: /* SVGA3dClipPlanes - identical */
3386 renderState = D3DRS_CLIPPLANEENABLE;
3387 val = pRenderState[i].uintValue;
3388 break;
3389
3390 case SVGA3D_RS_FOGCOLOR: /* SVGA3dColor - identical */
3391 renderState = D3DRS_FOGCOLOR;
3392 val = pRenderState[i].uintValue;
3393 break;
3394
3395 case SVGA3D_RS_FOGSTART: /* float */
3396 renderState = D3DRS_FOGSTART;
3397 val = pRenderState[i].uintValue;
3398 break;
3399
3400 case SVGA3D_RS_FOGEND: /* float */
3401 renderState = D3DRS_FOGEND;
3402 val = pRenderState[i].uintValue;
3403 break;
3404
3405 case SVGA3D_RS_FOGDENSITY: /* float */
3406 renderState = D3DRS_FOGDENSITY;
3407 val = pRenderState[i].uintValue;
3408 break;
3409
3410 case SVGA3D_RS_RANGEFOGENABLE: /* SVGA3dBool */
3411 renderState = D3DRS_RANGEFOGENABLE;
3412 val = pRenderState[i].uintValue;
3413 break;
3414
3415 case SVGA3D_RS_FOGMODE: /* SVGA3dFogMode */
3416 {
3417 SVGA3dFogMode mode;
3418 mode.uintValue = pRenderState[i].uintValue;
3419
3420 switch (mode.function)
3421 {
3422 case SVGA3D_FOGFUNC_INVALID:
3423 val = D3DFOG_NONE;
3424 break;
3425 case SVGA3D_FOGFUNC_EXP:
3426 val = D3DFOG_EXP;
3427 break;
3428 case SVGA3D_FOGFUNC_EXP2:
3429 val = D3DFOG_EXP2;
3430 break;
3431 case SVGA3D_FOGFUNC_LINEAR:
3432 val = D3DFOG_LINEAR;
3433 break;
3434 case SVGA3D_FOGFUNC_PER_VERTEX: /* unable to find a d3d9 equivalent */
3435 AssertMsgFailedReturn(("Unsupported fog function SVGA3D_FOGFUNC_PER_VERTEX\n"), VERR_INTERNAL_ERROR);
3436 break;
3437 default:
3438 AssertMsgFailedReturn(("Unexpected fog function %d\n", mode.function), VERR_INTERNAL_ERROR);
3439 break;
3440 }
3441
3442 /* The fog type determines the render state. */
3443 switch (mode.type)
3444 {
3445 case SVGA3D_FOGTYPE_VERTEX:
3446 renderState = D3DRS_FOGVERTEXMODE;
3447 break;
3448 case SVGA3D_FOGTYPE_PIXEL:
3449 renderState = D3DRS_FOGTABLEMODE;
3450 break;
3451 default:
3452 AssertMsgFailedReturn(("Unexpected fog type %d\n", mode.type), VERR_INTERNAL_ERROR);
3453 break;
3454 }
3455
3456 /* Set the fog base to depth or range. */
3457 switch (mode.base)
3458 {
3459 case SVGA3D_FOGBASE_DEPTHBASED:
3460 hr = pContext->pDevice->SetRenderState(D3DRS_RANGEFOGENABLE, FALSE);
3461 AssertMsgReturn(hr == D3D_OK, ("vmsvga3dSetRenderState: SetRenderState SVGA3D_FOGBASE_DEPTHBASED failed with %x\n", hr), VERR_INTERNAL_ERROR);
3462 break;
3463 case SVGA3D_FOGBASE_RANGEBASED:
3464 hr = pContext->pDevice->SetRenderState(D3DRS_RANGEFOGENABLE, TRUE);
3465 AssertMsgReturn(hr == D3D_OK, ("vmsvga3dSetRenderState: SetRenderState SVGA3D_FOGBASE_RANGEBASED failed with %x\n", hr), VERR_INTERNAL_ERROR);
3466 break;
3467 default:
3468 /* ignore */
3469 AssertMsgFailed(("Unexpected fog base %d\n", mode.base));
3470 break;
3471 }
3472 break;
3473 }
3474
3475 case SVGA3D_RS_FILLMODE: /* SVGA3dFillMode */
3476 {
3477 SVGA3dFillMode mode;
3478
3479 mode.uintValue = pRenderState[i].uintValue;
3480
3481 switch (mode.mode)
3482 {
3483 case SVGA3D_FILLMODE_POINT:
3484 val = D3DFILL_POINT;
3485 break;
3486 case SVGA3D_FILLMODE_LINE:
3487 val = D3DFILL_WIREFRAME;
3488 break;
3489 case SVGA3D_FILLMODE_FILL:
3490 val = D3DFILL_SOLID;
3491 break;
3492 default:
3493 AssertMsgFailedReturn(("Unexpected fill mode %d\n", mode.mode), VERR_INTERNAL_ERROR);
3494 break;
3495 }
3496 /** @todo ignoring face for now. */
3497 renderState = D3DRS_FILLMODE;
3498 break;
3499 }
3500
3501 case SVGA3D_RS_SHADEMODE: /* SVGA3dShadeMode */
3502 renderState = D3DRS_SHADEMODE;
3503 AssertCompile(D3DSHADE_FLAT == SVGA3D_SHADEMODE_FLAT);
3504 val = pRenderState[i].uintValue; /* SVGA3dShadeMode == D3DSHADEMODE */
3505 break;
3506
3507 case SVGA3D_RS_LINEPATTERN: /* SVGA3dLinePattern */
3508 /* No longer supported by d3d; mesagl comments suggest not all backends support it */
3509 /** @todo */
3510 Log(("WARNING: SVGA3D_RS_LINEPATTERN %x not supported!!\n", pRenderState[i].uintValue));
3511 /*
3512 renderState = D3DRS_LINEPATTERN;
3513 val = pRenderState[i].uintValue;
3514 */
3515 break;
3516
3517 case SVGA3D_RS_SRCBLEND: /* SVGA3dBlendOp */
3518 renderState = D3DRS_SRCBLEND;
3519 val = vmsvga3dBlendOp2D3D(pRenderState[i].uintValue, D3DBLEND_ONE /* default */);
3520 break;
3521
3522 case SVGA3D_RS_DSTBLEND: /* SVGA3dBlendOp */
3523 renderState = D3DRS_DESTBLEND;
3524 val = vmsvga3dBlendOp2D3D(pRenderState[i].uintValue, D3DBLEND_ZERO /* default */);
3525 break;
3526
3527 case SVGA3D_RS_BLENDEQUATION: /* SVGA3dBlendEquation - identical */
3528 AssertCompile(SVGA3D_BLENDEQ_MAXIMUM == D3DBLENDOP_MAX);
3529 renderState = D3DRS_BLENDOP;
3530 val = pRenderState[i].uintValue;
3531 break;
3532
3533 case SVGA3D_RS_CULLMODE: /* SVGA3dFace */
3534 {
3535 switch (pRenderState[i].uintValue)
3536 {
3537 case SVGA3D_FACE_NONE:
3538 val = D3DCULL_NONE;
3539 break;
3540 case SVGA3D_FACE_FRONT:
3541 val = D3DCULL_CW;
3542 break;
3543 case SVGA3D_FACE_BACK:
3544 val = D3DCULL_CCW;
3545 break;
3546 case SVGA3D_FACE_FRONT_BACK:
3547 AssertFailed();
3548 val = D3DCULL_CW;
3549 break;
3550 default:
3551 AssertMsgFailedReturn(("Unexpected cull mode %d\n", pRenderState[i].uintValue), VERR_INTERNAL_ERROR);
3552 break;
3553 }
3554 renderState = D3DRS_CULLMODE;
3555 break;
3556 }
3557
3558 case SVGA3D_RS_ZFUNC: /* SVGA3dCmpFunc - identical */
3559 AssertCompile(SVGA3D_CMP_ALWAYS == D3DCMP_ALWAYS);
3560 renderState = D3DRS_ZFUNC;
3561 val = pRenderState[i].uintValue;
3562 break;
3563
3564 case SVGA3D_RS_ALPHAFUNC: /* SVGA3dCmpFunc - identical */
3565 renderState = D3DRS_ALPHAFUNC;
3566 val = pRenderState[i].uintValue;
3567 break;
3568
3569 case SVGA3D_RS_STENCILENABLE: /* SVGA3dBool */
3570 renderState = D3DRS_STENCILENABLE;
3571 val = pRenderState[i].uintValue;
3572 break;
3573
3574 case SVGA3D_RS_STENCILREF: /* uint32_t */
3575 renderState = D3DRS_STENCILREF;
3576 val = pRenderState[i].uintValue;
3577 break;
3578
3579 case SVGA3D_RS_STENCILMASK: /* uint32_t */
3580 renderState = D3DRS_STENCILMASK;
3581 val = pRenderState[i].uintValue;
3582 break;
3583
3584 case SVGA3D_RS_STENCILWRITEMASK: /* uint32_t */
3585 renderState = D3DRS_STENCILWRITEMASK;
3586 val = pRenderState[i].uintValue;
3587 break;
3588
3589 case SVGA3D_RS_STENCILFUNC: /* SVGA3dCmpFunc - identical */
3590 renderState = D3DRS_STENCILFUNC;
3591 val = pRenderState[i].uintValue;
3592 break;
3593
3594 case SVGA3D_RS_STENCILFAIL: /* SVGA3dStencilOp - identical */
3595 AssertCompile(D3DSTENCILOP_KEEP == SVGA3D_STENCILOP_KEEP);
3596 AssertCompile(D3DSTENCILOP_DECR == SVGA3D_STENCILOP_DECR);
3597 renderState = D3DRS_STENCILFAIL;
3598 val = pRenderState[i].uintValue;
3599 break;
3600
3601 case SVGA3D_RS_STENCILZFAIL: /* SVGA3dStencilOp - identical */
3602 renderState = D3DRS_STENCILZFAIL;
3603 val = pRenderState[i].uintValue;
3604 break;
3605
3606 case SVGA3D_RS_STENCILPASS: /* SVGA3dStencilOp - identical */
3607 renderState = D3DRS_STENCILPASS;
3608 val = pRenderState[i].uintValue;
3609 break;
3610
3611 case SVGA3D_RS_ALPHAREF: /* float (0.0 .. 1.0) */
3612 renderState = D3DRS_ALPHAREF;
3613 val = (uint8_t)(pRenderState[i].floatValue * 255.0f); /* D3DRS_ALPHAREF 0..255 */
3614 break;
3615
3616 case SVGA3D_RS_FRONTWINDING: /* SVGA3dFrontWinding */
3617 Assert(pRenderState[i].uintValue == SVGA3D_FRONTWINDING_CW);
3618 /*
3619 renderState = D3DRS_FRONTWINDING; //D3DRS_TWOSIDEDSTENCILMODE
3620 val = pRenderState[i].uintValue;
3621 */
3622 break;
3623
3624 case SVGA3D_RS_COORDINATETYPE: /* SVGA3dCoordinateType */
3625 Assert(pRenderState[i].uintValue == SVGA3D_COORDINATE_LEFTHANDED);
3626 /** @todo setup a view matrix to scale the world space by -1 in the z-direction for right handed coordinates. */
3627 /*
3628 renderState = D3DRS_COORDINATETYPE;
3629 val = pRenderState[i].uintValue;
3630 */
3631 break;
3632
3633 case SVGA3D_RS_ZBIAS: /* float */
3634 /** @todo unknown meaning; depth bias is not identical
3635 renderState = D3DRS_DEPTHBIAS;
3636 val = pRenderState[i].uintValue;
3637 */
3638 Log(("vmsvga3dSetRenderState: WARNING unsupported SVGA3D_RS_ZBIAS\n"));
3639 break;
3640
3641 case SVGA3D_RS_SLOPESCALEDEPTHBIAS: /* float */
3642 renderState = D3DRS_SLOPESCALEDEPTHBIAS;
3643 val = pRenderState[i].uintValue;
3644 break;
3645
3646 case SVGA3D_RS_DEPTHBIAS: /* float */
3647 renderState = D3DRS_DEPTHBIAS;
3648 val = pRenderState[i].uintValue;
3649 break;
3650
3651 case SVGA3D_RS_COLORWRITEENABLE: /* SVGA3dColorMask - identical to D3DCOLORWRITEENABLE_* */
3652 renderState = D3DRS_COLORWRITEENABLE;
3653 val = pRenderState[i].uintValue;
3654 break;
3655
3656 case SVGA3D_RS_VERTEXMATERIALENABLE: /* SVGA3dBool */
3657 //AssertFailed();
3658 renderState = D3DRS_INDEXEDVERTEXBLENDENABLE; /* correct?? */
3659 val = pRenderState[i].uintValue;
3660 break;
3661
3662 case SVGA3D_RS_DIFFUSEMATERIALSOURCE: /* SVGA3dVertexMaterial - identical */
3663 AssertCompile(D3DMCS_COLOR2 == SVGA3D_VERTEXMATERIAL_SPECULAR);
3664 renderState = D3DRS_DIFFUSEMATERIALSOURCE;
3665 val = pRenderState[i].uintValue;
3666 break;
3667
3668 case SVGA3D_RS_SPECULARMATERIALSOURCE: /* SVGA3dVertexMaterial - identical */
3669 renderState = D3DRS_SPECULARMATERIALSOURCE;
3670 val = pRenderState[i].uintValue;
3671 break;
3672
3673 case SVGA3D_RS_AMBIENTMATERIALSOURCE: /* SVGA3dVertexMaterial - identical */
3674 renderState = D3DRS_AMBIENTMATERIALSOURCE;
3675 val = pRenderState[i].uintValue;
3676 break;
3677
3678 case SVGA3D_RS_EMISSIVEMATERIALSOURCE: /* SVGA3dVertexMaterial - identical */
3679 renderState = D3DRS_EMISSIVEMATERIALSOURCE;
3680 val = pRenderState[i].uintValue;
3681 break;
3682
3683 case SVGA3D_RS_TEXTUREFACTOR: /* SVGA3dColor - identical */
3684 renderState = D3DRS_TEXTUREFACTOR;
3685 val = pRenderState[i].uintValue;
3686 break;
3687
3688 case SVGA3D_RS_LOCALVIEWER: /* SVGA3dBool */
3689 renderState = D3DRS_LOCALVIEWER;
3690 val = pRenderState[i].uintValue;
3691 break;
3692
3693 case SVGA3D_RS_SCISSORTESTENABLE: /* SVGA3dBool */
3694 renderState = D3DRS_SCISSORTESTENABLE;
3695 val = pRenderState[i].uintValue;
3696 break;
3697
3698 case SVGA3D_RS_BLENDCOLOR: /* SVGA3dColor - identical */
3699 renderState = D3DRS_BLENDFACTOR;
3700 val = pRenderState[i].uintValue;
3701 break;
3702
3703 case SVGA3D_RS_STENCILENABLE2SIDED: /* SVGA3dBool */
3704 renderState = D3DRS_TWOSIDEDSTENCILMODE;
3705 val = pRenderState[i].uintValue;
3706 break;
3707
3708 case SVGA3D_RS_CCWSTENCILFUNC: /* SVGA3dCmpFunc - identical */
3709 renderState = D3DRS_CCW_STENCILFUNC;
3710 val = pRenderState[i].uintValue;
3711 break;
3712
3713 case SVGA3D_RS_CCWSTENCILFAIL: /* SVGA3dStencilOp - identical */
3714 renderState = D3DRS_CCW_STENCILFAIL;
3715 val = pRenderState[i].uintValue;
3716 break;
3717
3718 case SVGA3D_RS_CCWSTENCILZFAIL: /* SVGA3dStencilOp - identical */
3719 renderState = D3DRS_CCW_STENCILZFAIL;
3720 val = pRenderState[i].uintValue;
3721 break;
3722
3723 case SVGA3D_RS_CCWSTENCILPASS: /* SVGA3dStencilOp - identical */
3724 renderState = D3DRS_CCW_STENCILPASS;
3725 val = pRenderState[i].uintValue;
3726 break;
3727
3728 case SVGA3D_RS_VERTEXBLEND: /* SVGA3dVertexBlendFlags - identical */
3729 AssertCompile(SVGA3D_VBLEND_DISABLE == D3DVBF_DISABLE);
3730 renderState = D3DRS_VERTEXBLEND;
3731 val = pRenderState[i].uintValue;
3732 break;
3733
3734 case SVGA3D_RS_OUTPUTGAMMA: /* float */
3735 //AssertFailed();
3736 /*
3737 D3DRS_SRGBWRITEENABLE ??
3738 renderState = D3DRS_OUTPUTGAMMA;
3739 val = pRenderState[i].uintValue;
3740 */
3741 break;
3742
3743 case SVGA3D_RS_ZVISIBLE: /* SVGA3dBool */
3744 AssertFailed();
3745 /*
3746 renderState = D3DRS_ZVISIBLE;
3747 val = pRenderState[i].uintValue;
3748 */
3749 break;
3750
3751 case SVGA3D_RS_LASTPIXEL: /* SVGA3dBool */
3752 renderState = D3DRS_LASTPIXEL;
3753 val = pRenderState[i].uintValue;
3754 break;
3755
3756 case SVGA3D_RS_CLIPPING: /* SVGA3dBool */
3757 renderState = D3DRS_CLIPPING;
3758 val = pRenderState[i].uintValue;
3759 break;
3760
3761 case SVGA3D_RS_WRAP0: /* SVGA3dWrapFlags - identical */
3762 Assert(SVGA3D_WRAPCOORD_3 == D3DWRAPCOORD_3);
3763 renderState = D3DRS_WRAP0;
3764 val = pRenderState[i].uintValue;
3765 break;
3766
3767 case SVGA3D_RS_WRAP1: /* SVGA3dWrapFlags - identical */
3768 renderState = D3DRS_WRAP1;
3769 val = pRenderState[i].uintValue;
3770 break;
3771
3772 case SVGA3D_RS_WRAP2: /* SVGA3dWrapFlags - identical */
3773 renderState = D3DRS_WRAP2;
3774 val = pRenderState[i].uintValue;
3775 break;
3776
3777 case SVGA3D_RS_WRAP3: /* SVGA3dWrapFlags - identical */
3778 renderState = D3DRS_WRAP3;
3779 val = pRenderState[i].uintValue;
3780 break;
3781
3782 case SVGA3D_RS_WRAP4: /* SVGA3dWrapFlags - identical */
3783 renderState = D3DRS_WRAP4;
3784 val = pRenderState[i].uintValue;
3785 break;
3786
3787 case SVGA3D_RS_WRAP5: /* SVGA3dWrapFlags - identical */
3788 renderState = D3DRS_WRAP5;
3789 val = pRenderState[i].uintValue;
3790 break;
3791
3792 case SVGA3D_RS_WRAP6: /* SVGA3dWrapFlags - identical */
3793 renderState = D3DRS_WRAP6;
3794 val = pRenderState[i].uintValue;
3795 break;
3796
3797 case SVGA3D_RS_WRAP7: /* SVGA3dWrapFlags - identical */
3798 renderState = D3DRS_WRAP7;
3799 val = pRenderState[i].uintValue;
3800 break;
3801
3802 case SVGA3D_RS_WRAP8: /* SVGA3dWrapFlags - identical */
3803 renderState = D3DRS_WRAP8;
3804 val = pRenderState[i].uintValue;
3805 break;
3806
3807 case SVGA3D_RS_WRAP9: /* SVGA3dWrapFlags - identical */
3808 renderState = D3DRS_WRAP9;
3809 val = pRenderState[i].uintValue;
3810 break;
3811
3812 case SVGA3D_RS_WRAP10: /* SVGA3dWrapFlags - identical */
3813 renderState = D3DRS_WRAP10;
3814 val = pRenderState[i].uintValue;
3815 break;
3816
3817 case SVGA3D_RS_WRAP11: /* SVGA3dWrapFlags - identical */
3818 renderState = D3DRS_WRAP11;
3819 val = pRenderState[i].uintValue;
3820 break;
3821
3822 case SVGA3D_RS_WRAP12: /* SVGA3dWrapFlags - identical */
3823 renderState = D3DRS_WRAP12;
3824 val = pRenderState[i].uintValue;
3825 break;
3826
3827 case SVGA3D_RS_WRAP13: /* SVGA3dWrapFlags - identical */
3828 renderState = D3DRS_WRAP13;
3829 val = pRenderState[i].uintValue;
3830 break;
3831
3832 case SVGA3D_RS_WRAP14: /* SVGA3dWrapFlags - identical */
3833 renderState = D3DRS_WRAP14;
3834 val = pRenderState[i].uintValue;
3835 break;
3836
3837 case SVGA3D_RS_WRAP15: /* SVGA3dWrapFlags - identical */
3838 renderState = D3DRS_WRAP15;
3839 val = pRenderState[i].uintValue;
3840 break;
3841
3842 case SVGA3D_RS_MULTISAMPLEANTIALIAS: /* SVGA3dBool */
3843 renderState = D3DRS_MULTISAMPLEANTIALIAS;
3844 val = pRenderState[i].uintValue;
3845 break;
3846
3847 case SVGA3D_RS_MULTISAMPLEMASK: /* uint32_t */
3848 renderState = D3DRS_MULTISAMPLEMASK;
3849 val = pRenderState[i].uintValue;
3850 break;
3851
3852 case SVGA3D_RS_INDEXEDVERTEXBLENDENABLE: /* SVGA3dBool */
3853 renderState = D3DRS_INDEXEDVERTEXBLENDENABLE;
3854 val = pRenderState[i].uintValue;
3855 break;
3856
3857 case SVGA3D_RS_TWEENFACTOR: /* float */
3858 renderState = D3DRS_TWEENFACTOR;
3859 val = pRenderState[i].uintValue;
3860 break;
3861
3862 case SVGA3D_RS_ANTIALIASEDLINEENABLE: /* SVGA3dBool */
3863 renderState = D3DRS_ANTIALIASEDLINEENABLE;
3864 val = pRenderState[i].uintValue;
3865 break;
3866
3867 case SVGA3D_RS_COLORWRITEENABLE1: /* SVGA3dColorMask - identical to D3DCOLORWRITEENABLE_* */
3868 renderState = D3DRS_COLORWRITEENABLE1;
3869 val = pRenderState[i].uintValue;
3870 break;
3871
3872 case SVGA3D_RS_COLORWRITEENABLE2: /* SVGA3dColorMask - identical to D3DCOLORWRITEENABLE_* */
3873 renderState = D3DRS_COLORWRITEENABLE2;
3874 val = pRenderState[i].uintValue;
3875 break;
3876
3877 case SVGA3D_RS_COLORWRITEENABLE3: /* SVGA3dColorMask - identical to D3DCOLORWRITEENABLE_* */
3878 renderState = D3DRS_COLORWRITEENABLE3;
3879 val = pRenderState[i].uintValue;
3880 break;
3881
3882 case SVGA3D_RS_SEPARATEALPHABLENDENABLE: /* SVGA3dBool */
3883 renderState = D3DRS_SEPARATEALPHABLENDENABLE;
3884 val = pRenderState[i].uintValue;
3885 break;
3886
3887 case SVGA3D_RS_SRCBLENDALPHA: /* SVGA3dBlendOp */
3888 renderState = D3DRS_SRCBLENDALPHA;
3889 val = vmsvga3dBlendOp2D3D(pRenderState[i].uintValue, D3DBLEND_ONE /* default */);
3890 break;
3891
3892 case SVGA3D_RS_DSTBLENDALPHA: /* SVGA3dBlendOp */
3893 renderState = D3DRS_DESTBLENDALPHA;
3894 val = vmsvga3dBlendOp2D3D(pRenderState[i].uintValue, D3DBLEND_ZERO /* default */);
3895 break;
3896
3897 case SVGA3D_RS_BLENDEQUATIONALPHA: /* SVGA3dBlendEquation - identical */
3898 renderState = D3DRS_BLENDOPALPHA;
3899 val = pRenderState[i].uintValue;
3900 break;
3901
3902 case SVGA3D_RS_TRANSPARENCYANTIALIAS: /* SVGA3dTransparencyAntialiasType */
3903 AssertFailed();
3904 /*
3905 renderState = D3DRS_TRANSPARENCYANTIALIAS;
3906 val = pRenderState[i].uintValue;
3907 */
3908 break;
3909
3910 case SVGA3D_RS_LINEWIDTH: /* float */
3911 AssertFailed();
3912 /*
3913 renderState = D3DRS_LINEWIDTH;
3914 val = pRenderState[i].uintValue;
3915 */
3916 break;
3917
3918 case SVGA3D_RS_MAX: /* shut up MSC */
3919 case SVGA3D_RS_INVALID:
3920 AssertFailedBreak();
3921 }
3922
3923 if (renderState != D3DRS_FORCE_DWORD)
3924 {
3925 hr = pContext->pDevice->SetRenderState(renderState, val);
3926 AssertMsgReturn(hr == D3D_OK, ("vmsvga3dSetRenderState: SetRenderState failed with %x\n", hr), VERR_INTERNAL_ERROR);
3927 }
3928 }
3929
3930 return VINF_SUCCESS;
3931}
3932
3933static DECLCALLBACK(int) vmsvga3dBackSetRenderTarget(PVGASTATECC pThisCC, uint32_t cid, SVGA3dRenderTargetType type, SVGA3dSurfaceImageId target)
3934{
3935 HRESULT hr;
3936 PVMSVGA3DSTATE pState = pThisCC->svga.p3dState;
3937
3938 AssertReturn(pState, VERR_NO_MEMORY);
3939 AssertReturn((unsigned)type < SVGA3D_RT_MAX, VERR_INVALID_PARAMETER);
3940
3941 LogFunc(("cid=%u type=%x sid=%u face=%u mipmap=%u\n", cid, type, target.sid, target.face, target.mipmap));
3942
3943 PVMSVGA3DCONTEXT pContext;
3944 int rc = vmsvga3dContextFromCid(pState, cid, &pContext);
3945 AssertRCReturn(rc, rc);
3946 AssertReturn(pContext->pDevice, VERR_INTERNAL_ERROR);
3947
3948 /* Save for vm state save/restore. */
3949 pContext->state.aRenderTargets[type] = target.sid;
3950 /** @todo Also save target.face and target.mipmap */
3951
3952 if (target.sid == SVGA3D_INVALID_ID)
3953 {
3954 /* Disable render target. */
3955 switch (type)
3956 {
3957 case SVGA3D_RT_DEPTH:
3958 hr = pContext->pDevice->SetDepthStencilSurface(NULL);
3959 AssertMsgReturn(hr == D3D_OK, ("SetDepthStencilSurface failed with %x\n", hr), VERR_INTERNAL_ERROR);
3960 break;
3961
3962 case SVGA3D_RT_STENCIL:
3963 /* ignore; correct?? */
3964 break;
3965
3966 case SVGA3D_RT_COLOR0:
3967 case SVGA3D_RT_COLOR1:
3968 case SVGA3D_RT_COLOR2:
3969 case SVGA3D_RT_COLOR3:
3970 case SVGA3D_RT_COLOR4:
3971 case SVGA3D_RT_COLOR5:
3972 case SVGA3D_RT_COLOR6:
3973 case SVGA3D_RT_COLOR7:
3974 if (pState->fSupportedSurfaceNULL)
3975 {
3976 /* Create a dummy render target to satisfy D3D. This path is usually taken only to render
3977 * into a depth buffer without wishing to update an actual color render target.
3978 * The dimensions of the render target must match the dimensions of the depth render target,
3979 * which is usually equal to the viewport width and height.
3980 */
3981 IDirect3DSurface9 *pDummyRenderTarget;
3982 hr = pContext->pDevice->CreateRenderTarget(pContext->state.RectViewPort.w,
3983 pContext->state.RectViewPort.h,
3984 FOURCC_NULL,
3985 D3DMULTISAMPLE_NONE,
3986 0,
3987 FALSE,
3988 &pDummyRenderTarget,
3989 NULL);
3990
3991 AssertMsgReturn(hr == D3D_OK, ("CreateRenderTarget failed with %x\n", hr), VERR_INTERNAL_ERROR);
3992
3993 hr = pContext->pDevice->SetRenderTarget(type - SVGA3D_RT_COLOR0, pDummyRenderTarget);
3994 D3D_RELEASE(pDummyRenderTarget);
3995 }
3996 else
3997 hr = pContext->pDevice->SetRenderTarget(type - SVGA3D_RT_COLOR0, NULL);
3998
3999 AssertMsgReturn(hr == D3D_OK, ("SetRenderTarget failed with %x\n", hr), VERR_INTERNAL_ERROR);
4000 break;
4001
4002 default:
4003 AssertFailedReturn(VERR_INVALID_PARAMETER);
4004 }
4005 return VINF_SUCCESS;
4006 }
4007
4008 PVMSVGA3DSURFACE pRenderTarget;
4009 rc = vmsvga3dSurfaceFromSid(pState, target.sid, &pRenderTarget);
4010 AssertRCReturn(rc, rc);
4011
4012 switch (type)
4013 {
4014 case SVGA3D_RT_DEPTH:
4015 case SVGA3D_RT_STENCIL:
4016 AssertReturn(target.face == 0 && target.mipmap == 0, VERR_INVALID_PARAMETER);
4017 if (!pRenderTarget->u.pSurface)
4018 {
4019 DWORD cQualityLevels = 0;
4020
4021 /* Query the nr of quality levels for this particular format */
4022 if (pRenderTarget->multiSampleTypeD3D != D3DMULTISAMPLE_NONE)
4023 {
4024 hr = pState->pD3D9->CheckDeviceMultiSampleType(D3DADAPTER_DEFAULT,
4025 D3DDEVTYPE_HAL,
4026 pRenderTarget->formatD3D,
4027 TRUE, /* Windowed */
4028 pRenderTarget->multiSampleTypeD3D,
4029 &cQualityLevels);
4030 Assert(hr == D3D_OK || hr == D3DERR_NOTAVAILABLE);
4031 }
4032
4033 if ( pState->fSupportedSurfaceINTZ
4034 && pRenderTarget->multiSampleTypeD3D == D3DMULTISAMPLE_NONE
4035 && ( pRenderTarget->formatD3D == D3DFMT_D24S8
4036 || pRenderTarget->formatD3D == D3DFMT_D24X8
4037 || pRenderTarget->formatD3D == D3DFMT_D32
4038 || pRenderTarget->formatD3D == D3DFMT_D16))
4039 {
4040 LogFunc(("Creating stencil surface as texture!\n"));
4041 int rc2 = vmsvga3dBackCreateTexture(pThisCC, pContext, cid, pRenderTarget);
4042 AssertRC(rc2); /* non-fatal, will use CreateDepthStencilSurface */
4043 }
4044
4045 if (!pRenderTarget->fStencilAsTexture)
4046 {
4047 Assert(!pRenderTarget->u.pSurface);
4048
4049 LogFunc(("DEPTH/STENCIL; cQualityLevels=%d\n", cQualityLevels));
4050 hr = pContext->pDevice->CreateDepthStencilSurface(pRenderTarget->paMipmapLevels[0].mipmapSize.width,
4051 pRenderTarget->paMipmapLevels[0].mipmapSize.height,
4052 pRenderTarget->formatD3D,
4053 pRenderTarget->multiSampleTypeD3D,
4054 ((cQualityLevels >= 1) ? cQualityLevels - 1 : 0), /* 0 - (levels-1) */
4055 FALSE, /* not discardable */
4056 &pRenderTarget->u.pSurface,
4057 NULL);
4058 AssertMsgReturn(hr == D3D_OK, ("CreateDepthStencilSurface failed with %x\n", hr), VERR_INTERNAL_ERROR);
4059 pRenderTarget->enmD3DResType = VMSVGA3D_D3DRESTYPE_SURFACE;
4060 }
4061
4062 pRenderTarget->idAssociatedContext = cid;
4063
4064#if 0 /* doesn't work */
4065 if ( !pRenderTarget->fStencilAsTexture
4066 && pRenderTarget->fDirty)
4067 {
4068 Log(("vmsvga3dSetRenderTarget: sync dirty depth/stencil buffer\n"));
4069 Assert(pRenderTarget->cLevels == 1);
4070
4071 for (uint32_t i = 0; i < pRenderTarget->cLevels; i++)
4072 {
4073 if (pRenderTarget->paMipmapLevels[i].fDirty)
4074 {
4075 D3DLOCKED_RECT LockedRect;
4076
4077 hr = pRenderTarget->u.pSurface->LockRect(&LockedRect,
4078 NULL, /* entire surface */
4079 0);
4080
4081 AssertMsgReturn(hr == D3D_OK, ("vmsvga3dSetRenderTarget: LockRect failed with %x\n", hr), VERR_INTERNAL_ERROR);
4082
4083 Log(("vmsvga3dSetRenderTarget: sync dirty texture mipmap level %d (pitch %x vs %x)\n", i, LockedRect.Pitch, pRenderTarget->paMipmapLevels[i].cbSurfacePitch));
4084
4085 uint8_t *pDest = (uint8_t *)LockedRect.pBits;
4086 uint8_t *pSrc = (uint8_t *)pRenderTarget->paMipmapLevels[i].pSurfaceData;
4087 for (uint32_t j = 0; j < pRenderTarget->paMipmapLevels[i].size.height; j++)
4088 {
4089 memcpy(pDest, pSrc, pRenderTarget->paMipmapLevels[i].cbSurfacePitch);
4090
4091 pDest += LockedRect.Pitch;
4092 pSrc += pRenderTarget->paMipmapLevels[i].cbSurfacePitch;
4093 }
4094
4095 hr = pRenderTarget->u.pSurface->UnlockRect();
4096 AssertMsgReturn(hr == D3D_OK, ("vmsvga3dSetRenderTarget: UnlockRect failed with %x\n", hr), VERR_INTERNAL_ERROR);
4097
4098 pRenderTarget->paMipmapLevels[i].fDirty = false;
4099 }
4100 }
4101 }
4102#endif
4103 }
4104
4105 /** @todo Assert(!pRenderTarget->fDirty); */
4106
4107 AssertReturn(pRenderTarget->u.pSurface, VERR_INVALID_PARAMETER);
4108
4109 pRenderTarget->fUsageD3D |= D3DUSAGE_DEPTHSTENCIL;
4110 pRenderTarget->f.surfaceFlags |= SVGA3D_SURFACE_HINT_DEPTHSTENCIL;
4111
4112 if (pRenderTarget->fStencilAsTexture)
4113 {
4114 IDirect3DSurface9 *pStencilSurface;
4115
4116 rc = vmsvga3dGetD3DSurface(pState, pContext, pRenderTarget, target.face, target.mipmap, /*fLockable=*/ false, &pStencilSurface);
4117 AssertRCReturn(rc, rc);
4118
4119 hr = pContext->pDevice->SetDepthStencilSurface(pStencilSurface);
4120 D3D_RELEASE(pStencilSurface);
4121 AssertMsgReturn(hr == D3D_OK, ("SetDepthStencilSurface failed with %x\n", hr), VERR_INTERNAL_ERROR);
4122 }
4123 else
4124 {
4125 Assert(pRenderTarget->idAssociatedContext == cid);
4126 AssertReturn(pRenderTarget->enmD3DResType == VMSVGA3D_D3DRESTYPE_SURFACE, VERR_INVALID_PARAMETER);
4127 hr = pContext->pDevice->SetDepthStencilSurface(pRenderTarget->u.pSurface);
4128 AssertMsgReturn(hr == D3D_OK, ("SetDepthStencilSurface failed with %x\n", hr), VERR_INTERNAL_ERROR);
4129 }
4130 break;
4131
4132 case SVGA3D_RT_COLOR0:
4133 case SVGA3D_RT_COLOR1:
4134 case SVGA3D_RT_COLOR2:
4135 case SVGA3D_RT_COLOR3:
4136 case SVGA3D_RT_COLOR4:
4137 case SVGA3D_RT_COLOR5:
4138 case SVGA3D_RT_COLOR6:
4139 case SVGA3D_RT_COLOR7:
4140 {
4141 IDirect3DSurface9 *pSurface;
4142 bool fTexture = false;
4143
4144 /* Must flush the other context's 3d pipeline to make sure all drawing is complete for the surface we're about to use. */
4145 vmsvga3dSurfaceFlush(pRenderTarget);
4146
4147 if (pRenderTarget->f.surfaceFlags & SVGA3D_SURFACE_HINT_TEXTURE)
4148 {
4149 fTexture = true;
4150
4151 /* A texture surface can be used as a render target to fill it and later on used as a texture. */
4152 if (!pRenderTarget->u.pTexture)
4153 {
4154 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));
4155 rc = vmsvga3dBackCreateTexture(pThisCC, pContext, cid, pRenderTarget);
4156 AssertRCReturn(rc, rc);
4157 }
4158
4159 rc = vmsvga3dGetD3DSurface(pState, pContext, pRenderTarget, target.face, target.mipmap, false, &pSurface);
4160 AssertRCReturn(rc, rc);
4161 }
4162 else
4163 {
4164 AssertReturn(target.face == 0 && target.mipmap == 0, VERR_INVALID_PARAMETER);
4165 if (!pRenderTarget->u.pSurface)
4166 {
4167 DWORD cQualityLevels = 0;
4168
4169 /* Query the nr of quality levels for this particular format */
4170 if (pRenderTarget->multiSampleTypeD3D != D3DMULTISAMPLE_NONE)
4171 {
4172 hr = pState->pD3D9->CheckDeviceMultiSampleType(D3DADAPTER_DEFAULT,
4173 D3DDEVTYPE_HAL,
4174 pRenderTarget->formatD3D,
4175 TRUE, /* Windowed */
4176 pRenderTarget->multiSampleTypeD3D,
4177 &cQualityLevels);
4178 Assert(hr == D3D_OK || hr == D3DERR_NOTAVAILABLE);
4179 }
4180
4181 LogFunc(("COLOR; cQualityLevels=%d\n", cQualityLevels));
4182 LogFunc(("Create rendertarget (%d,%d) formatD3D=%x multisample=%x\n",
4183 pRenderTarget->paMipmapLevels[0].mipmapSize.width, pRenderTarget->paMipmapLevels[0].mipmapSize.height, pRenderTarget->formatD3D, pRenderTarget->multiSampleTypeD3D));
4184
4185 hr = pContext->pDevice->CreateRenderTarget(pRenderTarget->paMipmapLevels[0].mipmapSize.width,
4186 pRenderTarget->paMipmapLevels[0].mipmapSize.height,
4187 pRenderTarget->formatD3D,
4188 pRenderTarget->multiSampleTypeD3D,
4189 ((cQualityLevels >= 1) ? cQualityLevels - 1 : 0), /* 0 - (levels-1) */
4190 TRUE, /* lockable */
4191 &pRenderTarget->u.pSurface,
4192 NULL);
4193 AssertReturn(hr == D3D_OK, VERR_INTERNAL_ERROR);
4194
4195 pRenderTarget->idAssociatedContext = cid;
4196 pRenderTarget->enmD3DResType = VMSVGA3D_D3DRESTYPE_SURFACE;
4197 }
4198 else
4199 AssertReturn(pRenderTarget->fUsageD3D & D3DUSAGE_RENDERTARGET, VERR_INVALID_PARAMETER);
4200
4201 Assert(pRenderTarget->idAssociatedContext == cid);
4202 AssertMsgReturn(pRenderTarget->enmD3DResType == VMSVGA3D_D3DRESTYPE_SURFACE,
4203 ("Invalid render target %#x\n", pRenderTarget->enmD3DResType),
4204 VERR_INVALID_PARAMETER);
4205 pSurface = pRenderTarget->u.pSurface;
4206 }
4207
4208 AssertReturn(pSurface, VERR_INVALID_PARAMETER);
4209 Assert(!pRenderTarget->fDirty);
4210
4211 pRenderTarget->fUsageD3D |= D3DUSAGE_RENDERTARGET;
4212 pRenderTarget->f.surfaceFlags |= SVGA3D_SURFACE_HINT_RENDERTARGET;
4213
4214 hr = pContext->pDevice->SetRenderTarget(type - SVGA3D_RT_COLOR0, pSurface);
4215 if (fTexture)
4216 D3D_RELEASE(pSurface); /* Release reference to texture level 0 */
4217 AssertMsgReturn(hr == D3D_OK, ("SetRenderTarget failed with %x\n", hr), VERR_INTERNAL_ERROR);
4218
4219 /* Changing the render target resets the viewport; restore it here. */
4220 if (pContext->state.u32UpdateFlags & VMSVGA3D_UPDATE_VIEWPORT)
4221 vmsvga3dBackSetViewPort(pThisCC, cid, &pContext->state.RectViewPort);
4222 if (pContext->state.u32UpdateFlags & VMSVGA3D_UPDATE_ZRANGE)
4223 vmsvga3dBackSetZRange(pThisCC, cid, pContext->state.zRange);
4224 /* Changing the render target also resets the scissor rectangle; restore it as well. */
4225 if (pContext->state.u32UpdateFlags & VMSVGA3D_UPDATE_SCISSORRECT)
4226 vmsvga3dBackSetScissorRect(pThisCC, cid, &pContext->state.RectScissor);
4227
4228 break;
4229 }
4230
4231 default:
4232 AssertFailedReturn(VERR_INVALID_PARAMETER);
4233 }
4234
4235 return VINF_SUCCESS;
4236}
4237
4238/**
4239 * Convert SVGA texture combiner value to its D3D equivalent
4240 */
4241static DWORD vmsvga3dTextureCombiner2D3D(uint32_t value)
4242{
4243 switch (value)
4244 {
4245 case SVGA3D_TC_DISABLE:
4246 return D3DTOP_DISABLE;
4247 case SVGA3D_TC_SELECTARG1:
4248 return D3DTOP_SELECTARG1;
4249 case SVGA3D_TC_SELECTARG2:
4250 return D3DTOP_SELECTARG2;
4251 case SVGA3D_TC_MODULATE:
4252 return D3DTOP_MODULATE;
4253 case SVGA3D_TC_ADD:
4254 return D3DTOP_ADD;
4255 case SVGA3D_TC_ADDSIGNED:
4256 return D3DTOP_ADDSIGNED;
4257 case SVGA3D_TC_SUBTRACT:
4258 return D3DTOP_SUBTRACT;
4259 case SVGA3D_TC_BLENDTEXTUREALPHA:
4260 return D3DTOP_BLENDTEXTUREALPHA;
4261 case SVGA3D_TC_BLENDDIFFUSEALPHA:
4262 return D3DTOP_BLENDDIFFUSEALPHA;
4263 case SVGA3D_TC_BLENDCURRENTALPHA:
4264 return D3DTOP_BLENDCURRENTALPHA;
4265 case SVGA3D_TC_BLENDFACTORALPHA:
4266 return D3DTOP_BLENDFACTORALPHA;
4267 case SVGA3D_TC_MODULATE2X:
4268 return D3DTOP_MODULATE2X;
4269 case SVGA3D_TC_MODULATE4X:
4270 return D3DTOP_MODULATE4X;
4271 case SVGA3D_TC_DSDT:
4272 AssertFailed(); /** @todo ??? */
4273 return D3DTOP_DISABLE;
4274 case SVGA3D_TC_DOTPRODUCT3:
4275 return D3DTOP_DOTPRODUCT3;
4276 case SVGA3D_TC_BLENDTEXTUREALPHAPM:
4277 return D3DTOP_BLENDTEXTUREALPHAPM;
4278 case SVGA3D_TC_ADDSIGNED2X:
4279 return D3DTOP_ADDSIGNED2X;
4280 case SVGA3D_TC_ADDSMOOTH:
4281 return D3DTOP_ADDSMOOTH;
4282 case SVGA3D_TC_PREMODULATE:
4283 return D3DTOP_PREMODULATE;
4284 case SVGA3D_TC_MODULATEALPHA_ADDCOLOR:
4285 return D3DTOP_MODULATEALPHA_ADDCOLOR;
4286 case SVGA3D_TC_MODULATECOLOR_ADDALPHA:
4287 return D3DTOP_MODULATECOLOR_ADDALPHA;
4288 case SVGA3D_TC_MODULATEINVALPHA_ADDCOLOR:
4289 return D3DTOP_MODULATEINVALPHA_ADDCOLOR;
4290 case SVGA3D_TC_MODULATEINVCOLOR_ADDALPHA:
4291 return D3DTOP_MODULATEINVCOLOR_ADDALPHA;
4292 case SVGA3D_TC_BUMPENVMAPLUMINANCE:
4293 return D3DTOP_BUMPENVMAPLUMINANCE;
4294 case SVGA3D_TC_MULTIPLYADD:
4295 return D3DTOP_MULTIPLYADD;
4296 case SVGA3D_TC_LERP:
4297 return D3DTOP_LERP;
4298 default:
4299 AssertFailed();
4300 return D3DTOP_DISABLE;
4301 }
4302}
4303
4304/**
4305 * Convert SVGA texture arg data value to its D3D equivalent
4306 */
4307static DWORD vmsvga3dTextureArgData2D3D(uint32_t value)
4308{
4309 switch (value)
4310 {
4311 case SVGA3D_TA_CONSTANT:
4312 return D3DTA_CONSTANT;
4313 case SVGA3D_TA_PREVIOUS:
4314 return D3DTA_CURRENT; /* current = previous */
4315 case SVGA3D_TA_DIFFUSE:
4316 return D3DTA_DIFFUSE;
4317 case SVGA3D_TA_TEXTURE:
4318 return D3DTA_TEXTURE;
4319 case SVGA3D_TA_SPECULAR:
4320 return D3DTA_SPECULAR;
4321 default:
4322 AssertFailed();
4323 return D3DTA_DIFFUSE;
4324 }
4325}
4326
4327/**
4328 * Convert SVGA texture transform flag value to its D3D equivalent
4329 */
4330static DWORD vmsvga3dTextTransformFlags2D3D(uint32_t value)
4331{
4332 switch (value)
4333 {
4334 case SVGA3D_TEX_TRANSFORM_OFF:
4335 return D3DTTFF_DISABLE;
4336 case SVGA3D_TEX_TRANSFORM_S:
4337 return D3DTTFF_COUNT1; /** @todo correct? */
4338 case SVGA3D_TEX_TRANSFORM_T:
4339 return D3DTTFF_COUNT2; /** @todo correct? */
4340 case SVGA3D_TEX_TRANSFORM_R:
4341 return D3DTTFF_COUNT3; /** @todo correct? */
4342 case SVGA3D_TEX_TRANSFORM_Q:
4343 return D3DTTFF_COUNT4; /** @todo correct? */
4344 case SVGA3D_TEX_PROJECTED:
4345 return D3DTTFF_PROJECTED;
4346 default:
4347 AssertFailed();
4348 return D3DTTFF_DISABLE;
4349 }
4350}
4351
4352static DWORD vmsvga3dSamplerIndex2D3D(uint32_t idxSampler)
4353{
4354 if (idxSampler < SVGA3D_MAX_SAMPLERS_PS)
4355 return idxSampler;
4356 return (idxSampler - SVGA3D_MAX_SAMPLERS_PS) + D3DDMAPSAMPLER;
4357}
4358
4359static DECLCALLBACK(int) vmsvga3dBackSetTextureState(PVGASTATECC pThisCC, uint32_t cid, uint32_t cTextureStates, SVGA3dTextureState *pTextureState)
4360{
4361 DWORD val = 0; /* Shut up MSC */
4362 HRESULT hr;
4363 PVMSVGA3DCONTEXT pContext;
4364 PVMSVGA3DSTATE pState = pThisCC->svga.p3dState;
4365 AssertReturn(pState, VERR_NO_MEMORY);
4366
4367 LogFunc(("%x cTextureState=%d\n", cid, cTextureStates));
4368
4369 int rc = vmsvga3dContextFromCid(pState, cid, &pContext);
4370 AssertRCReturn(rc, rc);
4371 AssertReturn(pContext->pDevice, VERR_INTERNAL_ERROR);
4372
4373 for (unsigned i = 0; i < cTextureStates; i++)
4374 {
4375 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));
4376
4377 if (pTextureState[i].name == SVGA3D_TS_BIND_TEXTURE)
4378 {
4379 /* Special case: binding a texture to a sampler. Stage is the sampler index. */
4380 const uint32_t sid = pTextureState[i].value;
4381 const uint32_t idxSampler = pTextureState[i].stage;
4382
4383 if (RT_UNLIKELY(idxSampler >= SVGA3D_MAX_SAMPLERS))
4384 {
4385 AssertMsgFailed(("pTextureState[%d]: SVGA3D_TS_BIND_TEXTURE idxSampler=%d, sid=%u\n", i, idxSampler, sid));
4386 continue;
4387 }
4388
4389 const DWORD d3dSampler = vmsvga3dSamplerIndex2D3D(idxSampler);
4390 if (sid == SVGA3D_INVALID_ID)
4391 {
4392 LogFunc(("SVGA3D_TS_BIND_TEXTURE: unbind sampler=%d\n", idxSampler));
4393
4394 pContext->aSidActiveTextures[idxSampler] = SVGA3D_INVALID_ID;
4395
4396 /* Unselect the currently associated texture. */
4397 hr = pContext->pDevice->SetTexture(d3dSampler, NULL);
4398 AssertMsgReturn(hr == D3D_OK, ("SetTexture failed with %x\n", hr), VERR_INTERNAL_ERROR);
4399 }
4400 else
4401 {
4402 PVMSVGA3DSURFACE pSurface;
4403 rc = vmsvga3dSurfaceFromSid(pState, sid, &pSurface);
4404 AssertRCReturn(rc, rc);
4405
4406 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));
4407
4408 if (!pSurface->u.pTexture)
4409 {
4410 Assert(pSurface->idAssociatedContext == SVGA3D_INVALID_ID);
4411 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));
4412 rc = vmsvga3dBackCreateTexture(pThisCC, pContext, cid, pSurface);
4413 AssertRCReturn(rc, rc);
4414 }
4415 else
4416 {
4417 /* Must flush the other context's 3d pipeline to make sure all drawing is complete for the surface we're about to use. */
4418 vmsvga3dSurfaceFlush(pSurface);
4419 }
4420
4421 AssertReturn( pSurface->enmD3DResType == VMSVGA3D_D3DRESTYPE_TEXTURE
4422 || pSurface->enmD3DResType == VMSVGA3D_D3DRESTYPE_CUBE_TEXTURE
4423 || pSurface->enmD3DResType == VMSVGA3D_D3DRESTYPE_VOLUME_TEXTURE,
4424 VERR_INVALID_PARAMETER);
4425
4426 if (pSurface->idAssociatedContext != cid)
4427 {
4428 LogFunc(("Using texture sid=%u created for another context (%d vs %d)\n", sid, pSurface->idAssociatedContext, cid));
4429
4430 PVMSVGA3DSHAREDSURFACE pSharedSurface = vmsvga3dSurfaceGetSharedCopy(pState, pContext, pSurface);
4431 AssertReturn(pSharedSurface, VERR_INTERNAL_ERROR);
4432
4433 hr = pContext->pDevice->SetTexture(d3dSampler, pSharedSurface->u.pTexture);
4434 }
4435 else
4436 hr = pContext->pDevice->SetTexture(d3dSampler, pSurface->u.pTexture);
4437
4438 AssertMsgReturn(hr == D3D_OK, ("SetTexture failed with %x\n", hr), VERR_INTERNAL_ERROR);
4439
4440 pContext->aSidActiveTextures[idxSampler] = sid;
4441 }
4442 /* Finished; continue with the next one. */
4443 continue;
4444 }
4445
4446 D3DTEXTURESTAGESTATETYPE textureType = D3DTSS_FORCE_DWORD;
4447 D3DSAMPLERSTATETYPE samplerType = D3DSAMP_FORCE_DWORD;
4448 switch (pTextureState[i].name)
4449 {
4450 case SVGA3D_TS_COLOROP: /* SVGA3dTextureCombiner */
4451 textureType = D3DTSS_COLOROP;
4452 val = vmsvga3dTextureCombiner2D3D(pTextureState[i].value);
4453 break;
4454
4455 case SVGA3D_TS_COLORARG0: /* SVGA3dTextureArgData */
4456 textureType = D3DTSS_COLORARG0;
4457 val = vmsvga3dTextureArgData2D3D(pTextureState[i].value);
4458 break;
4459
4460 case SVGA3D_TS_COLORARG1: /* SVGA3dTextureArgData */
4461 textureType = D3DTSS_COLORARG1;
4462 val = vmsvga3dTextureArgData2D3D(pTextureState[i].value);
4463 break;
4464
4465 case SVGA3D_TS_COLORARG2: /* SVGA3dTextureArgData */
4466 textureType = D3DTSS_COLORARG2;
4467 val = vmsvga3dTextureArgData2D3D(pTextureState[i].value);
4468 break;
4469
4470 case SVGA3D_TS_ALPHAOP: /* SVGA3dTextureCombiner */
4471 textureType = D3DTSS_ALPHAOP;
4472 val = vmsvga3dTextureCombiner2D3D(pTextureState[i].value);
4473 break;
4474
4475 case SVGA3D_TS_ALPHAARG0: /* SVGA3dTextureArgData */
4476 textureType = D3DTSS_ALPHAARG0;
4477 val = vmsvga3dTextureArgData2D3D(pTextureState[i].value);
4478 break;
4479
4480 case SVGA3D_TS_ALPHAARG1: /* SVGA3dTextureArgData */
4481 textureType = D3DTSS_ALPHAARG1;
4482 val = vmsvga3dTextureArgData2D3D(pTextureState[i].value);
4483 break;
4484
4485 case SVGA3D_TS_ALPHAARG2: /* SVGA3dTextureArgData */
4486 textureType = D3DTSS_ALPHAARG2;
4487 val = vmsvga3dTextureArgData2D3D(pTextureState[i].value);
4488 break;
4489
4490 case SVGA3D_TS_BUMPENVMAT00: /* float */
4491 textureType = D3DTSS_BUMPENVMAT00;
4492 val = pTextureState[i].value;
4493 break;
4494
4495 case SVGA3D_TS_BUMPENVMAT01: /* float */
4496 textureType = D3DTSS_BUMPENVMAT01;
4497 val = pTextureState[i].value;
4498 break;
4499
4500 case SVGA3D_TS_BUMPENVMAT10: /* float */
4501 textureType = D3DTSS_BUMPENVMAT10;
4502 val = pTextureState[i].value;
4503 break;
4504
4505 case SVGA3D_TS_BUMPENVMAT11: /* float */
4506 textureType = D3DTSS_BUMPENVMAT11;
4507 val = pTextureState[i].value;
4508 break;
4509
4510 case SVGA3D_TS_TEXCOORDINDEX: /* uint32_t */
4511 textureType = D3DTSS_TEXCOORDINDEX;
4512 val = pTextureState[i].value;
4513 break;
4514
4515 case SVGA3D_TS_BUMPENVLSCALE: /* float */
4516 textureType = D3DTSS_BUMPENVLSCALE;
4517 val = pTextureState[i].value;
4518 break;
4519
4520 case SVGA3D_TS_BUMPENVLOFFSET: /* float */
4521 textureType = D3DTSS_BUMPENVLOFFSET;
4522 val = pTextureState[i].value;
4523 break;
4524
4525 case SVGA3D_TS_TEXTURETRANSFORMFLAGS: /* SVGA3dTexTransformFlags */
4526 textureType = D3DTSS_TEXTURETRANSFORMFLAGS;
4527 val = vmsvga3dTextTransformFlags2D3D(pTextureState[i].value);
4528 break;
4529
4530 case SVGA3D_TS_ADDRESSW: /* SVGA3dTextureAddress */
4531 samplerType = D3DSAMP_ADDRESSW;
4532 val = pTextureState[i].value; /* Identical otherwise */
4533 Assert(pTextureState[i].value != SVGA3D_TEX_ADDRESS_EDGE);
4534 break;
4535
4536 case SVGA3D_TS_ADDRESSU: /* SVGA3dTextureAddress */
4537 samplerType = D3DSAMP_ADDRESSU;
4538 val = pTextureState[i].value; /* Identical otherwise */
4539 Assert(pTextureState[i].value != SVGA3D_TEX_ADDRESS_EDGE);
4540 break;
4541
4542 case SVGA3D_TS_ADDRESSV: /* SVGA3dTextureAddress */
4543 samplerType = D3DSAMP_ADDRESSV;
4544 val = pTextureState[i].value; /* Identical otherwise */
4545 Assert(pTextureState[i].value != SVGA3D_TEX_ADDRESS_EDGE);
4546 break;
4547
4548 case SVGA3D_TS_MIPFILTER: /* SVGA3dTextureFilter */
4549 samplerType = D3DSAMP_MIPFILTER;
4550 val = pTextureState[i].value; /* Identical otherwise */
4551 Assert(pTextureState[i].value != SVGA3D_TEX_FILTER_FLATCUBIC);
4552 Assert(pTextureState[i].value != SVGA3D_TEX_FILTER_GAUSSIANCUBIC);
4553 break;
4554
4555 case SVGA3D_TS_MAGFILTER: /* SVGA3dTextureFilter */
4556 samplerType = D3DSAMP_MAGFILTER;
4557 val = pTextureState[i].value; /* Identical otherwise */
4558 Assert(pTextureState[i].value != SVGA3D_TEX_FILTER_FLATCUBIC);
4559 Assert(pTextureState[i].value != SVGA3D_TEX_FILTER_GAUSSIANCUBIC);
4560 break;
4561
4562 case SVGA3D_TS_MINFILTER: /* SVGA3dTextureFilter */
4563 samplerType = D3DSAMP_MINFILTER;
4564 val = pTextureState[i].value; /* Identical otherwise */
4565 Assert(pTextureState[i].value != SVGA3D_TEX_FILTER_FLATCUBIC);
4566 Assert(pTextureState[i].value != SVGA3D_TEX_FILTER_GAUSSIANCUBIC);
4567 break;
4568
4569 case SVGA3D_TS_BORDERCOLOR: /* SVGA3dColor */
4570 samplerType = D3DSAMP_BORDERCOLOR;
4571 val = pTextureState[i].value; /* Identical */
4572 break;
4573
4574 case SVGA3D_TS_TEXTURE_LOD_BIAS: /* float */
4575 samplerType = D3DSAMP_MIPMAPLODBIAS;
4576 val = pTextureState[i].value; /* Identical */
4577 break;
4578
4579 case SVGA3D_TS_TEXTURE_MIPMAP_LEVEL: /* uint32_t */
4580 samplerType = D3DSAMP_MAXMIPLEVEL;
4581 val = pTextureState[i].value; /* Identical?? */
4582 break;
4583
4584 case SVGA3D_TS_TEXTURE_ANISOTROPIC_LEVEL: /* uint32_t */
4585 samplerType = D3DSAMP_MAXANISOTROPY;
4586 val = pTextureState[i].value; /* Identical?? */
4587 break;
4588
4589 case SVGA3D_TS_GAMMA: /* float */
4590 samplerType = D3DSAMP_SRGBTEXTURE;
4591 /* Boolean in D3D */
4592 if (pTextureState[i].floatValue == 1.0f)
4593 val = FALSE;
4594 else
4595 val = TRUE;
4596 break;
4597
4598 /* Internal commands, that don't map directly to the SetTextureStageState API. */
4599 case SVGA3D_TS_TEXCOORDGEN: /* SVGA3dTextureCoordGen */
4600 AssertFailed();
4601 break;
4602
4603 case SVGA3D_TS_MAX: /* shut up MSC */
4604 case SVGA3D_TS_INVALID:
4605 case SVGA3D_TS_BIND_TEXTURE:
4606 AssertFailedBreak();
4607 default: /** @todo Remaining TSs. Avoid MSC warning for now. */
4608 break;
4609 }
4610
4611 const uint32_t currentStage = pTextureState[i].stage;
4612 /* Record the texture state for vm state saving. */
4613 if ( currentStage < RT_ELEMENTS(pContext->state.aTextureStates)
4614 && (unsigned)pTextureState[i].name < RT_ELEMENTS(pContext->state.aTextureStates[0]))
4615 {
4616 pContext->state.aTextureStates[currentStage][pTextureState[i].name] = pTextureState[i];
4617 }
4618
4619 if (textureType != D3DTSS_FORCE_DWORD)
4620 {
4621 if (RT_UNLIKELY(currentStage >= SVGA3D_MAX_TEXTURE_STAGES))
4622 {
4623 AssertMsgFailed(("pTextureState[%d].stage=%#x name=%#x value=%#x\n", i, pTextureState[i].stage, pTextureState[i].name, pTextureState[i].value));
4624 continue;
4625 }
4626
4627 hr = pContext->pDevice->SetTextureStageState(currentStage, textureType, val);
4628 AssertMsg(hr == D3D_OK, ("SetTextureStageState failed with %x\n", hr));
4629 }
4630 else if (samplerType != D3DSAMP_FORCE_DWORD)
4631 {
4632 if (RT_UNLIKELY(currentStage >= SVGA3D_MAX_SAMPLERS))
4633 {
4634 AssertMsgFailed(("pTextureState[%d].stage=%#x name=%#x value=%#x\n", i, pTextureState[i].stage, pTextureState[i].name, pTextureState[i].value));
4635 continue;
4636 }
4637
4638 hr = pContext->pDevice->SetSamplerState(currentStage, samplerType, val);
4639 AssertMsg(hr == D3D_OK, ("SetSamplerState failed with %x\n", hr));
4640 }
4641 else
4642 {
4643 AssertFailed();
4644 }
4645 }
4646
4647 return VINF_SUCCESS;
4648}
4649
4650static DECLCALLBACK(int) vmsvga3dBackSetMaterial(PVGASTATECC pThisCC, uint32_t cid, SVGA3dFace face, SVGA3dMaterial *pMaterial)
4651{
4652 HRESULT hr;
4653 PVMSVGA3DSTATE pState = pThisCC->svga.p3dState;
4654 AssertReturn(pState, VERR_NO_MEMORY);
4655
4656 LogFunc(("cid=%u face %d\n", cid, face));
4657
4658 PVMSVGA3DCONTEXT pContext;
4659 int rc = vmsvga3dContextFromCid(pState, cid, &pContext);
4660 AssertRCReturn(rc, rc);
4661 AssertReturn(pContext->pDevice, VERR_INTERNAL_ERROR);
4662
4663 AssertReturn((unsigned)face < SVGA3D_FACE_MAX, VERR_INVALID_PARAMETER);
4664
4665 /* Save for vm state save/restore. */
4666 pContext->state.aMaterial[face].fValid = true;
4667 pContext->state.aMaterial[face].material = *pMaterial;
4668 pContext->state.u32UpdateFlags |= VMSVGA3D_UPDATE_MATERIAL;
4669
4670 /* @note face not used for D3D9 */
4671 /** @todo ignore everything except SVGA3D_FACE_NONE? */
4672 //Assert(face == SVGA3D_FACE_NONE);
4673 if (face != SVGA3D_FACE_NONE)
4674 Log(("Unsupported face %d!!\n", face));
4675
4676 D3DMATERIAL9 material;
4677 material.Diffuse.r = pMaterial->diffuse[0];
4678 material.Diffuse.g = pMaterial->diffuse[1];
4679 material.Diffuse.b = pMaterial->diffuse[2];
4680 material.Diffuse.a = pMaterial->diffuse[3];
4681 material.Ambient.r = pMaterial->ambient[0];
4682 material.Ambient.g = pMaterial->ambient[1];
4683 material.Ambient.b = pMaterial->ambient[2];
4684 material.Ambient.a = pMaterial->ambient[3];
4685 material.Specular.r = pMaterial->specular[0];
4686 material.Specular.g = pMaterial->specular[1];
4687 material.Specular.b = pMaterial->specular[2];
4688 material.Specular.a = pMaterial->specular[3];
4689 material.Emissive.r = pMaterial->emissive[0];
4690 material.Emissive.g = pMaterial->emissive[1];
4691 material.Emissive.b = pMaterial->emissive[2];
4692 material.Emissive.a = pMaterial->emissive[3];
4693 material.Power = pMaterial->shininess;
4694
4695 hr = pContext->pDevice->SetMaterial(&material);
4696 AssertMsgReturn(hr == D3D_OK, ("SetMaterial failed with %x\n", hr), VERR_INTERNAL_ERROR);
4697
4698 return VINF_SUCCESS;
4699}
4700
4701static DECLCALLBACK(int) vmsvga3dBackSetLightData(PVGASTATECC pThisCC, uint32_t cid, uint32_t index, SVGA3dLightData *pData)
4702{
4703 HRESULT hr;
4704 D3DLIGHT9 light;
4705 PVMSVGA3DCONTEXT pContext;
4706 PVMSVGA3DSTATE pState = pThisCC->svga.p3dState;
4707 AssertReturn(pState, VERR_NO_MEMORY);
4708
4709 Log(("vmsvga3dSetLightData %x index=%d\n", cid, index));
4710 ASSERT_GUEST_RETURN(index < SVGA3D_MAX_LIGHTS, VERR_INVALID_PARAMETER);
4711
4712 int rc = vmsvga3dContextFromCid(pState, cid, &pContext);
4713 AssertRCReturn(rc, rc);
4714 AssertReturn(pContext->pDevice, VERR_INTERNAL_ERROR);
4715
4716 switch (pData->type)
4717 {
4718 case SVGA3D_LIGHTTYPE_POINT:
4719 light.Type = D3DLIGHT_POINT;
4720 break;
4721
4722 case SVGA3D_LIGHTTYPE_SPOT1: /* 1-cone, in degrees */
4723 light.Type = D3DLIGHT_SPOT;
4724 break;
4725
4726 case SVGA3D_LIGHTTYPE_DIRECTIONAL:
4727 light.Type = D3DLIGHT_DIRECTIONAL;
4728 break;
4729
4730 case SVGA3D_LIGHTTYPE_SPOT2: /* 2-cone, in radians */
4731 default:
4732 Log(("Unsupported light type!!\n"));
4733 return VERR_INVALID_PARAMETER;
4734 }
4735
4736 /* Store for vm state save/restore */
4737 pContext->state.aLightData[index].fValidData = true;
4738 pContext->state.aLightData[index].data = *pData;
4739
4740 light.Diffuse.r = pData->diffuse[0];
4741 light.Diffuse.g = pData->diffuse[1];
4742 light.Diffuse.b = pData->diffuse[2];
4743 light.Diffuse.a = pData->diffuse[3];
4744 light.Specular.r = pData->specular[0];
4745 light.Specular.g = pData->specular[1];
4746 light.Specular.b = pData->specular[2];
4747 light.Specular.a = pData->specular[3];
4748 light.Ambient.r = pData->ambient[0];
4749 light.Ambient.g = pData->ambient[1];
4750 light.Ambient.b = pData->ambient[2];
4751 light.Ambient.a = pData->ambient[3];
4752 light.Position.x = pData->position[0];
4753 light.Position.y = pData->position[1];
4754 light.Position.z = pData->position[2]; /* @note 4th position not available in D3D9 */
4755 light.Direction.x = pData->direction[0];
4756 light.Direction.y = pData->direction[1];
4757 light.Direction.z = pData->direction[2]; /* @note 4th position not available in D3D9 */
4758 light.Range = pData->range;
4759 light.Falloff = pData->falloff;
4760 light.Attenuation0 = pData->attenuation0;
4761 light.Attenuation1 = pData->attenuation1;
4762 light.Attenuation2 = pData->attenuation2;
4763 light.Theta = pData->theta;
4764 light.Phi = pData->phi;
4765
4766 hr = pContext->pDevice->SetLight(index, &light);
4767 AssertMsgReturn(hr == D3D_OK, ("SetLight failed with %x\n", hr), VERR_INTERNAL_ERROR);
4768
4769 return VINF_SUCCESS;
4770}
4771
4772static DECLCALLBACK(int) vmsvga3dBackSetLightEnabled(PVGASTATECC pThisCC, uint32_t cid, uint32_t index, uint32_t enabled)
4773{
4774 HRESULT hr;
4775 PVMSVGA3DCONTEXT pContext;
4776 PVMSVGA3DSTATE pState = pThisCC->svga.p3dState;
4777 AssertReturn(pState, VERR_NO_MEMORY);
4778
4779 Log(("vmsvga3dSetLightEnabled %x %d -> %d\n", cid, index, enabled));
4780 AssertReturn(index < SVGA3D_MAX_LIGHTS, VERR_INVALID_PARAMETER);
4781
4782 int rc = vmsvga3dContextFromCid(pState, cid, &pContext);
4783 AssertRCReturn(rc, rc);
4784 AssertReturn(pContext->pDevice, VERR_INTERNAL_ERROR);
4785
4786 /* Store for vm state save/restore */
4787 pContext->state.aLightData[index].fEnabled = !!enabled;
4788
4789 hr = pContext->pDevice->LightEnable(index, (BOOL)enabled);
4790 AssertMsgReturn(hr == D3D_OK, ("LightEnable failed with %x\n", hr), VERR_INTERNAL_ERROR);
4791
4792 return VINF_SUCCESS;
4793}
4794
4795static DECLCALLBACK(int) vmsvga3dBackSetViewPort(PVGASTATECC pThisCC, uint32_t cid, SVGA3dRect *pRect)
4796{
4797 HRESULT hr;
4798 D3DVIEWPORT9 viewPort;
4799 PVMSVGA3DCONTEXT pContext;
4800 PVMSVGA3DSTATE pState = pThisCC->svga.p3dState;
4801 AssertReturn(pState, VERR_NO_MEMORY);
4802
4803 Log(("vmsvga3dSetViewPort %x (%d,%d)(%d,%d)\n", cid, pRect->x, pRect->y, pRect->w, pRect->h));
4804
4805 int rc = vmsvga3dContextFromCid(pState, cid, &pContext);
4806 AssertRCReturn(rc, rc);
4807 AssertReturn(pContext->pDevice, VERR_INTERNAL_ERROR);
4808
4809 /* Save for vm state save/restore. */
4810 pContext->state.RectViewPort = *pRect;
4811 pContext->state.u32UpdateFlags |= VMSVGA3D_UPDATE_VIEWPORT;
4812
4813 hr = pContext->pDevice->GetViewport(&viewPort);
4814 AssertMsgReturn(hr == D3D_OK, ("GetViewport failed with %x\n", hr), VERR_INTERNAL_ERROR);
4815
4816 viewPort.X = pRect->x;
4817 viewPort.Y = pRect->y;
4818 viewPort.Width = pRect->w;
4819 viewPort.Height = pRect->h;
4820 /* viewPort.MinZ & MaxZ are not changed from the current setting. */
4821
4822 hr = pContext->pDevice->SetViewport(&viewPort);
4823 AssertMsgReturn(hr == D3D_OK, ("SetViewport failed with %x\n", hr), VERR_INTERNAL_ERROR);
4824
4825 return VINF_SUCCESS;
4826}
4827
4828static DECLCALLBACK(int) vmsvga3dBackSetClipPlane(PVGASTATECC pThisCC, uint32_t cid, uint32_t index, float plane[4])
4829{
4830 HRESULT hr;
4831 PVMSVGA3DCONTEXT pContext;
4832 PVMSVGA3DSTATE pState = pThisCC->svga.p3dState;
4833 AssertReturn(pState, VERR_NO_MEMORY);
4834
4835 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)));
4836 AssertReturn(index < SVGA3D_NUM_CLIPPLANES, VERR_INVALID_PARAMETER);
4837
4838 int rc = vmsvga3dContextFromCid(pState, cid, &pContext);
4839 AssertRCReturn(rc, rc);
4840 AssertReturn(pContext->pDevice, VERR_INTERNAL_ERROR);
4841
4842 /* Store for vm state save/restore. */
4843 pContext->state.aClipPlane[index].fValid = true;
4844 memcpy(pContext->state.aClipPlane[index].plane, plane, sizeof(pContext->state.aClipPlane[index].plane));
4845
4846 hr = pContext->pDevice->SetClipPlane(index, plane);
4847 AssertMsgReturn(hr == D3D_OK, ("SetClipPlane failed with %x\n", hr), VERR_INTERNAL_ERROR);
4848 return VINF_SUCCESS;
4849}
4850
4851static DECLCALLBACK(int) vmsvga3dBackCommandClear(PVGASTATECC pThisCC, uint32_t cid, SVGA3dClearFlag clearFlag, uint32_t color, float depth,
4852 uint32_t stencil, uint32_t cRects, SVGA3dRect *pRect)
4853{
4854 /* From SVGA3D_BeginClear comments:
4855 *
4856 * Clear is not affected by clipping, depth test, or other
4857 * render state which affects the fragment pipeline.
4858 *
4859 * Therefore this code must ignore the current scissor rect.
4860 */
4861
4862 DWORD clearFlagD3D = 0;
4863 D3DRECT *pRectD3D = NULL;
4864 HRESULT hr;
4865 PVMSVGA3DCONTEXT pContext;
4866 PVMSVGA3DSTATE pState = pThisCC->svga.p3dState;
4867 AssertReturn(pState, VERR_NO_MEMORY);
4868
4869 Log(("vmsvga3dCommandClear %x clearFlag=%x color=%x depth=%d stencil=%x cRects=%d\n", cid, clearFlag, color, (uint32_t)(depth * 100.0), stencil, cRects));
4870
4871 int rc = vmsvga3dContextFromCid(pState, cid, &pContext);
4872 AssertRCReturn(rc, rc);
4873 AssertReturn(pContext->pDevice, VERR_INTERNAL_ERROR);
4874
4875 PVMSVGA3DSURFACE pRT;
4876 rc = vmsvga3dSurfaceFromSid(pState, pContext->state.aRenderTargets[SVGA3D_RT_COLOR0], &pRT);
4877 AssertRCReturn(rc, rc);
4878
4879 if (clearFlag & SVGA3D_CLEAR_COLOR)
4880 clearFlagD3D |= D3DCLEAR_TARGET;
4881 if (clearFlag & SVGA3D_CLEAR_STENCIL)
4882 clearFlagD3D |= D3DCLEAR_STENCIL;
4883 if (clearFlag & SVGA3D_CLEAR_DEPTH)
4884 clearFlagD3D |= D3DCLEAR_ZBUFFER;
4885
4886 if (cRects)
4887 {
4888 pRectD3D = (D3DRECT *)RTMemAlloc(sizeof(D3DRECT) * cRects);
4889 AssertReturn(pRectD3D, VERR_NO_MEMORY);
4890
4891 for (unsigned i=0; i < cRects; i++)
4892 {
4893 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));
4894 pRectD3D[i].x1 = pRect[i].x;
4895 pRectD3D[i].y1 = pRect[i].y;
4896 pRectD3D[i].x2 = pRect[i].x + pRect[i].w; /* exclusive */
4897 pRectD3D[i].y2 = pRect[i].y + pRect[i].h; /* exclusive */
4898 }
4899 }
4900
4901 RECT currentScissorRect;
4902 pContext->pDevice->GetScissorRect(&currentScissorRect);
4903
4904 RECT clearScissorRect;
4905 clearScissorRect.left = 0;
4906 clearScissorRect.top = 0;
4907 clearScissorRect.right = pRT->paMipmapLevels[0].mipmapSize.width;
4908 clearScissorRect.bottom = pRT->paMipmapLevels[0].mipmapSize.height;
4909 pContext->pDevice->SetScissorRect(&clearScissorRect);
4910
4911 hr = pContext->pDevice->Clear(cRects, pRectD3D, clearFlagD3D, (D3DCOLOR)color, depth, stencil);
4912
4913 if (pRectD3D)
4914 RTMemFree(pRectD3D);
4915
4916 pContext->pDevice->SetScissorRect(&currentScissorRect);
4917
4918 AssertMsgReturn(hr == D3D_OK, ("Clear failed with %x\n", hr), VERR_INTERNAL_ERROR);
4919
4920 /* Make sure we can track drawing usage of active render targets. */
4921 for (uint32_t i = 0; i < RT_ELEMENTS(pContext->state.aRenderTargets); ++i)
4922 if (pContext->state.aRenderTargets[i] != SVGA3D_INVALID_ID)
4923 vmsvga3dSurfaceTrackUsageById(pState, pContext, pContext->state.aRenderTargets[i]);
4924
4925 return VINF_SUCCESS;
4926}
4927
4928/* Convert VMWare vertex declaration to its D3D equivalent. */
4929static int vmsvga3dVertexDecl2D3D(const SVGA3dVertexArrayIdentity &identity, D3DVERTEXELEMENT9 *pVertexElement)
4930{
4931 /* usage, method and type are identical; make sure. */
4932 AssertCompile(SVGA3D_DECLTYPE_FLOAT1 == D3DDECLTYPE_FLOAT1);
4933 AssertCompile(SVGA3D_DECLTYPE_FLOAT16_4 == D3DDECLTYPE_FLOAT16_4);
4934 AssertCompile(SVGA3D_DECLMETHOD_DEFAULT == D3DDECLMETHOD_DEFAULT);
4935 AssertCompile(SVGA3D_DECLMETHOD_LOOKUPPRESAMPLED == D3DDECLMETHOD_LOOKUPPRESAMPLED);
4936 AssertCompile(D3DDECLUSAGE_POSITION == SVGA3D_DECLUSAGE_POSITION);
4937 AssertCompile(D3DDECLUSAGE_SAMPLE == SVGA3D_DECLUSAGE_SAMPLE);
4938
4939 pVertexElement->Stream = 0;
4940 pVertexElement->Offset = 0;
4941 pVertexElement->Type = identity.type;
4942 pVertexElement->Method = identity.method;
4943 pVertexElement->Usage = identity.usage;
4944 pVertexElement->UsageIndex = identity.usageIndex;
4945 return VINF_SUCCESS;
4946}
4947
4948/* Convert VMWare primitive type to its D3D equivalent. */
4949static int vmsvga3dPrimitiveType2D3D(SVGA3dPrimitiveType PrimitiveType, D3DPRIMITIVETYPE *pPrimitiveTypeD3D)
4950{
4951 switch (PrimitiveType)
4952 {
4953 case SVGA3D_PRIMITIVE_TRIANGLELIST:
4954 *pPrimitiveTypeD3D = D3DPT_TRIANGLELIST;
4955 break;
4956 case SVGA3D_PRIMITIVE_POINTLIST:
4957 *pPrimitiveTypeD3D = D3DPT_POINTLIST;
4958 break;
4959 case SVGA3D_PRIMITIVE_LINELIST:
4960 *pPrimitiveTypeD3D = D3DPT_LINELIST;
4961 break;
4962 case SVGA3D_PRIMITIVE_LINESTRIP:
4963 *pPrimitiveTypeD3D = D3DPT_LINESTRIP;
4964 break;
4965 case SVGA3D_PRIMITIVE_TRIANGLESTRIP:
4966 *pPrimitiveTypeD3D = D3DPT_TRIANGLESTRIP;
4967 break;
4968 case SVGA3D_PRIMITIVE_TRIANGLEFAN:
4969 *pPrimitiveTypeD3D = D3DPT_TRIANGLEFAN;
4970 break;
4971 default:
4972 return VERR_INVALID_PARAMETER;
4973 }
4974 return VINF_SUCCESS;
4975}
4976
4977
4978static int vmsvga3dDrawPrimitivesSyncVertexBuffer(PVMSVGA3DCONTEXT pContext,
4979 PVMSVGA3DSURFACE pVertexSurface)
4980{
4981 HRESULT hr;
4982 if ( pVertexSurface->u.pSurface
4983 && pVertexSurface->enmD3DResType != VMSVGA3D_D3DRESTYPE_VERTEX_BUFFER)
4984 {
4985 /* The buffer object is not an vertex one. Recreate the D3D resource. */
4986 AssertReturn(pVertexSurface->enmD3DResType == VMSVGA3D_D3DRESTYPE_INDEX_BUFFER, VERR_INVALID_PARAMETER);
4987 D3D_RELEASE(pVertexSurface->u.pIndexBuffer);
4988 pVertexSurface->enmD3DResType = VMSVGA3D_D3DRESTYPE_NONE;
4989
4990 LogFunc(("index -> vertex buffer sid=%u\n", pVertexSurface->id));
4991 }
4992
4993 bool fSync = pVertexSurface->fDirty;
4994 if (!pVertexSurface->u.pVertexBuffer)
4995 {
4996 LogFunc(("Create vertex buffer sid=%u fDirty=%d\n", pVertexSurface->id, pVertexSurface->fDirty));
4997
4998 const DWORD Usage = D3DUSAGE_DYNAMIC | D3DUSAGE_WRITEONLY; /* possible severe performance penalty otherwise (according to d3d debug output */
4999 hr = pContext->pDevice->CreateVertexBuffer(pVertexSurface->paMipmapLevels[0].cbSurface,
5000 Usage,
5001 0, /* non-FVF */
5002 D3DPOOL_DEFAULT,
5003 &pVertexSurface->u.pVertexBuffer,
5004 NULL);
5005 AssertMsgReturn(hr == D3D_OK, ("CreateVertexBuffer failed with %x\n", hr), VERR_INTERNAL_ERROR);
5006
5007 pVertexSurface->enmD3DResType = VMSVGA3D_D3DRESTYPE_VERTEX_BUFFER;
5008 pVertexSurface->idAssociatedContext = pContext->id;
5009 pVertexSurface->f.surfaceFlags |= SVGA3D_SURFACE_HINT_VERTEXBUFFER;
5010 fSync = true;
5011 }
5012
5013 if (fSync)
5014 {
5015 LogFunc(("sync vertex buffer\n"));
5016 Assert(pVertexSurface->u.pVertexBuffer);
5017
5018 void *pvData;
5019 hr = pVertexSurface->u.pVertexBuffer->Lock(0, 0, &pvData, D3DLOCK_DISCARD);
5020 AssertMsgReturn(hr == D3D_OK, ("Lock vertex failed with %x\n", hr), VERR_INTERNAL_ERROR);
5021
5022 memcpy(pvData, pVertexSurface->paMipmapLevels[0].pSurfaceData, pVertexSurface->paMipmapLevels[0].cbSurface);
5023
5024 hr = pVertexSurface->u.pVertexBuffer->Unlock();
5025 AssertMsgReturn(hr == D3D_OK, ("Unlock vertex failed with %x\n", hr), VERR_INTERNAL_ERROR);
5026 }
5027
5028 return VINF_SUCCESS;
5029}
5030
5031
5032static int vmsvga3dDrawPrimitivesSyncIndexBuffer(PVMSVGA3DCONTEXT pContext,
5033 PVMSVGA3DSURFACE pIndexSurface,
5034 uint32_t indexWidth)
5035{
5036 HRESULT hr;
5037 if ( pIndexSurface->u.pSurface
5038 && pIndexSurface->enmD3DResType != VMSVGA3D_D3DRESTYPE_INDEX_BUFFER)
5039 {
5040 /* The buffer object is not an index one. Must recreate the D3D resource. */
5041 AssertReturn(pIndexSurface->enmD3DResType == VMSVGA3D_D3DRESTYPE_VERTEX_BUFFER, VERR_INVALID_PARAMETER);
5042 D3D_RELEASE(pIndexSurface->u.pVertexBuffer);
5043 pIndexSurface->enmD3DResType = VMSVGA3D_D3DRESTYPE_NONE;
5044
5045 LogFunc(("vertex -> index buffer sid=%u\n", pIndexSurface->id));
5046 }
5047
5048 bool fSync = pIndexSurface->fDirty;
5049 if (!pIndexSurface->u.pIndexBuffer)
5050 {
5051 LogFunc(("Create index buffer fDirty=%d\n", pIndexSurface->fDirty));
5052
5053 const DWORD Usage = D3DUSAGE_DYNAMIC | D3DUSAGE_WRITEONLY; /* possible severe performance penalty otherwise (according to d3d debug output */
5054 const D3DFORMAT Format = (indexWidth == sizeof(uint16_t)) ? D3DFMT_INDEX16 : D3DFMT_INDEX32;
5055 hr = pContext->pDevice->CreateIndexBuffer(pIndexSurface->paMipmapLevels[0].cbSurface,
5056 Usage,
5057 Format,
5058 D3DPOOL_DEFAULT,
5059 &pIndexSurface->u.pIndexBuffer,
5060 NULL);
5061 AssertMsgReturn(hr == D3D_OK, ("CreateIndexBuffer failed with %x\n", hr), VERR_INTERNAL_ERROR);
5062
5063 pIndexSurface->enmD3DResType = VMSVGA3D_D3DRESTYPE_INDEX_BUFFER;
5064 pIndexSurface->idAssociatedContext = pContext->id;
5065 pIndexSurface->f.surfaceFlags |= SVGA3D_SURFACE_HINT_INDEXBUFFER;
5066 fSync = true;
5067 }
5068
5069 if (fSync)
5070 {
5071 LogFunc(("sync index buffer\n"));
5072 Assert(pIndexSurface->u.pIndexBuffer);
5073
5074 void *pvData;
5075 hr = pIndexSurface->u.pIndexBuffer->Lock(0, 0, &pvData, D3DLOCK_DISCARD);
5076 AssertMsgReturn(hr == D3D_OK, ("Lock index failed with %x\n", hr), VERR_INTERNAL_ERROR);
5077
5078 memcpy(pvData, pIndexSurface->paMipmapLevels[0].pSurfaceData, pIndexSurface->paMipmapLevels[0].cbSurface);
5079
5080 hr = pIndexSurface->u.pIndexBuffer->Unlock();
5081 AssertMsgReturn(hr == D3D_OK, ("Unlock index failed with %x\n", hr), VERR_INTERNAL_ERROR);
5082 }
5083
5084 return VINF_SUCCESS;
5085}
5086
5087static int vmsvga3dDrawPrimitivesProcessVertexDecls(const uint32_t numVertexDecls,
5088 const SVGA3dVertexDecl *pVertexDecl,
5089 const uint32_t idStream,
5090 const uint32_t uVertexMinOffset,
5091 const uint32_t uVertexMaxOffset,
5092 D3DVERTEXELEMENT9 *pVertexElement)
5093{
5094 RT_NOREF(uVertexMaxOffset); /* Logging only. */
5095 Assert(numVertexDecls);
5096
5097 /* Create a vertex declaration array */
5098 for (uint32_t iVertex = 0; iVertex < numVertexDecls; ++iVertex)
5099 {
5100 LogFunc(("vertex %d type=%s (%d) method=%s (%d) usage=%s (%d) usageIndex=%d stride=%d offset=%d (%d min=%d max=%d)\n",
5101 iVertex,
5102 vmsvgaDeclType2String(pVertexDecl[iVertex].identity.type), pVertexDecl[iVertex].identity.type,
5103 vmsvgaDeclMethod2String(pVertexDecl[iVertex].identity.method), pVertexDecl[iVertex].identity.method,
5104 vmsvgaDeclUsage2String(pVertexDecl[iVertex].identity.usage), pVertexDecl[iVertex].identity.usage,
5105 pVertexDecl[iVertex].identity.usageIndex,
5106 pVertexDecl[iVertex].array.stride,
5107 pVertexDecl[iVertex].array.offset - uVertexMinOffset,
5108 pVertexDecl[iVertex].array.offset,
5109 uVertexMinOffset, uVertexMaxOffset));
5110
5111 int rc = vmsvga3dVertexDecl2D3D(pVertexDecl[iVertex].identity, &pVertexElement[iVertex]);
5112 AssertRCReturn(rc, rc);
5113
5114 pVertexElement[iVertex].Stream = idStream;
5115 pVertexElement[iVertex].Offset = pVertexDecl[iVertex].array.offset - uVertexMinOffset;
5116
5117#ifdef LOG_ENABLED
5118 if (pVertexDecl[iVertex].array.stride == 0)
5119 LogFunc(("stride == 0! Can be valid\n"));
5120
5121 if (pVertexElement[iVertex].Offset >= pVertexDecl[0].array.stride)
5122 LogFunc(("WARNING: offset > stride!!\n"));
5123#endif
5124 }
5125
5126 return VINF_SUCCESS;
5127}
5128
5129static DECLCALLBACK(int) vmsvga3dBackDrawPrimitives(PVGASTATECC pThisCC, uint32_t cid, uint32_t numVertexDecls, SVGA3dVertexDecl *pVertexDecl,
5130 uint32_t numRanges, SVGA3dPrimitiveRange *pRange,
5131 uint32_t cVertexDivisor, SVGA3dVertexDivisor *pVertexDivisor)
5132{
5133 static const D3DVERTEXELEMENT9 sVertexEnd = D3DDECL_END();
5134
5135 PVMSVGA3DSTATE pState = pThisCC->svga.p3dState;
5136 AssertReturn(pState, VERR_INTERNAL_ERROR);
5137
5138 PVMSVGA3DCONTEXT pContext;
5139 int rc = vmsvga3dContextFromCid(pState, cid, &pContext);
5140 AssertRCReturn(rc, rc);
5141 AssertReturn(pContext->pDevice, VERR_INTERNAL_ERROR);
5142
5143 HRESULT hr;
5144
5145 /* SVGA driver may use the same surface for both index and vertex data. So we can not clear fDirty flag,
5146 * after updating a vertex buffer for example, because the same surface might be used for index buffer later.
5147 * So keep pointers to all used surfaces in the following two arrays and clear fDirty flag at the end.
5148 */
5149 PVMSVGA3DSURFACE aVertexSurfaces[SVGA3D_MAX_VERTEX_ARRAYS];
5150 PVMSVGA3DSURFACE aIndexSurfaces[SVGA3D_MAX_DRAW_PRIMITIVE_RANGES];
5151 RT_ZERO(aVertexSurfaces);
5152 RT_ZERO(aIndexSurfaces);
5153
5154 LogFunc(("cid=%u numVertexDecls=%d numRanges=%d, cVertexDivisor=%d\n", cid, numVertexDecls, numRanges, cVertexDivisor));
5155
5156 AssertReturn(numVertexDecls && numVertexDecls <= SVGA3D_MAX_VERTEX_ARRAYS, VERR_INVALID_PARAMETER);
5157 AssertReturn(numRanges && numRanges <= SVGA3D_MAX_DRAW_PRIMITIVE_RANGES, VERR_INVALID_PARAMETER);
5158 AssertReturn(!cVertexDivisor || cVertexDivisor == numVertexDecls, VERR_INVALID_PARAMETER);
5159
5160#if 0
5161 /* Dump render target to a bitmap. */
5162 if (pContext->state.aRenderTargets[SVGA3D_RT_COLOR0] != SVGA3D_INVALID_ID)
5163 {
5164 vmsvga3dUpdateHeapBuffersForSurfaces(pThisCC, pContext->state.aRenderTargets[SVGA3D_RT_COLOR0]);
5165 PVMSVGA3DSURFACE pSurface;
5166 int rc2 = vmsvga3dSurfaceFromSid(pState, pContext->state.aRenderTargets[SVGA3D_RT_COLOR0], &pSurface);
5167 if (RT_SUCCESS(rc2))
5168 vmsvga3dInfoSurfaceToBitmap(NULL, pSurface, "bmpd3d", "rt", "-pre");
5169 }
5170 for (int iTex = 0; iTex < SVGA3D_MAX_SAMPLERS_PS; ++iTex)
5171 {
5172 IDirect3DBaseTexture9 *pBaseTexture = 0;
5173 hr = pContext->pDevice->GetTexture(iTex, &pBaseTexture);
5174 if (hr == S_OK && pBaseTexture)
5175 {
5176 D3DSURFACE_DESC surfDesc;
5177 RT_ZERO(surfDesc);
5178
5179 IDirect3DTexture9 *pTexture = (IDirect3DTexture9 *)pBaseTexture;
5180 pTexture->GetLevelDesc(0, &surfDesc);
5181
5182 Log(("Texture[%d]: %dx%d\n",
5183 iTex, surfDesc.Width, surfDesc.Height));
5184 D3D_RELEASE(pBaseTexture);
5185 }
5186 }
5187#endif
5188
5189 /*
5190 * Process all vertex declarations. Each vertex buffer surface is represented by one stream source id.
5191 */
5192 D3DVERTEXELEMENT9 aVertexElements[SVGA3D_MAX_VERTEX_ARRAYS + 1];
5193
5194 uint32_t iCurrentVertex = 0;
5195 uint32_t iCurrentStreamId = 0;
5196 while (iCurrentVertex < numVertexDecls)
5197 {
5198 const uint32_t sidVertex = pVertexDecl[iCurrentVertex].array.surfaceId;
5199 const uint32_t strideVertex = pVertexDecl[iCurrentVertex].array.stride;
5200
5201 PVMSVGA3DSURFACE pVertexSurface;
5202 rc = vmsvga3dSurfaceFromSid(pState, sidVertex, &pVertexSurface);
5203 AssertRCBreak(rc);
5204
5205 rc = vmsvga3dDrawPrimitivesSyncVertexBuffer(pContext, pVertexSurface);
5206 AssertRCBreak(rc);
5207
5208 uint32_t uVertexMinOffset = UINT32_MAX;
5209 uint32_t uVertexMaxOffset = 0;
5210
5211 uint32_t iVertex;
5212 for (iVertex = iCurrentVertex; iVertex < numVertexDecls; ++iVertex)
5213 {
5214 /* Remember, so we can mark it as not dirty later. */
5215 aVertexSurfaces[iVertex] = pVertexSurface;
5216
5217 /* New surface id -> new stream id. */
5218 if (pVertexDecl[iVertex].array.surfaceId != sidVertex)
5219 break;
5220
5221 const uint32_t uVertexOffset = pVertexDecl[iVertex].array.offset;
5222 const uint32_t uNewVertexMinOffset = RT_MIN(uVertexMinOffset, uVertexOffset);
5223 const uint32_t uNewVertexMaxOffset = RT_MAX(uVertexMaxOffset, uVertexOffset);
5224
5225 /* We must put vertex declarations that start at a different element in another stream as d3d only handles offsets < stride. */
5226 if ( uNewVertexMaxOffset - uNewVertexMinOffset >= strideVertex
5227 && strideVertex != 0)
5228 break;
5229
5230 uVertexMinOffset = uNewVertexMinOffset;
5231 uVertexMaxOffset = uNewVertexMaxOffset;
5232 }
5233
5234 rc = vmsvga3dDrawPrimitivesProcessVertexDecls(iVertex - iCurrentVertex,
5235 &pVertexDecl[iCurrentVertex],
5236 iCurrentStreamId,
5237 uVertexMinOffset,
5238 uVertexMaxOffset,
5239 &aVertexElements[iCurrentVertex]);
5240 AssertRCBreak(rc);
5241
5242 LogFunc(("SetStreamSource vertex sid=%u stream %d min offset=%d stride=%d\n",
5243 pVertexSurface->id, iCurrentStreamId, uVertexMinOffset, strideVertex));
5244
5245 hr = pContext->pDevice->SetStreamSource(iCurrentStreamId,
5246 pVertexSurface->u.pVertexBuffer,
5247 uVertexMinOffset,
5248 strideVertex);
5249 AssertMsgBreakStmt(hr == D3D_OK, ("SetStreamSource failed with %x\n", hr), rc = VERR_INTERNAL_ERROR);
5250
5251 if (cVertexDivisor)
5252 {
5253 LogFunc(("SetStreamSourceFreq[%d]=%x\n", iCurrentStreamId, pVertexDivisor[iCurrentVertex].value));
5254 HRESULT hr2 = pContext->pDevice->SetStreamSourceFreq(iCurrentStreamId,
5255 pVertexDivisor[iCurrentVertex].value);
5256 Assert(SUCCEEDED(hr2)); RT_NOREF(hr2);
5257 }
5258
5259 iCurrentVertex = iVertex;
5260 ++iCurrentStreamId;
5261 }
5262
5263 /* iCurrentStreamId is equal to the total number of streams and the value is used for cleanup at the function end. */
5264
5265 AssertRCReturn(rc, rc);
5266
5267 /* Mark the end. */
5268 memcpy(&aVertexElements[numVertexDecls], &sVertexEnd, sizeof(sVertexEnd));
5269
5270 /* Check if this context already has the same vertex declaration. */
5271 if ( pContext->d3dState.pVertexDecl
5272 && pContext->d3dState.cVertexElements == numVertexDecls + 1
5273 && memcmp(pContext->d3dState.aVertexElements,
5274 aVertexElements,
5275 pContext->d3dState.cVertexElements * sizeof(aVertexElements[0])) == 0)
5276 {
5277 /* Same. */
5278 }
5279 else
5280 {
5281 /* Create and set the vertex declaration. */
5282 IDirect3DVertexDeclaration9 *pVertexDecl0;
5283 hr = pContext->pDevice->CreateVertexDeclaration(&aVertexElements[0], &pVertexDecl0);
5284 AssertMsgReturn(hr == D3D_OK, ("CreateVertexDeclaration failed with %x\n", hr), VERR_INTERNAL_ERROR);
5285
5286 hr = pContext->pDevice->SetVertexDeclaration(pVertexDecl0);
5287 AssertMsgReturnStmt(hr == D3D_OK, ("SetVertexDeclaration failed with %x\n", hr),
5288 D3D_RELEASE(pVertexDecl0),
5289 VERR_INTERNAL_ERROR);
5290
5291 /* The new vertex declaration has been successfully set. Delete the old one. */
5292 D3D_RELEASE(pContext->d3dState.pVertexDecl);
5293
5294 /* Remember the new vertext declaration. */
5295 pContext->d3dState.pVertexDecl = pVertexDecl0;
5296 pContext->d3dState.cVertexElements = numVertexDecls + 1;
5297 memcpy(pContext->d3dState.aVertexElements,
5298 aVertexElements,
5299 pContext->d3dState.cVertexElements * sizeof(aVertexElements[0]));
5300 }
5301
5302 /* Begin a scene before rendering anything. */
5303 hr = pContext->pDevice->BeginScene();
5304 AssertMsgReturn(hr == D3D_OK, ("BeginScene failed with %x\n", hr), VERR_INTERNAL_ERROR);
5305
5306 /* Now draw the primitives. */
5307 for (uint32_t iPrimitive = 0; iPrimitive < numRanges; ++iPrimitive)
5308 {
5309 Log(("Primitive %d: type %s\n", iPrimitive, vmsvga3dPrimitiveType2String(pRange[iPrimitive].primType)));
5310
5311 const uint32_t sidIndex = pRange[iPrimitive].indexArray.surfaceId;
5312 PVMSVGA3DSURFACE pIndexSurface = NULL;
5313
5314 D3DPRIMITIVETYPE PrimitiveTypeD3D;
5315 rc = vmsvga3dPrimitiveType2D3D(pRange[iPrimitive].primType, &PrimitiveTypeD3D);
5316 AssertRCBreak(rc);
5317
5318 /* Triangle strips or fans with just one primitive don't make much sense and are identical to triangle lists.
5319 * Workaround for NVidia driver crash when encountering some of these.
5320 */
5321 if ( pRange[iPrimitive].primitiveCount == 1
5322 && ( PrimitiveTypeD3D == D3DPT_TRIANGLESTRIP
5323 || PrimitiveTypeD3D == D3DPT_TRIANGLEFAN))
5324 PrimitiveTypeD3D = D3DPT_TRIANGLELIST;
5325
5326 if (sidIndex != SVGA3D_INVALID_ID)
5327 {
5328 AssertMsg(pRange[iPrimitive].indexWidth == sizeof(uint32_t) || pRange[iPrimitive].indexWidth == sizeof(uint16_t),
5329 ("Unsupported primitive width %d\n", pRange[iPrimitive].indexWidth));
5330
5331 rc = vmsvga3dSurfaceFromSid(pState, sidIndex, &pIndexSurface);
5332 AssertRCBreak(rc);
5333
5334 aIndexSurfaces[iPrimitive] = pIndexSurface;
5335
5336 Log(("vmsvga3dDrawPrimitives: index sid=%u\n", sidIndex));
5337
5338 rc = vmsvga3dDrawPrimitivesSyncIndexBuffer(pContext, pIndexSurface, pRange[iPrimitive].indexWidth);
5339 AssertRCBreak(rc);
5340
5341 hr = pContext->pDevice->SetIndices(pIndexSurface->u.pIndexBuffer);
5342 AssertMsg(hr == D3D_OK, ("SetIndices vertex failed with %x\n", hr));
5343 }
5344 else
5345 {
5346 hr = pContext->pDevice->SetIndices(NULL);
5347 AssertMsg(hr == D3D_OK, ("SetIndices vertex (NULL) failed with %x\n", hr));
5348 }
5349
5350 const uint32_t strideVertex = pVertexDecl[0].array.stride;
5351
5352 if (!pIndexSurface)
5353 {
5354 /* Render without an index buffer */
5355 Log(("DrawPrimitive %x primitivecount=%d index index bias=%d stride=%d\n",
5356 PrimitiveTypeD3D, pRange[iPrimitive].primitiveCount, pRange[iPrimitive].indexBias, strideVertex));
5357
5358 hr = pContext->pDevice->DrawPrimitive(PrimitiveTypeD3D,
5359 pRange[iPrimitive].indexBias,
5360 pRange[iPrimitive].primitiveCount);
5361 AssertMsgBreakStmt(hr == D3D_OK, ("DrawPrimitive failed with %x\n", hr), rc = VERR_INTERNAL_ERROR);
5362 }
5363 else
5364 {
5365 UINT numVertices;
5366 if (pVertexDecl[0].rangeHint.last)
5367 {
5368 /* Both SVGA3dArrayRangeHint definition and the SVGA driver code imply that 'last' is exclusive,
5369 * hence compute the difference.
5370 */
5371 numVertices = pVertexDecl[0].rangeHint.last - pVertexDecl[0].rangeHint.first;
5372 }
5373 else
5374 {
5375 /* Range hint is not provided. */
5376 PVMSVGA3DSURFACE pVertexSurface = aVertexSurfaces[0];
5377 numVertices = pVertexSurface->paMipmapLevels[0].cbSurface / strideVertex
5378 - pVertexDecl[0].array.offset / strideVertex
5379 - pVertexDecl[0].rangeHint.first
5380 - pRange[iPrimitive].indexBias;
5381 }
5382
5383 /* Render with an index buffer */
5384 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",
5385 PrimitiveTypeD3D, pRange[iPrimitive].indexArray.offset / pRange[iPrimitive].indexWidth, pRange[iPrimitive].indexArray.offset,
5386 pVertexDecl[0].rangeHint.first, pVertexDecl[0].rangeHint.last, numVertices, pRange[iPrimitive].primitiveCount,
5387 (pRange[iPrimitive].indexWidth == sizeof(uint16_t)) ? "D3DFMT_INDEX16" : "D3DFMT_INDEX32",
5388 pRange[iPrimitive].indexBias, strideVertex));
5389
5390 hr = pContext->pDevice->DrawIndexedPrimitive(PrimitiveTypeD3D,
5391 pRange[iPrimitive].indexBias, /* BaseVertexIndex */
5392 0, /* MinVertexIndex */
5393 numVertices,
5394 pRange[iPrimitive].indexArray.offset / pRange[iPrimitive].indexWidth, /* StartIndex */
5395 pRange[iPrimitive].primitiveCount);
5396 AssertMsgBreakStmt(hr == D3D_OK, ("DrawIndexedPrimitive failed with %x\n", hr), rc = VERR_INTERNAL_ERROR);
5397 }
5398 }
5399
5400 /* End the scene and do some cleanup regardless of the rc. */
5401 hr = pContext->pDevice->EndScene();
5402 AssertMsgReturn(hr == D3D_OK, ("EndScene failed with %x\n", hr), VERR_INTERNAL_ERROR);
5403
5404 /* Cleanup. */
5405 uint32_t i;
5406 /* Clear all streams, because they are set at the beginning of this function anyway.
5407 * Now the vertex buffers can be safely deleted/recreated if necessary.
5408 */
5409 for (i = 0; i < iCurrentStreamId; ++i)
5410 {
5411 LogFunc(("clear stream %d\n", i));
5412 HRESULT hr2 = pContext->pDevice->SetStreamSource(i, NULL, 0, 0);
5413 AssertMsg(hr2 == D3D_OK, ("SetStreamSource(%d, NULL) failed with %x\n", i, hr2)); RT_NOREF(hr2);
5414 }
5415
5416 if (cVertexDivisor)
5417 {
5418 /* "When you are finished rendering the instance data, be sure to reset the vertex stream frequency back..." */
5419 for (i = 0; i < iCurrentStreamId; ++i)
5420 {
5421 LogFunc(("reset stream freq %d\n", i));
5422 HRESULT hr2 = pContext->pDevice->SetStreamSourceFreq(i, 1);
5423 AssertMsg(hr2 == D3D_OK, ("SetStreamSourceFreq(%d, 1) failed with %x\n", i, hr2)); RT_NOREF(hr2);
5424 }
5425 }
5426
5427 if (RT_SUCCESS(rc))
5428 {
5429 for (i = 0; i < numVertexDecls; ++i)
5430 {
5431 if (aVertexSurfaces[i])
5432 {
5433 aVertexSurfaces[i]->paMipmapLevels[0].fDirty = false;
5434 aVertexSurfaces[i]->fDirty = false;
5435 }
5436 }
5437
5438 for (i = 0; i < numRanges; ++i)
5439 {
5440 if (aIndexSurfaces[i])
5441 {
5442 aIndexSurfaces[i]->paMipmapLevels[0].fDirty = false;
5443 aIndexSurfaces[i]->fDirty = false;
5444 }
5445 }
5446
5447 /* Make sure we can track drawing usage of active render targets and textures. */
5448 vmsvga3dContextTrackUsage(pThisCC, pContext);
5449 }
5450
5451#if 0
5452 /* Dump render target to a bitmap. */
5453 if (pContext->state.aRenderTargets[SVGA3D_RT_COLOR0] != SVGA3D_INVALID_ID)
5454 {
5455 vmsvga3dUpdateHeapBuffersForSurfaces(pThisCC, pContext->state.aRenderTargets[SVGA3D_RT_COLOR0]);
5456 PVMSVGA3DSURFACE pSurface;
5457 int rc2 = vmsvga3dSurfaceFromSid(pState, pContext->state.aRenderTargets[SVGA3D_RT_COLOR0], &pSurface);
5458 if (RT_SUCCESS(rc2))
5459 vmsvga3dInfoSurfaceToBitmap(NULL, pSurface, "bmpd3d", "rt", "-post");
5460# if 0
5461 for (i = 0; i < SVGA3D_MAX_SAMPLERS_PS; ++i)
5462 {
5463 if (pContext->aSidActiveTextures[i] != SVGA3D_INVALID_ID)
5464 {
5465 char szSuffix[32];
5466 RTStrPrintf2(szSuffix, sizeof(szSuffix), "-tx%d", i);
5467 vmsvga3dUpdateHeapBuffersForSurfaces(pThisCC, pContext->aSidActiveTextures[i]);
5468 rc2 = vmsvga3dSurfaceFromSid(pState, pContext->aSidActiveTextures[i], &pSurface);
5469 if (RT_SUCCESS(rc2))
5470 vmsvga3dInfoSurfaceToBitmap(NULL, pSurface, "bmpd3d", "rt", szSuffix);
5471 }
5472 }
5473# endif
5474 }
5475#endif
5476
5477 return rc;
5478}
5479
5480
5481static DECLCALLBACK(int) vmsvga3dBackSetScissorRect(PVGASTATECC pThisCC, uint32_t cid, SVGA3dRect *pRect)
5482{
5483 HRESULT hr;
5484 RECT rect;
5485 PVMSVGA3DSTATE pState = pThisCC->svga.p3dState;
5486 AssertReturn(pState, VERR_NO_MEMORY);
5487
5488 Log(("vmsvga3dSetScissorRect %x (%d,%d)(%d,%d)\n", cid, pRect->x, pRect->y, pRect->w, pRect->h));
5489
5490 PVMSVGA3DCONTEXT pContext;
5491 int rc = vmsvga3dContextFromCid(pState, cid, &pContext);
5492 AssertRCReturn(rc, rc);
5493 AssertReturn(pContext->pDevice, VERR_INTERNAL_ERROR);
5494
5495 /* Store for vm state save/restore. */
5496 pContext->state.u32UpdateFlags |= VMSVGA3D_UPDATE_SCISSORRECT;
5497 pContext->state.RectScissor = *pRect;
5498
5499 rect.left = pRect->x;
5500 rect.top = pRect->y;
5501 rect.right = rect.left + pRect->w; /* exclusive */
5502 rect.bottom = rect.top + pRect->h; /* exclusive */
5503
5504 hr = pContext->pDevice->SetScissorRect(&rect);
5505 AssertMsgReturn(hr == D3D_OK, ("vmsvga3dSetScissorRect: SetScissorRect failed with %x\n", hr), VERR_INTERNAL_ERROR);
5506
5507 return VINF_SUCCESS;
5508}
5509
5510
5511static DECLCALLBACK(int) vmsvga3dBackShaderDefine(PVGASTATECC pThisCC, uint32_t cid, uint32_t shid, SVGA3dShaderType type,
5512 uint32_t cbData, uint32_t *pShaderData)
5513{
5514 HRESULT hr;
5515 PVMSVGA3DSHADER pShader;
5516 PVMSVGA3DSTATE pState = pThisCC->svga.p3dState;
5517 AssertReturn(pState, VERR_NO_MEMORY);
5518
5519 Log(("vmsvga3dShaderDefine %x shid=%d type=%s cbData=%d\n", cid, shid, (type == SVGA3D_SHADERTYPE_VS) ? "VERTEX" : "PIXEL", cbData));
5520
5521 PVMSVGA3DCONTEXT pContext;
5522 int rc = vmsvga3dContextFromCid(pState, cid, &pContext);
5523 AssertRCReturn(rc, rc);
5524 AssertReturn(pContext->pDevice, VERR_INTERNAL_ERROR);
5525
5526 AssertReturn(shid < SVGA3D_MAX_SHADER_IDS, VERR_INVALID_PARAMETER);
5527
5528 rc = vmsvga3dShaderParse(type, cbData, pShaderData);
5529 if (RT_FAILURE(rc))
5530 {
5531 AssertRC(rc);
5532 vmsvga3dShaderLogRel("Failed to parse", type, cbData, pShaderData);
5533 return rc;
5534 }
5535
5536 if (type == SVGA3D_SHADERTYPE_VS)
5537 {
5538 if (shid >= pContext->cVertexShaders)
5539 {
5540 void *pvNew = RTMemRealloc(pContext->paVertexShader, sizeof(VMSVGA3DSHADER) * (shid + 1));
5541 AssertReturn(pvNew, VERR_NO_MEMORY);
5542 pContext->paVertexShader = (PVMSVGA3DSHADER)pvNew;
5543 memset(&pContext->paVertexShader[pContext->cVertexShaders], 0, sizeof(VMSVGA3DSHADER) * (shid + 1 - pContext->cVertexShaders));
5544 for (uint32_t i = pContext->cVertexShaders; i < shid + 1; i++)
5545 pContext->paVertexShader[i].id = SVGA3D_INVALID_ID;
5546 pContext->cVertexShaders = shid + 1;
5547 }
5548 /* If one already exists with this id, then destroy it now. */
5549 if (pContext->paVertexShader[shid].id != SVGA3D_INVALID_ID)
5550 vmsvga3dBackShaderDestroy(pThisCC, cid, shid, pContext->paVertexShader[shid].type);
5551
5552 pShader = &pContext->paVertexShader[shid];
5553 }
5554 else
5555 {
5556 Assert(type == SVGA3D_SHADERTYPE_PS);
5557 if (shid >= pContext->cPixelShaders)
5558 {
5559 void *pvNew = RTMemRealloc(pContext->paPixelShader, sizeof(VMSVGA3DSHADER) * (shid + 1));
5560 AssertReturn(pvNew, VERR_NO_MEMORY);
5561 pContext->paPixelShader = (PVMSVGA3DSHADER)pvNew;
5562 memset(&pContext->paPixelShader[pContext->cPixelShaders], 0, sizeof(VMSVGA3DSHADER) * (shid + 1 - pContext->cPixelShaders));
5563 for (uint32_t i = pContext->cPixelShaders; i < shid + 1; i++)
5564 pContext->paPixelShader[i].id = SVGA3D_INVALID_ID;
5565 pContext->cPixelShaders = shid + 1;
5566 }
5567 /* If one already exists with this id, then destroy it now. */
5568 if (pContext->paPixelShader[shid].id != SVGA3D_INVALID_ID)
5569 vmsvga3dBackShaderDestroy(pThisCC, cid, shid, pContext->paPixelShader[shid].type);
5570
5571 pShader = &pContext->paPixelShader[shid];
5572 }
5573
5574 memset(pShader, 0, sizeof(*pShader));
5575 pShader->id = shid;
5576 pShader->cid = cid;
5577 pShader->type = type;
5578 pShader->cbData = cbData;
5579 pShader->pShaderProgram = RTMemAllocZ(cbData);
5580 AssertReturn(pShader->pShaderProgram, VERR_NO_MEMORY);
5581 memcpy(pShader->pShaderProgram, pShaderData, cbData);
5582
5583#ifdef DUMP_SHADER_DISASSEMBLY
5584 LPD3DXBUFFER pDisassembly;
5585 hr = D3DXDisassembleShader((const DWORD *)pShaderData, FALSE, NULL, &pDisassembly);
5586 if (hr == D3D_OK)
5587 {
5588 Log(("Shader disassembly:\n%s\n", pDisassembly->GetBufferPointer()));
5589 D3D_RELEASE(pDisassembly);
5590 }
5591#endif
5592
5593 switch (type)
5594 {
5595 case SVGA3D_SHADERTYPE_VS:
5596 hr = pContext->pDevice->CreateVertexShader((const DWORD *)pShaderData, &pShader->u.pVertexShader);
5597 break;
5598
5599 case SVGA3D_SHADERTYPE_PS:
5600 hr = pContext->pDevice->CreatePixelShader((const DWORD *)pShaderData, &pShader->u.pPixelShader);
5601 break;
5602
5603 default:
5604 AssertFailedReturn(VERR_INVALID_PARAMETER);
5605 }
5606 if (hr != D3D_OK)
5607 {
5608 /* Dump the shader code. */
5609 vmsvga3dShaderLogRel("Failed to create", type, cbData, pShaderData);
5610
5611 RTMemFree(pShader->pShaderProgram);
5612 memset(pShader, 0, sizeof(*pShader));
5613 pShader->id = SVGA3D_INVALID_ID;
5614 }
5615
5616 AssertMsgReturn(hr == D3D_OK, ("vmsvga3dShaderDefine: CreateVertex/PixelShader failed with %x\n", hr), VERR_INTERNAL_ERROR);
5617 return VINF_SUCCESS;
5618}
5619
5620static DECLCALLBACK(int) vmsvga3dBackShaderDestroy(PVGASTATECC pThisCC, uint32_t cid, uint32_t shid, SVGA3dShaderType type)
5621{
5622 PVMSVGA3DSTATE pState = pThisCC->svga.p3dState;
5623 AssertReturn(pState, VERR_NO_MEMORY);
5624 PVMSVGA3DSHADER pShader = NULL;
5625
5626 Log(("vmsvga3dShaderDestroy %x shid=%d type=%s\n", cid, shid, (type == SVGA3D_SHADERTYPE_VS) ? "VERTEX" : "PIXEL"));
5627
5628 PVMSVGA3DCONTEXT pContext;
5629 int rc = vmsvga3dContextFromCid(pState, cid, &pContext);
5630 AssertRCReturn(rc, rc);
5631
5632 if (type == SVGA3D_SHADERTYPE_VS)
5633 {
5634 if ( shid < pContext->cVertexShaders
5635 && pContext->paVertexShader[shid].id == shid)
5636 {
5637 pShader = &pContext->paVertexShader[shid];
5638 D3D_RELEASE(pShader->u.pVertexShader);
5639 }
5640 }
5641 else
5642 {
5643 Assert(type == SVGA3D_SHADERTYPE_PS);
5644 if ( shid < pContext->cPixelShaders
5645 && pContext->paPixelShader[shid].id == shid)
5646 {
5647 pShader = &pContext->paPixelShader[shid];
5648 D3D_RELEASE(pShader->u.pPixelShader);
5649 }
5650 }
5651
5652 if (pShader)
5653 {
5654 if (pShader->pShaderProgram)
5655 RTMemFree(pShader->pShaderProgram);
5656
5657 memset(pShader, 0, sizeof(*pShader));
5658 pShader->id = SVGA3D_INVALID_ID;
5659 }
5660 else
5661 AssertFailedReturn(VERR_INVALID_PARAMETER);
5662
5663 return VINF_SUCCESS;
5664}
5665
5666static DECLCALLBACK(int) vmsvga3dBackShaderSet(PVGASTATECC pThisCC, PVMSVGA3DCONTEXT pContext, uint32_t cid, SVGA3dShaderType type, uint32_t shid)
5667{
5668 PVMSVGA3DSTATE pState = pThisCC->svga.p3dState;
5669 AssertReturn(pState, VERR_NO_MEMORY);
5670 HRESULT hr;
5671
5672 Log(("vmsvga3dShaderSet %x type=%s shid=%d\n", cid, (type == SVGA3D_SHADERTYPE_VS) ? "VERTEX" : "PIXEL", shid));
5673
5674 NOREF(pContext);
5675 int rc = vmsvga3dContextFromCid(pState, cid, &pContext);
5676 AssertRCReturn(rc, rc);
5677 AssertReturn(pContext->pDevice, VERR_INTERNAL_ERROR);
5678
5679 if (type == SVGA3D_SHADERTYPE_VS)
5680 {
5681 /* Save for vm state save/restore. */
5682 pContext->state.shidVertex = shid;
5683 pContext->state.u32UpdateFlags |= VMSVGA3D_UPDATE_VERTEXSHADER;
5684
5685 if ( shid < pContext->cVertexShaders
5686 && pContext->paVertexShader[shid].id == shid)
5687 {
5688 PVMSVGA3DSHADER pShader = &pContext->paVertexShader[shid];
5689 Assert(type == pShader->type);
5690
5691 hr = pContext->pDevice->SetVertexShader(pShader->u.pVertexShader);
5692 AssertMsgReturn(hr == D3D_OK, ("vmsvga3dShaderSet: SetVertex/PixelShader failed with %x\n", hr), VERR_INTERNAL_ERROR);
5693 }
5694 else
5695 if (shid == SVGA_ID_INVALID)
5696 {
5697 /* Unselect shader. */
5698 hr = pContext->pDevice->SetVertexShader(NULL);
5699 AssertMsgReturn(hr == D3D_OK, ("vmsvga3dShaderSet: SetVertex/PixelShader failed with %x\n", hr), VERR_INTERNAL_ERROR);
5700 }
5701 else
5702 AssertFailedReturn(VERR_INVALID_PARAMETER);
5703 }
5704 else
5705 {
5706 /* Save for vm state save/restore. */
5707 pContext->state.shidPixel = shid;
5708 pContext->state.u32UpdateFlags |= VMSVGA3D_UPDATE_PIXELSHADER;
5709
5710 Assert(type == SVGA3D_SHADERTYPE_PS);
5711 if ( shid < pContext->cPixelShaders
5712 && pContext->paPixelShader[shid].id == shid)
5713 {
5714 PVMSVGA3DSHADER pShader = &pContext->paPixelShader[shid];
5715 Assert(type == pShader->type);
5716
5717 hr = pContext->pDevice->SetPixelShader(pShader->u.pPixelShader);
5718 AssertMsgReturn(hr == D3D_OK, ("vmsvga3dShaderSet: SetVertex/PixelShader failed with %x\n", hr), VERR_INTERNAL_ERROR);
5719 }
5720 else
5721 if (shid == SVGA_ID_INVALID)
5722 {
5723 /* Unselect shader. */
5724 hr = pContext->pDevice->SetPixelShader(NULL);
5725 AssertMsgReturn(hr == D3D_OK, ("vmsvga3dShaderSet: SetVertex/PixelShader failed with %x\n", hr), VERR_INTERNAL_ERROR);
5726 }
5727 else
5728 AssertFailedReturn(VERR_INVALID_PARAMETER);
5729 }
5730
5731 return VINF_SUCCESS;
5732}
5733
5734static DECLCALLBACK(int) vmsvga3dBackShaderSetConst(PVGASTATECC pThisCC, uint32_t cid, uint32_t reg, SVGA3dShaderType type,
5735 SVGA3dShaderConstType ctype, uint32_t cRegisters, uint32_t *pValues)
5736{
5737 HRESULT hr;
5738 PVMSVGA3DSTATE pState = pThisCC->svga.p3dState;
5739 AssertReturn(pState, VERR_NO_MEMORY);
5740
5741 Log(("vmsvga3dShaderSetConst %x reg=%x type=%s ctype=%x\n", cid, reg, (type == SVGA3D_SHADERTYPE_VS) ? "VERTEX" : "PIXEL", ctype));
5742
5743 PVMSVGA3DCONTEXT pContext;
5744 int rc = vmsvga3dContextFromCid(pState, cid, &pContext);
5745 AssertRCReturn(rc, rc);
5746 AssertReturn(pContext->pDevice, VERR_INTERNAL_ERROR);
5747
5748 for (uint32_t i = 0; i < cRegisters; i++)
5749 {
5750#ifdef LOG_ENABLED
5751 switch (ctype)
5752 {
5753 case SVGA3D_CONST_TYPE_FLOAT:
5754 {
5755 float *pValuesF = (float *)pValues;
5756 Log(("ConstantF %d: value=" FLOAT_FMT_STR ", " FLOAT_FMT_STR ", " FLOAT_FMT_STR ", " FLOAT_FMT_STR "\n",
5757 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])));
5758 break;
5759 }
5760
5761 case SVGA3D_CONST_TYPE_INT:
5762 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]));
5763 break;
5764
5765 case SVGA3D_CONST_TYPE_BOOL:
5766 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]));
5767 break;
5768
5769 default:
5770 AssertFailedReturn(VERR_INVALID_PARAMETER);
5771 }
5772#endif
5773 vmsvga3dSaveShaderConst(pContext, reg + i, type, ctype, pValues[i*4 + 0], pValues[i*4 + 1], pValues[i*4 + 2], pValues[i*4 + 3]);
5774 }
5775
5776 switch (type)
5777 {
5778 case SVGA3D_SHADERTYPE_VS:
5779 switch (ctype)
5780 {
5781 case SVGA3D_CONST_TYPE_FLOAT:
5782 hr = pContext->pDevice->SetVertexShaderConstantF(reg, (const float *)pValues, cRegisters);
5783 break;
5784
5785 case SVGA3D_CONST_TYPE_INT:
5786 hr = pContext->pDevice->SetVertexShaderConstantI(reg, (const int *)pValues, cRegisters);
5787 break;
5788
5789 case SVGA3D_CONST_TYPE_BOOL:
5790 hr = pContext->pDevice->SetVertexShaderConstantB(reg, (const BOOL *)pValues, cRegisters);
5791 break;
5792
5793 default:
5794 AssertFailedReturn(VERR_INVALID_PARAMETER);
5795 }
5796 AssertMsgReturn(hr == D3D_OK, ("vmsvga3dShaderSetConst: SetVertexShader failed with %x\n", hr), VERR_INTERNAL_ERROR);
5797 break;
5798
5799 case SVGA3D_SHADERTYPE_PS:
5800 switch (ctype)
5801 {
5802 case SVGA3D_CONST_TYPE_FLOAT:
5803 {
5804 hr = pContext->pDevice->SetPixelShaderConstantF(reg, (const float *)pValues, cRegisters);
5805 break;
5806 }
5807
5808 case SVGA3D_CONST_TYPE_INT:
5809 hr = pContext->pDevice->SetPixelShaderConstantI(reg, (const int *)pValues, cRegisters);
5810 break;
5811
5812 case SVGA3D_CONST_TYPE_BOOL:
5813 hr = pContext->pDevice->SetPixelShaderConstantB(reg, (const BOOL *)pValues, cRegisters);
5814 break;
5815
5816 default:
5817 AssertFailedReturn(VERR_INVALID_PARAMETER);
5818 }
5819 AssertMsgReturn(hr == D3D_OK, ("vmsvga3dShaderSetConst: SetPixelShader failed with %x\n", hr), VERR_INTERNAL_ERROR);
5820 break;
5821
5822 default:
5823 AssertFailedReturn(VERR_INVALID_PARAMETER);
5824 }
5825 return VINF_SUCCESS;
5826}
5827
5828static DECLCALLBACK(int) vmsvga3dBackOcclusionQueryCreate(PVGASTATECC pThisCC, PVMSVGA3DCONTEXT pContext)
5829{
5830 RT_NOREF(pThisCC);
5831 HRESULT hr = pContext->pDevice->CreateQuery(D3DQUERYTYPE_OCCLUSION, &pContext->occlusion.pQuery);
5832 AssertMsgReturn(hr == D3D_OK, ("CreateQuery(D3DQUERYTYPE_OCCLUSION) failed with %x\n", hr), VERR_INTERNAL_ERROR);
5833 return VINF_SUCCESS;
5834}
5835
5836static DECLCALLBACK(int) vmsvga3dBackOcclusionQueryDelete(PVGASTATECC pThisCC, PVMSVGA3DCONTEXT pContext)
5837{
5838 RT_NOREF(pThisCC);
5839 D3D_RELEASE(pContext->occlusion.pQuery);
5840 return VINF_SUCCESS;
5841}
5842
5843static DECLCALLBACK(int) vmsvga3dBackOcclusionQueryBegin(PVGASTATECC pThisCC, PVMSVGA3DCONTEXT pContext)
5844{
5845 RT_NOREF(pThisCC);
5846 HRESULT hr = pContext->occlusion.pQuery->Issue(D3DISSUE_BEGIN);
5847 AssertMsgReturnStmt(hr == D3D_OK, ("D3DISSUE_BEGIN(D3DQUERYTYPE_OCCLUSION) failed with %x\n", hr),
5848 D3D_RELEASE(pContext->occlusion.pQuery), VERR_INTERNAL_ERROR);
5849 return VINF_SUCCESS;
5850}
5851
5852static DECLCALLBACK(int) vmsvga3dBackOcclusionQueryEnd(PVGASTATECC pThisCC, PVMSVGA3DCONTEXT pContext)
5853{
5854 RT_NOREF(pThisCC);
5855 HRESULT hr = pContext->occlusion.pQuery->Issue(D3DISSUE_END);
5856 AssertMsgReturnStmt(hr == D3D_OK, ("D3DISSUE_END(D3DQUERYTYPE_OCCLUSION) failed with %x\n", hr),
5857 D3D_RELEASE(pContext->occlusion.pQuery), VERR_INTERNAL_ERROR);
5858 return VINF_SUCCESS;
5859}
5860
5861static DECLCALLBACK(int) vmsvga3dBackOcclusionQueryGetData(PVGASTATECC pThisCC, PVMSVGA3DCONTEXT pContext, uint32_t *pu32Pixels)
5862{
5863 RT_NOREF(pThisCC);
5864 HRESULT hr = D3D_OK;
5865 /* Wait until the data becomes available. */
5866 DWORD dwPixels = 0;
5867 do
5868 {
5869 hr = pContext->occlusion.pQuery->GetData((void *)&dwPixels, sizeof(DWORD), D3DGETDATA_FLUSH);
5870 } while (hr == S_FALSE);
5871
5872 AssertMsgReturnStmt(hr == D3D_OK, ("GetData(D3DQUERYTYPE_OCCLUSION) failed with %x\n", hr),
5873 D3D_RELEASE(pContext->occlusion.pQuery), VERR_INTERNAL_ERROR);
5874
5875 LogFunc(("Query result: dwPixels %d\n", dwPixels));
5876 *pu32Pixels = dwPixels;
5877 return VINF_SUCCESS;
5878}
5879
5880static void vmsvgaDumpD3DCaps(D3DCAPS9 *pCaps, D3DADAPTER_IDENTIFIER9 const *pai9)
5881{
5882 bool const fBufferingSaved = RTLogRelSetBuffering(true /*fBuffered*/);
5883
5884 LogRel(("\nD3D9 adapter: %s %RX16:%RX16 [%s, version %d.%d.%d.%d]\n",
5885 pai9->Description, pai9->VendorId, pai9->DeviceId, pai9->Driver,
5886 RT_HI_U16(pai9->DriverVersion.HighPart), RT_LO_U16(pai9->DriverVersion.HighPart),
5887 RT_HI_U16(pai9->DriverVersion.LowPart), RT_LO_U16(pai9->DriverVersion.LowPart)));
5888
5889 LogRel(("\nD3D device caps: DevCaps2:\n"));
5890 if (pCaps->DevCaps2 & D3DDEVCAPS2_ADAPTIVETESSRTPATCH)
5891 LogRel((" - D3DDEVCAPS2_ADAPTIVETESSRTPATCH\n"));
5892 if (pCaps->DevCaps2 & D3DDEVCAPS2_ADAPTIVETESSNPATCH)
5893 LogRel((" - D3DDEVCAPS2_ADAPTIVETESSNPATCH\n"));
5894 if (pCaps->DevCaps2 & D3DDEVCAPS2_CAN_STRETCHRECT_FROM_TEXTURES)
5895 LogRel((" - D3DDEVCAPS2_CAN_STRETCHRECT_FROM_TEXTURES\n"));
5896 if (pCaps->DevCaps2 & D3DDEVCAPS2_DMAPNPATCH)
5897 LogRel((" - D3DDEVCAPS2_DMAPNPATCH\n"));
5898 if (pCaps->DevCaps2 & D3DDEVCAPS2_PRESAMPLEDDMAPNPATCH)
5899 LogRel((" - D3DDEVCAPS2_PRESAMPLEDDMAPNPATCH\n"));
5900 if (pCaps->DevCaps2 & D3DDEVCAPS2_STREAMOFFSET)
5901 LogRel((" - D3DDEVCAPS2_STREAMOFFSET\n"));
5902 if (pCaps->DevCaps2 & D3DDEVCAPS2_VERTEXELEMENTSCANSHARESTREAMOFFSET)
5903 LogRel((" - D3DDEVCAPS2_VERTEXELEMENTSCANSHARESTREAMOFFSET\n"));
5904
5905 LogRel(("\nCaps2:\n"));
5906 if (pCaps->Caps2 & D3DCAPS2_CANAUTOGENMIPMAP)
5907 LogRel((" - D3DCAPS2_CANAUTOGENMIPMAP\n"));
5908 if (pCaps->Caps2 & D3DCAPS2_CANCALIBRATEGAMMA)
5909 LogRel((" - D3DCAPS2_CANCALIBRATEGAMMA\n"));
5910 if (pCaps->Caps2 & D3DCAPS2_CANSHARERESOURCE)
5911 LogRel((" - D3DCAPS2_CANSHARERESOURCE\n"));
5912 if (pCaps->Caps2 & D3DCAPS2_CANMANAGERESOURCE)
5913 LogRel((" - D3DCAPS2_CANMANAGERESOURCE\n"));
5914 if (pCaps->Caps2 & D3DCAPS2_DYNAMICTEXTURES)
5915 LogRel((" - D3DCAPS2_DYNAMICTEXTURES\n"));
5916 if (pCaps->Caps2 & D3DCAPS2_FULLSCREENGAMMA)
5917 LogRel((" - D3DCAPS2_FULLSCREENGAMMA\n"));
5918
5919 LogRel(("\nCaps3:\n"));
5920 if (pCaps->Caps3 & D3DCAPS3_ALPHA_FULLSCREEN_FLIP_OR_DISCARD)
5921 LogRel((" - D3DCAPS3_ALPHA_FULLSCREEN_FLIP_OR_DISCARD\n"));
5922 if (pCaps->Caps3 & D3DCAPS3_COPY_TO_VIDMEM)
5923 LogRel((" - D3DCAPS3_COPY_TO_VIDMEM\n"));
5924 if (pCaps->Caps3 & D3DCAPS3_COPY_TO_SYSTEMMEM)
5925 LogRel((" - D3DCAPS3_COPY_TO_SYSTEMMEM\n"));
5926 if (pCaps->Caps3 & D3DCAPS3_DXVAHD)
5927 LogRel((" - D3DCAPS3_DXVAHD\n"));
5928 if (pCaps->Caps3 & D3DCAPS3_LINEAR_TO_SRGB_PRESENTATION)
5929 LogRel((" - D3DCAPS3_LINEAR_TO_SRGB_PRESENTATION\n"));
5930
5931 LogRel(("\nPresentationIntervals:\n"));
5932 if (pCaps->PresentationIntervals & D3DPRESENT_INTERVAL_IMMEDIATE)
5933 LogRel((" - D3DPRESENT_INTERVAL_IMMEDIATE\n"));
5934 if (pCaps->PresentationIntervals & D3DPRESENT_INTERVAL_ONE)
5935 LogRel((" - D3DPRESENT_INTERVAL_ONE\n"));
5936 if (pCaps->PresentationIntervals & D3DPRESENT_INTERVAL_TWO)
5937 LogRel((" - D3DPRESENT_INTERVAL_TWO\n"));
5938 if (pCaps->PresentationIntervals & D3DPRESENT_INTERVAL_THREE)
5939 LogRel((" - D3DPRESENT_INTERVAL_THREE\n"));
5940 if (pCaps->PresentationIntervals & D3DPRESENT_INTERVAL_FOUR)
5941 LogRel((" - D3DPRESENT_INTERVAL_FOUR\n"));
5942
5943 LogRel(("\nDevcaps:\n"));
5944 if (pCaps->DevCaps & D3DDEVCAPS_CANBLTSYSTONONLOCAL)
5945 LogRel((" - D3DDEVCAPS_CANBLTSYSTONONLOCAL\n"));
5946 if (pCaps->DevCaps & D3DDEVCAPS_CANRENDERAFTERFLIP)
5947 LogRel((" - D3DDEVCAPS_CANRENDERAFTERFLIP\n"));
5948 if (pCaps->DevCaps & D3DDEVCAPS_DRAWPRIMITIVES2)
5949 LogRel((" - D3DDEVCAPS_DRAWPRIMITIVES2\n"));
5950 if (pCaps->DevCaps & D3DDEVCAPS_DRAWPRIMITIVES2EX)
5951 LogRel((" - D3DDEVCAPS_DRAWPRIMITIVES2EX\n"));
5952 if (pCaps->DevCaps & D3DDEVCAPS_DRAWPRIMTLVERTEX)
5953 LogRel((" - D3DDEVCAPS_DRAWPRIMTLVERTEX\n"));
5954 if (pCaps->DevCaps & D3DDEVCAPS_EXECUTESYSTEMMEMORY)
5955 LogRel((" - D3DDEVCAPS_EXECUTESYSTEMMEMORY\n"));
5956 if (pCaps->DevCaps & D3DDEVCAPS_EXECUTEVIDEOMEMORY)
5957 LogRel((" - D3DDEVCAPS_EXECUTEVIDEOMEMORY\n"));
5958 if (pCaps->DevCaps & D3DDEVCAPS_HWRASTERIZATION)
5959 LogRel((" - D3DDEVCAPS_HWRASTERIZATION\n"));
5960 if (pCaps->DevCaps & D3DDEVCAPS_HWTRANSFORMANDLIGHT)
5961 LogRel((" - D3DDEVCAPS_HWTRANSFORMANDLIGHT\n"));
5962 if (pCaps->DevCaps & D3DDEVCAPS_NPATCHES)
5963 LogRel((" - D3DDEVCAPS_NPATCHES\n"));
5964 if (pCaps->DevCaps & D3DDEVCAPS_PUREDEVICE)
5965 LogRel((" - D3DDEVCAPS_PUREDEVICE\n"));
5966 if (pCaps->DevCaps & D3DDEVCAPS_QUINTICRTPATCHES)
5967 LogRel((" - D3DDEVCAPS_QUINTICRTPATCHES\n"));
5968 if (pCaps->DevCaps & D3DDEVCAPS_RTPATCHES)
5969 LogRel((" - D3DDEVCAPS_RTPATCHES\n"));
5970 if (pCaps->DevCaps & D3DDEVCAPS_RTPATCHHANDLEZERO)
5971 LogRel((" - D3DDEVCAPS_RTPATCHHANDLEZERO\n"));
5972 if (pCaps->DevCaps & D3DDEVCAPS_SEPARATETEXTUREMEMORIES)
5973 LogRel((" - D3DDEVCAPS_SEPARATETEXTUREMEMORIES\n"));
5974 if (pCaps->DevCaps & D3DDEVCAPS_TEXTURENONLOCALVIDMEM)
5975 LogRel((" - D3DDEVCAPS_TEXTURENONLOCALVIDMEM\n"));
5976 if (pCaps->DevCaps & D3DDEVCAPS_TEXTURESYSTEMMEMORY)
5977 LogRel((" - D3DDEVCAPS_TEXTURESYSTEMMEMORY\n"));
5978 if (pCaps->DevCaps & D3DDEVCAPS_TEXTUREVIDEOMEMORY)
5979 LogRel((" - D3DDEVCAPS_TEXTUREVIDEOMEMORY\n"));
5980 if (pCaps->DevCaps & D3DDEVCAPS_TLVERTEXSYSTEMMEMORY)
5981 LogRel((" - D3DDEVCAPS_TLVERTEXSYSTEMMEMORY\n"));
5982 if (pCaps->DevCaps & D3DDEVCAPS_TLVERTEXVIDEOMEMORY)
5983 LogRel((" - D3DDEVCAPS_TLVERTEXVIDEOMEMORY\n"));
5984
5985 LogRel(("\nTextureCaps:\n"));
5986 if (pCaps->TextureCaps & D3DPTEXTURECAPS_ALPHA)
5987 LogRel((" - D3DPTEXTURECAPS_ALPHA\n"));
5988 if (pCaps->TextureCaps & D3DPTEXTURECAPS_ALPHAPALETTE)
5989 LogRel((" - D3DPTEXTURECAPS_ALPHAPALETTE\n"));
5990 if (pCaps->TextureCaps & D3DPTEXTURECAPS_CUBEMAP)
5991 LogRel((" - D3DPTEXTURECAPS_CUBEMAP\n"));
5992 if (pCaps->TextureCaps & D3DPTEXTURECAPS_CUBEMAP_POW2)
5993 LogRel((" - D3DPTEXTURECAPS_CUBEMAP_POW2\n"));
5994 if (pCaps->TextureCaps & D3DPTEXTURECAPS_MIPCUBEMAP)
5995 LogRel((" - D3DPTEXTURECAPS_MIPCUBEMAP\n"));
5996 if (pCaps->TextureCaps & D3DPTEXTURECAPS_MIPMAP)
5997 LogRel((" - D3DPTEXTURECAPS_MIPMAP\n"));
5998 if (pCaps->TextureCaps & D3DPTEXTURECAPS_MIPVOLUMEMAP)
5999 LogRel((" - D3DPTEXTURECAPS_MIPVOLUMEMAP\n"));
6000 if (pCaps->TextureCaps & D3DPTEXTURECAPS_NONPOW2CONDITIONAL)
6001 LogRel((" - D3DPTEXTURECAPS_NONPOW2CONDITIONAL\n"));
6002 if (pCaps->TextureCaps & D3DPTEXTURECAPS_POW2)
6003 LogRel((" - D3DPTEXTURECAPS_POW2\n"));
6004 if (pCaps->TextureCaps & D3DPTEXTURECAPS_NOPROJECTEDBUMPENV)
6005 LogRel((" - D3DPTEXTURECAPS_NOPROJECTEDBUMPENV\n"));
6006 if (pCaps->TextureCaps & D3DPTEXTURECAPS_PERSPECTIVE)
6007 LogRel((" - D3DPTEXTURECAPS_PERSPECTIVE\n"));
6008 if (pCaps->TextureCaps & D3DPTEXTURECAPS_POW2)
6009 LogRel((" - D3DPTEXTURECAPS_POW2\n"));
6010 if (pCaps->TextureCaps & D3DPTEXTURECAPS_PROJECTED)
6011 LogRel((" - D3DPTEXTURECAPS_PROJECTED\n"));
6012 if (pCaps->TextureCaps & D3DPTEXTURECAPS_SQUAREONLY)
6013 LogRel((" - D3DPTEXTURECAPS_SQUAREONLY\n"));
6014 if (pCaps->TextureCaps & D3DPTEXTURECAPS_TEXREPEATNOTSCALEDBYSIZE)
6015 LogRel((" - D3DPTEXTURECAPS_TEXREPEATNOTSCALEDBYSIZE\n"));
6016 if (pCaps->TextureCaps & D3DPTEXTURECAPS_VOLUMEMAP)
6017 LogRel((" - D3DPTEXTURECAPS_VOLUMEMAP\n"));
6018 if (pCaps->TextureCaps & D3DPTEXTURECAPS_VOLUMEMAP_POW2)
6019 LogRel((" - D3DPTEXTURECAPS_VOLUMEMAP_POW2\n"));
6020
6021 LogRel(("\nTextureFilterCaps\n"));
6022 if (pCaps->TextureFilterCaps & D3DPTFILTERCAPS_CONVOLUTIONMONO)
6023 LogRel((" - D3DPTFILTERCAPS_CONVOLUTIONMONO\n"));
6024 if (pCaps->TextureFilterCaps & D3DPTFILTERCAPS_MAGFPOINT)
6025 LogRel((" - D3DPTFILTERCAPS_MAGFPOINT\n"));
6026 if (pCaps->TextureFilterCaps & D3DPTFILTERCAPS_MAGFLINEAR)
6027 LogRel((" - D3DPTFILTERCAPS_MAGFLINEAR\n"));
6028 if (pCaps->TextureFilterCaps & D3DPTFILTERCAPS_MAGFANISOTROPIC)
6029 LogRel((" - D3DPTFILTERCAPS_MAGFANISOTROPIC\n"));
6030 if (pCaps->TextureFilterCaps & D3DPTFILTERCAPS_MAGFPYRAMIDALQUAD)
6031 LogRel((" - D3DPTFILTERCAPS_MAGFPYRAMIDALQUAD\n"));
6032 if (pCaps->TextureFilterCaps & D3DPTFILTERCAPS_MAGFGAUSSIANQUAD)
6033 LogRel((" - D3DPTFILTERCAPS_MAGFGAUSSIANQUAD\n"));
6034 if (pCaps->TextureFilterCaps & D3DPTFILTERCAPS_MINFPOINT)
6035 LogRel((" - D3DPTFILTERCAPS_MINFPOINT\n"));
6036 if (pCaps->TextureFilterCaps & D3DPTFILTERCAPS_MINFLINEAR)
6037 LogRel((" - D3DPTFILTERCAPS_MINFLINEAR\n"));
6038 if (pCaps->TextureFilterCaps & D3DPTFILTERCAPS_MINFANISOTROPIC)
6039 LogRel((" - D3DPTFILTERCAPS_MINFANISOTROPIC\n"));
6040 if (pCaps->TextureFilterCaps & D3DPTFILTERCAPS_MINFPYRAMIDALQUAD)
6041 LogRel((" - D3DPTFILTERCAPS_MINFPYRAMIDALQUAD\n"));
6042 if (pCaps->TextureFilterCaps & D3DPTFILTERCAPS_MINFGAUSSIANQUAD)
6043 LogRel((" - D3DPTFILTERCAPS_MINFGAUSSIANQUAD\n"));
6044 if (pCaps->TextureFilterCaps & D3DPTFILTERCAPS_MIPFPOINT)
6045 LogRel((" - D3DPTFILTERCAPS_MIPFPOINT\n"));
6046 if (pCaps->TextureFilterCaps & D3DPTFILTERCAPS_MIPFLINEAR)
6047 LogRel((" - D3DPTFILTERCAPS_MIPFLINEAR\n"));
6048
6049 LogRel(("\nCubeTextureFilterCaps\n"));
6050 if (pCaps->CubeTextureFilterCaps & D3DPTFILTERCAPS_CONVOLUTIONMONO)
6051 LogRel((" - D3DPTFILTERCAPS_CONVOLUTIONMONO\n"));
6052 if (pCaps->CubeTextureFilterCaps & D3DPTFILTERCAPS_MAGFPOINT)
6053 LogRel((" - D3DPTFILTERCAPS_MAGFPOINT\n"));
6054 if (pCaps->CubeTextureFilterCaps & D3DPTFILTERCAPS_MAGFLINEAR)
6055 LogRel((" - D3DPTFILTERCAPS_MAGFLINEAR\n"));
6056 if (pCaps->CubeTextureFilterCaps & D3DPTFILTERCAPS_MAGFANISOTROPIC)
6057 LogRel((" - D3DPTFILTERCAPS_MAGFANISOTROPIC\n"));
6058 if (pCaps->CubeTextureFilterCaps & D3DPTFILTERCAPS_MAGFPYRAMIDALQUAD)
6059 LogRel((" - D3DPTFILTERCAPS_MAGFPYRAMIDALQUAD\n"));
6060 if (pCaps->CubeTextureFilterCaps & D3DPTFILTERCAPS_MAGFGAUSSIANQUAD)
6061 LogRel((" - D3DPTFILTERCAPS_MAGFGAUSSIANQUAD\n"));
6062 if (pCaps->CubeTextureFilterCaps & D3DPTFILTERCAPS_MINFPOINT)
6063 LogRel((" - D3DPTFILTERCAPS_MINFPOINT\n"));
6064 if (pCaps->CubeTextureFilterCaps & D3DPTFILTERCAPS_MINFLINEAR)
6065 LogRel((" - D3DPTFILTERCAPS_MINFLINEAR\n"));
6066 if (pCaps->CubeTextureFilterCaps & D3DPTFILTERCAPS_MINFANISOTROPIC)
6067 LogRel((" - D3DPTFILTERCAPS_MINFANISOTROPIC\n"));
6068 if (pCaps->CubeTextureFilterCaps & D3DPTFILTERCAPS_MINFPYRAMIDALQUAD)
6069 LogRel((" - D3DPTFILTERCAPS_MINFPYRAMIDALQUAD\n"));
6070 if (pCaps->CubeTextureFilterCaps & D3DPTFILTERCAPS_MINFGAUSSIANQUAD)
6071 LogRel((" - D3DPTFILTERCAPS_MINFGAUSSIANQUAD\n"));
6072 if (pCaps->CubeTextureFilterCaps & D3DPTFILTERCAPS_MIPFPOINT)
6073 LogRel((" - D3DPTFILTERCAPS_MIPFPOINT\n"));
6074 if (pCaps->CubeTextureFilterCaps & D3DPTFILTERCAPS_MIPFLINEAR)
6075 LogRel((" - D3DPTFILTERCAPS_MIPFLINEAR\n"));
6076
6077 LogRel(("\nVolumeTextureFilterCaps\n"));
6078 if (pCaps->VolumeTextureFilterCaps & D3DPTFILTERCAPS_CONVOLUTIONMONO)
6079 LogRel((" - D3DPTFILTERCAPS_CONVOLUTIONMONO\n"));
6080 if (pCaps->VolumeTextureFilterCaps & D3DPTFILTERCAPS_MAGFPOINT)
6081 LogRel((" - D3DPTFILTERCAPS_MAGFPOINT\n"));
6082 if (pCaps->VolumeTextureFilterCaps & D3DPTFILTERCAPS_MAGFLINEAR)
6083 LogRel((" - D3DPTFILTERCAPS_MAGFLINEAR\n"));
6084 if (pCaps->VolumeTextureFilterCaps & D3DPTFILTERCAPS_MAGFANISOTROPIC)
6085 LogRel((" - D3DPTFILTERCAPS_MAGFANISOTROPIC\n"));
6086 if (pCaps->VolumeTextureFilterCaps & D3DPTFILTERCAPS_MAGFPYRAMIDALQUAD)
6087 LogRel((" - D3DPTFILTERCAPS_MAGFPYRAMIDALQUAD\n"));
6088 if (pCaps->VolumeTextureFilterCaps & D3DPTFILTERCAPS_MAGFGAUSSIANQUAD)
6089 LogRel((" - D3DPTFILTERCAPS_MAGFGAUSSIANQUAD\n"));
6090 if (pCaps->VolumeTextureFilterCaps & D3DPTFILTERCAPS_MINFPOINT)
6091 LogRel((" - D3DPTFILTERCAPS_MINFPOINT\n"));
6092 if (pCaps->VolumeTextureFilterCaps & D3DPTFILTERCAPS_MINFLINEAR)
6093 LogRel((" - D3DPTFILTERCAPS_MINFLINEAR\n"));
6094 if (pCaps->VolumeTextureFilterCaps & D3DPTFILTERCAPS_MINFANISOTROPIC)
6095 LogRel((" - D3DPTFILTERCAPS_MINFANISOTROPIC\n"));
6096 if (pCaps->VolumeTextureFilterCaps & D3DPTFILTERCAPS_MINFPYRAMIDALQUAD)
6097 LogRel((" - D3DPTFILTERCAPS_MINFPYRAMIDALQUAD\n"));
6098 if (pCaps->VolumeTextureFilterCaps & D3DPTFILTERCAPS_MINFGAUSSIANQUAD)
6099 LogRel((" - D3DPTFILTERCAPS_MINFGAUSSIANQUAD\n"));
6100 if (pCaps->VolumeTextureFilterCaps & D3DPTFILTERCAPS_MIPFPOINT)
6101 LogRel((" - D3DPTFILTERCAPS_MIPFPOINT\n"));
6102 if (pCaps->VolumeTextureFilterCaps & D3DPTFILTERCAPS_MIPFLINEAR)
6103 LogRel((" - D3DPTFILTERCAPS_MIPFLINEAR\n"));
6104
6105 LogRel(("\nTextureAddressCaps:\n"));
6106 if (pCaps->TextureAddressCaps & D3DPTADDRESSCAPS_BORDER)
6107 LogRel((" - D3DPTADDRESSCAPS_BORDER\n"));
6108 if (pCaps->TextureAddressCaps & D3DPTADDRESSCAPS_CLAMP)
6109 LogRel((" - D3DPTADDRESSCAPS_CLAMP\n"));
6110 if (pCaps->TextureAddressCaps & D3DPTADDRESSCAPS_INDEPENDENTUV)
6111 LogRel((" - D3DPTADDRESSCAPS_INDEPENDENTUV\n"));
6112 if (pCaps->TextureAddressCaps & D3DPTADDRESSCAPS_MIRROR)
6113 LogRel((" - D3DPTADDRESSCAPS_MIRROR\n"));
6114 if (pCaps->TextureAddressCaps & D3DPTADDRESSCAPS_MIRRORONCE)
6115 LogRel((" - D3DPTADDRESSCAPS_MIRRORONCE\n"));
6116 if (pCaps->TextureAddressCaps & D3DPTADDRESSCAPS_WRAP)
6117 LogRel((" - D3DPTADDRESSCAPS_WRAP\n"));
6118
6119 LogRel(("\nTextureOpCaps:\n"));
6120 if (pCaps->TextureOpCaps & D3DTEXOPCAPS_DISABLE)
6121 LogRel((" - D3DTEXOPCAPS_DISABLE\n"));
6122 if (pCaps->TextureOpCaps & D3DTEXOPCAPS_SELECTARG1)
6123 LogRel((" - D3DTEXOPCAPS_SELECTARG1\n"));
6124 if (pCaps->TextureOpCaps & D3DTEXOPCAPS_SELECTARG2)
6125 LogRel((" - D3DTEXOPCAPS_SELECTARG2\n"));
6126 if (pCaps->TextureOpCaps & D3DTEXOPCAPS_MODULATE)
6127 LogRel((" - D3DTEXOPCAPS_MODULATE\n"));
6128 if (pCaps->TextureOpCaps & D3DTEXOPCAPS_MODULATE2X)
6129 LogRel((" - D3DTEXOPCAPS_MODULATE2X\n"));
6130 if (pCaps->TextureOpCaps & D3DTEXOPCAPS_MODULATE4X)
6131 LogRel((" - D3DTEXOPCAPS_MODULATE4X\n"));
6132 if (pCaps->TextureOpCaps & D3DTEXOPCAPS_ADD)
6133 LogRel((" - D3DTEXOPCAPS_ADD\n"));
6134 if (pCaps->TextureOpCaps & D3DTEXOPCAPS_ADDSIGNED)
6135 LogRel((" - D3DTEXOPCAPS_ADDSIGNED\n"));
6136 if (pCaps->TextureOpCaps & D3DTEXOPCAPS_ADDSIGNED2X)
6137 LogRel((" - D3DTEXOPCAPS_ADDSIGNED2X\n"));
6138 if (pCaps->TextureOpCaps & D3DTEXOPCAPS_SUBTRACT)
6139 LogRel((" - D3DTEXOPCAPS_SUBTRACT\n"));
6140 if (pCaps->TextureOpCaps & D3DTEXOPCAPS_ADDSMOOTH)
6141 LogRel((" - D3DTEXOPCAPS_ADDSMOOTH\n"));
6142 if (pCaps->TextureOpCaps & D3DTEXOPCAPS_BLENDDIFFUSEALPHA)
6143 LogRel((" - D3DTEXOPCAPS_BLENDDIFFUSEALPHA\n"));
6144 if (pCaps->TextureOpCaps & D3DTEXOPCAPS_BLENDTEXTUREALPHA)
6145 LogRel((" - D3DTEXOPCAPS_BLENDTEXTUREALPHA\n"));
6146 if (pCaps->TextureOpCaps & D3DTEXOPCAPS_BLENDFACTORALPHA)
6147 LogRel((" - D3DTEXOPCAPS_BLENDFACTORALPHA\n"));
6148 if (pCaps->TextureOpCaps & D3DTEXOPCAPS_BLENDTEXTUREALPHAPM)
6149 LogRel((" - D3DTEXOPCAPS_BLENDTEXTUREALPHAPM\n"));
6150 if (pCaps->TextureOpCaps & D3DTEXOPCAPS_BLENDCURRENTALPHA)
6151 LogRel((" - D3DTEXOPCAPS_BLENDCURRENTALPHA\n"));
6152 if (pCaps->TextureOpCaps & D3DTEXOPCAPS_PREMODULATE)
6153 LogRel((" - D3DTEXOPCAPS_PREMODULATE\n"));
6154 if (pCaps->TextureOpCaps & D3DTEXOPCAPS_MODULATEALPHA_ADDCOLOR)
6155 LogRel((" - D3DTEXOPCAPS_MODULATEALPHA_ADDCOLOR\n"));
6156 if (pCaps->TextureOpCaps & D3DTEXOPCAPS_MODULATECOLOR_ADDALPHA)
6157 LogRel((" - D3DTEXOPCAPS_MODULATECOLOR_ADDALPHA\n"));
6158 if (pCaps->TextureOpCaps & D3DTEXOPCAPS_MODULATEINVALPHA_ADDCOLOR)
6159 LogRel((" - D3DTEXOPCAPS_MODULATEINVALPHA_ADDCOLOR\n"));
6160 if (pCaps->TextureOpCaps & D3DTEXOPCAPS_MODULATEINVCOLOR_ADDALPHA)
6161 LogRel((" - D3DTEXOPCAPS_MODULATEINVCOLOR_ADDALPHA\n"));
6162 if (pCaps->TextureOpCaps & D3DTEXOPCAPS_BUMPENVMAP)
6163 LogRel((" - D3DTEXOPCAPS_BUMPENVMAP\n"));
6164 if (pCaps->TextureOpCaps & D3DTEXOPCAPS_BUMPENVMAPLUMINANCE)
6165 LogRel((" - D3DTEXOPCAPS_BUMPENVMAPLUMINANCE\n"));
6166 if (pCaps->TextureOpCaps & D3DTEXOPCAPS_DOTPRODUCT3)
6167 LogRel((" - D3DTEXOPCAPS_DOTPRODUCT3\n"));
6168 if (pCaps->TextureOpCaps & D3DTEXOPCAPS_MULTIPLYADD)
6169 LogRel((" - D3DTEXOPCAPS_MULTIPLYADD\n"));
6170 if (pCaps->TextureOpCaps & D3DTEXOPCAPS_LERP)
6171 LogRel((" - D3DTEXOPCAPS_LERP\n"));
6172
6173
6174 LogRel(("\n"));
6175 LogRel(("PixelShaderVersion: %#x (%u.%u)\n", pCaps->PixelShaderVersion,
6176 D3DSHADER_VERSION_MAJOR(pCaps->PixelShaderVersion), D3DSHADER_VERSION_MINOR(pCaps->PixelShaderVersion)));
6177 LogRel(("VertexShaderVersion: %#x (%u.%u)\n", pCaps->VertexShaderVersion,
6178 D3DSHADER_VERSION_MAJOR(pCaps->VertexShaderVersion), D3DSHADER_VERSION_MINOR(pCaps->VertexShaderVersion)));
6179
6180 LogRel(("\n"));
6181 RTLogRelSetBuffering(fBufferingSaved);
6182}
6183
6184/**
6185 * Worker for vmsvga3dUpdateHeapBuffersForSurfaces.
6186 *
6187 * This will allocate heap buffers if necessary, thus increasing the memory
6188 * usage of the process.
6189 *
6190 * @todo Would be interesting to share this code with the saved state code.
6191 *
6192 * @returns VBox status code.
6193 * @param pThisCC The VGA/VMSVGA context.
6194 * @param pSurface The surface to refresh the heap buffers for.
6195 */
6196static DECLCALLBACK(int) vmsvga3dBackSurfaceUpdateHeapBuffers(PVGASTATECC pThisCC, PVMSVGA3DSURFACE pSurface)
6197{
6198 PVMSVGA3DSTATE pState = pThisCC->svga.p3dState;
6199 AssertReturn(pState, VERR_INVALID_STATE);
6200
6201 /*
6202 * Currently we've got trouble retreving bit for DEPTHSTENCIL
6203 * surfaces both for OpenGL and D3D, so skip these here (don't
6204 * wast memory on them).
6205 */
6206 uint32_t const fSwitchFlags = pSurface->f.s.surface1Flags & VMSVGA3D_SURFACE_HINT_SWITCH_MASK;
6207 if ( fSwitchFlags != SVGA3D_SURFACE_HINT_DEPTHSTENCIL
6208 && fSwitchFlags != (SVGA3D_SURFACE_HINT_DEPTHSTENCIL | SVGA3D_SURFACE_HINT_TEXTURE))
6209 {
6210
6211 /*
6212 * Work thru each mipmap level for each face.
6213 */
6214 for (uint32_t iFace = 0; iFace < pSurface->cFaces; iFace++)
6215 {
6216 PVMSVGA3DMIPMAPLEVEL pMipmapLevel = &pSurface->paMipmapLevels[iFace * pSurface->cLevels];
6217 for (uint32_t i = 0; i < pSurface->cLevels; i++, pMipmapLevel++)
6218 {
6219 if (VMSVGA3DSURFACE_HAS_HW_SURFACE(pSurface))
6220 {
6221 Assert(pMipmapLevel->cbSurface);
6222 Assert(pMipmapLevel->cbSurface == pMipmapLevel->cbSurfacePlane * pMipmapLevel->mipmapSize.depth);
6223
6224 /*
6225 * Make sure we've got surface memory buffer.
6226 */
6227 uint8_t *pbDst = (uint8_t *)pMipmapLevel->pSurfaceData;
6228 if (!pbDst)
6229 {
6230 pMipmapLevel->pSurfaceData = pbDst = (uint8_t *)RTMemAllocZ(pMipmapLevel->cbSurface);
6231 AssertReturn(pbDst, VERR_NO_MEMORY);
6232 }
6233
6234 /*
6235 * D3D specifics.
6236 */
6237 Assert(pSurface->enmD3DResType != VMSVGA3D_D3DRESTYPE_NONE);
6238
6239 HRESULT hr;
6240 switch (pSurface->enmD3DResType)
6241 {
6242 case VMSVGA3D_D3DRESTYPE_VOLUME_TEXTURE:
6243 AssertFailed(); /// @todo
6244 break;
6245
6246 case VMSVGA3D_D3DRESTYPE_SURFACE:
6247 case VMSVGA3D_D3DRESTYPE_TEXTURE:
6248 case VMSVGA3D_D3DRESTYPE_CUBE_TEXTURE:
6249 {
6250 /*
6251 * Lock the buffer and make it accessible to memcpy.
6252 */
6253 D3DLOCKED_RECT LockedRect;
6254 if (pSurface->enmD3DResType == VMSVGA3D_D3DRESTYPE_CUBE_TEXTURE)
6255 {
6256 hr = pSurface->u.pCubeTexture->LockRect(vmsvga3dCubemapFaceFromIndex(iFace),
6257 i, /* texture level */
6258 &LockedRect,
6259 NULL,
6260 D3DLOCK_READONLY);
6261 }
6262 else if (pSurface->enmD3DResType == VMSVGA3D_D3DRESTYPE_TEXTURE)
6263 {
6264 if (pSurface->bounce.pTexture)
6265 {
6266 if ( !pSurface->fDirty
6267 && RT_BOOL(fSwitchFlags & SVGA3D_SURFACE_HINT_RENDERTARGET))
6268 {
6269 /** @todo stricter checks for associated context */
6270 uint32_t cid = pSurface->idAssociatedContext;
6271 PVMSVGA3DCONTEXT pContext;
6272 int rc = vmsvga3dContextFromCid(pState, cid, &pContext);
6273 AssertRCReturn(rc, rc);
6274
6275 IDirect3DSurface9 *pDst = NULL;
6276 hr = pSurface->bounce.pTexture->GetSurfaceLevel(i, &pDst);
6277 AssertMsgReturn(hr == D3D_OK, ("GetSurfaceLevel failed with %#x\n", hr), VERR_INTERNAL_ERROR);
6278
6279 IDirect3DSurface9 *pSrc = NULL;
6280 hr = pSurface->u.pTexture->GetSurfaceLevel(i, &pSrc);
6281 AssertMsgReturn(hr == D3D_OK, ("GetSurfaceLevel failed with %#x\n", hr), VERR_INTERNAL_ERROR);
6282
6283 hr = pContext->pDevice->GetRenderTargetData(pSrc, pDst);
6284 AssertMsgReturn(hr == D3D_OK, ("GetRenderTargetData failed with %#x\n", hr), VERR_INTERNAL_ERROR);
6285
6286 pSrc->Release();
6287 pDst->Release();
6288 }
6289
6290 hr = pSurface->bounce.pTexture->LockRect(i, /* texture level */
6291 &LockedRect,
6292 NULL,
6293 D3DLOCK_READONLY);
6294 }
6295 else
6296 hr = pSurface->u.pTexture->LockRect(i, /* texture level */
6297 &LockedRect,
6298 NULL,
6299 D3DLOCK_READONLY);
6300 }
6301 else
6302 hr = pSurface->u.pSurface->LockRect(&LockedRect,
6303 NULL,
6304 D3DLOCK_READONLY);
6305 AssertMsgReturn(hr == D3D_OK, ("LockRect failed with %x\n", hr), VERR_INTERNAL_ERROR);
6306
6307 /*
6308 * Copy the data. Take care in case the pitch differs.
6309 */
6310 if (pMipmapLevel->cbSurfacePitch == (uint32_t)LockedRect.Pitch)
6311 memcpy(pbDst, LockedRect.pBits, pMipmapLevel->cbSurface);
6312 else
6313 for (uint32_t j = 0; j < pMipmapLevel->cBlocksY; j++)
6314 memcpy(pbDst + j * pMipmapLevel->cbSurfacePitch,
6315 (uint8_t *)LockedRect.pBits + j * LockedRect.Pitch,
6316 pMipmapLevel->cbSurfacePitch);
6317
6318 /*
6319 * Release the buffer.
6320 */
6321 if (fSwitchFlags & SVGA3D_SURFACE_HINT_TEXTURE)
6322 {
6323 if (pSurface->bounce.pTexture)
6324 {
6325 hr = pSurface->bounce.pTexture->UnlockRect(i);
6326 AssertMsgReturn(hr == D3D_OK, ("UnlockRect failed with %#x\n", hr), VERR_INTERNAL_ERROR);
6327 }
6328 else
6329 hr = pSurface->u.pTexture->UnlockRect(i);
6330 }
6331 else
6332 hr = pSurface->u.pSurface->UnlockRect();
6333 AssertMsgReturn(hr == D3D_OK, ("UnlockRect failed with %#x\n", hr), VERR_INTERNAL_ERROR);
6334 break;
6335 }
6336
6337 case VMSVGA3D_D3DRESTYPE_VERTEX_BUFFER:
6338 case VMSVGA3D_D3DRESTYPE_INDEX_BUFFER:
6339 {
6340 /* Current type of the buffer. */
6341 const bool fVertex = (pSurface->enmD3DResType == VMSVGA3D_D3DRESTYPE_VERTEX_BUFFER);
6342
6343 void *pvD3DData = NULL;
6344 if (fVertex)
6345 hr = pSurface->u.pVertexBuffer->Lock(0, 0, &pvD3DData, D3DLOCK_READONLY);
6346 else
6347 hr = pSurface->u.pIndexBuffer->Lock(0, 0, &pvD3DData, D3DLOCK_READONLY);
6348 AssertMsgReturn(hr == D3D_OK, ("Lock %s failed with %x\n", fVertex ? "vertex" : "index", hr), VERR_INTERNAL_ERROR);
6349
6350 memcpy(pbDst, pvD3DData, pMipmapLevel->cbSurface);
6351
6352 if (fVertex)
6353 hr = pSurface->u.pVertexBuffer->Unlock();
6354 else
6355 hr = pSurface->u.pIndexBuffer->Unlock();
6356 AssertMsg(hr == D3D_OK, ("Unlock %s failed with %x\n", fVertex ? "vertex" : "index", hr));
6357 break;
6358 }
6359
6360 default:
6361 AssertMsgFailed(("flags %#x, type %d\n", fSwitchFlags, pSurface->enmD3DResType));
6362 }
6363
6364 }
6365 /* else: There is no data in hardware yet, so whatever we got is already current. */
6366 }
6367 }
6368 }
6369
6370 return VINF_SUCCESS;
6371}
6372
6373
6374static DECLCALLBACK(int) vmsvga3dBackQueryInterface(PVGASTATECC pThisCC, char const *pszInterfaceName, void *pvInterfaceFuncs, size_t cbInterfaceFuncs)
6375{
6376 RT_NOREF(pThisCC);
6377
6378 int rc = VINF_SUCCESS;
6379 if (RTStrCmp(pszInterfaceName, VMSVGA3D_BACKEND_INTERFACE_NAME_3D) == 0)
6380 {
6381 if (cbInterfaceFuncs == sizeof(VMSVGA3DBACKENDFUNCS3D))
6382 {
6383 if (pvInterfaceFuncs)
6384 {
6385 VMSVGA3DBACKENDFUNCS3D *p = (VMSVGA3DBACKENDFUNCS3D *)pvInterfaceFuncs;
6386 p->pfnInit = vmsvga3dBackInit;
6387 p->pfnPowerOn = vmsvga3dBackPowerOn;
6388 p->pfnTerminate = vmsvga3dBackTerminate;
6389 p->pfnReset = vmsvga3dBackReset;
6390 p->pfnQueryCaps = vmsvga3dBackQueryCaps;
6391 p->pfnChangeMode = vmsvga3dBackChangeMode;
6392 p->pfnCreateTexture = vmsvga3dBackCreateTexture;
6393 p->pfnSurfaceDestroy = vmsvga3dBackSurfaceDestroy;
6394 p->pfnSurfaceInvalidateImage = vmsvga3dBackSurfaceInvalidateImage;
6395 p->pfnSurfaceCopy = vmsvga3dBackSurfaceCopy;
6396 p->pfnSurfaceDMACopyBox = vmsvga3dBackSurfaceDMACopyBox;
6397 p->pfnSurfaceStretchBlt = vmsvga3dBackSurfaceStretchBlt;
6398 p->pfnUpdateHostScreenViewport = vmsvga3dBackUpdateHostScreenViewport;
6399 p->pfnDefineScreen = vmsvga3dBackDefineScreen;
6400 p->pfnDestroyScreen = vmsvga3dBackDestroyScreen;
6401 p->pfnSurfaceBlitToScreen = vmsvga3dBackSurfaceBlitToScreen;
6402 p->pfnSurfaceUpdateHeapBuffers = vmsvga3dBackSurfaceUpdateHeapBuffers;
6403 }
6404 }
6405 else
6406 {
6407 AssertFailed();
6408 rc = VERR_INVALID_PARAMETER;
6409 }
6410 }
6411 else if (RTStrCmp(pszInterfaceName, VMSVGA3D_BACKEND_INTERFACE_NAME_VGPU9) == 0)
6412 {
6413 if (cbInterfaceFuncs == sizeof(VMSVGA3DBACKENDFUNCSVGPU9))
6414 {
6415 if (pvInterfaceFuncs)
6416 {
6417 VMSVGA3DBACKENDFUNCSVGPU9 *p = (VMSVGA3DBACKENDFUNCSVGPU9 *)pvInterfaceFuncs;
6418 p->pfnContextDefine = vmsvga3dBackContextDefine;
6419 p->pfnContextDestroy = vmsvga3dBackContextDestroy;
6420 p->pfnSetTransform = vmsvga3dBackSetTransform;
6421 p->pfnSetZRange = vmsvga3dBackSetZRange;
6422 p->pfnSetRenderState = vmsvga3dBackSetRenderState;
6423 p->pfnSetRenderTarget = vmsvga3dBackSetRenderTarget;
6424 p->pfnSetTextureState = vmsvga3dBackSetTextureState;
6425 p->pfnSetMaterial = vmsvga3dBackSetMaterial;
6426 p->pfnSetLightData = vmsvga3dBackSetLightData;
6427 p->pfnSetLightEnabled = vmsvga3dBackSetLightEnabled;
6428 p->pfnSetViewPort = vmsvga3dBackSetViewPort;
6429 p->pfnSetClipPlane = vmsvga3dBackSetClipPlane;
6430 p->pfnCommandClear = vmsvga3dBackCommandClear;
6431 p->pfnDrawPrimitives = vmsvga3dBackDrawPrimitives;
6432 p->pfnSetScissorRect = vmsvga3dBackSetScissorRect;
6433 p->pfnGenerateMipmaps = vmsvga3dBackGenerateMipmaps;
6434 p->pfnShaderDefine = vmsvga3dBackShaderDefine;
6435 p->pfnShaderDestroy = vmsvga3dBackShaderDestroy;
6436 p->pfnShaderSet = vmsvga3dBackShaderSet;
6437 p->pfnShaderSetConst = vmsvga3dBackShaderSetConst;
6438 p->pfnOcclusionQueryCreate = vmsvga3dBackOcclusionQueryCreate;
6439 p->pfnOcclusionQueryDelete = vmsvga3dBackOcclusionQueryDelete;
6440 p->pfnOcclusionQueryBegin = vmsvga3dBackOcclusionQueryBegin;
6441 p->pfnOcclusionQueryEnd = vmsvga3dBackOcclusionQueryEnd;
6442 p->pfnOcclusionQueryGetData = vmsvga3dBackOcclusionQueryGetData;
6443 }
6444 }
6445 else
6446 {
6447 AssertFailed();
6448 rc = VERR_INVALID_PARAMETER;
6449 }
6450 }
6451 else
6452 rc = VERR_NOT_IMPLEMENTED;
6453 return rc;
6454}
6455
6456
6457extern VMSVGA3DBACKENDDESC const g_BackendLegacy =
6458{
6459 "LEGACY",
6460 vmsvga3dBackQueryInterface
6461};
注意: 瀏覽 TracBrowser 來幫助您使用儲存庫瀏覽器

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