VirtualBox

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

Last change on this file since 81080 was 81080, checked in by vboxsync, 5 years ago

Devices/Graphics: correctly process the vertex attribute arrays with zero stride in the OpenGL backend.

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