VirtualBox

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

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

Devices/Graphics: VMSVGA vmsvga3dDrawPrimitives should take into account vertex stride == 0.

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