VirtualBox

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

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

VMSVGA3D: fixed assertion

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