VirtualBox

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

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

Devices/Graphics: VMSVGA: removed sidRenderTarget, because it does not work if the guest uses multiple render targets and render targets are already tracked anyway.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 268.9 KB
Line 
1/* $Id: DevVGA-SVGA3d-win.cpp 69859 2017-11-28 11:54:06Z 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->state.shidVertex = SVGA3D_INVALID_ID;
2799 pContext->state.shidPixel = SVGA3D_INVALID_ID;
2800
2801 for (uint32_t i = 0; i < RT_ELEMENTS(pContext->state.aRenderTargets); i++)
2802 pContext->state.aRenderTargets[i] = SVGA3D_INVALID_ID;
2803
2804 /* Create a context window. */
2805 CREATESTRUCT cs;
2806
2807 AssertReturn(pThis->svga.u64HostWindowId, VERR_INTERNAL_ERROR);
2808
2809 cs.lpCreateParams = NULL;
2810 cs.dwExStyle = WS_EX_NOACTIVATE | WS_EX_NOPARENTNOTIFY | WS_EX_TRANSPARENT;
2811#ifdef DEBUG_GFX_WINDOW
2812 cs.lpszName = (char *)RTMemAllocZ(256);
2813 RTStrPrintf((char *)cs.lpszName, 256, "Context %d OpenGL Window", cid);
2814#else
2815 cs.lpszName = NULL;
2816#endif
2817 cs.lpszClass = NULL;
2818#ifdef DEBUG_GFX_WINDOW
2819 cs.style = WS_CLIPSIBLINGS | WS_CLIPCHILDREN | WS_VISIBLE | WS_CAPTION;
2820#else
2821 cs.style = WS_CLIPSIBLINGS | WS_CLIPCHILDREN | WS_DISABLED | WS_CHILD | WS_VISIBLE;
2822#endif
2823 cs.x = 0;
2824 cs.y = 0;
2825 cs.cx = pThis->svga.uWidth;
2826 cs.cy = pThis->svga.uHeight;
2827 cs.hwndParent = (HWND)pThis->svga.u64HostWindowId;
2828 cs.hMenu = NULL;
2829 cs.hInstance = pState->hInstance;
2830
2831 rc = vmsvga3dSendThreadMessage(pState->pWindowThread, pState->WndRequestSem, WM_VMSVGA3D_CREATEWINDOW, (WPARAM)&pContext->hwnd, (LPARAM)&cs);
2832 AssertRCReturn(rc, rc);
2833
2834 /* Changed when the function returns. */
2835 PresParam.BackBufferWidth = 0;
2836 PresParam.BackBufferHeight = 0;
2837 PresParam.BackBufferFormat = D3DFMT_UNKNOWN;
2838 PresParam.BackBufferCount = 0;
2839
2840 PresParam.MultiSampleType = D3DMULTISAMPLE_NONE;
2841 PresParam.MultiSampleQuality = 0;
2842 PresParam.SwapEffect = D3DSWAPEFFECT_FLIP;
2843 PresParam.hDeviceWindow = pContext->hwnd;
2844 PresParam.Windowed = TRUE; /** @todo */
2845 PresParam.EnableAutoDepthStencil = FALSE;
2846 PresParam.AutoDepthStencilFormat = D3DFMT_UNKNOWN; /* not relevant */
2847 PresParam.Flags = 0;
2848 PresParam.FullScreen_RefreshRateInHz = 0; /* windowed -> 0 */
2849 /** @todo consider using D3DPRESENT_DONOTWAIT so we don't wait for the GPU during Present calls. */
2850 PresParam.PresentationInterval = D3DPRESENT_INTERVAL_IMMEDIATE;
2851
2852#ifdef VBOX_VMSVGA3D_WITH_WINE_OPENGL
2853 hr = pState->pD3D9->CreateDevice(D3DADAPTER_DEFAULT,
2854 D3DDEVTYPE_HAL,
2855 pContext->hwnd,
2856 D3DCREATE_MULTITHREADED | D3DCREATE_MIXED_VERTEXPROCESSING, //D3DCREATE_HARDWARE_VERTEXPROCESSING,
2857 &PresParam,
2858 &pContext->pDevice);
2859#else
2860 hr = pState->pD3D9->CreateDeviceEx(D3DADAPTER_DEFAULT,
2861 D3DDEVTYPE_HAL,
2862 pContext->hwnd,
2863 D3DCREATE_MULTITHREADED | D3DCREATE_MIXED_VERTEXPROCESSING, //D3DCREATE_HARDWARE_VERTEXPROCESSING,
2864 &PresParam,
2865 NULL,
2866 &pContext->pDevice);
2867#endif
2868 AssertMsgReturn(hr == D3D_OK, ("vmsvga3dContextDefine: CreateDevice failed with %x\n", hr), VERR_INTERNAL_ERROR);
2869
2870 Log(("vmsvga3dContextDefine: Backbuffer (%d,%d) count=%d format=%x\n", PresParam.BackBufferWidth, PresParam.BackBufferHeight, PresParam.BackBufferCount, PresParam.BackBufferFormat));
2871 return VINF_SUCCESS;
2872}
2873
2874/**
2875 * Destroy an existing 3d context
2876 *
2877 * @returns VBox status code.
2878 * @param pThis VGA device instance data.
2879 * @param cid Context id
2880 */
2881int vmsvga3dContextDestroy(PVGASTATE pThis, uint32_t cid)
2882{
2883 PVMSVGA3DSTATE pState = pThis->svga.p3dState;
2884 AssertReturn(pState, VERR_NO_MEMORY);
2885
2886 AssertReturn(cid < SVGA3D_MAX_CONTEXT_IDS, VERR_INVALID_PARAMETER);
2887
2888 if ( cid < pState->cContexts
2889 && pState->papContexts[cid]->id == cid)
2890 {
2891 PVMSVGA3DCONTEXT pContext = pState->papContexts[cid];
2892
2893 Log(("vmsvga3dContextDestroy id %x\n", cid));
2894
2895 /* Check for all surfaces that are associated with this context to remove all dependencies */
2896 for (uint32_t sid = 0; sid < pState->cSurfaces; sid++)
2897 {
2898 PVMSVGA3DSURFACE pSurface = pState->papSurfaces[sid];
2899 if ( pSurface->id == sid
2900 && pSurface->idAssociatedContext == cid)
2901 {
2902 int rc;
2903
2904 LogFunc(("Remove all dependencies for surface sid=%x\n", sid));
2905
2906 uint32_t surfaceFlags = pSurface->surfaceFlags;
2907 SVGA3dSurfaceFormat format = pSurface->format;
2908 SVGA3dSurfaceFace face[SVGA3D_MAX_SURFACE_FACES];
2909 uint32_t multisampleCount = pSurface->multiSampleCount;
2910 SVGA3dTextureFilter autogenFilter = pSurface->autogenFilter;
2911 SVGA3dSize *pMipLevelSize;
2912 uint32_t cFaces = pSurface->cFaces;
2913
2914 pMipLevelSize = (SVGA3dSize *)RTMemAllocZ(pSurface->faces[0].numMipLevels * pSurface->cFaces * sizeof(SVGA3dSize));
2915 AssertReturn(pMipLevelSize, VERR_NO_MEMORY);
2916
2917 for (uint32_t face=0; face < pSurface->cFaces; face++)
2918 {
2919 for (uint32_t i = 0; i < pSurface->faces[0].numMipLevels; i++)
2920 {
2921 uint32_t idx = i + face * pSurface->faces[0].numMipLevels;
2922 memcpy(&pMipLevelSize[idx], &pSurface->pMipmapLevels[idx].mipmapSize, sizeof(SVGA3dSize));
2923 }
2924 }
2925 memcpy(face, pSurface->faces, sizeof(pSurface->faces));
2926
2927 /* Recreate the surface with the original settings; destroys the contents, but that seems fairly safe since the context is also destroyed. */
2928 /** @todo not safe with shared objects */
2929 Assert(pSurface->pSharedObjectTree == NULL);
2930
2931 rc = vmsvga3dSurfaceDestroy(pThis, sid);
2932 AssertRC(rc);
2933
2934 rc = vmsvga3dSurfaceDefine(pThis, sid, surfaceFlags, format, face, multisampleCount, autogenFilter, face[0].numMipLevels * cFaces, pMipLevelSize);
2935 AssertRC(rc);
2936
2937 Assert(!pSurface->u.pSurface);
2938 }
2939 else
2940 {
2941 /* Check for a shared surface object. */
2942 PVMSVGA3DSHAREDSURFACE pSharedSurface = (PVMSVGA3DSHAREDSURFACE)RTAvlU32Get(&pSurface->pSharedObjectTree, cid);
2943 if (pSharedSurface)
2944 {
2945 LogFunc(("Remove shared dependency for surface sid=%x\n", sid));
2946
2947 switch (pSurface->enmD3DResType)
2948 {
2949 case VMSVGA3D_D3DRESTYPE_TEXTURE:
2950 Assert(pSharedSurface->u.pTexture);
2951 D3D_RELEASE(pSharedSurface->u.pTexture);
2952 break;
2953
2954 case VMSVGA3D_D3DRESTYPE_CUBE_TEXTURE:
2955 Assert(pSharedSurface->u.pCubeTexture);
2956 D3D_RELEASE(pSharedSurface->u.pCubeTexture);
2957 break;
2958
2959 case VMSVGA3D_D3DRESTYPE_VOLUME_TEXTURE:
2960 Assert(pSharedSurface->u.pVolumeTexture);
2961 D3D_RELEASE(pSharedSurface->u.pVolumeTexture);
2962 break;
2963
2964 default:
2965 AssertFailed();
2966 break;
2967 }
2968 RTAvlU32Remove(&pSurface->pSharedObjectTree, cid);
2969 RTMemFree(pSharedSurface);
2970 }
2971 }
2972 }
2973
2974 /* Destroy all leftover pixel shaders. */
2975 for (uint32_t i = 0; i < pContext->cPixelShaders; i++)
2976 {
2977 if (pContext->paPixelShader[i].id != SVGA3D_INVALID_ID)
2978 vmsvga3dShaderDestroy(pThis, pContext->paPixelShader[i].cid, pContext->paPixelShader[i].id, pContext->paPixelShader[i].type);
2979 }
2980 if (pContext->paPixelShader)
2981 RTMemFree(pContext->paPixelShader);
2982
2983 /* Destroy all leftover vertex shaders. */
2984 for (uint32_t i = 0; i < pContext->cVertexShaders; i++)
2985 {
2986 if (pContext->paVertexShader[i].id != SVGA3D_INVALID_ID)
2987 vmsvga3dShaderDestroy(pThis, pContext->paVertexShader[i].cid, pContext->paVertexShader[i].id, pContext->paVertexShader[i].type);
2988 }
2989 if (pContext->paVertexShader)
2990 RTMemFree(pContext->paVertexShader);
2991
2992 if (pContext->state.paVertexShaderConst)
2993 RTMemFree(pContext->state.paVertexShaderConst);
2994 if (pContext->state.paPixelShaderConst)
2995 RTMemFree(pContext->state.paPixelShaderConst);
2996
2997 vmsvga3dOcclusionQueryDelete(pState, pContext);
2998
2999 /* Release the D3D device object */
3000 D3D_RELEASE(pContext->pDevice);
3001
3002 /* Destroy the window we've created. */
3003 int rc = vmsvga3dSendThreadMessage(pState->pWindowThread, pState->WndRequestSem, WM_VMSVGA3D_DESTROYWINDOW, (WPARAM)pContext->hwnd, 0);
3004 AssertRC(rc);
3005
3006 memset(pContext, 0, sizeof(*pContext));
3007 pContext->id = SVGA3D_INVALID_ID;
3008 }
3009 else
3010 AssertFailed();
3011
3012 return VINF_SUCCESS;
3013}
3014
3015static int vmsvga3dContextTrackUsage(PVGASTATE pThis, PVMSVGA3DCONTEXT pContext)
3016{
3017#ifndef VBOX_VMSVGA3D_WITH_WINE_OPENGL
3018 PVMSVGA3DSTATE pState = pThis->svga.p3dState;
3019 AssertReturn(pState, VERR_NO_MEMORY);
3020
3021 /* Inject fences to make sure we can track surface usage in case the client wants to reuse it in another context. */
3022 for (uint32_t i = 0; i < RT_ELEMENTS(pContext->aSidActiveTextures); ++i)
3023 {
3024 if (pContext->aSidActiveTextures[i] != SVGA3D_INVALID_ID)
3025 vmsvga3dSurfaceTrackUsageById(pState, pContext, pContext->aSidActiveTextures[i]);
3026 }
3027 for (uint32_t i = 0; i < RT_ELEMENTS(pContext->state.aRenderTargets); ++i)
3028 if (pContext->state.aRenderTargets[i] != SVGA3D_INVALID_ID)
3029 vmsvga3dSurfaceTrackUsageById(pState, pContext, pContext->state.aRenderTargets[i]);
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 if (pState->fSupportedSurfaceNULL)
4160 {
4161 /* Create a dummy render target to satisfy D3D. This path is usually taken only to render
4162 * into a depth buffer without wishing to update an actual color render target.
4163 */
4164 IDirect3DSurface9 *pDummyRenderTarget;
4165 hr = pContext->pDevice->CreateRenderTarget(pThis->svga.uWidth,
4166 pThis->svga.uHeight,
4167 FOURCC_NULL,
4168 D3DMULTISAMPLE_NONE,
4169 0,
4170 FALSE,
4171 &pDummyRenderTarget,
4172 NULL);
4173
4174 AssertMsgReturn(hr == D3D_OK, ("CreateRenderTarget failed with %x\n", hr), VERR_INTERNAL_ERROR);
4175
4176 hr = pContext->pDevice->SetRenderTarget(type - SVGA3D_RT_COLOR0, pDummyRenderTarget);
4177 D3D_RELEASE(pDummyRenderTarget);
4178 }
4179 else
4180 hr = pContext->pDevice->SetRenderTarget(type - SVGA3D_RT_COLOR0, NULL);
4181
4182 AssertMsgReturn(hr == D3D_OK, ("SetRenderTarget failed with %x\n", hr), VERR_INTERNAL_ERROR);
4183 break;
4184
4185 default:
4186 AssertFailedReturn(VERR_INVALID_PARAMETER);
4187 }
4188 return VINF_SUCCESS;
4189 }
4190
4191 rc = vmsvga3dSurfaceFromSid(pState, target.sid, &pRenderTarget);
4192 AssertRCReturn(rc, rc);
4193
4194 switch (type)
4195 {
4196 case SVGA3D_RT_DEPTH:
4197 case SVGA3D_RT_STENCIL:
4198 AssertReturn(target.face == 0 && target.mipmap == 0, VERR_INVALID_PARAMETER);
4199 if (!pRenderTarget->u.pSurface)
4200 {
4201 DWORD cQualityLevels = 0;
4202
4203 /* Query the nr of quality levels for this particular format */
4204 if (pRenderTarget->multiSampleTypeD3D != D3DMULTISAMPLE_NONE)
4205 {
4206 hr = pState->pD3D9->CheckDeviceMultiSampleType(D3DADAPTER_DEFAULT,
4207 D3DDEVTYPE_HAL,
4208 pRenderTarget->formatD3D,
4209 TRUE, /* Windowed */
4210 pRenderTarget->multiSampleTypeD3D,
4211 &cQualityLevels);
4212 Assert(hr == D3D_OK || hr == D3DERR_NOTAVAILABLE);
4213 }
4214
4215 if ( pState->fSupportedSurfaceINTZ
4216 && pRenderTarget->multiSampleTypeD3D == D3DMULTISAMPLE_NONE
4217 && ( pRenderTarget->formatD3D == D3DFMT_D24S8
4218 || pRenderTarget->formatD3D == D3DFMT_D24X8))
4219 {
4220 LogFunc(("Creating stencil surface as texture!\n"));
4221 int rc = vmsvga3dBackCreateTexture(pState, pContext, cid, pRenderTarget);
4222 AssertRC(rc); /* non-fatal, will use CreateDepthStencilSurface */
4223 }
4224
4225 if (!pRenderTarget->fStencilAsTexture)
4226 {
4227 Assert(!pRenderTarget->u.pSurface);
4228
4229 LogFunc(("DEPTH/STENCIL; cQualityLevels=%d\n", cQualityLevels));
4230 hr = pContext->pDevice->CreateDepthStencilSurface(pRenderTarget->pMipmapLevels[0].mipmapSize.width,
4231 pRenderTarget->pMipmapLevels[0].mipmapSize.height,
4232 pRenderTarget->formatD3D,
4233 pRenderTarget->multiSampleTypeD3D,
4234 ((cQualityLevels >= 1) ? cQualityLevels - 1 : 0), /* 0 - (levels-1) */
4235 FALSE, /* not discardable */
4236 &pRenderTarget->u.pSurface,
4237 NULL);
4238 AssertMsgReturn(hr == D3D_OK, ("CreateDepthStencilSurface failed with %x\n", hr), VERR_INTERNAL_ERROR);
4239 pRenderTarget->enmD3DResType = VMSVGA3D_D3DRESTYPE_SURFACE;
4240 }
4241
4242 pRenderTarget->idAssociatedContext = cid;
4243
4244#if 0 /* doesn't work */
4245 if ( !pRenderTarget->fStencilAsTexture
4246 && pRenderTarget->fDirty)
4247 {
4248 Log(("vmsvga3dSetRenderTarget: sync dirty depth/stencil buffer\n"));
4249 Assert(pRenderTarget->faces[0].numMipLevels == 1);
4250
4251 for (uint32_t i = 0; i < pRenderTarget->faces[0].numMipLevels; i++)
4252 {
4253 if (pRenderTarget->pMipmapLevels[i].fDirty)
4254 {
4255 D3DLOCKED_RECT LockedRect;
4256
4257 hr = pRenderTarget->u.pSurface->LockRect(&LockedRect,
4258 NULL, /* entire surface */
4259 0);
4260
4261 AssertMsgReturn(hr == D3D_OK, ("vmsvga3dSetRenderTarget: LockRect failed with %x\n", hr), VERR_INTERNAL_ERROR);
4262
4263 Log(("vmsvga3dSetRenderTarget: sync dirty texture mipmap level %d (pitch %x vs %x)\n", i, LockedRect.Pitch, pRenderTarget->pMipmapLevels[i].cbSurfacePitch));
4264
4265 uint8_t *pDest = (uint8_t *)LockedRect.pBits;
4266 uint8_t *pSrc = (uint8_t *)pRenderTarget->pMipmapLevels[i].pSurfaceData;
4267 for (uint32_t j = 0; j < pRenderTarget->pMipmapLevels[i].size.height; j++)
4268 {
4269 memcpy(pDest, pSrc, pRenderTarget->pMipmapLevels[i].cbSurfacePitch);
4270
4271 pDest += LockedRect.Pitch;
4272 pSrc += pRenderTarget->pMipmapLevels[i].cbSurfacePitch;
4273 }
4274
4275 hr = pRenderTarget->u.pSurface->UnlockRect();
4276 AssertMsgReturn(hr == D3D_OK, ("vmsvga3dSetRenderTarget: UnlockRect failed with %x\n", hr), VERR_INTERNAL_ERROR);
4277
4278 pRenderTarget->pMipmapLevels[i].fDirty = false;
4279 }
4280 }
4281 }
4282#endif
4283 }
4284 Assert(pRenderTarget->idAssociatedContext == cid);
4285
4286 /** @todo Assert(!pRenderTarget->fDirty); */
4287
4288 AssertReturn(pRenderTarget->u.pSurface, VERR_INVALID_PARAMETER);
4289
4290 pRenderTarget->fUsageD3D |= D3DUSAGE_DEPTHSTENCIL;
4291 pRenderTarget->surfaceFlags |= SVGA3D_SURFACE_HINT_DEPTHSTENCIL;
4292
4293 if (pRenderTarget->fStencilAsTexture)
4294 {
4295 IDirect3DSurface9 *pStencilSurface;
4296
4297 hr = pRenderTarget->u.pTexture->GetSurfaceLevel(0, &pStencilSurface);
4298 AssertMsgReturn(hr == D3D_OK, ("GetSurfaceLevel failed with %x\n", hr), VERR_INTERNAL_ERROR);
4299
4300 hr = pContext->pDevice->SetDepthStencilSurface(pStencilSurface);
4301 D3D_RELEASE(pStencilSurface);
4302 AssertMsgReturn(hr == D3D_OK, ("SetDepthStencilSurface failed with %x\n", hr), VERR_INTERNAL_ERROR);
4303 }
4304 else
4305 {
4306 hr = pContext->pDevice->SetDepthStencilSurface(pRenderTarget->u.pSurface);
4307 AssertMsgReturn(hr == D3D_OK, ("SetDepthStencilSurface failed with %x\n", hr), VERR_INTERNAL_ERROR);
4308 }
4309 break;
4310
4311 case SVGA3D_RT_COLOR0:
4312 case SVGA3D_RT_COLOR1:
4313 case SVGA3D_RT_COLOR2:
4314 case SVGA3D_RT_COLOR3:
4315 case SVGA3D_RT_COLOR4:
4316 case SVGA3D_RT_COLOR5:
4317 case SVGA3D_RT_COLOR6:
4318 case SVGA3D_RT_COLOR7:
4319 {
4320 IDirect3DSurface9 *pSurface;
4321 bool fTexture = false;
4322
4323 /* Must flush the other context's 3d pipeline to make sure all drawing is complete for the surface we're about to use. */
4324 vmsvga3dSurfaceFlush(pThis, pRenderTarget);
4325
4326 if (pRenderTarget->surfaceFlags & SVGA3D_SURFACE_HINT_TEXTURE)
4327 {
4328 fTexture = true;
4329
4330 /* A texture surface can be used as a render target to fill it and later on used as a texture. */
4331 if (!pRenderTarget->u.pTexture)
4332 {
4333 LogFunc(("Create texture to be used as render target; sid=%x type=%d format=%d -> create texture\n", target.sid, pRenderTarget->surfaceFlags, pRenderTarget->format));
4334 int rc = vmsvga3dBackCreateTexture(pState, pContext, cid, pRenderTarget);
4335 AssertRCReturn(rc, rc);
4336 }
4337
4338 rc = vmsvga3dGetD3DSurface(pContext, pRenderTarget, target.face, target.mipmap, false, &pSurface);
4339 AssertRCReturn(rc, rc);
4340 }
4341 else
4342 {
4343 AssertReturn(target.face == 0 && target.mipmap == 0, VERR_INVALID_PARAMETER);
4344 if (!pRenderTarget->u.pSurface)
4345 {
4346 DWORD cQualityLevels = 0;
4347
4348 /* Query the nr of quality levels for this particular format */
4349 if (pRenderTarget->multiSampleTypeD3D != D3DMULTISAMPLE_NONE)
4350 {
4351 hr = pState->pD3D9->CheckDeviceMultiSampleType(D3DADAPTER_DEFAULT,
4352 D3DDEVTYPE_HAL,
4353 pRenderTarget->formatD3D,
4354 TRUE, /* Windowed */
4355 pRenderTarget->multiSampleTypeD3D,
4356 &cQualityLevels);
4357 Assert(hr == D3D_OK || hr == D3DERR_NOTAVAILABLE);
4358 }
4359
4360 LogFunc(("COLOR; cQualityLevels=%d\n", cQualityLevels));
4361 LogFunc(("Create rendertarget (%d,%d) formatD3D=%x multisample=%x\n",
4362 pRenderTarget->pMipmapLevels[0].mipmapSize.width, pRenderTarget->pMipmapLevels[0].mipmapSize.height, pRenderTarget->formatD3D, pRenderTarget->multiSampleTypeD3D));
4363
4364 hr = pContext->pDevice->CreateRenderTarget(pRenderTarget->pMipmapLevels[0].mipmapSize.width,
4365 pRenderTarget->pMipmapLevels[0].mipmapSize.height,
4366 pRenderTarget->formatD3D,
4367 pRenderTarget->multiSampleTypeD3D,
4368 ((cQualityLevels >= 1) ? cQualityLevels - 1 : 0), /* 0 - (levels-1) */
4369 TRUE, /* lockable */
4370 &pRenderTarget->u.pSurface,
4371 NULL);
4372 AssertReturn(hr == D3D_OK, VERR_INTERNAL_ERROR);
4373
4374 pRenderTarget->idAssociatedContext = cid;
4375 pRenderTarget->enmD3DResType = VMSVGA3D_D3DRESTYPE_SURFACE;
4376 }
4377 else
4378 AssertReturn(pRenderTarget->fUsageD3D & D3DUSAGE_RENDERTARGET, VERR_INVALID_PARAMETER);
4379
4380 Assert(pRenderTarget->idAssociatedContext == cid);
4381 Assert(pRenderTarget->enmD3DResType == VMSVGA3D_D3DRESTYPE_SURFACE);
4382 pSurface = pRenderTarget->u.pSurface;
4383 }
4384
4385 AssertReturn(pRenderTarget->u.pSurface, VERR_INVALID_PARAMETER);
4386 Assert(!pRenderTarget->fDirty);
4387
4388 pRenderTarget->fUsageD3D |= D3DUSAGE_RENDERTARGET;
4389 pRenderTarget->surfaceFlags |= SVGA3D_SURFACE_HINT_RENDERTARGET;
4390
4391 hr = pContext->pDevice->SetRenderTarget(type - SVGA3D_RT_COLOR0, pSurface);
4392 if (fTexture)
4393 D3D_RELEASE(pSurface); /* Release reference to texture level 0 */
4394 AssertMsgReturn(hr == D3D_OK, ("SetRenderTarget failed with %x\n", hr), VERR_INTERNAL_ERROR);
4395
4396 /* Changing the render target resets the viewport; restore it here. */
4397 if (pContext->state.u32UpdateFlags & VMSVGA3D_UPDATE_VIEWPORT)
4398 vmsvga3dSetViewPort(pThis, cid, &pContext->state.RectViewPort);
4399 /* Changing the render target also resets the scissor rectangle; restore it as well. */
4400 if (pContext->state.u32UpdateFlags & VMSVGA3D_UPDATE_SCISSORRECT)
4401 vmsvga3dSetScissorRect(pThis, cid, &pContext->state.RectScissor);
4402
4403 break;
4404 }
4405
4406 default:
4407 AssertFailedReturn(VERR_INVALID_PARAMETER);
4408 }
4409
4410 return VINF_SUCCESS;
4411}
4412
4413/**
4414 * Convert SVGA texture combiner value to its D3D equivalent
4415 */
4416static DWORD vmsvga3dTextureCombiner2D3D(uint32_t value)
4417{
4418 switch (value)
4419 {
4420 case SVGA3D_TC_DISABLE:
4421 return D3DTOP_DISABLE;
4422 case SVGA3D_TC_SELECTARG1:
4423 return D3DTOP_SELECTARG1;
4424 case SVGA3D_TC_SELECTARG2:
4425 return D3DTOP_SELECTARG2;
4426 case SVGA3D_TC_MODULATE:
4427 return D3DTOP_MODULATE;
4428 case SVGA3D_TC_ADD:
4429 return D3DTOP_ADD;
4430 case SVGA3D_TC_ADDSIGNED:
4431 return D3DTOP_ADDSIGNED;
4432 case SVGA3D_TC_SUBTRACT:
4433 return D3DTOP_SUBTRACT;
4434 case SVGA3D_TC_BLENDTEXTUREALPHA:
4435 return D3DTOP_BLENDTEXTUREALPHA;
4436 case SVGA3D_TC_BLENDDIFFUSEALPHA:
4437 return D3DTOP_BLENDDIFFUSEALPHA;
4438 case SVGA3D_TC_BLENDCURRENTALPHA:
4439 return D3DTOP_BLENDCURRENTALPHA;
4440 case SVGA3D_TC_BLENDFACTORALPHA:
4441 return D3DTOP_BLENDFACTORALPHA;
4442 case SVGA3D_TC_MODULATE2X:
4443 return D3DTOP_MODULATE2X;
4444 case SVGA3D_TC_MODULATE4X:
4445 return D3DTOP_MODULATE4X;
4446 case SVGA3D_TC_DSDT:
4447 AssertFailed(); /** @todo ??? */
4448 return D3DTOP_DISABLE;
4449 case SVGA3D_TC_DOTPRODUCT3:
4450 return D3DTOP_DOTPRODUCT3;
4451 case SVGA3D_TC_BLENDTEXTUREALPHAPM:
4452 return D3DTOP_BLENDTEXTUREALPHAPM;
4453 case SVGA3D_TC_ADDSIGNED2X:
4454 return D3DTOP_ADDSIGNED2X;
4455 case SVGA3D_TC_ADDSMOOTH:
4456 return D3DTOP_ADDSMOOTH;
4457 case SVGA3D_TC_PREMODULATE:
4458 return D3DTOP_PREMODULATE;
4459 case SVGA3D_TC_MODULATEALPHA_ADDCOLOR:
4460 return D3DTOP_MODULATEALPHA_ADDCOLOR;
4461 case SVGA3D_TC_MODULATECOLOR_ADDALPHA:
4462 return D3DTOP_MODULATECOLOR_ADDALPHA;
4463 case SVGA3D_TC_MODULATEINVALPHA_ADDCOLOR:
4464 return D3DTOP_MODULATEINVALPHA_ADDCOLOR;
4465 case SVGA3D_TC_MODULATEINVCOLOR_ADDALPHA:
4466 return D3DTOP_MODULATEINVCOLOR_ADDALPHA;
4467 case SVGA3D_TC_BUMPENVMAPLUMINANCE:
4468 return D3DTOP_BUMPENVMAPLUMINANCE;
4469 case SVGA3D_TC_MULTIPLYADD:
4470 return D3DTOP_MULTIPLYADD;
4471 case SVGA3D_TC_LERP:
4472 return D3DTOP_LERP;
4473 default:
4474 AssertFailed();
4475 return D3DTOP_DISABLE;
4476 }
4477}
4478
4479/**
4480 * Convert SVGA texture arg data value to its D3D equivalent
4481 */
4482static DWORD vmsvga3dTextureArgData2D3D(uint32_t value)
4483{
4484 switch (value)
4485 {
4486 case SVGA3D_TA_CONSTANT:
4487 return D3DTA_CONSTANT;
4488 case SVGA3D_TA_PREVIOUS:
4489 return D3DTA_CURRENT; /* current = previous */
4490 case SVGA3D_TA_DIFFUSE:
4491 return D3DTA_DIFFUSE;
4492 case SVGA3D_TA_TEXTURE:
4493 return D3DTA_TEXTURE;
4494 case SVGA3D_TA_SPECULAR:
4495 return D3DTA_SPECULAR;
4496 default:
4497 AssertFailed();
4498 return 0;
4499 }
4500}
4501
4502/**
4503 * Convert SVGA texture transform flag value to its D3D equivalent
4504 */
4505static DWORD vmsvga3dTextTransformFlags2D3D(uint32_t value)
4506{
4507 switch (value)
4508 {
4509 case SVGA3D_TEX_TRANSFORM_OFF:
4510 return D3DTTFF_DISABLE;
4511 case SVGA3D_TEX_TRANSFORM_S:
4512 return D3DTTFF_COUNT1; /** @todo correct? */
4513 case SVGA3D_TEX_TRANSFORM_T:
4514 return D3DTTFF_COUNT2; /** @todo correct? */
4515 case SVGA3D_TEX_TRANSFORM_R:
4516 return D3DTTFF_COUNT3; /** @todo correct? */
4517 case SVGA3D_TEX_TRANSFORM_Q:
4518 return D3DTTFF_COUNT4; /** @todo correct? */
4519 case SVGA3D_TEX_PROJECTED:
4520 return D3DTTFF_PROJECTED;
4521 default:
4522 AssertFailed();
4523 return 0;
4524 }
4525}
4526
4527static DWORD vmsvga3dSamplerIndex2D3D(uint32_t idxSampler)
4528{
4529 if (idxSampler < SVGA3D_MAX_SAMPLERS_PS)
4530 return idxSampler;
4531 return (idxSampler - SVGA3D_MAX_SAMPLERS_PS) + D3DDMAPSAMPLER;
4532}
4533
4534int vmsvga3dSetTextureState(PVGASTATE pThis, uint32_t cid, uint32_t cTextureStates, SVGA3dTextureState *pTextureState)
4535{
4536 DWORD val = 0; /* Shut up MSC */
4537 HRESULT hr;
4538 PVMSVGA3DCONTEXT pContext;
4539 PVMSVGA3DSTATE pState = pThis->svga.p3dState;
4540 AssertReturn(pState, VERR_NO_MEMORY);
4541
4542 LogFunc(("%x cTextureState=%d\n", cid, cTextureStates));
4543
4544 int rc = vmsvga3dContextFromCid(pState, cid, &pContext);
4545 AssertRCReturn(rc, rc);
4546
4547 for (unsigned i = 0; i < cTextureStates; i++)
4548 {
4549 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));
4550
4551 if (pTextureState[i].name == SVGA3D_TS_BIND_TEXTURE)
4552 {
4553 /* Special case: binding a texture to a sampler. Stage is the sampler index. */
4554 const uint32_t sid = pTextureState[i].value;
4555 const uint32_t idxSampler = pTextureState[i].stage;
4556
4557 if (RT_UNLIKELY(idxSampler >= SVGA3D_MAX_SAMPLERS))
4558 {
4559 AssertMsgFailed(("pTextureState[%d]: SVGA3D_TS_BIND_TEXTURE idxSampler=%d, sid=%x\n", i, idxSampler, sid));
4560 continue;
4561 }
4562
4563 const DWORD d3dSampler = vmsvga3dSamplerIndex2D3D(idxSampler);
4564 if (sid == SVGA3D_INVALID_ID)
4565 {
4566 LogFunc(("SVGA3D_TS_BIND_TEXTURE: unbind sampler=%d\n", idxSampler));
4567
4568 pContext->aSidActiveTextures[idxSampler] = SVGA3D_INVALID_ID;
4569
4570 /* Unselect the currently associated texture. */
4571 hr = pContext->pDevice->SetTexture(d3dSampler, NULL);
4572 AssertMsgReturn(hr == D3D_OK, ("SetTexture failed with %x\n", hr), VERR_INTERNAL_ERROR);
4573 }
4574 else
4575 {
4576 PVMSVGA3DSURFACE pSurface;
4577 rc = vmsvga3dSurfaceFromSid(pState, sid, &pSurface);
4578 AssertRCReturn(rc, rc);
4579
4580 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));
4581
4582 if (!pSurface->u.pTexture)
4583 {
4584 Assert(pSurface->idAssociatedContext == SVGA3D_INVALID_ID);
4585 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));
4586 rc = vmsvga3dBackCreateTexture(pState, pContext, cid, pSurface);
4587 AssertRCReturn(rc, rc);
4588 }
4589 else
4590 {
4591 Assert( pSurface->enmD3DResType == VMSVGA3D_D3DRESTYPE_TEXTURE
4592 || pSurface->enmD3DResType == VMSVGA3D_D3DRESTYPE_CUBE_TEXTURE
4593 || pSurface->enmD3DResType == VMSVGA3D_D3DRESTYPE_VOLUME_TEXTURE);
4594 /* Must flush the other context's 3d pipeline to make sure all drawing is complete for the surface we're about to use. */
4595 vmsvga3dSurfaceFlush(pThis, pSurface);
4596 }
4597
4598#ifndef VBOX_VMSVGA3D_WITH_WINE_OPENGL
4599 if (pSurface->idAssociatedContext != cid)
4600 {
4601 LogFunc(("Using texture sid=%x created for another context (%d vs %d)\n", sid, pSurface->idAssociatedContext, cid));
4602
4603 PVMSVGA3DSHAREDSURFACE pSharedSurface = vmsvga3dSurfaceGetSharedCopy(pContext, pSurface);
4604 AssertReturn(pSharedSurface, VERR_INTERNAL_ERROR);
4605
4606 hr = pContext->pDevice->SetTexture(d3dSampler, pSharedSurface->u.pTexture);
4607 }
4608 else
4609#endif
4610 hr = pContext->pDevice->SetTexture(d3dSampler, pSurface->u.pTexture);
4611
4612 AssertMsgReturn(hr == D3D_OK, ("SetTexture failed with %x\n", hr), VERR_INTERNAL_ERROR);
4613
4614 pContext->aSidActiveTextures[idxSampler] = sid;
4615 }
4616 /* Finished; continue with the next one. */
4617 continue;
4618 }
4619
4620 D3DTEXTURESTAGESTATETYPE textureType = D3DTSS_FORCE_DWORD;
4621 D3DSAMPLERSTATETYPE samplerType = D3DSAMP_FORCE_DWORD;
4622 switch (pTextureState[i].name)
4623 {
4624 case SVGA3D_TS_COLOROP: /* SVGA3dTextureCombiner */
4625 textureType = D3DTSS_COLOROP;
4626 val = vmsvga3dTextureCombiner2D3D(pTextureState[i].value);
4627 break;
4628
4629 case SVGA3D_TS_COLORARG0: /* SVGA3dTextureArgData */
4630 textureType = D3DTSS_COLORARG0;
4631 val = vmsvga3dTextureArgData2D3D(pTextureState[i].value);
4632 break;
4633
4634 case SVGA3D_TS_COLORARG1: /* SVGA3dTextureArgData */
4635 textureType = D3DTSS_COLORARG1;
4636 val = vmsvga3dTextureArgData2D3D(pTextureState[i].value);
4637 break;
4638
4639 case SVGA3D_TS_COLORARG2: /* SVGA3dTextureArgData */
4640 textureType = D3DTSS_COLORARG2;
4641 val = vmsvga3dTextureArgData2D3D(pTextureState[i].value);
4642 break;
4643
4644 case SVGA3D_TS_ALPHAOP: /* SVGA3dTextureCombiner */
4645 textureType = D3DTSS_ALPHAOP;
4646 val = vmsvga3dTextureCombiner2D3D(pTextureState[i].value);
4647 break;
4648
4649 case SVGA3D_TS_ALPHAARG0: /* SVGA3dTextureArgData */
4650 textureType = D3DTSS_ALPHAARG0;
4651 val = vmsvga3dTextureArgData2D3D(pTextureState[i].value);
4652 break;
4653
4654 case SVGA3D_TS_ALPHAARG1: /* SVGA3dTextureArgData */
4655 textureType = D3DTSS_ALPHAARG1;
4656 val = vmsvga3dTextureArgData2D3D(pTextureState[i].value);
4657 break;
4658
4659 case SVGA3D_TS_ALPHAARG2: /* SVGA3dTextureArgData */
4660 textureType = D3DTSS_ALPHAARG2;
4661 val = vmsvga3dTextureArgData2D3D(pTextureState[i].value);
4662 break;
4663
4664 case SVGA3D_TS_BUMPENVMAT00: /* float */
4665 textureType = D3DTSS_BUMPENVMAT00;
4666 val = pTextureState[i].value;
4667 break;
4668
4669 case SVGA3D_TS_BUMPENVMAT01: /* float */
4670 textureType = D3DTSS_BUMPENVMAT01;
4671 val = pTextureState[i].value;
4672 break;
4673
4674 case SVGA3D_TS_BUMPENVMAT10: /* float */
4675 textureType = D3DTSS_BUMPENVMAT10;
4676 val = pTextureState[i].value;
4677 break;
4678
4679 case SVGA3D_TS_BUMPENVMAT11: /* float */
4680 textureType = D3DTSS_BUMPENVMAT11;
4681 val = pTextureState[i].value;
4682 break;
4683
4684 case SVGA3D_TS_TEXCOORDINDEX: /* uint32_t */
4685 textureType = D3DTSS_TEXCOORDINDEX;
4686 val = pTextureState[i].value;
4687 break;
4688
4689 case SVGA3D_TS_BUMPENVLSCALE: /* float */
4690 textureType = D3DTSS_BUMPENVLSCALE;
4691 val = pTextureState[i].value;
4692 break;
4693
4694 case SVGA3D_TS_BUMPENVLOFFSET: /* float */
4695 textureType = D3DTSS_BUMPENVLOFFSET;
4696 val = pTextureState[i].value;
4697 break;
4698
4699 case SVGA3D_TS_TEXTURETRANSFORMFLAGS: /* SVGA3dTexTransformFlags */
4700 textureType = D3DTSS_TEXTURETRANSFORMFLAGS;
4701 val = vmsvga3dTextTransformFlags2D3D(pTextureState[i].value);
4702 break;
4703
4704 case SVGA3D_TS_ADDRESSW: /* SVGA3dTextureAddress */
4705 samplerType = D3DSAMP_ADDRESSW;
4706 val = pTextureState[i].value; /* Identical otherwise */
4707 Assert(pTextureState[i].value != SVGA3D_TEX_ADDRESS_EDGE);
4708 break;
4709
4710 case SVGA3D_TS_ADDRESSU: /* SVGA3dTextureAddress */
4711 samplerType = D3DSAMP_ADDRESSU;
4712 val = pTextureState[i].value; /* Identical otherwise */
4713 Assert(pTextureState[i].value != SVGA3D_TEX_ADDRESS_EDGE);
4714 break;
4715
4716 case SVGA3D_TS_ADDRESSV: /* SVGA3dTextureAddress */
4717 samplerType = D3DSAMP_ADDRESSV;
4718 val = pTextureState[i].value; /* Identical otherwise */
4719 Assert(pTextureState[i].value != SVGA3D_TEX_ADDRESS_EDGE);
4720 break;
4721
4722 case SVGA3D_TS_MIPFILTER: /* SVGA3dTextureFilter */
4723 samplerType = D3DSAMP_MIPFILTER;
4724 val = pTextureState[i].value; /* Identical otherwise */
4725 Assert(pTextureState[i].value != SVGA3D_TEX_FILTER_FLATCUBIC);
4726 Assert(pTextureState[i].value != SVGA3D_TEX_FILTER_GAUSSIANCUBIC);
4727 break;
4728
4729 case SVGA3D_TS_MAGFILTER: /* SVGA3dTextureFilter */
4730 samplerType = D3DSAMP_MAGFILTER;
4731 val = pTextureState[i].value; /* Identical otherwise */
4732 Assert(pTextureState[i].value != SVGA3D_TEX_FILTER_FLATCUBIC);
4733 Assert(pTextureState[i].value != SVGA3D_TEX_FILTER_GAUSSIANCUBIC);
4734 break;
4735
4736 case SVGA3D_TS_MINFILTER: /* SVGA3dTextureFilter */
4737 samplerType = D3DSAMP_MINFILTER;
4738 val = pTextureState[i].value; /* Identical otherwise */
4739 Assert(pTextureState[i].value != SVGA3D_TEX_FILTER_FLATCUBIC);
4740 Assert(pTextureState[i].value != SVGA3D_TEX_FILTER_GAUSSIANCUBIC);
4741 break;
4742
4743 case SVGA3D_TS_BORDERCOLOR: /* SVGA3dColor */
4744 samplerType = D3DSAMP_BORDERCOLOR;
4745 val = pTextureState[i].value; /* Identical */
4746 break;
4747
4748 case SVGA3D_TS_TEXTURE_LOD_BIAS: /* float */
4749 samplerType = D3DSAMP_MIPMAPLODBIAS;
4750 val = pTextureState[i].value; /* Identical */
4751 break;
4752
4753 case SVGA3D_TS_TEXTURE_MIPMAP_LEVEL: /* uint32_t */
4754 samplerType = D3DSAMP_MAXMIPLEVEL;
4755 val = pTextureState[i].value; /* Identical?? */
4756 break;
4757
4758 case SVGA3D_TS_TEXTURE_ANISOTROPIC_LEVEL: /* uint32_t */
4759 samplerType = D3DSAMP_MAXANISOTROPY;
4760 val = pTextureState[i].value; /* Identical?? */
4761 break;
4762
4763 case SVGA3D_TS_GAMMA: /* float */
4764 samplerType = D3DSAMP_SRGBTEXTURE;
4765 /* Boolean in D3D */
4766 if (pTextureState[i].floatValue == 1.0f)
4767 val = FALSE;
4768 else
4769 val = TRUE;
4770 break;
4771
4772 /* Internal commands, that don't map directly to the SetTextureStageState API. */
4773 case SVGA3D_TS_TEXCOORDGEN: /* SVGA3dTextureCoordGen */
4774 AssertFailed();
4775 break;
4776
4777 case SVGA3D_TS_MAX: /* shut up MSC */
4778 case SVGA3D_TS_INVALID:
4779 case SVGA3D_TS_BIND_TEXTURE:
4780 AssertFailedBreak();
4781 }
4782
4783 const uint32_t currentStage = pTextureState[i].stage;
4784 /* Record the texture state for vm state saving. */
4785 if ( currentStage < RT_ELEMENTS(pContext->state.aTextureStates)
4786 && pTextureState[i].name < RT_ELEMENTS(pContext->state.aTextureStates[0]))
4787 {
4788 pContext->state.aTextureStates[currentStage][pTextureState[i].name] = pTextureState[i];
4789 }
4790
4791 if (textureType != D3DTSS_FORCE_DWORD)
4792 {
4793 if (RT_UNLIKELY(currentStage >= SVGA3D_MAX_TEXTURE_STAGES))
4794 {
4795 AssertMsgFailed(("pTextureState[%d].stage=%#x name=%#x value=%#x\n", i, pTextureState[i].stage, pTextureState[i].name, pTextureState[i].value));
4796 continue;
4797 }
4798
4799 hr = pContext->pDevice->SetTextureStageState(currentStage, textureType, val);
4800 AssertMsg(hr == D3D_OK, ("SetTextureStageState failed with %x\n", hr));
4801 }
4802 else if (samplerType != D3DSAMP_FORCE_DWORD)
4803 {
4804 if (RT_UNLIKELY(currentStage >= SVGA3D_MAX_SAMPLERS))
4805 {
4806 AssertMsgFailed(("pTextureState[%d].stage=%#x name=%#x value=%#x\n", i, pTextureState[i].stage, pTextureState[i].name, pTextureState[i].value));
4807 continue;
4808 }
4809
4810 hr = pContext->pDevice->SetSamplerState(currentStage, samplerType, val);
4811 AssertMsg(hr == D3D_OK, ("SetSamplerState failed with %x\n", hr));
4812 }
4813 else
4814 {
4815 AssertFailed();
4816 }
4817 }
4818
4819 return VINF_SUCCESS;
4820}
4821
4822int vmsvga3dSetMaterial(PVGASTATE pThis, uint32_t cid, SVGA3dFace face, SVGA3dMaterial *pMaterial)
4823{
4824 HRESULT hr;
4825 D3DMATERIAL9 material;
4826 PVMSVGA3DCONTEXT pContext;
4827 PVMSVGA3DSTATE pState = pThis->svga.p3dState;
4828 AssertReturn(pState, VERR_NO_MEMORY);
4829
4830 LogFunc(("cid=%x face %d\n", cid, face));
4831
4832 int rc = vmsvga3dContextFromCid(pState, cid, &pContext);
4833 AssertRCReturn(rc, rc);
4834
4835 AssertReturn(face < SVGA3D_FACE_MAX, VERR_INVALID_PARAMETER);
4836
4837 /* Save for vm state save/restore. */
4838 pContext->state.aMaterial[face].fValid = true;
4839 pContext->state.aMaterial[face].material = *pMaterial;
4840 pContext->state.u32UpdateFlags |= VMSVGA3D_UPDATE_MATERIAL;
4841
4842 /* @note face not used for D3D9 */
4843 /** @todo ignore everything except SVGA3D_FACE_NONE? */
4844 //Assert(face == SVGA3D_FACE_NONE);
4845 if (face != SVGA3D_FACE_NONE)
4846 Log(("Unsupported face %d!!\n", face));
4847
4848 material.Diffuse.r = pMaterial->diffuse[0];
4849 material.Diffuse.g = pMaterial->diffuse[1];
4850 material.Diffuse.b = pMaterial->diffuse[2];
4851 material.Diffuse.a = pMaterial->diffuse[3];
4852 material.Ambient.r = pMaterial->ambient[0];
4853 material.Ambient.g = pMaterial->ambient[1];
4854 material.Ambient.b = pMaterial->ambient[2];
4855 material.Ambient.a = pMaterial->ambient[3];
4856 material.Specular.r = pMaterial->specular[0];
4857 material.Specular.g = pMaterial->specular[1];
4858 material.Specular.b = pMaterial->specular[2];
4859 material.Specular.a = pMaterial->specular[3];
4860 material.Emissive.r = pMaterial->emissive[0];
4861 material.Emissive.g = pMaterial->emissive[1];
4862 material.Emissive.b = pMaterial->emissive[2];
4863 material.Emissive.a = pMaterial->emissive[3];
4864 material.Power = pMaterial->shininess;
4865
4866 hr = pContext->pDevice->SetMaterial(&material);
4867 AssertMsgReturn(hr == D3D_OK, ("SetMaterial failed with %x\n", hr), VERR_INTERNAL_ERROR);
4868
4869 return VINF_SUCCESS;
4870}
4871
4872int vmsvga3dSetLightData(PVGASTATE pThis, uint32_t cid, uint32_t index, SVGA3dLightData *pData)
4873{
4874 HRESULT hr;
4875 D3DLIGHT9 light;
4876 PVMSVGA3DCONTEXT pContext;
4877 PVMSVGA3DSTATE pState = pThis->svga.p3dState;
4878 AssertReturn(pState, VERR_NO_MEMORY);
4879
4880 Log(("vmsvga3dSetLightData %x index=%d\n", cid, index));
4881
4882 int rc = vmsvga3dContextFromCid(pState, cid, &pContext);
4883 AssertRCReturn(rc, rc);
4884
4885 switch (pData->type)
4886 {
4887 case SVGA3D_LIGHTTYPE_POINT:
4888 light.Type = D3DLIGHT_POINT;
4889 break;
4890
4891 case SVGA3D_LIGHTTYPE_SPOT1: /* 1-cone, in degrees */
4892 light.Type = D3DLIGHT_SPOT;
4893 break;
4894
4895 case SVGA3D_LIGHTTYPE_DIRECTIONAL:
4896 light.Type = D3DLIGHT_DIRECTIONAL;
4897 break;
4898
4899 case SVGA3D_LIGHTTYPE_SPOT2: /* 2-cone, in radians */
4900 default:
4901 Log(("Unsupported light type!!\n"));
4902 return VERR_INVALID_PARAMETER;
4903 }
4904
4905 /* Store for vm state save/restore */
4906 if (index < SVGA3D_MAX_LIGHTS)
4907 {
4908 pContext->state.aLightData[index].fValidData = true;
4909 pContext->state.aLightData[index].data = *pData;
4910 }
4911 else
4912 AssertFailed();
4913
4914 light.Diffuse.r = pData->diffuse[0];
4915 light.Diffuse.g = pData->diffuse[1];
4916 light.Diffuse.b = pData->diffuse[2];
4917 light.Diffuse.a = pData->diffuse[3];
4918 light.Specular.r = pData->specular[0];
4919 light.Specular.g = pData->specular[1];
4920 light.Specular.b = pData->specular[2];
4921 light.Specular.a = pData->specular[3];
4922 light.Ambient.r = pData->ambient[0];
4923 light.Ambient.g = pData->ambient[1];
4924 light.Ambient.b = pData->ambient[2];
4925 light.Ambient.a = pData->ambient[3];
4926 light.Position.x = pData->position[0];
4927 light.Position.y = pData->position[1];
4928 light.Position.z = pData->position[2]; /* @note 4th position not available in D3D9 */
4929 light.Direction.x = pData->direction[0];
4930 light.Direction.y = pData->direction[1];
4931 light.Direction.z = pData->direction[2]; /* @note 4th position not available in D3D9 */
4932 light.Range = pData->range;
4933 light.Falloff = pData->falloff;
4934 light.Attenuation0 = pData->attenuation0;
4935 light.Attenuation1 = pData->attenuation1;
4936 light.Attenuation2 = pData->attenuation2;
4937 light.Theta = pData->theta;
4938 light.Phi = pData->phi;
4939
4940 hr = pContext->pDevice->SetLight(index, &light);
4941 AssertMsgReturn(hr == D3D_OK, ("SetLight failed with %x\n", hr), VERR_INTERNAL_ERROR);
4942
4943 return VINF_SUCCESS;
4944}
4945
4946int vmsvga3dSetLightEnabled(PVGASTATE pThis, uint32_t cid, uint32_t index, uint32_t enabled)
4947{
4948 HRESULT hr;
4949 PVMSVGA3DCONTEXT pContext;
4950 PVMSVGA3DSTATE pState = pThis->svga.p3dState;
4951 AssertReturn(pState, VERR_NO_MEMORY);
4952
4953 Log(("vmsvga3dSetLightEnabled %x %d -> %d\n", cid, index, enabled));
4954
4955 int rc = vmsvga3dContextFromCid(pState, cid, &pContext);
4956 AssertRCReturn(rc, rc);
4957
4958 /* Store for vm state save/restore */
4959 if (index < SVGA3D_MAX_LIGHTS)
4960 pContext->state.aLightData[index].fEnabled = !!enabled;
4961 else
4962 AssertFailed();
4963
4964 hr = pContext->pDevice->LightEnable(index, (BOOL)enabled);
4965 AssertMsgReturn(hr == D3D_OK, ("LightEnable failed with %x\n", hr), VERR_INTERNAL_ERROR);
4966
4967 return VINF_SUCCESS;
4968}
4969
4970int vmsvga3dSetViewPort(PVGASTATE pThis, uint32_t cid, SVGA3dRect *pRect)
4971{
4972 HRESULT hr;
4973 D3DVIEWPORT9 viewPort;
4974 PVMSVGA3DCONTEXT pContext;
4975 PVMSVGA3DSTATE pState = pThis->svga.p3dState;
4976 AssertReturn(pState, VERR_NO_MEMORY);
4977
4978 Log(("vmsvga3dSetViewPort %x (%d,%d)(%d,%d)\n", cid, pRect->x, pRect->y, pRect->w, pRect->h));
4979
4980 int rc = vmsvga3dContextFromCid(pState, cid, &pContext);
4981 AssertRCReturn(rc, rc);
4982
4983 /* Save for vm state save/restore. */
4984 pContext->state.RectViewPort = *pRect;
4985 pContext->state.u32UpdateFlags |= VMSVGA3D_UPDATE_VIEWPORT;
4986
4987 hr = pContext->pDevice->GetViewport(&viewPort);
4988 AssertMsgReturn(hr == D3D_OK, ("GetViewport failed with %x\n", hr), VERR_INTERNAL_ERROR);
4989
4990 viewPort.X = pRect->x;
4991 viewPort.Y = pRect->y;
4992 viewPort.Width = pRect->w;
4993 viewPort.Height = pRect->h;
4994 /* viewPort.MinZ & MaxZ are not changed from the current setting. */
4995
4996 hr = pContext->pDevice->SetViewport(&viewPort);
4997 AssertMsgReturn(hr == D3D_OK, ("SetViewport failed with %x\n", hr), VERR_INTERNAL_ERROR);
4998
4999 return VINF_SUCCESS;
5000}
5001
5002int vmsvga3dSetClipPlane(PVGASTATE pThis, uint32_t cid, uint32_t index, float plane[4])
5003{
5004 HRESULT hr;
5005 PVMSVGA3DCONTEXT pContext;
5006 PVMSVGA3DSTATE pState = pThis->svga.p3dState;
5007 AssertReturn(pState, VERR_NO_MEMORY);
5008
5009 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)));
5010 AssertReturn(index < SVGA3D_CLIPPLANE_MAX, VERR_INVALID_PARAMETER);
5011
5012 int rc = vmsvga3dContextFromCid(pState, cid, &pContext);
5013 AssertRCReturn(rc, rc);
5014
5015 /* Store for vm state save/restore. */
5016 pContext->state.aClipPlane[index].fValid = true;
5017 memcpy(pContext->state.aClipPlane[index].plane, plane, sizeof(pContext->state.aClipPlane[index].plane));
5018
5019 hr = pContext->pDevice->SetClipPlane(index, plane);
5020 AssertMsgReturn(hr == D3D_OK, ("SetClipPlane failed with %x\n", hr), VERR_INTERNAL_ERROR);
5021 return VINF_SUCCESS;
5022}
5023
5024int vmsvga3dCommandClear(PVGASTATE pThis, uint32_t cid, SVGA3dClearFlag clearFlag, uint32_t color, float depth, uint32_t stencil, uint32_t cRects, SVGA3dRect *pRect)
5025{
5026 DWORD clearFlagD3D = 0;
5027 D3DRECT *pRectD3D = NULL;
5028 HRESULT hr;
5029 PVMSVGA3DCONTEXT pContext;
5030 PVMSVGA3DSTATE pState = pThis->svga.p3dState;
5031 AssertReturn(pState, VERR_NO_MEMORY);
5032
5033 Log(("vmsvga3dCommandClear %x clearFlag=%x color=%x depth=%d stencil=%x cRects=%d\n", cid, clearFlag, color, (uint32_t)(depth * 100.0), stencil, cRects));
5034
5035 int rc = vmsvga3dContextFromCid(pState, cid, &pContext);
5036 AssertRCReturn(rc, rc);
5037
5038 if (clearFlag & SVGA3D_CLEAR_COLOR)
5039 clearFlagD3D |= D3DCLEAR_TARGET;
5040 if (clearFlag & SVGA3D_CLEAR_STENCIL)
5041 clearFlagD3D |= D3DCLEAR_STENCIL;
5042 if (clearFlag & SVGA3D_CLEAR_DEPTH)
5043 clearFlagD3D |= D3DCLEAR_ZBUFFER;
5044
5045 if (cRects)
5046 {
5047 pRectD3D = (D3DRECT *)RTMemAlloc(sizeof(D3DRECT) * cRects);
5048 AssertReturn(pRectD3D, VERR_NO_MEMORY);
5049
5050 for (unsigned i=0; i < cRects; i++)
5051 {
5052 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));
5053 pRectD3D[i].x1 = pRect[i].x;
5054 pRectD3D[i].y1 = pRect[i].y;
5055 pRectD3D[i].x2 = pRect[i].x + pRect[i].w; /* exclusive */
5056 pRectD3D[i].y2 = pRect[i].y + pRect[i].h; /* exclusive */
5057 }
5058 }
5059
5060 hr = pContext->pDevice->Clear(cRects, pRectD3D, clearFlagD3D, (D3DCOLOR)color, depth, stencil);
5061 if (pRectD3D)
5062 RTMemFree(pRectD3D);
5063
5064 AssertMsgReturn(hr == D3D_OK, ("Clear failed with %x\n", hr), VERR_INTERNAL_ERROR);
5065
5066 /* Make sure we can track drawing usage of active render targets. */
5067 for (uint32_t i = 0; i < RT_ELEMENTS(pContext->state.aRenderTargets); ++i)
5068 if (pContext->state.aRenderTargets[i] != SVGA3D_INVALID_ID)
5069 vmsvga3dSurfaceTrackUsageById(pState, pContext, pContext->state.aRenderTargets[i]);
5070
5071 return VINF_SUCCESS;
5072}
5073
5074/* Convert VMWare vertex declaration to its D3D equivalent. */
5075static int vmsvga3dVertexDecl2D3D(const SVGA3dVertexArrayIdentity &identity, D3DVERTEXELEMENT9 *pVertexElement)
5076{
5077 /* usage, method and type are identical; make sure. */
5078 AssertCompile(SVGA3D_DECLTYPE_FLOAT1 == D3DDECLTYPE_FLOAT1);
5079 AssertCompile(SVGA3D_DECLTYPE_FLOAT16_4 == D3DDECLTYPE_FLOAT16_4);
5080 AssertCompile(SVGA3D_DECLMETHOD_DEFAULT == D3DDECLMETHOD_DEFAULT);
5081 AssertCompile(SVGA3D_DECLMETHOD_LOOKUPPRESAMPLED == D3DDECLMETHOD_LOOKUPPRESAMPLED);
5082 AssertCompile(D3DDECLUSAGE_POSITION == SVGA3D_DECLUSAGE_POSITION);
5083 AssertCompile(D3DDECLUSAGE_SAMPLE == SVGA3D_DECLUSAGE_SAMPLE);
5084
5085 pVertexElement->Stream = 0;
5086 pVertexElement->Offset = 0;
5087 pVertexElement->Type = identity.type;
5088 pVertexElement->Method = identity.method;
5089 pVertexElement->Usage = identity.usage;
5090 pVertexElement->UsageIndex = identity.usageIndex;
5091 return VINF_SUCCESS;
5092}
5093
5094/* Convert VMWare primitive type to its D3D equivalent. */
5095static int vmsvga3dPrimitiveType2D3D(SVGA3dPrimitiveType PrimitiveType, D3DPRIMITIVETYPE *pPrimitiveTypeD3D)
5096{
5097 switch (PrimitiveType)
5098 {
5099 case SVGA3D_PRIMITIVE_TRIANGLELIST:
5100 *pPrimitiveTypeD3D = D3DPT_TRIANGLELIST;
5101 break;
5102 case SVGA3D_PRIMITIVE_POINTLIST:
5103 *pPrimitiveTypeD3D = D3DPT_POINTLIST;
5104 break;
5105 case SVGA3D_PRIMITIVE_LINELIST:
5106 *pPrimitiveTypeD3D = D3DPT_LINELIST;
5107 break;
5108 case SVGA3D_PRIMITIVE_LINESTRIP:
5109 *pPrimitiveTypeD3D = D3DPT_LINESTRIP;
5110 break;
5111 case SVGA3D_PRIMITIVE_TRIANGLESTRIP:
5112 *pPrimitiveTypeD3D = D3DPT_TRIANGLESTRIP;
5113 break;
5114 case SVGA3D_PRIMITIVE_TRIANGLEFAN:
5115 *pPrimitiveTypeD3D = D3DPT_TRIANGLEFAN;
5116 break;
5117 default:
5118 return VERR_INVALID_PARAMETER;
5119 }
5120 return VINF_SUCCESS;
5121}
5122
5123#ifdef OLD_DRAW_PRIMITIVES /* Old vmsvga3dDrawPrimitives */
5124
5125static int vmsvga3dDrawPrimitivesProcessVertexDecls(PVMSVGA3DSTATE pState, PVMSVGA3DCONTEXT pContext, uint32_t numVertexDecls, SVGA3dVertexDecl *pVertexDecl, uint32_t idStream, D3DVERTEXELEMENT9 *pVertexElement)
5126{
5127 HRESULT hr;
5128 int rc;
5129 uint32_t uVertexMinOffset = 0xffffffff;
5130 uint32_t uVertexMaxOffset = 0;
5131
5132 /* Create a vertex declaration array */
5133 for (unsigned iVertex = 0; iVertex < numVertexDecls; iVertex++)
5134 {
5135 unsigned sidVertex = pVertexDecl[iVertex].array.surfaceId;
5136 PVMSVGA3DSURFACE pVertexSurface;
5137
5138 AssertReturn(sidVertex < SVGA3D_MAX_SURFACE_IDS, VERR_INVALID_PARAMETER);
5139 AssertReturn(sidVertex < pState->cSurfaces && pState->papSurfaces[sidVertex]->id == sidVertex, VERR_INVALID_PARAMETER);
5140
5141 pVertexSurface = pState->papSurfaces[sidVertex];
5142 Log(("vmsvga3dDrawPrimitives: vertex sid=%x stream %d\n", sidVertex, idStream));
5143 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));
5144
5145 rc = vmsvga3dVertexDecl2D3D(pVertexDecl[iVertex].identity, &pVertexElement[iVertex]);
5146 AssertRCReturn(rc, rc);
5147 pVertexElement[iVertex].Stream = idStream;
5148
5149#ifdef LOG_ENABLED
5150 if (pVertexDecl[iVertex].array.stride == 0)
5151 Log(("vmsvga3dDrawPrimitives: stride == 0! Can be valid\n"));
5152#endif
5153
5154 /* Find the min and max vertex offset to determine the right base offset to use in the vertex declaration. */
5155 if (pVertexDecl[iVertex].array.offset > uVertexMaxOffset)
5156 uVertexMaxOffset = pVertexDecl[iVertex].array.offset;
5157 if (pVertexDecl[iVertex].array.offset < uVertexMinOffset)
5158 uVertexMinOffset = pVertexDecl[iVertex].array.offset;
5159
5160 if ( pVertexSurface->u.pSurface
5161 && pVertexSurface->enmD3DResType != VMSVGA3D_D3DRESTYPE_VERTEX_BUFFER)
5162 {
5163 /* The buffer object is not an vertex one. Switch type. */
5164 Assert(pVertexSurface->enmD3DResType == VMSVGA3D_D3DRESTYPE_INDEX_BUFFER);
5165 D3D_RELEASE(pVertexSurface->u.pIndexBuffer);
5166 pVertexSurface->enmD3DResType = VMSVGA3D_D3DRESTYPE_NONE;
5167
5168 LogFunc(("index -> vertex buffer sid=%x\n", sidVertex));
5169 }
5170
5171 if (!pVertexSurface->u.pVertexBuffer)
5172 {
5173 Assert(iVertex == 0);
5174
5175 LogFunc(("create vertex buffer fDirty=%d\n", pVertexSurface->fDirty));
5176 hr = pContext->pDevice->CreateVertexBuffer(pVertexSurface->pMipmapLevels[0].cbSurface,
5177 D3DUSAGE_DYNAMIC | D3DUSAGE_WRITEONLY /* possible severe performance penalty otherwise (according to d3d debug output) */,
5178 0, /* non-FVF */
5179 D3DPOOL_DEFAULT,
5180 &pVertexSurface->u.pVertexBuffer,
5181 NULL);
5182 AssertMsgReturn(hr == D3D_OK, ("CreateVertexBuffer failed with %x\n", hr), VERR_INTERNAL_ERROR);
5183
5184 pVertexSurface->enmD3DResType = VMSVGA3D_D3DRESTYPE_VERTEX_BUFFER;
5185 pVertexSurface->idAssociatedContext = pContext->id;
5186
5187 if (pVertexSurface->fDirty)
5188 {
5189 void *pData;
5190
5191 hr = pVertexSurface->u.pVertexBuffer->Lock(0, 0, &pData, 0);
5192 AssertMsgReturn(hr == D3D_OK, ("Lock vertex failed with %x\n", hr), VERR_INTERNAL_ERROR);
5193
5194 memcpy(pData, pVertexSurface->pMipmapLevels[0].pSurfaceData, pVertexSurface->pMipmapLevels[0].cbSurface);
5195
5196 hr = pVertexSurface->u.pVertexBuffer->Unlock();
5197 AssertMsgReturn(hr == D3D_OK, ("Unlock vertex failed with %x\n", hr), VERR_INTERNAL_ERROR);
5198 pVertexSurface->pMipmapLevels[0].fDirty = false;
5199 pVertexSurface->fDirty = false;
5200 }
5201 pVertexSurface->surfaceFlags |= SVGA3D_SURFACE_HINT_VERTEXBUFFER;
5202 }
5203 }
5204
5205 /* Set the right vertex offset values for each declaration. */
5206 for (unsigned iVertex = 0; iVertex < numVertexDecls; iVertex++)
5207 {
5208 pVertexElement[iVertex].Offset = pVertexDecl[iVertex].array.offset - uVertexMinOffset;
5209#ifdef LOG_ENABLED
5210 if (pVertexElement[iVertex].Offset >= pVertexDecl[0].array.stride)
5211 LogFunc(("WARNING: offset > stride!!\n"));
5212#endif
5213
5214 LogFunc(("vertex %d offset = %d (stride %d) (min=%d max=%d)\n", iVertex, pVertexDecl[iVertex].array.offset, pVertexDecl[iVertex].array.stride, uVertexMinOffset, uVertexMaxOffset));
5215 }
5216
5217 PVMSVGA3DSURFACE pVertexSurface;
5218 unsigned sidVertex = pVertexDecl[0].array.surfaceId;
5219 unsigned strideVertex = pVertexDecl[0].array.stride;
5220
5221 pVertexSurface = pState->papSurfaces[sidVertex];
5222
5223 LogFunc(("SetStreamSource %d min offset=%d stride=%d\n", idStream, uVertexMinOffset, strideVertex));
5224 hr = pContext->pDevice->SetStreamSource(idStream,
5225 pVertexSurface->u.pVertexBuffer,
5226 uVertexMinOffset,
5227 strideVertex);
5228
5229 AssertMsgReturn(hr == D3D_OK, ("SetStreamSource failed with %x\n", hr), VERR_INTERNAL_ERROR);
5230 return VINF_SUCCESS;
5231}
5232
5233int vmsvga3dDrawPrimitives(PVGASTATE pThis, uint32_t cid, uint32_t numVertexDecls, SVGA3dVertexDecl *pVertexDecl,
5234 uint32_t numRanges, SVGA3dPrimitiveRange *pRange,
5235 uint32_t cVertexDivisor, SVGA3dVertexDivisor *pVertexDivisor)
5236{
5237 RT_NOREF(pVertexDivisor);
5238 PVMSVGA3DCONTEXT pContext;
5239 PVMSVGA3DSTATE pState = pThis->svga.p3dState;
5240 AssertReturn(pState, VERR_INTERNAL_ERROR);
5241 int rc = VINF_SUCCESS;
5242 HRESULT hr;
5243 uint32_t iCurrentVertex, iCurrentStreamId;
5244 IDirect3DVertexDeclaration9 *pVertexDeclD3D = NULL;
5245 D3DVERTEXELEMENT9 *pVertexElement = NULL;
5246 D3DVERTEXELEMENT9 VertexEnd = D3DDECL_END();
5247
5248 LogFunc(("%x numVertexDecls=%d numRanges=%d, cVertexDivisor=%d\n", cid, numVertexDecls, numRanges, cVertexDivisor));
5249
5250 AssertReturn(numVertexDecls && numVertexDecls <= SVGA3D_MAX_VERTEX_ARRAYS, VERR_INVALID_PARAMETER);
5251 AssertReturn(numRanges && numRanges <= SVGA3D_MAX_DRAW_PRIMITIVE_RANGES, VERR_INVALID_PARAMETER);
5252 AssertReturn(!cVertexDivisor || cVertexDivisor == numVertexDecls, VERR_INVALID_PARAMETER);
5253
5254 rc = vmsvga3dContextFromCid(pState, cid, &pContext);
5255 AssertRCReturn(rc, rc);
5256
5257 /* Begin a scene before rendering anything. */
5258 hr = pContext->pDevice->BeginScene();
5259 AssertMsgReturn(hr == D3D_OK, ("BeginScene failed with %x\n", hr), VERR_INTERNAL_ERROR);
5260
5261 pVertexElement = (D3DVERTEXELEMENT9 *)RTMemAllocZ(sizeof(D3DVERTEXELEMENT9) * (numVertexDecls + 1));
5262 if (!pVertexElement)
5263 {
5264 Assert(pVertexElement);
5265 rc = VERR_INTERNAL_ERROR;
5266 goto internal_error;
5267 }
5268
5269 /* Process all vertex declarations. Each vertex buffer is represented by one stream source id. */
5270 iCurrentVertex = 0;
5271 iCurrentStreamId = 0;
5272 while (iCurrentVertex < numVertexDecls)
5273 {
5274 uint32_t sidVertex = SVGA_ID_INVALID;
5275 uint32_t iVertex;
5276 uint32_t uVertexMinOffset = 0xffffffff;
5277
5278 for (iVertex = iCurrentVertex; iVertex < numVertexDecls; iVertex++)
5279 {
5280 if ( ( sidVertex != SVGA_ID_INVALID
5281 && pVertexDecl[iVertex].array.surfaceId != sidVertex
5282 )
5283 /* We must put vertex declarations that start at a different element in another stream as d3d only handles offsets < stride. */
5284 || ( uVertexMinOffset != 0xffffffff
5285 && pVertexDecl[iVertex].array.offset >= uVertexMinOffset + pVertexDecl[iCurrentVertex].array.stride
5286 )
5287 )
5288 break;
5289 sidVertex = pVertexDecl[iVertex].array.surfaceId;
5290
5291 if (uVertexMinOffset > pVertexDecl[iVertex].array.offset)
5292 uVertexMinOffset = pVertexDecl[iVertex].array.offset;
5293 }
5294
5295 rc = vmsvga3dDrawPrimitivesProcessVertexDecls(pState, pContext, iVertex - iCurrentVertex, &pVertexDecl[iCurrentVertex], iCurrentStreamId, &pVertexElement[iCurrentVertex]);
5296 if (RT_FAILURE(rc))
5297 goto internal_error;
5298
5299 if (cVertexDivisor)
5300 {
5301 LogFunc(("SetStreamSourceFreq[%d]=%x\n", iCurrentStreamId, pVertexDivisor[iCurrentStreamId].value));
5302 HRESULT hr2 = pContext->pDevice->SetStreamSourceFreq(iCurrentStreamId, pVertexDivisor[iCurrentStreamId].value);
5303 Assert(SUCCEEDED(hr2)); RT_NOREF(hr2);
5304 }
5305
5306 iCurrentVertex = iVertex;
5307 iCurrentStreamId++;
5308 }
5309
5310 /* Mark the end. */
5311 memcpy(&pVertexElement[numVertexDecls], &VertexEnd, sizeof(VertexEnd));
5312
5313 hr = pContext->pDevice->CreateVertexDeclaration(&pVertexElement[0],
5314 &pVertexDeclD3D);
5315 if (hr != D3D_OK)
5316 {
5317 AssertMsg(hr == D3D_OK, ("vmsvga3dDrawPrimitives: CreateVertexDeclaration failed with %x\n", hr));
5318 rc = VERR_INTERNAL_ERROR;
5319 goto internal_error;
5320 }
5321
5322 hr = pContext->pDevice->SetVertexDeclaration(pVertexDeclD3D);
5323 if (hr != D3D_OK)
5324 {
5325 AssertMsg(hr == D3D_OK, ("vmsvga3dDrawPrimitives: SetVertexDeclaration failed with %x\n", hr));
5326 rc = VERR_INTERNAL_ERROR;
5327 goto internal_error;
5328 }
5329
5330 /* Now draw the primitives. */
5331 for (unsigned iPrimitive = 0; iPrimitive < numRanges; iPrimitive++)
5332 {
5333 D3DPRIMITIVETYPE PrimitiveTypeD3D;
5334 unsigned sidIndex = pRange[iPrimitive].indexArray.surfaceId;
5335 PVMSVGA3DSURFACE pIndexSurface = NULL;
5336
5337 Log(("Primitive %d: type %s\n", iPrimitive, vmsvga3dPrimitiveType2String(pRange[iPrimitive].primType)));
5338 rc = vmsvga3dPrimitiveType2D3D(pRange[iPrimitive].primType, &PrimitiveTypeD3D);
5339 if (RT_FAILURE(rc))
5340 {
5341 AssertRC(rc);
5342 goto internal_error;
5343 }
5344
5345 /* Triangle strips or fans with just one primitive don't make much sense and are identical to triangle lists.
5346 * Workaround for NVidia driver crash when encountering some of these.
5347 */
5348 if ( pRange[iPrimitive].primitiveCount == 1
5349 && ( PrimitiveTypeD3D == D3DPT_TRIANGLESTRIP
5350 || PrimitiveTypeD3D == D3DPT_TRIANGLEFAN))
5351 PrimitiveTypeD3D = D3DPT_TRIANGLELIST;
5352
5353 if (sidIndex != SVGA3D_INVALID_ID)
5354 {
5355 AssertMsg(pRange[iPrimitive].indexWidth == sizeof(uint32_t) || pRange[iPrimitive].indexWidth == sizeof(uint16_t), ("Unsupported primitive width %d\n", pRange[iPrimitive].indexWidth));
5356
5357 rc = vmsvga3dSurfaceFromSid(pState, sidIndex, &pIndexSurface);
5358 if (RT_FAILURE(rc))
5359 goto internal_error;
5360
5361 Log(("vmsvga3dDrawPrimitives: index sid=%x\n", sidIndex));
5362
5363 if ( pIndexSurface->u.pSurface
5364 && pIndexSurface->enmD3DResType != VMSVGA3D_D3DRESTYPE_INDEX_BUFFER)
5365 {
5366 /* The buffer object is not an index one. Switch type. */
5367 Assert(pIndexSurface->enmD3DResType == VMSVGA3D_D3DRESTYPE_VERTEX_BUFFER);
5368 D3D_RELEASE(pIndexSurface->u.pVertexBuffer);
5369 pIndexSurface->enmD3DResType = VMSVGA3D_D3DRESTYPE_NONE;
5370
5371 LogFunc(("vertex -> index buffer sid=%x\n", sidIndex));
5372 }
5373
5374 if (!pIndexSurface->u.pIndexBuffer)
5375 {
5376 Log(("vmsvga3dDrawPrimitives: create index buffer fDirty=%d\n", pIndexSurface->fDirty));
5377
5378 hr = pContext->pDevice->CreateIndexBuffer(pIndexSurface->pMipmapLevels[0].cbSurface,
5379 D3DUSAGE_DYNAMIC | D3DUSAGE_WRITEONLY /* possible severe performance penalty otherwise (according to d3d debug output */,
5380 (pRange[iPrimitive].indexWidth == sizeof(uint16_t)) ? D3DFMT_INDEX16 : D3DFMT_INDEX32,
5381 D3DPOOL_DEFAULT,
5382 &pIndexSurface->u.pIndexBuffer,
5383 NULL);
5384 if (hr != D3D_OK)
5385 {
5386 AssertMsg(hr == D3D_OK, ("vmsvga3dDrawPrimitives: CreateIndexBuffer failed with %x\n", hr));
5387 rc = VERR_INTERNAL_ERROR;
5388 goto internal_error;
5389 }
5390 pIndexSurface->enmD3DResType = VMSVGA3D_D3DRESTYPE_INDEX_BUFFER;
5391
5392 if (pIndexSurface->fDirty)
5393 {
5394 void *pData;
5395
5396 Log(("vmsvga3dDrawPrimitives: sync index buffer\n"));
5397
5398 hr = pIndexSurface->u.pIndexBuffer->Lock(0, 0, &pData, 0);
5399 AssertMsg(hr == D3D_OK, ("vmsvga3dDrawPrimitives: Lock vertex failed with %x\n", hr));
5400
5401 memcpy(pData, pIndexSurface->pMipmapLevels[0].pSurfaceData, pIndexSurface->pMipmapLevels[0].cbSurface);
5402
5403 hr = pIndexSurface->u.pIndexBuffer->Unlock();
5404 AssertMsg(hr == D3D_OK, ("vmsvga3dDrawPrimitives: Unlock vertex failed with %x\n", hr));
5405
5406 pIndexSurface->pMipmapLevels[0].fDirty = false;
5407 pIndexSurface->fDirty = false;
5408 }
5409 pIndexSurface->surfaceFlags |= SVGA3D_SURFACE_HINT_INDEXBUFFER;
5410 }
5411
5412 hr = pContext->pDevice->SetIndices(pIndexSurface->u.pIndexBuffer);
5413 AssertMsg(hr == D3D_OK, ("SetIndices vertex failed with %x\n", hr));
5414 }
5415 else
5416 {
5417 hr = pContext->pDevice->SetIndices(NULL);
5418 AssertMsg(hr == D3D_OK, ("SetIndices vertex (NULL) failed with %x\n", hr));
5419 }
5420
5421 PVMSVGA3DSURFACE pVertexSurface;
5422 unsigned sidVertex = pVertexDecl[0].array.surfaceId;
5423 unsigned strideVertex = pVertexDecl[0].array.stride;
5424
5425 pVertexSurface = pState->papSurfaces[sidVertex];
5426
5427 if (!pIndexSurface)
5428 {
5429 /* Render without an index buffer */
5430 Log(("DrawPrimitive %x primitivecount=%d index index bias=%d stride=%d\n", PrimitiveTypeD3D, pRange[iPrimitive].primitiveCount, pRange[iPrimitive].indexBias, strideVertex));
5431
5432 hr = pContext->pDevice->DrawPrimitive(PrimitiveTypeD3D,
5433 pRange[iPrimitive].indexBias,
5434 pRange[iPrimitive].primitiveCount);
5435 if (hr != D3D_OK)
5436 {
5437 AssertMsg(hr == D3D_OK, ("vmsvga3dDrawPrimitives: DrawPrimitive failed with %x\n", hr));
5438 rc = VERR_INTERNAL_ERROR;
5439 goto internal_error;
5440 }
5441 }
5442 else
5443 {
5444 Assert(pRange[iPrimitive].indexBias >= 0); /** @todo */
5445
5446 UINT numVertices;
5447
5448 if (pVertexDecl[0].rangeHint.last)
5449 numVertices = pVertexDecl[0].rangeHint.last - pVertexDecl[0].rangeHint.first + 1;
5450 else
5451 numVertices = pVertexSurface->pMipmapLevels[0].cbSurface / strideVertex - pVertexDecl[0].array.offset / strideVertex - pVertexDecl[0].rangeHint.first - pRange[iPrimitive].indexBias;
5452
5453 /* Render with an index buffer */
5454 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));
5455
5456 hr = pContext->pDevice->DrawIndexedPrimitive(PrimitiveTypeD3D,
5457 pRange[iPrimitive].indexBias, /* BaseVertexIndex */
5458 0, /* MinVertexIndex */
5459 numVertices,
5460 pRange[iPrimitive].indexArray.offset / pRange[iPrimitive].indexWidth, /* StartIndex */
5461 pRange[iPrimitive].primitiveCount);
5462 if (hr != D3D_OK)
5463 {
5464 AssertMsg(hr == D3D_OK, ("vmsvga3dDrawPrimitives: DrawIndexedPrimitive failed with %x\n", hr));
5465 rc = VERR_INTERNAL_ERROR;
5466 goto internal_error;
5467 }
5468 }
5469 }
5470 D3D_RELEASE(pVertexDeclD3D);
5471 RTMemFree(pVertexElement);
5472
5473 hr = pContext->pDevice->EndScene();
5474 AssertMsgReturn(hr == D3D_OK, ("EndScene failed with %x\n", hr), VERR_INTERNAL_ERROR);
5475
5476 /* Clear streams above 1 as they might accidentally be reused in the future. */
5477 if (iCurrentStreamId > 1)
5478 {
5479 for (uint32_t i = 1; i < iCurrentStreamId; i++)
5480 {
5481 Log(("vmsvga3dDrawPrimitives: clear stream %d\n", i));
5482 hr = pContext->pDevice->SetStreamSource(i, NULL, 0, 0);
5483 AssertMsgReturn(hr == D3D_OK, ("vmsvga3dDrawPrimitives: SetStreamSource failed with %x\n", hr), VERR_INTERNAL_ERROR);
5484 }
5485 }
5486
5487 if (cVertexDivisor)
5488 {
5489 /* "When you are finished rendering the instance data, be sure to reset the vertex stream frequency back..." */
5490 for (uint32_t i = 0; i < cVertexDivisor; ++i)
5491 {
5492 HRESULT hr2 = pContext->pDevice->SetStreamSourceFreq(i, 1);
5493 Assert(SUCCEEDED(hr2)); RT_NOREF(hr2);
5494 }
5495 }
5496
5497#if 0 /* Flush queue */
5498 {
5499 IDirect3DQuery9 *pQuery;
5500 hr = pContext->pDevice->CreateQuery(D3DQUERYTYPE_EVENT, &pQuery);
5501 AssertMsgReturn(hr == D3D_OK, ("vmsvga3dSurfaceTrackUsage: CreateQuery failed with %x\n", hr), VERR_INTERNAL_ERROR);
5502
5503 hr = pQuery->Issue(D3DISSUE_END);
5504 AssertMsgReturn(hr == D3D_OK, ("vmsvga3dSurfaceTrackUsage: Issue failed with %x\n", hr), VERR_INTERNAL_ERROR);
5505 while (true)
5506 {
5507 hr = pQuery->GetData(NULL, 0, D3DGETDATA_FLUSH);
5508 if (hr != S_FALSE) break;
5509
5510 RTThreadSleep(1);
5511 }
5512 AssertMsgReturn(hr == S_OK, ("vmsvga3dSurfaceFinishDrawing: GetData failed with %x\n", hr), VERR_INTERNAL_ERROR);
5513
5514 D3D_RELEASE(pQuery);
5515 }
5516#endif
5517
5518 /* Make sure we can track drawing usage of active render targets and textures. */
5519 vmsvga3dContextTrackUsage(pThis, pContext);
5520
5521#ifdef DEBUG_GFX_WINDOW
5522 if (pContext->aSidActiveTexture[0] == 0x62)
5523//// if (pContext->sidActiveTexture == 0x3d)
5524 {
5525 SVGA3dCopyRect rect;
5526
5527 rect.srcx = rect.srcy = rect.x = rect.y = 0;
5528 rect.w = 800;
5529 rect.h = 600;
5530 vmsvga3dCommandPresent(pThis, pContext->state.aRenderTargets[SVGA3D_RT_COLOR0] /*pContext->aSidActiveTexture[0] */, 0, &rect);
5531 }
5532#endif
5533 return rc;
5534
5535internal_error:
5536 D3D_RELEASE(pVertexDeclD3D);
5537 if (pVertexElement)
5538 RTMemFree(pVertexElement);
5539
5540 hr = pContext->pDevice->EndScene();
5541 AssertMsgReturn(hr == D3D_OK, ("EndScene failed with %x\n", hr), VERR_INTERNAL_ERROR);
5542
5543 return rc;
5544}
5545
5546#else /* New vmsvga3dDrawPrimitives */
5547
5548static int vmsvga3dDrawPrimitivesSyncVertexBuffer(PVMSVGA3DCONTEXT pContext,
5549 PVMSVGA3DSURFACE pVertexSurface)
5550{
5551 HRESULT hr;
5552 if ( pVertexSurface->u.pSurface
5553 && pVertexSurface->enmD3DResType != VMSVGA3D_D3DRESTYPE_VERTEX_BUFFER)
5554 {
5555 /* The buffer object is not an vertex one. Recreate the D3D resource. */
5556 Assert(pVertexSurface->enmD3DResType == VMSVGA3D_D3DRESTYPE_INDEX_BUFFER);
5557 D3D_RELEASE(pVertexSurface->u.pIndexBuffer);
5558 pVertexSurface->enmD3DResType = VMSVGA3D_D3DRESTYPE_NONE;
5559
5560 LogFunc(("index -> vertex buffer sid=%x\n", pVertexSurface->id));
5561 }
5562
5563 bool fSync = pVertexSurface->fDirty;
5564 if (!pVertexSurface->u.pVertexBuffer)
5565 {
5566 LogFunc(("Create vertex buffer fDirty=%d\n", pVertexSurface->fDirty));
5567
5568 const DWORD Usage = D3DUSAGE_DYNAMIC | D3DUSAGE_WRITEONLY; /* possible severe performance penalty otherwise (according to d3d debug output */
5569 hr = pContext->pDevice->CreateVertexBuffer(pVertexSurface->pMipmapLevels[0].cbSurface,
5570 Usage,
5571 0, /* non-FVF */
5572 D3DPOOL_DEFAULT,
5573 &pVertexSurface->u.pVertexBuffer,
5574 NULL);
5575 AssertMsgReturn(hr == D3D_OK, ("CreateVertexBuffer failed with %x\n", hr), VERR_INTERNAL_ERROR);
5576
5577 pVertexSurface->enmD3DResType = VMSVGA3D_D3DRESTYPE_VERTEX_BUFFER;
5578 pVertexSurface->idAssociatedContext = pContext->id;
5579 pVertexSurface->surfaceFlags |= SVGA3D_SURFACE_HINT_VERTEXBUFFER;
5580 fSync = true;
5581 }
5582
5583 if (fSync)
5584 {
5585 LogFunc(("sync vertex buffer\n"));
5586 Assert(pVertexSurface->u.pVertexBuffer);
5587
5588 void *pvData;
5589 hr = pVertexSurface->u.pVertexBuffer->Lock(0, 0, &pvData, D3DLOCK_DISCARD);
5590 AssertMsgReturn(hr == D3D_OK, ("Lock vertex failed with %x\n", hr), VERR_INTERNAL_ERROR);
5591
5592 memcpy(pvData, pVertexSurface->pMipmapLevels[0].pSurfaceData, pVertexSurface->pMipmapLevels[0].cbSurface);
5593
5594 hr = pVertexSurface->u.pVertexBuffer->Unlock();
5595 AssertMsgReturn(hr == D3D_OK, ("Unlock vertex failed with %x\n", hr), VERR_INTERNAL_ERROR);
5596 }
5597
5598 return VINF_SUCCESS;
5599}
5600
5601
5602static int vmsvga3dDrawPrimitivesSyncIndexBuffer(PVMSVGA3DCONTEXT pContext,
5603 PVMSVGA3DSURFACE pIndexSurface,
5604 uint32_t indexWidth)
5605{
5606 HRESULT hr;
5607 if ( pIndexSurface->u.pSurface
5608 && pIndexSurface->enmD3DResType != VMSVGA3D_D3DRESTYPE_INDEX_BUFFER)
5609 {
5610 /* The buffer object is not an index one. Must recreate the D3D resource. */
5611 Assert(pIndexSurface->enmD3DResType == VMSVGA3D_D3DRESTYPE_VERTEX_BUFFER);
5612 D3D_RELEASE(pIndexSurface->u.pVertexBuffer);
5613 pIndexSurface->enmD3DResType = VMSVGA3D_D3DRESTYPE_NONE;
5614
5615 LogFunc(("vertex -> index buffer sid=%x\n", pIndexSurface->id));
5616 }
5617
5618 bool fSync = pIndexSurface->fDirty;
5619 if (!pIndexSurface->u.pIndexBuffer)
5620 {
5621 LogFunc(("Create index buffer fDirty=%d\n", pIndexSurface->fDirty));
5622
5623 const DWORD Usage = D3DUSAGE_DYNAMIC | D3DUSAGE_WRITEONLY; /* possible severe performance penalty otherwise (according to d3d debug output */
5624 const D3DFORMAT Format = (indexWidth == sizeof(uint16_t)) ? D3DFMT_INDEX16 : D3DFMT_INDEX32;
5625 hr = pContext->pDevice->CreateIndexBuffer(pIndexSurface->pMipmapLevels[0].cbSurface,
5626 Usage,
5627 Format,
5628 D3DPOOL_DEFAULT,
5629 &pIndexSurface->u.pIndexBuffer,
5630 NULL);
5631 AssertMsgReturn(hr == D3D_OK, ("CreateIndexBuffer failed with %x\n", hr), VERR_INTERNAL_ERROR);
5632
5633 pIndexSurface->enmD3DResType = VMSVGA3D_D3DRESTYPE_INDEX_BUFFER;
5634 pIndexSurface->idAssociatedContext = pContext->id;
5635 pIndexSurface->surfaceFlags |= SVGA3D_SURFACE_HINT_INDEXBUFFER;
5636 fSync = true;
5637 }
5638
5639 if (fSync)
5640 {
5641 LogFunc(("sync index buffer\n"));
5642 Assert(pIndexSurface->u.pIndexBuffer);
5643
5644 void *pvData;
5645 hr = pIndexSurface->u.pIndexBuffer->Lock(0, 0, &pvData, D3DLOCK_DISCARD);
5646 AssertMsgReturn(hr == D3D_OK, ("Lock index failed with %x\n", hr), VERR_INTERNAL_ERROR);
5647
5648 memcpy(pvData, pIndexSurface->pMipmapLevels[0].pSurfaceData, pIndexSurface->pMipmapLevels[0].cbSurface);
5649
5650 hr = pIndexSurface->u.pIndexBuffer->Unlock();
5651 AssertMsgReturn(hr == D3D_OK, ("Unlock index failed with %x\n", hr), VERR_INTERNAL_ERROR);
5652 }
5653
5654 return VINF_SUCCESS;
5655}
5656
5657static int vmsvga3dDrawPrimitivesProcessVertexDecls(const uint32_t numVertexDecls,
5658 const SVGA3dVertexDecl *pVertexDecl,
5659 const uint32_t idStream,
5660 const uint32_t uVertexMinOffset,
5661 const uint32_t uVertexMaxOffset,
5662 D3DVERTEXELEMENT9 *pVertexElement)
5663{
5664 RT_NOREF(uVertexMaxOffset); /* Logging only. */
5665 Assert(numVertexDecls);
5666
5667 /* Create a vertex declaration array */
5668 for (uint32_t iVertex = 0; iVertex < numVertexDecls; ++iVertex)
5669 {
5670 LogFunc(("vertex %d type=%s (%d) method=%s (%d) usage=%s (%d) usageIndex=%d stride=%d offset=%d (%d min=%d max=%d)\n",
5671 iVertex,
5672 vmsvgaDeclType2String(pVertexDecl[iVertex].identity.type), pVertexDecl[iVertex].identity.type,
5673 vmsvgaDeclMethod2String(pVertexDecl[iVertex].identity.method), pVertexDecl[iVertex].identity.method,
5674 vmsvgaDeclUsage2String(pVertexDecl[iVertex].identity.usage), pVertexDecl[iVertex].identity.usage,
5675 pVertexDecl[iVertex].identity.usageIndex,
5676 pVertexDecl[iVertex].array.stride,
5677 pVertexDecl[iVertex].array.offset - uVertexMinOffset,
5678 pVertexDecl[iVertex].array.offset,
5679 uVertexMinOffset, uVertexMaxOffset));
5680
5681 int rc = vmsvga3dVertexDecl2D3D(pVertexDecl[iVertex].identity, &pVertexElement[iVertex]);
5682 AssertRCReturn(rc, rc);
5683
5684 pVertexElement[iVertex].Stream = idStream;
5685 pVertexElement[iVertex].Offset = pVertexDecl[iVertex].array.offset - uVertexMinOffset;
5686
5687#ifdef LOG_ENABLED
5688 if (pVertexDecl[iVertex].array.stride == 0)
5689 LogFunc(("stride == 0! Can be valid\n"));
5690
5691 if (pVertexElement[iVertex].Offset >= pVertexDecl[0].array.stride)
5692 LogFunc(("WARNING: offset > stride!!\n"));
5693#endif
5694 }
5695
5696 return VINF_SUCCESS;
5697}
5698
5699int vmsvga3dDrawPrimitives(PVGASTATE pThis, uint32_t cid, uint32_t numVertexDecls, SVGA3dVertexDecl *pVertexDecl,
5700 uint32_t numRanges, SVGA3dPrimitiveRange *pRange,
5701 uint32_t cVertexDivisor, SVGA3dVertexDivisor *pVertexDivisor)
5702{
5703 static const D3DVERTEXELEMENT9 sVertexEnd = D3DDECL_END();
5704
5705 PVMSVGA3DSTATE pState = pThis->svga.p3dState;
5706 AssertReturn(pState, VERR_INTERNAL_ERROR);
5707
5708 PVMSVGA3DCONTEXT pContext;
5709 int rc = vmsvga3dContextFromCid(pState, cid, &pContext);
5710 AssertRCReturn(rc, rc);
5711
5712 HRESULT hr;
5713
5714 /* SVGA driver may use the same surface for both index and vertex data. So we can not clear fDirty flag,
5715 * after updating a vertex buffer for example, because the same surface might be used for index buffer later.
5716 * So keep pointers to all used surfaces in the following two arrays and clear fDirty flag at the end.
5717 */
5718 PVMSVGA3DSURFACE aVertexSurfaces[SVGA3D_MAX_VERTEX_ARRAYS];
5719 PVMSVGA3DSURFACE aIndexSurfaces[SVGA3D_MAX_DRAW_PRIMITIVE_RANGES];
5720 RT_ZERO(aVertexSurfaces);
5721 RT_ZERO(aIndexSurfaces);
5722
5723 LogFunc(("cid=%x numVertexDecls=%d numRanges=%d, cVertexDivisor=%d\n", cid, numVertexDecls, numRanges, cVertexDivisor));
5724
5725 AssertReturn(numVertexDecls && numVertexDecls <= SVGA3D_MAX_VERTEX_ARRAYS, VERR_INVALID_PARAMETER);
5726 AssertReturn(numRanges && numRanges <= SVGA3D_MAX_DRAW_PRIMITIVE_RANGES, VERR_INVALID_PARAMETER);
5727 AssertReturn(!cVertexDivisor || cVertexDivisor == numVertexDecls, VERR_INVALID_PARAMETER);
5728
5729 /*
5730 * Process all vertex declarations. Each vertex buffer surface is represented by one stream source id.
5731 */
5732 D3DVERTEXELEMENT9 aVertexElements[SVGA3D_MAX_VERTEX_ARRAYS + 1];
5733
5734 uint32_t iCurrentVertex = 0;
5735 uint32_t iCurrentStreamId = 0;
5736 while (iCurrentVertex < numVertexDecls)
5737 {
5738 const uint32_t sidVertex = pVertexDecl[iCurrentVertex].array.surfaceId;
5739 const uint32_t strideVertex = pVertexDecl[iCurrentVertex].array.stride;
5740
5741 PVMSVGA3DSURFACE pVertexSurface;
5742 rc = vmsvga3dSurfaceFromSid(pState, sidVertex, &pVertexSurface);
5743 AssertRCBreak(rc);
5744
5745 rc = vmsvga3dDrawPrimitivesSyncVertexBuffer(pContext, pVertexSurface);
5746 AssertRCBreak(rc);
5747
5748 uint32_t uVertexMinOffset = UINT32_MAX;
5749 uint32_t uVertexMaxOffset = 0;
5750
5751 uint32_t iVertex;
5752 for (iVertex = iCurrentVertex; iVertex < numVertexDecls; ++iVertex)
5753 {
5754 /* Remember, so we can mark it as not dirty later. */
5755 aVertexSurfaces[iVertex] = pVertexSurface;
5756
5757 /* New surface id -> new stream id. */
5758 if (pVertexDecl[iVertex].array.surfaceId != sidVertex)
5759 break;
5760
5761 const uint32_t uVertexOffset = pVertexDecl[iVertex].array.offset;
5762 const uint32_t uNewVertexMinOffset = RT_MIN(uVertexMinOffset, uVertexOffset);
5763 const uint32_t uNewVertexMaxOffset = RT_MAX(uVertexMaxOffset, uVertexOffset);
5764
5765 /* We must put vertex declarations that start at a different element in another stream as d3d only handles offsets < stride. */
5766 if ( uNewVertexMaxOffset - uNewVertexMinOffset >= strideVertex
5767 && strideVertex != 0)
5768 break;
5769
5770 uVertexMinOffset = uNewVertexMinOffset;
5771 uVertexMaxOffset = uNewVertexMaxOffset;
5772 }
5773
5774 rc = vmsvga3dDrawPrimitivesProcessVertexDecls(iVertex - iCurrentVertex,
5775 &pVertexDecl[iCurrentVertex],
5776 iCurrentStreamId,
5777 uVertexMinOffset,
5778 uVertexMaxOffset,
5779 &aVertexElements[iCurrentVertex]);
5780 AssertRCBreak(rc);
5781
5782 LogFunc(("SetStreamSource vertex sid=%x stream %d min offset=%d stride=%d\n",
5783 pVertexSurface->id, iCurrentStreamId, uVertexMinOffset, strideVertex));
5784
5785 hr = pContext->pDevice->SetStreamSource(iCurrentStreamId,
5786 pVertexSurface->u.pVertexBuffer,
5787 uVertexMinOffset,
5788 strideVertex);
5789 AssertMsgBreakStmt(hr == D3D_OK, ("SetStreamSource failed with %x\n", hr), rc = VERR_INTERNAL_ERROR);
5790
5791 if (cVertexDivisor)
5792 {
5793 LogFunc(("SetStreamSourceFreq[%d]=%x\n", iCurrentStreamId, pVertexDivisor[iCurrentStreamId].value));
5794 HRESULT hr2 = pContext->pDevice->SetStreamSourceFreq(iCurrentStreamId,
5795 pVertexDivisor[iCurrentStreamId].value);
5796 Assert(SUCCEEDED(hr2)); RT_NOREF(hr2);
5797 }
5798
5799 iCurrentVertex = iVertex;
5800 ++iCurrentStreamId;
5801 }
5802
5803 /* iCurrentStreamId is equal to the total number of streams and the value is used for cleanup at the function end. */
5804
5805 AssertRCReturn(rc, rc);
5806
5807 /* Mark the end. */
5808 memcpy(&aVertexElements[numVertexDecls], &sVertexEnd, sizeof(sVertexEnd));
5809
5810 /* Create and set the vertex declaration. */
5811 IDirect3DVertexDeclaration9 *pVertexDeclD3D = NULL;
5812 hr = pContext->pDevice->CreateVertexDeclaration(&aVertexElements[0], &pVertexDeclD3D);
5813 AssertMsgReturn(hr == D3D_OK, ("CreateVertexDeclaration failed with %x\n", hr), VERR_INTERNAL_ERROR);
5814
5815 hr = pContext->pDevice->SetVertexDeclaration(pVertexDeclD3D);
5816 AssertMsgReturnStmt(hr == D3D_OK, ("SetVertexDeclaration failed with %x\n", hr),
5817 D3D_RELEASE(pVertexDeclD3D), VERR_INTERNAL_ERROR);
5818
5819 /* Begin a scene before rendering anything. */
5820 hr = pContext->pDevice->BeginScene();
5821 AssertMsgReturnStmt(hr == D3D_OK, ("BeginScene failed with %x\n", hr),
5822 D3D_RELEASE(pVertexDeclD3D), VERR_INTERNAL_ERROR);
5823
5824 /* Now draw the primitives. */
5825 for (uint32_t iPrimitive = 0; iPrimitive < numRanges; ++iPrimitive)
5826 {
5827 Log(("Primitive %d: type %s\n", iPrimitive, vmsvga3dPrimitiveType2String(pRange[iPrimitive].primType)));
5828
5829 const uint32_t sidIndex = pRange[iPrimitive].indexArray.surfaceId;
5830 PVMSVGA3DSURFACE pIndexSurface = NULL;
5831
5832 D3DPRIMITIVETYPE PrimitiveTypeD3D;
5833 rc = vmsvga3dPrimitiveType2D3D(pRange[iPrimitive].primType, &PrimitiveTypeD3D);
5834 AssertRCBreak(rc);
5835
5836 /* Triangle strips or fans with just one primitive don't make much sense and are identical to triangle lists.
5837 * Workaround for NVidia driver crash when encountering some of these.
5838 */
5839 if ( pRange[iPrimitive].primitiveCount == 1
5840 && ( PrimitiveTypeD3D == D3DPT_TRIANGLESTRIP
5841 || PrimitiveTypeD3D == D3DPT_TRIANGLEFAN))
5842 PrimitiveTypeD3D = D3DPT_TRIANGLELIST;
5843
5844 if (sidIndex != SVGA3D_INVALID_ID)
5845 {
5846 AssertMsg(pRange[iPrimitive].indexWidth == sizeof(uint32_t) || pRange[iPrimitive].indexWidth == sizeof(uint16_t),
5847 ("Unsupported primitive width %d\n", pRange[iPrimitive].indexWidth));
5848
5849 rc = vmsvga3dSurfaceFromSid(pState, sidIndex, &pIndexSurface);
5850 AssertRCBreak(rc);
5851
5852 aIndexSurfaces[iPrimitive] = pIndexSurface;
5853
5854 Log(("vmsvga3dDrawPrimitives: index sid=%x\n", sidIndex));
5855
5856 rc = vmsvga3dDrawPrimitivesSyncIndexBuffer(pContext, pIndexSurface, pRange[iPrimitive].indexWidth);
5857 AssertRCBreak(rc);
5858
5859 hr = pContext->pDevice->SetIndices(pIndexSurface->u.pIndexBuffer);
5860 AssertMsg(hr == D3D_OK, ("SetIndices vertex failed with %x\n", hr));
5861 }
5862 else
5863 {
5864 hr = pContext->pDevice->SetIndices(NULL);
5865 AssertMsg(hr == D3D_OK, ("SetIndices vertex (NULL) failed with %x\n", hr));
5866 }
5867
5868 const uint32_t strideVertex = pVertexDecl[0].array.stride;
5869
5870 if (!pIndexSurface)
5871 {
5872 /* Render without an index buffer */
5873 Log(("DrawPrimitive %x primitivecount=%d index index bias=%d stride=%d\n",
5874 PrimitiveTypeD3D, pRange[iPrimitive].primitiveCount, pRange[iPrimitive].indexBias, strideVertex));
5875
5876 hr = pContext->pDevice->DrawPrimitive(PrimitiveTypeD3D,
5877 pRange[iPrimitive].indexBias,
5878 pRange[iPrimitive].primitiveCount);
5879 AssertMsgBreakStmt(hr == D3D_OK, ("DrawPrimitive failed with %x\n", hr), rc = VERR_INTERNAL_ERROR);
5880 }
5881 else
5882 {
5883 Assert(pRange[iPrimitive].indexBias >= 0); /** @todo */
5884
5885 UINT numVertices;
5886 if (pVertexDecl[0].rangeHint.last)
5887 {
5888 /* Both SVGA3dArrayRangeHint definition and the SVGA driver code imply that 'last' is exclusive,
5889 * hence compute the difference.
5890 */
5891 numVertices = pVertexDecl[0].rangeHint.last - pVertexDecl[0].rangeHint.first;
5892 }
5893 else
5894 {
5895 /* Range hint is not provided. */
5896 PVMSVGA3DSURFACE pVertexSurface = aVertexSurfaces[0];
5897 numVertices = pVertexSurface->pMipmapLevels[0].cbSurface / strideVertex
5898 - pVertexDecl[0].array.offset / strideVertex
5899 - pVertexDecl[0].rangeHint.first
5900 - pRange[iPrimitive].indexBias;
5901 }
5902
5903 /* Render with an index buffer */
5904 Log(("DrawIndexedPrimitive %x startindex=%d numVertices=%d, primitivecount=%d index format=%s index bias=%d stride=%d\n",
5905 PrimitiveTypeD3D, pVertexDecl[0].rangeHint.first, numVertices, pRange[iPrimitive].primitiveCount,
5906 (pRange[iPrimitive].indexWidth == sizeof(uint16_t)) ? "D3DFMT_INDEX16" : "D3DFMT_INDEX32",
5907 pRange[iPrimitive].indexBias, strideVertex));
5908
5909 hr = pContext->pDevice->DrawIndexedPrimitive(PrimitiveTypeD3D,
5910 pRange[iPrimitive].indexBias, /* BaseVertexIndex */
5911 0, /* MinVertexIndex */
5912 numVertices,
5913 pRange[iPrimitive].indexArray.offset / pRange[iPrimitive].indexWidth, /* StartIndex */
5914 pRange[iPrimitive].primitiveCount);
5915 AssertMsgBreakStmt(hr == D3D_OK, ("DrawIndexedPrimitive failed with %x\n", hr), rc = VERR_INTERNAL_ERROR);
5916 }
5917 }
5918
5919 /* Release vertex declaration, end the scene and do some cleanup regardless of the rc. */
5920 D3D_RELEASE(pVertexDeclD3D);
5921
5922 hr = pContext->pDevice->EndScene();
5923 AssertMsgReturn(hr == D3D_OK, ("EndScene failed with %x\n", hr), VERR_INTERNAL_ERROR);
5924
5925 /* Cleanup. */
5926 uint32_t i;
5927 /* Clear streams above 1 as they might accidentally be reused in the future. */
5928 for (i = 1; i < iCurrentStreamId; ++i)
5929 {
5930 LogFunc(("clear stream %d\n", i));
5931 HRESULT hr2 = pContext->pDevice->SetStreamSource(i, NULL, 0, 0);
5932 AssertMsg(hr2 == D3D_OK, ("SetStreamSource(%d, NULL) failed with %x\n", i, hr2)); RT_NOREF(hr2);
5933 }
5934
5935 if (cVertexDivisor)
5936 {
5937 /* "When you are finished rendering the instance data, be sure to reset the vertex stream frequency back..." */
5938 for (i = 0; i < iCurrentStreamId; ++i)
5939 {
5940 LogFunc(("reset stream freq %d\n", i));
5941 HRESULT hr2 = pContext->pDevice->SetStreamSourceFreq(i, 1);
5942 AssertMsg(hr2 == D3D_OK, ("SetStreamSourceFreq(%d, 1) failed with %x\n", i, hr2)); RT_NOREF(hr2);
5943 }
5944 }
5945
5946 if (RT_SUCCESS(rc))
5947 {
5948 for (i = 0; i < numVertexDecls; ++i)
5949 {
5950 if (aVertexSurfaces[i])
5951 {
5952 aVertexSurfaces[i]->pMipmapLevels[0].fDirty = false;
5953 aVertexSurfaces[i]->fDirty = false;
5954 }
5955 }
5956
5957 for (i = 0; i < numRanges; ++i)
5958 {
5959 if (aIndexSurfaces[i])
5960 {
5961 aIndexSurfaces[i]->pMipmapLevels[0].fDirty = false;
5962 aIndexSurfaces[i]->fDirty = false;
5963 }
5964 }
5965
5966 /* Make sure we can track drawing usage of active render targets and textures. */
5967 vmsvga3dContextTrackUsage(pThis, pContext);
5968 }
5969
5970 return rc;
5971}
5972
5973#endif /* New vmsvga3dDrawPrimitives */
5974
5975int vmsvga3dSetScissorRect(PVGASTATE pThis, uint32_t cid, SVGA3dRect *pRect)
5976{
5977 HRESULT hr;
5978 RECT rect;
5979 PVMSVGA3DCONTEXT pContext;
5980 PVMSVGA3DSTATE pState = pThis->svga.p3dState;
5981 AssertReturn(pState, VERR_NO_MEMORY);
5982
5983 Log(("vmsvga3dSetScissorRect %x (%d,%d)(%d,%d)\n", cid, pRect->x, pRect->y, pRect->w, pRect->h));
5984
5985 if ( cid >= pState->cContexts
5986 || pState->papContexts[cid]->id != cid)
5987 {
5988 Log(("vmsvga3dSetScissorRect invalid context id!\n"));
5989 return VERR_INVALID_PARAMETER;
5990 }
5991 pContext = pState->papContexts[cid];
5992
5993 /* Store for vm state save/restore. */
5994 pContext->state.u32UpdateFlags |= VMSVGA3D_UPDATE_SCISSORRECT;
5995 pContext->state.RectScissor = *pRect;
5996
5997 rect.left = pRect->x;
5998 rect.top = pRect->y;
5999 rect.right = rect.left + pRect->w; /* exclusive */
6000 rect.bottom = rect.top + pRect->h; /* exclusive */
6001
6002 hr = pContext->pDevice->SetScissorRect(&rect);
6003 AssertMsgReturn(hr == D3D_OK, ("vmsvga3dSetScissorRect: SetScissorRect failed with %x\n", hr), VERR_INTERNAL_ERROR);
6004
6005 return VINF_SUCCESS;
6006}
6007
6008
6009int vmsvga3dShaderDefine(PVGASTATE pThis, uint32_t cid, uint32_t shid, SVGA3dShaderType type, uint32_t cbData, uint32_t *pShaderData)
6010{
6011 HRESULT hr;
6012 PVMSVGA3DCONTEXT pContext;
6013 PVMSVGA3DSHADER pShader;
6014 PVMSVGA3DSTATE pState = pThis->svga.p3dState;
6015 AssertReturn(pState, VERR_NO_MEMORY);
6016
6017 Log(("vmsvga3dShaderDefine %x shid=%x type=%s cbData=%x\n", cid, shid, (type == SVGA3D_SHADERTYPE_VS) ? "VERTEX" : "PIXEL", cbData));
6018#ifdef LOG_ENABLED
6019 Log3(("Shader code:\n"));
6020 const uint32_t cTokensPerLine = 8;
6021 const uint32_t *paTokens = (uint32_t *)pShaderData;
6022 const uint32_t cTokens = cbData / sizeof(uint32_t);
6023 for (uint32_t iToken = 0; iToken < cTokens; ++iToken)
6024 {
6025 if ((iToken % cTokensPerLine) == 0)
6026 {
6027 if (iToken == 0)
6028 Log3(("0x%08X,", paTokens[iToken]));
6029 else
6030 Log3(("\n0x%08X,", paTokens[iToken]));
6031 }
6032 else
6033 Log3((" 0x%08X,", paTokens[iToken]));
6034 }
6035 Log3(("\n"));
6036#endif
6037
6038 if ( cid >= pState->cContexts
6039 || pState->papContexts[cid]->id != cid)
6040 {
6041 Log(("vmsvga3dShaderDefine invalid context id!\n"));
6042 return VERR_INVALID_PARAMETER;
6043 }
6044 pContext = pState->papContexts[cid];
6045
6046 AssertReturn(shid < SVGA3D_MAX_SHADER_IDS, VERR_INVALID_PARAMETER);
6047 if (type == SVGA3D_SHADERTYPE_VS)
6048 {
6049 if (shid >= pContext->cVertexShaders)
6050 {
6051 void *pvNew = RTMemRealloc(pContext->paVertexShader, sizeof(VMSVGA3DSHADER) * (shid + 1));
6052 AssertReturn(pvNew, VERR_NO_MEMORY);
6053 pContext->paVertexShader = (PVMSVGA3DSHADER)pvNew;
6054 memset(&pContext->paVertexShader[pContext->cVertexShaders], 0, sizeof(VMSVGA3DSHADER) * (shid + 1 - pContext->cVertexShaders));
6055 for (uint32_t i = pContext->cVertexShaders; i < shid + 1; i++)
6056 pContext->paVertexShader[i].id = SVGA3D_INVALID_ID;
6057 pContext->cVertexShaders = shid + 1;
6058 }
6059 /* If one already exists with this id, then destroy it now. */
6060 if (pContext->paVertexShader[shid].id != SVGA3D_INVALID_ID)
6061 vmsvga3dShaderDestroy(pThis, cid, shid, pContext->paVertexShader[shid].type);
6062
6063 pShader = &pContext->paVertexShader[shid];
6064 }
6065 else
6066 {
6067 Assert(type == SVGA3D_SHADERTYPE_PS);
6068 if (shid >= pContext->cPixelShaders)
6069 {
6070 void *pvNew = RTMemRealloc(pContext->paPixelShader, sizeof(VMSVGA3DSHADER) * (shid + 1));
6071 AssertReturn(pvNew, VERR_NO_MEMORY);
6072 pContext->paPixelShader = (PVMSVGA3DSHADER)pvNew;
6073 memset(&pContext->paPixelShader[pContext->cPixelShaders], 0, sizeof(VMSVGA3DSHADER) * (shid + 1 - pContext->cPixelShaders));
6074 for (uint32_t i = pContext->cPixelShaders; i < shid + 1; i++)
6075 pContext->paPixelShader[i].id = SVGA3D_INVALID_ID;
6076 pContext->cPixelShaders = shid + 1;
6077 }
6078 /* If one already exists with this id, then destroy it now. */
6079 if (pContext->paPixelShader[shid].id != SVGA3D_INVALID_ID)
6080 vmsvga3dShaderDestroy(pThis, cid, shid, pContext->paPixelShader[shid].type);
6081
6082 pShader = &pContext->paPixelShader[shid];
6083 }
6084
6085 memset(pShader, 0, sizeof(*pShader));
6086 pShader->id = shid;
6087 pShader->cid = cid;
6088 pShader->type = type;
6089 pShader->cbData = cbData;
6090 pShader->pShaderProgram = RTMemAllocZ(cbData);
6091 AssertReturn(pShader->pShaderProgram, VERR_NO_MEMORY);
6092 memcpy(pShader->pShaderProgram, pShaderData, cbData);
6093
6094#ifdef DUMP_SHADER_DISASSEMBLY
6095 LPD3DXBUFFER pDisassembly;
6096 hr = D3DXDisassembleShader((const DWORD *)pShaderData, FALSE, NULL, &pDisassembly);
6097 if (hr == D3D_OK)
6098 {
6099 Log(("Shader disassembly:\n%s\n", pDisassembly->GetBufferPointer()));
6100 D3D_RELEASE(pDisassembly);
6101 }
6102#endif
6103
6104 switch (type)
6105 {
6106 case SVGA3D_SHADERTYPE_VS:
6107 hr = pContext->pDevice->CreateVertexShader((const DWORD *)pShaderData, &pShader->u.pVertexShader);
6108 break;
6109
6110 case SVGA3D_SHADERTYPE_PS:
6111 hr = pContext->pDevice->CreatePixelShader((const DWORD *)pShaderData, &pShader->u.pPixelShader);
6112 break;
6113
6114 default:
6115 AssertFailedReturn(VERR_INVALID_PARAMETER);
6116 }
6117 if (hr != D3D_OK)
6118 {
6119 RTMemFree(pShader->pShaderProgram);
6120 memset(pShader, 0, sizeof(*pShader));
6121 pShader->id = SVGA3D_INVALID_ID;
6122 }
6123
6124 AssertMsgReturn(hr == D3D_OK, ("vmsvga3dShaderDefine: CreateVertex/PixelShader failed with %x\n", hr), VERR_INTERNAL_ERROR);
6125 return VINF_SUCCESS;
6126}
6127
6128int vmsvga3dShaderDestroy(PVGASTATE pThis, uint32_t cid, uint32_t shid, SVGA3dShaderType type)
6129{
6130 PVMSVGA3DCONTEXT pContext;
6131 PVMSVGA3DSTATE pState = pThis->svga.p3dState;
6132 AssertReturn(pState, VERR_NO_MEMORY);
6133 PVMSVGA3DSHADER pShader = NULL;
6134
6135 Log(("vmsvga3dShaderDestroy %x shid=%x type=%s\n", cid, shid, (type == SVGA3D_SHADERTYPE_VS) ? "VERTEX" : "PIXEL"));
6136
6137 if ( cid >= pState->cContexts
6138 || pState->papContexts[cid]->id != cid)
6139 {
6140 Log(("vmsvga3dShaderDestroy invalid context id!\n"));
6141 return VERR_INVALID_PARAMETER;
6142 }
6143 pContext = pState->papContexts[cid];
6144
6145 if (type == SVGA3D_SHADERTYPE_VS)
6146 {
6147 if ( shid < pContext->cVertexShaders
6148 && pContext->paVertexShader[shid].id == shid)
6149 {
6150 pShader = &pContext->paVertexShader[shid];
6151 D3D_RELEASE(pShader->u.pVertexShader);
6152 }
6153 }
6154 else
6155 {
6156 Assert(type == SVGA3D_SHADERTYPE_PS);
6157 if ( shid < pContext->cPixelShaders
6158 && pContext->paPixelShader[shid].id == shid)
6159 {
6160 pShader = &pContext->paPixelShader[shid];
6161 D3D_RELEASE(pShader->u.pPixelShader);
6162 }
6163 }
6164
6165 if (pShader)
6166 {
6167 if (pShader->pShaderProgram)
6168 RTMemFree(pShader->pShaderProgram);
6169
6170 memset(pShader, 0, sizeof(*pShader));
6171 pShader->id = SVGA3D_INVALID_ID;
6172 }
6173 else
6174 AssertFailedReturn(VERR_INVALID_PARAMETER);
6175
6176 return VINF_SUCCESS;
6177}
6178
6179int vmsvga3dShaderSet(PVGASTATE pThis, PVMSVGA3DCONTEXT pContext, uint32_t cid, SVGA3dShaderType type, uint32_t shid)
6180{
6181 PVMSVGA3DSTATE pState = pThis->svga.p3dState;
6182 AssertReturn(pState, VERR_NO_MEMORY);
6183 HRESULT hr;
6184
6185 Log(("vmsvga3dShaderSet %x type=%s shid=%d\n", cid, (type == SVGA3D_SHADERTYPE_VS) ? "VERTEX" : "PIXEL", shid));
6186
6187 NOREF(pContext);
6188 if ( cid >= pState->cContexts
6189 || pState->papContexts[cid]->id != cid)
6190 {
6191 Log(("vmsvga3dShaderSet invalid context id!\n"));
6192 return VERR_INVALID_PARAMETER;
6193 }
6194 pContext = pState->papContexts[cid];
6195
6196 if (type == SVGA3D_SHADERTYPE_VS)
6197 {
6198 /* Save for vm state save/restore. */
6199 pContext->state.shidVertex = shid;
6200 pContext->state.u32UpdateFlags |= VMSVGA3D_UPDATE_VERTEXSHADER;
6201
6202 if ( shid < pContext->cVertexShaders
6203 && pContext->paVertexShader[shid].id == shid)
6204 {
6205 PVMSVGA3DSHADER pShader = &pContext->paVertexShader[shid];
6206 Assert(type == pShader->type);
6207
6208 hr = pContext->pDevice->SetVertexShader(pShader->u.pVertexShader);
6209 AssertMsgReturn(hr == D3D_OK, ("vmsvga3dShaderSet: SetVertex/PixelShader failed with %x\n", hr), VERR_INTERNAL_ERROR);
6210 }
6211 else
6212 if (shid == SVGA_ID_INVALID)
6213 {
6214 /* Unselect shader. */
6215 hr = pContext->pDevice->SetVertexShader(NULL);
6216 AssertMsgReturn(hr == D3D_OK, ("vmsvga3dShaderSet: SetVertex/PixelShader failed with %x\n", hr), VERR_INTERNAL_ERROR);
6217 }
6218 else
6219 AssertFailedReturn(VERR_INVALID_PARAMETER);
6220 }
6221 else
6222 {
6223 /* Save for vm state save/restore. */
6224 pContext->state.shidPixel = shid;
6225 pContext->state.u32UpdateFlags |= VMSVGA3D_UPDATE_PIXELSHADER;
6226
6227 Assert(type == SVGA3D_SHADERTYPE_PS);
6228 if ( shid < pContext->cPixelShaders
6229 && pContext->paPixelShader[shid].id == shid)
6230 {
6231 PVMSVGA3DSHADER pShader = &pContext->paPixelShader[shid];
6232 Assert(type == pShader->type);
6233
6234 hr = pContext->pDevice->SetPixelShader(pShader->u.pPixelShader);
6235 AssertMsgReturn(hr == D3D_OK, ("vmsvga3dShaderSet: SetVertex/PixelShader failed with %x\n", hr), VERR_INTERNAL_ERROR);
6236 }
6237 else
6238 if (shid == SVGA_ID_INVALID)
6239 {
6240 /* Unselect shader. */
6241 hr = pContext->pDevice->SetPixelShader(NULL);
6242 AssertMsgReturn(hr == D3D_OK, ("vmsvga3dShaderSet: SetVertex/PixelShader failed with %x\n", hr), VERR_INTERNAL_ERROR);
6243 }
6244 else
6245 AssertFailedReturn(VERR_INVALID_PARAMETER);
6246 }
6247
6248 return VINF_SUCCESS;
6249}
6250
6251int vmsvga3dShaderSetConst(PVGASTATE pThis, uint32_t cid, uint32_t reg, SVGA3dShaderType type, SVGA3dShaderConstType ctype, uint32_t cRegisters, uint32_t *pValues)
6252{
6253 HRESULT hr;
6254 PVMSVGA3DCONTEXT pContext;
6255 PVMSVGA3DSTATE pState = pThis->svga.p3dState;
6256 AssertReturn(pState, VERR_NO_MEMORY);
6257
6258 Log(("vmsvga3dShaderSetConst %x reg=%x type=%s ctype=%x\n", cid, reg, (type == SVGA3D_SHADERTYPE_VS) ? "VERTEX" : "PIXEL", ctype));
6259
6260 if ( cid >= pState->cContexts
6261 || pState->papContexts[cid]->id != cid)
6262 {
6263 Log(("vmsvga3dShaderSetConst invalid context id!\n"));
6264 return VERR_INVALID_PARAMETER;
6265 }
6266 pContext = pState->papContexts[cid];
6267
6268 for (uint32_t i = 0; i < cRegisters; i++)
6269 {
6270 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]));
6271 vmsvga3dSaveShaderConst(pContext, reg + i, type, ctype, pValues[i*4 + 0], pValues[i*4 + 1], pValues[i*4 + 2], pValues[i*4 + 3]);
6272 }
6273
6274 switch (type)
6275 {
6276 case SVGA3D_SHADERTYPE_VS:
6277 switch (ctype)
6278 {
6279 case SVGA3D_CONST_TYPE_FLOAT:
6280 hr = pContext->pDevice->SetVertexShaderConstantF(reg, (const float *)pValues, cRegisters);
6281 break;
6282
6283 case SVGA3D_CONST_TYPE_INT:
6284 hr = pContext->pDevice->SetVertexShaderConstantI(reg, (const int *)pValues, cRegisters);
6285 break;
6286
6287 case SVGA3D_CONST_TYPE_BOOL:
6288 hr = pContext->pDevice->SetVertexShaderConstantB(reg, (const BOOL *)pValues, cRegisters);
6289 break;
6290
6291 default:
6292 AssertFailedReturn(VERR_INVALID_PARAMETER);
6293 }
6294 AssertMsgReturn(hr == D3D_OK, ("vmsvga3dShaderSetConst: SetVertexShader failed with %x\n", hr), VERR_INTERNAL_ERROR);
6295 break;
6296
6297 case SVGA3D_SHADERTYPE_PS:
6298 switch (ctype)
6299 {
6300 case SVGA3D_CONST_TYPE_FLOAT:
6301 {
6302 hr = pContext->pDevice->SetPixelShaderConstantF(reg, (const float *)pValues, cRegisters);
6303 break;
6304 }
6305
6306 case SVGA3D_CONST_TYPE_INT:
6307 hr = pContext->pDevice->SetPixelShaderConstantI(reg, (const int *)pValues, cRegisters);
6308 break;
6309
6310 case SVGA3D_CONST_TYPE_BOOL:
6311 hr = pContext->pDevice->SetPixelShaderConstantB(reg, (const BOOL *)pValues, cRegisters);
6312 break;
6313
6314 default:
6315 AssertFailedReturn(VERR_INVALID_PARAMETER);
6316 }
6317 AssertMsgReturn(hr == D3D_OK, ("vmsvga3dShaderSetConst: SetPixelShader failed with %x\n", hr), VERR_INTERNAL_ERROR);
6318 break;
6319
6320 default:
6321 AssertFailedReturn(VERR_INVALID_PARAMETER);
6322 }
6323 return VINF_SUCCESS;
6324}
6325
6326int vmsvga3dOcclusionQueryCreate(PVMSVGA3DSTATE pState, PVMSVGA3DCONTEXT pContext)
6327{
6328 RT_NOREF(pState);
6329 HRESULT hr = pContext->pDevice->CreateQuery(D3DQUERYTYPE_OCCLUSION, &pContext->occlusion.pQuery);
6330 AssertMsgReturn(hr == D3D_OK, ("CreateQuery(D3DQUERYTYPE_OCCLUSION) failed with %x\n", hr), VERR_INTERNAL_ERROR);
6331 return VINF_SUCCESS;
6332}
6333
6334int vmsvga3dOcclusionQueryDelete(PVMSVGA3DSTATE pState, PVMSVGA3DCONTEXT pContext)
6335{
6336 RT_NOREF(pState);
6337 D3D_RELEASE(pContext->occlusion.pQuery);
6338 return VINF_SUCCESS;
6339}
6340
6341int vmsvga3dOcclusionQueryBegin(PVMSVGA3DSTATE pState, PVMSVGA3DCONTEXT pContext)
6342{
6343 RT_NOREF(pState);
6344 HRESULT hr = pContext->occlusion.pQuery->Issue(D3DISSUE_BEGIN);
6345 AssertMsgReturnStmt(hr == D3D_OK, ("D3DISSUE_BEGIN(D3DQUERYTYPE_OCCLUSION) failed with %x\n", hr),
6346 D3D_RELEASE(pContext->occlusion.pQuery), VERR_INTERNAL_ERROR);
6347 return VINF_SUCCESS;
6348}
6349
6350int vmsvga3dOcclusionQueryEnd(PVMSVGA3DSTATE pState, PVMSVGA3DCONTEXT pContext)
6351{
6352 RT_NOREF(pState);
6353 HRESULT hr = pContext->occlusion.pQuery->Issue(D3DISSUE_END);
6354 AssertMsgReturnStmt(hr == D3D_OK, ("D3DISSUE_END(D3DQUERYTYPE_OCCLUSION) failed with %x\n", hr),
6355 D3D_RELEASE(pContext->occlusion.pQuery), VERR_INTERNAL_ERROR);
6356 return VINF_SUCCESS;
6357}
6358
6359int vmsvga3dOcclusionQueryGetData(PVMSVGA3DSTATE pState, PVMSVGA3DCONTEXT pContext, uint32_t *pu32Pixels)
6360{
6361 RT_NOREF(pState);
6362 HRESULT hr = D3D_OK;
6363 /* Wait until the data becomes available. */
6364 DWORD dwPixels = 0;
6365 do
6366 {
6367 hr = pContext->occlusion.pQuery->GetData((void *)&dwPixels, sizeof(DWORD), D3DGETDATA_FLUSH);
6368 } while (hr == S_FALSE);
6369
6370 AssertMsgReturnStmt(hr == D3D_OK, ("GetData(D3DQUERYTYPE_OCCLUSION) failed with %x\n", hr),
6371 D3D_RELEASE(pContext->occlusion.pQuery), VERR_INTERNAL_ERROR);
6372
6373 LogFunc(("Query result: dwPixels %d\n", dwPixels));
6374 *pu32Pixels = dwPixels;
6375 return VINF_SUCCESS;
6376}
6377
6378static void vmsvgaDumpD3DCaps(D3DCAPS9 *pCaps)
6379{
6380 bool const fBufferingSaved = RTLogRelSetBuffering(true /*fBuffered*/);
6381
6382 LogRel(("\nD3D device caps: DevCaps2:\n"));
6383 if (pCaps->DevCaps2 & D3DDEVCAPS2_ADAPTIVETESSRTPATCH)
6384 LogRel((" - D3DDEVCAPS2_ADAPTIVETESSRTPATCH\n"));
6385 if (pCaps->DevCaps2 & D3DDEVCAPS2_ADAPTIVETESSNPATCH)
6386 LogRel((" - D3DDEVCAPS2_ADAPTIVETESSNPATCH\n"));
6387 if (pCaps->DevCaps2 & D3DDEVCAPS2_CAN_STRETCHRECT_FROM_TEXTURES)
6388 LogRel((" - D3DDEVCAPS2_CAN_STRETCHRECT_FROM_TEXTURES\n"));
6389 if (pCaps->DevCaps2 & D3DDEVCAPS2_DMAPNPATCH)
6390 LogRel((" - D3DDEVCAPS2_DMAPNPATCH\n"));
6391 if (pCaps->DevCaps2 & D3DDEVCAPS2_PRESAMPLEDDMAPNPATCH)
6392 LogRel((" - D3DDEVCAPS2_PRESAMPLEDDMAPNPATCH\n"));
6393 if (pCaps->DevCaps2 & D3DDEVCAPS2_STREAMOFFSET)
6394 LogRel((" - D3DDEVCAPS2_STREAMOFFSET\n"));
6395 if (pCaps->DevCaps2 & D3DDEVCAPS2_VERTEXELEMENTSCANSHARESTREAMOFFSET)
6396 LogRel((" - D3DDEVCAPS2_VERTEXELEMENTSCANSHARESTREAMOFFSET\n"));
6397
6398 LogRel(("\nCaps2:\n"));
6399 if (pCaps->Caps2 & D3DCAPS2_CANAUTOGENMIPMAP)
6400 LogRel((" - D3DCAPS2_CANAUTOGENMIPMAP\n"));
6401 if (pCaps->Caps2 & D3DCAPS2_CANCALIBRATEGAMMA)
6402 LogRel((" - D3DCAPS2_CANCALIBRATEGAMMA\n"));
6403 if (pCaps->Caps2 & D3DCAPS2_CANSHARERESOURCE)
6404 LogRel((" - D3DCAPS2_CANSHARERESOURCE\n"));
6405 if (pCaps->Caps2 & D3DCAPS2_CANMANAGERESOURCE)
6406 LogRel((" - D3DCAPS2_CANMANAGERESOURCE\n"));
6407 if (pCaps->Caps2 & D3DCAPS2_DYNAMICTEXTURES)
6408 LogRel((" - D3DCAPS2_DYNAMICTEXTURES\n"));
6409 if (pCaps->Caps2 & D3DCAPS2_FULLSCREENGAMMA)
6410 LogRel((" - D3DCAPS2_FULLSCREENGAMMA\n"));
6411
6412 LogRel(("\nCaps3:\n"));
6413 if (pCaps->Caps3 & D3DCAPS3_ALPHA_FULLSCREEN_FLIP_OR_DISCARD)
6414 LogRel((" - D3DCAPS3_ALPHA_FULLSCREEN_FLIP_OR_DISCARD\n"));
6415 if (pCaps->Caps3 & D3DCAPS3_COPY_TO_VIDMEM)
6416 LogRel((" - D3DCAPS3_COPY_TO_VIDMEM\n"));
6417 if (pCaps->Caps3 & D3DCAPS3_COPY_TO_SYSTEMMEM)
6418 LogRel((" - D3DCAPS3_COPY_TO_SYSTEMMEM\n"));
6419 if (pCaps->Caps3 & D3DCAPS3_DXVAHD)
6420 LogRel((" - D3DCAPS3_DXVAHD\n"));
6421 if (pCaps->Caps3 & D3DCAPS3_LINEAR_TO_SRGB_PRESENTATION)
6422 LogRel((" - D3DCAPS3_LINEAR_TO_SRGB_PRESENTATION\n"));
6423
6424 LogRel(("\nPresentationIntervals:\n"));
6425 if (pCaps->PresentationIntervals & D3DPRESENT_INTERVAL_IMMEDIATE)
6426 LogRel((" - D3DPRESENT_INTERVAL_IMMEDIATE\n"));
6427 if (pCaps->PresentationIntervals & D3DPRESENT_INTERVAL_ONE)
6428 LogRel((" - D3DPRESENT_INTERVAL_ONE\n"));
6429 if (pCaps->PresentationIntervals & D3DPRESENT_INTERVAL_TWO)
6430 LogRel((" - D3DPRESENT_INTERVAL_TWO\n"));
6431 if (pCaps->PresentationIntervals & D3DPRESENT_INTERVAL_THREE)
6432 LogRel((" - D3DPRESENT_INTERVAL_THREE\n"));
6433 if (pCaps->PresentationIntervals & D3DPRESENT_INTERVAL_FOUR)
6434 LogRel((" - D3DPRESENT_INTERVAL_FOUR\n"));
6435
6436 LogRel(("\nDevcaps:\n"));
6437 if (pCaps->DevCaps & D3DDEVCAPS_CANBLTSYSTONONLOCAL)
6438 LogRel((" - D3DDEVCAPS_CANBLTSYSTONONLOCAL\n"));
6439 if (pCaps->DevCaps & D3DDEVCAPS_CANRENDERAFTERFLIP)
6440 LogRel((" - D3DDEVCAPS_CANRENDERAFTERFLIP\n"));
6441 if (pCaps->DevCaps & D3DDEVCAPS_DRAWPRIMITIVES2)
6442 LogRel((" - D3DDEVCAPS_DRAWPRIMITIVES2\n"));
6443 if (pCaps->DevCaps & D3DDEVCAPS_DRAWPRIMITIVES2EX)
6444 LogRel((" - D3DDEVCAPS_DRAWPRIMITIVES2EX\n"));
6445 if (pCaps->DevCaps & D3DDEVCAPS_DRAWPRIMTLVERTEX)
6446 LogRel((" - D3DDEVCAPS_DRAWPRIMTLVERTEX\n"));
6447 if (pCaps->DevCaps & D3DDEVCAPS_EXECUTESYSTEMMEMORY)
6448 LogRel((" - D3DDEVCAPS_EXECUTESYSTEMMEMORY\n"));
6449 if (pCaps->DevCaps & D3DDEVCAPS_EXECUTEVIDEOMEMORY)
6450 LogRel((" - D3DDEVCAPS_EXECUTEVIDEOMEMORY\n"));
6451 if (pCaps->DevCaps & D3DDEVCAPS_HWRASTERIZATION)
6452 LogRel((" - D3DDEVCAPS_HWRASTERIZATION\n"));
6453 if (pCaps->DevCaps & D3DDEVCAPS_HWTRANSFORMANDLIGHT)
6454 LogRel((" - D3DDEVCAPS_HWTRANSFORMANDLIGHT\n"));
6455 if (pCaps->DevCaps & D3DDEVCAPS_NPATCHES)
6456 LogRel((" - D3DDEVCAPS_NPATCHES\n"));
6457 if (pCaps->DevCaps & D3DDEVCAPS_PUREDEVICE)
6458 LogRel((" - D3DDEVCAPS_PUREDEVICE\n"));
6459 if (pCaps->DevCaps & D3DDEVCAPS_QUINTICRTPATCHES)
6460 LogRel((" - D3DDEVCAPS_QUINTICRTPATCHES\n"));
6461 if (pCaps->DevCaps & D3DDEVCAPS_RTPATCHES)
6462 LogRel((" - D3DDEVCAPS_RTPATCHES\n"));
6463 if (pCaps->DevCaps & D3DDEVCAPS_RTPATCHHANDLEZERO)
6464 LogRel((" - D3DDEVCAPS_RTPATCHHANDLEZERO\n"));
6465 if (pCaps->DevCaps & D3DDEVCAPS_SEPARATETEXTUREMEMORIES)
6466 LogRel((" - D3DDEVCAPS_SEPARATETEXTUREMEMORIES\n"));
6467 if (pCaps->DevCaps & D3DDEVCAPS_TEXTURENONLOCALVIDMEM)
6468 LogRel((" - D3DDEVCAPS_TEXTURENONLOCALVIDMEM\n"));
6469 if (pCaps->DevCaps & D3DDEVCAPS_TEXTURESYSTEMMEMORY)
6470 LogRel((" - D3DDEVCAPS_TEXTURESYSTEMMEMORY\n"));
6471 if (pCaps->DevCaps & D3DDEVCAPS_TEXTUREVIDEOMEMORY)
6472 LogRel((" - D3DDEVCAPS_TEXTUREVIDEOMEMORY\n"));
6473 if (pCaps->DevCaps & D3DDEVCAPS_TLVERTEXSYSTEMMEMORY)
6474 LogRel((" - D3DDEVCAPS_TLVERTEXSYSTEMMEMORY\n"));
6475 if (pCaps->DevCaps & D3DDEVCAPS_TLVERTEXVIDEOMEMORY)
6476 LogRel((" - D3DDEVCAPS_TLVERTEXVIDEOMEMORY\n"));
6477
6478 LogRel(("\nTextureCaps:\n"));
6479 if (pCaps->TextureCaps & D3DPTEXTURECAPS_ALPHA)
6480 LogRel((" - D3DPTEXTURECAPS_ALPHA\n"));
6481 if (pCaps->TextureCaps & D3DPTEXTURECAPS_ALPHAPALETTE)
6482 LogRel((" - D3DPTEXTURECAPS_ALPHAPALETTE\n"));
6483 if (pCaps->TextureCaps & D3DPTEXTURECAPS_CUBEMAP)
6484 LogRel((" - D3DPTEXTURECAPS_CUBEMAP\n"));
6485 if (pCaps->TextureCaps & D3DPTEXTURECAPS_CUBEMAP_POW2)
6486 LogRel((" - D3DPTEXTURECAPS_CUBEMAP_POW2\n"));
6487 if (pCaps->TextureCaps & D3DPTEXTURECAPS_MIPCUBEMAP)
6488 LogRel((" - D3DPTEXTURECAPS_MIPCUBEMAP\n"));
6489 if (pCaps->TextureCaps & D3DPTEXTURECAPS_MIPMAP)
6490 LogRel((" - D3DPTEXTURECAPS_MIPMAP\n"));
6491 if (pCaps->TextureCaps & D3DPTEXTURECAPS_MIPVOLUMEMAP)
6492 LogRel((" - D3DPTEXTURECAPS_MIPVOLUMEMAP\n"));
6493 if (pCaps->TextureCaps & D3DPTEXTURECAPS_NONPOW2CONDITIONAL)
6494 LogRel((" - D3DPTEXTURECAPS_NONPOW2CONDITIONAL\n"));
6495 if (pCaps->TextureCaps & D3DPTEXTURECAPS_POW2)
6496 LogRel((" - D3DPTEXTURECAPS_POW2\n"));
6497 if (pCaps->TextureCaps & D3DPTEXTURECAPS_NOPROJECTEDBUMPENV)
6498 LogRel((" - D3DPTEXTURECAPS_NOPROJECTEDBUMPENV\n"));
6499 if (pCaps->TextureCaps & D3DPTEXTURECAPS_PERSPECTIVE)
6500 LogRel((" - D3DPTEXTURECAPS_PERSPECTIVE\n"));
6501 if (pCaps->TextureCaps & D3DPTEXTURECAPS_POW2)
6502 LogRel((" - D3DPTEXTURECAPS_POW2\n"));
6503 if (pCaps->TextureCaps & D3DPTEXTURECAPS_PROJECTED)
6504 LogRel((" - D3DPTEXTURECAPS_PROJECTED\n"));
6505 if (pCaps->TextureCaps & D3DPTEXTURECAPS_SQUAREONLY)
6506 LogRel((" - D3DPTEXTURECAPS_SQUAREONLY\n"));
6507 if (pCaps->TextureCaps & D3DPTEXTURECAPS_TEXREPEATNOTSCALEDBYSIZE)
6508 LogRel((" - D3DPTEXTURECAPS_TEXREPEATNOTSCALEDBYSIZE\n"));
6509 if (pCaps->TextureCaps & D3DPTEXTURECAPS_VOLUMEMAP)
6510 LogRel((" - D3DPTEXTURECAPS_VOLUMEMAP\n"));
6511 if (pCaps->TextureCaps & D3DPTEXTURECAPS_VOLUMEMAP_POW2)
6512 LogRel((" - D3DPTEXTURECAPS_VOLUMEMAP_POW2\n"));
6513
6514 LogRel(("\nTextureFilterCaps\n"));
6515 if (pCaps->TextureFilterCaps & D3DPTFILTERCAPS_CONVOLUTIONMONO)
6516 LogRel((" - D3DPTFILTERCAPS_CONVOLUTIONMONO\n"));
6517 if (pCaps->TextureFilterCaps & D3DPTFILTERCAPS_MAGFPOINT)
6518 LogRel((" - D3DPTFILTERCAPS_MAGFPOINT\n"));
6519 if (pCaps->TextureFilterCaps & D3DPTFILTERCAPS_MAGFLINEAR)
6520 LogRel((" - D3DPTFILTERCAPS_MAGFLINEAR\n"));
6521 if (pCaps->TextureFilterCaps & D3DPTFILTERCAPS_MAGFANISOTROPIC)
6522 LogRel((" - D3DPTFILTERCAPS_MAGFANISOTROPIC\n"));
6523 if (pCaps->TextureFilterCaps & D3DPTFILTERCAPS_MAGFPYRAMIDALQUAD)
6524 LogRel((" - D3DPTFILTERCAPS_MAGFPYRAMIDALQUAD\n"));
6525 if (pCaps->TextureFilterCaps & D3DPTFILTERCAPS_MAGFGAUSSIANQUAD)
6526 LogRel((" - D3DPTFILTERCAPS_MAGFGAUSSIANQUAD\n"));
6527 if (pCaps->TextureFilterCaps & D3DPTFILTERCAPS_MINFPOINT)
6528 LogRel((" - D3DPTFILTERCAPS_MINFPOINT\n"));
6529 if (pCaps->TextureFilterCaps & D3DPTFILTERCAPS_MINFLINEAR)
6530 LogRel((" - D3DPTFILTERCAPS_MINFLINEAR\n"));
6531 if (pCaps->TextureFilterCaps & D3DPTFILTERCAPS_MINFANISOTROPIC)
6532 LogRel((" - D3DPTFILTERCAPS_MINFANISOTROPIC\n"));
6533 if (pCaps->TextureFilterCaps & D3DPTFILTERCAPS_MINFPYRAMIDALQUAD)
6534 LogRel((" - D3DPTFILTERCAPS_MINFPYRAMIDALQUAD\n"));
6535 if (pCaps->TextureFilterCaps & D3DPTFILTERCAPS_MINFGAUSSIANQUAD)
6536 LogRel((" - D3DPTFILTERCAPS_MINFGAUSSIANQUAD\n"));
6537 if (pCaps->TextureFilterCaps & D3DPTFILTERCAPS_MIPFPOINT)
6538 LogRel((" - D3DPTFILTERCAPS_MIPFPOINT\n"));
6539 if (pCaps->TextureFilterCaps & D3DPTFILTERCAPS_MIPFLINEAR)
6540 LogRel((" - D3DPTFILTERCAPS_MIPFLINEAR\n"));
6541
6542 LogRel(("\nCubeTextureFilterCaps\n"));
6543 if (pCaps->CubeTextureFilterCaps & D3DPTFILTERCAPS_CONVOLUTIONMONO)
6544 LogRel((" - D3DPTFILTERCAPS_CONVOLUTIONMONO\n"));
6545 if (pCaps->CubeTextureFilterCaps & D3DPTFILTERCAPS_MAGFPOINT)
6546 LogRel((" - D3DPTFILTERCAPS_MAGFPOINT\n"));
6547 if (pCaps->CubeTextureFilterCaps & D3DPTFILTERCAPS_MAGFLINEAR)
6548 LogRel((" - D3DPTFILTERCAPS_MAGFLINEAR\n"));
6549 if (pCaps->CubeTextureFilterCaps & D3DPTFILTERCAPS_MAGFANISOTROPIC)
6550 LogRel((" - D3DPTFILTERCAPS_MAGFANISOTROPIC\n"));
6551 if (pCaps->CubeTextureFilterCaps & D3DPTFILTERCAPS_MAGFPYRAMIDALQUAD)
6552 LogRel((" - D3DPTFILTERCAPS_MAGFPYRAMIDALQUAD\n"));
6553 if (pCaps->CubeTextureFilterCaps & D3DPTFILTERCAPS_MAGFGAUSSIANQUAD)
6554 LogRel((" - D3DPTFILTERCAPS_MAGFGAUSSIANQUAD\n"));
6555 if (pCaps->CubeTextureFilterCaps & D3DPTFILTERCAPS_MINFPOINT)
6556 LogRel((" - D3DPTFILTERCAPS_MINFPOINT\n"));
6557 if (pCaps->CubeTextureFilterCaps & D3DPTFILTERCAPS_MINFLINEAR)
6558 LogRel((" - D3DPTFILTERCAPS_MINFLINEAR\n"));
6559 if (pCaps->CubeTextureFilterCaps & D3DPTFILTERCAPS_MINFANISOTROPIC)
6560 LogRel((" - D3DPTFILTERCAPS_MINFANISOTROPIC\n"));
6561 if (pCaps->CubeTextureFilterCaps & D3DPTFILTERCAPS_MINFPYRAMIDALQUAD)
6562 LogRel((" - D3DPTFILTERCAPS_MINFPYRAMIDALQUAD\n"));
6563 if (pCaps->CubeTextureFilterCaps & D3DPTFILTERCAPS_MINFGAUSSIANQUAD)
6564 LogRel((" - D3DPTFILTERCAPS_MINFGAUSSIANQUAD\n"));
6565 if (pCaps->CubeTextureFilterCaps & D3DPTFILTERCAPS_MIPFPOINT)
6566 LogRel((" - D3DPTFILTERCAPS_MIPFPOINT\n"));
6567 if (pCaps->CubeTextureFilterCaps & D3DPTFILTERCAPS_MIPFLINEAR)
6568 LogRel((" - D3DPTFILTERCAPS_MIPFLINEAR\n"));
6569
6570 LogRel(("\nVolumeTextureFilterCaps\n"));
6571 if (pCaps->VolumeTextureFilterCaps & D3DPTFILTERCAPS_CONVOLUTIONMONO)
6572 LogRel((" - D3DPTFILTERCAPS_CONVOLUTIONMONO\n"));
6573 if (pCaps->VolumeTextureFilterCaps & D3DPTFILTERCAPS_MAGFPOINT)
6574 LogRel((" - D3DPTFILTERCAPS_MAGFPOINT\n"));
6575 if (pCaps->VolumeTextureFilterCaps & D3DPTFILTERCAPS_MAGFLINEAR)
6576 LogRel((" - D3DPTFILTERCAPS_MAGFLINEAR\n"));
6577 if (pCaps->VolumeTextureFilterCaps & D3DPTFILTERCAPS_MAGFANISOTROPIC)
6578 LogRel((" - D3DPTFILTERCAPS_MAGFANISOTROPIC\n"));
6579 if (pCaps->VolumeTextureFilterCaps & D3DPTFILTERCAPS_MAGFPYRAMIDALQUAD)
6580 LogRel((" - D3DPTFILTERCAPS_MAGFPYRAMIDALQUAD\n"));
6581 if (pCaps->VolumeTextureFilterCaps & D3DPTFILTERCAPS_MAGFGAUSSIANQUAD)
6582 LogRel((" - D3DPTFILTERCAPS_MAGFGAUSSIANQUAD\n"));
6583 if (pCaps->VolumeTextureFilterCaps & D3DPTFILTERCAPS_MINFPOINT)
6584 LogRel((" - D3DPTFILTERCAPS_MINFPOINT\n"));
6585 if (pCaps->VolumeTextureFilterCaps & D3DPTFILTERCAPS_MINFLINEAR)
6586 LogRel((" - D3DPTFILTERCAPS_MINFLINEAR\n"));
6587 if (pCaps->VolumeTextureFilterCaps & D3DPTFILTERCAPS_MINFANISOTROPIC)
6588 LogRel((" - D3DPTFILTERCAPS_MINFANISOTROPIC\n"));
6589 if (pCaps->VolumeTextureFilterCaps & D3DPTFILTERCAPS_MINFPYRAMIDALQUAD)
6590 LogRel((" - D3DPTFILTERCAPS_MINFPYRAMIDALQUAD\n"));
6591 if (pCaps->VolumeTextureFilterCaps & D3DPTFILTERCAPS_MINFGAUSSIANQUAD)
6592 LogRel((" - D3DPTFILTERCAPS_MINFGAUSSIANQUAD\n"));
6593 if (pCaps->VolumeTextureFilterCaps & D3DPTFILTERCAPS_MIPFPOINT)
6594 LogRel((" - D3DPTFILTERCAPS_MIPFPOINT\n"));
6595 if (pCaps->VolumeTextureFilterCaps & D3DPTFILTERCAPS_MIPFLINEAR)
6596 LogRel((" - D3DPTFILTERCAPS_MIPFLINEAR\n"));
6597
6598 LogRel(("\nTextureAddressCaps:\n"));
6599 if (pCaps->TextureAddressCaps & D3DPTADDRESSCAPS_BORDER)
6600 LogRel((" - D3DPTADDRESSCAPS_BORDER\n"));
6601 if (pCaps->TextureAddressCaps & D3DPTADDRESSCAPS_CLAMP)
6602 LogRel((" - D3DPTADDRESSCAPS_CLAMP\n"));
6603 if (pCaps->TextureAddressCaps & D3DPTADDRESSCAPS_INDEPENDENTUV)
6604 LogRel((" - D3DPTADDRESSCAPS_INDEPENDENTUV\n"));
6605 if (pCaps->TextureAddressCaps & D3DPTADDRESSCAPS_MIRROR)
6606 LogRel((" - D3DPTADDRESSCAPS_MIRROR\n"));
6607 if (pCaps->TextureAddressCaps & D3DPTADDRESSCAPS_MIRRORONCE)
6608 LogRel((" - D3DPTADDRESSCAPS_MIRRORONCE\n"));
6609 if (pCaps->TextureAddressCaps & D3DPTADDRESSCAPS_WRAP)
6610 LogRel((" - D3DPTADDRESSCAPS_WRAP\n"));
6611
6612 LogRel(("\nTextureOpCaps:\n"));
6613 if (pCaps->TextureOpCaps & D3DTEXOPCAPS_DISABLE)
6614 LogRel((" - D3DTEXOPCAPS_DISABLE\n"));
6615 if (pCaps->TextureOpCaps & D3DTEXOPCAPS_SELECTARG1)
6616 LogRel((" - D3DTEXOPCAPS_SELECTARG1\n"));
6617 if (pCaps->TextureOpCaps & D3DTEXOPCAPS_SELECTARG2)
6618 LogRel((" - D3DTEXOPCAPS_SELECTARG2\n"));
6619 if (pCaps->TextureOpCaps & D3DTEXOPCAPS_MODULATE)
6620 LogRel((" - D3DTEXOPCAPS_MODULATE\n"));
6621 if (pCaps->TextureOpCaps & D3DTEXOPCAPS_MODULATE2X)
6622 LogRel((" - D3DTEXOPCAPS_MODULATE2X\n"));
6623 if (pCaps->TextureOpCaps & D3DTEXOPCAPS_MODULATE4X)
6624 LogRel((" - D3DTEXOPCAPS_MODULATE4X\n"));
6625 if (pCaps->TextureOpCaps & D3DTEXOPCAPS_ADD)
6626 LogRel((" - D3DTEXOPCAPS_ADD\n"));
6627 if (pCaps->TextureOpCaps & D3DTEXOPCAPS_ADDSIGNED)
6628 LogRel((" - D3DTEXOPCAPS_ADDSIGNED\n"));
6629 if (pCaps->TextureOpCaps & D3DTEXOPCAPS_ADDSIGNED2X)
6630 LogRel((" - D3DTEXOPCAPS_ADDSIGNED2X\n"));
6631 if (pCaps->TextureOpCaps & D3DTEXOPCAPS_SUBTRACT)
6632 LogRel((" - D3DTEXOPCAPS_SUBTRACT\n"));
6633 if (pCaps->TextureOpCaps & D3DTEXOPCAPS_ADDSMOOTH)
6634 LogRel((" - D3DTEXOPCAPS_ADDSMOOTH\n"));
6635 if (pCaps->TextureOpCaps & D3DTEXOPCAPS_BLENDDIFFUSEALPHA)
6636 LogRel((" - D3DTEXOPCAPS_BLENDDIFFUSEALPHA\n"));
6637 if (pCaps->TextureOpCaps & D3DTEXOPCAPS_BLENDTEXTUREALPHA)
6638 LogRel((" - D3DTEXOPCAPS_BLENDTEXTUREALPHA\n"));
6639 if (pCaps->TextureOpCaps & D3DTEXOPCAPS_BLENDFACTORALPHA)
6640 LogRel((" - D3DTEXOPCAPS_BLENDFACTORALPHA\n"));
6641 if (pCaps->TextureOpCaps & D3DTEXOPCAPS_BLENDTEXTUREALPHAPM)
6642 LogRel((" - D3DTEXOPCAPS_BLENDTEXTUREALPHAPM\n"));
6643 if (pCaps->TextureOpCaps & D3DTEXOPCAPS_BLENDCURRENTALPHA)
6644 LogRel((" - D3DTEXOPCAPS_BLENDCURRENTALPHA\n"));
6645 if (pCaps->TextureOpCaps & D3DTEXOPCAPS_PREMODULATE)
6646 LogRel((" - D3DTEXOPCAPS_PREMODULATE\n"));
6647 if (pCaps->TextureOpCaps & D3DTEXOPCAPS_MODULATEALPHA_ADDCOLOR)
6648 LogRel((" - D3DTEXOPCAPS_MODULATEALPHA_ADDCOLOR\n"));
6649 if (pCaps->TextureOpCaps & D3DTEXOPCAPS_MODULATECOLOR_ADDALPHA)
6650 LogRel((" - D3DTEXOPCAPS_MODULATECOLOR_ADDALPHA\n"));
6651 if (pCaps->TextureOpCaps & D3DTEXOPCAPS_MODULATEINVALPHA_ADDCOLOR)
6652 LogRel((" - D3DTEXOPCAPS_MODULATEINVALPHA_ADDCOLOR\n"));
6653 if (pCaps->TextureOpCaps & D3DTEXOPCAPS_MODULATEINVCOLOR_ADDALPHA)
6654 LogRel((" - D3DTEXOPCAPS_MODULATEINVCOLOR_ADDALPHA\n"));
6655 if (pCaps->TextureOpCaps & D3DTEXOPCAPS_BUMPENVMAP)
6656 LogRel((" - D3DTEXOPCAPS_BUMPENVMAP\n"));
6657 if (pCaps->TextureOpCaps & D3DTEXOPCAPS_BUMPENVMAPLUMINANCE)
6658 LogRel((" - D3DTEXOPCAPS_BUMPENVMAPLUMINANCE\n"));
6659 if (pCaps->TextureOpCaps & D3DTEXOPCAPS_DOTPRODUCT3)
6660 LogRel((" - D3DTEXOPCAPS_DOTPRODUCT3\n"));
6661 if (pCaps->TextureOpCaps & D3DTEXOPCAPS_MULTIPLYADD)
6662 LogRel((" - D3DTEXOPCAPS_MULTIPLYADD\n"));
6663 if (pCaps->TextureOpCaps & D3DTEXOPCAPS_LERP)
6664 LogRel((" - D3DTEXOPCAPS_LERP\n"));
6665
6666
6667 LogRel(("\n"));
6668 LogRel(("PixelShaderVersion: %#x (%u.%u)\n", pCaps->PixelShaderVersion,
6669 D3DSHADER_VERSION_MAJOR(pCaps->PixelShaderVersion), D3DSHADER_VERSION_MINOR(pCaps->PixelShaderVersion)));
6670 LogRel(("VertexShaderVersion: %#x (%u.%u)\n", pCaps->VertexShaderVersion,
6671 D3DSHADER_VERSION_MAJOR(pCaps->VertexShaderVersion), D3DSHADER_VERSION_MINOR(pCaps->VertexShaderVersion)));
6672
6673 LogRel(("\n"));
6674 RTLogRelSetBuffering(fBufferingSaved);
6675}
6676
Note: See TracBrowser for help on using the repository browser.

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette