VirtualBox

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

Last change on this file since 55319 was 54789, checked in by vboxsync, 10 years ago

VMSVGA3d: Changed the surface arrays into pointer arrays to reduce the copying requried when growing (there maybe lots of them and growing by 1 is tedious). This also means the surface pointers remain valid across growths.

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