VirtualBox

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

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

DevVGA-SVGA3d-win.cpp: fixed swapped src and dst in vmsvga3dSurfaceBlitToScreen, cleanup.

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

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