VirtualBox

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

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

DevVGA-SVGA3d-win.cpp: Sync vmsvga3dSurfaceGetSharedCopy with vmsvga3dBackCreateTexture; create bounce texture for D3DFMT_D24S8 and D3DFMT_D24X8; use helper to get DepthStencil render target surface.

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