VirtualBox

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

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

3D: Clarify shader version

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 251.4 KB
Line 
1/* $Id: DevVGA-SVGA3d-win.cpp 86576 2020-10-14 15:09:53Z vboxsync $ */
2/** @file
3 * DevVMWare - VMWare SVGA device
4 */
5
6/*
7 * Copyright (C) 2013-2020 Oracle Corporation
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.virtualbox.org. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 */
17
18
19/*********************************************************************************************************************************
20* Header Files *
21*********************************************************************************************************************************/
22#define LOG_GROUP LOG_GROUP_DEV_VMSVGA
23#include <VBox/vmm/pdmdev.h>
24#include <VBox/version.h>
25#include <VBox/err.h>
26#include <VBox/log.h>
27#include <VBox/vmm/pgm.h>
28#include <VBox/AssertGuest.h>
29
30#include <iprt/assert.h>
31#include <iprt/semaphore.h>
32#include <iprt/uuid.h>
33#include <iprt/mem.h>
34#include <iprt/avl.h>
35
36#include <VBoxVideo.h> /* required by DevVGA.h */
37
38/* should go BEFORE any other DevVGA include to make all DevVGA.h config defines be visible */
39#include "DevVGA.h"
40
41#include "DevVGA-SVGA.h"
42#include "DevVGA-SVGA3d.h"
43#include "DevVGA-SVGA3d-internal.h"
44
45/* Enable to disassemble defined shaders. */
46#if defined(DEBUG) && 0 /* Disabled as we don't have the DirectX SDK avaible atm. */
47#define DUMP_SHADER_DISASSEMBLY
48#endif
49
50#ifdef DUMP_SHADER_DISASSEMBLY
51#include <d3dx9shader.h>
52#endif
53
54
55/*********************************************************************************************************************************
56* Defined Constants And Macros *
57*********************************************************************************************************************************/
58
59#define FOURCC_INTZ (D3DFORMAT)MAKEFOURCC('I', 'N', 'T', 'Z')
60#define FOURCC_NULL (D3DFORMAT)MAKEFOURCC('N', 'U', 'L', 'L')
61
62
63/*********************************************************************************************************************************
64* Structures and Typedefs *
65*********************************************************************************************************************************/
66
67typedef struct
68{
69 DWORD Usage;
70 D3DRESOURCETYPE ResourceType;
71 SVGA3dFormatOp FormatOp;
72} VMSVGA3DFORMATSUPPORT;
73
74
75/*********************************************************************************************************************************
76* Global Variables *
77*********************************************************************************************************************************/
78static VMSVGA3DFORMATSUPPORT const g_aFormatSupport[] =
79{
80 {
81 0,
82 D3DRTYPE_SURFACE,
83 SVGA3DFORMAT_OP_OFFSCREENPLAIN,
84 },
85 {
86 D3DUSAGE_RENDERTARGET,
87 D3DRTYPE_SURFACE,
88 (SVGA3dFormatOp) (SVGA3DFORMAT_OP_OFFSCREEN_RENDERTARGET | SVGA3DFORMAT_OP_SAME_FORMAT_RENDERTARGET),
89 },
90 {
91 D3DUSAGE_AUTOGENMIPMAP,
92 D3DRTYPE_TEXTURE,
93 SVGA3DFORMAT_OP_AUTOGENMIPMAP,
94 },
95 {
96 D3DUSAGE_DMAP,
97 D3DRTYPE_TEXTURE,
98 SVGA3DFORMAT_OP_DMAP,
99 },
100 {
101 0,
102 D3DRTYPE_TEXTURE,
103 SVGA3DFORMAT_OP_TEXTURE,
104 },
105 {
106 0,
107 D3DRTYPE_CUBETEXTURE,
108 SVGA3DFORMAT_OP_CUBETEXTURE,
109 },
110 {
111 0,
112 D3DRTYPE_VOLUMETEXTURE,
113 SVGA3DFORMAT_OP_VOLUMETEXTURE,
114 },
115 {
116 D3DUSAGE_QUERY_VERTEXTEXTURE,
117 D3DRTYPE_TEXTURE,
118 SVGA3DFORMAT_OP_VERTEXTEXTURE,
119 },
120 {
121 D3DUSAGE_QUERY_LEGACYBUMPMAP,
122 D3DRTYPE_TEXTURE,
123 SVGA3DFORMAT_OP_BUMPMAP,
124 },
125 {
126 D3DUSAGE_QUERY_SRGBREAD,
127 D3DRTYPE_TEXTURE,
128 SVGA3DFORMAT_OP_SRGBREAD,
129 },
130 {
131 D3DUSAGE_QUERY_SRGBWRITE,
132 D3DRTYPE_TEXTURE,
133 SVGA3DFORMAT_OP_SRGBWRITE,
134 }
135};
136
137static VMSVGA3DFORMATSUPPORT const g_aFeatureReject[] =
138{
139 {
140 D3DUSAGE_QUERY_WRAPANDMIP,
141 D3DRTYPE_TEXTURE,
142 SVGA3DFORMAT_OP_NOTEXCOORDWRAPNORMIP
143 },
144 {
145 D3DUSAGE_QUERY_FILTER,
146 D3DRTYPE_TEXTURE,
147 SVGA3DFORMAT_OP_NOFILTER
148 },
149 {
150 D3DUSAGE_QUERY_POSTPIXELSHADER_BLENDING,
151 D3DRTYPE_TEXTURE, /* ?? */
152 SVGA3DFORMAT_OP_NOALPHABLEND
153 },
154};
155
156
157/*********************************************************************************************************************************
158* Internal Functions *
159*********************************************************************************************************************************/
160static void vmsvgaDumpD3DCaps(D3DCAPS9 *pCaps, D3DADAPTER_IDENTIFIER9 const *pai9);
161
162
163int vmsvga3dInit(PPDMDEVINS pDevIns, PVGASTATE pThis, PVGASTATECC pThisCC)
164{
165 RT_NOREF(pDevIns, pThis);
166
167 PVMSVGA3DSTATE pState;
168 pThisCC->svga.p3dState = pState = (PVMSVGA3DSTATE)RTMemAllocZ(sizeof(VMSVGA3DSTATE));
169 AssertReturn(pThisCC->svga.p3dState, VERR_NO_MEMORY);
170
171 /* Create event semaphore. */
172 int rc = RTSemEventCreate(&pState->WndRequestSem);
173 if (RT_FAILURE(rc))
174 {
175 Log(("%s: Failed to create event semaphore for window handling.\n", __FUNCTION__));
176 return rc;
177 }
178
179 /* Create the async IO thread. */
180 rc = RTThreadCreate(&pState->pWindowThread, vmsvga3dWindowThread, pState->WndRequestSem, 0, RTTHREADTYPE_GUI, 0, "VMSVGA3DWND");
181 if (RT_FAILURE(rc))
182 {
183 AssertMsgFailed(("%s: Async IO Thread creation for 3d window handling failed rc=%d\n", __FUNCTION__, rc));
184 return rc;
185 }
186
187 return VINF_SUCCESS;
188}
189
190int vmsvga3dPowerOn(PPDMDEVINS pDevIns, PVGASTATE pThis, PVGASTATECC pThisCC)
191{
192 PVMSVGA3DSTATE pState = pThisCC->svga.p3dState;
193 AssertReturn(pThisCC->svga.p3dState, VERR_NO_MEMORY);
194 HRESULT hr;
195 RT_NOREF(pDevIns, pThis);
196
197 if (pState->pD3D9)
198 return VINF_SUCCESS; /* already initialized (load state) */
199
200#ifdef VBOX_VMSVGA3D_WITH_WINE_OPENGL
201 pState->pD3D9 = Direct3DCreate9(D3D_SDK_VERSION);
202 AssertReturn(pState->pD3D9, VERR_INTERNAL_ERROR);
203#else
204 /* Direct3DCreate9Ex was introduced in Vista, so resolve it dynamically. */
205 typedef HRESULT (WINAPI *PFNDIRECT3DCREATE9EX)(UINT, IDirect3D9Ex **);
206 PFNDIRECT3DCREATE9EX pfnDirect3dCreate9Ex = (PFNDIRECT3DCREATE9EX)RTLdrGetSystemSymbol("d3d9.dll", "Direct3DCreate9Ex");
207 if (!pfnDirect3dCreate9Ex)
208 return PDMDevHlpVMSetError(pDevIns, VERR_SYMBOL_NOT_FOUND, RT_SRC_POS,
209 "vmsvga3d: Unable to locate Direct3DCreate9Ex. This feature requires Vista and later.");
210 hr = pfnDirect3dCreate9Ex(D3D_SDK_VERSION, &pState->pD3D9);
211 AssertReturn(hr == D3D_OK, VERR_INTERNAL_ERROR);
212#endif
213 D3DADAPTER_IDENTIFIER9 ai9;
214 hr = pState->pD3D9->GetAdapterIdentifier(D3DADAPTER_DEFAULT, 0, &ai9);
215 AssertReturnStmt(hr == D3D_OK, D3D_RELEASE(pState->pD3D9), VERR_INTERNAL_ERROR);
216
217 hr = pState->pD3D9->GetDeviceCaps(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, &pState->caps);
218 AssertReturnStmt(hr == D3D_OK, D3D_RELEASE(pState->pD3D9), VERR_INTERNAL_ERROR);
219
220 vmsvgaDumpD3DCaps(&pState->caps, &ai9);
221
222 if (!D3D9CheckDeviceFormat(pState->pD3D9, 0, D3DRTYPE_TEXTURE, FOURCC_INTZ))
223 {
224 /* INTZ support is essential to support depth surfaces used as textures. */
225 LogRel(("VMSVGA: texture format INTZ not supported!!!\n"));
226 }
227 else
228 pState->fSupportedSurfaceINTZ = true;
229
230 if (!D3D9CheckDeviceFormat(pState->pD3D9, D3DUSAGE_RENDERTARGET, D3DRTYPE_SURFACE, FOURCC_NULL))
231 {
232 /* NULL is a dummy surface which can be used as a render target to save memory. */
233 LogRel(("VMSVGA: surface format NULL not supported!!!\n"));
234 }
235 else
236 pState->fSupportedSurfaceNULL = true;
237
238 /* Check if DX9 depth stencil textures are supported */
239 if (!D3D9CheckDeviceFormat(pState->pD3D9, D3DUSAGE_DEPTHSTENCIL, D3DRTYPE_TEXTURE, D3DFMT_D16))
240 {
241 LogRel(("VMSVGA: texture format D3DFMT_D16 not supported\n"));
242 }
243
244 if (!D3D9CheckDeviceFormat(pState->pD3D9, D3DUSAGE_DEPTHSTENCIL, D3DRTYPE_TEXTURE, D3DFMT_D24X8))
245 {
246 LogRel(("VMSVGA: texture format D3DFMT_D24X8 not supported\n"));
247 }
248
249 if (!D3D9CheckDeviceFormat(pState->pD3D9, D3DUSAGE_DEPTHSTENCIL, D3DRTYPE_TEXTURE, D3DFMT_D24S8))
250 {
251 LogRel(("VMSVGA: texture format D3DFMT_D24S8 not supported\n"));
252 }
253
254 /* Check some formats must be emulated. */
255 if (D3D9CheckDeviceFormat(pState->pD3D9, 0, D3DRTYPE_TEXTURE, D3DFMT_UYVY))
256 {
257 /* Native UYVY support has better performance. */
258 LogRel(("VMSVGA: texture format D3DFMT_UYVY supported\n"));
259 pState->fSupportedFormatUYVY = true;
260 }
261
262 if (D3D9CheckDeviceFormat(pState->pD3D9, 0, D3DRTYPE_TEXTURE, D3DFMT_YUY2))
263 {
264 /* Native YUY2 support has better performance. */
265 LogRel(("VMSVGA: texture format D3DFMT_YUY2 supported\n"));
266 pState->fSupportedFormatYUY2 = true;
267 }
268
269 if (D3D9CheckDeviceFormat(pState->pD3D9, D3DUSAGE_RENDERTARGET, D3DRTYPE_TEXTURE, D3DFMT_A8B8G8R8))
270 {
271 /* Native A8B8G8R8 support is good for OpenGL application in the guest. */
272 LogRel(("VMSVGA: texture format D3DFMT_A8B8G8R8 supported\n"));
273 pState->fSupportedFormatA8B8G8R8 = true;
274 }
275
276 return VINF_SUCCESS;
277}
278
279int vmsvga3dReset(PVGASTATECC pThisCC)
280{
281 PVMSVGA3DSTATE pState = pThisCC->svga.p3dState;
282 AssertReturn(pThisCC->svga.p3dState, VERR_NO_MEMORY);
283
284 /* Destroy all leftover surfaces. */
285 for (uint32_t i = 0; i < pState->cSurfaces; i++)
286 {
287 if (pState->papSurfaces[i]->id != SVGA3D_INVALID_ID)
288 vmsvga3dSurfaceDestroy(pThisCC, pState->papSurfaces[i]->id);
289 }
290
291 /* Destroy all leftover contexts. */
292 for (uint32_t i = 0; i < pState->cContexts; i++)
293 {
294 if (pState->papContexts[i]->id != SVGA3D_INVALID_ID)
295 vmsvga3dContextDestroy(pThisCC, pState->papContexts[i]->id);
296 }
297 return VINF_SUCCESS;
298}
299
300int vmsvga3dTerminate(PVGASTATECC pThisCC)
301{
302 PVMSVGA3DSTATE pState = pThisCC->svga.p3dState;
303 AssertReturn(pThisCC->svga.p3dState, VERR_NO_MEMORY);
304
305 int rc = vmsvga3dReset(pThisCC);
306 AssertRCReturn(rc, rc);
307
308 /* Terminate the window creation thread. */
309 rc = vmsvga3dSendThreadMessage(pState->pWindowThread, pState->WndRequestSem, WM_VMSVGA3D_EXIT, 0, 0);
310 AssertRCReturn(rc, rc);
311
312 RTSemEventDestroy(pState->WndRequestSem);
313
314 D3D_RELEASE(pState->pD3D9);
315
316 return VINF_SUCCESS;
317}
318
319void vmsvga3dUpdateHostScreenViewport(PVGASTATECC pThisCC, uint32_t idScreen, VMSVGAVIEWPORT const *pOldViewport)
320{
321 /** @todo Scroll the screen content without requiring the guest to redraw. */
322 RT_NOREF(pThisCC, idScreen, pOldViewport);
323}
324
325static uint32_t vmsvga3dGetSurfaceFormatSupport(PVMSVGA3DSTATE pState3D, uint32_t idx3dCaps, D3DFORMAT format)
326{
327 NOREF(idx3dCaps);
328 HRESULT hr;
329 uint32_t result = 0;
330
331 /* Try if the format can be used for the primary display. */
332 hr = pState3D->pD3D9->CheckDeviceFormat(D3DADAPTER_DEFAULT,
333 D3DDEVTYPE_HAL,
334 format,
335 0,
336 D3DRTYPE_SURFACE,
337 format);
338
339 for (unsigned i = 0; i < RT_ELEMENTS(g_aFormatSupport); i++)
340 {
341 hr = pState3D->pD3D9->CheckDeviceFormat(D3DADAPTER_DEFAULT,
342 D3DDEVTYPE_HAL,
343 D3DFMT_X8R8G8B8, /* assume standard 32-bit display mode */
344 g_aFormatSupport[i].Usage,
345 g_aFormatSupport[i].ResourceType,
346 format);
347 if (hr == D3D_OK)
348 result |= g_aFormatSupport[i].FormatOp;
349 }
350
351 /* Check for features only if the format is supported in any form. */
352 if (result)
353 {
354 for (unsigned i = 0; i < RT_ELEMENTS(g_aFeatureReject); i++)
355 {
356 hr = pState3D->pD3D9->CheckDeviceFormat(D3DADAPTER_DEFAULT,
357 D3DDEVTYPE_HAL,
358 D3DFMT_X8R8G8B8, /* assume standard 32-bit display mode */
359 g_aFeatureReject[i].Usage,
360 g_aFeatureReject[i].ResourceType,
361 format);
362 if (hr != D3D_OK)
363 result |= g_aFeatureReject[i].FormatOp;
364 }
365 }
366
367 /** @todo missing:
368 *
369 * SVGA3DFORMAT_OP_PIXELSIZE
370 */
371
372 switch (idx3dCaps)
373 {
374 case SVGA3D_DEVCAP_SURFACEFMT_X8R8G8B8:
375 case SVGA3D_DEVCAP_SURFACEFMT_X1R5G5B5:
376 case SVGA3D_DEVCAP_SURFACEFMT_R5G6B5:
377 result |= SVGA3DFORMAT_OP_MEMBEROFGROUP_ARGB
378 | SVGA3DFORMAT_OP_CONVERT_TO_ARGB
379 | SVGA3DFORMAT_OP_DISPLAYMODE /* Should not be set for alpha formats. */
380 | SVGA3DFORMAT_OP_3DACCELERATION; /* implies OP_DISPLAYMODE */
381 break;
382
383 case SVGA3D_DEVCAP_SURFACEFMT_A8R8G8B8:
384 case SVGA3D_DEVCAP_SURFACEFMT_A2R10G10B10:
385 case SVGA3D_DEVCAP_SURFACEFMT_A1R5G5B5:
386 case SVGA3D_DEVCAP_SURFACEFMT_A4R4G4B4:
387 result |= SVGA3DFORMAT_OP_MEMBEROFGROUP_ARGB
388 | SVGA3DFORMAT_OP_CONVERT_TO_ARGB
389 | SVGA3DFORMAT_OP_SAME_FORMAT_UP_TO_ALPHA_RENDERTARGET;
390 break;
391 /* These formats can't be used as textures on AMD drivers (Intel works).
392 * Still report them as textures to the guest and emulate them in the device.
393 */
394 case SVGA3D_DEVCAP_SURFACEFMT_UYVY:
395 case SVGA3D_DEVCAP_SURFACEFMT_YUY2:
396 result |= SVGA3DFORMAT_OP_TEXTURE;
397 break;
398 }
399 Log(("CAPS: %s =\n%s\n", vmsvga3dGetCapString(idx3dCaps), vmsvga3dGet3dFormatString(result)));
400
401 return result;
402}
403
404static uint32_t vmsvga3dGetDepthFormatSupport(PVMSVGA3DSTATE pState3D, uint32_t idx3dCaps, D3DFORMAT format)
405{
406 RT_NOREF(idx3dCaps);
407 HRESULT hr;
408 uint32_t result = 0;
409
410 hr = pState3D->pD3D9->CheckDeviceFormat(D3DADAPTER_DEFAULT,
411 D3DDEVTYPE_HAL,
412 D3DFMT_X8R8G8B8, /* assume standard 32-bit display mode */
413 D3DUSAGE_DEPTHSTENCIL,
414 D3DRTYPE_SURFACE,
415 format);
416 if (hr == D3D_OK)
417 result = SVGA3DFORMAT_OP_ZSTENCIL
418 | SVGA3DFORMAT_OP_ZSTENCIL_WITH_ARBITRARY_COLOR_DEPTH
419 | SVGA3DFORMAT_OP_TEXTURE /* Necessary for Ubuntu Unity */;
420
421 Log(("CAPS: %s =\n%s\n", vmsvga3dGetCapString(idx3dCaps), vmsvga3dGet3dFormatString(result)));
422 return result;
423}
424
425
426int vmsvga3dQueryCaps(PVGASTATECC pThisCC, uint32_t idx3dCaps, uint32_t *pu32Val)
427{
428 PVMSVGA3DSTATE pState = pThisCC->svga.p3dState;
429 AssertReturn(pState, VERR_NO_MEMORY);
430 D3DCAPS9 *pCaps = &pState->caps;
431 int rc = VINF_SUCCESS;
432
433 *pu32Val = 0;
434
435 switch (idx3dCaps)
436 {
437 case SVGA3D_DEVCAP_3D:
438 *pu32Val = 1; /* boolean? */
439 break;
440
441 case SVGA3D_DEVCAP_MAX_LIGHTS:
442 *pu32Val = pCaps->MaxActiveLights;
443 break;
444
445 case SVGA3D_DEVCAP_MAX_TEXTURES:
446 *pu32Val = pCaps->MaxSimultaneousTextures;
447 break;
448
449 case SVGA3D_DEVCAP_MAX_CLIP_PLANES:
450 *pu32Val = pCaps->MaxUserClipPlanes;
451 break;
452
453 case SVGA3D_DEVCAP_VERTEX_SHADER_VERSION:
454 switch (pCaps->VertexShaderVersion)
455 {
456 case D3DVS_VERSION(1,1):
457 *pu32Val = SVGA3DVSVERSION_11;
458 break;
459 case D3DVS_VERSION(2,0):
460 *pu32Val = SVGA3DVSVERSION_20;
461 break;
462 case D3DVS_VERSION(3,0):
463 *pu32Val = SVGA3DVSVERSION_30;
464 break;
465 case D3DVS_VERSION(4,0):
466 *pu32Val = SVGA3DVSVERSION_40;
467 break;
468 default:
469 LogRel(("VMSVGA: Unsupported vertex shader version %x\n", pCaps->VertexShaderVersion));
470 break;
471 }
472 break;
473
474 case SVGA3D_DEVCAP_VERTEX_SHADER:
475 /* boolean? */
476 *pu32Val = 1;
477 break;
478
479 case SVGA3D_DEVCAP_FRAGMENT_SHADER_VERSION:
480 switch (pCaps->PixelShaderVersion)
481 {
482 case D3DPS_VERSION(1,1):
483 *pu32Val = SVGA3DPSVERSION_11;
484 break;
485 case D3DPS_VERSION(1,2):
486 *pu32Val = SVGA3DPSVERSION_12;
487 break;
488 case D3DPS_VERSION(1,3):
489 *pu32Val = SVGA3DPSVERSION_13;
490 break;
491 case D3DPS_VERSION(1,4):
492 *pu32Val = SVGA3DPSVERSION_14;
493 break;
494 case D3DPS_VERSION(2,0):
495 *pu32Val = SVGA3DPSVERSION_20;
496 break;
497 case D3DPS_VERSION(3,0):
498 *pu32Val = SVGA3DPSVERSION_30;
499 break;
500 case D3DPS_VERSION(4,0):
501 *pu32Val = SVGA3DPSVERSION_40;
502 break;
503 default:
504 LogRel(("VMSVGA: Unsupported pixel shader version %x\n", pCaps->PixelShaderVersion));
505 break;
506 }
507 break;
508
509 case SVGA3D_DEVCAP_FRAGMENT_SHADER:
510 /* boolean? */
511 *pu32Val = 1;
512 break;
513
514 case SVGA3D_DEVCAP_S23E8_TEXTURES:
515 case SVGA3D_DEVCAP_S10E5_TEXTURES:
516 /* Must be obsolete by now; surface format caps specify the same thing. */
517 rc = VERR_INVALID_PARAMETER;
518 break;
519
520 case SVGA3D_DEVCAP_MAX_FIXED_VERTEXBLEND:
521 break;
522
523 /*
524 * 2. The BUFFER_FORMAT capabilities are deprecated, and they always
525 * return TRUE. Even on physical hardware that does not support
526 * these formats natively, the SVGA3D device will provide an emulation
527 * which should be invisible to the guest OS.
528 */
529 case SVGA3D_DEVCAP_D16_BUFFER_FORMAT:
530 case SVGA3D_DEVCAP_D24S8_BUFFER_FORMAT:
531 case SVGA3D_DEVCAP_D24X8_BUFFER_FORMAT:
532 *pu32Val = 1;
533 break;
534
535 case SVGA3D_DEVCAP_QUERY_TYPES:
536 break;
537
538 case SVGA3D_DEVCAP_TEXTURE_GRADIENT_SAMPLING:
539 break;
540
541 case SVGA3D_DEVCAP_MAX_POINT_SIZE:
542 AssertCompile(sizeof(uint32_t) == sizeof(float));
543 *(float *)pu32Val = pCaps->MaxPointSize;
544 break;
545
546 case SVGA3D_DEVCAP_MAX_SHADER_TEXTURES:
547 /** @todo ?? */
548 rc = VERR_INVALID_PARAMETER;
549 break;
550
551 case SVGA3D_DEVCAP_MAX_TEXTURE_WIDTH:
552 *pu32Val = pCaps->MaxTextureWidth;
553 break;
554
555 case SVGA3D_DEVCAP_MAX_TEXTURE_HEIGHT:
556 *pu32Val = pCaps->MaxTextureHeight;
557 break;
558
559 case SVGA3D_DEVCAP_MAX_VOLUME_EXTENT:
560 *pu32Val = pCaps->MaxVolumeExtent;
561 break;
562
563 case SVGA3D_DEVCAP_MAX_TEXTURE_REPEAT:
564 *pu32Val = pCaps->MaxTextureRepeat;
565 break;
566
567 case SVGA3D_DEVCAP_MAX_TEXTURE_ASPECT_RATIO:
568 *pu32Val = pCaps->MaxTextureAspectRatio;
569 break;
570
571 case SVGA3D_DEVCAP_MAX_TEXTURE_ANISOTROPY:
572 *pu32Val = pCaps->MaxAnisotropy;
573 break;
574
575 case SVGA3D_DEVCAP_MAX_PRIMITIVE_COUNT:
576 *pu32Val = pCaps->MaxPrimitiveCount;
577 break;
578
579 case SVGA3D_DEVCAP_MAX_VERTEX_INDEX:
580 *pu32Val = pCaps->MaxVertexIndex;
581 break;
582
583 case SVGA3D_DEVCAP_MAX_VERTEX_SHADER_INSTRUCTIONS:
584 *pu32Val = pCaps->MaxVertexShader30InstructionSlots;
585 break;
586
587 case SVGA3D_DEVCAP_MAX_FRAGMENT_SHADER_INSTRUCTIONS:
588 *pu32Val = pCaps->MaxPixelShader30InstructionSlots;
589 break;
590
591 case SVGA3D_DEVCAP_MAX_VERTEX_SHADER_TEMPS:
592 *pu32Val = pCaps->VS20Caps.NumTemps;
593 break;
594
595 case SVGA3D_DEVCAP_MAX_FRAGMENT_SHADER_TEMPS:
596 *pu32Val = pCaps->PS20Caps.NumTemps;
597 break;
598
599 case SVGA3D_DEVCAP_TEXTURE_OPS:
600 break;
601
602 case SVGA3D_DEVCAP_MULTISAMPLE_NONMASKABLESAMPLES:
603 break;
604
605 case SVGA3D_DEVCAP_MULTISAMPLE_MASKABLESAMPLES:
606 break;
607
608 case SVGA3D_DEVCAP_ALPHATOCOVERAGE:
609 break;
610
611 case SVGA3D_DEVCAP_SUPERSAMPLE:
612 break;
613
614 case SVGA3D_DEVCAP_AUTOGENMIPMAPS:
615 *pu32Val = !!(pCaps->Caps2 & D3DCAPS2_CANAUTOGENMIPMAP);
616 break;
617
618 case SVGA3D_DEVCAP_MAX_VERTEX_SHADER_TEXTURES:
619 break;
620
621 case SVGA3D_DEVCAP_MAX_RENDER_TARGETS: /** @todo same thing? */
622 case SVGA3D_DEVCAP_MAX_SIMULTANEOUS_RENDER_TARGETS:
623 *pu32Val = pCaps->NumSimultaneousRTs;
624 break;
625
626 /*
627 * This is the maximum number of SVGA context IDs that the guest
628 * can define using SVGA_3D_CMD_CONTEXT_DEFINE.
629 */
630 case SVGA3D_DEVCAP_MAX_CONTEXT_IDS:
631 *pu32Val = SVGA3D_MAX_CONTEXT_IDS;
632 break;
633
634 /*
635 * This is the maximum number of SVGA surface IDs that the guest
636 * can define using SVGA_3D_CMD_SURFACE_DEFINE*.
637 */
638 case SVGA3D_DEVCAP_MAX_SURFACE_IDS:
639 *pu32Val = SVGA3D_MAX_SURFACE_IDS;
640 break;
641
642 /* Supported surface formats. */
643 case SVGA3D_DEVCAP_SURFACEFMT_X8R8G8B8:
644 *pu32Val = vmsvga3dGetSurfaceFormatSupport(pState, idx3dCaps, D3DFMT_X8R8G8B8);
645 break;
646
647 case SVGA3D_DEVCAP_SURFACEFMT_A8R8G8B8:
648 *pu32Val = vmsvga3dGetSurfaceFormatSupport(pState, idx3dCaps, D3DFMT_A8R8G8B8);
649 break;
650
651 case SVGA3D_DEVCAP_SURFACEFMT_A2R10G10B10:
652 *pu32Val = vmsvga3dGetSurfaceFormatSupport(pState, idx3dCaps, D3DFMT_A2R10G10B10);
653 break;
654
655 case SVGA3D_DEVCAP_SURFACEFMT_X1R5G5B5:
656 *pu32Val = vmsvga3dGetSurfaceFormatSupport(pState, idx3dCaps, D3DFMT_X1R5G5B5);
657 break;
658
659 case SVGA3D_DEVCAP_SURFACEFMT_A1R5G5B5:
660 *pu32Val = vmsvga3dGetSurfaceFormatSupport(pState, idx3dCaps, D3DFMT_A1R5G5B5);
661 break;
662
663 case SVGA3D_DEVCAP_SURFACEFMT_A4R4G4B4:
664 *pu32Val = vmsvga3dGetSurfaceFormatSupport(pState, idx3dCaps, D3DFMT_A4R4G4B4);
665 break;
666
667 case SVGA3D_DEVCAP_SURFACEFMT_R5G6B5:
668 *pu32Val = vmsvga3dGetSurfaceFormatSupport(pState, idx3dCaps, D3DFMT_A4R4G4B4);
669 break;
670
671 case SVGA3D_DEVCAP_SURFACEFMT_LUMINANCE16:
672 *pu32Val = vmsvga3dGetSurfaceFormatSupport(pState, idx3dCaps, D3DFMT_L16);
673 break;
674
675 case SVGA3D_DEVCAP_SURFACEFMT_LUMINANCE8_ALPHA8:
676 *pu32Val = vmsvga3dGetSurfaceFormatSupport(pState, idx3dCaps, D3DFMT_A8L8);
677 break;
678
679 case SVGA3D_DEVCAP_SURFACEFMT_ALPHA8:
680 *pu32Val = vmsvga3dGetSurfaceFormatSupport(pState, idx3dCaps, D3DFMT_A8);
681 break;
682
683 case SVGA3D_DEVCAP_SURFACEFMT_LUMINANCE8:
684 *pu32Val = vmsvga3dGetSurfaceFormatSupport(pState, idx3dCaps, D3DFMT_L8);
685 break;
686
687 case SVGA3D_DEVCAP_SURFACEFMT_Z_D16:
688 *pu32Val = vmsvga3dGetDepthFormatSupport(pState, idx3dCaps, D3DFMT_D16);
689 break;
690
691 case SVGA3D_DEVCAP_SURFACEFMT_Z_D24S8:
692 case SVGA3D_DEVCAP_SURFACEFMT_Z_D24S8_INT: /** @todo not correct */
693 *pu32Val = vmsvga3dGetDepthFormatSupport(pState, idx3dCaps, D3DFMT_D24S8);
694 break;
695
696 case SVGA3D_DEVCAP_SURFACEFMT_Z_D24X8:
697 *pu32Val = vmsvga3dGetDepthFormatSupport(pState, idx3dCaps, D3DFMT_D24X8);
698 break;
699
700 case SVGA3D_DEVCAP_SURFACEFMT_Z_DF16:
701 /** @todo supposed to be floating-point, but unable to find a match for D3D9... */
702 *pu32Val = 0;
703 break;
704
705 case SVGA3D_DEVCAP_SURFACEFMT_Z_DF24:
706 *pu32Val = vmsvga3dGetDepthFormatSupport(pState, idx3dCaps, D3DFMT_D24FS8);
707 break;
708
709 case SVGA3D_DEVCAP_SURFACEFMT_DXT1:
710 *pu32Val = vmsvga3dGetSurfaceFormatSupport(pState, idx3dCaps, D3DFMT_DXT1);
711 break;
712
713 case SVGA3D_DEVCAP_SURFACEFMT_DXT2:
714 *pu32Val = vmsvga3dGetSurfaceFormatSupport(pState, idx3dCaps, D3DFMT_DXT2);
715 break;
716
717 case SVGA3D_DEVCAP_SURFACEFMT_DXT3:
718 *pu32Val = vmsvga3dGetSurfaceFormatSupport(pState, idx3dCaps, D3DFMT_DXT3);
719 break;
720
721 case SVGA3D_DEVCAP_SURFACEFMT_DXT4:
722 *pu32Val = vmsvga3dGetSurfaceFormatSupport(pState, idx3dCaps, D3DFMT_DXT4);
723 break;
724
725 case SVGA3D_DEVCAP_SURFACEFMT_DXT5:
726 *pu32Val = vmsvga3dGetSurfaceFormatSupport(pState, idx3dCaps, D3DFMT_DXT5);
727 break;
728
729 case SVGA3D_DEVCAP_SURFACEFMT_BUMPX8L8V8U8:
730 *pu32Val = vmsvga3dGetSurfaceFormatSupport(pState, idx3dCaps, D3DFMT_X8L8V8U8);
731 break;
732
733 case SVGA3D_DEVCAP_SURFACEFMT_A2W10V10U10:
734 *pu32Val = vmsvga3dGetSurfaceFormatSupport(pState, idx3dCaps, D3DFMT_A2W10V10U10);
735 break;
736
737 case SVGA3D_DEVCAP_SURFACEFMT_BUMPU8V8:
738 *pu32Val = vmsvga3dGetSurfaceFormatSupport(pState, idx3dCaps, D3DFMT_V8U8);
739 break;
740
741 case SVGA3D_DEVCAP_SURFACEFMT_Q8W8V8U8:
742 *pu32Val = vmsvga3dGetSurfaceFormatSupport(pState, idx3dCaps, D3DFMT_Q8W8V8U8);
743 break;
744
745 case SVGA3D_DEVCAP_SURFACEFMT_CxV8U8:
746 *pu32Val = vmsvga3dGetSurfaceFormatSupport(pState, idx3dCaps, D3DFMT_CxV8U8);
747 break;
748
749 case SVGA3D_DEVCAP_SURFACEFMT_R_S10E5:
750 *pu32Val = vmsvga3dGetSurfaceFormatSupport(pState, idx3dCaps, D3DFMT_R16F);
751 break;
752
753 case SVGA3D_DEVCAP_SURFACEFMT_R_S23E8:
754 *pu32Val = vmsvga3dGetSurfaceFormatSupport(pState, idx3dCaps, D3DFMT_R32F);
755 break;
756
757 case SVGA3D_DEVCAP_SURFACEFMT_RG_S10E5:
758 *pu32Val = vmsvga3dGetSurfaceFormatSupport(pState, idx3dCaps, D3DFMT_G16R16F);
759 break;
760
761 case SVGA3D_DEVCAP_SURFACEFMT_RG_S23E8:
762 *pu32Val = vmsvga3dGetSurfaceFormatSupport(pState, idx3dCaps, D3DFMT_G32R32F);
763 break;
764
765 case SVGA3D_DEVCAP_SURFACEFMT_ARGB_S10E5:
766 *pu32Val = vmsvga3dGetSurfaceFormatSupport(pState, idx3dCaps, D3DFMT_A16B16G16R16F);
767 break;
768
769 case SVGA3D_DEVCAP_SURFACEFMT_ARGB_S23E8:
770 *pu32Val = vmsvga3dGetSurfaceFormatSupport(pState, idx3dCaps, D3DFMT_A32B32G32R32F);
771 break;
772
773 case SVGA3D_DEVCAP_SURFACEFMT_V16U16:
774 *pu32Val = vmsvga3dGetSurfaceFormatSupport(pState, idx3dCaps, D3DFMT_V16U16);
775 break;
776
777 case SVGA3D_DEVCAP_SURFACEFMT_G16R16:
778 *pu32Val = vmsvga3dGetSurfaceFormatSupport(pState, idx3dCaps, D3DFMT_G16R16);
779 break;
780
781 case SVGA3D_DEVCAP_SURFACEFMT_A16B16G16R16:
782 *pu32Val = vmsvga3dGetSurfaceFormatSupport(pState, idx3dCaps, D3DFMT_A16B16G16R16);
783 break;
784
785 case SVGA3D_DEVCAP_SURFACEFMT_UYVY:
786 *pu32Val = vmsvga3dGetSurfaceFormatSupport(pState, idx3dCaps, D3DFMT_UYVY);
787 break;
788
789 case SVGA3D_DEVCAP_SURFACEFMT_YUY2:
790 *pu32Val = vmsvga3dGetSurfaceFormatSupport(pState, idx3dCaps, D3DFMT_YUY2);
791 break;
792
793 case SVGA3D_DEVCAP_SURFACEFMT_NV12:
794 *pu32Val = vmsvga3dGetSurfaceFormatSupport(pState, idx3dCaps, (D3DFORMAT)MAKEFOURCC('N', 'V', '1', '2'));
795 break;
796
797 case SVGA3D_DEVCAP_SURFACEFMT_AYUV:
798 *pu32Val = vmsvga3dGetSurfaceFormatSupport(pState, idx3dCaps, (D3DFORMAT)MAKEFOURCC('A', 'Y', 'U', 'V'));
799 break;
800
801 case SVGA3D_DEVCAP_SURFACEFMT_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->faces[0].numMipLevels;
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->faces[0].numMipLevels;
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->faces[0].numMipLevels == 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->faces[0].numMipLevels; i++)
2226 {
2227 RTMemFree(pSurface->paMipmapLevels[i].pSurfaceData);
2228 pSurface->paMipmapLevels[i].pSurfaceData = NULL;
2229 }
2230
2231 /* Track the UpdateTexture operation. */
2232 vmsvga3dSurfaceTrackUsage(pState, pContext, pSurface);
2233 }
2234 pSurface->fDirty = false;
2235
2236 Assert(pSurface->enmD3DResType != VMSVGA3D_D3DRESTYPE_NONE);
2237
2238 pSurface->surfaceFlags |= SVGA3D_SURFACE_HINT_TEXTURE;
2239 pSurface->idAssociatedContext = idAssociatedContext;
2240 return VINF_SUCCESS;
2241}
2242
2243
2244/**
2245 * Backend worker for implementing SVGA_3D_CMD_SURFACE_STRETCHBLT.
2246 *
2247 * @returns VBox status code.
2248 * @param pThis The VGA device instance.
2249 * @param pState The VMSVGA3d state.
2250 * @param pDstSurface The destination host surface.
2251 * @param uDstFace The destination face (valid).
2252 * @param uDstMipmap The destination mipmap level (valid).
2253 * @param pDstBox The destination box.
2254 * @param pSrcSurface The source host surface.
2255 * @param uSrcFace The destination face (valid).
2256 * @param uSrcMipmap The source mimap level (valid).
2257 * @param pSrcBox The source box.
2258 * @param enmMode The strecht blt mode .
2259 * @param pContext The VMSVGA3d context (already current for OGL).
2260 */
2261int vmsvga3dBackSurfaceStretchBlt(PVGASTATE pThis, PVMSVGA3DSTATE pState,
2262 PVMSVGA3DSURFACE pDstSurface, uint32_t uDstFace, uint32_t uDstMipmap, SVGA3dBox const *pDstBox,
2263 PVMSVGA3DSURFACE pSrcSurface, uint32_t uSrcFace, uint32_t uSrcMipmap, SVGA3dBox const *pSrcBox,
2264 SVGA3dStretchBltMode enmMode, PVMSVGA3DCONTEXT pContext)
2265{
2266 RT_NOREF(pThis);
2267
2268 HRESULT hr;
2269 int rc;
2270
2271 AssertReturn(pSrcSurface->enmD3DResType != VMSVGA3D_D3DRESTYPE_VOLUME_TEXTURE, VERR_NOT_IMPLEMENTED);
2272 AssertReturn(pDstSurface->enmD3DResType != VMSVGA3D_D3DRESTYPE_VOLUME_TEXTURE, VERR_NOT_IMPLEMENTED);
2273
2274 /* Flush the drawing pipeline for this surface as it could be used in a shared context. */
2275 vmsvga3dSurfaceFlush(pSrcSurface);
2276 vmsvga3dSurfaceFlush(pDstSurface);
2277
2278 RECT RectSrc;
2279 RectSrc.left = pSrcBox->x;
2280 RectSrc.top = pSrcBox->y;
2281 RectSrc.right = pSrcBox->x + pSrcBox->w; /* exclusive */
2282 RectSrc.bottom = pSrcBox->y + pSrcBox->h; /* exclusive */
2283 Assert(!pSrcBox->z);
2284
2285 RECT RectDst;
2286 RectDst.left = pDstBox->x;
2287 RectDst.top = pDstBox->y;
2288 RectDst.right = pDstBox->x + pDstBox->w; /* exclusive */
2289 RectDst.bottom = pDstBox->y + pDstBox->h; /* exclusive */
2290 Assert(!pDstBox->z);
2291
2292 IDirect3DSurface9 *pSrc;
2293 rc = vmsvga3dGetD3DSurface(pState, pContext, pSrcSurface, uSrcFace, uSrcMipmap, false, &pSrc);
2294 AssertRCReturn(rc, rc);
2295
2296 IDirect3DSurface9 *pDst;
2297 rc = vmsvga3dGetD3DSurface(pState, pContext, pDstSurface, uDstFace, uDstMipmap, false, &pDst);
2298 AssertRCReturn(rc, rc);
2299
2300 D3DTEXTUREFILTERTYPE moded3d;
2301 switch (enmMode)
2302 {
2303 case SVGA3D_STRETCH_BLT_POINT:
2304 moded3d = D3DTEXF_POINT;
2305 break;
2306
2307 case SVGA3D_STRETCH_BLT_LINEAR:
2308 moded3d = D3DTEXF_LINEAR;
2309 break;
2310
2311 default:
2312 AssertFailed();
2313 moded3d = D3DTEXF_NONE;
2314 break;
2315 }
2316
2317 hr = pContext->pDevice->StretchRect(pSrc, &RectSrc, pDst, &RectDst, moded3d);
2318
2319 D3D_RELEASE(pDst);
2320 D3D_RELEASE(pSrc);
2321
2322 AssertMsgReturn(hr == D3D_OK, ("StretchRect failed with %x\n", hr), VERR_INTERNAL_ERROR);
2323
2324 /* Track the StretchRect operation. */
2325 vmsvga3dSurfaceTrackUsage(pState, pContext, pSrcSurface);
2326 vmsvga3dSurfaceTrackUsage(pState, pContext, pDstSurface);
2327
2328 return VINF_SUCCESS;
2329}
2330
2331
2332/**
2333 * Backend worker for implementing SVGA_3D_CMD_SURFACE_DMA that copies one box.
2334 *
2335 * @returns Failure status code or @a rc.
2336 * @param pThis The shared VGA/VMSVGA instance data.
2337 * @param pThisCC The VGA/VMSVGA state for ring-3.
2338 * @param pState The VMSVGA3d state.
2339 * @param pSurface The host surface.
2340 * @param pMipLevel Mipmap level. The caller knows it already.
2341 * @param uHostFace The host face (valid).
2342 * @param uHostMipmap The host mipmap level (valid).
2343 * @param GuestPtr The guest pointer.
2344 * @param cbGuestPitch The guest pitch.
2345 * @param transfer The transfer direction.
2346 * @param pBox The box to copy (clipped, valid, except for guest's srcx, srcy, srcz).
2347 * @param pContext The context (for OpenGL).
2348 * @param rc The current rc for all boxes.
2349 * @param iBox The current box number (for Direct 3D).
2350 */
2351int vmsvga3dBackSurfaceDMACopyBox(PVGASTATE pThis, PVGASTATECC pThisCC, PVMSVGA3DSTATE pState, PVMSVGA3DSURFACE pSurface,
2352 PVMSVGA3DMIPMAPLEVEL pMipLevel, uint32_t uHostFace, uint32_t uHostMipmap,
2353 SVGAGuestPtr GuestPtr, uint32_t cbGuestPitch, SVGA3dTransferType transfer,
2354 SVGA3dCopyBox const *pBox, PVMSVGA3DCONTEXT pContext, int rc, int iBox)
2355{
2356 HRESULT hr = D3D_OK;
2357 const DWORD dwFlags = transfer == SVGA3D_READ_HOST_VRAM ? D3DLOCK_READONLY : 0;
2358
2359 AssertReturn(pSurface->enmD3DResType != VMSVGA3D_D3DRESTYPE_VOLUME_TEXTURE, VERR_NOT_IMPLEMENTED);
2360
2361 const bool fTexture = pSurface->enmD3DResType == VMSVGA3D_D3DRESTYPE_TEXTURE
2362 || pSurface->enmD3DResType == VMSVGA3D_D3DRESTYPE_CUBE_TEXTURE;
2363 if ( pSurface->enmD3DResType == VMSVGA3D_D3DRESTYPE_SURFACE
2364 || fTexture)
2365 {
2366 rc = vmsvga3dContextFromCid(pState, pSurface->idAssociatedContext, &pContext);
2367 AssertRCReturn(rc, rc);
2368 AssertReturn(pContext->pDevice, VERR_INTERNAL_ERROR);
2369
2370 /* Get the surface involved in the transfer. */
2371 IDirect3DSurface9 *pSurf;
2372 rc = vmsvga3dGetD3DSurface(pState, pContext, pSurface, uHostFace, uHostMipmap, true, &pSurf);
2373 AssertRCReturn(rc, rc);
2374
2375 if (transfer == SVGA3D_READ_HOST_VRAM)
2376 {
2377 /* Texture data is copied to the host VRAM.
2378 * Update the 'bounce' texture if necessary.
2379 */
2380 if ( fTexture
2381 && pSurface->bounce.pTexture
2382 && iBox == 0 /* only the first time */)
2383 {
2384 /** @todo inefficient for VRAM buffers!! */
2385 if (RT_BOOL(pSurface->surfaceFlags & SVGA3D_SURFACE_HINT_RENDERTARGET))
2386 {
2387 /* Copy the texture mipmap level to the bounce texture. */
2388 hr = D3D9GetRenderTargetData(pContext, pSurface, uHostFace, uHostMipmap);
2389 AssertMsgReturn(hr == D3D_OK, ("D3D9GetRenderTargetData failed with %x\n", hr), VERR_INTERNAL_ERROR);
2390 }
2391 }
2392 }
2393
2394 uint32_t const u32GuestBlockX = pBox->srcx / pSurface->cxBlock;
2395 uint32_t const u32GuestBlockY = pBox->srcy / pSurface->cyBlock;
2396 Assert(u32GuestBlockX * pSurface->cxBlock == pBox->srcx);
2397 Assert(u32GuestBlockY * pSurface->cyBlock == pBox->srcy);
2398 uint32_t const cBlocksX = (pBox->w + pSurface->cxBlock - 1) / pSurface->cxBlock;
2399 uint32_t const cBlocksY = (pBox->h + pSurface->cyBlock - 1) / pSurface->cyBlock;
2400 AssertMsgReturn(cBlocksX && cBlocksY, ("Empty box %dx%d\n", pBox->w, pBox->h), VERR_INTERNAL_ERROR);
2401
2402 /* vmsvgaR3GmrTransfer verifies uGuestOffset.
2403 * srcx(u32GuestBlockX) and srcy(u32GuestBlockY) have been verified in vmsvga3dSurfaceDMA
2404 * to not cause 32 bit overflow when multiplied by cbBlock and cbGuestPitch.
2405 */
2406 uint64_t const uGuestOffset = u32GuestBlockX * pSurface->cbBlock + u32GuestBlockY * cbGuestPitch;
2407 AssertReturn(uGuestOffset < UINT32_MAX, VERR_INVALID_PARAMETER);
2408
2409 RECT Rect;
2410 Rect.left = pBox->x;
2411 Rect.top = pBox->y;
2412 Rect.right = pBox->x + pBox->w; /* exclusive */
2413 Rect.bottom = pBox->y + pBox->h; /* exclusive */
2414
2415 D3DLOCKED_RECT LockedRect;
2416 hr = pSurf->LockRect(&LockedRect, &Rect, dwFlags);
2417 AssertMsgReturn(hr == D3D_OK, ("LockRect failed with %x\n", hr), VERR_INTERNAL_ERROR);
2418
2419 LogFunc(("Lock sid=%u %s(bounce=%d) memory for rectangle (%d,%d)(%d,%d)\n",
2420 pSurface->id, fTexture ? "TEXTURE " : "", RT_BOOL(pSurface->bounce.pTexture),
2421 Rect.left, Rect.top, Rect.right, Rect.bottom));
2422
2423 /* Prepare parameters for vmsvgaR3GmrTransfer, which needs the host buffer address, size
2424 * and offset of the first scanline.
2425 */
2426 uint32_t const cbLockedBuf = RT_ABS(LockedRect.Pitch) * cBlocksY;
2427 uint8_t *pu8LockedBuf = (uint8_t *)LockedRect.pBits;
2428 if (LockedRect.Pitch < 0)
2429 pu8LockedBuf -= cbLockedBuf + LockedRect.Pitch;
2430 uint32_t const offLockedBuf = (uint32_t)((uintptr_t)LockedRect.pBits - (uintptr_t)pu8LockedBuf);
2431
2432 rc = vmsvgaR3GmrTransfer(pThis,
2433 pThisCC,
2434 transfer,
2435 pu8LockedBuf,
2436 cbLockedBuf,
2437 offLockedBuf,
2438 LockedRect.Pitch,
2439 GuestPtr,
2440 (uint32_t)uGuestOffset,
2441 cbGuestPitch,
2442 cBlocksX * pSurface->cbBlock,
2443 cBlocksY);
2444 AssertRC(rc);
2445
2446 Log4(("first line:\n%.*Rhxd\n", cBlocksX * pSurface->cbBlock, LockedRect.pBits));
2447
2448 hr = pSurf->UnlockRect();
2449
2450 D3D_RELEASE(pSurf);
2451
2452 AssertMsgReturn(hr == D3D_OK, ("UnlockRect failed with %x\n", hr), VERR_INTERNAL_ERROR);
2453
2454 if (transfer == SVGA3D_WRITE_HOST_VRAM)
2455 {
2456 /* Data is copied to the texture. Copy updated 'bounce' texture to the actual if necessary.
2457 */
2458 /// @todo for the last iBox only.
2459 if ( fTexture
2460 && pSurface->bounce.pTexture)
2461 {
2462 LogFunc(("Sync texture from bounce buffer\n"));
2463
2464 /* Copy the new contents to the actual texture object. */
2465 hr = D3D9UpdateTexture(pContext, pSurface);
2466 AssertMsgReturn(hr == D3D_OK, ("UpdateTexture failed with %x\n", hr), VERR_INTERNAL_ERROR);
2467
2468 /* Track the copy operation. */
2469 vmsvga3dSurfaceTrackUsage(pState, pContext, pSurface);
2470 }
2471 }
2472 }
2473 else if ( pSurface->enmD3DResType == VMSVGA3D_D3DRESTYPE_VERTEX_BUFFER
2474 || pSurface->enmD3DResType == VMSVGA3D_D3DRESTYPE_INDEX_BUFFER)
2475 {
2476 /*
2477 * Mesa SVGA driver can use the same buffer either for vertex or index data.
2478 * But D3D distinguishes between index and vertex buffer objects.
2479 * Therefore it should be possible to switch the buffer type on the fly.
2480 *
2481 * Always save the data to the memory buffer in pSurface->paMipmapLevels and,
2482 * if necessary, recreate the corresponding D3D object with the data.
2483 */
2484
2485 /* Buffers are uncompressed. */
2486 AssertReturn(pSurface->cxBlock == 1 && pSurface->cyBlock == 1, VERR_INTERNAL_ERROR);
2487
2488 /* Caller already clipped pBox and buffers are 1-dimensional. */
2489 Assert(pBox->y == 0 && pBox->h == 1 && pBox->z == 0 && pBox->d == 1);
2490
2491 /* The caller has already updated pMipLevel->pSurfaceData, see VMSVGA3DSURFACE_NEEDS_DATA. */
2492
2493#ifdef LOG_ENABLED
2494 uint32_t const offHst = pBox->x * pSurface->cbBlock;
2495 uint32_t const cbWidth = pBox->w * pSurface->cbBlock;
2496 Log4(("Buffer updated at [0x%x;0x%x):\n%.*Rhxd\n", offHst, offHst + cbWidth, cbWidth, (uint8_t *)pMipLevel->pSurfaceData + offHst));
2497#endif
2498
2499 /* Do not bother to copy the data to the D3D resource now. vmsvga3dDrawPrimitives will do that.
2500 * The SVGA driver may use the same surface for both index and vertex data.
2501 */
2502
2503 /* Make sure that vmsvga3dDrawPrimitives fetches the new data. */
2504 pMipLevel->fDirty = true;
2505 pSurface->fDirty = true;
2506 }
2507 else
2508 {
2509 AssertMsgFailed(("Unsupported surface flags 0x%08X, type %d\n", pSurface->surfaceFlags, pSurface->enmD3DResType));
2510 }
2511
2512 return rc;
2513}
2514
2515int vmsvga3dGenerateMipmaps(PVGASTATECC pThisCC, uint32_t sid, SVGA3dTextureFilter filter)
2516{
2517 PVMSVGA3DSTATE pState = pThisCC->svga.p3dState;
2518 HRESULT hr;
2519
2520 PVMSVGA3DSURFACE pSurface;
2521 int rc = vmsvga3dSurfaceFromSid(pState, sid, &pSurface);
2522 AssertRCReturn(rc, rc);
2523 AssertReturn(pSurface->idAssociatedContext != SVGA3D_INVALID_ID, VERR_INTERNAL_ERROR);
2524
2525 Assert(filter != SVGA3D_TEX_FILTER_FLATCUBIC);
2526 Assert(filter != SVGA3D_TEX_FILTER_GAUSSIANCUBIC);
2527 pSurface->autogenFilter = filter;
2528
2529 Log(("vmsvga3dGenerateMipmaps: sid=%u filter=%d\n", sid, filter));
2530
2531 if (!pSurface->u.pSurface)
2532 {
2533 /** @todo stricter checks for associated context */
2534 uint32_t const cid = pSurface->idAssociatedContext;
2535
2536 PVMSVGA3DCONTEXT pContext;
2537 rc = vmsvga3dContextFromCid(pState, cid, &pContext);
2538 AssertRCReturn(rc, rc);
2539 AssertReturn(pContext->pDevice, VERR_INTERNAL_ERROR);
2540
2541 /* Unknown surface type; turn it into a texture. */
2542 LogFunc(("unknown src surface sid=%u type=%d format=%d -> create texture\n", sid, pSurface->surfaceFlags, pSurface->format));
2543 rc = vmsvga3dBackCreateTexture(pState, pContext, cid, pSurface);
2544 AssertRCReturn(rc, rc);
2545 }
2546
2547 AssertReturn( pSurface->enmD3DResType == VMSVGA3D_D3DRESTYPE_TEXTURE
2548 || pSurface->enmD3DResType == VMSVGA3D_D3DRESTYPE_CUBE_TEXTURE
2549 || pSurface->enmD3DResType == VMSVGA3D_D3DRESTYPE_VOLUME_TEXTURE,
2550 VERR_INVALID_PARAMETER);
2551
2552 hr = pSurface->u.pTexture->SetAutoGenFilterType((D3DTEXTUREFILTERTYPE)filter);
2553 AssertMsg(hr == D3D_OK, ("SetAutoGenFilterType failed with %x\n", hr));
2554
2555 /* Generate the mip maps. */
2556 pSurface->u.pTexture->GenerateMipSubLevels();
2557
2558 return VINF_SUCCESS;
2559}
2560
2561
2562/**
2563 * Create a new 3d context
2564 *
2565 * @returns VBox status code.
2566 * @param pThisCC The VGA/VMSVGA state for ring-3.
2567 * @param cid Context id
2568 */
2569int vmsvga3dContextDefine(PVGASTATECC pThisCC, uint32_t cid)
2570{
2571 int rc;
2572 PVMSVGA3DCONTEXT pContext;
2573 HRESULT hr;
2574 PVMSVGA3DSTATE pState = pThisCC->svga.p3dState;
2575
2576 Log(("vmsvga3dContextDefine id %x\n", cid));
2577
2578 AssertReturn(pState, VERR_NO_MEMORY);
2579 AssertReturn(cid < SVGA3D_MAX_CONTEXT_IDS, VERR_INVALID_PARAMETER);
2580
2581 if (cid >= pState->cContexts)
2582 {
2583 /* Grow the array. */
2584 uint32_t cNew = RT_ALIGN(cid + 15, 16);
2585 void *pvNew = RTMemRealloc(pState->papContexts, sizeof(pState->papContexts[0]) * cNew);
2586 AssertReturn(pvNew, VERR_NO_MEMORY);
2587 pState->papContexts = (PVMSVGA3DCONTEXT *)pvNew;
2588 while (pState->cContexts < cNew)
2589 {
2590 pContext = (PVMSVGA3DCONTEXT)RTMemAllocZ(sizeof(*pContext));
2591 AssertReturn(pContext, VERR_NO_MEMORY);
2592 pContext->id = SVGA3D_INVALID_ID;
2593 pState->papContexts[pState->cContexts++] = pContext;
2594 }
2595 }
2596 /* If one already exists with this id, then destroy it now. */
2597 if (pState->papContexts[cid]->id != SVGA3D_INVALID_ID)
2598 vmsvga3dContextDestroy(pThisCC, cid);
2599
2600 pContext = pState->papContexts[cid];
2601 memset(pContext, 0, sizeof(*pContext));
2602 pContext->id = cid;
2603 for (uint32_t i = 0; i< RT_ELEMENTS(pContext->aSidActiveTextures); i++)
2604 pContext->aSidActiveTextures[i] = SVGA3D_INVALID_ID;
2605 pContext->state.shidVertex = SVGA3D_INVALID_ID;
2606 pContext->state.shidPixel = SVGA3D_INVALID_ID;
2607
2608 for (uint32_t i = 0; i < RT_ELEMENTS(pContext->state.aRenderTargets); i++)
2609 pContext->state.aRenderTargets[i] = SVGA3D_INVALID_ID;
2610
2611 /* Create a context window with minimal 4x4 size. We will never use the swapchain
2612 * to present the rendered image. Rendered images from the guest will be copied to
2613 * the VMSVGA SCREEN object, which can be either an offscreen render target or
2614 * system memory in the guest VRAM.
2615 */
2616 rc = vmsvga3dContextWindowCreate(pState->hInstance, pState->pWindowThread, pState->WndRequestSem, &pContext->hwnd);
2617 AssertRCReturn(rc, rc);
2618
2619 /* Changed when the function returns. */
2620 D3DPRESENT_PARAMETERS PresParam;
2621 PresParam.BackBufferWidth = 0;
2622 PresParam.BackBufferHeight = 0;
2623 PresParam.BackBufferFormat = D3DFMT_UNKNOWN;
2624 PresParam.BackBufferCount = 0;
2625
2626 PresParam.MultiSampleType = D3DMULTISAMPLE_NONE;
2627 PresParam.MultiSampleQuality = 0;
2628 PresParam.SwapEffect = D3DSWAPEFFECT_DISCARD;
2629 PresParam.hDeviceWindow = pContext->hwnd;
2630 PresParam.Windowed = TRUE;
2631 PresParam.EnableAutoDepthStencil = FALSE;
2632 PresParam.AutoDepthStencilFormat = D3DFMT_UNKNOWN; /* not relevant */
2633 PresParam.Flags = 0;
2634 PresParam.FullScreen_RefreshRateInHz = 0; /* windowed -> 0 */
2635 /** @todo consider using D3DPRESENT_DONOTWAIT so we don't wait for the GPU during Present calls. */
2636 PresParam.PresentationInterval = D3DPRESENT_INTERVAL_IMMEDIATE;
2637
2638#if 0
2639 VMSVGA3DCREATEDEVICEPARAMS Params = { pState, pContext, &PresParam, 0 };
2640 rc = vmsvga3dSendThreadMessage(pState->pWindowThread, pState->WndRequestSem, WM_VMSVGA3D_CREATE_DEVICE, 0, (LPARAM)&Params);
2641 AssertRCReturn(rc, rc);
2642 hr = Params.hrc;
2643
2644#elif defined(VBOX_VMSVGA3D_WITH_WINE_OPENGL)
2645 hr = pState->pD3D9->CreateDevice(D3DADAPTER_DEFAULT,
2646 D3DDEVTYPE_HAL,
2647 pContext->hwnd,
2648 D3DCREATE_MULTITHREADED | D3DCREATE_MIXED_VERTEXPROCESSING, //D3DCREATE_HARDWARE_VERTEXPROCESSING,
2649 &PresParam,
2650 &pContext->pDevice);
2651#else
2652 /** @todo Docs indicates that we should be using
2653 * D3DCREATE_HARDWARE_VERTEXPROCESSING with W10 1607 and higher.
2654 * https://docs.microsoft.com/en-us/windows/win32/direct3d9/d3dcreate */
2655 hr = pState->pD3D9->CreateDeviceEx(D3DADAPTER_DEFAULT,
2656 D3DDEVTYPE_HAL,
2657 pContext->hwnd,
2658 D3DCREATE_MULTITHREADED | D3DCREATE_MIXED_VERTEXPROCESSING, //D3DCREATE_HARDWARE_VERTEXPROCESSING,
2659 &PresParam,
2660 NULL,
2661 &pContext->pDevice);
2662#endif
2663 AssertMsgReturn(hr == D3D_OK, ("vmsvga3dContextDefine: CreateDevice failed with %x\n", hr), VERR_INTERNAL_ERROR);
2664
2665 Log(("vmsvga3dContextDefine: Backbuffer (%d,%d) count=%d format=%x\n", PresParam.BackBufferWidth, PresParam.BackBufferHeight, PresParam.BackBufferCount, PresParam.BackBufferFormat));
2666 return VINF_SUCCESS;
2667}
2668
2669/**
2670 * Destroy an existing 3d context
2671 *
2672 * @returns VBox status code.
2673 * @param pThisCC The VGA/VMSVGA state for ring-3.
2674 * @param cid Context id
2675 */
2676int vmsvga3dContextDestroy(PVGASTATECC pThisCC, uint32_t cid)
2677{
2678 PVMSVGA3DSTATE pState = pThisCC->svga.p3dState;
2679 AssertReturn(pState, VERR_NO_MEMORY);
2680
2681 AssertReturn(cid < SVGA3D_MAX_CONTEXT_IDS, VERR_INVALID_PARAMETER);
2682
2683 if ( cid < pState->cContexts
2684 && pState->papContexts[cid]->id == cid)
2685 {
2686 PVMSVGA3DCONTEXT pContext = pState->papContexts[cid];
2687
2688 Log(("vmsvga3dContextDestroy id %x\n", cid));
2689
2690 /* Cleanup the device runtime state. */
2691 if (pContext->pDevice)
2692 pContext->pDevice->SetVertexDeclaration(NULL);
2693 D3D_RELEASE(pContext->d3dState.pVertexDecl);
2694
2695 /* Check for all surfaces that are associated with this context to remove all dependencies */
2696 for (uint32_t sid = 0; sid < pState->cSurfaces; sid++)
2697 {
2698 PVMSVGA3DSURFACE pSurface = pState->papSurfaces[sid];
2699 if ( pSurface->id == sid
2700 && pSurface->idAssociatedContext == cid)
2701 {
2702 int rc;
2703
2704 LogFunc(("Remove all dependencies for surface sid=%u\n", sid));
2705
2706 uint32_t const surfaceFlags = pSurface->surfaceFlags;
2707 SVGA3dSurfaceFormat const format = pSurface->format;
2708 uint32_t const multisampleCount = pSurface->multiSampleCount;
2709 SVGA3dTextureFilter const autogenFilter = pSurface->autogenFilter;
2710 uint32_t const cFaces = pSurface->cFaces;
2711 uint32_t const cMipLevels = pSurface->faces[0].numMipLevels;
2712
2713 SVGA3dSize *pMipLevelSize = (SVGA3dSize *)RTMemAllocZ(cMipLevels * cFaces * sizeof(SVGA3dSize));
2714 AssertReturn(pMipLevelSize, VERR_NO_MEMORY);
2715
2716 for (uint32_t face = 0; face < pSurface->cFaces; face++)
2717 {
2718 for (uint32_t i = 0; i < cMipLevels; i++)
2719 {
2720 uint32_t idx = i + face * cMipLevels;
2721 memcpy(&pMipLevelSize[idx], &pSurface->paMipmapLevels[idx].mipmapSize, sizeof(SVGA3dSize));
2722 }
2723 }
2724
2725 SVGA3dSurfaceFace aFaces[SVGA3D_MAX_SURFACE_FACES];
2726 AssertCompile(sizeof(aFaces) == sizeof(pSurface->faces));
2727 memcpy(aFaces, pSurface->faces, sizeof(pSurface->faces));
2728
2729 /* Recreate the surface with the original settings; destroys the contents, but that seems fairly safe since the context is also destroyed. */
2730#ifdef DEBUG_sunlover
2731 /** @todo not safe with shared objects */
2732 Assert(pSurface->pSharedObjectTree == NULL);
2733#endif
2734
2735 rc = vmsvga3dSurfaceDestroy(pThisCC, sid);
2736 AssertRC(rc);
2737
2738 rc = vmsvga3dSurfaceDefine(pThisCC, sid, surfaceFlags, format, aFaces, multisampleCount, autogenFilter,
2739 cMipLevels * cFaces, pMipLevelSize);
2740 AssertRC(rc);
2741
2742 Assert(!pSurface->u.pSurface);
2743 }
2744 else
2745 {
2746 /* Check for a shared surface object. */
2747 PVMSVGA3DSHAREDSURFACE pSharedSurface = (PVMSVGA3DSHAREDSURFACE)RTAvlU32Get(&pSurface->pSharedObjectTree, cid);
2748 if (pSharedSurface)
2749 {
2750 LogFunc(("Remove shared dependency for surface sid=%u\n", sid));
2751
2752 switch (pSurface->enmD3DResType)
2753 {
2754 case VMSVGA3D_D3DRESTYPE_TEXTURE:
2755 Assert(pSharedSurface->u.pTexture);
2756 D3D_RELEASE(pSharedSurface->u.pTexture);
2757 break;
2758
2759 case VMSVGA3D_D3DRESTYPE_CUBE_TEXTURE:
2760 Assert(pSharedSurface->u.pCubeTexture);
2761 D3D_RELEASE(pSharedSurface->u.pCubeTexture);
2762 break;
2763
2764 case VMSVGA3D_D3DRESTYPE_VOLUME_TEXTURE:
2765 Assert(pSharedSurface->u.pVolumeTexture);
2766 D3D_RELEASE(pSharedSurface->u.pVolumeTexture);
2767 break;
2768
2769 default:
2770 AssertFailed();
2771 break;
2772 }
2773 RTAvlU32Remove(&pSurface->pSharedObjectTree, cid);
2774 RTMemFree(pSharedSurface);
2775 }
2776 }
2777 }
2778
2779 /* Destroy all leftover pixel shaders. */
2780 for (uint32_t i = 0; i < pContext->cPixelShaders; i++)
2781 {
2782 if (pContext->paPixelShader[i].id != SVGA3D_INVALID_ID)
2783 vmsvga3dShaderDestroy(pThisCC, pContext->paPixelShader[i].cid, pContext->paPixelShader[i].id, pContext->paPixelShader[i].type);
2784 }
2785 if (pContext->paPixelShader)
2786 RTMemFree(pContext->paPixelShader);
2787
2788 /* Destroy all leftover vertex shaders. */
2789 for (uint32_t i = 0; i < pContext->cVertexShaders; i++)
2790 {
2791 if (pContext->paVertexShader[i].id != SVGA3D_INVALID_ID)
2792 vmsvga3dShaderDestroy(pThisCC, pContext->paVertexShader[i].cid, pContext->paVertexShader[i].id, pContext->paVertexShader[i].type);
2793 }
2794 if (pContext->paVertexShader)
2795 RTMemFree(pContext->paVertexShader);
2796
2797 if (pContext->state.paVertexShaderConst)
2798 RTMemFree(pContext->state.paVertexShaderConst);
2799 if (pContext->state.paPixelShaderConst)
2800 RTMemFree(pContext->state.paPixelShaderConst);
2801
2802 vmsvga3dOcclusionQueryDelete(pState, pContext);
2803
2804 /* Release the D3D device object */
2805 D3D_RELEASE(pContext->pDevice);
2806
2807 /* Destroy the window we've created. */
2808 int rc = vmsvga3dSendThreadMessage(pState->pWindowThread, pState->WndRequestSem, WM_VMSVGA3D_DESTROYWINDOW, (WPARAM)pContext->hwnd, 0);
2809 AssertRC(rc);
2810
2811 memset(pContext, 0, sizeof(*pContext));
2812 pContext->id = SVGA3D_INVALID_ID;
2813 }
2814 else
2815 AssertFailed();
2816
2817 return VINF_SUCCESS;
2818}
2819
2820int vmsvga3dBackDefineScreen(PVGASTATE pThis, PVGASTATECC pThisCC, VMSVGASCREENOBJECT *pScreen)
2821{
2822 RT_NOREF(pThis, pThisCC, pScreen);
2823 return VERR_NOT_IMPLEMENTED;
2824}
2825
2826int vmsvga3dBackDestroyScreen(PVGASTATECC pThisCC, VMSVGASCREENOBJECT *pScreen)
2827{
2828 RT_NOREF(pThisCC, pScreen);
2829 return VERR_NOT_IMPLEMENTED;
2830}
2831
2832int vmsvga3dBackSurfaceBlitToScreen(PVGASTATECC pThisCC, VMSVGASCREENOBJECT *pScreen,
2833 SVGASignedRect destRect, SVGA3dSurfaceImageId srcImage,
2834 SVGASignedRect srcRect, uint32_t cRects, SVGASignedRect *paRects)
2835{
2836 RT_NOREF(pThisCC, pScreen, destRect, srcImage, srcRect, cRects, paRects);
2837 return VERR_NOT_IMPLEMENTED;
2838}
2839
2840static int vmsvga3dContextTrackUsage(PVGASTATECC pThisCC, PVMSVGA3DCONTEXT pContext)
2841{
2842#ifndef VBOX_VMSVGA3D_WITH_WINE_OPENGL
2843 PVMSVGA3DSTATE pState = pThisCC->svga.p3dState;
2844 AssertReturn(pState, VERR_NO_MEMORY);
2845
2846 /* Inject fences to make sure we can track surface usage in case the client wants to reuse it in another context. */
2847 for (uint32_t i = 0; i < RT_ELEMENTS(pContext->aSidActiveTextures); ++i)
2848 {
2849 if (pContext->aSidActiveTextures[i] != SVGA3D_INVALID_ID)
2850 vmsvga3dSurfaceTrackUsageById(pState, pContext, pContext->aSidActiveTextures[i]);
2851 }
2852 for (uint32_t i = 0; i < RT_ELEMENTS(pContext->state.aRenderTargets); ++i)
2853 if (pContext->state.aRenderTargets[i] != SVGA3D_INVALID_ID)
2854 vmsvga3dSurfaceTrackUsageById(pState, pContext, pContext->state.aRenderTargets[i]);
2855#endif
2856 return VINF_SUCCESS;
2857}
2858
2859/* Handle resize */
2860int vmsvga3dChangeMode(PVGASTATECC pThisCC)
2861{
2862 PVMSVGA3DSTATE pState = pThisCC->svga.p3dState;
2863 AssertReturn(pState, VERR_NO_MEMORY);
2864
2865 /* Resize all active contexts. */
2866 for (uint32_t i = 0; i < pState->cContexts; i++)
2867 {
2868 PVMSVGA3DCONTEXT pContext = pState->papContexts[i];
2869 uint32_t cid = pContext->id;
2870
2871 if (cid != SVGA3D_INVALID_ID)
2872 {
2873 D3DPRESENT_PARAMETERS PresParam;
2874 D3DVIEWPORT9 viewportOrg;
2875 HRESULT hr;
2876
2877#ifdef VMSVGA3D_DIRECT3D9_RESET
2878 /* Sync back all surface data as everything is lost after the Reset. */
2879 for (uint32_t sid = 0; sid < pState->cSurfaces; sid++)
2880 {
2881 PVMSVGA3DSURFACE pSurface = pState->papSurfaces[sid];
2882 if ( pSurface->id == sid
2883 && pSurface->idAssociatedContext == cid
2884 && pSurface->u.pSurface)
2885 {
2886 Log(("vmsvga3dChangeMode: sync back data of surface sid=%u (fDirty=%d)\n", sid, pSurface->fDirty));
2887
2888 /* Reallocate our surface memory buffers. */
2889 for (uint32_t i = 0; i < pSurface->cMipLevels; i++)
2890 {
2891 PVMSVGA3DMIPMAPLEVEL pMipmapLevel = &pSurface->paMipmapLevels[i];
2892
2893 pMipmapLevel->pSurfaceData = RTMemAllocZ(pMipmapLevel->cbSurface);
2894 AssertReturn(pMipmapLevel->pSurfaceData, VERR_NO_MEMORY);
2895
2896 if (!pSurface->fDirty)
2897 {
2898 D3DLOCKED_RECT LockedRect;
2899
2900 if (pSurface->bounce.pTexture)
2901 {
2902 IDirect3DSurface9 *pSrc, *pDest;
2903
2904 /** @todo only sync when something was actually rendered (since the last sync) */
2905 Log(("vmsvga3dChangeMode: sync bounce buffer (level %d)\n", i));
2906 hr = pSurface->bounce.pTexture->GetSurfaceLevel(i, &pDest);
2907 AssertMsgReturn(hr == D3D_OK, ("vmsvga3dChangeMode: GetSurfaceLevel failed with %x\n", hr), VERR_INTERNAL_ERROR);
2908
2909 hr = pSurface->u.pTexture->GetSurfaceLevel(i, &pSrc);
2910 AssertMsgReturn(hr == D3D_OK, ("vmsvga3dChangeMode: GetSurfaceLevel failed with %x\n", hr), VERR_INTERNAL_ERROR);
2911
2912 hr = pContext->pDevice->GetRenderTargetData(pSrc, pDest);
2913 AssertMsgReturn(hr == D3D_OK, ("vmsvga3dChangeMode: GetRenderTargetData failed with %x\n", hr), VERR_INTERNAL_ERROR);
2914
2915 D3D_RELEASE(pSrc);
2916 D3D_RELEASE(pDest);
2917
2918 hr = pSurface->bounce.pTexture->LockRect(i,
2919 &LockedRect,
2920 NULL,
2921 D3DLOCK_READONLY);
2922 }
2923 else
2924 hr = pSurface->u.pTexture->LockRect(i,
2925 &LockedRect,
2926 NULL,
2927 D3DLOCK_READONLY);
2928 AssertMsgReturn(hr == D3D_OK, ("vmsvga3dChangeMode: LockRect failed with %x\n", hr), VERR_INTERNAL_ERROR);
2929
2930 /* Copy the data one line at a time in case the internal pitch is different. */
2931 for (uint32_t j = 0; j < pMipmapLevel->size.height; j++)
2932 {
2933 memcpy((uint8_t *)pMipmapLevel->pSurfaceData + j * pMipmapLevel->cbSurfacePitch, (uint8_t *)LockedRect.pBits + j * LockedRect.Pitch, pMipmapLevel->cbSurfacePitch);
2934 }
2935
2936 if (pSurface->bounce.pTexture)
2937 hr = pSurface->bounce.pTexture->UnlockRect(i);
2938 else
2939 hr = pSurface->u.pTexture->UnlockRect(i);
2940 AssertMsgReturn(hr == D3D_OK, ("vmsvga3dChangeMode: UnlockRect failed with %x\n", hr), VERR_INTERNAL_ERROR);
2941 }
2942 }
2943
2944
2945 switch (pSurface->flags & VMSVGA3D_SURFACE_HINT_SWITCH_MASK)
2946 {
2947 case SVGA3D_SURFACE_CUBEMAP:
2948 case SVGA3D_SURFACE_CUBEMAP | SVGA3D_SURFACE_HINT_TEXTURE:
2949 case SVGA3D_SURFACE_CUBEMAP | SVGA3D_SURFACE_HINT_TEXTURE | SVGA3D_SURFACE_HINT_RENDERTARGET:
2950 D3D_RELEASE(pSurface->u.pCubeTexture);
2951 D3D_RELEASE(pSurface->bounce.pCubeTexture);
2952 break;
2953
2954 case SVGA3D_SURFACE_HINT_INDEXBUFFER | SVGA3D_SURFACE_HINT_VERTEXBUFFER:
2955 case SVGA3D_SURFACE_HINT_INDEXBUFFER:
2956 case SVGA3D_SURFACE_HINT_VERTEXBUFFER:
2957 if (pSurface->fu32ActualUsageFlags == SVGA3D_SURFACE_HINT_VERTEXBUFFER)
2958 D3D_RELEASE(pSurface->u.pVertexBuffer);
2959 else if (pSurface->fu32ActualUsageFlags == SVGA3D_SURFACE_HINT_INDEXBUFFER)
2960 D3D_RELEASE(pSurface->u.pIndexBuffer);
2961 else
2962 AssertMsg(pSurface->u.pVertexBuffer == NULL, ("fu32ActualUsageFlags %x\n", pSurface->fu32ActualUsageFlags));
2963 break;
2964
2965 case SVGA3D_SURFACE_HINT_TEXTURE:
2966 case SVGA3D_SURFACE_HINT_TEXTURE | SVGA3D_SURFACE_HINT_RENDERTARGET:
2967 D3D_RELEASE(pSurface->u.pTexture);
2968 D3D_RELEASE(pSurface->bounce.pTexture);
2969 break;
2970
2971 case SVGA3D_SURFACE_HINT_RENDERTARGET:
2972 case SVGA3D_SURFACE_HINT_DEPTHSTENCIL:
2973 if (pSurface->fStencilAsTexture)
2974 D3D_RELEASE(pSurface->u.pTexture);
2975 else
2976 D3D_RELEASE(pSurface->u.pSurface);
2977 break;
2978
2979 default:
2980 AssertFailed();
2981 break;
2982 }
2983 RTAvlU32Destroy(&pSurface->pSharedObjectTree, vmsvga3dSharedSurfaceDestroyTree, pSurface);
2984 Assert(pSurface->pSharedObjectTree == NULL);
2985
2986 pSurface->idAssociatedContext = SVGA3D_INVALID_ID;
2987 pSurface->hSharedObject = 0;
2988 }
2989 }
2990#endif /* #ifdef VMSVGA3D_DIRECT3D9_RESET */
2991
2992 AssertReturn(pContext->pDevice, VERR_INTERNAL_ERROR);
2993
2994 /* Cleanup the device runtime state. */
2995 pContext->pDevice->SetVertexDeclaration(NULL);
2996 D3D_RELEASE(pContext->d3dState.pVertexDecl);
2997
2998 hr = pContext->pDevice->GetViewport(&viewportOrg);
2999 AssertMsgReturn(hr == D3D_OK, ("vmsvga3dChangeMode: GetViewport failed with %x\n", hr), VERR_INTERNAL_ERROR);
3000
3001 Log(("vmsvga3dChangeMode: old viewport settings (%d,%d)(%d,%d) z=%d/%d\n", viewportOrg.X, viewportOrg.Y, viewportOrg.Width, viewportOrg.Height, (uint32_t)(viewportOrg.MinZ * 100.0), (uint32_t)(viewportOrg.MaxZ * 100.0)));
3002
3003 /* Changed when the function returns. */
3004 PresParam.BackBufferWidth = 0;
3005 PresParam.BackBufferHeight = 0;
3006 PresParam.BackBufferFormat = D3DFMT_UNKNOWN;
3007 PresParam.BackBufferCount = 0;
3008
3009 PresParam.MultiSampleType = D3DMULTISAMPLE_NONE;
3010 PresParam.MultiSampleQuality = 0;
3011 PresParam.SwapEffect = D3DSWAPEFFECT_DISCARD;
3012 PresParam.hDeviceWindow = pContext->hwnd;
3013 PresParam.Windowed = TRUE;
3014 PresParam.EnableAutoDepthStencil = FALSE;
3015 PresParam.AutoDepthStencilFormat = D3DFMT_UNKNOWN; /* not relevant */
3016 PresParam.Flags = 0;
3017 PresParam.FullScreen_RefreshRateInHz = 0; /* windowed -> 0 */
3018 /** @todo consider using D3DPRESENT_DONOTWAIT so we don't wait for the GPU during Present calls. */
3019 PresParam.PresentationInterval = D3DPRESENT_INTERVAL_IMMEDIATE;;
3020
3021#ifdef VBOX_VMSVGA3D_WITH_WINE_OPENGL
3022 hr = pContext->pDevice->Reset(&PresParam);
3023 AssertMsgReturn(hr == D3D_OK, ("vmsvga3dChangeMode: Reset failed with %x\n", hr), VERR_INTERNAL_ERROR);
3024#else
3025 /* ResetEx does not trash the device state */
3026 hr = pContext->pDevice->ResetEx(&PresParam, NULL);
3027 AssertMsgReturn(hr == D3D_OK, ("vmsvga3dChangeMode: Reset failed with %x\n", hr), VERR_INTERNAL_ERROR);
3028#endif
3029 Log(("vmsvga3dChangeMode: Backbuffer (%d,%d) count=%d format=%x\n", PresParam.BackBufferWidth, PresParam.BackBufferHeight, PresParam.BackBufferCount, PresParam.BackBufferFormat));
3030
3031 /* ResetEx changes the viewport; restore it again. */
3032 hr = pContext->pDevice->SetViewport(&viewportOrg);
3033 AssertMsgReturn(hr == D3D_OK, ("vmsvga3dChangeMode: SetViewport failed with %x\n", hr), VERR_INTERNAL_ERROR);
3034
3035#ifdef LOG_ENABLED
3036 {
3037 D3DVIEWPORT9 viewport;
3038 hr = pContext->pDevice->GetViewport(&viewport);
3039 AssertMsgReturn(hr == D3D_OK, ("vmsvga3dChangeMode: GetViewport failed with %x\n", hr), VERR_INTERNAL_ERROR);
3040
3041 Log(("vmsvga3dChangeMode: changed viewport settings (%d,%d)(%d,%d) z=%d/%d\n", viewport.X, viewport.Y, viewport.Width, viewport.Height, (uint32_t)(viewport.MinZ * 100.0), (uint32_t)(viewport.MaxZ * 100.0)));
3042 }
3043#endif
3044
3045 /* First set the render targets as they change the internal state (reset viewport etc) */
3046 Log(("vmsvga3dChangeMode: Recreate render targets BEGIN\n"));
3047 for (uint32_t j = 0; j < RT_ELEMENTS(pContext->state.aRenderTargets); j++)
3048 {
3049 if (pContext->state.aRenderTargets[j] != SVGA3D_INVALID_ID)
3050 {
3051 SVGA3dSurfaceImageId target;
3052
3053 target.sid = pContext->state.aRenderTargets[j];
3054 target.face = 0;
3055 target.mipmap = 0;
3056 int rc = vmsvga3dSetRenderTarget(pThisCC, cid, (SVGA3dRenderTargetType)j, target);
3057 AssertRCReturn(rc, rc);
3058 }
3059 }
3060
3061#ifdef VMSVGA3D_DIRECT3D9_RESET
3062 /* Recreate the render state */
3063 Log(("vmsvga3dChangeMode: Recreate render state BEGIN\n"));
3064 for (uint32_t i = 0; i < RT_ELEMENTS(pContext->state.aRenderState); i++)
3065 {
3066 SVGA3dRenderState *pRenderState = &pContext->state.aRenderState[i];
3067
3068 if (pRenderState->state != SVGA3D_RS_INVALID)
3069 vmsvga3dSetRenderState(pThisCC, pContext->id, 1, pRenderState);
3070 }
3071 Log(("vmsvga3dChangeMode: Recreate render state END\n"));
3072
3073 /* Recreate the texture state */
3074 Log(("vmsvga3dChangeMode: Recreate texture state BEGIN\n"));
3075 for (uint32_t iStage = 0; iStage < RT_ELEMENTS(pContext->state.aTextureStates); iStage++)
3076 {
3077 for (uint32_t j = 0; j < RT_ELEMENTS(pContext->state.aTextureStates[0]); j++)
3078 {
3079 SVGA3dTextureState *pTextureState = &pContext->state.aTextureStates[iStage][j];
3080
3081 if (pTextureState->name != SVGA3D_RS_INVALID)
3082 vmsvga3dSetTextureState(pThisCC, pContext->id, 1, pTextureState);
3083 }
3084 }
3085 Log(("vmsvga3dChangeMode: Recreate texture state END\n"));
3086
3087 if (pContext->state.u32UpdateFlags & VMSVGA3D_UPDATE_SCISSORRECT)
3088 vmsvga3dSetScissorRect(pThis, cid, &pContext->state.RectScissor);
3089 if (pContext->state.u32UpdateFlags & VMSVGA3D_UPDATE_ZRANGE)
3090 vmsvga3dSetZRange(pThisCC, cid, pContext->state.zRange);
3091 if (pContext->state.u32UpdateFlags & VMSVGA3D_UPDATE_VIEWPORT)
3092 vmsvga3dSetViewPort(pThis, cid, &pContext->state.RectViewPort);
3093 if (pContext->state.u32UpdateFlags & VMSVGA3D_UPDATE_VERTEXSHADER)
3094 vmsvga3dShaderSet(pThis, pContext, cid, SVGA3D_SHADERTYPE_VS, pContext->state.shidVertex);
3095 if (pContext->state.u32UpdateFlags & VMSVGA3D_UPDATE_PIXELSHADER)
3096 vmsvga3dShaderSet(pThis, pContext, cid, SVGA3D_SHADERTYPE_PS, pContext->state.shidPixel);
3097 /** @todo restore more state data */
3098#endif /* #ifdef VMSVGA3D_DIRECT3D9_RESET */
3099 }
3100 }
3101 return VINF_SUCCESS;
3102}
3103
3104
3105int vmsvga3dSetTransform(PVGASTATECC pThisCC, uint32_t cid, SVGA3dTransformType type, float matrix[16])
3106{
3107 D3DTRANSFORMSTATETYPE d3dState;
3108 HRESULT hr;
3109 PVMSVGA3DSTATE pState = pThisCC->svga.p3dState;
3110 AssertReturn(pState, VERR_NO_MEMORY);
3111
3112 Log(("vmsvga3dSetTransform %x %s\n", cid, vmsvgaTransformToString(type)));
3113
3114 ASSERT_GUEST_RETURN((unsigned)type < SVGA3D_TRANSFORM_MAX, VERR_INVALID_PARAMETER);
3115
3116 PVMSVGA3DCONTEXT pContext;
3117 int rc = vmsvga3dContextFromCid(pState, cid, &pContext);
3118 AssertRCReturn(rc, rc);
3119 AssertReturn(pContext->pDevice, VERR_INTERNAL_ERROR);
3120
3121 switch (type)
3122 {
3123 case SVGA3D_TRANSFORM_VIEW:
3124 d3dState = D3DTS_VIEW;
3125 break;
3126 case SVGA3D_TRANSFORM_PROJECTION:
3127 d3dState = D3DTS_PROJECTION;
3128 break;
3129 case SVGA3D_TRANSFORM_TEXTURE0:
3130 d3dState = D3DTS_TEXTURE0;
3131 break;
3132 case SVGA3D_TRANSFORM_TEXTURE1:
3133 d3dState = D3DTS_TEXTURE1;
3134 break;
3135 case SVGA3D_TRANSFORM_TEXTURE2:
3136 d3dState = D3DTS_TEXTURE2;
3137 break;
3138 case SVGA3D_TRANSFORM_TEXTURE3:
3139 d3dState = D3DTS_TEXTURE3;
3140 break;
3141 case SVGA3D_TRANSFORM_TEXTURE4:
3142 d3dState = D3DTS_TEXTURE4;
3143 break;
3144 case SVGA3D_TRANSFORM_TEXTURE5:
3145 d3dState = D3DTS_TEXTURE5;
3146 break;
3147 case SVGA3D_TRANSFORM_TEXTURE6:
3148 d3dState = D3DTS_TEXTURE6;
3149 break;
3150 case SVGA3D_TRANSFORM_TEXTURE7:
3151 d3dState = D3DTS_TEXTURE7;
3152 break;
3153 case SVGA3D_TRANSFORM_WORLD:
3154 d3dState = D3DTS_WORLD;
3155 break;
3156 case SVGA3D_TRANSFORM_WORLD1:
3157 d3dState = D3DTS_WORLD1;
3158 break;
3159 case SVGA3D_TRANSFORM_WORLD2:
3160 d3dState = D3DTS_WORLD2;
3161 break;
3162 case SVGA3D_TRANSFORM_WORLD3:
3163 d3dState = D3DTS_WORLD3;
3164 break;
3165
3166 default:
3167 Log(("vmsvga3dSetTransform: unknown type!!\n"));
3168 return VERR_INVALID_PARAMETER;
3169 }
3170
3171 /* Save this matrix for vm state save/restore. */
3172 pContext->state.aTransformState[type].fValid = true;
3173 memcpy(pContext->state.aTransformState[type].matrix, matrix, sizeof(pContext->state.aTransformState[type].matrix));
3174 pContext->state.u32UpdateFlags |= VMSVGA3D_UPDATE_TRANSFORM;
3175
3176 Log(("Matrix [%d %d %d %d]\n", (int)(matrix[0] * 10.0), (int)(matrix[1] * 10.0), (int)(matrix[2] * 10.0), (int)(matrix[3] * 10.0)));
3177 Log((" [%d %d %d %d]\n", (int)(matrix[4] * 10.0), (int)(matrix[5] * 10.0), (int)(matrix[6] * 10.0), (int)(matrix[7] * 10.0)));
3178 Log((" [%d %d %d %d]\n", (int)(matrix[8] * 10.0), (int)(matrix[9] * 10.0), (int)(matrix[10] * 10.0), (int)(matrix[11] * 10.0)));
3179 Log((" [%d %d %d %d]\n", (int)(matrix[12] * 10.0), (int)(matrix[13] * 10.0), (int)(matrix[14] * 10.0), (int)(matrix[15] * 10.0)));
3180 hr = pContext->pDevice->SetTransform(d3dState, (const D3DMATRIX *)matrix);
3181 AssertMsgReturn(hr == D3D_OK, ("vmsvga3dSetTransform: SetTransform failed with %x\n", hr), VERR_INTERNAL_ERROR);
3182 return VINF_SUCCESS;
3183}
3184
3185int vmsvga3dSetZRange(PVGASTATECC pThisCC, uint32_t cid, SVGA3dZRange zRange)
3186{
3187 D3DVIEWPORT9 viewport;
3188 HRESULT hr;
3189 PVMSVGA3DSTATE pState = pThisCC->svga.p3dState;
3190 AssertReturn(pState, VERR_NO_MEMORY);
3191
3192 Log(("vmsvga3dSetZRange %x min=%d max=%d\n", cid, (uint32_t)(zRange.min * 100.0), (uint32_t)(zRange.max * 100.0)));
3193
3194 PVMSVGA3DCONTEXT pContext;
3195 int rc = vmsvga3dContextFromCid(pState, cid, &pContext);
3196 AssertRCReturn(rc, rc);
3197 AssertReturn(pContext->pDevice, VERR_INTERNAL_ERROR);
3198
3199 pContext->state.zRange = zRange;
3200 pContext->state.u32UpdateFlags |= VMSVGA3D_UPDATE_ZRANGE;
3201
3202 hr = pContext->pDevice->GetViewport(&viewport);
3203 AssertMsgReturn(hr == D3D_OK, ("vmsvga3dSetZRange: GetViewport failed with %x\n", hr), VERR_INTERNAL_ERROR);
3204
3205 Log(("vmsvga3dSetZRange: old viewport settings (%d,%d)(%d,%d) z=%d/%d\n", viewport.X, viewport.Y, viewport.Width, viewport.Height, (uint32_t)(viewport.MinZ * 100.0), (uint32_t)(viewport.MaxZ * 100.0)));
3206 /** @todo convert the depth range from -1-1 to 0-1 although we shouldn't be getting such values in the first place... */
3207 if (zRange.min < 0.0)
3208 zRange.min = 0.0;
3209 if (zRange.max > 1.0)
3210 zRange.max = 1.0;
3211
3212 viewport.MinZ = zRange.min;
3213 viewport.MaxZ = zRange.max;
3214 hr = pContext->pDevice->SetViewport(&viewport);
3215 AssertMsgReturn(hr == D3D_OK, ("vmsvga3dSetZRange: SetViewport failed with %x\n", hr), VERR_INTERNAL_ERROR);
3216 return VINF_SUCCESS;
3217}
3218
3219/**
3220 * Convert SVGA blend op value to its D3D equivalent
3221 */
3222static DWORD vmsvga3dBlendOp2D3D(uint32_t blendOp, DWORD defaultBlendOp)
3223{
3224 switch (blendOp)
3225 {
3226 case SVGA3D_BLENDOP_ZERO:
3227 return D3DBLEND_ZERO;
3228 case SVGA3D_BLENDOP_ONE:
3229 return D3DBLEND_ONE;
3230 case SVGA3D_BLENDOP_SRCCOLOR:
3231 return D3DBLEND_SRCCOLOR;
3232 case SVGA3D_BLENDOP_INVSRCCOLOR:
3233 return D3DBLEND_INVSRCCOLOR;
3234 case SVGA3D_BLENDOP_SRCALPHA:
3235 return D3DBLEND_SRCALPHA;
3236 case SVGA3D_BLENDOP_INVSRCALPHA:
3237 return D3DBLEND_INVSRCALPHA;
3238 case SVGA3D_BLENDOP_DESTALPHA:
3239 return D3DBLEND_DESTALPHA;
3240 case SVGA3D_BLENDOP_INVDESTALPHA:
3241 return D3DBLEND_INVDESTALPHA;
3242 case SVGA3D_BLENDOP_DESTCOLOR:
3243 return D3DBLEND_DESTCOLOR;
3244 case SVGA3D_BLENDOP_INVDESTCOLOR:
3245 return D3DBLEND_INVDESTCOLOR;
3246 case SVGA3D_BLENDOP_SRCALPHASAT:
3247 return D3DBLEND_SRCALPHASAT;
3248 case SVGA3D_BLENDOP_BLENDFACTOR:
3249 return D3DBLEND_BLENDFACTOR;
3250 case SVGA3D_BLENDOP_INVBLENDFACTOR:
3251 return D3DBLEND_INVBLENDFACTOR;
3252 default:
3253 AssertFailed();
3254 return defaultBlendOp;
3255 }
3256}
3257
3258int vmsvga3dSetRenderState(PVGASTATECC pThisCC, uint32_t cid, uint32_t cRenderStates, SVGA3dRenderState *pRenderState)
3259{
3260 DWORD val = 0; /* Shut up MSC */
3261 HRESULT hr;
3262 PVMSVGA3DSTATE pState = pThisCC->svga.p3dState;
3263 AssertReturn(pState, VERR_NO_MEMORY);
3264
3265 Log(("vmsvga3dSetRenderState cid=%u cRenderStates=%d\n", cid, cRenderStates));
3266
3267 PVMSVGA3DCONTEXT pContext;
3268 int rc = vmsvga3dContextFromCid(pState, cid, &pContext);
3269 AssertRCReturn(rc, rc);
3270 AssertReturn(pContext->pDevice, VERR_INTERNAL_ERROR);
3271
3272 for (unsigned i = 0; i < cRenderStates; i++)
3273 {
3274 D3DRENDERSTATETYPE renderState = D3DRS_FORCE_DWORD;
3275
3276 Log(("vmsvga3dSetRenderState: state=%s (%d) val=%x\n", vmsvga3dGetRenderStateName(pRenderState[i].state), pRenderState[i].state, pRenderState[i].uintValue));
3277 /* Save the render state for vm state saving. */
3278 ASSERT_GUEST_RETURN((unsigned)pRenderState[i].state < SVGA3D_RS_MAX, VERR_INVALID_PARAMETER);
3279 pContext->state.aRenderState[pRenderState[i].state] = pRenderState[i];
3280
3281 switch (pRenderState[i].state)
3282 {
3283 case SVGA3D_RS_ZENABLE: /* SVGA3dBool */
3284 renderState = D3DRS_ZENABLE;
3285 val = pRenderState[i].uintValue;
3286 Assert(val == D3DZB_FALSE || val == D3DZB_TRUE);
3287 break;
3288
3289 case SVGA3D_RS_ZWRITEENABLE: /* SVGA3dBool */
3290 renderState = D3DRS_ZWRITEENABLE;
3291 val = pRenderState[i].uintValue;
3292 break;
3293
3294 case SVGA3D_RS_ALPHATESTENABLE: /* SVGA3dBool */
3295 renderState = D3DRS_ALPHATESTENABLE;
3296 val = pRenderState[i].uintValue;
3297 break;
3298
3299 case SVGA3D_RS_DITHERENABLE: /* SVGA3dBool */
3300 renderState = D3DRS_DITHERENABLE;
3301 val = pRenderState[i].uintValue;
3302 break;
3303
3304 case SVGA3D_RS_BLENDENABLE: /* SVGA3dBool */
3305 renderState = D3DRS_ALPHABLENDENABLE;
3306 val = pRenderState[i].uintValue;
3307 break;
3308
3309 case SVGA3D_RS_FOGENABLE: /* SVGA3dBool */
3310 renderState = D3DRS_FOGENABLE;
3311 val = pRenderState[i].uintValue;
3312 break;
3313
3314 case SVGA3D_RS_SPECULARENABLE: /* SVGA3dBool */
3315 renderState = D3DRS_SPECULARENABLE;
3316 val = pRenderState[i].uintValue;
3317 break;
3318
3319 case SVGA3D_RS_LIGHTINGENABLE: /* SVGA3dBool */
3320 renderState = D3DRS_LIGHTING;
3321 val = pRenderState[i].uintValue;
3322 break;
3323
3324 case SVGA3D_RS_NORMALIZENORMALS: /* SVGA3dBool */
3325 renderState = D3DRS_NORMALIZENORMALS;
3326 val = pRenderState[i].uintValue;
3327 break;
3328
3329 case SVGA3D_RS_POINTSPRITEENABLE: /* SVGA3dBool */
3330 renderState = D3DRS_POINTSPRITEENABLE;
3331 val = pRenderState[i].uintValue;
3332 break;
3333
3334 case SVGA3D_RS_POINTSCALEENABLE: /* SVGA3dBool */
3335 renderState = D3DRS_POINTSCALEENABLE;
3336 val = pRenderState[i].uintValue;
3337 break;
3338
3339 case SVGA3D_RS_POINTSIZE: /* float */
3340 renderState = D3DRS_POINTSIZE;
3341 val = pRenderState[i].uintValue;
3342 Log(("SVGA3D_RS_POINTSIZE: %d\n", (uint32_t) (pRenderState[i].floatValue * 100.0)));
3343 break;
3344
3345 case SVGA3D_RS_POINTSIZEMIN: /* float */
3346 renderState = D3DRS_POINTSIZE_MIN;
3347 val = pRenderState[i].uintValue;
3348 Log(("SVGA3D_RS_POINTSIZEMIN: %d\n", (uint32_t) (pRenderState[i].floatValue * 100.0)));
3349 break;
3350
3351 case SVGA3D_RS_POINTSIZEMAX: /* float */
3352 renderState = D3DRS_POINTSIZE_MAX;
3353 val = pRenderState[i].uintValue;
3354 Log(("SVGA3D_RS_POINTSIZEMAX: %d\n", (uint32_t) (pRenderState[i].floatValue * 100.0)));
3355 break;
3356
3357 case SVGA3D_RS_POINTSCALE_A: /* float */
3358 renderState = D3DRS_POINTSCALE_A;
3359 val = pRenderState[i].uintValue;
3360 break;
3361
3362 case SVGA3D_RS_POINTSCALE_B: /* float */
3363 renderState = D3DRS_POINTSCALE_B;
3364 val = pRenderState[i].uintValue;
3365 break;
3366
3367 case SVGA3D_RS_POINTSCALE_C: /* float */
3368 renderState = D3DRS_POINTSCALE_C;
3369 val = pRenderState[i].uintValue;
3370 break;
3371
3372 case SVGA3D_RS_AMBIENT: /* SVGA3dColor - identical */
3373 renderState = D3DRS_AMBIENT;
3374 val = pRenderState[i].uintValue;
3375 break;
3376
3377 case SVGA3D_RS_CLIPPLANEENABLE: /* SVGA3dClipPlanes - identical */
3378 renderState = D3DRS_CLIPPLANEENABLE;
3379 val = pRenderState[i].uintValue;
3380 break;
3381
3382 case SVGA3D_RS_FOGCOLOR: /* SVGA3dColor - identical */
3383 renderState = D3DRS_FOGCOLOR;
3384 val = pRenderState[i].uintValue;
3385 break;
3386
3387 case SVGA3D_RS_FOGSTART: /* float */
3388 renderState = D3DRS_FOGSTART;
3389 val = pRenderState[i].uintValue;
3390 break;
3391
3392 case SVGA3D_RS_FOGEND: /* float */
3393 renderState = D3DRS_FOGEND;
3394 val = pRenderState[i].uintValue;
3395 break;
3396
3397 case SVGA3D_RS_FOGDENSITY: /* float */
3398 renderState = D3DRS_FOGDENSITY;
3399 val = pRenderState[i].uintValue;
3400 break;
3401
3402 case SVGA3D_RS_RANGEFOGENABLE: /* SVGA3dBool */
3403 renderState = D3DRS_RANGEFOGENABLE;
3404 val = pRenderState[i].uintValue;
3405 break;
3406
3407 case SVGA3D_RS_FOGMODE: /* SVGA3dFogMode */
3408 {
3409 SVGA3dFogMode mode;
3410 mode.uintValue = pRenderState[i].uintValue;
3411
3412 switch (mode.function)
3413 {
3414 case SVGA3D_FOGFUNC_INVALID:
3415 val = D3DFOG_NONE;
3416 break;
3417 case SVGA3D_FOGFUNC_EXP:
3418 val = D3DFOG_EXP;
3419 break;
3420 case SVGA3D_FOGFUNC_EXP2:
3421 val = D3DFOG_EXP2;
3422 break;
3423 case SVGA3D_FOGFUNC_LINEAR:
3424 val = D3DFOG_LINEAR;
3425 break;
3426 case SVGA3D_FOGFUNC_PER_VERTEX: /* unable to find a d3d9 equivalent */
3427 AssertMsgFailedReturn(("Unsupported fog function SVGA3D_FOGFUNC_PER_VERTEX\n"), VERR_INTERNAL_ERROR);
3428 break;
3429 default:
3430 AssertMsgFailedReturn(("Unexpected fog function %d\n", mode.function), VERR_INTERNAL_ERROR);
3431 break;
3432 }
3433
3434 /* The fog type determines the render state. */
3435 switch (mode.type)
3436 {
3437 case SVGA3D_FOGTYPE_VERTEX:
3438 renderState = D3DRS_FOGVERTEXMODE;
3439 break;
3440 case SVGA3D_FOGTYPE_PIXEL:
3441 renderState = D3DRS_FOGTABLEMODE;
3442 break;
3443 default:
3444 AssertMsgFailedReturn(("Unexpected fog type %d\n", mode.type), VERR_INTERNAL_ERROR);
3445 break;
3446 }
3447
3448 /* Set the fog base to depth or range. */
3449 switch (mode.base)
3450 {
3451 case SVGA3D_FOGBASE_DEPTHBASED:
3452 hr = pContext->pDevice->SetRenderState(D3DRS_RANGEFOGENABLE, FALSE);
3453 AssertMsgReturn(hr == D3D_OK, ("vmsvga3dSetRenderState: SetRenderState SVGA3D_FOGBASE_DEPTHBASED failed with %x\n", hr), VERR_INTERNAL_ERROR);
3454 break;
3455 case SVGA3D_FOGBASE_RANGEBASED:
3456 hr = pContext->pDevice->SetRenderState(D3DRS_RANGEFOGENABLE, TRUE);
3457 AssertMsgReturn(hr == D3D_OK, ("vmsvga3dSetRenderState: SetRenderState SVGA3D_FOGBASE_RANGEBASED failed with %x\n", hr), VERR_INTERNAL_ERROR);
3458 break;
3459 default:
3460 /* ignore */
3461 AssertMsgFailed(("Unexpected fog base %d\n", mode.base));
3462 break;
3463 }
3464 break;
3465 }
3466
3467 case SVGA3D_RS_FILLMODE: /* SVGA3dFillMode */
3468 {
3469 SVGA3dFillMode mode;
3470
3471 mode.uintValue = pRenderState[i].uintValue;
3472
3473 switch (mode.mode)
3474 {
3475 case SVGA3D_FILLMODE_POINT:
3476 val = D3DFILL_POINT;
3477 break;
3478 case SVGA3D_FILLMODE_LINE:
3479 val = D3DFILL_WIREFRAME;
3480 break;
3481 case SVGA3D_FILLMODE_FILL:
3482 val = D3DFILL_SOLID;
3483 break;
3484 default:
3485 AssertMsgFailedReturn(("Unexpected fill mode %d\n", mode.mode), VERR_INTERNAL_ERROR);
3486 break;
3487 }
3488 /** @todo ignoring face for now. */
3489 renderState = D3DRS_FILLMODE;
3490 break;
3491 }
3492
3493 case SVGA3D_RS_SHADEMODE: /* SVGA3dShadeMode */
3494 renderState = D3DRS_SHADEMODE;
3495 AssertCompile(D3DSHADE_FLAT == SVGA3D_SHADEMODE_FLAT);
3496 val = pRenderState[i].uintValue; /* SVGA3dShadeMode == D3DSHADEMODE */
3497 break;
3498
3499 case SVGA3D_RS_LINEPATTERN: /* SVGA3dLinePattern */
3500 /* No longer supported by d3d; mesagl comments suggest not all backends support it */
3501 /** @todo */
3502 Log(("WARNING: SVGA3D_RS_LINEPATTERN %x not supported!!\n", pRenderState[i].uintValue));
3503 /*
3504 renderState = D3DRS_LINEPATTERN;
3505 val = pRenderState[i].uintValue;
3506 */
3507 break;
3508
3509 case SVGA3D_RS_SRCBLEND: /* SVGA3dBlendOp */
3510 renderState = D3DRS_SRCBLEND;
3511 val = vmsvga3dBlendOp2D3D(pRenderState[i].uintValue, D3DBLEND_ONE /* default */);
3512 break;
3513
3514 case SVGA3D_RS_DSTBLEND: /* SVGA3dBlendOp */
3515 renderState = D3DRS_DESTBLEND;
3516 val = vmsvga3dBlendOp2D3D(pRenderState[i].uintValue, D3DBLEND_ZERO /* default */);
3517 break;
3518
3519 case SVGA3D_RS_BLENDEQUATION: /* SVGA3dBlendEquation - identical */
3520 AssertCompile(SVGA3D_BLENDEQ_MAXIMUM == D3DBLENDOP_MAX);
3521 renderState = D3DRS_BLENDOP;
3522 val = pRenderState[i].uintValue;
3523 break;
3524
3525 case SVGA3D_RS_CULLMODE: /* SVGA3dFace */
3526 {
3527 switch (pRenderState[i].uintValue)
3528 {
3529 case SVGA3D_FACE_NONE:
3530 val = D3DCULL_NONE;
3531 break;
3532 case SVGA3D_FACE_FRONT:
3533 val = D3DCULL_CW;
3534 break;
3535 case SVGA3D_FACE_BACK:
3536 val = D3DCULL_CCW;
3537 break;
3538 case SVGA3D_FACE_FRONT_BACK:
3539 AssertFailed();
3540 val = D3DCULL_CW;
3541 break;
3542 default:
3543 AssertMsgFailedReturn(("Unexpected cull mode %d\n", pRenderState[i].uintValue), VERR_INTERNAL_ERROR);
3544 break;
3545 }
3546 renderState = D3DRS_CULLMODE;
3547 break;
3548 }
3549
3550 case SVGA3D_RS_ZFUNC: /* SVGA3dCmpFunc - identical */
3551 AssertCompile(SVGA3D_CMP_ALWAYS == D3DCMP_ALWAYS);
3552 renderState = D3DRS_ZFUNC;
3553 val = pRenderState[i].uintValue;
3554 break;
3555
3556 case SVGA3D_RS_ALPHAFUNC: /* SVGA3dCmpFunc - identical */
3557 renderState = D3DRS_ALPHAFUNC;
3558 val = pRenderState[i].uintValue;
3559 break;
3560
3561 case SVGA3D_RS_STENCILENABLE: /* SVGA3dBool */
3562 renderState = D3DRS_STENCILENABLE;
3563 val = pRenderState[i].uintValue;
3564 break;
3565
3566 case SVGA3D_RS_STENCILREF: /* uint32_t */
3567 renderState = D3DRS_STENCILREF;
3568 val = pRenderState[i].uintValue;
3569 break;
3570
3571 case SVGA3D_RS_STENCILMASK: /* uint32_t */
3572 renderState = D3DRS_STENCILMASK;
3573 val = pRenderState[i].uintValue;
3574 break;
3575
3576 case SVGA3D_RS_STENCILWRITEMASK: /* uint32_t */
3577 renderState = D3DRS_STENCILWRITEMASK;
3578 val = pRenderState[i].uintValue;
3579 break;
3580
3581 case SVGA3D_RS_STENCILFUNC: /* SVGA3dCmpFunc - identical */
3582 renderState = D3DRS_STENCILFUNC;
3583 val = pRenderState[i].uintValue;
3584 break;
3585
3586 case SVGA3D_RS_STENCILFAIL: /* SVGA3dStencilOp - identical */
3587 AssertCompile(D3DSTENCILOP_KEEP == SVGA3D_STENCILOP_KEEP);
3588 AssertCompile(D3DSTENCILOP_DECR == SVGA3D_STENCILOP_DECR);
3589 renderState = D3DRS_STENCILFAIL;
3590 val = pRenderState[i].uintValue;
3591 break;
3592
3593 case SVGA3D_RS_STENCILZFAIL: /* SVGA3dStencilOp - identical */
3594 renderState = D3DRS_STENCILZFAIL;
3595 val = pRenderState[i].uintValue;
3596 break;
3597
3598 case SVGA3D_RS_STENCILPASS: /* SVGA3dStencilOp - identical */
3599 renderState = D3DRS_STENCILPASS;
3600 val = pRenderState[i].uintValue;
3601 break;
3602
3603 case SVGA3D_RS_ALPHAREF: /* float (0.0 .. 1.0) */
3604 renderState = D3DRS_ALPHAREF;
3605 val = (uint8_t)(pRenderState[i].floatValue * 255.0f); /* D3DRS_ALPHAREF 0..255 */
3606 break;
3607
3608 case SVGA3D_RS_FRONTWINDING: /* SVGA3dFrontWinding */
3609 Assert(pRenderState[i].uintValue == SVGA3D_FRONTWINDING_CW);
3610 /*
3611 renderState = D3DRS_FRONTWINDING; //D3DRS_TWOSIDEDSTENCILMODE
3612 val = pRenderState[i].uintValue;
3613 */
3614 break;
3615
3616 case SVGA3D_RS_COORDINATETYPE: /* SVGA3dCoordinateType */
3617 Assert(pRenderState[i].uintValue == SVGA3D_COORDINATE_LEFTHANDED);
3618 /** @todo setup a view matrix to scale the world space by -1 in the z-direction for right handed coordinates. */
3619 /*
3620 renderState = D3DRS_COORDINATETYPE;
3621 val = pRenderState[i].uintValue;
3622 */
3623 break;
3624
3625 case SVGA3D_RS_ZBIAS: /* float */
3626 /** @todo unknown meaning; depth bias is not identical
3627 renderState = D3DRS_DEPTHBIAS;
3628 val = pRenderState[i].uintValue;
3629 */
3630 Log(("vmsvga3dSetRenderState: WARNING unsupported SVGA3D_RS_ZBIAS\n"));
3631 break;
3632
3633 case SVGA3D_RS_SLOPESCALEDEPTHBIAS: /* float */
3634 renderState = D3DRS_SLOPESCALEDEPTHBIAS;
3635 val = pRenderState[i].uintValue;
3636 break;
3637
3638 case SVGA3D_RS_DEPTHBIAS: /* float */
3639 renderState = D3DRS_DEPTHBIAS;
3640 val = pRenderState[i].uintValue;
3641 break;
3642
3643 case SVGA3D_RS_COLORWRITEENABLE: /* SVGA3dColorMask - identical to D3DCOLORWRITEENABLE_* */
3644 renderState = D3DRS_COLORWRITEENABLE;
3645 val = pRenderState[i].uintValue;
3646 break;
3647
3648 case SVGA3D_RS_VERTEXMATERIALENABLE: /* SVGA3dBool */
3649 //AssertFailed();
3650 renderState = D3DRS_INDEXEDVERTEXBLENDENABLE; /* correct?? */
3651 val = pRenderState[i].uintValue;
3652 break;
3653
3654 case SVGA3D_RS_DIFFUSEMATERIALSOURCE: /* SVGA3dVertexMaterial - identical */
3655 AssertCompile(D3DMCS_COLOR2 == SVGA3D_VERTEXMATERIAL_SPECULAR);
3656 renderState = D3DRS_DIFFUSEMATERIALSOURCE;
3657 val = pRenderState[i].uintValue;
3658 break;
3659
3660 case SVGA3D_RS_SPECULARMATERIALSOURCE: /* SVGA3dVertexMaterial - identical */
3661 renderState = D3DRS_SPECULARMATERIALSOURCE;
3662 val = pRenderState[i].uintValue;
3663 break;
3664
3665 case SVGA3D_RS_AMBIENTMATERIALSOURCE: /* SVGA3dVertexMaterial - identical */
3666 renderState = D3DRS_AMBIENTMATERIALSOURCE;
3667 val = pRenderState[i].uintValue;
3668 break;
3669
3670 case SVGA3D_RS_EMISSIVEMATERIALSOURCE: /* SVGA3dVertexMaterial - identical */
3671 renderState = D3DRS_EMISSIVEMATERIALSOURCE;
3672 val = pRenderState[i].uintValue;
3673 break;
3674
3675 case SVGA3D_RS_TEXTUREFACTOR: /* SVGA3dColor - identical */
3676 renderState = D3DRS_TEXTUREFACTOR;
3677 val = pRenderState[i].uintValue;
3678 break;
3679
3680 case SVGA3D_RS_LOCALVIEWER: /* SVGA3dBool */
3681 renderState = D3DRS_LOCALVIEWER;
3682 val = pRenderState[i].uintValue;
3683 break;
3684
3685 case SVGA3D_RS_SCISSORTESTENABLE: /* SVGA3dBool */
3686 renderState = D3DRS_SCISSORTESTENABLE;
3687 val = pRenderState[i].uintValue;
3688 break;
3689
3690 case SVGA3D_RS_BLENDCOLOR: /* SVGA3dColor - identical */
3691 renderState = D3DRS_BLENDFACTOR;
3692 val = pRenderState[i].uintValue;
3693 break;
3694
3695 case SVGA3D_RS_STENCILENABLE2SIDED: /* SVGA3dBool */
3696 renderState = D3DRS_TWOSIDEDSTENCILMODE;
3697 val = pRenderState[i].uintValue;
3698 break;
3699
3700 case SVGA3D_RS_CCWSTENCILFUNC: /* SVGA3dCmpFunc - identical */
3701 renderState = D3DRS_CCW_STENCILFUNC;
3702 val = pRenderState[i].uintValue;
3703 break;
3704
3705 case SVGA3D_RS_CCWSTENCILFAIL: /* SVGA3dStencilOp - identical */
3706 renderState = D3DRS_CCW_STENCILFAIL;
3707 val = pRenderState[i].uintValue;
3708 break;
3709
3710 case SVGA3D_RS_CCWSTENCILZFAIL: /* SVGA3dStencilOp - identical */
3711 renderState = D3DRS_CCW_STENCILZFAIL;
3712 val = pRenderState[i].uintValue;
3713 break;
3714
3715 case SVGA3D_RS_CCWSTENCILPASS: /* SVGA3dStencilOp - identical */
3716 renderState = D3DRS_CCW_STENCILPASS;
3717 val = pRenderState[i].uintValue;
3718 break;
3719
3720 case SVGA3D_RS_VERTEXBLEND: /* SVGA3dVertexBlendFlags - identical */
3721 AssertCompile(SVGA3D_VBLEND_DISABLE == D3DVBF_DISABLE);
3722 renderState = D3DRS_VERTEXBLEND;
3723 val = pRenderState[i].uintValue;
3724 break;
3725
3726 case SVGA3D_RS_OUTPUTGAMMA: /* float */
3727 //AssertFailed();
3728 /*
3729 D3DRS_SRGBWRITEENABLE ??
3730 renderState = D3DRS_OUTPUTGAMMA;
3731 val = pRenderState[i].uintValue;
3732 */
3733 break;
3734
3735 case SVGA3D_RS_ZVISIBLE: /* SVGA3dBool */
3736 AssertFailed();
3737 /*
3738 renderState = D3DRS_ZVISIBLE;
3739 val = pRenderState[i].uintValue;
3740 */
3741 break;
3742
3743 case SVGA3D_RS_LASTPIXEL: /* SVGA3dBool */
3744 renderState = D3DRS_LASTPIXEL;
3745 val = pRenderState[i].uintValue;
3746 break;
3747
3748 case SVGA3D_RS_CLIPPING: /* SVGA3dBool */
3749 renderState = D3DRS_CLIPPING;
3750 val = pRenderState[i].uintValue;
3751 break;
3752
3753 case SVGA3D_RS_WRAP0: /* SVGA3dWrapFlags - identical */
3754 Assert(SVGA3D_WRAPCOORD_3 == D3DWRAPCOORD_3);
3755 renderState = D3DRS_WRAP0;
3756 val = pRenderState[i].uintValue;
3757 break;
3758
3759 case SVGA3D_RS_WRAP1: /* SVGA3dWrapFlags - identical */
3760 renderState = D3DRS_WRAP1;
3761 val = pRenderState[i].uintValue;
3762 break;
3763
3764 case SVGA3D_RS_WRAP2: /* SVGA3dWrapFlags - identical */
3765 renderState = D3DRS_WRAP2;
3766 val = pRenderState[i].uintValue;
3767 break;
3768
3769 case SVGA3D_RS_WRAP3: /* SVGA3dWrapFlags - identical */
3770 renderState = D3DRS_WRAP3;
3771 val = pRenderState[i].uintValue;
3772 break;
3773
3774 case SVGA3D_RS_WRAP4: /* SVGA3dWrapFlags - identical */
3775 renderState = D3DRS_WRAP4;
3776 val = pRenderState[i].uintValue;
3777 break;
3778
3779 case SVGA3D_RS_WRAP5: /* SVGA3dWrapFlags - identical */
3780 renderState = D3DRS_WRAP5;
3781 val = pRenderState[i].uintValue;
3782 break;
3783
3784 case SVGA3D_RS_WRAP6: /* SVGA3dWrapFlags - identical */
3785 renderState = D3DRS_WRAP6;
3786 val = pRenderState[i].uintValue;
3787 break;
3788
3789 case SVGA3D_RS_WRAP7: /* SVGA3dWrapFlags - identical */
3790 renderState = D3DRS_WRAP7;
3791 val = pRenderState[i].uintValue;
3792 break;
3793
3794 case SVGA3D_RS_WRAP8: /* SVGA3dWrapFlags - identical */
3795 renderState = D3DRS_WRAP8;
3796 val = pRenderState[i].uintValue;
3797 break;
3798
3799 case SVGA3D_RS_WRAP9: /* SVGA3dWrapFlags - identical */
3800 renderState = D3DRS_WRAP9;
3801 val = pRenderState[i].uintValue;
3802 break;
3803
3804 case SVGA3D_RS_WRAP10: /* SVGA3dWrapFlags - identical */
3805 renderState = D3DRS_WRAP10;
3806 val = pRenderState[i].uintValue;
3807 break;
3808
3809 case SVGA3D_RS_WRAP11: /* SVGA3dWrapFlags - identical */
3810 renderState = D3DRS_WRAP11;
3811 val = pRenderState[i].uintValue;
3812 break;
3813
3814 case SVGA3D_RS_WRAP12: /* SVGA3dWrapFlags - identical */
3815 renderState = D3DRS_WRAP12;
3816 val = pRenderState[i].uintValue;
3817 break;
3818
3819 case SVGA3D_RS_WRAP13: /* SVGA3dWrapFlags - identical */
3820 renderState = D3DRS_WRAP13;
3821 val = pRenderState[i].uintValue;
3822 break;
3823
3824 case SVGA3D_RS_WRAP14: /* SVGA3dWrapFlags - identical */
3825 renderState = D3DRS_WRAP14;
3826 val = pRenderState[i].uintValue;
3827 break;
3828
3829 case SVGA3D_RS_WRAP15: /* SVGA3dWrapFlags - identical */
3830 renderState = D3DRS_WRAP15;
3831 val = pRenderState[i].uintValue;
3832 break;
3833
3834 case SVGA3D_RS_MULTISAMPLEANTIALIAS: /* SVGA3dBool */
3835 renderState = D3DRS_MULTISAMPLEANTIALIAS;
3836 val = pRenderState[i].uintValue;
3837 break;
3838
3839 case SVGA3D_RS_MULTISAMPLEMASK: /* uint32_t */
3840 renderState = D3DRS_MULTISAMPLEMASK;
3841 val = pRenderState[i].uintValue;
3842 break;
3843
3844 case SVGA3D_RS_INDEXEDVERTEXBLENDENABLE: /* SVGA3dBool */
3845 renderState = D3DRS_INDEXEDVERTEXBLENDENABLE;
3846 val = pRenderState[i].uintValue;
3847 break;
3848
3849 case SVGA3D_RS_TWEENFACTOR: /* float */
3850 renderState = D3DRS_TWEENFACTOR;
3851 val = pRenderState[i].uintValue;
3852 break;
3853
3854 case SVGA3D_RS_ANTIALIASEDLINEENABLE: /* SVGA3dBool */
3855 renderState = D3DRS_ANTIALIASEDLINEENABLE;
3856 val = pRenderState[i].uintValue;
3857 break;
3858
3859 case SVGA3D_RS_COLORWRITEENABLE1: /* SVGA3dColorMask - identical to D3DCOLORWRITEENABLE_* */
3860 renderState = D3DRS_COLORWRITEENABLE1;
3861 val = pRenderState[i].uintValue;
3862 break;
3863
3864 case SVGA3D_RS_COLORWRITEENABLE2: /* SVGA3dColorMask - identical to D3DCOLORWRITEENABLE_* */
3865 renderState = D3DRS_COLORWRITEENABLE2;
3866 val = pRenderState[i].uintValue;
3867 break;
3868
3869 case SVGA3D_RS_COLORWRITEENABLE3: /* SVGA3dColorMask - identical to D3DCOLORWRITEENABLE_* */
3870 renderState = D3DRS_COLORWRITEENABLE3;
3871 val = pRenderState[i].uintValue;
3872 break;
3873
3874 case SVGA3D_RS_SEPARATEALPHABLENDENABLE: /* SVGA3dBool */
3875 renderState = D3DRS_SEPARATEALPHABLENDENABLE;
3876 val = pRenderState[i].uintValue;
3877 break;
3878
3879 case SVGA3D_RS_SRCBLENDALPHA: /* SVGA3dBlendOp */
3880 renderState = D3DRS_SRCBLENDALPHA;
3881 val = vmsvga3dBlendOp2D3D(pRenderState[i].uintValue, D3DBLEND_ONE /* default */);
3882 break;
3883
3884 case SVGA3D_RS_DSTBLENDALPHA: /* SVGA3dBlendOp */
3885 renderState = D3DRS_DESTBLENDALPHA;
3886 val = vmsvga3dBlendOp2D3D(pRenderState[i].uintValue, D3DBLEND_ZERO /* default */);
3887 break;
3888
3889 case SVGA3D_RS_BLENDEQUATIONALPHA: /* SVGA3dBlendEquation - identical */
3890 renderState = D3DRS_BLENDOPALPHA;
3891 val = pRenderState[i].uintValue;
3892 break;
3893
3894 case SVGA3D_RS_TRANSPARENCYANTIALIAS: /* SVGA3dTransparencyAntialiasType */
3895 AssertFailed();
3896 /*
3897 renderState = D3DRS_TRANSPARENCYANTIALIAS;
3898 val = pRenderState[i].uintValue;
3899 */
3900 break;
3901
3902 case SVGA3D_RS_LINEWIDTH: /* float */
3903 AssertFailed();
3904 /*
3905 renderState = D3DRS_LINEWIDTH;
3906 val = pRenderState[i].uintValue;
3907 */
3908 break;
3909
3910 case SVGA3D_RS_MAX: /* shut up MSC */
3911 case SVGA3D_RS_INVALID:
3912 AssertFailedBreak();
3913 }
3914
3915 if (renderState != D3DRS_FORCE_DWORD)
3916 {
3917 hr = pContext->pDevice->SetRenderState(renderState, val);
3918 AssertMsgReturn(hr == D3D_OK, ("vmsvga3dSetRenderState: SetRenderState failed with %x\n", hr), VERR_INTERNAL_ERROR);
3919 }
3920 }
3921
3922 return VINF_SUCCESS;
3923}
3924
3925int vmsvga3dSetRenderTarget(PVGASTATECC pThisCC, uint32_t cid, SVGA3dRenderTargetType type, SVGA3dSurfaceImageId target)
3926{
3927 HRESULT hr;
3928 PVMSVGA3DSTATE pState = pThisCC->svga.p3dState;
3929
3930 AssertReturn(pState, VERR_NO_MEMORY);
3931 AssertReturn((unsigned)type < SVGA3D_RT_MAX, VERR_INVALID_PARAMETER);
3932
3933 LogFunc(("cid=%u type=%x sid=%u face=%u mipmap=%u\n", cid, type, target.sid, target.face, target.mipmap));
3934
3935 PVMSVGA3DCONTEXT pContext;
3936 int rc = vmsvga3dContextFromCid(pState, cid, &pContext);
3937 AssertRCReturn(rc, rc);
3938 AssertReturn(pContext->pDevice, VERR_INTERNAL_ERROR);
3939
3940 /* Save for vm state save/restore. */
3941 pContext->state.aRenderTargets[type] = target.sid;
3942 /** @todo Also save target.face and target.mipmap */
3943
3944 if (target.sid == SVGA3D_INVALID_ID)
3945 {
3946 /* Disable render target. */
3947 switch (type)
3948 {
3949 case SVGA3D_RT_DEPTH:
3950 hr = pContext->pDevice->SetDepthStencilSurface(NULL);
3951 AssertMsgReturn(hr == D3D_OK, ("SetDepthStencilSurface failed with %x\n", hr), VERR_INTERNAL_ERROR);
3952 break;
3953
3954 case SVGA3D_RT_STENCIL:
3955 /* ignore; correct?? */
3956 break;
3957
3958 case SVGA3D_RT_COLOR0:
3959 case SVGA3D_RT_COLOR1:
3960 case SVGA3D_RT_COLOR2:
3961 case SVGA3D_RT_COLOR3:
3962 case SVGA3D_RT_COLOR4:
3963 case SVGA3D_RT_COLOR5:
3964 case SVGA3D_RT_COLOR6:
3965 case SVGA3D_RT_COLOR7:
3966 if (pState->fSupportedSurfaceNULL)
3967 {
3968 /* Create a dummy render target to satisfy D3D. This path is usually taken only to render
3969 * into a depth buffer without wishing to update an actual color render target.
3970 * The dimensions of the render target must match the dimensions of the depth render target,
3971 * which is usually equal to the viewport width and height.
3972 */
3973 IDirect3DSurface9 *pDummyRenderTarget;
3974 hr = pContext->pDevice->CreateRenderTarget(pContext->state.RectViewPort.w,
3975 pContext->state.RectViewPort.h,
3976 FOURCC_NULL,
3977 D3DMULTISAMPLE_NONE,
3978 0,
3979 FALSE,
3980 &pDummyRenderTarget,
3981 NULL);
3982
3983 AssertMsgReturn(hr == D3D_OK, ("CreateRenderTarget failed with %x\n", hr), VERR_INTERNAL_ERROR);
3984
3985 hr = pContext->pDevice->SetRenderTarget(type - SVGA3D_RT_COLOR0, pDummyRenderTarget);
3986 D3D_RELEASE(pDummyRenderTarget);
3987 }
3988 else
3989 hr = pContext->pDevice->SetRenderTarget(type - SVGA3D_RT_COLOR0, NULL);
3990
3991 AssertMsgReturn(hr == D3D_OK, ("SetRenderTarget failed with %x\n", hr), VERR_INTERNAL_ERROR);
3992 break;
3993
3994 default:
3995 AssertFailedReturn(VERR_INVALID_PARAMETER);
3996 }
3997 return VINF_SUCCESS;
3998 }
3999
4000 PVMSVGA3DSURFACE pRenderTarget;
4001 rc = vmsvga3dSurfaceFromSid(pState, target.sid, &pRenderTarget);
4002 AssertRCReturn(rc, rc);
4003
4004 switch (type)
4005 {
4006 case SVGA3D_RT_DEPTH:
4007 case SVGA3D_RT_STENCIL:
4008 AssertReturn(target.face == 0 && target.mipmap == 0, VERR_INVALID_PARAMETER);
4009 if (!pRenderTarget->u.pSurface)
4010 {
4011 DWORD cQualityLevels = 0;
4012
4013 /* Query the nr of quality levels for this particular format */
4014 if (pRenderTarget->multiSampleTypeD3D != D3DMULTISAMPLE_NONE)
4015 {
4016 hr = pState->pD3D9->CheckDeviceMultiSampleType(D3DADAPTER_DEFAULT,
4017 D3DDEVTYPE_HAL,
4018 pRenderTarget->formatD3D,
4019 TRUE, /* Windowed */
4020 pRenderTarget->multiSampleTypeD3D,
4021 &cQualityLevels);
4022 Assert(hr == D3D_OK || hr == D3DERR_NOTAVAILABLE);
4023 }
4024
4025 if ( pState->fSupportedSurfaceINTZ
4026 && pRenderTarget->multiSampleTypeD3D == D3DMULTISAMPLE_NONE
4027 && ( pRenderTarget->formatD3D == D3DFMT_D24S8
4028 || pRenderTarget->formatD3D == D3DFMT_D24X8
4029 || pRenderTarget->formatD3D == D3DFMT_D32
4030 || pRenderTarget->formatD3D == D3DFMT_D16))
4031 {
4032 LogFunc(("Creating stencil surface as texture!\n"));
4033 int rc2 = vmsvga3dBackCreateTexture(pState, pContext, cid, pRenderTarget);
4034 AssertRC(rc2); /* non-fatal, will use CreateDepthStencilSurface */
4035 }
4036
4037 if (!pRenderTarget->fStencilAsTexture)
4038 {
4039 Assert(!pRenderTarget->u.pSurface);
4040
4041 LogFunc(("DEPTH/STENCIL; cQualityLevels=%d\n", cQualityLevels));
4042 hr = pContext->pDevice->CreateDepthStencilSurface(pRenderTarget->paMipmapLevels[0].mipmapSize.width,
4043 pRenderTarget->paMipmapLevels[0].mipmapSize.height,
4044 pRenderTarget->formatD3D,
4045 pRenderTarget->multiSampleTypeD3D,
4046 ((cQualityLevels >= 1) ? cQualityLevels - 1 : 0), /* 0 - (levels-1) */
4047 FALSE, /* not discardable */
4048 &pRenderTarget->u.pSurface,
4049 NULL);
4050 AssertMsgReturn(hr == D3D_OK, ("CreateDepthStencilSurface failed with %x\n", hr), VERR_INTERNAL_ERROR);
4051 pRenderTarget->enmD3DResType = VMSVGA3D_D3DRESTYPE_SURFACE;
4052 }
4053
4054 pRenderTarget->idAssociatedContext = cid;
4055
4056#if 0 /* doesn't work */
4057 if ( !pRenderTarget->fStencilAsTexture
4058 && pRenderTarget->fDirty)
4059 {
4060 Log(("vmsvga3dSetRenderTarget: sync dirty depth/stencil buffer\n"));
4061 Assert(pRenderTarget->faces[0].numMipLevels == 1);
4062
4063 for (uint32_t i = 0; i < pRenderTarget->faces[0].numMipLevels; i++)
4064 {
4065 if (pRenderTarget->paMipmapLevels[i].fDirty)
4066 {
4067 D3DLOCKED_RECT LockedRect;
4068
4069 hr = pRenderTarget->u.pSurface->LockRect(&LockedRect,
4070 NULL, /* entire surface */
4071 0);
4072
4073 AssertMsgReturn(hr == D3D_OK, ("vmsvga3dSetRenderTarget: LockRect failed with %x\n", hr), VERR_INTERNAL_ERROR);
4074
4075 Log(("vmsvga3dSetRenderTarget: sync dirty texture mipmap level %d (pitch %x vs %x)\n", i, LockedRect.Pitch, pRenderTarget->paMipmapLevels[i].cbSurfacePitch));
4076
4077 uint8_t *pDest = (uint8_t *)LockedRect.pBits;
4078 uint8_t *pSrc = (uint8_t *)pRenderTarget->paMipmapLevels[i].pSurfaceData;
4079 for (uint32_t j = 0; j < pRenderTarget->paMipmapLevels[i].size.height; j++)
4080 {
4081 memcpy(pDest, pSrc, pRenderTarget->paMipmapLevels[i].cbSurfacePitch);
4082
4083 pDest += LockedRect.Pitch;
4084 pSrc += pRenderTarget->paMipmapLevels[i].cbSurfacePitch;
4085 }
4086
4087 hr = pRenderTarget->u.pSurface->UnlockRect();
4088 AssertMsgReturn(hr == D3D_OK, ("vmsvga3dSetRenderTarget: UnlockRect failed with %x\n", hr), VERR_INTERNAL_ERROR);
4089
4090 pRenderTarget->paMipmapLevels[i].fDirty = false;
4091 }
4092 }
4093 }
4094#endif
4095 }
4096
4097 /** @todo Assert(!pRenderTarget->fDirty); */
4098
4099 AssertReturn(pRenderTarget->u.pSurface, VERR_INVALID_PARAMETER);
4100
4101 pRenderTarget->fUsageD3D |= D3DUSAGE_DEPTHSTENCIL;
4102 pRenderTarget->surfaceFlags |= SVGA3D_SURFACE_HINT_DEPTHSTENCIL;
4103
4104 if (pRenderTarget->fStencilAsTexture)
4105 {
4106 IDirect3DSurface9 *pStencilSurface;
4107
4108 rc = vmsvga3dGetD3DSurface(pState, pContext, pRenderTarget, target.face, target.mipmap, /*fLockable=*/ false, &pStencilSurface);
4109 AssertRCReturn(rc, rc);
4110
4111 hr = pContext->pDevice->SetDepthStencilSurface(pStencilSurface);
4112 D3D_RELEASE(pStencilSurface);
4113 AssertMsgReturn(hr == D3D_OK, ("SetDepthStencilSurface failed with %x\n", hr), VERR_INTERNAL_ERROR);
4114 }
4115 else
4116 {
4117 Assert(pRenderTarget->idAssociatedContext == cid);
4118 AssertReturn(pRenderTarget->enmD3DResType == VMSVGA3D_D3DRESTYPE_SURFACE, VERR_INVALID_PARAMETER);
4119 hr = pContext->pDevice->SetDepthStencilSurface(pRenderTarget->u.pSurface);
4120 AssertMsgReturn(hr == D3D_OK, ("SetDepthStencilSurface failed with %x\n", hr), VERR_INTERNAL_ERROR);
4121 }
4122 break;
4123
4124 case SVGA3D_RT_COLOR0:
4125 case SVGA3D_RT_COLOR1:
4126 case SVGA3D_RT_COLOR2:
4127 case SVGA3D_RT_COLOR3:
4128 case SVGA3D_RT_COLOR4:
4129 case SVGA3D_RT_COLOR5:
4130 case SVGA3D_RT_COLOR6:
4131 case SVGA3D_RT_COLOR7:
4132 {
4133 IDirect3DSurface9 *pSurface;
4134 bool fTexture = false;
4135
4136 /* Must flush the other context's 3d pipeline to make sure all drawing is complete for the surface we're about to use. */
4137 vmsvga3dSurfaceFlush(pRenderTarget);
4138
4139 if (pRenderTarget->surfaceFlags & SVGA3D_SURFACE_HINT_TEXTURE)
4140 {
4141 fTexture = true;
4142
4143 /* A texture surface can be used as a render target to fill it and later on used as a texture. */
4144 if (!pRenderTarget->u.pTexture)
4145 {
4146 LogFunc(("Create texture to be used as render target; sid=%u type=%d format=%d -> create texture\n", target.sid, pRenderTarget->surfaceFlags, pRenderTarget->format));
4147 rc = vmsvga3dBackCreateTexture(pState, pContext, cid, pRenderTarget);
4148 AssertRCReturn(rc, rc);
4149 }
4150
4151 rc = vmsvga3dGetD3DSurface(pState, pContext, pRenderTarget, target.face, target.mipmap, false, &pSurface);
4152 AssertRCReturn(rc, rc);
4153 }
4154 else
4155 {
4156 AssertReturn(target.face == 0 && target.mipmap == 0, VERR_INVALID_PARAMETER);
4157 if (!pRenderTarget->u.pSurface)
4158 {
4159 DWORD cQualityLevels = 0;
4160
4161 /* Query the nr of quality levels for this particular format */
4162 if (pRenderTarget->multiSampleTypeD3D != D3DMULTISAMPLE_NONE)
4163 {
4164 hr = pState->pD3D9->CheckDeviceMultiSampleType(D3DADAPTER_DEFAULT,
4165 D3DDEVTYPE_HAL,
4166 pRenderTarget->formatD3D,
4167 TRUE, /* Windowed */
4168 pRenderTarget->multiSampleTypeD3D,
4169 &cQualityLevels);
4170 Assert(hr == D3D_OK || hr == D3DERR_NOTAVAILABLE);
4171 }
4172
4173 LogFunc(("COLOR; cQualityLevels=%d\n", cQualityLevels));
4174 LogFunc(("Create rendertarget (%d,%d) formatD3D=%x multisample=%x\n",
4175 pRenderTarget->paMipmapLevels[0].mipmapSize.width, pRenderTarget->paMipmapLevels[0].mipmapSize.height, pRenderTarget->formatD3D, pRenderTarget->multiSampleTypeD3D));
4176
4177 hr = pContext->pDevice->CreateRenderTarget(pRenderTarget->paMipmapLevels[0].mipmapSize.width,
4178 pRenderTarget->paMipmapLevels[0].mipmapSize.height,
4179 pRenderTarget->formatD3D,
4180 pRenderTarget->multiSampleTypeD3D,
4181 ((cQualityLevels >= 1) ? cQualityLevels - 1 : 0), /* 0 - (levels-1) */
4182 TRUE, /* lockable */
4183 &pRenderTarget->u.pSurface,
4184 NULL);
4185 AssertReturn(hr == D3D_OK, VERR_INTERNAL_ERROR);
4186
4187 pRenderTarget->idAssociatedContext = cid;
4188 pRenderTarget->enmD3DResType = VMSVGA3D_D3DRESTYPE_SURFACE;
4189 }
4190 else
4191 AssertReturn(pRenderTarget->fUsageD3D & D3DUSAGE_RENDERTARGET, VERR_INVALID_PARAMETER);
4192
4193 Assert(pRenderTarget->idAssociatedContext == cid);
4194 AssertMsgReturn(pRenderTarget->enmD3DResType == VMSVGA3D_D3DRESTYPE_SURFACE,
4195 ("Invalid render target %#x\n", pRenderTarget->enmD3DResType),
4196 VERR_INVALID_PARAMETER);
4197 pSurface = pRenderTarget->u.pSurface;
4198 }
4199
4200 AssertReturn(pSurface, VERR_INVALID_PARAMETER);
4201 Assert(!pRenderTarget->fDirty);
4202
4203 pRenderTarget->fUsageD3D |= D3DUSAGE_RENDERTARGET;
4204 pRenderTarget->surfaceFlags |= SVGA3D_SURFACE_HINT_RENDERTARGET;
4205
4206 hr = pContext->pDevice->SetRenderTarget(type - SVGA3D_RT_COLOR0, pSurface);
4207 if (fTexture)
4208 D3D_RELEASE(pSurface); /* Release reference to texture level 0 */
4209 AssertMsgReturn(hr == D3D_OK, ("SetRenderTarget failed with %x\n", hr), VERR_INTERNAL_ERROR);
4210
4211 /* Changing the render target resets the viewport; restore it here. */
4212 if (pContext->state.u32UpdateFlags & VMSVGA3D_UPDATE_VIEWPORT)
4213 vmsvga3dSetViewPort(pThisCC, cid, &pContext->state.RectViewPort);
4214 if (pContext->state.u32UpdateFlags & VMSVGA3D_UPDATE_ZRANGE)
4215 vmsvga3dSetZRange(pThisCC, cid, pContext->state.zRange);
4216 /* Changing the render target also resets the scissor rectangle; restore it as well. */
4217 if (pContext->state.u32UpdateFlags & VMSVGA3D_UPDATE_SCISSORRECT)
4218 vmsvga3dSetScissorRect(pThisCC, cid, &pContext->state.RectScissor);
4219
4220 break;
4221 }
4222
4223 default:
4224 AssertFailedReturn(VERR_INVALID_PARAMETER);
4225 }
4226
4227 return VINF_SUCCESS;
4228}
4229
4230/**
4231 * Convert SVGA texture combiner value to its D3D equivalent
4232 */
4233static DWORD vmsvga3dTextureCombiner2D3D(uint32_t value)
4234{
4235 switch (value)
4236 {
4237 case SVGA3D_TC_DISABLE:
4238 return D3DTOP_DISABLE;
4239 case SVGA3D_TC_SELECTARG1:
4240 return D3DTOP_SELECTARG1;
4241 case SVGA3D_TC_SELECTARG2:
4242 return D3DTOP_SELECTARG2;
4243 case SVGA3D_TC_MODULATE:
4244 return D3DTOP_MODULATE;
4245 case SVGA3D_TC_ADD:
4246 return D3DTOP_ADD;
4247 case SVGA3D_TC_ADDSIGNED:
4248 return D3DTOP_ADDSIGNED;
4249 case SVGA3D_TC_SUBTRACT:
4250 return D3DTOP_SUBTRACT;
4251 case SVGA3D_TC_BLENDTEXTUREALPHA:
4252 return D3DTOP_BLENDTEXTUREALPHA;
4253 case SVGA3D_TC_BLENDDIFFUSEALPHA:
4254 return D3DTOP_BLENDDIFFUSEALPHA;
4255 case SVGA3D_TC_BLENDCURRENTALPHA:
4256 return D3DTOP_BLENDCURRENTALPHA;
4257 case SVGA3D_TC_BLENDFACTORALPHA:
4258 return D3DTOP_BLENDFACTORALPHA;
4259 case SVGA3D_TC_MODULATE2X:
4260 return D3DTOP_MODULATE2X;
4261 case SVGA3D_TC_MODULATE4X:
4262 return D3DTOP_MODULATE4X;
4263 case SVGA3D_TC_DSDT:
4264 AssertFailed(); /** @todo ??? */
4265 return D3DTOP_DISABLE;
4266 case SVGA3D_TC_DOTPRODUCT3:
4267 return D3DTOP_DOTPRODUCT3;
4268 case SVGA3D_TC_BLENDTEXTUREALPHAPM:
4269 return D3DTOP_BLENDTEXTUREALPHAPM;
4270 case SVGA3D_TC_ADDSIGNED2X:
4271 return D3DTOP_ADDSIGNED2X;
4272 case SVGA3D_TC_ADDSMOOTH:
4273 return D3DTOP_ADDSMOOTH;
4274 case SVGA3D_TC_PREMODULATE:
4275 return D3DTOP_PREMODULATE;
4276 case SVGA3D_TC_MODULATEALPHA_ADDCOLOR:
4277 return D3DTOP_MODULATEALPHA_ADDCOLOR;
4278 case SVGA3D_TC_MODULATECOLOR_ADDALPHA:
4279 return D3DTOP_MODULATECOLOR_ADDALPHA;
4280 case SVGA3D_TC_MODULATEINVALPHA_ADDCOLOR:
4281 return D3DTOP_MODULATEINVALPHA_ADDCOLOR;
4282 case SVGA3D_TC_MODULATEINVCOLOR_ADDALPHA:
4283 return D3DTOP_MODULATEINVCOLOR_ADDALPHA;
4284 case SVGA3D_TC_BUMPENVMAPLUMINANCE:
4285 return D3DTOP_BUMPENVMAPLUMINANCE;
4286 case SVGA3D_TC_MULTIPLYADD:
4287 return D3DTOP_MULTIPLYADD;
4288 case SVGA3D_TC_LERP:
4289 return D3DTOP_LERP;
4290 default:
4291 AssertFailed();
4292 return D3DTOP_DISABLE;
4293 }
4294}
4295
4296/**
4297 * Convert SVGA texture arg data value to its D3D equivalent
4298 */
4299static DWORD vmsvga3dTextureArgData2D3D(uint32_t value)
4300{
4301 switch (value)
4302 {
4303 case SVGA3D_TA_CONSTANT:
4304 return D3DTA_CONSTANT;
4305 case SVGA3D_TA_PREVIOUS:
4306 return D3DTA_CURRENT; /* current = previous */
4307 case SVGA3D_TA_DIFFUSE:
4308 return D3DTA_DIFFUSE;
4309 case SVGA3D_TA_TEXTURE:
4310 return D3DTA_TEXTURE;
4311 case SVGA3D_TA_SPECULAR:
4312 return D3DTA_SPECULAR;
4313 default:
4314 AssertFailed();
4315 return D3DTA_DIFFUSE;
4316 }
4317}
4318
4319/**
4320 * Convert SVGA texture transform flag value to its D3D equivalent
4321 */
4322static DWORD vmsvga3dTextTransformFlags2D3D(uint32_t value)
4323{
4324 switch (value)
4325 {
4326 case SVGA3D_TEX_TRANSFORM_OFF:
4327 return D3DTTFF_DISABLE;
4328 case SVGA3D_TEX_TRANSFORM_S:
4329 return D3DTTFF_COUNT1; /** @todo correct? */
4330 case SVGA3D_TEX_TRANSFORM_T:
4331 return D3DTTFF_COUNT2; /** @todo correct? */
4332 case SVGA3D_TEX_TRANSFORM_R:
4333 return D3DTTFF_COUNT3; /** @todo correct? */
4334 case SVGA3D_TEX_TRANSFORM_Q:
4335 return D3DTTFF_COUNT4; /** @todo correct? */
4336 case SVGA3D_TEX_PROJECTED:
4337 return D3DTTFF_PROJECTED;
4338 default:
4339 AssertFailed();
4340 return D3DTTFF_DISABLE;
4341 }
4342}
4343
4344static DWORD vmsvga3dSamplerIndex2D3D(uint32_t idxSampler)
4345{
4346 if (idxSampler < SVGA3D_MAX_SAMPLERS_PS)
4347 return idxSampler;
4348 return (idxSampler - SVGA3D_MAX_SAMPLERS_PS) + D3DDMAPSAMPLER;
4349}
4350
4351int vmsvga3dSetTextureState(PVGASTATECC pThisCC, uint32_t cid, uint32_t cTextureStates, SVGA3dTextureState *pTextureState)
4352{
4353 DWORD val = 0; /* Shut up MSC */
4354 HRESULT hr;
4355 PVMSVGA3DCONTEXT pContext;
4356 PVMSVGA3DSTATE pState = pThisCC->svga.p3dState;
4357 AssertReturn(pState, VERR_NO_MEMORY);
4358
4359 LogFunc(("%x cTextureState=%d\n", cid, cTextureStates));
4360
4361 int rc = vmsvga3dContextFromCid(pState, cid, &pContext);
4362 AssertRCReturn(rc, rc);
4363 AssertReturn(pContext->pDevice, VERR_INTERNAL_ERROR);
4364
4365 for (unsigned i = 0; i < cTextureStates; i++)
4366 {
4367 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));
4368
4369 if (pTextureState[i].name == SVGA3D_TS_BIND_TEXTURE)
4370 {
4371 /* Special case: binding a texture to a sampler. Stage is the sampler index. */
4372 const uint32_t sid = pTextureState[i].value;
4373 const uint32_t idxSampler = pTextureState[i].stage;
4374
4375 if (RT_UNLIKELY(idxSampler >= SVGA3D_MAX_SAMPLERS))
4376 {
4377 AssertMsgFailed(("pTextureState[%d]: SVGA3D_TS_BIND_TEXTURE idxSampler=%d, sid=%u\n", i, idxSampler, sid));
4378 continue;
4379 }
4380
4381 const DWORD d3dSampler = vmsvga3dSamplerIndex2D3D(idxSampler);
4382 if (sid == SVGA3D_INVALID_ID)
4383 {
4384 LogFunc(("SVGA3D_TS_BIND_TEXTURE: unbind sampler=%d\n", idxSampler));
4385
4386 pContext->aSidActiveTextures[idxSampler] = SVGA3D_INVALID_ID;
4387
4388 /* Unselect the currently associated texture. */
4389 hr = pContext->pDevice->SetTexture(d3dSampler, NULL);
4390 AssertMsgReturn(hr == D3D_OK, ("SetTexture failed with %x\n", hr), VERR_INTERNAL_ERROR);
4391 }
4392 else
4393 {
4394 PVMSVGA3DSURFACE pSurface;
4395 rc = vmsvga3dSurfaceFromSid(pState, sid, &pSurface);
4396 AssertRCReturn(rc, rc);
4397
4398 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));
4399
4400 if (!pSurface->u.pTexture)
4401 {
4402 Assert(pSurface->idAssociatedContext == SVGA3D_INVALID_ID);
4403 LogFunc(("CreateTexture (%d,%d) level=%d fUsage=%x format=%x\n", pSurface->paMipmapLevels[0].mipmapSize.width, pSurface->paMipmapLevels[0].mipmapSize.height, pSurface->faces[0].numMipLevels, pSurface->fUsageD3D, pSurface->formatD3D));
4404 rc = vmsvga3dBackCreateTexture(pState, pContext, cid, pSurface);
4405 AssertRCReturn(rc, rc);
4406 }
4407 else
4408 {
4409 /* Must flush the other context's 3d pipeline to make sure all drawing is complete for the surface we're about to use. */
4410 vmsvga3dSurfaceFlush(pSurface);
4411 }
4412
4413 AssertReturn( pSurface->enmD3DResType == VMSVGA3D_D3DRESTYPE_TEXTURE
4414 || pSurface->enmD3DResType == VMSVGA3D_D3DRESTYPE_CUBE_TEXTURE
4415 || pSurface->enmD3DResType == VMSVGA3D_D3DRESTYPE_VOLUME_TEXTURE,
4416 VERR_INVALID_PARAMETER);
4417
4418#ifndef VBOX_VMSVGA3D_WITH_WINE_OPENGL
4419 if (pSurface->idAssociatedContext != cid)
4420 {
4421 LogFunc(("Using texture sid=%u created for another context (%d vs %d)\n", sid, pSurface->idAssociatedContext, cid));
4422
4423 PVMSVGA3DSHAREDSURFACE pSharedSurface = vmsvga3dSurfaceGetSharedCopy(pState, pContext, pSurface);
4424 AssertReturn(pSharedSurface, VERR_INTERNAL_ERROR);
4425
4426 hr = pContext->pDevice->SetTexture(d3dSampler, pSharedSurface->u.pTexture);
4427 }
4428 else
4429#endif
4430 hr = pContext->pDevice->SetTexture(d3dSampler, pSurface->u.pTexture);
4431
4432 AssertMsgReturn(hr == D3D_OK, ("SetTexture failed with %x\n", hr), VERR_INTERNAL_ERROR);
4433
4434 pContext->aSidActiveTextures[idxSampler] = sid;
4435 }
4436 /* Finished; continue with the next one. */
4437 continue;
4438 }
4439
4440 D3DTEXTURESTAGESTATETYPE textureType = D3DTSS_FORCE_DWORD;
4441 D3DSAMPLERSTATETYPE samplerType = D3DSAMP_FORCE_DWORD;
4442 switch (pTextureState[i].name)
4443 {
4444 case SVGA3D_TS_COLOROP: /* SVGA3dTextureCombiner */
4445 textureType = D3DTSS_COLOROP;
4446 val = vmsvga3dTextureCombiner2D3D(pTextureState[i].value);
4447 break;
4448
4449 case SVGA3D_TS_COLORARG0: /* SVGA3dTextureArgData */
4450 textureType = D3DTSS_COLORARG0;
4451 val = vmsvga3dTextureArgData2D3D(pTextureState[i].value);
4452 break;
4453
4454 case SVGA3D_TS_COLORARG1: /* SVGA3dTextureArgData */
4455 textureType = D3DTSS_COLORARG1;
4456 val = vmsvga3dTextureArgData2D3D(pTextureState[i].value);
4457 break;
4458
4459 case SVGA3D_TS_COLORARG2: /* SVGA3dTextureArgData */
4460 textureType = D3DTSS_COLORARG2;
4461 val = vmsvga3dTextureArgData2D3D(pTextureState[i].value);
4462 break;
4463
4464 case SVGA3D_TS_ALPHAOP: /* SVGA3dTextureCombiner */
4465 textureType = D3DTSS_ALPHAOP;
4466 val = vmsvga3dTextureCombiner2D3D(pTextureState[i].value);
4467 break;
4468
4469 case SVGA3D_TS_ALPHAARG0: /* SVGA3dTextureArgData */
4470 textureType = D3DTSS_ALPHAARG0;
4471 val = vmsvga3dTextureArgData2D3D(pTextureState[i].value);
4472 break;
4473
4474 case SVGA3D_TS_ALPHAARG1: /* SVGA3dTextureArgData */
4475 textureType = D3DTSS_ALPHAARG1;
4476 val = vmsvga3dTextureArgData2D3D(pTextureState[i].value);
4477 break;
4478
4479 case SVGA3D_TS_ALPHAARG2: /* SVGA3dTextureArgData */
4480 textureType = D3DTSS_ALPHAARG2;
4481 val = vmsvga3dTextureArgData2D3D(pTextureState[i].value);
4482 break;
4483
4484 case SVGA3D_TS_BUMPENVMAT00: /* float */
4485 textureType = D3DTSS_BUMPENVMAT00;
4486 val = pTextureState[i].value;
4487 break;
4488
4489 case SVGA3D_TS_BUMPENVMAT01: /* float */
4490 textureType = D3DTSS_BUMPENVMAT01;
4491 val = pTextureState[i].value;
4492 break;
4493
4494 case SVGA3D_TS_BUMPENVMAT10: /* float */
4495 textureType = D3DTSS_BUMPENVMAT10;
4496 val = pTextureState[i].value;
4497 break;
4498
4499 case SVGA3D_TS_BUMPENVMAT11: /* float */
4500 textureType = D3DTSS_BUMPENVMAT11;
4501 val = pTextureState[i].value;
4502 break;
4503
4504 case SVGA3D_TS_TEXCOORDINDEX: /* uint32_t */
4505 textureType = D3DTSS_TEXCOORDINDEX;
4506 val = pTextureState[i].value;
4507 break;
4508
4509 case SVGA3D_TS_BUMPENVLSCALE: /* float */
4510 textureType = D3DTSS_BUMPENVLSCALE;
4511 val = pTextureState[i].value;
4512 break;
4513
4514 case SVGA3D_TS_BUMPENVLOFFSET: /* float */
4515 textureType = D3DTSS_BUMPENVLOFFSET;
4516 val = pTextureState[i].value;
4517 break;
4518
4519 case SVGA3D_TS_TEXTURETRANSFORMFLAGS: /* SVGA3dTexTransformFlags */
4520 textureType = D3DTSS_TEXTURETRANSFORMFLAGS;
4521 val = vmsvga3dTextTransformFlags2D3D(pTextureState[i].value);
4522 break;
4523
4524 case SVGA3D_TS_ADDRESSW: /* SVGA3dTextureAddress */
4525 samplerType = D3DSAMP_ADDRESSW;
4526 val = pTextureState[i].value; /* Identical otherwise */
4527 Assert(pTextureState[i].value != SVGA3D_TEX_ADDRESS_EDGE);
4528 break;
4529
4530 case SVGA3D_TS_ADDRESSU: /* SVGA3dTextureAddress */
4531 samplerType = D3DSAMP_ADDRESSU;
4532 val = pTextureState[i].value; /* Identical otherwise */
4533 Assert(pTextureState[i].value != SVGA3D_TEX_ADDRESS_EDGE);
4534 break;
4535
4536 case SVGA3D_TS_ADDRESSV: /* SVGA3dTextureAddress */
4537 samplerType = D3DSAMP_ADDRESSV;
4538 val = pTextureState[i].value; /* Identical otherwise */
4539 Assert(pTextureState[i].value != SVGA3D_TEX_ADDRESS_EDGE);
4540 break;
4541
4542 case SVGA3D_TS_MIPFILTER: /* SVGA3dTextureFilter */
4543 samplerType = D3DSAMP_MIPFILTER;
4544 val = pTextureState[i].value; /* Identical otherwise */
4545 Assert(pTextureState[i].value != SVGA3D_TEX_FILTER_FLATCUBIC);
4546 Assert(pTextureState[i].value != SVGA3D_TEX_FILTER_GAUSSIANCUBIC);
4547 break;
4548
4549 case SVGA3D_TS_MAGFILTER: /* SVGA3dTextureFilter */
4550 samplerType = D3DSAMP_MAGFILTER;
4551 val = pTextureState[i].value; /* Identical otherwise */
4552 Assert(pTextureState[i].value != SVGA3D_TEX_FILTER_FLATCUBIC);
4553 Assert(pTextureState[i].value != SVGA3D_TEX_FILTER_GAUSSIANCUBIC);
4554 break;
4555
4556 case SVGA3D_TS_MINFILTER: /* SVGA3dTextureFilter */
4557 samplerType = D3DSAMP_MINFILTER;
4558 val = pTextureState[i].value; /* Identical otherwise */
4559 Assert(pTextureState[i].value != SVGA3D_TEX_FILTER_FLATCUBIC);
4560 Assert(pTextureState[i].value != SVGA3D_TEX_FILTER_GAUSSIANCUBIC);
4561 break;
4562
4563 case SVGA3D_TS_BORDERCOLOR: /* SVGA3dColor */
4564 samplerType = D3DSAMP_BORDERCOLOR;
4565 val = pTextureState[i].value; /* Identical */
4566 break;
4567
4568 case SVGA3D_TS_TEXTURE_LOD_BIAS: /* float */
4569 samplerType = D3DSAMP_MIPMAPLODBIAS;
4570 val = pTextureState[i].value; /* Identical */
4571 break;
4572
4573 case SVGA3D_TS_TEXTURE_MIPMAP_LEVEL: /* uint32_t */
4574 samplerType = D3DSAMP_MAXMIPLEVEL;
4575 val = pTextureState[i].value; /* Identical?? */
4576 break;
4577
4578 case SVGA3D_TS_TEXTURE_ANISOTROPIC_LEVEL: /* uint32_t */
4579 samplerType = D3DSAMP_MAXANISOTROPY;
4580 val = pTextureState[i].value; /* Identical?? */
4581 break;
4582
4583 case SVGA3D_TS_GAMMA: /* float */
4584 samplerType = D3DSAMP_SRGBTEXTURE;
4585 /* Boolean in D3D */
4586 if (pTextureState[i].floatValue == 1.0f)
4587 val = FALSE;
4588 else
4589 val = TRUE;
4590 break;
4591
4592 /* Internal commands, that don't map directly to the SetTextureStageState API. */
4593 case SVGA3D_TS_TEXCOORDGEN: /* SVGA3dTextureCoordGen */
4594 AssertFailed();
4595 break;
4596
4597 case SVGA3D_TS_MAX: /* shut up MSC */
4598 case SVGA3D_TS_INVALID:
4599 case SVGA3D_TS_BIND_TEXTURE:
4600 AssertFailedBreak();
4601 default: /** @todo Remaining TSs. Avoid MSC warning for now. */
4602 break;
4603 }
4604
4605 const uint32_t currentStage = pTextureState[i].stage;
4606 /* Record the texture state for vm state saving. */
4607 if ( currentStage < RT_ELEMENTS(pContext->state.aTextureStates)
4608 && (unsigned)pTextureState[i].name < RT_ELEMENTS(pContext->state.aTextureStates[0]))
4609 {
4610 pContext->state.aTextureStates[currentStage][pTextureState[i].name] = pTextureState[i];
4611 }
4612
4613 if (textureType != D3DTSS_FORCE_DWORD)
4614 {
4615 if (RT_UNLIKELY(currentStage >= SVGA3D_MAX_TEXTURE_STAGES))
4616 {
4617 AssertMsgFailed(("pTextureState[%d].stage=%#x name=%#x value=%#x\n", i, pTextureState[i].stage, pTextureState[i].name, pTextureState[i].value));
4618 continue;
4619 }
4620
4621 hr = pContext->pDevice->SetTextureStageState(currentStage, textureType, val);
4622 AssertMsg(hr == D3D_OK, ("SetTextureStageState failed with %x\n", hr));
4623 }
4624 else if (samplerType != D3DSAMP_FORCE_DWORD)
4625 {
4626 if (RT_UNLIKELY(currentStage >= SVGA3D_MAX_SAMPLERS))
4627 {
4628 AssertMsgFailed(("pTextureState[%d].stage=%#x name=%#x value=%#x\n", i, pTextureState[i].stage, pTextureState[i].name, pTextureState[i].value));
4629 continue;
4630 }
4631
4632 hr = pContext->pDevice->SetSamplerState(currentStage, samplerType, val);
4633 AssertMsg(hr == D3D_OK, ("SetSamplerState failed with %x\n", hr));
4634 }
4635 else
4636 {
4637 AssertFailed();
4638 }
4639 }
4640
4641 return VINF_SUCCESS;
4642}
4643
4644int vmsvga3dSetMaterial(PVGASTATECC pThisCC, uint32_t cid, SVGA3dFace face, SVGA3dMaterial *pMaterial)
4645{
4646 HRESULT hr;
4647 PVMSVGA3DSTATE pState = pThisCC->svga.p3dState;
4648 AssertReturn(pState, VERR_NO_MEMORY);
4649
4650 LogFunc(("cid=%u face %d\n", cid, face));
4651
4652 PVMSVGA3DCONTEXT pContext;
4653 int rc = vmsvga3dContextFromCid(pState, cid, &pContext);
4654 AssertRCReturn(rc, rc);
4655 AssertReturn(pContext->pDevice, VERR_INTERNAL_ERROR);
4656
4657 AssertReturn((unsigned)face < SVGA3D_FACE_MAX, VERR_INVALID_PARAMETER);
4658
4659 /* Save for vm state save/restore. */
4660 pContext->state.aMaterial[face].fValid = true;
4661 pContext->state.aMaterial[face].material = *pMaterial;
4662 pContext->state.u32UpdateFlags |= VMSVGA3D_UPDATE_MATERIAL;
4663
4664 /* @note face not used for D3D9 */
4665 /** @todo ignore everything except SVGA3D_FACE_NONE? */
4666 //Assert(face == SVGA3D_FACE_NONE);
4667 if (face != SVGA3D_FACE_NONE)
4668 Log(("Unsupported face %d!!\n", face));
4669
4670 D3DMATERIAL9 material;
4671 material.Diffuse.r = pMaterial->diffuse[0];
4672 material.Diffuse.g = pMaterial->diffuse[1];
4673 material.Diffuse.b = pMaterial->diffuse[2];
4674 material.Diffuse.a = pMaterial->diffuse[3];
4675 material.Ambient.r = pMaterial->ambient[0];
4676 material.Ambient.g = pMaterial->ambient[1];
4677 material.Ambient.b = pMaterial->ambient[2];
4678 material.Ambient.a = pMaterial->ambient[3];
4679 material.Specular.r = pMaterial->specular[0];
4680 material.Specular.g = pMaterial->specular[1];
4681 material.Specular.b = pMaterial->specular[2];
4682 material.Specular.a = pMaterial->specular[3];
4683 material.Emissive.r = pMaterial->emissive[0];
4684 material.Emissive.g = pMaterial->emissive[1];
4685 material.Emissive.b = pMaterial->emissive[2];
4686 material.Emissive.a = pMaterial->emissive[3];
4687 material.Power = pMaterial->shininess;
4688
4689 hr = pContext->pDevice->SetMaterial(&material);
4690 AssertMsgReturn(hr == D3D_OK, ("SetMaterial failed with %x\n", hr), VERR_INTERNAL_ERROR);
4691
4692 return VINF_SUCCESS;
4693}
4694
4695int vmsvga3dSetLightData(PVGASTATECC pThisCC, uint32_t cid, uint32_t index, SVGA3dLightData *pData)
4696{
4697 HRESULT hr;
4698 D3DLIGHT9 light;
4699 PVMSVGA3DCONTEXT pContext;
4700 PVMSVGA3DSTATE pState = pThisCC->svga.p3dState;
4701 AssertReturn(pState, VERR_NO_MEMORY);
4702
4703 Log(("vmsvga3dSetLightData %x index=%d\n", cid, index));
4704 ASSERT_GUEST_RETURN(index < SVGA3D_MAX_LIGHTS, VERR_INVALID_PARAMETER);
4705
4706 int rc = vmsvga3dContextFromCid(pState, cid, &pContext);
4707 AssertRCReturn(rc, rc);
4708 AssertReturn(pContext->pDevice, VERR_INTERNAL_ERROR);
4709
4710 switch (pData->type)
4711 {
4712 case SVGA3D_LIGHTTYPE_POINT:
4713 light.Type = D3DLIGHT_POINT;
4714 break;
4715
4716 case SVGA3D_LIGHTTYPE_SPOT1: /* 1-cone, in degrees */
4717 light.Type = D3DLIGHT_SPOT;
4718 break;
4719
4720 case SVGA3D_LIGHTTYPE_DIRECTIONAL:
4721 light.Type = D3DLIGHT_DIRECTIONAL;
4722 break;
4723
4724 case SVGA3D_LIGHTTYPE_SPOT2: /* 2-cone, in radians */
4725 default:
4726 Log(("Unsupported light type!!\n"));
4727 return VERR_INVALID_PARAMETER;
4728 }
4729
4730 /* Store for vm state save/restore */
4731 pContext->state.aLightData[index].fValidData = true;
4732 pContext->state.aLightData[index].data = *pData;
4733
4734 light.Diffuse.r = pData->diffuse[0];
4735 light.Diffuse.g = pData->diffuse[1];
4736 light.Diffuse.b = pData->diffuse[2];
4737 light.Diffuse.a = pData->diffuse[3];
4738 light.Specular.r = pData->specular[0];
4739 light.Specular.g = pData->specular[1];
4740 light.Specular.b = pData->specular[2];
4741 light.Specular.a = pData->specular[3];
4742 light.Ambient.r = pData->ambient[0];
4743 light.Ambient.g = pData->ambient[1];
4744 light.Ambient.b = pData->ambient[2];
4745 light.Ambient.a = pData->ambient[3];
4746 light.Position.x = pData->position[0];
4747 light.Position.y = pData->position[1];
4748 light.Position.z = pData->position[2]; /* @note 4th position not available in D3D9 */
4749 light.Direction.x = pData->direction[0];
4750 light.Direction.y = pData->direction[1];
4751 light.Direction.z = pData->direction[2]; /* @note 4th position not available in D3D9 */
4752 light.Range = pData->range;
4753 light.Falloff = pData->falloff;
4754 light.Attenuation0 = pData->attenuation0;
4755 light.Attenuation1 = pData->attenuation1;
4756 light.Attenuation2 = pData->attenuation2;
4757 light.Theta = pData->theta;
4758 light.Phi = pData->phi;
4759
4760 hr = pContext->pDevice->SetLight(index, &light);
4761 AssertMsgReturn(hr == D3D_OK, ("SetLight failed with %x\n", hr), VERR_INTERNAL_ERROR);
4762
4763 return VINF_SUCCESS;
4764}
4765
4766int vmsvga3dSetLightEnabled(PVGASTATECC pThisCC, uint32_t cid, uint32_t index, uint32_t enabled)
4767{
4768 HRESULT hr;
4769 PVMSVGA3DCONTEXT pContext;
4770 PVMSVGA3DSTATE pState = pThisCC->svga.p3dState;
4771 AssertReturn(pState, VERR_NO_MEMORY);
4772
4773 Log(("vmsvga3dSetLightEnabled %x %d -> %d\n", cid, index, enabled));
4774 AssertReturn(index < SVGA3D_MAX_LIGHTS, VERR_INVALID_PARAMETER);
4775
4776 int rc = vmsvga3dContextFromCid(pState, cid, &pContext);
4777 AssertRCReturn(rc, rc);
4778 AssertReturn(pContext->pDevice, VERR_INTERNAL_ERROR);
4779
4780 /* Store for vm state save/restore */
4781 pContext->state.aLightData[index].fEnabled = !!enabled;
4782
4783 hr = pContext->pDevice->LightEnable(index, (BOOL)enabled);
4784 AssertMsgReturn(hr == D3D_OK, ("LightEnable failed with %x\n", hr), VERR_INTERNAL_ERROR);
4785
4786 return VINF_SUCCESS;
4787}
4788
4789int vmsvga3dSetViewPort(PVGASTATECC pThisCC, uint32_t cid, SVGA3dRect *pRect)
4790{
4791 HRESULT hr;
4792 D3DVIEWPORT9 viewPort;
4793 PVMSVGA3DCONTEXT pContext;
4794 PVMSVGA3DSTATE pState = pThisCC->svga.p3dState;
4795 AssertReturn(pState, VERR_NO_MEMORY);
4796
4797 Log(("vmsvga3dSetViewPort %x (%d,%d)(%d,%d)\n", cid, pRect->x, pRect->y, pRect->w, pRect->h));
4798
4799 int rc = vmsvga3dContextFromCid(pState, cid, &pContext);
4800 AssertRCReturn(rc, rc);
4801 AssertReturn(pContext->pDevice, VERR_INTERNAL_ERROR);
4802
4803 /* Save for vm state save/restore. */
4804 pContext->state.RectViewPort = *pRect;
4805 pContext->state.u32UpdateFlags |= VMSVGA3D_UPDATE_VIEWPORT;
4806
4807 hr = pContext->pDevice->GetViewport(&viewPort);
4808 AssertMsgReturn(hr == D3D_OK, ("GetViewport failed with %x\n", hr), VERR_INTERNAL_ERROR);
4809
4810 viewPort.X = pRect->x;
4811 viewPort.Y = pRect->y;
4812 viewPort.Width = pRect->w;
4813 viewPort.Height = pRect->h;
4814 /* viewPort.MinZ & MaxZ are not changed from the current setting. */
4815
4816 hr = pContext->pDevice->SetViewport(&viewPort);
4817 AssertMsgReturn(hr == D3D_OK, ("SetViewport failed with %x\n", hr), VERR_INTERNAL_ERROR);
4818
4819 return VINF_SUCCESS;
4820}
4821
4822int vmsvga3dSetClipPlane(PVGASTATECC pThisCC, uint32_t cid, uint32_t index, float plane[4])
4823{
4824 HRESULT hr;
4825 PVMSVGA3DCONTEXT pContext;
4826 PVMSVGA3DSTATE pState = pThisCC->svga.p3dState;
4827 AssertReturn(pState, VERR_NO_MEMORY);
4828
4829 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)));
4830 AssertReturn(index < SVGA3D_NUM_CLIPPLANES, VERR_INVALID_PARAMETER);
4831
4832 int rc = vmsvga3dContextFromCid(pState, cid, &pContext);
4833 AssertRCReturn(rc, rc);
4834 AssertReturn(pContext->pDevice, VERR_INTERNAL_ERROR);
4835
4836 /* Store for vm state save/restore. */
4837 pContext->state.aClipPlane[index].fValid = true;
4838 memcpy(pContext->state.aClipPlane[index].plane, plane, sizeof(pContext->state.aClipPlane[index].plane));
4839
4840 hr = pContext->pDevice->SetClipPlane(index, plane);
4841 AssertMsgReturn(hr == D3D_OK, ("SetClipPlane failed with %x\n", hr), VERR_INTERNAL_ERROR);
4842 return VINF_SUCCESS;
4843}
4844
4845int vmsvga3dCommandClear(PVGASTATECC pThisCC, uint32_t cid, SVGA3dClearFlag clearFlag, uint32_t color, float depth,
4846 uint32_t stencil, uint32_t cRects, SVGA3dRect *pRect)
4847{
4848 /* From SVGA3D_BeginClear comments:
4849 *
4850 * Clear is not affected by clipping, depth test, or other
4851 * render state which affects the fragment pipeline.
4852 *
4853 * Therefore this code must ignore the current scissor rect.
4854 */
4855
4856 DWORD clearFlagD3D = 0;
4857 D3DRECT *pRectD3D = NULL;
4858 HRESULT hr;
4859 PVMSVGA3DCONTEXT pContext;
4860 PVMSVGA3DSTATE pState = pThisCC->svga.p3dState;
4861 AssertReturn(pState, VERR_NO_MEMORY);
4862
4863 Log(("vmsvga3dCommandClear %x clearFlag=%x color=%x depth=%d stencil=%x cRects=%d\n", cid, clearFlag, color, (uint32_t)(depth * 100.0), stencil, cRects));
4864
4865 int rc = vmsvga3dContextFromCid(pState, cid, &pContext);
4866 AssertRCReturn(rc, rc);
4867 AssertReturn(pContext->pDevice, VERR_INTERNAL_ERROR);
4868
4869 PVMSVGA3DSURFACE pRT;
4870 rc = vmsvga3dSurfaceFromSid(pState, pContext->state.aRenderTargets[SVGA3D_RT_COLOR0], &pRT);
4871 AssertRCReturn(rc, rc);
4872
4873 if (clearFlag & SVGA3D_CLEAR_COLOR)
4874 clearFlagD3D |= D3DCLEAR_TARGET;
4875 if (clearFlag & SVGA3D_CLEAR_STENCIL)
4876 clearFlagD3D |= D3DCLEAR_STENCIL;
4877 if (clearFlag & SVGA3D_CLEAR_DEPTH)
4878 clearFlagD3D |= D3DCLEAR_ZBUFFER;
4879
4880 if (cRects)
4881 {
4882 pRectD3D = (D3DRECT *)RTMemAlloc(sizeof(D3DRECT) * cRects);
4883 AssertReturn(pRectD3D, VERR_NO_MEMORY);
4884
4885 for (unsigned i=0; i < cRects; i++)
4886 {
4887 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));
4888 pRectD3D[i].x1 = pRect[i].x;
4889 pRectD3D[i].y1 = pRect[i].y;
4890 pRectD3D[i].x2 = pRect[i].x + pRect[i].w; /* exclusive */
4891 pRectD3D[i].y2 = pRect[i].y + pRect[i].h; /* exclusive */
4892 }
4893 }
4894
4895 RECT currentScissorRect;
4896 pContext->pDevice->GetScissorRect(&currentScissorRect);
4897
4898 RECT clearScissorRect;
4899 clearScissorRect.left = 0;
4900 clearScissorRect.top = 0;
4901 clearScissorRect.right = pRT->paMipmapLevels[0].mipmapSize.width;
4902 clearScissorRect.bottom = pRT->paMipmapLevels[0].mipmapSize.height;
4903 pContext->pDevice->SetScissorRect(&clearScissorRect);
4904
4905 hr = pContext->pDevice->Clear(cRects, pRectD3D, clearFlagD3D, (D3DCOLOR)color, depth, stencil);
4906
4907 if (pRectD3D)
4908 RTMemFree(pRectD3D);
4909
4910 pContext->pDevice->SetScissorRect(&currentScissorRect);
4911
4912 AssertMsgReturn(hr == D3D_OK, ("Clear failed with %x\n", hr), VERR_INTERNAL_ERROR);
4913
4914 /* Make sure we can track drawing usage of active render targets. */
4915 for (uint32_t i = 0; i < RT_ELEMENTS(pContext->state.aRenderTargets); ++i)
4916 if (pContext->state.aRenderTargets[i] != SVGA3D_INVALID_ID)
4917 vmsvga3dSurfaceTrackUsageById(pState, pContext, pContext->state.aRenderTargets[i]);
4918
4919 return VINF_SUCCESS;
4920}
4921
4922/* Convert VMWare vertex declaration to its D3D equivalent. */
4923static int vmsvga3dVertexDecl2D3D(const SVGA3dVertexArrayIdentity &identity, D3DVERTEXELEMENT9 *pVertexElement)
4924{
4925 /* usage, method and type are identical; make sure. */
4926 AssertCompile(SVGA3D_DECLTYPE_FLOAT1 == D3DDECLTYPE_FLOAT1);
4927 AssertCompile(SVGA3D_DECLTYPE_FLOAT16_4 == D3DDECLTYPE_FLOAT16_4);
4928 AssertCompile(SVGA3D_DECLMETHOD_DEFAULT == D3DDECLMETHOD_DEFAULT);
4929 AssertCompile(SVGA3D_DECLMETHOD_LOOKUPPRESAMPLED == D3DDECLMETHOD_LOOKUPPRESAMPLED);
4930 AssertCompile(D3DDECLUSAGE_POSITION == SVGA3D_DECLUSAGE_POSITION);
4931 AssertCompile(D3DDECLUSAGE_SAMPLE == SVGA3D_DECLUSAGE_SAMPLE);
4932
4933 pVertexElement->Stream = 0;
4934 pVertexElement->Offset = 0;
4935 pVertexElement->Type = identity.type;
4936 pVertexElement->Method = identity.method;
4937 pVertexElement->Usage = identity.usage;
4938 pVertexElement->UsageIndex = identity.usageIndex;
4939 return VINF_SUCCESS;
4940}
4941
4942/* Convert VMWare primitive type to its D3D equivalent. */
4943static int vmsvga3dPrimitiveType2D3D(SVGA3dPrimitiveType PrimitiveType, D3DPRIMITIVETYPE *pPrimitiveTypeD3D)
4944{
4945 switch (PrimitiveType)
4946 {
4947 case SVGA3D_PRIMITIVE_TRIANGLELIST:
4948 *pPrimitiveTypeD3D = D3DPT_TRIANGLELIST;
4949 break;
4950 case SVGA3D_PRIMITIVE_POINTLIST:
4951 *pPrimitiveTypeD3D = D3DPT_POINTLIST;
4952 break;
4953 case SVGA3D_PRIMITIVE_LINELIST:
4954 *pPrimitiveTypeD3D = D3DPT_LINELIST;
4955 break;
4956 case SVGA3D_PRIMITIVE_LINESTRIP:
4957 *pPrimitiveTypeD3D = D3DPT_LINESTRIP;
4958 break;
4959 case SVGA3D_PRIMITIVE_TRIANGLESTRIP:
4960 *pPrimitiveTypeD3D = D3DPT_TRIANGLESTRIP;
4961 break;
4962 case SVGA3D_PRIMITIVE_TRIANGLEFAN:
4963 *pPrimitiveTypeD3D = D3DPT_TRIANGLEFAN;
4964 break;
4965 default:
4966 return VERR_INVALID_PARAMETER;
4967 }
4968 return VINF_SUCCESS;
4969}
4970
4971
4972static int vmsvga3dDrawPrimitivesSyncVertexBuffer(PVMSVGA3DCONTEXT pContext,
4973 PVMSVGA3DSURFACE pVertexSurface)
4974{
4975 HRESULT hr;
4976 if ( pVertexSurface->u.pSurface
4977 && pVertexSurface->enmD3DResType != VMSVGA3D_D3DRESTYPE_VERTEX_BUFFER)
4978 {
4979 /* The buffer object is not an vertex one. Recreate the D3D resource. */
4980 AssertReturn(pVertexSurface->enmD3DResType == VMSVGA3D_D3DRESTYPE_INDEX_BUFFER, VERR_INVALID_PARAMETER);
4981 D3D_RELEASE(pVertexSurface->u.pIndexBuffer);
4982 pVertexSurface->enmD3DResType = VMSVGA3D_D3DRESTYPE_NONE;
4983
4984 LogFunc(("index -> vertex buffer sid=%u\n", pVertexSurface->id));
4985 }
4986
4987 bool fSync = pVertexSurface->fDirty;
4988 if (!pVertexSurface->u.pVertexBuffer)
4989 {
4990 LogFunc(("Create vertex buffer sid=%u fDirty=%d\n", pVertexSurface->id, pVertexSurface->fDirty));
4991
4992 const DWORD Usage = D3DUSAGE_DYNAMIC | D3DUSAGE_WRITEONLY; /* possible severe performance penalty otherwise (according to d3d debug output */
4993 hr = pContext->pDevice->CreateVertexBuffer(pVertexSurface->paMipmapLevels[0].cbSurface,
4994 Usage,
4995 0, /* non-FVF */
4996 D3DPOOL_DEFAULT,
4997 &pVertexSurface->u.pVertexBuffer,
4998 NULL);
4999 AssertMsgReturn(hr == D3D_OK, ("CreateVertexBuffer failed with %x\n", hr), VERR_INTERNAL_ERROR);
5000
5001 pVertexSurface->enmD3DResType = VMSVGA3D_D3DRESTYPE_VERTEX_BUFFER;
5002 pVertexSurface->idAssociatedContext = pContext->id;
5003 pVertexSurface->surfaceFlags |= SVGA3D_SURFACE_HINT_VERTEXBUFFER;
5004 fSync = true;
5005 }
5006
5007 if (fSync)
5008 {
5009 LogFunc(("sync vertex buffer\n"));
5010 Assert(pVertexSurface->u.pVertexBuffer);
5011
5012 void *pvData;
5013 hr = pVertexSurface->u.pVertexBuffer->Lock(0, 0, &pvData, D3DLOCK_DISCARD);
5014 AssertMsgReturn(hr == D3D_OK, ("Lock vertex failed with %x\n", hr), VERR_INTERNAL_ERROR);
5015
5016 memcpy(pvData, pVertexSurface->paMipmapLevels[0].pSurfaceData, pVertexSurface->paMipmapLevels[0].cbSurface);
5017
5018 hr = pVertexSurface->u.pVertexBuffer->Unlock();
5019 AssertMsgReturn(hr == D3D_OK, ("Unlock vertex failed with %x\n", hr), VERR_INTERNAL_ERROR);
5020 }
5021
5022 return VINF_SUCCESS;
5023}
5024
5025
5026static int vmsvga3dDrawPrimitivesSyncIndexBuffer(PVMSVGA3DCONTEXT pContext,
5027 PVMSVGA3DSURFACE pIndexSurface,
5028 uint32_t indexWidth)
5029{
5030 HRESULT hr;
5031 if ( pIndexSurface->u.pSurface
5032 && pIndexSurface->enmD3DResType != VMSVGA3D_D3DRESTYPE_INDEX_BUFFER)
5033 {
5034 /* The buffer object is not an index one. Must recreate the D3D resource. */
5035 AssertReturn(pIndexSurface->enmD3DResType == VMSVGA3D_D3DRESTYPE_VERTEX_BUFFER, VERR_INVALID_PARAMETER);
5036 D3D_RELEASE(pIndexSurface->u.pVertexBuffer);
5037 pIndexSurface->enmD3DResType = VMSVGA3D_D3DRESTYPE_NONE;
5038
5039 LogFunc(("vertex -> index buffer sid=%u\n", pIndexSurface->id));
5040 }
5041
5042 bool fSync = pIndexSurface->fDirty;
5043 if (!pIndexSurface->u.pIndexBuffer)
5044 {
5045 LogFunc(("Create index buffer fDirty=%d\n", pIndexSurface->fDirty));
5046
5047 const DWORD Usage = D3DUSAGE_DYNAMIC | D3DUSAGE_WRITEONLY; /* possible severe performance penalty otherwise (according to d3d debug output */
5048 const D3DFORMAT Format = (indexWidth == sizeof(uint16_t)) ? D3DFMT_INDEX16 : D3DFMT_INDEX32;
5049 hr = pContext->pDevice->CreateIndexBuffer(pIndexSurface->paMipmapLevels[0].cbSurface,
5050 Usage,
5051 Format,
5052 D3DPOOL_DEFAULT,
5053 &pIndexSurface->u.pIndexBuffer,
5054 NULL);
5055 AssertMsgReturn(hr == D3D_OK, ("CreateIndexBuffer failed with %x\n", hr), VERR_INTERNAL_ERROR);
5056
5057 pIndexSurface->enmD3DResType = VMSVGA3D_D3DRESTYPE_INDEX_BUFFER;
5058 pIndexSurface->idAssociatedContext = pContext->id;
5059 pIndexSurface->surfaceFlags |= SVGA3D_SURFACE_HINT_INDEXBUFFER;
5060 fSync = true;
5061 }
5062
5063 if (fSync)
5064 {
5065 LogFunc(("sync index buffer\n"));
5066 Assert(pIndexSurface->u.pIndexBuffer);
5067
5068 void *pvData;
5069 hr = pIndexSurface->u.pIndexBuffer->Lock(0, 0, &pvData, D3DLOCK_DISCARD);
5070 AssertMsgReturn(hr == D3D_OK, ("Lock index failed with %x\n", hr), VERR_INTERNAL_ERROR);
5071
5072 memcpy(pvData, pIndexSurface->paMipmapLevels[0].pSurfaceData, pIndexSurface->paMipmapLevels[0].cbSurface);
5073
5074 hr = pIndexSurface->u.pIndexBuffer->Unlock();
5075 AssertMsgReturn(hr == D3D_OK, ("Unlock index failed with %x\n", hr), VERR_INTERNAL_ERROR);
5076 }
5077
5078 return VINF_SUCCESS;
5079}
5080
5081static int vmsvga3dDrawPrimitivesProcessVertexDecls(const uint32_t numVertexDecls,
5082 const SVGA3dVertexDecl *pVertexDecl,
5083 const uint32_t idStream,
5084 const uint32_t uVertexMinOffset,
5085 const uint32_t uVertexMaxOffset,
5086 D3DVERTEXELEMENT9 *pVertexElement)
5087{
5088 RT_NOREF(uVertexMaxOffset); /* Logging only. */
5089 Assert(numVertexDecls);
5090
5091 /* Create a vertex declaration array */
5092 for (uint32_t iVertex = 0; iVertex < numVertexDecls; ++iVertex)
5093 {
5094 LogFunc(("vertex %d type=%s (%d) method=%s (%d) usage=%s (%d) usageIndex=%d stride=%d offset=%d (%d min=%d max=%d)\n",
5095 iVertex,
5096 vmsvgaDeclType2String(pVertexDecl[iVertex].identity.type), pVertexDecl[iVertex].identity.type,
5097 vmsvgaDeclMethod2String(pVertexDecl[iVertex].identity.method), pVertexDecl[iVertex].identity.method,
5098 vmsvgaDeclUsage2String(pVertexDecl[iVertex].identity.usage), pVertexDecl[iVertex].identity.usage,
5099 pVertexDecl[iVertex].identity.usageIndex,
5100 pVertexDecl[iVertex].array.stride,
5101 pVertexDecl[iVertex].array.offset - uVertexMinOffset,
5102 pVertexDecl[iVertex].array.offset,
5103 uVertexMinOffset, uVertexMaxOffset));
5104
5105 int rc = vmsvga3dVertexDecl2D3D(pVertexDecl[iVertex].identity, &pVertexElement[iVertex]);
5106 AssertRCReturn(rc, rc);
5107
5108 pVertexElement[iVertex].Stream = idStream;
5109 pVertexElement[iVertex].Offset = pVertexDecl[iVertex].array.offset - uVertexMinOffset;
5110
5111#ifdef LOG_ENABLED
5112 if (pVertexDecl[iVertex].array.stride == 0)
5113 LogFunc(("stride == 0! Can be valid\n"));
5114
5115 if (pVertexElement[iVertex].Offset >= pVertexDecl[0].array.stride)
5116 LogFunc(("WARNING: offset > stride!!\n"));
5117#endif
5118 }
5119
5120 return VINF_SUCCESS;
5121}
5122
5123int vmsvga3dDrawPrimitives(PVGASTATECC pThisCC, uint32_t cid, uint32_t numVertexDecls, SVGA3dVertexDecl *pVertexDecl,
5124 uint32_t numRanges, SVGA3dPrimitiveRange *pRange,
5125 uint32_t cVertexDivisor, SVGA3dVertexDivisor *pVertexDivisor)
5126{
5127 static const D3DVERTEXELEMENT9 sVertexEnd = D3DDECL_END();
5128
5129 PVMSVGA3DSTATE pState = pThisCC->svga.p3dState;
5130 AssertReturn(pState, VERR_INTERNAL_ERROR);
5131
5132 PVMSVGA3DCONTEXT pContext;
5133 int rc = vmsvga3dContextFromCid(pState, cid, &pContext);
5134 AssertRCReturn(rc, rc);
5135 AssertReturn(pContext->pDevice, VERR_INTERNAL_ERROR);
5136
5137 HRESULT hr;
5138
5139 /* SVGA driver may use the same surface for both index and vertex data. So we can not clear fDirty flag,
5140 * after updating a vertex buffer for example, because the same surface might be used for index buffer later.
5141 * So keep pointers to all used surfaces in the following two arrays and clear fDirty flag at the end.
5142 */
5143 PVMSVGA3DSURFACE aVertexSurfaces[SVGA3D_MAX_VERTEX_ARRAYS];
5144 PVMSVGA3DSURFACE aIndexSurfaces[SVGA3D_MAX_DRAW_PRIMITIVE_RANGES];
5145 RT_ZERO(aVertexSurfaces);
5146 RT_ZERO(aIndexSurfaces);
5147
5148 LogFunc(("cid=%u numVertexDecls=%d numRanges=%d, cVertexDivisor=%d\n", cid, numVertexDecls, numRanges, cVertexDivisor));
5149
5150 AssertReturn(numVertexDecls && numVertexDecls <= SVGA3D_MAX_VERTEX_ARRAYS, VERR_INVALID_PARAMETER);
5151 AssertReturn(numRanges && numRanges <= SVGA3D_MAX_DRAW_PRIMITIVE_RANGES, VERR_INVALID_PARAMETER);
5152 AssertReturn(!cVertexDivisor || cVertexDivisor == numVertexDecls, VERR_INVALID_PARAMETER);
5153
5154#if 0
5155 /* Dump render target to a bitmap. */
5156 if (pContext->state.aRenderTargets[SVGA3D_RT_COLOR0] != SVGA3D_INVALID_ID)
5157 {
5158 vmsvga3dUpdateHeapBuffersForSurfaces(pThisCC, pContext->state.aRenderTargets[SVGA3D_RT_COLOR0]);
5159 PVMSVGA3DSURFACE pSurface;
5160 int rc2 = vmsvga3dSurfaceFromSid(pState, pContext->state.aRenderTargets[SVGA3D_RT_COLOR0], &pSurface);
5161 if (RT_SUCCESS(rc2))
5162 vmsvga3dInfoSurfaceToBitmap(NULL, pSurface, "bmpd3d", "rt", "-pre");
5163 }
5164 for (int iTex = 0; iTex < SVGA3D_MAX_SAMPLERS_PS; ++iTex)
5165 {
5166 IDirect3DBaseTexture9 *pBaseTexture = 0;
5167 hr = pContext->pDevice->GetTexture(iTex, &pBaseTexture);
5168 if (hr == S_OK && pBaseTexture)
5169 {
5170 D3DSURFACE_DESC surfDesc;
5171 RT_ZERO(surfDesc);
5172
5173 IDirect3DTexture9 *pTexture = (IDirect3DTexture9 *)pBaseTexture;
5174 pTexture->GetLevelDesc(0, &surfDesc);
5175
5176 Log(("Texture[%d]: %dx%d\n",
5177 iTex, surfDesc.Width, surfDesc.Height));
5178 D3D_RELEASE(pBaseTexture);
5179 }
5180 }
5181#endif
5182
5183 /*
5184 * Process all vertex declarations. Each vertex buffer surface is represented by one stream source id.
5185 */
5186 D3DVERTEXELEMENT9 aVertexElements[SVGA3D_MAX_VERTEX_ARRAYS + 1];
5187
5188 uint32_t iCurrentVertex = 0;
5189 uint32_t iCurrentStreamId = 0;
5190 while (iCurrentVertex < numVertexDecls)
5191 {
5192 const uint32_t sidVertex = pVertexDecl[iCurrentVertex].array.surfaceId;
5193 const uint32_t strideVertex = pVertexDecl[iCurrentVertex].array.stride;
5194
5195 PVMSVGA3DSURFACE pVertexSurface;
5196 rc = vmsvga3dSurfaceFromSid(pState, sidVertex, &pVertexSurface);
5197 AssertRCBreak(rc);
5198
5199 rc = vmsvga3dDrawPrimitivesSyncVertexBuffer(pContext, pVertexSurface);
5200 AssertRCBreak(rc);
5201
5202 uint32_t uVertexMinOffset = UINT32_MAX;
5203 uint32_t uVertexMaxOffset = 0;
5204
5205 uint32_t iVertex;
5206 for (iVertex = iCurrentVertex; iVertex < numVertexDecls; ++iVertex)
5207 {
5208 /* Remember, so we can mark it as not dirty later. */
5209 aVertexSurfaces[iVertex] = pVertexSurface;
5210
5211 /* New surface id -> new stream id. */
5212 if (pVertexDecl[iVertex].array.surfaceId != sidVertex)
5213 break;
5214
5215 const uint32_t uVertexOffset = pVertexDecl[iVertex].array.offset;
5216 const uint32_t uNewVertexMinOffset = RT_MIN(uVertexMinOffset, uVertexOffset);
5217 const uint32_t uNewVertexMaxOffset = RT_MAX(uVertexMaxOffset, uVertexOffset);
5218
5219 /* We must put vertex declarations that start at a different element in another stream as d3d only handles offsets < stride. */
5220 if ( uNewVertexMaxOffset - uNewVertexMinOffset >= strideVertex
5221 && strideVertex != 0)
5222 break;
5223
5224 uVertexMinOffset = uNewVertexMinOffset;
5225 uVertexMaxOffset = uNewVertexMaxOffset;
5226 }
5227
5228 rc = vmsvga3dDrawPrimitivesProcessVertexDecls(iVertex - iCurrentVertex,
5229 &pVertexDecl[iCurrentVertex],
5230 iCurrentStreamId,
5231 uVertexMinOffset,
5232 uVertexMaxOffset,
5233 &aVertexElements[iCurrentVertex]);
5234 AssertRCBreak(rc);
5235
5236 LogFunc(("SetStreamSource vertex sid=%u stream %d min offset=%d stride=%d\n",
5237 pVertexSurface->id, iCurrentStreamId, uVertexMinOffset, strideVertex));
5238
5239 hr = pContext->pDevice->SetStreamSource(iCurrentStreamId,
5240 pVertexSurface->u.pVertexBuffer,
5241 uVertexMinOffset,
5242 strideVertex);
5243 AssertMsgBreakStmt(hr == D3D_OK, ("SetStreamSource failed with %x\n", hr), rc = VERR_INTERNAL_ERROR);
5244
5245 if (cVertexDivisor)
5246 {
5247 LogFunc(("SetStreamSourceFreq[%d]=%x\n", iCurrentStreamId, pVertexDivisor[iCurrentVertex].value));
5248 HRESULT hr2 = pContext->pDevice->SetStreamSourceFreq(iCurrentStreamId,
5249 pVertexDivisor[iCurrentVertex].value);
5250 Assert(SUCCEEDED(hr2)); RT_NOREF(hr2);
5251 }
5252
5253 iCurrentVertex = iVertex;
5254 ++iCurrentStreamId;
5255 }
5256
5257 /* iCurrentStreamId is equal to the total number of streams and the value is used for cleanup at the function end. */
5258
5259 AssertRCReturn(rc, rc);
5260
5261 /* Mark the end. */
5262 memcpy(&aVertexElements[numVertexDecls], &sVertexEnd, sizeof(sVertexEnd));
5263
5264 /* Check if this context already has the same vertex declaration. */
5265 if ( pContext->d3dState.pVertexDecl
5266 && pContext->d3dState.cVertexElements == numVertexDecls + 1
5267 && memcmp(pContext->d3dState.aVertexElements,
5268 aVertexElements,
5269 pContext->d3dState.cVertexElements * sizeof(aVertexElements[0])) == 0)
5270 {
5271 /* Same. */
5272 }
5273 else
5274 {
5275 /* Create and set the vertex declaration. */
5276 IDirect3DVertexDeclaration9 *pVertexDecl0;
5277 hr = pContext->pDevice->CreateVertexDeclaration(&aVertexElements[0], &pVertexDecl0);
5278 AssertMsgReturn(hr == D3D_OK, ("CreateVertexDeclaration failed with %x\n", hr), VERR_INTERNAL_ERROR);
5279
5280 hr = pContext->pDevice->SetVertexDeclaration(pVertexDecl0);
5281 AssertMsgReturnStmt(hr == D3D_OK, ("SetVertexDeclaration failed with %x\n", hr),
5282 D3D_RELEASE(pVertexDecl0),
5283 VERR_INTERNAL_ERROR);
5284
5285 /* The new vertex declaration has been successfully set. Delete the old one. */
5286 D3D_RELEASE(pContext->d3dState.pVertexDecl);
5287
5288 /* Remember the new vertext declaration. */
5289 pContext->d3dState.pVertexDecl = pVertexDecl0;
5290 pContext->d3dState.cVertexElements = numVertexDecls + 1;
5291 memcpy(pContext->d3dState.aVertexElements,
5292 aVertexElements,
5293 pContext->d3dState.cVertexElements * sizeof(aVertexElements[0]));
5294 }
5295
5296 /* Begin a scene before rendering anything. */
5297 hr = pContext->pDevice->BeginScene();
5298 AssertMsgReturn(hr == D3D_OK, ("BeginScene failed with %x\n", hr), VERR_INTERNAL_ERROR);
5299
5300 /* Now draw the primitives. */
5301 for (uint32_t iPrimitive = 0; iPrimitive < numRanges; ++iPrimitive)
5302 {
5303 Log(("Primitive %d: type %s\n", iPrimitive, vmsvga3dPrimitiveType2String(pRange[iPrimitive].primType)));
5304
5305 const uint32_t sidIndex = pRange[iPrimitive].indexArray.surfaceId;
5306 PVMSVGA3DSURFACE pIndexSurface = NULL;
5307
5308 D3DPRIMITIVETYPE PrimitiveTypeD3D;
5309 rc = vmsvga3dPrimitiveType2D3D(pRange[iPrimitive].primType, &PrimitiveTypeD3D);
5310 AssertRCBreak(rc);
5311
5312 /* Triangle strips or fans with just one primitive don't make much sense and are identical to triangle lists.
5313 * Workaround for NVidia driver crash when encountering some of these.
5314 */
5315 if ( pRange[iPrimitive].primitiveCount == 1
5316 && ( PrimitiveTypeD3D == D3DPT_TRIANGLESTRIP
5317 || PrimitiveTypeD3D == D3DPT_TRIANGLEFAN))
5318 PrimitiveTypeD3D = D3DPT_TRIANGLELIST;
5319
5320 if (sidIndex != SVGA3D_INVALID_ID)
5321 {
5322 AssertMsg(pRange[iPrimitive].indexWidth == sizeof(uint32_t) || pRange[iPrimitive].indexWidth == sizeof(uint16_t),
5323 ("Unsupported primitive width %d\n", pRange[iPrimitive].indexWidth));
5324
5325 rc = vmsvga3dSurfaceFromSid(pState, sidIndex, &pIndexSurface);
5326 AssertRCBreak(rc);
5327
5328 aIndexSurfaces[iPrimitive] = pIndexSurface;
5329
5330 Log(("vmsvga3dDrawPrimitives: index sid=%u\n", sidIndex));
5331
5332 rc = vmsvga3dDrawPrimitivesSyncIndexBuffer(pContext, pIndexSurface, pRange[iPrimitive].indexWidth);
5333 AssertRCBreak(rc);
5334
5335 hr = pContext->pDevice->SetIndices(pIndexSurface->u.pIndexBuffer);
5336 AssertMsg(hr == D3D_OK, ("SetIndices vertex failed with %x\n", hr));
5337 }
5338 else
5339 {
5340 hr = pContext->pDevice->SetIndices(NULL);
5341 AssertMsg(hr == D3D_OK, ("SetIndices vertex (NULL) failed with %x\n", hr));
5342 }
5343
5344 const uint32_t strideVertex = pVertexDecl[0].array.stride;
5345
5346 if (!pIndexSurface)
5347 {
5348 /* Render without an index buffer */
5349 Log(("DrawPrimitive %x primitivecount=%d index index bias=%d stride=%d\n",
5350 PrimitiveTypeD3D, pRange[iPrimitive].primitiveCount, pRange[iPrimitive].indexBias, strideVertex));
5351
5352 hr = pContext->pDevice->DrawPrimitive(PrimitiveTypeD3D,
5353 pRange[iPrimitive].indexBias,
5354 pRange[iPrimitive].primitiveCount);
5355 AssertMsgBreakStmt(hr == D3D_OK, ("DrawPrimitive failed with %x\n", hr), rc = VERR_INTERNAL_ERROR);
5356 }
5357 else
5358 {
5359 UINT numVertices;
5360 if (pVertexDecl[0].rangeHint.last)
5361 {
5362 /* Both SVGA3dArrayRangeHint definition and the SVGA driver code imply that 'last' is exclusive,
5363 * hence compute the difference.
5364 */
5365 numVertices = pVertexDecl[0].rangeHint.last - pVertexDecl[0].rangeHint.first;
5366 }
5367 else
5368 {
5369 /* Range hint is not provided. */
5370 PVMSVGA3DSURFACE pVertexSurface = aVertexSurfaces[0];
5371 numVertices = pVertexSurface->paMipmapLevels[0].cbSurface / strideVertex
5372 - pVertexDecl[0].array.offset / strideVertex
5373 - pVertexDecl[0].rangeHint.first
5374 - pRange[iPrimitive].indexBias;
5375 }
5376
5377 /* Render with an index buffer */
5378 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",
5379 PrimitiveTypeD3D, pRange[iPrimitive].indexArray.offset / pRange[iPrimitive].indexWidth, pRange[iPrimitive].indexArray.offset,
5380 pVertexDecl[0].rangeHint.first, pVertexDecl[0].rangeHint.last, numVertices, pRange[iPrimitive].primitiveCount,
5381 (pRange[iPrimitive].indexWidth == sizeof(uint16_t)) ? "D3DFMT_INDEX16" : "D3DFMT_INDEX32",
5382 pRange[iPrimitive].indexBias, strideVertex));
5383
5384 hr = pContext->pDevice->DrawIndexedPrimitive(PrimitiveTypeD3D,
5385 pRange[iPrimitive].indexBias, /* BaseVertexIndex */
5386 0, /* MinVertexIndex */
5387 numVertices,
5388 pRange[iPrimitive].indexArray.offset / pRange[iPrimitive].indexWidth, /* StartIndex */
5389 pRange[iPrimitive].primitiveCount);
5390 AssertMsgBreakStmt(hr == D3D_OK, ("DrawIndexedPrimitive failed with %x\n", hr), rc = VERR_INTERNAL_ERROR);
5391 }
5392 }
5393
5394 /* End the scene and do some cleanup regardless of the rc. */
5395 hr = pContext->pDevice->EndScene();
5396 AssertMsgReturn(hr == D3D_OK, ("EndScene failed with %x\n", hr), VERR_INTERNAL_ERROR);
5397
5398 /* Cleanup. */
5399 uint32_t i;
5400 /* Clear all streams, because they are set at the beginning of this function anyway.
5401 * Now the vertex buffers can be safely deleted/recreated if necessary.
5402 */
5403 for (i = 0; i < iCurrentStreamId; ++i)
5404 {
5405 LogFunc(("clear stream %d\n", i));
5406 HRESULT hr2 = pContext->pDevice->SetStreamSource(i, NULL, 0, 0);
5407 AssertMsg(hr2 == D3D_OK, ("SetStreamSource(%d, NULL) failed with %x\n", i, hr2)); RT_NOREF(hr2);
5408 }
5409
5410 if (cVertexDivisor)
5411 {
5412 /* "When you are finished rendering the instance data, be sure to reset the vertex stream frequency back..." */
5413 for (i = 0; i < iCurrentStreamId; ++i)
5414 {
5415 LogFunc(("reset stream freq %d\n", i));
5416 HRESULT hr2 = pContext->pDevice->SetStreamSourceFreq(i, 1);
5417 AssertMsg(hr2 == D3D_OK, ("SetStreamSourceFreq(%d, 1) failed with %x\n", i, hr2)); RT_NOREF(hr2);
5418 }
5419 }
5420
5421 if (RT_SUCCESS(rc))
5422 {
5423 for (i = 0; i < numVertexDecls; ++i)
5424 {
5425 if (aVertexSurfaces[i])
5426 {
5427 aVertexSurfaces[i]->paMipmapLevels[0].fDirty = false;
5428 aVertexSurfaces[i]->fDirty = false;
5429 }
5430 }
5431
5432 for (i = 0; i < numRanges; ++i)
5433 {
5434 if (aIndexSurfaces[i])
5435 {
5436 aIndexSurfaces[i]->paMipmapLevels[0].fDirty = false;
5437 aIndexSurfaces[i]->fDirty = false;
5438 }
5439 }
5440
5441 /* Make sure we can track drawing usage of active render targets and textures. */
5442 vmsvga3dContextTrackUsage(pThisCC, pContext);
5443 }
5444
5445#if 0
5446 /* Dump render target to a bitmap. */
5447 if (pContext->state.aRenderTargets[SVGA3D_RT_COLOR0] != SVGA3D_INVALID_ID)
5448 {
5449 vmsvga3dUpdateHeapBuffersForSurfaces(pThisCC, pContext->state.aRenderTargets[SVGA3D_RT_COLOR0]);
5450 PVMSVGA3DSURFACE pSurface;
5451 int rc2 = vmsvga3dSurfaceFromSid(pState, pContext->state.aRenderTargets[SVGA3D_RT_COLOR0], &pSurface);
5452 if (RT_SUCCESS(rc2))
5453 vmsvga3dInfoSurfaceToBitmap(NULL, pSurface, "bmpd3d", "rt", "-post");
5454# if 0
5455 for (i = 0; i < SVGA3D_MAX_SAMPLERS_PS; ++i)
5456 {
5457 if (pContext->aSidActiveTextures[i] != SVGA3D_INVALID_ID)
5458 {
5459 char szSuffix[32];
5460 RTStrPrintf2(szSuffix, sizeof(szSuffix), "-tx%d", i);
5461 vmsvga3dUpdateHeapBuffersForSurfaces(pThisCC, pContext->aSidActiveTextures[i]);
5462 rc2 = vmsvga3dSurfaceFromSid(pState, pContext->aSidActiveTextures[i], &pSurface);
5463 if (RT_SUCCESS(rc2))
5464 vmsvga3dInfoSurfaceToBitmap(NULL, pSurface, "bmpd3d", "rt", szSuffix);
5465 }
5466 }
5467# endif
5468 }
5469#endif
5470
5471 return rc;
5472}
5473
5474
5475int vmsvga3dSetScissorRect(PVGASTATECC pThisCC, uint32_t cid, SVGA3dRect *pRect)
5476{
5477 HRESULT hr;
5478 RECT rect;
5479 PVMSVGA3DSTATE pState = pThisCC->svga.p3dState;
5480 AssertReturn(pState, VERR_NO_MEMORY);
5481
5482 Log(("vmsvga3dSetScissorRect %x (%d,%d)(%d,%d)\n", cid, pRect->x, pRect->y, pRect->w, pRect->h));
5483
5484 PVMSVGA3DCONTEXT pContext;
5485 int rc = vmsvga3dContextFromCid(pState, cid, &pContext);
5486 AssertRCReturn(rc, rc);
5487 AssertReturn(pContext->pDevice, VERR_INTERNAL_ERROR);
5488
5489 /* Store for vm state save/restore. */
5490 pContext->state.u32UpdateFlags |= VMSVGA3D_UPDATE_SCISSORRECT;
5491 pContext->state.RectScissor = *pRect;
5492
5493 rect.left = pRect->x;
5494 rect.top = pRect->y;
5495 rect.right = rect.left + pRect->w; /* exclusive */
5496 rect.bottom = rect.top + pRect->h; /* exclusive */
5497
5498 hr = pContext->pDevice->SetScissorRect(&rect);
5499 AssertMsgReturn(hr == D3D_OK, ("vmsvga3dSetScissorRect: SetScissorRect failed with %x\n", hr), VERR_INTERNAL_ERROR);
5500
5501 return VINF_SUCCESS;
5502}
5503
5504
5505int vmsvga3dShaderDefine(PVGASTATECC pThisCC, uint32_t cid, uint32_t shid, SVGA3dShaderType type,
5506 uint32_t cbData, uint32_t *pShaderData)
5507{
5508 HRESULT hr;
5509 PVMSVGA3DSHADER pShader;
5510 PVMSVGA3DSTATE pState = pThisCC->svga.p3dState;
5511 AssertReturn(pState, VERR_NO_MEMORY);
5512
5513 Log(("vmsvga3dShaderDefine %x shid=%d type=%s cbData=%d\n", cid, shid, (type == SVGA3D_SHADERTYPE_VS) ? "VERTEX" : "PIXEL", cbData));
5514
5515 PVMSVGA3DCONTEXT pContext;
5516 int rc = vmsvga3dContextFromCid(pState, cid, &pContext);
5517 AssertRCReturn(rc, rc);
5518 AssertReturn(pContext->pDevice, VERR_INTERNAL_ERROR);
5519
5520 AssertReturn(shid < SVGA3D_MAX_SHADER_IDS, VERR_INVALID_PARAMETER);
5521
5522 rc = vmsvga3dShaderParse(type, cbData, pShaderData);
5523 if (RT_FAILURE(rc))
5524 {
5525 AssertRC(rc);
5526 vmsvga3dShaderLogRel("Failed to parse", type, cbData, pShaderData);
5527 return rc;
5528 }
5529
5530 if (type == SVGA3D_SHADERTYPE_VS)
5531 {
5532 if (shid >= pContext->cVertexShaders)
5533 {
5534 void *pvNew = RTMemRealloc(pContext->paVertexShader, sizeof(VMSVGA3DSHADER) * (shid + 1));
5535 AssertReturn(pvNew, VERR_NO_MEMORY);
5536 pContext->paVertexShader = (PVMSVGA3DSHADER)pvNew;
5537 memset(&pContext->paVertexShader[pContext->cVertexShaders], 0, sizeof(VMSVGA3DSHADER) * (shid + 1 - pContext->cVertexShaders));
5538 for (uint32_t i = pContext->cVertexShaders; i < shid + 1; i++)
5539 pContext->paVertexShader[i].id = SVGA3D_INVALID_ID;
5540 pContext->cVertexShaders = shid + 1;
5541 }
5542 /* If one already exists with this id, then destroy it now. */
5543 if (pContext->paVertexShader[shid].id != SVGA3D_INVALID_ID)
5544 vmsvga3dShaderDestroy(pThisCC, cid, shid, pContext->paVertexShader[shid].type);
5545
5546 pShader = &pContext->paVertexShader[shid];
5547 }
5548 else
5549 {
5550 Assert(type == SVGA3D_SHADERTYPE_PS);
5551 if (shid >= pContext->cPixelShaders)
5552 {
5553 void *pvNew = RTMemRealloc(pContext->paPixelShader, sizeof(VMSVGA3DSHADER) * (shid + 1));
5554 AssertReturn(pvNew, VERR_NO_MEMORY);
5555 pContext->paPixelShader = (PVMSVGA3DSHADER)pvNew;
5556 memset(&pContext->paPixelShader[pContext->cPixelShaders], 0, sizeof(VMSVGA3DSHADER) * (shid + 1 - pContext->cPixelShaders));
5557 for (uint32_t i = pContext->cPixelShaders; i < shid + 1; i++)
5558 pContext->paPixelShader[i].id = SVGA3D_INVALID_ID;
5559 pContext->cPixelShaders = shid + 1;
5560 }
5561 /* If one already exists with this id, then destroy it now. */
5562 if (pContext->paPixelShader[shid].id != SVGA3D_INVALID_ID)
5563 vmsvga3dShaderDestroy(pThisCC, cid, shid, pContext->paPixelShader[shid].type);
5564
5565 pShader = &pContext->paPixelShader[shid];
5566 }
5567
5568 memset(pShader, 0, sizeof(*pShader));
5569 pShader->id = shid;
5570 pShader->cid = cid;
5571 pShader->type = type;
5572 pShader->cbData = cbData;
5573 pShader->pShaderProgram = RTMemAllocZ(cbData);
5574 AssertReturn(pShader->pShaderProgram, VERR_NO_MEMORY);
5575 memcpy(pShader->pShaderProgram, pShaderData, cbData);
5576
5577#ifdef DUMP_SHADER_DISASSEMBLY
5578 LPD3DXBUFFER pDisassembly;
5579 hr = D3DXDisassembleShader((const DWORD *)pShaderData, FALSE, NULL, &pDisassembly);
5580 if (hr == D3D_OK)
5581 {
5582 Log(("Shader disassembly:\n%s\n", pDisassembly->GetBufferPointer()));
5583 D3D_RELEASE(pDisassembly);
5584 }
5585#endif
5586
5587 switch (type)
5588 {
5589 case SVGA3D_SHADERTYPE_VS:
5590 hr = pContext->pDevice->CreateVertexShader((const DWORD *)pShaderData, &pShader->u.pVertexShader);
5591 break;
5592
5593 case SVGA3D_SHADERTYPE_PS:
5594 hr = pContext->pDevice->CreatePixelShader((const DWORD *)pShaderData, &pShader->u.pPixelShader);
5595 break;
5596
5597 default:
5598 AssertFailedReturn(VERR_INVALID_PARAMETER);
5599 }
5600 if (hr != D3D_OK)
5601 {
5602 /* Dump the shader code. */
5603 vmsvga3dShaderLogRel("Failed to create", type, cbData, pShaderData);
5604
5605 RTMemFree(pShader->pShaderProgram);
5606 memset(pShader, 0, sizeof(*pShader));
5607 pShader->id = SVGA3D_INVALID_ID;
5608 }
5609
5610 AssertMsgReturn(hr == D3D_OK, ("vmsvga3dShaderDefine: CreateVertex/PixelShader failed with %x\n", hr), VERR_INTERNAL_ERROR);
5611 return VINF_SUCCESS;
5612}
5613
5614int vmsvga3dShaderDestroy(PVGASTATECC pThisCC, uint32_t cid, uint32_t shid, SVGA3dShaderType type)
5615{
5616 PVMSVGA3DSTATE pState = pThisCC->svga.p3dState;
5617 AssertReturn(pState, VERR_NO_MEMORY);
5618 PVMSVGA3DSHADER pShader = NULL;
5619
5620 Log(("vmsvga3dShaderDestroy %x shid=%d type=%s\n", cid, shid, (type == SVGA3D_SHADERTYPE_VS) ? "VERTEX" : "PIXEL"));
5621
5622 PVMSVGA3DCONTEXT pContext;
5623 int rc = vmsvga3dContextFromCid(pState, cid, &pContext);
5624 AssertRCReturn(rc, rc);
5625
5626 if (type == SVGA3D_SHADERTYPE_VS)
5627 {
5628 if ( shid < pContext->cVertexShaders
5629 && pContext->paVertexShader[shid].id == shid)
5630 {
5631 pShader = &pContext->paVertexShader[shid];
5632 D3D_RELEASE(pShader->u.pVertexShader);
5633 }
5634 }
5635 else
5636 {
5637 Assert(type == SVGA3D_SHADERTYPE_PS);
5638 if ( shid < pContext->cPixelShaders
5639 && pContext->paPixelShader[shid].id == shid)
5640 {
5641 pShader = &pContext->paPixelShader[shid];
5642 D3D_RELEASE(pShader->u.pPixelShader);
5643 }
5644 }
5645
5646 if (pShader)
5647 {
5648 if (pShader->pShaderProgram)
5649 RTMemFree(pShader->pShaderProgram);
5650
5651 memset(pShader, 0, sizeof(*pShader));
5652 pShader->id = SVGA3D_INVALID_ID;
5653 }
5654 else
5655 AssertFailedReturn(VERR_INVALID_PARAMETER);
5656
5657 return VINF_SUCCESS;
5658}
5659
5660int vmsvga3dShaderSet(PVGASTATECC pThisCC, PVMSVGA3DCONTEXT pContext, uint32_t cid, SVGA3dShaderType type, uint32_t shid)
5661{
5662 PVMSVGA3DSTATE pState = pThisCC->svga.p3dState;
5663 AssertReturn(pState, VERR_NO_MEMORY);
5664 HRESULT hr;
5665
5666 Log(("vmsvga3dShaderSet %x type=%s shid=%d\n", cid, (type == SVGA3D_SHADERTYPE_VS) ? "VERTEX" : "PIXEL", shid));
5667
5668 NOREF(pContext);
5669 int rc = vmsvga3dContextFromCid(pState, cid, &pContext);
5670 AssertRCReturn(rc, rc);
5671 AssertReturn(pContext->pDevice, VERR_INTERNAL_ERROR);
5672
5673 if (type == SVGA3D_SHADERTYPE_VS)
5674 {
5675 /* Save for vm state save/restore. */
5676 pContext->state.shidVertex = shid;
5677 pContext->state.u32UpdateFlags |= VMSVGA3D_UPDATE_VERTEXSHADER;
5678
5679 if ( shid < pContext->cVertexShaders
5680 && pContext->paVertexShader[shid].id == shid)
5681 {
5682 PVMSVGA3DSHADER pShader = &pContext->paVertexShader[shid];
5683 Assert(type == pShader->type);
5684
5685 hr = pContext->pDevice->SetVertexShader(pShader->u.pVertexShader);
5686 AssertMsgReturn(hr == D3D_OK, ("vmsvga3dShaderSet: SetVertex/PixelShader failed with %x\n", hr), VERR_INTERNAL_ERROR);
5687 }
5688 else
5689 if (shid == SVGA_ID_INVALID)
5690 {
5691 /* Unselect shader. */
5692 hr = pContext->pDevice->SetVertexShader(NULL);
5693 AssertMsgReturn(hr == D3D_OK, ("vmsvga3dShaderSet: SetVertex/PixelShader failed with %x\n", hr), VERR_INTERNAL_ERROR);
5694 }
5695 else
5696 AssertFailedReturn(VERR_INVALID_PARAMETER);
5697 }
5698 else
5699 {
5700 /* Save for vm state save/restore. */
5701 pContext->state.shidPixel = shid;
5702 pContext->state.u32UpdateFlags |= VMSVGA3D_UPDATE_PIXELSHADER;
5703
5704 Assert(type == SVGA3D_SHADERTYPE_PS);
5705 if ( shid < pContext->cPixelShaders
5706 && pContext->paPixelShader[shid].id == shid)
5707 {
5708 PVMSVGA3DSHADER pShader = &pContext->paPixelShader[shid];
5709 Assert(type == pShader->type);
5710
5711 hr = pContext->pDevice->SetPixelShader(pShader->u.pPixelShader);
5712 AssertMsgReturn(hr == D3D_OK, ("vmsvga3dShaderSet: SetVertex/PixelShader failed with %x\n", hr), VERR_INTERNAL_ERROR);
5713 }
5714 else
5715 if (shid == SVGA_ID_INVALID)
5716 {
5717 /* Unselect shader. */
5718 hr = pContext->pDevice->SetPixelShader(NULL);
5719 AssertMsgReturn(hr == D3D_OK, ("vmsvga3dShaderSet: SetVertex/PixelShader failed with %x\n", hr), VERR_INTERNAL_ERROR);
5720 }
5721 else
5722 AssertFailedReturn(VERR_INVALID_PARAMETER);
5723 }
5724
5725 return VINF_SUCCESS;
5726}
5727
5728int vmsvga3dShaderSetConst(PVGASTATECC pThisCC, uint32_t cid, uint32_t reg, SVGA3dShaderType type,
5729 SVGA3dShaderConstType ctype, uint32_t cRegisters, uint32_t *pValues)
5730{
5731 HRESULT hr;
5732 PVMSVGA3DSTATE pState = pThisCC->svga.p3dState;
5733 AssertReturn(pState, VERR_NO_MEMORY);
5734
5735 Log(("vmsvga3dShaderSetConst %x reg=%x type=%s ctype=%x\n", cid, reg, (type == SVGA3D_SHADERTYPE_VS) ? "VERTEX" : "PIXEL", ctype));
5736
5737 PVMSVGA3DCONTEXT pContext;
5738 int rc = vmsvga3dContextFromCid(pState, cid, &pContext);
5739 AssertRCReturn(rc, rc);
5740 AssertReturn(pContext->pDevice, VERR_INTERNAL_ERROR);
5741
5742 for (uint32_t i = 0; i < cRegisters; i++)
5743 {
5744#ifdef LOG_ENABLED
5745 switch (ctype)
5746 {
5747 case SVGA3D_CONST_TYPE_FLOAT:
5748 {
5749 float *pValuesF = (float *)pValues;
5750 Log(("ConstantF %d: value=" FLOAT_FMT_STR ", " FLOAT_FMT_STR ", " FLOAT_FMT_STR ", " FLOAT_FMT_STR "\n",
5751 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])));
5752 break;
5753 }
5754
5755 case SVGA3D_CONST_TYPE_INT:
5756 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]));
5757 break;
5758
5759 case SVGA3D_CONST_TYPE_BOOL:
5760 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]));
5761 break;
5762
5763 default:
5764 AssertFailedReturn(VERR_INVALID_PARAMETER);
5765 }
5766#endif
5767 vmsvga3dSaveShaderConst(pContext, reg + i, type, ctype, pValues[i*4 + 0], pValues[i*4 + 1], pValues[i*4 + 2], pValues[i*4 + 3]);
5768 }
5769
5770 switch (type)
5771 {
5772 case SVGA3D_SHADERTYPE_VS:
5773 switch (ctype)
5774 {
5775 case SVGA3D_CONST_TYPE_FLOAT:
5776 hr = pContext->pDevice->SetVertexShaderConstantF(reg, (const float *)pValues, cRegisters);
5777 break;
5778
5779 case SVGA3D_CONST_TYPE_INT:
5780 hr = pContext->pDevice->SetVertexShaderConstantI(reg, (const int *)pValues, cRegisters);
5781 break;
5782
5783 case SVGA3D_CONST_TYPE_BOOL:
5784 hr = pContext->pDevice->SetVertexShaderConstantB(reg, (const BOOL *)pValues, cRegisters);
5785 break;
5786
5787 default:
5788 AssertFailedReturn(VERR_INVALID_PARAMETER);
5789 }
5790 AssertMsgReturn(hr == D3D_OK, ("vmsvga3dShaderSetConst: SetVertexShader failed with %x\n", hr), VERR_INTERNAL_ERROR);
5791 break;
5792
5793 case SVGA3D_SHADERTYPE_PS:
5794 switch (ctype)
5795 {
5796 case SVGA3D_CONST_TYPE_FLOAT:
5797 {
5798 hr = pContext->pDevice->SetPixelShaderConstantF(reg, (const float *)pValues, cRegisters);
5799 break;
5800 }
5801
5802 case SVGA3D_CONST_TYPE_INT:
5803 hr = pContext->pDevice->SetPixelShaderConstantI(reg, (const int *)pValues, cRegisters);
5804 break;
5805
5806 case SVGA3D_CONST_TYPE_BOOL:
5807 hr = pContext->pDevice->SetPixelShaderConstantB(reg, (const BOOL *)pValues, cRegisters);
5808 break;
5809
5810 default:
5811 AssertFailedReturn(VERR_INVALID_PARAMETER);
5812 }
5813 AssertMsgReturn(hr == D3D_OK, ("vmsvga3dShaderSetConst: SetPixelShader failed with %x\n", hr), VERR_INTERNAL_ERROR);
5814 break;
5815
5816 default:
5817 AssertFailedReturn(VERR_INVALID_PARAMETER);
5818 }
5819 return VINF_SUCCESS;
5820}
5821
5822int vmsvga3dOcclusionQueryCreate(PVMSVGA3DSTATE pState, PVMSVGA3DCONTEXT pContext)
5823{
5824 RT_NOREF(pState);
5825 HRESULT hr = pContext->pDevice->CreateQuery(D3DQUERYTYPE_OCCLUSION, &pContext->occlusion.pQuery);
5826 AssertMsgReturn(hr == D3D_OK, ("CreateQuery(D3DQUERYTYPE_OCCLUSION) failed with %x\n", hr), VERR_INTERNAL_ERROR);
5827 return VINF_SUCCESS;
5828}
5829
5830int vmsvga3dOcclusionQueryDelete(PVMSVGA3DSTATE pState, PVMSVGA3DCONTEXT pContext)
5831{
5832 RT_NOREF(pState);
5833 D3D_RELEASE(pContext->occlusion.pQuery);
5834 return VINF_SUCCESS;
5835}
5836
5837int vmsvga3dOcclusionQueryBegin(PVMSVGA3DSTATE pState, PVMSVGA3DCONTEXT pContext)
5838{
5839 RT_NOREF(pState);
5840 HRESULT hr = pContext->occlusion.pQuery->Issue(D3DISSUE_BEGIN);
5841 AssertMsgReturnStmt(hr == D3D_OK, ("D3DISSUE_BEGIN(D3DQUERYTYPE_OCCLUSION) failed with %x\n", hr),
5842 D3D_RELEASE(pContext->occlusion.pQuery), VERR_INTERNAL_ERROR);
5843 return VINF_SUCCESS;
5844}
5845
5846int vmsvga3dOcclusionQueryEnd(PVMSVGA3DSTATE pState, PVMSVGA3DCONTEXT pContext)
5847{
5848 RT_NOREF(pState);
5849 HRESULT hr = pContext->occlusion.pQuery->Issue(D3DISSUE_END);
5850 AssertMsgReturnStmt(hr == D3D_OK, ("D3DISSUE_END(D3DQUERYTYPE_OCCLUSION) failed with %x\n", hr),
5851 D3D_RELEASE(pContext->occlusion.pQuery), VERR_INTERNAL_ERROR);
5852 return VINF_SUCCESS;
5853}
5854
5855int vmsvga3dOcclusionQueryGetData(PVMSVGA3DSTATE pState, PVMSVGA3DCONTEXT pContext, uint32_t *pu32Pixels)
5856{
5857 RT_NOREF(pState);
5858 HRESULT hr = D3D_OK;
5859 /* Wait until the data becomes available. */
5860 DWORD dwPixels = 0;
5861 do
5862 {
5863 hr = pContext->occlusion.pQuery->GetData((void *)&dwPixels, sizeof(DWORD), D3DGETDATA_FLUSH);
5864 } while (hr == S_FALSE);
5865
5866 AssertMsgReturnStmt(hr == D3D_OK, ("GetData(D3DQUERYTYPE_OCCLUSION) failed with %x\n", hr),
5867 D3D_RELEASE(pContext->occlusion.pQuery), VERR_INTERNAL_ERROR);
5868
5869 LogFunc(("Query result: dwPixels %d\n", dwPixels));
5870 *pu32Pixels = dwPixels;
5871 return VINF_SUCCESS;
5872}
5873
5874static void vmsvgaDumpD3DCaps(D3DCAPS9 *pCaps, D3DADAPTER_IDENTIFIER9 const *pai9)
5875{
5876 bool const fBufferingSaved = RTLogRelSetBuffering(true /*fBuffered*/);
5877
5878 LogRel(("\nD3D9 adapter: %s %RX16:%RX16 [%s, version %d.%d.%d.%d]\n",
5879 pai9->Description, pai9->VendorId, pai9->DeviceId, pai9->Driver,
5880 RT_HI_U16(pai9->DriverVersion.HighPart), RT_LO_U16(pai9->DriverVersion.HighPart),
5881 RT_HI_U16(pai9->DriverVersion.LowPart), RT_LO_U16(pai9->DriverVersion.LowPart)));
5882
5883 LogRel(("\nD3D device caps: DevCaps2:\n"));
5884 if (pCaps->DevCaps2 & D3DDEVCAPS2_ADAPTIVETESSRTPATCH)
5885 LogRel((" - D3DDEVCAPS2_ADAPTIVETESSRTPATCH\n"));
5886 if (pCaps->DevCaps2 & D3DDEVCAPS2_ADAPTIVETESSNPATCH)
5887 LogRel((" - D3DDEVCAPS2_ADAPTIVETESSNPATCH\n"));
5888 if (pCaps->DevCaps2 & D3DDEVCAPS2_CAN_STRETCHRECT_FROM_TEXTURES)
5889 LogRel((" - D3DDEVCAPS2_CAN_STRETCHRECT_FROM_TEXTURES\n"));
5890 if (pCaps->DevCaps2 & D3DDEVCAPS2_DMAPNPATCH)
5891 LogRel((" - D3DDEVCAPS2_DMAPNPATCH\n"));
5892 if (pCaps->DevCaps2 & D3DDEVCAPS2_PRESAMPLEDDMAPNPATCH)
5893 LogRel((" - D3DDEVCAPS2_PRESAMPLEDDMAPNPATCH\n"));
5894 if (pCaps->DevCaps2 & D3DDEVCAPS2_STREAMOFFSET)
5895 LogRel((" - D3DDEVCAPS2_STREAMOFFSET\n"));
5896 if (pCaps->DevCaps2 & D3DDEVCAPS2_VERTEXELEMENTSCANSHARESTREAMOFFSET)
5897 LogRel((" - D3DDEVCAPS2_VERTEXELEMENTSCANSHARESTREAMOFFSET\n"));
5898
5899 LogRel(("\nCaps2:\n"));
5900 if (pCaps->Caps2 & D3DCAPS2_CANAUTOGENMIPMAP)
5901 LogRel((" - D3DCAPS2_CANAUTOGENMIPMAP\n"));
5902 if (pCaps->Caps2 & D3DCAPS2_CANCALIBRATEGAMMA)
5903 LogRel((" - D3DCAPS2_CANCALIBRATEGAMMA\n"));
5904 if (pCaps->Caps2 & D3DCAPS2_CANSHARERESOURCE)
5905 LogRel((" - D3DCAPS2_CANSHARERESOURCE\n"));
5906 if (pCaps->Caps2 & D3DCAPS2_CANMANAGERESOURCE)
5907 LogRel((" - D3DCAPS2_CANMANAGERESOURCE\n"));
5908 if (pCaps->Caps2 & D3DCAPS2_DYNAMICTEXTURES)
5909 LogRel((" - D3DCAPS2_DYNAMICTEXTURES\n"));
5910 if (pCaps->Caps2 & D3DCAPS2_FULLSCREENGAMMA)
5911 LogRel((" - D3DCAPS2_FULLSCREENGAMMA\n"));
5912
5913 LogRel(("\nCaps3:\n"));
5914 if (pCaps->Caps3 & D3DCAPS3_ALPHA_FULLSCREEN_FLIP_OR_DISCARD)
5915 LogRel((" - D3DCAPS3_ALPHA_FULLSCREEN_FLIP_OR_DISCARD\n"));
5916 if (pCaps->Caps3 & D3DCAPS3_COPY_TO_VIDMEM)
5917 LogRel((" - D3DCAPS3_COPY_TO_VIDMEM\n"));
5918 if (pCaps->Caps3 & D3DCAPS3_COPY_TO_SYSTEMMEM)
5919 LogRel((" - D3DCAPS3_COPY_TO_SYSTEMMEM\n"));
5920 if (pCaps->Caps3 & D3DCAPS3_DXVAHD)
5921 LogRel((" - D3DCAPS3_DXVAHD\n"));
5922 if (pCaps->Caps3 & D3DCAPS3_LINEAR_TO_SRGB_PRESENTATION)
5923 LogRel((" - D3DCAPS3_LINEAR_TO_SRGB_PRESENTATION\n"));
5924
5925 LogRel(("\nPresentationIntervals:\n"));
5926 if (pCaps->PresentationIntervals & D3DPRESENT_INTERVAL_IMMEDIATE)
5927 LogRel((" - D3DPRESENT_INTERVAL_IMMEDIATE\n"));
5928 if (pCaps->PresentationIntervals & D3DPRESENT_INTERVAL_ONE)
5929 LogRel((" - D3DPRESENT_INTERVAL_ONE\n"));
5930 if (pCaps->PresentationIntervals & D3DPRESENT_INTERVAL_TWO)
5931 LogRel((" - D3DPRESENT_INTERVAL_TWO\n"));
5932 if (pCaps->PresentationIntervals & D3DPRESENT_INTERVAL_THREE)
5933 LogRel((" - D3DPRESENT_INTERVAL_THREE\n"));
5934 if (pCaps->PresentationIntervals & D3DPRESENT_INTERVAL_FOUR)
5935 LogRel((" - D3DPRESENT_INTERVAL_FOUR\n"));
5936
5937 LogRel(("\nDevcaps:\n"));
5938 if (pCaps->DevCaps & D3DDEVCAPS_CANBLTSYSTONONLOCAL)
5939 LogRel((" - D3DDEVCAPS_CANBLTSYSTONONLOCAL\n"));
5940 if (pCaps->DevCaps & D3DDEVCAPS_CANRENDERAFTERFLIP)
5941 LogRel((" - D3DDEVCAPS_CANRENDERAFTERFLIP\n"));
5942 if (pCaps->DevCaps & D3DDEVCAPS_DRAWPRIMITIVES2)
5943 LogRel((" - D3DDEVCAPS_DRAWPRIMITIVES2\n"));
5944 if (pCaps->DevCaps & D3DDEVCAPS_DRAWPRIMITIVES2EX)
5945 LogRel((" - D3DDEVCAPS_DRAWPRIMITIVES2EX\n"));
5946 if (pCaps->DevCaps & D3DDEVCAPS_DRAWPRIMTLVERTEX)
5947 LogRel((" - D3DDEVCAPS_DRAWPRIMTLVERTEX\n"));
5948 if (pCaps->DevCaps & D3DDEVCAPS_EXECUTESYSTEMMEMORY)
5949 LogRel((" - D3DDEVCAPS_EXECUTESYSTEMMEMORY\n"));
5950 if (pCaps->DevCaps & D3DDEVCAPS_EXECUTEVIDEOMEMORY)
5951 LogRel((" - D3DDEVCAPS_EXECUTEVIDEOMEMORY\n"));
5952 if (pCaps->DevCaps & D3DDEVCAPS_HWRASTERIZATION)
5953 LogRel((" - D3DDEVCAPS_HWRASTERIZATION\n"));
5954 if (pCaps->DevCaps & D3DDEVCAPS_HWTRANSFORMANDLIGHT)
5955 LogRel((" - D3DDEVCAPS_HWTRANSFORMANDLIGHT\n"));
5956 if (pCaps->DevCaps & D3DDEVCAPS_NPATCHES)
5957 LogRel((" - D3DDEVCAPS_NPATCHES\n"));
5958 if (pCaps->DevCaps & D3DDEVCAPS_PUREDEVICE)
5959 LogRel((" - D3DDEVCAPS_PUREDEVICE\n"));
5960 if (pCaps->DevCaps & D3DDEVCAPS_QUINTICRTPATCHES)
5961 LogRel((" - D3DDEVCAPS_QUINTICRTPATCHES\n"));
5962 if (pCaps->DevCaps & D3DDEVCAPS_RTPATCHES)
5963 LogRel((" - D3DDEVCAPS_RTPATCHES\n"));
5964 if (pCaps->DevCaps & D3DDEVCAPS_RTPATCHHANDLEZERO)
5965 LogRel((" - D3DDEVCAPS_RTPATCHHANDLEZERO\n"));
5966 if (pCaps->DevCaps & D3DDEVCAPS_SEPARATETEXTUREMEMORIES)
5967 LogRel((" - D3DDEVCAPS_SEPARATETEXTUREMEMORIES\n"));
5968 if (pCaps->DevCaps & D3DDEVCAPS_TEXTURENONLOCALVIDMEM)
5969 LogRel((" - D3DDEVCAPS_TEXTURENONLOCALVIDMEM\n"));
5970 if (pCaps->DevCaps & D3DDEVCAPS_TEXTURESYSTEMMEMORY)
5971 LogRel((" - D3DDEVCAPS_TEXTURESYSTEMMEMORY\n"));
5972 if (pCaps->DevCaps & D3DDEVCAPS_TEXTUREVIDEOMEMORY)
5973 LogRel((" - D3DDEVCAPS_TEXTUREVIDEOMEMORY\n"));
5974 if (pCaps->DevCaps & D3DDEVCAPS_TLVERTEXSYSTEMMEMORY)
5975 LogRel((" - D3DDEVCAPS_TLVERTEXSYSTEMMEMORY\n"));
5976 if (pCaps->DevCaps & D3DDEVCAPS_TLVERTEXVIDEOMEMORY)
5977 LogRel((" - D3DDEVCAPS_TLVERTEXVIDEOMEMORY\n"));
5978
5979 LogRel(("\nTextureCaps:\n"));
5980 if (pCaps->TextureCaps & D3DPTEXTURECAPS_ALPHA)
5981 LogRel((" - D3DPTEXTURECAPS_ALPHA\n"));
5982 if (pCaps->TextureCaps & D3DPTEXTURECAPS_ALPHAPALETTE)
5983 LogRel((" - D3DPTEXTURECAPS_ALPHAPALETTE\n"));
5984 if (pCaps->TextureCaps & D3DPTEXTURECAPS_CUBEMAP)
5985 LogRel((" - D3DPTEXTURECAPS_CUBEMAP\n"));
5986 if (pCaps->TextureCaps & D3DPTEXTURECAPS_CUBEMAP_POW2)
5987 LogRel((" - D3DPTEXTURECAPS_CUBEMAP_POW2\n"));
5988 if (pCaps->TextureCaps & D3DPTEXTURECAPS_MIPCUBEMAP)
5989 LogRel((" - D3DPTEXTURECAPS_MIPCUBEMAP\n"));
5990 if (pCaps->TextureCaps & D3DPTEXTURECAPS_MIPMAP)
5991 LogRel((" - D3DPTEXTURECAPS_MIPMAP\n"));
5992 if (pCaps->TextureCaps & D3DPTEXTURECAPS_MIPVOLUMEMAP)
5993 LogRel((" - D3DPTEXTURECAPS_MIPVOLUMEMAP\n"));
5994 if (pCaps->TextureCaps & D3DPTEXTURECAPS_NONPOW2CONDITIONAL)
5995 LogRel((" - D3DPTEXTURECAPS_NONPOW2CONDITIONAL\n"));
5996 if (pCaps->TextureCaps & D3DPTEXTURECAPS_POW2)
5997 LogRel((" - D3DPTEXTURECAPS_POW2\n"));
5998 if (pCaps->TextureCaps & D3DPTEXTURECAPS_NOPROJECTEDBUMPENV)
5999 LogRel((" - D3DPTEXTURECAPS_NOPROJECTEDBUMPENV\n"));
6000 if (pCaps->TextureCaps & D3DPTEXTURECAPS_PERSPECTIVE)
6001 LogRel((" - D3DPTEXTURECAPS_PERSPECTIVE\n"));
6002 if (pCaps->TextureCaps & D3DPTEXTURECAPS_POW2)
6003 LogRel((" - D3DPTEXTURECAPS_POW2\n"));
6004 if (pCaps->TextureCaps & D3DPTEXTURECAPS_PROJECTED)
6005 LogRel((" - D3DPTEXTURECAPS_PROJECTED\n"));
6006 if (pCaps->TextureCaps & D3DPTEXTURECAPS_SQUAREONLY)
6007 LogRel((" - D3DPTEXTURECAPS_SQUAREONLY\n"));
6008 if (pCaps->TextureCaps & D3DPTEXTURECAPS_TEXREPEATNOTSCALEDBYSIZE)
6009 LogRel((" - D3DPTEXTURECAPS_TEXREPEATNOTSCALEDBYSIZE\n"));
6010 if (pCaps->TextureCaps & D3DPTEXTURECAPS_VOLUMEMAP)
6011 LogRel((" - D3DPTEXTURECAPS_VOLUMEMAP\n"));
6012 if (pCaps->TextureCaps & D3DPTEXTURECAPS_VOLUMEMAP_POW2)
6013 LogRel((" - D3DPTEXTURECAPS_VOLUMEMAP_POW2\n"));
6014
6015 LogRel(("\nTextureFilterCaps\n"));
6016 if (pCaps->TextureFilterCaps & D3DPTFILTERCAPS_CONVOLUTIONMONO)
6017 LogRel((" - D3DPTFILTERCAPS_CONVOLUTIONMONO\n"));
6018 if (pCaps->TextureFilterCaps & D3DPTFILTERCAPS_MAGFPOINT)
6019 LogRel((" - D3DPTFILTERCAPS_MAGFPOINT\n"));
6020 if (pCaps->TextureFilterCaps & D3DPTFILTERCAPS_MAGFLINEAR)
6021 LogRel((" - D3DPTFILTERCAPS_MAGFLINEAR\n"));
6022 if (pCaps->TextureFilterCaps & D3DPTFILTERCAPS_MAGFANISOTROPIC)
6023 LogRel((" - D3DPTFILTERCAPS_MAGFANISOTROPIC\n"));
6024 if (pCaps->TextureFilterCaps & D3DPTFILTERCAPS_MAGFPYRAMIDALQUAD)
6025 LogRel((" - D3DPTFILTERCAPS_MAGFPYRAMIDALQUAD\n"));
6026 if (pCaps->TextureFilterCaps & D3DPTFILTERCAPS_MAGFGAUSSIANQUAD)
6027 LogRel((" - D3DPTFILTERCAPS_MAGFGAUSSIANQUAD\n"));
6028 if (pCaps->TextureFilterCaps & D3DPTFILTERCAPS_MINFPOINT)
6029 LogRel((" - D3DPTFILTERCAPS_MINFPOINT\n"));
6030 if (pCaps->TextureFilterCaps & D3DPTFILTERCAPS_MINFLINEAR)
6031 LogRel((" - D3DPTFILTERCAPS_MINFLINEAR\n"));
6032 if (pCaps->TextureFilterCaps & D3DPTFILTERCAPS_MINFANISOTROPIC)
6033 LogRel((" - D3DPTFILTERCAPS_MINFANISOTROPIC\n"));
6034 if (pCaps->TextureFilterCaps & D3DPTFILTERCAPS_MINFPYRAMIDALQUAD)
6035 LogRel((" - D3DPTFILTERCAPS_MINFPYRAMIDALQUAD\n"));
6036 if (pCaps->TextureFilterCaps & D3DPTFILTERCAPS_MINFGAUSSIANQUAD)
6037 LogRel((" - D3DPTFILTERCAPS_MINFGAUSSIANQUAD\n"));
6038 if (pCaps->TextureFilterCaps & D3DPTFILTERCAPS_MIPFPOINT)
6039 LogRel((" - D3DPTFILTERCAPS_MIPFPOINT\n"));
6040 if (pCaps->TextureFilterCaps & D3DPTFILTERCAPS_MIPFLINEAR)
6041 LogRel((" - D3DPTFILTERCAPS_MIPFLINEAR\n"));
6042
6043 LogRel(("\nCubeTextureFilterCaps\n"));
6044 if (pCaps->CubeTextureFilterCaps & D3DPTFILTERCAPS_CONVOLUTIONMONO)
6045 LogRel((" - D3DPTFILTERCAPS_CONVOLUTIONMONO\n"));
6046 if (pCaps->CubeTextureFilterCaps & D3DPTFILTERCAPS_MAGFPOINT)
6047 LogRel((" - D3DPTFILTERCAPS_MAGFPOINT\n"));
6048 if (pCaps->CubeTextureFilterCaps & D3DPTFILTERCAPS_MAGFLINEAR)
6049 LogRel((" - D3DPTFILTERCAPS_MAGFLINEAR\n"));
6050 if (pCaps->CubeTextureFilterCaps & D3DPTFILTERCAPS_MAGFANISOTROPIC)
6051 LogRel((" - D3DPTFILTERCAPS_MAGFANISOTROPIC\n"));
6052 if (pCaps->CubeTextureFilterCaps & D3DPTFILTERCAPS_MAGFPYRAMIDALQUAD)
6053 LogRel((" - D3DPTFILTERCAPS_MAGFPYRAMIDALQUAD\n"));
6054 if (pCaps->CubeTextureFilterCaps & D3DPTFILTERCAPS_MAGFGAUSSIANQUAD)
6055 LogRel((" - D3DPTFILTERCAPS_MAGFGAUSSIANQUAD\n"));
6056 if (pCaps->CubeTextureFilterCaps & D3DPTFILTERCAPS_MINFPOINT)
6057 LogRel((" - D3DPTFILTERCAPS_MINFPOINT\n"));
6058 if (pCaps->CubeTextureFilterCaps & D3DPTFILTERCAPS_MINFLINEAR)
6059 LogRel((" - D3DPTFILTERCAPS_MINFLINEAR\n"));
6060 if (pCaps->CubeTextureFilterCaps & D3DPTFILTERCAPS_MINFANISOTROPIC)
6061 LogRel((" - D3DPTFILTERCAPS_MINFANISOTROPIC\n"));
6062 if (pCaps->CubeTextureFilterCaps & D3DPTFILTERCAPS_MINFPYRAMIDALQUAD)
6063 LogRel((" - D3DPTFILTERCAPS_MINFPYRAMIDALQUAD\n"));
6064 if (pCaps->CubeTextureFilterCaps & D3DPTFILTERCAPS_MINFGAUSSIANQUAD)
6065 LogRel((" - D3DPTFILTERCAPS_MINFGAUSSIANQUAD\n"));
6066 if (pCaps->CubeTextureFilterCaps & D3DPTFILTERCAPS_MIPFPOINT)
6067 LogRel((" - D3DPTFILTERCAPS_MIPFPOINT\n"));
6068 if (pCaps->CubeTextureFilterCaps & D3DPTFILTERCAPS_MIPFLINEAR)
6069 LogRel((" - D3DPTFILTERCAPS_MIPFLINEAR\n"));
6070
6071 LogRel(("\nVolumeTextureFilterCaps\n"));
6072 if (pCaps->VolumeTextureFilterCaps & D3DPTFILTERCAPS_CONVOLUTIONMONO)
6073 LogRel((" - D3DPTFILTERCAPS_CONVOLUTIONMONO\n"));
6074 if (pCaps->VolumeTextureFilterCaps & D3DPTFILTERCAPS_MAGFPOINT)
6075 LogRel((" - D3DPTFILTERCAPS_MAGFPOINT\n"));
6076 if (pCaps->VolumeTextureFilterCaps & D3DPTFILTERCAPS_MAGFLINEAR)
6077 LogRel((" - D3DPTFILTERCAPS_MAGFLINEAR\n"));
6078 if (pCaps->VolumeTextureFilterCaps & D3DPTFILTERCAPS_MAGFANISOTROPIC)
6079 LogRel((" - D3DPTFILTERCAPS_MAGFANISOTROPIC\n"));
6080 if (pCaps->VolumeTextureFilterCaps & D3DPTFILTERCAPS_MAGFPYRAMIDALQUAD)
6081 LogRel((" - D3DPTFILTERCAPS_MAGFPYRAMIDALQUAD\n"));
6082 if (pCaps->VolumeTextureFilterCaps & D3DPTFILTERCAPS_MAGFGAUSSIANQUAD)
6083 LogRel((" - D3DPTFILTERCAPS_MAGFGAUSSIANQUAD\n"));
6084 if (pCaps->VolumeTextureFilterCaps & D3DPTFILTERCAPS_MINFPOINT)
6085 LogRel((" - D3DPTFILTERCAPS_MINFPOINT\n"));
6086 if (pCaps->VolumeTextureFilterCaps & D3DPTFILTERCAPS_MINFLINEAR)
6087 LogRel((" - D3DPTFILTERCAPS_MINFLINEAR\n"));
6088 if (pCaps->VolumeTextureFilterCaps & D3DPTFILTERCAPS_MINFANISOTROPIC)
6089 LogRel((" - D3DPTFILTERCAPS_MINFANISOTROPIC\n"));
6090 if (pCaps->VolumeTextureFilterCaps & D3DPTFILTERCAPS_MINFPYRAMIDALQUAD)
6091 LogRel((" - D3DPTFILTERCAPS_MINFPYRAMIDALQUAD\n"));
6092 if (pCaps->VolumeTextureFilterCaps & D3DPTFILTERCAPS_MINFGAUSSIANQUAD)
6093 LogRel((" - D3DPTFILTERCAPS_MINFGAUSSIANQUAD\n"));
6094 if (pCaps->VolumeTextureFilterCaps & D3DPTFILTERCAPS_MIPFPOINT)
6095 LogRel((" - D3DPTFILTERCAPS_MIPFPOINT\n"));
6096 if (pCaps->VolumeTextureFilterCaps & D3DPTFILTERCAPS_MIPFLINEAR)
6097 LogRel((" - D3DPTFILTERCAPS_MIPFLINEAR\n"));
6098
6099 LogRel(("\nTextureAddressCaps:\n"));
6100 if (pCaps->TextureAddressCaps & D3DPTADDRESSCAPS_BORDER)
6101 LogRel((" - D3DPTADDRESSCAPS_BORDER\n"));
6102 if (pCaps->TextureAddressCaps & D3DPTADDRESSCAPS_CLAMP)
6103 LogRel((" - D3DPTADDRESSCAPS_CLAMP\n"));
6104 if (pCaps->TextureAddressCaps & D3DPTADDRESSCAPS_INDEPENDENTUV)
6105 LogRel((" - D3DPTADDRESSCAPS_INDEPENDENTUV\n"));
6106 if (pCaps->TextureAddressCaps & D3DPTADDRESSCAPS_MIRROR)
6107 LogRel((" - D3DPTADDRESSCAPS_MIRROR\n"));
6108 if (pCaps->TextureAddressCaps & D3DPTADDRESSCAPS_MIRRORONCE)
6109 LogRel((" - D3DPTADDRESSCAPS_MIRRORONCE\n"));
6110 if (pCaps->TextureAddressCaps & D3DPTADDRESSCAPS_WRAP)
6111 LogRel((" - D3DPTADDRESSCAPS_WRAP\n"));
6112
6113 LogRel(("\nTextureOpCaps:\n"));
6114 if (pCaps->TextureOpCaps & D3DTEXOPCAPS_DISABLE)
6115 LogRel((" - D3DTEXOPCAPS_DISABLE\n"));
6116 if (pCaps->TextureOpCaps & D3DTEXOPCAPS_SELECTARG1)
6117 LogRel((" - D3DTEXOPCAPS_SELECTARG1\n"));
6118 if (pCaps->TextureOpCaps & D3DTEXOPCAPS_SELECTARG2)
6119 LogRel((" - D3DTEXOPCAPS_SELECTARG2\n"));
6120 if (pCaps->TextureOpCaps & D3DTEXOPCAPS_MODULATE)
6121 LogRel((" - D3DTEXOPCAPS_MODULATE\n"));
6122 if (pCaps->TextureOpCaps & D3DTEXOPCAPS_MODULATE2X)
6123 LogRel((" - D3DTEXOPCAPS_MODULATE2X\n"));
6124 if (pCaps->TextureOpCaps & D3DTEXOPCAPS_MODULATE4X)
6125 LogRel((" - D3DTEXOPCAPS_MODULATE4X\n"));
6126 if (pCaps->TextureOpCaps & D3DTEXOPCAPS_ADD)
6127 LogRel((" - D3DTEXOPCAPS_ADD\n"));
6128 if (pCaps->TextureOpCaps & D3DTEXOPCAPS_ADDSIGNED)
6129 LogRel((" - D3DTEXOPCAPS_ADDSIGNED\n"));
6130 if (pCaps->TextureOpCaps & D3DTEXOPCAPS_ADDSIGNED2X)
6131 LogRel((" - D3DTEXOPCAPS_ADDSIGNED2X\n"));
6132 if (pCaps->TextureOpCaps & D3DTEXOPCAPS_SUBTRACT)
6133 LogRel((" - D3DTEXOPCAPS_SUBTRACT\n"));
6134 if (pCaps->TextureOpCaps & D3DTEXOPCAPS_ADDSMOOTH)
6135 LogRel((" - D3DTEXOPCAPS_ADDSMOOTH\n"));
6136 if (pCaps->TextureOpCaps & D3DTEXOPCAPS_BLENDDIFFUSEALPHA)
6137 LogRel((" - D3DTEXOPCAPS_BLENDDIFFUSEALPHA\n"));
6138 if (pCaps->TextureOpCaps & D3DTEXOPCAPS_BLENDTEXTUREALPHA)
6139 LogRel((" - D3DTEXOPCAPS_BLENDTEXTUREALPHA\n"));
6140 if (pCaps->TextureOpCaps & D3DTEXOPCAPS_BLENDFACTORALPHA)
6141 LogRel((" - D3DTEXOPCAPS_BLENDFACTORALPHA\n"));
6142 if (pCaps->TextureOpCaps & D3DTEXOPCAPS_BLENDTEXTUREALPHAPM)
6143 LogRel((" - D3DTEXOPCAPS_BLENDTEXTUREALPHAPM\n"));
6144 if (pCaps->TextureOpCaps & D3DTEXOPCAPS_BLENDCURRENTALPHA)
6145 LogRel((" - D3DTEXOPCAPS_BLENDCURRENTALPHA\n"));
6146 if (pCaps->TextureOpCaps & D3DTEXOPCAPS_PREMODULATE)
6147 LogRel((" - D3DTEXOPCAPS_PREMODULATE\n"));
6148 if (pCaps->TextureOpCaps & D3DTEXOPCAPS_MODULATEALPHA_ADDCOLOR)
6149 LogRel((" - D3DTEXOPCAPS_MODULATEALPHA_ADDCOLOR\n"));
6150 if (pCaps->TextureOpCaps & D3DTEXOPCAPS_MODULATECOLOR_ADDALPHA)
6151 LogRel((" - D3DTEXOPCAPS_MODULATECOLOR_ADDALPHA\n"));
6152 if (pCaps->TextureOpCaps & D3DTEXOPCAPS_MODULATEINVALPHA_ADDCOLOR)
6153 LogRel((" - D3DTEXOPCAPS_MODULATEINVALPHA_ADDCOLOR\n"));
6154 if (pCaps->TextureOpCaps & D3DTEXOPCAPS_MODULATEINVCOLOR_ADDALPHA)
6155 LogRel((" - D3DTEXOPCAPS_MODULATEINVCOLOR_ADDALPHA\n"));
6156 if (pCaps->TextureOpCaps & D3DTEXOPCAPS_BUMPENVMAP)
6157 LogRel((" - D3DTEXOPCAPS_BUMPENVMAP\n"));
6158 if (pCaps->TextureOpCaps & D3DTEXOPCAPS_BUMPENVMAPLUMINANCE)
6159 LogRel((" - D3DTEXOPCAPS_BUMPENVMAPLUMINANCE\n"));
6160 if (pCaps->TextureOpCaps & D3DTEXOPCAPS_DOTPRODUCT3)
6161 LogRel((" - D3DTEXOPCAPS_DOTPRODUCT3\n"));
6162 if (pCaps->TextureOpCaps & D3DTEXOPCAPS_MULTIPLYADD)
6163 LogRel((" - D3DTEXOPCAPS_MULTIPLYADD\n"));
6164 if (pCaps->TextureOpCaps & D3DTEXOPCAPS_LERP)
6165 LogRel((" - D3DTEXOPCAPS_LERP\n"));
6166
6167
6168 LogRel(("\n"));
6169 LogRel(("PixelShaderVersion: %#x (%u.%u)\n", pCaps->PixelShaderVersion,
6170 D3DSHADER_VERSION_MAJOR(pCaps->PixelShaderVersion), D3DSHADER_VERSION_MINOR(pCaps->PixelShaderVersion)));
6171 LogRel(("VertexShaderVersion: %#x (%u.%u)\n", pCaps->VertexShaderVersion,
6172 D3DSHADER_VERSION_MAJOR(pCaps->VertexShaderVersion), D3DSHADER_VERSION_MINOR(pCaps->VertexShaderVersion)));
6173
6174 LogRel(("\n"));
6175 RTLogRelSetBuffering(fBufferingSaved);
6176}
6177
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