VirtualBox

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

Last change on this file since 63594 was 63562, checked in by vboxsync, 8 years ago

scm: cleaning up todos

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