VirtualBox

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

Last change on this file since 57444 was 57358, checked in by vboxsync, 9 years ago

*: scm cleanup run.

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

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