VirtualBox

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

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

Devices/Graphics: vmsvga3dQueryCaps for DX backend. bugref:9830

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