VirtualBox

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

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

VMSVGA3d: Applied patches from trivirt that among other things makes Chrome work on OpenGL hosts.

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