VirtualBox

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

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

bugref:8863: several DevVGA-SVGA fixes

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