VirtualBox

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

Last change on this file since 85012 was 84856, checked in by vboxsync, 5 years ago

scm

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