VirtualBox

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

Last change on this file since 78381 was 76553, checked in by vboxsync, 6 years ago

scm --update-copyright-year

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