VirtualBox

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

Last change on this file since 55998 was 55811, checked in by vboxsync, 10 years ago

DevVGA-SVGA3d-ogl.cpp,++: A couple of minor fixes and more GL success assertions.

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