VirtualBox

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

Last change on this file since 97572 was 96407, checked in by vboxsync, 2 years ago

scm copyright and license note update

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

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