VirtualBox

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

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

Devices/Graphics: VMSVGA: D3D backend fixes.

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

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