VirtualBox

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

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

SVGA3d/win: Fixed the scrolling issues with the SVGA_3D_CMD_PRESENT implementation (much simpler than OpenGL).

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