VirtualBox

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

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

Devices/Graphics: VMSVGA: sync the just created shared resource in D3D backend.

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