VirtualBox

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

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

Devices/Graphics: VMSVGA: info vmsvga3dsurf debugger command.

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

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