VirtualBox

source: vbox/trunk/src/VBox/Devices/Graphics/DevVGA-SVGA3d-internal.h@ 82088

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

Devices/Graphics: logrel unknown surface and context ids

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 55.1 KB
Line 
1/* $Id: DevVGA-SVGA3d-internal.h 82063 2019-11-21 07:45:53Z vboxsync $ */
2/** @file
3 * DevVMWare - VMWare SVGA device - 3D part, internal header.
4 */
5
6/*
7 * Copyright (C) 2013-2019 Oracle Corporation
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.virtualbox.org. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 */
17
18#ifndef VBOX_INCLUDED_SRC_Graphics_DevVGA_SVGA3d_internal_h
19#define VBOX_INCLUDED_SRC_Graphics_DevVGA_SVGA3d_internal_h
20#ifndef RT_WITHOUT_PRAGMA_ONCE
21# pragma once
22#endif
23
24/*
25 * Assert sane compilation environment.
26 */
27#ifndef IN_RING3
28# error "VMSVGA3D_INCL_INTERNALS is only for ring-3 code"
29#endif
30#ifdef VMSVGA3D_OPENGL
31# ifdef VMSVGA3D_DIRECT3D
32# error "Both VMSVGA3D_DIRECT3D and VMSVGA3D_OPENGL cannot be defined at the same time."
33# endif
34#elif !defined(VMSVGA3D_DIRECT3D)
35# error "Either VMSVGA3D_OPENGL or VMSVGA3D_DIRECT3D must be defined."
36#endif
37
38
39/*********************************************************************************************************************************
40* Header Files *
41*********************************************************************************************************************************/
42#include "DevVGA-SVGA3d.h"
43
44#if defined(VMSVGA3D_DYNAMIC_LOAD) && defined(VMSVGA3D_OPENGL)
45# include "DevVGA-SVGA3d-glLdr.h"
46#endif
47
48#ifdef RT_OS_WINDOWS
49# include <iprt/win/windows.h>
50# ifdef VMSVGA3D_DIRECT3D
51# include <d3d9.h>
52# include <iprt/avl.h>
53# else
54# include <GL/gl.h>
55# include "vmsvga_glext/wglext.h"
56# endif
57
58#elif defined(RT_OS_DARWIN)
59# include <OpenGL/OpenGL.h>
60# include <OpenGL/gl3.h>
61# include <OpenGL/gl3ext.h>
62# define GL_DO_NOT_WARN_IF_MULTI_GL_VERSION_HEADERS_INCLUDED
63# include <OpenGL/gl.h>
64# include <OpenGL/glext.h>
65# include "DevVGA-SVGA3d-cocoa.h"
66/* work around conflicting definition of GLhandleARB in VMware's glext.h */
67//#define GL_ARB_shader_objects
68// HACK
69typedef void (APIENTRYP PFNGLFOGCOORDPOINTERPROC) (GLenum type, GLsizei stride, const GLvoid *pointer);
70typedef void (APIENTRYP PFNGLCLIENTACTIVETEXTUREPROC) (GLenum texture);
71typedef void (APIENTRYP PFNGLGETPROGRAMIVARBPROC) (GLenum target, GLenum pname, GLint *params);
72# define GL_RGBA_S3TC 0x83A2
73# define GL_ALPHA8_EXT 0x803c
74# define GL_LUMINANCE8_EXT 0x8040
75# define GL_LUMINANCE16_EXT 0x8042
76# define GL_LUMINANCE4_ALPHA4_EXT 0x8043
77# define GL_LUMINANCE8_ALPHA8_EXT 0x8045
78# define GL_INT_2_10_10_10_REV 0x8D9F
79
80#else
81# include <X11/Xlib.h>
82# include <X11/Xatom.h>
83# include <GL/gl.h>
84# include <GL/glx.h>
85# include <GL/glext.h>
86# define VBOX_VMSVGA3D_GL_HACK_LEVEL 0x103
87#endif
88
89#include "vmsvga/svga3d_shaderdefs.h"
90#ifdef VMSVGA3D_OPENGL
91# include "vmsvga_glext/glext.h"
92# include "shaderlib/shaderlib.h"
93#endif
94
95
96/*********************************************************************************************************************************
97* Defined Constants And Macros *
98*********************************************************************************************************************************/
99#ifdef VMSVGA3D_OPENGL
100/** OpenGL: Create a dedicated context for handling surfaces in, thus
101 * avoiding orphaned surfaces after context destruction.
102 *
103 * This cures, for instance, an assertion on fedora 21 that happens in
104 * vmsvga3dSurfaceStretchBlt if the login screen and the desktop has different
105 * sizes. The context of the login screen seems to have just been destroyed
106 * earlier and I believe the driver/X/whoever is attemting to strech the old
107 * screen content onto the new sized screen.
108 *
109 * @remarks This probably comes at a slight preformance expense, as we currently
110 * switches context when setting up the surface the first time. Not sure
111 * if we really need to, but as this is an experiment, I'm playing it safe.
112 * @remarks The define has been made default, thus should no longer be used.
113 */
114# define VMSVGA3D_OGL_WITH_SHARED_CTX
115/** Fake surface ID for the shared context. */
116# define VMSVGA3D_SHARED_CTX_ID UINT32_C(0xffffeeee)
117
118/** @def VBOX_VMSVGA3D_GL_HACK_LEVEL
119 * Turns out that on Linux gl.h may often define the first 2-4 OpenGL versions
120 * worth of extensions, but missing out on a function pointer of fifteen. This
121 * causes headache for us when we use the function pointers below. This hack
122 * changes the code to call the known problematic functions directly.
123 * The value is ((x)<<16 | (y)) where x and y are taken from the GL_VERSION_x_y.
124 */
125# ifndef VBOX_VMSVGA3D_GL_HACK_LEVEL
126# define VBOX_VMSVGA3D_GL_HACK_LEVEL 0
127# endif
128
129/** Invalid OpenGL ID. */
130# define OPENGL_INVALID_ID 0
131
132# define VMSVGA3D_CLEAR_CURRENT_CONTEXT(pState) \
133 do { (pState)->idActiveContext = OPENGL_INVALID_ID; } while (0)
134
135/** @def VMSVGA3D_SET_CURRENT_CONTEXT
136 * Makes sure the @a pContext is the active OpenGL context.
137 * @parm pState The VMSVGA3d state.
138 * @parm pContext The new context.
139 */
140# ifdef RT_OS_WINDOWS
141# define VMSVGA3D_SET_CURRENT_CONTEXT(pState, pContext) \
142 do { \
143 if ((pState)->idActiveContext != (pContext)->id) \
144 { \
145 BOOL fMakeCurrentRc = wglMakeCurrent((pContext)->hdc, (pContext)->hglrc); \
146 Assert(fMakeCurrentRc == TRUE); RT_NOREF_PV(fMakeCurrentRc); \
147 LogFlowFunc(("Changing context: %#x -> %#x\n", (pState)->idActiveContext, (pContext)->id)); \
148 (pState)->idActiveContext = (pContext)->id; \
149 } \
150 } while (0)
151
152# elif defined(RT_OS_DARWIN)
153# define VMSVGA3D_SET_CURRENT_CONTEXT(pState, pContext) \
154 do { \
155 if ((pState)->idActiveContext != (pContext)->id) \
156 { \
157 vmsvga3dCocoaViewMakeCurrentContext((pContext)->cocoaView, (pContext)->cocoaContext); \
158 LogFlowFunc(("Changing context: %#x -> %#x\n", (pState)->idActiveContext, (pContext)->id)); \
159 (pState)->idActiveContext = (pContext)->id; \
160 } \
161 } while (0)
162# else
163# define VMSVGA3D_SET_CURRENT_CONTEXT(pState, pContext) \
164 do { \
165 if ((pState)->idActiveContext != (pContext)->id) \
166 { \
167 Bool fMakeCurrentRc = glXMakeCurrent((pState)->display, \
168 (pContext)->window, \
169 (pContext)->glxContext); \
170 Assert(fMakeCurrentRc == True); RT_NOREF_PV(fMakeCurrentRc); \
171 LogFlowFunc(("Changing context: %#x -> %#x\n", (pState)->idActiveContext, (pContext)->id)); \
172 (pState)->idActiveContext = (pContext)->id; \
173 } \
174 } while (0)
175# endif
176
177/** @def VMSVGA3D_CLEAR_GL_ERRORS
178 * Clears all pending OpenGL errors.
179 *
180 * If I understood this correctly, OpenGL maintains a bitmask internally and
181 * glGetError gets the next bit (clearing it) from the bitmap and translates it
182 * into a GL_XXX constant value which it then returns. A single OpenGL call can
183 * set more than one bit, and they stick around across calls, from what I
184 * understand.
185 *
186 * So in order to be able to use glGetError to check whether a function
187 * succeeded, we need to call glGetError until all error bits have been cleared.
188 * This macro does that (in all types of builds).
189 *
190 * @sa VMSVGA3D_GET_GL_ERROR, VMSVGA3D_GL_IS_SUCCESS
191 */
192# define VMSVGA3D_CLEAR_GL_ERRORS() \
193 do { \
194 if (RT_UNLIKELY(glGetError() != GL_NO_ERROR)) /* predict no errors pending */ \
195 { \
196 uint32_t iErrorClearingLoopsLeft = 64; \
197 while (glGetError() != GL_NO_ERROR && iErrorClearingLoopsLeft > 0) \
198 iErrorClearingLoopsLeft--; \
199 } \
200 } while (0)
201
202/** @def VMSVGA3D_GET_LAST_GL_ERROR
203 * Gets the last OpenGL error, stores it in a_pContext->lastError and returns
204 * it.
205 *
206 * @returns Same as glGetError.
207 * @param a_pContext The context to store the error in.
208 *
209 * @sa VMSVGA3D_GL_IS_SUCCESS, VMSVGA3D_GL_COMPLAIN
210 */
211# define VMSVGA3D_GET_GL_ERROR(a_pContext) ((a_pContext)->lastError = glGetError())
212
213/** @def VMSVGA3D_GL_SUCCESS
214 * Checks whether VMSVGA3D_GET_LAST_GL_ERROR() return GL_NO_ERROR.
215 *
216 * Will call glGetError() and store the result in a_pContext->lastError.
217 * Will predict GL_NO_ERROR outcome.
218 *
219 * @returns True on success, false on error.
220 * @parm a_pContext The context to store the error in.
221 *
222 * @sa VMSVGA3D_GET_GL_ERROR, VMSVGA3D_GL_COMPLAIN
223 */
224# define VMSVGA3D_GL_IS_SUCCESS(a_pContext) RT_LIKELY((((a_pContext)->lastError = glGetError()) == GL_NO_ERROR))
225
226/** @def VMSVGA3D_GL_COMPLAIN
227 * Complains about one or more OpenGL errors (first in a_pContext->lastError).
228 *
229 * Strict builds will trigger an assertion, while other builds will put the
230 * first few occurences in the release log.
231 *
232 * All GL errors will be cleared after invocation. Assumes lastError
233 * is an error, will not check for GL_NO_ERROR.
234 *
235 * @param a_pState The 3D state structure.
236 * @param a_pContext The context that holds the first error.
237 * @param a_LogRelDetails Argument list for LogRel or similar that describes
238 * the operation in greater detail.
239 *
240 * @sa VMSVGA3D_GET_GL_ERROR, VMSVGA3D_GL_IS_SUCCESS
241 */
242# ifdef VBOX_STRICT
243# define VMSVGA3D_GL_COMPLAIN(a_pState, a_pContext, a_LogRelDetails) \
244 do { \
245 AssertMsg((a_pState)->idActiveContext == (a_pContext)->id, \
246 ("idActiveContext=%#x id=%x\n", (a_pState)->idActiveContext, (a_pContext)->id)); \
247 RTAssertMsg2Weak a_LogRelDetails; \
248 GLenum iNextError; \
249 while ((iNextError = glGetError()) != GL_NO_ERROR) \
250 RTAssertMsg2Weak("next error: %#x\n", iNextError); \
251 AssertMsgFailed(("first error: %#x (idActiveContext=%#x)\n", (a_pContext)->lastError, (a_pContext)->id)); \
252 } while (0)
253# else
254# define VMSVGA3D_GL_COMPLAIN(a_pState, a_pContext, a_LogRelDetails) \
255 do { \
256 LogRelMax(32, ("VMSVGA3d: OpenGL error %#x (idActiveContext=%#x) on line %u ", (a_pContext)->lastError, (a_pContext)->id, __LINE__)); \
257 GLenum iNextError; \
258 while ((iNextError = glGetError()) != GL_NO_ERROR) \
259 LogRelMax(32, (" - also error %#x ", iNextError)); \
260 LogRelMax(32, a_LogRelDetails); \
261 } while (0)
262# endif
263
264/** @def VMSVGA3D_GL_GET_AND_COMPLAIN
265 * Combination of VMSVGA3D_GET_GL_ERROR and VMSVGA3D_GL_COMPLAIN, assuming that
266 * there is a pending error.
267 *
268 * @param a_pState The 3D state structure.
269 * @param a_pContext The context that holds the first error.
270 * @param a_LogRelDetails Argument list for LogRel or similar that describes
271 * the operation in greater detail.
272 *
273 * @sa VMSVGA3D_GET_GL_ERROR, VMSVGA3D_GL_IS_SUCCESS, VMSVGA3D_GL_COMPLAIN
274 */
275# define VMSVGA3D_GL_GET_AND_COMPLAIN(a_pState, a_pContext, a_LogRelDetails) \
276 do { \
277 VMSVGA3D_GET_GL_ERROR(a_pContext); \
278 VMSVGA3D_GL_COMPLAIN(a_pState, a_pContext, a_LogRelDetails); \
279 } while (0)
280
281/** @def VMSVGA3D_GL_ASSERT_SUCCESS
282 * Asserts that VMSVGA3D_GL_IS_SUCCESS is true, complains if not.
283 *
284 * Uses VMSVGA3D_GL_COMPLAIN for complaining, so check it out wrt to release
285 * logging in non-strict builds.
286 *
287 * @param a_pState The 3D state structure.
288 * @param a_pContext The context that holds the first error.
289 * @param a_LogRelDetails Argument list for LogRel or similar that describes
290 * the operation in greater detail.
291 *
292 * @sa VMSVGA3D_GET_GL_ERROR, VMSVGA3D_GL_IS_SUCCESS, VMSVGA3D_GL_COMPLAIN
293 */
294# define VMSVGA3D_GL_ASSERT_SUCCESS(a_pState, a_pContext, a_LogRelDetails) \
295 if (VMSVGA3D_GL_IS_SUCCESS(a_pContext)) \
296 { /* likely */ } \
297 else do { \
298 VMSVGA3D_GL_COMPLAIN(a_pState, a_pContext, a_LogRelDetails); \
299 } while (0)
300
301/** @def VMSVGA3D_ASSERT_GL_CALL_EX
302 * Executes the specified OpenGL API call and asserts that it succeeded, variant
303 * with extra logging flexibility.
304 *
305 * ASSUMES no GL errors pending prior to invocation - caller should use
306 * VMSVGA3D_CLEAR_GL_ERRORS if uncertain.
307 *
308 * Uses VMSVGA3D_GL_COMPLAIN for complaining, so check it out wrt to release
309 * logging in non-strict builds.
310 *
311 * @param a_GlCall Expression making an OpenGL call.
312 * @param a_pState The 3D state structure.
313 * @param a_pContext The context that holds the first error.
314 * @param a_LogRelDetails Argument list for LogRel or similar that describes
315 * the operation in greater detail.
316 *
317 * @sa VMSVGA3D_ASSERT_GL_CALL, VMSVGA3D_GL_ASSERT_SUCCESS,
318 * VMSVGA3D_GET_GL_ERROR, VMSVGA3D_GL_IS_SUCCESS, VMSVGA3D_GL_COMPLAIN
319 */
320# define VMSVGA3D_ASSERT_GL_CALL_EX(a_GlCall, a_pState, a_pContext, a_LogRelDetails) \
321 do { \
322 (a_GlCall); \
323 VMSVGA3D_GL_ASSERT_SUCCESS(a_pState, a_pContext, a_LogRelDetails); \
324 } while (0)
325
326/** @def VMSVGA3D_ASSERT_GL_CALL
327 * Executes the specified OpenGL API call and asserts that it succeeded.
328 *
329 * ASSUMES no GL errors pending prior to invocation - caller should use
330 * VMSVGA3D_CLEAR_GL_ERRORS if uncertain.
331 *
332 * Uses VMSVGA3D_GL_COMPLAIN for complaining, so check it out wrt to release
333 * logging in non-strict builds.
334 *
335 * @param a_GlCall Expression making an OpenGL call.
336 * @param a_pState The 3D state structure.
337 * @param a_pContext The context that holds the first error.
338 *
339 * @sa VMSVGA3D_ASSERT_GL_CALL_EX, VMSVGA3D_GL_ASSERT_SUCCESS,
340 * VMSVGA3D_GET_GL_ERROR, VMSVGA3D_GL_IS_SUCCESS, VMSVGA3D_GL_COMPLAIN
341 */
342# define VMSVGA3D_ASSERT_GL_CALL(a_GlCall, a_pState, a_pContext) \
343 VMSVGA3D_ASSERT_GL_CALL_EX(a_GlCall, a_pState, a_pContext, ("%s\n", #a_GlCall))
344
345
346/** @def VMSVGA3D_CHECK_LAST_ERROR
347 * Checks that the last OpenGL error code indicates success.
348 *
349 * Will assert and return VERR_INTERNAL_ERROR in strict builds, in other
350 * builds it will do nothing and is a NOOP.
351 *
352 * @parm pState The VMSVGA3d state.
353 * @parm pContext The context.
354 *
355 * @todo Replace with proper error handling, it's crazy to return
356 * VERR_INTERNAL_ERROR in strict builds and just barge on ahead in
357 * release builds.
358 */
359/** @todo Rename to VMSVGA3D_CHECK_LAST_ERROR_RETURN */
360# ifdef VBOX_STRICT
361# define VMSVGA3D_CHECK_LAST_ERROR(pState, pContext) do { \
362 Assert((pState)->idActiveContext == (pContext)->id); \
363 (pContext)->lastError = glGetError(); \
364 AssertMsgReturn((pContext)->lastError == GL_NO_ERROR, \
365 ("%s (%d): last error 0x%x\n", __FUNCTION__, __LINE__, (pContext)->lastError), \
366 VERR_INTERNAL_ERROR); \
367 } while (0)
368# else
369# define VMSVGA3D_CHECK_LAST_ERROR(pState, pContext) do { } while (0)
370# endif
371
372/** @def VMSVGA3D_CHECK_LAST_ERROR_WARN
373 * Checks that the last OpenGL error code indicates success.
374 *
375 * Will assert in strict builds, otherwise it's a NOOP.
376 *
377 * @parm pState The VMSVGA3d state.
378 * @parm pContext The new context.
379 */
380# ifdef VBOX_STRICT
381# define VMSVGA3D_CHECK_LAST_ERROR_WARN(pState, pContext) do { \
382 Assert((pState)->idActiveContext == (pContext)->id); \
383 (pContext)->lastError = glGetError(); \
384 AssertMsg((pContext)->lastError == GL_NO_ERROR, ("%s (%d): last error 0x%x\n", __FUNCTION__, __LINE__, (pContext)->lastError)); \
385 } while (0)
386# else
387# define VMSVGA3D_CHECK_LAST_ERROR_WARN(pState, pContext) do { } while (0)
388# endif
389
390#endif /* VMSVGA3D_OPENGL */
391
392#ifdef VMSVGA3D_DIRECT3D
393/* Enable to use Wine to convert D3D to opengl */
394//#define VBOX_VMSVGA3D_WITH_WINE_OPENGL
395#endif
396
397
398/*********************************************************************************************************************************
399* Structures and Typedefs *
400*********************************************************************************************************************************/
401/**
402 * Mipmap level.
403 */
404typedef struct VMSVGA3DMIPMAPLEVEL
405{
406 /** The mipmap size: width, height and depth. */
407 SVGA3dSize mipmapSize;
408 /** Width in blocks: (width + cxBlock - 1) / cxBlock. SSM: not saved, recalculated on load. */
409 uint32_t cBlocksX;
410 /** Height in blocks: (height + cyBlock - 1) / cyBlock. SSM: not saved, recalculated on load. */
411 uint32_t cBlocksY;
412 /** The scanline/pitch size in bytes: at least cBlocksX * cbBlock. */
413 uint32_t cbSurfacePitch;
414 /** The size (in bytes) of the mipmap plane: cbSurfacePitch * cBlocksY */
415 uint32_t cbSurfacePlane;
416 /** The size (in bytes) of the mipmap data when using the format the surface was
417 * defined with: cbSurfacePlane * mipmapSize.z */
418 uint32_t cbSurface;
419 /** Pointer to the mipmap bytes (cbSurface). Often NULL. If the surface has
420 * been realized in hardware, this may be outdated. */
421 void *pSurfaceData;
422 /** Set if pvSurfaceData contains data not realized in hardware or pushed to the
423 * hardware surface yet. */
424 bool fDirty;
425} VMSVGA3DMIPMAPLEVEL;
426/** Pointer to a mipmap level. */
427typedef VMSVGA3DMIPMAPLEVEL *PVMSVGA3DMIPMAPLEVEL;
428
429
430#ifdef VMSVGA3D_INCL_STRUCTURE_DESCRIPTORS
431/**
432 * SSM descriptor table for the VMSVGA3DMIPMAPLEVEL structure.
433 */
434static SSMFIELD const g_aVMSVGA3DMIPMAPLEVELFields[] =
435{
436 SSMFIELD_ENTRY( VMSVGA3DMIPMAPLEVEL, mipmapSize),
437 SSMFIELD_ENTRY( VMSVGA3DMIPMAPLEVEL, cbSurface),
438 SSMFIELD_ENTRY( VMSVGA3DMIPMAPLEVEL, cbSurfacePitch),
439 SSMFIELD_ENTRY_IGN_HCPTR( VMSVGA3DMIPMAPLEVEL, pSurfaceData),
440 SSMFIELD_ENTRY_IGNORE( VMSVGA3DMIPMAPLEVEL, fDirty),
441 SSMFIELD_ENTRY_TERM()
442};
443#endif
444
445typedef struct VMSVGATRANSFORMSTATE
446{
447 bool fValid;
448 float matrix[16];
449} VMSVGATRANSFORMSTATE;
450typedef VMSVGATRANSFORMSTATE *PVMSVGATRANSFORMSTATE;
451
452typedef struct VMSVGAMATERIALSTATE
453{
454 bool fValid;
455 SVGA3dMaterial material;
456} VMSVGAMATERIALSTATE;
457typedef VMSVGAMATERIALSTATE *PVMSVGAMATERIALSTATE;
458
459typedef struct VMSVGACLIPPLANESTATE
460{
461 bool fValid;
462 float plane[4];
463} VMSVGACLIPPLANESTATE;
464typedef VMSVGACLIPPLANESTATE *PVMSVGACLIPPLANESTATE;
465
466typedef struct VMSVGALIGHTSTATE
467{
468 bool fEnabled;
469 bool fValidData;
470 SVGA3dLightData data;
471} VMSVGALIGHTSTATE;
472typedef VMSVGALIGHTSTATE *PVMSVGALIGHTSTATE;
473
474typedef struct VMSVGASHADERCONST
475{
476 bool fValid;
477 SVGA3dShaderConstType ctype;
478 uint32_t value[4];
479} VMSVGASHADERCONST;
480typedef VMSVGASHADERCONST *PVMSVGASHADERCONST;
481
482#ifdef VMSVGA3D_INCL_STRUCTURE_DESCRIPTORS
483/**
484 * SSM descriptor table for the VMSVGASHADERCONST structure.
485 */
486static SSMFIELD const g_aVMSVGASHADERCONSTFields[] =
487{
488 SSMFIELD_ENTRY( VMSVGASHADERCONST, fValid),
489 SSMFIELD_ENTRY( VMSVGASHADERCONST, ctype),
490 SSMFIELD_ENTRY( VMSVGASHADERCONST, value),
491 SSMFIELD_ENTRY_TERM()
492};
493#endif
494
495#ifdef VMSVGA3D_DIRECT3D
496
497/* What kind of Direct3D resource has been created for the VMSVGA3D surface. */
498typedef enum VMSVGA3DD3DRESTYPE
499{
500 VMSVGA3D_D3DRESTYPE_NONE = 0,
501 VMSVGA3D_D3DRESTYPE_SURFACE = 1,
502 VMSVGA3D_D3DRESTYPE_TEXTURE = 2,
503 VMSVGA3D_D3DRESTYPE_CUBE_TEXTURE = 3,
504 VMSVGA3D_D3DRESTYPE_VOLUME_TEXTURE = 4,
505 VMSVGA3D_D3DRESTYPE_VERTEX_BUFFER = 5,
506 VMSVGA3D_D3DRESTYPE_INDEX_BUFFER = 6
507} VMSVGA3DD3DRESTYPE;
508
509/**
510 *
511 */
512typedef struct
513{
514 /** Key is context id. */
515 AVLU32NODECORE Core;
516 union
517 {
518 IDirect3DSurface9 *pSurface;
519 IDirect3DTexture9 *pTexture;
520 IDirect3DCubeTexture9 *pCubeTexture;
521 IDirect3DVolumeTexture9 *pVolumeTexture;
522 } u;
523} VMSVGA3DSHAREDSURFACE;
524typedef VMSVGA3DSHAREDSURFACE *PVMSVGA3DSHAREDSURFACE;
525#endif /* VMSVGA3D_DIRECT3D */
526
527#ifdef VMSVGA3D_OPENGL
528/* What kind of OpenGL resource has been created for the VMSVGA3D surface. */
529typedef enum VMSVGA3DOGLRESTYPE
530{
531 VMSVGA3D_OGLRESTYPE_NONE = 0,
532 VMSVGA3D_OGLRESTYPE_BUFFER = 1,
533 VMSVGA3D_OGLRESTYPE_TEXTURE = 2,
534 VMSVGA3D_OGLRESTYPE_RENDERBUFFER = 3
535} VMSVGA3DOGLRESTYPE;
536#endif
537
538/**
539 * VMSVGA3d surface.
540 */
541typedef struct VMSVGA3DSURFACE
542{
543 uint32_t id;
544#ifdef VMSVGA3D_OPENGL
545 uint32_t idWeakContextAssociation;
546#else
547 uint32_t idAssociatedContext;
548#endif
549 uint32_t surfaceFlags;
550 SVGA3dSurfaceFormat format;
551#ifdef VMSVGA3D_OPENGL
552 GLint internalFormatGL;
553 GLint formatGL;
554 GLint typeGL;
555 VMSVGA3DOGLRESTYPE enmOGLResType; /* Which resource was created for the surface. */
556 union
557 {
558 GLuint texture;
559 GLuint buffer;
560 GLuint renderbuffer;
561 } oglId;
562 GLenum targetGL; /* GL_TEXTURE_* */
563 GLenum bindingGL; /* GL_TEXTURE_BINDING_* */
564 /* Emulated formats */
565 bool fEmulated; /* Whether the texture format is emulated. */
566 GLuint idEmulated; /* GL name of the intermediate texture. */
567#endif
568 SVGA3dSurfaceFace faces[SVGA3D_MAX_SURFACE_FACES];
569 uint32_t cFaces;
570 uint32_t cMipmapLevels;
571 PVMSVGA3DMIPMAPLEVEL pMipmapLevels;
572 uint32_t multiSampleCount;
573 SVGA3dTextureFilter autogenFilter;
574#ifdef VMSVGA3D_DIRECT3D
575 D3DFORMAT formatD3D;
576 DWORD fUsageD3D;
577 D3DMULTISAMPLE_TYPE multiSampleTypeD3D;
578#endif
579
580 uint32_t cbBlock; /* block/pixel size in bytes */
581 /* Dimensions of the surface block, usually 1x1 except for compressed formats. */
582 uint32_t cxBlock; /* Block width in pixels. SSM: not saved, recalculated on load. */
583 uint32_t cyBlock; /* Block height in pixels. SSM: not saved, recalculated on load. */
584
585 /* Dirty state; surface was manually updated. */
586 bool fDirty;
587
588#ifdef VMSVGA3D_DIRECT3D
589 /* Handle for shared objects (currently only textures & render targets). */
590 HANDLE hSharedObject;
591 /** Event query inserted after each GPU operation that updates or uses this surface. */
592 IDirect3DQuery9 *pQuery;
593 /** The context id where the query has been created. */
594 uint32_t idQueryContext;
595 /** The type of actually created D3D resource. */
596 VMSVGA3DD3DRESTYPE enmD3DResType;
597 union
598 {
599 IDirect3DSurface9 *pSurface;
600 IDirect3DTexture9 *pTexture;
601 IDirect3DCubeTexture9 *pCubeTexture;
602 IDirect3DVolumeTexture9 *pVolumeTexture;
603 IDirect3DVertexBuffer9 *pVertexBuffer;
604 IDirect3DIndexBuffer9 *pIndexBuffer;
605 } u;
606 union
607 {
608 IDirect3DTexture9 *pTexture;
609 IDirect3DCubeTexture9 *pCubeTexture;
610 IDirect3DVolumeTexture9 *pVolumeTexture;
611 } bounce;
612 /** AVL tree containing VMSVGA3DSHAREDSURFACE structures. */
613 AVLU32TREE pSharedObjectTree;
614 bool fStencilAsTexture;
615 D3DFORMAT d3dfmtRequested;
616 union
617 {
618 IDirect3DTexture9 *pTexture;
619 IDirect3DCubeTexture9 *pCubeTexture;
620 IDirect3DVolumeTexture9 *pVolumeTexture;
621 } emulated;
622#endif
623} VMSVGA3DSURFACE;
624/** Pointer to a 3d surface. */
625typedef VMSVGA3DSURFACE *PVMSVGA3DSURFACE;
626
627#ifdef VMSVGA3D_INCL_STRUCTURE_DESCRIPTORS
628/**
629 * SSM descriptor table for the VMSVGA3DSURFACE structure.
630 */
631static SSMFIELD const g_aVMSVGA3DSURFACEFields[] =
632{
633 SSMFIELD_ENTRY( VMSVGA3DSURFACE, id),
634# ifdef VMSVGA3D_OPENGL
635 SSMFIELD_ENTRY( VMSVGA3DSURFACE, idWeakContextAssociation),
636# else
637 SSMFIELD_ENTRY( VMSVGA3DSURFACE, idAssociatedContext),
638# endif
639 SSMFIELD_ENTRY( VMSVGA3DSURFACE, surfaceFlags),
640 SSMFIELD_ENTRY( VMSVGA3DSURFACE, format),
641# ifdef VMSVGA3D_OPENGL
642 SSMFIELD_ENTRY( VMSVGA3DSURFACE, internalFormatGL),
643 SSMFIELD_ENTRY( VMSVGA3DSURFACE, formatGL),
644 SSMFIELD_ENTRY( VMSVGA3DSURFACE, typeGL),
645 SSMFIELD_ENTRY_IGNORE( VMSVGA3DSURFACE, id),
646# endif
647 SSMFIELD_ENTRY( VMSVGA3DSURFACE, faces),
648 SSMFIELD_ENTRY( VMSVGA3DSURFACE, cFaces),
649 SSMFIELD_ENTRY_IGN_HCPTR( VMSVGA3DSURFACE, pMipmapLevels),
650 SSMFIELD_ENTRY( VMSVGA3DSURFACE, multiSampleCount),
651 SSMFIELD_ENTRY( VMSVGA3DSURFACE, autogenFilter),
652# ifdef VMSVGA3D_DIRECT3D
653 SSMFIELD_ENTRY( VMSVGA3DSURFACE, format), /** @todo format duplicated. */
654 SSMFIELD_ENTRY_IGNORE( VMSVGA3DSURFACE, formatD3D),
655 SSMFIELD_ENTRY_IGNORE( VMSVGA3DSURFACE, fUsageD3D),
656 SSMFIELD_ENTRY_IGNORE( VMSVGA3DSURFACE, multiSampleTypeD3D),
657# endif
658 SSMFIELD_ENTRY( VMSVGA3DSURFACE, cbBlock),
659 SSMFIELD_ENTRY_IGNORE( VMSVGA3DSURFACE, fDirty),
660# ifdef VMSVGA3D_DIRECT3D
661 SSMFIELD_ENTRY_IGN_HCPTR( VMSVGA3DSURFACE, hSharedObject),
662 SSMFIELD_ENTRY_IGN_HCPTR( VMSVGA3DSURFACE, pQuery),
663 SSMFIELD_ENTRY_IGN_HCPTR( VMSVGA3DSURFACE, u.pSurface),
664 SSMFIELD_ENTRY_IGN_HCPTR( VMSVGA3DSURFACE, bounce.pTexture),
665 SSMFIELD_ENTRY_IGNORE( VMSVGA3DSURFACE, pSharedObjectTree),
666 SSMFIELD_ENTRY_IGNORE( VMSVGA3DSURFACE, fStencilAsTexture),
667# endif
668 SSMFIELD_ENTRY_TERM()
669};
670#endif
671
672/** Mask we frequently apply to VMSVGA3DSURFACE::flags for decing what kind
673 * of surface we're dealing. */
674#define VMSVGA3D_SURFACE_HINT_SWITCH_MASK \
675 ( SVGA3D_SURFACE_HINT_INDEXBUFFER | SVGA3D_SURFACE_HINT_VERTEXBUFFER \
676 | SVGA3D_SURFACE_HINT_TEXTURE | SVGA3D_SURFACE_HINT_RENDERTARGET \
677 | SVGA3D_SURFACE_HINT_DEPTHSTENCIL | SVGA3D_SURFACE_CUBEMAP )
678
679/** @def VMSVGA3DSURFACE_HAS_HW_SURFACE
680 * Checks whether the surface has a host hardware/library surface.
681 * @returns true/false
682 * @param a_pSurface The VMSVGA3d surface.
683 */
684#ifdef VMSVGA3D_DIRECT3D
685# define VMSVGA3DSURFACE_HAS_HW_SURFACE(a_pSurface) ((a_pSurface)->u.pSurface != NULL)
686#else
687# define VMSVGA3DSURFACE_HAS_HW_SURFACE(a_pSurface) ((a_pSurface)->oglId.texture != OPENGL_INVALID_ID)
688#endif
689
690
691
692typedef struct VMSVGA3DSHADER
693{
694 uint32_t id;
695 uint32_t cid;
696 SVGA3dShaderType type;
697 uint32_t cbData;
698 void *pShaderProgram;
699 union
700 {
701#ifdef VMSVGA3D_DIRECT3D
702 IDirect3DVertexShader9 *pVertexShader;
703 IDirect3DPixelShader9 *pPixelShader;
704#else
705 void *pVertexShader;
706 void *pPixelShader;
707#endif
708 void *pv;
709 } u;
710} VMSVGA3DSHADER;
711typedef VMSVGA3DSHADER *PVMSVGA3DSHADER;
712
713#ifdef VMSVGA3D_INCL_STRUCTURE_DESCRIPTORS
714/**
715 * SSM descriptor table for the VMSVGA3DSHADER structure.
716 */
717static SSMFIELD const g_aVMSVGA3DSHADERFields[] =
718{
719 SSMFIELD_ENTRY( VMSVGA3DSHADER, id),
720 SSMFIELD_ENTRY( VMSVGA3DSHADER, cid),
721 SSMFIELD_ENTRY( VMSVGA3DSHADER, type),
722 SSMFIELD_ENTRY( VMSVGA3DSHADER, cbData),
723 SSMFIELD_ENTRY_IGN_HCPTR( VMSVGA3DSHADER, pShaderProgram),
724 SSMFIELD_ENTRY_IGN_HCPTR( VMSVGA3DSHADER, u.pv),
725 SSMFIELD_ENTRY_TERM()
726};
727#endif
728
729/** @name VMSVGA3D_UPDATE_XXX - ...
730 * @{ */
731#define VMSVGA3D_UPDATE_SCISSORRECT RT_BIT_32(0)
732#define VMSVGA3D_UPDATE_ZRANGE RT_BIT_32(1)
733#define VMSVGA3D_UPDATE_VIEWPORT RT_BIT_32(2)
734#define VMSVGA3D_UPDATE_VERTEXSHADER RT_BIT_32(3)
735#define VMSVGA3D_UPDATE_PIXELSHADER RT_BIT_32(4)
736#define VMSVGA3D_UPDATE_TRANSFORM RT_BIT_32(5)
737#define VMSVGA3D_UPDATE_MATERIAL RT_BIT_32(6)
738/** @} */
739
740/* Query states. Mostly used for saved state. */
741typedef enum VMSVGA3DQUERYSTATE
742{
743 VMSVGA3DQUERYSTATE_NULL = 0, /* Not created. */
744 VMSVGA3DQUERYSTATE_SIGNALED = 1, /* Result obtained. The guest may or may not read the result yet. */
745 VMSVGA3DQUERYSTATE_BUILDING = 2, /* In process of collecting data. */
746 VMSVGA3DQUERYSTATE_ISSUED = 3, /* Data collected, but result is not yet obtained. */
747 VMSVGA3DQUERYSTATE_32BIT = 0x7fffffff
748} VMSVGA3DQUERYSTATE;
749AssertCompileSize(VMSVGA3DQUERYSTATE, sizeof(uint32_t));
750
751typedef struct VMSVGA3DQUERY
752{
753#ifdef VMSVGA3D_DIRECT3D
754 IDirect3DQuery9 *pQuery;
755#else /* VMSVGA3D_OPENGL */
756 GLuint idQuery;
757#endif
758 VMSVGA3DQUERYSTATE enmQueryState; /* VMSVGA3DQUERYSTATE_*. State is implicitly _NULL if pQuery is NULL. */
759 uint32_t u32QueryResult; /* Generic result. Enough for all VGPU9 queries. */
760} VMSVGA3DQUERY;
761
762#ifdef VMSVGA3D_INCL_STRUCTURE_DESCRIPTORS
763/**
764 * SSM descriptor table for the VMSVGA3DQUERY structure.
765 */
766static SSMFIELD const g_aVMSVGA3DQUERYFields[] =
767{
768#ifdef VMSVGA3D_DIRECT3D
769 SSMFIELD_ENTRY_IGN_HCPTR( VMSVGA3DQUERY, pQuery),
770#else /* VMSVGA3D_OPENGL */
771 SSMFIELD_ENTRY_IGNORE( VMSVGA3DQUERY, idQuery),
772#endif
773 SSMFIELD_ENTRY( VMSVGA3DQUERY, enmQueryState),
774 SSMFIELD_ENTRY( VMSVGA3DQUERY, u32QueryResult),
775 SSMFIELD_ENTRY_TERM()
776};
777#endif
778
779#ifdef VMSVGA3D_DIRECT3D
780#define VMSVGA3DQUERY_EXISTS(p) ((p)->pQuery && (p)->enmQueryState != VMSVGA3DQUERYSTATE_NULL)
781#else
782#define VMSVGA3DQUERY_EXISTS(p) ((p)->idQuery && (p)->enmQueryState != VMSVGA3DQUERYSTATE_NULL)
783#endif
784
785/**
786 * VMSVGA3d context.
787 */
788typedef struct VMSVGA3DCONTEXT
789{
790 uint32_t id;
791#ifdef RT_OS_WINDOWS
792# ifdef VMSVGA3D_DIRECT3D
793# ifdef VBOX_VMSVGA3D_WITH_WINE_OPENGL
794 IDirect3DDevice9 *pDevice;
795# else
796 IDirect3DDevice9Ex *pDevice;
797# endif
798# else
799 /* Device context of the context window. */
800 HDC hdc;
801 /* OpenGL rendering context handle. */
802 HGLRC hglrc;
803# endif
804 /* Device context window handle. */
805 HWND hwnd;
806#elif defined(RT_OS_DARWIN)
807 /* OpenGL rendering context */
808 NativeNSOpenGLContextRef cocoaContext;
809 NativeNSViewRef cocoaView;
810 bool fOtherProfile;
811#else
812 /** XGL rendering context handle */
813 GLXContext glxContext;
814 /** Device context window handle */
815 Window window;
816#endif
817
818#ifdef VMSVGA3D_OPENGL
819 /* Framebuffer object associated with this context. */
820 GLuint idFramebuffer;
821 /* Read and draw framebuffer objects for various operations. */
822 GLuint idReadFramebuffer;
823 GLuint idDrawFramebuffer;
824 /* Last GL error recorded. */
825 GLenum lastError;
826 void *pShaderContext;
827#endif
828
829 /* Current selected texture surfaces (if any) */
830 uint32_t aSidActiveTextures[SVGA3D_MAX_SAMPLERS];
831 /* Per context pixel and vertex shaders. */
832 uint32_t cPixelShaders;
833 PVMSVGA3DSHADER paPixelShader;
834 uint32_t cVertexShaders;
835 PVMSVGA3DSHADER paVertexShader;
836 /* Keep track of the internal state to be able to recreate the context properly (save/restore, window resize). */
837 struct
838 {
839 /** VMSVGA3D_UPDATE_XXX */
840 uint32_t u32UpdateFlags;
841
842 SVGA3dRenderState aRenderState[SVGA3D_RS_MAX];
843 /* aTextureStates contains both TextureStageStates and SamplerStates, therefore [SVGA3D_MAX_SAMPLERS]. */
844 SVGA3dTextureState aTextureStates[SVGA3D_MAX_SAMPLERS][SVGA3D_TS_MAX];
845 VMSVGATRANSFORMSTATE aTransformState[SVGA3D_TRANSFORM_MAX];
846 VMSVGAMATERIALSTATE aMaterial[SVGA3D_FACE_MAX];
847 VMSVGACLIPPLANESTATE aClipPlane[SVGA3D_CLIPPLANE_MAX];
848 VMSVGALIGHTSTATE aLightData[SVGA3D_MAX_LIGHTS];
849
850 uint32_t aRenderTargets[SVGA3D_RT_MAX];
851 SVGA3dRect RectScissor;
852 SVGA3dRect RectViewPort;
853 SVGA3dZRange zRange;
854 uint32_t shidPixel;
855 uint32_t shidVertex;
856
857 uint32_t cPixelShaderConst;
858 PVMSVGASHADERCONST paPixelShaderConst;
859 uint32_t cVertexShaderConst;
860 PVMSVGASHADERCONST paVertexShaderConst;
861 } state;
862
863 /* Occlusion query. */
864 VMSVGA3DQUERY occlusion;
865
866#ifdef VMSVGA3D_DIRECT3D
867 /* State which is currently applied to the D3D device. It is recreated as needed and not saved.
868 * The purpose is to remember the currently applied state and do not re-apply it if it has not changed.
869 * Unnecessary state changes are very bad for performance.
870 */
871 struct
872 {
873 /* Vertex declaration. */
874 IDirect3DVertexDeclaration9 *pVertexDecl;
875 uint32_t cVertexElements;
876 D3DVERTEXELEMENT9 aVertexElements[SVGA3D_MAX_VERTEX_ARRAYS + 1];
877 } d3dState;
878#endif
879} VMSVGA3DCONTEXT;
880/** Pointer to a VMSVGA3d context. */
881typedef VMSVGA3DCONTEXT *PVMSVGA3DCONTEXT;
882
883#ifdef VMSVGA3D_INCL_STRUCTURE_DESCRIPTORS
884/**
885 * SSM descriptor table for the VMSVGA3DCONTEXT structure.
886 */
887static SSMFIELD const g_aVMSVGA3DCONTEXTFields[] =
888{
889 SSMFIELD_ENTRY( VMSVGA3DCONTEXT, id),
890
891# ifdef RT_OS_WINDOWS
892# ifdef VMSVGA3D_DIRECT3D
893 SSMFIELD_ENTRY_IGN_HCPTR( VMSVGA3DCONTEXT, pDevice),
894# else
895 SSMFIELD_ENTRY_IGNORE( VMSVGA3DCONTEXT, hdc),
896 SSMFIELD_ENTRY_IGNORE( VMSVGA3DCONTEXT, hglrc),
897# endif
898 SSMFIELD_ENTRY_IGNORE( VMSVGA3DCONTEXT, hwnd),
899# elif defined(RT_OS_DARWIN)
900 SSMFIELD_ENTRY_IGNORE( VMSVGA3DCONTEXT, cocoaContext),
901 SSMFIELD_ENTRY_IGNORE( VMSVGA3DCONTEXT, cocoaView),
902 SSMFIELD_ENTRY_IGNORE( VMSVGA3DCONTEXT, fOtherProfile),
903# else
904 SSMFIELD_ENTRY_IGNORE( VMSVGA3DCONTEXT, glxContext),
905 SSMFIELD_ENTRY_IGNORE( VMSVGA3DCONTEXT, window),
906# endif
907
908#ifdef VMSVGA3D_OPENGL
909 SSMFIELD_ENTRY_IGNORE( VMSVGA3DCONTEXT, idFramebuffer),
910 SSMFIELD_ENTRY_IGNORE( VMSVGA3DCONTEXT, idReadFramebuffer),
911 SSMFIELD_ENTRY_IGNORE( VMSVGA3DCONTEXT, idDrawFramebuffer),
912 SSMFIELD_ENTRY( VMSVGA3DCONTEXT, lastError),
913 SSMFIELD_ENTRY_IGN_HCPTR( VMSVGA3DCONTEXT, pShaderContext),
914#endif
915
916 SSMFIELD_ENTRY_IGNORE( VMSVGA3DCONTEXT, aSidActiveTextures),
917 SSMFIELD_ENTRY( VMSVGA3DCONTEXT, cPixelShaders),
918 SSMFIELD_ENTRY_IGN_HCPTR( VMSVGA3DCONTEXT, paPixelShader),
919 SSMFIELD_ENTRY( VMSVGA3DCONTEXT, cVertexShaders),
920 SSMFIELD_ENTRY_IGN_HCPTR( VMSVGA3DCONTEXT, paVertexShader),
921 SSMFIELD_ENTRY( VMSVGA3DCONTEXT, state.u32UpdateFlags),
922
923 SSMFIELD_ENTRY( VMSVGA3DCONTEXT, state.aRenderState),
924 SSMFIELD_ENTRY_OLD( state.aTextureStates,
925 sizeof(SVGA3dTextureState) * /*SVGA3D_MAX_TEXTURE_STAGE=*/ 8 * /*SVGA3D_TS_MAX=*/ 30),
926 SSMFIELD_ENTRY( VMSVGA3DCONTEXT, state.aTransformState),
927 SSMFIELD_ENTRY( VMSVGA3DCONTEXT, state.aMaterial),
928 SSMFIELD_ENTRY( VMSVGA3DCONTEXT, state.aClipPlane),
929 SSMFIELD_ENTRY( VMSVGA3DCONTEXT, state.aLightData),
930
931 SSMFIELD_ENTRY( VMSVGA3DCONTEXT, state.aRenderTargets),
932 SSMFIELD_ENTRY( VMSVGA3DCONTEXT, state.RectScissor),
933 SSMFIELD_ENTRY( VMSVGA3DCONTEXT, state.RectViewPort),
934 SSMFIELD_ENTRY( VMSVGA3DCONTEXT, state.zRange),
935 SSMFIELD_ENTRY( VMSVGA3DCONTEXT, state.shidPixel),
936 SSMFIELD_ENTRY( VMSVGA3DCONTEXT, state.shidVertex),
937 SSMFIELD_ENTRY( VMSVGA3DCONTEXT, state.cPixelShaderConst),
938 SSMFIELD_ENTRY_IGN_HCPTR( VMSVGA3DCONTEXT, state.paPixelShaderConst),
939 SSMFIELD_ENTRY( VMSVGA3DCONTEXT, state.cVertexShaderConst),
940 SSMFIELD_ENTRY_IGN_HCPTR( VMSVGA3DCONTEXT, state.paVertexShaderConst),
941 SSMFIELD_ENTRY_TERM()
942};
943#endif /* VMSVGA3D_INCL_STRUCTURE_DESCRIPTORS */
944
945
946#ifdef VMSVGA3D_OPENGL
947typedef struct VMSVGA3DFORMATCONVERTER *PVMSVGA3DFORMATCONVERTER;
948#endif
949
950/**
951 * VMSVGA3d state data.
952 *
953 * Allocated on the heap and pointed to by VMSVGAState::p3dState.
954 */
955typedef struct VMSVGA3DSTATE
956{
957 /** The size of papContexts. */
958 uint32_t cContexts;
959 /** The size of papSurfaces. */
960 uint32_t cSurfaces;
961 /** Contexts indexed by ID. Grown as needed. */
962 PVMSVGA3DCONTEXT *papContexts;
963 /** Surfaces indexed by ID. Grown as needed. */
964 PVMSVGA3DSURFACE *papSurfaces;
965
966#ifdef RT_OS_WINDOWS
967# ifdef VMSVGA3D_DIRECT3D
968# ifdef VBOX_VMSVGA3D_WITH_WINE_OPENGL
969 IDirect3D9 *pD3D9;
970# else
971 IDirect3D9Ex *pD3D9;
972# endif
973 D3DCAPS9 caps;
974 bool fSupportedSurfaceINTZ;
975 bool fSupportedSurfaceNULL;
976 bool fSupportedFormatUYVY : 1;
977 bool fSupportedFormatYUY2 : 1;
978 bool fSupportedFormatA8B8G8R8 : 1;
979# endif
980 /** Window Thread. */
981 R3PTRTYPE(RTTHREAD) pWindowThread;
982 DWORD idWindowThread;
983 HMODULE hInstance;
984 /** Window request semaphore. */
985 RTSEMEVENT WndRequestSem;
986#elif defined(RT_OS_DARWIN)
987#else
988 /* The X display */
989 Display *display;
990 R3PTRTYPE(RTTHREAD) pWindowThread;
991 bool bTerminate;
992#endif
993
994#ifdef VMSVGA3D_OPENGL
995 float rsGLVersion;
996 /* Current active context. */
997 uint32_t idActiveContext;
998
999 struct
1000 {
1001 PFNGLISRENDERBUFFERPROC glIsRenderbuffer;
1002 PFNGLBINDRENDERBUFFERPROC glBindRenderbuffer;
1003 PFNGLDELETERENDERBUFFERSPROC glDeleteRenderbuffers;
1004 PFNGLGENRENDERBUFFERSPROC glGenRenderbuffers;
1005 PFNGLRENDERBUFFERSTORAGEPROC glRenderbufferStorage;
1006 PFNGLGETRENDERBUFFERPARAMETERIVPROC glGetRenderbufferParameteriv;
1007 PFNGLISFRAMEBUFFERPROC glIsFramebuffer;
1008 PFNGLBINDFRAMEBUFFERPROC glBindFramebuffer;
1009 PFNGLDELETEFRAMEBUFFERSPROC glDeleteFramebuffers;
1010 PFNGLGENFRAMEBUFFERSPROC glGenFramebuffers;
1011 PFNGLCHECKFRAMEBUFFERSTATUSPROC glCheckFramebufferStatus;
1012 PFNGLFRAMEBUFFERTEXTURE1DPROC glFramebufferTexture1D;
1013 PFNGLFRAMEBUFFERTEXTURE2DPROC glFramebufferTexture2D;
1014 PFNGLFRAMEBUFFERTEXTURE3DPROC glFramebufferTexture3D;
1015 PFNGLFRAMEBUFFERRENDERBUFFERPROC glFramebufferRenderbuffer;
1016 PFNGLGETFRAMEBUFFERATTACHMENTPARAMETERIVPROC glGetFramebufferAttachmentParameteriv;
1017 PFNGLGENERATEMIPMAPPROC glGenerateMipmap;
1018 PFNGLBLITFRAMEBUFFERPROC glBlitFramebuffer;
1019 PFNGLRENDERBUFFERSTORAGEMULTISAMPLEPROC glRenderbufferStorageMultisample;
1020 PFNGLFRAMEBUFFERTEXTURELAYERPROC glFramebufferTextureLayer;
1021 PFNGLPOINTPARAMETERFPROC glPointParameterf;
1022#if VBOX_VMSVGA3D_GL_HACK_LEVEL < 0x102
1023 PFNGLBLENDCOLORPROC glBlendColor;
1024 PFNGLBLENDEQUATIONPROC glBlendEquation;
1025#endif
1026 PFNGLBLENDEQUATIONSEPARATEPROC glBlendEquationSeparate;
1027 PFNGLBLENDFUNCSEPARATEPROC glBlendFuncSeparate;
1028 PFNGLSTENCILOPSEPARATEPROC glStencilOpSeparate;
1029 PFNGLSTENCILFUNCSEPARATEPROC glStencilFuncSeparate;
1030 PFNGLBINDBUFFERPROC glBindBuffer;
1031 PFNGLDELETEBUFFERSPROC glDeleteBuffers;
1032 PFNGLGENBUFFERSPROC glGenBuffers;
1033 PFNGLBUFFERDATAPROC glBufferData;
1034 PFNGLMAPBUFFERPROC glMapBuffer;
1035 PFNGLUNMAPBUFFERPROC glUnmapBuffer;
1036 PFNGLENABLEVERTEXATTRIBARRAYPROC glEnableVertexAttribArray;
1037 PFNGLDISABLEVERTEXATTRIBARRAYPROC glDisableVertexAttribArray;
1038 PFNGLVERTEXATTRIBPOINTERPROC glVertexAttribPointer;
1039 PFNGLFOGCOORDPOINTERPROC glFogCoordPointer;
1040 PFNGLDRAWELEMENTSINSTANCEDBASEVERTEXPROC glDrawElementsInstancedBaseVertex;
1041 PFNGLDRAWELEMENTSBASEVERTEXPROC glDrawElementsBaseVertex;
1042 PFNGLACTIVETEXTUREPROC glActiveTexture;
1043#if VBOX_VMSVGA3D_GL_HACK_LEVEL < 0x103
1044 PFNGLCLIENTACTIVETEXTUREPROC glClientActiveTexture;
1045#endif
1046 PFNGLGETPROGRAMIVARBPROC glGetProgramivARB;
1047 PFNGLPROVOKINGVERTEXPROC glProvokingVertex;
1048 PFNGLGENQUERIESPROC glGenQueries;
1049 PFNGLDELETEQUERIESPROC glDeleteQueries;
1050 PFNGLBEGINQUERYPROC glBeginQuery;
1051 PFNGLENDQUERYPROC glEndQuery;
1052 PFNGLGETQUERYOBJECTUIVPROC glGetQueryObjectuiv;
1053 PFNGLTEXIMAGE3DPROC glTexImage3D;
1054 PFNGLTEXSUBIMAGE3DPROC glTexSubImage3D;
1055 PFNGLVERTEXATTRIBDIVISORPROC glVertexAttribDivisor;
1056 PFNGLDRAWARRAYSINSTANCEDPROC glDrawArraysInstanced;
1057 PFNGLDRAWELEMENTSINSTANCEDPROC glDrawElementsInstanced;
1058 PFNGLCOMPRESSEDTEXIMAGE2DPROC glCompressedTexImage2D;
1059 PFNGLCOMPRESSEDTEXIMAGE3DPROC glCompressedTexImage3D;
1060 PFNGLCOMPRESSEDTEXSUBIMAGE2DPROC glCompressedTexSubImage2D;
1061 PFNGLCOMPRESSEDTEXSUBIMAGE3DPROC glCompressedTexSubImage3D;
1062 PFNGLDRAWBUFFERSPROC glDrawBuffers;
1063 PFNGLCREATESHADERPROC glCreateShader;
1064 PFNGLSHADERSOURCEPROC glShaderSource;
1065 PFNGLCOMPILESHADERPROC glCompileShader;
1066 PFNGLGETSHADERIVPROC glGetShaderiv;
1067 PFNGLGETSHADERINFOLOGPROC glGetShaderInfoLog;
1068 PFNGLCREATEPROGRAMPROC glCreateProgram;
1069 PFNGLATTACHSHADERPROC glAttachShader;
1070 PFNGLLINKPROGRAMPROC glLinkProgram;
1071 PFNGLGETPROGRAMIVPROC glGetProgramiv;
1072 PFNGLGETPROGRAMINFOLOGPROC glGetProgramInfoLog;
1073 PFNGLUSEPROGRAMPROC glUseProgram;
1074 PFNGLGETUNIFORMLOCATIONPROC glGetUniformLocation;
1075 PFNGLUNIFORM1IPROC glUniform1i;
1076 PFNGLUNIFORM4FVPROC glUniform4fv;
1077 PFNGLDETACHSHADERPROC glDetachShader;
1078 PFNGLDELETESHADERPROC glDeleteShader;
1079 PFNGLDELETEPROGRAMPROC glDeleteProgram;
1080 } ext;
1081
1082 struct
1083 {
1084 bool fS3TCSupported : 1;
1085 bool fTextureFilterAnisotropicSupported : 1;
1086 GLint maxActiveLights;
1087 GLint maxTextures;
1088 GLint maxClipDistances;
1089 GLint maxColorAttachments;
1090 GLint maxRectangleTextureSize;
1091 GLint maxTextureAnisotropy;
1092 GLint maxVertexShaderInstructions;
1093 GLint maxFragmentShaderInstructions;
1094 GLint maxVertexShaderTemps;
1095 GLint maxFragmentShaderTemps;
1096 GLfloat flPointSize[2];
1097 SVGA3dPixelShaderVersion fragmentShaderVersion;
1098 SVGA3dVertexShaderVersion vertexShaderVersion;
1099 } caps;
1100
1101 /** The GL_EXTENSIONS value (space padded) for the default OpenGL profile.
1102 * Free with RTStrFree. */
1103 R3PTRTYPE(char *) pszExtensions;
1104
1105 /** The GL_EXTENSIONS value (space padded) for the other OpenGL profile.
1106 * Free with RTStrFree.
1107 *
1108 * This is used to detect shader model version since some implementations
1109 * (darwin) hides extensions that have made it into core and probably a
1110 * bunch of others when using a OpenGL core profile instead of a legacy one */
1111 R3PTRTYPE(char *) pszOtherExtensions;
1112 /** The version of the other GL profile. */
1113 float rsOtherGLVersion;
1114
1115 /** Shader talk back interface. */
1116 VBOXVMSVGASHADERIF ShaderIf;
1117
1118# ifdef VMSVGA3D_OPENGL
1119 /** The shared context. */
1120 VMSVGA3DCONTEXT SharedCtx;
1121
1122 /** Conversion of emulated formats. Resources are created on the SharedCtx. */
1123 PVMSVGA3DFORMATCONVERTER pConv;
1124# endif
1125#endif /* VMSVGA3D_OPENGL */
1126} VMSVGA3DSTATE;
1127
1128#ifdef VMSVGA3D_INCL_STRUCTURE_DESCRIPTORS
1129/**
1130 * SSM descriptor table for the VMSVGA3DSTATE structure.
1131 *
1132 * @remarks This isn't a complete structure markup, only fields with state.
1133 */
1134static SSMFIELD const g_aVMSVGA3DSTATEFields[] =
1135{
1136# ifdef VMSVGA3D_OPENGL
1137 SSMFIELD_ENTRY( VMSVGA3DSTATE, rsGLVersion), /** @todo Why are we saving the GL version?? */
1138# endif
1139 SSMFIELD_ENTRY( VMSVGA3DSTATE, cContexts),
1140 SSMFIELD_ENTRY( VMSVGA3DSTATE, cSurfaces),
1141 SSMFIELD_ENTRY_TERM()
1142};
1143#endif /* VMSVGA3D_INCL_STRUCTURE_DESCRIPTORS */
1144
1145
1146#ifdef VMSVGA3D_DIRECT3D
1147D3DFORMAT vmsvga3dSurfaceFormat2D3D(SVGA3dSurfaceFormat format);
1148D3DMULTISAMPLE_TYPE vmsvga3dMultipeSampleCount2D3D(uint32_t multisampleCount);
1149DECLCALLBACK(int) vmsvga3dSharedSurfaceDestroyTree(PAVLU32NODECORE pNode, void *pvParam);
1150int vmsvga3dSurfaceFlush(PVMSVGA3DSURFACE pSurface);
1151#endif /* VMSVGA3D_DIRECT3D */
1152
1153
1154#ifdef VMSVGA3D_OPENGL
1155/** Save and setup everything. */
1156# define VMSVGA3D_PARANOID_TEXTURE_PACKING
1157
1158/**
1159 * Saved texture packing parameters (shared by both pack and unpack).
1160 */
1161typedef struct VMSVGAPACKPARAMS
1162{
1163 GLint iAlignment;
1164 GLint cxRow;
1165# ifdef VMSVGA3D_PARANOID_TEXTURE_PACKING
1166 GLint cyImage;
1167 GLboolean fSwapBytes;
1168 GLboolean fLsbFirst;
1169 GLint cSkipRows;
1170 GLint cSkipPixels;
1171 GLint cSkipImages;
1172# endif
1173} VMSVGAPACKPARAMS;
1174/** Pointer to saved texture packing parameters. */
1175typedef VMSVGAPACKPARAMS *PVMSVGAPACKPARAMS;
1176/** Pointer to const saved texture packing parameters. */
1177typedef VMSVGAPACKPARAMS const *PCVMSVGAPACKPARAMS;
1178
1179void vmsvga3dOglSetPackParams(PVMSVGA3DSTATE pState, PVMSVGA3DCONTEXT pContext, PVMSVGA3DSURFACE pSurface,
1180 PVMSVGAPACKPARAMS pSave);
1181void vmsvga3dOglRestorePackParams(PVMSVGA3DSTATE pState, PVMSVGA3DCONTEXT pContext, PVMSVGA3DSURFACE pSurface,
1182 PCVMSVGAPACKPARAMS pSave);
1183void vmsvga3dOglSetUnpackParams(PVMSVGA3DSTATE pState, PVMSVGA3DCONTEXT pContext, PVMSVGA3DSURFACE pSurface,
1184 PVMSVGAPACKPARAMS pSave);
1185void vmsvga3dOglRestoreUnpackParams(PVMSVGA3DSTATE pState, PVMSVGA3DCONTEXT pContext, PVMSVGA3DSURFACE pSurface,
1186 PCVMSVGAPACKPARAMS pSave);
1187
1188/** @name VMSVGA3D_DEF_CTX_F_XXX - vmsvga3dContextDefineOgl flags.
1189 * @{ */
1190/** When clear, the context is created using the default OpenGL profile.
1191 * When set, it's created using the alternative profile. The latter is only
1192 * allowed if the VBOX_VMSVGA3D_DUAL_OPENGL_PROFILE is set. */
1193# define VMSVGA3D_DEF_CTX_F_OTHER_PROFILE RT_BIT_32(0)
1194/** Defining the shared context. */
1195# define VMSVGA3D_DEF_CTX_F_SHARED_CTX RT_BIT_32(1)
1196/** Defining the init time context (EMT). */
1197# define VMSVGA3D_DEF_CTX_F_INIT RT_BIT_32(2)
1198/** @} */
1199int vmsvga3dContextDefineOgl(PVGASTATE pThis, uint32_t cid, uint32_t fFlags);
1200void vmsvga3dSurfaceFormat2OGL(PVMSVGA3DSURFACE pSurface, SVGA3dSurfaceFormat format);
1201
1202#endif /* VMSVGA3D_OPENGL */
1203
1204
1205/* DevVGA-SVGA3d-shared.cpp: */
1206int vmsvga3dSaveShaderConst(PVMSVGA3DCONTEXT pContext, uint32_t reg, SVGA3dShaderType type, SVGA3dShaderConstType ctype,
1207 uint32_t val1, uint32_t val2, uint32_t val3, uint32_t val4);
1208
1209
1210
1211/* Command implementation workers. */
1212void vmsvga3dBackSurfaceDestroy(PVMSVGA3DSTATE pState, PVMSVGA3DSURFACE pSurface);
1213int vmsvga3dBackSurfaceStretchBlt(PVGASTATE pThis, PVMSVGA3DSTATE pState,
1214 PVMSVGA3DSURFACE pDstSurface, uint32_t uDstFace, uint32_t uDstMipmap, SVGA3dBox const *pDstBox,
1215 PVMSVGA3DSURFACE pSrcSurface, uint32_t uSrcFace, uint32_t uSrcMipmap, SVGA3dBox const *pSrcBox,
1216 SVGA3dStretchBltMode enmMode, PVMSVGA3DCONTEXT pContext);
1217int vmsvga3dBackSurfaceDMACopyBox(PVGASTATE pThis, PVMSVGA3DSTATE pState, PVMSVGA3DSURFACE pSurface,
1218 PVMSVGA3DMIPMAPLEVEL pMipLevel, uint32_t uHostFace, uint32_t uHostMipmap,
1219 SVGAGuestPtr GuestPtr, uint32_t cbGuestPitch, SVGA3dTransferType transfer,
1220 SVGA3dCopyBox const *pBox, PVMSVGA3DCONTEXT pContext, int rc, int iBox);
1221
1222int vmsvga3dBackCreateTexture(PVMSVGA3DSTATE pState, PVMSVGA3DCONTEXT pContext, uint32_t idAssociatedContext,
1223 PVMSVGA3DSURFACE pSurface);
1224
1225DECLINLINE(int) vmsvga3dContextFromCid(PVMSVGA3DSTATE pState, uint32_t cid, PVMSVGA3DCONTEXT *ppContext)
1226{
1227 /** @todo stricter checks for associated context */
1228 AssertReturn(cid < pState->cContexts, VERR_INVALID_PARAMETER);
1229 PVMSVGA3DCONTEXT const pContext = pState->papContexts[cid];
1230 if (RT_LIKELY(pContext && pContext->id == cid))
1231 {
1232 *ppContext = pContext;
1233 return VINF_SUCCESS;
1234 }
1235 LogRelMax(64, ("VMSVGA: unknown cid=%d (%s cid=%d)\n", cid, pContext ? "expected" : "null", pContext ? pContext->id : -1));
1236 return VERR_INVALID_PARAMETER;
1237}
1238
1239DECLINLINE(int) vmsvga3dSurfaceFromSid(PVMSVGA3DSTATE pState, uint32_t sid, PVMSVGA3DSURFACE *ppSurface)
1240{
1241 AssertReturn(sid < pState->cSurfaces, VERR_INVALID_PARAMETER);
1242 PVMSVGA3DSURFACE const pSurface = pState->papSurfaces[sid];
1243 if (RT_LIKELY(pSurface && pSurface->id == sid))
1244 {
1245 *ppSurface = pSurface;
1246 return VINF_SUCCESS;
1247 }
1248 LogRelMax(64, ("VMSVGA: unknown sid=%d (%s sid=%d)\n", sid, pSurface ? "expected" : "null", pSurface ? pSurface->id : -1));
1249 return VERR_INVALID_PARAMETER;
1250}
1251
1252DECLINLINE(int) vmsvga3dMipmapLevel(PVMSVGA3DSURFACE pSurface, uint32_t face, uint32_t mipmap,
1253 PVMSVGA3DMIPMAPLEVEL *ppMipmapLevel)
1254{
1255 /* Can use faces[0].numMipLevels, because numMipLevels is the same for all faces. */
1256 const uint32_t numMipLevels = pSurface->faces[0].numMipLevels;
1257
1258 AssertMsgReturn(face < pSurface->cFaces,
1259 ("cFaces %d, face %d\n", pSurface->cFaces, face),
1260 VERR_INVALID_PARAMETER);
1261 AssertMsgReturn(mipmap < numMipLevels,
1262 ("numMipLevels %d, mipmap %d", numMipLevels, mipmap),
1263 VERR_INVALID_PARAMETER);
1264
1265 *ppMipmapLevel = &pSurface->pMipmapLevels[face * numMipLevels + mipmap];
1266 return VINF_SUCCESS;
1267}
1268
1269#ifdef VMSVGA3D_DIRECT3D
1270DECLINLINE(D3DCUBEMAP_FACES) vmsvga3dCubemapFaceFromIndex(uint32_t iFace)
1271{
1272 D3DCUBEMAP_FACES Face;
1273 switch (iFace)
1274 {
1275 case 0: Face = D3DCUBEMAP_FACE_POSITIVE_X; break;
1276 case 1: Face = D3DCUBEMAP_FACE_NEGATIVE_X; break;
1277 case 2: Face = D3DCUBEMAP_FACE_POSITIVE_Y; break;
1278 case 3: Face = D3DCUBEMAP_FACE_NEGATIVE_Y; break;
1279 case 4: Face = D3DCUBEMAP_FACE_POSITIVE_Z; break;
1280 default:
1281 case 5: Face = D3DCUBEMAP_FACE_NEGATIVE_Z; break;
1282 }
1283 return Face;
1284}
1285#else /* VMSVGA3D_OPENGL */
1286DECLINLINE(GLenum) vmsvga3dCubemapFaceFromIndex(uint32_t iFace)
1287{
1288 GLint Face;
1289 switch (iFace)
1290 {
1291 case 0: Face = GL_TEXTURE_CUBE_MAP_POSITIVE_X; break;
1292 case 1: Face = GL_TEXTURE_CUBE_MAP_NEGATIVE_X; break;
1293 case 2: Face = GL_TEXTURE_CUBE_MAP_POSITIVE_Y; break;
1294 case 3: Face = GL_TEXTURE_CUBE_MAP_NEGATIVE_Y; break;
1295 case 4: Face = GL_TEXTURE_CUBE_MAP_POSITIVE_Z; break;
1296 default:
1297 case 5: Face = GL_TEXTURE_CUBE_MAP_NEGATIVE_Z; break;
1298 }
1299 return Face;
1300}
1301#endif
1302
1303int vmsvga3dOcclusionQueryCreate(PVMSVGA3DSTATE pState, PVMSVGA3DCONTEXT pContext);
1304int vmsvga3dOcclusionQueryDelete(PVMSVGA3DSTATE pState, PVMSVGA3DCONTEXT pContext);
1305int vmsvga3dOcclusionQueryBegin(PVMSVGA3DSTATE pState, PVMSVGA3DCONTEXT pContext);
1306int vmsvga3dOcclusionQueryEnd(PVMSVGA3DSTATE pState, PVMSVGA3DCONTEXT pContext);
1307int vmsvga3dOcclusionQueryGetData(PVMSVGA3DSTATE pState, PVMSVGA3DCONTEXT pContext, uint32_t *pu32Pixels);
1308
1309void vmsvga3dInfoSurfaceToBitmap(PCDBGFINFOHLP pHlp, PVMSVGA3DSURFACE pSurface,
1310 const char *pszPath, const char *pszNamePrefix, const char *pszNameSuffix);
1311
1312#ifdef VMSVGA3D_DIRECT3D
1313#define D3D_RELEASE(ptr) do { \
1314 if (ptr) \
1315 { \
1316 (ptr)->Release(); \
1317 (ptr) = 0; \
1318 } \
1319} while (0)
1320
1321HRESULT D3D9UpdateTexture(PVMSVGA3DCONTEXT pContext,
1322 PVMSVGA3DSURFACE pSurface);
1323HRESULT D3D9GetRenderTargetData(PVMSVGA3DCONTEXT pContext,
1324 PVMSVGA3DSURFACE pSurface,
1325 uint32_t uFace,
1326 uint32_t uMipmap);
1327HRESULT D3D9GetSurfaceLevel(PVMSVGA3DSURFACE pSurface,
1328 uint32_t uFace,
1329 uint32_t uMipmap,
1330 bool fBounce,
1331 IDirect3DSurface9 **ppD3DSurface);
1332D3DFORMAT D3D9GetActualFormat(PVMSVGA3DSTATE pState,
1333 D3DFORMAT d3dfmt);
1334bool D3D9CheckDeviceFormat(IDirect3D9 *pD3D9,
1335 DWORD Usage,
1336 D3DRESOURCETYPE RType,
1337 D3DFORMAT CheckFormat);
1338#endif
1339
1340#ifdef VMSVGA3D_OPENGL
1341void vmsvga3dOnSharedContextDefine(PVMSVGA3DSTATE pState);
1342void vmsvga3dOnSharedContextDestroy(PVMSVGA3DSTATE pState);
1343
1344DECLINLINE(GLuint) GLTextureId(PVMSVGA3DSURFACE pSurface)
1345{
1346 return pSurface->fEmulated ? pSurface->idEmulated : pSurface->oglId.texture;
1347}
1348
1349void FormatConvUpdateTexture(PVMSVGA3DSTATE pState,
1350 PVMSVGA3DCONTEXT pCurrentContext,
1351 PVMSVGA3DSURFACE pSurface,
1352 uint32_t iMipmap);
1353void FormatConvReadTexture(PVMSVGA3DSTATE pState,
1354 PVMSVGA3DCONTEXT pCurrentContext,
1355 PVMSVGA3DSURFACE pSurface,
1356 uint32_t iMipmap);
1357#endif
1358
1359#endif /* !VBOX_INCLUDED_SRC_Graphics_DevVGA_SVGA3d_internal_h */
1360
Note: See TracBrowser for help on using the repository browser.

© 2023 Oracle
ContactPrivacy policyTerms of Use