VirtualBox

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

Last change on this file since 73214 was 73195, checked in by vboxsync, 6 years ago

DevVGA: debug build fix

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