VirtualBox

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

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

Devices/Graphics: log the shader float constants as floats

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

© 2023 Oracle
ContactPrivacy policyTerms of Use