VirtualBox

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

Last change on this file since 74201 was 73736, checked in by vboxsync, 6 years ago

DevVGA-SVGA: simplified creation of per context device windows; implement SVGA_3D_CMD_PRESENT using SVGA_3D_CMD_BLIT_SURFACE_TO_SCREEN.

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

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