VirtualBox

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

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

Devices/Graphics: VMSVGA: do not recreate D3D vertex declaration if it has not changed.

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

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette