VirtualBox

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

Last change on this file since 69304 was 69294, checked in by vboxsync, 7 years ago

Devices/Graphics: scm cleanups

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