VirtualBox

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

Last change on this file since 82090 was 82090, checked in by vboxsync, 5 years ago

Devices/Graphics: use helpers to resolve surface and context ids; log the ids as decimal values.

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