VirtualBox

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

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

Devices/Graphics,Main,include: Experimental graphics output (build fix). bugref:9695

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