VirtualBox

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

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

VMSVGA: Ignore invalid SVGA_3D_CMD_SETLIGHTENABLED, bugref:9583

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