VirtualBox

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

Last change on this file since 69833 was 69665, checked in by vboxsync, 7 years ago

Devices/Graphics: VMSVGA D3D backend: added SVGA3D_R8G8B8A8_UNORM, comment, fix for instancing cleanup.

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