VirtualBox

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

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

Devices/Graphics: VMSVGA: Initial support for volume textures. Remember the type of created D3D resource.

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

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