VirtualBox

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

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

build fix

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 231.3 KB
Line 
1/* $Id: DevVGA-SVGA3d-win.cpp 57520 2015-08-24 23:44:44Z 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
2362 if (pSurface->flags & SVGA3D_SURFACE_HINT_TEXTURE)
2363 pSurfaceD3D->Release();
2364
2365 AssertMsgReturn(hr == D3D_OK, ("vmsvga3dCommandPresent: StretchRect failed with %x\n", hr), VERR_INTERNAL_ERROR);
2366
2367 hr = pContext->pDevice->Present(NULL, NULL, NULL, NULL);
2368 AssertMsgReturn(hr == D3D_OK, ("vmsvga3dCommandPresent: Present failed with %x\n", hr), VERR_INTERNAL_ERROR);
2369
2370 pBackBuffer->Release();
2371 return VINF_SUCCESS;
2372}
2373
2374
2375/**
2376 * Create a new 3d context
2377 *
2378 * @returns VBox status code.
2379 * @param pThis VGA device instance data.
2380 * @param cid Context id
2381 */
2382int vmsvga3dContextDefine(PVGASTATE pThis, uint32_t cid)
2383{
2384 int rc;
2385 PVMSVGA3DCONTEXT pContext;
2386 HRESULT hr;
2387 D3DPRESENT_PARAMETERS PresParam;
2388 PVMSVGA3DSTATE pState = pThis->svga.p3dState;
2389
2390 AssertReturn(pState, VERR_NO_MEMORY);
2391 AssertReturn(cid < SVGA3D_MAX_CONTEXT_IDS, VERR_INVALID_PARAMETER);
2392
2393 Log(("vmsvga3dContextDefine id %x\n", cid));
2394
2395 if (cid >= pState->cContexts)
2396 {
2397 /* Grow the array. */
2398 uint32_t cNew = RT_ALIGN(cid + 15, 16);
2399 void *pvNew = RTMemRealloc(pState->papContexts, sizeof(pState->papContexts[0]) * cNew);
2400 AssertReturn(pvNew, VERR_NO_MEMORY);
2401 pState->papContexts = (PVMSVGA3DCONTEXT *)pvNew;
2402 while (pState->cContexts < cNew)
2403 {
2404 pContext = (PVMSVGA3DCONTEXT)RTMemAllocZ(sizeof(*pContext));
2405 AssertReturn(pContext, VERR_NO_MEMORY);
2406 pContext->id = SVGA3D_INVALID_ID;
2407 pState->papContexts[pState->cContexts++] = pContext;
2408 }
2409 }
2410 /* If one already exists with this id, then destroy it now. */
2411 if (pState->papContexts[cid]->id != SVGA3D_INVALID_ID)
2412 vmsvga3dContextDestroy(pThis, cid);
2413
2414 pContext = pState->papContexts[cid];
2415 memset(pContext, 0, sizeof(*pContext));
2416 pContext->id = cid;
2417 for (uint32_t i = 0; i< RT_ELEMENTS(pContext->aSidActiveTexture); i++)
2418 pContext->aSidActiveTexture[i] = SVGA3D_INVALID_ID;
2419 pContext->sidRenderTarget = SVGA3D_INVALID_ID;
2420 pContext->state.shidVertex = SVGA3D_INVALID_ID;
2421 pContext->state.shidPixel = SVGA3D_INVALID_ID;
2422
2423 for (uint32_t i = 0; i < RT_ELEMENTS(pContext->state.aRenderTargets); i++)
2424 pContext->state.aRenderTargets[i] = SVGA3D_INVALID_ID;
2425
2426 /* Create a context window. */
2427 CREATESTRUCT cs;
2428
2429 AssertReturn(pThis->svga.u64HostWindowId, VERR_INTERNAL_ERROR);
2430
2431 cs.lpCreateParams = NULL;
2432 cs.dwExStyle = WS_EX_NOACTIVATE | WS_EX_NOPARENTNOTIFY | WS_EX_TRANSPARENT;
2433#ifdef DEBUG_GFX_WINDOW
2434 cs.lpszName = (char *)RTMemAllocZ(256);
2435 RTStrPrintf((char *)cs.lpszName, 256, "Context %d OpenGL Window", cid);
2436#else
2437 cs.lpszName = NULL;
2438#endif
2439 cs.lpszClass = NULL;
2440#ifdef DEBUG_GFX_WINDOW
2441 cs.style = WS_CLIPSIBLINGS | WS_CLIPCHILDREN | WS_VISIBLE | WS_CAPTION;
2442#else
2443 cs.style = WS_CLIPSIBLINGS | WS_CLIPCHILDREN | WS_DISABLED | WS_CHILD | WS_VISIBLE;
2444#endif
2445 cs.x = 0;
2446 cs.y = 0;
2447 cs.cx = pThis->svga.uWidth;
2448 cs.cy = pThis->svga.uHeight;
2449 cs.hwndParent = (HWND)pThis->svga.u64HostWindowId;
2450 cs.hMenu = NULL;
2451 cs.hInstance = pState->hInstance;
2452
2453 rc = vmsvga3dSendThreadMessage(pState->pWindowThread, pState->WndRequestSem, WM_VMSVGA3D_CREATEWINDOW, (WPARAM)&pContext->hwnd, (LPARAM)&cs);
2454 AssertRCReturn(rc, rc);
2455
2456 /* Changed when the function returns. */
2457 PresParam.BackBufferWidth = 0;
2458 PresParam.BackBufferHeight = 0;
2459 PresParam.BackBufferFormat = D3DFMT_UNKNOWN;
2460 PresParam.BackBufferCount = 0;
2461
2462 PresParam.MultiSampleType = D3DMULTISAMPLE_NONE;
2463 PresParam.MultiSampleQuality = 0;
2464 PresParam.SwapEffect = D3DSWAPEFFECT_FLIP;
2465 PresParam.hDeviceWindow = pContext->hwnd;
2466 PresParam.Windowed = TRUE; /* @todo */
2467 PresParam.EnableAutoDepthStencil = FALSE;
2468 PresParam.AutoDepthStencilFormat = D3DFMT_UNKNOWN; /* not relevant */
2469 PresParam.Flags = 0;
2470 PresParam.FullScreen_RefreshRateInHz = 0; /* windowed -> 0 */
2471 /* @todo consider using D3DPRESENT_DONOTWAIT so we don't wait for the GPU during Present calls. */
2472 PresParam.PresentationInterval = D3DPRESENT_INTERVAL_IMMEDIATE;
2473
2474#ifdef VBOX_VMSVGA3D_WITH_WINE_OPENGL
2475 hr = pState->pD3D9->CreateDevice(D3DADAPTER_DEFAULT,
2476 D3DDEVTYPE_HAL,
2477 pContext->hwnd,
2478 D3DCREATE_MULTITHREADED | D3DCREATE_MIXED_VERTEXPROCESSING, //D3DCREATE_HARDWARE_VERTEXPROCESSING,
2479 &PresParam,
2480 &pContext->pDevice);
2481#else
2482 hr = pState->pD3D9->CreateDeviceEx(D3DADAPTER_DEFAULT,
2483 D3DDEVTYPE_HAL,
2484 pContext->hwnd,
2485 D3DCREATE_MULTITHREADED | D3DCREATE_MIXED_VERTEXPROCESSING, //D3DCREATE_HARDWARE_VERTEXPROCESSING,
2486 &PresParam,
2487 NULL,
2488 &pContext->pDevice);
2489#endif
2490 AssertMsgReturn(hr == D3D_OK, ("vmsvga3dContextDefine: CreateDevice failed with %x\n", hr), VERR_INTERNAL_ERROR);
2491
2492 Log(("vmsvga3dContextDefine: Backbuffer (%d,%d) count=%d format=%x\n", PresParam.BackBufferWidth, PresParam.BackBufferHeight, PresParam.BackBufferCount, PresParam.BackBufferFormat));
2493 return VINF_SUCCESS;
2494}
2495
2496/**
2497 * Destroy an existing 3d context
2498 *
2499 * @returns VBox status code.
2500 * @param pThis VGA device instance data.
2501 * @param cid Context id
2502 */
2503int vmsvga3dContextDestroy(PVGASTATE pThis, uint32_t cid)
2504{
2505 PVMSVGA3DSTATE pState = pThis->svga.p3dState;
2506 AssertReturn(pState, VERR_NO_MEMORY);
2507
2508 AssertReturn(cid < SVGA3D_MAX_CONTEXT_IDS, VERR_INVALID_PARAMETER);
2509
2510 if ( cid < pState->cContexts
2511 && pState->papContexts[cid]->id == cid)
2512 {
2513 PVMSVGA3DCONTEXT pContext = pState->papContexts[cid];
2514
2515 Log(("vmsvga3dContextDestroy id %x\n", cid));
2516
2517 /* Check for all surfaces that are associated with this context to remove all dependencies */
2518 for (uint32_t sid = 0; sid < pState->cSurfaces; sid++)
2519 {
2520 PVMSVGA3DSURFACE pSurface = pState->papSurfaces[sid];
2521 if ( pSurface->id == sid
2522 && pSurface->idAssociatedContext == cid)
2523 {
2524 int rc;
2525
2526 Log(("vmsvga3dContextDestroy: remove all dependencies for surface %x\n", sid));
2527
2528 uint32_t surfaceFlags = pSurface->flags;
2529 SVGA3dSurfaceFormat format = pSurface->format;
2530 SVGA3dSurfaceFace face[SVGA3D_MAX_SURFACE_FACES];
2531 uint32_t multisampleCount = pSurface->multiSampleCount;
2532 SVGA3dTextureFilter autogenFilter = pSurface->autogenFilter;
2533 SVGA3dSize *pMipLevelSize;
2534 uint32_t cFaces = pSurface->cFaces;
2535
2536 pMipLevelSize = (SVGA3dSize *)RTMemAllocZ(pSurface->faces[0].numMipLevels * pSurface->cFaces * sizeof(SVGA3dSize));
2537 AssertReturn(pMipLevelSize, VERR_NO_MEMORY);
2538
2539 for (uint32_t face=0; face < pSurface->cFaces; face++)
2540 {
2541 for (uint32_t i = 0; i < pSurface->faces[0].numMipLevels; i++)
2542 {
2543 uint32_t idx = i + face * pSurface->faces[0].numMipLevels;
2544 memcpy(&pMipLevelSize[idx], &pSurface->pMipmapLevels[idx].size, sizeof(SVGA3dSize));
2545 }
2546 }
2547 memcpy(face, pSurface->faces, sizeof(pSurface->faces));
2548
2549 /* Recreate the surface with the original settings; destroys the contents, but that seems fairly safe since the context is also destroyed. */
2550 /** @todo not safe with shared objects */
2551 Assert(pSurface->pSharedObjectTree == NULL);
2552
2553 rc = vmsvga3dSurfaceDestroy(pThis, sid);
2554 AssertRC(rc);
2555
2556 rc = vmsvga3dSurfaceDefine(pThis, sid, surfaceFlags, format, face, multisampleCount, autogenFilter, face[0].numMipLevels * cFaces, pMipLevelSize);
2557 AssertRC(rc);
2558
2559 Assert(!pSurface->u.pSurface);
2560 }
2561 else
2562 {
2563 /* Check for a shared surface object. */
2564 PVMSVGA3DSHAREDSURFACE pSharedSurface = (PVMSVGA3DSHAREDSURFACE)RTAvlU32Get(&pSurface->pSharedObjectTree, cid);
2565 if (pSharedSurface)
2566 {
2567 Log(("vmsvga3dContextDestroy: remove shared dependency for surface %x\n", sid));
2568
2569 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))
2570 {
2571 case SVGA3D_SURFACE_HINT_TEXTURE:
2572 case SVGA3D_SURFACE_HINT_TEXTURE | SVGA3D_SURFACE_HINT_RENDERTARGET:
2573 Assert(pSharedSurface->u.pTexture);
2574 if (pSharedSurface->u.pTexture)
2575 pSharedSurface->u.pTexture->Release();
2576 break;
2577
2578 default:
2579 AssertFailed();
2580 break;
2581 }
2582 RTAvlU32Remove(&pSurface->pSharedObjectTree, cid);
2583 RTMemFree(pSharedSurface);
2584 }
2585 }
2586 }
2587
2588 /* Destroy all leftover pixel shaders. */
2589 for (uint32_t i = 0; i < pContext->cPixelShaders; i++)
2590 {
2591 if (pContext->paPixelShader[i].id != SVGA3D_INVALID_ID)
2592 vmsvga3dShaderDestroy(pThis, pContext->paPixelShader[i].cid, pContext->paPixelShader[i].id, pContext->paPixelShader[i].type);
2593 }
2594 if (pContext->paPixelShader)
2595 RTMemFree(pContext->paPixelShader);
2596
2597 /* Destroy all leftover vertex shaders. */
2598 for (uint32_t i = 0; i < pContext->cVertexShaders; i++)
2599 {
2600 if (pContext->paVertexShader[i].id != SVGA3D_INVALID_ID)
2601 vmsvga3dShaderDestroy(pThis, pContext->paVertexShader[i].cid, pContext->paVertexShader[i].id, pContext->paVertexShader[i].type);
2602 }
2603 if (pContext->paVertexShader)
2604 RTMemFree(pContext->paVertexShader);
2605
2606 if (pContext->state.paVertexShaderConst)
2607 RTMemFree(pContext->state.paVertexShaderConst);
2608 if (pContext->state.paPixelShaderConst)
2609 RTMemFree(pContext->state.paPixelShaderConst);
2610
2611 /* Release the D3D device object */
2612 pContext->pDevice->Release();
2613
2614 /* Destroy the window we've created. */
2615 int rc = vmsvga3dSendThreadMessage(pState->pWindowThread, pState->WndRequestSem, WM_VMSVGA3D_DESTROYWINDOW, (WPARAM)pContext->hwnd, 0);
2616 AssertRC(rc);
2617
2618 memset(pContext, 0, sizeof(*pContext));
2619 pContext->id = SVGA3D_INVALID_ID;
2620 }
2621 else
2622 AssertFailed();
2623
2624 return VINF_SUCCESS;
2625}
2626
2627static int vmsvga3dContextTrackUsage(PVGASTATE pThis, PVMSVGA3DCONTEXT pContext)
2628{
2629#ifndef VBOX_VMSVGA3D_WITH_WINE_OPENGL
2630 PVMSVGA3DSTATE pState = pThis->svga.p3dState;
2631 AssertReturn(pState, VERR_NO_MEMORY);
2632
2633 /* Inject fences to make sure we can track surface usage in case the client wants to reuse it in another context. */
2634 for (uint32_t i = 0; i < RT_ELEMENTS(pContext->aSidActiveTexture); i++)
2635 {
2636 if (pContext->aSidActiveTexture[i] != SVGA3D_INVALID_ID)
2637 vmsvga3dSurfaceTrackUsageById(pState, pContext, pContext->aSidActiveTexture[i]);
2638 }
2639 if (pContext->sidRenderTarget != SVGA3D_INVALID_ID)
2640 vmsvga3dSurfaceTrackUsageById(pState, pContext, pContext->sidRenderTarget);
2641#endif
2642 return VINF_SUCCESS;
2643}
2644
2645/* Handle resize */
2646int vmsvga3dChangeMode(PVGASTATE pThis)
2647{
2648 PVMSVGA3DSTATE pState = pThis->svga.p3dState;
2649 AssertReturn(pState, VERR_NO_MEMORY);
2650
2651 /* Resize all active contexts. */
2652 for (uint32_t i = 0; i < pState->cContexts; i++)
2653 {
2654 PVMSVGA3DCONTEXT pContext = pState->papContexts[i];
2655 uint32_t cid = pContext->id;
2656
2657 if (cid != SVGA3D_INVALID_ID)
2658 {
2659 CREATESTRUCT cs;
2660 D3DPRESENT_PARAMETERS PresParam;
2661 D3DVIEWPORT9 viewportOrg;
2662 HRESULT hr;
2663
2664#ifdef VMSVGA3D_DIRECT3D9_RESET
2665 /* Sync back all surface data as everything is lost after the Reset. */
2666 for (uint32_t sid = 0; sid < pState->cSurfaces; sid++)
2667 {
2668 PVMSVGA3DSURFACE pSurface = pState->papSurfaces[sid];
2669 if ( pSurface->id == sid
2670 && pSurface->idAssociatedContext == cid
2671 && pSurface->u.pSurface)
2672 {
2673 Log(("vmsvga3dChangeMode: sync back data of surface sid=%x (fDirty=%d)\n", sid, pSurface->fDirty));
2674
2675 /* Reallocate our surface memory buffers. */
2676 for (uint32_t i = 0; i < pSurface->cMipLevels; i++)
2677 {
2678 PVMSVGA3DMIPMAPLEVEL pMipmapLevel = &pSurface->pMipmapLevels[i];
2679
2680 pMipmapLevel->pSurfaceData = RTMemAllocZ(pMipmapLevel->cbSurface);
2681 AssertReturn(pMipmapLevel->pSurfaceData, VERR_NO_MEMORY);
2682
2683 if (!pSurface->fDirty)
2684 {
2685 D3DLOCKED_RECT LockedRect;
2686
2687 if (pSurface->bounce.pTexture)
2688 {
2689 IDirect3DSurface9 *pSrc, *pDest;
2690
2691 /* @todo only sync when something was actually rendered (since the last sync) */
2692 Log(("vmsvga3dChangeMode: sync bounce buffer (level %d)\n", i));
2693 hr = pSurface->bounce.pTexture->GetSurfaceLevel(i, &pDest);
2694 AssertMsgReturn(hr == D3D_OK, ("vmsvga3dChangeMode: GetSurfaceLevel failed with %x\n", hr), VERR_INTERNAL_ERROR);
2695
2696 hr = pSurface->u.pTexture->GetSurfaceLevel(i, &pSrc);
2697 AssertMsgReturn(hr == D3D_OK, ("vmsvga3dChangeMode: GetSurfaceLevel failed with %x\n", hr), VERR_INTERNAL_ERROR);
2698
2699 hr = pContext->pDevice->GetRenderTargetData(pSrc, pDest);
2700 AssertMsgReturn(hr == D3D_OK, ("vmsvga3dChangeMode: GetRenderTargetData failed with %x\n", hr), VERR_INTERNAL_ERROR);
2701
2702 pSrc->Release();
2703 pDest->Release();
2704
2705 hr = pSurface->bounce.pTexture->LockRect(i,
2706 &LockedRect,
2707 NULL,
2708 D3DLOCK_READONLY);
2709 }
2710 else
2711 hr = pSurface->u.pTexture->LockRect(i,
2712 &LockedRect,
2713 NULL,
2714 D3DLOCK_READONLY);
2715 AssertMsgReturn(hr == D3D_OK, ("vmsvga3dChangeMode: LockRect failed with %x\n", hr), VERR_INTERNAL_ERROR);
2716
2717 /* Copy the data one line at a time in case the internal pitch is different. */
2718 for (uint32_t j = 0; j < pMipmapLevel->size.height; j++)
2719 {
2720 memcpy((uint8_t *)pMipmapLevel->pSurfaceData + j * pMipmapLevel->cbSurfacePitch, (uint8_t *)LockedRect.pBits + j * LockedRect.Pitch, pMipmapLevel->cbSurfacePitch);
2721 }
2722
2723 if (pSurface->bounce.pTexture)
2724 hr = pSurface->bounce.pTexture->UnlockRect(i);
2725 else
2726 hr = pSurface->u.pTexture->UnlockRect(i);
2727 AssertMsgReturn(hr == D3D_OK, ("vmsvga3dChangeMode: UnlockRect failed with %x\n", hr), VERR_INTERNAL_ERROR);
2728 }
2729 }
2730
2731
2732 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))
2733 {
2734 case SVGA3D_SURFACE_CUBEMAP:
2735 AssertFailed(); /* @todo */
2736 break;
2737
2738 case SVGA3D_SURFACE_HINT_INDEXBUFFER:
2739 pSurface->u.pIndexBuffer->Release();
2740 break;
2741
2742 case SVGA3D_SURFACE_HINT_VERTEXBUFFER:
2743 pSurface->u.pVertexBuffer->Release();
2744 pSurface->u.pVertexBuffer = NULL;
2745 break;
2746
2747 case SVGA3D_SURFACE_HINT_TEXTURE:
2748 case SVGA3D_SURFACE_HINT_TEXTURE | SVGA3D_SURFACE_HINT_RENDERTARGET:
2749 pSurface->u.pTexture->Release();
2750 pSurface->u.pTexture = NULL;
2751 if (pSurface->bounce.pTexture)
2752 {
2753 pSurface->bounce.pTexture->Release();
2754 pSurface->bounce.pTexture = NULL;
2755 }
2756 break;
2757
2758 case SVGA3D_SURFACE_HINT_RENDERTARGET:
2759 case SVGA3D_SURFACE_HINT_DEPTHSTENCIL:
2760 if (pSurface->fStencilAsTexture)
2761 pSurface->u.pTexture->Release();
2762 else
2763 pSurface->u.pSurface->Release();
2764 pSurface->u.pSurface = NULL;
2765 break;
2766
2767 default:
2768 break;
2769 }
2770 RTAvlU32Destroy(&pSurface->pSharedObjectTree, vmsvga3dSharedSurfaceDestroyTree, pSurface);
2771 Assert(pSurface->pSharedObjectTree == NULL);
2772
2773 pSurface->idAssociatedContext = SVGA3D_INVALID_ID;
2774 pSurface->hSharedObject = 0;
2775 }
2776 }
2777#endif /* #ifdef VMSVGA3D_DIRECT3D9_RESET */
2778 memset(&cs, 0, sizeof(cs));
2779 cs.cx = pThis->svga.uWidth;
2780 cs.cy = pThis->svga.uHeight;
2781
2782 Log(("vmsvga3dChangeMode: Resize window %x of context %d to (%d,%d)\n", pContext->hwnd, pContext->id, cs.cx, cs.cy));
2783
2784 AssertReturn(pContext->pDevice, VERR_INTERNAL_ERROR);
2785 hr = pContext->pDevice->GetViewport(&viewportOrg);
2786 AssertMsgReturn(hr == D3D_OK, ("vmsvga3dChangeMode: GetViewport failed with %x\n", hr), VERR_INTERNAL_ERROR);
2787
2788 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)));
2789
2790 /* Resize the window. */
2791 int rc = vmsvga3dSendThreadMessage(pState->pWindowThread, pState->WndRequestSem, WM_VMSVGA3D_RESIZEWINDOW, (WPARAM)pContext->hwnd, (LPARAM)&cs);
2792 AssertRC(rc);
2793
2794 /* Changed when the function returns. */
2795 PresParam.BackBufferWidth = 0;
2796 PresParam.BackBufferHeight = 0;
2797 PresParam.BackBufferFormat = D3DFMT_UNKNOWN;
2798 PresParam.BackBufferCount = 0;
2799
2800 PresParam.MultiSampleType = D3DMULTISAMPLE_NONE;
2801 PresParam.MultiSampleQuality = 0;
2802 PresParam.SwapEffect = D3DSWAPEFFECT_FLIP;
2803 PresParam.hDeviceWindow = pContext->hwnd;
2804 PresParam.Windowed = TRUE; /* @todo */
2805 PresParam.EnableAutoDepthStencil = FALSE;
2806 PresParam.AutoDepthStencilFormat = D3DFMT_UNKNOWN; /* not relevant */
2807 PresParam.Flags = 0;
2808 PresParam.FullScreen_RefreshRateInHz = 0; /* windowed -> 0 */
2809 /* @todo consider using D3DPRESENT_DONOTWAIT so we don't wait for the GPU during Present calls. */
2810 PresParam.PresentationInterval = D3DPRESENT_INTERVAL_IMMEDIATE;;
2811
2812#ifdef VBOX_VMSVGA3D_WITH_WINE_OPENGL
2813 hr = pContext->pDevice->Reset(&PresParam);
2814 AssertMsgReturn(hr == D3D_OK, ("vmsvga3dChangeMode: Reset failed with %x\n", hr), VERR_INTERNAL_ERROR);
2815#else
2816 /* ResetEx does not trash the device state */
2817 hr = pContext->pDevice->ResetEx(&PresParam, NULL);
2818 AssertMsgReturn(hr == D3D_OK, ("vmsvga3dChangeMode: Reset failed with %x\n", hr), VERR_INTERNAL_ERROR);
2819#endif
2820 Log(("vmsvga3dChangeMode: Backbuffer (%d,%d) count=%d format=%x\n", PresParam.BackBufferWidth, PresParam.BackBufferHeight, PresParam.BackBufferCount, PresParam.BackBufferFormat));
2821
2822 /* ResetEx changes the viewport; restore it again. */
2823 hr = pContext->pDevice->SetViewport(&viewportOrg);
2824 AssertMsgReturn(hr == D3D_OK, ("vmsvga3dChangeMode: SetViewport failed with %x\n", hr), VERR_INTERNAL_ERROR);
2825
2826#ifdef LOG_ENABLED
2827 {
2828 D3DVIEWPORT9 viewport;
2829 hr = pContext->pDevice->GetViewport(&viewport);
2830 AssertMsgReturn(hr == D3D_OK, ("vmsvga3dChangeMode: GetViewport failed with %x\n", hr), VERR_INTERNAL_ERROR);
2831
2832 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)));
2833 }
2834#endif
2835
2836 /* First set the render targets as they change the internal state (reset viewport etc) */
2837 Log(("vmsvga3dChangeMode: Recreate render targets BEGIN\n"));
2838 for (uint32_t j = 0; j < RT_ELEMENTS(pContext->state.aRenderTargets); j++)
2839 {
2840 if (pContext->state.aRenderTargets[j] != SVGA3D_INVALID_ID)
2841 {
2842 SVGA3dSurfaceImageId target;
2843
2844 target.sid = pContext->state.aRenderTargets[j];
2845 target.face = 0;
2846 target.mipmap = 0;
2847 rc = vmsvga3dSetRenderTarget(pThis, cid, (SVGA3dRenderTargetType)j, target);
2848 AssertRCReturn(rc, rc);
2849 }
2850 }
2851
2852#ifdef VMSVGA3D_DIRECT3D9_RESET
2853 /* Recreate the render state */
2854 Log(("vmsvga3dChangeMode: Recreate render state BEGIN\n"));
2855 for (uint32_t i = 0; i < RT_ELEMENTS(pContext->state.aRenderState); i++)
2856 {
2857 SVGA3dRenderState *pRenderState = &pContext->state.aRenderState[i];
2858
2859 if (pRenderState->state != SVGA3D_RS_INVALID)
2860 vmsvga3dSetRenderState(pThis, pContext->id, 1, pRenderState);
2861 }
2862 Log(("vmsvga3dChangeMode: Recreate render state END\n"));
2863
2864 /* Recreate the texture state */
2865 Log(("vmsvga3dChangeMode: Recreate texture state BEGIN\n"));
2866 for (uint32_t iStage = 0; iStage < SVGA3D_MAX_TEXTURE_STAGE; iStage++)
2867 {
2868 for (uint32_t j = 0; j < SVGA3D_TS_MAX; j++)
2869 {
2870 SVGA3dTextureState *pTextureState = &pContext->state.aTextureState[iStage][j];
2871
2872 if (pTextureState->name != SVGA3D_RS_INVALID)
2873 vmsvga3dSetTextureState(pThis, pContext->id, 1, pTextureState);
2874 }
2875 }
2876 Log(("vmsvga3dChangeMode: Recreate texture state END\n"));
2877
2878 if (pContext->state.u32UpdateFlags & VMSVGA3D_UPDATE_SCISSORRECT)
2879 vmsvga3dSetScissorRect(pThis, cid, &pContext->state.RectScissor);
2880 if (pContext->state.u32UpdateFlags & VMSVGA3D_UPDATE_ZRANGE)
2881 vmsvga3dSetZRange(pThis, cid, pContext->state.zRange);
2882 if (pContext->state.u32UpdateFlags & VMSVGA3D_UPDATE_VIEWPORT)
2883 vmsvga3dSetViewPort(pThis, cid, &pContext->state.RectViewPort);
2884 if (pContext->state.u32UpdateFlags & VMSVGA3D_UPDATE_VERTEXSHADER)
2885 vmsvga3dShaderSet(pThis, pContext, cid, SVGA3D_SHADERTYPE_VS, pContext->state.shidVertex);
2886 if (pContext->state.u32UpdateFlags & VMSVGA3D_UPDATE_PIXELSHADER)
2887 vmsvga3dShaderSet(pThis, pContext, cid, SVGA3D_SHADERTYPE_PS, pContext->state.shidPixel);
2888 /** @todo restore more state data */
2889#endif /* #ifdef VMSVGA3D_DIRECT3D9_RESET */
2890 }
2891 }
2892 return VINF_SUCCESS;
2893}
2894
2895
2896int vmsvga3dSetTransform(PVGASTATE pThis, uint32_t cid, SVGA3dTransformType type, float matrix[16])
2897{
2898 D3DTRANSFORMSTATETYPE d3dState;
2899 HRESULT hr;
2900 PVMSVGA3DCONTEXT pContext;
2901 PVMSVGA3DSTATE pState = pThis->svga.p3dState;
2902 AssertReturn(pState, VERR_NO_MEMORY);
2903
2904 Log(("vmsvga3dSetTransform %x %s\n", cid, vmsvgaTransformToString(type)));
2905
2906 if ( cid >= pState->cContexts
2907 || pState->papContexts[cid]->id != cid)
2908 {
2909 Log(("vmsvga3dSetTransform invalid context id!\n"));
2910 return VERR_INVALID_PARAMETER;
2911 }
2912 pContext = pState->papContexts[cid];
2913
2914 switch (type)
2915 {
2916 case SVGA3D_TRANSFORM_VIEW:
2917 d3dState = D3DTS_VIEW;
2918 break;
2919 case SVGA3D_TRANSFORM_PROJECTION:
2920 d3dState = D3DTS_PROJECTION;
2921 break;
2922 case SVGA3D_TRANSFORM_TEXTURE0:
2923 d3dState = D3DTS_TEXTURE0;
2924 break;
2925 case SVGA3D_TRANSFORM_TEXTURE1:
2926 d3dState = D3DTS_TEXTURE1;
2927 break;
2928 case SVGA3D_TRANSFORM_TEXTURE2:
2929 d3dState = D3DTS_TEXTURE2;
2930 break;
2931 case SVGA3D_TRANSFORM_TEXTURE3:
2932 d3dState = D3DTS_TEXTURE3;
2933 break;
2934 case SVGA3D_TRANSFORM_TEXTURE4:
2935 d3dState = D3DTS_TEXTURE4;
2936 break;
2937 case SVGA3D_TRANSFORM_TEXTURE5:
2938 d3dState = D3DTS_TEXTURE5;
2939 break;
2940 case SVGA3D_TRANSFORM_TEXTURE6:
2941 d3dState = D3DTS_TEXTURE6;
2942 break;
2943 case SVGA3D_TRANSFORM_TEXTURE7:
2944 d3dState = D3DTS_TEXTURE7;
2945 break;
2946 case SVGA3D_TRANSFORM_WORLD:
2947 d3dState = D3DTS_WORLD;
2948 break;
2949 case SVGA3D_TRANSFORM_WORLD1:
2950 d3dState = D3DTS_WORLD1;
2951 break;
2952 case SVGA3D_TRANSFORM_WORLD2:
2953 d3dState = D3DTS_WORLD2;
2954 break;
2955 case SVGA3D_TRANSFORM_WORLD3:
2956 d3dState = D3DTS_WORLD3;
2957 break;
2958
2959 default:
2960 Log(("vmsvga3dSetTransform: unknown type!!\n"));
2961 return VERR_INVALID_PARAMETER;
2962 }
2963
2964 /* Save this matrix for vm state save/restore. */
2965 pContext->state.aTransformState[type].fValid = true;
2966 memcpy(pContext->state.aTransformState[type].matrix, matrix, sizeof(pContext->state.aTransformState[type].matrix));
2967 pContext->state.u32UpdateFlags |= VMSVGA3D_UPDATE_TRANSFORM;
2968
2969 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)));
2970 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)));
2971 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)));
2972 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)));
2973 hr = pContext->pDevice->SetTransform(d3dState, (const D3DMATRIX *)matrix);
2974 AssertMsgReturn(hr == D3D_OK, ("vmsvga3dSetTransform: SetTransform failed with %x\n", hr), VERR_INTERNAL_ERROR);
2975 return VINF_SUCCESS;
2976}
2977
2978int vmsvga3dSetZRange(PVGASTATE pThis, uint32_t cid, SVGA3dZRange zRange)
2979{
2980 D3DVIEWPORT9 viewport;
2981 HRESULT hr;
2982 PVMSVGA3DCONTEXT pContext;
2983 PVMSVGA3DSTATE pState = pThis->svga.p3dState;
2984 AssertReturn(pState, VERR_NO_MEMORY);
2985
2986 Log(("vmsvga3dSetZRange %x min=%d max=%d\n", cid, (uint32_t)(zRange.min * 100.0), (uint32_t)(zRange.max * 100.0)));
2987
2988 if ( cid >= pState->cContexts
2989 || pState->papContexts[cid]->id != cid)
2990 {
2991 Log(("vmsvga3dSetZRange invalid context id!\n"));
2992 return VERR_INVALID_PARAMETER;
2993 }
2994 pContext = pState->papContexts[cid];
2995 pContext->state.zRange = zRange;
2996 pContext->state.u32UpdateFlags |= VMSVGA3D_UPDATE_ZRANGE;
2997
2998 hr = pContext->pDevice->GetViewport(&viewport);
2999 AssertMsgReturn(hr == D3D_OK, ("vmsvga3dSetZRange: GetViewport failed with %x\n", hr), VERR_INTERNAL_ERROR);
3000
3001 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)));
3002 /* @todo convert the depth range from -1-1 to 0-1 although we shouldn't be getting such values in the first place... */
3003 if (zRange.min < 0.0)
3004 zRange.min = 0.0;
3005 if (zRange.max > 1.0)
3006 zRange.max = 1.0;
3007
3008 viewport.MinZ = zRange.min;
3009 viewport.MaxZ = zRange.max;
3010 hr = pContext->pDevice->SetViewport(&viewport);
3011 AssertMsgReturn(hr == D3D_OK, ("vmsvga3dSetZRange: SetViewport failed with %x\n", hr), VERR_INTERNAL_ERROR);
3012 return VINF_SUCCESS;
3013}
3014
3015/**
3016 * Convert SVGA blend op value to its D3D equivalent
3017 */
3018static DWORD vmsvga3dBlendOp2D3D(uint32_t blendOp, DWORD defaultBlendOp)
3019{
3020 switch (blendOp)
3021 {
3022 case SVGA3D_BLENDOP_ZERO:
3023 return D3DBLEND_ZERO;
3024 case SVGA3D_BLENDOP_ONE:
3025 return D3DBLEND_ONE;
3026 case SVGA3D_BLENDOP_SRCCOLOR:
3027 return D3DBLEND_SRCCOLOR;
3028 case SVGA3D_BLENDOP_INVSRCCOLOR:
3029 return D3DBLEND_INVSRCCOLOR;
3030 case SVGA3D_BLENDOP_SRCALPHA:
3031 return D3DBLEND_SRCALPHA;
3032 case SVGA3D_BLENDOP_INVSRCALPHA:
3033 return D3DBLEND_INVSRCALPHA;
3034 case SVGA3D_BLENDOP_DESTALPHA:
3035 return D3DBLEND_DESTALPHA;
3036 case SVGA3D_BLENDOP_INVDESTALPHA:
3037 return D3DBLEND_INVDESTALPHA;
3038 case SVGA3D_BLENDOP_DESTCOLOR:
3039 return D3DBLEND_DESTCOLOR;
3040 case SVGA3D_BLENDOP_INVDESTCOLOR:
3041 return D3DBLEND_INVDESTCOLOR;
3042 case SVGA3D_BLENDOP_SRCALPHASAT:
3043 return D3DBLEND_SRCALPHASAT;
3044 case SVGA3D_BLENDOP_BLENDFACTOR:
3045 return D3DBLEND_BLENDFACTOR;
3046 case SVGA3D_BLENDOP_INVBLENDFACTOR:
3047 return D3DBLEND_INVBLENDFACTOR;
3048 default:
3049 AssertFailed();
3050 return defaultBlendOp;
3051 }
3052}
3053
3054int vmsvga3dSetRenderState(PVGASTATE pThis, uint32_t cid, uint32_t cRenderStates, SVGA3dRenderState *pRenderState)
3055{
3056 DWORD val;
3057 HRESULT hr;
3058 PVMSVGA3DCONTEXT pContext;
3059 PVMSVGA3DSTATE pState = pThis->svga.p3dState;
3060 AssertReturn(pState, VERR_NO_MEMORY);
3061
3062 Log(("vmsvga3dSetRenderState cid=%x cRenderStates=%d\n", cid, cRenderStates));
3063
3064 if ( cid >= pState->cContexts
3065 || pState->papContexts[cid]->id != cid)
3066 {
3067 Log(("vmsvga3dSetRenderState invalid context id!\n"));
3068 return VERR_INVALID_PARAMETER;
3069 }
3070 pContext = pState->papContexts[cid];
3071
3072 for (unsigned i = 0; i < cRenderStates; i++)
3073 {
3074 D3DRENDERSTATETYPE renderState = D3DRS_FORCE_DWORD;
3075
3076 Log(("vmsvga3dSetRenderState: state=%s (%d) val=%x\n", vmsvga3dGetRenderStateName(pRenderState[i].state), pRenderState[i].state, pRenderState[i].uintValue));
3077 /* Save the render state for vm state saving. */
3078 if (pRenderState[i].state < SVGA3D_RS_MAX)
3079 pContext->state.aRenderState[pRenderState[i].state] = pRenderState[i];
3080
3081 switch (pRenderState[i].state)
3082 {
3083 case SVGA3D_RS_ZENABLE: /* SVGA3dBool */
3084 renderState = D3DRS_ZENABLE;
3085 val = pRenderState[i].uintValue;
3086 Assert(val == D3DZB_FALSE || val == D3DZB_TRUE);
3087 break;
3088
3089 case SVGA3D_RS_ZWRITEENABLE: /* SVGA3dBool */
3090 renderState = D3DRS_ZWRITEENABLE;
3091 val = pRenderState[i].uintValue;
3092 break;
3093
3094 case SVGA3D_RS_ALPHATESTENABLE: /* SVGA3dBool */
3095 renderState = D3DRS_ALPHATESTENABLE;
3096 val = pRenderState[i].uintValue;
3097 break;
3098
3099 case SVGA3D_RS_DITHERENABLE: /* SVGA3dBool */
3100 renderState = D3DRS_DITHERENABLE;
3101 val = pRenderState[i].uintValue;
3102 break;
3103
3104 case SVGA3D_RS_BLENDENABLE: /* SVGA3dBool */
3105 renderState = D3DRS_ALPHABLENDENABLE;
3106 val = pRenderState[i].uintValue;
3107 break;
3108
3109 case SVGA3D_RS_FOGENABLE: /* SVGA3dBool */
3110 renderState = D3DRS_FOGENABLE;
3111 val = pRenderState[i].uintValue;
3112 break;
3113
3114 case SVGA3D_RS_SPECULARENABLE: /* SVGA3dBool */
3115 renderState = D3DRS_SPECULARENABLE;
3116 val = pRenderState[i].uintValue;
3117 break;
3118
3119 case SVGA3D_RS_LIGHTINGENABLE: /* SVGA3dBool */
3120 renderState = D3DRS_LIGHTING;
3121 val = pRenderState[i].uintValue;
3122 break;
3123
3124 case SVGA3D_RS_NORMALIZENORMALS: /* SVGA3dBool */
3125 renderState = D3DRS_NORMALIZENORMALS;
3126 val = pRenderState[i].uintValue;
3127 break;
3128
3129 case SVGA3D_RS_POINTSPRITEENABLE: /* SVGA3dBool */
3130 renderState = D3DRS_POINTSPRITEENABLE;
3131 val = pRenderState[i].uintValue;
3132 break;
3133
3134 case SVGA3D_RS_POINTSCALEENABLE: /* SVGA3dBool */
3135 renderState = D3DRS_POINTSCALEENABLE;
3136 val = pRenderState[i].uintValue;
3137 break;
3138
3139 case SVGA3D_RS_POINTSIZE: /* float */
3140 renderState = D3DRS_POINTSIZE;
3141 val = pRenderState[i].uintValue;
3142 Log(("SVGA3D_RS_POINTSIZE: %d\n", (uint32_t) (pRenderState[i].floatValue * 100.0)));
3143 break;
3144
3145 case SVGA3D_RS_POINTSIZEMIN: /* float */
3146 renderState = D3DRS_POINTSIZE_MIN;
3147 val = pRenderState[i].uintValue;
3148 Log(("SVGA3D_RS_POINTSIZEMIN: %d\n", (uint32_t) (pRenderState[i].floatValue * 100.0)));
3149 break;
3150
3151 case SVGA3D_RS_POINTSIZEMAX: /* float */
3152 renderState = D3DRS_POINTSIZE_MAX;
3153 val = pRenderState[i].uintValue;
3154 Log(("SVGA3D_RS_POINTSIZEMAX: %d\n", (uint32_t) (pRenderState[i].floatValue * 100.0)));
3155 break;
3156
3157 case SVGA3D_RS_POINTSCALE_A: /* float */
3158 renderState = D3DRS_POINTSCALE_A;
3159 val = pRenderState[i].uintValue;
3160 break;
3161
3162 case SVGA3D_RS_POINTSCALE_B: /* float */
3163 renderState = D3DRS_POINTSCALE_B;
3164 val = pRenderState[i].uintValue;
3165 break;
3166
3167 case SVGA3D_RS_POINTSCALE_C: /* float */
3168 renderState = D3DRS_POINTSCALE_C;
3169 val = pRenderState[i].uintValue;
3170 break;
3171
3172 case SVGA3D_RS_AMBIENT: /* SVGA3dColor - identical */
3173 renderState = D3DRS_AMBIENT;
3174 val = pRenderState[i].uintValue;
3175 break;
3176
3177 case SVGA3D_RS_CLIPPLANEENABLE: /* SVGA3dClipPlanes - identical */
3178 renderState = D3DRS_CLIPPLANEENABLE;
3179 val = pRenderState[i].uintValue;
3180 break;
3181
3182 case SVGA3D_RS_FOGCOLOR: /* SVGA3dColor - identical */
3183 renderState = D3DRS_FOGCOLOR;
3184 val = pRenderState[i].uintValue;
3185 break;
3186
3187 case SVGA3D_RS_FOGSTART: /* float */
3188 renderState = D3DRS_FOGSTART;
3189 val = pRenderState[i].uintValue;
3190 break;
3191
3192 case SVGA3D_RS_FOGEND: /* float */
3193 renderState = D3DRS_FOGEND;
3194 val = pRenderState[i].uintValue;
3195 break;
3196
3197 case SVGA3D_RS_FOGDENSITY: /* float */
3198 renderState = D3DRS_FOGDENSITY;
3199 val = pRenderState[i].uintValue;
3200 break;
3201
3202 case SVGA3D_RS_RANGEFOGENABLE: /* SVGA3dBool */
3203 renderState = D3DRS_RANGEFOGENABLE;
3204 val = pRenderState[i].uintValue;
3205 break;
3206
3207 case SVGA3D_RS_FOGMODE: /* SVGA3dFogMode */
3208 {
3209 SVGA3dFogMode mode;
3210 mode.uintValue = pRenderState[i].uintValue;
3211
3212 switch (mode.s.function)
3213 {
3214 case SVGA3D_FOGFUNC_INVALID:
3215 val = D3DFOG_NONE;
3216 break;
3217 case SVGA3D_FOGFUNC_EXP:
3218 val = D3DFOG_EXP;
3219 break;
3220 case SVGA3D_FOGFUNC_EXP2:
3221 val = D3DFOG_EXP2;
3222 break;
3223 case SVGA3D_FOGFUNC_LINEAR:
3224 val = D3DFOG_LINEAR;
3225 break;
3226 case SVGA3D_FOGFUNC_PER_VERTEX: /* unable to find a d3d9 equivalent */
3227 AssertMsgFailedReturn(("Unsupported fog function SVGA3D_FOGFUNC_PER_VERTEX\n"), VERR_INTERNAL_ERROR);
3228 break;
3229 default:
3230 AssertMsgFailedReturn(("Unexpected fog function %d\n", mode.s.function), VERR_INTERNAL_ERROR);
3231 break;
3232 }
3233
3234 /* The fog type determines the render state. */
3235 switch (mode.s.type)
3236 {
3237 case SVGA3D_FOGTYPE_VERTEX:
3238 renderState = D3DRS_FOGVERTEXMODE;
3239 break;
3240 case SVGA3D_FOGTYPE_PIXEL:
3241 renderState = D3DRS_FOGTABLEMODE;
3242 break;
3243 default:
3244 AssertMsgFailedReturn(("Unexpected fog type %d\n", mode.s.type), VERR_INTERNAL_ERROR);
3245 break;
3246 }
3247
3248 /* Set the fog base to depth or range. */
3249 switch (mode.s.base)
3250 {
3251 case SVGA3D_FOGBASE_DEPTHBASED:
3252 hr = pContext->pDevice->SetRenderState(D3DRS_RANGEFOGENABLE, FALSE);
3253 AssertMsgReturn(hr == D3D_OK, ("vmsvga3dSetRenderState: SetRenderState SVGA3D_FOGBASE_DEPTHBASED failed with %x\n", hr), VERR_INTERNAL_ERROR);
3254 break;
3255 case SVGA3D_FOGBASE_RANGEBASED:
3256 hr = pContext->pDevice->SetRenderState(D3DRS_RANGEFOGENABLE, TRUE);
3257 AssertMsgReturn(hr == D3D_OK, ("vmsvga3dSetRenderState: SetRenderState SVGA3D_FOGBASE_RANGEBASED failed with %x\n", hr), VERR_INTERNAL_ERROR);
3258 break;
3259 default:
3260 /* ignore */
3261 AssertMsgFailed(("Unexpected fog base %d\n", mode.s.base));
3262 break;
3263 }
3264 break;
3265 }
3266
3267 case SVGA3D_RS_FILLMODE: /* SVGA3dFillMode */
3268 {
3269 SVGA3dFillMode mode;
3270
3271 mode.uintValue = pRenderState[i].uintValue;
3272
3273 switch (mode.s.mode)
3274 {
3275 case SVGA3D_FILLMODE_POINT:
3276 val = D3DFILL_POINT;
3277 break;
3278 case SVGA3D_FILLMODE_LINE:
3279 val = D3DFILL_WIREFRAME;
3280 break;
3281 case SVGA3D_FILLMODE_FILL:
3282 val = D3DFILL_SOLID;
3283 break;
3284 default:
3285 AssertMsgFailedReturn(("Unexpected fill mode %d\n", mode.s.mode), VERR_INTERNAL_ERROR);
3286 break;
3287 }
3288 /* @todo ignoring face for now. */
3289 renderState = D3DRS_FILLMODE;
3290 break;
3291 }
3292
3293 case SVGA3D_RS_SHADEMODE: /* SVGA3dShadeMode */
3294 renderState = D3DRS_SHADEMODE;
3295 AssertCompile(D3DSHADE_FLAT == SVGA3D_SHADEMODE_FLAT);
3296 val = pRenderState[i].uintValue; /* SVGA3dShadeMode == D3DSHADEMODE */
3297 break;
3298
3299 case SVGA3D_RS_LINEPATTERN: /* SVGA3dLinePattern */
3300 /* No longer supported by d3d; mesagl comments suggest not all backends support it */
3301 /* @todo */
3302 Log(("WARNING: SVGA3D_RS_LINEPATTERN %x not supported!!\n", pRenderState[i].uintValue));
3303 /*
3304 renderState = D3DRS_LINEPATTERN;
3305 val = pRenderState[i].uintValue;
3306 */
3307 break;
3308
3309 case SVGA3D_RS_SRCBLEND: /* SVGA3dBlendOp */
3310 renderState = D3DRS_SRCBLEND;
3311 val = vmsvga3dBlendOp2D3D(pRenderState[i].uintValue, D3DBLEND_ONE /* default */);
3312 break;
3313
3314 case SVGA3D_RS_DSTBLEND: /* SVGA3dBlendOp */
3315 renderState = D3DRS_DESTBLEND;
3316 val = vmsvga3dBlendOp2D3D(pRenderState[i].uintValue, D3DBLEND_ZERO /* default */);
3317 break;
3318
3319 case SVGA3D_RS_BLENDEQUATION: /* SVGA3dBlendEquation - identical */
3320 AssertCompile(SVGA3D_BLENDEQ_MAXIMUM == D3DBLENDOP_MAX);
3321 renderState = D3DRS_BLENDOP;
3322 val = pRenderState[i].uintValue;
3323 break;
3324
3325 case SVGA3D_RS_CULLMODE: /* SVGA3dFace */
3326 {
3327 switch (pRenderState[i].uintValue)
3328 {
3329 case SVGA3D_FACE_NONE:
3330 val = D3DCULL_NONE;
3331 break;
3332 case SVGA3D_FACE_FRONT:
3333 val = D3DCULL_CW;
3334 break;
3335 case SVGA3D_FACE_BACK:
3336 val = D3DCULL_CCW;
3337 break;
3338 case SVGA3D_FACE_FRONT_BACK:
3339 AssertFailed();
3340 val = D3DCULL_CW;
3341 break;
3342 default:
3343 AssertMsgFailedReturn(("Unexpected cull mode %d\n", pRenderState[i].uintValue), VERR_INTERNAL_ERROR);
3344 break;
3345 }
3346 renderState = D3DRS_CULLMODE;
3347 break;
3348 }
3349
3350 case SVGA3D_RS_ZFUNC: /* SVGA3dCmpFunc - identical */
3351 AssertCompile(SVGA3D_CMP_ALWAYS == D3DCMP_ALWAYS);
3352 renderState = D3DRS_ZFUNC;
3353 val = pRenderState[i].uintValue;
3354 break;
3355
3356 case SVGA3D_RS_ALPHAFUNC: /* SVGA3dCmpFunc - identical */
3357 renderState = D3DRS_ALPHAFUNC;
3358 val = pRenderState[i].uintValue;
3359 break;
3360
3361 case SVGA3D_RS_STENCILENABLE: /* SVGA3dBool */
3362 renderState = D3DRS_STENCILENABLE;
3363 val = pRenderState[i].uintValue;
3364 break;
3365
3366 case SVGA3D_RS_STENCILREF: /* uint32_t */
3367 renderState = D3DRS_STENCILREF;
3368 val = pRenderState[i].uintValue;
3369 break;
3370
3371 case SVGA3D_RS_STENCILMASK: /* uint32_t */
3372 renderState = D3DRS_STENCILMASK;
3373 val = pRenderState[i].uintValue;
3374 break;
3375
3376 case SVGA3D_RS_STENCILWRITEMASK: /* uint32_t */
3377 renderState = D3DRS_STENCILWRITEMASK;
3378 val = pRenderState[i].uintValue;
3379 break;
3380
3381 case SVGA3D_RS_STENCILFUNC: /* SVGA3dCmpFunc - identical */
3382 renderState = D3DRS_STENCILFUNC;
3383 val = pRenderState[i].uintValue;
3384 break;
3385
3386 case SVGA3D_RS_STENCILFAIL: /* SVGA3dStencilOp - identical */
3387 AssertCompile(D3DSTENCILOP_KEEP == SVGA3D_STENCILOP_KEEP);
3388 AssertCompile(D3DSTENCILOP_DECR == SVGA3D_STENCILOP_DECR);
3389 renderState = D3DRS_STENCILFAIL;
3390 val = pRenderState[i].uintValue;
3391 break;
3392
3393 case SVGA3D_RS_STENCILZFAIL: /* SVGA3dStencilOp - identical */
3394 renderState = D3DRS_STENCILZFAIL;
3395 val = pRenderState[i].uintValue;
3396 break;
3397
3398 case SVGA3D_RS_STENCILPASS: /* SVGA3dStencilOp - identical */
3399 renderState = D3DRS_STENCILPASS;
3400 val = pRenderState[i].uintValue;
3401 break;
3402
3403 case SVGA3D_RS_ALPHAREF: /* float (0.0 .. 1.0) */
3404 renderState = D3DRS_ALPHAREF;
3405 val = pRenderState[i].uintValue;
3406 break;
3407
3408 case SVGA3D_RS_FRONTWINDING: /* SVGA3dFrontWinding */
3409 Assert(pRenderState[i].uintValue == SVGA3D_FRONTWINDING_CW);
3410 /*
3411 renderState = D3DRS_FRONTWINDING; //D3DRS_TWOSIDEDSTENCILMODE
3412 val = pRenderState[i].uintValue;
3413 */
3414 break;
3415
3416 case SVGA3D_RS_COORDINATETYPE: /* SVGA3dCoordinateType */
3417 Assert(pRenderState[i].uintValue == SVGA3D_COORDINATE_LEFTHANDED);
3418 /* @todo setup a view matrix to scale the world space by -1 in the z-direction for right handed coordinates. */
3419 /*
3420 renderState = D3DRS_COORDINATETYPE;
3421 val = pRenderState[i].uintValue;
3422 */
3423 break;
3424
3425 case SVGA3D_RS_ZBIAS: /* float */
3426 /* @todo unknown meaning; depth bias is not identical
3427 renderState = D3DRS_DEPTHBIAS;
3428 val = pRenderState[i].uintValue;
3429 */
3430 Log(("vmsvga3dSetRenderState: WARNING unsupported SVGA3D_RS_ZBIAS\n"));
3431 break;
3432
3433 case SVGA3D_RS_SLOPESCALEDEPTHBIAS: /* float */
3434 renderState = D3DRS_SLOPESCALEDEPTHBIAS;
3435 val = pRenderState[i].uintValue;
3436 break;
3437
3438 case SVGA3D_RS_DEPTHBIAS: /* float */
3439 renderState = D3DRS_DEPTHBIAS;
3440 val = pRenderState[i].uintValue;
3441 break;
3442
3443 case SVGA3D_RS_COLORWRITEENABLE: /* SVGA3dColorMask - identical to D3DCOLORWRITEENABLE_* */
3444 renderState = D3DRS_COLORWRITEENABLE;
3445 val = pRenderState[i].uintValue;
3446 break;
3447
3448 case SVGA3D_RS_VERTEXMATERIALENABLE: /* SVGA3dBool */
3449 //AssertFailed();
3450 renderState = D3DRS_INDEXEDVERTEXBLENDENABLE; /* correct?? */
3451 val = pRenderState[i].uintValue;
3452 break;
3453
3454 case SVGA3D_RS_DIFFUSEMATERIALSOURCE: /* SVGA3dVertexMaterial - identical */
3455 AssertCompile(D3DMCS_COLOR2 == SVGA3D_VERTEXMATERIAL_SPECULAR);
3456 renderState = D3DRS_DIFFUSEMATERIALSOURCE;
3457 val = pRenderState[i].uintValue;
3458 break;
3459
3460 case SVGA3D_RS_SPECULARMATERIALSOURCE: /* SVGA3dVertexMaterial - identical */
3461 renderState = D3DRS_SPECULARMATERIALSOURCE;
3462 val = pRenderState[i].uintValue;
3463 break;
3464
3465 case SVGA3D_RS_AMBIENTMATERIALSOURCE: /* SVGA3dVertexMaterial - identical */
3466 renderState = D3DRS_AMBIENTMATERIALSOURCE;
3467 val = pRenderState[i].uintValue;
3468 break;
3469
3470 case SVGA3D_RS_EMISSIVEMATERIALSOURCE: /* SVGA3dVertexMaterial - identical */
3471 renderState = D3DRS_EMISSIVEMATERIALSOURCE;
3472 val = pRenderState[i].uintValue;
3473 break;
3474
3475 case SVGA3D_RS_TEXTUREFACTOR: /* SVGA3dColor - identical */
3476 renderState = D3DRS_TEXTUREFACTOR;
3477 val = pRenderState[i].uintValue;
3478 break;
3479
3480 case SVGA3D_RS_LOCALVIEWER: /* SVGA3dBool */
3481 renderState = D3DRS_LOCALVIEWER;
3482 val = pRenderState[i].uintValue;
3483 break;
3484
3485 case SVGA3D_RS_SCISSORTESTENABLE: /* SVGA3dBool */
3486 renderState = D3DRS_SCISSORTESTENABLE;
3487 val = pRenderState[i].uintValue;
3488 break;
3489
3490 case SVGA3D_RS_BLENDCOLOR: /* SVGA3dColor - identical */
3491 renderState = D3DRS_BLENDFACTOR;
3492 val = pRenderState[i].uintValue;
3493 break;
3494
3495 case SVGA3D_RS_STENCILENABLE2SIDED: /* SVGA3dBool */
3496 renderState = D3DRS_TWOSIDEDSTENCILMODE;
3497 val = pRenderState[i].uintValue;
3498 break;
3499
3500 case SVGA3D_RS_CCWSTENCILFUNC: /* SVGA3dCmpFunc - identical */
3501 renderState = D3DRS_CCW_STENCILFUNC;
3502 val = pRenderState[i].uintValue;
3503 break;
3504
3505 case SVGA3D_RS_CCWSTENCILFAIL: /* SVGA3dStencilOp - identical */
3506 renderState = D3DRS_CCW_STENCILFAIL;
3507 val = pRenderState[i].uintValue;
3508 break;
3509
3510 case SVGA3D_RS_CCWSTENCILZFAIL: /* SVGA3dStencilOp - identical */
3511 renderState = D3DRS_CCW_STENCILZFAIL;
3512 val = pRenderState[i].uintValue;
3513 break;
3514
3515 case SVGA3D_RS_CCWSTENCILPASS: /* SVGA3dStencilOp - identical */
3516 renderState = D3DRS_CCW_STENCILPASS;
3517 val = pRenderState[i].uintValue;
3518 break;
3519
3520 case SVGA3D_RS_VERTEXBLEND: /* SVGA3dVertexBlendFlags - identical */
3521 AssertCompile(SVGA3D_VBLEND_DISABLE == D3DVBF_DISABLE);
3522 renderState = D3DRS_VERTEXBLEND;
3523 val = pRenderState[i].uintValue;
3524 break;
3525
3526 case SVGA3D_RS_OUTPUTGAMMA: /* float */
3527 //AssertFailed();
3528 /*
3529 D3DRS_SRGBWRITEENABLE ??
3530 renderState = D3DRS_OUTPUTGAMMA;
3531 val = pRenderState[i].uintValue;
3532 */
3533 break;
3534
3535 case SVGA3D_RS_ZVISIBLE: /* SVGA3dBool */
3536 AssertFailed();
3537 /*
3538 renderState = D3DRS_ZVISIBLE;
3539 val = pRenderState[i].uintValue;
3540 */
3541 break;
3542
3543 case SVGA3D_RS_LASTPIXEL: /* SVGA3dBool */
3544 renderState = D3DRS_LASTPIXEL;
3545 val = pRenderState[i].uintValue;
3546 break;
3547
3548 case SVGA3D_RS_CLIPPING: /* SVGA3dBool */
3549 renderState = D3DRS_CLIPPING;
3550 val = pRenderState[i].uintValue;
3551 break;
3552
3553 case SVGA3D_RS_WRAP0: /* SVGA3dWrapFlags - identical */
3554 Assert(SVGA3D_WRAPCOORD_3 == D3DWRAPCOORD_3);
3555 renderState = D3DRS_WRAP0;
3556 val = pRenderState[i].uintValue;
3557 break;
3558
3559 case SVGA3D_RS_WRAP1: /* SVGA3dWrapFlags - identical */
3560 renderState = D3DRS_WRAP1;
3561 val = pRenderState[i].uintValue;
3562 break;
3563
3564 case SVGA3D_RS_WRAP2: /* SVGA3dWrapFlags - identical */
3565 renderState = D3DRS_WRAP2;
3566 val = pRenderState[i].uintValue;
3567 break;
3568
3569 case SVGA3D_RS_WRAP3: /* SVGA3dWrapFlags - identical */
3570 renderState = D3DRS_WRAP3;
3571 val = pRenderState[i].uintValue;
3572 break;
3573
3574 case SVGA3D_RS_WRAP4: /* SVGA3dWrapFlags - identical */
3575 renderState = D3DRS_WRAP4;
3576 val = pRenderState[i].uintValue;
3577 break;
3578
3579 case SVGA3D_RS_WRAP5: /* SVGA3dWrapFlags - identical */
3580 renderState = D3DRS_WRAP5;
3581 val = pRenderState[i].uintValue;
3582 break;
3583
3584 case SVGA3D_RS_WRAP6: /* SVGA3dWrapFlags - identical */
3585 renderState = D3DRS_WRAP6;
3586 val = pRenderState[i].uintValue;
3587 break;
3588
3589 case SVGA3D_RS_WRAP7: /* SVGA3dWrapFlags - identical */
3590 renderState = D3DRS_WRAP7;
3591 val = pRenderState[i].uintValue;
3592 break;
3593
3594 case SVGA3D_RS_WRAP8: /* SVGA3dWrapFlags - identical */
3595 renderState = D3DRS_WRAP8;
3596 val = pRenderState[i].uintValue;
3597 break;
3598
3599 case SVGA3D_RS_WRAP9: /* SVGA3dWrapFlags - identical */
3600 renderState = D3DRS_WRAP9;
3601 val = pRenderState[i].uintValue;
3602 break;
3603
3604 case SVGA3D_RS_WRAP10: /* SVGA3dWrapFlags - identical */
3605 renderState = D3DRS_WRAP10;
3606 val = pRenderState[i].uintValue;
3607 break;
3608
3609 case SVGA3D_RS_WRAP11: /* SVGA3dWrapFlags - identical */
3610 renderState = D3DRS_WRAP11;
3611 val = pRenderState[i].uintValue;
3612 break;
3613
3614 case SVGA3D_RS_WRAP12: /* SVGA3dWrapFlags - identical */
3615 renderState = D3DRS_WRAP12;
3616 val = pRenderState[i].uintValue;
3617 break;
3618
3619 case SVGA3D_RS_WRAP13: /* SVGA3dWrapFlags - identical */
3620 renderState = D3DRS_WRAP13;
3621 val = pRenderState[i].uintValue;
3622 break;
3623
3624 case SVGA3D_RS_WRAP14: /* SVGA3dWrapFlags - identical */
3625 renderState = D3DRS_WRAP14;
3626 val = pRenderState[i].uintValue;
3627 break;
3628
3629 case SVGA3D_RS_WRAP15: /* SVGA3dWrapFlags - identical */
3630 renderState = D3DRS_WRAP15;
3631 val = pRenderState[i].uintValue;
3632 break;
3633
3634 case SVGA3D_RS_MULTISAMPLEANTIALIAS: /* SVGA3dBool */
3635 renderState = D3DRS_MULTISAMPLEANTIALIAS;
3636 val = pRenderState[i].uintValue;
3637 break;
3638
3639 case SVGA3D_RS_MULTISAMPLEMASK: /* uint32_t */
3640 renderState = D3DRS_MULTISAMPLEMASK;
3641 val = pRenderState[i].uintValue;
3642 break;
3643
3644 case SVGA3D_RS_INDEXEDVERTEXBLENDENABLE: /* SVGA3dBool */
3645 renderState = D3DRS_INDEXEDVERTEXBLENDENABLE;
3646 val = pRenderState[i].uintValue;
3647 break;
3648
3649 case SVGA3D_RS_TWEENFACTOR: /* float */
3650 renderState = D3DRS_TWEENFACTOR;
3651 val = pRenderState[i].uintValue;
3652 break;
3653
3654 case SVGA3D_RS_ANTIALIASEDLINEENABLE: /* SVGA3dBool */
3655 renderState = D3DRS_ANTIALIASEDLINEENABLE;
3656 val = pRenderState[i].uintValue;
3657 break;
3658
3659 case SVGA3D_RS_COLORWRITEENABLE1: /* SVGA3dColorMask - identical to D3DCOLORWRITEENABLE_* */
3660 renderState = D3DRS_COLORWRITEENABLE1;
3661 val = pRenderState[i].uintValue;
3662 break;
3663
3664 case SVGA3D_RS_COLORWRITEENABLE2: /* SVGA3dColorMask - identical to D3DCOLORWRITEENABLE_* */
3665 renderState = D3DRS_COLORWRITEENABLE2;
3666 val = pRenderState[i].uintValue;
3667 break;
3668
3669 case SVGA3D_RS_COLORWRITEENABLE3: /* SVGA3dColorMask - identical to D3DCOLORWRITEENABLE_* */
3670 renderState = D3DRS_COLORWRITEENABLE3;
3671 val = pRenderState[i].uintValue;
3672 break;
3673
3674 case SVGA3D_RS_SEPARATEALPHABLENDENABLE: /* SVGA3dBool */
3675 renderState = D3DRS_SEPARATEALPHABLENDENABLE;
3676 val = pRenderState[i].uintValue;
3677 break;
3678
3679 case SVGA3D_RS_SRCBLENDALPHA: /* SVGA3dBlendOp */
3680 renderState = D3DRS_SRCBLENDALPHA;
3681 val = vmsvga3dBlendOp2D3D(pRenderState[i].uintValue, D3DBLEND_ONE /* default */);
3682 break;
3683
3684 case SVGA3D_RS_DSTBLENDALPHA: /* SVGA3dBlendOp */
3685 renderState = D3DRS_DESTBLENDALPHA;
3686 val = vmsvga3dBlendOp2D3D(pRenderState[i].uintValue, D3DBLEND_ZERO /* default */);
3687 break;
3688
3689 case SVGA3D_RS_BLENDEQUATIONALPHA: /* SVGA3dBlendEquation - identical */
3690 renderState = D3DRS_BLENDOPALPHA;
3691 val = pRenderState[i].uintValue;
3692 break;
3693
3694 case SVGA3D_RS_TRANSPARENCYANTIALIAS: /* SVGA3dTransparencyAntialiasType */
3695 AssertFailed();
3696 /*
3697 renderState = D3DRS_TRANSPARENCYANTIALIAS;
3698 val = pRenderState[i].uintValue;
3699 */
3700 break;
3701
3702 case SVGA3D_RS_LINEAA: /* SVGA3dBool */
3703 renderState = D3DRS_ANTIALIASEDLINEENABLE;
3704 val = pRenderState[i].uintValue;
3705 break;
3706
3707 case SVGA3D_RS_LINEWIDTH: /* float */
3708 AssertFailed();
3709 /*
3710 renderState = D3DRS_LINEWIDTH;
3711 val = pRenderState[i].uintValue;
3712 */
3713 break;
3714 }
3715
3716 if (renderState != D3DRS_FORCE_DWORD)
3717 {
3718 hr = pContext->pDevice->SetRenderState(renderState, val);
3719 AssertMsgReturn(hr == D3D_OK, ("vmsvga3dSetRenderState: SetRenderState failed with %x\n", hr), VERR_INTERNAL_ERROR);
3720 }
3721 }
3722
3723 return VINF_SUCCESS;
3724}
3725
3726int vmsvga3dSetRenderTarget(PVGASTATE pThis, uint32_t cid, SVGA3dRenderTargetType type, SVGA3dSurfaceImageId target)
3727{
3728 HRESULT hr;
3729 PVMSVGA3DCONTEXT pContext;
3730 PVMSVGA3DSTATE pState = pThis->svga.p3dState;
3731 PVMSVGA3DSURFACE pRenderTarget;
3732
3733 AssertReturn(pState, VERR_NO_MEMORY);
3734 AssertReturn(type < SVGA3D_RT_MAX, VERR_INVALID_PARAMETER);
3735 AssertReturn(target.face == 0, VERR_INVALID_PARAMETER);
3736
3737 Log(("vmsvga3dSetRenderTarget cid=%x type=%x surface id=%x\n", cid, type, target.sid));
3738
3739 if ( cid >= pState->cContexts
3740 || pState->papContexts[cid]->id != cid)
3741 {
3742 Log(("vmsvga3dSetRenderTarget invalid context id!\n"));
3743 return VERR_INVALID_PARAMETER;
3744 }
3745 pContext = pState->papContexts[cid];
3746
3747 /* Save for vm state save/restore. */
3748 pContext->state.aRenderTargets[type] = target.sid;
3749
3750 if (target.sid == SVGA3D_INVALID_ID)
3751 {
3752 /* Disable render target. */
3753 switch (type)
3754 {
3755 case SVGA3D_RT_DEPTH:
3756 hr = pContext->pDevice->SetDepthStencilSurface(NULL);
3757 AssertMsgReturn(hr == D3D_OK, ("vmsvga3dSetRenderTarget: SetDepthStencilSurface failed with %x\n", hr), VERR_INTERNAL_ERROR);
3758 break;
3759
3760 case SVGA3D_RT_STENCIL:
3761 /* ignore; correct?? */
3762 break;
3763
3764 case SVGA3D_RT_COLOR0:
3765 case SVGA3D_RT_COLOR1:
3766 case SVGA3D_RT_COLOR2:
3767 case SVGA3D_RT_COLOR3:
3768 case SVGA3D_RT_COLOR4:
3769 case SVGA3D_RT_COLOR5:
3770 case SVGA3D_RT_COLOR6:
3771 case SVGA3D_RT_COLOR7:
3772 pContext->sidRenderTarget = SVGA3D_INVALID_ID;
3773
3774 if (pState->fSupportedSurfaceNULL)
3775 {
3776 /* Create a dummy render target to satisfy D3D. This path is usually taken only to render into a depth buffer without
3777 * wishing to update an actual color render target
3778 */
3779 IDirect3DSurface9* pDummyRenderTarget;
3780 hr = pContext->pDevice->CreateRenderTarget(pThis->svga.uWidth,
3781 pThis->svga.uHeight,
3782 FOURCC_NULL,
3783 D3DMULTISAMPLE_NONE,
3784 0,
3785 FALSE,
3786 &pDummyRenderTarget,
3787 NULL);
3788
3789 AssertMsgReturn(hr == D3D_OK, ("vmsvga3dSetRenderTarget: CreateRenderTarget failed with %x\n", hr), VERR_INTERNAL_ERROR);
3790
3791 hr = pContext->pDevice->SetRenderTarget(type - SVGA3D_RT_COLOR0, pDummyRenderTarget);
3792 pDummyRenderTarget->Release();
3793 }
3794 else
3795 hr = pContext->pDevice->SetRenderTarget(type - SVGA3D_RT_COLOR0, NULL);
3796
3797 AssertMsgReturn(hr == D3D_OK, ("vmsvga3dSetRenderTarget: SetRenderTarget failed with %x\n", hr), VERR_INTERNAL_ERROR);
3798 break;
3799
3800 default:
3801 AssertFailedReturn(VERR_INVALID_PARAMETER);
3802 }
3803 return VINF_SUCCESS;
3804 }
3805
3806 AssertReturn(target.sid < SVGA3D_MAX_SURFACE_IDS, VERR_INVALID_PARAMETER);
3807 AssertReturn(target.sid < pState->cSurfaces && pState->papSurfaces[target.sid]->id == target.sid, VERR_INVALID_PARAMETER);
3808 pRenderTarget = pState->papSurfaces[target.sid];
3809
3810 switch (type)
3811 {
3812 case SVGA3D_RT_DEPTH:
3813 case SVGA3D_RT_STENCIL:
3814 AssertReturn(target.mipmap == 0, VERR_INVALID_PARAMETER);
3815 if (!pRenderTarget->u.pSurface)
3816 {
3817 DWORD cQualityLevels = 0;
3818
3819 /* Query the nr of quality levels for this particular format */
3820 if (pRenderTarget->multiSampleTypeD3D != D3DMULTISAMPLE_NONE)
3821 {
3822 hr = pState->pD3D9->CheckDeviceMultiSampleType(D3DADAPTER_DEFAULT,
3823 D3DDEVTYPE_HAL,
3824 pRenderTarget->formatD3D,
3825 TRUE, /* Windowed */
3826 pRenderTarget->multiSampleTypeD3D,
3827 &cQualityLevels);
3828 Assert(hr == D3D_OK || hr == D3DERR_NOTAVAILABLE);
3829 }
3830
3831 if ( pState->fSupportedSurfaceINTZ
3832 && pRenderTarget->multiSampleTypeD3D == D3DMULTISAMPLE_NONE
3833 && ( pRenderTarget->formatD3D == D3DFMT_D24S8
3834 || pRenderTarget->formatD3D == D3DFMT_D24X8))
3835 {
3836 Log(("vmsvga3dSetRenderTarget: Creating stencil surface as texture!\n"));
3837 int rc = vmsvga3dBackCreateTexture(pState, pContext, cid, pRenderTarget);
3838 AssertRC(rc); /* non-fatal */
3839 }
3840
3841 if (!pRenderTarget->fStencilAsTexture)
3842 {
3843 Log(("vmsvga3dSetRenderTarget DEPTH/STENCIL; cQualityLevels=%d\n", cQualityLevels));
3844 hr = pContext->pDevice->CreateDepthStencilSurface(pRenderTarget->pMipmapLevels[0].size.width,
3845 pRenderTarget->pMipmapLevels[0].size.height,
3846 pRenderTarget->formatD3D,
3847 pRenderTarget->multiSampleTypeD3D,
3848 ((cQualityLevels >= 1) ? cQualityLevels - 1 : 0), /* 0 - (levels-1) */
3849 FALSE, /* not discardable */
3850 &pRenderTarget->u.pSurface,
3851 NULL);
3852 AssertMsgReturn(hr == D3D_OK, ("vmsvga3dSetRenderTarget: CreateDepthStencilSurface failed with %x\n", hr), VERR_INTERNAL_ERROR);
3853 }
3854
3855 pRenderTarget->idAssociatedContext = cid;
3856
3857#if 0 /* doesn't work */
3858 if ( !pRenderTarget->fStencilAsTexture
3859 && pRenderTarget->fDirty)
3860 {
3861 Log(("vmsvga3dSetRenderTarget: sync dirty depth/stencil buffer\n"));
3862 Assert(pRenderTarget->faces[0].numMipLevels == 1);
3863
3864 for (uint32_t i = 0; i < pRenderTarget->faces[0].numMipLevels; i++)
3865 {
3866 if (pRenderTarget->pMipmapLevels[i].fDirty)
3867 {
3868 D3DLOCKED_RECT LockedRect;
3869
3870 hr = pRenderTarget->u.pSurface->LockRect(&LockedRect,
3871 NULL, /* entire surface */
3872 0);
3873
3874 AssertMsgReturn(hr == D3D_OK, ("vmsvga3dSetRenderTarget: LockRect failed with %x\n", hr), VERR_INTERNAL_ERROR);
3875
3876 Log(("vmsvga3dSetRenderTarget: sync dirty texture mipmap level %d (pitch %x vs %x)\n", i, LockedRect.Pitch, pRenderTarget->pMipmapLevels[i].cbSurfacePitch));
3877
3878 uint8_t *pDest = (uint8_t *)LockedRect.pBits;
3879 uint8_t *pSrc = (uint8_t *)pRenderTarget->pMipmapLevels[i].pSurfaceData;
3880 for (uint32_t j = 0; j < pRenderTarget->pMipmapLevels[i].size.height; j++)
3881 {
3882 memcpy(pDest, pSrc, pRenderTarget->pMipmapLevels[i].cbSurfacePitch);
3883
3884 pDest += LockedRect.Pitch;
3885 pSrc += pRenderTarget->pMipmapLevels[i].cbSurfacePitch;
3886 }
3887
3888 hr = pRenderTarget->u.pSurface->UnlockRect();
3889 AssertMsgReturn(hr == D3D_OK, ("vmsvga3dSetRenderTarget: UnlockRect failed with %x\n", hr), VERR_INTERNAL_ERROR);
3890
3891 pRenderTarget->pMipmapLevels[i].fDirty = false;
3892 }
3893 }
3894 }
3895#endif
3896 }
3897 Assert(pRenderTarget->idAssociatedContext == cid);
3898
3899 /* @todo Assert(!pRenderTarget->fDirty); */
3900
3901 AssertReturn(pRenderTarget->u.pSurface, VERR_INVALID_PARAMETER);
3902
3903 pRenderTarget->fUsageD3D |= D3DUSAGE_DEPTHSTENCIL;
3904 pRenderTarget->flags |= SVGA3D_SURFACE_HINT_DEPTHSTENCIL;
3905
3906 if (pRenderTarget->fStencilAsTexture)
3907 {
3908 IDirect3DSurface9 *pStencilSurface;
3909
3910 hr = pRenderTarget->u.pTexture->GetSurfaceLevel(0, &pStencilSurface);
3911 AssertMsgReturn(hr == D3D_OK, ("vmsvga3dSetRenderTarget: GetSurfaceLevel failed with %x\n", hr), VERR_INTERNAL_ERROR);
3912
3913 hr = pContext->pDevice->SetDepthStencilSurface(pStencilSurface);
3914 pStencilSurface->Release();
3915 AssertMsgReturn(hr == D3D_OK, ("vmsvga3dSetRenderTarget: SetDepthStencilSurface failed with %x\n", hr), VERR_INTERNAL_ERROR);
3916 }
3917 else
3918 {
3919 hr = pContext->pDevice->SetDepthStencilSurface(pRenderTarget->u.pSurface);
3920 AssertMsgReturn(hr == D3D_OK, ("vmsvga3dSetRenderTarget: SetDepthStencilSurface failed with %x\n", hr), VERR_INTERNAL_ERROR);
3921 }
3922 break;
3923
3924 case SVGA3D_RT_COLOR0:
3925 case SVGA3D_RT_COLOR1:
3926 case SVGA3D_RT_COLOR2:
3927 case SVGA3D_RT_COLOR3:
3928 case SVGA3D_RT_COLOR4:
3929 case SVGA3D_RT_COLOR5:
3930 case SVGA3D_RT_COLOR6:
3931 case SVGA3D_RT_COLOR7:
3932 {
3933 IDirect3DSurface9 *pSurface;
3934 bool fTexture = false;
3935 bool fShared = false;
3936
3937 /* Must flush the other context's 3d pipeline to make sure all drawing is complete for the surface we're about to use. */
3938 vmsvga3dSurfaceFlush(pThis, pRenderTarget);
3939
3940 if (pRenderTarget->flags & SVGA3D_SURFACE_HINT_TEXTURE)
3941 {
3942 fTexture = true;
3943
3944 /* A texture surface can be used as a render target to fill it and later on used as a texture. */
3945 if (!pRenderTarget->u.pTexture)
3946 {
3947 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));
3948 int rc = vmsvga3dBackCreateTexture(pState, pContext, cid, pRenderTarget);
3949 AssertRCReturn(rc, rc);
3950 }
3951
3952#ifndef VBOX_VMSVGA3D_WITH_WINE_OPENGL
3953 if (pRenderTarget->idAssociatedContext != cid)
3954 {
3955 Log(("vmsvga3dSetRenderTarget; using texture %x created for another context (%d vs %d)\n", target.sid, pRenderTarget->idAssociatedContext, cid));
3956
3957 PVMSVGA3DSHAREDSURFACE pSharedSurface = vmsvga3dSurfaceGetSharedCopy(pThis, pContext, pRenderTarget);
3958 AssertReturn(pSharedSurface, VERR_INTERNAL_ERROR);
3959
3960 hr = pSharedSurface->u.pTexture->GetSurfaceLevel(target.mipmap,
3961 &pSurface);
3962
3963 fShared = true;
3964 }
3965 else
3966#endif
3967 hr = pRenderTarget->u.pTexture->GetSurfaceLevel(target.mipmap,
3968 &pSurface);
3969
3970 AssertMsgReturn(hr == D3D_OK, ("vmsvga3dSetRenderTarget: GetSurfaceLevel failed with %x\n", hr), VERR_INTERNAL_ERROR);
3971 }
3972 else
3973 {
3974 AssertReturn(target.mipmap == 0, VERR_INVALID_PARAMETER);
3975 if (!pRenderTarget->u.pSurface)
3976 {
3977 DWORD cQualityLevels = 0;
3978
3979 /* Query the nr of quality levels for this particular format */
3980 if (pRenderTarget->multiSampleTypeD3D != D3DMULTISAMPLE_NONE)
3981 {
3982 hr = pState->pD3D9->CheckDeviceMultiSampleType(D3DADAPTER_DEFAULT,
3983 D3DDEVTYPE_HAL,
3984 pRenderTarget->formatD3D,
3985 TRUE, /* Windowed */
3986 pRenderTarget->multiSampleTypeD3D,
3987 &cQualityLevels);
3988 Assert(hr == D3D_OK || hr == D3DERR_NOTAVAILABLE);
3989 }
3990
3991 Log(("vmsvga3dSetRenderTarget COLOR; cQualityLevels=%d\n", cQualityLevels));
3992 Log(("Create rendertarget (%d,%d) format=%x multisample=%x\n", pRenderTarget->pMipmapLevels[0].size.width, pRenderTarget->pMipmapLevels[0].size.height, pRenderTarget->formatD3D, pRenderTarget->multiSampleTypeD3D));
3993
3994 hr = pContext->pDevice->CreateRenderTarget(pRenderTarget->pMipmapLevels[0].size.width,
3995 pRenderTarget->pMipmapLevels[0].size.height,
3996 pRenderTarget->formatD3D,
3997 pRenderTarget->multiSampleTypeD3D,
3998 ((cQualityLevels >= 1) ? cQualityLevels - 1 : 0), /* 0 - (levels-1) */
3999 TRUE, /* lockable */
4000 &pRenderTarget->u.pSurface,
4001 NULL);
4002 AssertReturn(hr == D3D_OK, VERR_INTERNAL_ERROR);
4003
4004 pRenderTarget->idAssociatedContext = cid;
4005 }
4006 else
4007 AssertReturn(pRenderTarget->fUsageD3D & D3DUSAGE_RENDERTARGET, VERR_INVALID_PARAMETER);
4008
4009 Assert(pRenderTarget->idAssociatedContext == cid);
4010 pSurface = pRenderTarget->u.pSurface;
4011 }
4012
4013 AssertReturn(pRenderTarget->u.pSurface, VERR_INVALID_PARAMETER);
4014 Assert(!pRenderTarget->fDirty);
4015
4016 pRenderTarget->fUsageD3D |= D3DUSAGE_RENDERTARGET;
4017 pRenderTarget->flags |= SVGA3D_SURFACE_HINT_RENDERTARGET;
4018
4019 hr = pContext->pDevice->SetRenderTarget(type - SVGA3D_RT_COLOR0, pSurface);
4020 if (fTexture)
4021 pSurface->Release(); /* Release reference to texture level 0 */
4022 AssertMsgReturn(hr == D3D_OK, ("vmsvga3dSetRenderTarget: SetRenderTarget failed with %x\n", hr), VERR_INTERNAL_ERROR);
4023
4024 pContext->sidRenderTarget = target.sid;
4025
4026 /* Changing the render target resets the viewport; restore it here. */
4027 if (pContext->state.u32UpdateFlags & VMSVGA3D_UPDATE_VIEWPORT)
4028 vmsvga3dSetViewPort(pThis, cid, &pContext->state.RectViewPort);
4029 /* Changing the render target also resets the scissor rectangle; restore it as well. */
4030 if (pContext->state.u32UpdateFlags & VMSVGA3D_UPDATE_SCISSORRECT)
4031 vmsvga3dSetScissorRect(pThis, cid, &pContext->state.RectScissor);
4032
4033 break;
4034 }
4035
4036 default:
4037 AssertFailedReturn(VERR_INVALID_PARAMETER);
4038 }
4039
4040 return VINF_SUCCESS;
4041}
4042
4043/**
4044 * Convert SVGA texture combiner value to its D3D equivalent
4045 */
4046static DWORD vmsvga3dTextureCombiner2D3D(uint32_t value)
4047{
4048 switch (value)
4049 {
4050 case SVGA3D_TC_DISABLE:
4051 return D3DTOP_DISABLE;
4052 case SVGA3D_TC_SELECTARG1:
4053 return D3DTOP_SELECTARG1;
4054 case SVGA3D_TC_SELECTARG2:
4055 return D3DTOP_SELECTARG2;
4056 case SVGA3D_TC_MODULATE:
4057 return D3DTOP_MODULATE;
4058 case SVGA3D_TC_ADD:
4059 return D3DTOP_ADD;
4060 case SVGA3D_TC_ADDSIGNED:
4061 return D3DTOP_ADDSIGNED;
4062 case SVGA3D_TC_SUBTRACT:
4063 return D3DTOP_SUBTRACT;
4064 case SVGA3D_TC_BLENDTEXTUREALPHA:
4065 return D3DTOP_BLENDTEXTUREALPHA;
4066 case SVGA3D_TC_BLENDDIFFUSEALPHA:
4067 return D3DTOP_BLENDDIFFUSEALPHA;
4068 case SVGA3D_TC_BLENDCURRENTALPHA:
4069 return D3DTOP_BLENDCURRENTALPHA;
4070 case SVGA3D_TC_BLENDFACTORALPHA:
4071 return D3DTOP_BLENDFACTORALPHA;
4072 case SVGA3D_TC_MODULATE2X:
4073 return D3DTOP_MODULATE2X;
4074 case SVGA3D_TC_MODULATE4X:
4075 return D3DTOP_MODULATE4X;
4076 case SVGA3D_TC_DSDT:
4077 AssertFailed(); /* @todo ??? */
4078 return D3DTOP_DISABLE;
4079 case SVGA3D_TC_DOTPRODUCT3:
4080 return D3DTOP_DOTPRODUCT3;
4081 case SVGA3D_TC_BLENDTEXTUREALPHAPM:
4082 return D3DTOP_BLENDTEXTUREALPHAPM;
4083 case SVGA3D_TC_ADDSIGNED2X:
4084 return D3DTOP_ADDSIGNED2X;
4085 case SVGA3D_TC_ADDSMOOTH:
4086 return D3DTOP_ADDSMOOTH;
4087 case SVGA3D_TC_PREMODULATE:
4088 return D3DTOP_PREMODULATE;
4089 case SVGA3D_TC_MODULATEALPHA_ADDCOLOR:
4090 return D3DTOP_MODULATEALPHA_ADDCOLOR;
4091 case SVGA3D_TC_MODULATECOLOR_ADDALPHA:
4092 return D3DTOP_MODULATECOLOR_ADDALPHA;
4093 case SVGA3D_TC_MODULATEINVALPHA_ADDCOLOR:
4094 return D3DTOP_MODULATEINVALPHA_ADDCOLOR;
4095 case SVGA3D_TC_MODULATEINVCOLOR_ADDALPHA:
4096 return D3DTOP_MODULATEINVCOLOR_ADDALPHA;
4097 case SVGA3D_TC_BUMPENVMAPLUMINANCE:
4098 return D3DTOP_BUMPENVMAPLUMINANCE;
4099 case SVGA3D_TC_MULTIPLYADD:
4100 return D3DTOP_MULTIPLYADD;
4101 case SVGA3D_TC_LERP:
4102 return D3DTOP_LERP;
4103 default:
4104 AssertFailed();
4105 return D3DTOP_DISABLE;
4106 }
4107}
4108
4109/**
4110 * Convert SVGA texture arg data value to its D3D equivalent
4111 */
4112static DWORD vmsvga3dTextureArgData2D3D(uint32_t value)
4113{
4114 switch (value)
4115 {
4116 case SVGA3D_TA_CONSTANT:
4117 return D3DTA_CONSTANT;
4118 case SVGA3D_TA_PREVIOUS:
4119 return D3DTA_CURRENT; /* current = previous */
4120 case SVGA3D_TA_DIFFUSE:
4121 return D3DTA_DIFFUSE;
4122 case SVGA3D_TA_TEXTURE:
4123 return D3DTA_TEXTURE;
4124 case SVGA3D_TA_SPECULAR:
4125 return D3DTA_SPECULAR;
4126 default:
4127 AssertFailed();
4128 return 0;
4129 }
4130}
4131
4132/**
4133 * Convert SVGA texture transform flag value to its D3D equivalent
4134 */
4135static DWORD vmsvga3dTextTransformFlags2D3D(uint32_t value)
4136{
4137 switch (value)
4138 {
4139 case SVGA3D_TEX_TRANSFORM_OFF:
4140 return D3DTTFF_DISABLE;
4141 case SVGA3D_TEX_TRANSFORM_S:
4142 return D3DTTFF_COUNT1; /* @todo correct? */
4143 case SVGA3D_TEX_TRANSFORM_T:
4144 return D3DTTFF_COUNT2; /* @todo correct? */
4145 case SVGA3D_TEX_TRANSFORM_R:
4146 return D3DTTFF_COUNT3; /* @todo correct? */
4147 case SVGA3D_TEX_TRANSFORM_Q:
4148 return D3DTTFF_COUNT4; /* @todo correct? */
4149 case SVGA3D_TEX_PROJECTED:
4150 return D3DTTFF_PROJECTED;
4151 default:
4152 AssertFailed();
4153 return 0;
4154 }
4155}
4156
4157int vmsvga3dSetTextureState(PVGASTATE pThis, uint32_t cid, uint32_t cTextureStates, SVGA3dTextureState *pTextureState)
4158{
4159 DWORD val;
4160 HRESULT hr;
4161 PVMSVGA3DCONTEXT pContext;
4162 PVMSVGA3DSTATE pState = pThis->svga.p3dState;
4163 AssertReturn(pState, VERR_NO_MEMORY);
4164
4165 Log(("vmsvga3dSetTextureState %x cTextureState=%d\n", cid, cTextureStates));
4166
4167 if ( cid >= pState->cContexts
4168 || pState->papContexts[cid]->id != cid)
4169 {
4170 Log(("vmsvga3dSetTextureState invalid context id!\n"));
4171 return VERR_INVALID_PARAMETER;
4172 }
4173 pContext = pState->papContexts[cid];
4174
4175 for (unsigned i = 0; i < cTextureStates; i++)
4176 {
4177 D3DTEXTURESTAGESTATETYPE textureType = D3DTSS_FORCE_DWORD;
4178 D3DSAMPLERSTATETYPE samplerType = D3DSAMP_FORCE_DWORD;
4179 uint32_t currentStage = pTextureState[i].stage;
4180
4181 Log(("vmsvga3dSetTextureState: cid=%x stage=%d type=%s (%x) val=%x\n", cid, currentStage, vmsvga3dTextureStateToString(pTextureState[i].name), pTextureState[i].name, pTextureState[i].value));
4182
4183 /** @todo Is this the appropriate limit for all kinds of textures? It is the
4184 * size of aSidActiveTexture and for binding/unbinding we cannot exceed it. */
4185 if (RT_UNLIKELY(currentStage >= SVGA3D_MAX_TEXTURE_STAGE))
4186 {
4187 AssertMsgFailed(("pTextureState[%d].stage=%#x name=%#x\n", i, pTextureState[i].stage, pTextureState[i].name));
4188 continue;
4189 }
4190
4191 /* Record the texture state for vm state saving. */
4192 if ( currentStage < SVGA3D_MAX_TEXTURE_STAGE
4193 && pTextureState[i].name < SVGA3D_TS_MAX)
4194 {
4195 pContext->state.aTextureState[currentStage][pTextureState[i].name] = pTextureState[i];
4196 }
4197
4198 switch (pTextureState[i].name)
4199 {
4200 case SVGA3D_TS_COLOROP: /* SVGA3dTextureCombiner */
4201 textureType = D3DTSS_COLOROP;
4202 val = vmsvga3dTextureCombiner2D3D(pTextureState[i].value);
4203 break;
4204
4205 case SVGA3D_TS_COLORARG0: /* SVGA3dTextureArgData */
4206 textureType = D3DTSS_COLORARG0;
4207 val = vmsvga3dTextureArgData2D3D(pTextureState[i].value);
4208 break;
4209
4210 case SVGA3D_TS_COLORARG1: /* SVGA3dTextureArgData */
4211 textureType = D3DTSS_COLORARG1;
4212 val = vmsvga3dTextureArgData2D3D(pTextureState[i].value);
4213 break;
4214
4215 case SVGA3D_TS_COLORARG2: /* SVGA3dTextureArgData */
4216 textureType = D3DTSS_COLORARG2;
4217 val = vmsvga3dTextureArgData2D3D(pTextureState[i].value);
4218 break;
4219
4220 case SVGA3D_TS_ALPHAOP: /* SVGA3dTextureCombiner */
4221 textureType = D3DTSS_ALPHAOP;
4222 val = vmsvga3dTextureCombiner2D3D(pTextureState[i].value);
4223 break;
4224
4225 case SVGA3D_TS_ALPHAARG0: /* SVGA3dTextureArgData */
4226 textureType = D3DTSS_ALPHAARG0;
4227 val = vmsvga3dTextureArgData2D3D(pTextureState[i].value);
4228 break;
4229
4230 case SVGA3D_TS_ALPHAARG1: /* SVGA3dTextureArgData */
4231 textureType = D3DTSS_ALPHAARG1;
4232 val = vmsvga3dTextureArgData2D3D(pTextureState[i].value);
4233 break;
4234
4235 case SVGA3D_TS_ALPHAARG2: /* SVGA3dTextureArgData */
4236 textureType = D3DTSS_ALPHAARG2;
4237 val = vmsvga3dTextureArgData2D3D(pTextureState[i].value);
4238 break;
4239
4240 case SVGA3D_TS_BUMPENVMAT00: /* float */
4241 textureType = D3DTSS_BUMPENVMAT00;
4242 val = pTextureState[i].value;
4243 break;
4244
4245 case SVGA3D_TS_BUMPENVMAT01: /* float */
4246 textureType = D3DTSS_BUMPENVMAT01;
4247 val = pTextureState[i].value;
4248 break;
4249
4250 case SVGA3D_TS_BUMPENVMAT10: /* float */
4251 textureType = D3DTSS_BUMPENVMAT10;
4252 val = pTextureState[i].value;
4253 break;
4254
4255 case SVGA3D_TS_BUMPENVMAT11: /* float */
4256 textureType = D3DTSS_BUMPENVMAT11;
4257 val = pTextureState[i].value;
4258 break;
4259
4260 case SVGA3D_TS_TEXCOORDINDEX: /* uint32_t */
4261 textureType = D3DTSS_TEXCOORDINDEX;
4262 val = pTextureState[i].value;
4263 break;
4264
4265 case SVGA3D_TS_BUMPENVLSCALE: /* float */
4266 textureType = D3DTSS_BUMPENVLSCALE;
4267 val = pTextureState[i].value;
4268 break;
4269
4270 case SVGA3D_TS_BUMPENVLOFFSET: /* float */
4271 textureType = D3DTSS_BUMPENVLOFFSET;
4272 val = pTextureState[i].value;
4273 break;
4274
4275 case SVGA3D_TS_TEXTURETRANSFORMFLAGS: /* SVGA3dTexTransformFlags */
4276 textureType = D3DTSS_TEXTURETRANSFORMFLAGS;
4277 val = vmsvga3dTextTransformFlags2D3D(pTextureState[i].value);
4278 break;
4279
4280 case SVGA3D_TS_BIND_TEXTURE: /* SVGA3dSurfaceId */
4281 if (pTextureState[i].value == SVGA3D_INVALID_ID)
4282 {
4283 Log(("SVGA3D_TS_BIND_TEXTURE: stage %d, texture surface id=%x\n", currentStage, pTextureState[i].value));
4284
4285 pContext->aSidActiveTexture[currentStage] = SVGA3D_INVALID_ID;
4286 /* Unselect the currently associated texture. */
4287 hr = pContext->pDevice->SetTexture(currentStage, NULL);
4288 AssertMsgReturn(hr == D3D_OK, ("vmsvga3dSetTextureState: SetTexture failed with %x\n", hr), VERR_INTERNAL_ERROR);
4289 }
4290 else
4291 {
4292 HRESULT hr;
4293 uint32_t sid = pTextureState[i].value;
4294
4295 AssertReturn(sid < SVGA3D_MAX_SURFACE_IDS, VERR_INVALID_PARAMETER);
4296 AssertReturn(sid < pState->cSurfaces && pState->papSurfaces[sid]->id == sid, VERR_INVALID_PARAMETER);
4297
4298 PVMSVGA3DSURFACE pSurface = pState->papSurfaces[sid];
4299
4300 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));
4301
4302 if (!pSurface->u.pTexture)
4303 {
4304 Assert(pSurface->idAssociatedContext == SVGA3D_INVALID_ID);
4305 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));
4306 int rc = vmsvga3dBackCreateTexture(pState, pContext, cid, pSurface);
4307 AssertRCReturn(rc, rc);
4308 }
4309 else
4310 {
4311 /* Must flush the other context's 3d pipeline to make sure all drawing is complete for the surface we're about to use. */
4312 vmsvga3dSurfaceFlush(pThis, pSurface);
4313 }
4314
4315#ifndef VBOX_VMSVGA3D_WITH_WINE_OPENGL
4316 if (pSurface->idAssociatedContext != cid)
4317 {
4318 Log(("vmsvga3dSetTextureState; using texture %x created for another context (%d vs %d)\n", sid, pSurface->idAssociatedContext, cid));
4319
4320 PVMSVGA3DSHAREDSURFACE pSharedSurface = vmsvga3dSurfaceGetSharedCopy(pThis, pContext, pSurface);
4321 AssertReturn(pSharedSurface, VERR_INTERNAL_ERROR);
4322
4323 hr = pContext->pDevice->SetTexture(currentStage, pSharedSurface->u.pTexture);
4324 }
4325 else
4326#endif
4327 hr = pContext->pDevice->SetTexture(currentStage, pSurface->u.pTexture);
4328
4329 AssertMsgReturn(hr == D3D_OK, ("vmsvga3dSetTextureState: SetTexture failed with %x\n", hr), VERR_INTERNAL_ERROR);
4330
4331 pContext->aSidActiveTexture[currentStage] = sid;
4332 }
4333 /* Finished; continue with the next one. */
4334 continue;
4335
4336 case SVGA3D_TS_ADDRESSW: /* SVGA3dTextureAddress */
4337 samplerType = D3DSAMP_ADDRESSW;
4338 val = pTextureState[i].value; /* Identical otherwise */
4339 Assert(pTextureState[i].value != SVGA3D_TEX_ADDRESS_EDGE);
4340 break;
4341
4342 case SVGA3D_TS_ADDRESSU: /* SVGA3dTextureAddress */
4343 samplerType = D3DSAMP_ADDRESSU;
4344 val = pTextureState[i].value; /* Identical otherwise */
4345 Assert(pTextureState[i].value != SVGA3D_TEX_ADDRESS_EDGE);
4346 break;
4347
4348 case SVGA3D_TS_ADDRESSV: /* SVGA3dTextureAddress */
4349 samplerType = D3DSAMP_ADDRESSV;
4350 val = pTextureState[i].value; /* Identical otherwise */
4351 Assert(pTextureState[i].value != SVGA3D_TEX_ADDRESS_EDGE);
4352 break;
4353
4354 case SVGA3D_TS_MIPFILTER: /* SVGA3dTextureFilter */
4355 samplerType = D3DSAMP_MIPFILTER;
4356 val = pTextureState[i].value; /* Identical otherwise */
4357 Assert(pTextureState[i].value != SVGA3D_TEX_FILTER_FLATCUBIC);
4358 Assert(pTextureState[i].value != SVGA3D_TEX_FILTER_GAUSSIANCUBIC);
4359 break;
4360
4361 case SVGA3D_TS_MAGFILTER: /* SVGA3dTextureFilter */
4362 samplerType = D3DSAMP_MAGFILTER;
4363 val = pTextureState[i].value; /* Identical otherwise */
4364 Assert(pTextureState[i].value != SVGA3D_TEX_FILTER_FLATCUBIC);
4365 Assert(pTextureState[i].value != SVGA3D_TEX_FILTER_GAUSSIANCUBIC);
4366 break;
4367
4368 case SVGA3D_TS_MINFILTER: /* SVGA3dTextureFilter */
4369 samplerType = D3DSAMP_MINFILTER;
4370 val = pTextureState[i].value; /* Identical otherwise */
4371 Assert(pTextureState[i].value != SVGA3D_TEX_FILTER_FLATCUBIC);
4372 Assert(pTextureState[i].value != SVGA3D_TEX_FILTER_GAUSSIANCUBIC);
4373 break;
4374
4375 case SVGA3D_TS_BORDERCOLOR: /* SVGA3dColor */
4376 samplerType = D3DSAMP_BORDERCOLOR;
4377 val = pTextureState[i].value; /* Identical */
4378 break;
4379
4380 case SVGA3D_TS_TEXTURE_LOD_BIAS: /* float */
4381 samplerType = D3DSAMP_MIPMAPLODBIAS;
4382 val = pTextureState[i].value; /* Identical */
4383 break;
4384
4385 case SVGA3D_TS_TEXTURE_MIPMAP_LEVEL: /* uint32_t */
4386 samplerType = D3DSAMP_MAXMIPLEVEL;
4387 val = pTextureState[i].value; /* Identical?? */
4388 break;
4389
4390 case SVGA3D_TS_TEXTURE_ANISOTROPIC_LEVEL: /* uint32_t */
4391 samplerType = D3DSAMP_MAXANISOTROPY;
4392 val = pTextureState[i].value; /* Identical?? */
4393 break;
4394
4395 case SVGA3D_TS_GAMMA: /* float */
4396 samplerType = D3DSAMP_SRGBTEXTURE;
4397 /* Boolean in D3D */
4398 if (pTextureState[i].floatValue == 1.0f)
4399 val = FALSE;
4400 else
4401 val = TRUE;
4402 break;
4403
4404 /* Internal commands, that don't map directly to the SetTextureStageState API. */
4405 case SVGA3D_TS_TEXCOORDGEN: /* SVGA3dTextureCoordGen */
4406 AssertFailed();
4407 break;
4408 }
4409
4410 if (textureType != D3DTSS_FORCE_DWORD)
4411 {
4412 hr = pContext->pDevice->SetTextureStageState(currentStage, textureType, val);
4413 }
4414 else
4415 {
4416 Assert(samplerType != D3DSAMP_FORCE_DWORD);
4417 hr = pContext->pDevice->SetSamplerState(currentStage, samplerType, val);
4418 }
4419
4420 AssertMsgReturn(hr == D3D_OK, ("vmsvga3dSetTextureState: SetTextureStageState failed with %x\n", hr), VERR_INTERNAL_ERROR);
4421 }
4422
4423 return VINF_SUCCESS;
4424}
4425
4426int vmsvga3dSetMaterial(PVGASTATE pThis, uint32_t cid, SVGA3dFace face, SVGA3dMaterial *pMaterial)
4427{
4428 HRESULT hr;
4429 D3DMATERIAL9 material;
4430 PVMSVGA3DCONTEXT pContext;
4431 PVMSVGA3DSTATE pState = pThis->svga.p3dState;
4432 AssertReturn(pState, VERR_NO_MEMORY);
4433
4434 Log(("vmsvga3dSetMaterial %x face %d\n", cid, face));
4435
4436 if ( cid >= pState->cContexts
4437 || pState->papContexts[cid]->id != cid)
4438 {
4439 Log(("vmsvga3dSetMaterial invalid context id!\n"));
4440 return VERR_INVALID_PARAMETER;
4441 }
4442 pContext = pState->papContexts[cid];
4443
4444 AssertReturn(face < SVGA3D_FACE_MAX, VERR_INVALID_PARAMETER);
4445
4446 /* Save for vm state save/restore. */
4447 pContext->state.aMaterial[face].fValid = true;
4448 pContext->state.aMaterial[face].material = *pMaterial;
4449 pContext->state.u32UpdateFlags |= VMSVGA3D_UPDATE_MATERIAL;
4450
4451 /* @note face not used for D3D9 */
4452 /* @todo ignore everything except SVGA3D_FACE_NONE? */
4453 //Assert(face == SVGA3D_FACE_NONE);
4454 if (face != SVGA3D_FACE_NONE)
4455 Log(("Unsupported face %d!!\n", face));
4456
4457 material.Diffuse.r = pMaterial->diffuse[0];
4458 material.Diffuse.g = pMaterial->diffuse[1];
4459 material.Diffuse.b = pMaterial->diffuse[2];
4460 material.Diffuse.a = pMaterial->diffuse[3];
4461 material.Ambient.r = pMaterial->ambient[0];
4462 material.Ambient.g = pMaterial->ambient[1];
4463 material.Ambient.b = pMaterial->ambient[2];
4464 material.Ambient.a = pMaterial->ambient[3];
4465 material.Specular.r = pMaterial->specular[0];
4466 material.Specular.g = pMaterial->specular[1];
4467 material.Specular.b = pMaterial->specular[2];
4468 material.Specular.a = pMaterial->specular[3];
4469 material.Emissive.r = pMaterial->emissive[0];
4470 material.Emissive.g = pMaterial->emissive[1];
4471 material.Emissive.b = pMaterial->emissive[2];
4472 material.Emissive.a = pMaterial->emissive[3];
4473 material.Power = pMaterial->shininess;
4474
4475 hr = pContext->pDevice->SetMaterial(&material);
4476 AssertMsgReturn(hr == D3D_OK, ("vmsvga3dSetMaterial: SetMaterial failed with %x\n", hr), VERR_INTERNAL_ERROR);
4477
4478 return VINF_SUCCESS;
4479}
4480
4481int vmsvga3dSetLightData(PVGASTATE pThis, uint32_t cid, uint32_t index, SVGA3dLightData *pData)
4482{
4483 HRESULT hr;
4484 D3DLIGHT9 light;
4485 PVMSVGA3DCONTEXT pContext;
4486 PVMSVGA3DSTATE pState = pThis->svga.p3dState;
4487 AssertReturn(pState, VERR_NO_MEMORY);
4488
4489 Log(("vmsvga3dSetLightData %x index=%d\n", cid, index));
4490
4491 if ( cid >= pState->cContexts
4492 || pState->papContexts[cid]->id != cid)
4493 {
4494 Log(("vmsvga3dSetLightData invalid context id!\n"));
4495 return VERR_INVALID_PARAMETER;
4496 }
4497 pContext = pState->papContexts[cid];
4498
4499 switch (pData->type)
4500 {
4501 case SVGA3D_LIGHTTYPE_POINT:
4502 light.Type = D3DLIGHT_POINT;
4503 break;
4504
4505 case SVGA3D_LIGHTTYPE_SPOT1: /* 1-cone, in degrees */
4506 light.Type = D3DLIGHT_SPOT;
4507 break;
4508
4509 case SVGA3D_LIGHTTYPE_DIRECTIONAL:
4510 light.Type = D3DLIGHT_DIRECTIONAL;
4511 break;
4512
4513 case SVGA3D_LIGHTTYPE_SPOT2: /* 2-cone, in radians */
4514 default:
4515 Log(("Unsupported light type!!\n"));
4516 return VERR_INVALID_PARAMETER;
4517 }
4518
4519 /* Store for vm state save/restore */
4520 if (index < SVGA3D_MAX_LIGHTS)
4521 {
4522 pContext->state.aLightData[index].fValidData = true;
4523 pContext->state.aLightData[index].data = *pData;
4524 }
4525 else
4526 AssertFailed();
4527
4528 light.Diffuse.r = pData->diffuse[0];
4529 light.Diffuse.g = pData->diffuse[1];
4530 light.Diffuse.b = pData->diffuse[2];
4531 light.Diffuse.a = pData->diffuse[3];
4532 light.Specular.r = pData->specular[0];
4533 light.Specular.g = pData->specular[1];
4534 light.Specular.b = pData->specular[2];
4535 light.Specular.a = pData->specular[3];
4536 light.Ambient.r = pData->ambient[0];
4537 light.Ambient.g = pData->ambient[1];
4538 light.Ambient.b = pData->ambient[2];
4539 light.Ambient.a = pData->ambient[3];
4540 light.Position.x = pData->position[0];
4541 light.Position.y = pData->position[1];
4542 light.Position.z = pData->position[2]; /* @note 4th position not available in D3D9 */
4543 light.Direction.x = pData->direction[0];
4544 light.Direction.y = pData->direction[1];
4545 light.Direction.z = pData->direction[2]; /* @note 4th position not available in D3D9 */
4546 light.Range = pData->range;
4547 light.Falloff = pData->falloff;
4548 light.Attenuation0 = pData->attenuation0;
4549 light.Attenuation1 = pData->attenuation1;
4550 light.Attenuation2 = pData->attenuation2;
4551 light.Theta = pData->theta;
4552 light.Phi = pData->phi;
4553
4554 hr = pContext->pDevice->SetLight(index, &light);
4555 AssertMsgReturn(hr == D3D_OK, ("vmsvga3dSetLightData: SetLight failed with %x\n", hr), VERR_INTERNAL_ERROR);
4556
4557 return VINF_SUCCESS;
4558}
4559
4560int vmsvga3dSetLightEnabled(PVGASTATE pThis, uint32_t cid, uint32_t index, uint32_t enabled)
4561{
4562 HRESULT hr;
4563 PVMSVGA3DCONTEXT pContext;
4564 PVMSVGA3DSTATE pState = pThis->svga.p3dState;
4565 AssertReturn(pState, VERR_NO_MEMORY);
4566
4567 Log(("vmsvga3dSetLightEnabled %x %d -> %d\n", cid, index, enabled));
4568
4569 if ( cid >= pState->cContexts
4570 || pState->papContexts[cid]->id != cid)
4571 {
4572 Log(("vmsvga3dSetLightEnabled invalid context id!\n"));
4573 return VERR_INVALID_PARAMETER;
4574 }
4575 pContext = pState->papContexts[cid];
4576
4577 /* Store for vm state save/restore */
4578 if (index < SVGA3D_MAX_LIGHTS)
4579 pContext->state.aLightData[index].fEnabled = !!enabled;
4580 else
4581 AssertFailed();
4582
4583 hr = pContext->pDevice->LightEnable(index, (BOOL)enabled);
4584 AssertMsgReturn(hr == D3D_OK, ("vmsvga3dSetLightEnabled: LightEnable failed with %x\n", hr), VERR_INTERNAL_ERROR);
4585
4586 return VINF_SUCCESS;
4587}
4588
4589int vmsvga3dSetViewPort(PVGASTATE pThis, uint32_t cid, SVGA3dRect *pRect)
4590{
4591 HRESULT hr;
4592 D3DVIEWPORT9 viewPort;
4593 PVMSVGA3DCONTEXT pContext;
4594 PVMSVGA3DSTATE pState = pThis->svga.p3dState;
4595 AssertReturn(pState, VERR_NO_MEMORY);
4596
4597 Log(("vmsvga3dSetViewPort %x (%d,%d)(%d,%d)\n", cid, pRect->x, pRect->y, pRect->w, pRect->h));
4598
4599 if ( cid >= pState->cContexts
4600 || pState->papContexts[cid]->id != cid)
4601 {
4602 Log(("vmsvga3dSetViewPort invalid context id!\n"));
4603 return VERR_INVALID_PARAMETER;
4604 }
4605 /* Save for vm state save/restore. */
4606 pContext = pState->papContexts[cid];
4607 pContext->state.RectViewPort = *pRect;
4608 pContext->state.u32UpdateFlags |= VMSVGA3D_UPDATE_VIEWPORT;
4609
4610 hr = pContext->pDevice->GetViewport(&viewPort);
4611 AssertMsgReturn(hr == D3D_OK, ("vmsvga3dSetViewPort: GetViewport failed with %x\n", hr), VERR_INTERNAL_ERROR);
4612
4613 viewPort.X = pRect->x;
4614 viewPort.Y = pRect->y;
4615 viewPort.Width = pRect->w;
4616 viewPort.Height = pRect->h;
4617 /* viewPort.MinZ & MaxZ are not changed from the current setting. */
4618
4619 hr = pContext->pDevice->SetViewport(&viewPort);
4620 AssertMsgReturn(hr == D3D_OK, ("vmsvga3dSetViewPort: SetViewport failed with %x\n", hr), VERR_INTERNAL_ERROR);
4621
4622 return VINF_SUCCESS;
4623}
4624
4625int vmsvga3dSetClipPlane(PVGASTATE pThis, uint32_t cid, uint32_t index, float plane[4])
4626{
4627 HRESULT hr;
4628 PVMSVGA3DCONTEXT pContext;
4629 PVMSVGA3DSTATE pState = pThis->svga.p3dState;
4630 AssertReturn(pState, VERR_NO_MEMORY);
4631
4632 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)));
4633 AssertReturn(index < SVGA3D_CLIPPLANE_MAX, VERR_INVALID_PARAMETER);
4634
4635 if ( cid >= pState->cContexts
4636 || pState->papContexts[cid]->id != cid)
4637 {
4638 Log(("vmsvga3dSetClipPlane invalid context id!\n"));
4639 return VERR_INVALID_PARAMETER;
4640 }
4641 pContext = pState->papContexts[cid];
4642
4643 /* Store for vm state save/restore. */
4644 pContext->state.aClipPlane[index].fValid = true;
4645 memcpy(pContext->state.aClipPlane[index].plane, plane, sizeof(plane));
4646
4647 hr = pContext->pDevice->SetClipPlane(index, plane);
4648 AssertMsgReturn(hr == D3D_OK, ("vmsvga3dSetClipPlane: SetClipPlane failed with %x\n", hr), VERR_INTERNAL_ERROR);
4649 return VINF_SUCCESS;
4650}
4651
4652int vmsvga3dCommandClear(PVGASTATE pThis, uint32_t cid, SVGA3dClearFlag clearFlag, uint32_t color, float depth, uint32_t stencil, uint32_t cRects, SVGA3dRect *pRect)
4653{
4654 DWORD clearFlagD3D = 0;
4655 D3DRECT *pRectD3D = NULL;
4656 HRESULT hr;
4657 PVMSVGA3DCONTEXT pContext;
4658 PVMSVGA3DSTATE pState = pThis->svga.p3dState;
4659 AssertReturn(pState, VERR_NO_MEMORY);
4660
4661 Log(("vmsvga3dCommandClear %x clearFlag=%x color=%x depth=%d stencil=%x cRects=%d\n", cid, clearFlag, color, (uint32_t)(depth * 100.0), stencil, cRects));
4662
4663 if ( cid >= pState->cContexts
4664 || pState->papContexts[cid]->id != cid)
4665 {
4666 Log(("vmsvga3dCommandClear invalid context id!\n"));
4667 return VERR_INVALID_PARAMETER;
4668 }
4669 pContext = pState->papContexts[cid];
4670
4671 if (clearFlag & SVGA3D_CLEAR_COLOR)
4672 clearFlagD3D |= D3DCLEAR_TARGET;
4673 if (clearFlag & SVGA3D_CLEAR_STENCIL)
4674 clearFlagD3D |= D3DCLEAR_STENCIL;
4675 if (clearFlag & SVGA3D_CLEAR_DEPTH)
4676 clearFlagD3D |= D3DCLEAR_ZBUFFER;
4677
4678 if (cRects)
4679 {
4680 pRectD3D = (D3DRECT *)RTMemAlloc(sizeof(D3DRECT) * cRects);
4681 AssertReturn(pRectD3D, VERR_NO_MEMORY);
4682
4683 for (unsigned i=0; i < cRects; i++)
4684 {
4685 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));
4686 pRectD3D[i].x1 = pRect[i].x;
4687 pRectD3D[i].y1 = pRect[i].y;
4688 pRectD3D[i].x2 = pRect[i].x + pRect[i].w; /* exclusive */
4689 pRectD3D[i].y2 = pRect[i].y + pRect[i].h; /* exclusive */
4690 }
4691 }
4692
4693 hr = pContext->pDevice->Clear(cRects, pRectD3D, clearFlagD3D, (D3DCOLOR)color, depth, stencil);
4694 if (pRectD3D)
4695 RTMemFree(pRectD3D);
4696
4697 AssertMsgReturn(hr == D3D_OK, ("vmsvga3dCommandClear: Clear failed with %x\n", hr), VERR_INTERNAL_ERROR);
4698
4699 /* Make sure we can track drawing usage of active render targets. */
4700 if (pContext->sidRenderTarget != SVGA3D_INVALID_ID)
4701 vmsvga3dSurfaceTrackUsageById(pState, pContext, pContext->sidRenderTarget);
4702
4703 return VINF_SUCCESS;
4704}
4705
4706/* Convert VMWare vertex declaration to its D3D equivalent. */
4707int vmsvga3dVertexDecl2D3D(SVGA3dVertexArrayIdentity &identity, D3DVERTEXELEMENT9 *pVertexElement)
4708{
4709 /* usage, method and type are identical; make sure. */
4710 AssertCompile(SVGA3D_DECLTYPE_FLOAT1 == D3DDECLTYPE_FLOAT1);
4711 AssertCompile(SVGA3D_DECLTYPE_FLOAT16_4 == D3DDECLTYPE_FLOAT16_4);
4712 AssertCompile(SVGA3D_DECLMETHOD_DEFAULT == D3DDECLMETHOD_DEFAULT);
4713 AssertCompile(SVGA3D_DECLMETHOD_LOOKUPPRESAMPLED == D3DDECLMETHOD_LOOKUPPRESAMPLED);
4714 AssertCompile(D3DDECLUSAGE_POSITION == SVGA3D_DECLUSAGE_POSITION);
4715 AssertCompile(D3DDECLUSAGE_SAMPLE == SVGA3D_DECLUSAGE_SAMPLE);
4716
4717 pVertexElement->Stream = 0;
4718 pVertexElement->Offset = 0;
4719 pVertexElement->Type = identity.type;
4720 pVertexElement->Method = identity.method;
4721 pVertexElement->Usage = identity.usage;
4722 pVertexElement->UsageIndex = identity.usageIndex;
4723 return VINF_SUCCESS;
4724}
4725
4726/* Convert VMWare primitive type to its D3D equivalent. */
4727int vmsvga3dPrimitiveType2D3D(SVGA3dPrimitiveType PrimitiveType, D3DPRIMITIVETYPE *pPrimitiveTypeD3D)
4728{
4729 switch (PrimitiveType)
4730 {
4731 case SVGA3D_PRIMITIVE_TRIANGLELIST:
4732 *pPrimitiveTypeD3D = D3DPT_TRIANGLELIST;
4733 break;
4734 case SVGA3D_PRIMITIVE_POINTLIST:
4735 *pPrimitiveTypeD3D = D3DPT_POINTLIST;
4736 break;
4737 case SVGA3D_PRIMITIVE_LINELIST:
4738 *pPrimitiveTypeD3D = D3DPT_LINELIST;
4739 break;
4740 case SVGA3D_PRIMITIVE_LINESTRIP:
4741 *pPrimitiveTypeD3D = D3DPT_LINESTRIP;
4742 break;
4743 case SVGA3D_PRIMITIVE_TRIANGLESTRIP:
4744 *pPrimitiveTypeD3D = D3DPT_TRIANGLESTRIP;
4745 break;
4746 case SVGA3D_PRIMITIVE_TRIANGLEFAN:
4747 *pPrimitiveTypeD3D = D3DPT_TRIANGLEFAN;
4748 break;
4749 default:
4750 return VERR_INVALID_PARAMETER;
4751 }
4752 return VINF_SUCCESS;
4753}
4754
4755int vmsvga3dDrawPrimitivesProcessVertexDecls(PVMSVGA3DSTATE pState, PVMSVGA3DCONTEXT pContext, uint32_t numVertexDecls, SVGA3dVertexDecl *pVertexDecl, uint32_t idStream, D3DVERTEXELEMENT9 *pVertexElement)
4756{
4757 HRESULT hr;
4758 int rc;
4759 uint32_t uVertexMinOffset = 0xffffffff;
4760 uint32_t uVertexMaxOffset = 0;
4761
4762 /* Create a vertex declaration array */
4763 for (unsigned iVertex = 0; iVertex < numVertexDecls; iVertex++)
4764 {
4765 unsigned sidVertex = pVertexDecl[iVertex].array.surfaceId;
4766 PVMSVGA3DSURFACE pVertexSurface;
4767
4768 AssertReturn(sidVertex < SVGA3D_MAX_SURFACE_IDS, VERR_INVALID_PARAMETER);
4769 AssertReturn(sidVertex < pState->cSurfaces && pState->papSurfaces[sidVertex]->id == sidVertex, VERR_INVALID_PARAMETER);
4770
4771 pVertexSurface = pState->papSurfaces[sidVertex];
4772 Log(("vmsvga3dDrawPrimitives: vertex surface %x stream %d\n", sidVertex, idStream));
4773 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));
4774
4775 rc = vmsvga3dVertexDecl2D3D(pVertexDecl[iVertex].identity, &pVertexElement[iVertex]);
4776 AssertRCReturn(rc, rc);
4777 pVertexElement[iVertex].Stream = idStream;
4778
4779#ifdef LOG_ENABLED
4780 if (pVertexDecl[iVertex].array.stride == 0)
4781 Log(("vmsvga3dDrawPrimitives: stride == 0! Can be valid\n"));
4782#endif
4783
4784 /* Find the min and max vertex offset to determine the right base offset to use in the vertex declaration. */
4785 if (pVertexDecl[iVertex].array.offset > uVertexMaxOffset)
4786 uVertexMaxOffset = pVertexDecl[iVertex].array.offset;
4787 if (pVertexDecl[iVertex].array.offset < uVertexMinOffset)
4788 uVertexMinOffset = pVertexDecl[iVertex].array.offset;
4789
4790 if (!pVertexSurface->u.pVertexBuffer)
4791 {
4792 Assert(iVertex == 0);
4793
4794 Log(("vmsvga3dDrawPrimitives: create vertex buffer fDirty=%d\n", pVertexSurface->fDirty));
4795 hr = pContext->pDevice->CreateVertexBuffer(pVertexSurface->pMipmapLevels[0].cbSurface,
4796 D3DUSAGE_DYNAMIC | D3DUSAGE_WRITEONLY /* possible severe performance penalty otherwise (according to d3d debug output) */,
4797 0, /* non-FVF */
4798 D3DPOOL_DEFAULT,
4799 &pVertexSurface->u.pVertexBuffer,
4800 NULL);
4801 AssertMsgReturn(hr == D3D_OK, ("vmsvga3dDrawPrimitives: CreateVertexBuffer failed with %x\n", hr), VERR_INTERNAL_ERROR);
4802
4803 pVertexSurface->idAssociatedContext = pContext->id;
4804
4805 if (pVertexSurface->fDirty)
4806 {
4807 void *pData;
4808
4809 hr = pVertexSurface->u.pVertexBuffer->Lock(0, 0, &pData, 0);
4810 AssertMsgReturn(hr == D3D_OK, ("vmsvga3dDrawPrimitives: Lock vertex failed with %x\n", hr), VERR_INTERNAL_ERROR);
4811
4812 memcpy(pData, pVertexSurface->pMipmapLevels[0].pSurfaceData, pVertexSurface->pMipmapLevels[0].cbSurface);
4813
4814 hr = pVertexSurface->u.pVertexBuffer->Unlock();
4815 AssertMsgReturn(hr == D3D_OK, ("vmsvga3dDrawPrimitives: Unlock vertex failed with %x\n", hr), VERR_INTERNAL_ERROR);
4816 pVertexSurface->pMipmapLevels[0].fDirty = false;
4817 pVertexSurface->fDirty = false;
4818 }
4819 pVertexSurface->flags |= SVGA3D_SURFACE_HINT_VERTEXBUFFER;
4820 }
4821 else
4822 Assert(pVertexSurface->fDirty == false);
4823 }
4824
4825 /* Set the right vertex offset values for each declaration. */
4826 for (unsigned iVertex = 0; iVertex < numVertexDecls; iVertex++)
4827 {
4828 pVertexElement[iVertex].Offset = pVertexDecl[iVertex].array.offset - uVertexMinOffset;
4829#ifdef LOG_ENABLED
4830 if (pVertexElement[iVertex].Offset >= pVertexDecl[0].array.stride)
4831 Log(("vmsvga3dDrawPrimitives: WARNING: offset > stride!!\n"));
4832#endif
4833
4834 Log(("vmsvga3dDrawPrimitives: vertex %d offset = %d (stride %d) (min=%d max=%d)\n", iVertex, pVertexDecl[iVertex].array.offset, pVertexDecl[iVertex].array.stride, uVertexMinOffset, uVertexMaxOffset));
4835 }
4836
4837 PVMSVGA3DSURFACE pVertexSurface;
4838 unsigned sidVertex = pVertexDecl[0].array.surfaceId;
4839 unsigned strideVertex = pVertexDecl[0].array.stride;
4840
4841 pVertexSurface = pState->papSurfaces[sidVertex];
4842
4843 Log(("vmsvga3dDrawPrimitives: SetStreamSource %d min offset=%d stride=%d\n", idStream, uVertexMinOffset, strideVertex));
4844 hr = pContext->pDevice->SetStreamSource(idStream,
4845 pVertexSurface->u.pVertexBuffer,
4846 uVertexMinOffset,
4847 strideVertex);
4848
4849 AssertMsgReturn(hr == D3D_OK, ("vmsvga3dDrawPrimitives: SetStreamSource failed with %x\n", hr), VERR_INTERNAL_ERROR);
4850 return VINF_SUCCESS;
4851}
4852
4853int vmsvga3dDrawPrimitives(PVGASTATE pThis, uint32_t cid, uint32_t numVertexDecls, SVGA3dVertexDecl *pVertexDecl,
4854 uint32_t numRanges, SVGA3dPrimitiveRange *pRange,
4855 uint32_t cVertexDivisor, SVGA3dVertexDivisor *pVertexDivisor)
4856{
4857 PVMSVGA3DCONTEXT pContext;
4858 PVMSVGA3DSTATE pState = pThis->svga.p3dState;
4859 AssertReturn(pState, VERR_INTERNAL_ERROR);
4860 int rc;
4861 HRESULT hr;
4862 uint32_t iCurrentVertex, iCurrentStreamId;
4863 IDirect3DVertexDeclaration9 *pVertexDeclD3D = NULL;
4864 D3DVERTEXELEMENT9 *pVertexElement = NULL;
4865 D3DVERTEXELEMENT9 VertexEnd = D3DDECL_END();
4866
4867 Log(("vmsvga3dDrawPrimitives %x numVertexDecls=%d numRanges=%d, cVertexDivisor=%d\n", cid, numVertexDecls, numRanges, cVertexDivisor));
4868
4869 AssertReturn(numVertexDecls && numVertexDecls <= SVGA3D_MAX_VERTEX_ARRAYS, VERR_INVALID_PARAMETER);
4870 AssertReturn(numRanges && numRanges <= SVGA3D_MAX_DRAW_PRIMITIVE_RANGES, VERR_INVALID_PARAMETER);
4871 AssertReturn(!cVertexDivisor || cVertexDivisor == numVertexDecls, VERR_INVALID_PARAMETER);
4872 /* @todo */
4873 Assert(!cVertexDivisor);
4874
4875 if ( cid >= pState->cContexts
4876 || pState->papContexts[cid]->id != cid)
4877 {
4878 Log(("vmsvga3dDrawPrimitives invalid context id!\n"));
4879 return VERR_INVALID_PARAMETER;
4880 }
4881
4882 pContext = pState->papContexts[cid];
4883
4884 /* Begin a scene before rendering anything. */
4885 hr = pContext->pDevice->BeginScene();
4886 AssertMsgReturn(hr == D3D_OK, ("BeginScene failed with %x\n", hr), VERR_INTERNAL_ERROR);
4887
4888 pVertexElement = (D3DVERTEXELEMENT9 *)RTMemAllocZ(sizeof(D3DVERTEXELEMENT9) * (numVertexDecls + 1));
4889 if (!pVertexElement)
4890 {
4891 Assert(pVertexElement);
4892 rc = VERR_INTERNAL_ERROR;
4893 goto internal_error;
4894 }
4895
4896 /* Process all vertex declarations. Each vertex buffer is represented by one stream source id. */
4897 iCurrentVertex = 0;
4898 iCurrentStreamId = 0;
4899 while (iCurrentVertex < numVertexDecls)
4900 {
4901 uint32_t sidVertex = SVGA_ID_INVALID;
4902 uint32_t iVertex;
4903 uint32_t uVertexMinOffset = 0xffffffff;
4904
4905 for (iVertex = iCurrentVertex; iVertex < numVertexDecls; iVertex++)
4906 {
4907 if ( ( sidVertex != SVGA_ID_INVALID
4908 && pVertexDecl[iVertex].array.surfaceId != sidVertex
4909 )
4910 /* We must put vertex declarations that start at a different element in another stream as d3d only handles offsets < stride. */
4911 || ( uVertexMinOffset != 0xffffffff
4912 && pVertexDecl[iVertex].array.offset >= uVertexMinOffset + pVertexDecl[iCurrentVertex].array.stride
4913 )
4914 )
4915 break;
4916 sidVertex = pVertexDecl[iVertex].array.surfaceId;
4917
4918 if (uVertexMinOffset > pVertexDecl[iVertex].array.offset)
4919 uVertexMinOffset = pVertexDecl[iVertex].array.offset;
4920 }
4921
4922 rc = vmsvga3dDrawPrimitivesProcessVertexDecls(pState, pContext, iVertex - iCurrentVertex, &pVertexDecl[iCurrentVertex], iCurrentStreamId, &pVertexElement[iCurrentVertex]);
4923 if (RT_FAILURE(rc))
4924 goto internal_error;
4925
4926 iCurrentVertex = iVertex;
4927 iCurrentStreamId++;
4928 }
4929
4930 /* Mark the end. */
4931 memcpy(&pVertexElement[numVertexDecls], &VertexEnd, sizeof(VertexEnd));
4932
4933 hr = pContext->pDevice->CreateVertexDeclaration(&pVertexElement[0],
4934 &pVertexDeclD3D);
4935 if (hr != D3D_OK)
4936 {
4937 AssertMsg(hr == D3D_OK, ("vmsvga3dDrawPrimitives: CreateVertexDeclaration failed with %x\n", hr));
4938 rc = VERR_INTERNAL_ERROR;
4939 goto internal_error;
4940 }
4941
4942 hr = pContext->pDevice->SetVertexDeclaration(pVertexDeclD3D);
4943 if (hr != D3D_OK)
4944 {
4945 AssertMsg(hr == D3D_OK, ("vmsvga3dDrawPrimitives: SetVertexDeclaration failed with %x\n", hr));
4946 rc = VERR_INTERNAL_ERROR;
4947 goto internal_error;
4948 }
4949
4950 /* Now draw the primitives. */
4951 for (unsigned iPrimitive = 0; iPrimitive < numRanges; iPrimitive++)
4952 {
4953 D3DPRIMITIVETYPE PrimitiveTypeD3D;
4954 unsigned sidIndex = pRange[iPrimitive].indexArray.surfaceId;
4955 PVMSVGA3DSURFACE pIndexSurface = NULL;
4956
4957 Log(("Primitive %d: type %s\n", iPrimitive, vmsvga3dPrimitiveType2String(pRange[iPrimitive].primType)));
4958 rc = vmsvga3dPrimitiveType2D3D(pRange[iPrimitive].primType, &PrimitiveTypeD3D);
4959 if (RT_FAILURE(rc))
4960 {
4961 AssertRC(rc);
4962 goto internal_error;
4963 }
4964
4965 /* Triangle strips or fans with just one primitive don't make much sense and are identical to triangle lists.
4966 * Workaround for NVidia driver crash when encountering some of these.
4967 */
4968 if ( pRange[iPrimitive].primitiveCount == 1
4969 && ( PrimitiveTypeD3D == D3DPT_TRIANGLESTRIP
4970 || PrimitiveTypeD3D == D3DPT_TRIANGLEFAN))
4971 PrimitiveTypeD3D = D3DPT_TRIANGLELIST;
4972
4973 if (sidIndex != SVGA3D_INVALID_ID)
4974 {
4975 AssertMsg(pRange[iPrimitive].indexWidth == sizeof(uint32_t) || pRange[iPrimitive].indexWidth == sizeof(uint16_t), ("Unsupported primitive width %d\n", pRange[iPrimitive].indexWidth));
4976
4977 if ( sidIndex >= SVGA3D_MAX_SURFACE_IDS
4978 || sidIndex >= pState->cSurfaces
4979 || pState->papSurfaces[sidIndex]->id != sidIndex)
4980 {
4981 Assert(sidIndex < SVGA3D_MAX_SURFACE_IDS);
4982 Assert(sidIndex < pState->cSurfaces && pState->papSurfaces[sidIndex]->id == sidIndex);
4983 rc = VERR_INVALID_PARAMETER;
4984 goto internal_error;
4985 }
4986 pIndexSurface = pState->papSurfaces[sidIndex];
4987 Log(("vmsvga3dDrawPrimitives: index surface %x\n", sidIndex));
4988
4989 if (!pIndexSurface->u.pIndexBuffer)
4990 {
4991 Log(("vmsvga3dDrawPrimitives: create index buffer fDirty=%d\n", pIndexSurface->fDirty));
4992
4993 hr = pContext->pDevice->CreateIndexBuffer(pIndexSurface->pMipmapLevels[0].cbSurface,
4994 D3DUSAGE_DYNAMIC | D3DUSAGE_WRITEONLY /* possible severe performance penalty otherwise (according to d3d debug output */,
4995 (pRange[iPrimitive].indexWidth == sizeof(uint16_t)) ? D3DFMT_INDEX16 : D3DFMT_INDEX32,
4996 D3DPOOL_DEFAULT,
4997 &pIndexSurface->u.pIndexBuffer,
4998 NULL);
4999 if (hr != D3D_OK)
5000 {
5001 AssertMsg(hr == D3D_OK, ("vmsvga3dDrawPrimitives: CreateIndexBuffer failed with %x\n", hr));
5002 rc = VERR_INTERNAL_ERROR;
5003 goto internal_error;
5004 }
5005
5006 if (pIndexSurface->fDirty)
5007 {
5008 void *pData;
5009
5010 Log(("vmsvga3dDrawPrimitives: sync index buffer\n"));
5011
5012 hr = pIndexSurface->u.pIndexBuffer->Lock(0, 0, &pData, 0);
5013 AssertMsg(hr == D3D_OK, ("vmsvga3dDrawPrimitives: Lock vertex failed with %x\n", hr));
5014
5015 memcpy(pData, pIndexSurface->pMipmapLevels[0].pSurfaceData, pIndexSurface->pMipmapLevels[0].cbSurface);
5016
5017 hr = pIndexSurface->u.pIndexBuffer->Unlock();
5018 AssertMsg(hr == D3D_OK, ("vmsvga3dDrawPrimitives: Unlock vertex failed with %x\n", hr));
5019
5020 pIndexSurface->pMipmapLevels[0].fDirty = false;
5021 pIndexSurface->fDirty = false;
5022 }
5023 pIndexSurface->flags |= SVGA3D_SURFACE_HINT_INDEXBUFFER;
5024 }
5025 else
5026 Assert(pIndexSurface->fDirty == false);
5027
5028 hr = pContext->pDevice->SetIndices(pIndexSurface->u.pIndexBuffer);
5029 AssertMsg(hr == D3D_OK, ("vmsvga3dDrawPrimitives: SetIndices vertex failed with %x\n", hr));
5030 }
5031 else
5032 {
5033 hr = pContext->pDevice->SetIndices(NULL);
5034 AssertMsg(hr == D3D_OK, ("vmsvga3dDrawPrimitives: SetIndices vertex (NULL) failed with %x\n", hr));
5035 }
5036
5037 PVMSVGA3DSURFACE pVertexSurface;
5038 unsigned sidVertex = pVertexDecl[0].array.surfaceId;
5039 unsigned strideVertex = pVertexDecl[0].array.stride;
5040
5041 pVertexSurface = pState->papSurfaces[sidVertex];
5042
5043 if (!pIndexSurface)
5044 {
5045 /* Render without an index buffer */
5046 Log(("DrawPrimitive %x primitivecount=%d index index bias=%d stride=%d\n", PrimitiveTypeD3D, pRange[iPrimitive].primitiveCount, pRange[iPrimitive].indexBias, strideVertex));
5047
5048 hr = pContext->pDevice->DrawPrimitive(PrimitiveTypeD3D,
5049 pRange[iPrimitive].indexBias,
5050 pRange[iPrimitive].primitiveCount);
5051 if (hr != D3D_OK)
5052 {
5053 AssertMsg(hr == D3D_OK, ("vmsvga3dDrawPrimitives: DrawPrimitive failed with %x\n", hr));
5054 rc = VERR_INTERNAL_ERROR;
5055 goto internal_error;
5056 }
5057 }
5058 else
5059 {
5060 Assert(pRange[iPrimitive].indexBias >= 0); /* @todo */
5061
5062 UINT numVertices;
5063
5064 if (pVertexDecl[0].rangeHint.last)
5065 numVertices = pVertexDecl[0].rangeHint.last - pVertexDecl[0].rangeHint.first + 1;
5066 else
5067 numVertices = pVertexSurface->pMipmapLevels[0].cbSurface / strideVertex - pVertexDecl[0].array.offset / strideVertex - pVertexDecl[0].rangeHint.first - pRange[iPrimitive].indexBias;
5068
5069 /* Render with an index buffer */
5070 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));
5071
5072 hr = pContext->pDevice->DrawIndexedPrimitive(PrimitiveTypeD3D,
5073 pRange[iPrimitive].indexBias, /* BaseVertexIndex */
5074 0, /* MinVertexIndex */
5075 numVertices,
5076 pRange[iPrimitive].indexArray.offset / pRange[iPrimitive].indexWidth, /* StartIndex */
5077 pRange[iPrimitive].primitiveCount);
5078 if (hr != D3D_OK)
5079 {
5080 AssertMsg(hr == D3D_OK, ("vmsvga3dDrawPrimitives: DrawIndexedPrimitive failed with %x\n", hr));
5081 rc = VERR_INTERNAL_ERROR;
5082 goto internal_error;
5083 }
5084 }
5085 }
5086 pVertexDeclD3D->Release();
5087 RTMemFree(pVertexElement);
5088
5089 hr = pContext->pDevice->EndScene();
5090 AssertMsgReturn(hr == D3D_OK, ("EndScene failed with %x\n", hr), VERR_INTERNAL_ERROR);
5091
5092 /* Clear streams above 1 as they might accidentally be reused in the future. */
5093 if (iCurrentStreamId > 1)
5094 {
5095 for (uint32_t i = 1; i < iCurrentStreamId; i++)
5096 {
5097 Log(("vmsvga3dDrawPrimitives: clear stream %d\n", i));
5098 hr = pContext->pDevice->SetStreamSource(i, NULL, 0, 0);
5099 AssertMsgReturn(hr == D3D_OK, ("vmsvga3dDrawPrimitives: SetStreamSource failed with %x\n", hr), VERR_INTERNAL_ERROR);
5100 }
5101 }
5102
5103#if 0 /* Flush queue */
5104 {
5105 IDirect3DQuery9 *pQuery;
5106 hr = pContext->pDevice->CreateQuery(D3DQUERYTYPE_EVENT, &pQuery);
5107 AssertMsgReturn(hr == D3D_OK, ("vmsvga3dSurfaceTrackUsage: CreateQuery failed with %x\n", hr), VERR_INTERNAL_ERROR);
5108
5109 hr = pQuery->Issue(D3DISSUE_END);
5110 AssertMsgReturn(hr == D3D_OK, ("vmsvga3dSurfaceTrackUsage: Issue failed with %x\n", hr), VERR_INTERNAL_ERROR);
5111 while (true)
5112 {
5113 hr = pQuery->GetData(NULL, 0, D3DGETDATA_FLUSH);
5114 if (hr != S_FALSE) break;
5115
5116 RTThreadSleep(1);
5117 }
5118 AssertMsgReturn(hr == S_OK, ("vmsvga3dSurfaceFinishDrawing: GetData failed with %x\n", hr), VERR_INTERNAL_ERROR);
5119
5120 pQuery->Release();
5121 }
5122#endif
5123
5124 /* Make sure we can track drawing usage of active render targets and textures. */
5125 vmsvga3dContextTrackUsage(pThis, pContext);
5126
5127#ifdef DEBUG_GFX_WINDOW
5128 if (pContext->aSidActiveTexture[0] == 0x62)
5129//// if (pContext->sidActiveTexture == 0x3d)
5130 {
5131 SVGA3dCopyRect rect;
5132
5133 rect.srcx = rect.srcy = rect.x = rect.y = 0;
5134 rect.w = 800;
5135 rect.h = 600;
5136 vmsvga3dCommandPresent(pThis, pContext->sidRenderTarget /*pContext->aSidActiveTexture[0] */, 0, &rect);
5137 }
5138#endif
5139 return rc;
5140
5141internal_error:
5142 if (pVertexDeclD3D)
5143 pVertexDeclD3D->Release();
5144 if (pVertexElement)
5145 RTMemFree(pVertexElement);
5146
5147 hr = pContext->pDevice->EndScene();
5148 AssertMsgReturn(hr == D3D_OK, ("EndScene failed with %x\n", hr), VERR_INTERNAL_ERROR);
5149
5150 return rc;
5151}
5152
5153int vmsvga3dSetScissorRect(PVGASTATE pThis, uint32_t cid, SVGA3dRect *pRect)
5154{
5155 HRESULT hr;
5156 RECT rect;
5157 PVMSVGA3DCONTEXT pContext;
5158 PVMSVGA3DSTATE pState = pThis->svga.p3dState;
5159 AssertReturn(pState, VERR_NO_MEMORY);
5160
5161 Log(("vmsvga3dSetScissorRect %x (%d,%d)(%d,%d)\n", cid, pRect->x, pRect->y, pRect->w, pRect->h));
5162
5163 if ( cid >= pState->cContexts
5164 || pState->papContexts[cid]->id != cid)
5165 {
5166 Log(("vmsvga3dSetScissorRect invalid context id!\n"));
5167 return VERR_INVALID_PARAMETER;
5168 }
5169 pContext = pState->papContexts[cid];
5170
5171 /* Store for vm state save/restore. */
5172 pContext->state.u32UpdateFlags |= VMSVGA3D_UPDATE_SCISSORRECT;
5173 pContext->state.RectScissor = *pRect;
5174
5175 rect.left = pRect->x;
5176 rect.top = pRect->y;
5177 rect.right = rect.left + pRect->w; /* exclusive */
5178 rect.bottom = rect.top + pRect->h; /* exclusive */
5179
5180 hr = pContext->pDevice->SetScissorRect(&rect);
5181 AssertMsgReturn(hr == D3D_OK, ("vmsvga3dSetScissorRect: SetScissorRect failed with %x\n", hr), VERR_INTERNAL_ERROR);
5182
5183 return VINF_SUCCESS;
5184}
5185
5186
5187int vmsvga3dShaderDefine(PVGASTATE pThis, uint32_t cid, uint32_t shid, SVGA3dShaderType type, uint32_t cbData, uint32_t *pShaderData)
5188{
5189 HRESULT hr;
5190 PVMSVGA3DCONTEXT pContext;
5191 PVMSVGA3DSHADER pShader;
5192 PVMSVGA3DSTATE pState = pThis->svga.p3dState;
5193 AssertReturn(pState, VERR_NO_MEMORY);
5194
5195 Log(("vmsvga3dShaderDefine %x shid=%x type=%s cbData=%x\n", cid, shid, (type == SVGA3D_SHADERTYPE_VS) ? "VERTEX" : "PIXEL", cbData));
5196 Log3(("shader code:\n%.*Rhxd\n", cbData, pShaderData));
5197
5198 if ( cid >= pState->cContexts
5199 || pState->papContexts[cid]->id != cid)
5200 {
5201 Log(("vmsvga3dShaderDefine invalid context id!\n"));
5202 return VERR_INVALID_PARAMETER;
5203 }
5204 pContext = pState->papContexts[cid];
5205
5206 AssertReturn(shid < SVGA3D_MAX_SHADER_IDS, VERR_INVALID_PARAMETER);
5207 if (type == SVGA3D_SHADERTYPE_VS)
5208 {
5209 if (shid >= pContext->cVertexShaders)
5210 {
5211 void *pvNew = RTMemRealloc(pContext->paVertexShader, sizeof(VMSVGA3DSHADER) * (shid + 1));
5212 AssertReturn(pvNew, VERR_NO_MEMORY);
5213 pContext->paVertexShader = (PVMSVGA3DSHADER)pvNew;
5214 memset(&pContext->paVertexShader[pContext->cVertexShaders], 0, sizeof(VMSVGA3DSHADER) * (shid + 1 - pContext->cVertexShaders));
5215 for (uint32_t i = pContext->cVertexShaders; i < shid + 1; i++)
5216 pContext->paVertexShader[i].id = SVGA3D_INVALID_ID;
5217 pContext->cVertexShaders = shid + 1;
5218 }
5219 /* If one already exists with this id, then destroy it now. */
5220 if (pContext->paVertexShader[shid].id != SVGA3D_INVALID_ID)
5221 vmsvga3dShaderDestroy(pThis, cid, shid, pContext->paVertexShader[shid].type);
5222
5223 pShader = &pContext->paVertexShader[shid];
5224 }
5225 else
5226 {
5227 Assert(type == SVGA3D_SHADERTYPE_PS);
5228 if (shid >= pContext->cPixelShaders)
5229 {
5230 void *pvNew = RTMemRealloc(pContext->paPixelShader, sizeof(VMSVGA3DSHADER) * (shid + 1));
5231 AssertReturn(pvNew, VERR_NO_MEMORY);
5232 pContext->paPixelShader = (PVMSVGA3DSHADER)pvNew;
5233 memset(&pContext->paPixelShader[pContext->cPixelShaders], 0, sizeof(VMSVGA3DSHADER) * (shid + 1 - pContext->cPixelShaders));
5234 for (uint32_t i = pContext->cPixelShaders; i < shid + 1; i++)
5235 pContext->paPixelShader[i].id = SVGA3D_INVALID_ID;
5236 pContext->cPixelShaders = shid + 1;
5237 }
5238 /* If one already exists with this id, then destroy it now. */
5239 if (pContext->paPixelShader[shid].id != SVGA3D_INVALID_ID)
5240 vmsvga3dShaderDestroy(pThis, cid, shid, pContext->paPixelShader[shid].type);
5241
5242 pShader = &pContext->paPixelShader[shid];
5243 }
5244
5245 memset(pShader, 0, sizeof(*pShader));
5246 pShader->id = shid;
5247 pShader->cid = cid;
5248 pShader->type = type;
5249 pShader->cbData = cbData;
5250 pShader->pShaderProgram = RTMemAllocZ(cbData);
5251 AssertReturn(pShader->pShaderProgram, VERR_NO_MEMORY);
5252 memcpy(pShader->pShaderProgram, pShaderData, cbData);
5253
5254#ifdef DUMP_SHADER_DISASSEMBLY
5255 LPD3DXBUFFER pDisassembly;
5256 hr = D3DXDisassembleShader((const DWORD *)pShaderData, FALSE, NULL, &pDisassembly);
5257 if (hr == D3D_OK)
5258 {
5259 Log(("Shader disassembly:\n%s\n", pDisassembly->GetBufferPointer()));
5260 pDisassembly->Release();
5261 }
5262#endif
5263
5264 switch (type)
5265 {
5266 case SVGA3D_SHADERTYPE_VS:
5267 hr = pContext->pDevice->CreateVertexShader((const DWORD *)pShaderData, &pShader->u.pVertexShader);
5268 break;
5269
5270 case SVGA3D_SHADERTYPE_PS:
5271 hr = pContext->pDevice->CreatePixelShader((const DWORD *)pShaderData, &pShader->u.pPixelShader);
5272 break;
5273
5274 default:
5275 AssertFailedReturn(VERR_INVALID_PARAMETER);
5276 }
5277 if (hr != D3D_OK)
5278 {
5279 RTMemFree(pShader->pShaderProgram);
5280 memset(pShader, 0, sizeof(*pShader));
5281 pShader->id = SVGA3D_INVALID_ID;
5282 }
5283
5284 AssertMsgReturn(hr == D3D_OK, ("vmsvga3dShaderDefine: CreateVertex/PixelShader failed with %x\n", hr), VERR_INTERNAL_ERROR);
5285 return VINF_SUCCESS;
5286}
5287
5288int vmsvga3dShaderDestroy(PVGASTATE pThis, uint32_t cid, uint32_t shid, SVGA3dShaderType type)
5289{
5290 PVMSVGA3DCONTEXT pContext;
5291 PVMSVGA3DSTATE pState = pThis->svga.p3dState;
5292 AssertReturn(pState, VERR_NO_MEMORY);
5293 PVMSVGA3DSHADER pShader = NULL;
5294
5295 Log(("vmsvga3dShaderDestroy %x shid=%x type=%s\n", cid, shid, (type == SVGA3D_SHADERTYPE_VS) ? "VERTEX" : "PIXEL"));
5296
5297 if ( cid >= pState->cContexts
5298 || pState->papContexts[cid]->id != cid)
5299 {
5300 Log(("vmsvga3dShaderDestroy invalid context id!\n"));
5301 return VERR_INVALID_PARAMETER;
5302 }
5303 pContext = pState->papContexts[cid];
5304
5305 if (type == SVGA3D_SHADERTYPE_VS)
5306 {
5307 if ( shid < pContext->cVertexShaders
5308 && pContext->paVertexShader[shid].id == shid)
5309 {
5310 pShader = &pContext->paVertexShader[shid];
5311 pShader->u.pVertexShader->Release();
5312 }
5313 }
5314 else
5315 {
5316 Assert(type == SVGA3D_SHADERTYPE_PS);
5317 if ( shid < pContext->cPixelShaders
5318 && pContext->paPixelShader[shid].id == shid)
5319 {
5320 pShader = &pContext->paPixelShader[shid];
5321 pShader->u.pPixelShader->Release();
5322 }
5323 }
5324
5325 if (pShader)
5326 {
5327 if (pShader->pShaderProgram)
5328 RTMemFree(pShader->pShaderProgram);
5329
5330 memset(pShader, 0, sizeof(*pShader));
5331 pShader->id = SVGA3D_INVALID_ID;
5332 }
5333 else
5334 AssertFailedReturn(VERR_INVALID_PARAMETER);
5335
5336 return VINF_SUCCESS;
5337}
5338
5339int vmsvga3dShaderSet(PVGASTATE pThis, PVMSVGA3DCONTEXT pContext, uint32_t cid, SVGA3dShaderType type, uint32_t shid)
5340{
5341 PVMSVGA3DSTATE pState = pThis->svga.p3dState;
5342 AssertReturn(pState, VERR_NO_MEMORY);
5343 HRESULT hr;
5344
5345 Log(("vmsvga3dShaderSet %x type=%s shid=%d\n", cid, (type == SVGA3D_SHADERTYPE_VS) ? "VERTEX" : "PIXEL", shid));
5346
5347 NOREF(pContext);
5348 if ( cid >= pState->cContexts
5349 || pState->papContexts[cid]->id != cid)
5350 {
5351 Log(("vmsvga3dShaderSet invalid context id!\n"));
5352 return VERR_INVALID_PARAMETER;
5353 }
5354 pContext = pState->papContexts[cid];
5355
5356 if (type == SVGA3D_SHADERTYPE_VS)
5357 {
5358 /* Save for vm state save/restore. */
5359 pContext->state.shidVertex = shid;
5360 pContext->state.u32UpdateFlags |= VMSVGA3D_UPDATE_VERTEXSHADER;
5361
5362 if ( shid < pContext->cVertexShaders
5363 && pContext->paVertexShader[shid].id == shid)
5364 {
5365 PVMSVGA3DSHADER pShader = &pContext->paVertexShader[shid];
5366 Assert(type == pShader->type);
5367
5368 hr = pContext->pDevice->SetVertexShader(pShader->u.pVertexShader);
5369 AssertMsgReturn(hr == D3D_OK, ("vmsvga3dShaderSet: SetVertex/PixelShader failed with %x\n", hr), VERR_INTERNAL_ERROR);
5370 }
5371 else
5372 if (shid == SVGA_ID_INVALID)
5373 {
5374 /* Unselect shader. */
5375 hr = pContext->pDevice->SetVertexShader(NULL);
5376 AssertMsgReturn(hr == D3D_OK, ("vmsvga3dShaderSet: SetVertex/PixelShader failed with %x\n", hr), VERR_INTERNAL_ERROR);
5377 }
5378 else
5379 AssertFailedReturn(VERR_INVALID_PARAMETER);
5380 }
5381 else
5382 {
5383 /* Save for vm state save/restore. */
5384 pContext->state.shidPixel = shid;
5385 pContext->state.u32UpdateFlags |= VMSVGA3D_UPDATE_PIXELSHADER;
5386
5387 Assert(type == SVGA3D_SHADERTYPE_PS);
5388 if ( shid < pContext->cPixelShaders
5389 && pContext->paPixelShader[shid].id == shid)
5390 {
5391 PVMSVGA3DSHADER pShader = &pContext->paPixelShader[shid];
5392 Assert(type == pShader->type);
5393
5394 hr = pContext->pDevice->SetPixelShader(pShader->u.pPixelShader);
5395 AssertMsgReturn(hr == D3D_OK, ("vmsvga3dShaderSet: SetVertex/PixelShader failed with %x\n", hr), VERR_INTERNAL_ERROR);
5396 }
5397 else
5398 if (shid == SVGA_ID_INVALID)
5399 {
5400 /* Unselect shader. */
5401 hr = pContext->pDevice->SetPixelShader(NULL);
5402 AssertMsgReturn(hr == D3D_OK, ("vmsvga3dShaderSet: SetVertex/PixelShader failed with %x\n", hr), VERR_INTERNAL_ERROR);
5403 }
5404 else
5405 AssertFailedReturn(VERR_INVALID_PARAMETER);
5406 }
5407
5408 return VINF_SUCCESS;
5409}
5410
5411int vmsvga3dShaderSetConst(PVGASTATE pThis, uint32_t cid, uint32_t reg, SVGA3dShaderType type, SVGA3dShaderConstType ctype, uint32_t cRegisters, uint32_t *pValues)
5412{
5413 HRESULT hr;
5414 PVMSVGA3DCONTEXT pContext;
5415 PVMSVGA3DSTATE pState = pThis->svga.p3dState;
5416 AssertReturn(pState, VERR_NO_MEMORY);
5417
5418 Log(("vmsvga3dShaderSetConst %x reg=%x type=%s ctype=%x\n", cid, reg, (type == SVGA3D_SHADERTYPE_VS) ? "VERTEX" : "PIXEL", ctype));
5419
5420 if ( cid >= pState->cContexts
5421 || pState->papContexts[cid]->id != cid)
5422 {
5423 Log(("vmsvga3dShaderSetConst invalid context id!\n"));
5424 return VERR_INVALID_PARAMETER;
5425 }
5426 pContext = pState->papContexts[cid];
5427
5428 for (uint32_t i = 0; i < cRegisters; i++)
5429 {
5430 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]));
5431 vmsvga3dSaveShaderConst(pContext, reg + i, type, ctype, pValues[i*4 + 0], pValues[i*4 + 1], pValues[i*4 + 2], pValues[i*4 + 3]);
5432 }
5433
5434 switch (type)
5435 {
5436 case SVGA3D_SHADERTYPE_VS:
5437 switch (ctype)
5438 {
5439 case SVGA3D_CONST_TYPE_FLOAT:
5440 hr = pContext->pDevice->SetVertexShaderConstantF(reg, (const float *)pValues, cRegisters);
5441 break;
5442
5443 case SVGA3D_CONST_TYPE_INT:
5444 hr = pContext->pDevice->SetVertexShaderConstantI(reg, (const int *)pValues, cRegisters);
5445 break;
5446
5447 case SVGA3D_CONST_TYPE_BOOL:
5448 hr = pContext->pDevice->SetVertexShaderConstantB(reg, (const BOOL *)pValues, cRegisters);
5449 break;
5450
5451 default:
5452 AssertFailedReturn(VERR_INVALID_PARAMETER);
5453 }
5454 AssertMsgReturn(hr == D3D_OK, ("vmsvga3dShaderSetConst: SetVertexShader failed with %x\n", hr), VERR_INTERNAL_ERROR);
5455 break;
5456
5457 case SVGA3D_SHADERTYPE_PS:
5458 switch (ctype)
5459 {
5460 case SVGA3D_CONST_TYPE_FLOAT:
5461 {
5462 hr = pContext->pDevice->SetPixelShaderConstantF(reg, (const float *)pValues, cRegisters);
5463 break;
5464 }
5465
5466 case SVGA3D_CONST_TYPE_INT:
5467 hr = pContext->pDevice->SetPixelShaderConstantI(reg, (const int *)pValues, cRegisters);
5468 break;
5469
5470 case SVGA3D_CONST_TYPE_BOOL:
5471 hr = pContext->pDevice->SetPixelShaderConstantB(reg, (const BOOL *)pValues, cRegisters);
5472 break;
5473
5474 default:
5475 AssertFailedReturn(VERR_INVALID_PARAMETER);
5476 }
5477 AssertMsgReturn(hr == D3D_OK, ("vmsvga3dShaderSetConst: SetPixelShader failed with %x\n", hr), VERR_INTERNAL_ERROR);
5478 break;
5479
5480 default:
5481 AssertFailedReturn(VERR_INVALID_PARAMETER);
5482 }
5483 return VINF_SUCCESS;
5484}
5485
5486
5487int vmsvga3dQueryBegin(PVGASTATE pThis, uint32_t cid, SVGA3dQueryType type)
5488{
5489 AssertFailed();
5490 return VERR_NOT_IMPLEMENTED;
5491}
5492
5493int vmsvga3dQueryEnd(PVGASTATE pThis, uint32_t cid, SVGA3dQueryType type, SVGAGuestPtr guestResult)
5494{
5495 AssertFailed();
5496 return VERR_NOT_IMPLEMENTED;
5497}
5498
5499int vmsvga3dQueryWait(PVGASTATE pThis, uint32_t cid, SVGA3dQueryType type, SVGAGuestPtr guestResult)
5500{
5501 AssertFailed();
5502 return VERR_NOT_IMPLEMENTED;
5503}
5504
5505static void vmsvgaDumpD3DCaps(D3DCAPS9 *pCaps)
5506{
5507 bool const fBufferingSaved = RTLogRelSetBuffering(true /*fBuffered*/);
5508
5509 LogRel(("\nD3D device caps: DevCaps2:\n"));
5510 if (pCaps->DevCaps2 & D3DDEVCAPS2_ADAPTIVETESSRTPATCH)
5511 LogRel((" - D3DDEVCAPS2_ADAPTIVETESSRTPATCH\n"));
5512 if (pCaps->DevCaps2 & D3DDEVCAPS2_ADAPTIVETESSNPATCH)
5513 LogRel((" - D3DDEVCAPS2_ADAPTIVETESSNPATCH\n"));
5514 if (pCaps->DevCaps2 & D3DDEVCAPS2_CAN_STRETCHRECT_FROM_TEXTURES)
5515 LogRel((" - D3DDEVCAPS2_CAN_STRETCHRECT_FROM_TEXTURES\n"));
5516 if (pCaps->DevCaps2 & D3DDEVCAPS2_DMAPNPATCH)
5517 LogRel((" - D3DDEVCAPS2_DMAPNPATCH\n"));
5518 if (pCaps->DevCaps2 & D3DDEVCAPS2_PRESAMPLEDDMAPNPATCH)
5519 LogRel((" - D3DDEVCAPS2_PRESAMPLEDDMAPNPATCH\n"));
5520 if (pCaps->DevCaps2 & D3DDEVCAPS2_STREAMOFFSET)
5521 LogRel((" - D3DDEVCAPS2_STREAMOFFSET\n"));
5522 if (pCaps->DevCaps2 & D3DDEVCAPS2_VERTEXELEMENTSCANSHARESTREAMOFFSET)
5523 LogRel((" - D3DDEVCAPS2_VERTEXELEMENTSCANSHARESTREAMOFFSET\n"));
5524
5525 LogRel(("\nCaps2:\n"));
5526 if (pCaps->Caps2 & D3DCAPS2_CANAUTOGENMIPMAP)
5527 LogRel((" - D3DCAPS2_CANAUTOGENMIPMAP\n"));
5528 if (pCaps->Caps2 & D3DCAPS2_CANCALIBRATEGAMMA)
5529 LogRel((" - D3DCAPS2_CANCALIBRATEGAMMA\n"));
5530 if (pCaps->Caps2 & D3DCAPS2_CANSHARERESOURCE)
5531 LogRel((" - D3DCAPS2_CANSHARERESOURCE\n"));
5532 if (pCaps->Caps2 & D3DCAPS2_CANMANAGERESOURCE)
5533 LogRel((" - D3DCAPS2_CANMANAGERESOURCE\n"));
5534 if (pCaps->Caps2 & D3DCAPS2_DYNAMICTEXTURES)
5535 LogRel((" - D3DCAPS2_DYNAMICTEXTURES\n"));
5536 if (pCaps->Caps2 & D3DCAPS2_FULLSCREENGAMMA)
5537 LogRel((" - D3DCAPS2_FULLSCREENGAMMA\n"));
5538
5539 LogRel(("\nCaps3:\n"));
5540 if (pCaps->Caps3 & D3DCAPS3_ALPHA_FULLSCREEN_FLIP_OR_DISCARD)
5541 LogRel((" - D3DCAPS3_ALPHA_FULLSCREEN_FLIP_OR_DISCARD\n"));
5542 if (pCaps->Caps3 & D3DCAPS3_COPY_TO_VIDMEM)
5543 LogRel((" - D3DCAPS3_COPY_TO_VIDMEM\n"));
5544 if (pCaps->Caps3 & D3DCAPS3_COPY_TO_SYSTEMMEM)
5545 LogRel((" - D3DCAPS3_COPY_TO_SYSTEMMEM\n"));
5546 if (pCaps->Caps3 & D3DCAPS3_DXVAHD)
5547 LogRel((" - D3DCAPS3_DXVAHD\n"));
5548 if (pCaps->Caps3 & D3DCAPS3_LINEAR_TO_SRGB_PRESENTATION)
5549 LogRel((" - D3DCAPS3_LINEAR_TO_SRGB_PRESENTATION\n"));
5550
5551 LogRel(("\nPresentationIntervals:\n"));
5552 if (pCaps->PresentationIntervals & D3DPRESENT_INTERVAL_IMMEDIATE)
5553 LogRel((" - D3DPRESENT_INTERVAL_IMMEDIATE\n"));
5554 if (pCaps->PresentationIntervals & D3DPRESENT_INTERVAL_ONE)
5555 LogRel((" - D3DPRESENT_INTERVAL_ONE\n"));
5556 if (pCaps->PresentationIntervals & D3DPRESENT_INTERVAL_TWO)
5557 LogRel((" - D3DPRESENT_INTERVAL_TWO\n"));
5558 if (pCaps->PresentationIntervals & D3DPRESENT_INTERVAL_THREE)
5559 LogRel((" - D3DPRESENT_INTERVAL_THREE\n"));
5560 if (pCaps->PresentationIntervals & D3DPRESENT_INTERVAL_FOUR)
5561 LogRel((" - D3DPRESENT_INTERVAL_FOUR\n"));
5562
5563 LogRel(("\nDevcaps:\n"));
5564 if (pCaps->DevCaps & D3DDEVCAPS_CANBLTSYSTONONLOCAL)
5565 LogRel((" - D3DDEVCAPS_CANBLTSYSTONONLOCAL\n"));
5566 if (pCaps->DevCaps & D3DDEVCAPS_CANRENDERAFTERFLIP)
5567 LogRel((" - D3DDEVCAPS_CANRENDERAFTERFLIP\n"));
5568 if (pCaps->DevCaps & D3DDEVCAPS_DRAWPRIMITIVES2)
5569 LogRel((" - D3DDEVCAPS_DRAWPRIMITIVES2\n"));
5570 if (pCaps->DevCaps & D3DDEVCAPS_DRAWPRIMITIVES2EX)
5571 LogRel((" - D3DDEVCAPS_DRAWPRIMITIVES2EX\n"));
5572 if (pCaps->DevCaps & D3DDEVCAPS_DRAWPRIMTLVERTEX)
5573 LogRel((" - D3DDEVCAPS_DRAWPRIMTLVERTEX\n"));
5574 if (pCaps->DevCaps & D3DDEVCAPS_EXECUTESYSTEMMEMORY)
5575 LogRel((" - D3DDEVCAPS_EXECUTESYSTEMMEMORY\n"));
5576 if (pCaps->DevCaps & D3DDEVCAPS_EXECUTEVIDEOMEMORY)
5577 LogRel((" - D3DDEVCAPS_EXECUTEVIDEOMEMORY\n"));
5578 if (pCaps->DevCaps & D3DDEVCAPS_HWRASTERIZATION)
5579 LogRel((" - D3DDEVCAPS_HWRASTERIZATION\n"));
5580 if (pCaps->DevCaps & D3DDEVCAPS_HWTRANSFORMANDLIGHT)
5581 LogRel((" - D3DDEVCAPS_HWTRANSFORMANDLIGHT\n"));
5582 if (pCaps->DevCaps & D3DDEVCAPS_NPATCHES)
5583 LogRel((" - D3DDEVCAPS_NPATCHES\n"));
5584 if (pCaps->DevCaps & D3DDEVCAPS_PUREDEVICE)
5585 LogRel((" - D3DDEVCAPS_PUREDEVICE\n"));
5586 if (pCaps->DevCaps & D3DDEVCAPS_QUINTICRTPATCHES)
5587 LogRel((" - D3DDEVCAPS_QUINTICRTPATCHES\n"));
5588 if (pCaps->DevCaps & D3DDEVCAPS_RTPATCHES)
5589 LogRel((" - D3DDEVCAPS_RTPATCHES\n"));
5590 if (pCaps->DevCaps & D3DDEVCAPS_RTPATCHHANDLEZERO)
5591 LogRel((" - D3DDEVCAPS_RTPATCHHANDLEZERO\n"));
5592 if (pCaps->DevCaps & D3DDEVCAPS_SEPARATETEXTUREMEMORIES)
5593 LogRel((" - D3DDEVCAPS_SEPARATETEXTUREMEMORIES\n"));
5594 if (pCaps->DevCaps & D3DDEVCAPS_TEXTURENONLOCALVIDMEM)
5595 LogRel((" - D3DDEVCAPS_TEXTURENONLOCALVIDMEM\n"));
5596 if (pCaps->DevCaps & D3DDEVCAPS_TEXTURESYSTEMMEMORY)
5597 LogRel((" - D3DDEVCAPS_TEXTURESYSTEMMEMORY\n"));
5598 if (pCaps->DevCaps & D3DDEVCAPS_TEXTUREVIDEOMEMORY)
5599 LogRel((" - D3DDEVCAPS_TEXTUREVIDEOMEMORY\n"));
5600 if (pCaps->DevCaps & D3DDEVCAPS_TLVERTEXSYSTEMMEMORY)
5601 LogRel((" - D3DDEVCAPS_TLVERTEXSYSTEMMEMORY\n"));
5602 if (pCaps->DevCaps & D3DDEVCAPS_TLVERTEXVIDEOMEMORY)
5603 LogRel((" - D3DDEVCAPS_TLVERTEXVIDEOMEMORY\n"));
5604
5605 LogRel(("\nTextureCaps:\n"));
5606 if (pCaps->TextureCaps & D3DPTEXTURECAPS_ALPHA)
5607 LogRel((" - D3DPTEXTURECAPS_ALPHA\n"));
5608 if (pCaps->TextureCaps & D3DPTEXTURECAPS_ALPHAPALETTE)
5609 LogRel((" - D3DPTEXTURECAPS_ALPHAPALETTE\n"));
5610 if (pCaps->TextureCaps & D3DPTEXTURECAPS_CUBEMAP)
5611 LogRel((" - D3DPTEXTURECAPS_CUBEMAP\n"));
5612 if (pCaps->TextureCaps & D3DPTEXTURECAPS_CUBEMAP_POW2)
5613 LogRel((" - D3DPTEXTURECAPS_CUBEMAP_POW2\n"));
5614 if (pCaps->TextureCaps & D3DPTEXTURECAPS_MIPCUBEMAP)
5615 LogRel((" - D3DPTEXTURECAPS_MIPCUBEMAP\n"));
5616 if (pCaps->TextureCaps & D3DPTEXTURECAPS_MIPMAP)
5617 LogRel((" - D3DPTEXTURECAPS_MIPMAP\n"));
5618 if (pCaps->TextureCaps & D3DPTEXTURECAPS_MIPVOLUMEMAP)
5619 LogRel((" - D3DPTEXTURECAPS_MIPVOLUMEMAP\n"));
5620 if (pCaps->TextureCaps & D3DPTEXTURECAPS_NONPOW2CONDITIONAL)
5621 LogRel((" - D3DPTEXTURECAPS_NONPOW2CONDITIONAL\n"));
5622 if (pCaps->TextureCaps & D3DPTEXTURECAPS_POW2)
5623 LogRel((" - D3DPTEXTURECAPS_POW2\n"));
5624 if (pCaps->TextureCaps & D3DPTEXTURECAPS_NOPROJECTEDBUMPENV)
5625 LogRel((" - D3DPTEXTURECAPS_NOPROJECTEDBUMPENV\n"));
5626 if (pCaps->TextureCaps & D3DPTEXTURECAPS_PERSPECTIVE)
5627 LogRel((" - D3DPTEXTURECAPS_PERSPECTIVE\n"));
5628 if (pCaps->TextureCaps & D3DPTEXTURECAPS_POW2)
5629 LogRel((" - D3DPTEXTURECAPS_POW2\n"));
5630 if (pCaps->TextureCaps & D3DPTEXTURECAPS_PROJECTED)
5631 LogRel((" - D3DPTEXTURECAPS_PROJECTED\n"));
5632 if (pCaps->TextureCaps & D3DPTEXTURECAPS_SQUAREONLY)
5633 LogRel((" - D3DPTEXTURECAPS_SQUAREONLY\n"));
5634 if (pCaps->TextureCaps & D3DPTEXTURECAPS_TEXREPEATNOTSCALEDBYSIZE)
5635 LogRel((" - D3DPTEXTURECAPS_TEXREPEATNOTSCALEDBYSIZE\n"));
5636 if (pCaps->TextureCaps & D3DPTEXTURECAPS_VOLUMEMAP)
5637 LogRel((" - D3DPTEXTURECAPS_VOLUMEMAP\n"));
5638 if (pCaps->TextureCaps & D3DPTEXTURECAPS_VOLUMEMAP_POW2)
5639 LogRel((" - D3DPTEXTURECAPS_VOLUMEMAP_POW2\n"));
5640
5641 LogRel(("\nTextureFilterCaps\n"));
5642 if (pCaps->TextureFilterCaps & D3DPTFILTERCAPS_CONVOLUTIONMONO)
5643 LogRel((" - D3DPTFILTERCAPS_CONVOLUTIONMONO\n"));
5644 if (pCaps->TextureFilterCaps & D3DPTFILTERCAPS_MAGFPOINT)
5645 LogRel((" - D3DPTFILTERCAPS_MAGFPOINT\n"));
5646 if (pCaps->TextureFilterCaps & D3DPTFILTERCAPS_MAGFLINEAR)
5647 LogRel((" - D3DPTFILTERCAPS_MAGFLINEAR\n"));
5648 if (pCaps->TextureFilterCaps & D3DPTFILTERCAPS_MAGFANISOTROPIC)
5649 LogRel((" - D3DPTFILTERCAPS_MAGFANISOTROPIC\n"));
5650 if (pCaps->TextureFilterCaps & D3DPTFILTERCAPS_MAGFPYRAMIDALQUAD)
5651 LogRel((" - D3DPTFILTERCAPS_MAGFPYRAMIDALQUAD\n"));
5652 if (pCaps->TextureFilterCaps & D3DPTFILTERCAPS_MAGFGAUSSIANQUAD)
5653 LogRel((" - D3DPTFILTERCAPS_MAGFGAUSSIANQUAD\n"));
5654 if (pCaps->TextureFilterCaps & D3DPTFILTERCAPS_MINFPOINT)
5655 LogRel((" - D3DPTFILTERCAPS_MINFPOINT\n"));
5656 if (pCaps->TextureFilterCaps & D3DPTFILTERCAPS_MINFLINEAR)
5657 LogRel((" - D3DPTFILTERCAPS_MINFLINEAR\n"));
5658 if (pCaps->TextureFilterCaps & D3DPTFILTERCAPS_MINFANISOTROPIC)
5659 LogRel((" - D3DPTFILTERCAPS_MINFANISOTROPIC\n"));
5660 if (pCaps->TextureFilterCaps & D3DPTFILTERCAPS_MINFPYRAMIDALQUAD)
5661 LogRel((" - D3DPTFILTERCAPS_MINFPYRAMIDALQUAD\n"));
5662 if (pCaps->TextureFilterCaps & D3DPTFILTERCAPS_MINFGAUSSIANQUAD)
5663 LogRel((" - D3DPTFILTERCAPS_MINFGAUSSIANQUAD\n"));
5664 if (pCaps->TextureFilterCaps & D3DPTFILTERCAPS_MIPFPOINT)
5665 LogRel((" - D3DPTFILTERCAPS_MIPFPOINT\n"));
5666 if (pCaps->TextureFilterCaps & D3DPTFILTERCAPS_MIPFLINEAR)
5667 LogRel((" - D3DPTFILTERCAPS_MIPFLINEAR\n"));
5668
5669 LogRel(("\nCubeTextureFilterCaps\n"));
5670 if (pCaps->CubeTextureFilterCaps & D3DPTFILTERCAPS_CONVOLUTIONMONO)
5671 LogRel((" - D3DPTFILTERCAPS_CONVOLUTIONMONO\n"));
5672 if (pCaps->CubeTextureFilterCaps & D3DPTFILTERCAPS_MAGFPOINT)
5673 LogRel((" - D3DPTFILTERCAPS_MAGFPOINT\n"));
5674 if (pCaps->CubeTextureFilterCaps & D3DPTFILTERCAPS_MAGFLINEAR)
5675 LogRel((" - D3DPTFILTERCAPS_MAGFLINEAR\n"));
5676 if (pCaps->CubeTextureFilterCaps & D3DPTFILTERCAPS_MAGFANISOTROPIC)
5677 LogRel((" - D3DPTFILTERCAPS_MAGFANISOTROPIC\n"));
5678 if (pCaps->CubeTextureFilterCaps & D3DPTFILTERCAPS_MAGFPYRAMIDALQUAD)
5679 LogRel((" - D3DPTFILTERCAPS_MAGFPYRAMIDALQUAD\n"));
5680 if (pCaps->CubeTextureFilterCaps & D3DPTFILTERCAPS_MAGFGAUSSIANQUAD)
5681 LogRel((" - D3DPTFILTERCAPS_MAGFGAUSSIANQUAD\n"));
5682 if (pCaps->CubeTextureFilterCaps & D3DPTFILTERCAPS_MINFPOINT)
5683 LogRel((" - D3DPTFILTERCAPS_MINFPOINT\n"));
5684 if (pCaps->CubeTextureFilterCaps & D3DPTFILTERCAPS_MINFLINEAR)
5685 LogRel((" - D3DPTFILTERCAPS_MINFLINEAR\n"));
5686 if (pCaps->CubeTextureFilterCaps & D3DPTFILTERCAPS_MINFANISOTROPIC)
5687 LogRel((" - D3DPTFILTERCAPS_MINFANISOTROPIC\n"));
5688 if (pCaps->CubeTextureFilterCaps & D3DPTFILTERCAPS_MINFPYRAMIDALQUAD)
5689 LogRel((" - D3DPTFILTERCAPS_MINFPYRAMIDALQUAD\n"));
5690 if (pCaps->CubeTextureFilterCaps & D3DPTFILTERCAPS_MINFGAUSSIANQUAD)
5691 LogRel((" - D3DPTFILTERCAPS_MINFGAUSSIANQUAD\n"));
5692 if (pCaps->CubeTextureFilterCaps & D3DPTFILTERCAPS_MIPFPOINT)
5693 LogRel((" - D3DPTFILTERCAPS_MIPFPOINT\n"));
5694 if (pCaps->CubeTextureFilterCaps & D3DPTFILTERCAPS_MIPFLINEAR)
5695 LogRel((" - D3DPTFILTERCAPS_MIPFLINEAR\n"));
5696
5697 LogRel(("\nVolumeTextureFilterCaps\n"));
5698 if (pCaps->VolumeTextureFilterCaps & D3DPTFILTERCAPS_CONVOLUTIONMONO)
5699 LogRel((" - D3DPTFILTERCAPS_CONVOLUTIONMONO\n"));
5700 if (pCaps->VolumeTextureFilterCaps & D3DPTFILTERCAPS_MAGFPOINT)
5701 LogRel((" - D3DPTFILTERCAPS_MAGFPOINT\n"));
5702 if (pCaps->VolumeTextureFilterCaps & D3DPTFILTERCAPS_MAGFLINEAR)
5703 LogRel((" - D3DPTFILTERCAPS_MAGFLINEAR\n"));
5704 if (pCaps->VolumeTextureFilterCaps & D3DPTFILTERCAPS_MAGFANISOTROPIC)
5705 LogRel((" - D3DPTFILTERCAPS_MAGFANISOTROPIC\n"));
5706 if (pCaps->VolumeTextureFilterCaps & D3DPTFILTERCAPS_MAGFPYRAMIDALQUAD)
5707 LogRel((" - D3DPTFILTERCAPS_MAGFPYRAMIDALQUAD\n"));
5708 if (pCaps->VolumeTextureFilterCaps & D3DPTFILTERCAPS_MAGFGAUSSIANQUAD)
5709 LogRel((" - D3DPTFILTERCAPS_MAGFGAUSSIANQUAD\n"));
5710 if (pCaps->VolumeTextureFilterCaps & D3DPTFILTERCAPS_MINFPOINT)
5711 LogRel((" - D3DPTFILTERCAPS_MINFPOINT\n"));
5712 if (pCaps->VolumeTextureFilterCaps & D3DPTFILTERCAPS_MINFLINEAR)
5713 LogRel((" - D3DPTFILTERCAPS_MINFLINEAR\n"));
5714 if (pCaps->VolumeTextureFilterCaps & D3DPTFILTERCAPS_MINFANISOTROPIC)
5715 LogRel((" - D3DPTFILTERCAPS_MINFANISOTROPIC\n"));
5716 if (pCaps->VolumeTextureFilterCaps & D3DPTFILTERCAPS_MINFPYRAMIDALQUAD)
5717 LogRel((" - D3DPTFILTERCAPS_MINFPYRAMIDALQUAD\n"));
5718 if (pCaps->VolumeTextureFilterCaps & D3DPTFILTERCAPS_MINFGAUSSIANQUAD)
5719 LogRel((" - D3DPTFILTERCAPS_MINFGAUSSIANQUAD\n"));
5720 if (pCaps->VolumeTextureFilterCaps & D3DPTFILTERCAPS_MIPFPOINT)
5721 LogRel((" - D3DPTFILTERCAPS_MIPFPOINT\n"));
5722 if (pCaps->VolumeTextureFilterCaps & D3DPTFILTERCAPS_MIPFLINEAR)
5723 LogRel((" - D3DPTFILTERCAPS_MIPFLINEAR\n"));
5724
5725 LogRel(("\nTextureAddressCaps:\n"));
5726 if (pCaps->TextureAddressCaps & D3DPTADDRESSCAPS_BORDER)
5727 LogRel((" - D3DPTADDRESSCAPS_BORDER\n"));
5728 if (pCaps->TextureAddressCaps & D3DPTADDRESSCAPS_CLAMP)
5729 LogRel((" - D3DPTADDRESSCAPS_CLAMP\n"));
5730 if (pCaps->TextureAddressCaps & D3DPTADDRESSCAPS_INDEPENDENTUV)
5731 LogRel((" - D3DPTADDRESSCAPS_INDEPENDENTUV\n"));
5732 if (pCaps->TextureAddressCaps & D3DPTADDRESSCAPS_MIRROR)
5733 LogRel((" - D3DPTADDRESSCAPS_MIRROR\n"));
5734 if (pCaps->TextureAddressCaps & D3DPTADDRESSCAPS_MIRRORONCE)
5735 LogRel((" - D3DPTADDRESSCAPS_MIRRORONCE\n"));
5736 if (pCaps->TextureAddressCaps & D3DPTADDRESSCAPS_WRAP)
5737 LogRel((" - D3DPTADDRESSCAPS_WRAP\n"));
5738
5739 LogRel(("\nTextureOpCaps:\n"));
5740 if (pCaps->TextureOpCaps & D3DTEXOPCAPS_DISABLE)
5741 LogRel((" - D3DTEXOPCAPS_DISABLE\n"));
5742 if (pCaps->TextureOpCaps & D3DTEXOPCAPS_SELECTARG1)
5743 LogRel((" - D3DTEXOPCAPS_SELECTARG1\n"));
5744 if (pCaps->TextureOpCaps & D3DTEXOPCAPS_SELECTARG2)
5745 LogRel((" - D3DTEXOPCAPS_SELECTARG2\n"));
5746 if (pCaps->TextureOpCaps & D3DTEXOPCAPS_MODULATE)
5747 LogRel((" - D3DTEXOPCAPS_MODULATE\n"));
5748 if (pCaps->TextureOpCaps & D3DTEXOPCAPS_MODULATE2X)
5749 LogRel((" - D3DTEXOPCAPS_MODULATE2X\n"));
5750 if (pCaps->TextureOpCaps & D3DTEXOPCAPS_MODULATE4X)
5751 LogRel((" - D3DTEXOPCAPS_MODULATE4X\n"));
5752 if (pCaps->TextureOpCaps & D3DTEXOPCAPS_ADD)
5753 LogRel((" - D3DTEXOPCAPS_ADD\n"));
5754 if (pCaps->TextureOpCaps & D3DTEXOPCAPS_ADDSIGNED)
5755 LogRel((" - D3DTEXOPCAPS_ADDSIGNED\n"));
5756 if (pCaps->TextureOpCaps & D3DTEXOPCAPS_ADDSIGNED2X)
5757 LogRel((" - D3DTEXOPCAPS_ADDSIGNED2X\n"));
5758 if (pCaps->TextureOpCaps & D3DTEXOPCAPS_SUBTRACT)
5759 LogRel((" - D3DTEXOPCAPS_SUBTRACT\n"));
5760 if (pCaps->TextureOpCaps & D3DTEXOPCAPS_ADDSMOOTH)
5761 LogRel((" - D3DTEXOPCAPS_ADDSMOOTH\n"));
5762 if (pCaps->TextureOpCaps & D3DTEXOPCAPS_BLENDDIFFUSEALPHA)
5763 LogRel((" - D3DTEXOPCAPS_BLENDDIFFUSEALPHA\n"));
5764 if (pCaps->TextureOpCaps & D3DTEXOPCAPS_BLENDTEXTUREALPHA)
5765 LogRel((" - D3DTEXOPCAPS_BLENDTEXTUREALPHA\n"));
5766 if (pCaps->TextureOpCaps & D3DTEXOPCAPS_BLENDFACTORALPHA)
5767 LogRel((" - D3DTEXOPCAPS_BLENDFACTORALPHA\n"));
5768 if (pCaps->TextureOpCaps & D3DTEXOPCAPS_BLENDTEXTUREALPHAPM)
5769 LogRel((" - D3DTEXOPCAPS_BLENDTEXTUREALPHAPM\n"));
5770 if (pCaps->TextureOpCaps & D3DTEXOPCAPS_BLENDCURRENTALPHA)
5771 LogRel((" - D3DTEXOPCAPS_BLENDCURRENTALPHA\n"));
5772 if (pCaps->TextureOpCaps & D3DTEXOPCAPS_PREMODULATE)
5773 LogRel((" - D3DTEXOPCAPS_PREMODULATE\n"));
5774 if (pCaps->TextureOpCaps & D3DTEXOPCAPS_MODULATEALPHA_ADDCOLOR)
5775 LogRel((" - D3DTEXOPCAPS_MODULATEALPHA_ADDCOLOR\n"));
5776 if (pCaps->TextureOpCaps & D3DTEXOPCAPS_MODULATECOLOR_ADDALPHA)
5777 LogRel((" - D3DTEXOPCAPS_MODULATECOLOR_ADDALPHA\n"));
5778 if (pCaps->TextureOpCaps & D3DTEXOPCAPS_MODULATEINVALPHA_ADDCOLOR)
5779 LogRel((" - D3DTEXOPCAPS_MODULATEINVALPHA_ADDCOLOR\n"));
5780 if (pCaps->TextureOpCaps & D3DTEXOPCAPS_MODULATEINVCOLOR_ADDALPHA)
5781 LogRel((" - D3DTEXOPCAPS_MODULATEINVCOLOR_ADDALPHA\n"));
5782 if (pCaps->TextureOpCaps & D3DTEXOPCAPS_BUMPENVMAP)
5783 LogRel((" - D3DTEXOPCAPS_BUMPENVMAP\n"));
5784 if (pCaps->TextureOpCaps & D3DTEXOPCAPS_BUMPENVMAPLUMINANCE)
5785 LogRel((" - D3DTEXOPCAPS_BUMPENVMAPLUMINANCE\n"));
5786 if (pCaps->TextureOpCaps & D3DTEXOPCAPS_DOTPRODUCT3)
5787 LogRel((" - D3DTEXOPCAPS_DOTPRODUCT3\n"));
5788 if (pCaps->TextureOpCaps & D3DTEXOPCAPS_MULTIPLYADD)
5789 LogRel((" - D3DTEXOPCAPS_MULTIPLYADD\n"));
5790 if (pCaps->TextureOpCaps & D3DTEXOPCAPS_LERP)
5791 LogRel((" - D3DTEXOPCAPS_LERP\n"));
5792
5793
5794 LogRel(("\n"));
5795 LogRel(("PixelShaderVersion: %#x (%u.%u)\n", pCaps->PixelShaderVersion,
5796 D3DSHADER_VERSION_MAJOR(pCaps->PixelShaderVersion), D3DSHADER_VERSION_MINOR(pCaps->PixelShaderVersion)));
5797 LogRel(("VertexShaderVersion: %#x (%u.%u)\n", pCaps->VertexShaderVersion,
5798 D3DSHADER_VERSION_MAJOR(pCaps->VertexShaderVersion), D3DSHADER_VERSION_MINOR(pCaps->VertexShaderVersion)));
5799
5800 LogRel(("\n"));
5801 RTLogRelSetBuffering(fBufferingSaved);
5802}
5803
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