VirtualBox

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

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

Devices/Graphics: Release texture object on failure, bugref:9747

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